GnuCash  5.6-150-g038405b370+
Files | Data Structures
Gtk Utilities

The API in this file is designed to provide support functions that wrap the base gtk functions and make them easier to use. More...

Files

file  gnc-gtk-utils.h
 gtk helper routines.
 

Data Structures

struct  GncMenuModelSearch
 

gtk Miscellaneous Functions

void gnc_cbwe_set_by_string (GtkComboBox *cbwe, const gchar *text)
 Find an entry in the GtkComboBox by its text value, and set the widget to that value. More...
 
void gnc_cbwe_add_completion (GtkComboBox *cbwe)
 
void gnc_cbwe_require_list_item (GtkComboBox *cbwe)
 
gboolean gnc_is_dark_theme (GdkRGBA *fg_color)
 Return whether the current gtk theme is a dark one. More...
 
void gnc_style_context_get_background_color (GtkStyleContext *context, GtkStateFlags state, GdkRGBA *color)
 Wrapper to get the background color of a widget for a given state. More...
 
void gnc_style_context_get_border_color (GtkStyleContext *context, GtkStateFlags state, GdkRGBA *color)
 Wrapper to get the border color of a widget for a given state. More...
 
GtkWidget * gnc_get_dialog_widget_from_id (GtkDialog *dialog, const gchar *id)
 Find the Widget defined by 'id' in the dialog. More...
 
void gnc_disable_all_actions_in_group (GSimpleActionGroup *action_group)
 Disable all the actions in a simple action group. More...
 
void gnc_add_accelerator_keys_for_menu (GtkWidget *menu, GMenuModel *model, GtkAccelGroup *accel_group)
 Add accelerator keys for menu item widgets. More...
 
GtkWidget * gnc_find_menu_item_by_action_name (GtkWidget *menu, const gchar *action_name)
 Search the menu for the menu item based on action name. More...
 
GtkWidget * gnc_find_menu_item_by_action_label (GtkWidget *menu, const gchar *action_label)
 Search the menu for the menu item based on the action label. More...
 
GList * gnc_menu_get_items (GtkWidget *menu)
 Return a list of GtkMenuItems. More...
 
GtkWidget * gnc_find_toolbar_item (GtkWidget *toolbar, const gchar *action_name)
 Search the toolbar for the tool item based on the action name. More...
 
void gnc_menu_item_setup_tooltip_to_statusbar_callback (GtkWidget *menu_item, GtkWidget *statusbar)
 Setup the callbacks for menu bar items so the tooltip can be displayed in the status bar. More...
 
void gnc_tool_item_setup_tooltip_to_statusbar_callback (GtkWidget *tool_item, GtkWidget *statusbar)
 Setup the callbacks for tool bar items so the tooltip can be displayed in the status bar. More...
 
gboolean gnc_menubar_model_find_item (GMenuModel *menu_model, GncMenuModelSearch *gsm)
 Find a GtkMenu item from the action name. More...
 
GtkWidget * gnc_menubar_model_find_menu_item (GMenuModel *menu_model, GtkWidget *menu, const gchar *action_name)
 Find a GtkMenu item from the action name. More...
 
gboolean gnc_menubar_model_update_item (GMenuModel *menu_model, const gchar *action_name, const gchar *target, const gchar *label, const gchar *accel_name, const gchar *tooltip)
 Update the GMenuModel item based on the action name by copying existing item, removing it and inserting a new one in same location. More...
 
void gnc_menubar_model_remove_items_with_attrib (GMenuModel *menu_model, const gchar *attrib)
 Remove GMenuModel entries based on having an attribute value equal to attrib, it does not matter what the value is. More...
 
#define GNC_MENU_ATTRIBUTE_ACCELERATOR   "accel"
 
#define GNC_MENU_ATTRIBUTE_TOOLTIP   "tooltip"
 
#define GNC_MENU_ATTRIBUTE_TEMPORARY   "temp"
 

Detailed Description

The API in this file is designed to provide support functions that wrap the base gtk functions and make them easier to use.

Function Documentation

◆ gnc_add_accelerator_keys_for_menu()

void gnc_add_accelerator_keys_for_menu ( GtkWidget *  menu,
GMenuModel *  model,
GtkAccelGroup *  accel_group 
)

Add accelerator keys for menu item widgets.

Parameters
menuThe menu widget.
modelThe menu bar model.
accel_groupThe accelerator group to use.

Definition at line 431 of file gnc-gtk-utils.c.

432 {
433  g_return_if_fail (GTK_IS_WIDGET(menu));
434  g_return_if_fail (model != NULL);
435  g_return_if_fail (accel_group != NULL);
436 
437  // this updates the menu accelerators based on accelerator-map
438  gtk_accel_map_foreach (model, (GtkAccelMapForeach)accel_map_foreach_func);
439 
440  gtk_container_foreach (GTK_CONTAINER(menu), add_accel_for_menu_lookup, accel_group);
441 }

◆ gnc_cbwe_set_by_string()

void gnc_cbwe_set_by_string ( GtkComboBox *  cbwe,
const gchar *  text 
)

Find an entry in the GtkComboBox by its text value, and set the widget to that value.

This function also records the index of that text value for use when the user leaves the widget.

Parameters
cbweA pointer to a GtkComboBox with entry widget.
textThe entry text to find in the model of the combo box entry.

Definition at line 41 of file gnc-gtk-utils.c.

43 {
44  GtkTreeModel *model;
45  GtkTreeIter iter;
46  gchar *tree_string;
47  gint column, index, id;
48  gboolean match;
49 
50  model = gtk_combo_box_get_model(GTK_COMBO_BOX(cbwe));
51  if (!gtk_tree_model_get_iter_first(model, &iter))
52  {
53  /* empty tree */
54  gtk_combo_box_set_active(GTK_COMBO_BOX(cbwe), -1);
55  return;
56  }
57 
58  column = gtk_combo_box_get_entry_text_column(cbwe);
59  do
60  {
61  gtk_tree_model_get(model, &iter, column, &tree_string, -1);
62  match = g_utf8_collate(text, tree_string) == 0;
63  g_free(tree_string);
64  if (!match)
65  continue;
66 
67  /* Found a matching string */
68  id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(cbwe), CHANGED_ID));
69  g_signal_handler_block(cbwe, id);
70  gtk_combo_box_set_active_iter(GTK_COMBO_BOX(cbwe), &iter);
71  g_signal_handler_unblock(cbwe, id);
72 
73  index = gtk_combo_box_get_active(GTK_COMBO_BOX(cbwe));
74  g_object_set_data(G_OBJECT(cbwe), LAST_INDEX, GINT_TO_POINTER(index));
75  return;
76  }
77  while (gtk_tree_model_iter_next(model, &iter));
78 }

◆ gnc_disable_all_actions_in_group()

void gnc_disable_all_actions_in_group ( GSimpleActionGroup *  action_group)

Disable all the actions in a simple action group.

Parameters
action_groupThe GSimpleActionGroup

Definition at line 342 of file gnc-gtk-utils.c.

343 {
344  gchar **actions;
345  gint num_actions;
346 
347  g_return_if_fail (action_group != NULL);
348 
349  actions = g_action_group_list_actions (G_ACTION_GROUP(action_group));
350  num_actions = g_strv_length (actions);
351 
352  // Disable the actions
353  for (gint i = 0; i < num_actions; i++)
354  {
355  GAction *action = g_action_map_lookup_action (G_ACTION_MAP(action_group),
356  actions[i]);
357  g_simple_action_set_enabled (G_SIMPLE_ACTION(action), FALSE);
358  }
359  g_strfreev (actions);
360 }

◆ gnc_find_menu_item_by_action_label()

GtkWidget* gnc_find_menu_item_by_action_label ( GtkWidget *  menu,
const gchar *  action_label 
)

Search the menu for the menu item based on the action label.

Parameters
menuThe menu widget.
action_labelThe GtkMenuItem label.
Returns
The menu item widget or NULL.

Definition at line 528 of file gnc-gtk-utils.c.

529 {
530  GtkWidget *ret = NULL;
531  const gchar *action_name = NULL;
532 
533  g_return_val_if_fail (GTK_IS_WIDGET(menu), NULL);
534  g_return_val_if_fail (action_label != NULL, NULL);
535 
536  if (GTK_IS_CONTAINER(menu))
537  {
538  GList *container_list = gtk_container_get_children (GTK_CONTAINER(menu));
539  for (GList *n = container_list; !ret && n; n = n->next)
540  ret = find_menu_item_func (n->data, action_name, action_label);
541  g_list_free (container_list);
542  }
543  return ret;
544 }

◆ gnc_find_menu_item_by_action_name()

GtkWidget* gnc_find_menu_item_by_action_name ( GtkWidget *  menu,
const gchar *  action_name 
)

Search the menu for the menu item based on action name.

Parameters
menuThe menu widget.
action_nameThe GAction name.
Returns
The menu item widget or NULL.

Definition at line 500 of file gnc-gtk-utils.c.

501 {
502  GtkWidget *ret = NULL;
503  const gchar *action_label = NULL;
504 
505  g_return_val_if_fail (GTK_IS_WIDGET(menu), NULL);
506  g_return_val_if_fail (action_name != NULL, NULL);
507 
508  if (GTK_IS_CONTAINER(menu))
509  {
510  GList *container_list = gtk_container_get_children (GTK_CONTAINER(menu));
511  for (GList *n = container_list; !ret && n; n = n->next)
512  ret = find_menu_item_func (n->data, action_name, action_label);
513  g_list_free (container_list);
514  }
515  return ret;
516 }

◆ gnc_find_toolbar_item()

GtkWidget* gnc_find_toolbar_item ( GtkWidget *  toolbar,
const gchar *  action_name 
)

Search the toolbar for the tool item based on the action name.

Parameters
toolbarThe toolbar widget.
action_nameThe GAction name.
Returns
The tool item widget or NULL.

Definition at line 613 of file gnc-gtk-utils.c.

614 {
615  struct find_tool_item_struct ftis;
616 
617  g_return_val_if_fail (GTK_IS_TOOLBAR(toolbar), NULL);
618  g_return_val_if_fail (action_name != NULL, NULL);
619 
620  ftis.action_name = action_name;
621  ftis.found_tool_item = NULL;
622 
623  gtk_container_foreach (GTK_CONTAINER(toolbar), find_tool_action, &ftis);
624 
625  return ftis.found_tool_item;
626 }

◆ gnc_get_dialog_widget_from_id()

GtkWidget* gnc_get_dialog_widget_from_id ( GtkDialog *  dialog,
const gchar *  id 
)

Find the Widget defined by 'id' in the dialog.

Parameters
dialogThe dialog to search for 'id'.
idThe widget name to find in the dialog.
Returns
The widget defined by id in the dialog or NULL.

Definition at line 330 of file gnc-gtk-utils.c.

331 {
332  GtkWidget *content_area = gtk_dialog_get_content_area (dialog);
333  return find_widget_func (content_area, id);
334 }

◆ gnc_is_dark_theme()

gboolean gnc_is_dark_theme ( GdkRGBA *  fg_color)

Return whether the current gtk theme is a dark one.

A theme is considered "dark" if it has a dark background color with a light foreground color (used for text and so on). We only test on the foreground color assuming a sane theme chooses enough contrast between foreground and background colors.

Parameters
fg_colorThe foreground color to test.
Returns
TRUE if the theme is considered dark, FALSE otherwise.

Definition at line 236 of file gnc-gtk-utils.c.

237 {
238  gboolean is_dark = FALSE;
239 
240  // Counting the perceptive luminance - human eye favors green color...
241  double lightness = (0.299 * fg_color->red + 0.587 * fg_color->green + 0.114 * fg_color->blue);
242 
243  if (lightness > 0.5)
244  is_dark = TRUE;
245 
246  return is_dark;
247 }

◆ gnc_menu_get_items()

GList* gnc_menu_get_items ( GtkWidget *  menu)

Return a list of GtkMenuItems.

Parameters
menuThe menu widget.
Returns
A GList of menu items or NULL.

Definition at line 571 of file gnc-gtk-utils.c.

572 {
573  GList *list = NULL;
574 
575  g_return_val_if_fail (GTK_IS_WIDGET(menu), NULL);
576 
577  gtk_container_foreach (GTK_CONTAINER(menu), menu_item_list, &list);
578 
579  return list;
580 }

◆ gnc_menu_item_setup_tooltip_to_statusbar_callback()

void gnc_menu_item_setup_tooltip_to_statusbar_callback ( GtkWidget *  menu_item,
GtkWidget *  statusbar 
)

Setup the callbacks for menu bar items so the tooltip can be displayed in the status bar.

Parameters
menu_itemThe menubar menu item widget.
statusbarThe statusbar widget to display the tooltip.

Definition at line 1053 of file gnc-gtk-utils.c.

1055 {
1056  g_return_if_fail (menu_item != NULL);
1057  g_return_if_fail (statusbar != NULL);
1058 
1059  if (GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menu_item), "added-callbacks")))
1060  return;
1061 
1062  g_signal_connect (menu_item, "select",
1063  G_CALLBACK(menu_item_select_cb),
1064  statusbar);
1065  g_signal_connect (menu_item, "deselect",
1066  G_CALLBACK(menu_item_deselect_cb),
1067  statusbar);
1068  g_object_set (G_OBJECT(menu_item), "has-tooltip", FALSE, NULL);
1069 
1070  g_object_set_data (G_OBJECT(menu_item), "added-callbacks", GINT_TO_POINTER(1));
1071 }

◆ gnc_menubar_model_find_item()

gboolean gnc_menubar_model_find_item ( GMenuModel *  menu_model,
GncMenuModelSearch *  gsm 
)

Find a GtkMenu item from the action name.

This is done by first finding the action name in the GMenuModel and then doing a search for the label text in the GtkMenu.

NOTE: This is done this way as the action_name field of the GtkMenuItem is not populated from the model.

Parameters
menu_modelThe GMenuModel of the menu.
gsmThe GncMenuModelSearch structure.
Returns
TRUE if GMenuModel item found or FALSE if not.

Definition at line 745 of file gnc-gtk-utils.c.

746 {
747 
748  g_return_val_if_fail (menu_model != NULL, FALSE);
749  g_return_val_if_fail (gsm != NULL, FALSE);
750 
751  gsm->model = NULL;
752 
753  items_from_model (menu_model, gsm);
754 
755  if (gsm->model)
756  return TRUE;
757 
758  return FALSE;
759 }

◆ gnc_menubar_model_find_menu_item()

GtkWidget* gnc_menubar_model_find_menu_item ( GMenuModel *  menu_model,
GtkWidget *  menu,
const gchar *  action_name 
)

Find a GtkMenu item from the action name.

This is done by first finding the action name in the GMenuModel and then doing a search for the label text in the GtkMenu.

NOTE: This is done this way as the action_name field of the GtkMenuItem is not populated from the model.

Parameters
menu_modelThe GMenuModel of the menu.
menuThe GtkMenu built from the model.
action_nameThe action name of the menu item to find.
Returns
The GtkMenuItem if found or NULL

Definition at line 778 of file gnc-gtk-utils.c.

779 {
780  GncMenuModelSearch *gsm;
781  GtkWidget *menu_item = NULL;
782 
783  g_return_val_if_fail (menu_model != NULL, NULL);
784  g_return_val_if_fail (menu != NULL, NULL);
785  g_return_val_if_fail (action_name != NULL, NULL);
786 
787  gsm = g_new0 (GncMenuModelSearch, 1);
788 
789  gsm->search_action_label = NULL;
790  gsm->search_action_name = action_name;
791  gsm->search_action_target = NULL;
792 
793  if (gnc_menubar_model_find_item (menu_model, gsm))
794  menu_item = gnc_find_menu_item_by_action_label (menu, gsm->search_action_label);
795 
796  g_free (gsm);
797  return menu_item;
798 }
gboolean gnc_menubar_model_find_item(GMenuModel *menu_model, GncMenuModelSearch *gsm)
Find a GtkMenu item from the action name.
GtkWidget * gnc_find_menu_item_by_action_label(GtkWidget *menu, const gchar *action_label)
Search the menu for the menu item based on the action label.

◆ gnc_menubar_model_remove_items_with_attrib()

void gnc_menubar_model_remove_items_with_attrib ( GMenuModel *  menu_model,
const gchar *  attrib 
)

Remove GMenuModel entries based on having an attribute value equal to attrib, it does not matter what the value is.

Parameters
menu_modelThe GMenuModel of the menu.
attribThe attribute to look for.

Definition at line 987 of file gnc-gtk-utils.c.

988 {
989  GList *remove_list = NULL;
990 
991  g_return_if_fail (menu_model != NULL);
992  g_return_if_fail (attrib != NULL);
993 
994  remove_items_from_model (menu_model, &remove_list, attrib);
995 
996  g_list_foreach (remove_list, (GFunc)remove_items, NULL);
997  g_list_free (remove_list);
998 }

◆ gnc_menubar_model_update_item()

gboolean gnc_menubar_model_update_item ( GMenuModel *  menu_model,
const gchar *  action_name,
const gchar *  target,
const gchar *  label,
const gchar *  accel_name,
const gchar *  tooltip 
)

Update the GMenuModel item based on the action name by copying existing item, removing it and inserting a new one in same location.

Parameters
menu_modelThe GMenuModel of the menu.
action_nameThe action name to update.
targetThe action target if required, else NULL.
labelThe new menu label text.
accel_nameThe accelerator string
tooltipThe new tooltip text if any.
Returns
TRUE if item found and updated or FALSE if not.

Definition at line 819 of file gnc-gtk-utils.c.

822 {
823  GncMenuModelSearch *gsm;
824  gboolean found = FALSE;
825 
826  g_return_val_if_fail (menu_model != NULL, FALSE);
827  g_return_val_if_fail (action_name != NULL, FALSE);
828 
829  gsm = g_new0 (GncMenuModelSearch, 1);
830 
831  gsm->search_action_label = NULL;
832  gsm->search_action_name = action_name;
833  gsm->search_action_target = target;
834 
835  if (gnc_menubar_model_find_item (menu_model, gsm))
836  {
837  GMenuAttributeIter *iter;
838  const gchar *key;
839  GVariant *value;
840  GVariant *old_target = NULL;
841  const gchar *old_action = NULL;
842  const gchar *old_temp = NULL;
843  const gchar *old_accel = NULL;
844  const gchar *old_tooltip = NULL;
845 
846  iter = g_menu_model_iterate_item_attributes (gsm->model, gsm->index);
847  while (g_menu_attribute_iter_get_next (iter, &key, &value))
848  {
849  if (g_str_equal (key, GNC_MENU_ATTRIBUTE_TEMPORARY) &&
850  g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
851  old_temp = g_variant_get_string (value, NULL);
852  else if (g_str_equal (key, G_MENU_ATTRIBUTE_ACTION) &&
853  g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
854  old_action = g_variant_get_string (value, NULL);
855  else if (g_str_equal (key, GNC_MENU_ATTRIBUTE_ACCELERATOR) &&
856  g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
857  old_accel = g_variant_get_string (value, NULL);
858  else if (g_str_equal (key, GNC_MENU_ATTRIBUTE_TOOLTIP) &&
859  g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
860  old_tooltip = g_variant_get_string (value, NULL);
861  else if (g_str_equal (key, G_MENU_ATTRIBUTE_TARGET))
862  old_target = g_variant_ref (value);
863 
864  g_variant_unref (value);
865  }
866  g_object_unref (iter);
867 
868  if (!label && !gsm->search_action_label)
869  {
870  if (old_target)
871  g_variant_unref (old_target);
872 
873  g_free (gsm);
874  return found;
875  }
876 
877  if ((accel_name && g_strcmp0 (old_accel, accel_name) != 0) ||
878  (tooltip && g_strcmp0 (old_tooltip, tooltip) != 0) ||
879  (label && g_strcmp0 (gsm->search_action_label, label) != 0))
880  {
881  GMenuItem *item = NULL;
882 
883  if (label)
884  item = g_menu_item_new (label, old_action);
885  else
886  item = g_menu_item_new (gsm->search_action_label, old_action);
887 
888  if (tooltip)
889  g_menu_item_set_attribute (item, GNC_MENU_ATTRIBUTE_TOOLTIP, "s", tooltip);
890  else
891  {
892  if (old_tooltip)
893  g_menu_item_set_attribute (item, GNC_MENU_ATTRIBUTE_TOOLTIP, "s", old_tooltip);
894  }
895  if (accel_name)
896  g_menu_item_set_attribute (item, GNC_MENU_ATTRIBUTE_ACCELERATOR, "s", accel_name);
897  else
898  {
899  if (old_accel)
900  g_menu_item_set_attribute (item, GNC_MENU_ATTRIBUTE_ACCELERATOR, "s", old_accel);
901  }
902  if (old_temp)
903  g_menu_item_set_attribute (item, GNC_MENU_ATTRIBUTE_TEMPORARY, "s", old_temp);
904 
905  if (old_target)
906  g_menu_item_set_attribute_value (item, G_MENU_ATTRIBUTE_TARGET, old_target);
907 
908  g_menu_remove (G_MENU(gsm->model), gsm->index);
909  g_menu_insert_item (G_MENU(gsm->model), gsm->index, item);
910  g_object_unref (item);
911  found = TRUE;
912  }
913  if (old_target)
914  g_variant_unref (old_target);
915  }
916  g_free (gsm);
917  return found;
918 }
gboolean gnc_menubar_model_find_item(GMenuModel *menu_model, GncMenuModelSearch *gsm)
Find a GtkMenu item from the action name.

◆ gnc_style_context_get_background_color()

void gnc_style_context_get_background_color ( GtkStyleContext *  context,
GtkStateFlags  state,
GdkRGBA *  color 
)

Wrapper to get the background color of a widget for a given state.

Parameters
contextStyle context of widget.
stateThe stateflag of the widget.
colorThe returned background color of the widget.

Definition at line 258 of file gnc-gtk-utils.c.

261 {
262  GdkRGBA *c;
263 
264  g_return_if_fail (color != NULL);
265  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
266 
267  gtk_style_context_get (context,
268  state,
269  GTK_STYLE_PROPERTY_BACKGROUND_COLOR, &c,
270  NULL);
271  *color = *c;
272  gdk_rgba_free (c);
273 }

◆ gnc_style_context_get_border_color()

void gnc_style_context_get_border_color ( GtkStyleContext *  context,
GtkStateFlags  state,
GdkRGBA *  color 
)

Wrapper to get the border color of a widget for a given state.

Parameters
contextStyle context of widget.
stateThe stateflag of the widget.
colorThe returned border color of the widget.

Definition at line 284 of file gnc-gtk-utils.c.

287 {
288  GdkRGBA *c;
289 
290  g_return_if_fail (color != NULL);
291  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
292 
293  gtk_style_context_get (context,
294  state,
295  GTK_STYLE_PROPERTY_BORDER_COLOR, &c,
296  NULL);
297  *color = *c;
298  gdk_rgba_free (c);
299 }

◆ gnc_tool_item_setup_tooltip_to_statusbar_callback()

void gnc_tool_item_setup_tooltip_to_statusbar_callback ( GtkWidget *  tool_item,
GtkWidget *  statusbar 
)

Setup the callbacks for tool bar items so the tooltip can be displayed in the status bar.

Parameters
tool_itemThe toolbar tool item widget.
statusbarThe statusbar widget to display the tooltip.

Definition at line 1101 of file gnc-gtk-utils.c.

1103 {
1104  GtkWidget *child;
1105 
1106  g_return_if_fail (tool_item != NULL);
1107  g_return_if_fail (statusbar != NULL);
1108 
1109  child = gtk_bin_get_child (GTK_BIN(tool_item));
1110 
1111  gtk_widget_add_events (GTK_WIDGET(child),
1112  GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK
1113  | GDK_FOCUS_CHANGE_MASK);
1114 
1115  g_signal_connect (child, "enter-notify-event",
1116  G_CALLBACK (tool_item_enter_event),
1117  statusbar);
1118 
1119  g_signal_connect (child, "leave-notify-event",
1120  G_CALLBACK (tool_item_leave_event),
1121  statusbar);
1122 
1123  g_object_set (G_OBJECT(tool_item), "has-tooltip", FALSE, NULL);
1124 }