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).
34 * <title>Creating a HildonAppMenu</title>
36 * HildonAppMenu *menu;
40 * menu = HILDON_APP_MENU (hildon_app_menu_new ());
42 * // Create a button and add it to the menu
43 * button = gtk_button_new_with_label ("Menu command one");
44 * g_signal_connect (button, "clicked", G_CALLBACK (button_one_clicked), userdata);
45 * hildon_app_menu_append (menu, GTK_BUTTON (button));
47 * button = gtk_button_new_with_label ("Menu command two");
48 * g_signal_connect (button, "clicked", G_CALLBACK (button_two_clicked), userdata);
49 * hildon_app_menu_append (menu, GTK_BUTTON (button));
51 * // Create a filter and add it to the menu
52 * filter = gtk_radio_button_new_with_label (NULL, "Filter one");
53 * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
54 * g_signal_connect (filter, "clicked", G_CALLBACK (filter_two_clicked), userdata);
55 * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
57 * filter = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (filter), "Filter two");
58 * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
59 * g_signal_connect (filter, "clicked", G_CALLBACK (filter_three_clicked), userdata);
60 * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter));
63 * gtk_widget_show (menu);
69 #include "hildon-app-menu.h"
70 #include "hildon-app-menu-private.h"
74 #include <X11/Xatom.h>
78 grab_transfer_window_get (GtkWidget *widget);
80 G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
83 * hildon_app_menu_new:
85 * Creates a new HildonAppMenu.
87 * Return value: A @HildonAppMenu.
90 hildon_app_menu_new (void)
92 GtkWidget *menu = g_object_new (HILDON_TYPE_APP_MENU, NULL);
97 * hildon_app_menu_append
98 * @menu : A @HildonAppMenu
99 * @item : A @GtkButton to add to the HildonAppMenu
101 * Adds the @item to the @HildonAppMenu
104 hildon_app_menu_append (HildonAppMenu *menu,
107 HildonAppMenuPrivate *priv;
110 g_return_if_fail (HILDON_IS_APP_MENU (menu));
111 g_return_if_fail (GTK_IS_BUTTON (item));
113 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
115 /* Calculate the row and column number */
116 col = priv->nitems % 2;
117 row = (priv->nitems - col) / 2;
120 /* GtkTable already calls gtk_table_resize() if necessary */
121 gtk_table_attach_defaults (priv->table, GTK_WIDGET (item), col, col + 1, row, row + 1);
123 /* Close the menu when the button is clicked */
124 g_signal_connect_swapped (item, "clicked", G_CALLBACK (gtk_widget_hide), menu);
126 gtk_widget_show (GTK_WIDGET (item));
130 * hildon_app_menu_add_filter
131 * @menu : A @HildonAppMenu
132 * @filter : A @GtkButton to add to the #HildonAppMenu.
134 * Adds the @filter to the #HildonAppMenu.
138 hildon_app_menu_add_filter (HildonAppMenu *menu,
141 HildonAppMenuPrivate *priv;
143 g_return_if_fail (HILDON_IS_APP_MENU (menu));
144 g_return_if_fail (GTK_IS_BUTTON (filter));
146 priv = HILDON_APP_MENU_GET_PRIVATE(menu);
148 /* Pack the filter in the group and set its size */
149 gtk_box_pack_start (GTK_BOX (priv->filters_hbox), GTK_WIDGET (filter), TRUE, TRUE, 0);
150 gtk_size_group_add_widget (priv->sizegroup, GTK_WIDGET (filter));
152 /* Close the menu when the button is clicked */
153 g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
155 gtk_widget_show (GTK_WIDGET (filter));
159 hildon_app_menu_map (GtkWidget *widget)
161 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
163 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
165 /* Grab pointer and keyboard */
166 if (priv->transfer_window == NULL) {
167 gboolean has_grab = FALSE;
169 priv->transfer_window = grab_transfer_window_get (widget);
171 if (gdk_pointer_grab (priv->transfer_window, TRUE,
172 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
173 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
174 GDK_POINTER_MOTION_MASK, NULL, NULL,
175 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
176 if (gdk_keyboard_grab (priv->transfer_window, TRUE,
177 GDK_CURRENT_TIME) == GDK_GRAB_SUCCESS) {
180 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
186 gtk_grab_add (widget);
188 gdk_window_destroy (priv->transfer_window);
189 priv->transfer_window = NULL;
195 hildon_app_menu_unmap (GtkWidget *widget)
197 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
199 /* Remove the grab */
200 if (priv->transfer_window != NULL) {
201 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
203 gtk_grab_remove (widget);
205 gdk_window_destroy (priv->transfer_window);
206 priv->transfer_window = NULL;
209 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unmap (widget);
213 hildon_app_menu_button_release (GtkWidget *widget,
214 GdkEventButton *event)
217 gboolean released_outside;
219 gdk_window_get_position (widget->window, &x, &y);
221 /* Whether the button has been released outside the widget */
222 released_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
223 event->y_root < y || event->y_root > y + widget->allocation.height);
225 if (released_outside) {
226 gtk_widget_hide (widget);
229 if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event) {
230 return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event (widget, event);
236 /* Grab transfer window (based on the one from GtkMenu) */
238 grab_transfer_window_get (GtkWidget *widget)
241 GdkWindowAttr attributes;
242 gint attributes_mask;
246 attributes.width = 10;
247 attributes.height = 10;
248 attributes.window_type = GDK_WINDOW_TEMP;
249 attributes.wclass = GDK_INPUT_ONLY;
250 attributes.override_redirect = TRUE;
251 attributes.event_mask = 0;
253 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
255 window = gdk_window_new (gtk_widget_get_root_window (widget),
256 &attributes, attributes_mask);
257 gdk_window_set_user_data (window, widget);
259 gdk_window_show (window);
265 hildon_app_menu_realize (GtkWidget *widget)
269 const gchar *notification_type = "_HILDON_WM_WINDOW_TYPE_APP_MENU";
271 GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
273 gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
275 display = gdk_drawable_get_display (widget->window);
276 atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE");
277 XChangeProperty (GDK_WINDOW_XDISPLAY (widget->window), GDK_WINDOW_XID (widget->window),
278 atom, XA_STRING, 8, PropModeReplace, (guchar *) notification_type,
279 strlen (notification_type));
283 hildon_app_menu_init (HildonAppMenu *menu)
285 GtkWidget *alignment;
288 guint horizontal_spacing, vertical_spacing,
289 inner_border, external_border;
290 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
292 gtk_widget_style_get (GTK_WIDGET (menu),
293 "horizontal-spacing", &horizontal_spacing,
294 "vertical-spacing", &vertical_spacing,
295 "inner-border", &inner_border,
296 "external-border", &external_border,
299 /* Initialize private variables */
300 priv->filters_hbox = GTK_BOX (gtk_hbox_new (FALSE, 0));
301 priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, vertical_spacing));
302 priv->table = GTK_TABLE (gtk_table_new (1, 2, TRUE));
303 priv->sizegroup = GTK_SIZE_GROUP (gtk_size_group_new (GTK_SIZE_GROUP_BOTH));
305 priv->transfer_window = NULL;
307 /* Set spacing between table elements */
308 gtk_table_set_row_spacings (priv->table, vertical_spacing);
309 gtk_table_set_col_spacings (priv->table, horizontal_spacing);
311 /* Align the filters to the center */
312 alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
313 gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
315 /* Pack everything */
316 gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
317 gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
318 gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
320 /* Set default size */
321 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
322 width = gdk_screen_get_width (screen) - external_border * 2;
323 gtk_window_set_default_size (GTK_WINDOW (menu), width, -1);
325 /* Set inner border */
326 gtk_container_set_border_width (GTK_CONTAINER (menu), inner_border);
328 gtk_window_set_modal (GTK_WINDOW (menu), TRUE);
330 gtk_widget_show_all (GTK_WIDGET (priv->vbox));
334 hildon_app_menu_finalize (GObject *object)
336 HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
338 g_object_unref (priv->sizegroup);
339 if (priv->transfer_window)
340 gdk_window_destroy (priv->transfer_window);
342 g_signal_handlers_destroy (object);
343 G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
347 hildon_app_menu_class_init (HildonAppMenuClass *klass)
349 GObjectClass *gobject_class = (GObjectClass *)klass;
350 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
352 gobject_class->finalize = hildon_app_menu_finalize;
353 widget_class->map = hildon_app_menu_map;
354 widget_class->unmap = hildon_app_menu_unmap;
355 widget_class->realize = hildon_app_menu_realize;
356 widget_class->button_release_event = hildon_app_menu_button_release;
358 g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
360 gtk_widget_class_install_style_property (
363 "horizontal-spacing",
364 "Horizontal spacing on menu items",
365 "Horizontal spacing between each menu item (but not filters)",
369 gtk_widget_class_install_style_property (
373 "Vertical spacing on menu items",
374 "Vertical spacing between each menu item (but not filters)",
378 gtk_widget_class_install_style_property (
382 "Border between menu edges and buttons",
383 "Border between menu edges and buttons",
387 gtk_widget_class_install_style_property (
391 "Border between menu and screen edges",
392 "Border between the right and left edges of the menu and the screen edges",