]> Pileus Git - ~andy/gtk/blob - tests/testappchooserbutton.c
app-chooser-button: rename GtkAppChooserComboBox -> GtkAppChooserButton
[~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 static GtkWidget *toplevel, *combobox, *box;
26 static GtkWidget *sel_image, *sel_name;
27
28 static void
29 combo_changed_cb (GtkComboBox *cb,
30                   gpointer     user_data)
31 {
32   GAppInfo *app_info;
33
34   app_info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (cb));
35
36   if (app_info == NULL)
37     return;
38
39   gtk_image_set_from_gicon (GTK_IMAGE (sel_image), g_app_info_get_icon (app_info),
40                             GTK_ICON_SIZE_DIALOG);
41   gtk_label_set_text (GTK_LABEL (sel_name), g_app_info_get_display_name (app_info));
42
43   g_object_unref (app_info);
44 }
45
46 static void
47 special_item_activated_cb (GtkAppChooserButton *b,
48                            gpointer user_data)
49 {
50   gtk_image_set_from_gicon (GTK_IMAGE (sel_image), g_themed_icon_new ("face-smile"),
51                             GTK_ICON_SIZE_DIALOG);
52   gtk_label_set_text (GTK_LABEL (sel_name), "Special Item");
53 }
54
55 int
56 main (int argc,
57       char **argv)
58 {
59   GtkWidget *w;
60
61   g_type_init ();
62   gtk_init (&argc, &argv);
63
64   toplevel = gtk_window_new (GTK_WINDOW_TOPLEVEL);
65   gtk_container_set_border_width (GTK_CONTAINER (toplevel), 12);
66
67   box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
68   gtk_container_add (GTK_CONTAINER (toplevel), box);
69
70   combobox = gtk_app_chooser_button_new ("image/jpeg");
71   gtk_box_pack_start (GTK_BOX (box), combobox, TRUE, TRUE, 0);
72
73   g_signal_connect (combobox, "changed",
74                     G_CALLBACK (combo_changed_cb), NULL);
75
76   w = gtk_label_new (NULL);
77   gtk_label_set_markup (GTK_LABEL (w), "<b>Selected app info</b>");
78   gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
79
80   w = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
81   gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
82
83   sel_image = gtk_image_new ();
84   gtk_box_pack_start (GTK_BOX (w), sel_image, TRUE, TRUE, 0);
85   sel_name = gtk_label_new (NULL);
86   gtk_box_pack_start (GTK_BOX (w), sel_name, TRUE, TRUE, 0);
87
88   gtk_app_chooser_button_append_separator (GTK_APP_CHOOSER_BUTTON (combobox));
89   gtk_app_chooser_button_append_custom_item (GTK_APP_CHOOSER_BUTTON (combobox),
90                                              "Hey, I'm special!",
91                                              g_themed_icon_new ("face-smile"),
92                                              special_item_activated_cb,
93                                              NULL);
94
95   gtk_app_chooser_button_set_show_dialog_item (GTK_APP_CHOOSER_BUTTON (combobox),
96                                                TRUE);
97
98   /* test refresh on a combo */
99   gtk_app_chooser_refresh (GTK_APP_CHOOSER (combobox));
100
101   gtk_widget_show_all (toplevel);
102
103   g_signal_connect (toplevel, "delete-event",
104                     G_CALLBACK (gtk_main_quit), NULL);
105
106   gtk_main ();
107
108   return EXIT_SUCCESS;
109 }