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 menu_item_button_event (GtkButton *item,
133 GdkEventButton *event,
137 remove_item_from_list (GList **list,
141 hildon_app_menu_apply_style (GtkWidget *widget);
143 G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
146 * hildon_app_menu_new:
148 * Creates a new #HildonAppMenu.
150 * Return value: A #HildonAppMenu.
155 hildon_app_menu_new (void)
157 GtkWidget *menu = g_object_new (HILDON_TYPE_APP_MENU, NULL);
162 * hildon_app_menu_insert:
163 * @menu : A #HildonAppMenu
164 * @item : A #GtkButton to add to the #HildonAppMenu
165 * @position : The position in the item list where @item is added (from 0 to n-1).
167 * Adds @item to @menu at the position indicated by @position.
172 hildon_app_menu_insert (HildonAppMenu *menu,
176 HildonAppMenuPrivate *priv;
178 g_return_if_fail (HILDON_IS_APP_MENU (menu));
179 g_return_if_fail (GTK_IS_BUTTON (item));
181 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
183 /* Force widget size */
184 hildon_gtk_widget_set_theme_size (GTK_WIDGET (item),
185 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
187 /* Add the item to the menu */
188 g_object_ref_sink (item);
189 priv->buttons = g_list_insert (priv->buttons, item, position);
190 if (GTK_WIDGET_VISIBLE (item))
191 hildon_app_menu_repack_items (menu, position);
193 /* Enable accelerators */
194 g_signal_connect (item, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
196 /* Close the menu when the button is clicked */
197 g_signal_connect_swapped (item, "clicked", G_CALLBACK (gtk_widget_hide), menu);
198 g_signal_connect (item, "notify::visible", G_CALLBACK (item_visibility_changed), menu);
200 /* Remove item from list when it is destroyed */
201 g_object_weak_ref (G_OBJECT (item), (GWeakNotify) remove_item_from_list, &(priv->buttons));
205 * hildon_app_menu_append:
206 * @menu : A #HildonAppMenu
207 * @item : A #GtkButton to add to the #HildonAppMenu
209 * Adds @item to the end of the menu's item list.
214 hildon_app_menu_append (HildonAppMenu *menu,
217 hildon_app_menu_insert (menu, item, -1);
221 * hildon_app_menu_prepend:
222 * @menu : A #HildonAppMenu
223 * @item : A #GtkButton to add to the #HildonAppMenu
225 * Adds @item to the beginning of the menu's item list.
230 hildon_app_menu_prepend (HildonAppMenu *menu,
233 hildon_app_menu_insert (menu, item, 0);
237 * hildon_app_menu_reorder_child:
238 * @menu : A #HildonAppMenu
239 * @item : A #GtkButton to move
240 * @position : The new position to place @item (from 0 to n-1).
242 * Moves a #GtkButton to a new position within #HildonAppMenu.
247 hildon_app_menu_reorder_child (HildonAppMenu *menu,
251 HildonAppMenuPrivate *priv;
254 g_return_if_fail (HILDON_IS_APP_MENU (menu));
255 g_return_if_fail (GTK_IS_BUTTON (item));
256 g_return_if_fail (position >= 0);
258 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
259 old_position = g_list_index (priv->buttons, item);
261 g_return_if_fail (old_position >= 0);
264 priv->buttons = g_list_remove (priv->buttons, item);
265 priv->buttons = g_list_insert (priv->buttons, item, position);
267 hildon_app_menu_repack_items (menu, MIN (old_position, position));
271 * hildon_app_menu_add_filter:
272 * @menu : A #HildonAppMenu
273 * @filter : A #GtkButton to add to the #HildonAppMenu.
275 * Adds the @filter to @menu.
280 hildon_app_menu_add_filter (HildonAppMenu *menu,
283 HildonAppMenuPrivate *priv;
285 g_return_if_fail (HILDON_IS_APP_MENU (menu));
286 g_return_if_fail (GTK_IS_BUTTON (filter));
288 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
290 /* Force widget size */
291 hildon_gtk_widget_set_theme_size (GTK_WIDGET (filter),
292 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
294 /* Add the filter to the menu */
295 g_object_ref_sink (filter);
296 priv->filters = g_list_append (priv->filters, filter);
297 if (GTK_WIDGET_VISIBLE (filter))
298 hildon_app_menu_repack_filters (menu);
300 /* Enable accelerators */
301 g_signal_connect (filter, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
303 /* Close the menu when the button is clicked */
304 g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
305 g_signal_connect (filter, "notify::visible", G_CALLBACK (filter_visibility_changed), menu);
307 /* Remove filter from list when it is destroyed */
308 g_object_weak_ref (G_OBJECT (filter), (GWeakNotify) remove_item_from_list, &(priv->filters));
312 hildon_app_menu_set_columns (HildonAppMenu *menu,
315 HildonAppMenuPrivate *priv;
317 g_return_if_fail (HILDON_IS_APP_MENU (menu));
318 g_return_if_fail (columns > 0);
320 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
322 if (columns != priv->columns) {
323 priv->columns = columns;
324 hildon_app_menu_repack_items (menu, 0);
329 parent_window_topmost_notify (HildonWindow *parent_win,
333 if (!hildon_window_get_is_topmost (parent_win))
334 gtk_widget_hide (menu);
338 parent_window_unmapped (HildonWindow *parent_win,
341 gtk_widget_hide (menu);
345 hildon_app_menu_set_parent_window (HildonAppMenu *self,
346 GtkWindow *parent_window)
348 HildonAppMenuPrivate *priv;
350 g_return_if_fail (HILDON_IS_APP_MENU (self));
351 g_return_if_fail (parent_window == NULL || GTK_IS_WINDOW (parent_window));
353 priv = HILDON_APP_MENU_GET_PRIVATE(self);
355 /* Disconnect old handlers, if any */
356 if (priv->parent_window) {
357 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, self);
358 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, self);
361 /* Connect a new handler */
363 g_signal_connect (parent_window, "notify::is-topmost", G_CALLBACK (parent_window_topmost_notify), self);
364 g_signal_connect (parent_window, "unmap", G_CALLBACK (parent_window_unmapped), self);
367 priv->parent_window = parent_window;
369 if (parent_window == NULL && GTK_WIDGET_VISIBLE (self))
370 gtk_widget_hide (GTK_WIDGET (self));
373 gpointer G_GNUC_INTERNAL
374 hildon_app_menu_get_parent_window (HildonAppMenu *self)
376 HildonAppMenuPrivate *priv;
378 g_return_val_if_fail (HILDON_IS_APP_MENU (self), NULL);
380 priv = HILDON_APP_MENU_GET_PRIVATE (self);
382 return priv->parent_window;
386 screen_size_changed (GdkScreen *screen,
389 hildon_app_menu_apply_style (GTK_WIDGET (menu));
391 if (gdk_screen_get_width (screen) > gdk_screen_get_height (screen)) {
392 hildon_app_menu_set_columns (menu, 2);
394 hildon_app_menu_set_columns (menu, 1);
399 can_activate_accel (GtkWidget *widget,
403 return GTK_WIDGET_VISIBLE (widget);
407 item_visibility_changed (GtkWidget *item,
411 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
413 if (! priv->inhibit_repack)
414 hildon_app_menu_repack_items (menu, g_list_index (priv->buttons, item));
418 filter_visibility_changed (GtkWidget *item,
422 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
424 if (! priv->inhibit_repack)
425 hildon_app_menu_repack_filters (menu);
429 remove_item_from_list (GList **list,
432 *list = g_list_remove (*list, item);
436 hildon_app_menu_show_all (GtkWidget *widget)
438 HildonAppMenu *menu = HILDON_APP_MENU (widget);
439 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
441 priv->inhibit_repack = TRUE;
443 /* Show children, but not self. */
444 g_list_foreach (priv->buttons, (GFunc) gtk_widget_show_all, NULL);
445 g_list_foreach (priv->filters, (GFunc) gtk_widget_show_all, NULL);
447 priv->inhibit_repack = FALSE;
449 hildon_app_menu_repack_items (menu, 0);
450 hildon_app_menu_repack_filters (menu);
455 hildon_app_menu_hide_all (GtkWidget *widget)
457 HildonAppMenu *menu = HILDON_APP_MENU (widget);
458 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
460 priv->inhibit_repack = TRUE;
462 /* Hide children, but not self. */
463 g_list_foreach (priv->buttons, (GFunc) gtk_widget_hide_all, NULL);
464 g_list_foreach (priv->filters, (GFunc) gtk_widget_hide_all, NULL);
466 priv->inhibit_repack = FALSE;
468 hildon_app_menu_repack_items (menu, 0);
469 hildon_app_menu_repack_filters (menu);
473 * There's a race condition that can freeze the UI if a dialog appears
474 * between a HildonAppMenu and its parent window, see NB#100468
477 hildon_app_menu_find_intruder (gpointer data)
479 GtkWidget *widget = GTK_WIDGET (data);
480 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
482 priv->find_intruder_idle_id = 0;
484 /* If there's a window between the menu and its parent window, hide the menu */
485 if (priv->parent_window) {
486 gboolean intruder_found = FALSE;
487 GdkScreen *screen = gtk_widget_get_screen (widget);
488 GList *stack = gdk_screen_get_window_stack (screen);
489 GList *parent_pos = g_list_find (stack, GTK_WIDGET (priv->parent_window)->window);
490 GList *toplevels = gtk_window_list_toplevels ();
493 for (i = toplevels; i != NULL && !intruder_found; i = i->next) {
494 if (i->data != widget && i->data != priv->parent_window) {
495 if (g_list_find (parent_pos, GTK_WIDGET (i->data)->window)) {
496 /* HildonBanners are not closed automatically when
497 * a new window appears, so we must close them by
498 * hand to make the AppMenu work as expected.
499 * Yes, this is a hack. See NB#111027 */
500 if (HILDON_IS_BANNER (i->data)) {
501 gtk_widget_hide (i->data);
502 } else if (!HILDON_IS_ANIMATION_ACTOR (i->data)) {
503 intruder_found = TRUE;
509 g_list_foreach (stack, (GFunc) g_object_unref, NULL);
511 g_list_free (toplevels);
514 gtk_widget_hide (widget);
521 hildon_app_menu_map (GtkWidget *widget)
523 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
525 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
527 if (priv->find_intruder_idle_id == 0)
528 priv->find_intruder_idle_id = gdk_threads_add_idle (hildon_app_menu_find_intruder, widget);
532 hildon_app_menu_hide_idle (gpointer widget)
534 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
535 gtk_widget_hide (GTK_WIDGET (widget));
536 priv->hide_idle_id = 0;
540 /* Send keyboard accelerators to the parent window, if necessary.
541 * This code is heavily based on gtk_menu_key_press ()
544 hildon_app_menu_key_press (GtkWidget *widget,
547 GtkWindow *parent_window;
548 HildonAppMenuPrivate *priv;
550 g_return_val_if_fail (HILDON_IS_APP_MENU (widget), FALSE);
551 g_return_val_if_fail (event != NULL, FALSE);
553 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->key_press_event (widget, event))
556 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
557 parent_window = priv->parent_window;
560 guint accel_key, accel_mods;
561 GdkModifierType consumed_modifiers;
563 GSList *accel_groups;
566 display = gtk_widget_get_display (widget);
568 /* Figure out what modifiers went into determining the key symbol */
569 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
570 event->hardware_keycode, event->state, event->group,
571 NULL, NULL, NULL, &consumed_modifiers);
573 accel_key = gdk_keyval_to_lower (event->keyval);
574 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
576 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
577 * We re-upper case when we match against the keyval, but display and save in caseless form.
579 if (accel_key != event->keyval)
580 accel_mods |= GDK_SHIFT_MASK;
582 accel_groups = gtk_accel_groups_from_object (G_OBJECT (parent_window));
584 for (list = accel_groups; list; list = list->next) {
585 GtkAccelGroup *accel_group = list->data;
587 if (gtk_accel_group_query (accel_group, accel_key, accel_mods, NULL)) {
588 gtk_window_activate_key (parent_window, event);
589 priv->hide_idle_id = gdk_threads_add_idle (hildon_app_menu_hide_idle, widget);
599 hildon_app_menu_delete_event_handler (GtkWidget *widget,
602 /* Hide the menu if it receives a delete-event, but don't destroy it */
603 gtk_widget_hide (widget);
608 hildon_app_menu_size_request (GtkWidget *widget,
609 GtkRequisition *requisition)
611 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
613 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->size_request (widget, requisition);
615 requisition->width = priv->width_request;
619 hildon_app_menu_realize (GtkWidget *widget)
621 Atom property, window_type;
623 GdkDisplay *gdkdisplay;
626 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
628 gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
630 gdkdisplay = gdk_drawable_get_display (widget->window);
631 xdisplay = GDK_WINDOW_XDISPLAY (widget->window);
633 property = gdk_x11_get_xatom_by_name_for_display (gdkdisplay, "_NET_WM_WINDOW_TYPE");
634 window_type = XInternAtom (xdisplay, "_HILDON_WM_WINDOW_TYPE_APP_MENU", False);
635 XChangeProperty (xdisplay, GDK_WINDOW_XID (widget->window), property,
636 XA_ATOM, 32, PropModeReplace, (guchar *) &window_type, 1);
638 /* Detect any screen changes */
639 screen = gtk_widget_get_screen (widget);
640 g_signal_connect (screen, "size-changed", G_CALLBACK (screen_size_changed), widget);
642 /* Force menu to set the initial layout */
643 screen_size_changed (screen, HILDON_APP_MENU (widget));
647 hildon_app_menu_unrealize (GtkWidget *widget)
649 GdkScreen *screen = gtk_widget_get_screen (widget);
650 /* Disconnect "size-changed" signal handler */
651 g_signal_handlers_disconnect_by_func (screen, G_CALLBACK (screen_size_changed), widget);
653 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unrealize (widget);
657 hildon_app_menu_apply_style (GtkWidget *widget)
660 gint filter_group_width;
661 guint horizontal_spacing, vertical_spacing, filter_vertical_spacing;
662 guint inner_border, external_border;
663 HildonAppMenuPrivate *priv;
665 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
667 gtk_widget_style_get (widget,
668 "horizontal-spacing", &horizontal_spacing,
669 "vertical-spacing", &vertical_spacing,
670 "filter-group-width", &filter_group_width,
671 "filter-vertical-spacing", &filter_vertical_spacing,
672 "inner-border", &inner_border,
673 "external-border", &external_border,
677 gtk_table_set_row_spacings (priv->table, vertical_spacing);
678 gtk_table_set_col_spacings (priv->table, horizontal_spacing);
679 gtk_box_set_spacing (priv->vbox, filter_vertical_spacing);
681 /* Set inner border */
682 gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
684 /* Set width of the group of filter buttons */
685 gtk_widget_set_size_request (GTK_WIDGET (priv->filters_hbox), filter_group_width, -1);
687 /* Compute width request */
688 screen = gtk_widget_get_screen (widget);
689 if (gdk_screen_get_width (screen) < gdk_screen_get_height (screen)) {
692 priv->width_request = gdk_screen_get_width (screen) - external_border * 2;
695 gdk_window_move_resize (widget->window,
696 external_border, 0, 1, 1);
698 gtk_widget_queue_resize (widget);
702 hildon_app_menu_style_set (GtkWidget *widget,
703 GtkStyle *previous_style)
705 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set)
706 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set (widget, previous_style);
708 hildon_app_menu_apply_style (widget);
712 hildon_app_menu_repack_filters (HildonAppMenu *menu)
714 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
717 for (iter = priv->filters; iter != NULL; iter = iter->next) {
718 GtkWidget *filter = GTK_WIDGET (iter->data);
719 GtkWidget *parent = gtk_widget_get_parent (filter);
721 g_object_ref (filter);
722 gtk_container_remove (GTK_CONTAINER (parent), filter);
726 for (iter = priv->filters; iter != NULL; iter = iter->next) {
727 GtkWidget *filter = GTK_WIDGET (iter->data);
728 if (GTK_WIDGET_VISIBLE (filter)) {
729 gtk_box_pack_start (GTK_BOX (priv->filters_hbox), filter, TRUE, TRUE, 0);
730 g_object_unref (filter);
731 /* GtkButton must be realized for accelerators to work */
732 gtk_widget_realize (filter);
738 * When items displayed in the menu change (e.g, a new item is added,
739 * an item is hidden or the list is reordered), the layout must be
740 * updated. To do this we repack all items starting from a given one.
743 hildon_app_menu_repack_items (HildonAppMenu *menu,
746 HildonAppMenuPrivate *priv;
747 gint row, col, nvisible, i;
750 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
753 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
754 /* Count number of visible items */
755 if (GTK_WIDGET_VISIBLE (iter->data))
757 /* Remove buttons from their parent */
758 if (start_from != -1 && i >= start_from) {
759 GtkWidget *item = GTK_WIDGET (iter->data);
760 GtkWidget *parent = gtk_widget_get_parent (item);
763 gtk_container_remove (GTK_CONTAINER (parent), item);
769 /* If items have been removed, recalculate the size of the menu */
770 if (start_from != -1)
771 gtk_window_resize (GTK_WINDOW (menu), 1, 1);
773 /* Set the final size now to avoid unnecessary resizes later */
775 gtk_table_resize (priv->table, ((nvisible - 1) / priv->columns) + 1, priv->columns);
779 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
780 GtkWidget *item = GTK_WIDGET (iter->data);
781 if (GTK_WIDGET_VISIBLE (item)) {
782 /* Don't add an item to the table if it's already there */
783 if (gtk_widget_get_parent (item) == NULL) {
784 gtk_table_attach_defaults (priv->table, item, col, col + 1, row, row + 1);
785 g_object_unref (item);
786 /* GtkButton must be realized for accelerators to work */
787 gtk_widget_realize (item);
789 if (++col == priv->columns) {
798 * hildon_app_menu_popup:
799 * @menu: a #HildonAppMenu
800 * @parent_window: a #GtkWindow
802 * Displays a menu on top of a window and makes it available for
808 hildon_app_menu_popup (HildonAppMenu *menu,
809 GtkWindow *parent_window)
811 HildonAppMenuPrivate *priv;
812 gboolean show_menu = FALSE;
815 g_return_if_fail (HILDON_IS_APP_MENU (menu));
816 g_return_if_fail (GTK_IS_WINDOW (parent_window));
818 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
820 /* Don't show menu if it doesn't contain visible items */
821 for (i = priv->buttons; i && !show_menu; i = i->next)
822 show_menu = GTK_WIDGET_VISIBLE (i->data);
824 for (i = priv->filters; i && !show_menu; i = i->next)
825 show_menu = GTK_WIDGET_VISIBLE (i->data);
828 hildon_app_menu_set_parent_window (menu, parent_window);
829 gtk_widget_show (GTK_WIDGET (menu));
835 * hildon_app_menu_get_items:
836 * @menu: a #HildonAppMenu
838 * Returns a list of all items (regular items, not filters) contained
841 * Returns: a newly-allocated list containing the items in @menu
846 hildon_app_menu_get_items (HildonAppMenu *menu)
848 HildonAppMenuPrivate *priv;
850 g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
852 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
854 return g_list_copy (priv->buttons);
858 * hildon_app_menu_get_filters:
859 * @menu: a #HildonAppMenu
861 * Returns a list of all filters contained in @menu.
863 * Returns: a newly-allocated list containing the filters in @menu
868 hildon_app_menu_get_filters (HildonAppMenu *menu)
870 HildonAppMenuPrivate *priv;
872 g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
874 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
876 return g_list_copy (priv->filters);
880 hildon_app_menu_init (HildonAppMenu *menu)
882 GtkWidget *alignment;
883 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
885 /* Initialize private variables */
886 priv->parent_window = NULL;
887 priv->transfer_window = NULL;
888 priv->pressed_outside = FALSE;
889 priv->inhibit_repack = FALSE;
890 priv->last_pressed_button = NULL;
891 priv->buttons = NULL;
892 priv->filters = NULL;
894 priv->width_request = -1;
895 priv->find_intruder_idle_id = 0;
896 priv->hide_idle_id = 0;
898 /* Create boxes and tables */
899 priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
900 priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 0));
901 priv->table = GTK_TABLE (gtk_table_new (1, priv->columns, TRUE));
903 /* Align the filters to the center */
904 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
905 gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
907 /* Pack everything */
908 gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
909 gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
910 gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
912 /* This should be treated like a normal, ref-counted widget */
913 g_object_force_floating (G_OBJECT (menu));
914 GTK_WINDOW (menu)->has_user_ref_count = FALSE;
916 gtk_widget_show_all (GTK_WIDGET (priv->vbox));
920 hildon_app_menu_finalize (GObject *object)
922 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
924 if (priv->find_intruder_idle_id) {
925 g_source_remove (priv->find_intruder_idle_id);
926 priv->find_intruder_idle_id = 0;
929 if (priv->hide_idle_id) {
930 g_source_remove (priv->hide_idle_id);
931 priv->hide_idle_id = 0;
934 if (priv->parent_window) {
935 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, object);
936 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, object);
939 if (priv->transfer_window)
940 gdk_window_destroy (priv->transfer_window);
942 g_list_foreach (priv->buttons, (GFunc) g_object_unref, NULL);
943 g_list_foreach (priv->filters, (GFunc) g_object_unref, NULL);
945 g_list_free (priv->buttons);
946 g_list_free (priv->filters);
948 g_signal_handlers_destroy (object);
949 G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
953 hildon_app_menu_class_init (HildonAppMenuClass *klass)
955 GObjectClass *gobject_class = (GObjectClass *)klass;
956 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
958 gobject_class->finalize = hildon_app_menu_finalize;
959 widget_class->show_all = hildon_app_menu_show_all;
960 widget_class->hide_all = hildon_app_menu_hide_all;
961 widget_class->map = hildon_app_menu_map;
962 widget_class->realize = hildon_app_menu_realize;
963 widget_class->unrealize = hildon_app_menu_unrealize;
964 widget_class->key_press_event = hildon_app_menu_key_press;
965 widget_class->style_set = hildon_app_menu_style_set;
966 widget_class->delete_event = hildon_app_menu_delete_event_handler;
967 widget_class->size_request = hildon_app_menu_size_request;
969 g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
971 gtk_widget_class_install_style_property (
974 "horizontal-spacing",
975 "Horizontal spacing on menu items",
976 "Horizontal spacing between each menu item. Does not apply to filter buttons.",
980 gtk_widget_class_install_style_property (
984 "Vertical spacing on menu items",
985 "Vertical spacing between each menu item. Does not apply to filter buttons.",
989 gtk_widget_class_install_style_property (
992 "filter-group-width",
993 "Width of the group of filter buttons",
994 "Total width of the group of filter buttons, "
995 "or -1 to use the natural size request.",
999 gtk_widget_class_install_style_property (
1002 "filter-vertical-spacing",
1003 "Vertical spacing between filters and menu items",
1004 "Vertical spacing between filters and menu items",
1008 gtk_widget_class_install_style_property (
1012 "Border between menu edges and buttons",
1013 "Border between menu edges and buttons",
1017 gtk_widget_class_install_style_property (
1021 "Border between menu and screen edges (in horizontal mode)",
1022 "Border between the right and left edges of the menu and "
1023 "the screen edges (in horizontal mode)",