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"
104 grab_transfer_window_get (GtkWidget *widget);
107 hildon_app_menu_repack_items (HildonAppMenu *menu,
111 hildon_app_menu_repack_filters (HildonAppMenu *menu);
114 can_activate_accel (GtkWidget *widget,
119 item_visibility_changed (GtkWidget *item,
121 HildonAppMenu *menu);
124 filter_visibility_changed (GtkWidget *item,
126 HildonAppMenu *menu);
129 remove_item_from_list (GList **list,
133 hildon_app_menu_apply_style (GtkWidget *widget);
135 G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
138 * hildon_app_menu_new:
140 * Creates a new #HildonAppMenu.
142 * Return value: A #HildonAppMenu.
147 hildon_app_menu_new (void)
149 GtkWidget *menu = g_object_new (HILDON_TYPE_APP_MENU, NULL);
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).
159 * Adds @item to @menu at the position indicated by @position.
164 hildon_app_menu_insert (HildonAppMenu *menu,
168 HildonAppMenuPrivate *priv;
170 g_return_if_fail (HILDON_IS_APP_MENU (menu));
171 g_return_if_fail (GTK_IS_BUTTON (item));
173 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
175 /* Force widget size */
176 hildon_gtk_widget_set_theme_size (GTK_WIDGET (item),
177 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
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);
185 /* Enable accelerators */
186 g_signal_connect (item, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
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);
192 /* Remove item from list when it is destroyed */
193 g_object_weak_ref (G_OBJECT (item), (GWeakNotify) remove_item_from_list, &(priv->buttons));
197 * hildon_app_menu_append:
198 * @menu : A #HildonAppMenu
199 * @item : A #GtkButton to add to the #HildonAppMenu
201 * Adds @item to the end of the menu's item list.
206 hildon_app_menu_append (HildonAppMenu *menu,
209 hildon_app_menu_insert (menu, item, -1);
213 * hildon_app_menu_prepend:
214 * @menu : A #HildonAppMenu
215 * @item : A #GtkButton to add to the #HildonAppMenu
217 * Adds @item to the beginning of the menu's item list.
222 hildon_app_menu_prepend (HildonAppMenu *menu,
225 hildon_app_menu_insert (menu, item, 0);
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).
234 * Moves a #GtkButton to a new position within #HildonAppMenu.
239 hildon_app_menu_reorder_child (HildonAppMenu *menu,
243 HildonAppMenuPrivate *priv;
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);
250 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
251 old_position = g_list_index (priv->buttons, item);
253 g_return_if_fail (old_position >= 0);
256 priv->buttons = g_list_remove (priv->buttons, item);
257 priv->buttons = g_list_insert (priv->buttons, item, position);
259 hildon_app_menu_repack_items (menu, MIN (old_position, position));
263 * hildon_app_menu_add_filter:
264 * @menu : A #HildonAppMenu
265 * @filter : A #GtkButton to add to the #HildonAppMenu.
267 * Adds the @filter to @menu.
272 hildon_app_menu_add_filter (HildonAppMenu *menu,
275 HildonAppMenuPrivate *priv;
277 g_return_if_fail (HILDON_IS_APP_MENU (menu));
278 g_return_if_fail (GTK_IS_BUTTON (filter));
280 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
282 /* Force widget size */
283 hildon_gtk_widget_set_theme_size (GTK_WIDGET (filter),
284 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
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);
292 /* Enable accelerators */
293 g_signal_connect (filter, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
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);
299 /* Remove filter from list when it is destroyed */
300 g_object_weak_ref (G_OBJECT (filter), (GWeakNotify) remove_item_from_list, &(priv->filters));
304 hildon_app_menu_set_columns (HildonAppMenu *menu,
307 HildonAppMenuPrivate *priv;
309 g_return_if_fail (HILDON_IS_APP_MENU (menu));
310 g_return_if_fail (columns > 0);
312 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
314 if (columns != priv->columns) {
315 priv->columns = columns;
316 hildon_app_menu_repack_items (menu, 0);
321 parent_window_topmost_notify (HildonWindow *parent_win,
325 if (!hildon_window_get_is_topmost (parent_win))
326 gtk_widget_hide (menu);
330 parent_window_unmapped (HildonWindow *parent_win,
333 gtk_widget_hide (menu);
337 hildon_app_menu_set_parent_window (HildonAppMenu *self,
338 GtkWindow *parent_window)
340 HildonAppMenuPrivate *priv;
342 g_return_if_fail (HILDON_IS_APP_MENU (self));
343 g_return_if_fail (parent_window == NULL || GTK_IS_WINDOW (parent_window));
345 priv = HILDON_APP_MENU_GET_PRIVATE(self);
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);
353 /* Connect a new handler */
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);
359 priv->parent_window = parent_window;
361 if (parent_window == NULL && GTK_WIDGET_VISIBLE (self))
362 gtk_widget_hide (GTK_WIDGET (self));
365 gpointer G_GNUC_INTERNAL
366 hildon_app_menu_get_parent_window (HildonAppMenu *self)
368 HildonAppMenuPrivate *priv;
370 g_return_val_if_fail (HILDON_IS_APP_MENU (self), NULL);
372 priv = HILDON_APP_MENU_GET_PRIVATE (self);
374 return priv->parent_window;
378 screen_size_changed (GdkScreen *screen,
381 hildon_app_menu_apply_style (GTK_WIDGET (menu));
383 if (gdk_screen_get_width (screen) > gdk_screen_get_height (screen)) {
384 hildon_app_menu_set_columns (menu, 2);
386 hildon_app_menu_set_columns (menu, 1);
391 can_activate_accel (GtkWidget *widget,
395 return GTK_WIDGET_VISIBLE (widget);
399 item_visibility_changed (GtkWidget *item,
403 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
405 hildon_app_menu_repack_items (menu, g_list_index (priv->buttons, item));
409 filter_visibility_changed (GtkWidget *item,
413 hildon_app_menu_repack_filters (menu);
417 remove_item_from_list (GList **list,
420 *list = g_list_remove (*list, item);
424 hildon_app_menu_show (GtkWidget *widget)
426 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
427 gboolean show_menu = FALSE;
430 /* Don't show menu if it doesn't contain visible items */
431 for (i = priv->buttons; i && !show_menu; i = i->next)
432 show_menu = GTK_WIDGET_VISIBLE (i->data);
434 for (i = priv->filters; i && !show_menu; i = i->next)
435 show_menu = GTK_WIDGET_VISIBLE (i->data);
438 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->show (widget);
443 hildon_app_menu_show_all (GtkWidget *widget)
445 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
447 /* Show children, but not self. */
448 g_list_foreach (priv->buttons, (GFunc) gtk_widget_show_all, NULL);
449 g_list_foreach (priv->filters, (GFunc) gtk_widget_show_all, NULL);
454 hildon_app_menu_hide_all (GtkWidget *widget)
456 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
458 /* Hide children, but not self. */
459 g_list_foreach (priv->buttons, (GFunc) gtk_widget_hide_all, NULL);
460 g_list_foreach (priv->filters, (GFunc) gtk_widget_hide_all, NULL);
464 * There's a race condition that can freeze the UI if a dialog appears
465 * between a HildonAppMenu and its parent window, see NB#100468
468 hildon_app_menu_find_intruder (gpointer data)
470 GtkWidget *widget = GTK_WIDGET (data);
471 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
473 priv->find_intruder_idle_id = 0;
475 /* If there's a window between the menu and its parent window, hide the menu */
476 if (priv->parent_window) {
477 gboolean intruder_found = FALSE;
478 GdkScreen *screen = gtk_widget_get_screen (widget);
479 GList *stack = gdk_screen_get_window_stack (screen);
480 GList *parent_pos = g_list_find (stack, GTK_WIDGET (priv->parent_window)->window);
481 GList *toplevels = gtk_window_list_toplevels ();
484 for (i = toplevels; i != NULL && !intruder_found; i = i->next) {
485 if (i->data != widget && i->data != priv->parent_window) {
486 if (g_list_find (parent_pos, GTK_WIDGET (i->data)->window)) {
487 intruder_found = TRUE;
492 g_list_foreach (stack, (GFunc) g_object_unref, NULL);
494 g_list_free (toplevels);
497 gtk_widget_hide (widget);
504 hildon_app_menu_map (GtkWidget *widget)
506 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
508 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
510 /* Grab pointer and keyboard */
511 if (priv->transfer_window == NULL) {
512 gboolean has_grab = FALSE;
514 priv->transfer_window = grab_transfer_window_get (widget);
516 if (gdk_pointer_grab (priv->transfer_window, TRUE,
517 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
518 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
519 GDK_POINTER_MOTION_MASK, NULL, NULL,
520 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
521 if (gdk_keyboard_grab (priv->transfer_window, TRUE,
522 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
525 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
531 gtk_grab_add (widget);
533 gdk_window_destroy (priv->transfer_window);
534 priv->transfer_window = NULL;
538 /* Make the menu temporary when it's mapped, so it's closed if a
539 * new window appears */
540 gtk_window_set_is_temporary (GTK_WINDOW (widget), TRUE);
542 priv->find_intruder_idle_id = gdk_threads_add_idle (hildon_app_menu_find_intruder, widget);
546 hildon_app_menu_unmap (GtkWidget *widget)
548 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
550 /* Remove the grab */
551 if (priv->transfer_window != NULL) {
552 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
554 gtk_grab_remove (widget);
556 gdk_window_destroy (priv->transfer_window);
557 priv->transfer_window = NULL;
560 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unmap (widget);
562 gtk_window_set_is_temporary (GTK_WINDOW (widget), FALSE);
566 hildon_app_menu_grab_notify (GtkWidget *widget,
567 gboolean was_grabbed)
569 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->grab_notify)
570 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->grab_notify (widget, was_grabbed);
572 if (!was_grabbed && GTK_WIDGET_VISIBLE (widget))
573 gtk_widget_hide (widget);
577 hildon_app_menu_hide_idle (gpointer widget)
579 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
580 gtk_widget_hide (GTK_WIDGET (widget));
581 priv->hide_idle_id = 0;
585 /* Send keyboard accelerators to the parent window, if necessary.
586 * This code is heavily based on gtk_menu_key_press ()
589 hildon_app_menu_key_press (GtkWidget *widget,
592 GtkWindow *parent_window;
593 HildonAppMenuPrivate *priv;
595 g_return_val_if_fail (HILDON_IS_APP_MENU (widget), FALSE);
596 g_return_val_if_fail (event != NULL, FALSE);
598 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->key_press_event (widget, event))
601 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
602 parent_window = priv->parent_window;
605 guint accel_key, accel_mods;
606 GdkModifierType consumed_modifiers;
608 GSList *accel_groups;
611 display = gtk_widget_get_display (widget);
613 /* Figure out what modifiers went into determining the key symbol */
614 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
615 event->hardware_keycode, event->state, event->group,
616 NULL, NULL, NULL, &consumed_modifiers);
618 accel_key = gdk_keyval_to_lower (event->keyval);
619 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
621 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
622 * We re-upper case when we match against the keyval, but display and save in caseless form.
624 if (accel_key != event->keyval)
625 accel_mods |= GDK_SHIFT_MASK;
627 accel_groups = gtk_accel_groups_from_object (G_OBJECT (parent_window));
629 for (list = accel_groups; list; list = list->next) {
630 GtkAccelGroup *accel_group = list->data;
632 if (gtk_accel_group_query (accel_group, accel_key, accel_mods, NULL)) {
633 gtk_window_activate_key (parent_window, event);
634 priv->hide_idle_id = gdk_threads_add_idle (hildon_app_menu_hide_idle, widget);
644 hildon_app_menu_button_press (GtkWidget *widget,
645 GdkEventButton *event)
648 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
650 gdk_window_get_position (widget->window, &x, &y);
652 /* Whether the button has been pressed outside the widget */
653 priv->pressed_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
654 event->y_root < y || event->y_root > y + widget->allocation.height);
656 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event) {
657 return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event (widget, event);
664 hildon_app_menu_button_release (GtkWidget *widget,
665 GdkEventButton *event)
667 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
669 if (priv->pressed_outside) {
671 gboolean released_outside;
673 gdk_window_get_position (widget->window, &x, &y);
675 /* Whether the button has been released outside the widget */
676 released_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
677 event->y_root < y || event->y_root > y + widget->allocation.height);
679 if (released_outside) {
680 gtk_widget_hide (widget);
683 priv->pressed_outside = FALSE; /* Always reset pressed_outside to FALSE */
686 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event) {
687 return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event (widget, event);
694 hildon_app_menu_delete_event_handler (GtkWidget *widget,
697 /* Hide the menu if it receives a delete-event, but don't destroy it */
698 gtk_widget_hide (widget);
702 /* Grab transfer window (based on the one from GtkMenu) */
704 grab_transfer_window_get (GtkWidget *widget)
707 GdkWindowAttr attributes;
708 gint attributes_mask;
712 attributes.width = 10;
713 attributes.height = 10;
714 attributes.window_type = GDK_WINDOW_TEMP;
715 attributes.wclass = GDK_INPUT_ONLY;
716 attributes.override_redirect = TRUE;
717 attributes.event_mask = 0;
719 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
721 window = gdk_window_new (gtk_widget_get_root_window (widget),
722 &attributes, attributes_mask);
723 gdk_window_set_user_data (window, widget);
725 gdk_window_show (window);
731 hildon_app_menu_size_request (GtkWidget *widget,
732 GtkRequisition *requisition)
734 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
736 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->size_request (widget, requisition);
738 requisition->width = priv->width_request;
742 hildon_app_menu_realize (GtkWidget *widget)
744 Atom property, window_type;
746 GdkDisplay *gdkdisplay;
749 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
751 gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
753 gdkdisplay = gdk_drawable_get_display (widget->window);
754 xdisplay = GDK_WINDOW_XDISPLAY (widget->window);
756 property = gdk_x11_get_xatom_by_name_for_display (gdkdisplay, "_NET_WM_WINDOW_TYPE");
757 window_type = XInternAtom (xdisplay, "_HILDON_WM_WINDOW_TYPE_APP_MENU", False);
758 XChangeProperty (xdisplay, GDK_WINDOW_XID (widget->window), property,
759 XA_ATOM, 32, PropModeReplace, (guchar *) &window_type, 1);
761 /* Detect any screen changes */
762 screen = gtk_widget_get_screen (widget);
763 g_signal_connect (screen, "size-changed", G_CALLBACK (screen_size_changed), widget);
765 /* Force menu to set the initial layout */
766 screen_size_changed (screen, HILDON_APP_MENU (widget));
770 hildon_app_menu_unrealize (GtkWidget *widget)
772 GdkScreen *screen = gtk_widget_get_screen (widget);
773 /* Disconnect "size-changed" signal handler */
774 g_signal_handlers_disconnect_by_func (screen, G_CALLBACK (screen_size_changed), widget);
776 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unrealize (widget);
780 hildon_app_menu_apply_style (GtkWidget *widget)
783 guint horizontal_spacing, vertical_spacing, filter_vertical_spacing;
784 guint inner_border, external_border;
785 HildonAppMenuPrivate *priv;
787 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
789 gtk_widget_style_get (widget,
790 "horizontal-spacing", &horizontal_spacing,
791 "vertical-spacing", &vertical_spacing,
792 "filter-vertical-spacing", &filter_vertical_spacing,
793 "inner-border", &inner_border,
794 "external-border", &external_border,
798 gtk_table_set_row_spacings (priv->table, vertical_spacing);
799 gtk_table_set_col_spacings (priv->table, horizontal_spacing);
800 gtk_box_set_spacing (priv->vbox, filter_vertical_spacing);
802 /* Set inner border */
803 gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
805 /* Compute width request */
806 screen = gtk_widget_get_screen (widget);
807 if (gdk_screen_get_width (screen) < gdk_screen_get_height (screen)) {
810 priv->width_request = gdk_screen_get_width (screen) - external_border * 2;
811 gtk_window_move (GTK_WINDOW (widget), external_border, 0);
812 gtk_widget_queue_resize (widget);
816 hildon_app_menu_style_set (GtkWidget *widget,
817 GtkStyle *previous_style)
819 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set)
820 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set (widget, previous_style);
822 hildon_app_menu_apply_style (widget);
826 hildon_app_menu_repack_filters (HildonAppMenu *menu)
828 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
831 for (iter = priv->filters; iter != NULL; iter = iter->next) {
832 GtkWidget *filter = GTK_WIDGET (iter->data);
833 GtkWidget *parent = gtk_widget_get_parent (filter);
835 g_object_ref (filter);
836 gtk_container_remove (GTK_CONTAINER (parent), filter);
840 for (iter = priv->filters; iter != NULL; iter = iter->next) {
841 GtkWidget *filter = GTK_WIDGET (iter->data);
842 if (GTK_WIDGET_VISIBLE (filter)) {
843 gtk_box_pack_start (GTK_BOX (priv->filters_hbox), filter, TRUE, TRUE, 0);
844 g_object_unref (filter);
845 /* GtkButton must be realized for accelerators to work */
846 gtk_widget_realize (filter);
852 * When items displayed in the menu change (e.g, a new item is added,
853 * an item is hidden or the list is reordered), the layout must be
854 * updated. To do this we repack all items starting from a given one.
857 hildon_app_menu_repack_items (HildonAppMenu *menu,
860 HildonAppMenuPrivate *priv;
864 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
866 /* Remove buttons from their parent */
867 if (start_from != -1) {
868 for (iter = g_list_nth (priv->buttons, start_from); iter != NULL; iter = iter->next) {
869 GtkWidget *item = GTK_WIDGET (iter->data);
870 GtkWidget *parent = gtk_widget_get_parent (item);
873 gtk_container_remove (GTK_CONTAINER (parent), item);
877 /* If items have been removed, recalculate the size of the menu */
878 gtk_window_resize (GTK_WINDOW (menu), 1, 1);
883 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
884 GtkWidget *item = GTK_WIDGET (iter->data);
885 if (GTK_WIDGET_VISIBLE (item)) {
886 /* Don't add an item to the table if it's already there */
887 if (gtk_widget_get_parent (item) == NULL) {
888 gtk_table_attach_defaults (priv->table, item, col, col + 1, row, row + 1);
889 g_object_unref (item);
890 /* GtkButton must be realized for accelerators to work */
891 gtk_widget_realize (item);
893 if (++col == priv->columns) {
900 /* The number of rows/columns might have changed, so we have to
901 * resize the table */
903 gtk_table_resize (priv->table, MAX (row, 1), priv->columns);
905 gtk_table_resize (priv->table, row + 1, priv->columns);
910 * hildon_app_menu_popup:
911 * @menu: a #HildonAppMenu
912 * @parent_window: a #GtkWindow
914 * Displays a menu on top of a window and makes it available for
920 hildon_app_menu_popup (HildonAppMenu *menu,
921 GtkWindow *parent_window)
923 g_return_if_fail (HILDON_IS_APP_MENU (menu));
924 g_return_if_fail (GTK_IS_WINDOW (parent_window));
926 hildon_app_menu_set_parent_window (menu, parent_window);
927 gtk_widget_show (GTK_WIDGET (menu));
931 * hildon_app_menu_get_items:
932 * @menu: a #HildonAppMenu
934 * Returns a list of all items (regular items, not filters) contained
937 * Returns: a newly-allocated list containing the items in @menu
942 hildon_app_menu_get_items (HildonAppMenu *menu)
944 HildonAppMenuPrivate *priv;
946 g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
948 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
950 return g_list_copy (priv->buttons);
954 * hildon_app_menu_get_filters:
955 * @menu: a #HildonAppMenu
957 * Returns a list of all filters contained in @menu.
959 * Returns: a newly-allocated list containing the filters in @menu
964 hildon_app_menu_get_filters (HildonAppMenu *menu)
966 HildonAppMenuPrivate *priv;
968 g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
970 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
972 return g_list_copy (priv->filters);
976 hildon_app_menu_init (HildonAppMenu *menu)
978 GtkWidget *alignment;
979 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
981 /* Initialize private variables */
982 priv->parent_window = NULL;
983 priv->transfer_window = NULL;
984 priv->pressed_outside = FALSE;
985 priv->buttons = NULL;
986 priv->filters = NULL;
988 priv->width_request = -1;
989 priv->find_intruder_idle_id = 0;
990 priv->hide_idle_id = 0;
992 /* Create boxes and tables */
993 priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
994 priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 0));
995 priv->table = GTK_TABLE (gtk_table_new (1, priv->columns, TRUE));
997 /* Align the filters to the center */
998 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
999 gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
1001 /* Pack everything */
1002 gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
1003 gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
1004 gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
1006 /* Make menu a modal window */
1007 gtk_window_set_modal (GTK_WINDOW (menu), TRUE);
1009 /* This should be treated like a normal, ref-counted widget */
1010 g_object_force_floating (G_OBJECT (menu));
1011 GTK_WINDOW (menu)->has_user_ref_count = FALSE;
1013 gtk_widget_show_all (GTK_WIDGET (priv->vbox));
1017 hildon_app_menu_finalize (GObject *object)
1019 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
1021 if (priv->find_intruder_idle_id) {
1022 g_source_remove (priv->find_intruder_idle_id);
1023 priv->find_intruder_idle_id = 0;
1026 if (priv->hide_idle_id) {
1027 g_source_remove (priv->hide_idle_id);
1028 priv->hide_idle_id = 0;
1031 if (priv->parent_window) {
1032 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, object);
1033 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, object);
1036 if (priv->transfer_window)
1037 gdk_window_destroy (priv->transfer_window);
1039 g_list_foreach (priv->buttons, (GFunc) g_object_unref, NULL);
1040 g_list_foreach (priv->filters, (GFunc) g_object_unref, NULL);
1042 g_list_free (priv->buttons);
1043 g_list_free (priv->filters);
1045 g_signal_handlers_destroy (object);
1046 G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
1050 hildon_app_menu_class_init (HildonAppMenuClass *klass)
1052 GObjectClass *gobject_class = (GObjectClass *)klass;
1053 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
1055 gobject_class->finalize = hildon_app_menu_finalize;
1056 widget_class->show = hildon_app_menu_show;
1057 widget_class->show_all = hildon_app_menu_show_all;
1058 widget_class->hide_all = hildon_app_menu_hide_all;
1059 widget_class->map = hildon_app_menu_map;
1060 widget_class->unmap = hildon_app_menu_unmap;
1061 widget_class->realize = hildon_app_menu_realize;
1062 widget_class->unrealize = hildon_app_menu_unrealize;
1063 widget_class->grab_notify = hildon_app_menu_grab_notify;
1064 widget_class->key_press_event = hildon_app_menu_key_press;
1065 widget_class->button_press_event = hildon_app_menu_button_press;
1066 widget_class->button_release_event = hildon_app_menu_button_release;
1067 widget_class->style_set = hildon_app_menu_style_set;
1068 widget_class->delete_event = hildon_app_menu_delete_event_handler;
1069 widget_class->size_request = hildon_app_menu_size_request;
1071 g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
1073 gtk_widget_class_install_style_property (
1076 "horizontal-spacing",
1077 "Horizontal spacing on menu items",
1078 "Horizontal spacing between each menu item. Does not apply to filter buttons.",
1082 gtk_widget_class_install_style_property (
1086 "Vertical spacing on menu items",
1087 "Vertical spacing between each menu item. Does not apply to filter buttons.",
1091 gtk_widget_class_install_style_property (
1094 "filter-vertical-spacing",
1095 "Vertical spacing between filters and menu items",
1096 "Vertical spacing between filters and menu items",
1100 gtk_widget_class_install_style_property (
1104 "Border between menu edges and buttons",
1105 "Border between menu edges and buttons",
1109 gtk_widget_class_install_style_property (
1113 "Border between menu and screen edges (in horizontal mode)",
1114 "Border between the right and left edges of the menu and "
1115 "the screen edges (in horizontal mode)",