2
* Copyright Ā© 2011 Red Hat, Inc.
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.
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.
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.
18
* Authors: Peter Hutterer <peter.hutterer@redhat.com>
19
* Bastien Nocera <hadess@hadess.net>
25
#include <glib/gi18n.h>
26
#include "cc-wacom-stylus-page.h"
27
#include "cc-wacom-nav-button.h"
32
#define WID(x) (GtkWidget *) gtk_builder_get_object (priv->builder, x)
33
#define CWID(x) (GtkContainer *) gtk_builder_get_object (priv->builder, x)
35
G_DEFINE_TYPE (CcWacomStylusPage, cc_wacom_stylus_page, GTK_TYPE_BOX)
37
#define WACOM_STYLUS_PAGE_PRIVATE(o) \
38
(G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_WACOM_STYLUS_PAGE, CcWacomStylusPagePrivate))
40
struct _CcWacomStylusPagePrivate
42
GsdWacomStylus *stylus, *eraser;
45
GSettings *stylus_settings, *eraser_settings;
48
/* Button combo box storage columns */
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.
59
#define N_PRESSURE_CURVES 7
60
static const gint32 PRESSURE_CURVES[N_PRESSURE_CURVES][4] = {
61
{ 0, 75, 25, 100 }, /* soft */
64
{ 0, 0, 100, 100 }, /* neutral */
67
{ 75, 0, 100, 25 } /* firm */
71
set_pressurecurve (GtkRange *range, GSettings *settings)
73
gint slider_val = gtk_range_get_value (range);
78
for (i = 0; i < G_N_ELEMENTS (values); i++)
79
values[i] = g_variant_new_int32 (PRESSURE_CURVES[slider_val][i]);
81
array = g_variant_new_array (G_VARIANT_TYPE_INT32, values, G_N_ELEMENTS (values));
83
g_settings_set_value (settings, "pressurecurve", array);
87
tip_feel_value_changed_cb (GtkRange *range, gpointer user_data)
89
set_pressurecurve (range, CC_WACOM_STYLUS_PAGE(user_data)->priv->stylus_settings);
93
eraser_feel_value_changed_cb (GtkRange *range, gpointer user_data)
95
set_pressurecurve (range, CC_WACOM_STYLUS_PAGE(user_data)->priv->eraser_settings);
99
set_feel_from_gsettings (GtkAdjustment *adjustment, GSettings *settings)
102
const gint32 *values;
106
variant = g_settings_get_value (settings, "pressurecurve");
107
values = g_variant_get_fixed_array (variant, &nvalues, sizeof (gint32));
110
g_warning ("Invalid pressure curve format, expected 4 values (got %"G_GSIZE_FORMAT")", nvalues);
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);
123
set_button_mapping_from_gsettings (GtkComboBox *combo, GSettings* settings, gint current_button)
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);
140
gtk_tree_model_get (model, &iter,
141
BUTTONNUMBER_COLUMN, &button,
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
150
/* 0 vs 1-indexed array/button numbers */
151
if (button == values[current_button - 1]) {
152
gtk_combo_box_set_active_iter (combo, &iter);
156
valid = gtk_tree_model_iter_next (model, &iter);
161
map_button (GSettings *settings, int button2, int button3)
163
GVariant *current; /* current mapping */
164
GVariant *array; /* new mapping */
170
current = g_settings_get_value (settings, "buttonmapping");
171
values = g_variant_get_fixed_array (current, &nvalues, sizeof (gint32));
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);
178
tmp[i] = g_variant_new_int32 (button3);
180
tmp[i] = g_variant_new_int32 (values[i]);
183
array = g_variant_new_array (G_VARIANT_TYPE_INT32, tmp, nvalues);
184
g_settings_set_value (settings, "buttonmapping", array);
190
button_changed_cb (GtkComboBox *combo, gpointer user_data)
192
CcWacomStylusPagePrivate *priv = CC_WACOM_STYLUS_PAGE(user_data)->priv;
194
GtkListStore *liststore;
198
if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (WID ("combo-bottombutton")), &iter))
201
liststore = GTK_LIST_STORE (WID ("liststore-buttons"));
202
gtk_tree_model_get (GTK_TREE_MODEL (liststore), &iter,
203
BUTTONNUMBER_COLUMN, &mapping_b2,
206
if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (WID ("combo-topbutton")), &iter))
209
gtk_tree_model_get (GTK_TREE_MODEL (liststore), &iter,
210
BUTTONNUMBER_COLUMN, &mapping_b3,
213
map_button (priv->stylus_settings, mapping_b2, mapping_b3);
217
combobox_text_cellrenderer (GtkComboBox *combo, int name_column)
219
GtkCellRenderer *renderer;
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);
227
/* Boilerplate code goes below */
230
cc_wacom_stylus_page_get_property (GObject *object,
238
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
243
cc_wacom_stylus_page_set_property (GObject *object,
251
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
256
cc_wacom_stylus_page_dispose (GObject *object)
258
CcWacomStylusPagePrivate *priv = CC_WACOM_STYLUS_PAGE (object)->priv;
261
g_object_unref (priv->builder);
262
priv->builder = NULL;
266
G_OBJECT_CLASS (cc_wacom_stylus_page_parent_class)->dispose (object);
270
cc_wacom_stylus_page_class_init (CcWacomStylusPageClass *klass)
272
GObjectClass *object_class = G_OBJECT_CLASS (klass);
274
g_type_class_add_private (klass, sizeof (CcWacomStylusPagePrivate));
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;
282
cc_wacom_stylus_page_init (CcWacomStylusPage *self)
284
CcWacomStylusPagePrivate *priv;
285
GError *error = NULL;
291
"adjustment-tip-feel",
292
"adjustment-eraser-feel",
296
priv = self->priv = WACOM_STYLUS_PAGE_PRIVATE (self);
298
priv->builder = gtk_builder_new ();
300
gtk_builder_add_objects_from_file (priv->builder,
301
GNOMECC_UI_DIR "/wacom-stylus-page.ui",
305
g_warning ("Error loading UI file: %s", error->message);
306
g_object_unref (priv->builder);
307
g_error_free (error);
311
box = WID ("stylus-grid");
312
gtk_container_add (GTK_CONTAINER (self), box);
313
gtk_widget_set_vexpand (GTK_WIDGET (box), TRUE);
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);
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);
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);
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);
337
set_icon_name (CcWacomStylusPage *page,
338
const char *widget_name,
339
const char *icon_name)
341
CcWacomStylusPagePrivate *priv;
342
char *filename, *path;
346
filename = g_strdup_printf ("%s.svg", icon_name);
347
path = g_build_filename (GNOMECC_UI_DIR, filename, NULL);
350
gtk_image_set_from_file (GTK_IMAGE (WID (widget_name)), path);
354
/* Different types of layout for the stylus config */
356
LAYOUT_NORMAL, /* eraser, 2 buttons, tip */
357
LAYOUT_INKING, /* tip */
358
LAYOUT_AIRBRUSH, /* eraser, 1 button, tip */
363
remove_buttons (CcWacomStylusPagePrivate *priv)
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"));
372
remove_button (CcWacomStylusPagePrivate *priv)
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"));
380
remove_eraser (CcWacomStylusPagePrivate *priv)
382
gtk_widget_destroy (WID ("eraser-box"));
383
gtk_widget_destroy (WID ("label-eraser-feel"));
387
update_stylus_ui (CcWacomStylusPage *page,
390
CcWacomStylusPagePrivate *priv = page->priv;
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);
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);
421
/* We already warn about it in cc_wacom_stylus_page_new () */
427
cc_wacom_stylus_page_new (GsdWacomStylus *stylus,
428
GsdWacomStylus *eraser)
430
CcWacomStylusPage *page;
431
CcWacomStylusPagePrivate *priv;
435
g_return_val_if_fail (GSD_IS_WACOM_STYLUS (stylus), NULL);
437
page = g_object_new (CC_TYPE_WACOM_STYLUS_PAGE, NULL);
440
priv->stylus = stylus;
441
priv->eraser = eraser;
444
set_icon_name (page, "image-stylus", gsd_wacom_stylus_get_icon_name (stylus));
447
priv->stylus_settings = gsd_wacom_stylus_get_settings (stylus);
449
priv->eraser_settings = gsd_wacom_stylus_get_settings (eraser);
452
gtk_label_set_text (GTK_LABEL (WID ("label-stylus")), gsd_wacom_stylus_get_name (stylus));
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;
462
layout = LAYOUT_OTHER;
463
if (num_buttons == 0)
464
remove_buttons (priv);
465
else if (num_buttons == 1)
466
remove_button (priv);
469
remove_eraser (priv);
471
g_warning ("The layout of this page is not known, %d buttons, %s eraser",
472
num_buttons, eraser ? "with" : "without");
475
update_stylus_ui (page, layout);
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);
484
set_feel_from_gsettings (GTK_ADJUSTMENT (WID ("adjustment-eraser-feel")), priv->eraser_settings);
486
g_object_set (G_OBJECT (page), "margin-top", 16, NULL);
488
return GTK_WIDGET (page);
492
cc_wacom_stylus_page_get_stylus (CcWacomStylusPage *page)
494
return page->priv->stylus;
498
cc_wacom_stylus_page_set_navigation (CcWacomStylusPage *page,
499
GtkNotebook *notebook)
501
CcWacomStylusPagePrivate *priv;
503
g_return_if_fail (CC_IS_WACOM_STYLUS_PAGE (page));
507
g_object_set (G_OBJECT (priv->nav),
508
"notebook", notebook,