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 * The #HildonAppMenu is a GTK widget which represents an application
24 * menu 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, the #HildonAppMenu can contain a group of filter buttons
33 * (#GtkToggleButton or #GtkRadioButton).
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().
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.
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.
52 * <title>Creating a HildonAppMenu</title>
55 * HildonAppMenu *menu;
59 * win = hildon_stackable_window_new ();
60 * menu = HILDON_APP_MENU (hildon_app_menu_new ());
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));
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));
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));
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));
84 * // Show all menu items
85 * gtk_widget_show_all (GTK_WIDGET (menu));
87 * // Add the menu to the window
88 * hildon_window_set_app_menu (HILDON_WINDOW (win), menu);
95 #include <X11/Xatom.h>
98 #include "hildon-gtk.h"
99 #include "hildon-app-menu.h"
100 #include "hildon-app-menu-private.h"
101 #include "hildon-window.h"
102 #include "hildon-banner.h"
105 grab_transfer_window_get (GtkWidget *widget);
108 hildon_app_menu_repack_items (HildonAppMenu *menu,
112 hildon_app_menu_repack_filters (HildonAppMenu *menu);
115 can_activate_accel (GtkWidget *widget,
120 item_visibility_changed (GtkWidget *item,
122 HildonAppMenu *menu);
125 filter_visibility_changed (GtkWidget *item,
127 HildonAppMenu *menu);
130 remove_item_from_list (GList **list,
134 hildon_app_menu_apply_style (GtkWidget *widget);
136 G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
139 * hildon_app_menu_new:
141 * Creates a new #HildonAppMenu.
143 * Return value: A #HildonAppMenu.
148 hildon_app_menu_new (void)
150 GtkWidget *menu = g_object_new (HILDON_TYPE_APP_MENU, NULL);
155 * hildon_app_menu_insert:
156 * @menu : A #HildonAppMenu
157 * @item : A #GtkButton to add to the #HildonAppMenu
158 * @position : The position in the item list where @item is added (from 0 to n-1).
160 * Adds @item to @menu at the position indicated by @position.
165 hildon_app_menu_insert (HildonAppMenu *menu,
169 HildonAppMenuPrivate *priv;
171 g_return_if_fail (HILDON_IS_APP_MENU (menu));
172 g_return_if_fail (GTK_IS_BUTTON (item));
174 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
176 /* Force widget size */
177 hildon_gtk_widget_set_theme_size (GTK_WIDGET (item),
178 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
180 /* Add the item to the menu */
181 g_object_ref_sink (item);
182 priv->buttons = g_list_insert (priv->buttons, item, position);
183 if (GTK_WIDGET_VISIBLE (item))
184 hildon_app_menu_repack_items (menu, position);
186 /* Enable accelerators */
187 g_signal_connect (item, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
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);
193 /* Remove item from list when it is destroyed */
194 g_object_weak_ref (G_OBJECT (item), (GWeakNotify) remove_item_from_list, &(priv->buttons));
198 * hildon_app_menu_append:
199 * @menu : A #HildonAppMenu
200 * @item : A #GtkButton to add to the #HildonAppMenu
202 * Adds @item to the end of the menu's item list.
207 hildon_app_menu_append (HildonAppMenu *menu,
210 hildon_app_menu_insert (menu, item, -1);
214 * hildon_app_menu_prepend:
215 * @menu : A #HildonAppMenu
216 * @item : A #GtkButton to add to the #HildonAppMenu
218 * Adds @item to the beginning of the menu's item list.
223 hildon_app_menu_prepend (HildonAppMenu *menu,
226 hildon_app_menu_insert (menu, item, 0);
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).
235 * Moves a #GtkButton to a new position within #HildonAppMenu.
240 hildon_app_menu_reorder_child (HildonAppMenu *menu,
244 HildonAppMenuPrivate *priv;
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);
251 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
252 old_position = g_list_index (priv->buttons, item);
254 g_return_if_fail (old_position >= 0);
257 priv->buttons = g_list_remove (priv->buttons, item);
258 priv->buttons = g_list_insert (priv->buttons, item, position);
260 hildon_app_menu_repack_items (menu, MIN (old_position, position));
264 * hildon_app_menu_add_filter:
265 * @menu : A #HildonAppMenu
266 * @filter : A #GtkButton to add to the #HildonAppMenu.
268 * Adds the @filter to @menu.
273 hildon_app_menu_add_filter (HildonAppMenu *menu,
276 HildonAppMenuPrivate *priv;
278 g_return_if_fail (HILDON_IS_APP_MENU (menu));
279 g_return_if_fail (GTK_IS_BUTTON (filter));
281 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
283 /* Force widget size */
284 hildon_gtk_widget_set_theme_size (GTK_WIDGET (filter),
285 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
287 /* Add the filter to the menu */
288 g_object_ref_sink (filter);
289 priv->filters = g_list_append (priv->filters, filter);
290 if (GTK_WIDGET_VISIBLE (filter))
291 hildon_app_menu_repack_filters (menu);
293 /* Enable accelerators */
294 g_signal_connect (filter, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
296 /* Close the menu when the button is clicked */
297 g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
298 g_signal_connect (filter, "notify::visible", G_CALLBACK (filter_visibility_changed), menu);
300 /* Remove filter from list when it is destroyed */
301 g_object_weak_ref (G_OBJECT (filter), (GWeakNotify) remove_item_from_list, &(priv->filters));
305 hildon_app_menu_set_columns (HildonAppMenu *menu,
308 HildonAppMenuPrivate *priv;
310 g_return_if_fail (HILDON_IS_APP_MENU (menu));
311 g_return_if_fail (columns > 0);
313 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
315 if (columns != priv->columns) {
316 priv->columns = columns;
317 hildon_app_menu_repack_items (menu, 0);
322 parent_window_topmost_notify (HildonWindow *parent_win,
326 if (!hildon_window_get_is_topmost (parent_win))
327 gtk_widget_hide (menu);
331 parent_window_unmapped (HildonWindow *parent_win,
334 gtk_widget_hide (menu);
338 hildon_app_menu_set_parent_window (HildonAppMenu *self,
339 GtkWindow *parent_window)
341 HildonAppMenuPrivate *priv;
343 g_return_if_fail (HILDON_IS_APP_MENU (self));
344 g_return_if_fail (parent_window == NULL || GTK_IS_WINDOW (parent_window));
346 priv = HILDON_APP_MENU_GET_PRIVATE(self);
348 /* Disconnect old handlers, if any */
349 if (priv->parent_window) {
350 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, self);
351 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, self);
354 /* Connect a new handler */
356 g_signal_connect (parent_window, "notify::is-topmost", G_CALLBACK (parent_window_topmost_notify), self);
357 g_signal_connect (parent_window, "unmap", G_CALLBACK (parent_window_unmapped), self);
360 priv->parent_window = parent_window;
362 if (parent_window == NULL && GTK_WIDGET_VISIBLE (self))
363 gtk_widget_hide (GTK_WIDGET (self));
366 gpointer G_GNUC_INTERNAL
367 hildon_app_menu_get_parent_window (HildonAppMenu *self)
369 HildonAppMenuPrivate *priv;
371 g_return_val_if_fail (HILDON_IS_APP_MENU (self), NULL);
373 priv = HILDON_APP_MENU_GET_PRIVATE (self);
375 return priv->parent_window;
379 screen_size_changed (GdkScreen *screen,
382 hildon_app_menu_apply_style (GTK_WIDGET (menu));
384 if (gdk_screen_get_width (screen) > gdk_screen_get_height (screen)) {
385 hildon_app_menu_set_columns (menu, 2);
387 hildon_app_menu_set_columns (menu, 1);
392 can_activate_accel (GtkWidget *widget,
396 return GTK_WIDGET_VISIBLE (widget);
400 item_visibility_changed (GtkWidget *item,
404 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
406 if (! priv->inhibit_repack)
407 hildon_app_menu_repack_items (menu, g_list_index (priv->buttons, item));
411 filter_visibility_changed (GtkWidget *item,
415 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
417 if (! priv->inhibit_repack)
418 hildon_app_menu_repack_filters (menu);
422 remove_item_from_list (GList **list,
425 *list = g_list_remove (*list, item);
429 hildon_app_menu_show_all (GtkWidget *widget)
431 HildonAppMenu *menu = HILDON_APP_MENU (widget);
432 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
434 priv->inhibit_repack = TRUE;
436 /* Show children, but not self. */
437 g_list_foreach (priv->buttons, (GFunc) gtk_widget_show_all, NULL);
438 g_list_foreach (priv->filters, (GFunc) gtk_widget_show_all, NULL);
440 priv->inhibit_repack = FALSE;
442 hildon_app_menu_repack_items (menu, 0);
443 hildon_app_menu_repack_filters (menu);
448 hildon_app_menu_hide_all (GtkWidget *widget)
450 HildonAppMenu *menu = HILDON_APP_MENU (widget);
451 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
453 priv->inhibit_repack = TRUE;
455 /* Hide children, but not self. */
456 g_list_foreach (priv->buttons, (GFunc) gtk_widget_hide_all, NULL);
457 g_list_foreach (priv->filters, (GFunc) gtk_widget_hide_all, NULL);
459 priv->inhibit_repack = FALSE;
461 hildon_app_menu_repack_items (menu, 0);
462 hildon_app_menu_repack_filters (menu);
466 * There's a race condition that can freeze the UI if a dialog appears
467 * between a HildonAppMenu and its parent window, see NB#100468
470 hildon_app_menu_find_intruder (gpointer data)
472 GtkWidget *widget = GTK_WIDGET (data);
473 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
475 priv->find_intruder_idle_id = 0;
477 /* If there's a window between the menu and its parent window, hide the menu */
478 if (priv->parent_window) {
479 gboolean intruder_found = FALSE;
480 GdkScreen *screen = gtk_widget_get_screen (widget);
481 GList *stack = gdk_screen_get_window_stack (screen);
482 GList *parent_pos = g_list_find (stack, GTK_WIDGET (priv->parent_window)->window);
483 GList *toplevels = gtk_window_list_toplevels ();
486 for (i = toplevels; i != NULL && !intruder_found; i = i->next) {
487 if (i->data != widget && i->data != priv->parent_window) {
488 if (g_list_find (parent_pos, GTK_WIDGET (i->data)->window)) {
489 /* HildonBanners are not closed automatically when
490 * a new window appears, so we must close them by
491 * hand to make the AppMenu work as expected.
492 * Yes, this is a hack. See NB#111027 */
493 if (HILDON_IS_BANNER (i->data)) {
494 gtk_widget_hide (i->data);
496 intruder_found = TRUE;
502 g_list_foreach (stack, (GFunc) g_object_unref, NULL);
504 g_list_free (toplevels);
507 gtk_widget_hide (widget);
514 hildon_app_menu_map (GtkWidget *widget)
516 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
518 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
520 /* Grab pointer and keyboard */
521 if (priv->transfer_window == NULL) {
522 gboolean has_grab = FALSE;
524 priv->transfer_window = grab_transfer_window_get (widget);
526 if (gdk_pointer_grab (priv->transfer_window, TRUE,
527 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
528 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
529 GDK_POINTER_MOTION_MASK, NULL, NULL,
530 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
531 if (gdk_keyboard_grab (priv->transfer_window, TRUE,
532 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
535 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
541 gtk_grab_add (widget);
543 gdk_window_destroy (priv->transfer_window);
544 priv->transfer_window = NULL;
548 /* Make the menu temporary when it's mapped, so it's closed if a
549 * new window appears */
550 gtk_window_set_is_temporary (GTK_WINDOW (widget), TRUE);
552 priv->find_intruder_idle_id = gdk_threads_add_idle (hildon_app_menu_find_intruder, widget);
556 hildon_app_menu_unmap (GtkWidget *widget)
558 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
560 /* Remove the grab */
561 if (priv->transfer_window != NULL) {
562 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
564 gtk_grab_remove (widget);
566 gdk_window_destroy (priv->transfer_window);
567 priv->transfer_window = NULL;
570 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unmap (widget);
572 gtk_window_set_is_temporary (GTK_WINDOW (widget), FALSE);
576 hildon_app_menu_grab_notify (GtkWidget *widget,
577 gboolean was_grabbed)
579 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->grab_notify)
580 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->grab_notify (widget, was_grabbed);
582 if (!was_grabbed && GTK_WIDGET_VISIBLE (widget))
583 gtk_widget_hide (widget);
587 hildon_app_menu_hide_idle (gpointer widget)
589 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
590 gtk_widget_hide (GTK_WIDGET (widget));
591 priv->hide_idle_id = 0;
595 /* Send keyboard accelerators to the parent window, if necessary.
596 * This code is heavily based on gtk_menu_key_press ()
599 hildon_app_menu_key_press (GtkWidget *widget,
602 GtkWindow *parent_window;
603 HildonAppMenuPrivate *priv;
605 g_return_val_if_fail (HILDON_IS_APP_MENU (widget), FALSE);
606 g_return_val_if_fail (event != NULL, FALSE);
608 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->key_press_event (widget, event))
611 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
612 parent_window = priv->parent_window;
615 guint accel_key, accel_mods;
616 GdkModifierType consumed_modifiers;
618 GSList *accel_groups;
621 display = gtk_widget_get_display (widget);
623 /* Figure out what modifiers went into determining the key symbol */
624 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
625 event->hardware_keycode, event->state, event->group,
626 NULL, NULL, NULL, &consumed_modifiers);
628 accel_key = gdk_keyval_to_lower (event->keyval);
629 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
631 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
632 * We re-upper case when we match against the keyval, but display and save in caseless form.
634 if (accel_key != event->keyval)
635 accel_mods |= GDK_SHIFT_MASK;
637 accel_groups = gtk_accel_groups_from_object (G_OBJECT (parent_window));
639 for (list = accel_groups; list; list = list->next) {
640 GtkAccelGroup *accel_group = list->data;
642 if (gtk_accel_group_query (accel_group, accel_key, accel_mods, NULL)) {
643 gtk_window_activate_key (parent_window, event);
644 priv->hide_idle_id = gdk_threads_add_idle (hildon_app_menu_hide_idle, widget);
654 hildon_app_menu_button_press (GtkWidget *widget,
655 GdkEventButton *event)
658 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
660 gdk_window_get_position (widget->window, &x, &y);
662 /* Whether the button has been pressed outside the widget */
663 priv->pressed_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
664 event->y_root < y || event->y_root > y + widget->allocation.height);
666 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event) {
667 return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event (widget, event);
674 hildon_app_menu_button_release (GtkWidget *widget,
675 GdkEventButton *event)
677 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
679 if (priv->pressed_outside) {
681 gboolean released_outside;
683 gdk_window_get_position (widget->window, &x, &y);
685 /* Whether the button has been released outside the widget */
686 released_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
687 event->y_root < y || event->y_root > y + widget->allocation.height);
689 if (released_outside) {
690 gtk_widget_hide (widget);
693 priv->pressed_outside = FALSE; /* Always reset pressed_outside to FALSE */
696 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event) {
697 return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event (widget, event);
704 hildon_app_menu_delete_event_handler (GtkWidget *widget,
707 /* Hide the menu if it receives a delete-event, but don't destroy it */
708 gtk_widget_hide (widget);
712 /* Grab transfer window (based on the one from GtkMenu) */
714 grab_transfer_window_get (GtkWidget *widget)
717 GdkWindowAttr attributes;
718 gint attributes_mask;
722 attributes.width = 10;
723 attributes.height = 10;
724 attributes.window_type = GDK_WINDOW_TEMP;
725 attributes.wclass = GDK_INPUT_ONLY;
726 attributes.override_redirect = TRUE;
727 attributes.event_mask = 0;
729 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
731 window = gdk_window_new (gtk_widget_get_root_window (widget),
732 &attributes, attributes_mask);
733 gdk_window_set_user_data (window, widget);
735 gdk_window_show (window);
741 hildon_app_menu_size_request (GtkWidget *widget,
742 GtkRequisition *requisition)
744 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
746 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->size_request (widget, requisition);
748 requisition->width = priv->width_request;
752 hildon_app_menu_realize (GtkWidget *widget)
754 Atom property, window_type;
756 GdkDisplay *gdkdisplay;
759 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
761 gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
763 gdkdisplay = gdk_drawable_get_display (widget->window);
764 xdisplay = GDK_WINDOW_XDISPLAY (widget->window);
766 property = gdk_x11_get_xatom_by_name_for_display (gdkdisplay, "_NET_WM_WINDOW_TYPE");
767 window_type = XInternAtom (xdisplay, "_HILDON_WM_WINDOW_TYPE_APP_MENU", False);
768 XChangeProperty (xdisplay, GDK_WINDOW_XID (widget->window), property,
769 XA_ATOM, 32, PropModeReplace, (guchar *) &window_type, 1);
771 /* Detect any screen changes */
772 screen = gtk_widget_get_screen (widget);
773 g_signal_connect (screen, "size-changed", G_CALLBACK (screen_size_changed), widget);
775 /* Force menu to set the initial layout */
776 screen_size_changed (screen, HILDON_APP_MENU (widget));
780 hildon_app_menu_unrealize (GtkWidget *widget)
782 GdkScreen *screen = gtk_widget_get_screen (widget);
783 /* Disconnect "size-changed" signal handler */
784 g_signal_handlers_disconnect_by_func (screen, G_CALLBACK (screen_size_changed), widget);
786 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unrealize (widget);
790 hildon_app_menu_apply_style (GtkWidget *widget)
793 guint horizontal_spacing, vertical_spacing, filter_vertical_spacing;
794 guint inner_border, external_border;
795 HildonAppMenuPrivate *priv;
797 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
799 gtk_widget_style_get (widget,
800 "horizontal-spacing", &horizontal_spacing,
801 "vertical-spacing", &vertical_spacing,
802 "filter-vertical-spacing", &filter_vertical_spacing,
803 "inner-border", &inner_border,
804 "external-border", &external_border,
808 gtk_table_set_row_spacings (priv->table, vertical_spacing);
809 gtk_table_set_col_spacings (priv->table, horizontal_spacing);
810 gtk_box_set_spacing (priv->vbox, filter_vertical_spacing);
812 /* Set inner border */
813 gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
815 /* Compute width request */
816 screen = gtk_widget_get_screen (widget);
817 if (gdk_screen_get_width (screen) < gdk_screen_get_height (screen)) {
820 priv->width_request = gdk_screen_get_width (screen) - external_border * 2;
821 gtk_window_move (GTK_WINDOW (widget), external_border, 0);
822 gtk_widget_queue_resize (widget);
826 hildon_app_menu_style_set (GtkWidget *widget,
827 GtkStyle *previous_style)
829 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set)
830 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set (widget, previous_style);
832 hildon_app_menu_apply_style (widget);
836 hildon_app_menu_repack_filters (HildonAppMenu *menu)
838 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
841 for (iter = priv->filters; iter != NULL; iter = iter->next) {
842 GtkWidget *filter = GTK_WIDGET (iter->data);
843 GtkWidget *parent = gtk_widget_get_parent (filter);
845 g_object_ref (filter);
846 gtk_container_remove (GTK_CONTAINER (parent), filter);
850 for (iter = priv->filters; iter != NULL; iter = iter->next) {
851 GtkWidget *filter = GTK_WIDGET (iter->data);
852 if (GTK_WIDGET_VISIBLE (filter)) {
853 gtk_box_pack_start (GTK_BOX (priv->filters_hbox), filter, TRUE, TRUE, 0);
854 g_object_unref (filter);
855 /* GtkButton must be realized for accelerators to work */
856 gtk_widget_realize (filter);
862 * When items displayed in the menu change (e.g, a new item is added,
863 * an item is hidden or the list is reordered), the layout must be
864 * updated. To do this we repack all items starting from a given one.
867 hildon_app_menu_repack_items (HildonAppMenu *menu,
870 HildonAppMenuPrivate *priv;
871 gint row, col, nvisible, i;
874 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
877 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
878 /* Count number of visible items */
879 if (GTK_WIDGET_VISIBLE (iter->data))
881 /* Remove buttons from their parent */
882 if (start_from != -1 && i >= start_from) {
883 GtkWidget *item = GTK_WIDGET (iter->data);
884 GtkWidget *parent = gtk_widget_get_parent (item);
887 gtk_container_remove (GTK_CONTAINER (parent), item);
893 /* If items have been removed, recalculate the size of the menu */
894 if (start_from != -1)
895 gtk_window_resize (GTK_WINDOW (menu), 1, 1);
897 /* Set the final size now to avoid unnecessary resizes later */
899 gtk_table_resize (priv->table, ((nvisible - 1) / priv->columns) + 1, priv->columns);
903 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
904 GtkWidget *item = GTK_WIDGET (iter->data);
905 if (GTK_WIDGET_VISIBLE (item)) {
906 /* Don't add an item to the table if it's already there */
907 if (gtk_widget_get_parent (item) == NULL) {
908 gtk_table_attach_defaults (priv->table, item, col, col + 1, row, row + 1);
909 g_object_unref (item);
910 /* GtkButton must be realized for accelerators to work */
911 gtk_widget_realize (item);
913 if (++col == priv->columns) {
922 * hildon_app_menu_popup:
923 * @menu: a #HildonAppMenu
924 * @parent_window: a #GtkWindow
926 * Displays a menu on top of a window and makes it available for
932 hildon_app_menu_popup (HildonAppMenu *menu,
933 GtkWindow *parent_window)
935 HildonAppMenuPrivate *priv;
936 gboolean show_menu = FALSE;
939 g_return_if_fail (HILDON_IS_APP_MENU (menu));
940 g_return_if_fail (GTK_IS_WINDOW (parent_window));
942 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
944 /* Don't show menu if it doesn't contain visible items */
945 for (i = priv->buttons; i && !show_menu; i = i->next)
946 show_menu = GTK_WIDGET_VISIBLE (i->data);
948 for (i = priv->filters; i && !show_menu; i = i->next)
949 show_menu = GTK_WIDGET_VISIBLE (i->data);
952 hildon_app_menu_set_parent_window (menu, parent_window);
953 gtk_widget_show (GTK_WIDGET (menu));
959 * hildon_app_menu_get_items:
960 * @menu: a #HildonAppMenu
962 * Returns a list of all items (regular items, not filters) contained
965 * Returns: a newly-allocated list containing the items in @menu
970 hildon_app_menu_get_items (HildonAppMenu *menu)
972 HildonAppMenuPrivate *priv;
974 g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
976 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
978 return g_list_copy (priv->buttons);
982 * hildon_app_menu_get_filters:
983 * @menu: a #HildonAppMenu
985 * Returns a list of all filters contained in @menu.
987 * Returns: a newly-allocated list containing the filters in @menu
992 hildon_app_menu_get_filters (HildonAppMenu *menu)
994 HildonAppMenuPrivate *priv;
996 g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
998 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
1000 return g_list_copy (priv->filters);
1004 hildon_app_menu_init (HildonAppMenu *menu)
1006 GtkWidget *alignment;
1007 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
1009 /* Initialize private variables */
1010 priv->parent_window = NULL;
1011 priv->transfer_window = NULL;
1012 priv->pressed_outside = FALSE;
1013 priv->inhibit_repack = FALSE;
1014 priv->buttons = NULL;
1015 priv->filters = NULL;
1017 priv->width_request = -1;
1018 priv->find_intruder_idle_id = 0;
1019 priv->hide_idle_id = 0;
1021 /* Create boxes and tables */
1022 priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
1023 priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 0));
1024 priv->table = GTK_TABLE (gtk_table_new (1, priv->columns, TRUE));
1026 /* Align the filters to the center */
1027 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
1028 gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
1030 /* Pack everything */
1031 gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
1032 gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
1033 gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
1035 /* This should be treated like a normal, ref-counted widget */
1036 g_object_force_floating (G_OBJECT (menu));
1037 GTK_WINDOW (menu)->has_user_ref_count = FALSE;
1039 gtk_widget_show_all (GTK_WIDGET (priv->vbox));
1043 hildon_app_menu_finalize (GObject *object)
1045 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
1047 if (priv->find_intruder_idle_id) {
1048 g_source_remove (priv->find_intruder_idle_id);
1049 priv->find_intruder_idle_id = 0;
1052 if (priv->hide_idle_id) {
1053 g_source_remove (priv->hide_idle_id);
1054 priv->hide_idle_id = 0;
1057 if (priv->parent_window) {
1058 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, object);
1059 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, object);
1062 if (priv->transfer_window)
1063 gdk_window_destroy (priv->transfer_window);
1065 g_list_foreach (priv->buttons, (GFunc) g_object_unref, NULL);
1066 g_list_foreach (priv->filters, (GFunc) g_object_unref, NULL);
1068 g_list_free (priv->buttons);
1069 g_list_free (priv->filters);
1071 g_signal_handlers_destroy (object);
1072 G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
1076 hildon_app_menu_class_init (HildonAppMenuClass *klass)
1078 GObjectClass *gobject_class = (GObjectClass *)klass;
1079 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
1081 gobject_class->finalize = hildon_app_menu_finalize;
1082 widget_class->show_all = hildon_app_menu_show_all;
1083 widget_class->hide_all = hildon_app_menu_hide_all;
1084 widget_class->map = hildon_app_menu_map;
1085 widget_class->unmap = hildon_app_menu_unmap;
1086 widget_class->realize = hildon_app_menu_realize;
1087 widget_class->unrealize = hildon_app_menu_unrealize;
1088 widget_class->grab_notify = hildon_app_menu_grab_notify;
1089 widget_class->key_press_event = hildon_app_menu_key_press;
1090 widget_class->button_press_event = hildon_app_menu_button_press;
1091 widget_class->button_release_event = hildon_app_menu_button_release;
1092 widget_class->style_set = hildon_app_menu_style_set;
1093 widget_class->delete_event = hildon_app_menu_delete_event_handler;
1094 widget_class->size_request = hildon_app_menu_size_request;
1096 g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
1098 gtk_widget_class_install_style_property (
1101 "horizontal-spacing",
1102 "Horizontal spacing on menu items",
1103 "Horizontal spacing between each menu item. Does not apply to filter buttons.",
1107 gtk_widget_class_install_style_property (
1111 "Vertical spacing on menu items",
1112 "Vertical spacing between each menu item. Does not apply to filter buttons.",
1116 gtk_widget_class_install_style_property (
1119 "filter-vertical-spacing",
1120 "Vertical spacing between filters and menu items",
1121 "Vertical spacing between filters and menu items",
1125 gtk_widget_class_install_style_property (
1129 "Border between menu edges and buttons",
1130 "Border between menu edges and buttons",
1134 gtk_widget_class_install_style_property (
1138 "Border between menu and screen edges (in horizontal mode)",
1139 "Border between the right and left edges of the menu and "
1140 "the screen edges (in horizontal mode)",