1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Jsh 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-2000. 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 <glib.h>
29 #include <gtk/gtk.h>
30 #include <gdk/gdkkeysyms.h>
31
32 #include "claws-marshal.h"
33 #include "gtkcmoptionmenu.h"
34 #include "utils.h"
35 #include "gtkutils.h"
36
37 #define CHILD_LEFT_SPACING 4
38 #define CHILD_RIGHT_SPACING 1
39 #define CHILD_TOP_SPACING 1
40 #define CHILD_BOTTOM_SPACING 1
41
42 typedef struct _GtkCMOptionMenuProps GtkCMOptionMenuProps;
43
44 struct _GtkCMOptionMenuProps
45 {
46 gboolean interior_focus;
47 GtkRequisition indicator_size;
48 GtkBorder indicator_spacing;
49 gint focus_width;
50 gint focus_pad;
51 };
52
53 static const GtkCMOptionMenuProps default_props = {
54 TRUE,
55 { 7, 13 },
56 { 7, 5, 2, 2 }, /* Left, right, top, bottom */
57 1,
58 0
59 };
60
61 static void gtk_cmoption_menu_destroy (GtkObject *object);
62 static void gtk_cmoption_menu_set_property (GObject *object,
63 guint prop_id,
64 const GValue *value,
65 GParamSpec *pspec);
66 static void gtk_cmoption_menu_get_property (GObject *object,
67 guint prop_id,
68 GValue *value,
69 GParamSpec *pspec);
70 static void gtk_cmoption_menu_size_request (GtkWidget *widget,
71 GtkRequisition *requisition);
72 static void gtk_cmoption_menu_size_allocate (GtkWidget *widget,
73 GtkAllocation *allocation);
74 static void gtk_cmoption_menu_paint (GtkWidget *widget,
75 GdkRectangle *area);
76 static gint gtk_cmoption_menu_expose (GtkWidget *widget,
77 GdkEventExpose *event);
78 static gint gtk_cmoption_menu_button_press (GtkWidget *widget,
79 GdkEventButton *event);
80 static gint gtk_cmoption_menu_key_press (GtkWidget *widget,
81 GdkEventKey *event);
82 static void gtk_cmoption_menu_selection_done (GtkMenuShell *menu_shell,
83 GtkCMOptionMenu *option_menu);
84 static void gtk_cmoption_menu_update_contents (GtkCMOptionMenu *option_menu);
85 static void gtk_cmoption_menu_remove_contents (GtkCMOptionMenu *option_menu);
86 static void gtk_cmoption_menu_calc_size (GtkCMOptionMenu *option_menu);
87 static void gtk_cmoption_menu_position (GtkMenu *menu,
88 gint *x,
89 gint *y,
90 gint *scroll_offet,
91 gpointer user_data);
92 static void gtk_cmoption_menu_show_all (GtkWidget *widget);
93 static void gtk_cmoption_menu_hide_all (GtkWidget *widget);
94 static gboolean gtk_cmoption_menu_mnemonic_activate (GtkWidget *widget,
95 gboolean group_cycling);
96 static GType gtk_cmoption_menu_child_type (GtkContainer *container);
97 static gint gtk_cmoption_menu_scroll_event (GtkWidget *widget,
98 GdkEventScroll *event);
99
100 enum
101 {
102 CHANGED,
103 LAST_SIGNAL
104 };
105
106 enum
107 {
108 PROP_0,
109 PROP_MENU,
110 LAST_PROP
111 };
112
113 static guint signals[LAST_SIGNAL] = { 0 };
114
G_DEFINE_TYPE(GtkCMOptionMenu,gtk_cmoption_menu,GTK_TYPE_BUTTON)115 G_DEFINE_TYPE (GtkCMOptionMenu, gtk_cmoption_menu, GTK_TYPE_BUTTON)
116
117 static void
118 gtk_cmoption_menu_class_init (GtkCMOptionMenuClass *class)
119 {
120 GObjectClass *gobject_class;
121 GtkObjectClass *object_class;
122 GtkWidgetClass *widget_class;
123 GtkContainerClass *container_class;
124
125 gobject_class = (GObjectClass*) class;
126 object_class = (GtkObjectClass*) class;
127 widget_class = (GtkWidgetClass*) class;
128 container_class = (GtkContainerClass*) class;
129
130 signals[CHANGED] =
131 g_signal_new (("changed"),
132 G_OBJECT_CLASS_TYPE (class),
133 G_SIGNAL_RUN_LAST,
134 G_STRUCT_OFFSET (GtkCMOptionMenuClass, changed),
135 NULL, NULL,
136 claws_marshal_VOID__VOID,
137 G_TYPE_NONE, 0);
138
139 gobject_class->set_property = gtk_cmoption_menu_set_property;
140 gobject_class->get_property = gtk_cmoption_menu_get_property;
141 object_class->destroy = gtk_cmoption_menu_destroy;
142
143 widget_class->size_request = gtk_cmoption_menu_size_request;
144 widget_class->size_allocate = gtk_cmoption_menu_size_allocate;
145 widget_class->expose_event = gtk_cmoption_menu_expose;
146 widget_class->button_press_event = gtk_cmoption_menu_button_press;
147 widget_class->key_press_event = gtk_cmoption_menu_key_press;
148 widget_class->scroll_event = gtk_cmoption_menu_scroll_event;
149 widget_class->show_all = gtk_cmoption_menu_show_all;
150 widget_class->hide_all = gtk_cmoption_menu_hide_all;
151 widget_class->mnemonic_activate = gtk_cmoption_menu_mnemonic_activate;
152
153 container_class->child_type = gtk_cmoption_menu_child_type;
154
155 g_object_class_install_property (gobject_class,
156 PROP_MENU,
157 g_param_spec_object ("menu",
158 ("Menu"),
159 ("The menu of options"),
160 GTK_TYPE_MENU,
161 G_PARAM_READWRITE));
162
163 gtk_widget_class_install_style_property (widget_class,
164 g_param_spec_boxed ("indicator-size",
165 ("Indicator Size"),
166 ("Size of dropdown indicator"),
167 GTK_TYPE_REQUISITION,
168 G_PARAM_READABLE));
169 gtk_widget_class_install_style_property (widget_class,
170 g_param_spec_boxed ("indicator-spacing",
171 ("Indicator Spacing"),
172 ("Spacing around indicator"),
173 GTK_TYPE_BORDER,
174 G_PARAM_READABLE));
175 }
176
177 static GType
gtk_cmoption_menu_child_type(GtkContainer * container)178 gtk_cmoption_menu_child_type (GtkContainer *container)
179 {
180 return G_TYPE_NONE;
181 }
182
183 static void
gtk_cmoption_menu_init(GtkCMOptionMenu * option_menu)184 gtk_cmoption_menu_init (GtkCMOptionMenu *option_menu)
185 {
186 gtk_widget_set_can_focus (GTK_WIDGET(option_menu), TRUE);
187 gtk_widget_set_can_default (GTK_WIDGET(option_menu), FALSE);
188 gtk_widget_set_receives_default (GTK_WIDGET(option_menu), FALSE);
189
190 option_menu->menu = NULL;
191 option_menu->menu_item = NULL;
192 option_menu->width = 0;
193 option_menu->height = 0;
194 }
195
196 GtkWidget*
gtk_cmoption_menu_new(void)197 gtk_cmoption_menu_new (void)
198 {
199 return g_object_new (GTK_TYPE_CMOPTION_MENU, NULL);
200 }
201
202 GtkWidget*
gtk_cmoption_menu_get_menu(GtkCMOptionMenu * option_menu)203 gtk_cmoption_menu_get_menu (GtkCMOptionMenu *option_menu)
204 {
205 cm_return_val_if_fail (GTK_IS_CMOPTION_MENU (option_menu), NULL);
206
207 return option_menu->menu;
208 }
209
210 static void
gtk_cmoption_menu_detacher(GtkWidget * widget,GtkMenu * menu)211 gtk_cmoption_menu_detacher (GtkWidget *widget,
212 GtkMenu *menu)
213 {
214 GtkCMOptionMenu *option_menu;
215
216 cm_return_if_fail (GTK_IS_CMOPTION_MENU (widget));
217
218 option_menu = GTK_CMOPTION_MENU (widget);
219 cm_return_if_fail (option_menu->menu == (GtkWidget*) menu);
220
221 gtk_cmoption_menu_remove_contents (option_menu);
222 g_signal_handlers_disconnect_by_func (option_menu->menu,
223 gtk_cmoption_menu_selection_done,
224 option_menu);
225 g_signal_handlers_disconnect_by_func (option_menu->menu,
226 gtk_cmoption_menu_calc_size,
227 option_menu);
228
229 option_menu->menu = NULL;
230 g_object_notify (G_OBJECT (option_menu), "menu");
231 }
232
233 void
gtk_cmoption_menu_set_menu(GtkCMOptionMenu * option_menu,GtkWidget * menu)234 gtk_cmoption_menu_set_menu (GtkCMOptionMenu *option_menu,
235 GtkWidget *menu)
236 {
237 cm_return_if_fail (GTK_IS_CMOPTION_MENU (option_menu));
238 cm_return_if_fail (GTK_IS_MENU (menu));
239
240 if (option_menu->menu != menu)
241 {
242 gtk_cmoption_menu_remove_menu (option_menu);
243
244 option_menu->menu = menu;
245 gtk_menu_attach_to_widget (GTK_MENU (menu),
246 GTK_WIDGET (option_menu),
247 gtk_cmoption_menu_detacher);
248
249 gtk_cmoption_menu_calc_size (option_menu);
250
251 g_signal_connect_after (option_menu->menu, "selection-done",
252 G_CALLBACK (gtk_cmoption_menu_selection_done),
253 option_menu);
254 g_signal_connect_swapped (option_menu->menu, "size_request",
255 G_CALLBACK (gtk_cmoption_menu_calc_size),
256 option_menu);
257
258 if (gtk_widget_get_parent (GTK_WIDGET (option_menu)))
259 gtk_widget_queue_resize (GTK_WIDGET (option_menu));
260
261 gtk_cmoption_menu_update_contents (option_menu);
262
263 g_object_notify (G_OBJECT (option_menu), "menu");
264 }
265 }
266
267 void
gtk_cmoption_menu_remove_menu(GtkCMOptionMenu * option_menu)268 gtk_cmoption_menu_remove_menu (GtkCMOptionMenu *option_menu)
269 {
270 cm_return_if_fail (GTK_IS_CMOPTION_MENU (option_menu));
271
272 if (option_menu->menu)
273 {
274 if (option_menu->menu_item ==
275 gtk_menu_get_active (GTK_MENU (option_menu->menu)))
276 gtk_menu_shell_cancel (GTK_MENU_SHELL (option_menu->menu));
277
278 gtk_menu_detach (GTK_MENU (option_menu->menu));
279 }
280 }
281
282 void
gtk_cmoption_menu_set_history(GtkCMOptionMenu * option_menu,guint index)283 gtk_cmoption_menu_set_history (GtkCMOptionMenu *option_menu,
284 guint index)
285 {
286 GtkWidget *menu_item;
287
288 cm_return_if_fail (GTK_IS_CMOPTION_MENU (option_menu));
289
290 if (option_menu->menu)
291 {
292 gtk_menu_set_active (GTK_MENU (option_menu->menu), index);
293 menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
294
295 if (menu_item != option_menu->menu_item)
296 gtk_cmoption_menu_update_contents (option_menu);
297 }
298 }
299
300 /**
301 * gtk_cmoption_menu_get_history:
302 * @option_menu: a #GtkCMOptionMenu
303 *
304 * Retrieves the index of the currently selected menu item. The menu
305 * items are numbered from top to bottom, starting with 0.
306 *
307 * Return value: index of the selected menu item, or -1 if there are no menu items
308 * Deprecated: 2.4: Use #GtkComboBox instead.
309 **/
310 gint
gtk_cmoption_menu_get_history(GtkCMOptionMenu * option_menu)311 gtk_cmoption_menu_get_history (GtkCMOptionMenu *option_menu)
312 {
313 GtkWidget *active_widget;
314 GList *list;
315 gint index;
316
317 cm_return_val_if_fail (GTK_IS_CMOPTION_MENU (option_menu), -1);
318
319 if (option_menu->menu)
320 {
321 active_widget = gtk_menu_get_active (GTK_MENU (option_menu->menu));
322
323 if (active_widget)
324 {
325 list = gtk_container_get_children (GTK_CONTAINER
326 (GTK_MENU_SHELL (option_menu->menu)));
327 index = g_list_index (list, active_widget);
328 g_list_free (list);
329
330 return index;
331 }
332 else
333 return -1;
334 }
335 else
336 return -1;
337 }
338
339 static void
gtk_cmoption_menu_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)340 gtk_cmoption_menu_set_property (GObject *object,
341 guint prop_id,
342 const GValue *value,
343 GParamSpec *pspec)
344 {
345 GtkCMOptionMenu *option_menu = GTK_CMOPTION_MENU (object);
346
347 switch (prop_id)
348 {
349 case PROP_MENU:
350 gtk_cmoption_menu_set_menu (option_menu, g_value_get_object (value));
351 break;
352
353 default:
354 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
355 break;
356 }
357 }
358
359 static void
gtk_cmoption_menu_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)360 gtk_cmoption_menu_get_property (GObject *object,
361 guint prop_id,
362 GValue *value,
363 GParamSpec *pspec)
364 {
365 GtkCMOptionMenu *option_menu = GTK_CMOPTION_MENU (object);
366
367 switch (prop_id)
368 {
369 case PROP_MENU:
370 g_value_set_object (value, option_menu->menu);
371 break;
372
373 default:
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
375 break;
376 }
377 }
378
379 static void
gtk_cmoption_menu_destroy(GtkObject * object)380 gtk_cmoption_menu_destroy (GtkObject *object)
381 {
382 GtkCMOptionMenu *option_menu;
383
384 cm_return_if_fail (GTK_IS_CMOPTION_MENU (object));
385
386 option_menu = GTK_CMOPTION_MENU (object);
387
388 if (option_menu->menu)
389 gtk_widget_destroy (option_menu->menu);
390
391 if (GTK_OBJECT_CLASS (gtk_cmoption_menu_parent_class)->destroy)
392 (* GTK_OBJECT_CLASS (gtk_cmoption_menu_parent_class)->destroy) (object);
393 }
394
395 static void
gtk_cmoption_menu_get_props(GtkCMOptionMenu * option_menu,GtkCMOptionMenuProps * props)396 gtk_cmoption_menu_get_props (GtkCMOptionMenu *option_menu,
397 GtkCMOptionMenuProps *props)
398 {
399 GtkRequisition *indicator_size;
400 GtkBorder *indicator_spacing;
401
402 gtk_widget_style_get (GTK_WIDGET (option_menu),
403 "indicator-size", &indicator_size,
404 "indicator-spacing", &indicator_spacing,
405 "interior-focus", &props->interior_focus,
406 "focus-line-width", &props->focus_width,
407 "focus-padding", &props->focus_pad,
408 NULL);
409
410 if (indicator_size)
411 props->indicator_size = *indicator_size;
412 else
413 props->indicator_size = default_props.indicator_size;
414
415 if (indicator_spacing)
416 props->indicator_spacing = *indicator_spacing;
417 else
418 props->indicator_spacing = default_props.indicator_spacing;
419
420 gtk_requisition_free (indicator_size);
421 gtk_border_free (indicator_spacing);
422 }
423
424 static void
gtk_cmoption_menu_size_request(GtkWidget * widget,GtkRequisition * requisition)425 gtk_cmoption_menu_size_request (GtkWidget *widget,
426 GtkRequisition *requisition)
427 {
428 GtkCMOptionMenu *option_menu = GTK_CMOPTION_MENU (widget);
429 GtkCMOptionMenuProps props;
430 gint tmp;
431 GtkWidget *child;
432 GtkRequisition child_requisition = { 0, 0 };
433 GtkStyle *style;
434 gint border_width;
435
436 gtk_cmoption_menu_get_props (option_menu, &props);
437
438 child = gtk_bin_get_child (GTK_BIN (option_menu));
439 if (child && gtk_widget_get_visible (child))
440 {
441 gtk_widget_size_request (child, &child_requisition);
442
443 requisition->width += child_requisition.width;
444 requisition->height += child_requisition.height;
445 }
446
447 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
448 style = gtk_widget_get_style (widget);
449 requisition->width = ((border_width +
450 style->xthickness + props.focus_pad) * 2 +
451 MAX (child_requisition.width, option_menu->width) +
452 props.indicator_size.width +
453 props.indicator_spacing.left + props.indicator_spacing.right +
454 CHILD_LEFT_SPACING + CHILD_RIGHT_SPACING + props.focus_width * 2);
455 requisition->height = ((border_width +
456 style->ythickness + props.focus_pad) * 2 +
457 MAX (child_requisition.height, option_menu->height) +
458 CHILD_TOP_SPACING + CHILD_BOTTOM_SPACING + props.focus_width * 2);
459
460 tmp = (requisition->height - MAX (child_requisition.height, option_menu->height) +
461 props.indicator_size.height + props.indicator_spacing.top + props.indicator_spacing.bottom);
462 requisition->height = MAX (requisition->height, tmp);
463 }
464
465 static void
gtk_cmoption_menu_size_allocate(GtkWidget * widget,GtkAllocation * allocation)466 gtk_cmoption_menu_size_allocate (GtkWidget *widget,
467 GtkAllocation *allocation)
468 {
469 GtkWidget *child;
470 GtkStyle *style;
471 GtkButton *button = GTK_BUTTON (widget);
472 GtkAllocation child_allocation, widget_allocation;
473 GtkCMOptionMenuProps props;
474 gint border_width;
475
476 gtk_cmoption_menu_get_props (GTK_CMOPTION_MENU (widget), &props);
477 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
478
479 gtk_widget_set_allocation (widget, allocation);
480 if (gtk_widget_get_realized (widget))
481 gdk_window_move_resize (gtk_button_get_event_window (button),
482 allocation->x + border_width, allocation->y + border_width,
483 allocation->width - border_width * 2, allocation->height - border_width * 2);
484
485 child = gtk_bin_get_child (GTK_BIN (widget));
486 if (child && gtk_widget_get_visible (child))
487 {
488 style = gtk_widget_get_style (widget);
489 gint xthickness = style->xthickness;
490 gint ythickness = style->ythickness;
491
492 gtk_widget_get_allocation (widget, &widget_allocation);
493 child_allocation.x = widget_allocation.x + border_width + xthickness + props.focus_width + props.focus_pad + CHILD_LEFT_SPACING;
494 child_allocation.y = widget_allocation.y + border_width + ythickness + props.focus_width + props.focus_pad + CHILD_TOP_SPACING;
495 child_allocation.width = MAX (1, allocation->width - (border_width + xthickness + props.focus_width + props.focus_pad) * 2 -
496 props.indicator_size.width - props.indicator_spacing.left - props.indicator_spacing.right -
497 CHILD_LEFT_SPACING - CHILD_RIGHT_SPACING);
498 child_allocation.height = MAX (1, allocation->height - (border_width + ythickness + props.focus_width + props.focus_pad) * 2 -
499 CHILD_TOP_SPACING - CHILD_BOTTOM_SPACING);
500
501 if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
502 child_allocation.x += props.indicator_size.width + props.indicator_spacing.left + props.indicator_spacing.right;
503
504 gtk_widget_size_allocate (child, &child_allocation);
505 }
506 }
507
508 static void
gtk_cmoption_menu_paint(GtkWidget * widget,GdkRectangle * area)509 gtk_cmoption_menu_paint (GtkWidget *widget,
510 GdkRectangle *area)
511 {
512 GtkAllocation allocation;
513 GtkStyle *style;
514 GdkRectangle button_area;
515 GdkWindow *window;
516 GtkCMOptionMenuProps props;
517 gint border_width;
518 gint tab_x;
519
520 cm_return_if_fail (GTK_IS_CMOPTION_MENU (widget));
521 cm_return_if_fail (area != NULL);
522
523 if (gtk_widget_is_drawable (widget))
524 {
525 gtk_widget_get_allocation (widget, &allocation);
526 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
527 gtk_cmoption_menu_get_props (GTK_CMOPTION_MENU (widget), &props);
528
529 button_area.x = allocation.x + border_width;
530 button_area.y = allocation.y + border_width;
531 button_area.width = allocation.width - 2 * border_width;
532 button_area.height = allocation.height - 2 * border_width;
533
534 if (!props.interior_focus && gtk_widget_has_focus (widget))
535 {
536 button_area.x += props.focus_width + props.focus_pad;
537 button_area.y += props.focus_width + props.focus_pad;
538 button_area.width -= 2 * (props.focus_width + props.focus_pad);
539 button_area.height -= 2 * (props.focus_width + props.focus_pad);
540 }
541
542 style = gtk_widget_get_style (widget);
543 window = gtk_widget_get_window (widget);
544 gtk_paint_box (style, window,
545 gtk_widget_get_state (widget), GTK_SHADOW_OUT,
546 area, widget, "optionmenu",
547 button_area.x, button_area.y,
548 button_area.width, button_area.height);
549
550 if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
551 tab_x = button_area.x + props.indicator_spacing.right +
552 style->xthickness;
553 else
554 tab_x = button_area.x + button_area.width -
555 props.indicator_size.width - props.indicator_spacing.right -
556 style->xthickness;
557
558 gtk_paint_tab (style, window,
559 gtk_widget_get_state (widget), GTK_SHADOW_OUT,
560 area, widget, "optionmenutab",
561 tab_x,
562 button_area.y + (button_area.height - props.indicator_size.height) / 2,
563 props.indicator_size.width, props.indicator_size.height);
564
565 if (gtk_widget_has_focus (widget))
566 {
567 if (props.interior_focus)
568 {
569 button_area.x += style->xthickness + props.focus_pad;
570 button_area.y += style->ythickness + props.focus_pad;
571 button_area.width -= 2 * (style->xthickness + props.focus_pad) +
572 props.indicator_spacing.left +
573 props.indicator_spacing.right +
574 props.indicator_size.width;
575 button_area.height -= 2 * (style->ythickness + props.focus_pad);
576 if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
577 button_area.x += props.indicator_spacing.left +
578 props.indicator_spacing.right +
579 props.indicator_size.width;
580 }
581 else
582 {
583 button_area.x -= props.focus_width + props.focus_pad;
584 button_area.y -= props.focus_width + props.focus_pad;
585 button_area.width += 2 * (props.focus_width + props.focus_pad);
586 button_area.height += 2 * (props.focus_width + props.focus_pad);
587 }
588
589 gtk_paint_focus (style, window, gtk_widget_get_state (widget),
590 area, widget, "button",
591 button_area.x,
592 button_area.y,
593 button_area.width,
594 button_area.height);
595 }
596 }
597 }
598
599 static gint
gtk_cmoption_menu_expose(GtkWidget * widget,GdkEventExpose * event)600 gtk_cmoption_menu_expose (GtkWidget *widget,
601 GdkEventExpose *event)
602 {
603 GtkWidget *child;
604 cm_return_val_if_fail (GTK_IS_CMOPTION_MENU (widget), FALSE);
605 cm_return_val_if_fail (event != NULL, FALSE);
606
607 if (gtk_widget_is_drawable (widget))
608 {
609 gtk_cmoption_menu_paint (widget, &event->area);
610
611
612 /* The following code tries to draw the child in two places at
613 * once. It fails miserably for several reasons
614 *
615 * - If the child is not no-window, removing generates
616 * more expose events. Bad, bad, bad.
617 *
618 * - Even if the child is no-window, removing it now (properly)
619 * clears the space where it was, so it does no good
620 */
621
622 #if 0
623 remove_child = FALSE;
624 child = GTK_BUTTON (widget)->child;
625
626 if (!child)
627 {
628 if (!GTK_CMOPTION_MENU (widget)->menu)
629 return FALSE;
630 gtk_cmoption_menu_update_contents (GTK_CMOPTION_MENU (widget));
631 child = GTK_BUTTON (widget)->child;
632 if (!child)
633 return FALSE;
634 remove_child = TRUE;
635 }
636
637 child_event = *event;
638
639 if (GTK_WIDGET_NO_WINDOW (child) &&
640 gtk_widget_intersect (child, &event->area, &child_event.area))
641 gtk_widget_event (child, (GdkEvent*) &child_event);
642
643 if (remove_child)
644 gtk_cmoption_menu_remove_contents (GTK_CMOPTION_MENU (widget));
645 #else
646 child = gtk_bin_get_child (GTK_BIN (widget));
647 if (child)
648 gtk_container_propagate_expose (GTK_CONTAINER (widget),
649 child,
650 event);
651 #endif /* 0 */
652 }
653
654 return FALSE;
655 }
656
657 static gint
gtk_cmoption_menu_button_press(GtkWidget * widget,GdkEventButton * event)658 gtk_cmoption_menu_button_press (GtkWidget *widget,
659 GdkEventButton *event)
660 {
661 GtkCMOptionMenu *option_menu;
662 GtkWidget *menu_item;
663
664 cm_return_val_if_fail (GTK_IS_CMOPTION_MENU (widget), FALSE);
665 cm_return_val_if_fail (event != NULL, FALSE);
666
667 option_menu = GTK_CMOPTION_MENU (widget);
668
669 if ((event->type == GDK_BUTTON_PRESS) &&
670 (event->button == 1))
671 {
672 gtk_cmoption_menu_remove_contents (option_menu);
673 gtk_menu_popup (GTK_MENU (option_menu->menu), NULL, NULL,
674 gtk_cmoption_menu_position, option_menu,
675 event->button, event->time);
676 menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
677 if (menu_item)
678 gtk_menu_shell_select_item (GTK_MENU_SHELL (option_menu->menu), menu_item);
679 return TRUE;
680 }
681
682 return FALSE;
683 }
684
685 static gint
gtk_cmoption_menu_key_press(GtkWidget * widget,GdkEventKey * event)686 gtk_cmoption_menu_key_press (GtkWidget *widget,
687 GdkEventKey *event)
688 {
689 GtkCMOptionMenu *option_menu;
690 GtkWidget *menu_item;
691
692 cm_return_val_if_fail (GTK_IS_CMOPTION_MENU (widget), FALSE);
693 cm_return_val_if_fail (event != NULL, FALSE);
694
695 option_menu = GTK_CMOPTION_MENU (widget);
696
697 switch (event->keyval)
698 {
699 case GDK_KEY_KP_Space:
700 case GDK_KEY_space:
701 gtk_cmoption_menu_remove_contents (option_menu);
702 gtk_menu_popup (GTK_MENU (option_menu->menu), NULL, NULL,
703 gtk_cmoption_menu_position, option_menu,
704 0, event->time);
705 menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
706 if (menu_item)
707 gtk_menu_shell_select_item (GTK_MENU_SHELL (option_menu->menu), menu_item);
708 return TRUE;
709 }
710
711 return FALSE;
712 }
713
714 static void
gtk_cmoption_menu_selection_done(GtkMenuShell * menu_shell,GtkCMOptionMenu * option_menu)715 gtk_cmoption_menu_selection_done (GtkMenuShell *menu_shell,
716 GtkCMOptionMenu *option_menu)
717 {
718 cm_return_if_fail (menu_shell != NULL);
719 cm_return_if_fail (GTK_IS_CMOPTION_MENU (option_menu));
720
721 gtk_cmoption_menu_update_contents (option_menu);
722 }
723
724 static void
gtk_cmoption_menu_changed(GtkCMOptionMenu * option_menu)725 gtk_cmoption_menu_changed (GtkCMOptionMenu *option_menu)
726 {
727 cm_return_if_fail (GTK_IS_CMOPTION_MENU (option_menu));
728
729 g_signal_emit (option_menu, signals[CHANGED], 0);
730 }
731
732 static void
gtk_cmoption_menu_select_first_sensitive(GtkCMOptionMenu * option_menu)733 gtk_cmoption_menu_select_first_sensitive (GtkCMOptionMenu *option_menu)
734 {
735 if (option_menu->menu)
736 {
737 GList *children = gtk_container_get_children (GTK_CONTAINER (GTK_MENU_SHELL (option_menu->menu)));
738 gint index = 0;
739
740 while (children)
741 {
742 if (gtk_widget_get_sensitive (children->data))
743 {
744 gtk_cmoption_menu_set_history (option_menu, index);
745 return;
746 }
747
748 children = children->next;
749 index++;
750 }
751
752 g_list_free (children);
753 }
754 }
755
756 static void
gtk_cmoption_menu_item_state_changed_cb(GtkWidget * widget,GtkStateType previous_state,GtkCMOptionMenu * option_menu)757 gtk_cmoption_menu_item_state_changed_cb (GtkWidget *widget,
758 GtkStateType previous_state,
759 GtkCMOptionMenu *option_menu)
760 {
761 GtkWidget *child = gtk_bin_get_child (GTK_BIN (option_menu));
762
763 if (child && gtk_widget_get_sensitive (child) != gtk_widget_is_sensitive (widget))
764 gtk_widget_set_sensitive (child, gtk_widget_is_sensitive (widget));
765 }
766
767 static void
gtk_cmoption_menu_item_destroy_cb(GtkWidget * widget,GtkCMOptionMenu * option_menu)768 gtk_cmoption_menu_item_destroy_cb (GtkWidget *widget,
769 GtkCMOptionMenu *option_menu)
770 {
771 GtkWidget *child = gtk_bin_get_child (GTK_BIN (option_menu));
772
773 if (child)
774 {
775 g_object_ref (child);
776 gtk_cmoption_menu_remove_contents (option_menu);
777 gtk_widget_destroy (child);
778 g_object_unref (child);
779
780 gtk_cmoption_menu_select_first_sensitive (option_menu);
781 }
782 }
783
784 static void
gtk_cmoption_menu_update_contents(GtkCMOptionMenu * option_menu)785 gtk_cmoption_menu_update_contents (GtkCMOptionMenu *option_menu)
786 {
787 GtkWidget *child;
788 GtkRequisition child_requisition;
789 GtkAllocation allocation;
790
791 cm_return_if_fail (GTK_IS_CMOPTION_MENU (option_menu));
792
793 if (option_menu->menu)
794 {
795 GtkWidget *old_item = option_menu->menu_item;
796
797 gtk_cmoption_menu_remove_contents (option_menu);
798
799 option_menu->menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
800 if (option_menu->menu_item)
801 {
802 g_object_ref (option_menu->menu_item);
803 child = gtk_bin_get_child (GTK_BIN (option_menu->menu_item));
804 if (child)
805 {
806 if (!gtk_widget_is_sensitive (option_menu->menu_item))
807 gtk_widget_set_sensitive (child, FALSE);
808 gtk_widget_reparent (child, GTK_WIDGET (option_menu));
809 }
810
811 g_signal_connect (option_menu->menu_item, "state_changed",
812 G_CALLBACK (gtk_cmoption_menu_item_state_changed_cb), option_menu);
813 g_signal_connect (option_menu->menu_item, "destroy",
814 G_CALLBACK (gtk_cmoption_menu_item_destroy_cb), option_menu);
815
816 gtk_widget_size_request (child, &child_requisition);
817 gtk_widget_get_allocation (GTK_WIDGET (option_menu), &allocation);
818 gtk_widget_size_allocate (GTK_WIDGET (option_menu),
819 &allocation);
820
821 if (gtk_widget_is_drawable (GTK_WIDGET(option_menu)))
822 gtk_widget_queue_draw (GTK_WIDGET (option_menu));
823 }
824
825 if (old_item != option_menu->menu_item)
826 gtk_cmoption_menu_changed (option_menu);
827 }
828 }
829
830 static void
gtk_cmoption_menu_remove_contents(GtkCMOptionMenu * option_menu)831 gtk_cmoption_menu_remove_contents (GtkCMOptionMenu *option_menu)
832 {
833 GtkWidget *child;
834
835 cm_return_if_fail (GTK_IS_CMOPTION_MENU (option_menu));
836
837 if (option_menu->menu_item)
838 {
839 child = gtk_bin_get_child (GTK_BIN (option_menu));
840
841 if (child)
842 {
843 gtk_widget_set_sensitive (child, TRUE);
844 gtk_widget_set_state (child, GTK_STATE_NORMAL);
845 gtk_widget_reparent (child, option_menu->menu_item);
846 }
847
848 g_signal_handlers_disconnect_by_func (option_menu->menu_item,
849 gtk_cmoption_menu_item_state_changed_cb,
850 option_menu);
851 g_signal_handlers_disconnect_by_func (option_menu->menu_item,
852 gtk_cmoption_menu_item_destroy_cb,
853 option_menu);
854
855 g_object_unref (option_menu->menu_item);
856 option_menu->menu_item = NULL;
857 }
858 }
859
860 static void
gtk_cmoption_menu_calc_size(GtkCMOptionMenu * option_menu)861 gtk_cmoption_menu_calc_size (GtkCMOptionMenu *option_menu)
862 {
863 GtkWidget *child;
864 GList *children, *walk;
865 GtkRequisition child_requisition;
866 gint old_width = option_menu->width;
867 gint old_height = option_menu->height;
868
869 cm_return_if_fail (GTK_IS_CMOPTION_MENU (option_menu));
870
871 option_menu->width = 0;
872 option_menu->height = 0;
873
874 if (option_menu->menu)
875 {
876 children = gtk_container_get_children (GTK_CONTAINER (GTK_MENU_SHELL (option_menu->menu)));
877 walk = children;
878 while (walk)
879 {
880 child = walk->data;
881 walk = walk->next;
882
883 if (gtk_widget_get_visible (child))
884 {
885 GtkWidget *inner = gtk_bin_get_child (GTK_BIN (child));
886
887 if (inner)
888 {
889 gtk_widget_size_request (inner, &child_requisition);
890
891 option_menu->width = MAX (option_menu->width, child_requisition.width);
892 option_menu->height = MAX (option_menu->height, child_requisition.height);
893 }
894 }
895 }
896
897 g_list_free(children);
898 }
899
900 if (old_width != option_menu->width || old_height != option_menu->height)
901 gtk_widget_queue_resize (GTK_WIDGET (option_menu));
902 }
903
904 static void
gtk_cmoption_menu_position(GtkMenu * menu,gint * x,gint * y,gboolean * push_in,gpointer user_data)905 gtk_cmoption_menu_position (GtkMenu *menu,
906 gint *x,
907 gint *y,
908 gboolean *push_in,
909 gpointer user_data)
910 {
911 GtkCMOptionMenu *option_menu;
912 GtkWidget *active;
913 GtkWidget *child;
914 GtkWidget *widget;
915 GtkRequisition requisition;
916 GtkAllocation allocation;
917 GList *children, *cur;
918 gint screen_width;
919 gint menu_xpos;
920 gint menu_ypos;
921 gint menu_width;
922
923 cm_return_if_fail (GTK_IS_CMOPTION_MENU (user_data));
924
925 option_menu = GTK_CMOPTION_MENU (user_data);
926 widget = GTK_WIDGET (option_menu);
927
928 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
929 menu_width = requisition.width;
930
931 active = gtk_menu_get_active (GTK_MENU (option_menu->menu));
932 gdk_window_get_origin (gtk_widget_get_window (widget), &menu_xpos, &menu_ypos);
933
934 gtk_widget_get_allocation (widget, &allocation);
935 menu_xpos += allocation.x;
936 menu_ypos += allocation.y + allocation.height / 2 - 2;
937
938 if (active != NULL)
939 {
940 gtk_widget_get_child_requisition (active, &requisition);
941 menu_ypos -= requisition.height / 2;
942 }
943
944 children = gtk_container_get_children (GTK_CONTAINER (GTK_MENU_SHELL (option_menu->menu)));
945 for (cur = children; cur && cur->data; cur = cur->next)
946 {
947 child = cur->data;
948
949 if (active == child)
950 break;
951
952 if (gtk_widget_get_visible (child))
953 {
954 gtk_widget_get_child_requisition (child, &requisition);
955 menu_ypos -= requisition.height;
956 }
957 }
958
959 g_list_free (children);
960
961 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
962 menu_xpos = menu_xpos + allocation.width - menu_width;
963
964 /* Clamp the position on screen */
965 screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
966
967 if (menu_xpos < 0)
968 menu_xpos = 0;
969 else if ((menu_xpos + menu_width) > screen_width)
970 menu_xpos -= ((menu_xpos + menu_width) - screen_width);
971
972 *x = menu_xpos;
973 *y = menu_ypos;
974 *push_in = TRUE;
975 }
976
977
978 static void
gtk_cmoption_menu_show_all(GtkWidget * widget)979 gtk_cmoption_menu_show_all (GtkWidget *widget)
980 {
981 GtkContainer *container;
982 GtkCMOptionMenu *option_menu;
983
984 cm_return_if_fail (GTK_IS_CMOPTION_MENU (widget));
985 container = GTK_CONTAINER (widget);
986 option_menu = GTK_CMOPTION_MENU (widget);
987
988 gtk_widget_show (widget);
989 gtk_container_foreach (container, (GtkCallback) gtk_widget_show_all, NULL);
990 if (option_menu->menu)
991 gtk_widget_show_all (option_menu->menu);
992 if (option_menu->menu_item)
993 gtk_widget_show_all (option_menu->menu_item);
994 }
995
996
997 static void
gtk_cmoption_menu_hide_all(GtkWidget * widget)998 gtk_cmoption_menu_hide_all (GtkWidget *widget)
999 {
1000 GtkContainer *container;
1001
1002 cm_return_if_fail (GTK_IS_CMOPTION_MENU (widget));
1003 container = GTK_CONTAINER (widget);
1004
1005 gtk_widget_hide (widget);
1006 gtk_container_foreach (container, (GtkCallback) gtk_widget_hide, NULL);
1007 }
1008
1009 static gboolean
gtk_cmoption_menu_mnemonic_activate(GtkWidget * widget,gboolean group_cycling)1010 gtk_cmoption_menu_mnemonic_activate (GtkWidget *widget,
1011 gboolean group_cycling)
1012 {
1013 gtk_widget_grab_focus (widget);
1014 return TRUE;
1015 }
1016
1017 static gint
gtk_cmoption_menu_scroll_event(GtkWidget * widget,GdkEventScroll * event)1018 gtk_cmoption_menu_scroll_event (GtkWidget *widget,
1019 GdkEventScroll *event)
1020 {
1021 GtkCMOptionMenu *option_menu = GTK_CMOPTION_MENU (widget);
1022 gint index, n_children;
1023 gint index_dir;
1024 GList *l, *children;
1025 GtkMenuItem *item;
1026
1027 index = gtk_cmoption_menu_get_history (option_menu);
1028
1029 if (index != -1)
1030 {
1031 children = gtk_container_get_children (GTK_CONTAINER (GTK_MENU_SHELL (option_menu->menu)));
1032 n_children = (gint)g_list_length (children);
1033
1034 if (event->direction == GDK_SCROLL_UP)
1035 index_dir = -1;
1036 else
1037 index_dir = 1;
1038
1039
1040 while (TRUE)
1041 {
1042 index += index_dir;
1043
1044 if (index < 0)
1045 break;
1046 if (index >= n_children)
1047 break;
1048
1049 l = g_list_nth (children, (guint)index);
1050 item = GTK_MENU_ITEM (l->data);
1051 if (gtk_widget_get_visible (GTK_WIDGET(item)) &&
1052 gtk_widget_is_sensitive (GTK_WIDGET(item)))
1053 {
1054 gtk_cmoption_menu_set_history (option_menu, index);
1055 gtk_menu_item_activate (GTK_MENU_ITEM (item));
1056 break;
1057 }
1058
1059 }
1060
1061 g_list_free (children);
1062 }
1063
1064 return TRUE;
1065 }
1066
1067