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 hildon_app_menu_repack_items (menu, g_list_index (priv->buttons, item));
410 filter_visibility_changed (GtkWidget *item,
414 hildon_app_menu_repack_filters (menu);
418 remove_item_from_list (GList **list,
421 *list = g_list_remove (*list, item);
425 hildon_app_menu_show_all (GtkWidget *widget)
427 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
429 /* Show children, but not self. */
430 g_list_foreach (priv->buttons, (GFunc) gtk_widget_show_all, NULL);
431 g_list_foreach (priv->filters, (GFunc) gtk_widget_show_all, NULL);
436 hildon_app_menu_hide_all (GtkWidget *widget)
438 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
440 /* Hide children, but not self. */
441 g_list_foreach (priv->buttons, (GFunc) gtk_widget_hide_all, NULL);
442 g_list_foreach (priv->filters, (GFunc) gtk_widget_hide_all, NULL);
446 * There's a race condition that can freeze the UI if a dialog appears
447 * between a HildonAppMenu and its parent window, see NB#100468
450 hildon_app_menu_find_intruder (gpointer data)
452 GtkWidget *widget = GTK_WIDGET (data);
453 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
455 priv->find_intruder_idle_id = 0;
457 /* If there's a window between the menu and its parent window, hide the menu */
458 if (priv->parent_window) {
459 gboolean intruder_found = FALSE;
460 GdkScreen *screen = gtk_widget_get_screen (widget);
461 GList *stack = gdk_screen_get_window_stack (screen);
462 GList *parent_pos = g_list_find (stack, GTK_WIDGET (priv->parent_window)->window);
463 GList *toplevels = gtk_window_list_toplevels ();
466 for (i = toplevels; i != NULL && !intruder_found; i = i->next) {
467 if (i->data != widget && i->data != priv->parent_window) {
468 if (g_list_find (parent_pos, GTK_WIDGET (i->data)->window)) {
469 /* HildonBanners are not closed automatically when
470 * a new window appears, so we must close them by
471 * hand to make the AppMenu work as expected.
472 * Yes, this is a hack. See NB#111027 */
473 if (HILDON_IS_BANNER (i->data)) {
474 gtk_widget_hide (i->data);
476 intruder_found = TRUE;
482 g_list_foreach (stack, (GFunc) g_object_unref, NULL);
484 g_list_free (toplevels);
487 gtk_widget_hide (widget);
494 hildon_app_menu_map (GtkWidget *widget)
496 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
498 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
500 /* Grab pointer and keyboard */
501 if (priv->transfer_window == NULL) {
502 gboolean has_grab = FALSE;
504 priv->transfer_window = grab_transfer_window_get (widget);
506 if (gdk_pointer_grab (priv->transfer_window, TRUE,
507 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
508 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
509 GDK_POINTER_MOTION_MASK, NULL, NULL,
510 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
511 if (gdk_keyboard_grab (priv->transfer_window, TRUE,
512 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
515 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
521 gtk_grab_add (widget);
523 gdk_window_destroy (priv->transfer_window);
524 priv->transfer_window = NULL;
528 /* Make the menu temporary when it's mapped, so it's closed if a
529 * new window appears */
530 gtk_window_set_is_temporary (GTK_WINDOW (widget), TRUE);
532 priv->find_intruder_idle_id = gdk_threads_add_idle (hildon_app_menu_find_intruder, widget);
536 hildon_app_menu_unmap (GtkWidget *widget)
538 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
540 /* Remove the grab */
541 if (priv->transfer_window != NULL) {
542 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
544 gtk_grab_remove (widget);
546 gdk_window_destroy (priv->transfer_window);
547 priv->transfer_window = NULL;
550 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unmap (widget);
552 gtk_window_set_is_temporary (GTK_WINDOW (widget), FALSE);
556 hildon_app_menu_grab_notify (GtkWidget *widget,
557 gboolean was_grabbed)
559 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->grab_notify)
560 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->grab_notify (widget, was_grabbed);
562 if (!was_grabbed && GTK_WIDGET_VISIBLE (widget))
563 gtk_widget_hide (widget);
567 hildon_app_menu_hide_idle (gpointer widget)
569 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
570 gtk_widget_hide (GTK_WIDGET (widget));
571 priv->hide_idle_id = 0;
575 /* Send keyboard accelerators to the parent window, if necessary.
576 * This code is heavily based on gtk_menu_key_press ()
579 hildon_app_menu_key_press (GtkWidget *widget,
582 GtkWindow *parent_window;
583 HildonAppMenuPrivate *priv;
585 g_return_val_if_fail (HILDON_IS_APP_MENU (widget), FALSE);
586 g_return_val_if_fail (event != NULL, FALSE);
588 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->key_press_event (widget, event))
591 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
592 parent_window = priv->parent_window;
595 guint accel_key, accel_mods;
596 GdkModifierType consumed_modifiers;
598 GSList *accel_groups;
601 display = gtk_widget_get_display (widget);
603 /* Figure out what modifiers went into determining the key symbol */
604 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
605 event->hardware_keycode, event->state, event->group,
606 NULL, NULL, NULL, &consumed_modifiers);
608 accel_key = gdk_keyval_to_lower (event->keyval);
609 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
611 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
612 * We re-upper case when we match against the keyval, but display and save in caseless form.
614 if (accel_key != event->keyval)
615 accel_mods |= GDK_SHIFT_MASK;
617 accel_groups = gtk_accel_groups_from_object (G_OBJECT (parent_window));
619 for (list = accel_groups; list; list = list->next) {
620 GtkAccelGroup *accel_group = list->data;
622 if (gtk_accel_group_query (accel_group, accel_key, accel_mods, NULL)) {
623 gtk_window_activate_key (parent_window, event);
624 priv->hide_idle_id = gdk_threads_add_idle (hildon_app_menu_hide_idle, widget);
634 hildon_app_menu_button_press (GtkWidget *widget,
635 GdkEventButton *event)
638 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
640 gdk_window_get_position (widget->window, &x, &y);
642 /* Whether the button has been pressed outside the widget */
643 priv->pressed_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
644 event->y_root < y || event->y_root > y + widget->allocation.height);
646 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event) {
647 return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event (widget, event);
654 hildon_app_menu_button_release (GtkWidget *widget,
655 GdkEventButton *event)
657 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
659 if (priv->pressed_outside) {
661 gboolean released_outside;
663 gdk_window_get_position (widget->window, &x, &y);
665 /* Whether the button has been released outside the widget */
666 released_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
667 event->y_root < y || event->y_root > y + widget->allocation.height);
669 if (released_outside) {
670 gtk_widget_hide (widget);
673 priv->pressed_outside = FALSE; /* Always reset pressed_outside to FALSE */
676 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event) {
677 return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event (widget, event);
684 hildon_app_menu_delete_event_handler (GtkWidget *widget,
687 /* Hide the menu if it receives a delete-event, but don't destroy it */
688 gtk_widget_hide (widget);
692 /* Grab transfer window (based on the one from GtkMenu) */
694 grab_transfer_window_get (GtkWidget *widget)
697 GdkWindowAttr attributes;
698 gint attributes_mask;
702 attributes.width = 10;
703 attributes.height = 10;
704 attributes.window_type = GDK_WINDOW_TEMP;
705 attributes.wclass = GDK_INPUT_ONLY;
706 attributes.override_redirect = TRUE;
707 attributes.event_mask = 0;
709 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
711 window = gdk_window_new (gtk_widget_get_root_window (widget),
712 &attributes, attributes_mask);
713 gdk_window_set_user_data (window, widget);
715 gdk_window_show (window);
721 hildon_app_menu_size_request (GtkWidget *widget,
722 GtkRequisition *requisition)
724 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
726 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->size_request (widget, requisition);
728 requisition->width = priv->width_request;
732 hildon_app_menu_realize (GtkWidget *widget)
734 Atom property, window_type;
736 GdkDisplay *gdkdisplay;
739 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
741 gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
743 gdkdisplay = gdk_drawable_get_display (widget->window);
744 xdisplay = GDK_WINDOW_XDISPLAY (widget->window);
746 property = gdk_x11_get_xatom_by_name_for_display (gdkdisplay, "_NET_WM_WINDOW_TYPE");
747 window_type = XInternAtom (xdisplay, "_HILDON_WM_WINDOW_TYPE_APP_MENU", False);
748 XChangeProperty (xdisplay, GDK_WINDOW_XID (widget->window), property,
749 XA_ATOM, 32, PropModeReplace, (guchar *) &window_type, 1);
751 /* Detect any screen changes */
752 screen = gtk_widget_get_screen (widget);
753 g_signal_connect (screen, "size-changed", G_CALLBACK (screen_size_changed), widget);
755 /* Force menu to set the initial layout */
756 screen_size_changed (screen, HILDON_APP_MENU (widget));
760 hildon_app_menu_unrealize (GtkWidget *widget)
762 GdkScreen *screen = gtk_widget_get_screen (widget);
763 /* Disconnect "size-changed" signal handler */
764 g_signal_handlers_disconnect_by_func (screen, G_CALLBACK (screen_size_changed), widget);
766 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unrealize (widget);
770 hildon_app_menu_apply_style (GtkWidget *widget)
773 guint horizontal_spacing, vertical_spacing, filter_vertical_spacing;
774 guint inner_border, external_border;
775 HildonAppMenuPrivate *priv;
777 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
779 gtk_widget_style_get (widget,
780 "horizontal-spacing", &horizontal_spacing,
781 "vertical-spacing", &vertical_spacing,
782 "filter-vertical-spacing", &filter_vertical_spacing,
783 "inner-border", &inner_border,
784 "external-border", &external_border,
788 gtk_table_set_row_spacings (priv->table, vertical_spacing);
789 gtk_table_set_col_spacings (priv->table, horizontal_spacing);
790 gtk_box_set_spacing (priv->vbox, filter_vertical_spacing);
792 /* Set inner border */
793 gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
795 /* Compute width request */
796 screen = gtk_widget_get_screen (widget);
797 if (gdk_screen_get_width (screen) < gdk_screen_get_height (screen)) {
800 priv->width_request = gdk_screen_get_width (screen) - external_border * 2;
801 gtk_window_move (GTK_WINDOW (widget), external_border, 0);
802 gtk_widget_queue_resize (widget);
806 hildon_app_menu_style_set (GtkWidget *widget,
807 GtkStyle *previous_style)
809 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set)
810 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set (widget, previous_style);
812 hildon_app_menu_apply_style (widget);
816 hildon_app_menu_repack_filters (HildonAppMenu *menu)
818 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
821 for (iter = priv->filters; iter != NULL; iter = iter->next) {
822 GtkWidget *filter = GTK_WIDGET (iter->data);
823 GtkWidget *parent = gtk_widget_get_parent (filter);
825 g_object_ref (filter);
826 gtk_container_remove (GTK_CONTAINER (parent), filter);
830 for (iter = priv->filters; iter != NULL; iter = iter->next) {
831 GtkWidget *filter = GTK_WIDGET (iter->data);
832 if (GTK_WIDGET_VISIBLE (filter)) {
833 gtk_box_pack_start (GTK_BOX (priv->filters_hbox), filter, TRUE, TRUE, 0);
834 g_object_unref (filter);
835 /* GtkButton must be realized for accelerators to work */
836 gtk_widget_realize (filter);
842 * When items displayed in the menu change (e.g, a new item is added,
843 * an item is hidden or the list is reordered), the layout must be
844 * updated. To do this we repack all items starting from a given one.
847 hildon_app_menu_repack_items (HildonAppMenu *menu,
850 HildonAppMenuPrivate *priv;
854 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
856 /* Remove buttons from their parent */
857 if (start_from != -1) {
858 for (iter = g_list_nth (priv->buttons, start_from); iter != NULL; iter = iter->next) {
859 GtkWidget *item = GTK_WIDGET (iter->data);
860 GtkWidget *parent = gtk_widget_get_parent (item);
863 gtk_container_remove (GTK_CONTAINER (parent), item);
867 /* If items have been removed, recalculate the size of the menu */
868 gtk_window_resize (GTK_WINDOW (menu), 1, 1);
873 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
874 GtkWidget *item = GTK_WIDGET (iter->data);
875 if (GTK_WIDGET_VISIBLE (item)) {
876 /* Don't add an item to the table if it's already there */
877 if (gtk_widget_get_parent (item) == NULL) {
878 gtk_table_attach_defaults (priv->table, item, col, col + 1, row, row + 1);
879 g_object_unref (item);
880 /* GtkButton must be realized for accelerators to work */
881 gtk_widget_realize (item);
883 if (++col == priv->columns) {
890 /* The number of rows/columns might have changed, so we have to
891 * resize the table */
893 gtk_table_resize (priv->table, MAX (row, 1), priv->columns);
895 gtk_table_resize (priv->table, row + 1, priv->columns);
900 * hildon_app_menu_popup:
901 * @menu: a #HildonAppMenu
902 * @parent_window: a #GtkWindow
904 * Displays a menu on top of a window and makes it available for
910 hildon_app_menu_popup (HildonAppMenu *menu,
911 GtkWindow *parent_window)
913 HildonAppMenuPrivate *priv;
914 gboolean show_menu = FALSE;
917 g_return_if_fail (HILDON_IS_APP_MENU (menu));
918 g_return_if_fail (GTK_IS_WINDOW (parent_window));
920 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
922 /* Don't show menu if it doesn't contain visible items */
923 for (i = priv->buttons; i && !show_menu; i = i->next)
924 show_menu = GTK_WIDGET_VISIBLE (i->data);
926 for (i = priv->filters; i && !show_menu; i = i->next)
927 show_menu = GTK_WIDGET_VISIBLE (i->data);
930 hildon_app_menu_set_parent_window (menu, parent_window);
931 gtk_widget_show (GTK_WIDGET (menu));
937 * hildon_app_menu_get_items:
938 * @menu: a #HildonAppMenu
940 * Returns a list of all items (regular items, not filters) contained
943 * Returns: a newly-allocated list containing the items in @menu
948 hildon_app_menu_get_items (HildonAppMenu *menu)
950 HildonAppMenuPrivate *priv;
952 g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
954 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
956 return g_list_copy (priv->buttons);
960 * hildon_app_menu_get_filters:
961 * @menu: a #HildonAppMenu
963 * Returns a list of all filters contained in @menu.
965 * Returns: a newly-allocated list containing the filters in @menu
970 hildon_app_menu_get_filters (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->filters);
982 hildon_app_menu_init (HildonAppMenu *menu)
984 GtkWidget *alignment;
985 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
987 /* Initialize private variables */
988 priv->parent_window = NULL;
989 priv->transfer_window = NULL;
990 priv->pressed_outside = FALSE;
991 priv->buttons = NULL;
992 priv->filters = NULL;
994 priv->width_request = -1;
995 priv->find_intruder_idle_id = 0;
996 priv->hide_idle_id = 0;
998 /* Create boxes and tables */
999 priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
1000 priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 0));
1001 priv->table = GTK_TABLE (gtk_table_new (1, priv->columns, TRUE));
1003 /* Align the filters to the center */
1004 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
1005 gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
1007 /* Pack everything */
1008 gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
1009 gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
1010 gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
1012 /* This should be treated like a normal, ref-counted widget */
1013 g_object_force_floating (G_OBJECT (menu));
1014 GTK_WINDOW (menu)->has_user_ref_count = FALSE;
1016 gtk_widget_show_all (GTK_WIDGET (priv->vbox));
1020 hildon_app_menu_finalize (GObject *object)
1022 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
1024 if (priv->find_intruder_idle_id) {
1025 g_source_remove (priv->find_intruder_idle_id);
1026 priv->find_intruder_idle_id = 0;
1029 if (priv->hide_idle_id) {
1030 g_source_remove (priv->hide_idle_id);
1031 priv->hide_idle_id = 0;
1034 if (priv->parent_window) {
1035 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, object);
1036 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, object);
1039 if (priv->transfer_window)
1040 gdk_window_destroy (priv->transfer_window);
1042 g_list_foreach (priv->buttons, (GFunc) g_object_unref, NULL);
1043 g_list_foreach (priv->filters, (GFunc) g_object_unref, NULL);
1045 g_list_free (priv->buttons);
1046 g_list_free (priv->filters);
1048 g_signal_handlers_destroy (object);
1049 G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
1053 hildon_app_menu_class_init (HildonAppMenuClass *klass)
1055 GObjectClass *gobject_class = (GObjectClass *)klass;
1056 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
1058 gobject_class->finalize = hildon_app_menu_finalize;
1059 widget_class->show_all = hildon_app_menu_show_all;
1060 widget_class->hide_all = hildon_app_menu_hide_all;
1061 widget_class->map = hildon_app_menu_map;
1062 widget_class->unmap = hildon_app_menu_unmap;
1063 widget_class->realize = hildon_app_menu_realize;
1064 widget_class->unrealize = hildon_app_menu_unrealize;
1065 widget_class->grab_notify = hildon_app_menu_grab_notify;
1066 widget_class->key_press_event = hildon_app_menu_key_press;
1067 widget_class->button_press_event = hildon_app_menu_button_press;
1068 widget_class->button_release_event = hildon_app_menu_button_release;
1069 widget_class->style_set = hildon_app_menu_style_set;
1070 widget_class->delete_event = hildon_app_menu_delete_event_handler;
1071 widget_class->size_request = hildon_app_menu_size_request;
1073 g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
1075 gtk_widget_class_install_style_property (
1078 "horizontal-spacing",
1079 "Horizontal spacing on menu items",
1080 "Horizontal spacing between each menu item. Does not apply to filter buttons.",
1084 gtk_widget_class_install_style_property (
1088 "Vertical spacing on menu items",
1089 "Vertical spacing between each menu item. Does not apply to filter buttons.",
1093 gtk_widget_class_install_style_property (
1096 "filter-vertical-spacing",
1097 "Vertical spacing between filters and menu items",
1098 "Vertical spacing between filters and menu items",
1102 gtk_widget_class_install_style_property (
1106 "Border between menu edges and buttons",
1107 "Border between menu edges and buttons",
1111 gtk_widget_class_install_style_property (
1115 "Border between menu and screen edges (in horizontal mode)",
1116 "Border between the right and left edges of the menu and "
1117 "the screen edges (in horizontal mode)",