Codebase list xapp / 402874b
Add an icon chooser button. Displays an image of the icon and opens the icon chooser dialog on click. (#53) Stephen Collins authored 5 years ago Clement Lefebvre committed 5 years ago
7 changed file(s) with 385 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1616 <chapter>
1717 <title>API reference</title>
1818 <xi:include href="xml/xapp-gtk-window.xml"/>
19 <xi:include href="xml/xapp-icon-chooser-button.xml"/>
1920 <xi:include href="xml/xapp-icon-chooser-dialog.xml"/>
2021 <xi:include href="xml/xapp-kbd-layout-controller.xml"/>
2122 <xi:include href="xml/xapp-monitor-blanker.xml"/>
1010
1111 xapp_headers = [
1212 'xapp-gtk-window.h',
13 'xapp-icon-chooser-button.h',
1314 'xapp-icon-chooser-dialog.h',
1415 'xapp-kbd-layout-controller.h',
1516 'xapp-monitor-blanker.h',
2021 xapp_sources = [
2122 'xapp-glade-catalog.c',
2223 'xapp-gtk-window.c',
24 'xapp-icon-chooser-button.c',
2325 'xapp-icon-chooser-dialog.c',
2426 'xapp-kbd-layout-controller.c',
2527 'xapp-monitor-blanker.c',
11 #include <glib-object.h>
22
33 #include "xapp-gtk-window.h"
4 #include "xapp-icon-chooser-button.h"
45 #include "xapp-stack-sidebar.h"
56
67 void
1011 xapp_glade_catalog_init (const gchar *catalog_name)
1112 {
1213 g_type_ensure (XAPP_TYPE_GTK_WINDOW);
14 g_type_ensure (XAPP_TYPE_ICON_CHOOSER_BUTTON);
1315 g_type_ensure (XAPP_TYPE_STACK_SIDEBAR);
1416 }
33 <glade-widget-classes>
44 <glade-widget-class name="XAppGtkWindow" generic-name="xappgtkwindow"
55 title="XAppGtkWindow" parent="GtkWindow" />
6 <glade-widget-class name="XAppIconChooserButton" generic-name="xappiconchooserbutton"
7 title="XAppIconChooserButton" />
68 <glade-widget-class name="XAppStackSidebar" generic-name="xappstacksidebar"
79 title="XAppStackSidebar" />
810 </glade-widget-classes>
911
1012 <glade-widget-group name="xapp-widgets" title="XApp Widgets">
1113 <glade-widget-class-ref name="XAppGtkWindow" />
14 <glade-widget-class-ref name="XAppIconChooserButton" />
1215 <glade-widget-class-ref name="XAppStackSidebar" />
1316 </glade-widget-group>
1417 </glade-catalog>
0 #include <config.h>
1 #include "xapp-icon-chooser-button.h"
2 #include <glib/gi18n-lib.h>
3
4 #define XAPP_BUTTON_ICON_SIZE_DEFAULT GTK_ICON_SIZE_DIALOG
5 #define XAPP_DIALOG_ICON_SIZE_DEFAULT XAPP_ICON_SIZE_32
6
7 /**
8 * SECTION:xapp-icon-chooser-button
9 * @Short_description: A button for selecting an icon
10 * @Title: XAppIconChooserButton
11 *
12 * The XAppIconChooserButton creates a button so that
13 * the user can select an icon. When the button is clicked
14 * it will open an XAppIconChooserDialog. The currently
15 * selected icon will be displayed as the button image.
16 */
17
18 typedef struct
19 {
20 GtkWidget *image;
21 XAppIconChooserDialog *dialog;
22 GtkIconSize icon_size;
23 gchar *icon_string;
24 } XAppIconChooserButtonPrivate;
25
26 struct _XAppIconChooserButton
27 {
28 GtkButton parent_instance;
29 };
30
31 enum
32 {
33 PROP_0,
34 PROP_ICON_SIZE,
35 PROP_ICON,
36 N_PROPERTIES
37 };
38
39 static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
40
41 G_DEFINE_TYPE_WITH_PRIVATE (XAppIconChooserButton, xapp_icon_chooser_button, GTK_TYPE_BUTTON)
42
43 static void
44 on_clicked (GtkButton *button)
45 {
46 XAppIconChooserButtonPrivate *priv;
47 GtkResponseType response;
48
49 priv = xapp_icon_chooser_button_get_instance_private (XAPP_ICON_CHOOSER_BUTTON (button));
50
51 gtk_window_set_transient_for (GTK_WINDOW (priv->dialog), GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (button))));
52
53 if (priv->icon_string == NULL)
54 {
55 response = xapp_icon_chooser_dialog_run (priv->dialog);
56 }
57 else
58 {
59 response = xapp_icon_chooser_dialog_run_with_icon (priv->dialog, priv->icon_string);
60 }
61
62 if (response == GTK_RESPONSE_OK)
63 {
64 gchar *icon;
65
66 icon = xapp_icon_chooser_dialog_get_icon_string (priv->dialog);
67 xapp_icon_chooser_button_set_icon (XAPP_ICON_CHOOSER_BUTTON (button), icon);
68 }
69 }
70
71 void
72 xapp_icon_chooser_button_get_property (GObject *object,
73 guint prop_id,
74 GValue *value,
75 GParamSpec *pspec)
76 {
77 XAppIconChooserButton *button;
78 XAppIconChooserButtonPrivate *priv;
79
80 button = XAPP_ICON_CHOOSER_BUTTON (object);
81 priv = xapp_icon_chooser_button_get_instance_private (button);
82
83 switch (prop_id)
84 {
85 case PROP_ICON_SIZE:
86 g_value_set_enum (value, priv->icon_size);
87 break;
88 case PROP_ICON:
89 g_value_set_string (value, priv->icon_string);
90 break;
91 default:
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93 break;
94 }
95 }
96
97 void
98 xapp_icon_chooser_button_set_property (GObject *object,
99 guint prop_id,
100 const GValue *value,
101 GParamSpec *pspec)
102 {
103 XAppIconChooserButton *button;
104 XAppIconChooserButtonPrivate *priv;
105
106 button = XAPP_ICON_CHOOSER_BUTTON (object);
107 priv = xapp_icon_chooser_button_get_instance_private (button);
108
109 switch (prop_id)
110 {
111 case PROP_ICON_SIZE:
112 xapp_icon_chooser_button_set_icon_size (button, g_value_get_enum (value));
113 break;
114 case PROP_ICON:
115 priv->icon_string = g_strdup (g_value_get_string (value));
116 break;
117 default:
118 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119 break;
120 }
121 }
122
123 static void
124 xapp_icon_chooser_button_init (XAppIconChooserButton *button)
125 {
126 XAppIconChooserButtonPrivate *priv;
127
128 priv = xapp_icon_chooser_button_get_instance_private (button);
129
130 priv->image = gtk_image_new_from_icon_name ("unkown", XAPP_BUTTON_ICON_SIZE_DEFAULT);
131 gtk_button_set_image (GTK_BUTTON (button), priv->image);
132
133 gtk_widget_set_hexpand (GTK_WIDGET (button), FALSE);
134 gtk_widget_set_vexpand (GTK_WIDGET (button), FALSE);
135 gtk_widget_set_halign (GTK_WIDGET (button), GTK_ALIGN_CENTER);
136 gtk_widget_set_valign (GTK_WIDGET (button), GTK_ALIGN_CENTER);
137
138 xapp_icon_chooser_button_set_icon_size (button, -1);
139
140 priv->dialog = xapp_icon_chooser_dialog_new ();
141 }
142
143 static void
144 xapp_icon_chooser_button_class_init (XAppIconChooserButtonClass *klass)
145 {
146 GtkBindingSet *binding_set;
147 GObjectClass *object_class = G_OBJECT_CLASS (klass);
148 GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
149
150 object_class->get_property = xapp_icon_chooser_button_get_property;
151 object_class->set_property = xapp_icon_chooser_button_set_property;
152
153 button_class->clicked = on_clicked;
154
155 /**
156 * XAppIconChooserButton:icon-size:
157 *
158 * The size to use when displaying the icon.
159 */
160 obj_properties[PROP_ICON_SIZE] =
161 g_param_spec_enum ("icon-size",
162 _("Icon size"),
163 _("The preferred icon size."),
164 GTK_TYPE_ICON_SIZE,
165 GTK_ICON_SIZE_DND,
166 G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
167
168 /**
169 * XAppIconChooserButton:icon:
170 *
171 * The preferred size to use when looking up icons. This only works with icon names.
172 * Additionally, there is no guarantee that a selected icon name will exist in a
173 * particular size.
174 */
175 obj_properties[PROP_ICON] =
176 g_param_spec_string ("icon",
177 _("Icon"),
178 _("The string representing the icon."),
179 "",
180 G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
181
182 g_object_class_install_properties (object_class, N_PROPERTIES, obj_properties);
183 }
184
185 /**
186 * xapp_icon_chooser_button_new:
187 *
188 * Creates a new #XAppIconChooserButton and sets its icon to @icon.
189 *
190 * Returns: a newly created #XAppIconChooserButton
191 */
192 XAppIconChooserButton *
193 xapp_icon_chooser_button_new (void)
194 {
195 return g_object_new (XAPP_TYPE_ICON_CHOOSER_BUTTON, NULL);
196 }
197
198 /**
199 * xapp_icon_chooser_button_new_with_size:
200 * @icon_size: the size of icon to use in the button, or NULL to use the default value.
201 *
202 * Creates a new #XAppIconChooserButton, and sets the sizes of the button image and the icons in
203 * the dialog. Note that xapp_icon_chooser_button_new_with_size (NULL, NULL) is the same as calling
204 * xapp_icon_chooser_button_new ().
205 *
206 * Returns: a newly created #XAppIconChooserButton
207 */
208 XAppIconChooserButton *
209 xapp_icon_chooser_button_new_with_size (GtkIconSize icon_size)
210 {
211 XAppIconChooserButton *button;
212
213 button = g_object_new (XAPP_TYPE_ICON_CHOOSER_BUTTON, NULL);
214
215 xapp_icon_chooser_button_set_icon_size (button, icon_size);
216
217 return button;
218 }
219
220 /**
221 * xapp_icon_chooser_button_set_icon_size:
222 * @button: a #XAppIconChooserButton
223 * @icon_size: the size of icon to use in the button, or -1 to use the default value.
224 *
225 * Sets the icon size used in the button.
226 */
227 void
228 xapp_icon_chooser_button_set_icon_size (XAppIconChooserButton *button,
229 GtkIconSize icon_size)
230 {
231 XAppIconChooserButtonPrivate *priv;
232
233 priv = xapp_icon_chooser_button_get_instance_private (button);
234
235 if (icon_size == -1)
236 {
237 priv->icon_size = XAPP_BUTTON_ICON_SIZE_DEFAULT;
238 }
239 else
240 {
241 priv->icon_size = icon_size;
242 }
243
244 switch (priv->icon_size)
245 {
246 case GTK_ICON_SIZE_MENU:
247 case GTK_ICON_SIZE_SMALL_TOOLBAR:
248 case GTK_ICON_SIZE_BUTTON:
249 gtk_image_set_pixel_size (GTK_IMAGE (priv->image), 16);
250 break;
251 case GTK_ICON_SIZE_LARGE_TOOLBAR:
252 gtk_image_set_pixel_size (GTK_IMAGE (priv->image), 24);
253 break;
254 case GTK_ICON_SIZE_DND:
255 gtk_image_set_pixel_size (GTK_IMAGE (priv->image), 32);
256 break;
257 case GTK_ICON_SIZE_DIALOG:
258 gtk_image_set_pixel_size (GTK_IMAGE (priv->image), 48);
259 break;
260 default:
261 g_warning ("%d is not a valid icon size", priv->icon_size);
262 break;
263 }
264
265 g_object_notify (G_OBJECT (button), "icon-size");
266 }
267
268 /**
269 * xapp_icon_chooser_button_get_icon:
270 * @button: a #XAppIconChooserButton
271 *
272 * Gets the icon from the #XAppIconChooserButton.
273 *
274 * returns: a string representing the icon. This may be an icon name or a file path.
275 */
276 const gchar*
277 xapp_icon_chooser_button_get_icon (XAppIconChooserButton *button)
278 {
279 XAppIconChooserButtonPrivate *priv;
280
281 priv = xapp_icon_chooser_button_get_instance_private (button);
282
283 return priv->icon_string;
284 }
285
286 /**
287 * xapp_icon_chooser_button_set_icon:
288 * @button: a #XAppIconChooserButton
289 * @icon: (nullable): a string representing the icon to be set. This may be an icon name or a file path.
290 *
291 * Sets the icon on the #XAppIconChooserButton.
292 */
293 void
294 xapp_icon_chooser_button_set_icon (XAppIconChooserButton *button,
295 gchar *icon)
296 {
297 XAppIconChooserButtonPrivate *priv;
298 const gchar *icon_string;
299
300 priv = xapp_icon_chooser_button_get_instance_private (button);
301
302 priv->icon_string = icon;
303
304 if (icon == NULL)
305 {
306 icon_string = "unkown";
307 }
308 else
309 {
310 icon_string = icon;
311 }
312
313 if (g_strrstr (icon_string, "/"))
314 {
315 gtk_image_set_from_file (GTK_IMAGE (priv->image), icon_string);
316 }
317 else
318 {
319 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image), icon_string, priv->icon_size);
320 }
321
322 g_object_notify (G_OBJECT (button), "icon");
323 }
324
325 /**
326 * xapp_icon_chooser_button_get_dialog:
327 * @button: a #XAppIconChooserButton
328 *
329 * Gets a reference to the icon chooser dialog for the #XAppIconChooserButton.
330 * This is useful for setting properties on the dialog.
331 *
332 * Returns: the XAppIconChooserDialog
333 */
334 XAppIconChooserDialog *
335 xapp_icon_chooser_button_get_dialog (XAppIconChooserButton *button)
336 {
337 XAppIconChooserButtonPrivate *priv;
338
339 priv = xapp_icon_chooser_button_get_instance_private (button);
340
341 return priv->dialog;
342 }
0 #ifndef _XAPP_ICON_CHOOSER_BUTTON_H_
1 #define _XAPP_ICON_CHOOSER_BUTTON_H_
2
3 #include <glib-object.h>
4 #include <gtk/gtk.h>
5 #include "xapp-icon-chooser-dialog.h"
6 #include "xapp-enums.h"
7
8 G_BEGIN_DECLS
9
10 #define XAPP_TYPE_ICON_CHOOSER_BUTTON (xapp_icon_chooser_button_get_type ())
11
12 G_DECLARE_FINAL_TYPE (XAppIconChooserButton, xapp_icon_chooser_button, XAPP, ICON_CHOOSER_BUTTON, GtkButton)
13
14 XAppIconChooserButton * xapp_icon_chooser_button_new (void);
15
16 XAppIconChooserButton * xapp_icon_chooser_button_new_with_size (GtkIconSize icon_size);
17
18 void xapp_icon_chooser_button_set_icon_size (XAppIconChooserButton *button,
19 GtkIconSize icon_size);
20
21 void xapp_icon_chooser_button_set_icon (XAppIconChooserButton *button,
22 gchar *icon);
23
24 const gchar* xapp_icon_chooser_button_get_icon (XAppIconChooserButton *button);
25
26 G_END_DECLS
27
28 #endif /* _XAPP_ICON_CHOOSER_DIALOG_H_ */
2020 class GtkWindow(XApp.GtkWindow):
2121 pass
2222
23 class GtkButton(XApp.IconChooserButton):
24 pass
25
2326 class GtkBin(XApp.StackSidebar):
2427 pass
2528
2629 GtkWindow = override(GtkWindow)
30 GtkButton = override(GtkButton)
2731 GtkBin = override(GtkBin)
2832 __all__.append('GtkWindow')
33 __all__.append('GtkButton')
2934 __all__.append('GtkBin')