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 with
34 * with hildon_stackable_window_set_main_menu(). The menu will appear
35 * when the user presses the window title bar.
37 * Alternatively, you can show it by hand using gtk_widget_show().
39 * The menu will be automatically hidden when one of its buttons is
40 * clicked. Use g_signal_connect_after() when connecting callbacks to
41 * buttons to make sure that they're called after the menu
42 * disappears. Alternatively, you can add the button to the menu
43 * before connecting any callback.
46 * <title>Creating a HildonAppMenu</title>
48 * HildonStackableWindow *win;
49 * HildonAppMenu *menu;
53 * win = HILDON_STACKABLE_WINDOW (hildon_stackable_window_new ());
54 * menu = HILDON_APP_MENU (hildon_app_menu_new ());
56 * // Create a button and add it to the menu
57 * button = gtk_button_new_with_label ("Menu command one");
58 * g_signal_connect_after (button, "clicked", G_CALLBACK (button_one_clicked), userdata);
59 * hildon_app_menu_append (menu, GTK_BUTTON (button));
62 * button = gtk_button_new_with_label ("Menu command two");
63 * g_signal_connect_after (button, "clicked", G_CALLBACK (button_two_clicked), userdata);
64 * hildon_app_menu_append (menu, GTK_BUTTON (button));
66 * // Create a filter and add it to the menu
67 * filter = gtk_radio_button_new_with_label (NULL, "Filter one");
68 * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
69 * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_one_clicked), userdata);
70 * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
73 * filter = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (filter), "Filter two");
74 * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
75 * g_signal_connect_after (filter, "clicked", G_CALLBACK (filter_two_clicked), userdata);
76 * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
78 * // Add the menu to the window
79 * hildon_stackable_window_set_main_menu (win, menu);
86 #include <X11/Xatom.h>
89 #include "hildon-app-menu.h"
90 #include "hildon-app-menu-private.h"
93 grab_transfer_window_get (GtkWidget *widget);
95 G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
98 * hildon_app_menu_new:
100 * Creates a new #HildonAppMenu.
102 * Return value: A #HildonAppMenu.
105 hildon_app_menu_new (void)
107 GtkWidget *menu = g_object_new (HILDON_TYPE_APP_MENU, NULL);
112 * hildon_app_menu_append:
113 * @menu : A #HildonAppMenu
114 * @item : A #GtkButton to add to the #HildonAppMenu
116 * Adds the @item to @menu.
119 hildon_app_menu_append (HildonAppMenu *menu,
122 HildonAppMenuPrivate *priv;
125 g_return_if_fail (HILDON_IS_APP_MENU (menu));
126 g_return_if_fail (GTK_IS_BUTTON (item));
128 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
130 /* Calculate the row and column number */
131 col = priv->nitems % 2;
132 row = (priv->nitems - col) / 2;
135 /* GtkTable already calls gtk_table_resize() if necessary */
136 gtk_table_attach_defaults (priv->table, GTK_WIDGET (item), col, col + 1, row, row + 1);
138 /* Close the menu when the button is clicked */
139 g_signal_connect_swapped (item, "clicked", G_CALLBACK (gtk_widget_hide), menu);
141 gtk_widget_show (GTK_WIDGET (item));
145 * hildon_app_menu_add_filter
146 * @menu : A #HildonAppMenu
147 * @filter : A #GtkButton to add to the #HildonAppMenu.
149 * Adds the @filter to @menu.
152 hildon_app_menu_add_filter (HildonAppMenu *menu,
155 HildonAppMenuPrivate *priv;
157 g_return_if_fail (HILDON_IS_APP_MENU (menu));
158 g_return_if_fail (GTK_IS_BUTTON (filter));
160 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
162 /* Pack the filter in the group and set its size */
163 gtk_box_pack_start (GTK_BOX (priv->filters_hbox), GTK_WIDGET (filter), TRUE, TRUE, 0);
165 /* Close the menu when the button is clicked */
166 g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
168 gtk_widget_show (GTK_WIDGET (filter));
172 hildon_app_menu_map (GtkWidget *widget)
174 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
176 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
178 /* Grab pointer and keyboard */
179 if (priv->transfer_window == NULL) {
180 gboolean has_grab = FALSE;
182 priv->transfer_window = grab_transfer_window_get (widget);
184 if (gdk_pointer_grab (priv->transfer_window, TRUE,
185 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
186 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
187 GDK_POINTER_MOTION_MASK, NULL, NULL,
188 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
189 if (gdk_keyboard_grab (priv->transfer_window, TRUE,
190 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
193 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
199 gtk_grab_add (widget);
201 gdk_window_destroy (priv->transfer_window);
202 priv->transfer_window = NULL;
208 hildon_app_menu_unmap (GtkWidget *widget)
210 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
212 /* Remove the grab */
213 if (priv->transfer_window != NULL) {
214 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
216 gtk_grab_remove (widget);
218 gdk_window_destroy (priv->transfer_window);
219 priv->transfer_window = NULL;
222 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unmap (widget);
226 hildon_app_menu_button_press (GtkWidget *widget,
227 GdkEventButton *event)
230 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
232 gdk_window_get_position (widget->window, &x, &y);
234 /* Whether the button has been pressed outside the widget */
235 priv->pressed_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
236 event->y_root < y || event->y_root > y + widget->allocation.height);
238 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event) {
239 return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event (widget, event);
246 hildon_app_menu_button_release (GtkWidget *widget,
247 GdkEventButton *event)
249 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
251 if (priv->pressed_outside) {
253 gboolean released_outside;
255 gdk_window_get_position (widget->window, &x, &y);
257 /* Whether the button has been released outside the widget */
258 released_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
259 event->y_root < y || event->y_root > y + widget->allocation.height);
261 if (released_outside) {
262 gtk_widget_hide (widget);
265 priv->pressed_outside = FALSE; /* Always reset pressed_outside to FALSE */
268 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event) {
269 return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event (widget, event);
275 /* Grab transfer window (based on the one from GtkMenu) */
277 grab_transfer_window_get (GtkWidget *widget)
280 GdkWindowAttr attributes;
281 gint attributes_mask;
285 attributes.width = 10;
286 attributes.height = 10;
287 attributes.window_type = GDK_WINDOW_TEMP;
288 attributes.wclass = GDK_INPUT_ONLY;
289 attributes.override_redirect = TRUE;
290 attributes.event_mask = 0;
292 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
294 window = gdk_window_new (gtk_widget_get_root_window (widget),
295 &attributes, attributes_mask);
296 gdk_window_set_user_data (window, widget);
298 gdk_window_show (window);
304 hildon_app_menu_realize (GtkWidget *widget)
308 const gchar *notification_type = "_HILDON_WM_WINDOW_TYPE_APP_MENU";
310 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
312 gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
314 display = gdk_drawable_get_display (widget->window);
315 atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE");
316 XChangeProperty (GDK_WINDOW_XDISPLAY (widget->window), GDK_WINDOW_XID (widget->window),
317 atom, XA_STRING, 8, PropModeReplace, (guchar *) notification_type,
318 strlen (notification_type));
322 hildon_app_menu_style_set (GtkWidget *widget,
323 GtkStyle *previous_style)
327 guint horizontal_spacing, vertical_spacing, inner_border, external_border;
328 HildonAppMenuPrivate *priv;
330 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set)
331 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->style_set (widget, previous_style);
333 priv = HILDON_APP_MENU_GET_PRIVATE (widget);
335 gtk_widget_style_get (widget,
336 "horizontal-spacing", &horizontal_spacing,
337 "vertical-spacing", &vertical_spacing,
338 "inner-border", &inner_border,
339 "external-border", &external_border,
343 gtk_table_set_row_spacings (priv->table, vertical_spacing);
344 gtk_table_set_col_spacings (priv->table, horizontal_spacing);
345 gtk_box_set_spacing (priv->vbox, vertical_spacing);
347 /* Set inner border */
348 gtk_container_set_border_width (GTK_CONTAINER (widget), inner_border);
350 /* Set default size */
351 screen = gtk_widget_get_screen (widget);
352 width = gdk_screen_get_width (screen) - external_border * 2;
353 gtk_window_set_default_size (GTK_WINDOW (widget), width, -1);
357 hildon_app_menu_init (HildonAppMenu *menu)
359 GtkWidget *alignment;
360 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
362 /* Initialize private variables */
363 priv->filters_hbox = GTK_BOX (gtk_hbox_new (TRUE, 0));
364 priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 0));
365 priv->table = GTK_TABLE (gtk_table_new (1, 2, TRUE));
367 priv->transfer_window = NULL;
368 priv->pressed_outside = FALSE;
370 /* Align the filters to the center */
371 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
372 gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
374 /* Pack everything */
375 gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
376 gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
377 gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
379 gtk_window_set_modal (GTK_WINDOW (menu), TRUE);
381 gtk_widget_show_all (GTK_WIDGET (priv->vbox));
385 hildon_app_menu_finalize (GObject *object)
387 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
389 if (priv->transfer_window)
390 gdk_window_destroy (priv->transfer_window);
392 g_signal_handlers_destroy (object);
393 G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
397 hildon_app_menu_class_init (HildonAppMenuClass *klass)
399 GObjectClass *gobject_class = (GObjectClass *)klass;
400 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
402 gobject_class->finalize = hildon_app_menu_finalize;
403 widget_class->map = hildon_app_menu_map;
404 widget_class->unmap = hildon_app_menu_unmap;
405 widget_class->realize = hildon_app_menu_realize;
406 widget_class->button_press_event = hildon_app_menu_button_press;
407 widget_class->button_release_event = hildon_app_menu_button_release;
408 widget_class->style_set = hildon_app_menu_style_set;
410 g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
412 gtk_widget_class_install_style_property (
415 "horizontal-spacing",
416 "Horizontal spacing on menu items",
417 "Horizontal spacing between each menu item. Does not apply to filter buttons.",
421 gtk_widget_class_install_style_property (
425 "Vertical spacing on menu items",
426 "Vertical spacing between each menu item. Does not apply to filter buttons.",
430 gtk_widget_class_install_style_property (
434 "Border between menu edges and buttons",
435 "Border between menu edges and buttons",
439 gtk_widget_class_install_style_property (
443 "Border between menu and screen edges",
444 "Border between the right and left edges of the menu and the screen edges",