2 * This file is a part of hildon
4 * Copyright (C) 2008 Nokia Corporation, all rights reserved.
6 * Contact: Karl Lattimer <karl.lattimer@nokia.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser Public License as published by
10 * the Free Software Foundation; version 2 of the license.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser Public License for more details.
20 * SECTION:hildon-app-menu
21 * @short_description: Widget representing the application menu in the Hildon framework.
23 * The #HildonAppMenu is a GTK widget which represents an application
24 * menu in the Hildon framework.
26 * This menu opens from the top of the screen and contains a number of
27 * entries (#GtkButton) organized in two columns. Entries are added
28 * left to right and top to bottom.
30 * Besides that, the #HildonAppMenu can contain a group of filter buttons
31 * (#GtkToggleButton or #GtkRadioButton).
33 * To use a #HildonAppMenu, add it to a #HildonStackableWindow using
34 * hildon_stackable_window_set_main_menu(). The menu will appear when
35 * the user presses the window title bar. Alternatively, you can show
36 * it by hand using gtk_widget_show().
38 * The menu will be automatically hidden when one of its buttons is
39 * clicked. Use g_signal_connect_after() when connecting callbacks to
40 * buttons to make sure that they're called after the menu
41 * disappears. Alternatively, you can add the button to the menu
42 * before connecting any callback.
44 * Although implemented with a #GtkWindow, #HildonAppMenu behaves like
45 * a normal ref-counted widget, so g_object_ref(), g_object_unref(),
46 * g_object_ref_sink() and friends will behave just like with any
47 * other non-toplevel widget.
50 * <title>Creating a HildonAppMenu</title>
52 * HildonStackableWindow *win;
53 * HildonAppMenu *menu;
57 * win = HILDON_STACKABLE_WINDOW (hildon_stackable_window_new ());
58 * menu = HILDON_APP_MENU (hildon_app_menu_new ());
60 * // Create a button and add it to the menu
61 * button = gtk_button_new_with_label ("Menu command one");
62 * g_signal_connect_after (button, "clicked", G_CALLBACK (button_one_clicked), userdata);
63 * hildon_app_menu_append (menu, GTK_BUTTON (button));
66 * button = gtk_button_new_with_label ("Menu command two");
67 * g_signal_connect_after (button, "clicked", G_CALLBACK (button_two_clicked), userdata);
68 * hildon_app_menu_append (menu, GTK_BUTTON (button));
70 * // Create a filter and add it to the menu
71 * filter = gtk_radio_button_new_with_label (NULL, "Filter one");
72 * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
73 * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_one_clicked), userdata);
74 * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
77 * filter = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (filter), "Filter two");
78 * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
79 * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_two_clicked), userdata);
80 * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
82 * // Add the menu to the window
83 * hildon_stackable_window_set_main_menu (win, menu);
90 #include <X11/Xatom.h>
93 #include "hildon-app-menu.h"
94 #include "hildon-app-menu-private.h"
101 grab_transfer_window_get (GtkWidget *widget);
104 hildon_app_menu_construct_child (HildonAppMenu *menu);
107 button_visibility_changed (GtkWidget *item,
109 HildonAppMenu *menu);
111 G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
114 * hildon_app_menu_new:
116 * Creates a new #HildonAppMenu.
118 * Return value: A #HildonAppMenu.
121 hildon_app_menu_new (void)
123 GtkWidget *menu = g_object_new (HILDON_TYPE_APP_MENU, NULL);
128 * hildon_app_menu_insert:
129 * @menu : A #HildonAppMenu
130 * @item : A #GtkButton to add to the #HildonAppMenu
131 * @position : The position in the item list where @item is added (from 0 to n-1).
133 * Adds @item to @menu at the position indicated by @position.
136 hildon_app_menu_insert (HildonAppMenu *menu,
140 HildonAppMenuPrivate *priv;
142 g_return_if_fail (HILDON_IS_APP_MENU (menu));
143 g_return_if_fail (GTK_IS_BUTTON (item));
145 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
147 /* Add the item to the menu */
148 gtk_widget_show (GTK_WIDGET (item));
149 priv->buttons = g_list_insert (priv->buttons, item, position);
150 hildon_app_menu_construct_child (menu);
152 /* Close the menu when the button is clicked */
153 g_signal_connect_swapped (item, "clicked", G_CALLBACK (gtk_widget_hide), menu);
154 g_signal_connect (item, "notify::visible", G_CALLBACK (button_visibility_changed), menu);
158 * hildon_app_menu_append:
159 * @menu : A #HildonAppMenu
160 * @item : A #GtkButton to add to the #HildonAppMenu
162 * Adds @item to the end of the menu's item list.
165 hildon_app_menu_append (HildonAppMenu *menu,
168 hildon_app_menu_insert (menu, item, -1);
172 * hildon_app_menu_prepend:
173 * @menu : A #HildonAppMenu
174 * @item : A #GtkButton to add to the #HildonAppMenu
176 * Adds @item to the beginning of the menu's item list.
179 hildon_app_menu_prepend (HildonAppMenu *menu,
182 hildon_app_menu_insert (menu, item, 0);
186 * hildon_app_menu_reorder_child:
187 * @menu : A #HildonAppMenu
188 * @item : A #GtkButton to move
189 * @position : The new position to place @item (from 0 to n-1).
191 * Moves a #GtkButton to a new position within #HildonAppMenu.
194 hildon_app_menu_reorder_child (HildonAppMenu *menu,
198 HildonAppMenuPrivate *priv;
200 g_return_if_fail (HILDON_IS_APP_MENU (menu));
201 g_return_if_fail (GTK_IS_BUTTON (item));
202 g_return_if_fail (position >= 0);
204 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
206 g_return_if_fail (g_list_find (priv->buttons, item));
209 priv->buttons = g_list_remove (priv->buttons, item);
210 priv->buttons = g_list_insert (priv->buttons, item, position);
212 hildon_app_menu_construct_child (menu);
216 * hildon_app_menu_add_filter:
217 * @menu : A #HildonAppMenu
218 * @filter : A #GtkButton to add to the #HildonAppMenu.
220 * Adds the @filter to @menu.
223 hildon_app_menu_add_filter (HildonAppMenu *menu,
226 HildonAppMenuPrivate *priv;
228 g_return_if_fail (HILDON_IS_APP_MENU (menu));
229 g_return_if_fail (GTK_IS_BUTTON (filter));
231 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
233 /* Add the filter to the menu */
234 gtk_widget_show (GTK_WIDGET (filter));
235 priv->filters = g_list_append (priv->filters, filter);
236 hildon_app_menu_construct_child (menu);
238 /* Close the menu when the button is clicked */
239 g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
240 g_signal_connect (filter, "notify::visible", G_CALLBACK (button_visibility_changed), menu);
244 hildon_app_menu_set_columns (HildonAppMenu *menu,
247 HildonAppMenuPrivate *priv;
249 g_warning ("This property will be removed in the future. See documentation for details");
251 g_return_if_fail (HILDON_IS_APP_MENU (menu));
252 g_return_if_fail (columns > 0);
254 priv = HILDON_APP_MENU_GET_PRIVATE (menu);
256 if (columns != priv->columns) {
257 priv->columns = columns;
258 hildon_app_menu_construct_child (menu);
263 hildon_app_menu_set_property (GObject *object,
268 HildonAppMenu *menu = HILDON_APP_MENU (object);
273 hildon_app_menu_set_columns (menu, g_value_get_uint (value));
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282 button_visibility_changed (GtkWidget *item,
286 hildon_app_menu_construct_child (menu);
290 hildon_app_menu_map (GtkWidget *widget)
292 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
294 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
296 /* Grab pointer and keyboard */
297 if (priv->transfer_window == NULL) {
298 gboolean has_grab = FALSE;
300 priv->transfer_window = grab_transfer_window_get (widget);
302 if (gdk_pointer_grab (priv->transfer_window, TRUE,
303 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
304 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
305 GDK_POINTER_MOTION_MASK, NULL, NULL,
306 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
307 if (gdk_keyboard_grab (priv->transfer_window, TRUE,
308 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
311 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
317 gtk_grab_add (widget);
319 gdk_window_destroy (priv->transfer_window);
320 priv->transfer_window = NULL;
326 hildon_app_menu_unmap (GtkWidget *widget)
328 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
330 /* Remove the grab */
331 if (priv->transfer_window != NULL) {
332 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
334 gtk_grab_remove (widget);
336 gdk_window_destroy (priv->transfer_window);
337 priv->transfer_window = NULL;
340 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unmap (widget);
344 hildon_app_menu_button_press (GtkWidget *widget,
345 GdkEventButton *event)
348 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
350 gdk_window_get_position (widget->window, &x, &y);
352 /* Whether the button has been pressed outside the widget */
353 priv->pressed_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
354 event->y_root < y || event->y_root > y + widget->allocation.height);
356 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event) {
357 return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event (widget, event);
364 hildon_app_menu_button_release (GtkWidget *widget,
365 GdkEventButton *event)
367 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
369 if (priv->pressed_outside) {
371 gboolean released_outside;
373 gdk_window_get_position (widget->window, &x, &y);
375 /* Whether the button has been released outside the widget */
376 released_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
377 event->y_root < y || event->y_root > y + widget->allocation.height);
379 if (released_outside) {
380 gtk_widget_hide (widget);
383 priv->pressed_outside = FALSE; /* Always reset pressed_outside to FALSE */
386 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event) {
387 return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event (widget, event);
393 /* Grab transfer window (based on the one from GtkMenu) */
395 grab_transfer_window_get (GtkWidget *widget)
398 GdkWindowAttr attributes;
399 gint attributes_mask;
403 attributes.width = 10;
404 attributes.height = 10;
405 attributes.window_type = GDK_WINDOW_TEMP;
406 attributes.wclass = GDK_INPUT_ONLY;
407 attributes.override_redirect = TRUE;
408 attributes.event_mask = 0;
410 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
412 window = gdk_window_new (gtk_widget_get_root_window (widget),
413 &attributes, attributes_mask);
414 gdk_window_set_user_data (window, widget);
416 gdk_window_show (window);
422 hildon_app_menu_realize (GtkWidget *widget)
426 const gchar *notification_type = "_HILDON_WM_WINDOW_TYPE_APP_MENU";
428 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
430 gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
432 display = gdk_drawable_get_display (widget->window);
433 atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE");
434 XChangeProperty (GDK_WINDOW_XDISPLAY (widget->window), GDK_WINDOW_XID (widget->window),
435 atom, XA_STRING, 8, PropModeReplace, (guchar *) notification_type,
436 strlen (notification_type));
440 hildon_app_menu_apply_style (GtkWidget *widget)
444 guint horizontal_spacing, vertical_spacing, inner_border, external_border;
445 HildonAppMenuPrivate *priv;
447 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
449 gtk_widget_style_get (widget,
450 "horizontal-spacing", &horizontal_spacing,
451 "vertical-spacing", &vertical_spacing,
452 "inner-border", &inner_border,
453 "external-border", &external_border,
457 gtk_table_set_row_spacings (priv->table, vertical_spacing);
458 gtk_table_set_col_spacings (priv->table, horizontal_spacing);
459 gtk_box_set_spacing (priv->vbox, vertical_spacing);
461 /* Set inner border */
462 gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
464 /* Set default size */
465 screen = gtk_widget_get_screen (widget);
466 width = gdk_screen_get_width (screen) - external_border * 2;
467 gtk_window_set_default_size (GTK_WINDOW (widget), width, -1);
471 hildon_app_menu_style_set (GtkWidget *widget,
472 GtkStyle *previous_style)
474 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set)
475 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set (widget, previous_style);
477 hildon_app_menu_apply_style (widget);
481 hildon_app_menu_construct_child (HildonAppMenu *menu)
483 HildonAppMenuPrivate *priv;
487 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
489 /* Remove all buttons from their parents */
490 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
491 GtkWidget *item = GTK_WIDGET (iter->data);
492 GtkWidget *parent = gtk_widget_get_parent (item);
495 gtk_container_remove (GTK_CONTAINER (parent), item);
499 for (iter = priv->filters; iter != NULL; iter = iter->next) {
500 GtkWidget *item = GTK_WIDGET (iter->data);
501 GtkWidget *parent = gtk_widget_get_parent (item);
504 gtk_container_remove (GTK_CONTAINER (parent), item);
508 /* Resize the table */
509 gtk_table_resize (priv->table, 1, priv->columns);
511 /* Resize the menu to its minimum size */
512 gtk_window_resize (GTK_WINDOW (menu), 1, 1);
516 for (iter = priv->buttons; iter != NULL; iter = iter->next) {
517 GtkWidget *item = GTK_WIDGET (iter->data);
518 if (GTK_WIDGET_VISIBLE (item)) {
519 gtk_table_attach_defaults (priv->table, item, col, col + 1, row, row + 1);
520 if (++col == priv->columns) {
527 for (iter = priv->filters; iter != NULL; iter = iter->next) {
528 GtkWidget *filter = GTK_WIDGET (iter->data);
529 if (GTK_WIDGET_VISIBLE (filter)) {
530 gtk_box_pack_start (GTK_BOX (priv->filters_hbox), filter, TRUE, TRUE, 0);
534 if (GTK_WIDGET_VISIBLE (GTK_WIDGET (menu))) {
535 gtk_window_reshow_with_initial_size (GTK_WINDOW (menu));
540 hildon_app_menu_init (HildonAppMenu *menu)
542 GtkWidget *alignment;
543 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
545 /* Initialize private variables */
546 priv->transfer_window = NULL;
547 priv->pressed_outside = FALSE;
548 priv->buttons = NULL;
549 priv->filters = NULL;
552 /* Create boxes and tables */
553 priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
554 priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 0));
555 priv->table = GTK_TABLE (gtk_table_new (1, priv->columns, TRUE));
557 /* Align the filters to the center */
558 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
559 gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
561 /* Pack everything */
562 gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
563 gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
564 gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
566 /* Apply style properties */
567 hildon_app_menu_apply_style (GTK_WIDGET (menu));
569 /* Make menu a modal window */
570 gtk_window_set_modal (GTK_WINDOW (menu), TRUE);
572 /* This should be treated like a normal, ref-counted widget */
573 g_object_force_floating (G_OBJECT (menu));
574 GTK_WINDOW (menu)->has_user_ref_count = FALSE;
576 gtk_widget_show_all (GTK_WIDGET (priv->vbox));
580 hildon_app_menu_finalize (GObject *object)
582 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
584 if (priv->transfer_window)
585 gdk_window_destroy (priv->transfer_window);
587 g_list_free (priv->buttons);
588 g_list_free (priv->filters);
590 g_signal_handlers_destroy (object);
591 G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
595 hildon_app_menu_class_init (HildonAppMenuClass *klass)
597 GObjectClass *gobject_class = (GObjectClass *)klass;
598 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
600 gobject_class->finalize = hildon_app_menu_finalize;
601 gobject_class->set_property = hildon_app_menu_set_property;
602 widget_class->map = hildon_app_menu_map;
603 widget_class->unmap = hildon_app_menu_unmap;
604 widget_class->realize = hildon_app_menu_realize;
605 widget_class->button_press_event = hildon_app_menu_button_press;
606 widget_class->button_release_event = hildon_app_menu_button_release;
607 widget_class->style_set = hildon_app_menu_style_set;
609 g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
611 g_object_class_install_property (
617 "Number of columns used to display menu items. "
618 "IMPORTANT: this is a temporary property. Don't use unless really needed. "
619 "The number of columns will be managed automatically in the future, "
620 "and this property will be removed.",
624 gtk_widget_class_install_style_property (
627 "horizontal-spacing",
628 "Horizontal spacing on menu items",
629 "Horizontal spacing between each menu item. Does not apply to filter buttons.",
633 gtk_widget_class_install_style_property (
637 "Vertical spacing on menu items",
638 "Vertical spacing between each menu item. Does not apply to filter buttons.",
642 gtk_widget_class_install_style_property (
646 "Border between menu edges and buttons",
647 "Border between menu edges and buttons",
651 gtk_widget_class_install_style_property (
655 "Border between menu and screen edges",
656 "Border between the right and left edges of the menu and the screen edges",