37 #include <gdk/gdkkeysyms.h> 39 #include "completioncell.h" 47 #include <gnc-unicode.h> 49 typedef struct _PopBox
55 GHashTable* item_hash;
56 GtkListStore* item_store;
61 gboolean signals_connected;
66 gboolean sort_enabled;
67 gboolean register_is_reversed;
70 gboolean in_list_select;
76 #define DONT_TEXT N_("Don't autocomplete") 79 enum GncCompletionColumn
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,
94 static void gnc_completion_cell_leave (BasicCell* bcell);
95 static void gnc_completion_cell_destroy (BasicCell* bcell);
98 gnc_completion_cell_new (
void)
101 gnc_completion_cell_init (cell);
108 gnc_basic_cell_init (& (cell->cell));
110 cell->cell.is_popup = TRUE;
112 cell->cell.destroy = gnc_completion_cell_destroy;
114 cell->cell.gui_realize = gnc_completion_cell_gui_realize;
115 cell->cell.gui_destroy = gnc_completion_cell_gui_destroy;
120 box->item_edit = NULL;
121 box->item_list = NULL;
122 box->item_store = NULL;
124 box->signals_connected = FALSE;
125 box->list_popped = FALSE;
126 box->autosize = FALSE;
127 box->register_is_reversed = FALSE;
129 box->sort_enabled = FALSE;
131 cell->cell.gui_private = box;
134 box->in_list_select = FALSE;
137 box->item_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
143 gnc_item_edit_hide_popup (box->item_edit);
144 box->list_popped = FALSE;
148 select_item_cb (
GncItemList* item_list,
char* item_string, gpointer user_data)
151 PopBox* box = cell->cell.gui_private;
153 box->in_list_select = TRUE;
154 gnucash_sheet_modify_current_cell (box->sheet, item_string);
155 box->in_list_select = FALSE;
161 text_width (PangoLayout *layout)
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;
170 horizontal_scroll_to_found_text (
PopBox* box,
char* item_string, gint found_location)
172 if (!gtk_widget_get_realized (GTK_WIDGET(box->item_list->tree_view)))
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);
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);
188 gint item_string_width = text_width (layout);
190 pango_layout_set_text (layout, start_string, -1);
192 gint start_string_width = text_width (layout);
194 pango_attr_list_unref (atlist);
195 g_object_unref (layout);
196 g_free (start_string);
198 if (item_string_width <= alloc.width)
201 scroll_point = start_string_width - alloc.width / 2;
203 if (scroll_point < 0)
206 gtk_tree_view_scroll_to_point (box->item_list->tree_view, scroll_point, -1);
210 change_item_cb (
GncItemList* item_list,
char* item_string, gpointer user_data)
213 PopBox* box = cell->cell.gui_private;
215 box->in_list_select = TRUE;
216 gnucash_sheet_modify_current_cell (box->sheet, item_string);
217 box->in_list_select = FALSE;
219 GtkTreeModel *model = gtk_tree_view_get_model (item_list->tree_view);
220 GtkTreeSelection *selection = gtk_tree_view_get_selection (item_list->tree_view);
222 if (gtk_tree_selection_get_selected (selection, &model, &iter))
225 gtk_tree_model_get (model, &iter, FOUND_LOCATION_COL, &found_location, -1);
226 horizontal_scroll_to_found_text (box, item_string, found_location);
231 activate_item_cb (
GncItemList* item_list,
char* item_string, gpointer user_data)
234 PopBox* box = cell->cell.gui_private;
241 PopBox* box = cell->cell.gui_private;
243 if (!box->signals_connected)
246 g_signal_handlers_block_matched (G_OBJECT(box->item_list),
248 0, 0, NULL, NULL, cell);
254 PopBox* box = cell->cell.gui_private;
256 if (!box->signals_connected)
259 g_signal_handlers_unblock_matched (G_OBJECT(box->item_list),
261 0, 0, NULL, NULL, cell);
265 key_press_item_cb (
GncItemList* item_list, GdkEventKey* event, gpointer user_data)
268 PopBox* box = cell->cell.gui_private;
270 switch (event->keyval)
273 block_list_signals (cell);
274 gnc_item_list_select (box->item_list, NULL);
275 unblock_list_signals (cell);
280 gtk_widget_event (GTK_WIDGET (box->sheet),
290 PopBox* box = cell->cell.gui_private;
292 if (!box->signals_connected)
295 g_signal_handlers_disconnect_matched (G_OBJECT(box->item_list),
297 0, 0, NULL, NULL, cell);
299 box->signals_connected = FALSE;
305 PopBox* box = cell->cell.gui_private;
307 if (box->signals_connected)
310 g_signal_connect (G_OBJECT(box->item_list),
"select_item",
311 G_CALLBACK(select_item_cb), cell);
313 g_signal_connect (G_OBJECT(box->item_list),
"change_item",
314 G_CALLBACK(change_item_cb), cell);
316 g_signal_connect (G_OBJECT(box->item_list),
"activate_item",
317 G_CALLBACK(activate_item_cb), cell);
319 g_signal_connect (G_OBJECT(box->item_list),
"key_press_event",
320 G_CALLBACK(key_press_item_cb), cell);
322 box->signals_connected = TRUE;
326 gnc_completion_cell_gui_destroy (BasicCell* bcell)
330 if (!cell->cell.gui_realize)
332 PopBox* box = bcell->gui_private;
337 completion_disconnect_signals (cell);
338 g_object_unref (box->item_list);
339 box->item_list = NULL;
343 g_object_unref (box->item_store);
344 box->item_store = NULL;
348 cell->cell.gui_realize = gnc_completion_cell_gui_realize;
349 cell->cell.gui_move = NULL;
350 cell->cell.enter_cell = NULL;
351 cell->cell.leave_cell = NULL;
352 cell->cell.gui_destroy = NULL;
357 gnc_completion_cell_destroy (BasicCell* bcell)
360 PopBox* box = cell->cell.gui_private;
362 gnc_completion_cell_gui_destroy (& (cell->cell));
367 g_hash_table_destroy (box->item_hash);
370 cell->cell.gui_private = NULL;
372 cell->cell.gui_private = NULL;
373 cell->cell.gui_realize = NULL;
377 sort_func (GtkTreeModel* model, GtkTreeIter* iter_a, GtkTreeIter* iter_b, gpointer user_data)
379 gint a_weight, b_weight;
382 gtk_tree_model_get (model, iter_a, WEIGHT_COL, &a_weight, -1);
383 gtk_tree_model_get (model, iter_b, WEIGHT_COL, &b_weight, -1);
385 if (a_weight < b_weight)
387 else if (a_weight > b_weight)
400 PopBox* box = cell->cell.gui_private;
401 box->sort_enabled = enabled;
405 set_sort_column_enabled (
PopBox* box, gboolean enable)
409 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE(box->item_list->list_store),
410 WEIGHT_COL, sort_func, box->item_list, NULL);
412 gnc_item_list_set_sort_column (box->item_list, WEIGHT_COL);
415 gnc_item_list_set_sort_column (box->item_list, GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID);
424 PopBox* box = cell->cell.gui_private;
427 gnc_item_list_disconnect_store (box->item_list);
429 block_list_signals (cell);
431 if (box->sort_enabled)
432 set_sort_column_enabled (box, FALSE);
434 gtk_list_store_clear (box->item_store);
436 if (box->sort_enabled)
437 set_sort_column_enabled (box, TRUE);
439 unblock_list_signals (cell);
442 gnc_item_list_connect_store (box->item_list, box->item_store);
453 PopBox* box = cell->cell.gui_private;
459 g_hash_table_remove_all (box->item_hash);
460 item_store_clear (cell);
469 if (!cell || !menustr)
472 PopBox* box = cell->cell.gui_private;
476 gpointer value = g_hash_table_lookup (box->item_hash, menustr);
477 gboolean update = FALSE;
480 if (!box->register_is_reversed)
488 g_hash_table_insert (box->item_hash, g_strdup (menustr),
489 GINT_TO_POINTER(box->occurrence));
496 gnc_completion_cell_set_value (
CompletionCell* cell,
const char* str)
501 gnc_basic_cell_set_value (&cell->cell, str);
505 list_store_append (GtkListStore *store,
char*
string,
506 char* markup, gint weight, gint found_location)
510 g_return_if_fail (store);
511 g_return_if_fail (
string);
512 g_return_if_fail (markup);
514 gtk_list_store_append (store, &iter);
516 gtk_list_store_set (store, &iter, TEXT_COL,
string,
517 TEXT_MARKUP_COL, markup,
519 FOUND_LOCATION_COL, found_location, -1);
523 test_and_add (
PopBox* box,
const gchar *text, gint start_pos,
524 gpointer key, gint occurrence_difference)
527 gint text_length = g_utf8_strlen (text, -1);
529 if (start_pos >= text_length)
532 gchar *sub_text = g_utf8_substring (text, start_pos, text_length);
533 int pos = 0, len = 0;
534 if (gnc_unicode_has_substring_base_chars (box->newval, sub_text, &pos, &len))
536 gchar *markup = NULL, *prefix = NULL, *match = NULL, *suffix = NULL;
537 gint found_location = start_pos + pos;
538 gboolean have_boundary = FALSE;
542 if (found_location > 0)
543 prefix = g_utf8_substring (text, 0, found_location);
545 prefix = g_strdup (
"");
547 prefix_length = g_utf8_strlen (prefix, -1);
549 match = g_utf8_substring (text, found_location, found_location + len);
553 gunichar prev = g_utf8_get_char (g_utf8_offset_to_pointer (sub_text, pos - 1));
554 if (prev && (g_unichar_isspace (prev) || g_unichar_ispunct (prev)))
555 have_boundary = TRUE;
557 ret_value = found_location + 1;
560 suffix = g_utf8_substring (text, found_location + len, text_length);
562 markup = g_markup_printf_escaped (
"%s<b>%s</b>%s%s", prefix, match, suffix,
" ");
564 if ((prefix_length == 0 ) || have_boundary)
566 weight = occurrence_difference;
568 if (gnc_unicode_compare_base_chars (sub_text, box->newval) == 0)
571 list_store_append (box->item_store, key, markup, weight, found_location);
583 add_item (gpointer key, gpointer value, gpointer user_data)
586 gchar *hash_entry = g_strdup (key);
588 if (hash_entry && *hash_entry)
591 gint occurrence_difference;
594 if (box->register_is_reversed)
595 occurrence_difference = GPOINTER_TO_INT(value) + 1;
597 occurrence_difference = box->occurrence - GPOINTER_TO_INT(value);
601 start_pos = test_and_add (box, hash_entry, start_pos, key, occurrence_difference);
603 while (start_pos != -1);
609 select_first_entry_in_list (
PopBox* box)
611 GtkTreeModel *model = gtk_tree_view_get_model (box->item_list->tree_view);
615 if (!gtk_tree_model_get_iter_first (model, &iter))
618 if (!gtk_tree_model_iter_next (model, &iter))
621 gtk_tree_model_get (model, &iter, TEXT_COL, &
string, -1);
623 gnc_item_list_select (box->item_list,
string);
625 GtkTreePath* path = gtk_tree_path_new_first ();
626 gtk_tree_view_scroll_to_cell (box->item_list->tree_view,
627 path, NULL, TRUE, 0.5, 0.0);
628 gtk_tree_path_free (path);
635 PopBox* box = cell->cell.gui_private;
637 box->in_list_select = FALSE;
638 box->item_edit->popup_allocation_height = -1;
641 box->newval = g_strdup(str);
645 box->newval_len = g_utf8_strlen (str, -1);
648 gnc_item_list_disconnect_store (box->item_list);
650 block_list_signals (cell);
652 if (box->sort_enabled)
653 set_sort_column_enabled (box, FALSE);
655 gtk_list_store_clear (box->item_store);
658 gchar *markup = g_markup_printf_escaped (
"<i>%s</i>", DONT_TEXT);
659 list_store_append (box->item_store, DONT_TEXT, markup, 0, 0);
663 g_hash_table_foreach (box->item_hash, add_item, box);
665 if (box->sort_enabled)
666 set_sort_column_enabled (box, TRUE);
668 unblock_list_signals (cell);
671 gnc_item_list_connect_store (box->item_list, box->item_store);
674 gtk_tree_view_column_queue_resize (gtk_tree_view_get_column (
675 box->item_list->tree_view, TEXT_COL));
678 if (gtk_tree_model_iter_n_children (GTK_TREE_MODEL(box->item_store), NULL) == 1)
683 gnc_item_edit_show_popup (box->item_edit);
685 block_list_signals (cell);
686 select_first_entry_in_list (box);
687 unblock_list_signals (cell);
689 g_free (box->newval);
693 gnc_completion_cell_modify_verify (BasicCell* bcell,
698 int* cursor_position,
699 int* start_selection,
703 PopBox* box = cell->cell.gui_private;
705 if (box->in_list_select)
707 if (g_strcmp0 (newval, DONT_TEXT) == 0)
709 gnc_basic_cell_set_value_internal (bcell, newval);
710 *cursor_position = -1;
711 *start_selection = 0;
717 if (change == NULL || *cursor_position < bcell->value_chars)
718 *start_selection = *end_selection = *cursor_position;
720 gchar *start_of_text = g_utf8_substring (newval, 0, *cursor_position);
721 populate_list_store (cell, start_of_text);
722 g_free (start_of_text);
724 if (g_strcmp0 (newval,
"") == 0)
726 block_list_signals (cell);
727 gnc_item_list_select (box->item_list, NULL);
728 unblock_list_signals (cell);
731 gnc_basic_cell_set_value_internal (bcell, newval);
740 PopBox* box = cell->cell.gui_private;
742 if (box->item_hash && (g_hash_table_size (box->item_hash) == 1))
744 GList *keys = g_hash_table_get_keys (box->item_hash);
745 char *ret = g_strdup (keys->data);
753 gnc_completion_cell_direct_update (BasicCell* bcell,
754 int* cursor_position,
755 int* start_selection,
760 PopBox* box = cell->cell.gui_private;
761 GdkEventKey*
event = gui_data;
763 if (event->type != GDK_KEY_PRESS)
766 switch (event->keyval)
771 const char *value = gnc_table_get_model_entry (box->sheet->table, bcell->cell_name);
773 gnc_basic_cell_set_value_internal (bcell, value);
774 bcell->changed = FALSE;
775 *cursor_position = 0;
776 *start_selection = 0;
782 case GDK_KEY_ISO_Left_Tab:
784 if (event->state & GDK_CONTROL_MASK)
786 char* hash_string = get_entry_from_hash_if_size_is_one (cell);
790 gnc_basic_cell_set_value_internal (bcell, hash_string);
791 *cursor_position = strlen (hash_string);
793 g_free (hash_string);
802 g_signal_emit_by_name (G_OBJECT(box->item_list),
"change_item",
803 string, (gpointer)bcell);
811 box->in_list_select = gnc_item_in_list (box->item_list, bcell->value);
814 item_store_clear (cell);
825 PopBox* box = cell->cell.gui_private;
827 if (is_reversed != box->register_is_reversed)
829 gnc_completion_cell_clear_menu (cell);
830 box->register_is_reversed = is_reversed;
836 gnc_completion_cell_gui_realize (BasicCell* bcell, gpointer data)
838 GnucashSheet* sheet = data;
839 GncItemEdit* item_edit = gnucash_sheet_get_item_edit (sheet);
841 PopBox* box = cell->cell.gui_private;
845 box->item_edit = item_edit;
846 box->item_store = gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_STRING,
847 G_TYPE_INT, G_TYPE_INT);
848 box->item_list = GNC_ITEM_LIST(gnc_item_list_new (box->item_store));
850 block_list_signals (cell);
851 set_sort_column_enabled (box, FALSE);
852 unblock_list_signals (cell);
854 gtk_widget_show_all (GTK_WIDGET(box->item_list));
855 g_object_ref_sink (box->item_list);
858 cell->cell.gui_realize = NULL;
859 cell->cell.gui_move = gnc_completion_cell_gui_move;
860 cell->cell.enter_cell = gnc_completion_cell_enter;
861 cell->cell.leave_cell = gnc_completion_cell_leave;
862 cell->cell.gui_destroy = gnc_completion_cell_gui_destroy;
863 cell->cell.modify_verify = gnc_completion_cell_modify_verify;
864 cell->cell.direct_update = gnc_completion_cell_direct_update;
868 reset_item_list_to_default_setup (BasicCell* bcell)
870 PopBox* box = bcell->gui_private;
871 PopupToggle popup_toggle;
875 popup_toggle = box->item_edit->popup_toggle;
876 gtk_widget_set_sensitive (GTK_WIDGET(popup_toggle.tbutton), TRUE);
877 gtk_widget_set_visible (GTK_WIDGET(popup_toggle.tbutton), TRUE);
879 GtkTreeViewColumn *column = gtk_tree_view_get_column (
880 GTK_TREE_VIEW(box->item_list->tree_view), TEXT_COL);
881 gtk_tree_view_column_clear_attributes (column,box->item_list->renderer);
882 gtk_tree_view_column_add_attribute (column, box->item_list->renderer,
884 box->list_popped = FALSE;
888 gnc_completion_cell_gui_move (BasicCell* bcell)
890 PopBox* box = bcell->gui_private;
894 gnc_item_edit_set_popup (box->item_edit, NULL, NULL,
895 NULL, NULL, NULL, NULL, NULL);
897 reset_item_list_to_default_setup (bcell);
901 popup_get_height (GtkWidget* widget,
903 G_GNUC_UNUSED
int row_height,
907 GtkScrolledWindow* scrollwin = GNC_ITEM_LIST(widget)->scrollwin;
911 if (box->item_edit->popup_allocation_height != -1)
912 height = box->item_edit->popup_allocation_height;
914 height = gnc_item_list_get_popup_height (GNC_ITEM_LIST(widget));
916 if (height < space_available)
920 gint ret_height = height ? height : 1;
922 gtk_widget_set_size_request (GTK_WIDGET(scrollwin), -1, ret_height);
923 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(scrollwin),
924 GTK_POLICY_AUTOMATIC, GTK_POLICY_NEVER);
928 gtk_widget_set_size_request (GTK_WIDGET(scrollwin), -1, -1);
930 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(scrollwin),
931 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
932 return space_available;
936 popup_autosize (GtkWidget* widget,
942 if (!box || !box->autosize)
945 return gnc_item_list_autosize (GNC_ITEM_LIST(widget)) + 20;
949 popup_set_focus (GtkWidget* widget,
950 G_GNUC_UNUSED gpointer user_data)
955 if (gnc_item_list_num_entries (GNC_ITEM_LIST(widget)))
956 gtk_widget_grab_focus (GTK_WIDGET (GNC_ITEM_LIST(widget)->tree_view));
960 popup_post_show (GtkWidget* widget,
961 G_GNUC_UNUSED gpointer user_data)
963 gnc_item_list_autosize (GNC_ITEM_LIST(widget));
964 gnc_item_list_show_selected (GNC_ITEM_LIST(widget));
968 popup_get_width (GtkWidget* widget,
969 G_GNUC_UNUSED gpointer user_data)
972 gtk_widget_get_allocation (GTK_WIDGET (GNC_ITEM_LIST(widget)->tree_view),
978 tree_view_size_allocate_cb (GtkWidget *widget,
979 G_GNUC_UNUSED GtkAllocation *allocation,
982 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(widget));
983 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(widget));
985 if (gtk_tree_selection_get_selected (selection, &model, &iter))
990 gtk_tree_model_get (model, &iter, TEXT_COL, &item_text,
991 FOUND_LOCATION_COL, &found_location, -1);
992 horizontal_scroll_to_found_text (box, item_text, found_location);
998 gnc_completion_cell_enter (BasicCell* bcell,
999 int* cursor_position,
1000 int* start_selection,
1004 PopBox* box = bcell->gui_private;
1005 PopupToggle popup_toggle;
1007 gnc_item_edit_set_popup (box->item_edit,
1008 GTK_WIDGET(box->item_list),
1009 popup_get_height, popup_autosize,
1010 popup_set_focus, popup_post_show,
1011 popup_get_width, box);
1013 popup_toggle = box->item_edit->popup_toggle;
1014 gtk_widget_set_sensitive (GTK_WIDGET(popup_toggle.tbutton), FALSE);
1015 gtk_widget_set_visible (GTK_WIDGET(popup_toggle.tbutton), FALSE);
1017 GtkTreeViewColumn *column = gtk_tree_view_get_column (
1018 GTK_TREE_VIEW(box->item_list->tree_view), TEXT_COL);
1019 gtk_tree_view_column_clear_attributes (column, box->item_list->renderer);
1020 gtk_tree_view_column_add_attribute (column, box->item_list->renderer,
1021 "markup", TEXT_MARKUP_COL);
1023 g_signal_connect (G_OBJECT(box->item_list->tree_view),
"size-allocate",
1024 G_CALLBACK(tree_view_size_allocate_cb), box);
1026 completion_connect_signals (cell);
1028 *cursor_position = -1;
1029 *start_selection = 0;
1030 *end_selection = -1;
1036 gnc_completion_cell_leave (BasicCell* bcell)
1038 PopBox* box = bcell->gui_private;
1042 gnc_item_edit_set_popup (box->item_edit, NULL, NULL,
1043 NULL, NULL, NULL, NULL, NULL);
1045 reset_item_list_to_default_setup (bcell);
1047 if (box->strict && !box->in_list_select)
1048 gnc_basic_cell_set_value_internal (bcell,
"");
1057 PopBox* box = cell->cell.gui_private;
1061 box->strict = strict;
1070 PopBox* box = cell->cell.gui_private;
1074 box->autosize = autosize;
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'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.
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's active GtkListStore.