Mark HildonBanner as supporting portrait mode.
[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 #HildonWindow using
36  * hildon_window_set_app_menu(). The menu will appear when the user
37  * presses the window title bar. Alternatively, you can show it by
38  * hand using hildon_app_menu_popup().
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  * GtkWidget *win;
55  * HildonAppMenu *menu;
56  * GtkWidget *button;
57  * GtkWidget *filter;
58  * <!-- -->
59  * win = 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  * <!-- -->
67  * // Another button
68  * button = gtk_button_new_with_label ("Menu command two");
69  * g_signal_connect_after (button, "clicked", G_CALLBACK (button_two_clicked), userdata);
70  * hildon_app_menu_append (menu, GTK_BUTTON (button));
71  * <!-- -->
72  * // Create a filter and add it to the menu
73  * filter = gtk_radio_button_new_with_label (NULL, "Filter one");
74  * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
75  * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_one_clicked), userdata);
76  * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
77  * <!-- -->
78  * // Add a new filter
79  * filter = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (filter), "Filter two");
80  * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
81  * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_two_clicked), userdata);
82  * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
83  * <!-- -->
84  * // Show all menu items
85  * gtk_widget_show_all (GTK_WIDGET (menu));
86  * <!-- -->
87  * // Add the menu to the window
88  * hildon_window_set_app_menu (HILDON_WINDOW (win), menu);
89  * </programlisting>
90  * </example>
91  *
92  */
93
94 #include                                        <string.h>
95 #include                                        <X11/Xatom.h>
96 #include                                        <gdk/gdkx.h>
97
98 #include                                        "hildon-gtk.h"
99 #include                                        "hildon-app-menu.h"
100 #include                                        "hildon-app-menu-private.h"
101 #include                                        "hildon-window.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     g_signal_connect (item, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
187
188     /* Close the menu when the button is clicked */
189     g_signal_connect_swapped (item, "clicked", G_CALLBACK (gtk_widget_hide), menu);
190     g_signal_connect (item, "notify::visible", G_CALLBACK (item_visibility_changed), menu);
191
192     /* Remove item from list when it is destroyed */
193     g_object_weak_ref (G_OBJECT (item), (GWeakNotify) remove_item_from_list, &(priv->buttons));
194 }
195
196 /**
197  * hildon_app_menu_append:
198  * @menu : A #HildonAppMenu
199  * @item : A #GtkButton to add to the #HildonAppMenu
200  *
201  * Adds @item to the end of the menu's item list.
202  *
203  * Since: 2.2
204  */
205 void
206 hildon_app_menu_append                          (HildonAppMenu *menu,
207                                                  GtkButton     *item)
208 {
209     hildon_app_menu_insert (menu, item, -1);
210 }
211
212 /**
213  * hildon_app_menu_prepend:
214  * @menu : A #HildonAppMenu
215  * @item : A #GtkButton to add to the #HildonAppMenu
216  *
217  * Adds @item to the beginning of the menu's item list.
218  *
219  * Since: 2.2
220  */
221 void
222 hildon_app_menu_prepend                         (HildonAppMenu *menu,
223                                                  GtkButton     *item)
224 {
225     hildon_app_menu_insert (menu, item, 0);
226 }
227
228 /**
229  * hildon_app_menu_reorder_child:
230  * @menu : A #HildonAppMenu
231  * @item : A #GtkButton to move
232  * @position : The new position to place @item (from 0 to n-1).
233  *
234  * Moves a #GtkButton to a new position within #HildonAppMenu.
235  *
236  * Since: 2.2
237  */
238 void
239 hildon_app_menu_reorder_child                   (HildonAppMenu *menu,
240                                                  GtkButton     *item,
241                                                  gint           position)
242 {
243     HildonAppMenuPrivate *priv;
244     gint old_position;
245
246     g_return_if_fail (HILDON_IS_APP_MENU (menu));
247     g_return_if_fail (GTK_IS_BUTTON (item));
248     g_return_if_fail (position >= 0);
249
250     priv = HILDON_APP_MENU_GET_PRIVATE (menu);
251     old_position = g_list_index (priv->buttons, item);
252
253     g_return_if_fail (old_position >= 0);
254
255     /* Move the item */
256     priv->buttons = g_list_remove (priv->buttons, item);
257     priv->buttons = g_list_insert (priv->buttons, item, position);
258
259     hildon_app_menu_repack_items (menu, MIN (old_position, position));
260 }
261
262 /**
263  * hildon_app_menu_add_filter:
264  * @menu : A #HildonAppMenu
265  * @filter : A #GtkButton to add to the #HildonAppMenu.
266  *
267  * Adds the @filter to @menu.
268  *
269  * Since: 2.2
270  */
271 void
272 hildon_app_menu_add_filter                      (HildonAppMenu *menu,
273                                                  GtkButton *filter)
274 {
275     HildonAppMenuPrivate *priv;
276
277     g_return_if_fail (HILDON_IS_APP_MENU (menu));
278     g_return_if_fail (GTK_IS_BUTTON (filter));
279
280     priv = HILDON_APP_MENU_GET_PRIVATE(menu);
281
282     /* Force widget size */
283     hildon_gtk_widget_set_theme_size (GTK_WIDGET (filter),
284                                       HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
285
286     /* Add the filter to the menu */
287     gtk_widget_show (GTK_WIDGET (filter));
288     g_object_ref_sink (filter);
289     priv->filters = g_list_append (priv->filters, filter);
290     hildon_app_menu_repack_filters (menu);
291
292     /* Enable accelerators */
293     g_signal_connect (filter, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
294
295     /* Close the menu when the button is clicked */
296     g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
297     g_signal_connect (filter, "notify::visible", G_CALLBACK (filter_visibility_changed), menu);
298
299     /* Remove filter from list when it is destroyed */
300     g_object_weak_ref (G_OBJECT (filter), (GWeakNotify) remove_item_from_list, &(priv->filters));
301 }
302
303 static void
304 hildon_app_menu_set_columns                     (HildonAppMenu *menu,
305                                                  guint          columns)
306 {
307     HildonAppMenuPrivate *priv;
308
309     g_return_if_fail (HILDON_IS_APP_MENU (menu));
310     g_return_if_fail (columns > 0);
311
312     priv = HILDON_APP_MENU_GET_PRIVATE (menu);
313
314     if (columns != priv->columns) {
315         priv->columns = columns;
316         hildon_app_menu_repack_items (menu, 0);
317     }
318 }
319
320 static void
321 parent_window_topmost_notify                   (HildonWindow *parent_win,
322                                                 GParamSpec   *arg1,
323                                                 GtkWidget    *menu)
324 {
325     if (!hildon_window_get_is_topmost (parent_win))
326         gtk_widget_hide (menu);
327 }
328
329 static void
330 parent_window_unmapped                         (HildonWindow *parent_win,
331                                                 GtkWidget    *menu)
332 {
333     gtk_widget_hide (menu);
334 }
335
336 void G_GNUC_INTERNAL
337 hildon_app_menu_set_parent_window              (HildonAppMenu *self,
338                                                 GtkWindow     *parent_window)
339 {
340     HildonAppMenuPrivate *priv;
341
342     g_return_if_fail (HILDON_IS_APP_MENU (self));
343     g_return_if_fail (parent_window == NULL || GTK_IS_WINDOW (parent_window));
344
345     priv = HILDON_APP_MENU_GET_PRIVATE(self);
346
347     /* Disconnect old handlers, if any */
348     if (priv->parent_window) {
349         g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, self);
350         g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, self);
351     }
352
353     /* Connect a new handler */
354     if (parent_window) {
355         g_signal_connect (parent_window, "notify::is-topmost", G_CALLBACK (parent_window_topmost_notify), self);
356         g_signal_connect (parent_window, "unmap", G_CALLBACK (parent_window_unmapped), self);
357     }
358
359     priv->parent_window = parent_window;
360
361     if (parent_window == NULL && GTK_WIDGET_VISIBLE (self))
362         gtk_widget_hide (GTK_WIDGET (self));
363 }
364
365 gpointer G_GNUC_INTERNAL
366 hildon_app_menu_get_parent_window              (HildonAppMenu *self)
367 {
368     HildonAppMenuPrivate *priv;
369
370     g_return_val_if_fail (HILDON_IS_APP_MENU (self), NULL);
371
372     priv = HILDON_APP_MENU_GET_PRIVATE (self);
373
374     return priv->parent_window;
375 }
376
377 static void
378 screen_size_changed                            (GdkScreen     *screen,
379                                                 HildonAppMenu *menu)
380 {
381     hildon_app_menu_apply_style (GTK_WIDGET (menu));
382
383     if (gdk_screen_get_width (screen) > gdk_screen_get_height (screen)) {
384         hildon_app_menu_set_columns (menu, 2);
385     } else {
386         hildon_app_menu_set_columns (menu, 1);
387     }
388 }
389
390 static gboolean
391 can_activate_accel                              (GtkWidget *widget,
392                                                  guint      signal_id,
393                                                  gpointer   user_data)
394 {
395     return GTK_WIDGET_VISIBLE (widget);
396 }
397
398 static void
399 item_visibility_changed                         (GtkWidget     *item,
400                                                  GParamSpec    *arg1,
401                                                  HildonAppMenu *menu)
402 {
403     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
404
405     hildon_app_menu_repack_items (menu, g_list_index (priv->buttons, item));
406 }
407
408 static void
409 filter_visibility_changed                       (GtkWidget     *item,
410                                                  GParamSpec    *arg1,
411                                                  HildonAppMenu *menu)
412 {
413     hildon_app_menu_repack_filters (menu);
414 }
415
416 static void
417 remove_item_from_list                           (GList    **list,
418                                                  gpointer   item)
419 {
420     *list = g_list_remove (*list, item);
421 }
422
423 static void
424 hildon_app_menu_show_all                        (GtkWidget *widget)
425 {
426     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
427
428     /* Show children, but not self. */
429     g_list_foreach (priv->buttons, (GFunc) gtk_widget_show_all, NULL);
430     g_list_foreach (priv->filters, (GFunc) gtk_widget_show_all, NULL);
431 }
432
433
434 static void
435 hildon_app_menu_hide_all                        (GtkWidget *widget)
436 {
437     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
438
439     /* Hide children, but not self. */
440     g_list_foreach (priv->buttons, (GFunc) gtk_widget_hide_all, NULL);
441     g_list_foreach (priv->filters, (GFunc) gtk_widget_hide_all, NULL);
442 }
443
444 /*
445  * There's a race condition that can freeze the UI if a dialog appears
446  * between a HildonAppMenu and its parent window, see NB#100468
447  */
448 static gboolean
449 hildon_app_menu_find_intruder                   (gpointer data)
450 {
451     GtkWidget *widget = GTK_WIDGET (data);
452     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
453
454     priv->find_intruder_idle_id = 0;
455
456     /* If there's a window between the menu and its parent window, hide the menu */
457     if (priv->parent_window) {
458         gboolean intruder_found = FALSE;
459         GdkScreen *screen = gtk_widget_get_screen (widget);
460         GList *stack = gdk_screen_get_window_stack (screen);
461         GList *parent_pos = g_list_find (stack, GTK_WIDGET (priv->parent_window)->window);
462         GList *toplevels = gtk_window_list_toplevels ();
463         GList *i;
464
465         for (i = toplevels; i != NULL && !intruder_found; i = i->next) {
466             if (i->data != widget && i->data != priv->parent_window) {
467                 if (g_list_find (parent_pos, GTK_WIDGET (i->data)->window)) {
468                     intruder_found = TRUE;
469                 }
470             }
471         }
472
473         g_list_foreach (stack, (GFunc) g_object_unref, NULL);
474         g_list_free (stack);
475         g_list_free (toplevels);
476
477         if (intruder_found)
478             gtk_widget_hide (widget);
479     }
480
481     return FALSE;
482 }
483
484 static void
485 hildon_app_menu_map                             (GtkWidget *widget)
486 {
487     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
488
489     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
490
491     /* Grab pointer and keyboard */
492     if (priv->transfer_window == NULL) {
493         gboolean has_grab = FALSE;
494
495         priv->transfer_window = grab_transfer_window_get (widget);
496
497         if (gdk_pointer_grab (priv->transfer_window, TRUE,
498                               GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
499                               GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
500                               GDK_POINTER_MOTION_MASK, NULL, NULL,
501                               GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
502             if (gdk_keyboard_grab (priv->transfer_window, TRUE,
503                                    GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
504                 has_grab = TRUE;
505             } else {
506                 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
507                                             GDK_CURRENT_TIME);
508             }
509         }
510
511         if (has_grab) {
512             gtk_grab_add (widget);
513         } else {
514             gdk_window_destroy (priv->transfer_window);
515             priv->transfer_window = NULL;
516         }
517     }
518
519     /* Make the menu temporary when it's mapped, so it's closed if a
520      * new window appears */
521     gtk_window_set_is_temporary (GTK_WINDOW (widget), TRUE);
522
523     priv->find_intruder_idle_id = gdk_threads_add_idle (hildon_app_menu_find_intruder, widget);
524 }
525
526 static void
527 hildon_app_menu_unmap                           (GtkWidget *widget)
528 {
529     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
530
531     /* Remove the grab */
532     if (priv->transfer_window != NULL) {
533         gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
534                                     GDK_CURRENT_TIME);
535         gtk_grab_remove (widget);
536
537         gdk_window_destroy (priv->transfer_window);
538         priv->transfer_window = NULL;
539     }
540
541     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unmap (widget);
542
543     gtk_window_set_is_temporary (GTK_WINDOW (widget), FALSE);
544 }
545
546 static void
547 hildon_app_menu_grab_notify                     (GtkWidget *widget,
548                                                  gboolean   was_grabbed)
549 {
550     if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->grab_notify)
551         GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->grab_notify (widget, was_grabbed);
552
553     if (!was_grabbed && GTK_WIDGET_VISIBLE (widget))
554         gtk_widget_hide (widget);
555 }
556
557 static gboolean
558 hildon_app_menu_hide_idle                       (gpointer widget)
559 {
560     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
561     gtk_widget_hide (GTK_WIDGET (widget));
562     priv->hide_idle_id = 0;
563     return FALSE;
564 }
565
566 /* Send keyboard accelerators to the parent window, if necessary.
567  * This code is heavily based on gtk_menu_key_press ()
568  */
569 static gboolean
570 hildon_app_menu_key_press                       (GtkWidget   *widget,
571                                                  GdkEventKey *event)
572 {
573     GtkWindow *parent_window;
574     HildonAppMenuPrivate *priv;
575
576     g_return_val_if_fail (HILDON_IS_APP_MENU (widget), FALSE);
577     g_return_val_if_fail (event != NULL, FALSE);
578
579     if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->key_press_event (widget, event))
580         return TRUE;
581
582     priv = HILDON_APP_MENU_GET_PRIVATE (widget);
583     parent_window = priv->parent_window;
584
585     if (parent_window) {
586         guint accel_key, accel_mods;
587         GdkModifierType consumed_modifiers;
588         GdkDisplay *display;
589         GSList *accel_groups;
590         GSList *list;
591
592         display = gtk_widget_get_display (widget);
593
594         /* Figure out what modifiers went into determining the key symbol */
595         gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
596                                              event->hardware_keycode, event->state, event->group,
597                                              NULL, NULL, NULL, &consumed_modifiers);
598
599         accel_key = gdk_keyval_to_lower (event->keyval);
600         accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
601
602         /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
603          * We re-upper case when we match against the keyval, but display and save in caseless form.
604          */
605         if (accel_key != event->keyval)
606             accel_mods |= GDK_SHIFT_MASK;
607
608         accel_groups = gtk_accel_groups_from_object (G_OBJECT (parent_window));
609
610         for (list = accel_groups; list; list = list->next) {
611             GtkAccelGroup *accel_group = list->data;
612
613             if (gtk_accel_group_query (accel_group, accel_key, accel_mods, NULL)) {
614                 gtk_window_activate_key (parent_window, event);
615                 priv->hide_idle_id = gdk_threads_add_idle (hildon_app_menu_hide_idle, widget);
616                 break;
617             }
618         }
619     }
620
621     return TRUE;
622 }
623
624 static gboolean
625 hildon_app_menu_button_press                    (GtkWidget *widget,
626                                                  GdkEventButton *event)
627 {
628     int x, y;
629     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
630
631     gdk_window_get_position (widget->window, &x, &y);
632
633     /* Whether the button has been pressed outside the widget */
634     priv->pressed_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
635                              event->y_root < y || event->y_root > y + widget->allocation.height);
636
637     if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event) {
638         return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event (widget, event);
639     } else {
640         return FALSE;
641     }
642 }
643
644 static gboolean
645 hildon_app_menu_button_release                  (GtkWidget *widget,
646                                                  GdkEventButton *event)
647 {
648     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
649
650     if (priv->pressed_outside) {
651         int x, y;
652         gboolean released_outside;
653
654         gdk_window_get_position (widget->window, &x, &y);
655
656         /* Whether the button has been released outside the widget */
657         released_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
658                             event->y_root < y || event->y_root > y + widget->allocation.height);
659
660         if (released_outside) {
661             gtk_widget_hide (widget);
662         }
663
664         priv->pressed_outside = FALSE; /* Always reset pressed_outside to FALSE */
665     }
666
667     if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event) {
668         return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event (widget, event);
669     } else {
670         return FALSE;
671     }
672 }
673
674 static gboolean
675 hildon_app_menu_delete_event_handler            (GtkWidget   *widget,
676                                                  GdkEventAny *event)
677 {
678     /* Hide the menu if it receives a delete-event, but don't destroy it */
679     gtk_widget_hide (widget);
680     return TRUE;
681 }
682
683 /* Grab transfer window (based on the one from GtkMenu) */
684 static GdkWindow *
685 grab_transfer_window_get                        (GtkWidget *widget)
686 {
687     GdkWindow *window;
688     GdkWindowAttr attributes;
689     gint attributes_mask;
690
691     attributes.x = 0;
692     attributes.y = 0;
693     attributes.width = 10;
694     attributes.height = 10;
695     attributes.window_type = GDK_WINDOW_TEMP;
696     attributes.wclass = GDK_INPUT_ONLY;
697     attributes.override_redirect = TRUE;
698     attributes.event_mask = 0;
699
700     attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
701
702     window = gdk_window_new (gtk_widget_get_root_window (widget),
703                                  &attributes, attributes_mask);
704     gdk_window_set_user_data (window, widget);
705
706     gdk_window_show (window);
707
708     return window;
709 }
710
711 static void
712 hildon_app_menu_size_request                    (GtkWidget      *widget,
713                                                  GtkRequisition *requisition)
714 {
715     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
716
717     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->size_request (widget, requisition);
718
719     requisition->width = priv->width_request;
720 }
721
722 static void
723 hildon_app_menu_realize                         (GtkWidget *widget)
724 {
725     Atom property, window_type;
726     Display *xdisplay;
727     GdkDisplay *gdkdisplay;
728     GdkScreen *screen;
729
730     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
731
732     gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
733
734     gdkdisplay = gdk_drawable_get_display (widget->window);
735     xdisplay = GDK_WINDOW_XDISPLAY (widget->window);
736
737     property = gdk_x11_get_xatom_by_name_for_display (gdkdisplay, "_NET_WM_WINDOW_TYPE");
738     window_type = XInternAtom (xdisplay, "_HILDON_WM_WINDOW_TYPE_APP_MENU", False);
739     XChangeProperty (xdisplay, GDK_WINDOW_XID (widget->window), property,
740                      XA_ATOM, 32, PropModeReplace, (guchar *) &window_type, 1);
741
742     /* Detect any screen changes */
743     screen = gtk_widget_get_screen (widget);
744     g_signal_connect (screen, "size-changed", G_CALLBACK (screen_size_changed), widget);
745
746     /* Force menu to set the initial layout */
747     screen_size_changed (screen, HILDON_APP_MENU (widget));
748 }
749
750 static void
751 hildon_app_menu_unrealize                       (GtkWidget *widget)
752 {
753     GdkScreen *screen = gtk_widget_get_screen (widget);
754     /* Disconnect "size-changed" signal handler */
755     g_signal_handlers_disconnect_by_func (screen, G_CALLBACK (screen_size_changed), widget);
756
757     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unrealize (widget);
758 }
759
760 static void
761 hildon_app_menu_apply_style                     (GtkWidget *widget)
762 {
763     GdkScreen *screen;
764     guint horizontal_spacing, vertical_spacing, filter_vertical_spacing;
765     guint inner_border, external_border;
766     HildonAppMenuPrivate *priv;
767
768     priv = HILDON_APP_MENU_GET_PRIVATE (widget);
769
770     gtk_widget_style_get (widget,
771                           "horizontal-spacing", &horizontal_spacing,
772                           "vertical-spacing", &vertical_spacing,
773                           "filter-vertical-spacing", &filter_vertical_spacing,
774                           "inner-border", &inner_border,
775                           "external-border", &external_border,
776                           NULL);
777
778     /* Set spacings */
779     gtk_table_set_row_spacings (priv->table, vertical_spacing);
780     gtk_table_set_col_spacings (priv->table, horizontal_spacing);
781     gtk_box_set_spacing (priv->vbox, filter_vertical_spacing);
782
783     /* Set inner border */
784     gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
785
786     /* Compute width request */
787     screen = gtk_widget_get_screen (widget);
788     if (gdk_screen_get_width (screen) < gdk_screen_get_height (screen)) {
789         external_border = 0;
790     }
791     priv->width_request = gdk_screen_get_width (screen) - external_border * 2;
792     gtk_window_move (GTK_WINDOW (widget), external_border, 0);
793     gtk_widget_queue_resize (widget);
794 }
795
796 static void
797 hildon_app_menu_style_set                       (GtkWidget *widget,
798                                                  GtkStyle  *previous_style)
799 {
800     if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set)
801         GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set (widget, previous_style);
802
803     hildon_app_menu_apply_style (widget);
804 }
805
806 static void
807 hildon_app_menu_repack_filters                  (HildonAppMenu *menu)
808 {
809     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
810     GList *iter;
811
812     for (iter = priv->filters; iter != NULL; iter = iter->next) {
813         GtkWidget *filter = GTK_WIDGET (iter->data);
814         GtkWidget *parent = gtk_widget_get_parent (filter);
815         if (parent) {
816             g_object_ref (filter);
817             gtk_container_remove (GTK_CONTAINER (parent), filter);
818         }
819     }
820
821     for (iter = priv->filters; iter != NULL; iter = iter->next) {
822         GtkWidget *filter = GTK_WIDGET (iter->data);
823         if (GTK_WIDGET_VISIBLE (filter)) {
824             gtk_box_pack_start (GTK_BOX (priv->filters_hbox), filter, TRUE, TRUE, 0);
825             g_object_unref (filter);
826             /* GtkButton must be realized for accelerators to work */
827             gtk_widget_realize (filter);
828         }
829     }
830 }
831
832 /*
833  * When items displayed in the menu change (e.g, a new item is added,
834  * an item is hidden or the list is reordered), the layout must be
835  * updated. To do this we repack all items starting from a given one.
836  */
837 static void
838 hildon_app_menu_repack_items                    (HildonAppMenu *menu,
839                                                  gint           start_from)
840 {
841     HildonAppMenuPrivate *priv;
842     gint row, col;
843     GList *iter;
844
845     priv = HILDON_APP_MENU_GET_PRIVATE(menu);
846
847     /* Remove buttons from their parent */
848     if (start_from != -1) {
849         for (iter = g_list_nth (priv->buttons, start_from); iter != NULL; iter = iter->next) {
850             GtkWidget *item = GTK_WIDGET (iter->data);
851             GtkWidget *parent = gtk_widget_get_parent (item);
852             if (parent) {
853                 g_object_ref (item);
854                 gtk_container_remove (GTK_CONTAINER (parent), item);
855             }
856         }
857
858         /* If items have been removed, recalculate the size of the menu */
859         gtk_window_resize (GTK_WINDOW (menu), 1, 1);
860     }
861
862     /* Add buttons */
863     row = col = 0;
864     for (iter = priv->buttons; iter != NULL; iter = iter->next) {
865         GtkWidget *item = GTK_WIDGET (iter->data);
866         if (GTK_WIDGET_VISIBLE (item)) {
867             /* Don't add an item to the table if it's already there */
868             if (gtk_widget_get_parent (item) == NULL) {
869                 gtk_table_attach_defaults (priv->table, item, col, col + 1, row, row + 1);
870                 g_object_unref (item);
871                 /* GtkButton must be realized for accelerators to work */
872                 gtk_widget_realize (item);
873             }
874             if (++col == priv->columns) {
875                 col = 0;
876                 row++;
877             }
878         }
879     }
880
881     /* The number of rows/columns might have changed, so we have to
882      * resize the table */
883     if (col == 0) {
884         gtk_table_resize (priv->table, MAX (row, 1), priv->columns);
885     } else {
886         gtk_table_resize (priv->table, row + 1, priv->columns);
887     }
888 }
889
890 /**
891  * hildon_app_menu_popup:
892  * @menu: a #HildonAppMenu
893  * @parent_window: a #GtkWindow
894  *
895  * Displays a menu on top of a window and makes it available for
896  * selection.
897  *
898  * Since: 2.2
899  **/
900 void
901 hildon_app_menu_popup                           (HildonAppMenu *menu,
902                                                  GtkWindow     *parent_window)
903 {
904     HildonAppMenuPrivate *priv;
905     gboolean show_menu = FALSE;
906     GList *i;
907
908     g_return_if_fail (HILDON_IS_APP_MENU (menu));
909     g_return_if_fail (GTK_IS_WINDOW (parent_window));
910
911     priv = HILDON_APP_MENU_GET_PRIVATE (menu);
912
913     /* Don't show menu if it doesn't contain visible items */
914     for (i = priv->buttons; i && !show_menu; i = i->next)
915         show_menu = GTK_WIDGET_VISIBLE (i->data);
916
917     for (i = priv->filters; i && !show_menu; i = i->next)
918         show_menu = GTK_WIDGET_VISIBLE (i->data);
919
920     if (show_menu) {
921         hildon_app_menu_set_parent_window (menu, parent_window);
922         gtk_widget_show (GTK_WIDGET (menu));
923     }
924
925 }
926
927 /**
928  * hildon_app_menu_get_items:
929  * @menu: a #HildonAppMenu
930  *
931  * Returns a list of all items (regular items, not filters) contained
932  * in @menu.
933  *
934  * Returns: a newly-allocated list containing the items in @menu
935  *
936  * Since: 2.2
937  **/
938 GList *
939 hildon_app_menu_get_items                       (HildonAppMenu *menu)
940 {
941     HildonAppMenuPrivate *priv;
942
943     g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
944
945     priv = HILDON_APP_MENU_GET_PRIVATE (menu);
946
947     return g_list_copy (priv->buttons);
948 }
949
950 /**
951  * hildon_app_menu_get_filters:
952  * @menu: a #HildonAppMenu
953  *
954  * Returns a list of all filters contained in @menu.
955  *
956  * Returns: a newly-allocated list containing the filters in @menu
957  *
958  * Since: 2.2
959  **/
960 GList *
961 hildon_app_menu_get_filters                     (HildonAppMenu *menu)
962 {
963     HildonAppMenuPrivate *priv;
964
965     g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
966
967     priv = HILDON_APP_MENU_GET_PRIVATE (menu);
968
969     return g_list_copy (priv->filters);
970 }
971
972 static void
973 hildon_app_menu_init                            (HildonAppMenu *menu)
974 {
975     GtkWidget *alignment;
976     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
977
978     /* Initialize private variables */
979     priv->parent_window = NULL;
980     priv->transfer_window = NULL;
981     priv->pressed_outside = FALSE;
982     priv->buttons = NULL;
983     priv->filters = NULL;
984     priv->columns = 2;
985     priv->width_request = -1;
986     priv->find_intruder_idle_id = 0;
987     priv->hide_idle_id = 0;
988
989     /* Create boxes and tables */
990     priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
991     priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 0));
992     priv->table = GTK_TABLE (gtk_table_new (1, priv->columns, TRUE));
993
994     /* Align the filters to the center */
995     alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
996     gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
997
998     /* Pack everything */
999     gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
1000     gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
1001     gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
1002
1003     /* Make menu a modal window */
1004     gtk_window_set_modal (GTK_WINDOW (menu), TRUE);
1005
1006     /* This should be treated like a normal, ref-counted widget */
1007     g_object_force_floating (G_OBJECT (menu));
1008     GTK_WINDOW (menu)->has_user_ref_count = FALSE;
1009
1010     gtk_widget_show_all (GTK_WIDGET (priv->vbox));
1011 }
1012
1013 static void
1014 hildon_app_menu_finalize                        (GObject *object)
1015 {
1016     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
1017
1018     if (priv->find_intruder_idle_id) {
1019         g_source_remove (priv->find_intruder_idle_id);
1020         priv->find_intruder_idle_id = 0;
1021     }
1022
1023     if (priv->hide_idle_id) {
1024         g_source_remove (priv->hide_idle_id);
1025         priv->hide_idle_id = 0;
1026     }
1027
1028     if (priv->parent_window) {
1029         g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, object);
1030         g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, object);
1031     }
1032
1033     if (priv->transfer_window)
1034         gdk_window_destroy (priv->transfer_window);
1035
1036     g_list_foreach (priv->buttons, (GFunc) g_object_unref, NULL);
1037     g_list_foreach (priv->filters, (GFunc) g_object_unref, NULL);
1038
1039     g_list_free (priv->buttons);
1040     g_list_free (priv->filters);
1041
1042     g_signal_handlers_destroy (object);
1043     G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
1044 }
1045
1046 static void
1047 hildon_app_menu_class_init                      (HildonAppMenuClass *klass)
1048 {
1049     GObjectClass *gobject_class = (GObjectClass *)klass;
1050     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
1051
1052     gobject_class->finalize = hildon_app_menu_finalize;
1053     widget_class->show_all = hildon_app_menu_show_all;
1054     widget_class->hide_all = hildon_app_menu_hide_all;
1055     widget_class->map = hildon_app_menu_map;
1056     widget_class->unmap = hildon_app_menu_unmap;
1057     widget_class->realize = hildon_app_menu_realize;
1058     widget_class->unrealize = hildon_app_menu_unrealize;
1059     widget_class->grab_notify = hildon_app_menu_grab_notify;
1060     widget_class->key_press_event = hildon_app_menu_key_press;
1061     widget_class->button_press_event = hildon_app_menu_button_press;
1062     widget_class->button_release_event = hildon_app_menu_button_release;
1063     widget_class->style_set = hildon_app_menu_style_set;
1064     widget_class->delete_event = hildon_app_menu_delete_event_handler;
1065     widget_class->size_request = hildon_app_menu_size_request;
1066
1067     g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
1068
1069     gtk_widget_class_install_style_property (
1070         widget_class,
1071         g_param_spec_uint (
1072             "horizontal-spacing",
1073             "Horizontal spacing on menu items",
1074             "Horizontal spacing between each menu item. Does not apply to filter buttons.",
1075             0, G_MAXUINT, 16,
1076             G_PARAM_READABLE));
1077
1078     gtk_widget_class_install_style_property (
1079         widget_class,
1080         g_param_spec_uint (
1081             "vertical-spacing",
1082             "Vertical spacing on menu items",
1083             "Vertical spacing between each menu item. Does not apply to filter buttons.",
1084             0, G_MAXUINT, 16,
1085             G_PARAM_READABLE));
1086
1087     gtk_widget_class_install_style_property (
1088         widget_class,
1089         g_param_spec_uint (
1090             "filter-vertical-spacing",
1091             "Vertical spacing between filters and menu items",
1092             "Vertical spacing between filters and menu items",
1093             0, G_MAXUINT, 8,
1094             G_PARAM_READABLE));
1095
1096     gtk_widget_class_install_style_property (
1097         widget_class,
1098         g_param_spec_uint (
1099             "inner-border",
1100             "Border between menu edges and buttons",
1101             "Border between menu edges and buttons",
1102             0, G_MAXUINT, 16,
1103             G_PARAM_READABLE));
1104
1105     gtk_widget_class_install_style_property (
1106         widget_class,
1107         g_param_spec_uint (
1108             "external-border",
1109             "Border between menu and screen edges (in horizontal mode)",
1110             "Border between the right and left edges of the menu and "
1111             "the screen edges (in horizontal mode)",
1112             0, G_MAXUINT, 50,
1113             G_PARAM_READABLE));
1114 }