2008-12-11 Claudio Saavedra <csaavedra@igalia.com>
[hildon] / src / 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: Widget representing the application menu in the Hildon framework.
22  *
23  * The #HildonAppMenu is a GTK widget which represents an application
24  * menu in the Hildon 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, the #HildonAppMenu can contain a group of filter buttons
33  * (#GtkToggleButton or #GtkRadioButton).
34  *
35  * To use a #HildonAppMenu, add it to a #HildonStackableWindow using
36  * hildon_stackable_window_set_main_menu(). The menu will appear when
37  * the user presses the window title bar. Alternatively, you can show
38  * it by hand using gtk_widget_show().
39  *
40  * The menu will be automatically hidden when one of its buttons is
41  * clicked. Use g_signal_connect_after() when connecting callbacks to
42  * buttons to make sure that they're called after the menu
43  * disappears. Alternatively, you can add the button to the menu
44  * before connecting any callback.
45  *
46  * Although implemented with a #GtkWindow, #HildonAppMenu behaves like
47  * a normal ref-counted widget, so g_object_ref(), g_object_unref(),
48  * g_object_ref_sink() and friends will behave just like with any
49  * other non-toplevel widget.
50  *
51  * <example>
52  * <title>Creating a HildonAppMenu</title>
53  * <programlisting>
54  * HildonStackableWindow *win;
55  * HildonAppMenu *menu;
56  * GtkWidget *button;
57  * GtkWidget *filter;
58  * <!-- -->
59  * win = HILDON_STACKABLE_WINDOW (hildon_stackable_window_new ());
60  * menu = HILDON_APP_MENU (hildon_app_menu_new ());
61  * <!-- -->
62  * // Create a button and add it to the menu
63  * button = gtk_button_new_with_label ("Menu command one");
64  * g_signal_connect_after (button, "clicked", G_CALLBACK (button_one_clicked), userdata);
65  * hildon_app_menu_append (menu, GTK_BUTTON (button));
66  * gtk_widget_show (button);
67  * <!-- -->
68  * // Another button
69  * button = gtk_button_new_with_label ("Menu command two");
70  * g_signal_connect_after (button, "clicked", G_CALLBACK (button_two_clicked), userdata);
71  * hildon_app_menu_append (menu, GTK_BUTTON (button));
72  * gtk_widget_show (button);
73  * <!-- -->
74  * // Create a filter and add it to the menu
75  * filter = gtk_radio_button_new_with_label (NULL, "Filter one");
76  * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
77  * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_one_clicked), userdata);
78  * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
79  * gtk_widget_show (filter);
80  * <!-- -->
81  * // Add a new filter
82  * filter = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (filter), "Filter two");
83  * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
84  * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_two_clicked), userdata);
85  * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
86  * gtk_widget_show (filter);
87  * <!-- -->
88  * // Add the menu to the window
89  * hildon_stackable_window_set_main_menu (win, menu);
90  * </programlisting>
91  * </example>
92  *
93  */
94
95 #include                                        <string.h>
96 #include                                        <X11/Xatom.h>
97 #include                                        <gdk/gdkx.h>
98
99 #include                                        "hildon-gtk.h"
100 #include                                        "hildon-app-menu.h"
101 #include                                        "hildon-app-menu-private.h"
102
103 static GdkWindow *
104 grab_transfer_window_get                        (GtkWidget *widget);
105
106 static void
107 hildon_app_menu_repack_items                    (HildonAppMenu *menu,
108                                                  gint           start_from);
109
110 static void
111 hildon_app_menu_repack_filters                  (HildonAppMenu *menu);
112
113 static gboolean
114 can_activate_accel                              (GtkWidget *widget,
115                                                  guint      signal_id,
116                                                  gpointer   user_data);
117
118 static void
119 item_visibility_changed                         (GtkWidget     *item,
120                                                  GParamSpec    *arg1,
121                                                  HildonAppMenu *menu);
122
123 static void
124 filter_visibility_changed                       (GtkWidget     *item,
125                                                  GParamSpec    *arg1,
126                                                  HildonAppMenu *menu);
127
128 static void
129 remove_item_from_list                           (GList    **list,
130                                                  gpointer   item);
131
132 static void
133 hildon_app_menu_apply_style                     (GtkWidget *widget);
134
135 G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
136
137 /**
138  * hildon_app_menu_new:
139  *
140  * Creates a new #HildonAppMenu.
141  *
142  * Return value: A #HildonAppMenu.
143  *
144  * Since: 2.2
145  **/
146 GtkWidget *
147 hildon_app_menu_new                             (void)
148 {
149     GtkWidget *menu = g_object_new (HILDON_TYPE_APP_MENU, NULL);
150     return menu;
151 }
152
153 /**
154  * hildon_app_menu_insert:
155  * @menu : A #HildonAppMenu
156  * @item : A #GtkButton to add to the #HildonAppMenu
157  * @position : The position in the item list where @item is added (from 0 to n-1).
158  *
159  * Adds @item to @menu at the position indicated by @position.
160  *
161  * Since: 2.2
162  */
163 void
164 hildon_app_menu_insert                          (HildonAppMenu *menu,
165                                                  GtkButton     *item,
166                                                  gint           position)
167 {
168     HildonAppMenuPrivate *priv;
169
170     g_return_if_fail (HILDON_IS_APP_MENU (menu));
171     g_return_if_fail (GTK_IS_BUTTON (item));
172
173     priv = HILDON_APP_MENU_GET_PRIVATE(menu);
174
175     /* Force widget size */
176     hildon_gtk_widget_set_theme_size (GTK_WIDGET (item),
177                                       HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
178
179     /* Add the item to the menu */
180     gtk_widget_show (GTK_WIDGET (item));
181     g_object_ref_sink (item);
182     priv->buttons = g_list_insert (priv->buttons, item, position);
183     hildon_app_menu_repack_items (menu, position);
184
185     /* Enable accelerators */
186     gtk_widget_realize (GTK_WIDGET (item));
187     g_signal_connect (item, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
188
189     /* Close the menu when the button is clicked */
190     g_signal_connect_swapped (item, "clicked", G_CALLBACK (gtk_widget_hide), menu);
191     g_signal_connect (item, "notify::visible", G_CALLBACK (item_visibility_changed), menu);
192
193     /* Remove item from list when it is destroyed */
194     g_object_weak_ref (G_OBJECT (item), (GWeakNotify) remove_item_from_list, &(priv->buttons));
195 }
196
197 /**
198  * hildon_app_menu_append:
199  * @menu : A #HildonAppMenu
200  * @item : A #GtkButton to add to the #HildonAppMenu
201  *
202  * Adds @item to the end of the menu's item list.
203  *
204  * Since: 2.2
205  */
206 void
207 hildon_app_menu_append                          (HildonAppMenu *menu,
208                                                  GtkButton     *item)
209 {
210     hildon_app_menu_insert (menu, item, -1);
211 }
212
213 /**
214  * hildon_app_menu_prepend:
215  * @menu : A #HildonAppMenu
216  * @item : A #GtkButton to add to the #HildonAppMenu
217  *
218  * Adds @item to the beginning of the menu's item list.
219  *
220  * Since: 2.2
221  */
222 void
223 hildon_app_menu_prepend                         (HildonAppMenu *menu,
224                                                  GtkButton     *item)
225 {
226     hildon_app_menu_insert (menu, item, 0);
227 }
228
229 /**
230  * hildon_app_menu_reorder_child:
231  * @menu : A #HildonAppMenu
232  * @item : A #GtkButton to move
233  * @position : The new position to place @item (from 0 to n-1).
234  *
235  * Moves a #GtkButton to a new position within #HildonAppMenu.
236  *
237  * Since: 2.2
238  */
239 void
240 hildon_app_menu_reorder_child                   (HildonAppMenu *menu,
241                                                  GtkButton     *item,
242                                                  gint           position)
243 {
244     HildonAppMenuPrivate *priv;
245     gint old_position;
246
247     g_return_if_fail (HILDON_IS_APP_MENU (menu));
248     g_return_if_fail (GTK_IS_BUTTON (item));
249     g_return_if_fail (position >= 0);
250
251     priv = HILDON_APP_MENU_GET_PRIVATE (menu);
252     old_position = g_list_index (priv->buttons, item);
253
254     g_return_if_fail (old_position >= 0);
255
256     /* Move the item */
257     priv->buttons = g_list_remove (priv->buttons, item);
258     priv->buttons = g_list_insert (priv->buttons, item, position);
259
260     hildon_app_menu_repack_items (menu, MIN (old_position, position));
261 }
262
263 /**
264  * hildon_app_menu_add_filter:
265  * @menu : A #HildonAppMenu
266  * @filter : A #GtkButton to add to the #HildonAppMenu.
267  *
268  * Adds the @filter to @menu.
269  *
270  * Since: 2.2
271  */
272 void
273 hildon_app_menu_add_filter                      (HildonAppMenu *menu,
274                                                  GtkButton *filter)
275 {
276     HildonAppMenuPrivate *priv;
277
278     g_return_if_fail (HILDON_IS_APP_MENU (menu));
279     g_return_if_fail (GTK_IS_BUTTON (filter));
280
281     priv = HILDON_APP_MENU_GET_PRIVATE(menu);
282
283     /* Force widget size */
284     hildon_gtk_widget_set_theme_size (GTK_WIDGET (filter),
285                                       HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
286
287     /* Add the filter to the menu */
288     gtk_widget_show (GTK_WIDGET (filter));
289     g_object_ref_sink (filter);
290     priv->filters = g_list_append (priv->filters, filter);
291     hildon_app_menu_repack_filters (menu);
292
293     /* Enable accelerators */
294     gtk_widget_realize (GTK_WIDGET (filter));
295     g_signal_connect (filter, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
296
297     /* Close the menu when the button is clicked */
298     g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
299     g_signal_connect (filter, "notify::visible", G_CALLBACK (filter_visibility_changed), menu);
300
301     /* Remove filter from list when it is destroyed */
302     g_object_weak_ref (G_OBJECT (filter), (GWeakNotify) remove_item_from_list, &(priv->filters));
303 }
304
305 static void
306 hildon_app_menu_set_columns                     (HildonAppMenu *menu,
307                                                  guint          columns)
308 {
309     HildonAppMenuPrivate *priv;
310
311     g_return_if_fail (HILDON_IS_APP_MENU (menu));
312     g_return_if_fail (columns > 0);
313
314     priv = HILDON_APP_MENU_GET_PRIVATE (menu);
315
316     if (columns != priv->columns) {
317         priv->columns = columns;
318         hildon_app_menu_repack_items (menu, 0);
319     }
320 }
321
322 void G_GNUC_INTERNAL
323 hildon_app_menu_set_parent_window              (HildonAppMenu *self,
324                                                 GtkWindow     *parent_window)
325 {
326     HildonAppMenuPrivate *priv;
327
328     g_return_if_fail (HILDON_IS_APP_MENU (self));
329     g_return_if_fail (parent_window == NULL || GTK_IS_WINDOW (parent_window));
330
331     priv = HILDON_APP_MENU_GET_PRIVATE(self);
332
333     priv->parent_window = parent_window;
334
335     if (parent_window == NULL && GTK_WIDGET_VISIBLE (self))
336         gtk_widget_hide (GTK_WIDGET (self));
337 }
338
339 gpointer G_GNUC_INTERNAL
340 hildon_app_menu_get_parent_window              (HildonAppMenu *self)
341 {
342     HildonAppMenuPrivate *priv;
343
344     g_return_val_if_fail (HILDON_IS_APP_MENU (self), NULL);
345
346     priv = HILDON_APP_MENU_GET_PRIVATE (self);
347
348     return priv->parent_window;
349 }
350
351 static void
352 screen_size_changed                            (GdkScreen     *screen,
353                                                 HildonAppMenu *menu)
354 {
355     hildon_app_menu_apply_style (GTK_WIDGET (menu));
356
357     if (gdk_screen_get_width (screen) > gdk_screen_get_height (screen)) {
358         hildon_app_menu_set_columns (menu, 2);
359     } else {
360         hildon_app_menu_set_columns (menu, 1);
361     }
362 }
363
364 static gboolean
365 can_activate_accel                              (GtkWidget *widget,
366                                                  guint      signal_id,
367                                                  gpointer   user_data)
368 {
369     return GTK_WIDGET_VISIBLE (widget);
370 }
371
372 static void
373 item_visibility_changed                         (GtkWidget     *item,
374                                                  GParamSpec    *arg1,
375                                                  HildonAppMenu *menu)
376 {
377     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
378
379     hildon_app_menu_repack_items (menu, g_list_index (priv->buttons, item));
380 }
381
382 static void
383 filter_visibility_changed                       (GtkWidget     *item,
384                                                  GParamSpec    *arg1,
385                                                  HildonAppMenu *menu)
386 {
387     hildon_app_menu_repack_filters (menu);
388 }
389
390 static void
391 remove_item_from_list                           (GList    **list,
392                                                  gpointer   item)
393 {
394     *list = g_list_remove (*list, item);
395 }
396
397 static void
398 hildon_app_menu_show                            (GtkWidget *widget)
399 {
400     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
401
402     /* Show the menu only if it's not empty */
403     if (priv->buttons || priv->filters) {
404         GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->show (widget);
405     }
406 }
407
408 static void
409 hildon_app_menu_map                             (GtkWidget *widget)
410 {
411     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
412
413     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
414
415     /* Grab pointer and keyboard */
416     if (priv->transfer_window == NULL) {
417         gboolean has_grab = FALSE;
418
419         priv->transfer_window = grab_transfer_window_get (widget);
420
421         if (gdk_pointer_grab (priv->transfer_window, TRUE,
422                               GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
423                               GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
424                               GDK_POINTER_MOTION_MASK, NULL, NULL,
425                               GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
426             if (gdk_keyboard_grab (priv->transfer_window, TRUE,
427                                    GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
428                 has_grab = TRUE;
429             } else {
430                 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
431                                             GDK_CURRENT_TIME);
432             }
433         }
434
435         if (has_grab) {
436             gtk_grab_add (widget);
437         } else {
438             gdk_window_destroy (priv->transfer_window);
439             priv->transfer_window = NULL;
440         }
441     }
442 }
443
444 static void
445 hildon_app_menu_unmap                           (GtkWidget *widget)
446 {
447     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
448
449     /* Remove the grab */
450     if (priv->transfer_window != NULL) {
451         gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
452                                     GDK_CURRENT_TIME);
453         gtk_grab_remove (widget);
454
455         gdk_window_destroy (priv->transfer_window);
456         priv->transfer_window = NULL;
457     }
458
459     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unmap (widget);
460 }
461
462 static gboolean
463 hildon_app_menu_hide_idle                       (gpointer widget)
464 {
465     gtk_widget_hide (GTK_WIDGET (widget));
466     return FALSE;
467 }
468
469 /* Send keyboard accelerators to the parent window, if necessary.
470  * This code is heavily based on gtk_menu_key_press ()
471  */
472 static gboolean
473 hildon_app_menu_key_press                       (GtkWidget   *widget,
474                                                  GdkEventKey *event)
475 {
476     GtkWindow *parent_window;
477     HildonAppMenuPrivate *priv;
478
479     g_return_val_if_fail (HILDON_IS_APP_MENU (widget), FALSE);
480     g_return_val_if_fail (event != NULL, FALSE);
481
482     if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->key_press_event (widget, event))
483         return TRUE;
484
485     priv = HILDON_APP_MENU_GET_PRIVATE (widget);
486     parent_window = priv->parent_window;
487
488     if (parent_window) {
489         guint accel_key, accel_mods;
490         GdkModifierType consumed_modifiers;
491         GdkDisplay *display;
492         GSList *accel_groups;
493         GSList *list;
494
495         display = gtk_widget_get_display (widget);
496
497         /* Figure out what modifiers went into determining the key symbol */
498         gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
499                                              event->hardware_keycode, event->state, event->group,
500                                              NULL, NULL, NULL, &consumed_modifiers);
501
502         accel_key = gdk_keyval_to_lower (event->keyval);
503         accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
504
505         /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
506          * We re-upper case when we match against the keyval, but display and save in caseless form.
507          */
508         if (accel_key != event->keyval)
509             accel_mods |= GDK_SHIFT_MASK;
510
511         accel_groups = gtk_accel_groups_from_object (G_OBJECT (parent_window));
512
513         for (list = accel_groups; list; list = list->next) {
514             GtkAccelGroup *accel_group = list->data;
515
516             if (gtk_accel_group_query (accel_group, accel_key, accel_mods, NULL)) {
517                 gtk_window_activate_key (parent_window, event);
518                 gdk_threads_add_idle (hildon_app_menu_hide_idle, widget);
519                 break;
520             }
521         }
522     }
523
524     return TRUE;
525 }
526
527 static gboolean
528 hildon_app_menu_button_press                    (GtkWidget *widget,
529                                                  GdkEventButton *event)
530 {
531     int x, y;
532     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
533
534     gdk_window_get_position (widget->window, &x, &y);
535
536     /* Whether the button has been pressed outside the widget */
537     priv->pressed_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
538                              event->y_root < y || event->y_root > y + widget->allocation.height);
539
540     if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event) {
541         return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event (widget, event);
542     } else {
543         return FALSE;
544     }
545 }
546
547 static gboolean
548 hildon_app_menu_button_release                  (GtkWidget *widget,
549                                                  GdkEventButton *event)
550 {
551     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
552
553     if (priv->pressed_outside) {
554         int x, y;
555         gboolean released_outside;
556
557         gdk_window_get_position (widget->window, &x, &y);
558
559         /* Whether the button has been released outside the widget */
560         released_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
561                             event->y_root < y || event->y_root > y + widget->allocation.height);
562
563         if (released_outside) {
564             gtk_widget_hide (widget);
565         }
566
567         priv->pressed_outside = FALSE; /* Always reset pressed_outside to FALSE */
568     }
569
570     if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event) {
571         return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event (widget, event);
572     } else {
573         return FALSE;
574     }
575 }
576
577 /* Grab transfer window (based on the one from GtkMenu) */
578 static GdkWindow *
579 grab_transfer_window_get                        (GtkWidget *widget)
580 {
581     GdkWindow *window;
582     GdkWindowAttr attributes;
583     gint attributes_mask;
584
585     attributes.x = 0;
586     attributes.y = 0;
587     attributes.width = 10;
588     attributes.height = 10;
589     attributes.window_type = GDK_WINDOW_TEMP;
590     attributes.wclass = GDK_INPUT_ONLY;
591     attributes.override_redirect = TRUE;
592     attributes.event_mask = 0;
593
594     attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
595
596     window = gdk_window_new (gtk_widget_get_root_window (widget),
597                                  &attributes, attributes_mask);
598     gdk_window_set_user_data (window, widget);
599
600     gdk_window_show (window);
601
602     return window;
603 }
604
605 static void
606 hildon_app_menu_realize                         (GtkWidget *widget)
607 {
608     Atom property, window_type;
609     Display *xdisplay;
610     GdkDisplay *gdkdisplay;
611     GdkScreen *screen;
612
613     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
614
615     gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
616
617     gdkdisplay = gdk_drawable_get_display (widget->window);
618     xdisplay = GDK_WINDOW_XDISPLAY (widget->window);
619
620     property = gdk_x11_get_xatom_by_name_for_display (gdkdisplay, "_NET_WM_WINDOW_TYPE");
621     window_type = XInternAtom (xdisplay, "_HILDON_WM_WINDOW_TYPE_APP_MENU", False);
622     XChangeProperty (xdisplay, GDK_WINDOW_XID (widget->window), property,
623                      XA_ATOM, 32, PropModeReplace, (guchar *) &window_type, 1);
624
625     /* Detect any screen changes */
626     screen = gtk_widget_get_screen (widget);
627     g_signal_connect (screen, "size-changed", G_CALLBACK (screen_size_changed), widget);
628
629     /* Force menu to set the initial layout */
630     screen_size_changed (screen, HILDON_APP_MENU (widget));
631 }
632
633 static void
634 hildon_app_menu_unrealize                       (GtkWidget *widget)
635 {
636     GdkScreen *screen = gtk_widget_get_screen (widget);
637     /* Disconnect "size-changed" signal handler */
638     g_signal_handlers_disconnect_by_func (screen, G_CALLBACK (screen_size_changed), widget);
639
640     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unrealize (widget);
641 }
642
643 static void
644 hildon_app_menu_apply_style                     (GtkWidget *widget)
645 {
646     GdkScreen *screen;
647     gint width;
648     guint horizontal_spacing, vertical_spacing, inner_border, external_border;
649     HildonAppMenuPrivate *priv;
650
651     priv = HILDON_APP_MENU_GET_PRIVATE (widget);
652
653     gtk_widget_style_get (widget,
654                           "horizontal-spacing", &horizontal_spacing,
655                           "vertical-spacing", &vertical_spacing,
656                           "inner-border", &inner_border,
657                           "external-border", &external_border,
658                           NULL);
659
660     /* Set spacings */
661     gtk_table_set_row_spacings (priv->table, vertical_spacing);
662     gtk_table_set_col_spacings (priv->table, horizontal_spacing);
663     gtk_box_set_spacing (priv->vbox, vertical_spacing);
664
665     /* Set inner border */
666     gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
667
668     /* Set default size */
669     screen = gtk_widget_get_screen (widget);
670     width = gdk_screen_get_width (screen) - external_border * 2;
671     gtk_window_set_default_size (GTK_WINDOW (widget), width, -1);
672 }
673
674 static void
675 hildon_app_menu_style_set                       (GtkWidget *widget,
676                                                  GtkStyle  *previous_style)
677 {
678     if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set)
679         GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set (widget, previous_style);
680
681     hildon_app_menu_apply_style (widget);
682 }
683
684 static void
685 hildon_app_menu_repack_filters                  (HildonAppMenu *menu)
686 {
687     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
688     GList *iter;
689
690     for (iter = priv->filters; iter != NULL; iter = iter->next) {
691         GtkWidget *filter = GTK_WIDGET (iter->data);
692         GtkWidget *parent = gtk_widget_get_parent (filter);
693         if (parent) {
694             g_object_ref (filter);
695             gtk_container_remove (GTK_CONTAINER (parent), filter);
696         }
697     }
698
699     for (iter = priv->filters; iter != NULL; iter = iter->next) {
700         GtkWidget *filter = GTK_WIDGET (iter->data);
701         if (GTK_WIDGET_VISIBLE (filter)) {
702             gtk_box_pack_start (GTK_BOX (priv->filters_hbox), filter, TRUE, TRUE, 0);
703             g_object_unref (filter);
704         }
705     }
706 }
707
708 /*
709  * When items displayed in the menu change (e.g, a new item is added,
710  * an item is hidden or the list is reordered), the layout must be
711  * updated. To do this we repack all items starting from a given one.
712  */
713 static void
714 hildon_app_menu_repack_items                    (HildonAppMenu *menu,
715                                                  gint           start_from)
716 {
717     HildonAppMenuPrivate *priv;
718     gint row, col;
719     GList *iter;
720
721     priv = HILDON_APP_MENU_GET_PRIVATE(menu);
722
723     /* Remove buttons from their parent */
724     if (start_from != -1) {
725         for (iter = g_list_nth (priv->buttons, start_from); iter != NULL; iter = iter->next) {
726             GtkWidget *item = GTK_WIDGET (iter->data);
727             GtkWidget *parent = gtk_widget_get_parent (item);
728             if (parent) {
729                 g_object_ref (item);
730                 gtk_container_remove (GTK_CONTAINER (parent), item);
731             }
732         }
733
734         /* If items have been removed, recalculate the size of the menu */
735         gtk_window_resize (GTK_WINDOW (menu), 1, 1);
736     }
737
738     /* Add buttons */
739     row = col = 0;
740     for (iter = priv->buttons; iter != NULL; iter = iter->next) {
741         GtkWidget *item = GTK_WIDGET (iter->data);
742         if (GTK_WIDGET_VISIBLE (item)) {
743             /* Don't add an item to the table if it's already there */
744             if (gtk_widget_get_parent (item) == NULL) {
745                 gtk_table_attach_defaults (priv->table, item, col, col + 1, row, row + 1);
746                 g_object_unref (item);
747             }
748             if (++col == priv->columns) {
749                 col = 0;
750                 row++;
751             }
752         }
753     }
754
755     /* The number of rows/columns might have changed, so we have to
756      * resize the table */
757     if (col == 0) {
758         gtk_table_resize (priv->table, MAX (row, 1), priv->columns);
759     } else {
760         gtk_table_resize (priv->table, row + 1, priv->columns);
761     }
762
763     if (GTK_WIDGET_VISIBLE (GTK_WIDGET (menu))) {
764         gtk_window_reshow_with_initial_size (GTK_WINDOW (menu));
765     }
766 }
767
768 static void
769 hildon_app_menu_init                            (HildonAppMenu *menu)
770 {
771     GtkWidget *alignment;
772     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
773
774     /* Initialize private variables */
775     priv->parent_window = NULL;
776     priv->transfer_window = NULL;
777     priv->pressed_outside = FALSE;
778     priv->buttons = NULL;
779     priv->filters = NULL;
780     priv->columns = 2;
781
782     /* Create boxes and tables */
783     priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
784     priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 0));
785     priv->table = GTK_TABLE (gtk_table_new (1, priv->columns, TRUE));
786
787     /* Align the filters to the center */
788     alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
789     gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
790
791     /* Pack everything */
792     gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
793     gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
794     gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
795
796     /* Make menu a modal window */
797     gtk_window_set_modal (GTK_WINDOW (menu), TRUE);
798
799     /* This should be treated like a normal, ref-counted widget */
800     g_object_force_floating (G_OBJECT (menu));
801     GTK_WINDOW (menu)->has_user_ref_count = FALSE;
802
803     gtk_widget_show_all (GTK_WIDGET (priv->vbox));
804 }
805
806 static void
807 hildon_app_menu_finalize                        (GObject *object)
808 {
809     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
810
811     if (priv->transfer_window)
812         gdk_window_destroy (priv->transfer_window);
813
814     g_list_foreach (priv->buttons, (GFunc) g_object_unref, NULL);
815     g_list_foreach (priv->filters, (GFunc) g_object_unref, NULL);
816
817     g_list_free (priv->buttons);
818     g_list_free (priv->filters);
819
820     g_signal_handlers_destroy (object);
821     G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
822 }
823
824 static void
825 hildon_app_menu_class_init                      (HildonAppMenuClass *klass)
826 {
827     GObjectClass *gobject_class = (GObjectClass *)klass;
828     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
829
830     gobject_class->finalize = hildon_app_menu_finalize;
831     widget_class->show = hildon_app_menu_show;
832     widget_class->map = hildon_app_menu_map;
833     widget_class->unmap = hildon_app_menu_unmap;
834     widget_class->realize = hildon_app_menu_realize;
835     widget_class->unrealize = hildon_app_menu_unrealize;
836     widget_class->key_press_event = hildon_app_menu_key_press;
837     widget_class->button_press_event = hildon_app_menu_button_press;
838     widget_class->button_release_event = hildon_app_menu_button_release;
839     widget_class->style_set = hildon_app_menu_style_set;
840
841     g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
842
843     gtk_widget_class_install_style_property (
844         widget_class,
845         g_param_spec_uint (
846             "horizontal-spacing",
847             "Horizontal spacing on menu items",
848             "Horizontal spacing between each menu item. Does not apply to filter buttons.",
849             0, G_MAXUINT, 16,
850             G_PARAM_READABLE));
851
852     gtk_widget_class_install_style_property (
853         widget_class,
854         g_param_spec_uint (
855             "vertical-spacing",
856             "Vertical spacing on menu items",
857             "Vertical spacing between each menu item. Does not apply to filter buttons.",
858             0, G_MAXUINT, 16,
859             G_PARAM_READABLE));
860
861     gtk_widget_class_install_style_property (
862         widget_class,
863         g_param_spec_uint (
864             "inner-border",
865             "Border between menu edges and buttons",
866             "Border between menu edges and buttons",
867             0, G_MAXUINT, 16,
868             G_PARAM_READABLE));
869
870     gtk_widget_class_install_style_property (
871         widget_class,
872         g_param_spec_uint (
873             "external-border",
874             "Border between menu and screen edges",
875             "Border between the right and left edges of the menu and the screen edges",
876             0, G_MAXUINT, 40,
877             G_PARAM_READABLE));
878 }