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 emit_menu_changed (HildonAppMenu *menu,
140 hildon_app_menu_apply_style (GtkWidget *widget);
142 G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
150 static guint app_menu_signals[LAST_SIGNAL] = { 0 };
153 * hildon_app_menu_new:
155 * Creates a new #HildonAppMenu.
157 * Return value: A #HildonAppMenu.
162 hildon_app_menu_new (void)
164 GtkWidget *menu = g_object_new (HILDON_TYPE_APP_MENU, NULL);
169 * hildon_app_menu_insert:
170 * @menu : A #HildonAppMenu
171 * @item : A #GtkButton to add to the #HildonAppMenu
172 * @position : The position in the item list where @item is added (from 0 to n-1).
174 * Adds @item to @menu at the position indicated by @position.
179 hildon_app_menu_insert (HildonAppMenu *menu,
183 HildonAppMenuPrivate *priv;
185 g_return_if_fail (HILDON_IS_APP_MENU (menu));
186 g_return_if_fail (GTK_IS_BUTTON (item));
188 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
190 /* Force widget size */
191 hildon_gtk_widget_set_theme_size (GTK_WIDGET (item),
192 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
194 /* Add the item to the menu */
195 g_object_ref_sink (item);
196 priv->buttons = g_list_insert (priv->buttons, item, position);
197 if (GTK_WIDGET_VISIBLE (item))
198 hildon_app_menu_repack_items (menu, position);
200 /* Enable accelerators */
201 g_signal_connect (item, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
203 /* Close the menu when the button is clicked */
204 g_signal_connect_swapped (item, "clicked", G_CALLBACK (gtk_widget_hide), menu);
205 g_signal_connect (item, "notify::visible", G_CALLBACK (item_visibility_changed), menu);
207 /* Remove item from list when it is destroyed */
208 g_object_weak_ref (G_OBJECT (item), (GWeakNotify) remove_item_from_list, &(priv->buttons));
209 g_object_weak_ref (G_OBJECT (item), (GWeakNotify) emit_menu_changed, menu);
211 g_signal_emit (menu, app_menu_signals[CHANGED], 0);
215 * hildon_app_menu_append:
216 * @menu : A #HildonAppMenu
217 * @item : A #GtkButton to add to the #HildonAppMenu
219 * Adds @item to the end of the menu's item list.
224 hildon_app_menu_append (HildonAppMenu *menu,
227 hildon_app_menu_insert (menu, item, -1);
231 * hildon_app_menu_prepend:
232 * @menu : A #HildonAppMenu
233 * @item : A #GtkButton to add to the #HildonAppMenu
235 * Adds @item to the beginning of the menu's item list.
240 hildon_app_menu_prepend (HildonAppMenu *menu,
243 hildon_app_menu_insert (menu, item, 0);
247 * hildon_app_menu_reorder_child:
248 * @menu : A #HildonAppMenu
249 * @item : A #GtkButton to move
250 * @position : The new position to place @item (from 0 to n-1).
252 * Moves a #GtkButton to a new position within #HildonAppMenu.
257 hildon_app_menu_reorder_child (HildonAppMenu *menu,
261 HildonAppMenuPrivate *priv;
264 g_return_if_fail (HILDON_IS_APP_MENU (menu));
265 g_return_if_fail (GTK_IS_BUTTON (item));
266 g_return_if_fail (position >= 0);
268 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
269 old_position = g_list_index (priv->buttons, item);
271 g_return_if_fail (old_position >= 0);
274 priv->buttons = g_list_remove (priv->buttons, item);
275 priv->buttons = g_list_insert (priv->buttons, item, position);
277 hildon_app_menu_repack_items (menu, MIN (old_position, position));
281 * hildon_app_menu_add_filter:
282 * @menu : A #HildonAppMenu
283 * @filter : A #GtkButton to add to the #HildonAppMenu.
285 * Adds the @filter to @menu.
290 hildon_app_menu_add_filter (HildonAppMenu *menu,
293 HildonAppMenuPrivate *priv;
295 g_return_if_fail (HILDON_IS_APP_MENU (menu));
296 g_return_if_fail (GTK_IS_BUTTON (filter));
298 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
300 /* Force widget size */
301 hildon_gtk_widget_set_theme_size (GTK_WIDGET (filter),
302 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
304 /* Add the filter to the menu */
305 g_object_ref_sink (filter);
306 priv->filters = g_list_append (priv->filters, filter);
307 if (GTK_WIDGET_VISIBLE (filter))
308 hildon_app_menu_repack_filters (menu);
310 /* Enable accelerators */
311 g_signal_connect (filter, "can-activate-accel", G_CALLBACK (can_activate_accel), NULL);
313 /* Close the menu when the button is clicked */
314 g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
315 g_signal_connect (filter, "notify::visible", G_CALLBACK (filter_visibility_changed), menu);
317 /* Remove filter from list when it is destroyed */
318 g_object_weak_ref (G_OBJECT (filter), (GWeakNotify) remove_item_from_list, &(priv->filters));
319 g_object_weak_ref (G_OBJECT (filter), (GWeakNotify) emit_menu_changed, menu);
321 g_signal_emit (menu, app_menu_signals[CHANGED], 0);
325 hildon_app_menu_set_columns (HildonAppMenu *menu,
328 HildonAppMenuPrivate *priv;
330 g_return_if_fail (HILDON_IS_APP_MENU (menu));
331 g_return_if_fail (columns > 0);
333 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
335 if (columns != priv->columns) {
336 priv->columns = columns;
337 hildon_app_menu_repack_items (menu, 0);
342 parent_window_topmost_notify (HildonWindow *parent_win,
346 if (!hildon_window_get_is_topmost (parent_win))
347 gtk_widget_hide (menu);
351 parent_window_unmapped (HildonWindow *parent_win,
354 gtk_widget_hide (menu);
358 hildon_app_menu_set_parent_window (HildonAppMenu *self,
359 GtkWindow *parent_window)
361 HildonAppMenuPrivate *priv;
363 g_return_if_fail (HILDON_IS_APP_MENU (self));
364 g_return_if_fail (parent_window == NULL || GTK_IS_WINDOW (parent_window));
366 priv = HILDON_APP_MENU_GET_PRIVATE(self);
368 /* Disconnect old handlers, if any */
369 if (priv->parent_window) {
370 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, self);
371 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, self);
374 /* Connect a new handler */
376 g_signal_connect (parent_window, "notify::is-topmost", G_CALLBACK (parent_window_topmost_notify), self);
377 g_signal_connect (parent_window, "unmap", G_CALLBACK (parent_window_unmapped), self);
380 priv->parent_window = parent_window;
382 if (parent_window == NULL && GTK_WIDGET_VISIBLE (self))
383 gtk_widget_hide (GTK_WIDGET (self));
386 gpointer G_GNUC_INTERNAL
387 hildon_app_menu_get_parent_window (HildonAppMenu *self)
389 HildonAppMenuPrivate *priv;
391 g_return_val_if_fail (HILDON_IS_APP_MENU (self), NULL);
393 priv = HILDON_APP_MENU_GET_PRIVATE (self);
395 return priv->parent_window;
399 screen_size_changed (GdkScreen *screen,
402 hildon_app_menu_apply_style (GTK_WIDGET (menu));
404 if (gdk_screen_get_width (screen) > gdk_screen_get_height (screen)) {
405 hildon_app_menu_set_columns (menu, 2);
407 hildon_app_menu_set_columns (menu, 1);
412 can_activate_accel (GtkWidget *widget,
416 return GTK_WIDGET_VISIBLE (widget);
420 item_visibility_changed (GtkWidget *item,
424 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
426 if (! priv->inhibit_repack)
427 hildon_app_menu_repack_items (menu, g_list_index (priv->buttons, item));
428 g_signal_emit (menu, app_menu_signals[CHANGED], 0);
432 filter_visibility_changed (GtkWidget *item,
436 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (menu);
438 if (! priv->inhibit_repack)
439 hildon_app_menu_repack_filters (menu);
440 g_signal_emit (menu, app_menu_signals[CHANGED], 0);
444 remove_item_from_list (GList **list,
447 *list = g_list_remove (*list, item);
451 emit_menu_changed (HildonAppMenu *menu,
454 g_signal_emit (menu, app_menu_signals[CHANGED], 0);
458 hildon_app_menu_show_all (GtkWidget *widget)
460 HildonAppMenu *menu = HILDON_APP_MENU (widget);
461 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
463 priv->inhibit_repack = TRUE;
465 /* Show children, but not self. */
466 g_list_foreach (priv->buttons, (GFunc) gtk_widget_show_all, NULL);
467 g_list_foreach (priv->filters, (GFunc) gtk_widget_show_all, NULL);
469 priv->inhibit_repack = FALSE;
471 hildon_app_menu_repack_items (menu, 0);
472 hildon_app_menu_repack_filters (menu);
477 hildon_app_menu_hide_all (GtkWidget *widget)
479 HildonAppMenu *menu = HILDON_APP_MENU (widget);
480 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
482 priv->inhibit_repack = TRUE;
484 /* Hide children, but not self. */
485 g_list_foreach (priv->buttons, (GFunc) gtk_widget_hide_all, NULL);
486 g_list_foreach (priv->filters, (GFunc) gtk_widget_hide_all, NULL);
488 priv->inhibit_repack = FALSE;
490 hildon_app_menu_repack_items (menu, 0);
491 hildon_app_menu_repack_filters (menu);
495 * There's a race condition that can freeze the UI if a dialog appears
496 * between a HildonAppMenu and its parent window, see NB#100468
499 hildon_app_menu_find_intruder (gpointer data)
501 GtkWidget *widget = GTK_WIDGET (data);
502 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
504 priv->find_intruder_idle_id = 0;
506 /* If there's a window between the menu and its parent window, hide the menu */
507 if (priv->parent_window) {
508 gboolean intruder_found = FALSE;
509 GdkScreen *screen = gtk_widget_get_screen (widget);
510 GList *stack = gdk_screen_get_window_stack (screen);
511 GList *parent_pos = g_list_find (stack, GTK_WIDGET (priv->parent_window)->window);
512 GList *toplevels = gtk_window_list_toplevels ();
515 for (i = toplevels; i != NULL && !intruder_found; i = i->next) {
516 if (i->data != widget && i->data != priv->parent_window) {
517 if (g_list_find (parent_pos, GTK_WIDGET (i->data)->window)) {
518 /* HildonBanners are not closed automatically when
519 * a new window appears, so we must close them by
520 * hand to make the AppMenu work as expected.
521 * Yes, this is a hack. See NB#111027 */
522 if (HILDON_IS_BANNER (i->data)) {
523 gtk_widget_hide (i->data);
524 } else if (!HILDON_IS_ANIMATION_ACTOR (i->data)) {
525 intruder_found = TRUE;
531 g_list_foreach (stack, (GFunc) g_object_unref, NULL);
533 g_list_free (toplevels);
536 gtk_widget_hide (widget);
543 hildon_app_menu_map (GtkWidget *widget)
545 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
547 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
549 if (priv->find_intruder_idle_id == 0)
550 priv->find_intruder_idle_id = gdk_threads_add_idle (hildon_app_menu_find_intruder, widget);
554 hildon_app_menu_grab_notify (GtkWidget *widget,
555 gboolean was_grabbed)
557 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->grab_notify)
558 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->grab_notify (widget, was_grabbed);
560 if (!was_grabbed && GTK_WIDGET_VISIBLE (widget))
561 gtk_widget_hide (widget);
565 hildon_app_menu_hide_idle (gpointer widget)
567 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
568 gtk_widget_hide (GTK_WIDGET (widget));
569 priv->hide_idle_id = 0;
573 /* Send keyboard accelerators to the parent window, if necessary.
574 * This code is heavily based on gtk_menu_key_press ()
577 hildon_app_menu_key_press (GtkWidget *widget,
580 GtkWindow *parent_window;
581 HildonAppMenuPrivate *priv;
583 g_return_val_if_fail (HILDON_IS_APP_MENU (widget), FALSE);
584 g_return_val_if_fail (event != NULL, FALSE);
586 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->key_press_event (widget, event))
589 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
590 parent_window = priv->parent_window;
593 guint accel_key, accel_mods;
594 GdkModifierType consumed_modifiers;
596 GSList *accel_groups;
599 display = gtk_widget_get_display (widget);
601 /* Figure out what modifiers went into determining the key symbol */
602 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
603 event->hardware_keycode, event->state, event->group,
604 NULL, NULL, NULL, &consumed_modifiers);
606 accel_key = gdk_keyval_to_lower (event->keyval);
607 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
609 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
610 * We re-upper case when we match against the keyval, but display and save in caseless form.
612 if (accel_key != event->keyval)
613 accel_mods |= GDK_SHIFT_MASK;
615 accel_groups = gtk_accel_groups_from_object (G_OBJECT (parent_window));
617 for (list = accel_groups; list; list = list->next) {
618 GtkAccelGroup *accel_group = list->data;
620 if (gtk_accel_group_query (accel_group, accel_key, accel_mods, NULL)) {
621 gtk_window_activate_key (parent_window, event);
622 priv->hide_idle_id = gdk_threads_add_idle (hildon_app_menu_hide_idle, widget);
632 hildon_app_menu_delete_event_handler (GtkWidget *widget,
635 /* Hide the menu if it receives a delete-event, but don't destroy it */
636 gtk_widget_hide (widget);
641 hildon_app_menu_size_request (GtkWidget *widget,
642 GtkRequisition *requisition)
644 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE (widget);
646 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->size_request (widget, requisition);
648 requisition->width = priv->width_request;
652 hildon_app_menu_realize (GtkWidget *widget)
654 Atom property, window_type;
656 GdkDisplay *gdkdisplay;
659 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
661 gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
663 gdkdisplay = gdk_drawable_get_display (widget->window);
664 xdisplay = GDK_WINDOW_XDISPLAY (widget->window);
666 property = gdk_x11_get_xatom_by_name_for_display (gdkdisplay, "_NET_WM_WINDOW_TYPE");
667 window_type = XInternAtom (xdisplay, "_HILDON_WM_WINDOW_TYPE_APP_MENU", False);
668 XChangeProperty (xdisplay, GDK_WINDOW_XID (widget->window), property,
669 XA_ATOM, 32, PropModeReplace, (guchar *) &window_type, 1);
671 /* Detect any screen changes */
672 screen = gtk_widget_get_screen (widget);
673 g_signal_connect (screen, "size-changed", G_CALLBACK (screen_size_changed), widget);
675 /* Force menu to set the initial layout */
676 screen_size_changed (screen, HILDON_APP_MENU (widget));
680 hildon_app_menu_unrealize (GtkWidget *widget)
682 GdkScreen *screen = gtk_widget_get_screen (widget);
683 /* Disconnect "size-changed" signal handler */
684 g_signal_handlers_disconnect_by_func (screen, G_CALLBACK (screen_size_changed), widget);
686 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unrealize (widget);
690 hildon_app_menu_apply_style (GtkWidget *widget)
693 gint filter_group_width;
694 guint horizontal_spacing, vertical_spacing, filter_vertical_spacing;
695 guint inner_border, external_border;
696 HildonAppMenuPrivate *priv;
698 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
700 gtk_widget_style_get (widget,
701 "horizontal-spacing", &horizontal_spacing,
702 "vertical-spacing", &vertical_spacing,
703 "filter-group-width", &filter_group_width,
704 "filter-vertical-spacing", &filter_vertical_spacing,
705 "inner-border", &inner_border,
706 "external-border", &external_border,
710 gtk_table_set_row_spacings (priv->table, vertical_spacing);
711 gtk_table_set_col_spacings (priv->table, horizontal_spacing);
712 gtk_box_set_spacing (priv->vbox, filter_vertical_spacing);
714 /* Set inner border */
715 gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
717 /* Set width of the group of filter buttons */
718 gtk_widget_set_size_request (GTK_WIDGET (priv->filters_hbox), filter_group_width, -1);
720 /* Compute width request */
721 screen = gtk_widget_get_screen (widget);
722 if (gdk_screen_get_width (screen) < gdk_screen_get_height (screen)) {
725 priv->width_request = gdk_screen_get_width (screen) - external_border * 2;
728 gdk_window_move_resize (widget->window,
729 external_border, 0, 1, 1);
731 gtk_widget_queue_resize (widget);
735 hildon_app_menu_style_set (GtkWidget *widget,
736 GtkStyle *previous_style)
738 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set)
739 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set (widget, previous_style);
741 hildon_app_menu_apply_style (widget);
745 hildon_app_menu_repack_filters (HildonAppMenu *menu)
747 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
750 for (iter = priv->filters; iter != NULL; iter = iter->next) {
751 GtkWidget *filter = GTK_WIDGET (iter->data);
752 GtkWidget *parent = gtk_widget_get_parent (filter);
754 g_object_ref (filter);
755 gtk_container_remove (GTK_CONTAINER (parent), filter);
759 for (iter = priv->filters; iter != NULL; iter = iter->next) {
760 GtkWidget *filter = GTK_WIDGET (iter->data);
761 if (GTK_WIDGET_VISIBLE (filter)) {
762 gtk_box_pack_start (GTK_BOX (priv->filters_hbox), filter, TRUE, TRUE, 0);
763 g_object_unref (filter);
764 /* GtkButton must be realized for accelerators to work */
765 gtk_widget_realize (filter);
771 * When items displayed in the menu change (e.g, a new item is added,
772 * an item is hidden or the list is reordered), the layout must be
773 * updated. To do this we repack all items starting from a given one.
776 hildon_app_menu_repack_items (HildonAppMenu *menu,
779 HildonAppMenuPrivate *priv;
780 gint row, col, nvisible, i;
783 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
786 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
787 /* Count number of visible items */
788 if (GTK_WIDGET_VISIBLE (iter->data))
790 /* Remove buttons from their parent */
791 if (start_from != -1 && i >= start_from) {
792 GtkWidget *item = GTK_WIDGET (iter->data);
793 GtkWidget *parent = gtk_widget_get_parent (item);
796 gtk_container_remove (GTK_CONTAINER (parent), item);
802 /* If items have been removed, recalculate the size of the menu */
803 if (start_from != -1)
804 gtk_window_resize (GTK_WINDOW (menu), 1, 1);
806 /* Set the final size now to avoid unnecessary resizes later */
808 gtk_table_resize (priv->table, ((nvisible - 1) / priv->columns) + 1, priv->columns);
812 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
813 GtkWidget *item = GTK_WIDGET (iter->data);
814 if (GTK_WIDGET_VISIBLE (item)) {
815 /* Don't add an item to the table if it's already there */
816 if (gtk_widget_get_parent (item) == NULL) {
817 gtk_table_attach_defaults (priv->table, item, col, col + 1, row, row + 1);
818 g_object_unref (item);
819 /* GtkButton must be realized for accelerators to work */
820 gtk_widget_realize (item);
822 if (++col == priv->columns) {
829 gtk_widget_queue_draw (GTK_WIDGET (menu));
833 * hildon_app_menu_has_visible_children:
834 * @menu: a #HildonAppMenu
836 * Returns whether this menu has any visible items
837 * and/or filters. If this is %FALSE, the menu will
840 * Returns: whether there are visible items or filters
845 hildon_app_menu_has_visible_children (HildonAppMenu *menu)
847 HildonAppMenuPrivate *priv;
849 gboolean show_menu = FALSE;
851 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
853 /* Don't show menu if it doesn't contain visible items */
854 for (i = priv->buttons; i && !show_menu; i = i->next)
855 show_menu = GTK_WIDGET_VISIBLE (i->data);
857 for (i = priv->filters; i && !show_menu; i = i->next)
858 show_menu = GTK_WIDGET_VISIBLE (i->data);
864 * hildon_app_menu_popup:
865 * @menu: a #HildonAppMenu
866 * @parent_window: a #GtkWindow
868 * Displays a menu on top of a window and makes it available for
874 hildon_app_menu_popup (HildonAppMenu *menu,
875 GtkWindow *parent_window)
877 g_return_if_fail (HILDON_IS_APP_MENU (menu));
878 g_return_if_fail (GTK_IS_WINDOW (parent_window));
880 if (hildon_app_menu_has_visible_children (menu)) {
881 hildon_app_menu_set_parent_window (menu, parent_window);
882 gtk_widget_show (GTK_WIDGET (menu));
888 * hildon_app_menu_get_items:
889 * @menu: a #HildonAppMenu
891 * Returns a list of all items (regular items, not filters) contained
894 * Returns: a newly-allocated list containing the items in @menu
899 hildon_app_menu_get_items (HildonAppMenu *menu)
901 HildonAppMenuPrivate *priv;
903 g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
905 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
907 return g_list_copy (priv->buttons);
911 * hildon_app_menu_get_filters:
912 * @menu: a #HildonAppMenu
914 * Returns a list of all filters contained in @menu.
916 * Returns: a newly-allocated list containing the filters in @menu
921 hildon_app_menu_get_filters (HildonAppMenu *menu)
923 HildonAppMenuPrivate *priv;
925 g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
927 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
929 return g_list_copy (priv->filters);
933 hildon_app_menu_init (HildonAppMenu *menu)
935 GtkWidget *alignment;
936 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
938 /* Initialize private variables */
939 priv->parent_window = NULL;
940 priv->transfer_window = NULL;
941 priv->pressed_outside = FALSE;
942 priv->inhibit_repack = FALSE;
943 priv->last_pressed_button = NULL;
944 priv->buttons = NULL;
945 priv->filters = NULL;
947 priv->width_request = -1;
948 priv->find_intruder_idle_id = 0;
949 priv->hide_idle_id = 0;
951 /* Create boxes and tables */
952 priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
953 priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 0));
954 priv->table = GTK_TABLE (gtk_table_new (1, priv->columns, TRUE));
956 /* Align the filters to the center */
957 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
958 gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
960 /* Pack everything */
961 gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
962 gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
963 gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
965 /* This should be treated like a normal, ref-counted widget */
966 g_object_force_floating (G_OBJECT (menu));
967 GTK_WINDOW (menu)->has_user_ref_count = FALSE;
969 gtk_widget_show_all (GTK_WIDGET (priv->vbox));
973 hildon_app_menu_finalize (GObject *object)
975 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
977 if (priv->find_intruder_idle_id) {
978 g_source_remove (priv->find_intruder_idle_id);
979 priv->find_intruder_idle_id = 0;
982 if (priv->hide_idle_id) {
983 g_source_remove (priv->hide_idle_id);
984 priv->hide_idle_id = 0;
987 if (priv->parent_window) {
988 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_topmost_notify, object);
989 g_signal_handlers_disconnect_by_func (priv->parent_window, parent_window_unmapped, object);
992 if (priv->transfer_window)
993 gdk_window_destroy (priv->transfer_window);
995 g_list_foreach (priv->buttons, (GFunc) g_object_unref, NULL);
996 g_list_foreach (priv->filters, (GFunc) g_object_unref, NULL);
998 g_list_free (priv->buttons);
999 g_list_free (priv->filters);
1001 g_signal_handlers_destroy (object);
1002 G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
1006 hildon_app_menu_class_init (HildonAppMenuClass *klass)
1008 GObjectClass *gobject_class = (GObjectClass *)klass;
1009 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
1011 gobject_class->finalize = hildon_app_menu_finalize;
1012 widget_class->show_all = hildon_app_menu_show_all;
1013 widget_class->hide_all = hildon_app_menu_hide_all;
1014 widget_class->map = hildon_app_menu_map;
1015 widget_class->realize = hildon_app_menu_realize;
1016 widget_class->unrealize = hildon_app_menu_unrealize;
1017 widget_class->grab_notify = hildon_app_menu_grab_notify;
1018 widget_class->key_press_event = hildon_app_menu_key_press;
1019 widget_class->style_set = hildon_app_menu_style_set;
1020 widget_class->delete_event = hildon_app_menu_delete_event_handler;
1021 widget_class->size_request = hildon_app_menu_size_request;
1023 g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
1025 gtk_widget_class_install_style_property (
1028 "horizontal-spacing",
1029 "Horizontal spacing on menu items",
1030 "Horizontal spacing between each menu item. Does not apply to filter buttons.",
1034 gtk_widget_class_install_style_property (
1038 "Vertical spacing on menu items",
1039 "Vertical spacing between each menu item. Does not apply to filter buttons.",
1043 gtk_widget_class_install_style_property (
1046 "filter-group-width",
1047 "Width of the group of filter buttons",
1048 "Total width of the group of filter buttons, "
1049 "or -1 to use the natural size request.",
1053 gtk_widget_class_install_style_property (
1056 "filter-vertical-spacing",
1057 "Vertical spacing between filters and menu items",
1058 "Vertical spacing between filters and menu items",
1062 gtk_widget_class_install_style_property (
1066 "Border between menu edges and buttons",
1067 "Border between menu edges and buttons",
1071 gtk_widget_class_install_style_property (
1075 "Border between menu and screen edges (in horizontal mode)",
1076 "Border between the right and left edges of the menu and "
1077 "the screen edges (in horizontal mode)",
1083 * HildonAppMenu::changed:
1084 * @widget: the widget that received the signal
1086 * The HildonAppMenu::changed signal is emitted whenever an
1087 * item or filter is added or removed from the menu.
1091 app_menu_signals[CHANGED] =
1092 g_signal_new ("changed",
1093 G_TYPE_FROM_CLASS (klass),
1094 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1097 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL);