Codebase list xapp / debian/2.0.7-1 libxapp / xapp-icon-chooser-button.c
debian/2.0.7-1

Tree @debian/2.0.7-1 (Download .tar.gz)

xapp-icon-chooser-button.c @debian/2.0.7-1raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include <config.h>
#include "xapp-icon-chooser-button.h"
#include <glib/gi18n-lib.h>

#define XAPP_BUTTON_ICON_SIZE_DEFAULT GTK_ICON_SIZE_DIALOG

/**
 * SECTION:xapp-icon-chooser-button
 * @Short_description: A button for selecting an icon
 * @Title: XAppIconChooserButton
 *
 * The XAppIconChooserButton creates a button so that
 * the user can select an icon. When the button is clicked
 * it will open an XAppIconChooserDialog. The currently
 * selected icon will be displayed as the button image.
 */

typedef struct
{
    GtkWidget               *image;
    XAppIconChooserDialog   *dialog;
    GtkIconSize              icon_size;
    gchar                   *icon_string;
    gchar                   *category_string;
} XAppIconChooserButtonPrivate;

struct _XAppIconChooserButton
{
    GtkButton parent_instance;
};

enum
{
    PROP_0,
    PROP_ICON_SIZE,
    PROP_ICON,
    PROP_CATEGORY,
    N_PROPERTIES
};

static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };

G_DEFINE_TYPE_WITH_PRIVATE (XAppIconChooserButton, xapp_icon_chooser_button, GTK_TYPE_BUTTON)

static void
ensure_dialog (XAppIconChooserButton *button)
{
    XAppIconChooserButtonPrivate *priv;

    priv = xapp_icon_chooser_button_get_instance_private (button);

    if (priv->dialog != NULL)
    {
        return;
    }

    priv->dialog = xapp_icon_chooser_dialog_new ();
}

static void
on_clicked (GtkButton *button)
{
    XAppIconChooserButtonPrivate *priv;
    GtkResponseType               response;
    GtkWidget *toplevel;

    toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
    priv = xapp_icon_chooser_button_get_instance_private (XAPP_ICON_CHOOSER_BUTTON (button));

    ensure_dialog (XAPP_ICON_CHOOSER_BUTTON (button));

    gtk_window_set_transient_for (GTK_WINDOW (priv->dialog), GTK_WINDOW (toplevel));
    gtk_window_set_modal (GTK_WINDOW (priv->dialog), gtk_window_get_modal (GTK_WINDOW (toplevel)));

    if (priv->category_string)
    {
        response = xapp_icon_chooser_dialog_run_with_category (priv->dialog, priv->category_string);
    }
    else if (priv->icon_string)
    {
        response = xapp_icon_chooser_dialog_run_with_icon (priv->dialog, priv->icon_string);
    }
    else
    {
        response = xapp_icon_chooser_dialog_run (priv->dialog);
    }

    if (response == GTK_RESPONSE_OK)
    {
        gchar *icon;

        icon = xapp_icon_chooser_dialog_get_icon_string (priv->dialog);
        xapp_icon_chooser_button_set_icon (XAPP_ICON_CHOOSER_BUTTON (button), icon);
    }
}

void
xapp_icon_chooser_button_get_property (GObject    *object,
                                       guint       prop_id,
                                       GValue     *value,
                                       GParamSpec *pspec)
{
    XAppIconChooserButton        *button;
    XAppIconChooserButtonPrivate *priv;

    button = XAPP_ICON_CHOOSER_BUTTON (object);
    priv = xapp_icon_chooser_button_get_instance_private (button);

    switch (prop_id)
    {
        case PROP_ICON_SIZE:
            g_value_set_enum (value, priv->icon_size);
            break;
        case PROP_ICON:
            g_value_set_string (value, priv->icon_string);
            break;
        case PROP_CATEGORY:
            g_value_set_string (value, priv->category_string);
            break;
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
            break;
    }
}

void
xapp_icon_chooser_button_set_property (GObject      *object,
                                       guint         prop_id,
                                       const GValue *value,
                                       GParamSpec   *pspec)
{
    XAppIconChooserButton        *button;

    button = XAPP_ICON_CHOOSER_BUTTON (object);

    switch (prop_id)
    {
        case PROP_ICON_SIZE:
            xapp_icon_chooser_button_set_icon_size (button, g_value_get_enum (value));
            break;
        case PROP_ICON:
            xapp_icon_chooser_button_set_icon (button, g_value_get_string (value));
            break;
        case PROP_CATEGORY:
            xapp_icon_chooser_button_set_default_category (button, g_value_get_string (value));
            break;
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
            break;
    }
}

void
xapp_icon_chooser_button_dispose (GObject *object)
{
    XAppIconChooserButtonPrivate *priv;

    priv = xapp_icon_chooser_button_get_instance_private (XAPP_ICON_CHOOSER_BUTTON (object));

    g_clear_pointer (&priv->icon_string, g_free);
    g_clear_pointer (&priv->category_string, g_free);

    g_clear_object (&priv->dialog);

    G_OBJECT_CLASS (xapp_icon_chooser_button_parent_class)->dispose (object);
}

static void
xapp_icon_chooser_button_init (XAppIconChooserButton *button)
{
    XAppIconChooserButtonPrivate *priv;

    priv = xapp_icon_chooser_button_get_instance_private (button);

    priv->image = gtk_image_new_from_icon_name ("unkown", XAPP_BUTTON_ICON_SIZE_DEFAULT);
    gtk_button_set_image (GTK_BUTTON (button), priv->image);

    gtk_widget_set_hexpand (GTK_WIDGET (button), FALSE);
    gtk_widget_set_vexpand (GTK_WIDGET (button), FALSE);
    gtk_widget_set_halign (GTK_WIDGET (button), GTK_ALIGN_CENTER);
    gtk_widget_set_valign (GTK_WIDGET (button), GTK_ALIGN_CENTER);

    xapp_icon_chooser_button_set_icon_size (button, -1);

    priv->dialog = NULL;
}

static void
xapp_icon_chooser_button_class_init (XAppIconChooserButtonClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);

    object_class->get_property = xapp_icon_chooser_button_get_property;
    object_class->set_property = xapp_icon_chooser_button_set_property;
    object_class->dispose = xapp_icon_chooser_button_dispose;

    button_class->clicked = on_clicked;

    /**
     * XAppIconChooserButton:icon-size:
     *
     * The size to use when displaying the icon.
     */
    obj_properties[PROP_ICON_SIZE] =
        g_param_spec_enum ("icon-size",
                           _("Icon size"),
                           _("The preferred icon size."),
                           GTK_TYPE_ICON_SIZE,
                           GTK_ICON_SIZE_DND,
                           G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

    /**
     * XAppIconChooserButton:icon:
     *
     * The preferred size to use when looking up icons. This only works with icon names.
     * Additionally, there is no guarantee that a selected icon name will exist in a
     * particular size.
     */
    obj_properties[PROP_ICON] =
        g_param_spec_string ("icon",
                             _("Icon"),
                             _("The string representing the icon."),
                             "",
                             G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

    /**
     * XAppIconChooserButton:category:
     *
     * The category selected by default.
     */
    obj_properties[PROP_CATEGORY] =
        g_param_spec_string ("category",
                             _("Category"),
                             _("The default category."),
                             "",
                             G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

    g_object_class_install_properties (object_class, N_PROPERTIES, obj_properties);
}

/**
 * xapp_icon_chooser_button_new:
 *
 * Creates a new #XAppIconChooserButton and sets its icon to @icon.
 *
 * Returns: a newly created #XAppIconChooserButton
 */
XAppIconChooserButton *
xapp_icon_chooser_button_new (void)
{
    return g_object_new (XAPP_TYPE_ICON_CHOOSER_BUTTON, NULL);
}

/**
 * xapp_icon_chooser_button_new_with_size:
 * @icon_size: the size of icon to use in the button, or NULL to use the default value.
 *
 * Creates a new #XAppIconChooserButton, and sets the sizes of the button image and the icons in
 * the dialog. Note that xapp_icon_chooser_button_new_with_size (NULL, NULL) is the same as calling
 * xapp_icon_chooser_button_new ().
 *
 * Returns: a newly created #XAppIconChooserButton
 */
XAppIconChooserButton *
xapp_icon_chooser_button_new_with_size (GtkIconSize   icon_size)
{
    XAppIconChooserButton *button;

    button = g_object_new (XAPP_TYPE_ICON_CHOOSER_BUTTON, NULL);

    xapp_icon_chooser_button_set_icon_size (button, icon_size);

    return button;
}

/**
 * xapp_icon_chooser_button_set_icon_size:
 * @button: a #XAppIconChooserButton
 * @icon_size: the size of icon to use in the button, or -1 to use the default value.
 *
 * Sets the icon size used in the button.
 */
void
xapp_icon_chooser_button_set_icon_size (XAppIconChooserButton *button,
                                        GtkIconSize            icon_size)
{
    XAppIconChooserButtonPrivate *priv;
    gint width, height;
    gchar *icon;

    priv = xapp_icon_chooser_button_get_instance_private (button);

    if (icon_size == -1)
    {
        priv->icon_size = XAPP_BUTTON_ICON_SIZE_DEFAULT;
    }
    else
    {
        priv->icon_size = icon_size;
    }

    gtk_icon_size_lookup (priv->icon_size, &width, &height);
    gtk_image_set_pixel_size (GTK_IMAGE (priv->image), width);

    // We need to make sure the icon gets resized if it's a file path. Since
    // this means regenerating the pixbuf anyway, it's easier to just call
    // xapp_icon_chooser_button_set_icon, but we need to dup the string so
    // it doens't get freed before it gets used.
    icon = g_strdup(priv->icon_string);
    xapp_icon_chooser_button_set_icon (button, icon);
    g_free (icon);

    g_object_notify (G_OBJECT (button), "icon-size");
}

/**
 * xapp_icon_chooser_button_get_icon:
 * @button: a #XAppIconChooserButton
 *
 * Gets the icon from the #XAppIconChooserButton.
 *
 * returns: a string representing the icon. This may be an icon name or a file path.
 */
const gchar*
xapp_icon_chooser_button_get_icon (XAppIconChooserButton *button)
{
    XAppIconChooserButtonPrivate *priv;

    priv = xapp_icon_chooser_button_get_instance_private (button);

    return priv->icon_string;
}

/**
 * xapp_icon_chooser_button_set_icon:
 * @button: a #XAppIconChooserButton
 * @icon: (nullable): a string representing the icon to be set. This may be an icon name or a file path.
 *
 * Sets the icon on the #XAppIconChooserButton.
 */
void
xapp_icon_chooser_button_set_icon (XAppIconChooserButton *button,
                                   const gchar           *icon)
{
    XAppIconChooserButtonPrivate *priv;
    const gchar                  *icon_string;

    priv = xapp_icon_chooser_button_get_instance_private (button);

    if (priv->icon_string != NULL)
    {
        g_free (priv->icon_string);
    }

    if (icon == NULL)
    {
        priv->icon_string = NULL;
        icon_string = "unkown";
    }
    else
    {
        priv->icon_string = g_strdup (icon);
        icon_string = icon;
    }

    if (g_strrstr (icon_string, "/"))
    {
        GdkPixbuf *pixbuf;
        gint width, height;

        gtk_icon_size_lookup (priv->icon_size, &width, &height);

        pixbuf = gdk_pixbuf_new_from_file_at_size (icon_string, width, height, NULL);

        gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
    }
    else
    {
        gtk_image_set_from_icon_name (GTK_IMAGE (priv->image), icon_string, priv->icon_size);
    }

    g_object_notify (G_OBJECT (button), "icon");
}

/**
 * xapp_icon_chooser_button_set_default_category:
 * @button: a #XAppIconChooserButton
 * @category: (nullable): a string representing the category selected by default.
 *
 * Sets the icon on the #XAppIconChooserButton.
 */
void
xapp_icon_chooser_button_set_default_category (XAppIconChooserButton *button,
                                               const gchar           *category)
{
    XAppIconChooserButtonPrivate *priv;

    priv = xapp_icon_chooser_button_get_instance_private (button);

    if (priv->category_string != NULL)
    {
        g_free (priv->category_string);
    }

    priv->category_string = g_strdup (category);
}

/**
 * xapp_icon_chooser_button_get_dialog:
 * @button: a #XAppIconChooserButton
 *
 * Gets a reference to the icon chooser dialog for the #XAppIconChooserButton.
 * This is useful for setting properties on the dialog.
 *
 * Returns: (transfer none): the #XAppIconChooserDialog
 */
XAppIconChooserDialog *
xapp_icon_chooser_button_get_dialog (XAppIconChooserButton *button)
{
    XAppIconChooserButtonPrivate *priv;

    ensure_dialog (button);

    priv = xapp_icon_chooser_button_get_instance_private (button);

    return priv->dialog;
}