GnuCash  5.6-150-g038405b370+
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
completioncell-gnome.c
1 /********************************************************************\
2  * completioncell-gnome.c -- completion combobox cell for gnome *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License as *
6  * published by the Free Software Foundation; either version 2 of *
7  * the License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License*
15  * along with this program; if not, contact: *
16  * *
17  * Free Software Foundation Voice: +1-617-542-5942 *
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19  * Boston, MA 02110-1301, USA gnu@gnu.org *
20  * *
21 \********************************************************************/
22 
23 /*
24  * FILE: completioncell-gnome.c
25  *
26  * FUNCTION: Implement gnome portion of a entry completion combo widget
27  * embedded in a table cell.
28  *
29  * HISTORY:
30  * @author Copyright (c) 2023 Robert Fewell
31  */
32 
33 #include <config.h>
34 
35 #include <string.h>
36 #include <stdbool.h>
37 #include <gdk/gdkkeysyms.h>
38 
39 #include "completioncell.h"
40 #include "gnc-prefs.h"
41 #include "gnucash-item-edit.h"
42 #include "gnucash-item-list.h"
43 #include "gnucash-sheet.h"
44 #include "gnucash-sheetP.h"
45 #include "table-allgui.h"
46 #include "gnc-glib-utils.h"
47 #include <gnc-unicode.h>
48 
49 typedef struct _PopBox
50 {
51  GnucashSheet* sheet;
52  GncItemEdit* item_edit;
53  GncItemList* item_list;
54 
55  GHashTable* item_hash; // the item hash table
56  GtkListStore* item_store; // the item list store
57 
58  gchar* newval; // string value to find
59  gint newval_len; // length of string value to find
60 
61  gboolean signals_connected; // list signals connected
62  gboolean list_popped; // list is popped up
63 
64  gboolean autosize; // autosize the popup width
65 
66  gboolean sort_enabled; // sort of list store enabled
67  gboolean register_is_reversed; // whether the register is reversed
68 
69  gboolean strict; // text entry must be in the list
70  gboolean in_list_select; // item selected in the list
71 
72  gint occurrence; // the position in the list
73 
74 } PopBox;
75 
76 #define DONT_TEXT N_("Don't autocomplete")
77 
79 enum GncCompletionColumn
80 {
81  TEXT_COL, //0
82  TEXT_MARKUP_COL, //1
83  WEIGHT_COL, //2
84  FOUND_LOCATION_COL, //3
85 };
86 
87 static void gnc_completion_cell_gui_realize (BasicCell* bcell, gpointer w);
88 static void gnc_completion_cell_gui_move (BasicCell* bcell);
89 static void gnc_completion_cell_gui_destroy (BasicCell* bcell);
90 static gboolean gnc_completion_cell_enter (BasicCell* bcell,
91  int* cursor_position,
92  int* start_selection,
93  int* end_selection);
94 static void gnc_completion_cell_leave (BasicCell* bcell);
95 static void gnc_completion_cell_destroy (BasicCell* bcell);
96 
97 BasicCell*
98 gnc_completion_cell_new (void)
99 {
100  CompletionCell* cell = g_new0 (CompletionCell, 1);
101  gnc_completion_cell_init (cell);
102  return &cell->cell;
103 }
104 
105 void
106 gnc_completion_cell_init (CompletionCell* cell)
107 {
108  gnc_basic_cell_init (& (cell->cell));
109 
110  cell->cell.is_popup = TRUE;
111 
112  cell->cell.destroy = gnc_completion_cell_destroy;
113 
114  cell->cell.gui_realize = gnc_completion_cell_gui_realize;
115  cell->cell.gui_destroy = gnc_completion_cell_gui_destroy;
116 
117  PopBox* box = g_new0 (PopBox, 1);
118 
119  box->sheet = NULL;
120  box->item_edit = NULL;
121  box->item_list = NULL;
122  box->item_store = gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_STRING,
123  G_TYPE_INT, G_TYPE_INT);
124  box->signals_connected = FALSE;
125  box->list_popped = FALSE;
126  box->autosize = FALSE;
127  box->register_is_reversed = FALSE;
128 
129  box->sort_enabled = FALSE;
130 
131  cell->cell.gui_private = box;
132 
133  box->strict = FALSE;
134  box->in_list_select = FALSE;
135  box->occurrence = 0;
136 
137  box->item_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
138 }
139 
140 static void
141 hide_popup (PopBox* box)
142 {
143  gnc_item_edit_hide_popup (box->item_edit);
144  box->list_popped = FALSE;
145 }
146 
147 static void
148 select_item_cb (GncItemList* item_list, char* item_string, gpointer user_data)
149 {
150  CompletionCell* cell = user_data;
151  PopBox* box = cell->cell.gui_private;
152 
153  box->in_list_select = TRUE;
154  gnucash_sheet_modify_current_cell (box->sheet, item_string);
155  box->in_list_select = FALSE;
156 
157  hide_popup (box);
158 }
159 
160 static gint
161 text_width (PangoLayout *layout)
162 {
163  PangoRectangle logical_rect;
164  pango_layout_set_width (layout, -1);
165  pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
166  return logical_rect.width;
167 }
168 
169 static void
170 horizontal_scroll_to_found_text (PopBox* box, char* item_string, gint found_location)
171 {
172  if (!gtk_widget_get_realized (GTK_WIDGET(box->item_list->tree_view)))
173  return;
174 
175  GtkAllocation alloc;
176  gtk_widget_get_allocation (GTK_WIDGET(box->item_list->tree_view), &alloc);
177  gint scroll_point = 0;
178  gchar *start_string = g_utf8_substring (item_string, 0, found_location + box->newval_len);
179 
180  PangoLayout *layout = gtk_widget_create_pango_layout (GTK_WIDGET(box->item_list->tree_view), item_string);
181  PangoAttrList *atlist = pango_attr_list_new ();
182  PangoAttribute *bold_weight = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
183  bold_weight->start_index = found_location;
184  bold_weight->end_index = found_location + box->newval_len;
185  pango_attr_list_insert (atlist, bold_weight);
186  pango_layout_set_attributes (layout, atlist);
187 
188  gint item_string_width = text_width (layout);
189 
190  pango_layout_set_text (layout, start_string, -1);
191 
192  gint start_string_width = text_width (layout);
193 
194  pango_attr_list_unref (atlist);
195  g_object_unref (layout);
196  g_free (start_string);
197 
198  if (item_string_width <= alloc.width)
199  scroll_point = 0;
200  else
201  scroll_point = start_string_width - alloc.width / 2;
202 
203  if (scroll_point < 0)
204  scroll_point = 0;
205 
206  gtk_tree_view_scroll_to_point (box->item_list->tree_view, scroll_point, -1);
207 }
208 
209 static void
210 change_item_cb (GncItemList* item_list, char* item_string, gpointer user_data)
211 {
212  CompletionCell* cell = user_data;
213  PopBox* box = cell->cell.gui_private;
214 
215  box->in_list_select = TRUE;
216  gnucash_sheet_modify_current_cell (box->sheet, item_string);
217  box->in_list_select = FALSE;
218 
219  GtkTreeModel *model = gtk_tree_view_get_model (item_list->tree_view);
220  GtkTreeSelection *selection = gtk_tree_view_get_selection (item_list->tree_view);
221  GtkTreeIter iter;
222  if (gtk_tree_selection_get_selected (selection, &model, &iter))
223  {
224  gint found_location;
225  gtk_tree_model_get (model, &iter, FOUND_LOCATION_COL, &found_location, -1);
226  horizontal_scroll_to_found_text (box, item_string, found_location);
227  }
228 }
229 
230 static void
231 activate_item_cb (GncItemList* item_list, char* item_string, gpointer user_data)
232 {
233  CompletionCell* cell = user_data;
234  PopBox* box = cell->cell.gui_private;
235  hide_popup (box);
236 }
237 
238 static void
239 block_list_signals (CompletionCell* cell)
240 {
241  PopBox* box = cell->cell.gui_private;
242 
243  if (!box->signals_connected)
244  return;
245 
246  g_signal_handlers_block_matched (G_OBJECT(box->item_list),
247  G_SIGNAL_MATCH_DATA,
248  0, 0, NULL, NULL, cell);
249 }
250 
251 static void
252 unblock_list_signals (CompletionCell* cell)
253 {
254  PopBox* box = cell->cell.gui_private;
255 
256  if (!box->signals_connected)
257  return;
258 
259  g_signal_handlers_unblock_matched (G_OBJECT(box->item_list),
260  G_SIGNAL_MATCH_DATA,
261  0, 0, NULL, NULL, cell);
262 }
263 
264 static void
265 key_press_item_cb (GncItemList* item_list, GdkEventKey* event, gpointer user_data)
266 {
267  CompletionCell* cell = user_data;
268  PopBox* box = cell->cell.gui_private;
269 
270  switch (event->keyval)
271  {
272  case GDK_KEY_Escape:
273  block_list_signals (cell); // Prevent recursion, unselect all
274  gnc_item_list_select (box->item_list, NULL);
275  unblock_list_signals (cell);
276  hide_popup (box);
277  break;
278 
279  default:
280  gtk_widget_event (GTK_WIDGET (box->sheet),
281  (GdkEvent*) event);
282  break;
283  }
284 }
285 
286 static void
287 completion_disconnect_signals (CompletionCell* cell)
288 {
289  PopBox* box = cell->cell.gui_private;
290 
291  if (!box->signals_connected)
292  return;
293 
294  g_signal_handlers_disconnect_matched (G_OBJECT(box->item_list),
295  G_SIGNAL_MATCH_DATA,
296  0, 0, NULL, NULL, cell);
297 
298  box->signals_connected = FALSE;
299 }
300 
301 static void
302 completion_connect_signals (CompletionCell* cell)
303 {
304  PopBox* box = cell->cell.gui_private;
305 
306  if (box->signals_connected)
307  return;
308 
309  g_signal_connect (G_OBJECT(box->item_list), "select_item",
310  G_CALLBACK(select_item_cb), cell);
311 
312  g_signal_connect (G_OBJECT(box->item_list), "change_item",
313  G_CALLBACK(change_item_cb), cell);
314 
315  g_signal_connect (G_OBJECT(box->item_list), "activate_item",
316  G_CALLBACK(activate_item_cb), cell);
317 
318  g_signal_connect (G_OBJECT(box->item_list), "key_press_event",
319  G_CALLBACK(key_press_item_cb), cell);
320 
321  box->signals_connected = TRUE;
322 }
323 
324 static void
325 gnc_completion_cell_gui_destroy (BasicCell* bcell)
326 {
327  CompletionCell* cell = (CompletionCell*) bcell;
328 
329  if (cell->cell.gui_realize)
330  {
331  PopBox* box = bcell->gui_private;
332  if (box && box->item_list)
333  {
334  completion_disconnect_signals (cell);
335  g_object_unref (box->item_list);
336  box->item_list = NULL;
337  }
338  /* allow the widget to be shown again */
339  cell->cell.gui_realize = gnc_completion_cell_gui_realize;
340  cell->cell.gui_move = NULL;
341  cell->cell.enter_cell = NULL;
342  cell->cell.leave_cell = NULL;
343  cell->cell.gui_destroy = NULL;
344  }
345 }
346 
347 static void
348 gnc_completion_cell_destroy (BasicCell* bcell)
349 {
350  CompletionCell* cell = (CompletionCell*) bcell;
351  PopBox* box = cell->cell.gui_private;
352 
353  gnc_completion_cell_gui_destroy (& (cell->cell));
354 
355  if (box)
356  {
357  if (box->item_hash)
358  g_hash_table_destroy (box->item_hash);
359 
360  g_free (box);
361  cell->cell.gui_private = NULL;
362  }
363  cell->cell.gui_private = NULL;
364  cell->cell.gui_realize = NULL;
365 }
366 
367 static gint
368 sort_func (GtkTreeModel* model, GtkTreeIter* iter_a, GtkTreeIter* iter_b, gpointer user_data)
369 {
370  gint a_weight, b_weight;
371  gint ret = 0;
372 
373  gtk_tree_model_get (model, iter_a, WEIGHT_COL, &a_weight, -1);
374  gtk_tree_model_get (model, iter_b, WEIGHT_COL, &b_weight, -1);
375 
376  if (a_weight < b_weight)
377  ret = -1;
378  else if (a_weight > b_weight)
379  ret = 1;
380 
381  return ret;
382 }
383 
384 void
386  gboolean enabled)
387 {
388  if (!cell)
389  return;
390 
391  PopBox* box = cell->cell.gui_private;
392  box->sort_enabled = enabled;
393 }
394 
395 static void
396 set_sort_column_enabled (PopBox* box, gboolean enable)
397 {
398  if (enable)
399  {
400  gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE(box->item_list->list_store),
401  WEIGHT_COL, sort_func, box->item_list, NULL);
402 
403  gnc_item_list_set_sort_column (box->item_list, WEIGHT_COL);
404  }
405  else
406  gnc_item_list_set_sort_column (box->item_list, GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID);
407 }
408 
409 static void
410 item_store_clear (CompletionCell* cell)
411 {
412  if (!cell)
413  return;
414 
415  PopBox* box = cell->cell.gui_private;
416 
417  // disconnect list store from tree view
418  GtkListStore *store = gnc_item_list_disconnect_store (box->item_list);
419 
420  block_list_signals (cell);
421 
422  if (box->sort_enabled) // if sorting, disable it
423  set_sort_column_enabled (box, FALSE);
424 
425  gtk_list_store_clear (box->item_store);
426 
427  if (box->sort_enabled) // if sorting, enable it
428  set_sort_column_enabled (box, TRUE);
429 
430  unblock_list_signals (cell);
431 
432  // reconect list store to tree view
433  gnc_item_list_connect_store (box->item_list, store);
434 
435  hide_popup (box);
436 }
437 
438 void
439 gnc_completion_cell_clear_menu (CompletionCell* cell)
440 {
441  if (!cell)
442  return;
443 
444  PopBox* box = cell->cell.gui_private;
445  if (!box)
446  return;
447 
448  if (box->item_list)
449  {
450  g_hash_table_remove_all (box->item_hash);
451  item_store_clear (cell);
452  box->occurrence = 0;
453  }
454 }
455 
456 void
458  const char* menustr)
459 {
460  if (!cell || !menustr)
461  return;
462 
463  PopBox* box = cell->cell.gui_private;
464 
465  if (box->item_hash)
466  {
467  gpointer value = g_hash_table_lookup (box->item_hash, menustr);
468  gboolean update = FALSE;
469  if (value)
470  {
471  if (!box->register_is_reversed)
472  update = TRUE;
473  }
474  else
475  update = TRUE;
476 
477  if (update)
478  {
479  g_hash_table_insert (box->item_hash, g_strdup (menustr),
480  GINT_TO_POINTER(box->occurrence));
481  }
482  box->occurrence++;
483  }
484 }
485 
486 void
487 gnc_completion_cell_set_value (CompletionCell* cell, const char* str)
488 {
489  if (!cell || !str)
490 
491  gnc_basic_cell_set_value (&cell->cell, str);
492 }
493 
494 static inline void
495 list_store_append (GtkListStore *store, char* string,
496  char* markup, gint weight, gint found_location)
497 {
498  GtkTreeIter iter;
499 
500  g_return_if_fail (store);
501  g_return_if_fail (string);
502  g_return_if_fail (markup);
503 
504  gtk_list_store_append (store, &iter);
505 
506  gtk_list_store_set (store, &iter, TEXT_COL, string,
507  TEXT_MARKUP_COL, markup,
508  WEIGHT_COL, weight,
509  FOUND_LOCATION_COL, found_location, -1);
510 }
511 
512 static gint
513 test_and_add (PopBox* box, const gchar *text, gint start_pos,
514  gpointer key, gint occurrence_difference)
515 {
516  gint ret_value = -1;
517  gint text_length = g_utf8_strlen (text, -1);
518 
519  if (start_pos >= text_length)
520  return ret_value;
521 
522  gchar *sub_text = g_utf8_substring (text, start_pos, text_length);
523  int pos = 0, len = 0;
524  if (gnc_unicode_has_substring_base_chars (box->newval, sub_text, &pos, &len))
525  {
526  gchar *markup = NULL, *prefix = NULL, *match = NULL, *suffix = NULL;
527  gint found_location = start_pos + pos;
528  gboolean have_boundary = FALSE;
529  gint prefix_length;
530  gint weight;
531 
532  if (found_location > 0)
533  prefix = g_utf8_substring (text, 0, found_location);
534  else
535  prefix = g_strdup ("");
536 
537  prefix_length = g_utf8_strlen (prefix, -1);
538 
539  match = g_utf8_substring (text, found_location, found_location + len);
540 
541  if (pos >= 1)
542  {
543  gunichar prev = g_utf8_get_char (g_utf8_offset_to_pointer (sub_text, pos - 1));
544  if (prev && (g_unichar_isspace (prev) || g_unichar_ispunct (prev)))
545  have_boundary = TRUE;
546  else
547  ret_value = found_location + 1;
548  }
549 
550  suffix = g_utf8_substring (text, found_location + len, text_length);
551 
552  markup = g_markup_printf_escaped ("%s<b>%s</b>%s%s", prefix, match, suffix, " ");
553 
554  if ((prefix_length == 0 ) || have_boundary)
555  {
556  weight = occurrence_difference; // sorted by recent first
557 
558  if (gnc_unicode_compare_base_chars (sub_text, box->newval) == 0) // exact match
559  weight = 1;
560 
561  list_store_append (box->item_store, key, markup, weight, found_location);
562  }
563  g_free (markup);
564  g_free (prefix);
565  g_free (match);
566  g_free (suffix);
567  }
568  g_free (sub_text);
569  return ret_value;
570 }
571 
572 static void
573 add_item (gpointer key, gpointer value, gpointer user_data)
574 {
575  PopBox* box = user_data;
576  gchar *hash_entry = g_strdup (key);
577 
578  if (hash_entry && *hash_entry)
579  {
580  gint start_pos = 0;
581  gint occurrence_difference;
583 
584  if (box->register_is_reversed)
585  occurrence_difference = GPOINTER_TO_INT(value) + 1;
586  else
587  occurrence_difference = box->occurrence - GPOINTER_TO_INT(value);
588 
589  do
590  {
591  start_pos = test_and_add (box, hash_entry, start_pos, key, occurrence_difference);
592  }
593  while (start_pos != -1);
594  }
595  g_free (hash_entry);
596 }
597 
598 static void
599 select_first_entry_in_list (PopBox* box)
600 {
601  GtkTreeModel *model = gtk_tree_view_get_model (box->item_list->tree_view);
602  GtkTreeIter iter;
603  gchar* string;
604 
605  if (!gtk_tree_model_get_iter_first (model, &iter))
606  return;
607 
608  if (!gtk_tree_model_iter_next (model, &iter))
609  return;
610 
611  gtk_tree_model_get (model, &iter, TEXT_COL, &string, -1);
612 
613  gnc_item_list_select (box->item_list, string);
614 
615  GtkTreePath* path = gtk_tree_path_new_first ();
616  gtk_tree_view_scroll_to_cell (box->item_list->tree_view,
617  path, NULL, TRUE, 0.5, 0.0);
618  gtk_tree_path_free (path);
619  g_free (string);
620 }
621 
622 static void
623 populate_list_store (CompletionCell* cell, gchar* str)
624 {
625  PopBox* box = cell->cell.gui_private;
626 
627  box->in_list_select = FALSE;
628  box->item_edit->popup_allocation_height = -1;
629 
630  if (str && *str)
631  box->newval = g_strdup(str);
632  else
633  return;
634 
635  box->newval_len = g_utf8_strlen (str, -1);
636 
637  // disconnect list store from tree view
638  box->item_store = gnc_item_list_disconnect_store (box->item_list);
639 
640  block_list_signals (cell);
641 
642  if (box->sort_enabled) // if sorting, disable it
643  set_sort_column_enabled (box, FALSE);
644 
645  gtk_list_store_clear (box->item_store);
646 
647  // add the don't first entry
648  gchar *markup = g_markup_printf_escaped ("<i>%s</i>", DONT_TEXT);
649  list_store_append (box->item_store, DONT_TEXT, markup, 0, 0);
650  g_free (markup);
651 
652  // add to the list store
653  g_hash_table_foreach (box->item_hash, add_item, box);
654 
655  if (box->sort_enabled) // if sorting, enable it
656  set_sort_column_enabled (box, TRUE);
657 
658  unblock_list_signals (cell);
659 
660  // reconnect list store to tree view
661  gnc_item_list_connect_store (box->item_list, box->item_store);
662 
663  // reset horizontal scrolling
664  gtk_tree_view_column_queue_resize (gtk_tree_view_get_column (
665  box->item_list->tree_view, TEXT_COL));
666 
667  // if no entries, do not show popup
668  if (gtk_tree_model_iter_n_children (GTK_TREE_MODEL(box->item_store), NULL) == 1)
669  {
670  hide_popup (box);
671  }
672  else
673  gnc_item_edit_show_popup (box->item_edit);
674 
675  block_list_signals (cell); // Prevent recursion, select first entry
676  select_first_entry_in_list (box);
677  unblock_list_signals (cell);
678 
679  g_free (box->newval);
680 }
681 
682 static void
683 gnc_completion_cell_modify_verify (BasicCell* bcell,
684  const char* change,
685  int change_len,
686  const char* newval,
687  int newval_len,
688  int* cursor_position,
689  int* start_selection,
690  int* end_selection)
691 {
692  CompletionCell* cell = (CompletionCell*) bcell;
693  PopBox* box = cell->cell.gui_private;
694 
695  if (box->in_list_select)
696  {
697  if (g_strcmp0 (newval, DONT_TEXT) == 0)
698  return;
699  gnc_basic_cell_set_value_internal (bcell, newval);
700  *cursor_position = -1;
701  *start_selection = 0;
702  *end_selection = 0;
703  return;
704  }
705 
706  // Are were deleting or inserting in the middle.
707  if (change == NULL || *cursor_position < bcell->value_chars)
708  *start_selection = *end_selection = *cursor_position;
709 
710  gchar *start_of_text = g_utf8_substring (newval, 0, *cursor_position);
711  populate_list_store (cell, start_of_text);
712  g_free (start_of_text);
713 
714  if (g_strcmp0 (newval, "") == 0)
715  {
716  block_list_signals (cell); // Prevent recursion, unselect all
717  gnc_item_list_select (box->item_list, NULL);
718  unblock_list_signals (cell);
719  hide_popup (box);
720  }
721  gnc_basic_cell_set_value_internal (bcell, newval);
722 }
723 
724 static char*
725 get_entry_from_hash_if_size_is_one (CompletionCell* cell)
726 {
727  if (!cell)
728  return NULL;
729 
730  PopBox* box = cell->cell.gui_private;
731 
732  if (box->item_hash && (g_hash_table_size (box->item_hash) == 1))
733  {
734  GList *keys = g_hash_table_get_keys (box->item_hash);
735  char *ret = g_strdup (keys->data);
736  g_list_free (keys);
737  return ret;
738  }
739  return NULL;
740 }
741 
742 static gboolean
743 gnc_completion_cell_direct_update (BasicCell* bcell,
744  int* cursor_position,
745  int* start_selection,
746  int* end_selection,
747  void* gui_data)
748 {
749  CompletionCell* cell = (CompletionCell*) bcell;
750  PopBox* box = cell->cell.gui_private;
751  GdkEventKey* event = gui_data;
752 
753  if (event->type != GDK_KEY_PRESS)
754  return FALSE;
755 
756  switch (event->keyval)
757  {
758  case GDK_KEY_Tab:
759  case GDK_KEY_ISO_Left_Tab:
760  {
761  if (event->state & GDK_CONTROL_MASK)
762  {
763  char* hash_string = get_entry_from_hash_if_size_is_one (cell);
764 
765  if (hash_string)
766  {
767  gnc_basic_cell_set_value_internal (bcell, hash_string);
768  *cursor_position = strlen (hash_string);
769  }
770  g_free (hash_string);
771  return TRUE;
772  }
773 
774  char* string = gnc_item_list_get_selection (box->item_list);
775 
776  if (!string)
777  break;
778 
779  g_signal_emit_by_name (G_OBJECT(box->item_list), "change_item",
780  string, (gpointer)bcell);
781 
782  g_free (string);
783  break;
784  }
785  }
786 
787  if (box->strict)
788  box->in_list_select = gnc_item_in_list (box->item_list, bcell->value);
789 
790  if (!bcell->value)
791  item_store_clear (cell);
792 
793  return FALSE;
794 }
795 
796 void
798 {
799  if (!cell)
800  return;
801 
802  PopBox* box = cell->cell.gui_private;
803 
804  if (is_reversed != box->register_is_reversed)
805  {
806  gnc_completion_cell_clear_menu (cell);
807  box->register_is_reversed = is_reversed;
808  box->occurrence = 0;
809  }
810 }
811 
812 static void
813 gnc_completion_cell_gui_realize (BasicCell* bcell, gpointer data)
814 {
815  GnucashSheet* sheet = data;
816  GncItemEdit* item_edit = gnucash_sheet_get_item_edit (sheet);
817  CompletionCell* cell = (CompletionCell*) bcell;
818  PopBox* box = cell->cell.gui_private;
819 
820  /* initialize gui-specific, private data */
821  box->sheet = sheet;
822  box->item_edit = item_edit;
823  box->item_list = GNC_ITEM_LIST(gnc_item_list_new (box->item_store));
824 
825  block_list_signals (cell);
826  set_sort_column_enabled (box, FALSE);
827  unblock_list_signals (cell);
828 
829  gtk_widget_show_all (GTK_WIDGET(box->item_list));
830  g_object_ref_sink (box->item_list);
831 
832  /* to mark cell as realized, remove the realize method */
833  cell->cell.gui_realize = NULL;
834  cell->cell.gui_move = gnc_completion_cell_gui_move;
835  cell->cell.enter_cell = gnc_completion_cell_enter;
836  cell->cell.leave_cell = gnc_completion_cell_leave;
837  cell->cell.gui_destroy = gnc_completion_cell_gui_destroy;
838  cell->cell.modify_verify = gnc_completion_cell_modify_verify;
839  cell->cell.direct_update = gnc_completion_cell_direct_update;
840 }
841 
842 static void
843 reset_item_list_to_default_setup (BasicCell* bcell)
844 {
845  PopBox* box = bcell->gui_private;
846  PopupToggle popup_toggle;
847 
848  item_store_clear ((CompletionCell*) bcell);
849 
850  popup_toggle = box->item_edit->popup_toggle;
851  gtk_widget_set_sensitive (GTK_WIDGET(popup_toggle.tbutton), TRUE);
852  gtk_widget_set_visible (GTK_WIDGET(popup_toggle.tbutton), TRUE);
853 
854  GtkTreeViewColumn *column = gtk_tree_view_get_column (
855  GTK_TREE_VIEW(box->item_list->tree_view), TEXT_COL);
856  gtk_tree_view_column_clear_attributes (column,box->item_list->renderer);
857  gtk_tree_view_column_add_attribute (column, box->item_list->renderer,
858  "text", TEXT_COL);
859  box->list_popped = FALSE;
860 }
861 
862 static void
863 gnc_completion_cell_gui_move (BasicCell* bcell)
864 {
865  PopBox* box = bcell->gui_private;
866 
867  completion_disconnect_signals ((CompletionCell*) bcell);
868 
869  gnc_item_edit_set_popup (box->item_edit, NULL, NULL,
870  NULL, NULL, NULL, NULL, NULL);
871 
872  reset_item_list_to_default_setup (bcell);
873 }
874 
875 static int
876 popup_get_height (GtkWidget* widget,
877  int space_available,
878  G_GNUC_UNUSED int row_height,
879  gpointer user_data)
880 {
881  PopBox* box = user_data;
882  GtkScrolledWindow* scrollwin = GNC_ITEM_LIST(widget)->scrollwin;
883  int height;
884 
885  // if popup_allocation_height set use that
886  if (box->item_edit->popup_allocation_height != -1)
887  height = box->item_edit->popup_allocation_height;
888  else
889  height = gnc_item_list_get_popup_height (GNC_ITEM_LIST(widget));
890 
891  if (height < space_available)
892  {
893  // if the list is empty height would be 0 so return 1 instead to
894  // satisfy the check_popup_height_is_true function
895  gint ret_height = height ? height : 1;
896 
897  gtk_widget_set_size_request (GTK_WIDGET(scrollwin), -1, ret_height);
898  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(scrollwin),
899  GTK_POLICY_AUTOMATIC, GTK_POLICY_NEVER);
900  return ret_height;
901  }
902  else
903  gtk_widget_set_size_request (GTK_WIDGET(scrollwin), -1, -1);
904 
905  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(scrollwin),
906  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
907  return space_available;
908 }
909 
910 static int
911 popup_autosize (GtkWidget* widget,
912  int max_width,
913  gpointer user_data)
914 {
915  PopBox* box = user_data;
916 
917  if (!box || !box->autosize)
918  return max_width;
919 
920  return gnc_item_list_autosize (GNC_ITEM_LIST(widget)) + 20;
921 }
922 
923 static void
924 popup_set_focus (GtkWidget* widget,
925  G_GNUC_UNUSED gpointer user_data)
926 {
927  /* An empty GtkTreeView grabbing focus causes the key_press events to be
928  * lost because there's no entry cell to handle them.
929  */
930  if (gnc_item_list_num_entries (GNC_ITEM_LIST(widget)))
931  gtk_widget_grab_focus (GTK_WIDGET (GNC_ITEM_LIST(widget)->tree_view));
932 }
933 
934 static void
935 popup_post_show (GtkWidget* widget,
936  G_GNUC_UNUSED gpointer user_data)
937 {
938  gnc_item_list_autosize (GNC_ITEM_LIST(widget));
939  gnc_item_list_show_selected (GNC_ITEM_LIST(widget));
940 }
941 
942 static int
943 popup_get_width (GtkWidget* widget,
944  G_GNUC_UNUSED gpointer user_data)
945 {
946  GtkAllocation alloc;
947  gtk_widget_get_allocation (GTK_WIDGET (GNC_ITEM_LIST(widget)->tree_view),
948  &alloc);
949  return alloc.width;
950 }
951 
952 static void
953 tree_view_size_allocate_cb (GtkWidget *widget,
954  G_GNUC_UNUSED GtkAllocation *allocation,
955  gpointer user_data)
956 {
957  GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(widget));
958  GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(widget));
959  GtkTreeIter iter;
960  if (gtk_tree_selection_get_selected (selection, &model, &iter))
961  {
962  PopBox* box = user_data;
963  gint found_location;
964  gchar *item_text;
965  gtk_tree_model_get (model, &iter, TEXT_COL, &item_text,
966  FOUND_LOCATION_COL, &found_location, -1);
967  horizontal_scroll_to_found_text (box, item_text, found_location);
968  g_free (item_text);
969  }
970 }
971 
972 static gboolean
973 gnc_completion_cell_enter (BasicCell* bcell,
974  int* cursor_position,
975  int* start_selection,
976  int* end_selection)
977 {
978  CompletionCell* cell = (CompletionCell*) bcell;
979  PopBox* box = bcell->gui_private;
980  PopupToggle popup_toggle;
981 
982  gnc_item_edit_set_popup (box->item_edit,
983  GTK_WIDGET(box->item_list),
984  popup_get_height, popup_autosize,
985  popup_set_focus, popup_post_show,
986  popup_get_width, box);
987 
988  popup_toggle = box->item_edit->popup_toggle;
989  gtk_widget_set_sensitive (GTK_WIDGET(popup_toggle.tbutton), FALSE);
990  gtk_widget_set_visible (GTK_WIDGET(popup_toggle.tbutton), FALSE);
991 
992  GtkTreeViewColumn *column = gtk_tree_view_get_column (
993  GTK_TREE_VIEW(box->item_list->tree_view), TEXT_COL);
994  gtk_tree_view_column_clear_attributes (column, box->item_list->renderer);
995  gtk_tree_view_column_add_attribute (column, box->item_list->renderer,
996  "markup", TEXT_MARKUP_COL);
997 
998  g_signal_connect (G_OBJECT(box->item_list->tree_view), "size-allocate",
999  G_CALLBACK(tree_view_size_allocate_cb), box);
1000 
1001  completion_connect_signals (cell);
1002 
1003  *cursor_position = -1;
1004  *start_selection = 0;
1005  *end_selection = -1;
1006 
1007  return TRUE;
1008 }
1009 
1010 static void
1011 gnc_completion_cell_leave (BasicCell* bcell)
1012 {
1013  PopBox* box = bcell->gui_private;
1014 
1015  completion_disconnect_signals ((CompletionCell*) bcell);
1016 
1017  gnc_item_edit_set_popup (box->item_edit, NULL, NULL,
1018  NULL, NULL, NULL, NULL, NULL);
1019 
1020  reset_item_list_to_default_setup (bcell);
1021 
1022  if (box->strict && !box->in_list_select)
1023  gnc_basic_cell_set_value_internal (bcell, "");
1024 }
1025 
1026 void
1028 {
1029  if (!cell)
1030  return;
1031 
1032  PopBox* box = cell->cell.gui_private;
1033  if (!box)
1034  return;
1035 
1036  box->strict = strict;
1037 }
1038 
1039 void
1041 {
1042  if (!cell)
1043  return;
1044 
1045  PopBox* box = cell->cell.gui_private;
1046  if (!box)
1047  return;
1048 
1049  box->autosize = autosize;
1050 }
The CompletionCell object implements a cell handler with a "combination-box" pull-down menu in it...
void gnc_completion_cell_reverse_sort(CompletionCell *cell, gboolean is_reversed)
Register the sort direction.
void gnc_completion_cell_set_sort_enabled(CompletionCell *cell, gboolean enabled)
Enable sorting of the menu item&#39;s contents.
void gnc_utf8_strip_invalid_and_controls(gchar *str)
Strip any non-utf8 characters and any control characters (everything < 0x20, , , ...
Public Declarations for GncItemList class.
Public declarations of GnucashRegister class.
Private declarations for GnucashSheet class.
GLib helper routines.
Generic api to store and retrieve preferences.
void gnc_completion_cell_add_menu_item(CompletionCell *cell, const char *menustr)
Add a menu item to the hash table list.
Public declarations for GncItemEdit class.
void gnc_completion_cell_set_strict(CompletionCell *cell, gboolean strict)
Determines whether the cell will accept strings not in the menu.
Declarations for the Table object.
void gnc_completion_cell_set_autosize(CompletionCell *cell, gboolean autosize)
Determines whether the popup list autosizes itself or uses all available space.
char * gnc_item_list_get_selection(GncItemList *item_list)
Retrieve the selected string from the item_list&#39;s active GtkListStore.