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: Application menu for Hildon applications.
23 * #HildonAppMenu is an application menu for applications in the Hildon
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"
107 #include "hildon-animation-actor.h"
110 hildon_app_menu_repack_items (HildonAppMenu *menu,
114 hildon_app_menu_repack_filters (HildonAppMenu *menu);
117 can_activate_accel (GtkWidget *widget,
122 item_visibility_changed (GtkWidget *item,
124 HildonAppMenu *menu);
127 filter_visibility_changed (GtkWidget *item,
129 HildonAppMenu *menu);
132 remove_item_from_list (GList **list,
136 hildon_app_menu_apply_style (GtkWidget *widget);
138 G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
141 * hildon_app_menu_new:
143 * Creates a new #HildonAppMenu.
145 * Return value: A #HildonAppMenu.
150 hildon_app_menu_new (void)
152 GtkWidget *menu = g_object_new (HILDON_TYPE_APP_MENU, NULL);
157 * hildon_app_menu_insert:
158 * @menu : A #HildonAppMenu
159 * @item : A #GtkButton to add to the #HildonAppMenu
160 * @position : The position in the item list where @item is added (from 0 to n-1).
162 * Adds @item to @menu at the position indicated by @position.
167 hildon_app_menu_insert (HildonAppMenu *menu,
171 HildonAppMenuPrivate *priv;
173 g_return_if_fail (HILDON_IS_APP_MENU (menu));
174 g_return_if_fail (GTK_IS_BUTTON (item));
176 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
178 /* Force widget size */
179 hildon_gtk_widget_set_theme_size (GTK_WIDGET (item),
180 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
182 /* Add the item to the menu */
183 g_object_ref_sink (item);
184 priv->buttons = g_list_insert (priv->buttons, item, position);
185 if (GTK_WIDGET_VISIBLE (item))
186 hildon_app_menu_repack_items (menu, position);
188 /* Enable accelerators */
189 g_signal_connect (item, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
191 /* Close the menu when the button is clicked */
192 g_signal_connect_swapped (item, "clicked", G_CALLBACK (gtk_widget_hide), menu);
193 g_signal_connect (item, "notify::visible", G_CALLBACK (item_visibility_changed), menu);
195 /* Remove item from list when it is destroyed */
196 g_object_weak_ref (G_OBJECT (item), (GWeakNotify) remove_item_from_list, &(priv->buttons));
200 * hildon_app_menu_append:
201 * @menu : A #HildonAppMenu
202 * @item : A #GtkButton to add to the #HildonAppMenu
204 * Adds @item to the end of the menu's item list.
209 hildon_app_menu_append (HildonAppMenu *menu,
212 hildon_app_menu_insert (menu, item, -1);
216 * hildon_app_menu_prepend:
217 * @menu : A #HildonAppMenu
218 * @item : A #GtkButton to add to the #HildonAppMenu
220 * Adds @item to the beginning of the menu's item list.
225 hildon_app_menu_prepend (HildonAppMenu *menu,
228 hildon_app_menu_insert (menu, item, 0);
232 * hildon_app_menu_reorder_child:
233 * @menu : A #HildonAppMenu
234 * @item : A #GtkButton to move
235 * @position : The new position to place @item (from 0 to n-1).
237 * Moves a #GtkButton to a new position within #HildonAppMenu.
242 hildon_app_menu_reorder_child (HildonAppMenu *menu,
246 HildonAppMenuPrivate *priv;
249 g_return_if_fail (HILDON_IS_APP_MENU (menu));
250 g_return_if_fail (GTK_IS_BUTTON (item));
251 g_return_if_fail (position >= 0);
253 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
254 old_position = g_list_index (priv->buttons, item);
256 g_return_if_fail (old_position >= 0);
259 priv->buttons = g_list_remove (priv->buttons, item);
260 priv->buttons = g_list_insert (priv->buttons, item, position);
262 hildon_app_menu_repack_items (menu, MIN (old_position, position));
266 * hildon_app_menu_add_filter:
267 * @menu : A #HildonAppMenu
268 * @filter : A #GtkButton to add to the #HildonAppMenu.
270 * Adds the @filter to @menu.
275 hildon_app_menu_add_filter (HildonAppMenu *menu,
278 HildonAppMenuPrivate *priv;
280 g_return_if_fail (HILDON_IS_APP_MENU (menu));
281 g_return_if_fail (GTK_IS_BUTTON (filter));
283 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
285 /* Force widget size */
286 hildon_gtk_widget_set_theme_size (GTK_WIDGET (filter),
287 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
289 /* Add the filter to the menu */
290 g_object_ref_sink (filter);
291 priv->filters = g_list_append (priv->filters, filter);
292 if (GTK_WIDGET_VISIBLE (filter))
293 hildon_app_menu_repack_filters (menu);
295 /* Enable accelerators */
296 g_signal_connect (filter, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
298 /* Close the menu when the button is clicked */
299 g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
300 g_signal_connect (filter, "notify::visible", G_CALLBACK (filter_visibility_changed), menu);
302 /* Remove filter from list when it is destroyed */
303 g_object_weak_ref (G_OBJECT (filter), (GWeakNotify) remove_item_from_list, &(priv->filters));
307 hildon_app_menu_set_columns (HildonAppMenu *menu,
310 HildonAppMenuPrivate *priv;
312 g_return_if_fail (HILDON_IS_APP_MENU (menu));
313 g_return_if_fail (columns > 0);
315 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
317 if (columns != priv->columns) {
318 priv->columns = columns;
319 hildon_app_menu_repack_items (menu, 0);
324 parent_window_topmost_notify (HildonWindow *parent_win,
328 if (!hildon_window_get_is_topmost (parent_win))
329 gtk_widget_hide (menu);
333 parent_window_unmapped (HildonWindow *parent_win,
336 gtk_widget_hide (menu);
340 hildon_app_menu_set_parent_window (HildonAppMenu *self,
341 GtkWindow *parent_window)
343 HildonAppMenuPrivate *priv;
345 g_return_if_fail (HILDON_IS_APP_MENU (self));
346 g_return_if_fail (parent_window == NULL || GTK_IS_WINDOW (parent_window));
348 priv = HILDON_APP_MENU_GET_PRIVATE(self);
350 /* Disconnect old handlers, if any */
351 if (priv->parent_window) {
352 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, self);
353 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, self);
356 /* Connect a new handler */
358 g_signal_connect (parent_window, "notify::is-topmost", G_CALLBACK (parent_window_topmost_notify), self);
359 g_signal_connect (parent_window, "unmap", G_CALLBACK (parent_window_unmapped), self);
362 priv->parent_window = parent_window;
364 if (parent_window == NULL && GTK_WIDGET_VISIBLE (self))
365 gtk_widget_hide (GTK_WIDGET (self));
368 gpointer G_GNUC_INTERNAL
369 hildon_app_menu_get_parent_window (HildonAppMenu *self)
371 HildonAppMenuPrivate *priv;
373 g_return_val_if_fail (HILDON_IS_APP_MENU (self), NULL);
375 priv = HILDON_APP_MENU_GET_PRIVATE (self);
377 return priv->parent_window;
381 screen_size_changed (GdkScreen *screen,
384 hildon_app_menu_apply_style (GTK_WIDGET (menu));
386 if (gdk_screen_get_width (screen) > gdk_screen_get_height (screen)) {
387 hildon_app_menu_set_columns (menu, 2);
389 hildon_app_menu_set_columns (menu, 1);
394 can_activate_accel (GtkWidget *widget,
398 return GTK_WIDGET_VISIBLE (widget);
402 item_visibility_changed (GtkWidget *item,
406 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
408 if (! priv->inhibit_repack)
409 hildon_app_menu_repack_items (menu, g_list_index (priv->buttons, item));
413 filter_visibility_changed (GtkWidget *item,
417 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
419 if (! priv->inhibit_repack)
420 hildon_app_menu_repack_filters (menu);
424 remove_item_from_list (GList **list,
427 *list = g_list_remove (*list, item);
431 hildon_app_menu_show_all (GtkWidget *widget)
433 HildonAppMenu *menu = HILDON_APP_MENU (widget);
434 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
436 priv->inhibit_repack = TRUE;
438 /* Show children, but not self. */
439 g_list_foreach (priv->buttons, (GFunc) gtk_widget_show_all, NULL);
440 g_list_foreach (priv->filters, (GFunc) gtk_widget_show_all, NULL);
442 priv->inhibit_repack = FALSE;
444 hildon_app_menu_repack_items (menu, 0);
445 hildon_app_menu_repack_filters (menu);
450 hildon_app_menu_hide_all (GtkWidget *widget)
452 HildonAppMenu *menu = HILDON_APP_MENU (widget);
453 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
455 priv->inhibit_repack = TRUE;
457 /* Hide children, but not self. */
458 g_list_foreach (priv->buttons, (GFunc) gtk_widget_hide_all, NULL);
459 g_list_foreach (priv->filters, (GFunc) gtk_widget_hide_all, NULL);
461 priv->inhibit_repack = FALSE;
463 hildon_app_menu_repack_items (menu, 0);
464 hildon_app_menu_repack_filters (menu);
468 * There's a race condition that can freeze the UI if a dialog appears
469 * between a HildonAppMenu and its parent window, see NB#100468
472 hildon_app_menu_find_intruder (gpointer data)
474 GtkWidget *widget = GTK_WIDGET (data);
475 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
477 priv->find_intruder_idle_id = 0;
479 /* If there's a window between the menu and its parent window, hide the menu */
480 if (priv->parent_window) {
481 gboolean intruder_found = FALSE;
482 GdkScreen *screen = gtk_widget_get_screen (widget);
483 GList *stack = gdk_screen_get_window_stack (screen);
484 GList *parent_pos = g_list_find (stack, GTK_WIDGET (priv->parent_window)->window);
485 GList *toplevels = gtk_window_list_toplevels ();
488 for (i = toplevels; i != NULL && !intruder_found; i = i->next) {
489 if (i->data != widget && i->data != priv->parent_window) {
490 if (g_list_find (parent_pos, GTK_WIDGET (i->data)->window)) {
491 /* HildonBanners are not closed automatically when
492 * a new window appears, so we must close them by
493 * hand to make the AppMenu work as expected.
494 * Yes, this is a hack. See NB#111027 */
495 if (HILDON_IS_BANNER (i->data)) {
496 gtk_widget_hide (i->data);
497 } else if (!HILDON_IS_ANIMATION_ACTOR (i->data)) {
498 intruder_found = TRUE;
504 g_list_foreach (stack, (GFunc) g_object_unref, NULL);
506 g_list_free (toplevels);
509 gtk_widget_hide (widget);
516 hildon_app_menu_map (GtkWidget *widget)
518 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
520 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
522 if (priv->find_intruder_idle_id == 0)
523 priv->find_intruder_idle_id = gdk_threads_add_idle (hildon_app_menu_find_intruder, widget);
527 hildon_app_menu_hide_idle (gpointer widget)
529 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
530 gtk_widget_hide (GTK_WIDGET (widget));
531 priv->hide_idle_id = 0;
535 /* Send keyboard accelerators to the parent window, if necessary.
536 * This code is heavily based on gtk_menu_key_press ()
539 hildon_app_menu_key_press (GtkWidget *widget,
542 GtkWindow *parent_window;
543 HildonAppMenuPrivate *priv;
545 g_return_val_if_fail (HILDON_IS_APP_MENU (widget), FALSE);
546 g_return_val_if_fail (event != NULL, FALSE);
548 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->key_press_event (widget, event))
551 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
552 parent_window = priv->parent_window;
555 guint accel_key, accel_mods;
556 GdkModifierType consumed_modifiers;
558 GSList *accel_groups;
561 display = gtk_widget_get_display (widget);
563 /* Figure out what modifiers went into determining the key symbol */
564 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
565 event->hardware_keycode, event->state, event->group,
566 NULL, NULL, NULL, &consumed_modifiers);
568 accel_key = gdk_keyval_to_lower (event->keyval);
569 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
571 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
572 * We re-upper case when we match against the keyval, but display and save in caseless form.
574 if (accel_key != event->keyval)
575 accel_mods |= GDK_SHIFT_MASK;
577 accel_groups = gtk_accel_groups_from_object (G_OBJECT (parent_window));
579 for (list = accel_groups; list; list = list->next) {
580 GtkAccelGroup *accel_group = list->data;
582 if (gtk_accel_group_query (accel_group, accel_key, accel_mods, NULL)) {
583 gtk_window_activate_key (parent_window, event);
584 priv->hide_idle_id = gdk_threads_add_idle (hildon_app_menu_hide_idle, widget);
594 hildon_app_menu_delete_event_handler (GtkWidget *widget,
597 /* Hide the menu if it receives a delete-event, but don't destroy it */
598 gtk_widget_hide (widget);
603 hildon_app_menu_size_request (GtkWidget *widget,
604 GtkRequisition *requisition)
606 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
608 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->size_request (widget, requisition);
610 requisition->width = priv->width_request;
614 hildon_app_menu_realize (GtkWidget *widget)
616 Atom property, window_type;
618 GdkDisplay *gdkdisplay;
621 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
623 gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
625 gdkdisplay = gdk_drawable_get_display (widget->window);
626 xdisplay = GDK_WINDOW_XDISPLAY (widget->window);
628 property = gdk_x11_get_xatom_by_name_for_display (gdkdisplay, "_NET_WM_WINDOW_TYPE");
629 window_type = XInternAtom (xdisplay, "_HILDON_WM_WINDOW_TYPE_APP_MENU", False);
630 XChangeProperty (xdisplay, GDK_WINDOW_XID (widget->window), property,
631 XA_ATOM, 32, PropModeReplace, (guchar *) &window_type, 1);
633 /* Detect any screen changes */
634 screen = gtk_widget_get_screen (widget);
635 g_signal_connect (screen, "size-changed", G_CALLBACK (screen_size_changed), widget);
637 /* Force menu to set the initial layout */
638 screen_size_changed (screen, HILDON_APP_MENU (widget));
642 hildon_app_menu_unrealize (GtkWidget *widget)
644 GdkScreen *screen = gtk_widget_get_screen (widget);
645 /* Disconnect "size-changed" signal handler */
646 g_signal_handlers_disconnect_by_func (screen, G_CALLBACK (screen_size_changed), widget);
648 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unrealize (widget);
652 hildon_app_menu_apply_style (GtkWidget *widget)
655 gint filter_group_width;
656 guint horizontal_spacing, vertical_spacing, filter_vertical_spacing;
657 guint inner_border, external_border;
658 HildonAppMenuPrivate *priv;
660 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
662 gtk_widget_style_get (widget,
663 "horizontal-spacing", &horizontal_spacing,
664 "vertical-spacing", &vertical_spacing,
665 "filter-group-width", &filter_group_width,
666 "filter-vertical-spacing", &filter_vertical_spacing,
667 "inner-border", &inner_border,
668 "external-border", &external_border,
672 gtk_table_set_row_spacings (priv->table, vertical_spacing);
673 gtk_table_set_col_spacings (priv->table, horizontal_spacing);
674 gtk_box_set_spacing (priv->vbox, filter_vertical_spacing);
676 /* Set inner border */
677 gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
679 /* Set width of the group of filter buttons */
680 gtk_widget_set_size_request (GTK_WIDGET (priv->filters_hbox), filter_group_width, -1);
682 /* Compute width request */
683 screen = gtk_widget_get_screen (widget);
684 if (gdk_screen_get_width (screen) < gdk_screen_get_height (screen)) {
687 priv->width_request = gdk_screen_get_width (screen) - external_border * 2;
690 gdk_window_move_resize (widget->window,
691 external_border, 0, 1, 1);
693 gtk_widget_queue_resize (widget);
697 hildon_app_menu_style_set (GtkWidget *widget,
698 GtkStyle *previous_style)
700 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set)
701 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set (widget, previous_style);
703 hildon_app_menu_apply_style (widget);
707 hildon_app_menu_repack_filters (HildonAppMenu *menu)
709 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
712 for (iter = priv->filters; iter != NULL; iter = iter->next) {
713 GtkWidget *filter = GTK_WIDGET (iter->data);
714 GtkWidget *parent = gtk_widget_get_parent (filter);
716 g_object_ref (filter);
717 gtk_container_remove (GTK_CONTAINER (parent), filter);
721 for (iter = priv->filters; iter != NULL; iter = iter->next) {
722 GtkWidget *filter = GTK_WIDGET (iter->data);
723 if (GTK_WIDGET_VISIBLE (filter)) {
724 gtk_box_pack_start (GTK_BOX (priv->filters_hbox), filter, TRUE, TRUE, 0);
725 g_object_unref (filter);
726 /* GtkButton must be realized for accelerators to work */
727 gtk_widget_realize (filter);
733 * When items displayed in the menu change (e.g, a new item is added,
734 * an item is hidden or the list is reordered), the layout must be
735 * updated. To do this we repack all items starting from a given one.
738 hildon_app_menu_repack_items (HildonAppMenu *menu,
741 HildonAppMenuPrivate *priv;
742 gint row, col, nvisible, i;
745 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
748 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
749 /* Count number of visible items */
750 if (GTK_WIDGET_VISIBLE (iter->data))
752 /* Remove buttons from their parent */
753 if (start_from != -1 && i >= start_from) {
754 GtkWidget *item = GTK_WIDGET (iter->data);
755 GtkWidget *parent = gtk_widget_get_parent (item);
758 gtk_container_remove (GTK_CONTAINER (parent), item);
764 /* If items have been removed, recalculate the size of the menu */
765 if (start_from != -1)
766 gtk_window_resize (GTK_WINDOW (menu), 1, 1);
768 /* Set the final size now to avoid unnecessary resizes later */
770 gtk_table_resize (priv->table, ((nvisible - 1) / priv->columns) + 1, priv->columns);
774 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
775 GtkWidget *item = GTK_WIDGET (iter->data);
776 if (GTK_WIDGET_VISIBLE (item)) {
777 /* Don't add an item to the table if it's already there */
778 if (gtk_widget_get_parent (item) == NULL) {
779 gtk_table_attach_defaults (priv->table, item, col, col + 1, row, row + 1);
780 g_object_unref (item);
781 /* GtkButton must be realized for accelerators to work */
782 gtk_widget_realize (item);
784 if (++col == priv->columns) {
793 * hildon_app_menu_popup:
794 * @menu: a #HildonAppMenu
795 * @parent_window: a #GtkWindow
797 * Displays a menu on top of a window and makes it available for
803 hildon_app_menu_popup (HildonAppMenu *menu,
804 GtkWindow *parent_window)
806 HildonAppMenuPrivate *priv;
807 gboolean show_menu = FALSE;
810 g_return_if_fail (HILDON_IS_APP_MENU (menu));
811 g_return_if_fail (GTK_IS_WINDOW (parent_window));
813 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
815 /* Don't show menu if it doesn't contain visible items */
816 for (i = priv->buttons; i && !show_menu; i = i->next)
817 show_menu = GTK_WIDGET_VISIBLE (i->data);
819 for (i = priv->filters; i && !show_menu; i = i->next)
820 show_menu = GTK_WIDGET_VISIBLE (i->data);
823 hildon_app_menu_set_parent_window (menu, parent_window);
824 gtk_widget_show (GTK_WIDGET (menu));
830 * hildon_app_menu_get_items:
831 * @menu: a #HildonAppMenu
833 * Returns a list of all items (regular items, not filters) contained
836 * Returns: a newly-allocated list containing the items in @menu
841 hildon_app_menu_get_items (HildonAppMenu *menu)
843 HildonAppMenuPrivate *priv;
845 g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
847 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
849 return g_list_copy (priv->buttons);
853 * hildon_app_menu_get_filters:
854 * @menu: a #HildonAppMenu
856 * Returns a list of all filters contained in @menu.
858 * Returns: a newly-allocated list containing the filters in @menu
863 hildon_app_menu_get_filters (HildonAppMenu *menu)
865 HildonAppMenuPrivate *priv;
867 g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
869 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
871 return g_list_copy (priv->filters);
875 hildon_app_menu_init (HildonAppMenu *menu)
877 GtkWidget *alignment;
878 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
880 /* Initialize private variables */
881 priv->parent_window = NULL;
882 priv->transfer_window = NULL;
883 priv->pressed_outside = FALSE;
884 priv->inhibit_repack = FALSE;
885 priv->last_pressed_button = NULL;
886 priv->buttons = NULL;
887 priv->filters = NULL;
889 priv->width_request = -1;
890 priv->find_intruder_idle_id = 0;
891 priv->hide_idle_id = 0;
893 /* Create boxes and tables */
894 priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
895 priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 0));
896 priv->table = GTK_TABLE (gtk_table_new (1, priv->columns, TRUE));
898 /* Align the filters to the center */
899 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
900 gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
902 /* Pack everything */
903 gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
904 gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
905 gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
907 /* This should be treated like a normal, ref-counted widget */
908 g_object_force_floating (G_OBJECT (menu));
909 GTK_WINDOW (menu)->has_user_ref_count = FALSE;
911 gtk_widget_show_all (GTK_WIDGET (priv->vbox));
915 hildon_app_menu_finalize (GObject *object)
917 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
919 if (priv->find_intruder_idle_id) {
920 g_source_remove (priv->find_intruder_idle_id);
921 priv->find_intruder_idle_id = 0;
924 if (priv->hide_idle_id) {
925 g_source_remove (priv->hide_idle_id);
926 priv->hide_idle_id = 0;
929 if (priv->parent_window) {
930 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, object);
931 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, object);
934 if (priv->transfer_window)
935 gdk_window_destroy (priv->transfer_window);
937 g_list_foreach (priv->buttons, (GFunc) g_object_unref, NULL);
938 g_list_foreach (priv->filters, (GFunc) g_object_unref, NULL);
940 g_list_free (priv->buttons);
941 g_list_free (priv->filters);
943 g_signal_handlers_destroy (object);
944 G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
948 hildon_app_menu_class_init (HildonAppMenuClass *klass)
950 GObjectClass *gobject_class = (GObjectClass *)klass;
951 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
953 gobject_class->finalize = hildon_app_menu_finalize;
954 widget_class->show_all = hildon_app_menu_show_all;
955 widget_class->hide_all = hildon_app_menu_hide_all;
956 widget_class->map = hildon_app_menu_map;
957 widget_class->realize = hildon_app_menu_realize;
958 widget_class->unrealize = hildon_app_menu_unrealize;
959 widget_class->key_press_event = hildon_app_menu_key_press;
960 widget_class->style_set = hildon_app_menu_style_set;
961 widget_class->delete_event = hildon_app_menu_delete_event_handler;
962 widget_class->size_request = hildon_app_menu_size_request;
964 g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
966 gtk_widget_class_install_style_property (
969 "horizontal-spacing",
970 "Horizontal spacing on menu items",
971 "Horizontal spacing between each menu item. Does not apply to filter buttons.",
975 gtk_widget_class_install_style_property (
979 "Vertical spacing on menu items",
980 "Vertical spacing between each menu item. Does not apply to filter buttons.",
984 gtk_widget_class_install_style_property (
987 "filter-group-width",
988 "Width of the group of filter buttons",
989 "Total width of the group of filter buttons, "
990 "or -1 to use the natural size request.",
994 gtk_widget_class_install_style_property (
997 "filter-vertical-spacing",
998 "Vertical spacing between filters and menu items",
999 "Vertical spacing between filters and menu items",
1003 gtk_widget_class_install_style_property (
1007 "Border between menu edges and buttons",
1008 "Border between menu edges and buttons",
1012 gtk_widget_class_install_style_property (
1016 "Border between menu and screen edges (in horizontal mode)",
1017 "Border between the right and left edges of the menu and "
1018 "the screen edges (in horizontal mode)",