]> Pileus Git - ~andy/gtk/blob - tests/testappchooserbutton.c
Merge branch 'master' into open-with-dialog
[~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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <stdlib.h>
22
23 #include <gtk/gtk.h>
24
25 #define CUSTOM_ITEM "custom-item"
26
27 static GtkWidget *toplevel, *combobox, *box;
28 static GtkWidget *sel_image, *sel_name;
29
30 static void
31 combo_changed_cb (GtkComboBox *cb,
32                   gpointer     user_data)
33 {
34   GAppInfo *app_info;
35
36   app_info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (cb));
37
38   if (app_info == NULL)
39     return;
40
41   gtk_image_set_from_gicon (GTK_IMAGE (sel_image), g_app_info_get_icon (app_info),
42                             GTK_ICON_SIZE_DIALOG);
43   gtk_label_set_text (GTK_LABEL (sel_name), g_app_info_get_display_name (app_info));
44
45   g_object_unref (app_info);
46 }
47
48 static void
49 special_item_activated_cb (GtkAppChooserButton *b,
50                            const gchar *item_name,
51                            gpointer user_data)
52 {
53   gtk_image_set_from_gicon (GTK_IMAGE (sel_image), g_themed_icon_new ("face-smile"),
54                             GTK_ICON_SIZE_DIALOG);
55   gtk_label_set_text (GTK_LABEL (sel_name), "Special Item");
56 }
57
58 static void
59 action_cb (GtkAppChooserButton *b,
60            const gchar *item_name,
61            gpointer user_data)
62 {
63   g_print ("Activated custom item %s\n", item_name);
64 }
65
66 int
67 main (int argc,
68       char **argv)
69 {
70   GtkWidget *w;
71
72   g_type_init ();
73   gtk_init (&argc, &argv);
74
75   toplevel = gtk_window_new (GTK_WINDOW_TOPLEVEL);
76   gtk_container_set_border_width (GTK_CONTAINER (toplevel), 12);
77
78   box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
79   gtk_container_add (GTK_CONTAINER (toplevel), box);
80
81   combobox = gtk_app_chooser_button_new ("image/jpeg");
82   gtk_box_pack_start (GTK_BOX (box), combobox, TRUE, TRUE, 0);
83
84   g_signal_connect (combobox, "changed",
85                     G_CALLBACK (combo_changed_cb), NULL);
86
87   w = gtk_label_new (NULL);
88   gtk_label_set_markup (GTK_LABEL (w), "<b>Selected app info</b>");
89   gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
90
91   w = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
92   gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
93
94   sel_image = gtk_image_new ();
95   gtk_box_pack_start (GTK_BOX (w), sel_image, TRUE, TRUE, 0);
96   sel_name = gtk_label_new (NULL);
97   gtk_box_pack_start (GTK_BOX (w), sel_name, TRUE, TRUE, 0);
98
99   gtk_app_chooser_button_append_separator (GTK_APP_CHOOSER_BUTTON (combobox));
100   gtk_app_chooser_button_append_custom_item (GTK_APP_CHOOSER_BUTTON (combobox),
101                                              CUSTOM_ITEM,
102                                              "Hey, I'm special!",
103                                              g_themed_icon_new ("face-smile"));
104
105   /* this one will trigger a warning, and will not be added */
106   gtk_app_chooser_button_append_custom_item (GTK_APP_CHOOSER_BUTTON (combobox),
107                                              CUSTOM_ITEM,
108                                              "Hey, I'm fake!",
109                                              g_themed_icon_new ("face-evil"));
110
111   gtk_app_chooser_button_set_show_dialog_item (GTK_APP_CHOOSER_BUTTON (combobox),
112                                                TRUE);
113
114   /* connect to the detailed signal */
115   g_signal_connect (combobox, "custom-item-activated::" CUSTOM_ITEM,
116                     G_CALLBACK (special_item_activated_cb), NULL);
117
118   /* connect to the generic signal too */
119   g_signal_connect (combobox, "custom-item-activated",
120                     G_CALLBACK (action_cb), NULL);
121
122   /* test refresh on a combo */
123   gtk_app_chooser_refresh (GTK_APP_CHOOSER (combobox));
124
125   gtk_widget_show_all (toplevel);
126
127   g_signal_connect (toplevel, "delete-event",
128                     G_CALLBACK (gtk_main_quit), NULL);
129
130   gtk_main ();
131
132   return EXIT_SUCCESS;
133 }