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