0c84211cb0f37f431cd1a523482e7c5d8fa9f3fc
[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 filter buttons
31  * (#GtkToggleButton or #GtkRadioButton), which can be grouped.
32  *
33  * <example>
34  * <title>Creating a HildonAppMenu</title>
35  * <programlisting>
36  * HildonAppMenu *menu;
37  * GtkWidget *button;
38  * GtkWidget *filter;
39  * GtkWidget *filtergroup;
40  * <!-- -->
41  * menu = HILDON_APP_MENU (hildon_app_menu_new ());
42  * <!-- -->
43  * // Create a button and add it to the menu
44  * button = gtk_button_new_with_label ("Menu command one");
45  * g_signal_connect (button, "clicked", G_CALLBACK (button_one_clicked), userdata);
46  * hildon_app_menu_append (menu, GTK_BUTTON (button));
47  * // Another button
48  * button = gtk_button_new_with_label ("Menu command two");
49  * g_signal_connect (button, "clicked", G_CALLBACK (button_two_clicked), userdata);
50  * hildon_app_menu_append (menu, GTK_BUTTON (button));
51  * <!-- -->
52  * // Create a filter and add it to the menu
53  * filter = gtk_toggle_button_new_with_label ("Filter one");
54  * g_signal_connect (filter, "clicked", G_CALLBACK (filter_one_clicked), userdata);
55  * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter), NULL);
56  * <!-- -->
57  * // Create another filter and add it to a new filter group
58  * filter = gtk_radio_button_new_with_label (NULL, "Filter two");
59  * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
60  * g_signal_connect (filter, "clicked", G_CALLBACK (filter_two_clicked), userdata);
61  * filtergroup = hildon_app_menu_add_filter (menu, GTK_BUTTON (filter), NULL);
62  * // Add a new filter to the same filter group
63  * filter = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (filter), "Filter three");
64  * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (filter), FALSE);
65  * g_signal_connect (filter, "clicked", G_CALLBACK (filter_three_clicked), userdata);
66  * hildon_app_menu_add_filter (menu, GTK_BUTTON (filter), filtergroup);
67  * <!-- -->
68  * // Pop the menu up
69  * hildon_app_menu_popup (menu);
70  * </programlisting>
71  * </example>
72  *
73  */
74
75 #include "hildon-app-menu.h"
76 #include "hildon-app-menu-private.h"
77
78 static GdkWindow *
79 grab_transfer_window_get                        (GtkWidget *widget);
80
81 G_DEFINE_TYPE (HildonAppMenu, hildon_app_menu, GTK_TYPE_WINDOW);
82
83 /**
84  * hildon_app_menu_new:
85  *
86  * Creates a new HildonAppMenu.
87  *
88  * Return value: A @HildonAppMenu.
89  **/
90 GtkWidget *
91 hildon_app_menu_new                             (void)
92 {
93     GtkWidget *menu = g_object_new (HILDON_TYPE_APP_MENU, NULL);
94     return menu;
95 }
96
97 /**
98  * hildon_app_menu_append
99  * @menu : A @HildonAppMenu
100  * @item : A @GtkButton to add to the HildonAppMenu
101  *
102  * Adds the @item to the @HildonAppMenu
103  */
104 void
105 hildon_app_menu_append                          (HildonAppMenu *menu,
106                                                  GtkButton *item)
107 {
108     HildonAppMenuPrivate *priv;
109     int row, col;
110
111     g_return_if_fail (HILDON_IS_APP_MENU (menu));
112     g_return_if_fail (GTK_IS_BUTTON (item));
113
114     priv = HILDON_APP_MENU_GET_PRIVATE(menu);
115
116     /* Calculate the row and column number */
117     col = priv->nitems % 2;
118     row = (priv->nitems - col) / 2;
119     priv->nitems++;
120
121     /* GtkTable already calls gtk_table_resize() if necessary */
122     gtk_table_attach_defaults (priv->table, GTK_WIDGET (item), col, col + 1, row, row + 1);
123
124     /* Close the menu when the button is clicked */
125     g_signal_connect_swapped (item, "clicked", G_CALLBACK (gtk_widget_hide), menu);
126
127     gtk_widget_show (GTK_WIDGET (item));
128 }
129
130 /**
131  * hildon_app_menu_add_filter
132  * @menu : A @HildonAppMenu
133  * @filter : A @GtkButton to add to the HildonAppMenu
134  * @group : An existing filter group, or %NULL to create a new one
135  *
136  * Adds the @filter to the @HildonAppMenu, to the group specified by @group
137  *
138  * Return value: The filter group where the filter has been added
139  */
140 GtkWidget *
141 hildon_app_menu_add_filter                      (HildonAppMenu *menu,
142                                                  GtkButton *filter,
143                                                  GtkWidget *group)
144 {
145     HildonAppMenuPrivate *priv;
146
147     g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
148     g_return_val_if_fail (GTK_IS_BUTTON (filter), NULL);
149     g_return_val_if_fail (!group || GTK_IS_BOX (group), NULL);
150
151     priv = HILDON_APP_MENU_GET_PRIVATE(menu);
152
153     /* Create a new group if needed */
154     if (!group) {
155         group = gtk_hbox_new (TRUE, 0);
156         gtk_box_pack_start (priv->filters_hbox, group, TRUE, TRUE, 0);
157         gtk_widget_show (group);
158     }
159
160     /* Pack the filter in the group and set its size */
161     gtk_box_pack_start (GTK_BOX (group), GTK_WIDGET (filter), TRUE, TRUE, 0);
162     gtk_size_group_add_widget (priv->sizegroup, GTK_WIDGET (filter));
163
164     /* Close the menu when the button is clicked */
165     g_signal_connect_swapped (filter, "clicked", G_CALLBACK (gtk_widget_hide), menu);
166
167     gtk_widget_show (GTK_WIDGET (filter));
168
169     return group;
170 }
171
172 /**
173  * hildon_app_menu_get_group_from_filter
174  * @menu : A @HildonAppMenu
175  * @filter : A @GtkButton previously added to the menu
176  *
177  * Gets the filter group from a @filter previously added to a @HildonAppMenu
178  *
179  * Return value: The group where the @filter is in, or %NULL
180  */
181 GtkWidget *
182 hildon_app_menu_get_group_from_filter           (HildonAppMenu *menu,
183                                                  GtkButton *filter)
184 {
185     HildonAppMenuPrivate *priv;
186     GList *grouplist;
187     GtkWidget *result = NULL;
188
189     g_return_val_if_fail (HILDON_IS_APP_MENU (menu), NULL);
190     g_return_val_if_fail (GTK_IS_BUTTON (filter), NULL);
191
192     priv = HILDON_APP_MENU_GET_PRIVATE(menu);
193
194     /* Get the list of filter groups */
195     grouplist = gtk_container_get_children (GTK_CONTAINER (priv->filters_hbox));
196
197     for (; grouplist != NULL && !result; grouplist = grouplist->next) {
198
199         GtkBox *group = GTK_BOX (grouplist->data);
200         GList *items = gtk_container_get_children (GTK_CONTAINER (group));
201
202         /* Look for the filter inside each filter group */
203         for (; items != NULL && !result; items = items->next) {
204             if (filter == items->data) {
205                 result = GTK_WIDGET (group);
206             }
207         }
208         g_list_free (items);
209
210     }
211     g_list_free (grouplist);
212
213     if (!result)
214         g_critical("Filter not found in hildon app menu!");
215
216     return result;
217 }
218
219 /**
220  * hildon_app_menu_popup
221  * @menu : A @HildonAppMenu
222  *
223  * Displays the @HildonAppMenu on top of the screen
224  */
225 void
226 hildon_app_menu_popup                           (HildonAppMenu *menu)
227 {
228     g_return_if_fail (HILDON_IS_APP_MENU (menu));
229     int x, xpos;
230     GtkRequisition req;
231     GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (menu));
232
233     /* Position the menu in the top center of the screen */
234     gtk_window_get_default_size (GTK_WINDOW (menu), &x, NULL);
235     gtk_widget_size_request (GTK_WIDGET (menu), &req);
236     xpos = (gdk_screen_get_width (screen) - MAX(x, req.width)) / 2;
237     gtk_window_move (GTK_WINDOW (menu), xpos, 0);
238
239     gtk_widget_show (GTK_WIDGET (menu));
240 }
241
242 static void
243 hildon_app_menu_map                             (GtkWidget *widget)
244 {
245     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
246
247     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->map (widget);
248
249     /* Grab pointer and keyboard */
250     if (priv->transfer_window == NULL) {
251         priv->transfer_window = grab_transfer_window_get (widget);
252         gdk_pointer_grab (
253             priv->transfer_window, TRUE,
254             GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
255             GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
256             GDK_POINTER_MOTION_MASK, NULL, NULL, GDK_CURRENT_TIME);
257         gdk_keyboard_grab (priv->transfer_window, TRUE, GDK_CURRENT_TIME);
258         gtk_grab_add (widget);
259     }
260 }
261
262 static void
263 hildon_app_menu_unmap                           (GtkWidget *widget)
264 {
265     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
266
267     if (priv->transfer_window != NULL) {
268         /* Remove the grab */
269         gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
270                                     GDK_CURRENT_TIME);
271         gtk_grab_remove (widget);
272
273         /* Destroy the transfer window */
274         gdk_window_destroy (priv->transfer_window);
275         priv->transfer_window = NULL;
276     }
277
278     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->unmap (widget);
279 }
280
281 static gboolean
282 hildon_app_menu_button_release                  (GtkWidget *widget,
283                                                  GdkEventButton *event)
284 {
285     int x, y;
286     gboolean released_outside;
287     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
288
289     gdk_window_get_position (widget->window, &x, &y);
290
291     /* Whether the button has been released outside the widget */
292     released_outside = (event->window != priv->transfer_window ||
293                         event->x < x || event->x > x + widget->allocation.width ||
294                         event->y < y || event->y > y + widget->allocation.height);
295
296     if (released_outside) {
297         gtk_widget_hide (widget);
298     }
299
300     if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event) {
301         return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event (widget, event);
302     } else {
303         return FALSE;
304     }
305 }
306
307 /* Grab transfer window (based on the one from GtkMenu) */
308 static GdkWindow *
309 grab_transfer_window_get                        (GtkWidget *widget)
310 {
311     GdkWindow *window;
312     GdkWindowAttr attributes;
313     gint attributes_mask;
314
315     attributes.x = 0;
316     attributes.y = 0;
317     attributes.width = 10;
318     attributes.height = 10;
319     attributes.window_type = GDK_WINDOW_TEMP;
320     attributes.wclass = GDK_INPUT_ONLY;
321     attributes.override_redirect = TRUE;
322     attributes.event_mask = 0;
323
324     attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
325
326     window = gdk_window_new (gtk_widget_get_root_window (widget),
327                                  &attributes, attributes_mask);
328     gdk_window_set_user_data (window, widget);
329
330     gdk_window_show (window);
331
332     return window;
333 }
334
335 static void
336 hildon_app_menu_realize                         (GtkWidget *widget)
337 {
338     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
339     gdk_window_set_override_redirect(widget->window, TRUE);
340 }
341
342 static void
343 hildon_app_menu_init                            (HildonAppMenu *menu)
344 {
345     GtkWidget *alignment;
346     GdkScreen *screen;
347     int width;
348     guint filter_group_spacing, horizontal_spacing, vertical_spacing,
349             external_border;
350     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
351
352     gtk_widget_style_get (GTK_WIDGET (menu),
353                           "filter-group-spacing", &filter_group_spacing,
354                           "horizontal-spacing", &horizontal_spacing,
355                           "vertical-spacing", &vertical_spacing,
356                           "external-border", &external_border,
357                           NULL);
358
359     /* Initialize private variables */
360     priv->filters_hbox = GTK_BOX (gtk_hbox_new (FALSE, filter_group_spacing));
361     priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, 10));
362     priv->table = GTK_TABLE (gtk_table_new (1, 2, TRUE));
363     priv->sizegroup = GTK_SIZE_GROUP (gtk_size_group_new (GTK_SIZE_GROUP_BOTH));
364     priv->nitems = 0;
365     priv->transfer_window = NULL;
366
367     /* Set spacing between table elements */
368     gtk_table_set_row_spacings (priv->table, vertical_spacing);
369     gtk_table_set_col_spacings (priv->table, horizontal_spacing);
370
371     /* Align the filters to the center */
372     alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
373     gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
374
375     /* Pack everything */
376     gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
377     gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
378     gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
379
380     /* Set default size */
381     screen = gtk_widget_get_screen (GTK_WIDGET (menu));
382     width = gdk_screen_get_width (screen) - external_border * 2;
383     gtk_window_set_default_size (GTK_WINDOW (menu), width, -1);
384
385     gtk_widget_show_all (GTK_WIDGET (priv->vbox));
386
387     gtk_window_set_type_hint (GTK_WINDOW (menu), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
388 }
389
390 static void
391 hildon_app_menu_finalize                        (GObject *object)
392 {
393     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
394
395     g_object_unref (priv->sizegroup);
396     if (priv->transfer_window)
397         gdk_window_destroy (priv->transfer_window);
398
399     g_signal_handlers_destroy (object);
400     G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
401 }
402
403 static void
404 hildon_app_menu_class_init                      (HildonAppMenuClass *klass)
405 {
406     GObjectClass *gobject_class = (GObjectClass *)klass;
407     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
408
409     gobject_class->finalize = hildon_app_menu_finalize;
410     widget_class->map = hildon_app_menu_map;
411     widget_class->unmap = hildon_app_menu_unmap;
412     widget_class->realize = hildon_app_menu_realize;
413     widget_class->button_release_event = hildon_app_menu_button_release;
414
415     g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
416
417     gtk_widget_class_install_style_property (
418         widget_class,
419         g_param_spec_uint (
420             "filter-group-spacing",
421             "Space between filter groups",
422             "Space in pixels between the filter groups",
423             0, G_MAXUINT, 10,
424             G_PARAM_READABLE));
425
426     gtk_widget_class_install_style_property (
427         widget_class,
428         g_param_spec_uint (
429             "horizontal-spacing",
430             "Horizontal spacing on menu items",
431             "Horizontal spacing between each menu item (but not filters)",
432             0, G_MAXUINT, 10,
433             G_PARAM_READABLE));
434
435     gtk_widget_class_install_style_property (
436         widget_class,
437         g_param_spec_uint (
438             "vertical-spacing",
439             "Vertical spacing on menu items",
440             "Vertical spacing between each menu item (but not filters)",
441             0, G_MAXUINT, 10,
442             G_PARAM_READABLE));
443
444     gtk_widget_class_install_style_property (
445         widget_class,
446         g_param_spec_uint (
447             "external-border",
448             "Border between menu and screen edges",
449             "Border between the right and left edges of the menu and the screen edges",
450             0, G_MAXUINT, 40,
451             G_PARAM_READABLE));
452 }