e9ee90034368c923cdaf600bae129c31f2e60e40
[hildon] / src / hildon-app-menu.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Karl Lattimer <karl.lattimer@nokia.com>
7  *
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.
11  *
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.
16  *
17  */
18
19 /**
20  * SECTION:hildon-app-menu
21  * @short_description: Widget representing the application menu in the Hildon framework.
22  *
23  * The #HildonAppMenu is a GTK widget which represents an application
24  * menu in the Hildon framework.
25  *
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.
29  *
30  * Besides that, the #HildonAppMenu can contain a group of filter buttons
31  * (#GtkToggleButton or #GtkRadioButton).
32  *
33  * <example>
34  * <title>Creating a HildonAppMenu</title>
35  * <programlisting>
36  * HildonAppMenu *menu;
37  * GtkWidget *button;
38  * GtkWidget *filter;
39  * <!-- -->
40  * menu = HILDON_APP_MENU (hildon_app_menu_new ());
41  * <!-- -->
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));
46  * // Another 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));
50  * <!-- -->
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));
56  * // Add a new 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));
61  * <!-- -->
62  * // Show the menu
63  * gtk_widget_show (menu);
64  * </programlisting>
65  * </example>
66  *
67  */
68
69 #include                                        "hildon-app-menu.h"
70 #include                                        "hildon-app-menu-private.h"
71
72 #include                                        <gdk/gdkx.h>
73 #include                                        <X11/X.h>
74 #include                                        <X11/Xatom.h>
75 #include                                        <string.h>
76
77 static GdkWindow *
78 grab_transfer_window_get                        (GtkWidget *widget);
79
80 G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
81
82 /**
83  * hildon_app_menu_new:
84  *
85  * Creates a new HildonAppMenu.
86  *
87  * Return value: A @HildonAppMenu.
88  **/
89 GtkWidget *
90 hildon_app_menu_new                             (void)
91 {
92     GtkWidget *menu = g_object_new (HILDON_TYPE_APP_MENU, NULL);
93     return menu;
94 }
95
96 /**
97  * hildon_app_menu_append
98  * @menu : A @HildonAppMenu
99  * @item : A @GtkButton to add to the HildonAppMenu
100  *
101  * Adds the @item to the @HildonAppMenu
102  */
103 void
104 hildon_app_menu_append                          (HildonAppMenu *menu,
105                                                  GtkButton *item)
106 {
107     HildonAppMenuPrivate *priv;
108     int row, col;
109
110     g_return_if_fail (HILDON_IS_APP_MENU (menu));
111     g_return_if_fail (GTK_IS_BUTTON (item));
112
113     priv = HILDON_APP_MENU_GET_PRIVATE(menu);
114
115     /* Calculate the row and column number */
116     col = priv->nitems % 2;
117     row = (priv->nitems - col) / 2;
118     priv->nitems++;
119
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);
122
123     /* Close the menu when the button is clicked */
124     g_signal_connect_swapped (item, "clicked", G_CALLBACK (gtk_widget_hide), menu);
125
126     gtk_widget_show (GTK_WIDGET (item));
127 }
128
129 /**
130  * hildon_app_menu_add_filter
131  * @menu : A @HildonAppMenu
132  * @filter : A @GtkButton to add to the #HildonAppMenu.
133  *
134  * Adds the @filter to the #HildonAppMenu.
135  *
136  */
137 void
138 hildon_app_menu_add_filter                      (HildonAppMenu *menu,
139                                                  GtkButton *filter)
140 {
141     HildonAppMenuPrivate *priv;
142
143     g_return_if_fail (HILDON_IS_APP_MENU (menu));
144     g_return_if_fail (GTK_IS_BUTTON (filter));
145
146     priv = HILDON_APP_MENU_GET_PRIVATE(menu);
147
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));
151
152     /* Close the menu when the button is clicked */
153     g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
154
155     gtk_widget_show (GTK_WIDGET (filter));
156 }
157
158 static void
159 hildon_app_menu_map                             (GtkWidget *widget)
160 {
161     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
162
163     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
164
165     /* Grab pointer and keyboard */
166     if (priv->transfer_window == NULL) {
167         gboolean has_grab = FALSE;
168
169         priv->transfer_window = grab_transfer_window_get (widget);
170
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) {
178                 has_grab = TRUE;
179             } else {
180                 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
181                                             GDK_CURRENT_TIME);
182             }
183         }
184
185         if (has_grab) {
186             gtk_grab_add (widget);
187         } else {
188             gdk_window_destroy (priv->transfer_window);
189             priv->transfer_window = NULL;
190         }
191     }
192 }
193
194 static void
195 hildon_app_menu_unmap                           (GtkWidget *widget)
196 {
197     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
198
199     /* Remove the grab */
200     if (priv->transfer_window != NULL) {
201         gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
202                                     GDK_CURRENT_TIME);
203         gtk_grab_remove (widget);
204
205         gdk_window_destroy (priv->transfer_window);
206         priv->transfer_window = NULL;
207     }
208
209     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unmap (widget);
210 }
211
212 static gboolean
213 hildon_app_menu_button_release                  (GtkWidget *widget,
214                                                  GdkEventButton *event)
215 {
216     int x, y;
217     gboolean released_outside;
218
219     gdk_window_get_position (widget->window, &x, &y);
220
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);
224
225     if (released_outside) {
226         gtk_widget_hide (widget);
227     }
228
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);
231     } else {
232         return FALSE;
233     }
234 }
235
236 /* Grab transfer window (based on the one from GtkMenu) */
237 static GdkWindow *
238 grab_transfer_window_get                        (GtkWidget *widget)
239 {
240     GdkWindow *window;
241     GdkWindowAttr attributes;
242     gint attributes_mask;
243
244     attributes.x = 0;
245     attributes.y = 0;
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;
252
253     attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
254
255     window = gdk_window_new (gtk_widget_get_root_window (widget),
256                                  &attributes, attributes_mask);
257     gdk_window_set_user_data (window, widget);
258
259     gdk_window_show (window);
260
261     return window;
262 }
263
264 static void
265 hildon_app_menu_realize                         (GtkWidget *widget)
266 {
267     GdkDisplay *display;
268     Atom atom;
269     const gchar *notification_type = "_HILDON_WM_WINDOW_TYPE_APP_MENU";
270
271     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
272
273     gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
274
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));
280 }
281
282 static void
283 hildon_app_menu_init                            (HildonAppMenu *menu)
284 {
285     GtkWidget *alignment;
286     GdkScreen *screen;
287     int width;
288     guint horizontal_spacing, vertical_spacing,
289             inner_border, external_border;
290     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
291
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,
297                           NULL);
298
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));
304     priv->nitems = 0;
305     priv->transfer_window = NULL;
306
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);
310
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));
314
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);
319
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);
324
325     /* Set inner border */
326     gtk_container_set_border_width (GTK_CONTAINER (menu), inner_border);
327
328     gtk_window_set_modal (GTK_WINDOW (menu), TRUE);
329
330     gtk_widget_show_all (GTK_WIDGET (priv->vbox));
331 }
332
333 static void
334 hildon_app_menu_finalize                        (GObject *object)
335 {
336     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
337
338     g_object_unref (priv->sizegroup);
339     if (priv->transfer_window)
340         gdk_window_destroy (priv->transfer_window);
341
342     g_signal_handlers_destroy (object);
343     G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
344 }
345
346 static void
347 hildon_app_menu_class_init                      (HildonAppMenuClass *klass)
348 {
349     GObjectClass *gobject_class = (GObjectClass *)klass;
350     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
351
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;
357
358     g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
359
360     gtk_widget_class_install_style_property (
361         widget_class,
362         g_param_spec_uint (
363             "horizontal-spacing",
364             "Horizontal spacing on menu items",
365             "Horizontal spacing between each menu item (but not filters)",
366             0, G_MAXUINT, 16,
367             G_PARAM_READABLE));
368
369     gtk_widget_class_install_style_property (
370         widget_class,
371         g_param_spec_uint (
372             "vertical-spacing",
373             "Vertical spacing on menu items",
374             "Vertical spacing between each menu item (but not filters)",
375             0, G_MAXUINT, 16,
376             G_PARAM_READABLE));
377
378     gtk_widget_class_install_style_property (
379         widget_class,
380         g_param_spec_uint (
381             "inner-border",
382             "Border between menu edges and buttons",
383             "Border between menu edges and buttons",
384             0, G_MAXUINT, 16,
385             G_PARAM_READABLE));
386
387     gtk_widget_class_install_style_property (
388         widget_class,
389         g_param_spec_uint (
390             "external-border",
391             "Border between menu and screen edges",
392             "Border between the right and left edges of the menu and the screen edges",
393             0, G_MAXUINT, 40,
394             G_PARAM_READABLE));
395 }