~ubuntu-branches/ubuntu/trusty/unity-control-center/trusty

« back to all changes in this revision

Viewing changes to panels/wacom/cc-wacom-stylus-page.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-01-08 16:29:18 UTC
  • Revision ID: package-import@ubuntu.com-20140108162918-g29dd08tr913y2qh
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright Ā© 2011 Red Hat, Inc.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 *
 
18
 * Authors: Peter Hutterer <peter.hutterer@redhat.com>
 
19
 *          Bastien Nocera <hadess@hadess.net>
 
20
 *
 
21
 */
 
22
 
 
23
#include <config.h>
 
24
 
 
25
#include <glib/gi18n.h>
 
26
#include "cc-wacom-stylus-page.h"
 
27
#include "cc-wacom-nav-button.h"
 
28
#include <gtk/gtk.h>
 
29
 
 
30
#include <string.h>
 
31
 
 
32
#define WID(x) (GtkWidget *) gtk_builder_get_object (priv->builder, x)
 
33
#define CWID(x) (GtkContainer *) gtk_builder_get_object (priv->builder, x)
 
34
 
 
35
G_DEFINE_TYPE (CcWacomStylusPage, cc_wacom_stylus_page, GTK_TYPE_BOX)
 
36
 
 
37
#define WACOM_STYLUS_PAGE_PRIVATE(o) \
 
38
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_WACOM_STYLUS_PAGE, CcWacomStylusPagePrivate))
 
39
 
 
40
struct _CcWacomStylusPagePrivate
 
41
{
 
42
        GsdWacomStylus *stylus, *eraser;
 
43
        GtkBuilder     *builder;
 
44
        GtkWidget      *nav;
 
45
        GSettings      *stylus_settings, *eraser_settings;
 
46
};
 
47
 
 
48
/* Button combo box storage columns */
 
49
enum {
 
50
        BUTTONNUMBER_COLUMN,
 
51
        BUTTONNAME_COLUMN,
 
52
        N_BUTTONCOLUMNS
 
53
};
 
54
 
 
55
/* GSettings stores pressurecurve as 4 values like the driver. We map slider
 
56
 * scale to these values given the array below. These settings were taken from
 
57
 * wacomcpl, where they've been around for years.
 
58
 */
 
59
#define N_PRESSURE_CURVES 7
 
60
static const gint32 PRESSURE_CURVES[N_PRESSURE_CURVES][4] = {
 
61
                {       0,      75,     25,     100     },      /* soft */
 
62
                {       0,      50,     50,     100     },
 
63
                {       0,      25,     75,     100     },
 
64
                {       0,      0,      100,    100     },      /* neutral */
 
65
                {       25,     0,      100,    75      },
 
66
                {       50,     0,      100,    50      },
 
67
                {       75,     0,      100,    25      }       /* firm */
 
68
};
 
69
 
 
70
static void
 
71
set_pressurecurve (GtkRange *range, GSettings *settings)
 
72
{
 
73
        gint            slider_val = gtk_range_get_value (range);
 
74
        GVariant        *values[4],
 
75
                        *array;
 
76
        int             i;
 
77
 
 
78
        for (i = 0; i < G_N_ELEMENTS (values); i++)
 
79
                values[i] = g_variant_new_int32 (PRESSURE_CURVES[slider_val][i]);
 
80
 
 
81
        array = g_variant_new_array (G_VARIANT_TYPE_INT32, values, G_N_ELEMENTS (values));
 
82
 
 
83
        g_settings_set_value (settings, "pressurecurve", array);
 
84
}
 
85
 
 
86
static void
 
87
tip_feel_value_changed_cb (GtkRange *range, gpointer user_data)
 
88
{
 
89
    set_pressurecurve (range, CC_WACOM_STYLUS_PAGE(user_data)->priv->stylus_settings);
 
90
}
 
91
 
 
92
static void
 
93
eraser_feel_value_changed_cb (GtkRange *range, gpointer user_data)
 
94
{
 
95
    set_pressurecurve (range, CC_WACOM_STYLUS_PAGE(user_data)->priv->eraser_settings);
 
96
}
 
97
 
 
98
static void
 
99
set_feel_from_gsettings (GtkAdjustment *adjustment, GSettings *settings)
 
100
{
 
101
        GVariant        *variant;
 
102
        const gint32    *values;
 
103
        gsize           nvalues;
 
104
        int             i;
 
105
 
 
106
        variant = g_settings_get_value (settings, "pressurecurve");
 
107
        values = g_variant_get_fixed_array (variant, &nvalues, sizeof (gint32));
 
108
 
 
109
        if (nvalues != 4) {
 
110
                g_warning ("Invalid pressure curve format, expected 4 values (got %"G_GSIZE_FORMAT")", nvalues);
 
111
                return;
 
112
        }
 
113
 
 
114
        for (i = 0; i < N_PRESSURE_CURVES; i++) {
 
115
                if (memcmp (PRESSURE_CURVES[i], values, sizeof (gint32) * 4) == 0) {
 
116
                        gtk_adjustment_set_value (adjustment, i);
 
117
                        break;
 
118
                }
 
119
        }
 
120
}
 
121
 
 
122
static void
 
123
set_button_mapping_from_gsettings (GtkComboBox *combo, GSettings* settings, gint current_button)
 
124
{
 
125
        GVariant        *current;
 
126
        gsize            nvalues;
 
127
        const gint      *values;
 
128
        GtkTreeModel    *model;
 
129
        GtkTreeIter      iter;
 
130
        gboolean         valid;
 
131
 
 
132
        current = g_settings_get_value (settings, "buttonmapping");
 
133
        values = g_variant_get_fixed_array (current, &nvalues, sizeof (gint32));
 
134
        model = gtk_combo_box_get_model (combo);
 
135
        valid = gtk_tree_model_get_iter_first (model, &iter);
 
136
 
 
137
        while (valid) {
 
138
                gint button;
 
139
 
 
140
                gtk_tree_model_get (model, &iter,
 
141
                                    BUTTONNUMBER_COLUMN, &button,
 
142
                                    -1);
 
143
 
 
144
                /* Currently button values match logical X buttons. If we
 
145
                 * introduce things like double-click, this code must
 
146
                 * change. Recommendation: use negative buttons numbers for
 
147
                 * special ones.
 
148
                 */
 
149
 
 
150
                /* 0 vs 1-indexed array/button numbers */
 
151
                if (button == values[current_button - 1]) {
 
152
                        gtk_combo_box_set_active_iter (combo, &iter);
 
153
                        break;
 
154
                }
 
155
 
 
156
                valid = gtk_tree_model_iter_next (model, &iter);
 
157
        }
 
158
}
 
159
 
 
160
static void
 
161
map_button (GSettings *settings, int button2, int button3)
 
162
{
 
163
        GVariant        *current; /* current mapping */
 
164
        GVariant        *array;   /* new mapping */
 
165
        GVariant        **tmp;
 
166
        gsize            nvalues;
 
167
        const gint      *values;
 
168
        gint             i;
 
169
 
 
170
        current = g_settings_get_value (settings, "buttonmapping");
 
171
        values = g_variant_get_fixed_array (current, &nvalues, sizeof (gint32));
 
172
 
 
173
        tmp = g_malloc (nvalues * sizeof (GVariant*));
 
174
        for (i = 0; i < nvalues; i++) {
 
175
                if (i == 1) /* zero indexed array vs one-indexed buttons */
 
176
                        tmp[i] = g_variant_new_int32 (button2);
 
177
                else if (i == 2)
 
178
                        tmp[i] = g_variant_new_int32 (button3);
 
179
                else
 
180
                        tmp[i] = g_variant_new_int32 (values[i]);
 
181
        }
 
182
 
 
183
        array = g_variant_new_array (G_VARIANT_TYPE_INT32, tmp, nvalues);
 
184
        g_settings_set_value (settings, "buttonmapping", array);
 
185
 
 
186
        g_free (tmp);
 
187
}
 
188
 
 
189
static void
 
190
button_changed_cb (GtkComboBox *combo, gpointer user_data)
 
191
{
 
192
        CcWacomStylusPagePrivate        *priv = CC_WACOM_STYLUS_PAGE(user_data)->priv;
 
193
        GtkTreeIter             iter;
 
194
        GtkListStore            *liststore;
 
195
        gint                    mapping_b2,
 
196
                                mapping_b3;
 
197
 
 
198
        if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (WID ("combo-bottombutton")), &iter))
 
199
                return;
 
200
 
 
201
        liststore = GTK_LIST_STORE (WID ("liststore-buttons"));
 
202
        gtk_tree_model_get (GTK_TREE_MODEL (liststore), &iter,
 
203
                            BUTTONNUMBER_COLUMN, &mapping_b2,
 
204
                            -1);
 
205
 
 
206
        if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (WID ("combo-topbutton")), &iter))
 
207
                return;
 
208
 
 
209
        gtk_tree_model_get (GTK_TREE_MODEL (liststore), &iter,
 
210
                            BUTTONNUMBER_COLUMN, &mapping_b3,
 
211
                            -1);
 
212
 
 
213
        map_button (priv->stylus_settings, mapping_b2, mapping_b3);
 
214
}
 
215
 
 
216
static void
 
217
combobox_text_cellrenderer (GtkComboBox *combo, int name_column)
 
218
{
 
219
        GtkCellRenderer *renderer;
 
220
 
 
221
        renderer = gtk_cell_renderer_text_new ();
 
222
        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
 
223
        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
 
224
                                        "text", BUTTONNAME_COLUMN, NULL);
 
225
}
 
226
 
 
227
/* Boilerplate code goes below */
 
228
 
 
229
static void
 
230
cc_wacom_stylus_page_get_property (GObject    *object,
 
231
                             guint       property_id,
 
232
                             GValue     *value,
 
233
                             GParamSpec *pspec)
 
234
{
 
235
        switch (property_id)
 
236
        {
 
237
                default:
 
238
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
239
        }
 
240
}
 
241
 
 
242
static void
 
243
cc_wacom_stylus_page_set_property (GObject      *object,
 
244
                             guint         property_id,
 
245
                             const GValue *value,
 
246
                             GParamSpec   *pspec)
 
247
{
 
248
        switch (property_id)
 
249
        {
 
250
                default:
 
251
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
252
        }
 
253
}
 
254
 
 
255
static void
 
256
cc_wacom_stylus_page_dispose (GObject *object)
 
257
{
 
258
        CcWacomStylusPagePrivate *priv = CC_WACOM_STYLUS_PAGE (object)->priv;
 
259
 
 
260
        if (priv->builder) {
 
261
                g_object_unref (priv->builder);
 
262
                priv->builder = NULL;
 
263
        }
 
264
 
 
265
 
 
266
        G_OBJECT_CLASS (cc_wacom_stylus_page_parent_class)->dispose (object);
 
267
}
 
268
 
 
269
static void
 
270
cc_wacom_stylus_page_class_init (CcWacomStylusPageClass *klass)
 
271
{
 
272
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
273
 
 
274
        g_type_class_add_private (klass, sizeof (CcWacomStylusPagePrivate));
 
275
 
 
276
        object_class->get_property = cc_wacom_stylus_page_get_property;
 
277
        object_class->set_property = cc_wacom_stylus_page_set_property;
 
278
        object_class->dispose = cc_wacom_stylus_page_dispose;
 
279
}
 
280
 
 
281
static void
 
282
cc_wacom_stylus_page_init (CcWacomStylusPage *self)
 
283
{
 
284
        CcWacomStylusPagePrivate *priv;
 
285
        GError *error = NULL;
 
286
        GtkComboBox *combo;
 
287
        GtkWidget *box;
 
288
        char *objects[] = {
 
289
                "stylus-grid",
 
290
                "liststore-buttons",
 
291
                "adjustment-tip-feel",
 
292
                "adjustment-eraser-feel",
 
293
                NULL
 
294
        };
 
295
 
 
296
        priv = self->priv = WACOM_STYLUS_PAGE_PRIVATE (self);
 
297
 
 
298
        priv->builder = gtk_builder_new ();
 
299
 
 
300
        gtk_builder_add_objects_from_file (priv->builder,
 
301
                                           GNOMECC_UI_DIR "/wacom-stylus-page.ui",
 
302
                                           objects,
 
303
                                           &error);
 
304
        if (error != NULL) {
 
305
                g_warning ("Error loading UI file: %s", error->message);
 
306
                g_object_unref (priv->builder);
 
307
                g_error_free (error);
 
308
                return;
 
309
        }
 
310
 
 
311
        box = WID ("stylus-grid");
 
312
        gtk_container_add (GTK_CONTAINER (self), box);
 
313
        gtk_widget_set_vexpand (GTK_WIDGET (box), TRUE);
 
314
 
 
315
        g_signal_connect (WID ("scale-tip-feel"), "value-changed",
 
316
                          G_CALLBACK (tip_feel_value_changed_cb), self);
 
317
        g_signal_connect (WID ("scale-eraser-feel"), "value-changed",
 
318
                          G_CALLBACK (eraser_feel_value_changed_cb), self);
 
319
 
 
320
        combo = GTK_COMBO_BOX (WID ("combo-topbutton"));
 
321
        combobox_text_cellrenderer (combo, BUTTONNAME_COLUMN);
 
322
        g_signal_connect (G_OBJECT (combo), "changed",
 
323
                          G_CALLBACK (button_changed_cb), self);
 
324
 
 
325
        combo = GTK_COMBO_BOX (WID ("combo-bottombutton"));
 
326
        combobox_text_cellrenderer (combo, BUTTONNAME_COLUMN);
 
327
        g_signal_connect (G_OBJECT (combo), "changed",
 
328
                          G_CALLBACK (button_changed_cb), self);
 
329
 
 
330
        priv->nav = cc_wacom_nav_button_new ();
 
331
        gtk_widget_set_halign (priv->nav, GTK_ALIGN_END);
 
332
        gtk_widget_set_margin_left (priv->nav, 10);
 
333
        gtk_grid_attach (GTK_GRID (box), priv->nav, 1, 0, 1, 1);
 
334
}
 
335
 
 
336
static void
 
337
set_icon_name (CcWacomStylusPage *page,
 
338
               const char  *widget_name,
 
339
               const char  *icon_name)
 
340
{
 
341
        CcWacomStylusPagePrivate *priv;
 
342
        char *filename, *path;
 
343
 
 
344
        priv = page->priv;
 
345
 
 
346
        filename = g_strdup_printf ("%s.svg", icon_name);
 
347
        path = g_build_filename (GNOMECC_UI_DIR, filename, NULL);
 
348
        g_free (filename);
 
349
 
 
350
        gtk_image_set_from_file (GTK_IMAGE (WID (widget_name)), path);
 
351
        g_free (path);
 
352
}
 
353
 
 
354
/* Different types of layout for the stylus config */
 
355
enum {
 
356
        LAYOUT_NORMAL,   /* eraser, 2 buttons, tip */
 
357
        LAYOUT_INKING,   /* tip */
 
358
        LAYOUT_AIRBRUSH, /* eraser, 1 button, tip */
 
359
        LAYOUT_OTHER
 
360
};
 
361
 
 
362
static void
 
363
remove_buttons (CcWacomStylusPagePrivate *priv)
 
364
{
 
365
        gtk_widget_destroy (WID ("combo-topbutton"));
 
366
        gtk_widget_destroy (WID ("combo-bottombutton"));
 
367
        gtk_widget_destroy (WID ("label-top-button"));
 
368
        gtk_widget_destroy (WID ("label-lower-button"));
 
369
}
 
370
 
 
371
static void
 
372
remove_button (CcWacomStylusPagePrivate *priv)
 
373
{
 
374
        gtk_widget_destroy (WID ("combo-topbutton"));
 
375
        gtk_widget_destroy (WID ("label-top-button"));
 
376
        gtk_label_set_text (GTK_LABEL (WID ("label-lower-button")), _("Button"));
 
377
}
 
378
 
 
379
static void
 
380
remove_eraser (CcWacomStylusPagePrivate *priv)
 
381
{
 
382
        gtk_widget_destroy (WID ("eraser-box"));
 
383
        gtk_widget_destroy (WID ("label-eraser-feel"));
 
384
}
 
385
 
 
386
static void
 
387
update_stylus_ui (CcWacomStylusPage *page,
 
388
                  int                layout)
 
389
{
 
390
        CcWacomStylusPagePrivate *priv = page->priv;
 
391
 
 
392
        switch (layout) {
 
393
        case LAYOUT_NORMAL:
 
394
                /* easy! */
 
395
                break;
 
396
        case LAYOUT_INKING:
 
397
                remove_buttons (page->priv);
 
398
                remove_eraser (page->priv);
 
399
                gtk_container_child_set (CWID ("stylus-controls-grid"),
 
400
                                         WID ("label-tip-feel"),
 
401
                                         "top_attach", 0, NULL);
 
402
                gtk_container_child_set (CWID ("stylus-controls-grid"),
 
403
                                         WID ("box-tip-feel"),
 
404
                                         "top_attach", 0, NULL);
 
405
                break;
 
406
        case LAYOUT_AIRBRUSH:
 
407
                remove_button (page->priv);
 
408
                gtk_container_child_set (CWID ("stylus-controls-grid"),
 
409
                                         WID ("label-lower-button"),
 
410
                                         "top_attach", 1, NULL);
 
411
                gtk_container_child_set (CWID ("stylus-controls-grid"),
 
412
                                         WID ("combo-bottombutton"),
 
413
                                         "top_attach", 1, NULL);
 
414
                gtk_container_child_set (CWID ("stylus-controls-grid"),
 
415
                                         WID ("label-tip-feel"),
 
416
                                         "top_attach", 2, NULL);
 
417
                gtk_container_child_set (CWID ("stylus-controls-grid"),
 
418
                                         WID ("box-tip-feel"),
 
419
                                         "top_attach", 2, NULL);
 
420
        case LAYOUT_OTHER:
 
421
                /* We already warn about it in cc_wacom_stylus_page_new () */
 
422
                break;
 
423
        }
 
424
}
 
425
 
 
426
GtkWidget *
 
427
cc_wacom_stylus_page_new (GsdWacomStylus *stylus,
 
428
                          GsdWacomStylus *eraser)
 
429
{
 
430
        CcWacomStylusPage *page;
 
431
        CcWacomStylusPagePrivate *priv;
 
432
        int num_buttons;
 
433
        int layout;
 
434
 
 
435
        g_return_val_if_fail (GSD_IS_WACOM_STYLUS (stylus), NULL);
 
436
 
 
437
        page = g_object_new (CC_TYPE_WACOM_STYLUS_PAGE, NULL);
 
438
 
 
439
        priv = page->priv;
 
440
        priv->stylus = stylus;
 
441
        priv->eraser = eraser;
 
442
 
 
443
        /* Icon */
 
444
        set_icon_name (page, "image-stylus", gsd_wacom_stylus_get_icon_name (stylus));
 
445
 
 
446
        /* Settings */
 
447
        priv->stylus_settings = gsd_wacom_stylus_get_settings (stylus);
 
448
        if (eraser != NULL)
 
449
                priv->eraser_settings = gsd_wacom_stylus_get_settings (eraser);
 
450
 
 
451
        /* Stylus name */
 
452
        gtk_label_set_text (GTK_LABEL (WID ("label-stylus")), gsd_wacom_stylus_get_name (stylus));
 
453
 
 
454
        num_buttons = gsd_wacom_stylus_get_num_buttons (stylus);
 
455
        if (num_buttons == 0 && eraser == NULL)
 
456
                layout = LAYOUT_INKING;
 
457
        else if (num_buttons == 2 && eraser != NULL)
 
458
                layout = LAYOUT_NORMAL;
 
459
        else if (num_buttons == 1 && eraser != NULL)
 
460
                layout = LAYOUT_AIRBRUSH;
 
461
        else {
 
462
                layout = LAYOUT_OTHER;
 
463
                if (num_buttons == 0)
 
464
                        remove_buttons (priv);
 
465
                else if (num_buttons == 1)
 
466
                        remove_button (priv);
 
467
 
 
468
                if (eraser == NULL)
 
469
                        remove_eraser (priv);
 
470
 
 
471
                g_warning ("The layout of this page is not known, %d buttons, %s eraser",
 
472
                           num_buttons, eraser ? "with" : "without");
 
473
        }
 
474
 
 
475
        update_stylus_ui (page, layout);
 
476
 
 
477
        if (num_buttons == 2)
 
478
                set_button_mapping_from_gsettings (GTK_COMBO_BOX (WID ("combo-topbutton")), priv->stylus_settings, 3);
 
479
        if (num_buttons >= 1)
 
480
                set_button_mapping_from_gsettings (GTK_COMBO_BOX (WID ("combo-bottombutton")), priv->stylus_settings, 2);
 
481
        set_feel_from_gsettings (GTK_ADJUSTMENT (WID ("adjustment-tip-feel")), priv->stylus_settings);
 
482
 
 
483
        if (eraser != NULL)
 
484
                set_feel_from_gsettings (GTK_ADJUSTMENT (WID ("adjustment-eraser-feel")), priv->eraser_settings);
 
485
 
 
486
        g_object_set (G_OBJECT (page), "margin-top", 16, NULL);
 
487
 
 
488
        return GTK_WIDGET (page);
 
489
}
 
490
 
 
491
GsdWacomStylus *
 
492
cc_wacom_stylus_page_get_stylus (CcWacomStylusPage *page)
 
493
{
 
494
        return page->priv->stylus;
 
495
}
 
496
 
 
497
void
 
498
cc_wacom_stylus_page_set_navigation (CcWacomStylusPage *page,
 
499
                                     GtkNotebook *notebook)
 
500
{
 
501
        CcWacomStylusPagePrivate *priv;
 
502
 
 
503
        g_return_if_fail (CC_IS_WACOM_STYLUS_PAGE (page));
 
504
 
 
505
        priv = page->priv;
 
506
 
 
507
        g_object_set (G_OBJECT (priv->nav),
 
508
                      "notebook", notebook,
 
509
                      NULL);
 
510
}