]> Pileus Git - ~andy/gtk/blob - tests/testappchooserbutton.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testappchooserbutton.c
1 /* testappchooserbutton.c
2  * Copyright (C) 2010 Red Hat, Inc.
3  * Authors: Cosimo Cecchi
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "config.h"
20
21 #include <stdlib.h>
22 #include <gtk/gtk.h>
23
24 #define CUSTOM_ITEM "custom-item"
25
26 static GtkWidget *toplevel, *combobox, *box;
27 static GtkWidget *sel_image, *sel_name;
28
29 static void
30 combo_changed_cb (GtkComboBox *cb,
31                   gpointer     user_data)
32 {
33   GAppInfo *app_info;
34
35   app_info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (cb));
36
37   if (app_info == NULL)
38     return;
39
40   gtk_image_set_from_gicon (GTK_IMAGE (sel_image), g_app_info_get_icon (app_info),
41                             GTK_ICON_SIZE_DIALOG);
42   gtk_label_set_text (GTK_LABEL (sel_name), g_app_info_get_display_name (app_info));
43
44   g_object_unref (app_info);
45 }
46
47 static void
48 special_item_activated_cb (GtkAppChooserButton *b,
49                            const gchar *item_name,
50                            gpointer user_data)
51 {
52   gtk_image_set_from_gicon (GTK_IMAGE (sel_image), g_themed_icon_new ("face-smile"),
53                             GTK_ICON_SIZE_DIALOG);
54   gtk_label_set_text (GTK_LABEL (sel_name), "Special Item");
55 }
56
57 static void
58 action_cb (GtkAppChooserButton *b,
59            const gchar *item_name,
60            gpointer user_data)
61 {
62   g_print ("Activated custom item %s\n", item_name);
63 }
64
65 int
66 main (int argc,
67       char **argv)
68 {
69   GtkWidget *w;
70
71   gtk_init (&argc, &argv);
72
73   toplevel = gtk_window_new (GTK_WINDOW_TOPLEVEL);
74   gtk_container_set_border_width (GTK_CONTAINER (toplevel), 12);
75
76   box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
77   gtk_container_add (GTK_CONTAINER (toplevel), box);
78
79   combobox = gtk_app_chooser_button_new ("image/jpeg");
80   gtk_box_pack_start (GTK_BOX (box), combobox, TRUE, TRUE, 0);
81
82   g_signal_connect (combobox, "changed",
83                     G_CALLBACK (combo_changed_cb), NULL);
84
85   w = gtk_label_new (NULL);
86   gtk_label_set_markup (GTK_LABEL (w), "<b>Selected app info</b>");
87   gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
88
89   w = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
90   gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
91
92   sel_image = gtk_image_new ();
93   gtk_box_pack_start (GTK_BOX (w), sel_image, TRUE, TRUE, 0);
94   sel_name = gtk_label_new (NULL);
95   gtk_box_pack_start (GTK_BOX (w), sel_name, TRUE, TRUE, 0);
96
97   gtk_app_chooser_button_set_heading (GTK_APP_CHOOSER_BUTTON (combobox), "Choose one, <i>not</i> two");
98   gtk_app_chooser_button_append_separator (GTK_APP_CHOOSER_BUTTON (combobox));
99   gtk_app_chooser_button_append_custom_item (GTK_APP_CHOOSER_BUTTON (combobox),
100                                              CUSTOM_ITEM,
101                                              "Hey, I'm special!",
102                                              g_themed_icon_new ("face-smile"));
103
104   /* this one will trigger a warning, and will not be added */
105   gtk_app_chooser_button_append_custom_item (GTK_APP_CHOOSER_BUTTON (combobox),
106                                              CUSTOM_ITEM,
107                                              "Hey, I'm fake!",
108                                              g_themed_icon_new ("face-evil"));
109
110   gtk_app_chooser_button_set_show_dialog_item (GTK_APP_CHOOSER_BUTTON (combobox),
111                                                TRUE);
112   gtk_app_chooser_button_set_show_default_item (GTK_APP_CHOOSER_BUTTON (combobox),
113                                                 TRUE);
114
115   /* connect to the detailed signal */
116   g_signal_connect (combobox, "custom-item-activated::" CUSTOM_ITEM,
117                     G_CALLBACK (special_item_activated_cb), NULL);
118
119   /* connect to the generic signal too */
120   g_signal_connect (combobox, "custom-item-activated",
121                     G_CALLBACK (action_cb), NULL);
122
123   /* test refresh on a combo */
124   gtk_app_chooser_refresh (GTK_APP_CHOOSER (combobox));
125
126 #if 0
127   gtk_app_chooser_button_set_active_custom_item (GTK_APP_CHOOSER_BUTTON (combobox),
128                                                  CUSTOM_ITEM);
129 #endif
130   gtk_widget_show_all (toplevel);
131
132   g_signal_connect (toplevel, "delete-event",
133                     G_CALLBACK (gtk_main_quit), NULL);
134
135   gtk_main ();
136
137   return EXIT_SUCCESS;
138 }