gdaui-data-cell-renderer-textual.c 21.6 KB
Newer Older
1
/*
2
 * Copyright (C) 2009 - 2012 Vivien Malerba <malerba@gnome-db.org>
3
 * Copyright (C) 2010 David King <davidk@openismus.com>
4
 * Copyright (C) 2011 Murray Cumming <murrayc@murrayc.com>
5
 * Copyright (C) 2018 Daniel Espinosa <esodan@gmail.com>
6 7
 *
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9 10 11 12 13 14
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the
19 20
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301, USA.
21 22 23 24 25 26 27 28 29 30 31 32
 */

#define OPTIMIZE 0

#include <glib/gi18n-lib.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <libgda/libgda.h>
#include "gdaui-data-cell-renderer-textual.h"
#include "gdaui-data-entry.h"
#include "gdaui-entry-string.h"
33
#include "gdaui-entry-number.h"
34 35 36 37 38
#include "gdaui-entry-date.h"
#include "gdaui-entry-time.h"
#include "gdaui-entry-timestamp.h"
#include <libgda/gda-enum-types.h>
#include "marshallers/gdaui-custom-marshal.h"
39
#include "gdaui-data-cell-renderer-util.h"
40

41
#define MAX_ACCEPTED_STRING_LENGTH 800U
42 43 44 45 46

static void gdaui_data_cell_renderer_textual_dispose    (GObject *object);
static void gdaui_data_cell_renderer_textual_finalize   (GObject *object);

static void gdaui_data_cell_renderer_textual_get_property  (GObject *object,
47 48 49
							    guint param_id,
							    GValue *value,
							    GParamSpec *pspec);
50
static void gdaui_data_cell_renderer_textual_set_property  (GObject *object,
51 52 53
							    guint param_id,
							    const GValue *value,
							    GParamSpec *pspec);
54
static void gdaui_data_cell_renderer_textual_get_size   (GtkCellRenderer          *cell,
55
							 GtkWidget                *widget,
56
							 const GdkRectangle       *cell_area,
57 58 59 60
							 gint                     *x_offset,
							 gint                     *y_offset,
							 gint                     *width,
							 gint                     *height);
61
static void gdaui_data_cell_renderer_textual_render     (GtkCellRenderer          *cell,
62
							 cairo_t                  *cr,
63
							 GtkWidget                *widget,
64 65
							 const GdkRectangle       *background_area,
							 const GdkRectangle       *cell_area,
66
							 GtkCellRendererState      flags);
67 68

static GtkCellEditable *gdaui_data_cell_renderer_textual_start_editing (GtkCellRenderer      *cell,
69 70 71
									GdkEvent             *event,
									GtkWidget            *widget,
									const gchar          *path,
72 73
									const GdkRectangle   *background_area,
									const GdkRectangle   *cell_area,
74
									GtkCellRendererState  flags);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

enum {
	CHANGED,
	LAST_SIGNAL
};

enum {
	PROP_0,
	PROP_VALUE,
	PROP_VALUE_ATTRIBUTES,
	PROP_TO_BE_DELETED,
	PROP_DATA_HANDLER,
	PROP_TYPE,
	PROP_OPTIONS
};

91
typedef struct
92 93 94 95 96 97
{
	GdaDataHandler *dh;
	GType           type;
	gboolean        type_forced; /* TRUE if ->type has been forced by a value and changed from what was specified */
	GValue         *value;
	gboolean        to_be_deleted;
98
	gboolean        invalid;
99 100 101 102 103 104

	gchar          *options;
	gchar          *currency;
	gint            max_length;
	gint            n_decimals;
	guchar          thousands_sep;
105

106 107 108
} GdauiDataCellRendererTextualPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (GdauiDataCellRendererTextual, gdaui_data_cell_renderer_textual, GTK_TYPE_CELL_RENDERER_TEXT)
109

110
typedef struct
111 112 113 114
{
	/* text renderer */
	gulong focus_out_id;
} GdauiDataCellRendererTextualInfo;
Vivien Malerba's avatar
Vivien Malerba committed
115
#define GDAUI_DATA_CELL_RENDERER_TEXTUAL_INFO_KEY "__info_key"
116 117 118

static guint text_cell_renderer_textual_signals [LAST_SIGNAL];

Vivien Malerba's avatar
Vivien Malerba committed
119
#define GDAUI_DATA_CELL_RENDERER_TEXTUAL_PATH "__path"
120 121 122 123

static void
gdaui_data_cell_renderer_textual_init (GdauiDataCellRendererTextual *datacell)
{
124 125 126 127 128 129
	GdauiDataCellRendererTextualPrivate *priv = gdaui_data_cell_renderer_textual_get_instance_private (datacell);
	priv->dh = NULL;
	priv->type = GDA_TYPE_NULL;
	priv->type_forced = FALSE;
	priv->value = NULL;
	priv->options = NULL;
130

131
	priv->n_decimals = -1; /* unlimited */
132

133 134
	g_object_set ((GObject*) datacell, "xalign", 0.0, "yalign", 0.5,
		      "xpad", 2, "ypad", 2, NULL);
135 136 137 138 139 140 141 142
}

static void
gdaui_data_cell_renderer_textual_class_init (GdauiDataCellRendererTextualClass *class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (class);
	GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);

143
	object_class->dispose = gdaui_data_cell_renderer_textual_dispose;
144 145 146 147 148 149 150 151
	object_class->finalize = gdaui_data_cell_renderer_textual_finalize;

	object_class->get_property = gdaui_data_cell_renderer_textual_get_property;
	object_class->set_property = gdaui_data_cell_renderer_textual_set_property;

	cell_class->get_size = gdaui_data_cell_renderer_textual_get_size;
	cell_class->render = gdaui_data_cell_renderer_textual_render;
	cell_class->start_editing = gdaui_data_cell_renderer_textual_start_editing;
152

153 154 155 156 157 158
	g_object_class_install_property (object_class,
					 PROP_VALUE,
					 g_param_spec_pointer ("value",
							       _("Value"),
							       _("GValue to render"),
							       G_PARAM_WRITABLE));
159

160 161
	g_object_class_install_property (object_class,
					 PROP_VALUE_ATTRIBUTES,
Vivien Malerba's avatar
Vivien Malerba committed
162
					 g_param_spec_flags ("value-attributes", NULL, NULL, GDA_TYPE_VALUE_ATTRIBUTE,
163
							     GDA_VALUE_ATTR_NONE, G_PARAM_READWRITE));
164 165 166

	g_object_class_install_property (object_class,
					 PROP_TO_BE_DELETED,
Vivien Malerba's avatar
Vivien Malerba committed
167
					 g_param_spec_boolean ("to-be-deleted", NULL, NULL, FALSE,
168 169 170
                                                               G_PARAM_WRITABLE));
	g_object_class_install_property(object_class,
					PROP_DATA_HANDLER,
Vivien Malerba's avatar
Vivien Malerba committed
171
					g_param_spec_object("data-handler", NULL, NULL, GDA_TYPE_DATA_HANDLER,
172 173 174 175 176 177 178 179 180 181
							    G_PARAM_WRITABLE|G_PARAM_CONSTRUCT_ONLY));
	g_object_class_install_property(object_class,
					PROP_TYPE,
					g_param_spec_gtype("type", NULL, NULL, G_TYPE_NONE,
							   G_PARAM_WRITABLE|G_PARAM_CONSTRUCT_ONLY));

	g_object_class_install_property(object_class,
	                                PROP_OPTIONS,
	                                g_param_spec_string("options", NULL, NULL, NULL,
	                                                    G_PARAM_WRITABLE));
182

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	text_cell_renderer_textual_signals [CHANGED] =
		g_signal_new ("changed",
			      G_OBJECT_CLASS_TYPE (object_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (GdauiDataCellRendererTextualClass, changed),
			      NULL, NULL,
			      _gdaui_marshal_VOID__STRING_VALUE,
			      G_TYPE_NONE, 2,
			      G_TYPE_STRING,
			      G_TYPE_VALUE);

}

static void
gdaui_data_cell_renderer_textual_dispose (GObject *object)
{
	GdauiDataCellRendererTextual *datacell = GDAUI_DATA_CELL_RENDERER_TEXTUAL (object);
200
	GdauiDataCellRendererTextualPrivate *priv = gdaui_data_cell_renderer_textual_get_instance_private (datacell);
201

202 203 204
	if (priv->dh) {
		g_object_unref (G_OBJECT (priv->dh));
		priv->dh = NULL;
205 206 207
	}

	/* parent class */
208
	G_OBJECT_CLASS (gdaui_data_cell_renderer_textual_parent_class)->dispose (object);
209 210 211 212 213 214
}

static void
gdaui_data_cell_renderer_textual_finalize (GObject *object)
{
	GdauiDataCellRendererTextual *datacell = GDAUI_DATA_CELL_RENDERER_TEXTUAL (object);
215
	GdauiDataCellRendererTextualPrivate *priv = gdaui_data_cell_renderer_textual_get_instance_private (datacell);
216

217
	g_free (priv->options);
218 219

	/* parent class */
220
	G_OBJECT_CLASS (gdaui_data_cell_renderer_textual_parent_class)->finalize (object);
221 222 223 224 225
}


static void
gdaui_data_cell_renderer_textual_get_property (GObject *object,
226
					       guint param_id,
227
					       G_GNUC_UNUSED GValue *value,
228
					       GParamSpec *pspec)
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
{
	switch (param_id) {
	case PROP_VALUE_ATTRIBUTES:
		/* nothing to do */
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	}
}

static gboolean
is_numerical (GType type, gboolean *is_int)
{
	if ((type == G_TYPE_INT64) ||
	    (type == G_TYPE_UINT64) ||
	    (type == G_TYPE_LONG) ||
	    (type == G_TYPE_ULONG) ||
	    (type == G_TYPE_INT) ||
	    (type == G_TYPE_UINT) ||
	    (type == G_TYPE_CHAR) ||
	    (type == G_TYPE_UCHAR) ||
	    (type == GDA_TYPE_SHORT) ||
	    (type == GDA_TYPE_USHORT)) {
		*is_int = TRUE;
		return TRUE;
	}
	else if ((type == G_TYPE_FLOAT) ||
		 (type == G_TYPE_DOUBLE) ||
		 (type == GDA_TYPE_NUMERIC)) {
		*is_int = FALSE;
		return TRUE;
	}
	else {
		*is_int = FALSE;
		return FALSE;
	}
}

/*
 * Add thousands separator and number of decimals if necessary
 * always return FALSE because it's a timeout call and we want to remove it.
 */
static gchar *
adjust_numeric_display (const gchar *number_text, gboolean is_int, gint n_decimals, guchar decimal_sep, guchar thousands_sep)
{
	gchar *new_text;
	gchar *ptr;
	gint number_length;
	gint i, index;

	number_length = strlen (number_text);

	/* make a copy of current number representation in a tmp buffer */
283
	new_text = g_new (gchar,
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
			  number_length * 2 + ((n_decimals >= 0) ? n_decimals : 0) + 1);
	memcpy (new_text, number_text, number_length + 1);

	/* try to set ptr to the 1st decimal separator */
	for (ptr = new_text, i = 0; *ptr && (*ptr != decimal_sep); ptr++, i++);

	/* handle decimals if necessary */
	if ((n_decimals >= 0) && !is_int) {
		if ((n_decimals == 0) && (*ptr == decimal_sep)) {
			*ptr = 0;
			number_length = i;
		}
		else if (n_decimals > 0) {
			gint n = 0;
			if (*ptr != decimal_sep) {
				g_assert (*ptr == 0);
				*ptr = decimal_sep;
				ptr++;
			}
			else {
				for (ptr++; *ptr && (n < n_decimals); n++, ptr++)
					g_assert (isdigit (*ptr));
306

307 308 309 310
				if (*ptr)
					*ptr = 0;
			}

311
			for (; n < n_decimals; n++, ptr++)
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
				*ptr = '0';
			*ptr = 0;
		}
		number_length = strlen (new_text);
	}

	/* add thousands separator if necessary */
	if (thousands_sep) {
		index = i;
		for (i--; i > 0; i--) {
			if (isdigit (new_text [i-1]) && (index - i) % 3 == 0) {
				memmove (new_text + i + 1, new_text + i, number_length - i + 1);
				number_length ++;
				new_text [i] = thousands_sep;
			}
		}
	}
329

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
	return new_text;
}

static guchar
get_default_decimal_sep ()
{
        static guchar value = 0;

        if (value == 0) {
                gchar text[20];
                sprintf (text, "%f", 1.23);
                value = text[1];
        }
        return value;
}

static guchar
get_default_thousands_sep ()
{
        static guchar value = 255;

        if (value == 255) {
                gchar text[20];
                sprintf (text, "%f", 1234.);
                if (text[1] == '2')
                        value = 0;
                else
                        value = text[1];
        }
        return value;
}

static void
gdaui_data_cell_renderer_textual_set_property (GObject *object,
364 365 366
					       guint param_id,
					       const GValue *value,
					       GParamSpec *pspec)
367 368 369 370 371
{
	GdauiDataCellRendererTextual *datacell = GDAUI_DATA_CELL_RENDERER_TEXTUAL (object);
	gfloat xalign = 0.;
	const gchar* options;
	static gchar *too_long_msg = NULL;
372
	static gint too_long_msg_len;
373
	GdauiDataCellRendererTextualPrivate *priv = gdaui_data_cell_renderer_textual_get_instance_private (datacell);
374 375

	if (!too_long_msg) {
376 377
		too_long_msg = g_strconcat ("<b><i>&lt;", _("string truncated because too long"),
					    "&gt;</i></b>", NULL);
378 379 380 381 382 383 384 385
		too_long_msg_len = strlen (too_long_msg);
	}

	switch (param_id) {
	case PROP_VALUE:
		/* FIXME: is it necessary to make a copy of value, couldn't we just use the
		 * value AS IS for performances reasons ? */
		if (! OPTIMIZE) {
386 387 388
			if (priv->value) {
				gda_value_free (priv->value);
				priv->value = NULL;
389 390 391 392 393 394 395 396 397
			}
		}

		if (value) {
			GValue *gval = g_value_get_pointer (value);
			if (gval && !gda_value_is_null (gval)) {
				gchar *str;
				gboolean is_int, is_num;

398 399 400 401
				if (G_VALUE_TYPE (gval) != priv->type) {
					if (!priv->type_forced) {
						priv->type_forced = TRUE;
						if (priv->type != GDA_TYPE_NULL)
402 403 404
							g_warning (_("Data cell renderer's specified type (%s) "
								     "differs from actual "
								     "value to display type (%s)"),
405
								   g_type_name (priv->type),
406
								   g_type_name (G_VALUE_TYPE (gval)));
407 408 409 410 411
					}
					else
						g_warning (_("Data cell renderer asked to display values of different "
							     "data types, at least %s and %s, which means the data model has "
							     "some incoherencies"),
412
							   g_type_name (priv->type),
413
							   g_type_name (G_VALUE_TYPE (gval)));
414
					priv->type = G_VALUE_TYPE (gval);
415
				}
416
				is_num = is_numerical (priv->type, &is_int);
417 418 419 420
				if (is_num)
					xalign = 1.;

				if (! OPTIMIZE)
421
					priv->value = gda_value_copy (gval);
422

423 424
				if (!priv->dh && (priv->type != GDA_TYPE_NULL))
					priv->dh = g_object_ref (gda_data_handler_get_default (priv->type));
425

426 427
				if (priv->dh) {
					str = gda_data_handler_get_str_from_value (priv->dh, gval);
428 429
					gboolean use_markup = FALSE;
					if (str) {
Murray Cumming's avatar
Murray Cumming committed
430
						guint length;
431
						length = g_utf8_strlen (str, -1);
432 433
						if (length > MAX_ACCEPTED_STRING_LENGTH + too_long_msg_len) {
							gchar *tmp;
434 435 436 437
							tmp = g_utf8_offset_to_pointer (str,
											MAX_ACCEPTED_STRING_LENGTH);
							*tmp = 0;
							tmp = g_markup_escape_text (str, -1);
438 439
							g_free (str);
							str = g_strconcat (tmp, too_long_msg, NULL);
440
							g_free (tmp);
441 442
							use_markup = TRUE;
						}
443
					}
444

445
					if (priv->options) {
446 447
						/* extra specific treatments to handle the options */
						gchar *tmp_str;
448

449 450
						if (!is_num && (priv->max_length > 0))
							str [priv->max_length] = 0;
451
						else if (is_num) {
452
							tmp_str = adjust_numeric_display (str, is_int,
453
											  priv->n_decimals,
454
											  get_default_decimal_sep (),
455
											  priv->thousands_sep);
456 457 458
							g_free (str);
							str = tmp_str;
						}
459 460
						if (priv->currency) {
							tmp_str = g_strdup_printf ("%s %s", str, priv->currency);
461 462 463 464 465
							g_free (str);
							str = tmp_str;
						}
					}

466 467 468 469 470 471
					if (use_markup)
						g_object_set (G_OBJECT (object), "markup",
							      str, "xalign", xalign, NULL);
					else
						g_object_set (G_OBJECT (object), "text",
							      str, "xalign", xalign, NULL);
472 473 474
					g_free (str);
				}
				else
475
					g_object_set (G_OBJECT (object), "text", _("<non-printable>"),
476 477
						      "xalign", xalign, NULL);
			}
478
			else if (gval)
479
				g_object_set (G_OBJECT (object), "text", "", NULL);
480
			else {
481
				priv->invalid = TRUE;
482
				g_object_set (G_OBJECT (object), "text", "", NULL);
483
			}
484
		}
485
		else {
486
			priv->invalid = TRUE;
487
			g_object_set (G_OBJECT (object), "text", "", NULL);
488
		}
489 490
		break;
	case PROP_VALUE_ATTRIBUTES:
491
		priv->invalid = g_value_get_flags (value) & GDA_VALUE_ATTR_DATA_NON_VALID ? TRUE : FALSE;
492 493
		break;
	case PROP_TO_BE_DELETED:
494
		priv->to_be_deleted = g_value_get_boolean (value);
495 496
		break;
	case PROP_DATA_HANDLER:
497 498 499 500 501
		if (priv->dh)
			g_object_unref (G_OBJECT (priv->dh));
		priv->dh = GDA_DATA_HANDLER (g_value_get_object (value));
		if (priv->dh)
			g_object_ref (G_OBJECT (priv->dh));
502 503
		break;
	case PROP_TYPE:
504
		priv->type = g_value_get_gtype (value);
505 506 507 508 509 510 511
		break;
	case PROP_OPTIONS:
		options = g_value_get_string(value);
		if (options) {
			GdaQuarkList *params;
			const gchar *str;

512
			priv->options = g_strdup (options);
513 514 515 516

			params = gda_quark_list_new_from_string (options);
			str = gda_quark_list_find (params, "MAX_SIZE");
			if (str)
517
				priv->max_length = atoi (str);
518 519 520
			str = gda_quark_list_find (params, "THOUSAND_SEP");
			if (str) {
				if ((*str == 't') || (*str == 'T'))
521
					priv->thousands_sep = get_default_thousands_sep ();
522
				else
523
					priv->thousands_sep = 0;
524 525 526
			}
			str = gda_quark_list_find (params, "NB_DECIMALS");
			if (str)
527
				priv->n_decimals = atoi (str);
528
			str = gda_quark_list_find (params, "CURRENCY");
529
			if (str)
530
				priv->currency = g_strdup_printf ("%s ", str);
531 532 533 534 535 536 537 538 539 540 541
			gda_quark_list_free (params);
		}

		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	}
}

/**
542
 * gdaui_data_cell_renderer_textual_new:
543
 * @dh: (nullable): a #GdaDataHandler object, or %NULL
544 545
 * @type: the #GType being edited
 * @options: options as a string
546
 *
547 548 549 550 551 552 553
 * Creates a new #GdauiDataCellRendererTextual. Adjust how text is drawn using
 * object properties. Object properties can be
 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
 * you can bind a property to a value in a #GtkTreeModel. For example,
 * you can bind the "text" property on the cell renderer to a string
 * value in the model, thus rendering a different string in each row
 * of the #GtkTreeView
554
 *
Vivien Malerba's avatar
Vivien Malerba committed
555
 * Returns: (transfer full): the new cell renderer
556 557 558 559 560 561 562 563
 **/
GtkCellRenderer *
gdaui_data_cell_renderer_textual_new (GdaDataHandler *dh, GType type, const gchar *options)
{
	GObject *obj;

	g_return_val_if_fail (!dh || GDA_IS_DATA_HANDLER (dh), NULL);
	obj = g_object_new (GDAUI_TYPE_DATA_CELL_RENDERER_TEXTUAL,
Vivien Malerba's avatar
Vivien Malerba committed
564
			    "type", type, "data-handler", dh,
565 566 567 568 569 570 571 572
	                    "options", options, NULL);

	return GTK_CELL_RENDERER (obj);
}


static void
gdaui_data_cell_renderer_textual_get_size (GtkCellRenderer *cell,
573
					   GtkWidget       *widget,
574
					   const GdkRectangle *cell_area,
575 576 577 578
					   gint            *x_offset,
					   gint            *y_offset,
					   gint            *width,
					   gint            *height)
579 580 581 582 583 584 585
{
	GtkCellRendererClass *text_class = g_type_class_peek (GTK_TYPE_CELL_RENDERER_TEXT);
	(text_class->get_size) (cell, widget, cell_area, x_offset, y_offset, width, height);
}

static void
gdaui_data_cell_renderer_textual_render (GtkCellRenderer      *cell,
586
					 cairo_t              *cr,
587
					 GtkWidget            *widget,
588 589
					 const GdkRectangle   *background_area,
					 const GdkRectangle   *cell_area,
590 591
					 GtkCellRendererState  flags)

592
{
593
	GdauiDataCellRendererTextual *datacell = (GdauiDataCellRendererTextual*) cell;
594
	GtkCellRendererClass *text_class = g_type_class_peek (GTK_TYPE_CELL_RENDERER_TEXT);
595
	(text_class->render) (cell, cr, widget, background_area, cell_area, flags);
596
	GdauiDataCellRendererTextualPrivate *priv = gdaui_data_cell_renderer_textual_get_instance_private (datacell);
597

598
	if (priv->to_be_deleted) {
599 600 601 602
		cairo_set_source_rgba (cr, 0., 0., 0., 1.);
		cairo_rectangle (cr, cell_area->x, cell_area->y + cell_area->height / 2. - .5,
				 cell_area->width, 1.);
		cairo_fill (cr);
603
	}
604
	if (priv->invalid)
605
		gdaui_data_cell_renderer_draw_invalid_area (cr, cell_area);
606 607 608 609
}

static void
gdaui_data_cell_renderer_textual_editing_done (GtkCellEditable *entry,
610
					       gpointer         data)
611 612 613 614 615 616 617 618 619 620 621 622 623
{
	const gchar *path;
	GdauiDataCellRendererTextualInfo *info;
	GValue *value;

	info = g_object_get_data (G_OBJECT (data),
				  GDAUI_DATA_CELL_RENDERER_TEXTUAL_INFO_KEY);

	if (info->focus_out_id > 0) {
		g_signal_handler_disconnect (entry, info->focus_out_id);
		info->focus_out_id = 0;
	}

624 625
	if (g_object_class_find_property (G_OBJECT_GET_CLASS (entry), "editing-canceled")) {
		gboolean editing_canceled;
626

627 628
		g_object_get (G_OBJECT (entry), "editing-canceled", &editing_canceled, NULL);
		if (editing_canceled)
629 630 631 632
			return;
	}

	path = g_object_get_data (G_OBJECT (entry), GDAUI_DATA_CELL_RENDERER_TEXTUAL_PATH);
633

634 635 636 637 638 639 640
	value = gdaui_data_entry_get_value (GDAUI_DATA_ENTRY (entry));
	g_signal_emit (data, text_cell_renderer_textual_signals[CHANGED], 0, path, value);
	gda_value_free (value);
}

static gboolean
gdaui_data_cell_renderer_textual_focus_out_event (GtkWidget *entry,
641
						  G_GNUC_UNUSED GdkEvent  *event,
642
						  gpointer   data)
643 644 645 646 647 648 649 650 651
{
	gdaui_data_cell_renderer_textual_editing_done (GTK_CELL_EDITABLE (entry), data);

	/* entry needs focus-out-event */
	return FALSE;
}

static GtkCellEditable *
gdaui_data_cell_renderer_textual_start_editing (GtkCellRenderer      *cell,
652 653
						G_GNUC_UNUSED GdkEvent             *event,
						G_GNUC_UNUSED GtkWidget            *widget,
654
						const gchar          *path,
655 656
						G_GNUC_UNUSED const GdkRectangle *background_area,
						G_GNUC_UNUSED const GdkRectangle *cell_area,
657
						G_GNUC_UNUSED GtkCellRendererState  flags)
658 659 660 661 662
{
	GdauiDataCellRendererTextual *datacell;
	GtkWidget *entry;
	GdauiDataCellRendererTextualInfo *info;
	gboolean editable;
663

664
	datacell = GDAUI_DATA_CELL_RENDERER_TEXTUAL (cell);
665
	GdauiDataCellRendererTextualPrivate *priv = gdaui_data_cell_renderer_textual_get_instance_private (datacell);
666 667 668 669 670

	/* If the cell isn't editable we return NULL. */
	g_object_get (G_OBJECT (cell), "editable", &editable, NULL);
	if (!editable)
		return NULL;
671

672
	/* If there is no data handler then the cell also is not editable */
673
	if (!priv->dh)
674 675
		return NULL;

676 677 678 679 680 681 682 683
	if (priv->type == G_TYPE_DATE)
		entry = gdaui_entry_date_new (priv->dh);
	else if (priv->type == GDA_TYPE_TIME)
		entry = gdaui_entry_time_new (priv->dh);
	else if (priv->type == G_TYPE_DATE_TIME)
		entry = gdaui_entry_timestamp_new (priv->dh);
	else if (gdaui_entry_number_is_type_numeric (priv->type))
		entry = gdaui_entry_number_new (priv->dh, priv->type, priv->options);
684
	else
685
		entry = gdaui_entry_string_new (priv->dh, priv->type, priv->options);
686

687
	 g_object_set (G_OBJECT (entry), "is-cell-renderer", TRUE, NULL);
688

689
	if (OPTIMIZE) {
690 691 692 693
		GValue *orig;
		gchar *text;

		g_object_get (G_OBJECT (cell), "text", &text, NULL);
694
		orig = gda_data_handler_get_value_from_str (priv->dh, text, priv->type);
695
		g_free (text);
696
		gdaui_data_entry_set_reference_value (GDAUI_DATA_ENTRY (entry), orig);
697 698
	}
	else
699
		gdaui_data_entry_set_reference_value (GDAUI_DATA_ENTRY (entry), priv->value);
700

701
	info = g_new0 (GdauiDataCellRendererTextualInfo, 1);
702
 	g_object_set_data_full (G_OBJECT (entry), GDAUI_DATA_CELL_RENDERER_TEXTUAL_PATH, g_strdup (path), g_free);
703
	g_object_set_data_full (G_OBJECT (cell), GDAUI_DATA_CELL_RENDERER_TEXTUAL_INFO_KEY, info, g_free);
704

Vivien Malerba's avatar
Vivien Malerba committed
705
	g_signal_connect (entry, "editing-done",
706
			  G_CALLBACK (gdaui_data_cell_renderer_textual_editing_done), datacell);
Vivien Malerba's avatar
Vivien Malerba committed
707
	info->focus_out_id = g_signal_connect (entry, "focus-out-event",
708 709 710 711 712
					       G_CALLBACK (gdaui_data_cell_renderer_textual_focus_out_event),
					       datacell);
	gtk_widget_show (entry);
	return GTK_CELL_EDITABLE (entry);
}