12a80c4ddde001974fbe736dbff4d8ec2ac5b19a
[hildon] / hildon / hildon-app-menu.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser Public License as published by
10  * the Free Software Foundation; version 2 of the license.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser Public License for more details.
16  *
17  */
18
19 /**
20  * SECTION:hildon-app-menu
21  * @short_description: Application menu for Hildon applications.
22  *
23  * #HildonAppMenu is an application menu for applications in the Hildon
24  * framework.
25  *
26  * This menu opens from the top of the screen and contains a number of
27  * entries (#GtkButton) organized in one or two columns, depending on
28  * the size of the screen (the number of columns changes automatically
29  * if the screen is resized). Entries are added left to right and top
30  * to bottom.
31  *
32  * Besides that, #HildonAppMenu can contain a group of filter buttons
33  * (#GtkToggleButton or #GtkRadioButton). Filters are meant to change
34  * the way data is presented in the application, rather than change
35  * the layout of the menu itself. For example, a file manager can have
36  * filters to decide the order used to display a list of files (name,
37  * date, size, etc.).
38  *
39  * To use a #HildonAppMenu, add it to a #HildonWindow using
40  * hildon_window_set_app_menu(). The menu will appear when the user
41  * presses the window title bar. Alternatively, you can show it by
42  * hand using hildon_app_menu_popup().
43  *
44  * The menu will be automatically hidden when one of its buttons is
45  * clicked. Use g_signal_connect_after() when connecting callbacks to
46  * buttons to make sure that they're called after the menu
47  * disappears. Alternatively, you can add the button to the menu
48  * before connecting any callback.
49  *
50  * Although implemented with a #GtkWindow, #HildonAppMenu behaves like
51  * a normal ref-counted widget, so g_object_ref(), g_object_unref(),
52  * g_object_ref_sink() and friends will behave just like with any
53  * other non-toplevel widget.
54  *
55  * <example>
56  * <title>Creating a HildonAppMenu</title>
57  * <programlisting>
58  * GtkWidget *win;
59  * HildonAppMenu *menu;
60  * GtkWidget *button;
61  * GtkWidget *filter;
62  * <!-- -->
63  * win = hildon_stackable_window_new ();
64  * menu = HILDON_APP_MENU (hildon_app_menu_new ());
65  * <!-- -->
66  * // Create a button and add it to the menu
67  * button = gtk_button_new_with_label ("Menu command one");
68  * g_signal_connect_after (button, "clicked", G_CALLBACK (button_one_clicked), userdata);
69  * hildon_app_menu_append (menu, GTK_BUTTON (button));
70  * <!-- -->
71  * // Another button
72  * button = gtk_button_new_with_label ("Menu command two");
73  * g_signal_connect_after (button, "clicked", G_CALLBACK (button_two_clicked), userdata);
74  * hildon_app_menu_append (menu, GTK_BUTTON (button));
75  * <!-- -->
76  * // Create a filter and add it to the menu
77  * filter = gtk_radio_button_new_with_label (NULL, "Filter one");
78  * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
79  * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_one_clicked), userdata);
80  * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
81  * <!-- -->
82  * // Add a new filter
83  * filter = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (filter), "Filter two");
84  * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
85  * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_two_clicked), userdata);
86  * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
87  * <!-- -->
88  * // Show all menu items
89  * gtk_widget_show_all (GTK_WIDGET (menu));
90  * <!-- -->
91  * // Add the menu to the window
92  * hildon_window_set_app_menu (HILDON_WINDOW (win), menu);
93  * </programlisting>
94  * </example>
95  *
96  */
97
98 #include                                        <string.h>
99 #include                                        <X11/Xatom.h>
100 #include                                        <gdk/gdkx.h>
101
102 #include                                        "hildon-gtk.h"
103 #include                                        "hildon-app-menu.h"
104 #include                                        "hildon-app-menu-private.h"
105 #include                                        "hildon-window.h"
106 #include                                        "hildon-banner.h"
107 #include                                        "hildon-animation-actor.h"
108
109 static void
110 hildon_app_menu_repack_items                    (HildonAppMenu *menu,
111                                                  gint           start_from);
112
113 static void
114 hildon_app_menu_repack_filters                  (HildonAppMenu *menu);
115
116 static gboolean
117 can_activate_accel                              (GtkWidget *widget,
118                                                  guint      signal_id,
119                                                  gpointer   user_data);
120
121 static void
122 item_visibility_changed                         (GtkWidget     *item,
123                                                  GParamSpec    *arg1,
124                                                  HildonAppMenu *menu);
125
126 static void
127 filter_visibility_changed                       (GtkWidget     *item,
128                                                  GParamSpec    *arg1,
129                                                  HildonAppMenu *menu);
130
131 static gboolean
132 menu_item_button_event                          (GtkButton      *item,
133                                                  GdkEventButton *event,
134                                                  GtkWidget      *menu);
135
136 static void
137 remove_item_from_list                           (GList    **list,
138                                                  gpointer   item);
139
140 static void
141 hildon_app_menu_apply_style                     (GtkWidget *widget);
142
143 G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
144
145 /**
146  * hildon_app_menu_new:
147  *
148  * Creates a new #HildonAppMenu.
149  *
150  * Return value: A #HildonAppMenu.
151  *
152  * Since: 2.2
153  **/
154 GtkWidget *
155 hildon_app_menu_new                             (void)
156 {
157     GtkWidget *menu = g_object_new (HILDON_TYPE_APP_MENU, NULL);
158     return menu;
159 }
160
161 /**
162  * hildon_app_menu_insert:
163  * @menu : A #HildonAppMenu
164  * @item : A #GtkButton to add to the #HildonAppMenu
165  * @position : The position in the item list where @item is added (from 0 to n-1).
166  *
167  * Adds @item to @menu at the position indicated by @position.
168  *
169  * Since: 2.2
170  */
171 void
172 hildon_app_menu_insert                          (HildonAppMenu *menu,
173                                                  GtkButton     *item,
174                                                  gint           position)
175 {
176     HildonAppMenuPrivate *priv;
177
178     g_return_if_fail (HILDON_IS_APP_MENU (menu));
179     g_return_if_fail (GTK_IS_BUTTON (item));
180
181     priv = HILDON_APP_MENU_GET_PRIVATE(menu);
182
183     /* Force widget size */
184     hildon_gtk_widget_set_theme_size (GTK_WIDGET (item),
185                                       HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
186
187     /* Add the item to the menu */
188     g_object_ref_sink (item);
189     priv->buttons = g_list_insert (priv->buttons, item, position);
190     if (GTK_WIDGET_VISIBLE (item))
191         hildon_app_menu_repack_items (menu, position);
192
193     /* Enable accelerators */
194     g_signal_connect (item, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
195
196     /* Close the menu when the button is clicked */
197     g_signal_connect_swapped (item, "clicked", G_CALLBACK (gtk_widget_hide), menu);
198     g_signal_connect (item, "notify::visible", G_CALLBACK (item_visibility_changed), menu);
199
200     /* Remove item from list when it is destroyed */
201     g_object_weak_ref (G_OBJECT (item), (GWeakNotify) remove_item_from_list, &(priv->buttons));
202 }
203
204 /**
205  * hildon_app_menu_append:
206  * @menu : A #HildonAppMenu
207  * @item : A #GtkButton to add to the #HildonAppMenu
208  *
209  * Adds @item to the end of the menu's item list.
210  *
211  * Since: 2.2
212  */
213 void
214 hildon_app_menu_append                          (HildonAppMenu *menu,
215                                                  GtkButton     *item)
216 {
217     hildon_app_menu_insert (menu, item, -1);
218 }
219
220 /**
221  * hildon_app_menu_prepend:
222  * @menu : A #HildonAppMenu
223  * @item : A #GtkButton to add to the #HildonAppMenu
224  *
225  * Adds @item to the beginning of the menu's item list.
226  *
227  * Since: 2.2
228  */
229 void
230 hildon_app_menu_prepend                         (HildonAppMenu *menu,
231                                                  GtkButton     *item)
232 {
233     hildon_app_menu_insert (menu, item, 0);
234 }
235
236 /**
237  * hildon_app_menu_reorder_child:
238  * @menu : A #HildonAppMenu
239  * @item : A #GtkButton to move
240  * @position : The new position to place @item (from 0 to n-1).
241  *
242  * Moves a #GtkButton to a new position within #HildonAppMenu.
243  *
244  * Since: 2.2
245  */
246 void
247 hildon_app_menu_reorder_child                   (HildonAppMenu *menu,
248                                                  GtkButton     *item,
249                                                  gint           position)
250 {
251     HildonAppMenuPrivate *priv;
252     gint old_position;
253
254     g_return_if_fail (HILDON_IS_APP_MENU (menu));
255     g_return_if_fail (GTK_IS_BUTTON (item));
256     g_return_if_fail (position >= 0);
257
258     priv = HILDON_APP_MENU_GET_PRIVATE (menu);
259     old_position = g_list_index (priv->buttons, item);
260
261     g_return_if_fail (old_position >= 0);
262
263     /* Move the item */
264     priv->buttons = g_list_remove (priv->buttons, item);
265     priv->buttons = g_list_insert (priv->buttons, item, position);
266
267     hildon_app_menu_repack_items (menu, MIN (old_position, position));
268 }
269
270 /**
271  * hildon_app_menu_add_filter:
272  * @menu : A #HildonAppMenu
273  * @filter : A #GtkButton to add to the #HildonAppMenu.
274  *
275  * Adds the @filter to @menu.
276  *
277  * Since: 2.2
278  */
279 void
280 hildon_app_menu_add_filter                      (HildonAppMenu *menu,
281                                                  GtkButton *filter)
282 {
283     HildonAppMenuPrivate *priv;
284
285     g_return_if_fail (HILDON_IS_APP_MENU (menu));
286     g_return_if_fail (GTK_IS_BUTTON (filter));
287
288     priv = HILDON_APP_MENU_GET_PRIVATE(menu);
289
290     /* Force widget size */
291     hildon_gtk_widget_set_theme_size (GTK_WIDGET (filter),
292                                       HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
293
294     /* Add the filter to the menu */
295     g_object_ref_sink (filter);
296     priv->filters = g_list_append (priv->filters, filter);
297     if (GTK_WIDGET_VISIBLE (filter))
298         hildon_app_menu_repack_filters (menu);
299
300     /* Enable accelerators */
301     g_signal_connect (filter, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
302
303     /* Close the menu when the button is clicked */
304     g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
305     g_signal_connect (filter, "notify::visible", G_CALLBACK (filter_visibility_changed), menu);
306
307     /* Remove filter from list when it is destroyed */
308     g_object_weak_ref (G_OBJECT (filter), (GWeakNotify) remove_item_from_list, &(priv->filters));
309 }
310
311 static void
312 hildon_app_menu_set_columns                     (HildonAppMenu *menu,
313                                                  guint          columns)
314 {
315     HildonAppMenuPrivate *priv;
316
317     g_return_if_fail (HILDON_IS_APP_MENU (menu));
318     g_return_if_fail (columns > 0);
319
320     priv = HILDON_APP_MENU_GET_PRIVATE (menu);
321
322     if (columns != priv->columns) {
323         priv->columns = columns;
324         hildon_app_menu_repack_items (menu, 0);
325     }
326 }
327
328 static void
329 parent_window_topmost_notify                   (HildonWindow *parent_win,
330                                                 GParamSpec   *arg1,
331                                                 GtkWidget    *menu)
332 {
333     if (!hildon_window_get_is_topmost (parent_win))
334         gtk_widget_hide (menu);
335 }
336
337 static void
338 parent_window_unmapped                         (HildonWindow *parent_win,
339                                                 GtkWidget    *menu)
340 {
341     gtk_widget_hide (menu);
342 }
343
344 void G_GNUC_INTERNAL
345 hildon_app_menu_set_parent_window              (HildonAppMenu *self,
346                                                 GtkWindow     *parent_window)
347 {
348     HildonAppMenuPrivate *priv;
349
350     g_return_if_fail (HILDON_IS_APP_MENU (self));
351     g_return_if_fail (parent_window == NULL || GTK_IS_WINDOW (parent_window));
352
353     priv = HILDON_APP_MENU_GET_PRIVATE(self);
354
355     /* Disconnect old handlers, if any */
356     if (priv->parent_window) {
357         g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, self);
358         g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, self);
359     }
360
361     /* Connect a new handler */
362     if (parent_window) {
363         g_signal_connect (parent_window, "notify::is-topmost", G_CALLBACK (parent_window_topmost_notify), self);
364         g_signal_connect (parent_window, "unmap", G_CALLBACK (parent_window_unmapped), self);
365     }
366
367     priv->parent_window = parent_window;
368
369     if (parent_window == NULL && GTK_WIDGET_VISIBLE (self))
370         gtk_widget_hide (GTK_WIDGET (self));
371 }
372
373 gpointer G_GNUC_INTERNAL
374 hildon_app_menu_get_parent_window              (HildonAppMenu *self)
375 {
376     HildonAppMenuPrivate *priv;
377
378     g_return_val_if_fail (HILDON_IS_APP_MENU (self), NULL);
379
380     priv = HILDON_APP_MENU_GET_PRIVATE (self);
381
382     return priv->parent_window;
383 }
384
385 static void
386 screen_size_changed                            (GdkScreen     *screen,
387                                                 HildonAppMenu *menu)
388 {
389     hildon_app_menu_apply_style (GTK_WIDGET (menu));
390
391     if (gdk_screen_get_width (screen) > gdk_screen_get_height (screen)) {
392         hildon_app_menu_set_columns (menu, 2);
393     } else {
394         hildon_app_menu_set_columns (menu, 1);
395     }
396 }
397
398 static gboolean
399 can_activate_accel                              (GtkWidget *widget,
400                                                  guint      signal_id,
401                                                  gpointer   user_data)
402 {
403     return GTK_WIDGET_VISIBLE (widget);
404 }
405
406 static void
407 item_visibility_changed                         (GtkWidget     *item,
408                                                  GParamSpec    *arg1,
409                                                  HildonAppMenu *menu)
410 {
411     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
412
413     if (! priv->inhibit_repack)
414         hildon_app_menu_repack_items (menu, g_list_index (priv->buttons, item));
415 }
416
417 static void
418 filter_visibility_changed                       (GtkWidget     *item,
419                                                  GParamSpec    *arg1,
420                                                  HildonAppMenu *menu)
421 {
422     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
423
424     if (! priv->inhibit_repack)
425         hildon_app_menu_repack_filters (menu);
426 }
427
428 static void
429 remove_item_from_list                           (GList    **list,
430                                                  gpointer   item)
431 {
432     *list = g_list_remove (*list, item);
433 }
434
435 static void
436 hildon_app_menu_show_all                        (GtkWidget *widget)
437 {
438     HildonAppMenu *menu = HILDON_APP_MENU (widget);
439     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
440
441     priv->inhibit_repack = TRUE;
442
443     /* Show children, but not self. */
444     g_list_foreach (priv->buttons, (GFunc) gtk_widget_show_all, NULL);
445     g_list_foreach (priv->filters, (GFunc) gtk_widget_show_all, NULL);
446
447     priv->inhibit_repack = FALSE;
448
449     hildon_app_menu_repack_items (menu, 0);
450     hildon_app_menu_repack_filters (menu);
451 }
452
453
454 static void
455 hildon_app_menu_hide_all                        (GtkWidget *widget)
456 {
457     HildonAppMenu *menu = HILDON_APP_MENU (widget);
458     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
459
460     priv->inhibit_repack = TRUE;
461
462     /* Hide children, but not self. */
463     g_list_foreach (priv->buttons, (GFunc) gtk_widget_hide_all, NULL);
464     g_list_foreach (priv->filters, (GFunc) gtk_widget_hide_all, NULL);
465
466     priv->inhibit_repack = FALSE;
467
468     hildon_app_menu_repack_items (menu, 0);
469     hildon_app_menu_repack_filters (menu);
470 }
471
472 /*
473  * There's a race condition that can freeze the UI if a dialog appears
474  * between a HildonAppMenu and its parent window, see NB#100468
475  */
476 static gboolean
477 hildon_app_menu_find_intruder                   (gpointer data)
478 {
479     GtkWidget *widget = GTK_WIDGET (data);
480     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
481
482     priv->find_intruder_idle_id = 0;
483
484     /* If there's a window between the menu and its parent window, hide the menu */
485     if (priv->parent_window) {
486         gboolean intruder_found = FALSE;
487         GdkScreen *screen = gtk_widget_get_screen (widget);
488         GList *stack = gdk_screen_get_window_stack (screen);
489         GList *parent_pos = g_list_find (stack, GTK_WIDGET (priv->parent_window)->window);
490         GList *toplevels = gtk_window_list_toplevels ();
491         GList *i;
492
493         for (i = toplevels; i != NULL && !intruder_found; i = i->next) {
494             if (i->data != widget && i->data != priv->parent_window) {
495                 if (g_list_find (parent_pos, GTK_WIDGET (i->data)->window)) {
496                     /* HildonBanners are not closed automatically when
497                      * a new window appears, so we must close them by
498                      * hand to make the AppMenu work as expected.
499                      * Yes, this is a hack. See NB#111027 */
500                     if (HILDON_IS_BANNER (i->data)) {
501                         gtk_widget_hide (i->data);
502                     } else if (!HILDON_IS_ANIMATION_ACTOR (i->data)) {
503                         intruder_found = TRUE;
504                     }
505                 }
506             }
507         }
508
509         g_list_foreach (stack, (GFunc) g_object_unref, NULL);
510         g_list_free (stack);
511         g_list_free (toplevels);
512
513         if (intruder_found)
514             gtk_widget_hide (widget);
515     }
516
517     return FALSE;
518 }
519
520 static void
521 hildon_app_menu_map                             (GtkWidget *widget)
522 {
523     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
524
525     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
526
527     if (priv->find_intruder_idle_id == 0)
528         priv->find_intruder_idle_id = gdk_threads_add_idle (hildon_app_menu_find_intruder, widget);
529 }
530
531 static gboolean
532 hildon_app_menu_hide_idle                       (gpointer widget)
533 {
534     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
535     gtk_widget_hide (GTK_WIDGET (widget));
536     priv->hide_idle_id = 0;
537     return FALSE;
538 }
539
540 /* Send keyboard accelerators to the parent window, if necessary.
541  * This code is heavily based on gtk_menu_key_press ()
542  */
543 static gboolean
544 hildon_app_menu_key_press                       (GtkWidget   *widget,
545                                                  GdkEventKey *event)
546 {
547     GtkWindow *parent_window;
548     HildonAppMenuPrivate *priv;
549
550     g_return_val_if_fail (HILDON_IS_APP_MENU (widget), FALSE);
551     g_return_val_if_fail (event != NULL, FALSE);
552
553     if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->key_press_event (widget, event))
554         return TRUE;
555
556     priv = HILDON_APP_MENU_GET_PRIVATE (widget);
557     parent_window = priv->parent_window;
558
559     if (parent_window) {
560         guint accel_key, accel_mods;
561         GdkModifierType consumed_modifiers;
562         GdkDisplay *display;
563         GSList *accel_groups;
564         GSList *list;
565
566         display = gtk_widget_get_display (widget);
567
568         /* Figure out what modifiers went into determining the key symbol */
569         gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
570                                              event->hardware_keycode, event->state, event->group,
571                                              NULL, NULL, NULL, &consumed_modifiers);
572
573         accel_key = gdk_keyval_to_lower (event->keyval);
574         accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
575
576         /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
577          * We re-upper case when we match against the keyval, but display and save in caseless form.
578          */
579         if (accel_key != event->keyval)
580             accel_mods |= GDK_SHIFT_MASK;
581
582         accel_groups = gtk_accel_groups_from_object (G_OBJECT (parent_window));
583
584         for (list = accel_groups; list; list = list->next) {
585             GtkAccelGroup *accel_group = list->data;
586
587             if (gtk_accel_group_query (accel_group, accel_key, accel_mods, NULL)) {
588                 gtk_window_activate_key (parent_window, event);
589                 priv->hide_idle_id = gdk_threads_add_idle (hildon_app_menu_hide_idle, widget);
590                 break;
591             }
592         }
593     }
594
595     return TRUE;
596 }
597
598 static gboolean
599 hildon_app_menu_delete_event_handler            (GtkWidget   *widget,
600                                                  GdkEventAny *event)
601 {
602     /* Hide the menu if it receives a delete-event, but don't destroy it */
603     gtk_widget_hide (widget);
604     return TRUE;
605 }
606
607 static void
608 hildon_app_menu_size_request                    (GtkWidget      *widget,
609                                                  GtkRequisition *requisition)
610 {
611     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
612
613     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->size_request (widget, requisition);
614
615     requisition->width = priv->width_request;
616 }
617
618 static void
619 hildon_app_menu_realize                         (GtkWidget *widget)
620 {
621     Atom property, window_type;
622     Display *xdisplay;
623     GdkDisplay *gdkdisplay;
624     GdkScreen *screen;
625
626     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
627
628     gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
629
630     gdkdisplay = gdk_drawable_get_display (widget->window);
631     xdisplay = GDK_WINDOW_XDISPLAY (widget->window);
632
633     property = gdk_x11_get_xatom_by_name_for_display (gdkdisplay, "_NET_WM_WINDOW_TYPE");
634     window_type = XInternAtom (xdisplay, "_HILDON_WM_WINDOW_TYPE_APP_MENU", False);
635     XChangeProperty (xdisplay, GDK_WINDOW_XID (widget->window), property,
636                      XA_ATOM, 32, PropModeReplace, (guchar *) &window_type, 1);
637
638     /* Detect any screen changes */
639     screen = gtk_widget_get_screen (widget);
640     g_signal_connect (screen, "size-changed", G_CALLBACK (screen_size_changed), widget);
641
642     /* Force menu to set the initial layout */
643     screen_size_changed (screen, HILDON_APP_MENU (widget));
644 }
645
646 static void
647 hildon_app_menu_unrealize                       (GtkWidget *widget)
648 {
649     GdkScreen *screen = gtk_widget_get_screen (widget);
650     /* Disconnect "size-changed" signal handler */
651     g_signal_handlers_disconnect_by_func (screen, G_CALLBACK (screen_size_changed), widget);
652
653     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unrealize (widget);
654 }
655
656 static void
657 hildon_app_menu_apply_style                     (GtkWidget *widget)
658 {
659     GdkScreen *screen;
660     gint filter_group_width;
661     guint horizontal_spacing, vertical_spacing, filter_vertical_spacing;
662     guint inner_border, external_border;
663     HildonAppMenuPrivate *priv;
664
665     priv = HILDON_APP_MENU_GET_PRIVATE (widget);
666
667     gtk_widget_style_get (widget,
668                           "horizontal-spacing", &horizontal_spacing,
669                           "vertical-spacing", &vertical_spacing,
670                           "filter-group-width", &filter_group_width,
671                           "filter-vertical-spacing", &filter_vertical_spacing,
672                           "inner-border", &inner_border,
673                           "external-border", &external_border,
674                           NULL);
675
676     /* Set spacings */
677     gtk_table_set_row_spacings (priv->table, vertical_spacing);
678     gtk_table_set_col_spacings (priv->table, horizontal_spacing);
679     gtk_box_set_spacing (priv->vbox, filter_vertical_spacing);
680
681     /* Set inner border */
682     gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
683
684     /* Set width of the group of filter buttons */
685     gtk_widget_set_size_request (GTK_WIDGET (priv->filters_hbox), filter_group_width, -1);
686
687     /* Compute width request */
688     screen = gtk_widget_get_screen (widget);
689     if (gdk_screen_get_width (screen) < gdk_screen_get_height (screen)) {
690         external_border = 0;
691     }
692     priv->width_request = gdk_screen_get_width (screen) - external_border * 2;
693
694     if (widget->window)
695       gdk_window_move_resize (widget->window,
696                               external_border, 0, 1, 1);
697
698     gtk_widget_queue_resize (widget);
699 }
700
701 static void
702 hildon_app_menu_style_set                       (GtkWidget *widget,
703                                                  GtkStyle  *previous_style)
704 {
705     if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set)
706         GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set (widget, previous_style);
707
708     hildon_app_menu_apply_style (widget);
709 }
710
711 static void
712 hildon_app_menu_repack_filters                  (HildonAppMenu *menu)
713 {
714     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
715     GList *iter;
716
717     for (iter = priv->filters; iter != NULL; iter = iter->next) {
718         GtkWidget *filter = GTK_WIDGET (iter->data);
719         GtkWidget *parent = gtk_widget_get_parent (filter);
720         if (parent) {
721             g_object_ref (filter);
722             gtk_container_remove (GTK_CONTAINER (parent), filter);
723         }
724     }
725
726     for (iter = priv->filters; iter != NULL; iter = iter->next) {
727         GtkWidget *filter = GTK_WIDGET (iter->data);
728         if (GTK_WIDGET_VISIBLE (filter)) {
729             gtk_box_pack_start (GTK_BOX (priv->filters_hbox), filter, TRUE, TRUE, 0);
730             g_object_unref (filter);
731             /* GtkButton must be realized for accelerators to work */
732             gtk_widget_realize (filter);
733         }
734     }
735 }
736
737 /*
738  * When items displayed in the menu change (e.g, a new item is added,
739  * an item is hidden or the list is reordered), the layout must be
740  * updated. To do this we repack all items starting from a given one.
741  */
742 static void
743 hildon_app_menu_repack_items                    (HildonAppMenu *menu,
744                                                  gint           start_from)
745 {
746     HildonAppMenuPrivate *priv;
747     gint row, col, nvisible, i;
748     GList *iter;
749
750     priv = HILDON_APP_MENU_GET_PRIVATE(menu);
751
752     i = nvisible = 0;
753     for (iter = priv->buttons; iter != NULL; iter = iter->next) {
754         /* Count number of visible items */
755         if (GTK_WIDGET_VISIBLE (iter->data))
756             nvisible++;
757         /* Remove buttons from their parent */
758         if (start_from != -1 && i >= start_from) {
759             GtkWidget *item = GTK_WIDGET (iter->data);
760             GtkWidget *parent = gtk_widget_get_parent (item);
761             if (parent) {
762                 g_object_ref (item);
763                 gtk_container_remove (GTK_CONTAINER (parent), item);
764             }
765         }
766         i++;
767     }
768
769     /* If items have been removed, recalculate the size of the menu */
770     if (start_from != -1)
771         gtk_window_resize (GTK_WINDOW (menu), 1, 1);
772
773     /* Set the final size now to avoid unnecessary resizes later */
774     if (nvisible > 0)
775         gtk_table_resize (priv->table, ((nvisible - 1) / priv->columns) + 1, priv->columns);
776
777     /* Add buttons */
778     row = col = 0;
779     for (iter = priv->buttons; iter != NULL; iter = iter->next) {
780         GtkWidget *item = GTK_WIDGET (iter->data);
781         if (GTK_WIDGET_VISIBLE (item)) {
782             /* Don't add an item to the table if it's already there */
783             if (gtk_widget_get_parent (item) == NULL) {
784                 gtk_table_attach_defaults (priv->table, item, col, col + 1, row, row + 1);
785                 g_object_unref (item);
786                 /* GtkButton must be realized for accelerators to work */
787                 gtk_widget_realize (item);
788             }
789             if (++col == priv->columns) {
790                 col = 0;
791                 row++;
792             }
793         }
794     }
795 }
796
797 /**
798  * hildon_app_menu_popup:
799  * @menu: a #HildonAppMenu
800  * @parent_window: a #GtkWindow
801  *
802  * Displays a menu on top of a window and makes it available for
803  * selection.
804  *
805  * Since: 2.2
806  **/
807 void
808 hildon_app_menu_popup                           (HildonAppMenu *menu,
809                                                  GtkWindow     *parent_window)
810 {
811     HildonAppMenuPrivate *priv;
812     gboolean show_menu = FALSE;
813     GList *i;
814
815     g_return_if_fail (HILDON_IS_APP_MENU (menu));
816     g_return_if_fail (GTK_IS_WINDOW (parent_window));
817
818     priv = HILDON_APP_MENU_GET_PRIVATE (menu);
819
820     /* Don't show menu if it doesn't contain visible items */
821     for (i = priv->buttons; i && !show_menu; i = i->next)
822         show_menu = GTK_WIDGET_VISIBLE (i->data);
823
824     for (i = priv->filters; i && !show_menu; i = i->next)
825         show_menu = GTK_WIDGET_VISIBLE (i->data);
826
827     if (show_menu) {
828         hildon_app_menu_set_parent_window (menu, parent_window);
829         gtk_widget_show (GTK_WIDGET (menu));
830     }
831
832 }
833
834 /**
835  * hildon_app_menu_get_items:
836  * @menu: a #HildonAppMenu
837  *
838  * Returns a list of all items (regular items, not filters) contained
839  * in @menu.
840  *
841  * Returns: a newly-allocated list containing the items in @menu
842  *
843  * Since: 2.2
844  **/
845 GList *
846 hildon_app_menu_get_items                       (HildonAppMenu *menu)
847 {
848     HildonAppMenuPrivate *priv;
849
850     g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
851
852     priv = HILDON_APP_MENU_GET_PRIVATE (menu);
853
854     return g_list_copy (priv->buttons);
855 }
856
857 /**
858  * hildon_app_menu_get_filters:
859  * @menu: a #HildonAppMenu
860  *
861  * Returns a list of all filters contained in @menu.
862  *
863  * Returns: a newly-allocated list containing the filters in @menu
864  *
865  * Since: 2.2
866  **/
867 GList *
868 hildon_app_menu_get_filters                     (HildonAppMenu *menu)
869 {
870     HildonAppMenuPrivate *priv;
871
872     g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
873
874     priv = HILDON_APP_MENU_GET_PRIVATE (menu);
875
876     return g_list_copy (priv->filters);
877 }
878
879 static void
880 hildon_app_menu_init                            (HildonAppMenu *menu)
881 {
882     GtkWidget *alignment;
883     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
884
885     /* Initialize private variables */
886     priv->parent_window = NULL;
887     priv->transfer_window = NULL;
888     priv->pressed_outside = FALSE;
889     priv->inhibit_repack = FALSE;
890     priv->last_pressed_button = NULL;
891     priv->buttons = NULL;
892     priv->filters = NULL;
893     priv->columns = 2;
894     priv->width_request = -1;
895     priv->find_intruder_idle_id = 0;
896     priv->hide_idle_id = 0;
897
898     /* Create boxes and tables */
899     priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
900     priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 0));
901     priv->table = GTK_TABLE (gtk_table_new (1, priv->columns, TRUE));
902
903     /* Align the filters to the center */
904     alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
905     gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
906
907     /* Pack everything */
908     gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
909     gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
910     gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
911
912     /* This should be treated like a normal, ref-counted widget */
913     g_object_force_floating (G_OBJECT (menu));
914     GTK_WINDOW (menu)->has_user_ref_count = FALSE;
915
916     gtk_widget_show_all (GTK_WIDGET (priv->vbox));
917 }
918
919 static void
920 hildon_app_menu_finalize                        (GObject *object)
921 {
922     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
923
924     if (priv->find_intruder_idle_id) {
925         g_source_remove (priv->find_intruder_idle_id);
926         priv->find_intruder_idle_id = 0;
927     }
928
929     if (priv->hide_idle_id) {
930         g_source_remove (priv->hide_idle_id);
931         priv->hide_idle_id = 0;
932     }
933
934     if (priv->parent_window) {
935         g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, object);
936         g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, object);
937     }
938
939     if (priv->transfer_window)
940         gdk_window_destroy (priv->transfer_window);
941
942     g_list_foreach (priv->buttons, (GFunc) g_object_unref, NULL);
943     g_list_foreach (priv->filters, (GFunc) g_object_unref, NULL);
944
945     g_list_free (priv->buttons);
946     g_list_free (priv->filters);
947
948     g_signal_handlers_destroy (object);
949     G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
950 }
951
952 static void
953 hildon_app_menu_class_init                      (HildonAppMenuClass *klass)
954 {
955     GObjectClass *gobject_class = (GObjectClass *)klass;
956     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
957
958     gobject_class->finalize = hildon_app_menu_finalize;
959     widget_class->show_all = hildon_app_menu_show_all;
960     widget_class->hide_all = hildon_app_menu_hide_all;
961     widget_class->map = hildon_app_menu_map;
962     widget_class->realize = hildon_app_menu_realize;
963     widget_class->unrealize = hildon_app_menu_unrealize;
964     widget_class->key_press_event = hildon_app_menu_key_press;
965     widget_class->style_set = hildon_app_menu_style_set;
966     widget_class->delete_event = hildon_app_menu_delete_event_handler;
967     widget_class->size_request = hildon_app_menu_size_request;
968
969     g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
970
971     gtk_widget_class_install_style_property (
972         widget_class,
973         g_param_spec_uint (
974             "horizontal-spacing",
975             "Horizontal spacing on menu items",
976             "Horizontal spacing between each menu item. Does not apply to filter buttons.",
977             0, G_MAXUINT, 16,
978             G_PARAM_READABLE));
979
980     gtk_widget_class_install_style_property (
981         widget_class,
982         g_param_spec_uint (
983             "vertical-spacing",
984             "Vertical spacing on menu items",
985             "Vertical spacing between each menu item. Does not apply to filter buttons.",
986             0, G_MAXUINT, 16,
987             G_PARAM_READABLE));
988
989     gtk_widget_class_install_style_property (
990         widget_class,
991         g_param_spec_int (
992             "filter-group-width",
993             "Width of the group of filter buttons",
994             "Total width of the group of filter buttons, "
995             "or -1 to use the natural size request.",
996             -1, G_MAXINT, 444,
997             G_PARAM_READABLE));
998
999     gtk_widget_class_install_style_property (
1000         widget_class,
1001         g_param_spec_uint (
1002             "filter-vertical-spacing",
1003             "Vertical spacing between filters and menu items",
1004             "Vertical spacing between filters and menu items",
1005             0, G_MAXUINT, 8,
1006             G_PARAM_READABLE));
1007
1008     gtk_widget_class_install_style_property (
1009         widget_class,
1010         g_param_spec_uint (
1011             "inner-border",
1012             "Border between menu edges and buttons",
1013             "Border between menu edges and buttons",
1014             0, G_MAXUINT, 16,
1015             G_PARAM_READABLE));
1016
1017     gtk_widget_class_install_style_property (
1018         widget_class,
1019         g_param_spec_uint (
1020             "external-border",
1021             "Border between menu and screen edges (in horizontal mode)",
1022             "Border between the right and left edges of the menu and "
1023             "the screen edges (in horizontal mode)",
1024             0, G_MAXUINT, 50,
1025             G_PARAM_READABLE));
1026 }