aboutsummaryrefslogtreecommitdiff
path: root/patches/button-menu-icons.c
blob: 471a0d07a286161557d68a2dcc66f5b7fe800d0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/* Button / Menu Icons:
 * (from gtk3-classic patch 'appearance__buttons-menus-icons.patch')
 *
 * Applies appropriate icons to buttons and menus as if they were stock buttons
 * or menu items.
 *
 * Enable the icons by adding to your settings.ini file:
 *
 *    gtk-button-images = 1
 *    gtk-menu-images = 1
 *
 * Some desktop environments may set these properties via their own appearance
 * preferences configuration tool; check for such an option or set of options
 * first.
 */


#include <gtk/gtk.h>
#include <glib.h>

#include <stdlib.h>
#include <string.h>

#include <gtk3-classic.h>


#undef  G_LOG_DOMAIN
#define G_LOG_DOMAIN	"Gtk3-Classic::Button-Menu-Icons"


/*
 * Most of the button / menu labels and their associated icons listed herein
 * are taken from the gtkstock.h header file which comes with GTK, with the
 * exception that most of the  GTK-specific icon names have been replaced with
 * their generic FreeDesktop equivalents.  Other entries in this table may have
 * been extracted from the original patch from gtk3-classic.
 */

#define ICON(_label, _icon_name) \
 { .label = _label, .icon_name = _icon_name }

typedef struct
{
	const char *	label;
	const char *	translated_label;
	const char *	icon_name;
} Icon;

static int	icons_list_length;
static Icon	icons[] =
{
	ICON ("_About",		"help-about"),
	ICON ("_Add",		"list-add"),
	ICON ("_Apply",		"gtk-apply"),
	ICON ("_Ascending",	"view-sort-ascending"),
	ICON ("Authentication",	"dialog-authentication"),
	ICON ("_Back",		"back"),
	ICON ("Best _Fit",	"zoom-fit-best"),
	ICON ("_Bold",		"format-text-bold"),
	ICON ("_Bottom",	"bottom"),
	ICON ("_Cancel",	"gtk-cancel"),
	ICON ("_CD-ROM",	"media-optical"),
	ICON ("_Center",	"format-justify-center"),
	ICON ("_Clear",		"edit-clear"),
	ICON ("_Close",		"window-close"),
	ICON ("_Color",		"gtk-select-color"),
	ICON ("Color Selection","gtk-select-color"),
	ICON ("_Connect",	"gtk-connect"),
	ICON ("_Convert",	"gtk-convert"),
	ICON ("_Copy",		"edit-copy"),
	ICON ("Copy _Link Address", "edit-copy"),
	ICON ("Copy URL",	"edit-copy"),
	ICON ("_Credits",	"help-about"),
	ICON ("C_ustomize",	"gtk-edit"),
	ICON ("Cu_t",		"edit-cut"),
	ICON ("_Delete",	"edit-delete"),
	ICON ("_Descending",	"view-sort-descending"),
	ICON ("_Discard",	"edit-delete"),
	ICON ("_Disconnect",	"gtk-disconnect"),
	ICON ("_Down",		"down"),
	ICON ("_Edit",		"gtk-edit"),
	ICON ("Error",		"dialog-error"),
	ICON ("_Execute",	"gtk-execute"),
	ICON ("_File",		"text-x-generic"),
	ICON ("_Fill",		"format-justify-fill"),
	ICON ("_Find",		"edit-find"),
	ICON ("Find and _Replace", "edit-find-replace"),
	ICON ("_First",		"go-first"),
	ICON ("Fol_der",	"folder"),
	ICON ("_Font",		"gtk-select-font"),
	ICON ("Font Selection","gtk-select-font"),
	ICON ("_Forward",	"forward"),
	ICON ("_Fullscreen",	"view-fullscreen"),
	ICON ("_Hard Disk",	"media-harddisk"),
	ICON ("_Help",		"help-contents"),
	ICON ("_Home",		"go-home"),
	ICON ("Image",		"image-x-generic"),
	ICON ("Indent",		"format-indent-more"),
	ICON ("_Info",		"dialog-information"),
	ICON ("_Information",	"dialog-information"),
	ICON ("Insert _Emoji",	"face-smile"),
	ICON ("_Italic",	"format-text-italic"),
	ICON ("_Jump to",	"go-jump"),
	ICON ("Landscape",	"gtk-orientation-landscape"),
	ICON ("_Last",		"go-last"),
	ICON ("_Leave Fullscreen", "view-restore"),
	ICON ("_Left",		"format-justify-left"),
	ICON ("_License",	"text-x-generic"),
	ICON ("Lock",		"changes-prevent"),
	ICON ("_Network",	"network"),
	ICON ("Networks",	"network"),
	ICON ("_New",		"document-new"),
	ICON ("_Next",		"forward"),
	ICON ("_No",		"gtk-no"),
	ICON ("_Normal Size",	"zoom-original"),
	ICON ("_OK",		"gtk-ok"),
	ICON ("_Open",		"document-open"),
	ICON ("_Open Link",	"go-jump"),
	ICON ("Page Set_up",	"document-page-setup"),
	ICON ("_Password",	"dialog-authentication"),
	ICON ("_Paste",		"edit-paste"),
	ICON ("P_ause",		"media-playback-pause"),
	ICON ("Pick a Color",	"gtk-select-color"),
	ICON ("Pick a Font",	"gtk-select-font"),
	ICON ("_Play",		"media-playback-play"),
	ICON ("Portrait",	"gtk-orientation-portrait"),
	ICON ("_Preferences",	"preferences-system"),
	ICON ("Pre_view",	"document-print-preview"),
	ICON ("Pre_vious",	"back"),
	ICON ("_Print",		"document-print"),
	ICON ("Print Pre_view",	"document-print-preview"),
	ICON ("_Properties",	"document-properties"),
	ICON ("_Question",	"dialog-question"),
	ICON ("_Quit",		"application-exit"),
	ICON ("_Record",	"media-record"),
	ICON ("_Redo",		"edit-redo"),
	ICON ("_Refresh",	"view-refresh"),
	ICON ("_Remove",	"list-remove"),
	ICON ("_Replace",	"edit-find-replace"),
	ICON ("Reverse landscape", "gtk-orientation-reverse-landscape"),
	ICON ("Reverse portrait", "gtk-orientation-reverse-portrait"),
	ICON ("_Revert",	"document-revert"),
	ICON ("R_ewind",	"media-seek-backward"),
	ICON ("_Right",		"format-justify-right"),
	ICON ("_Save",		"document-save"),
	ICON ("Save _As",	"document-save-as"),
	ICON ("Search",		"system-search"),
	ICON ("_Select",	"gtk-ok"),
	ICON ("Select _All",	"edit-select-all"),
	ICON ("Select a Color",	"gtk-select-color"),
	ICON ("Select a Font",	"gtk-select-font"),
	ICON ("_Spell Check",	"tools-check-spelling"),
	ICON ("_Stop",		"process-stop"),
	ICON ("_Strikethrough",	"format-text-strikethrough"),
	ICON ("_Top",		"top"),
	ICON ("_Undelete",	"gtk-undelete"),
	ICON ("_Underline",	"format-text-underline"),
	ICON ("_Undo",		"edit-undo"),
	ICON ("Unindent",	"format-indent-less"),
	ICON ("Unlock",		"changes-allow"),
	ICON ("_Up",		"up"),
	ICON ("Warning",	"dialog-warning"),
	ICON ("_Yes",		"gtk-yes"),
	ICON ("Zoom _In",	"zoom-in"),
	ICON ("Zoom _Out",	"zoom-out"),
	{ NULL, NULL, NULL }
};

static int sort_icon_labels	(const void * icon1, const void * icon2);
static int search_icon_labels	(const void * key, const void * icon);


INTERCEPTED_CLASS_METHOD (gtk_button, notify,
			  (GObject *	object,
			   GParamSpec *	pspec),
			  void)


INTERCEPTED_CLASS_METHOD (gtk_menu_item, constructor,
			  (GType			type,
			   guint			n_construct_properties,
			   GObjectConstructParam *	construct_params),
			  GObject *)

INTERCEPTED_CLASS_METHOD (gtk_image_menu_item, parent_set,
			  (GtkWidget *	widget,
			   GtkWidget *	previous_parent),
			  void)

INTERCEPTED_CLASS_METHOD (gtk_image_menu_item, set_label,
			  (GtkMenuItem *	menu_item,
			   const gchar *	label),
			  void)


void button_menu_icons_init ()
{
	{
		GtkButtonClass * gtk_button_class =
		 g_type_class_ref (GTK_TYPE_BUTTON);

		INTERCEPT_CLASS_METHOD (gtk_button, G_OBJECT_CLASS, notify)

		g_type_class_unref (gtk_button_class);
	}


	{
		GtkMenuItemClass * gtk_menu_item_class =
		 g_type_class_ref (GTK_TYPE_MENU_ITEM);
		G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
		GtkImageMenuItemClass * gtk_image_menu_item_class =
		 g_type_class_ref (GTK_TYPE_IMAGE_MENU_ITEM);
		G_GNUC_END_IGNORE_DEPRECATIONS;

		INTERCEPT_CLASS_METHOD (
		 gtk_menu_item, G_OBJECT_CLASS, constructor)
		INTERCEPT_CLASS_METHOD (
		 gtk_image_menu_item, GTK_WIDGET_CLASS, parent_set)
		INTERCEPT_CLASS_METHOD (
		 gtk_image_menu_item, GTK_MENU_ITEM_CLASS, set_label)

		g_type_class_unref (gtk_menu_item_class);
		g_type_class_unref (gtk_image_menu_item_class);
	}


	for (icons_list_length = 0;
	     icons[icons_list_length].label != NULL;
	     icons_list_length++)
	{
		icons[icons_list_length].translated_label =
		 g_dgettext (GTK_GETTEXT_DOMAIN,
			     icons[icons_list_length].label);
	}

	qsort (icons, icons_list_length, sizeof (Icon), sort_icon_labels);
}


static void
new_gtk_button_notify
(GObject *	object,
 GParamSpec *	pspec)
{
	GtkSettings *	settings;
	gboolean	buttons_have_icons;
	GtkButton *	button;
	GtkWidget *	image;
	const char *	label_text;
	Icon *		icon;


	CALL_ORIGINAL_CLASS_METHOD (gtk_button, notify, (object, pspec));

	if (strcmp (pspec->name, "label") != 0)
		return;

	button = GTK_BUTTON (object);
	label_text = gtk_button_get_label (button);
	image = gtk_button_get_image (button);
	if (! label_text || image)
		return;

	settings = gtk_widget_get_settings (GTK_WIDGET (object));
	g_object_get (settings,
		      "gtk-button-images", &buttons_have_icons,
		      NULL);
	if (! buttons_have_icons)
		return;

	icon = bsearch (label_text, icons,
			icons_list_length, sizeof (Icon),
			search_icon_labels);
	if (icon)
	{
		image = gtk_image_new_from_icon_name (icon->icon_name,
						      GTK_ICON_SIZE_BUTTON);
		gtk_button_set_image (button, image);
	}
}


static GObject *
new_gtk_menu_item_constructor
(GType				type,
 guint				n_construct_properties,
 GObjectConstructParam *	construct_params)
{
	if (type == GTK_TYPE_MENU_ITEM)
	{
		G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
		type = GTK_TYPE_IMAGE_MENU_ITEM;
		G_GNUC_END_IGNORE_DEPRECATIONS;
	}

	return CALL_ORIGINAL_CLASS_METHOD_UNCHECKED (gtk_menu_item,
						     constructor,
						     (type,
						      n_construct_properties,
						      construct_params))
}


static void
new_gtk_image_menu_item_parent_set
(GtkWidget *	widget,
 GtkWidget *	previous_parent)

{
	GtkWidget *		parent;


	CALL_ORIGINAL_CLASS_METHOD (gtk_image_menu_item, parent_set,
				    (widget, previous_parent));


	parent = gtk_widget_get_parent (widget);
	if (parent && GTK_IS_MENU_BAR (parent))
	{
		G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
		gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (widget),
					       NULL);
		G_GNUC_END_IGNORE_DEPRECATIONS;
	}
}


static void
new_gtk_image_menu_item_set_label
(GtkMenuItem *	menu_item,
 const gchar *	label)

{
	GtkWidget *		widget;
	GtkImageMenuItem *	image_menu_item;
	GtkWidget *		parent;
	GtkSettings *		settings;
	gboolean		menus_have_icons;
	Icon *			icon;
	GtkWidget *		image;


	CALL_ORIGINAL_CLASS_METHOD (gtk_image_menu_item, set_label,
				    (menu_item, label));


	widget = GTK_WIDGET (menu_item);
	parent = gtk_widget_get_parent (widget);
	if (parent && GTK_IS_MENU_BAR (parent))
		return;

	G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
	image_menu_item = GTK_IMAGE_MENU_ITEM (menu_item);
	image = gtk_image_menu_item_get_image (image_menu_item);
	G_GNUC_END_IGNORE_DEPRECATIONS;
	if (! label || image)
		return;

	settings = gtk_widget_get_settings (widget);
	g_object_get (settings,
		      "gtk-menu-images", &menus_have_icons,
		      NULL);
	if (! menus_have_icons)
		return;

	icon = bsearch (label, icons,
			icons_list_length, sizeof (Icon),
			search_icon_labels);
	if (icon)
	{
		image = gtk_image_new_from_icon_name (icon->icon_name,
						      GTK_ICON_SIZE_MENU);
		G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
		gtk_image_menu_item_set_image (image_menu_item, image);
		G_GNUC_END_IGNORE_DEPRECATIONS;
	}
}


/* Helper functions */

static int sort_icon_labels (const void * icon1, const void * icon2)
{
	const char *	s1 = ((Icon *) icon1)->translated_label;
	const char *	s2 = ((Icon *) icon2)->translated_label;
	int		i;
	int		j;


	i = 0;
	j = 0;
	do
	{
		if (s1[i] == '_')
			i++;
		if (s2[j] == '_')
			j++;

		if (s1[i] != s2[j])
			return s1[i] - s2[j];

		if (s1[i] == '\0')
			break;

		i++;
		j++;
	}
	while (1);


	return 0;
}


static int search_icon_labels (const void * key, const void * icon)
{
	const char *	key_string = key;
	const char *	label = ((Icon *) icon)->translated_label;
	int		k;
	int		l;

	k = 0;
	l = 0;
	do
	{
		if (key_string[k] == '_')
			k++;
		if (label[l] == '_')
			l++;

		if (key_string[k] != label[l])
			return key_string[k] - label[l];

		if (key_string[k] == '\0')
			break;

		k++;
		l++;

		/* Ignore ellipses following the label text, so that buttons /
		 * menu items such as 'Open...' will be themed as well.
		 */
		if (strcmp (&key_string[k], "...") == 0)
			break;
	}
	while (1);


	return 0;
}
bgstack15