gnumeric-expr-entry.c 73.7 KB
Newer Older
1 2
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

3 4
/*
 * gnumeric-expr-entry.c: An entry widget specialized to handle expressions
5
 * and ranges.
6 7 8 9 10
 *
 * Author:
 *   Jon Kre Hellan (hellan@acm.org)
 */

11
#include <gnumeric-config.h>
12
#include <glib/gi18n-lib.h>
13
#include <gnumeric.h>
14
#include "gnumeric-expr-entry.h"
15

16
#include <wbc-gtk-impl.h>
17
#include <sheet-control-gui-priv.h>
18
#include <gnm-pane.h>
19 20 21 22 23 24
#include <sheet-merge.h>
#include <parse-util.h>
#include <gui-util.h>
#include <ranges.h>
#include <value.h>
#include <expr.h>
25
#include <func.h>
26
#include <dependent.h>
27
#include <sheet.h>
28
#include <sheet-style.h>
29
#include <workbook.h>
30
#include <sheet-view.h>
31
#include <selection.h>
32
#include <commands.h>
33
#include <gnm-format.h>
34
#include <number-match.h>
35
#include <gnm-datetime.h>
36
#include <gnumeric-conf.h>
37
#include <dead-kittens.h>
38
#include <dialogs/dialogs.h>
39
#include <goffice/goffice.h>
Jody Goldberg's avatar
Jody Goldberg committed
40

Jody Goldberg's avatar
Jody Goldberg committed
41
#include <gsf/gsf-impl-utils.h>
42
#include <gtk/gtk.h>
43
#include <gdk/gdkkeysyms.h>
Jody Goldberg's avatar
Jody Goldberg committed
44
#include <string.h>
45

46 47
#define UNICODE_LEFT_TRIANGLE "\xe2\x97\x80"
#define UNICODE_RIGHT_TRIANGLE "\xe2\x96\xb6"
Morten Welinder's avatar
Morten Welinder committed
48 49
#define UNICODE_CROSS_AND_SKULLBONES "\xe2\x98\xa0"
#define UNICODE_ELLIPSIS "\xe2\x80\xa6"
50
#define UNICODE_ELLIPSIS_VERT "\xe2\x8b\xae"
51
#define UNICODE_ARROW_UP "\xe2\x87\xa7"
Morten Welinder's avatar
Morten Welinder committed
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#warning We should replace these token names with the correct values
   enum yytokentype {
     STRING = 258,
     QUOTED_STRING = 259,
     CONSTANT = 260,
     RANGEREF = 261,
     tok_GTE = 262,
     tok_LTE = 263,
     tok_NE = 264,
     tok_AND = 265,
     tok_OR = 266,
     tok_NOT = 267,
     INTERSECT = 268,
     ARG_SEP = 269,
     ARRAY_COL_SEP = 270,
     ARRAY_ROW_SEP = 271,
     SHEET_SEP = 272,
     INVALID_TOKEN = 273,
     tok_RIGHT_EXP = 274,
     tok_LEFT_EXP = 275,
     tok_PLUS = 276,
     tok_NEG = 277,
     RANGE_INTERSECT = 278,
     RANGE_SEP = 279
   };
#define  TOKEN_UNMATCHED_APOSTROPHY INVALID_TOKEN
#define  TOKEN_PARENTHESIS_OPEN 40
#define  TOKEN_PARENTHESIS_CLOSED 41
#define  TOKEN_BRACE_OPEN 123
#define  TOKEN_BRACE_CLOSED 125

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
GType
gnm_update_type_get_type (void)
{
  static GType etype = 0;
  if (etype == 0) {
    static const GEnumValue values[] = {
      { GNM_UPDATE_CONTINUOUS, "GNM_UPDATE_CONTINUOUS", "continuous" },
      { GNM_UPDATE_DISCONTINUOUS, "GNM_UPDATE_DISCONTINUOUS", "discontinuous" },
      { GNM_UPDATE_DELAYED, "GNM_UPDATE_DELAYED", "delayed" },
      { 0, NULL, NULL }
    };
    etype = g_enum_register_static (g_intern_static_string ("GnmUpdateType"), values);
  }
  return etype;
}
99

100
typedef struct {
Jody Goldberg's avatar
Jody Goldberg committed
101
	GnmRangeRef ref;
102 103 104
	int	    text_start;
	int	    text_end;
	gboolean    is_valid;
105 106
} Rangesel;

107
struct _GnmExprEntry {
108
	GtkBox	parent;
109 110

	GtkEntry		*entry;
111
	GtkWidget               *calendar_combo;
112
	gulong                   calendar_combo_changed;
113
	GtkWidget		*icon;
114 115
	SheetControlGUI		*scg;	/* the source of the edit */
	Sheet			*sheet;	/* from scg */
116
	GnmParsePos		 pp;	/* from scg->sv */
117
	WBCGtk			*wbcg;	/* from scg */
118 119
	Rangesel		 rangesel;

120
	GnmExprEntryFlags	 flags;
121 122
	int			 freeze_count;

123
	GnmUpdateType		 update_policy;
124
	guint			 update_timeout_id;
125

126 127 128
	gboolean                 is_cell_renderer;  /* as cell_editable */
	gboolean                 editing_canceled;  /* as cell_editable */
	gboolean                 ignore_changes; /* internal mutex */
129

130 131 132
	gboolean                 feedback_disabled;
	GnmLexerItem            *lexer_items;
	GnmExprTop const        *texpr;
133 134 135
	struct {
		GtkWidget       *tooltip;
		GnmFunc         *fd;
136
		gint             args;
137
		gboolean         had_stuff;
138
		gulong           handlerid;
139
		gboolean         enabled;
140
		gboolean         is_expr;
141
		gboolean         completion_se_valid;
142 143 144
		gchar           *completion;
		guint            completion_start;
		guint            completion_end;
145
	}                        tooltip;
146 147

	GOFormat const *constant_format;
148 149
};

150
typedef struct _GnmExprEntryClass {
151
	GtkBoxClass base;
152

Jody Goldberg's avatar
Jody Goldberg committed
153 154 155
	void (* update)   (GnmExprEntry *gee, gboolean user_requested_update);
	void (* changed)  (GnmExprEntry *gee);
	void (* activate) (GnmExprEntry *gee);
156
} GnmExprEntryClass;
157

158 159
/* Signals */
enum {
160 161
	UPDATE,
	CHANGED,
162
	ACTIVATE,
163 164 165
	LAST_SIGNAL
};

166 167 168
/* Properties */
enum {
	PROP_0,
169 170
	PROP_UPDATE_POLICY,
	PROP_WITH_ICON,
171
	PROP_TEXT,
172
	PROP_FLAGS,
Jody Goldberg's avatar
Jody Goldberg committed
173
	PROP_SCG,
174
	PROP_WBCG,
175 176
	PROP_CONSTANT_FORMAT,
	PROP_EDITING_CANCELED
177 178
};

179 180 181 182
static guint signals[LAST_SIGNAL] = { 0 };

static void gee_set_value_double (GogDataEditor *editor, double val,
				  GODateConventions const *date_conv);
183

184 185 186 187 188 189
/* Internal routines
 */
static void     gee_rangesel_reset (GnmExprEntry *gee);
static void     gee_rangesel_update_text (GnmExprEntry *gee);
static void     gee_detach_scg (GnmExprEntry *gee);
static void     gee_remove_update_timer (GnmExprEntry *range);
190
static void     cb_gee_notify_cursor_position (GnmExprEntry *gee);
191

192
static gboolean gee_debug;
193
static GtkWidgetClass *parent_class = NULL;
194

195 196 197 198 199 200
static gboolean
gee_is_editing (GnmExprEntry *gee)
{
	return (gee != NULL && gee->wbcg != NULL && wbcg_is_editing (gee->wbcg));
}

201 202 203 204 205 206
static GnmConventions const *
gee_convs (const GnmExprEntry *gee)
{
	return sheet_get_conventions (gee->sheet);
}

207 208 209 210 211 212 213 214 215 216 217 218
static inline void
gee_force_abs_rel (GnmExprEntry *gee)
{
	Rangesel *rs = &gee->rangesel;
	if ((gee->flags & GNM_EE_FORCE_ABS_REF))
		rs->ref.a.col_relative = rs->ref.b.col_relative =
			rs->ref.a.row_relative = rs->ref.b.row_relative = FALSE;
	else if ((gee->flags & GNM_EE_FORCE_REL_REF))
		rs->ref.a.col_relative = rs->ref.b.col_relative =
			rs->ref.a.row_relative = rs->ref.b.row_relative = TRUE;
}

219
static void
220
gee_rangesel_reset (GnmExprEntry *gee)
221 222 223 224 225
{
	Rangesel *rs = &gee->rangesel;

	rs->text_start = 0;
	rs->text_end = 0;
Jody Goldberg's avatar
Jody Goldberg committed
226
	memset (&rs->ref, 0, sizeof (GnmRange));
227 228 229 230
	rs->ref.a.col_relative =
	rs->ref.b.col_relative =
	rs->ref.a.row_relative =
	rs->ref.b.row_relative = ((gee->flags & (GNM_EE_FORCE_ABS_REF|GNM_EE_DEFAULT_ABS_REF)) == 0);
231 232 233 234 235

	gee->rangesel.is_valid = FALSE;
}

static void
236
gee_destroy (GtkWidget *widget)
237
{
238
	GnmExprEntry *gee = GNM_EXPR_ENTRY (widget);
Jody Goldberg's avatar
Jody Goldberg committed
239 240
	gee_remove_update_timer (gee);
	gee_detach_scg (gee);
241
	((GtkWidgetClass *)(parent_class))->destroy (widget);
242 243
}

244 245 246 247 248 249 250 251 252 253 254 255 256
static void
cb_icon_clicked (GtkButton *icon,
		 GnmExprEntry *entry)
{
	GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));

	/* TODO special-case GnmExprEntry being directly packed
	 * into a GtkWindow. Currently, we just use it in
	 * GtkDialogs so the current window child widget
	 * is never identical to the entry when it is
	 * not rolled up.
	 */

257
	if (toplevel != NULL && gtk_widget_is_toplevel (toplevel)) {
258 259 260 261 262 263 264 265
		GtkWidget *old_entry_parent;
		GtkWidget *old_toplevel_child;
		GParamSpec **container_props_pspec;
		GValueArray *container_props;

		g_assert (GTK_IS_WINDOW (toplevel));

		if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (icon))) {
266 267
			int width, height;
			guint n;
268 269 270 271 272 273 274 275 276 277

			/* roll-up request */

			old_toplevel_child = gtk_bin_get_child (GTK_BIN (toplevel));
			g_assert (GTK_IS_WIDGET (old_toplevel_child));

			old_entry_parent = gtk_widget_get_parent (GTK_WIDGET (entry));
			g_assert (GTK_IS_CONTAINER (old_entry_parent));

			g_object_set_data_full (G_OBJECT (entry), "old_entry_parent",
278 279
						g_object_ref (old_entry_parent),
						(GDestroyNotify) g_object_unref);
280 281 282 283

			g_return_if_fail ((GtkWidget *) entry != old_toplevel_child);

			g_object_set_data_full (G_OBJECT (entry), "old_toplevel_child",
284 285
						g_object_ref (old_toplevel_child),
						(GDestroyNotify) g_object_unref);
286 287 288 289

			gtk_window_get_size (GTK_WINDOW (toplevel), &width, &height);
			g_object_set_data (G_OBJECT (entry), "old_window_width", GUINT_TO_POINTER (width));
			g_object_set_data (G_OBJECT (entry), "old_window_height", GUINT_TO_POINTER (height));
290
			g_object_set_data (G_OBJECT (entry), "old_default",
291
					   gtk_window_get_default_widget (GTK_WINDOW (toplevel)));
292 293 294 295 296 297

			container_props = NULL;

			container_props_pspec = gtk_container_class_list_child_properties
					(G_OBJECT_GET_CLASS (old_entry_parent), &n);

298
			if (container_props_pspec[0] != NULL) {
299
				guint ui;
300 301 302

				container_props = g_value_array_new (n);

303
				for (ui = 0; ui < n; ui++) {
304
					GValue value = G_VALUE_INIT;
305
					g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (container_props_pspec[ui]));
306 307

					gtk_container_child_get_property (GTK_CONTAINER (old_entry_parent), GTK_WIDGET (entry),
308
									  g_param_spec_get_name (container_props_pspec[ui]),
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
									  &value);
					g_value_array_append (container_props, &value);
				}
			}

			g_object_set_data_full (G_OBJECT (entry), "container_props",
						container_props,
						(GDestroyNotify) g_value_array_free);
			g_object_set_data_full (G_OBJECT (entry), "container_props_pspec",
						container_props_pspec,
						(GDestroyNotify) g_free);

			gtk_container_remove (GTK_CONTAINER (toplevel), old_toplevel_child);
			gtk_widget_reparent (GTK_WIDGET (entry), toplevel);

			gtk_widget_grab_focus (GTK_WIDGET (entry->entry));
325 326 327
			gtk_widget_set_can_default (GTK_WIDGET (icon), TRUE);
			gtk_widget_grab_default (GTK_WIDGET (icon));

328 329 330 331
			gtk_window_resize (GTK_WINDOW (toplevel), 1, 1);

		} else {
			int i;
332
			gpointer default_widget;
333 334 335 336 337 338 339 340 341

			/* reset rolled-up window */

			old_toplevel_child = g_object_get_data (G_OBJECT (entry), "old_toplevel_child");
			g_assert (GTK_IS_WIDGET (old_toplevel_child));

			old_entry_parent = g_object_get_data (G_OBJECT (entry), "old_entry_parent");
			g_assert (GTK_IS_CONTAINER (old_entry_parent));

342
			g_object_ref (entry);
343 344 345
			gtk_container_remove (GTK_CONTAINER (toplevel), GTK_WIDGET (entry));
			gtk_container_add (GTK_CONTAINER (toplevel), old_toplevel_child);
			gtk_container_add (GTK_CONTAINER (old_entry_parent), GTK_WIDGET (entry));
346
			g_object_unref (entry);
347 348 349 350

			container_props = g_object_get_data (G_OBJECT (entry), "container_props");
			container_props_pspec = g_object_get_data (G_OBJECT (entry), "container_props_pspec");

351 352 353 354
			for (i = 0; container_props_pspec[i] != NULL; i++) {
				gtk_container_child_set_property (GTK_CONTAINER (old_entry_parent), GTK_WIDGET (entry),
								  g_param_spec_get_name (container_props_pspec[i]),
								  g_value_array_get_nth (container_props, i));
355 356 357 358 359
			}

			gtk_window_resize (GTK_WINDOW (toplevel),
					   GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (entry), "old_window_width")),
					   GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (entry), "old_window_height")));
360 361 362 363 364
			default_widget = g_object_get_data (G_OBJECT (entry), "old_default");
			if (default_widget != NULL) {
				gtk_window_set_default (GTK_WINDOW (toplevel), GTK_WIDGET (default_widget));
				g_object_set_data (G_OBJECT (entry), "old_default", NULL);
			}
365 366 367 368 369 370 371 372 373 374 375

			g_object_set_data (G_OBJECT (entry), "old_entry_parent", NULL);
			g_object_set_data (G_OBJECT (entry), "old_toplevel_child", NULL);
			g_object_set_data (G_OBJECT (entry), "container_props", NULL);
			g_object_set_data (G_OBJECT (entry), "container_props_pspec", NULL);
		}
	} else {
		g_warning ("GnmExprEntry button was clicked, but entry has no toplevel parent.");
	}
}

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
static GnmValue *
get_matched_value (GnmExprEntry *gee)
{
	GODateConventions const *date_conv =
		workbook_date_conv (gee->sheet->workbook);
	const char *text = gnm_expr_entry_get_text (gee);

	return format_match_number (text, gee->constant_format, date_conv);
}


static void
gee_update_calendar (GnmExprEntry *gee)
{
	GDate date;
	GnmValue *v;
	GODateConventions const *date_conv =
		workbook_date_conv (gee->sheet->workbook);

	if (!gee->calendar_combo)
		return;

	v = get_matched_value (gee);
	if (!v)
		return;

402 403 404
	if (datetime_value_to_g (&date, v, date_conv)) {
		g_signal_handler_block (gee->calendar_combo,
					gee->calendar_combo_changed);
405 406 407
		go_calendar_button_set_date
			(GO_CALENDAR_BUTTON (gee->calendar_combo),
			 &date);
408 409 410
		g_signal_handler_unblock (gee->calendar_combo,
					  gee->calendar_combo_changed);
	}
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425

	value_release (v);
}

static void
cb_calendar_changed (GOCalendarButton *calb, GnmExprEntry *gee)
{
	GDate date;
	GODateConventions const *date_conv =
		workbook_date_conv (gee->sheet->workbook);
	int serial;

	if (!go_calendar_button_get_date (calb, &date))
		return;

426
	serial = go_date_g_to_serial (&date, date_conv);
427 428 429 430

	gee_set_value_double (GOG_DATA_EDITOR (gee), serial, date_conv);
}

431 432 433 434 435 436 437 438 439 440
static void
gee_set_format (GnmExprEntry *gee, GOFormat const *fmt)
{
	if (fmt == gee->constant_format)
		return;

	if (fmt) go_format_ref (fmt);
	go_format_unref (gee->constant_format);
	gee->constant_format = fmt;

441 442
	if (gee_debug)
		g_printerr ("Setting format %s\n", fmt ? go_format_as_XL (fmt) : "-");
443

444 445 446 447 448 449
	if (fmt && go_format_is_date (fmt)) {
		if (!gee->calendar_combo) {
			gee->calendar_combo = go_calendar_button_new ();
			gtk_widget_show (gee->calendar_combo);
			gtk_box_pack_start (GTK_BOX (gee), gee->calendar_combo,
					    FALSE, TRUE, 0);
450 451 452 453 454
			gee->calendar_combo_changed =
				g_signal_connect (G_OBJECT (gee->calendar_combo),
						  "changed",
						  G_CALLBACK (cb_calendar_changed),
						  gee);
455 456 457 458 459 460
			gee_update_calendar (gee);
		}
	} else {
		if (gee->calendar_combo) {
			gtk_widget_destroy (gee->calendar_combo);
			gee->calendar_combo = NULL;
461
			gee->calendar_combo_changed = 0;
462 463 464
		}
	}

465 466 467
	g_object_notify (G_OBJECT (gee), "constant-format");
}

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
static void
gee_set_with_icon (GnmExprEntry *gee, gboolean with_icon)
{
	gboolean has_icon = (gee->icon != NULL);
	with_icon = !!with_icon;

	if (has_icon == with_icon)
		return;

	if (with_icon) {
		gee->icon = gtk_toggle_button_new ();
		gtk_container_add (GTK_CONTAINER (gee->icon),
				   gtk_image_new_from_stock ("Gnumeric_ExprEntry",
							     GTK_ICON_SIZE_MENU));
		gtk_box_pack_end (GTK_BOX (gee), gee->icon, FALSE, FALSE, 0);
		gtk_widget_show_all (gee->icon);
		g_signal_connect (gee->icon, "clicked",
				  G_CALLBACK (cb_icon_clicked), gee);
	} else
		gtk_widget_destroy (gee->icon);
}

490 491 492 493 494 495
static void
gee_set_property (GObject      *object,
		  guint         prop_id,
		  GValue const *value,
		  GParamSpec   *pspec)
{
496
	GnmExprEntry *gee = GNM_EXPR_ENTRY (object);
497 498
	switch (prop_id) {
	case PROP_UPDATE_POLICY:
499
		gnm_expr_entry_set_update_policy (gee, g_value_get_enum (value));
500 501 502
		break;

	case PROP_WITH_ICON:
503
		gee_set_with_icon (gee, g_value_get_boolean (value));
504 505
		break;

506 507 508 509 510 511 512
	case PROP_TEXT: {
		const char *new_txt = g_value_get_string (value);
		const char *old_txt = gnm_expr_entry_get_text (gee);
		if (go_str_compare (new_txt, old_txt)) {
			gnm_expr_entry_load_from_text (gee, new_txt);
			gnm_expr_entry_signal_update (gee, FALSE);
		}
513
		break;
514
	}
515

516 517 518 519
	case PROP_FLAGS:
		gnm_expr_entry_set_flags (gee,
			g_value_get_uint (value), GNM_EE_MASK);
		break;
520
	case PROP_SCG:
521
		gnm_expr_entry_set_scg (gee,
522
			SHEET_CONTROL_GUI (g_value_get_object (value)));
523
		break;
Jody Goldberg's avatar
Jody Goldberg committed
524 525
	case PROP_WBCG:
		g_return_if_fail (gee->wbcg == NULL);
526
		gee->wbcg = WBC_GTK (g_value_get_object (value));
Jody Goldberg's avatar
Jody Goldberg committed
527
		break;
528 529 530
	case PROP_CONSTANT_FORMAT:
		gee_set_format (gee, g_value_get_pointer (value));
		break;
531 532
	case PROP_EDITING_CANCELED:
		gee->editing_canceled = g_value_get_boolean (value);
533 534 535 536 537 538 539 540 541 542 543
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
	}
}

static void
gee_get_property (GObject      *object,
		  guint         prop_id,
		  GValue       *value,
		  GParamSpec   *pspec)
{
544
	GnmExprEntry *gee = GNM_EXPR_ENTRY (object);
545 546 547 548 549 550 551
	switch (prop_id) {
	case PROP_UPDATE_POLICY:
		g_value_set_enum (value, gee->update_policy);
		break;
	case PROP_WITH_ICON:
		g_value_set_boolean (value, gee->icon != NULL);
		break;
552 553 554
	case PROP_TEXT:
		g_value_set_string (value, gnm_expr_entry_get_text (gee));
		break;
555 556 557
	case PROP_FLAGS:
		g_value_set_uint (value, gee->flags);
		break;
558 559
	case PROP_SCG:
		g_value_set_object (value, G_OBJECT (gee->scg));
560
		break;
Jody Goldberg's avatar
Jody Goldberg committed
561 562 563
	case PROP_WBCG:
		g_value_set_object (value, G_OBJECT (gee->wbcg));
		break;
564 565 566
	case PROP_CONSTANT_FORMAT:
		g_value_set_pointer (value, (gpointer)gee->constant_format);
		break;
567 568
	case PROP_EDITING_CANCELED:
		g_value_set_boolean (value, gee->editing_canceled);
569 570 571 572 573 574
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
	}
}

static void
575
cb_entry_activate (GnmExprEntry *gee)
576
{
577
	g_signal_emit (G_OBJECT (gee), signals[ACTIVATE], 0);
Jody Goldberg's avatar
Jody Goldberg committed
578
	gnm_expr_entry_signal_update (gee, TRUE);
579 580 581
}

static void
582 583
gee_destroy_feedback_range (GnmExprEntry *gee)
{
584 585 586 587 588 589 590 591
	WBCGtk *wbcg = scg_wbcg (gee->scg);
	int page, pages = wbcg_get_n_scg (wbcg);

	for (page = 0; page < pages; page++) {
		SheetControlGUI *scg = wbcg_get_nth_scg (wbcg, page);
		SCG_FOREACH_PANE (scg, pane,
				  gnm_pane_expr_cursor_stop (pane););
	}
592 593
}

594
static void
595
gnm_expr_entry_colour_ranges (GnmExprEntry *gee, int start, int end, GnmRangeRef *rr, int colour,
596
			      PangoAttrList **attrs, gboolean insert_cursor)
597 598 599 600 601 602
{
	static struct {
		guint16 red;
		guint16 green;
		guint16 blue;
		gchar const *name;
603 604
	} colours[] = {{0x0, 0xFFFF, 0x0, "00:ff:00:ff"},
		       {0x0, 0x0, 0xFFFF, "00:00:ff:ff"},
605 606 607 608 609 610 611 612 613
		       {0xFFFF, 0x0, 0x0, "ff:00:00:ff"}, 
		       {0x0, 0x80FF, 0x80FF, "00:80:80:ff"}, 
		       {0xA0FF, 0xA0FF, 0x0, "a0:a0:00:ff"}, 
		       {0xA0FF, 0x0, 0xA0FF, "a0:00:a0:ff"}};
	PangoAttribute *at;
	GnmRange r;
	GnmRange const *merge; /*[#127415]*/
	Sheet *start_sheet, *end_sheet;
	Sheet *sheet = scg_sheet (gee->scg);
614
	SheetControlGUI *scg = NULL;
615

616 617 618
	if (*attrs == NULL)
		*attrs = pango_attr_list_new ();

619 620 621 622 623 624
	colour = colour % G_N_ELEMENTS (colours);

	gnm_rangeref_normalize_pp (rr, &gee->pp,
				   &start_sheet,
				   &end_sheet,
				   &r);
625
	if (start_sheet != end_sheet)
626
		return;
627 628 629 630 631 632 633 634 635 636 637
	if (insert_cursor) {
		if (range_is_singleton  (&r) &&
		    NULL != (merge = gnm_sheet_merge_is_corner
			     (start_sheet, &r.start)))
			r = *merge;
		if (start_sheet == sheet) 
			scg = gee->scg;
		else {
			WBCGtk *wbcg = scg_wbcg (gee->scg);
			scg = wbcg_get_nth_scg (wbcg, start_sheet->index_in_wb);
		}
638
	
639 640 641
		SCG_FOREACH_PANE (scg, pane, gnm_pane_expr_cursor_bound_set 
				  (pane, &r, colours[colour].name););
	}
642

643 644
	at = pango_attr_foreground_new (colours[colour].red, colours[colour].green, 
					colours[colour].blue);
645 646
	at->start_index = start;
	at->end_index = end;
647
	
648
	pango_attr_list_change (*attrs, at);
649 650
}

651 652 653 654 655 656 657
/* WARNING : DO NOT CALL THIS FROM FROM UPDATE.  It may create another
 *           canvas-item which would in turn call update and confuse the
 *           canvas.
 */
static void
gee_scan_for_range (GnmExprEntry *gee)
{
658 659
	PangoAttrList *attrs = NULL;

660
	parse_pos_init_editpos (&gee->pp, scg_view (gee->scg));
661
	gee_destroy_feedback_range (gee);
662 663
	if (!gee->feedback_disabled && gee_is_editing (gee) && gee->lexer_items != NULL) {
		GnmLexerItem *gli = gee->lexer_items;
664 665 666 667 668
		int colour = 1; /* We start with 1 since GINT_TO_POINTER (0) == NULL */
		GHashTable *hash = g_hash_table_new_full ((GHashFunc) gnm_rangeref_hash,
							  (GEqualFunc) gnm_rangeref_equal,
							  g_free,
							  NULL);
669 670 671 672 673 674 675 676 677
		do {
			if (gli->token == RANGEREF) {
				char const *text = gtk_entry_get_text (gee->entry);
				char *rtext = g_strndup (text + gli->start, 
							 gli->end - gli->start);
				char const *tmp;
				GnmRangeRef rr;
				tmp = rangeref_parse (&rr, rtext, 
						      &gee->pp, gee_convs (gee));
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
				if (tmp != rtext) {
					gpointer val;
					gint this_colour;
					gboolean insert_cursor;
					if (rr.a.sheet == NULL)
						rr.a.sheet = gee->sheet;
					if (rr.b.sheet == NULL)
						rr.b.sheet = rr.a.sheet;
					val = g_hash_table_lookup (hash, &rr);
					if (val == NULL) {
						GnmRangeRef *rrr = gnm_rangeref_dup (&rr);
						this_colour = colour++;
						g_hash_table_insert (hash, rrr, GINT_TO_POINTER (this_colour));
						insert_cursor = TRUE;
						rtext = NULL;
 					} else {
						this_colour = GPOINTER_TO_INT (val);
						insert_cursor = FALSE;
					}
697
					gnm_expr_entry_colour_ranges (gee, gli->start, gli->end, &rr, 
698 699
								      this_colour, &attrs, insert_cursor);
				}
700
				g_free (rtext);
701
			}
702 703
		} while (gli++->token != 0);
		g_hash_table_destroy (hash);
704
	}
705 706 707 708 709
	if (attrs)
		g_object_set_data_full (G_OBJECT (gee->entry), "gnm:range-attributes", attrs, 
					(GDestroyNotify) pango_attr_list_unref);
	else
		g_object_set_data (G_OBJECT (gee->entry), "gnm:range-attributes", NULL);	
710 711 712 713
}

static void
gee_update_env (GnmExprEntry *gee)
714
{
715
	if (!gee->ignore_changes) {
716 717 718 719
		if (NULL != gee->scg &&
#warning why do we want this dichotomy
		    !gee->is_cell_renderer &&
		    !gnm_expr_entry_can_rangesel (gee))
720
			scg_rangesel_stop (gee->scg, FALSE);
721 722 723

		if (gnm_expr_char_start_p (gtk_entry_get_text (gee->entry)))
			gee_scan_for_range (gee);
724 725
	}

726 727
}

728
static void
729
gee_delete_tooltip (GnmExprEntry *gee, gboolean remove_completion)
730
{
731 732 733 734 735 736 737 738
	if (gee->tooltip.tooltip) {
		gtk_widget_destroy (gee->tooltip.tooltip);
		gee->tooltip.tooltip = NULL;
	}
	if (gee->tooltip.fd) {
		gnm_func_unref (gee->tooltip.fd);
		gee->tooltip.fd = NULL;
	}
Andreas J. Guelzow 's avatar
Andreas J. Guelzow committed
739
	if (gee->tooltip.handlerid != 0 && gee->entry != NULL) {
740
		g_signal_handler_disconnect (gtk_widget_get_toplevel
741 742 743 744
					     (GTK_WIDGET (gee->entry)),
					     gee->tooltip.handlerid);
		gee->tooltip.handlerid = 0;
	}
745
	if (remove_completion) {
746 747
		g_free (gee->tooltip.completion);
		gee->tooltip.completion = NULL;
748
		gee->tooltip.completion_se_valid = FALSE;
749
	}
750 751
}

752
void
753 754 755
gnm_expr_entry_close_tips  (GnmExprEntry *gee)
{
	if (gee != NULL)
756
		gee_delete_tooltip (gee, FALSE);
757 758
}

759
static gboolean
760 761 762
cb_gee_focus_out_event (GtkWidget         *widget,
			GdkEventFocus     *event,
			gpointer           user_data);
763 764

static GtkWidget *
Morten Welinder's avatar
Morten Welinder committed
765
gee_create_tooltip (GnmExprEntry *gee, gchar const *str,
766
		    gchar const *marked_str, gboolean set_tabs)
767
{
768
	GtkWidget *toplevel, *label, *tip;
769
	gint root_x = 0, root_y = 0;
770 771
	GtkAllocation allocation;
	GdkWindow *gdkw;
772 773
	gchar *markup = NULL;
	GString *string;
774

775 776
	toplevel = gtk_widget_get_toplevel (GTK_WIDGET (gee->entry));
	gtk_widget_add_events(toplevel, GDK_FOCUS_CHANGE_MASK);
777
	if (gee->tooltip.handlerid == 0)
778
		gee->tooltip.handlerid = g_signal_connect
779 780
			(G_OBJECT (toplevel), "focus-out-event",
			 G_CALLBACK (cb_gee_focus_out_event), gee);
781 782 783 784

	label = gnumeric_create_tooltip (toplevel);
	tip = gtk_widget_get_toplevel (label);

Morten Welinder's avatar
Morten Welinder committed
785
	if (str)
786 787 788 789 790
		markup = gnm_func_convert_markup_to_pango (str);
	string = g_string_new (markup);
	if (marked_str)
		g_string_append (string, marked_str);
	gtk_label_set_markup (GTK_LABEL (label), string->str);
791
	g_free (markup);
792
	g_string_free (string, TRUE);
793

794 795 796
	if (set_tabs) {
		PangoLayout *pl = gtk_label_get_layout (GTK_LABEL (label));
		PangoTabArray *tabs;
Morten Welinder's avatar
Morten Welinder committed
797 798 799
		tabs = pango_tab_array_new_with_positions
			(2, TRUE,
			 PANGO_TAB_LEFT, 120,
800 801 802 803 804
			 PANGO_TAB_LEFT, 140);
		pango_layout_set_tabs (pl, tabs);
		pango_tab_array_free (tabs);
	}

805 806 807
	gdkw = gtk_widget_get_window (GTK_WIDGET (gee->entry));
	gdk_window_get_origin (gdkw, &root_x, &root_y);
	gtk_widget_get_allocation (GTK_WIDGET (gee->entry), &allocation);
808

809 810
	gtk_window_move (GTK_WINDOW (tip),
			 root_x + allocation.x,
811
			 root_y + allocation.y + allocation.height);
812 813 814 815

	gtk_widget_show_all (tip);

	return tip;
816 817
}

818 819 820 821 822 823 824 825 826 827
static void
gee_set_tooltip_argument (GString *str, char *arg, gboolean optional)
{
	if (optional)
		g_string_append_c (str, '[');
	g_string_append (str, arg);
	if (optional)
		g_string_append_c (str, ']');
}

828
static void
829
gee_set_tooltip (GnmExprEntry *gee, GnmFunc *fd, gint args, gboolean had_stuff)
830 831 832 833 834
{
	GString *str;
	gchar sep = go_locale_get_arg_sep ();
	gint min, max, i;
	gboolean first = TRUE;
Morten Welinder's avatar
Morten Welinder committed
835
	char *extra = NULL;
836 837
	gboolean localized_function_names = gee->sheet->convs->localized_function_names;
	const char *fdname;
838

839 840 841
	gnm_func_load_if_stub (fd);
	function_def_count_args (fd, &min, &max);

Morten Welinder's avatar
Morten Welinder committed
842
	if ((gee->tooltip.fd)
843 844
	    && (gee->tooltip.fd == fd && gee->tooltip.args == args
		&& gee->tooltip.had_stuff == (max == 0 && args == 0 && had_stuff)))
845
			return;
846
	gee_delete_tooltip (gee, FALSE);
847 848

	gee->tooltip.fd = fd;
849
	gnm_func_ref (gee->tooltip.fd);
850

851 852 853
	fdname = gnm_func_get_name (fd, localized_function_names);

	str = g_string_new (fdname);
854 855 856 857 858 859 860 861 862 863
	g_string_append_c (str, '(');

	for (i = 0; i < max; i++) {
		char *arg_name = function_def_get_arg_name
			(fd, i);
		if (arg_name != NULL) {
			if (first)
				first = FALSE;
			else
				g_string_append_c (str, sep);
864
			if (i == args) {
Morten Welinder's avatar
Morten Welinder committed
865 866 867 868
				extra = g_strdup_printf
					(_("%s: %s"),
					 arg_name,
					 gnm_func_get_arg_description (fd, i));
869
				g_string_append (str, UNICODE_RIGHT_TRIANGLE);
Morten Welinder's avatar
Morten Welinder committed
870 871 872
			}
			gee_set_tooltip_argument (str, arg_name, i >= min);
			if (i == args)
873
				g_string_append (str, UNICODE_LEFT_TRIANGLE);
874 875 876 877 878 879 880
			g_free (arg_name);
		} else
			break;
	}
	if (i < max) {
		if (!first)
			g_string_append_c (str, sep);
881
		g_string_append
Morten Welinder's avatar
Morten Welinder committed
882
			(str, (args >= i && args < max)
883
			 ? UNICODE_RIGHT_TRIANGLE UNICODE_ELLIPSIS UNICODE_LEFT_TRIANGLE
Morten Welinder's avatar
Morten Welinder committed
884 885
			 : UNICODE_ELLIPSIS);
	}
886
	if (max == 0 && args == 0 && !had_stuff) {
887
		extra = g_strdup_printf (_("%s takes no arguments"),
888
					 fdname);
889
	} else if (args >= max) {
890
		g_string_append (str, UNICODE_RIGHT_TRIANGLE UNICODE_CROSS_AND_SKULLBONES UNICODE_LEFT_TRIANGLE);
Morten Welinder's avatar
Morten Welinder committed
891
		extra = g_strdup_printf (_("Too many arguments for %s"),
892
					 fdname);
893 894
	}
	g_string_append_c (str, ')');
Morten Welinder's avatar
Morten Welinder committed
895 896 897 898 899
	if (extra) {
		g_string_append_c (str, '\n');
		g_string_append (str, extra);
		g_free (extra);
	}
900

Morten Welinder's avatar
Morten Welinder committed
901
	gee->tooltip.tooltip = gee_create_tooltip
902
		(gee, str->str, _("\n\n<i>Ctrl-F4 to close tooltip</i>"), FALSE);
903
	gee->tooltip.args = args;
904
	gee->tooltip.had_stuff = (max == 0 && args == 0 && had_stuff);
905

906 907 908
	g_string_free (str, TRUE);
}

909
static gboolean
910
gee_set_tooltip_completion (GnmExprEntry *gee, GSList *list, guint start, guint end)
911 912
{
	GString *str;
913
	GString *str_marked;
914 915
	gint i = 0;
	gint max = 10;
916
	GSList *list_c = list;
917
	gchar const *name = NULL;
918
	gboolean show_tool_tip;
919
	gboolean localized_function_names = gee->sheet->convs->localized_function_names;
920

921
	gee_delete_tooltip (gee, TRUE);
922 923

	str = g_string_new (NULL);
924
	for (; list_c != NULL && ++i < max; list_c = list_c->next) {
925
		GnmFunc *fd = list_c->data;
926
		name = gnm_func_get_name (fd, localized_function_names);
927 928 929
		if ((end - start) < (guint) g_utf8_strlen (name, -1))
			/* xgettext: the first %s is a function name and */
			/* the second %s the function description */
Morten Welinder's avatar
Morten Welinder committed
930
			g_string_append_printf (str, _("%s : \t%s\n"), name,
931 932 933 934
						gnm_func_get_description (fd));
		else {
			/* xgettext: the first %s is a function name and */
			/* the second %s the function description */
Morten Welinder's avatar
Morten Welinder committed
935
			g_string_append_printf (str, _("[%s : \t%s]\n"), name,
936 937 938
						gnm_func_get_description (fd));
			i--;
		}
939
	}
940 941 942 943

	str_marked = g_string_new (NULL);
	if (i == max)
		g_string_append (str_marked, UNICODE_ELLIPSIS_VERT "\n");
944
	if (i == 1) {
Morten Welinder's avatar
Morten Welinder committed
945
		gee->tooltip.completion
946
			= g_strdup (name);
947 948
		/*xgettext: short form for: "type F4-key to complete the name"*/
		g_string_append (str_marked, _("\n<i>F4 to complete</i>"));
949
	} else if (i > 1)
950
		/*xgettext: short form for: "type shift-F4-keys to select the completion"*/
951
		g_string_append (str_marked, _("\n<i>\xe2\x87\xa7""F4 to select</i>"));
952 953
	else
		g_string_truncate (str, str->len - 1);
954 955 956
	gee->tooltip.completion_start = start;
	gee->tooltip.completion_end = end;
	gee->tooltip.completion_se_valid = TRUE;
957 958
	show_tool_tip = gnm_conf_get_core_gui_editing_function_name_tooltips ();
	if (show_tool_tip)
Morten Welinder's avatar
Morten Welinder committed
959
		gee->tooltip.tooltip = gee_create_tooltip
960
			(gee, str->str, str_marked->str, TRUE);
961
	g_string_free (str, TRUE);
962
	g_string_free (str_marked, TRUE);
963
	g_slist_free_full (list, (GDestroyNotify) gnm_func_unref);
964
	return show_tool_tip;
965 966
}

967 968 969
static void
gee_dump_lexer (GnmLexerItem *gli) {
	g_print ("************\n");
970
	do {
Morten Welinder's avatar
Morten Welinder committed
971
		g_print ("%2" G_GSIZE_FORMAT " to %2" G_GSIZE_FORMAT ": %d\n",
972
			 gli->start, gli->end, gli->token);
973
	} while (gli++->token != 0);
974
	g_print ("************\n");
Morten Welinder's avatar
Morten Welinder committed
975

976 977
}

978 979
static gint
func_def_cmp (gconstpointer a_, gconstpointer b_, gpointer user)
980
{
981 982 983 984 985 986 987
	GnmFunc const * const a = (GnmFunc const * const)a_;
	GnmFunc const * const b = (GnmFunc const * const)b_;
	GnmExprEntry *gee = user;
	gboolean localized = gee->sheet->convs->localized_function_names;

	return g_utf8_collate (gnm_func_get_name (a, localized),
			       gnm_func_get_name (b, localized));
988 989
}

990 991 992 993 994 995 996

static void
gee_update_lexer_items (GnmExprEntry *gee)
{
	GtkEditable *editable = GTK_EDITABLE (gee->entry);
	char *str = gtk_editable_get_chars (editable, 0, -1);
	Sheet *sheet = scg_sheet (gee->scg);
997 998
	GOFormat const *format;
	gboolean forced_text;
999 1000 1001 1002 1003 1004 1005 1006 1007

	g_free (gee->lexer_items);
	gee->lexer_items = NULL;

	if (gee->texpr != NULL) {
		gnm_expr_top_unref (gee->texpr);
		gee->texpr = NULL;
	}

1008
	parse_pos_init_editpos (&gee->pp, scg_view (gee->scg));
1009 1010 1011
	format = gnm_style_get_format
		(sheet_style_get (sheet, gee->pp.eval.col, gee->pp.eval.row));
	forced_text = ((format != NULL) && go_format_is_text (format));
1012

1013
	if (!gee->feedback_disabled && !forced_text) {
1014
		gee->texpr = gnm_expr_parse_str
1015
			((str[0] == '=') ? str+1 : str,
1016
			 &gee->pp, GNM_EXPR_PARSE_DEFAULT
1017
			 | GNM_EXPR_PARSE_UNKNOWN_NAMES_ARE_STRINGS,
1018 1019 1020
			 sheet_get_conventions (sheet), NULL);
	}

1021 1022
	gee->tooltip.is_expr =  (!forced_text) &&
		(NULL != gnm_expr_char_start_p (str));
1023
	if (!(gee->flags & GNM_EE_SINGLE_RANGE)) {
Morten Welinder's avatar
Morten Welinder committed
1024
		gee->lexer_items = gnm_expr_lex_all
1025 1026 1027 1028 1029 1030
			(str, &gee->pp,
			 GNM_EXPR_PARSE_UNKNOWN_NAMES_ARE_STRINGS,
			 NULL);
		if (gnm_debug_flag ("functooltip"))
			gee_dump_lexer (gee->lexer_items);
	}
Morten Welinder's avatar
Morten Welinder committed
1031
	g_free (str);
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
}

static GnmLexerItem *
gee_duplicate_lexer_items (GnmLexerItem *gli)
{
	int n = 1;
	GnmLexerItem *gli_c = gli;

	while (gli_c->token != 0) {
		gli_c++;
		n++;
	}

	return g_memdup (gli, n * sizeof (GnmLexerItem));
}

1048 1049 1050 1051
static void
gee_check_tooltip (GnmExprEntry *gee)
{
	GtkEditable *editable = GTK_EDITABLE (gee->entry);
1052
	gint  end, args = 0;
1053
	guint end_t;
1054
	char *str;
1055
	gboolean stuff = FALSE, completion_se_set = FALSE;
1056
	GnmLexerItem *gli, *gli_c;
1057
	int last_token = 0;
1058

Morten Welinder's avatar
Morten Welinder committed
1059
	if (gee->lexer_items == NULL || !gee->tooltip.enabled ||
1060
	    (!gee->tooltip.is_expr && !gee->is_cell_renderer)) {
1061
		gee_delete_tooltip (gee, TRUE);
1062
		return;
1063
	}
1064

1065 1066 1067
	end = gtk_editable_get_position (editable);

	if (end == 0) {
1068
		gee_delete_tooltip (gee, TRUE);
1069 1070
		return;
	}
1071

1072 1073
	str = gtk_editable_get_chars (editable, 0, -1);
	end_t = g_utf8_offset_to_pointer (str, end) - str;
1074

1075 1076

	gli_c = gli = gee_duplicate_lexer_items (gee->lexer_items);
1077

1078 1079
	/*
	 * If we have an open string at the end of the entry, we
1080
	 * need to adjust.
1081
	 */
Morten Welinder's avatar
Morten Welinder committed
1082

1083
	for (; gli->token != 0; gli++) {
1084 1085 1086 1087
		if (gli->start >= end_t) {
			gli->token = 0;
			break;
		}
1088 1089 1090 1091 1092 1093
		if (gli->token != TOKEN_UNMATCHED_APOSTROPHY)
			continue;
		if (gli->start == 0)
			goto not_found;
		gli->token = 0;
		stuff = TRUE;
1094
		break;
1095
	}
1096 1097
	if (gli > gli_c)
		gli--;
1098 1099
	if (gli > gli_c)
		last_token = (gli - 1)->token;
1100

1101
	/* This creates the completion tooltip */
1102
	if (!stuff && gli->token == STRING && last_token != CONSTANT) {
1103 1104 1105 1106 1107 1108
		guint start_t = gli->start;
		char *prefix;
		GSList *list;

		end_t = gli->end;
		prefix = g_strndup (str + start_t, end_t - start_t);
Morten Welinder's avatar
Morten Welinder committed
1109
		list = gnm_func_lookup_prefix
1110 1111 1112
			(prefix, gee->sheet->workbook);
		g_free (prefix);
		if (list != NULL) {
1113
			list = g_slist_sort_with_data
Morten Welinder's avatar
Morten Welinder committed
1114
				(list,
1115 1116
				 func_def_cmp,
				 gee);
Morten Welinder's avatar
Morten Welinder committed
1117
			if (gee_set_tooltip_completion
1118 1119 1120 1121 1122
			    (gee, list, start_t, end_t)) {
				g_free (str);
				g_free (gli_c);
				return;
			}
1123
		} else {
1124 1125
			g_free (gee->tooltip.completion);
			gee->tooltip.completion = NULL;
1126 1127 1128
			gee->tooltip.completion_start = start_t;
			gee->tooltip.completion_end = end_t;
			gee->tooltip.completion_se_valid = TRUE;
1129
		}
Morten Welinder's avatar
Morten Welinder committed
1130
		completion_se_set = TRUE;
1131 1132 1133
	} else {
		g_free (gee->tooltip.completion);
		gee->tooltip.completion = NULL;
Morten Welinder's avatar
Morten Welinder committed
1134
		gee->tooltip.completion_se_valid = FALSE;
1135
	}
Morten Welinder's avatar
Morten Welinder committed
1136

1137 1138 1139

	if (!gnm_conf_get_core_gui_editing_function_argument_tooltips ())
		goto not_found;
1140

1141
	if (gnm_debug_flag ("functooltip"))
1142 1143
		g_print ("Last token considered is %d from %2"
			 G_GSIZE_FORMAT " to %2" G_GSIZE_FORMAT ".\n",
1144
			 gli->token, gli->start, gli->end);
Morten Welinder's avatar
Morten Welinder committed
1145

1146

1147 1148 1149
	while (gli->start > 1) {
		switch (gli->token) {
		case TOKEN_PARENTHESIS_OPEN:
1150
			if ((gli - 1)->token == STRING) {
1151 1152
				gint start_t = (gli - 1)->start;
				gint end_t = (gli - 1)->end;
Morten Welinder's avatar
Morten Welinder committed
1153
				char *name = g_strndup (str + start_t,
1154 1155 1156 1157 1158 1159 1160 1161
							end_t - start_t);
				GnmFunc	*fd = gnm_func_lookup (name, NULL);
				g_free (name);
				if (fd != NULL) {
					gee_set_tooltip (gee, fd, args, stuff);
					g_free (str);
					g_free (gli_c);
					return;
1162 1163
				}
			}
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
			stuff = TRUE;
			args = 0;
			break;
		case TOKEN_BRACE_OPEN:
			stuff = (args == 0);
			args = 0;
			break;
		case TOKEN_PARENTHESIS_CLOSED: {
			gint para = 1;
			gli--;
			while (gli->start > 1 && para > 0) {
				switch (gli->token) {
				case TOKEN_PARENTHESIS_CLOSED:
					para++;
					break;
				case TOKEN_PARENTHESIS_OPEN:
Morten Welinder's avatar
Morten Welinder committed
1180
					para--;
1181 1182 1183 1184 1185
					break;
				default:
					break;
				}
				gli--;
1186
			}
1187 1188 1189
			gli++;
			stuff = (args == 0);
			break;
1190
		}
1191 1192 1193 1194 1195 1196 1197 1198 1199
		case TOKEN_BRACE_CLOSED: {
			gint para = 1;
			gli--;
			while (gli->start > 1 && para > 0) {
				switch (gli->token) {
				case TOKEN_BRACE_CLOSED:
					para++;
					break;
				case TOKEN_BRACE_OPEN:
Morten Welinder's avatar
Morten Welinder committed
1200
					para--;
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
					break;
				default:
					break;
				}
				gli--;
			}
			gli++;
			stuff = (args == 0);
			break;
		}
1211
		case ARG_SEP:
1212 1213 1214 1215 1216 1217 1218 1219
			args++;
			break;
		default:
			stuff = (args == 0);
			break;
		}
		if (gli->start > 1)
			gli--;
1220
	}
1221

1222
 not_found:
1223
	g_free (str);
1224
	g_free (gli_c);
1225
	gee_delete_tooltip (gee, !completion_se_set);
1226 1227
	return;
}
1228

1229
static gboolean
1230 1231 1232
cb_gee_focus_out_event (G_GNUC_UNUSED GtkWidget     *widget,
			G_GNUC_UNUSED GdkEventFocus *event,
			gpointer                     user_data)
1233
{
1234
	gee_delete_tooltip (user_data, FALSE);
1235 1236 1237
	return FALSE;
}

1238 1239 1240 1241 1242
static void
cb_gee_notify_cursor_position (GnmExprEntry *gee)
{
	gee_update_env (gee);
	gee_check_tooltip (gee);
1243 1244
}

1245 1246 1247
static void
cb_entry_changed (GnmExprEntry *gee)
{
1248
	gee_update_lexer_items (gee);
1249
	gee_update_env (gee);
1250
	gee_update_calendar (gee);
1251
	gee_check_tooltip (gee);
1252
	g_signal_emit (G_OBJECT (gee), signals[CHANGED], 0);
1253 1254 1255
}

static gboolean
1256 1257 1258
cb_gee_key_press_event (GtkEntry	*entry,
			GdkEventKey	*event,
			GnmExprEntry	*gee)
1259
{
1260
	WBCGtk *wbcg  = gee->wbcg;
Jody Goldberg's avatar
Jody Goldberg committed
1261
	gboolean is_enter = FALSE;
1262 1263 1264
	int state = gnumeric_filter_modifiers (event->state);

	switch (event->keyval) {
1265 1266
	case GDK_KEY_Up:	case GDK_KEY_KP_Up:
	case GDK_KEY_Down:	case GDK_KEY_KP_Down:
1267 1268 1269 1270
		if (gee->is_cell_renderer)
			return FALSE;
		/* Ignore these keys */
		return TRUE;
1271 1272 1273
		/* GDK_KEY_F2 starts editing */
		/* GDK_KEY_F3 opens the paste names dialog */
	case GDK_KEY_F4: {
1274 1275 1276 1277
		/* Cycle absolute reference mode through the sequence rel/rel,
		 * abs/abs, rel/abs, abs/rel and back to rel/rel. Update text
		 * displayed in entry.
		 */
Andreas J. Guelzow 's avatar