gimpdeviceinfoeditor.c 26.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis
 *
 * gimpdeviceinfoeditor.c
 * Copyright (C) 2010 Michael Natterer <mitch@gimp.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 20 21 22
 */

#include "config.h"

23
#include <gegl.h>
24 25 26 27 28 29
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

30 31 32
#include "core/gimpcurve.h"

#include "gimpcurveview.h"
33 34 35 36 37 38
#include "gimpdeviceinfo.h"
#include "gimpdeviceinfoeditor.h"

#include "gimp-intl.h"


39 40 41 42
#define CURVE_SIZE   256
#define CURVE_BORDER   4


43 44 45 46 47 48
enum
{
  PROP_0,
  PROP_INFO
};

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
enum
{
  AXIS_COLUMN_INDEX,
  AXIS_COLUMN_NAME,
  AXIS_COLUMN_INPUT_NAME,
  AXIS_N_COLUMNS
};

enum
{
  INPUT_COLUMN_INDEX,
  INPUT_COLUMN_NAME,
  INPUT_N_COLUMNS
};

64
enum
65
{
66 67 68 69 70
  KEY_COLUMN_INDEX,
  KEY_COLUMN_NAME,
  KEY_COLUMN_KEY,
  KEY_COLUMN_MASK,
  KEY_N_COLUMNS
71 72 73 74 75 76 77 78 79
};


typedef struct _GimpDeviceInfoEditorPrivate GimpDeviceInfoEditorPrivate;

struct _GimpDeviceInfoEditorPrivate
{
  GimpDeviceInfo *info;

80 81
  GtkWidget      *vbox;

82
  GtkListStore   *input_store;
83

84 85 86
  GtkListStore   *axis_store;
  GtkTreeIter     axis_iters[GDK_AXIS_LAST - GDK_AXIS_X];

87
  GtkListStore   *key_store;
88 89

  GtkWidget      *notebook;
90 91 92 93 94 95 96 97
};

#define GIMP_DEVICE_INFO_EDITOR_GET_PRIVATE(editor) \
        G_TYPE_INSTANCE_GET_PRIVATE (editor, \
                                     GIMP_TYPE_DEVICE_INFO_EDITOR, \
                                     GimpDeviceInfoEditorPrivate)


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
static void   gimp_device_info_editor_constructed   (GObject              *object);
static void   gimp_device_info_editor_finalize      (GObject              *object);
static void   gimp_device_info_editor_set_property  (GObject              *object,
                                                     guint                 property_id,
                                                     const GValue         *value,
                                                     GParamSpec           *pspec);
static void   gimp_device_info_editor_get_property  (GObject              *object,
                                                     guint                 property_id,
                                                     GValue               *value,
                                                     GParamSpec           *pspec);

static void   gimp_device_info_editor_set_axes      (GimpDeviceInfoEditor *editor);

static void   gimp_device_info_editor_axis_changed  (GtkCellRendererCombo *combo,
                                                     const gchar          *path_string,
                                                     GtkTreeIter          *new_iter,
                                                     GimpDeviceInfoEditor *editor);
static void   gimp_device_info_editor_axis_selected (GtkTreeSelection     *selection,
                                                     GimpDeviceInfoEditor *editor);

static void   gimp_device_info_editor_key_edited    (GtkCellRendererAccel *accel,
                                                     const char           *path_string,
                                                     guint                 accel_key,
                                                     GdkModifierType       accel_mask,
                                                     guint                 hardware_keycode,
                                                     GimpDeviceInfoEditor *editor);
static void   gimp_device_info_editor_key_cleared   (GtkCellRendererAccel *accel,
                                                     const char           *path_string,
                                                     GimpDeviceInfoEditor *editor);

static void   gimp_device_info_editor_curve_reset   (GtkWidget            *button,
                                                     GimpCurve            *curve);
130

131

132
G_DEFINE_TYPE (GimpDeviceInfoEditor, gimp_device_info_editor, GTK_TYPE_BOX)
133 134 135 136

#define parent_class gimp_device_info_editor_parent_class


137 138 139 140 141 142 143
static const gchar *const axis_use_strings[] =
{
  N_("X"),
  N_("Y"),
  N_("Pressure"),
  N_("X tilt"),
  N_("Y tilt"),
144
  /* Wheel as in mouse or input device wheel.
145
   * Some pens would use the same axis for their rotation feature.
146
   * See bug 791455.
147 148 149
   * Yet GTK+ has a different axis since v. 3.22.
   * TODO: this should be actually tested with a device having such
   * feature.
150
   */
151 152 153 154
  N_("Wheel"),
  N_("Distance"),
  N_("Rotation"),
  N_("Slider")
155 156 157
};


158 159 160 161 162
static void
gimp_device_info_editor_class_init (GimpDeviceInfoEditorClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

163
  object_class->constructed  = gimp_device_info_editor_constructed;
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  object_class->finalize     = gimp_device_info_editor_finalize;
  object_class->set_property = gimp_device_info_editor_set_property;
  object_class->get_property = gimp_device_info_editor_get_property;

  g_object_class_install_property (object_class, PROP_INFO,
                                   g_param_spec_object ("info",
                                                        NULL, NULL,
                                                        GIMP_TYPE_DEVICE_INFO,
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

  g_type_class_add_private (object_class, sizeof (GimpDeviceInfoEditorPrivate));
}

static void
gimp_device_info_editor_init (GimpDeviceInfoEditor *editor)
{
181
  GimpDeviceInfoEditorPrivate *private;
182
  GtkWidget                   *frame;
183 184
  GtkWidget                   *frame2;
  GtkWidget                   *view;
185
  GtkTreeSelection            *sel;
186
  GtkWidget                   *scrolled_win;
187
  GtkWidget                   *key_view;
188
  GtkCellRenderer             *cell;
189
  gint                         i;
190 191 192

  private = GIMP_DEVICE_INFO_EDITOR_GET_PRIVATE (editor);

193 194 195
  gtk_orientable_set_orientation (GTK_ORIENTABLE (editor),
                                  GTK_ORIENTATION_HORIZONTAL);

196 197
  gtk_box_set_spacing (GTK_BOX (editor), 12);

198
  private->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
199 200
  gtk_box_pack_start (GTK_BOX (editor), private->vbox, TRUE, TRUE, 0);
  gtk_widget_show (private->vbox);
201

202 203
  /*  the axes  */

204
  /* The axes of an input device */
205
  frame = gimp_frame_new (_("Axes"));
206
  gtk_box_pack_start (GTK_BOX (private->vbox), frame, FALSE, FALSE, 0);
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
  gtk_widget_show (frame);

  frame2 = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame2), GTK_SHADOW_IN);
  gtk_container_add (GTK_CONTAINER (frame), frame2);
  gtk_widget_show (frame2);

  private->axis_store = gtk_list_store_new (AXIS_N_COLUMNS,
                                            G_TYPE_INT,
                                            G_TYPE_STRING,
                                            G_TYPE_STRING);

  for (i = GDK_AXIS_X; i < GDK_AXIS_LAST; i++)
    {
      const gchar *string = gettext (axis_use_strings[i - 1]);

      gtk_list_store_insert_with_values (private->axis_store,
                                         &private->axis_iters[i - 1], -1,
                                         AXIS_COLUMN_INDEX, i,
                                         AXIS_COLUMN_NAME,  string,
                                         -1);
    }

  view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (private->axis_store));
  g_object_unref (private->axis_store);

  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (view), FALSE);

  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
                                               -1, NULL,
                                               gtk_cell_renderer_text_new (),
                                               "text", AXIS_COLUMN_NAME,
                                               NULL);

  private->input_store = gtk_list_store_new (INPUT_N_COLUMNS,
                                             G_TYPE_INT,
                                             G_TYPE_STRING);

  cell = gtk_cell_renderer_combo_new ();
  g_object_set (cell,
                "mode",        GTK_CELL_RENDERER_MODE_EDITABLE,
                "editable",    TRUE,
                "model",       private->input_store,
                "text-column", INPUT_COLUMN_NAME,
                "has-entry",   FALSE,
                NULL);

  g_object_unref (private->input_store);

  g_signal_connect (cell, "changed",
                    G_CALLBACK (gimp_device_info_editor_axis_changed),
                    editor);

  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
                                               -1, NULL,
                                               cell,
                                               "text", AXIS_COLUMN_INPUT_NAME,
                                               NULL);

  gtk_container_add (GTK_CONTAINER (frame2), view);
  gtk_widget_show (view);

269 270
  sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
  gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
271
  gtk_tree_selection_select_iter (sel, &private->axis_iters[0]);
272 273 274 275 276

  g_signal_connect (sel, "changed",
                    G_CALLBACK (gimp_device_info_editor_axis_selected),
                    editor);

277 278
  /*  the keys  */

279 280 281 282
  frame = gimp_frame_new (_("Keys"));
  gtk_box_pack_end (GTK_BOX (private->vbox), frame, TRUE, TRUE, 0);
  gtk_widget_show (frame);

283 284 285 286 287 288 289 290
  private->key_store = gtk_list_store_new (KEY_N_COLUMNS,
                                           G_TYPE_INT,
                                           G_TYPE_STRING,
                                           G_TYPE_UINT,
                                           GDK_TYPE_MODIFIER_TYPE);
  key_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (private->key_store));
  g_object_unref (private->key_store);

291 292
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (key_view), FALSE);

293
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (key_view),
294
                                               -1, NULL,
295 296 297 298 299
                                               gtk_cell_renderer_text_new (),
                                               "text", KEY_COLUMN_NAME,
                                               NULL);

  cell = gtk_cell_renderer_accel_new ();
300 301 302 303
  g_object_set (cell,
                "mode",     GTK_CELL_RENDERER_MODE_EDITABLE,
                "editable", TRUE,
                NULL);
304
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (key_view),
305
                                               -1, NULL,
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
                                               cell,
                                               "accel-key",  KEY_COLUMN_KEY,
                                               "accel-mods", KEY_COLUMN_MASK,
                                               NULL);

  g_signal_connect (cell, "accel-edited",
                    G_CALLBACK (gimp_device_info_editor_key_edited),
                    editor);
  g_signal_connect (cell, "accel-cleared",
                    G_CALLBACK (gimp_device_info_editor_key_cleared),
                    editor);

  scrolled_win = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
                                       GTK_SHADOW_IN);
321 322
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
323
  gtk_container_add (GTK_CONTAINER (frame), scrolled_win);
324 325 326 327
  gtk_widget_show (scrolled_win);

  gtk_container_add (GTK_CONTAINER (scrolled_win), key_view);
  gtk_widget_show (key_view);
328 329
}

330 331
static void
gimp_device_info_editor_constructed (GObject *object)
332
{
333
  GimpDeviceInfoEditor        *editor  = GIMP_DEVICE_INFO_EDITOR (object);
334
  GimpDeviceInfoEditorPrivate *private;
335 336
  GtkWidget                   *frame;
  GtkWidget                   *grid;
337 338
  GtkWidget                   *label;
  GtkWidget                   *combo;
339
  gint                         n_axes;
340 341 342 343 344
  gint                         n_keys;
  gint                         i;

  private = GIMP_DEVICE_INFO_EDITOR_GET_PRIVATE (object);

345
  G_OBJECT_CLASS (parent_class)->constructed (object);
346

347
  gimp_assert (GIMP_IS_DEVICE_INFO (private->info));
348

349
  /*  general device information  */
350

351 352 353 354
  frame = gimp_frame_new (_("General"));
  gtk_box_pack_start (GTK_BOX (private->vbox), frame, FALSE, FALSE, 0);
  gtk_box_reorder_child (GTK_BOX (private->vbox), frame, 0);
  gtk_widget_show (frame);
355

356 357 358 359 360
  grid = gtk_grid_new ();
  gtk_grid_set_row_spacing (GTK_GRID (grid), 4);
  gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
  gtk_container_add (GTK_CONTAINER (frame), grid);
  gtk_widget_show (grid);
361 362 363

  combo = gimp_prop_enum_combo_box_new (G_OBJECT (private->info), "mode",
                                        0, 0);
364 365 366 367 368 369 370 371 372 373 374 375 376
  gimp_grid_attach_aligned (GTK_GRID (grid), 0, 0,
                            _("_Mode:"), 0.0, 0.5,
                            combo, 1);

  label = gimp_prop_enum_label_new (G_OBJECT (private->info), "source");
  gimp_grid_attach_aligned (GTK_GRID (grid), 0, 1,
                            _("Source:"), 0.0, 0.5,
                            label, 1);

  label = gimp_prop_label_new (G_OBJECT (private->info), "vendor-id");
  gimp_grid_attach_aligned (GTK_GRID (grid), 0, 2,
                            _("Vendor ID:"), 0.0, 0.5,
                            label, 1);
377

378 379 380 381
  label = gimp_prop_label_new (G_OBJECT (private->info), "product-id");
  gimp_grid_attach_aligned (GTK_GRID (grid), 0, 3,
                            _("Product ID:"), 0.0, 0.5,
                            label, 1);
382

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  label = gimp_prop_enum_label_new (G_OBJECT (private->info), "tool-type");
  gimp_grid_attach_aligned (GTK_GRID (grid), 0, 4,
                            _("Tool type:"), 0.0, 0.5,
                            label, 1);

  label = gimp_prop_label_new (G_OBJECT (private->info), "tool-serial");
  gimp_grid_attach_aligned (GTK_GRID (grid), 0, 5,
                            _("Tool serial:"), 0.0, 0.5,
                            label, 1);

  label = gimp_prop_label_new (G_OBJECT (private->info), "tool-hardware-id");
  gimp_grid_attach_aligned (GTK_GRID (grid), 0, 6,
                            _("Tool hardware ID:"), 0.0, 0.5,
                            label, 1);

398 399
  /*  the axes  */

400
  n_axes = gimp_device_info_get_n_axes (private->info);
401

402
  for (i = -1; i < n_axes; i++)
403
    {
404
      gchar name[16];
405

406 407 408 409
      if (i == -1)
        g_snprintf (name, sizeof (name), _("none"));
      else
        g_snprintf (name, sizeof (name), "%d", i + 1);
410

411 412 413 414
      gtk_list_store_insert_with_values (private->input_store, NULL, -1,
                                         INPUT_COLUMN_INDEX, i,
                                         INPUT_COLUMN_NAME,  name,
                                         -1);
415 416
    }

417 418
  gimp_device_info_editor_set_axes (editor);

419 420 421 422 423 424
  /*  the keys  */

  n_keys = gimp_device_info_get_n_keys (private->info);

  for (i = 0; i < n_keys; i++)
    {
425
      gchar           string[16];
426 427
      guint           keyval;
      GdkModifierType modifiers;
428

429
      g_snprintf (string, sizeof (string), "%d", i + 1);
430 431 432

      gimp_device_info_get_key (private->info, i, &keyval, &modifiers);

433
      gtk_list_store_insert_with_values (private->key_store, NULL, -1,
434 435 436 437 438
                                         KEY_COLUMN_INDEX, i,
                                         KEY_COLUMN_NAME,  string,
                                         KEY_COLUMN_KEY,   keyval,
                                         KEY_COLUMN_MASK,  modifiers,
                                         -1);
439 440
    }

441 442 443 444 445 446 447 448 449 450 451 452 453
  /*  the curves  */

  private->notebook = gtk_notebook_new ();
  gtk_notebook_set_show_border (GTK_NOTEBOOK (private->notebook), FALSE);
  gtk_notebook_set_show_tabs (GTK_NOTEBOOK (private->notebook), FALSE);
  gtk_box_pack_start (GTK_BOX (editor), private->notebook, TRUE, TRUE, 0);
  gtk_widget_show (private->notebook);

  for (i = GDK_AXIS_X; i < GDK_AXIS_LAST; i++)
    {
      GimpCurve *curve;
      gchar     *title;

454
      /* e.g. "Pressure Curve" for mapping input device axes */
455 456 457 458 459 460 461 462 463 464 465 466 467
      title = g_strdup_printf (_("%s Curve"), axis_use_strings[i - 1]);

      frame = gimp_frame_new (title);
      gtk_notebook_append_page (GTK_NOTEBOOK (private->notebook), frame, NULL);
      gtk_widget_show (frame);

      g_free (title);

      curve = gimp_device_info_get_curve (private->info, i);

      if (curve)
        {
          GtkWidget *vbox;
468
          GtkWidget *hbox;
469 470 471 472 473
          GtkWidget *view;
          GtkWidget *label;
          GtkWidget *combo;
          GtkWidget *button;

474
          vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
475 476 477 478 479 480 481 482 483 484 485
          gtk_box_set_spacing (GTK_BOX (vbox), 6);
          gtk_container_add (GTK_CONTAINER (frame), vbox);
          gtk_widget_show (vbox);

          frame = gtk_frame_new (NULL);
          gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
          gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
          gtk_widget_show (frame);

          view = gimp_curve_view_new ();
          g_object_set (view,
486
                        "gimp",         GIMP_TOOL_PRESET (private->info)->gimp,
487 488 489 490 491 492 493 494
                        "border-width", CURVE_BORDER,
                        NULL);
          gtk_widget_set_size_request (view,
                                       CURVE_SIZE + CURVE_BORDER * 2,
                                       CURVE_SIZE + CURVE_BORDER * 2);
          gtk_container_add (GTK_CONTAINER (frame), view);
          gtk_widget_show (view);

495
          gimp_curve_view_set_curve (GIMP_CURVE_VIEW (view), curve, NULL);
496

497
          hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
498
          gtk_box_set_spacing (GTK_BOX (hbox), 6);
499
          gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
500 501 502 503 504 505 506 507
          gtk_widget_show (hbox);

          label = gtk_label_new_with_mnemonic (_("Curve _type:"));
          gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
          gtk_widget_show (label);

          combo = gimp_prop_enum_combo_box_new (G_OBJECT (curve),
                                                "curve-type", 0, 0);
508 509
          gimp_enum_combo_box_set_icon_prefix (GIMP_ENUM_COMBO_BOX (combo),
                                               "gimp-curve");
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
          gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0);
          gtk_widget_show (combo);

          gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);

          button = gtk_button_new_with_mnemonic (_("_Reset Curve"));
          gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
          gtk_widget_show (button);

          g_signal_connect (button, "clicked",
                            G_CALLBACK (gimp_device_info_editor_curve_reset),
                            curve);
        }
      else
        {
          GtkWidget *label;
          gchar     *string;

          string = g_strdup_printf (_("The axis '%s' has no curve"),
                                    axis_use_strings[i - 1]);

          label = gtk_label_new (string);
          gtk_container_add (GTK_CONTAINER (frame), label);
          gtk_widget_show (label);
534

Massimo Valentini's avatar
Massimo Valentini committed
535
          g_free (string);
536 537
        }
    }
538 539 540 541 542 543 544 545 546
}

static void
gimp_device_info_editor_finalize (GObject *object)
{
  GimpDeviceInfoEditorPrivate *private;

  private = GIMP_DEVICE_INFO_EDITOR_GET_PRIVATE (object);

547
  g_clear_object (&private->info);
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gimp_device_info_editor_set_property (GObject      *object,
                                      guint         property_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
{
  GimpDeviceInfoEditorPrivate *private;

  private = GIMP_DEVICE_INFO_EDITOR_GET_PRIVATE (object);

  switch (property_id)
    {
    case PROP_INFO:
      private->info = g_value_dup_object (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_device_info_editor_get_property (GObject    *object,
                                      guint       property_id,
                                      GValue     *value,
                                      GParamSpec *pspec)
{
  GimpDeviceInfoEditorPrivate *private;

  private = GIMP_DEVICE_INFO_EDITOR_GET_PRIVATE (object);

  switch (property_id)
    {
    case PROP_INFO:
      g_value_set_object (value, private->info);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

596
static void
597
gimp_device_info_editor_set_axes (GimpDeviceInfoEditor *editor)
598 599
{
  GimpDeviceInfoEditorPrivate *private;
600 601
  gint                         n_axes;
  gint                         i;
602 603 604

  private = GIMP_DEVICE_INFO_EDITOR_GET_PRIVATE (editor);

605 606 607
  n_axes = gimp_device_info_get_n_axes (private->info);

  for (i = GDK_AXIS_X; i < GDK_AXIS_LAST; i++)
608
    {
609
      gchar input_name[16];
610 611 612
      gint  j;

      for (j = 0; j < n_axes; j++)
613
        {
614 615 616 617 618 619 620 621 622 623 624
          if (gimp_device_info_get_axis_use (private->info, j) == i)
            break;
        }

      if (j == n_axes)
        j = -1;

      if (j == -1)
        g_snprintf (input_name, sizeof (input_name), _("none"));
      else
        g_snprintf (input_name, sizeof (input_name), "%d", j + 1);
625

626 627 628 629 630 631
      gtk_list_store_set (private->axis_store,
                          &private->axis_iters[i - 1],
                          AXIS_COLUMN_INPUT_NAME, input_name,
                          -1);
    }
}
632

633
static void
634 635 636 637 638 639 640 641
gimp_device_info_editor_axis_changed (GtkCellRendererCombo  *combo,
                                      const gchar           *path_string,
                                      GtkTreeIter           *new_iter,
                                      GimpDeviceInfoEditor  *editor)
{
  GimpDeviceInfoEditorPrivate *private;
  GtkTreePath                 *path;
  GtkTreeIter                  new_use_iter;
642

643
  private = GIMP_DEVICE_INFO_EDITOR_GET_PRIVATE (editor);
644

645
  path = gtk_tree_path_new_from_string (path_string);
646

647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
  if (gtk_tree_model_get_iter (GTK_TREE_MODEL (private->axis_store),
                               &new_use_iter, path))
    {
      GdkAxisUse new_use  = GDK_AXIS_IGNORE;
      GdkAxisUse old_use  = GDK_AXIS_IGNORE;
      gint       new_axis = -1;
      gint       old_axis = -1;
      gint       n_axes;
      gint       i;

      gtk_tree_model_get (GTK_TREE_MODEL (private->axis_store), &new_use_iter,
                          AXIS_COLUMN_INDEX, &new_use,
                          -1);

      gtk_tree_model_get (GTK_TREE_MODEL (private->input_store), new_iter,
                          INPUT_COLUMN_INDEX, &new_axis,
                          -1);

      n_axes = gimp_device_info_get_n_axes (private->info);

      for (i = 0; i < n_axes; i++)
        if (gimp_device_info_get_axis_use (private->info, i) == new_use)
          {
            old_axis = i;
            break;
          }

      if (new_axis == old_axis)
        goto out;

      if (new_axis != -1)
        old_use = gimp_device_info_get_axis_use (private->info, new_axis);

      /* we must always have an x and a y axis */
      if ((new_axis == -1 && (new_use == GDK_AXIS_X ||
                              new_use == GDK_AXIS_Y)) ||
          (old_axis == -1 && (old_use == GDK_AXIS_X ||
                              old_use == GDK_AXIS_Y)))
        {
          /* do nothing */
        }
      else
        {
690
          if (new_axis != -1)
691 692 693 694 695 696
            gimp_device_info_set_axis_use (private->info, new_axis, new_use);

          if (old_axis != -1)
            gimp_device_info_set_axis_use (private->info, old_axis, old_use);

          gimp_device_info_editor_set_axes (editor);
697 698
        }
    }
699 700 701

 out:
  gtk_tree_path_free (path);
702 703
}

704
static void
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
gimp_device_info_editor_axis_selected (GtkTreeSelection     *selection,
                                       GimpDeviceInfoEditor *editor)
{
  GimpDeviceInfoEditorPrivate *private;
  GtkTreeIter                  iter;

  private = GIMP_DEVICE_INFO_EDITOR_GET_PRIVATE (editor);

  if (gtk_tree_selection_get_selected (selection, NULL, &iter))
    {
      GdkAxisUse use;

      gtk_tree_model_get (GTK_TREE_MODEL (private->axis_store), &iter,
                          AXIS_COLUMN_INDEX, &use,
                          -1);

      gtk_notebook_set_current_page (GTK_NOTEBOOK (private->notebook),
                                     use - 1);
    }
}

726 727 728 729 730 731 732
static void
gimp_device_info_editor_key_edited (GtkCellRendererAccel *accel,
                                    const char           *path_string,
                                    guint                 accel_key,
                                    GdkModifierType       accel_mask,
                                    guint                 hardware_keycode,
                                    GimpDeviceInfoEditor *editor)
733 734
{
  GimpDeviceInfoEditorPrivate *private;
735 736
  GtkTreePath                 *path;
  GtkTreeIter                  iter;
737

738
  private = GIMP_DEVICE_INFO_EDITOR_GET_PRIVATE (editor);
739

740
  path = gtk_tree_path_new_from_string (path_string);
741

742 743 744
  if (gtk_tree_model_get_iter (GTK_TREE_MODEL (private->key_store), &iter, path))
    {
      gint index;
745

746 747 748
      gtk_tree_model_get (GTK_TREE_MODEL (private->key_store), &iter,
                          KEY_COLUMN_INDEX, &index,
                          -1);
749

750 751 752 753
      gtk_list_store_set (private->key_store, &iter,
                          KEY_COLUMN_KEY,  accel_key,
                          KEY_COLUMN_MASK, accel_mask,
                          -1);
754

755
      gimp_device_info_set_key (private->info, index, accel_key, accel_mask);
756
    }
757

758
  gtk_tree_path_free (path);
759 760 761
}

static void
762 763 764
gimp_device_info_editor_key_cleared (GtkCellRendererAccel *accel,
                                     const char           *path_string,
                                     GimpDeviceInfoEditor *editor)
765
{
766 767 768 769 770
  GimpDeviceInfoEditorPrivate *private;
  GtkTreePath                 *path;
  GtkTreeIter                  iter;

  private = GIMP_DEVICE_INFO_EDITOR_GET_PRIVATE (editor);
771

772 773 774
  path = gtk_tree_path_new_from_string (path_string);

  if (gtk_tree_model_get_iter (GTK_TREE_MODEL (private->key_store), &iter, path))
775
    {
776
      gint index;
777

778 779 780
      gtk_tree_model_get (GTK_TREE_MODEL (private->key_store), &iter,
                          KEY_COLUMN_INDEX, &index,
                          -1);
781

782 783 784 785
      gtk_list_store_set (private->key_store, &iter,
                          KEY_COLUMN_KEY,  0,
                          KEY_COLUMN_MASK, 0,
                          -1);
786

787
      gimp_device_info_set_key (private->info, index, 0, 0);
788 789
    }

790 791
  gtk_tree_path_free (path);
}
792

793 794 795 796 797 798 799 800
static void
gimp_device_info_editor_curve_reset (GtkWidget *button,
                                     GimpCurve *curve)
{
  gimp_curve_reset (curve, TRUE);
}


801 802 803 804 805 806 807 808 809 810 811
/*  public functions  */

GtkWidget *
gimp_device_info_editor_new (GimpDeviceInfo *info)
{
  g_return_val_if_fail (GIMP_IS_DEVICE_INFO (info), NULL);

  return g_object_new (GIMP_TYPE_DEVICE_INFO_EDITOR,
                       "info", info,
                       NULL);
}