summaryrefslogtreecommitdiff
path: root/plugins/gtk+/glade-eprop-enum-int.c
blob: 29ece34fc5a67e7582ffcac55f75f74d93ee0b82 (plain)
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443

#include <config.h>

#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <gtk/gtk.h>
#include <gladeui/glade.h>
#include <glib/gi18n-lib.h>

#include "glade-eprop-enum-int.h"

/* GObjectClass */
static void        glade_eprop_enum_int_init         (GladeEPropEnumInt      *eprop);
static void        glade_eprop_enum_int_class_init   (GladeEPropEnumIntClass *klass);
static void        glade_eprop_enum_int_finalize     (GObject      *object);
static void        glade_eprop_enum_int_set_property (GObject      *object,
                                                      guint         property_id,
                                                      const GValue *value,
                                                      GParamSpec   *pspec);

/* GladeEditorPropertyClass */
static void        glade_eprop_enum_int_load         (GladeEditorProperty *eprop, GladeProperty *property);
static GtkWidget * glade_eprop_enum_int_create_input (GladeEditorProperty *eprop);

typedef struct
{
  GType      type;
  GtkWidget *combo_box;
  GtkWidget *entry;

  guint      focus_out_idle;
} GladeEPropEnumIntPrivate;

enum {
  PROP_0,
  PROP_TYPE
};

enum {
  COLUMN_ENUM_TEXT,
  COLUMN_VALUE_INT,
  N_COLUMNS
};

G_DEFINE_TYPE_WITH_PRIVATE (GladeEPropEnumInt,
                            glade_eprop_enum_int,
                            GLADE_TYPE_EDITOR_PROPERTY);

static void
glade_eprop_enum_int_init (GladeEPropEnumInt *eprop)
{

}

static void
glade_eprop_enum_int_class_init (GladeEPropEnumIntClass *klass)
{
  GladeEditorPropertyClass *eprop_class  = GLADE_EDITOR_PROPERTY_CLASS (klass);
  GObjectClass             *object_class = G_OBJECT_CLASS (klass);

  eprop_class->load          = glade_eprop_enum_int_load;
  eprop_class->create_input  = glade_eprop_enum_int_create_input;

  object_class->finalize     = glade_eprop_enum_int_finalize;
  object_class->set_property = glade_eprop_enum_int_set_property;

  g_object_class_install_property
    (object_class, PROP_TYPE,
     g_param_spec_gtype ("type", _("Type"),
      _("Type"),
      G_TYPE_NONE, G_PARAM_WRITABLE|G_PARAM_CONSTRUCT_ONLY));
}

static void
glade_eprop_enum_int_load (GladeEditorProperty *eprop, GladeProperty *property)
{
  GladeEPropEnumInt        *eprop_enum = GLADE_EPROP_ENUM_INT (eprop);
  GladeEPropEnumIntPrivate *priv       = glade_eprop_enum_int_get_instance_private (eprop_enum);

  /* Chain up first */
  GLADE_EDITOR_PROPERTY_CLASS (glade_eprop_enum_int_parent_class)->load (eprop, property);

  if (property)
    {
      GEnumClass *enum_class;
      gint value;
      guint i;
      gboolean found = FALSE;

      enum_class = g_type_class_ref (priv->type);
      value = g_value_get_int (glade_property_inline_value (property));

      /*
       * If we find the value in our enum, then set the active item, otherwise
       * set the entry text
       */
      for (i = 0; i < enum_class->n_values; i++)
        {
          if (enum_class->values[i].value == value)
            {
              found = TRUE;
              break;
            }
        }

      if (found)
        {
          gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combo_box), i);
        }
      else
        {
          gchar *text = g_strdup_printf ("%d", value);
          gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
          g_free (text);
        }

      g_type_class_unref (enum_class);
    }
}


static const gchar *
string_from_value (GType etype, gint val)
{
  GEnumClass *eclass;
  const gchar *string = NULL;
  guint i;

  g_return_val_if_fail ((eclass = g_type_class_ref (etype)) != NULL, NULL);
  for (i = 0; i < eclass->n_values; i++)
    {
      if (val == eclass->values[i].value)
        {
          if (glade_type_has_displayable_values (etype))
            {
              if (!glade_displayable_value_is_disabled (etype, eclass->values[i].value_nick))
                string = glade_get_displayable_value (etype, eclass->values[i].value_nick);
            }
          else
            {
              string = eclass->values[i].value_nick;
            }

          break;
        }
    }
  g_type_class_unref (eclass);

  return string;
}

static gboolean
value_from_string (GType etype, const gchar *string, gint *value)
{
  GEnumClass *eclass;
  GEnumValue *ev;
  gchar *endptr;
  gint val = 0;
  gboolean found = FALSE;

  /* Try a number first */
  val = strtoul (string, &endptr, 0);
  if (endptr != string)
      found = TRUE;

  if (!found)
    {
      eclass = g_type_class_ref (etype);
      ev = g_enum_get_value_by_name (eclass, string);
      if (!ev)
        ev = g_enum_get_value_by_nick (eclass, string);
      
      if (ev)
        {
          val = ev->value;
          found = TRUE;
        }

      if (!found && string && string[0])
        {
          /* Try Displayables */
          string = glade_get_value_from_displayable (etype, string);
          if (string)
            {
              ev = g_enum_get_value_by_name (eclass, string);
              if (!ev)
                ev = g_enum_get_value_by_nick (eclass, string);

              if (ev)
                {
                  val = ev->value;
                  found = TRUE;
                }
            }
        }

      g_type_class_unref (eclass);
  }

  if (found)
    *value = val;

  return found;
}

static void
glade_eprop_enum_int_changed_combo (GtkWidget *combo_box, GladeEditorProperty *eprop)
{
  GladeEPropEnumInt *eprop_enum = GLADE_EPROP_ENUM_INT (eprop);
  GladeEPropEnumIntPrivate *priv = glade_eprop_enum_int_get_instance_private (eprop_enum);
  gint ival;
  GtkTreeModel *tree_model;
  GtkTreeIter iter;
  GValue val = { 0, };
  gboolean error = FALSE;

  if (glade_editor_property_loading (eprop))
    return;
  
  tree_model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));

  if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
    {
      gtk_tree_model_get (tree_model, &iter,
                          COLUMN_VALUE_INT, &ival,
                          -1);
    }
  else
    {
      const char *text = gtk_entry_get_text (GTK_ENTRY (priv->entry));

      if (!value_from_string (priv->type, text, &ival))
        error = TRUE;
    }
  
  if (error)
    {
      gtk_entry_set_icon_from_icon_name (GTK_ENTRY (priv->entry), GTK_ENTRY_ICON_SECONDARY, "dialog-warning-symbolic");
    }
  else
    {
      gtk_entry_set_icon_from_icon_name (GTK_ENTRY (priv->entry), GTK_ENTRY_ICON_SECONDARY, NULL);
    }
  
  if (!error)
    {
      g_value_init (&val, G_TYPE_INT);
      g_value_set_int (&val, ival);

      glade_editor_property_commit_no_callback (eprop, &val);
      g_value_unset (&val);
    }
}

static gchar *
glade_eprop_enum_int_format_entry_cb (GtkComboBox       *combo,
                                      const gchar       *path,
                                      GladeEPropEnumInt *eprop_enum)
{
  GladeEPropEnumIntPrivate *priv = glade_eprop_enum_int_get_instance_private (eprop_enum);
  GtkTreeIter iter;
  GtkTreeModel *model;
  gint value;
  const char *text;
  gchar *endptr;
  gboolean is_number = FALSE;
  
  model = gtk_combo_box_get_model (combo);

  /* Check if we currently have a number in the entry */
  text = gtk_entry_get_text (GTK_ENTRY (priv->entry));
  value = strtoul (text, &endptr, 0);
  if (endptr != text)
    is_number = TRUE;
  
  /* Get the selected value */
  gtk_tree_model_get_iter_from_string (model, &iter, path);
  gtk_tree_model_get (model, &iter, 
                      COLUMN_VALUE_INT, &value,
                      -1);
  
  /* If we're currently in focus, and we have a number in the entry
   * already, then we dont want to change it.
   *
   * E.g. If the user types "1" and we change it to "Pony" right away,
   *      then we ignore that the user might want to enter "10"
   *
   * After focusing out, we'll force refresh this so that we settle
   * on displaying an enum value if one is valid.
   */
  if (is_number && gtk_widget_has_focus (priv->entry))
    return g_strdup_printf ("%d", value);

  /* Now format a nice displayable value if possible
   */
  text = string_from_value (priv->type, value);
  if (text)
    return g_strdup (text);

  return g_strdup_printf ("%d", value);
}

static gboolean
glade_eprop_enum_int_focus_out_idle (gpointer user_data)
{
  GladeEPropEnumInt        *eprop_enum = GLADE_EPROP_ENUM_INT (user_data);
  GladeEPropEnumIntPrivate *priv = glade_eprop_enum_int_get_instance_private (eprop_enum);

  /* If the editor is no longer loaded with a property (i.e. the user changed
   * focus by selecting another widget), then just return.
   */
  if (glade_editor_property_get_property (GLADE_EDITOR_PROPERTY (eprop_enum)))
    {
      /* After focusing out, ensure we have a valid selection here if an entered
       * number matches an enum value. Do this by provoking the combobox to
       * format it's value.
       */
      g_signal_emit_by_name (priv->combo_box, "changed");
    }

  priv->focus_out_idle = 0;

  return FALSE;
}

static gboolean
glade_eprop_enum_int_entry_focus_out (GtkWidget *widget,
                                      GdkEvent  *event,
                                      GladeEPropEnumInt *eprop_enum)
{
  GladeEPropEnumIntPrivate *priv = glade_eprop_enum_int_get_instance_private (eprop_enum);

  /* Queue the focus out idle, we may want to reformat the entry here
   */
  if (priv->focus_out_idle == 0)
    priv->focus_out_idle = g_idle_add (glade_eprop_enum_int_focus_out_idle, eprop_enum);

  return FALSE;
}

static GtkWidget *
glade_eprop_enum_int_create_input (GladeEditorProperty *eprop)
{
  GladeEPropEnumInt *eprop_enum = GLADE_EPROP_ENUM_INT (eprop);
  GladeEPropEnumIntPrivate *priv = glade_eprop_enum_int_get_instance_private (eprop_enum);
  GtkListStore *list_store;
  GtkTreeIter iter;
  guint i;
  GEnumClass *enum_class;
  
  enum_class = g_type_class_ref (priv->type);
 
  list_store = gtk_list_store_new (N_COLUMNS,
                                   G_TYPE_STRING, /* COLUMN_ENUM_TEXT */
                                   G_TYPE_INT);   /* COLUMN_VALUE_INT */

  if (!glade_type_has_displayable_values (priv->type))
    g_warning ("No displayable value found for type %s", g_type_name (priv->type));

  gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter);
  for (i = 0; i < enum_class->n_values; i++)
    {
      if (glade_displayable_value_is_disabled (priv->type, enum_class->values[i].value_nick))
        continue;

      gtk_list_store_append (list_store, &iter);
      gtk_list_store_set (list_store, &iter,
                          COLUMN_ENUM_TEXT, string_from_value (priv->type, enum_class->values[i].value),
                          COLUMN_VALUE_INT, enum_class->values[i].value,
                          -1);
    }

  priv->combo_box = gtk_combo_box_new_with_model_and_entry (GTK_TREE_MODEL (list_store));
  priv->entry     = gtk_bin_get_child (GTK_BIN (priv->combo_box));
  gtk_widget_set_halign (priv->combo_box, GTK_ALIGN_FILL);
  gtk_widget_set_valign (priv->combo_box, GTK_ALIGN_CENTER);
  gtk_widget_set_hexpand (priv->combo_box, TRUE);
  
  gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (priv->combo_box), 0);

  g_signal_connect (G_OBJECT (priv->combo_box), "changed",
                    G_CALLBACK (glade_eprop_enum_int_changed_combo), eprop);
  g_signal_connect (G_OBJECT (priv->combo_box), "format-entry-text",
                    G_CALLBACK (glade_eprop_enum_int_format_entry_cb), eprop);

  g_signal_connect_after (G_OBJECT (priv->entry), "focus-out-event",
                          G_CALLBACK (glade_eprop_enum_int_entry_focus_out), eprop);

  
  glade_util_remove_scroll_events (priv->combo_box);

  g_type_class_unref (enum_class);
  
  return priv->combo_box;
}

static void
glade_eprop_enum_int_finalize (GObject *object)
{
  GladeEPropEnumInt        *self = GLADE_EPROP_ENUM_INT(object);
  GladeEPropEnumIntPrivate *priv = glade_eprop_enum_int_get_instance_private (self);

  /* Be safe, dont leave idles hanging around */
  if (priv->focus_out_idle != 0)
    g_source_remove (priv->focus_out_idle);
}

static void
glade_eprop_enum_int_set_property (GObject      *object,
                                   guint         property_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)
{
  GladeEPropEnumInt        *self = GLADE_EPROP_ENUM_INT(object);
  GladeEPropEnumIntPrivate *priv = glade_eprop_enum_int_get_instance_private (self);

  switch (property_id)
    {
    case PROP_TYPE :
      priv->type = g_value_get_gtype (value);
      break;
      
    default:
      /* We don't have any other property... */
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

GladeEditorProperty *
glade_eprop_enum_int_new (GladePropertyDef   *pdef,
                          GType               type,
                          gboolean            use_command)
{
  GladeEditorProperty *eprop = 
    g_object_new (GLADE_TYPE_EPROP_ENUM_INT, 
                  "property-def", pdef,
                  "use-command", use_command,
                  "type", type,
                  NULL);
  return eprop;
}