* src/hildon-app-menu-private.h * src/hildon-app-menu.c (hildon_app_menu_button_press...
[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_press                    (GtkWidget *widget,
214                                                  GdkEventButton *event)
215 {
216     int x, y;
217     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
218
219     gdk_window_get_position (widget->window, &x, &y);
220
221     /* Whether the button has been pressed outside the widget */
222     priv->pressed_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 (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event) {
226         return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_press_event (widget, event);
227     } else {
228         return FALSE;
229     }
230 }
231
232 static gboolean
233 hildon_app_menu_button_release                  (GtkWidget *widget,
234                                                  GdkEventButton *event)
235 {
236     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(widget);
237
238     if (priv->pressed_outside) {
239         int x, y;
240         gboolean released_outside;
241
242         gdk_window_get_position (widget->window, &x, &y);
243
244         /* Whether the button has been released outside the widget */
245         released_outside = (event->x_root < x || event->x_root > x + widget->allocation.width ||
246                             event->y_root < y || event->y_root > y + widget->allocation.height);
247
248         if (released_outside) {
249             gtk_widget_hide (widget);
250         }
251
252         priv->pressed_outside = FALSE; /* Always reset pressed_outside to FALSE */
253     }
254
255     if (GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event) {
256         return GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->button_release_event (widget, event);
257     } else {
258         return FALSE;
259     }
260 }
261
262 /* Grab transfer window (based on the one from GtkMenu) */
263 static GdkWindow *
264 grab_transfer_window_get                        (GtkWidget *widget)
265 {
266     GdkWindow *window;
267     GdkWindowAttr attributes;
268     gint attributes_mask;
269
270     attributes.x = 0;
271     attributes.y = 0;
272     attributes.width = 10;
273     attributes.height = 10;
274     attributes.window_type = GDK_WINDOW_TEMP;
275     attributes.wclass = GDK_INPUT_ONLY;
276     attributes.override_redirect = TRUE;
277     attributes.event_mask = 0;
278
279     attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
280
281     window = gdk_window_new (gtk_widget_get_root_window (widget),
282                                  &attributes, attributes_mask);
283     gdk_window_set_user_data (window, widget);
284
285     gdk_window_show (window);
286
287     return window;
288 }
289
290 static void
291 hildon_app_menu_realize                         (GtkWidget *widget)
292 {
293     GdkDisplay *display;
294     Atom atom;
295     const gchar *notification_type = "_HILDON_WM_WINDOW_TYPE_APP_MENU";
296
297     GTK_WIDGET_CLASS (hildon_app_menu_parent_class)->realize (widget);
298
299     gdk_window_set_decorations (widget->window, GDK_DECOR_BORDER);
300
301     display = gdk_drawable_get_display (widget->window);
302     atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_WINDOW_TYPE");
303     XChangeProperty (GDK_WINDOW_XDISPLAY (widget->window), GDK_WINDOW_XID (widget->window),
304                      atom, XA_STRING, 8, PropModeReplace, (guchar *) notification_type,
305                      strlen (notification_type));
306 }
307
308 static void
309 hildon_app_menu_init                            (HildonAppMenu *menu)
310 {
311     GtkWidget *alignment;
312     GdkScreen *screen;
313     int width;
314     guint horizontal_spacing, vertical_spacing,
315             inner_border, external_border;
316     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(menu);
317
318     gtk_widget_style_get (GTK_WIDGET (menu),
319                           "horizontal-spacing", &horizontal_spacing,
320                           "vertical-spacing", &vertical_spacing,
321                           "inner-border", &inner_border,
322                           "external-border", &external_border,
323                           NULL);
324
325     /* Initialize private variables */
326     priv->filters_hbox = GTK_BOX (gtk_hbox_new (FALSE, 0));
327     priv->vbox = GTK_BOX (gtk_vbox_new (FALSE, vertical_spacing));
328     priv->table = GTK_TABLE (gtk_table_new (1, 2, TRUE));
329     priv->sizegroup = GTK_SIZE_GROUP (gtk_size_group_new (GTK_SIZE_GROUP_BOTH));
330     priv->nitems = 0;
331     priv->transfer_window = NULL;
332     priv->pressed_outside = FALSE;
333
334     /* Set spacing between table elements */
335     gtk_table_set_row_spacings (priv->table, vertical_spacing);
336     gtk_table_set_col_spacings (priv->table, horizontal_spacing);
337
338     /* Align the filters to the center */
339     alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
340     gtk_container_add (GTK_CONTAINER (alignment), GTK_WIDGET (priv->filters_hbox));
341
342     /* Pack everything */
343     gtk_container_add (GTK_CONTAINER (menu), GTK_WIDGET (priv->vbox));
344     gtk_box_pack_start (priv->vbox, alignment, TRUE, TRUE, 0);
345     gtk_box_pack_start (priv->vbox, GTK_WIDGET (priv->table), TRUE, TRUE, 0);
346
347     /* Set default size */
348     screen = gtk_widget_get_screen (GTK_WIDGET (menu));
349     width = gdk_screen_get_width (screen) - external_border * 2;
350     gtk_window_set_default_size (GTK_WINDOW (menu), width, -1);
351
352     /* Set inner border */
353     gtk_container_set_border_width (GTK_CONTAINER (menu), inner_border);
354
355     gtk_window_set_modal (GTK_WINDOW (menu), TRUE);
356
357     gtk_widget_show_all (GTK_WIDGET (priv->vbox));
358 }
359
360 static void
361 hildon_app_menu_finalize                        (GObject *object)
362 {
363     HildonAppMenuPrivate *priv = HILDON_APP_MENU_GET_PRIVATE(object);
364
365     g_object_unref (priv->sizegroup);
366     if (priv->transfer_window)
367         gdk_window_destroy (priv->transfer_window);
368
369     g_signal_handlers_destroy (object);
370     G_OBJECT_CLASS (hildon_app_menu_parent_class)->finalize (object);
371 }
372
373 static void
374 hildon_app_menu_class_init                      (HildonAppMenuClass *klass)
375 {
376     GObjectClass *gobject_class = (GObjectClass *)klass;
377     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
378
379     gobject_class->finalize = hildon_app_menu_finalize;
380     widget_class->map = hildon_app_menu_map;
381     widget_class->unmap = hildon_app_menu_unmap;
382     widget_class->realize = hildon_app_menu_realize;
383     widget_class->button_press_event = hildon_app_menu_button_press;
384     widget_class->button_release_event = hildon_app_menu_button_release;
385
386     g_type_class_add_private (klass, sizeof (HildonAppMenuPrivate));
387
388     gtk_widget_class_install_style_property (
389         widget_class,
390         g_param_spec_uint (
391             "horizontal-spacing",
392             "Horizontal spacing on menu items",
393             "Horizontal spacing between each menu item (but not filters)",
394             0, G_MAXUINT, 16,
395             G_PARAM_READABLE));
396
397     gtk_widget_class_install_style_property (
398         widget_class,
399         g_param_spec_uint (
400             "vertical-spacing",
401             "Vertical spacing on menu items",
402             "Vertical spacing between each menu item (but not filters)",
403             0, G_MAXUINT, 16,
404             G_PARAM_READABLE));
405
406     gtk_widget_class_install_style_property (
407         widget_class,
408         g_param_spec_uint (
409             "inner-border",
410             "Border between menu edges and buttons",
411             "Border between menu edges and buttons",
412             0, G_MAXUINT, 16,
413             G_PARAM_READABLE));
414
415     gtk_widget_class_install_style_property (
416         widget_class,
417         g_param_spec_uint (
418             "external-border",
419             "Border between menu and screen edges",
420             "Border between the right and left edges of the menu and the screen edges",
421             0, G_MAXUINT, 40,
422             G_PARAM_READABLE));
423 }