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
26static GtkWidget *toplevel, *button, *box;
27static GtkWidget *sel_image, *sel_name;
28
29static void
30combo_changed_cb (GtkAppChooserButton *chooser_button,
31 gpointer user_data)
32{
33 GAppInfo *app_info;
34
35 app_info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (chooser_button));
36
37 if (app_info == NULL)
38 return;
39
40 gtk_image_set_from_gicon (GTK_IMAGE (sel_image), icon: g_app_info_get_icon (appinfo: app_info));
41 gtk_label_set_text (GTK_LABEL (sel_name), str: g_app_info_get_display_name (appinfo: app_info));
42
43 g_object_unref (object: app_info);
44}
45
46static void
47special_item_activated_cb (GtkAppChooserButton *b,
48 const char *item_name,
49 gpointer user_data)
50{
51 gtk_image_set_from_gicon (GTK_IMAGE (sel_image), icon: g_themed_icon_new (iconname: "face-smile"));
52 gtk_label_set_text (GTK_LABEL (sel_name), str: "Special Item");
53}
54
55static void
56action_cb (GtkAppChooserButton *b,
57 const char *item_name,
58 gpointer user_data)
59{
60 g_print (format: "Activated custom item %s\n", item_name);
61}
62
63static void
64quit_cb (GtkWidget *widget,
65 gpointer data)
66{
67 gboolean *done = data;
68
69 *done = TRUE;
70
71 g_main_context_wakeup (NULL);
72}
73
74int
75main (int argc,
76 char **argv)
77{
78 GtkWidget *w;
79 gboolean done = FALSE;
80
81 gtk_init ();
82
83 toplevel = gtk_window_new ();
84
85 box = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 6);
86 gtk_widget_set_margin_top (widget: box, margin: 12);
87 gtk_widget_set_margin_bottom (widget: box, margin: 12);
88 gtk_widget_set_margin_start (widget: box, margin: 12);
89 gtk_widget_set_margin_end (widget: box, margin: 12);
90 gtk_window_set_child (GTK_WINDOW (toplevel), child: box);
91
92 button = gtk_app_chooser_button_new (content_type: "image/jpeg");
93 gtk_widget_set_vexpand (widget: button, TRUE);
94 gtk_box_append (GTK_BOX (box), child: button);
95
96 g_signal_connect (button, "changed",
97 G_CALLBACK (combo_changed_cb), NULL);
98
99 w = gtk_label_new (NULL);
100 gtk_label_set_markup (GTK_LABEL (w), str: "<b>Selected app info</b>");
101 gtk_widget_set_vexpand (widget: w, TRUE);
102 gtk_box_append (GTK_BOX (box), child: w);
103
104 w = gtk_box_new (orientation: GTK_ORIENTATION_HORIZONTAL, spacing: 6);
105 gtk_widget_set_vexpand (widget: w, TRUE);
106 gtk_box_append (GTK_BOX (box), child: w);
107
108 sel_image = gtk_image_new ();
109 gtk_widget_set_hexpand (widget: sel_image, TRUE);
110 gtk_box_append (GTK_BOX (w), child: sel_image);
111 sel_name = gtk_label_new (NULL);
112 gtk_widget_set_hexpand (widget: sel_name, TRUE);
113 gtk_box_append (GTK_BOX (w), child: sel_name);
114
115 gtk_app_chooser_button_set_heading (GTK_APP_CHOOSER_BUTTON (button), heading: "Choose one, <i>not</i> two");
116 gtk_app_chooser_button_append_separator (GTK_APP_CHOOSER_BUTTON (button));
117 gtk_app_chooser_button_append_custom_item (GTK_APP_CHOOSER_BUTTON (button),
118 CUSTOM_ITEM,
119 label: "Hey, I'm special!",
120 icon: g_themed_icon_new (iconname: "face-smile"));
121
122 /* this one will trigger a warning, and will not be added */
123 gtk_app_chooser_button_append_custom_item (GTK_APP_CHOOSER_BUTTON (button),
124 CUSTOM_ITEM,
125 label: "Hey, I'm fake!",
126 icon: g_themed_icon_new (iconname: "face-evil"));
127
128 gtk_app_chooser_button_set_show_dialog_item (GTK_APP_CHOOSER_BUTTON (button),
129 TRUE);
130 gtk_app_chooser_button_set_show_default_item (GTK_APP_CHOOSER_BUTTON (button),
131 TRUE);
132
133 /* connect to the detailed signal */
134 g_signal_connect (button, "custom-item-activated::" CUSTOM_ITEM,
135 G_CALLBACK (special_item_activated_cb), NULL);
136
137 /* connect to the generic signal too */
138 g_signal_connect (button, "custom-item-activated",
139 G_CALLBACK (action_cb), NULL);
140
141 /* test refresh on a combo */
142 gtk_app_chooser_refresh (GTK_APP_CHOOSER (button));
143
144#if 0
145 gtk_app_chooser_button_set_active_custom_item (GTK_APP_CHOOSER_BUTTON (button),
146 CUSTOM_ITEM);
147#endif
148 gtk_widget_show (widget: toplevel);
149
150 g_signal_connect (toplevel, "destroy", G_CALLBACK (quit_cb), &done);
151
152 while (!done)
153 g_main_context_iteration (NULL, TRUE);
154
155 return EXIT_SUCCESS;
156}
157

source code of gtk/tests/testappchooserbutton.c