2009-01-20 Claudio Saavedra <csaavedra@igalia.com>
[hildon] / examples / hildon-app-menu-example.c
1 /*
2  * This file is a part of hildon examples
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * Author: Karl Lattimer <karl.lattimer@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 #include                                        <gtk/gtk.h>
26 #include                                        "hildon.h"
27
28 static void
29 menu_button_clicked                             (GtkButton *button,
30                                                  GtkLabel *label)
31 {
32     const char *buttontext = gtk_button_get_label (button);
33     char *text = g_strdup_printf("Last option selected:\n%s", buttontext);
34     gtk_label_set_text (label, text);
35     g_free (text);
36     g_debug ("Button clicked: %s", buttontext);
37 }
38
39 static HildonAppMenu *
40 create_menu                                     (GtkWidget     *label,
41                                                  GtkAccelGroup *accel)
42 {
43     GtkWidget *button;
44     HildonSizeType buttonsize = HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH;
45     HildonAppMenu *menu = HILDON_APP_MENU (hildon_app_menu_new ());
46
47     /* Options */
48     button = hildon_gtk_button_new (buttonsize);
49     gtk_button_set_label (GTK_BUTTON (button), "Menu command one");
50     g_signal_connect_after (button, "clicked", G_CALLBACK (menu_button_clicked), label);
51     hildon_app_menu_append (menu, GTK_BUTTON (button));
52     gtk_widget_show (button);
53
54     gtk_widget_add_accelerator (button, "activate", accel, GDK_r, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
55
56     button = hildon_gtk_button_new (buttonsize);
57     gtk_button_set_label (GTK_BUTTON (button), "Menu command two");
58     g_signal_connect_after (button, "clicked", G_CALLBACK (menu_button_clicked), label);
59     hildon_app_menu_append (menu, GTK_BUTTON (button));
60     gtk_widget_show (button);
61
62     button = hildon_gtk_button_new (buttonsize);
63     gtk_button_set_label (GTK_BUTTON (button), "Menu command three");
64     g_signal_connect_after (button, "clicked", G_CALLBACK (menu_button_clicked), label);
65     hildon_app_menu_append (menu, GTK_BUTTON (button));
66     gtk_widget_show (button);
67
68     button = hildon_gtk_button_new (buttonsize);
69     gtk_button_set_label (GTK_BUTTON (button), "Menu command four");
70     g_signal_connect_after (button, "clicked", G_CALLBACK (menu_button_clicked), label);
71     hildon_app_menu_append (menu, GTK_BUTTON (button));
72     gtk_widget_show (button);
73
74     button = hildon_gtk_button_new (buttonsize);
75     gtk_button_set_label (GTK_BUTTON (button), "Menu command five");
76     g_signal_connect_after (button, "clicked", G_CALLBACK (menu_button_clicked), label);
77     hildon_app_menu_append (menu, GTK_BUTTON (button));
78     gtk_widget_show (button);
79
80     /* Filters */
81     button = hildon_gtk_radio_button_new (buttonsize, NULL);
82     gtk_button_set_label (GTK_BUTTON (button), "filter one");
83     g_signal_connect_after (button, "clicked", G_CALLBACK (menu_button_clicked), label);
84     hildon_app_menu_add_filter (menu, GTK_BUTTON (button));
85     gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), FALSE);
86     gtk_widget_show (button);
87
88     button = hildon_gtk_radio_button_new_from_widget (buttonsize, GTK_RADIO_BUTTON (button));
89     gtk_button_set_label (GTK_BUTTON (button), "filter two");
90     g_signal_connect_after (button, "clicked", G_CALLBACK (menu_button_clicked), label);
91     hildon_app_menu_add_filter (menu, GTK_BUTTON (button));
92     gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), FALSE);
93     gtk_widget_show (button);
94
95     return menu;
96 }
97
98 int
99 main                                            (int argc,
100                                                  char **argv)
101 {
102     GtkWidget *win;
103     GtkWidget *label;
104     GtkWidget *label2;
105     GtkBox *vbox;
106     HildonAppMenu *menu;
107     GtkAccelGroup *accel;
108
109     hildon_gtk_init (&argc, &argv);
110
111     label = gtk_label_new ("This is an example of the\nHildonAppMenu widget.\n\n"
112                            "Click on the titlebar\nto pop up the menu.");
113     label2 = gtk_label_new ("No menu option has been selected yet.");
114
115     accel = gtk_accel_group_new ();
116
117     gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_CENTER);
118     gtk_label_set_justify (GTK_LABEL (label2), GTK_JUSTIFY_CENTER);
119
120     vbox = GTK_BOX (gtk_vbox_new (FALSE, 10));
121     win = hildon_stackable_window_new ();
122
123     menu = create_menu (label2, accel);
124
125     hildon_stackable_window_set_main_menu (HILDON_STACKABLE_WINDOW (win), menu);
126
127     gtk_window_add_accel_group (GTK_WINDOW (win), accel);
128     g_object_unref (accel);
129
130     gtk_box_pack_start (vbox, label, TRUE, TRUE, 0);
131     gtk_box_pack_start (vbox, label2, TRUE, TRUE, 0);
132
133     gtk_container_set_border_width (GTK_CONTAINER (win), 20);
134     gtk_container_add (GTK_CONTAINER (win), GTK_WIDGET (vbox));
135
136     g_signal_connect (win, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
137
138     gtk_widget_show_all (win);
139
140     gtk_main ();
141
142     return 0;
143 }