c72ad6697d48ee47608c0493592e4e50b766102a
[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-app-menu.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 button clicked:\n%s", buttontext);
34     gtk_label_set_text (label, text);
35     g_free (text);
36 }
37
38 static HildonAppMenu *
39 create_menu                                     (GtkWidget *label)
40 {
41     GtkWidget *button;
42     GtkWidget *group;
43     HildonAppMenu *menu = HILDON_APP_MENU (hildon_app_menu_new ());
44
45     /* Options */
46     button = gtk_button_new_with_label ("Menu command one");
47     g_signal_connect (button, "clicked", G_CALLBACK (menu_button_clicked), label);
48     hildon_app_menu_append (menu, GTK_BUTTON (button));
49
50     button = gtk_button_new_with_label ("Menu command two");
51     g_signal_connect (button, "clicked", G_CALLBACK (menu_button_clicked), label);
52     hildon_app_menu_append (menu, GTK_BUTTON (button));
53
54     button = gtk_button_new_with_label ("Menu command three");
55     g_signal_connect (button, "clicked", G_CALLBACK (menu_button_clicked), label);
56     hildon_app_menu_append (menu, GTK_BUTTON (button));
57
58     button = gtk_button_new_with_label ("Menu command four");
59     g_signal_connect (button, "clicked", G_CALLBACK (menu_button_clicked), label);
60     hildon_app_menu_append (menu, GTK_BUTTON (button));
61
62     button = gtk_button_new_with_label ("Menu command five");
63     g_signal_connect (button, "clicked", G_CALLBACK (menu_button_clicked), label);
64     hildon_app_menu_append (menu, GTK_BUTTON (button));
65
66     /* Filters */
67     button = gtk_toggle_button_new_with_label ("filter one");
68     g_signal_connect (button, "clicked", G_CALLBACK (menu_button_clicked), label);
69     hildon_app_menu_add_filter (menu, GTK_BUTTON (button), NULL);
70
71     button = gtk_radio_button_new_with_label (NULL, "filter two");
72     g_signal_connect (button, "clicked", G_CALLBACK (menu_button_clicked), label);
73     group = hildon_app_menu_add_filter (menu, GTK_BUTTON (button), NULL);
74     gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), FALSE);
75
76     button = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (button), "filter three");
77     g_signal_connect (button, "clicked", G_CALLBACK (menu_button_clicked), label);
78     hildon_app_menu_add_filter (menu, GTK_BUTTON (button), group);
79     gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), FALSE);
80
81     return menu;
82 }
83
84 static void
85 button_clicked                                  (GtkButton *button,
86                                                  HildonAppMenu *menu)
87 {
88     gtk_widget_show (menu);
89 }
90
91 static void
92 close_app                                       (GtkWidget *widget,
93                                                  GdkEvent  *event,
94                                                  GtkWidget *menu)
95 {
96     gtk_widget_destroy (GTK_WIDGET (menu));
97     gtk_main_quit ();
98 }
99
100 int
101 main                                            (int argc,
102                                                  char **argv)
103 {
104     GtkWidget *win;
105     GtkWidget *button;
106     GtkWidget *label;
107     GtkWidget *label2;
108     GtkBox *vbox;
109     HildonAppMenu *menu;
110
111     gtk_init (&argc, &argv);
112
113     gtk_rc_parse_string ("style \"default\" {\n"
114                          "bg[NORMAL] = \"#505050\""
115                          "}\n"
116                          "class \"HildonAppMenu\" style \"default\"\n");
117
118     button = gtk_button_new_with_label ("Press me");
119     label = gtk_label_new ("This is an example of the\nHildonAppMenu widget");
120     label2 = gtk_label_new ("No button has been clicked");
121     vbox = GTK_BOX (gtk_vbox_new (FALSE, 10));
122     win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
123
124     menu = create_menu (label2);
125
126     gtk_box_pack_start (vbox, label, TRUE, TRUE, 0);
127     gtk_box_pack_start (vbox, button, TRUE, TRUE, 0);
128     gtk_box_pack_start (vbox, label2, TRUE, TRUE, 0);
129
130     gtk_container_set_border_width (GTK_CONTAINER (win), 20);
131     gtk_container_add (GTK_CONTAINER (win), GTK_WIDGET (vbox));
132
133     g_signal_connect (button, "clicked", G_CALLBACK(button_clicked), menu);
134     g_signal_connect (win, "delete_event", G_CALLBACK(close_app), menu);
135
136     gtk_widget_show_all (win);
137
138     gtk_main ();
139
140     return 0;
141 }