GnuCash  4.11-11-ge9df8d41d2+
Files
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.
 

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...
 

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_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_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 foregrond 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_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 }