2 * This file is a part of hildon
4 * Copyright (C) 2008 Nokia Corporation, all rights reserved.
6 * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
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.
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.
20 * SECTION:hildon-app-menu
21 * @short_description: Widget representing the application menu in the Hildon framework.
23 * #HildonAppMenu is a GTK widget which represents an application menu
24 * in the Hildon framework.
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
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,
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().
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.
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.
56 * <title>Creating a HildonAppMenu</title>
59 * HildonAppMenu *menu;
63 * win = hildon_stackable_window_new ();
64 * menu = HILDON_APP_MENU (hildon_app_menu_new ());
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));
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));
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));
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));
88 * // Show all menu items
89 * gtk_widget_show_all (GTK_WIDGET (menu));
91 * // Add the menu to the window
92 * hildon_window_set_app_menu (HILDON_WINDOW (win), menu);
99 #include <X11/Xatom.h>
100 #include <gdk/gdkx.h>
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"
109 grab_transfer_window_get (GtkWidget *widget);
112 hildon_app_menu_repack_items (HildonAppMenu *menu,
116 hildon_app_menu_repack_filters (HildonAppMenu *menu);
119 can_activate_accel (GtkWidget *widget,
124 item_visibility_changed (GtkWidget *item,
126 HildonAppMenu *menu);
129 filter_visibility_changed (GtkWidget *item,
131 HildonAppMenu *menu);
134 remove_item_from_list (GList **list,
138 hildon_app_menu_apply_style (GtkWidget *widget);
140 G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
143 * hildon_app_menu_new:
145 * Creates a new #HildonAppMenu.
147 * Return value: A #HildonAppMenu.
152 hildon_app_menu_new (void)
154 GtkWidget *menu = g_object_new (HILDON_TYPE_APP_MENU, NULL);
159 * hildon_app_menu_insert:
160 * @menu : A #HildonAppMenu
161 * @item : A #GtkButton to add to the #HildonAppMenu
162 * @position : The position in the item list where @item is added (from 0 to n-1).
164 * Adds @item to @menu at the position indicated by @position.
169 hildon_app_menu_insert (HildonAppMenu *menu,
173 HildonAppMenuPrivate *priv;
175 g_return_if_fail (HILDON_IS_APP_MENU (menu));
176 g_return_if_fail (GTK_IS_BUTTON (item));
178 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
180 /* Force widget size */
181 hildon_gtk_widget_set_theme_size (GTK_WIDGET (item),
182 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
184 /* Add the item to the menu */
185 g_object_ref_sink (item);
186 priv->buttons = g_list_insert (priv->buttons, item, position);
187 if (GTK_WIDGET_VISIBLE (item))
188 hildon_app_menu_repack_items (menu, position);
190 /* Enable accelerators */
191 g_signal_connect (item, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
193 /* Close the menu when the button is clicked */
194 g_signal_connect_swapped (item, "clicked", G_CALLBACK (gtk_widget_hide), menu);
195 g_signal_connect (item, "notify::visible", G_CALLBACK (item_visibility_changed), menu);
197 /* Remove item from list when it is destroyed */
198 g_object_weak_ref (G_OBJECT (item), (GWeakNotify) remove_item_from_list, &(priv->buttons));
202 * hildon_app_menu_append:
203 * @menu : A #HildonAppMenu
204 * @item : A #GtkButton to add to the #HildonAppMenu
206 * Adds @item to the end of the menu's item list.
211 hildon_app_menu_append (HildonAppMenu *menu,
214 hildon_app_menu_insert (menu, item, -1);
218 * hildon_app_menu_prepend:
219 * @menu : A #HildonAppMenu
220 * @item : A #GtkButton to add to the #HildonAppMenu
222 * Adds @item to the beginning of the menu's item list.
227 hildon_app_menu_prepend (HildonAppMenu *menu,
230 hildon_app_menu_insert (menu, item, 0);
234 * hildon_app_menu_reorder_child:
235 * @menu : A #HildonAppMenu
236 * @item : A #GtkButton to move
237 * @position : The new position to place @item (from 0 to n-1).
239 * Moves a #GtkButton to a new position within #HildonAppMenu.
244 hildon_app_menu_reorder_child (HildonAppMenu *menu,
248 HildonAppMenuPrivate *priv;
251 g_return_if_fail (HILDON_IS_APP_MENU (menu));
252 g_return_if_fail (GTK_IS_BUTTON (item));
253 g_return_if_fail (position >= 0);
255 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
256 old_position = g_list_index (priv->buttons, item);
258 g_return_if_fail (old_position >= 0);
261 priv->buttons = g_list_remove (priv->buttons, item);
262 priv->buttons = g_list_insert (priv->buttons, item, position);
264 hildon_app_menu_repack_items (menu, MIN (old_position, position));
268 * hildon_app_menu_add_filter:
269 * @menu : A #HildonAppMenu
270 * @filter : A #GtkButton to add to the #HildonAppMenu.
272 * Adds the @filter to @menu.
277 hildon_app_menu_add_filter (HildonAppMenu *menu,
280 HildonAppMenuPrivate *priv;
282 g_return_if_fail (HILDON_IS_APP_MENU (menu));
283 g_return_if_fail (GTK_IS_BUTTON (filter));
285 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
287 /* Force widget size */
288 hildon_gtk_widget_set_theme_size (GTK_WIDGET (filter),
289 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
291 /* Add the filter to the menu */
292 g_object_ref_sink (filter);
293 priv->filters = g_list_append (priv->filters, filter);
294 if (GTK_WIDGET_VISIBLE (filter))
295 hildon_app_menu_repack_filters (menu);
297 /* Enable accelerators */
298 g_signal_connect (filter, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
300 /* Close the menu when the button is clicked */
301 g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
302 g_signal_connect (filter, "notify::visible", G_CALLBACK (filter_visibility_changed), menu);
304 /* Remove filter from list when it is destroyed */
305 g_object_weak_ref (G_OBJECT (filter), (GWeakNotify) remove_item_from_list, &(priv->filters));
309 hildon_app_menu_set_columns (HildonAppMenu *menu,
312 HildonAppMenuPrivate *priv;
314 g_return_if_fail (HILDON_IS_APP_MENU (menu));
315 g_return_if_fail (columns > 0);
317 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
319 if (columns != priv->columns) {
320 priv->columns = columns;
321 hildon_app_menu_repack_items (menu, 0);
326 parent_window_topmost_notify (HildonWindow *parent_win,
330 if (!hildon_window_get_is_topmost (parent_win))
331 gtk_widget_hide (menu);
335 parent_window_unmapped (HildonWindow *parent_win,
338 gtk_widget_hide (menu);
342 hildon_app_menu_set_parent_window (HildonAppMenu *self,
343 GtkWindow *parent_window)
345 HildonAppMenuPrivate *priv;
347 g_return_if_fail (HILDON_IS_APP_MENU (self));
348 g_return_if_fail (parent_window == NULL || GTK_IS_WINDOW (parent_window));
350 priv = HILDON_APP_MENU_GET_PRIVATE(self);
352 /* Disconnect old handlers, if any */
353 if (priv->parent_window) {
354 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, self);
355 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, self);
358 /* Connect a new handler */
360 g_signal_connect (parent_window, "notify::is-topmost", G_CALLBACK (parent_window_topmost_notify), self);
361 g_signal_connect (parent_window, "unmap", G_CALLBACK (parent_window_unmapped), self);
364 priv->parent_window = parent_window;
366 if (parent_window == NULL && GTK_WIDGET_VISIBLE (self))
367 gtk_widget_hide (GTK_WIDGET (self));
370 gpointer G_GNUC_INTERNAL
371 hildon_app_menu_get_parent_window (HildonAppMenu *self)
373 HildonAppMenuPrivate *priv;
375 g_return_val_if_fail (HILDON_IS_APP_MENU (self), NULL);
377 priv = HILDON_APP_MENU_GET_PRIVATE (self);
379 return priv->parent_window;
383 screen_size_changed (GdkScreen *screen,
386 hildon_app_menu_apply_style (GTK_WIDGET (menu));
388 if (gdk_screen_get_width (screen) > gdk_screen_get_height (screen)) {
389 hildon_app_menu_set_columns (menu, 2);
391 hildon_app_menu_set_columns (menu, 1);
396 can_activate_accel (GtkWidget *widget,
400 return GTK_WIDGET_VISIBLE (widget);
404 item_visibility_changed (GtkWidget *item,
408 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
410 if (! priv->inhibit_repack)
411 hildon_app_menu_repack_items (menu, g_list_index (priv->buttons, item));
415 filter_visibility_changed (GtkWidget *item,
419 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
421 if (! priv->inhibit_repack)
422 hildon_app_menu_repack_filters (menu);
426 remove_item_from_list (GList **list,
429 *list = g_list_remove (*list, item);
433 hildon_app_menu_show_all (GtkWidget *widget)
435 HildonAppMenu *menu = HILDON_APP_MENU (widget);
436 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
438 priv->inhibit_repack = TRUE;
440 /* Show children, but not self. */
441 g_list_foreach (priv->buttons, (GFunc) gtk_widget_show_all, NULL);
442 g_list_foreach (priv->filters, (GFunc) gtk_widget_show_all, NULL);
444 priv->inhibit_repack = FALSE;
446 hildon_app_menu_repack_items (menu, 0);
447 hildon_app_menu_repack_filters (menu);
452 hildon_app_menu_hide_all (GtkWidget *widget)
454 HildonAppMenu *menu = HILDON_APP_MENU (widget);
455 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
457 priv->inhibit_repack = TRUE;
459 /* Hide children, but not self. */
460 g_list_foreach (priv->buttons, (GFunc) gtk_widget_hide_all, NULL);
461 g_list_foreach (priv->filters, (GFunc) gtk_widget_hide_all, NULL);
463 priv->inhibit_repack = FALSE;
465 hildon_app_menu_repack_items (menu, 0);
466 hildon_app_menu_repack_filters (menu);
470 * There's a race condition that can freeze the UI if a dialog appears
471 * between a HildonAppMenu and its parent window, see NB#100468
474 hildon_app_menu_find_intruder (gpointer data)
476 GtkWidget *widget = GTK_WIDGET (data);
477 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
479 priv->find_intruder_idle_id = 0;
481 /* If there's a window between the menu and its parent window, hide the menu */
482 if (priv->parent_window) {
483 gboolean intruder_found = FALSE;
484 GdkScreen *screen = gtk_widget_get_screen (widget);
485 GList *stack = gdk_screen_get_window_stack (screen);
486 GList *parent_pos = g_list_find (stack, GTK_WIDGET (priv->parent_window)->window);
487 GList *toplevels = gtk_window_list_toplevels ();
490 for (i = toplevels; i != NULL && !intruder_found; i = i->next) {
491 if (i->data != widget && i->data != priv->parent_window) {
492 if (g_list_find (parent_pos, GTK_WIDGET (i->data)->window)) {
493 /* HildonBanners are not closed automatically when
494 * a new window appears, so we must close them by
495 * hand to make the AppMenu work as expected.
496 * Yes, this is a hack. See NB#111027 */
497 if (HILDON_IS_BANNER (i->data)) {
498 gtk_widget_hide (i->data);
500 intruder_found = TRUE;
506 g_list_foreach (stack, (GFunc) g_object_unref, NULL);
508 g_list_free (toplevels);
511 gtk_widget_hide (widget);
518 hildon_app_menu_map (GtkWidget *widget)
520 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
522 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
524 /* Grab pointer and keyboard */
525 if (priv->transfer_window == NULL) {
526 gboolean has_grab = FALSE;
528 priv->transfer_window = grab_transfer_window_get (widget);
530 if (gdk_pointer_grab (priv->transfer_window, TRUE,
531 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
532 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
533 GDK_POINTER_MOTION_MASK, NULL, NULL,
534 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
535 if (gdk_keyboard_grab (priv->transfer_window, TRUE,
536 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
539 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
545 gtk_grab_add (widget);
547 gdk_window_destroy (priv->transfer_window);
548 priv->transfer_window = NULL;
552 /* Make the menu temporary when it's mapped, so it's closed if a
553 * new window appears */
554 gtk_window_set_is_temporary (GTK_WINDOW (widget), TRUE);
556 priv->find_intruder_idle_id = gdk_threads_add_idle (hildon_app_menu_find_intruder, widget);
560 hildon_app_menu_unmap (GtkWidget *widget)
562 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
564 /* Remove the grab */
565 if (priv->transfer_window != NULL) {
566 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
568 gtk_grab_remove (widget);
570 gdk_window_destroy (priv->transfer_window);
571 priv->transfer_window = NULL;
574 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unmap (widget);
576 gtk_window_set_is_temporary (GTK_WINDOW (widget), FALSE);
580 hildon_app_menu_grab_notify (GtkWidget *widget,
581 gboolean was_grabbed)
583 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->grab_notify)
584 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->grab_notify (widget, was_grabbed);
586 if (!was_grabbed && GTK_WIDGET_VISIBLE (widget))
587 gtk_widget_hide (widget);
591 hildon_app_menu_hide_idle (gpointer widget)
593 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
594 gtk_widget_hide (GTK_WIDGET (widget));
595 priv->hide_idle_id = 0;
599 /* Send keyboard accelerators to the parent window, if necessary.
600 * This code is heavily based on gtk_menu_key_press ()
603 hildon_app_menu_key_press (GtkWidget *widget,
606 GtkWindow *parent_window;
607 HildonAppMenuPrivate *priv;
609 g_return_val_if_fail (HILDON_IS_APP_MENU (widget), FALSE);
610 g_return_val_if_fail (event != NULL, FALSE);
612 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->key_press_event (widget, event))
615 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
616 parent_window = priv->parent_window;
619 guint accel_key, accel_mods;
620 GdkModifierType consumed_modifiers;
622 GSList *accel_groups;
625 display = gtk_widget_get_display (widget);
627 /* Figure out what modifiers went into determining the key symbol */
628 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
629 event->hardware_keycode, event->state, event->group,
630 NULL, NULL, NULL, &consumed_modifiers);
632 accel_key = gdk_keyval_to_lower (event->keyval);
633 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
635 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
636 * We re-upper case when we match against the keyval, but display and save in caseless form.
638 if (accel_key != event->keyval)
639 accel_mods |= GDK_SHIFT_MASK;
641 accel_groups = gtk_accel_groups_from_object (G_OBJECT (parent_window));
643 for (list = accel_groups; list; list = list->next) {
644 GtkAccelGroup *accel_group = list->data;
646 if (gtk_accel_group_query (accel_group, accel_key, accel_mods, NULL)) {
647 gtk_window_activate_key (parent_window, event);
648 priv->hide_idle_id = gdk_threads_add_idle (hildon_app_menu_hide_idle, widget);
658 hildon_app_menu_button_press (GtkWidget *widget,
659 GdkEventButton *event)
662 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
664 gdk_window_get_position (widget->window, &x, &y);
666 /* Whether the button has been pressed outside the widget */
667 priv->pressed_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
668 event->y_root < y || event->y_root > y + widget->allocation.height);
670 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event) {
671 return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event (widget, event);
678 hildon_app_menu_button_release (GtkWidget *widget,
679 GdkEventButton *event)
681 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
683 if (priv->pressed_outside) {
685 gboolean released_outside;
687 gdk_window_get_position (widget->window, &x, &y);
689 /* Whether the button has been released outside the widget */
690 released_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
691 event->y_root < y || event->y_root > y + widget->allocation.height);
693 if (released_outside) {
694 gtk_widget_hide (widget);
697 priv->pressed_outside = FALSE; /* Always reset pressed_outside to FALSE */
700 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event) {
701 return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event (widget, event);
708 hildon_app_menu_delete_event_handler (GtkWidget *widget,
711 /* Hide the menu if it receives a delete-event, but don't destroy it */
712 gtk_widget_hide (widget);
716 /* Grab transfer window (based on the one from GtkMenu) */
718 grab_transfer_window_get (GtkWidget *widget)
721 GdkWindowAttr attributes;
722 gint attributes_mask;
726 attributes.width = 10;
727 attributes.height = 10;
728 attributes.window_type = GDK_WINDOW_TEMP;
729 attributes.wclass = GDK_INPUT_ONLY;
730 attributes.override_redirect = TRUE;
731 attributes.event_mask = 0;
733 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
735 window = gdk_window_new (gtk_widget_get_root_window (widget),
736 &attributes, attributes_mask);
737 gdk_window_set_user_data (window, widget);
739 gdk_window_show (window);
745 hildon_app_menu_size_request (GtkWidget *widget,
746 GtkRequisition *requisition)
748 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
750 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->size_request (widget, requisition);
752 requisition->width = priv->width_request;
756 hildon_app_menu_realize (GtkWidget *widget)
758 Atom property, window_type;
760 GdkDisplay *gdkdisplay;
763 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
765 gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
767 gdkdisplay = gdk_drawable_get_display (widget->window);
768 xdisplay = GDK_WINDOW_XDISPLAY (widget->window);
770 property = gdk_x11_get_xatom_by_name_for_display (gdkdisplay, "_NET_WM_WINDOW_TYPE");
771 window_type = XInternAtom (xdisplay, "_HILDON_WM_WINDOW_TYPE_APP_MENU", False);
772 XChangeProperty (xdisplay, GDK_WINDOW_XID (widget->window), property,
773 XA_ATOM, 32, PropModeReplace, (guchar *) &window_type, 1);
775 /* Detect any screen changes */
776 screen = gtk_widget_get_screen (widget);
777 g_signal_connect (screen, "size-changed", G_CALLBACK (screen_size_changed), widget);
779 /* Force menu to set the initial layout */
780 screen_size_changed (screen, HILDON_APP_MENU (widget));
784 hildon_app_menu_unrealize (GtkWidget *widget)
786 GdkScreen *screen = gtk_widget_get_screen (widget);
787 /* Disconnect "size-changed" signal handler */
788 g_signal_handlers_disconnect_by_func (screen, G_CALLBACK (screen_size_changed), widget);
790 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unrealize (widget);
794 hildon_app_menu_apply_style (GtkWidget *widget)
797 guint horizontal_spacing, vertical_spacing, filter_vertical_spacing;
798 guint inner_border, external_border;
799 HildonAppMenuPrivate *priv;
801 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
803 gtk_widget_style_get (widget,
804 "horizontal-spacing", &horizontal_spacing,
805 "vertical-spacing", &vertical_spacing,
806 "filter-vertical-spacing", &filter_vertical_spacing,
807 "inner-border", &inner_border,
808 "external-border", &external_border,
812 gtk_table_set_row_spacings (priv->table, vertical_spacing);
813 gtk_table_set_col_spacings (priv->table, horizontal_spacing);
814 gtk_box_set_spacing (priv->vbox, filter_vertical_spacing);
816 /* Set inner border */
817 gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
819 /* Compute width request */
820 screen = gtk_widget_get_screen (widget);
821 if (gdk_screen_get_width (screen) < gdk_screen_get_height (screen)) {
824 priv->width_request = gdk_screen_get_width (screen) - external_border * 2;
825 gtk_window_move (GTK_WINDOW (widget), external_border, 0);
826 gtk_widget_queue_resize (widget);
830 hildon_app_menu_style_set (GtkWidget *widget,
831 GtkStyle *previous_style)
833 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set)
834 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set (widget, previous_style);
836 hildon_app_menu_apply_style (widget);
840 hildon_app_menu_repack_filters (HildonAppMenu *menu)
842 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
845 for (iter = priv->filters; iter != NULL; iter = iter->next) {
846 GtkWidget *filter = GTK_WIDGET (iter->data);
847 GtkWidget *parent = gtk_widget_get_parent (filter);
849 g_object_ref (filter);
850 gtk_container_remove (GTK_CONTAINER (parent), filter);
854 for (iter = priv->filters; iter != NULL; iter = iter->next) {
855 GtkWidget *filter = GTK_WIDGET (iter->data);
856 if (GTK_WIDGET_VISIBLE (filter)) {
857 gtk_box_pack_start (GTK_BOX (priv->filters_hbox), filter, TRUE, TRUE, 0);
858 g_object_unref (filter);
859 /* GtkButton must be realized for accelerators to work */
860 gtk_widget_realize (filter);
866 * When items displayed in the menu change (e.g, a new item is added,
867 * an item is hidden or the list is reordered), the layout must be
868 * updated. To do this we repack all items starting from a given one.
871 hildon_app_menu_repack_items (HildonAppMenu *menu,
874 HildonAppMenuPrivate *priv;
875 gint row, col, nvisible, i;
878 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
881 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
882 /* Count number of visible items */
883 if (GTK_WIDGET_VISIBLE (iter->data))
885 /* Remove buttons from their parent */
886 if (start_from != -1 && i >= start_from) {
887 GtkWidget *item = GTK_WIDGET (iter->data);
888 GtkWidget *parent = gtk_widget_get_parent (item);
891 gtk_container_remove (GTK_CONTAINER (parent), item);
897 /* If items have been removed, recalculate the size of the menu */
898 if (start_from != -1)
899 gtk_window_resize (GTK_WINDOW (menu), 1, 1);
901 /* Set the final size now to avoid unnecessary resizes later */
903 gtk_table_resize (priv->table, ((nvisible - 1) / priv->columns) + 1, priv->columns);
907 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
908 GtkWidget *item = GTK_WIDGET (iter->data);
909 if (GTK_WIDGET_VISIBLE (item)) {
910 /* Don't add an item to the table if it's already there */
911 if (gtk_widget_get_parent (item) == NULL) {
912 gtk_table_attach_defaults (priv->table, item, col, col + 1, row, row + 1);
913 g_object_unref (item);
914 /* GtkButton must be realized for accelerators to work */
915 gtk_widget_realize (item);
917 if (++col == priv->columns) {
926 * hildon_app_menu_popup:
927 * @menu: a #HildonAppMenu
928 * @parent_window: a #GtkWindow
930 * Displays a menu on top of a window and makes it available for
936 hildon_app_menu_popup (HildonAppMenu *menu,
937 GtkWindow *parent_window)
939 HildonAppMenuPrivate *priv;
940 gboolean show_menu = FALSE;
943 g_return_if_fail (HILDON_IS_APP_MENU (menu));
944 g_return_if_fail (GTK_IS_WINDOW (parent_window));
946 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
948 /* Don't show menu if it doesn't contain visible items */
949 for (i = priv->buttons; i && !show_menu; i = i->next)
950 show_menu = GTK_WIDGET_VISIBLE (i->data);
952 for (i = priv->filters; i && !show_menu; i = i->next)
953 show_menu = GTK_WIDGET_VISIBLE (i->data);
956 hildon_app_menu_set_parent_window (menu, parent_window);
957 gtk_widget_show (GTK_WIDGET (menu));
963 * hildon_app_menu_get_items:
964 * @menu: a #HildonAppMenu
966 * Returns a list of all items (regular items, not filters) contained
969 * Returns: a newly-allocated list containing the items in @menu
974 hildon_app_menu_get_items (HildonAppMenu *menu)
976 HildonAppMenuPrivate *priv;
978 g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
980 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
982 return g_list_copy (priv->buttons);
986 * hildon_app_menu_get_filters:
987 * @menu: a #HildonAppMenu
989 * Returns a list of all filters contained in @menu.
991 * Returns: a newly-allocated list containing the filters in @menu
996 hildon_app_menu_get_filters (HildonAppMenu *menu)
998 HildonAppMenuPrivate *priv;
1000 g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
1002 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
1004 return g_list_copy (priv->filters);
1008 hildon_app_menu_init (HildonAppMenu *menu)
1010 GtkWidget *alignment;
1011 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
1013 /* Initialize private variables */
1014 priv->parent_window = NULL;
1015 priv->transfer_window = NULL;
1016 priv->pressed_outside = FALSE;
1017 priv->inhibit_repack = FALSE;
1018 priv->buttons = NULL;
1019 priv->filters = NULL;
1021 priv->width_request = -1;
1022 priv->find_intruder_idle_id = 0;
1023 priv->hide_idle_id = 0;
1025 /* Create boxes and tables */
1026 priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
1027 priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 0));
1028 priv->table = GTK_TABLE (gtk_table_new (1, priv->columns, TRUE));
1030 /* Align the filters to the center */
1031 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
1032 gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
1034 /* Pack everything */
1035 gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
1036 gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
1037 gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
1039 /* This should be treated like a normal, ref-counted widget */
1040 g_object_force_floating (G_OBJECT (menu));
1041 GTK_WINDOW (menu)->has_user_ref_count = FALSE;
1043 gtk_widget_show_all (GTK_WIDGET (priv->vbox));
1047 hildon_app_menu_finalize (GObject *object)
1049 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
1051 if (priv->find_intruder_idle_id) {
1052 g_source_remove (priv->find_intruder_idle_id);
1053 priv->find_intruder_idle_id = 0;
1056 if (priv->hide_idle_id) {
1057 g_source_remove (priv->hide_idle_id);
1058 priv->hide_idle_id = 0;
1061 if (priv->parent_window) {
1062 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, object);
1063 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, object);
1066 if (priv->transfer_window)
1067 gdk_window_destroy (priv->transfer_window);
1069 g_list_foreach (priv->buttons, (GFunc) g_object_unref, NULL);
1070 g_list_foreach (priv->filters, (GFunc) g_object_unref, NULL);
1072 g_list_free (priv->buttons);
1073 g_list_free (priv->filters);
1075 g_signal_handlers_destroy (object);
1076 G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
1080 hildon_app_menu_class_init (HildonAppMenuClass *klass)
1082 GObjectClass *gobject_class = (GObjectClass *)klass;
1083 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
1085 gobject_class->finalize = hildon_app_menu_finalize;
1086 widget_class->show_all = hildon_app_menu_show_all;
1087 widget_class->hide_all = hildon_app_menu_hide_all;
1088 widget_class->map = hildon_app_menu_map;
1089 widget_class->unmap = hildon_app_menu_unmap;
1090 widget_class->realize = hildon_app_menu_realize;
1091 widget_class->unrealize = hildon_app_menu_unrealize;
1092 widget_class->grab_notify = hildon_app_menu_grab_notify;
1093 widget_class->key_press_event = hildon_app_menu_key_press;
1094 widget_class->button_press_event = hildon_app_menu_button_press;
1095 widget_class->button_release_event = hildon_app_menu_button_release;
1096 widget_class->style_set = hildon_app_menu_style_set;
1097 widget_class->delete_event = hildon_app_menu_delete_event_handler;
1098 widget_class->size_request = hildon_app_menu_size_request;
1100 g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
1102 gtk_widget_class_install_style_property (
1105 "horizontal-spacing",
1106 "Horizontal spacing on menu items",
1107 "Horizontal spacing between each menu item. Does not apply to filter buttons.",
1111 gtk_widget_class_install_style_property (
1115 "Vertical spacing on menu items",
1116 "Vertical spacing between each menu item. Does not apply to filter buttons.",
1120 gtk_widget_class_install_style_property (
1123 "filter-vertical-spacing",
1124 "Vertical spacing between filters and menu items",
1125 "Vertical spacing between filters and menu items",
1129 gtk_widget_class_install_style_property (
1133 "Border between menu edges and buttons",
1134 "Border between menu edges and buttons",
1138 gtk_widget_class_install_style_property (
1142 "Border between menu and screen edges (in horizontal mode)",
1143 "Border between the right and left edges of the menu and "
1144 "the screen edges (in horizontal mode)",