]> Pileus Git - ~andy/gtk/blob - gtk/gtkcheckmenuitem.c
Updated Norwegian bokmål translation
[~andy/gtk] / gtk / gtkcheckmenuitem.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25  */
26
27 #include "config.h"
28 #include "gtkcheckmenuitem.h"
29 #include "gtkmenuitemprivate.h"
30 #include "gtkaccellabel.h"
31 #include "gtkactivatable.h"
32 #include "gtktoggleaction.h"
33 #include "gtkmarshalers.h"
34 #include "gtkprivate.h"
35 #include "gtkintl.h"
36
37
38
39 struct _GtkCheckMenuItemPrivate
40 {
41   guint active             : 1;
42   guint always_show_toggle : 1;
43   guint draw_as_radio      : 1;
44   guint inconsistent       : 1;
45 };
46
47 enum {
48   TOGGLED,
49   LAST_SIGNAL
50 };
51
52 enum {
53   PROP_0,
54   PROP_ACTIVE,
55   PROP_INCONSISTENT,
56   PROP_DRAW_AS_RADIO
57 };
58
59 static gint gtk_check_menu_item_draw                 (GtkWidget             *widget,
60                                                       cairo_t               *cr);
61 static void gtk_check_menu_item_activate             (GtkMenuItem           *menu_item);
62 static void gtk_check_menu_item_toggle_size_request  (GtkMenuItem           *menu_item,
63                                                       gint                  *requisition);
64 static void gtk_real_check_menu_item_draw_indicator  (GtkCheckMenuItem      *check_menu_item,
65                                                       cairo_t               *cr);
66 static void gtk_check_menu_item_set_property         (GObject               *object,
67                                                       guint                  prop_id,
68                                                       const GValue          *value,
69                                                       GParamSpec            *pspec);
70 static void gtk_check_menu_item_get_property         (GObject               *object,
71                                                       guint                  prop_id,
72                                                       GValue                *value,
73                                                       GParamSpec            *pspec);
74
75 static void gtk_check_menu_item_activatable_interface_init (GtkActivatableIface  *iface);
76 static void gtk_check_menu_item_update                     (GtkActivatable       *activatable,
77                                                             GtkAction            *action,
78                                                             const gchar          *property_name);
79 static void gtk_check_menu_item_sync_action_properties     (GtkActivatable       *activatable,
80                                                             GtkAction            *action);
81
82 static GtkActivatableIface *parent_activatable_iface;
83 static guint                check_menu_item_signals[LAST_SIGNAL] = { 0 };
84
85 G_DEFINE_TYPE_WITH_CODE (GtkCheckMenuItem, gtk_check_menu_item, GTK_TYPE_MENU_ITEM,
86                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
87                                                 gtk_check_menu_item_activatable_interface_init))
88
89 static void
90 gtk_check_menu_item_class_init (GtkCheckMenuItemClass *klass)
91 {
92   GObjectClass *gobject_class;
93   GtkWidgetClass *widget_class;
94   GtkMenuItemClass *menu_item_class;
95   
96   gobject_class = G_OBJECT_CLASS (klass);
97   widget_class = (GtkWidgetClass*) klass;
98   menu_item_class = (GtkMenuItemClass*) klass;
99   
100   gobject_class->set_property = gtk_check_menu_item_set_property;
101   gobject_class->get_property = gtk_check_menu_item_get_property;
102
103   g_object_class_install_property (gobject_class,
104                                    PROP_ACTIVE,
105                                    g_param_spec_boolean ("active",
106                                                          P_("Active"),
107                                                          P_("Whether the menu item is checked"),
108                                                          FALSE,
109                                                          GTK_PARAM_READWRITE));
110   
111   g_object_class_install_property (gobject_class,
112                                    PROP_INCONSISTENT,
113                                    g_param_spec_boolean ("inconsistent",
114                                                          P_("Inconsistent"),
115                                                          P_("Whether to display an \"inconsistent\" state"),
116                                                          FALSE,
117                                                          GTK_PARAM_READWRITE));
118   
119   g_object_class_install_property (gobject_class,
120                                    PROP_DRAW_AS_RADIO,
121                                    g_param_spec_boolean ("draw-as-radio",
122                                                          P_("Draw as radio menu item"),
123                                                          P_("Whether the menu item looks like a radio menu item"),
124                                                          FALSE,
125                                                          GTK_PARAM_READWRITE));
126   
127   gtk_widget_class_install_style_property (widget_class,
128                                            g_param_spec_int ("indicator-size",
129                                                              P_("Indicator Size"),
130                                                              P_("Size of check or radio indicator"),
131                                                              0,
132                                                              G_MAXINT,
133                                                              13,
134                                                              GTK_PARAM_READABLE));
135
136   widget_class->draw = gtk_check_menu_item_draw;
137   
138   menu_item_class->activate = gtk_check_menu_item_activate;
139   menu_item_class->hide_on_activate = FALSE;
140   menu_item_class->toggle_size_request = gtk_check_menu_item_toggle_size_request;
141   
142   klass->toggled = NULL;
143   klass->draw_indicator = gtk_real_check_menu_item_draw_indicator;
144
145   check_menu_item_signals[TOGGLED] =
146     g_signal_new (I_("toggled"),
147                   G_OBJECT_CLASS_TYPE (gobject_class),
148                   G_SIGNAL_RUN_FIRST,
149                   G_STRUCT_OFFSET (GtkCheckMenuItemClass, toggled),
150                   NULL, NULL,
151                   _gtk_marshal_VOID__VOID,
152                   G_TYPE_NONE, 0);
153
154   g_type_class_add_private (klass, sizeof (GtkCheckMenuItemPrivate));
155 }
156
157 static void 
158 gtk_check_menu_item_activatable_interface_init (GtkActivatableIface  *iface)
159 {
160   parent_activatable_iface = g_type_interface_peek_parent (iface);
161   iface->update = gtk_check_menu_item_update;
162   iface->sync_action_properties = gtk_check_menu_item_sync_action_properties;
163 }
164
165 static void
166 gtk_check_menu_item_update (GtkActivatable *activatable,
167                             GtkAction      *action,
168                             const gchar    *property_name)
169 {
170   GtkCheckMenuItem *check_menu_item;
171
172   check_menu_item = GTK_CHECK_MENU_ITEM (activatable);
173
174   parent_activatable_iface->update (activatable, action, property_name);
175
176   if (strcmp (property_name, "active") == 0)
177     {
178       gtk_action_block_activate (action);
179       gtk_check_menu_item_set_active (check_menu_item, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
180       gtk_action_unblock_activate (action);
181     }
182
183   if (!gtk_activatable_get_use_action_appearance (activatable))
184     return;
185
186   if (strcmp (property_name, "draw-as-radio") == 0)
187     gtk_check_menu_item_set_draw_as_radio (check_menu_item,
188                                            gtk_toggle_action_get_draw_as_radio (GTK_TOGGLE_ACTION (action)));
189 }
190
191 static void
192 gtk_check_menu_item_sync_action_properties (GtkActivatable *activatable,
193                                             GtkAction      *action)
194 {
195   GtkCheckMenuItem *check_menu_item;
196
197   check_menu_item = GTK_CHECK_MENU_ITEM (activatable);
198
199   parent_activatable_iface->sync_action_properties (activatable, action);
200
201   if (!GTK_IS_TOGGLE_ACTION (action))
202     return;
203
204   gtk_action_block_activate (action);
205   gtk_check_menu_item_set_active (check_menu_item, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
206   gtk_action_unblock_activate (action);
207   
208   if (!gtk_activatable_get_use_action_appearance (activatable))
209     return;
210
211   gtk_check_menu_item_set_draw_as_radio (check_menu_item,
212                                          gtk_toggle_action_get_draw_as_radio (GTK_TOGGLE_ACTION (action)));
213 }
214
215 GtkWidget*
216 gtk_check_menu_item_new (void)
217 {
218   return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, NULL);
219 }
220
221 GtkWidget*
222 gtk_check_menu_item_new_with_label (const gchar *label)
223 {
224   return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, 
225                        "label", label,
226                        NULL);
227 }
228
229
230 /**
231  * gtk_check_menu_item_new_with_mnemonic:
232  * @label: The text of the button, with an underscore in front of the
233  *         mnemonic character
234  * @returns: a new #GtkCheckMenuItem
235  *
236  * Creates a new #GtkCheckMenuItem containing a label. The label
237  * will be created using gtk_label_new_with_mnemonic(), so underscores
238  * in @label indicate the mnemonic for the menu item.
239  **/
240 GtkWidget*
241 gtk_check_menu_item_new_with_mnemonic (const gchar *label)
242 {
243   return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, 
244                        "label", label,
245                        "use-underline", TRUE,
246                        NULL);
247 }
248
249 void
250 gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
251                                 gboolean          is_active)
252 {
253   GtkCheckMenuItemPrivate *priv;
254
255   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
256
257   priv = check_menu_item->priv;
258
259   is_active = is_active != 0;
260
261   if (priv->active != is_active)
262     gtk_menu_item_activate (GTK_MENU_ITEM (check_menu_item));
263 }
264
265 /**
266  * gtk_check_menu_item_get_active:
267  * @check_menu_item: a #GtkCheckMenuItem
268  * 
269  * Returns whether the check menu item is active. See
270  * gtk_check_menu_item_set_active ().
271  * 
272  * Return value: %TRUE if the menu item is checked.
273  */
274 gboolean
275 gtk_check_menu_item_get_active (GtkCheckMenuItem *check_menu_item)
276 {
277   g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
278
279   return check_menu_item->priv->active;
280 }
281
282 static void
283 gtk_check_menu_item_toggle_size_request (GtkMenuItem *menu_item,
284                                          gint        *requisition)
285 {
286   guint toggle_spacing;
287   guint indicator_size;
288   
289   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (menu_item));
290   
291   gtk_widget_style_get (GTK_WIDGET (menu_item),
292                         "toggle-spacing", &toggle_spacing,
293                         "indicator-size", &indicator_size,
294                         NULL);
295
296   *requisition = indicator_size + toggle_spacing;
297 }
298
299 void
300 gtk_check_menu_item_toggled (GtkCheckMenuItem *check_menu_item)
301 {
302   g_signal_emit (check_menu_item, check_menu_item_signals[TOGGLED], 0);
303 }
304
305 /**
306  * gtk_check_menu_item_set_inconsistent:
307  * @check_menu_item: a #GtkCheckMenuItem
308  * @setting: %TRUE to display an "inconsistent" third state check
309  *
310  * If the user has selected a range of elements (such as some text or
311  * spreadsheet cells) that are affected by a boolean setting, and the
312  * current values in that range are inconsistent, you may want to
313  * display the check in an "in between" state. This function turns on
314  * "in between" display.  Normally you would turn off the inconsistent
315  * state again if the user explicitly selects a setting. This has to be
316  * done manually, gtk_check_menu_item_set_inconsistent() only affects
317  * visual appearance, it doesn't affect the semantics of the widget.
318  * 
319  **/
320 void
321 gtk_check_menu_item_set_inconsistent (GtkCheckMenuItem *check_menu_item,
322                                       gboolean          setting)
323 {
324   GtkCheckMenuItemPrivate *priv;
325
326   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
327
328   priv = check_menu_item->priv;
329   
330   setting = setting != FALSE;
331
332   if (setting != priv->inconsistent)
333     {
334       priv->inconsistent = setting;
335       gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
336       g_object_notify (G_OBJECT (check_menu_item), "inconsistent");
337     }
338 }
339
340 /**
341  * gtk_check_menu_item_get_inconsistent:
342  * @check_menu_item: a #GtkCheckMenuItem
343  * 
344  * Retrieves the value set by gtk_check_menu_item_set_inconsistent().
345  * 
346  * Return value: %TRUE if inconsistent
347  **/
348 gboolean
349 gtk_check_menu_item_get_inconsistent (GtkCheckMenuItem *check_menu_item)
350 {
351   g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
352
353   return check_menu_item->priv->inconsistent;
354 }
355
356 /**
357  * gtk_check_menu_item_set_draw_as_radio:
358  * @check_menu_item: a #GtkCheckMenuItem
359  * @draw_as_radio: whether @check_menu_item is drawn like a #GtkRadioMenuItem
360  *
361  * Sets whether @check_menu_item is drawn like a #GtkRadioMenuItem
362  *
363  * Since: 2.4
364  **/
365 void
366 gtk_check_menu_item_set_draw_as_radio (GtkCheckMenuItem *check_menu_item,
367                                        gboolean          draw_as_radio)
368 {
369   GtkCheckMenuItemPrivate *priv;
370
371   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
372
373   priv = check_menu_item->priv;
374
375   draw_as_radio = draw_as_radio != FALSE;
376
377   if (draw_as_radio != priv->draw_as_radio)
378     {
379       priv->draw_as_radio = draw_as_radio;
380
381       gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
382
383       g_object_notify (G_OBJECT (check_menu_item), "draw-as-radio");
384     }
385 }
386
387 /**
388  * gtk_check_menu_item_get_draw_as_radio:
389  * @check_menu_item: a #GtkCheckMenuItem
390  * 
391  * Returns whether @check_menu_item looks like a #GtkRadioMenuItem
392  * 
393  * Return value: Whether @check_menu_item looks like a #GtkRadioMenuItem
394  * 
395  * Since: 2.4
396  **/
397 gboolean
398 gtk_check_menu_item_get_draw_as_radio (GtkCheckMenuItem *check_menu_item)
399 {
400   g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
401   
402   return check_menu_item->priv->draw_as_radio;
403 }
404
405 static void
406 gtk_check_menu_item_init (GtkCheckMenuItem *check_menu_item)
407 {
408   GtkCheckMenuItemPrivate *priv;
409
410   check_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (check_menu_item,
411                                                        GTK_TYPE_CHECK_MENU_ITEM,
412                                                        GtkCheckMenuItemPrivate);
413   priv = check_menu_item->priv; 
414
415   priv->active = FALSE;
416   priv->always_show_toggle = TRUE;
417 }
418
419 static gint
420 gtk_check_menu_item_draw (GtkWidget *widget,
421                           cairo_t   *cr)
422 {
423   GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (widget);
424
425   if (GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->draw)
426     GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->draw (widget, cr);
427
428   if (GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator)
429     GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator (check_menu_item, cr);
430
431   return FALSE;
432 }
433
434 static void
435 gtk_check_menu_item_activate (GtkMenuItem *menu_item)
436 {
437   GtkCheckMenuItemPrivate *priv;
438
439   GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
440   priv = check_menu_item->priv;
441
442   priv->active = !priv->active;
443
444   gtk_check_menu_item_toggled (check_menu_item);
445   gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
446
447   GTK_MENU_ITEM_CLASS (gtk_check_menu_item_parent_class)->activate (menu_item);
448
449   g_object_notify (G_OBJECT (check_menu_item), "active");
450 }
451
452 static void
453 gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
454                                          cairo_t          *cr)
455 {
456   GtkCheckMenuItemPrivate *priv = check_menu_item->priv;
457   GtkWidget *widget;
458   gint x, y;
459
460   widget = GTK_WIDGET (check_menu_item);
461
462   if (gtk_widget_is_drawable (widget))
463     {
464       GtkAllocation allocation;
465       GtkStyleContext *context;
466       guint border_width;
467       guint offset;
468       guint toggle_size;
469       guint toggle_spacing;
470       guint horizontal_padding;
471       guint indicator_size;
472       GtkStateFlags state;
473       GtkBorder padding;
474
475       context = gtk_widget_get_style_context (widget);
476       state = gtk_widget_get_state_flags (widget);
477       gtk_style_context_get_padding (context, state, &padding);
478
479       gtk_widget_get_allocation (widget, &allocation);
480
481       gtk_widget_style_get (widget,
482                             "toggle-spacing", &toggle_spacing,
483                             "horizontal-padding", &horizontal_padding,
484                             "indicator-size", &indicator_size,
485                             NULL);
486
487       toggle_size = GTK_MENU_ITEM (check_menu_item)->priv->toggle_size;
488       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
489       offset = border_width + padding.left + 2;
490
491       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
492         {
493           x = offset + horizontal_padding +
494             (toggle_size - toggle_spacing - indicator_size) / 2;
495         }
496       else
497         {
498           x = allocation.width -
499             offset - horizontal_padding - toggle_size + toggle_spacing +
500             (toggle_size - toggle_spacing - indicator_size) / 2;
501         }
502
503       y = (allocation.height - indicator_size) / 2;
504
505       if (priv->active ||
506           priv->always_show_toggle ||
507           (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_PRELIGHT))
508         {
509           GdkWindow *window;
510
511           window = gtk_widget_get_window (widget);
512           gtk_style_context_save (context);
513
514           if (priv->inconsistent)
515             state |= GTK_STATE_FLAG_INCONSISTENT;
516           else if (priv->active)
517             state |= GTK_STATE_FLAG_ACTIVE;
518
519           if (!gtk_widget_is_sensitive (widget))
520             state |= GTK_STATE_FLAG_INSENSITIVE;
521
522           gtk_style_context_set_state (context, state);
523
524           if (priv->draw_as_radio)
525             {
526               gtk_style_context_add_class (context, GTK_STYLE_CLASS_RADIO);
527               gtk_render_option (context, cr, x, y,
528                                  indicator_size, indicator_size);
529             }
530           else
531             {
532               gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
533               gtk_render_check (context, cr, x, y,
534                                 indicator_size, indicator_size);
535             }
536
537           gtk_style_context_restore (context);
538         }
539     }
540 }
541
542
543 static void
544 gtk_check_menu_item_get_property (GObject     *object,
545                                   guint        prop_id,
546                                   GValue      *value,
547                                   GParamSpec  *pspec)
548 {
549   GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
550   GtkCheckMenuItemPrivate *priv = checkitem->priv;
551   
552   switch (prop_id)
553     {
554     case PROP_ACTIVE:
555       g_value_set_boolean (value, priv->active);
556       break;
557     case PROP_INCONSISTENT:
558       g_value_set_boolean (value, priv->inconsistent);
559       break;
560     case PROP_DRAW_AS_RADIO:
561       g_value_set_boolean (value, priv->draw_as_radio);
562       break;
563     default:
564       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
565       break;
566     }
567 }
568
569
570 static void
571 gtk_check_menu_item_set_property (GObject      *object,
572                                   guint         prop_id,
573                                   const GValue *value,
574                                   GParamSpec   *pspec)
575 {
576   GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
577   
578   switch (prop_id)
579     {
580     case PROP_ACTIVE:
581       gtk_check_menu_item_set_active (checkitem, g_value_get_boolean (value));
582       break;
583     case PROP_INCONSISTENT:
584       gtk_check_menu_item_set_inconsistent (checkitem, g_value_get_boolean (value));
585       break;
586     case PROP_DRAW_AS_RADIO:
587       gtk_check_menu_item_set_draw_as_radio (checkitem, g_value_get_boolean (value));
588       break;
589     default:
590       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
591       break;
592     }
593 }
594
595
596 /* Private */
597
598 /*
599  * _gtk_check_menu_item_set_active:
600  * @check_menu_item: a #GtkCheckMenuItem
601  * @is_active: whether the action is active or not
602  *
603  * Sets the #GtkCheckMenuItem:active property directly. This function does
604  * not emit signals or notifications: it is left to the caller to do so.
605  */
606 void
607 _gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
608                                  gboolean          is_active)
609 {
610   GtkCheckMenuItemPrivate *priv = check_menu_item->priv;
611
612   priv->active = is_active;
613 }