32 #include <glib/gi18n.h> 33 #include <gdk/gdkkeysyms.h> 47 static guint gnc_item_list_signals[LAST_SIGNAL];
49 gboolean _gnc_item_find_selection (GtkTreeModel* model, GtkTreePath* path,
50 GtkTreeIter* iter, gpointer data);
52 G_DEFINE_TYPE (
GncItemList, gnc_item_list, GTK_TYPE_EVENT_BOX);
59 g_return_val_if_fail (item_list != NULL, 0);
60 g_return_val_if_fail (IS_GNC_ITEM_LIST (item_list), 0);
62 model = gnc_item_list_using_temp (item_list) ?
63 GTK_TREE_MODEL (item_list->temp_store) :
64 GTK_TREE_MODEL (item_list->list_store);
65 return gtk_tree_model_iter_n_children (model, NULL);
72 GtkTreeSelection* selection;
74 g_return_if_fail (IS_GNC_ITEM_LIST (item_list));
75 g_return_if_fail (item_list->list_store != NULL);
77 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (item_list->tree_view));
79 g_signal_handlers_block_matched (G_OBJECT (selection), G_SIGNAL_MATCH_DATA,
80 0, 0, NULL, NULL, item_list);
81 gtk_list_store_clear (item_list->list_store);
82 g_signal_handlers_unblock_matched (G_OBJECT (selection), G_SIGNAL_MATCH_DATA,
83 0, 0, NULL, NULL, item_list);
88 gnc_item_list_append (
GncItemList* item_list,
const char*
string)
92 g_return_if_fail (IS_GNC_ITEM_LIST (item_list));
93 g_return_if_fail (item_list->list_store != NULL);
94 g_return_if_fail (
string != NULL);
95 gtk_list_store_append (item_list->list_store, &iter);
96 gtk_list_store_set (item_list->list_store, &iter, 0,
string, -1);
101 gnc_item_list_set_sort_column (
GncItemList* item_list, gint column_id)
103 g_return_if_fail (IS_GNC_ITEM_LIST (item_list));
105 gtk_tree_sortable_set_sort_column_id
106 (GTK_TREE_SORTABLE (item_list->list_store),
112 typedef struct _findSelectionData
115 const char* string_to_find;
116 GtkTreePath* found_path;
120 _gnc_item_find_selection (GtkTreeModel* model, GtkTreePath* path,
121 GtkTreeIter* iter, gpointer data)
127 gtk_tree_model_get (model, iter, 0, &iterStr, -1);
128 found = g_strcmp0 (to_find->string_to_find, iterStr) == 0;
132 to_find->found_path = gtk_tree_path_copy (path);
139 gnc_item_in_list (
GncItemList* item_list,
const char*
string)
144 g_return_val_if_fail (item_list != NULL, FALSE);
145 g_return_val_if_fail (IS_GNC_ITEM_LIST (item_list), FALSE);
148 to_find_data->item_list = item_list;
149 to_find_data->string_to_find = string;
151 gtk_tree_model_foreach (GTK_TREE_MODEL (item_list->list_store),
152 _gnc_item_find_selection,
155 result = (to_find_data->found_path != NULL);
156 g_free (to_find_data);
162 gnc_item_list_select (
GncItemList* item_list,
const char*
string)
164 GtkTreeSelection* tree_sel = NULL;
167 g_return_if_fail (item_list != NULL);
168 g_return_if_fail (IS_GNC_ITEM_LIST (item_list));
170 tree_sel = gtk_tree_view_get_selection (item_list->tree_view);
174 gtk_tree_selection_unselect_all (tree_sel);
179 to_find_data->item_list = item_list;
180 to_find_data->string_to_find = string;
182 gtk_tree_model_foreach (GTK_TREE_MODEL (item_list->list_store),
183 _gnc_item_find_selection,
186 if (to_find_data->found_path != NULL)
188 gtk_tree_view_set_cursor (item_list->tree_view, to_find_data->found_path, NULL,
190 gtk_tree_path_free (to_find_data->found_path);
192 gnc_item_list_show_selected (item_list);
195 g_free (to_find_data);
206 GtkTreeSelection *selection =
207 gtk_tree_view_get_selection (item_list->tree_view);
208 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
211 gtk_tree_model_get (model, &iter, 0, &
string, -1);
217 gnc_item_list_show_selected (
GncItemList* item_list)
219 GtkTreeSelection* selection;
223 g_return_if_fail (item_list != NULL);
224 g_return_if_fail (IS_GNC_ITEM_LIST (item_list));
226 selection = gtk_tree_view_get_selection (item_list->tree_view);
228 if (gtk_tree_selection_get_selected (selection, &model, &iter))
230 GtkTreePath* path = gtk_tree_model_get_path (model, &iter);
232 gtk_tree_view_scroll_to_cell (item_list->tree_view,
233 path, NULL, TRUE, 0.5, 0.0);
234 gtk_tree_path_free (path);
241 g_return_val_if_fail (item_list != NULL, 0);
242 g_return_val_if_fail (IS_GNC_ITEM_LIST (item_list), 0);
248 gnc_item_list_set_temp_store (
GncItemList *item_list, GtkListStore *store)
251 g_return_if_fail (item_list != 0);
253 item_list->temp_store = store;
256 gtk_tree_view_set_model (item_list->tree_view,
257 GTK_TREE_MODEL (item_list->temp_store));
261 gtk_tree_view_set_model (item_list->tree_view,
262 GTK_TREE_MODEL (item_list->list_store));
263 item_list->temp_store = NULL;
270 return item_list && item_list->temp_store;
274 gnc_item_list_disconnect_store (
GncItemList *item_list)
278 g_return_val_if_fail (item_list != NULL, NULL);
280 store = GTK_LIST_STORE(gtk_tree_view_get_model (item_list->tree_view));
282 gtk_tree_view_set_model (item_list->tree_view, NULL);
288 gnc_item_list_connect_store (
GncItemList *item_list, GtkListStore *list_store)
290 g_return_if_fail (item_list != 0);
292 gtk_tree_view_set_model (item_list->tree_view,
293 GTK_TREE_MODEL (list_store));
299 item_list->scrollwin = NULL;
300 item_list->tree_view = NULL;
301 item_list->list_store = NULL;
302 item_list->temp_store = NULL;
303 item_list->cell_height = 0;
308 gnc_item_list_button_event (GtkWidget* widget, GdkEventButton* event,
318 g_return_val_if_fail (IS_GNC_ITEM_LIST (data), FALSE);
320 item_list = GNC_ITEM_LIST (data);
322 switch (event->button)
325 if (!gtk_tree_view_get_path_at_pos (item_list->tree_view,
336 gtk_tree_view_set_cursor (item_list->tree_view, path, NULL, FALSE);
338 model = GTK_TREE_MODEL (item_list->list_store);
339 success = gtk_tree_model_get_iter (model, &iter, path);
341 gtk_tree_path_free (path);
346 gtk_tree_model_get (model, &iter, 0, &
string, -1);
348 g_signal_emit (G_OBJECT (item_list),
349 gnc_item_list_signals[ACTIVATE_ITEM],
362 gnc_item_list_key_event (GtkWidget* widget, GdkEventKey* event, gpointer data)
368 switch (event->keyval)
374 g_signal_emit (G_OBJECT (item_list),
375 gnc_item_list_signals[ACTIVATE_ITEM],
378 g_signal_emit (G_OBJECT (item_list), gnc_item_list_signals[CHANGE_ITEM], 0,
383 case GDK_KEY_Page_Up:
384 case GDK_KEY_Page_Down:
388 case GDK_KEY_KP_Down:
389 case GDK_KEY_KP_Page_Up:
390 case GDK_KEY_KP_Page_Down:
396 g_signal_stop_emission_by_name (G_OBJECT (widget),
"key_press_event");
398 g_signal_emit_by_name (G_OBJECT (item_list),
"key_press_event", event,
408 GObjectClass* object_class = G_OBJECT_CLASS (item_list_class);
410 gtk_widget_class_set_css_name (GTK_WIDGET_CLASS(item_list_class),
"gnc-id-sheet-list");
412 gnc_item_list_signals[SELECT_ITEM] =
413 g_signal_new (
"select_item",
414 G_OBJECT_CLASS_TYPE (object_class),
418 g_cclosure_marshal_VOID__POINTER,
422 gnc_item_list_signals[CHANGE_ITEM] =
423 g_signal_new (
"change_item",
424 G_OBJECT_CLASS_TYPE (object_class),
428 g_cclosure_marshal_VOID__POINTER,
432 gnc_item_list_signals[ACTIVATE_ITEM] =
433 g_signal_new (
"activate_item",
434 G_OBJECT_CLASS_TYPE (object_class),
438 g_cclosure_marshal_VOID__POINTER,
442 item_list_class->select_item = NULL;
443 item_list_class->change_item = NULL;
444 item_list_class->activate_item = NULL;
448 tree_view_selection_changed (GtkTreeSelection* selection,
456 g_return_if_fail (data);
457 g_return_if_fail (selection);
459 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
462 gtk_tree_model_get (model, &iter, 0, &
string, -1);
464 g_signal_emit (G_OBJECT (item_list), gnc_item_list_signals[CHANGE_ITEM], 0,
472 gnc_item_list_get_cell_height (
GncItemList *item_list)
475 gint min_height, nat_height;
476 gtk_cell_renderer_get_preferred_height (item_list->renderer,
477 GTK_WIDGET(item_list->tree_view),
485 gnc_item_list_get_popup_height (
GncItemList *item_list)
487 GtkWidget *hsbar = gtk_scrolled_window_get_hscrollbar (GTK_SCROLLED_WINDOW(item_list->scrollwin));
488 GtkStyleContext *context = gtk_widget_get_style_context (hsbar);
491 gboolean overlay = gtk_style_context_has_class (context,
"overlay-indicator");
492 int count = gnc_item_list_num_entries (item_list);
493 int height = count * (gnc_item_list_get_cell_height (item_list) + 2);
498 gtk_widget_get_preferred_height (hsbar, &minh, &nath);
499 height = height + minh;
506 gnc_item_list_new (GtkListStore* list_store)
508 GtkWidget* tree_view;
509 GtkTreeViewColumn* column;
512 GNC_ITEM_LIST (g_object_new (GNC_TYPE_ITEM_LIST,
515 item_list->scrollwin =
516 GTK_SCROLLED_WINDOW (gtk_scrolled_window_new(NULL, NULL));
517 gtk_container_add (GTK_CONTAINER (item_list),
518 GTK_WIDGET (item_list->scrollwin));
520 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (item_list->scrollwin),
521 GTK_POLICY_AUTOMATIC,
522 GTK_POLICY_AUTOMATIC);
524 if (NULL == list_store)
525 list_store = gtk_list_store_new (1, G_TYPE_STRING);
527 g_object_ref (list_store);
528 tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
529 g_object_unref (list_store);
531 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE);
532 gtk_tree_selection_set_mode (
533 gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
534 GTK_SELECTION_BROWSE);
535 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (list_store),
536 0, GTK_SORT_ASCENDING);
538 item_list->renderer = gtk_cell_renderer_text_new();
539 column = gtk_tree_view_column_new_with_attributes (_ (
"List"),
543 gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
545 gtk_container_add (GTK_CONTAINER (item_list->scrollwin), tree_view);
547 item_list->tree_view = GTK_TREE_VIEW (tree_view);
548 item_list->list_store = list_store;
550 g_signal_connect (G_OBJECT (tree_view),
"button_press_event",
551 G_CALLBACK (gnc_item_list_button_event), item_list);
553 g_signal_connect (G_OBJECT (tree_view),
"key_press_event",
554 G_CALLBACK (gnc_item_list_key_event), item_list);
556 g_signal_connect (G_OBJECT (gtk_tree_view_get_selection (
557 GTK_TREE_VIEW (tree_view))),
"changed",
558 G_CALLBACK (tree_view_selection_changed), item_list);
560 return GTK_WIDGET (item_list);
Public Declarations for GncItemList class.
All type declarations for the whole Gnucash engine.
char * gnc_item_list_get_selection(GncItemList *item_list)
Retrieve the selected string from the item_list's active GtkListStore.