aboutsummaryrefslogtreecommitdiff
path: root/libdbusmenu-gtk/genericmenuitem.c
blob: 098de67be0ed635cfd1ed123843d883de817107b (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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
A menuitem subclass that has the ability to do lots of different
things depending on it's settings.

Copyright 2009 Canonical Ltd.

Authors:
    Ted Gould <ted@canonical.com>

This program is free software: you can redistribute it and/or modify it 
under the terms of either or both of the following licenses:

1) the GNU Lesser General Public License version 3, as published by the 
Free Software Foundation; and/or
2) the GNU Lesser General Public License version 2.1, as published by 
the Free Software Foundation.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranties of 
MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 
PURPOSE.  See the applicable version of the GNU Lesser General Public 
License for more details.

You should have received a copy of both the GNU Lesser General Public 
License version 3 and version 2.1 along with this program.  If not, see 
<http://www.gnu.org/licenses/>
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gdk/gdk.h>

#include "genericmenuitem.h"

/*
	GenericmenuitemPrivate:
	@check_type: What type of check we have, or none at all.
	@state: What the state of our check is.
*/
struct _GenericmenuitemPrivate {
	GenericmenuitemCheckType   check_type;
	GenericmenuitemState       state;
	GenericmenuitemDisposition disposition;
};

/* Private macro */
#define GENERICMENUITEM_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), GENERICMENUITEM_TYPE, GenericmenuitemPrivate))

/* Prototypes */
static void genericmenuitem_class_init (GenericmenuitemClass *klass);
static void genericmenuitem_init       (Genericmenuitem *self);
static void genericmenuitem_dispose    (GObject *object);
static void genericmenuitem_finalize   (GObject *object);
static void set_label (GtkMenuItem * menu_item, const gchar * label);
static const gchar * get_label (GtkMenuItem * menu_item);
static void activate (GtkMenuItem * menu_item);

/* GObject stuff */
G_DEFINE_TYPE (Genericmenuitem, genericmenuitem, GTK_TYPE_CHECK_MENU_ITEM);

#if HAVE_GTK3
static void draw_indicator (GtkCheckMenuItem *check_menu_item, cairo_t *cr);
static void (*parent_draw_indicator) (GtkCheckMenuItem *check_menu_item, cairo_t *cr) = NULL;
#else
static void draw_indicator (GtkCheckMenuItem *check_menu_item, GdkRectangle *area);
static void (*parent_draw_indicator) (GtkCheckMenuItem *check_menu_item, GdkRectangle *area) = NULL;
#endif
static void (*parent_menuitem_activate) (GtkMenuItem * mi) = NULL;

/* Initializing all of the classes.  Most notably we're
   disabling the drawing of the check early. */
static void
genericmenuitem_class_init (GenericmenuitemClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	g_type_class_add_private (klass, sizeof (GenericmenuitemPrivate));

	object_class->dispose = genericmenuitem_dispose;
	object_class->finalize = genericmenuitem_finalize;

	GtkCheckMenuItemClass * check_class = GTK_CHECK_MENU_ITEM_CLASS (klass);

	parent_draw_indicator = check_class->draw_indicator;
	check_class->draw_indicator = draw_indicator;

	GtkMenuItemClass * menuitem_class = GTK_MENU_ITEM_CLASS (klass);
	menuitem_class->set_label = set_label;
	menuitem_class->get_label = get_label;
	parent_menuitem_activate = menuitem_class->activate;
	menuitem_class->activate = activate;

	return;
}

/* Sets default values for all the class variables.  Mostly,
   this puts us in a default state. */
static void
genericmenuitem_init (Genericmenuitem *self)
{
	self->priv = GENERICMENUITEM_GET_PRIVATE(self);

	self->priv->check_type = GENERICMENUITEM_CHECK_TYPE_NONE;
	self->priv->state = GENERICMENUITEM_STATE_UNCHECKED;
	self->priv->disposition = GENERICMENUITEM_DISPOSITION_NORMAL;

	return;
}

/* Clean everything up.  Whew, that can be work. */
static void
genericmenuitem_dispose (GObject *object)
{

	G_OBJECT_CLASS (genericmenuitem_parent_class)->dispose (object);
	return;
}

/* Now free memory, we no longer need it. */
static void
genericmenuitem_finalize (GObject *object)
{

	G_OBJECT_CLASS (genericmenuitem_parent_class)->finalize (object);
	return;
}

/* Checks to see if we should be drawing a little box at
   all.  If we should be, let's do that, otherwise we're
   going suppress the box drawing. */
#if HAVE_GTK3
static void
draw_indicator (GtkCheckMenuItem *check_menu_item, cairo_t *cr)
{
	Genericmenuitem * self = GENERICMENUITEM(check_menu_item);
	if (self->priv->check_type != GENERICMENUITEM_CHECK_TYPE_NONE) {
		parent_draw_indicator(check_menu_item, cr);
	}
	return;
}
#else
static void
draw_indicator (GtkCheckMenuItem *check_menu_item, GdkRectangle *area)
{
	Genericmenuitem * self = GENERICMENUITEM(check_menu_item);
	if (self->priv->check_type != GENERICMENUITEM_CHECK_TYPE_NONE) {
		parent_draw_indicator(check_menu_item, area);
	}
	return;
}
#endif

/* A small helper to look through the widgets in the
   box and find the one that is the label. */
static void
set_label_helper (GtkWidget * widget, gpointer data)
{
	GtkWidget ** labelval = (GtkWidget **)data;
	if (GTK_IS_LABEL(widget)) {
		*labelval = widget;
	}
	return;
}

/* A quick little function to grab the padding from the
   style.  It should be considered for caching when
   optimizing. */
static gint
get_toggle_space (GtkWidget * widget)
{
	gint padding = 0;
	gtk_widget_style_get(widget, "toggle-spacing", &padding, NULL);
	return padding;
}

/* Get the value to put in the span for the disposition */
static gchar *
get_text_color (GenericmenuitemDisposition disposition, GtkWidget * widget)
{
	struct {const gchar * color_name; const gchar * default_color;} values[] = {
		/* NORMAL */ { NULL, NULL},
		/* INFO   */ { "informational-color", "blue"},
		/* WARN   */ { "warning-color", "orange"},
		/* ALERT  */ { "error-color", "red"}
	};

#if GTK_CHECK_VERSION(3, 0, 0)
	GtkStyleContext * context = gtk_widget_get_style_context(widget);
	GdkRGBA color;

	if (gtk_style_context_lookup_color(context, values[disposition].color_name, &color)) {
		return g_strdup_printf("rgb(%d, %d, %d)", (gint)(color.red * 255), (gint)(color.green * 255), (gint)(color.blue * 255));
	}
#endif

	return g_strdup(values[disposition].default_color);
}

/* Set the label on the item */
static void
set_label (GtkMenuItem * menu_item, const gchar * in_label)
{
	if (in_label == NULL) return;

	/* Build a label that might include the colors of the disposition
	   so that it gets rendered in the menuitem. */
	gchar * local_label = NULL;
	switch (GENERICMENUITEM(menu_item)->priv->disposition) {
	case GENERICMENUITEM_DISPOSITION_NORMAL:
		local_label = g_strdup(in_label);
		break;
	case GENERICMENUITEM_DISPOSITION_INFORMATIONAL:
	case GENERICMENUITEM_DISPOSITION_WARNING:
	case GENERICMENUITEM_DISPOSITION_ALERT: {
		gchar * color = get_text_color(GENERICMENUITEM(menu_item)->priv->disposition, GTK_WIDGET(menu_item));
		local_label = g_markup_printf_escaped("<span fgcolor=\"%s\">%s</span>", color, in_label);
		g_free(color);
		break;
	}
	default:
		g_warn_if_reached();
		break;
	}

	GtkWidget * child = gtk_bin_get_child(GTK_BIN(menu_item));
	GtkLabel * labelw = NULL;
	gboolean suppress_update = FALSE;

	/* Try to find if we have a label already */
	if (child != NULL) {
		if (GTK_IS_LABEL(child)) {
			/* We've got a label, let's update it. */
			labelw = GTK_LABEL(child);
		} else if (GTK_IS_BOX(child)) {
			/* Look for the label in the box */
			gtk_container_foreach(GTK_CONTAINER(child), set_label_helper, &labelw);
		} else {
			/* We need to put the child into a new box and
			   make the box the child of the menu item.  Basically
			   we're inserting a box in the middle. */
			#ifdef HAVE_GTK3
			GtkWidget * hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
			                               get_toggle_space(GTK_WIDGET(menu_item)));
			#else
			GtkWidget * hbox = gtk_hbox_new(FALSE, get_toggle_space(GTK_WIDGET(menu_item)));
			#endif
			g_object_ref(child);
			gtk_container_remove(GTK_CONTAINER(menu_item), child);
			gtk_box_pack_start(GTK_BOX(hbox), child, FALSE, FALSE, 0);
			gtk_container_add(GTK_CONTAINER(menu_item), hbox);
			gtk_widget_show(hbox);
			g_object_unref(child);
			child = hbox;
			/* It's important to notice that labelw is not set
			   by this condition.  There was no label to find. */
		}
	}

	/* No we can see if we need to ethier build a label or just
	   update the one that we already have. */
	if (labelw == NULL) {
		/* Build it */
		labelw = GTK_LABEL(gtk_accel_label_new(local_label));
		gtk_label_set_use_underline(GTK_LABEL(labelw), TRUE);
		gtk_label_set_use_markup(GTK_LABEL(labelw), TRUE);
		gtk_misc_set_alignment(GTK_MISC(labelw), 0.0, 0.5);
		gtk_accel_label_set_accel_widget(GTK_ACCEL_LABEL(labelw), GTK_WIDGET(menu_item));
		gtk_label_set_markup_with_mnemonic(labelw, local_label);
		gtk_widget_show(GTK_WIDGET(labelw));

		/* Check to see if it needs to be in the bin for this
		   menu item or whether it gets packed in a box. */
		if (child == NULL) {
			gtk_container_add(GTK_CONTAINER(menu_item), GTK_WIDGET(labelw));
		} else {
			gtk_box_pack_end(GTK_BOX(child), GTK_WIDGET(labelw), TRUE, TRUE, 0);
		}
	} else {
		/* Oh, just an update.  No biggie. */
		if (!g_strcmp0(local_label, gtk_label_get_label(labelw))) {
			/* The only reason to suppress the update is if we had
			   a label and the value was the same as the one we're
			   getting in. */
			suppress_update = TRUE;
		} else {
			gtk_label_set_markup_with_mnemonic(labelw, local_label);
		}
	}

	/* If we changed the value, tell folks. */
	if (!suppress_update) {
		g_object_notify(G_OBJECT(menu_item), "label");
	}

	/* Clean up this */
	if (local_label != NULL) {
		g_free(local_label);
		local_label = NULL;
	}

	return;
}

/* Get the text of the label for the item */
static const gchar *
get_label (GtkMenuItem * menu_item)
{
	GtkWidget * child = gtk_bin_get_child(GTK_BIN(menu_item));
	GtkLabel * labelw = NULL;

	/* Try to find if we have a label already */
	if (child != NULL) {
		if (GTK_IS_LABEL(child)) {
			/* We've got a label, let's update it. */
			labelw = GTK_LABEL(child);
		} else if (GTK_IS_BOX(child)) {
			/* Look for the label in the box */
			gtk_container_foreach(GTK_CONTAINER(child), set_label_helper, &labelw);
		}
	}

	if (labelw != NULL) {
		return gtk_label_get_label(labelw);
	}

	return NULL;
}

/* Make sure we don't toggle when there is an
   activate like a normal check menu item. */
static void
activate (GtkMenuItem * menu_item)
{
	return;
}

/**
 * genericmenuitem_set_check_type:
 * @item: #Genericmenuitem to set the type on
 * @check_type: Which type of check should be displayed
 * 
 * This function changes the type of the checkmark that
 * appears in the left hand gutter for the menuitem.
*/
void
genericmenuitem_set_check_type (Genericmenuitem * item, GenericmenuitemCheckType check_type)
{
	if (item->priv->check_type == check_type) {
		return;
	}

	item->priv->check_type = check_type;

	switch (item->priv->check_type) {
	case GENERICMENUITEM_CHECK_TYPE_NONE:
		/* We don't need to do anything here as we're queuing the
		   draw and then when it draws it'll avoid drawing the
		   check on the item. */
		break;
	case GENERICMENUITEM_CHECK_TYPE_CHECKBOX:
		gtk_check_menu_item_set_draw_as_radio(GTK_CHECK_MENU_ITEM(item), FALSE);
		break;
	case GENERICMENUITEM_CHECK_TYPE_RADIO:
		gtk_check_menu_item_set_draw_as_radio(GTK_CHECK_MENU_ITEM(item), TRUE);
		break;
	default:
		g_warning("Generic Menuitem invalid check type: %d", check_type);
		return;
	}

	gtk_widget_queue_draw(GTK_WIDGET(item));

	return;
}

/**
 * genericmenuitem_set_state:
 * @item: #Genericmenuitem to set the type on
 * @check_type: What is the state of the check 
 * 
 * Sets the state of the check in the menu item.  It does
 * not require, but isn't really useful if the type of
 * check that the menuitem is set to #GENERICMENUITEM_CHECK_TYPE_NONE.
 */
void
genericmenuitem_set_state (Genericmenuitem * item, GenericmenuitemState state)
{
	if (item->priv->state == state) {
		return;
	}

	item->priv->state = state;

	GtkCheckMenuItem * check = GTK_CHECK_MENU_ITEM(item);
	gboolean goal_active = FALSE;

	switch (item->priv->state) {
	case GENERICMENUITEM_STATE_UNCHECKED:
		goal_active = FALSE;
		gtk_check_menu_item_set_inconsistent (check, FALSE);
		break;
	case GENERICMENUITEM_STATE_CHECKED:
		goal_active = TRUE;
		gtk_check_menu_item_set_inconsistent (check, FALSE);
		break;
	case GENERICMENUITEM_STATE_INDETERMINATE:
		goal_active = TRUE;
		gtk_check_menu_item_set_inconsistent (check, TRUE);
		break;
	default:
		g_warning("Generic Menuitem invalid check state: %d", state);
		return;
	}

	if (goal_active != gtk_check_menu_item_get_active(check)) {
		if (parent_menuitem_activate != NULL) {
			parent_menuitem_activate(GTK_MENU_ITEM(check));
		}
	}

	return;
}

/* A small helper to look through the widgets in the
   box and find the one that is the image. */
static void
set_image_helper (GtkWidget * widget, gpointer data)
{
	GtkWidget ** labelval = (GtkWidget **)data;
	if (GTK_IS_IMAGE(widget)) {
		*labelval = widget;
	}
	return;
}

/**
 * genericmenuitem_set_image:
 * @item: A #Genericmenuitem
 * @image: The image to set as the image of @item
 * 
 * Sets the image of the menu item.
*/
void
genericmenuitem_set_image (Genericmenuitem * menu_item, GtkWidget * image)
{
	GtkWidget * child = gtk_bin_get_child(GTK_BIN(menu_item));
	GtkImage * imagew = NULL;

	/* Try to find if we have a label already */
	if (child != NULL) {
		if (GTK_IS_IMAGE(child)) {
			/* We've got a label, let's update it. */
			imagew = GTK_IMAGE(child);
			child = NULL;
		} else if (GTK_IS_BOX(child)) {
			/* Look for the label in the box */
			gtk_container_foreach(GTK_CONTAINER(child), set_image_helper, &imagew);
		} else if (image != NULL) {
			/* We need to put the child into a new box and
			   make the box the child of the menu item.  Basically
			   we're inserting a box in the middle. */
			#ifdef HAVE_GTK3
			GtkWidget * hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
			                               get_toggle_space(GTK_WIDGET(menu_item)));
			#else
			GtkWidget * hbox = gtk_hbox_new(FALSE, get_toggle_space(GTK_WIDGET(menu_item)));
			#endif
			g_object_ref(child);
			gtk_container_remove(GTK_CONTAINER(menu_item), child);
			gtk_box_pack_end(GTK_BOX(hbox), child, TRUE, TRUE, 0);
			gtk_container_add(GTK_CONTAINER(menu_item), hbox);
			gtk_widget_show(hbox);
			g_object_unref(child);
			child = hbox;
			/* It's important to notice that imagew is not set
			   by this condition.  There was no label to find. */
		}
	}

        if (image == (GtkWidget *)imagew)
          return;

	/* No we can see if we need to ethier replace and image or
	   just put ourselves into the structures */
	if (imagew != NULL) {
		gtk_widget_destroy(GTK_WIDGET(imagew));
	}

	/* Check to see if it needs to be in the bin for this
	   menu item or whether it gets packed in a box. */
	if (image != NULL) {
		if (child == NULL) {
			gtk_container_add(GTK_CONTAINER(menu_item), GTK_WIDGET(image));
		} else {
			gtk_box_pack_start(GTK_BOX(child), GTK_WIDGET(image), FALSE, FALSE, 0);
		}

		gtk_widget_show(image);
	}

	return;
}

/**
 * genericmenuitem_get_image:
 * @item: A #Genericmenuitem
 * 
 * Returns the image if there is one.
 * 
 * Return value: (transfer none): A pointer to the image of the item or #NULL
 * 	if there isn't one.
*/
GtkWidget *
genericmenuitem_get_image (Genericmenuitem * menu_item)
{
	GtkWidget * child = gtk_bin_get_child(GTK_BIN(menu_item));
	GtkWidget * imagew = NULL;

	/* Try to find if we have a label already */
	if (child != NULL) {
		if (GTK_IS_IMAGE(child)) {
			/* We've got a label, let's update it. */
			imagew = child;
		} else if (GTK_IS_BOX(child)) {
			/* Look for the label in the box */
			gtk_container_foreach(GTK_CONTAINER(child), set_image_helper, &imagew);
		}
	}

	return imagew;
}

/**
 * genericmenuitem_set_disposition:
 * @item: A #Genericmenuitem
 * @disposition: The disposition of the item
 * 
 * Sets the disposition of the menuitem.
 */
void
genericmenuitem_set_disposition (Genericmenuitem * item, GenericmenuitemDisposition disposition)
{
	g_return_if_fail(IS_GENERICMENUITEM(item));

	if (item->priv->disposition == disposition)
		return;

	item->priv->disposition = disposition;
	
	set_label(GTK_MENU_ITEM(item), get_label(GTK_MENU_ITEM(item)));

	return;
}

/**
 * genericmenuitem_get_disposition:
 * @item: A #Genericmenuitem
 * 
 * Gets the disposition of the menuitem.
 *
 * Return value: The disposition of the menuitem.
 */
GenericmenuitemDisposition
genericmenuitem_get_disposition (Genericmenuitem * item)
{
	g_return_val_if_fail(IS_GENERICMENUITEM(item), GENERICMENUITEM_DISPOSITION_NORMAL);

	return item->priv->disposition;
}