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_grab_notify (GtkWidget *widget,
528 gboolean was_grabbed)
530 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->grab_notify)
531 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->grab_notify (widget, was_grabbed);
533 if (!was_grabbed && GTK_WIDGET_VISIBLE (widget))
534 gtk_widget_hide (widget);
538 hildon_app_menu_hide_idle (gpointer widget)
540 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
541 gtk_widget_hide (GTK_WIDGET (widget));
542 priv->hide_idle_id = 0;
546 /* Send keyboard accelerators to the parent window, if necessary.
547 * This code is heavily based on gtk_menu_key_press ()
550 hildon_app_menu_key_press (GtkWidget *widget,
553 GtkWindow *parent_window;
554 HildonAppMenuPrivate *priv;
556 g_return_val_if_fail (HILDON_IS_APP_MENU (widget), FALSE);
557 g_return_val_if_fail (event != NULL, FALSE);
559 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->key_press_event (widget, event))
562 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
563 parent_window = priv->parent_window;
566 guint accel_key, accel_mods;
567 GdkModifierType consumed_modifiers;
569 GSList *accel_groups;
572 display = gtk_widget_get_display (widget);
574 /* Figure out what modifiers went into determining the key symbol */
575 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
576 event->hardware_keycode, event->state, event->group,
577 NULL, NULL, NULL, &consumed_modifiers);
579 accel_key = gdk_keyval_to_lower (event->keyval);
580 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
582 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
583 * We re-upper case when we match against the keyval, but display and save in caseless form.
585 if (accel_key != event->keyval)
586 accel_mods |= GDK_SHIFT_MASK;
588 accel_groups = gtk_accel_groups_from_object (G_OBJECT (parent_window));
590 for (list = accel_groups; list; list = list->next) {
591 GtkAccelGroup *accel_group = list->data;
593 if (gtk_accel_group_query (accel_group, accel_key, accel_mods, NULL)) {
594 gtk_window_activate_key (parent_window, event);
595 priv->hide_idle_id = gdk_threads_add_idle (hildon_app_menu_hide_idle, widget);
605 hildon_app_menu_delete_event_handler (GtkWidget *widget,
608 /* Hide the menu if it receives a delete-event, but don't destroy it */
609 gtk_widget_hide (widget);
614 hildon_app_menu_size_request (GtkWidget *widget,
615 GtkRequisition *requisition)
617 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
619 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->size_request (widget, requisition);
621 requisition->width = priv->width_request;
625 hildon_app_menu_realize (GtkWidget *widget)
627 Atom property, window_type;
629 GdkDisplay *gdkdisplay;
632 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
634 gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
636 gdkdisplay = gdk_drawable_get_display (widget->window);
637 xdisplay = GDK_WINDOW_XDISPLAY (widget->window);
639 property = gdk_x11_get_xatom_by_name_for_display (gdkdisplay, "_NET_WM_WINDOW_TYPE");
640 window_type = XInternAtom (xdisplay, "_HILDON_WM_WINDOW_TYPE_APP_MENU", False);
641 XChangeProperty (xdisplay, GDK_WINDOW_XID (widget->window), property,
642 XA_ATOM, 32, PropModeReplace, (guchar *) &window_type, 1);
644 /* Detect any screen changes */
645 screen = gtk_widget_get_screen (widget);
646 g_signal_connect (screen, "size-changed", G_CALLBACK (screen_size_changed), widget);
648 /* Force menu to set the initial layout */
649 screen_size_changed (screen, HILDON_APP_MENU (widget));
653 hildon_app_menu_unrealize (GtkWidget *widget)
655 GdkScreen *screen = gtk_widget_get_screen (widget);
656 /* Disconnect "size-changed" signal handler */
657 g_signal_handlers_disconnect_by_func (screen, G_CALLBACK (screen_size_changed), widget);
659 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unrealize (widget);
663 hildon_app_menu_apply_style (GtkWidget *widget)
666 gint filter_group_width;
667 guint horizontal_spacing, vertical_spacing, filter_vertical_spacing;
668 guint inner_border, external_border;
669 HildonAppMenuPrivate *priv;
671 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
673 gtk_widget_style_get (widget,
674 "horizontal-spacing", &horizontal_spacing,
675 "vertical-spacing", &vertical_spacing,
676 "filter-group-width", &filter_group_width,
677 "filter-vertical-spacing", &filter_vertical_spacing,
678 "inner-border", &inner_border,
679 "external-border", &external_border,
683 gtk_table_set_row_spacings (priv->table, vertical_spacing);
684 gtk_table_set_col_spacings (priv->table, horizontal_spacing);
685 gtk_box_set_spacing (priv->vbox, filter_vertical_spacing);
687 /* Set inner border */
688 gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
690 /* Set width of the group of filter buttons */
691 gtk_widget_set_size_request (GTK_WIDGET (priv->filters_hbox), filter_group_width, -1);
693 /* Compute width request */
694 screen = gtk_widget_get_screen (widget);
695 if (gdk_screen_get_width (screen) < gdk_screen_get_height (screen)) {
698 priv->width_request = gdk_screen_get_width (screen) - external_border * 2;
701 gdk_window_move_resize (widget->window,
702 external_border, 0, 1, 1);
704 gtk_widget_queue_resize (widget);
708 hildon_app_menu_style_set (GtkWidget *widget,
709 GtkStyle *previous_style)
711 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set)
712 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set (widget, previous_style);
714 hildon_app_menu_apply_style (widget);
718 hildon_app_menu_repack_filters (HildonAppMenu *menu)
720 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
723 for (iter = priv->filters; iter != NULL; iter = iter->next) {
724 GtkWidget *filter = GTK_WIDGET (iter->data);
725 GtkWidget *parent = gtk_widget_get_parent (filter);
727 g_object_ref (filter);
728 gtk_container_remove (GTK_CONTAINER (parent), filter);
732 for (iter = priv->filters; iter != NULL; iter = iter->next) {
733 GtkWidget *filter = GTK_WIDGET (iter->data);
734 if (GTK_WIDGET_VISIBLE (filter)) {
735 gtk_box_pack_start (GTK_BOX (priv->filters_hbox), filter, TRUE, TRUE, 0);
736 g_object_unref (filter);
737 /* GtkButton must be realized for accelerators to work */
738 gtk_widget_realize (filter);
744 * When items displayed in the menu change (e.g, a new item is added,
745 * an item is hidden or the list is reordered), the layout must be
746 * updated. To do this we repack all items starting from a given one.
749 hildon_app_menu_repack_items (HildonAppMenu *menu,
752 HildonAppMenuPrivate *priv;
753 gint row, col, nvisible, i;
756 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
759 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
760 /* Count number of visible items */
761 if (GTK_WIDGET_VISIBLE (iter->data))
763 /* Remove buttons from their parent */
764 if (start_from != -1 && i >= start_from) {
765 GtkWidget *item = GTK_WIDGET (iter->data);
766 GtkWidget *parent = gtk_widget_get_parent (item);
769 gtk_container_remove (GTK_CONTAINER (parent), item);
775 /* If items have been removed, recalculate the size of the menu */
776 if (start_from != -1)
777 gtk_window_resize (GTK_WINDOW (menu), 1, 1);
779 /* Set the final size now to avoid unnecessary resizes later */
781 gtk_table_resize (priv->table, ((nvisible - 1) / priv->columns) + 1, priv->columns);
785 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
786 GtkWidget *item = GTK_WIDGET (iter->data);
787 if (GTK_WIDGET_VISIBLE (item)) {
788 /* Don't add an item to the table if it's already there */
789 if (gtk_widget_get_parent (item) == NULL) {
790 gtk_table_attach_defaults (priv->table, item, col, col + 1, row, row + 1);
791 g_object_unref (item);
792 /* GtkButton must be realized for accelerators to work */
793 gtk_widget_realize (item);
795 if (++col == priv->columns) {
804 * hildon_app_menu_popup:
805 * @menu: a #HildonAppMenu
806 * @parent_window: a #GtkWindow
808 * Displays a menu on top of a window and makes it available for
814 hildon_app_menu_popup (HildonAppMenu *menu,
815 GtkWindow *parent_window)
817 HildonAppMenuPrivate *priv;
818 gboolean show_menu = FALSE;
821 g_return_if_fail (HILDON_IS_APP_MENU (menu));
822 g_return_if_fail (GTK_IS_WINDOW (parent_window));
824 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
826 /* Don't show menu if it doesn't contain visible items */
827 for (i = priv->buttons; i && !show_menu; i = i->next)
828 show_menu = GTK_WIDGET_VISIBLE (i->data);
830 for (i = priv->filters; i && !show_menu; i = i->next)
831 show_menu = GTK_WIDGET_VISIBLE (i->data);
834 hildon_app_menu_set_parent_window (menu, parent_window);
835 gtk_widget_show (GTK_WIDGET (menu));
841 * hildon_app_menu_get_items:
842 * @menu: a #HildonAppMenu
844 * Returns a list of all items (regular items, not filters) contained
847 * Returns: a newly-allocated list containing the items in @menu
852 hildon_app_menu_get_items (HildonAppMenu *menu)
854 HildonAppMenuPrivate *priv;
856 g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
858 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
860 return g_list_copy (priv->buttons);
864 * hildon_app_menu_get_filters:
865 * @menu: a #HildonAppMenu
867 * Returns a list of all filters contained in @menu.
869 * Returns: a newly-allocated list containing the filters in @menu
874 hildon_app_menu_get_filters (HildonAppMenu *menu)
876 HildonAppMenuPrivate *priv;
878 g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
880 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
882 return g_list_copy (priv->filters);
886 hildon_app_menu_init (HildonAppMenu *menu)
888 GtkWidget *alignment;
889 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
891 /* Initialize private variables */
892 priv->parent_window = NULL;
893 priv->transfer_window = NULL;
894 priv->pressed_outside = FALSE;
895 priv->inhibit_repack = FALSE;
896 priv->last_pressed_button = NULL;
897 priv->buttons = NULL;
898 priv->filters = NULL;
900 priv->width_request = -1;
901 priv->find_intruder_idle_id = 0;
902 priv->hide_idle_id = 0;
904 /* Create boxes and tables */
905 priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
906 priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 0));
907 priv->table = GTK_TABLE (gtk_table_new (1, priv->columns, TRUE));
909 /* Align the filters to the center */
910 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
911 gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
913 /* Pack everything */
914 gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
915 gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
916 gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
918 /* This should be treated like a normal, ref-counted widget */
919 g_object_force_floating (G_OBJECT (menu));
920 GTK_WINDOW (menu)->has_user_ref_count = FALSE;
922 gtk_widget_show_all (GTK_WIDGET (priv->vbox));
926 hildon_app_menu_finalize (GObject *object)
928 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
930 if (priv->find_intruder_idle_id) {
931 g_source_remove (priv->find_intruder_idle_id);
932 priv->find_intruder_idle_id = 0;
935 if (priv->hide_idle_id) {
936 g_source_remove (priv->hide_idle_id);
937 priv->hide_idle_id = 0;
940 if (priv->parent_window) {
941 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, object);
942 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, object);
945 if (priv->transfer_window)
946 gdk_window_destroy (priv->transfer_window);
948 g_list_foreach (priv->buttons, (GFunc) g_object_unref, NULL);
949 g_list_foreach (priv->filters, (GFunc) g_object_unref, NULL);
951 g_list_free (priv->buttons);
952 g_list_free (priv->filters);
954 g_signal_handlers_destroy (object);
955 G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
959 hildon_app_menu_class_init (HildonAppMenuClass *klass)
961 GObjectClass *gobject_class = (GObjectClass *)klass;
962 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
964 gobject_class->finalize = hildon_app_menu_finalize;
965 widget_class->show_all = hildon_app_menu_show_all;
966 widget_class->hide_all = hildon_app_menu_hide_all;
967 widget_class->map = hildon_app_menu_map;
968 widget_class->realize = hildon_app_menu_realize;
969 widget_class->unrealize = hildon_app_menu_unrealize;
970 widget_class->grab_notify = hildon_app_menu_grab_notify;
971 widget_class->key_press_event = hildon_app_menu_key_press;
972 widget_class->style_set = hildon_app_menu_style_set;
973 widget_class->delete_event = hildon_app_menu_delete_event_handler;
974 widget_class->size_request = hildon_app_menu_size_request;
976 g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
978 gtk_widget_class_install_style_property (
981 "horizontal-spacing",
982 "Horizontal spacing on menu items",
983 "Horizontal spacing between each menu item. Does not apply to filter buttons.",
987 gtk_widget_class_install_style_property (
991 "Vertical spacing on menu items",
992 "Vertical spacing between each menu item. Does not apply to filter buttons.",
996 gtk_widget_class_install_style_property (
999 "filter-group-width",
1000 "Width of the group of filter buttons",
1001 "Total width of the group of filter buttons, "
1002 "or -1 to use the natural size request.",
1006 gtk_widget_class_install_style_property (
1009 "filter-vertical-spacing",
1010 "Vertical spacing between filters and menu items",
1011 "Vertical spacing between filters and menu items",
1015 gtk_widget_class_install_style_property (
1019 "Border between menu edges and buttons",
1020 "Border between menu edges and buttons",
1024 gtk_widget_class_install_style_property (
1028 "Border between menu and screen edges (in horizontal mode)",
1029 "Border between the right and left edges of the menu and "
1030 "the screen edges (in horizontal mode)",