gimpdevices.c 31.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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 2 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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
18 19
#include "config.h"

20 21 22
#include <string.h>

#include "appenv.h"
23 24
#include "devices.h"
#include "dialog_handler.h"
25
#include "gimpcontextpreview.h"
Sven Neumann's avatar
Sven Neumann committed
26
#include "gimpdnd.h"
27 28
#include "gimpbrushlist.h"
#include "gimpcontext.h"
29
#include "gimprc.h"
30
#include "gimpui.h"
31
#include "gradient.h"
32
#include "gradient_header.h"
33
#include "interface.h"
34
#include "session.h"
35 36
#include "tools.h"

37
#include "libgimp/gimpintl.h"
38
#include "libgimp/gimpenv.h"
39

40
#define CELL_SIZE 20 /* The size of the preview cells */
41

42
#define PREVIEW_EVENT_MASK  GDK_BUTTON_PRESS_MASK | \
43 44 45
			    GDK_BUTTON_RELEASE_MASK | \
			    GDK_ENTER_NOTIFY_MASK | \
                            GDK_LEAVE_NOTIFY_MASK
46

47 48 49
#define DEVICE_CONTEXT_MASK GIMP_CONTEXT_TOOL_MASK | \
                            GIMP_CONTEXT_FOREGROUND_MASK | \
			    GIMP_CONTEXT_BRUSH_MASK | \
50 51
			    GIMP_CONTEXT_PATTERN_MASK | \
                            GIMP_CONTEXT_GRADIENT_MASK
52

53 54
typedef struct _DeviceInfo DeviceInfo;

55 56 57 58 59 60
struct _DeviceInfo
{
  guint32       device;      /*  device ID  */
  gchar        *name;

  gshort        is_present;  /*  is the device currently present  */
61

62
  /*  gdk_input options - for not present devices  */
63

64 65 66 67
  GdkInputMode  mode;
  gint          num_axes;
  GdkAxisUse   *axes;
  gint          num_keys;
68 69
  GdkDeviceKey *keys;

70
  GimpContext  *context;
71 72 73 74
};

typedef struct _DeviceInfoDialog DeviceInfoDialog;

75 76 77 78 79 80
struct _DeviceInfoDialog
{
  gint       num_devices;

  guint32    current;
  guint32   *ids;
81 82 83 84 85 86 87 88

  GtkWidget *shell;
  GtkWidget *table;

  GtkWidget **frames;
  GtkWidget **tools;
  GtkWidget **colors;
  GtkWidget **brushes;
89
  GtkWidget **patterns;
90
  GtkWidget **gradients;
91
  GtkWidget **eventboxes;
92 93
};

94
/*  local functions */
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
static void     input_dialog_able_callback     (GtkWidget *w, guint32 deviceid, 
						gpointer data);

static void     devices_write_rc_device        (DeviceInfo *device_info,
						FILE *fp);
static void     devices_write_rc               (void);

static void     device_status_destroy_callback (void);
static void     devices_close_callback         (GtkWidget *, gpointer);

static void     device_status_update           (guint32 deviceid);
static void     device_status_update_current   (void);

static ToolType device_status_drag_tool        (GtkWidget *,
						gpointer);
static void     device_status_drop_tool        (GtkWidget *,
						ToolType,
						gpointer);
static void     device_status_drag_color       (GtkWidget *,
						guchar *, guchar *, guchar *,
						gpointer);
static void     device_status_drop_color       (GtkWidget *,
						guchar, guchar, guchar,
						gpointer);
static void     device_status_drop_brush       (GtkWidget *,
						GimpBrush *,
						gpointer);
static void     device_status_drop_pattern     (GtkWidget *,
						GPattern *,
						gpointer);
static void     device_status_drop_gradient    (GtkWidget *,
						gradient_t *,
						gpointer);

static void     device_status_color_changed    (GimpContext *context,
						gint         r,
						gint         g,
						gint         b,
						gpointer     data);
static void     device_status_data_changed     (GimpContext *context,
						gpointer     dummy,
						gpointer     data);

static void     device_status_context_connect  (GimpContext *context,
						guint32      deviceid);
Sven Neumann's avatar
Sven Neumann committed
140

141 142
/*  global data  */
gint current_device = GDK_CORE_POINTER;
143

144 145 146 147 148 149
/*  local data  */
static GList            *device_info_list = NULL;
static DeviceInfoDialog *deviceD          = NULL;

/*  if true, don't update device information dialog */
static gboolean          suppress_update  = FALSE;
150

Sven Neumann's avatar
Sven Neumann committed
151
/*  dnd stuff  */
152 153 154 155 156 157 158
static GtkTargetEntry tool_target_table[] =
{
  GIMP_TARGET_TOOL
};
static guint n_tool_targets = (sizeof (tool_target_table) /
			       sizeof (tool_target_table[0]));

Sven Neumann's avatar
Sven Neumann committed
159 160 161 162 163 164
static GtkTargetEntry color_area_target_table[] =
{
  GIMP_TARGET_COLOR
};
static guint n_color_area_targets = (sizeof (color_area_target_table) /
				     sizeof (color_area_target_table[0]));
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

/*  utility functions for the device lists  */

static GdkDeviceInfo *
gdk_device_info_get_by_id (guint32 deviceid)
{
  GdkDeviceInfo *info;
  GList *list;

  for (list = gdk_input_list_devices (); list; list = g_list_next (list))
    {
      info = (GdkDeviceInfo *) list->data;

      if (info->deviceid == deviceid)
	return info;
    }

  return NULL;
}

static DeviceInfo *
device_info_get_by_id (guint32 deviceid)
{
  DeviceInfo *info;
  GList *list;

  for (list = device_info_list; list; list = g_list_next (list))
    {
      info = (DeviceInfo *) list->data;

      if (info->device == deviceid)
	return info;
    }

  return NULL;
}

static DeviceInfo *
device_info_get_by_name (gchar *name)
{
  DeviceInfo *info;
  GList *list;

  for (list = device_info_list; list; list = g_list_next (list))
    {
      info = (DeviceInfo *) list->data;

      if (!strcmp (info->name, name))
	return info;
    }

  return NULL;
}

/*  the gtk input dialog  */
Sven Neumann's avatar
Sven Neumann committed
220

221
void 
222
input_dialog_create (void)
223 224
{
  static GtkWidget *inputd = NULL;
Michael Natterer's avatar
Michael Natterer committed
225
  GtkWidget        *hbbox;
226 227 228

  if (!inputd)
    {
229
      inputd = gtk_input_dialog_new ();
230 231

      /* register this one only */
232
      dialog_register (inputd);
233

234 235
      gtk_container_set_border_width
	(GTK_CONTAINER (GTK_DIALOG (inputd)->action_area), 2);
Michael Natterer's avatar
Michael Natterer committed
236 237 238
      gtk_box_set_homogeneous (GTK_BOX (GTK_DIALOG (inputd)->action_area),
			       FALSE);

239 240
      hbbox = gtk_hbutton_box_new ();
      gtk_button_box_set_spacing (GTK_BUTTON_BOX (hbbox), 4);
Michael Natterer's avatar
Michael Natterer committed
241 242 243 244 245 246 247 248

      gtk_widget_reparent (GTK_INPUT_DIALOG (inputd)->save_button, hbbox);
      GTK_WIDGET_SET_FLAGS (GTK_INPUT_DIALOG (inputd)->save_button,
			    GTK_CAN_DEFAULT);
      gtk_widget_reparent (GTK_INPUT_DIALOG (inputd)->close_button, hbbox);
      GTK_WIDGET_SET_FLAGS (GTK_INPUT_DIALOG (inputd)->close_button,
			    GTK_CAN_DEFAULT);

249 250
      gtk_box_pack_end (GTK_BOX (GTK_DIALOG (inputd)->action_area), hbbox,
			FALSE, FALSE, 0);
Michael Natterer's avatar
Michael Natterer committed
251 252
      gtk_widget_grab_default (GTK_INPUT_DIALOG (inputd)->close_button);
      gtk_widget_show(hbbox);
253

254
      gtk_signal_connect (GTK_OBJECT (GTK_INPUT_DIALOG (inputd)->save_button),
255
			  "clicked",
256 257
			  GTK_SIGNAL_FUNC (devices_write_rc),
			  NULL);
258
      gtk_signal_connect (GTK_OBJECT (GTK_INPUT_DIALOG (inputd)->close_button),
259
			  "clicked",
260 261
			  GTK_SIGNAL_FUNC (devices_close_callback),
			  inputd);
262

263
      gtk_signal_connect (GTK_OBJECT (inputd), "destroy",
264
			  GTK_SIGNAL_FUNC (gtk_widget_destroyed),
265
			  &inputd);
266

267
      gtk_signal_connect (GTK_OBJECT (inputd), "enable_device",
268 269
			  GTK_SIGNAL_FUNC (input_dialog_able_callback),
			  NULL);
270
      gtk_signal_connect (GTK_OBJECT (inputd), "disable_device",
271 272
			  GTK_SIGNAL_FUNC (input_dialog_able_callback),
			  NULL);
273 274 275 276

      /*  Connect the "F1" help key  */
      gimp_help_connect_help_accel (inputd,
				    gimp_standard_help_func,
277
				    "dialogs/input_devices.html");
278

279 280 281 282
      gtk_widget_show (inputd);
    }
  else
    {
283 284
      if (!GTK_WIDGET_MAPPED (inputd))
	gtk_widget_show (inputd);
285
      else
286
	gdk_window_raise (inputd->window);
287 288 289
    }
}

290
static void
291 292
input_dialog_able_callback (GtkWidget *widget,
			    guint32    deviceid,
293
			    gpointer   data)
294 295 296 297 298 299 300
{
  device_status_update (deviceid);
}

void 
devices_init (void)
{
301 302 303
  GdkDeviceInfo *gdk_info;
  DeviceInfo    *device_info;
  GList         *list;
304

305 306
  /*  create device info structures for present devices */
  for (list = gdk_input_list_devices (); list; list = g_list_next (list))
307
    {
308
      gdk_info = (GdkDeviceInfo *) list->data;
309

310
      device_info = g_new (DeviceInfo, 1);
311

312 313
      device_info->device     = gdk_info->deviceid;
      device_info->name       = g_strdup (gdk_info->name);
314 315
      device_info->is_present = TRUE;

316 317 318 319 320 321 322 323 324 325 326 327 328
      device_info->mode       = gdk_info->mode;
      device_info->num_axes   = gdk_info->num_axes;
      device_info->axes       = NULL;

      device_info->context    = gimp_context_new (device_info->name, NULL);
      gimp_context_define_args (device_info->context,
				DEVICE_CONTEXT_MASK,
				FALSE);
      gimp_context_copy_args (gimp_context_get_user (), device_info->context,
			      DEVICE_CONTEXT_MASK);
      device_status_context_connect (device_info->context, device_info->device);

      device_info_list = g_list_append (device_info_list, device_info);
329
    }
330 331 332
}

void
333
devices_restore (void)
334
{
335
  DeviceInfo *device_info;
336 337
  GimpContext *context;
  gchar *filename;
338

339
  /* Augment with information from rc file */
340 341 342
  filename = gimp_personal_rc_file ("devicerc");
  parse_gimprc_file (filename);
  g_free (filename);
343

344 345
  if ((device_info = device_info_get_by_id (current_device)) == NULL)
    return;
346

347 348
  suppress_update = TRUE;

349 350 351 352
  context = gimp_context_get_user ();

  gimp_context_copy_args (device_info->context, context, DEVICE_CONTEXT_MASK);
  gimp_context_set_parent (device_info->context, context);
353 354
  
  suppress_update = FALSE;
355 356 357
}

void
358 359 360 361 362 363 364 365 366
devices_rc_update (gchar        *name, 
		   DeviceValues  values,
		   GdkInputMode  mode, 
		   gint          num_axes,
		   GdkAxisUse   *axes, 
		   gint          num_keys, 
		   GdkDeviceKey *keys,
		   ToolType      tool,
		   guchar        foreground[], 
367 368 369 370
		   guchar        background[],
		   gchar        *brush_name, 
		   gchar        *pattern_name,
		   gchar        *gradient_name)
371 372 373
{
  DeviceInfo *device_info;

374 375
  /*  Find device if we have it  */
  device_info = device_info_get_by_name (name);
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

  if (!device_info)
    {
      device_info = g_new (DeviceInfo, 1);
      device_info->name = g_strdup (name);
      device_info->is_present = FALSE;

      if (values & DEVICE_AXES)
	{
	  device_info->num_axes = num_axes;
	  device_info->axes = g_new (GdkAxisUse, num_axes);
	  memcpy (device_info->axes, axes, num_axes * sizeof (GdkAxisUse));
	}
      else
	{
	  device_info->num_axes = 0;
	  device_info->axes = NULL;
	}

      if (values & DEVICE_KEYS)
	{
	  device_info->num_keys = num_keys;
	  device_info->keys = g_new (GdkDeviceKey, num_keys);
	  memcpy (device_info->keys, axes, num_keys * sizeof (GdkDeviceKey));
	}

      if (values & DEVICE_MODE)
	device_info->mode = mode;
      else
	device_info->mode = GDK_MODE_DISABLED;

407 408 409 410 411 412 413
      device_info->context = gimp_context_new (device_info->name, NULL);
      gimp_context_define_args (device_info->context,
				DEVICE_CONTEXT_MASK,
				FALSE);
      gimp_context_copy_args (gimp_context_get_user (), device_info->context,
			      DEVICE_CONTEXT_MASK);
      device_status_context_connect (device_info->context, device_info->device);
414

415
      device_info_list = g_list_append (device_info_list, device_info);
416 417 418
    }
  else
    {
419
      GdkDeviceInfo *gdk_info;
420

421
      gdk_info = gdk_device_info_get_by_id (device_info->device);
422 423 424 425 426 427 428 429

      if (gdk_info != NULL)
	{
	  if (values & DEVICE_MODE)
	    gdk_input_set_mode (gdk_info->deviceid, mode);
	  
	  if ((values & DEVICE_AXES) && num_axes >= gdk_info->num_axes)
	    gdk_input_set_axes (gdk_info->deviceid, axes);
430

431 432
	  if ((values & DEVICE_KEYS) && num_keys >= gdk_info->num_keys)
	    {
433
	      gint i;
434 435 436 437 438 439 440 441
	      
	      for (i=0; i<MAX (num_keys, gdk_info->num_keys); i++)
		gdk_input_set_key (gdk_info->deviceid, i,
				   keys[i].keyval, keys[i].modifiers);
	    }
	}
      else
	{
Sven Neumann's avatar
Sven Neumann committed
442 443
	  g_warning ("devices_rc_update called multiple times "
		     "for not present device\n");
444 445 446 447
	  return;
	}
    }

448 449 450 451
  if (values & DEVICE_TOOL)
    {
      gimp_context_set_tool (device_info->context, tool);
    }
452

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
  if (values & DEVICE_FOREGROUND)
    {
      gimp_context_set_foreground (device_info->context,
				   foreground[0],
				   foreground[1],
				   foreground[2]);
    }

  if (values & DEVICE_BACKGROUND)
    {
      gimp_context_set_background (device_info->context,
				   background[0],
				   background[1],
				   background[2]);
    }
468

469 470
  if (values & DEVICE_BRUSH)
    {
471 472 473 474 475 476 477 478 479 480 481 482 483
      GimpBrush *brush;

      brush = gimp_brush_list_get_brush (brush_list, brush_name);

      if (brush)
	{
	  gimp_context_set_brush (device_info->context, brush);
	}
      else if (no_data)
	{
	  g_free (device_info->context->brush_name);
	  device_info->context->brush_name = g_strdup (brush_name);
	}
484 485
    }

486 487
  if (values & DEVICE_PATTERN)
    {
488 489 490 491 492 493 494 495 496 497 498 499 500
      GPattern *pattern;

      pattern = pattern_list_get_pattern (pattern_list, pattern_name);

      if (pattern)
	{
	  gimp_context_set_pattern (device_info->context, pattern);
	}
      else if (no_data)
	{
	  g_free (device_info->context->pattern_name);
	  device_info->context->pattern_name = g_strdup (pattern_name);
	}
501 502
    }

503
  if (values & DEVICE_GRADIENT)
504
    {
505 506 507 508 509 510 511 512 513 514 515 516 517
      gradient_t *gradient;

      gradient = gradient_list_get_gradient (gradients_list, gradient_name);

      if (gradient)
	{
	  gimp_context_set_gradient (device_info->context, gradient);
	}
      else if (no_data)
	{
	  g_free (device_info->context->gradient_name);
	  device_info->context->gradient_name = g_strdup (gradient_name);
	}
518 519 520
    }
}

521
void
522 523 524
select_device (guint32 new_device)
{
  DeviceInfo *device_info;
525 526 527 528 529
  GimpContext *context;

  device_info = device_info_get_by_id (current_device);

  gimp_context_unset_parent (device_info->context);
530 531 532

  suppress_update = TRUE;
  
533
  device_info = device_info_get_by_id (new_device);
534

535
  current_device = new_device;
536

537
  context = gimp_context_get_user ();
538

539 540
  gimp_context_copy_args (device_info->context, context, DEVICE_CONTEXT_MASK);
  gimp_context_set_parent (device_info->context, context);
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

  suppress_update = FALSE;

  device_status_update_current ();
}

gint
devices_check_change (GdkEvent *event)
{
  guint32 device;

  switch (event->type)
    {
    case GDK_MOTION_NOTIFY:
      device = ((GdkEventMotion *)event)->deviceid;
      break;
    case GDK_BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
      device = ((GdkEventButton *)event)->deviceid;
      break;
    case GDK_PROXIMITY_OUT:
      device = ((GdkEventProximity *)event)->deviceid;
      break;
    default:
      device = current_device;
    }
567

568 569 570 571 572 573 574
  if (device != current_device)
    {
      select_device (device);
      return TRUE;
    }
  else
    {
575
      return FALSE;
576 577 578 579
    }
}

static void
580
devices_write_rc_device (DeviceInfo *device_info,
581
			 FILE       *fp)
582
{
583 584 585
  GdkDeviceInfo *gdk_info = NULL;
  gchar *mode = NULL;
  gint i;
586 587

  if (device_info->is_present)
588
    gdk_info = gdk_device_info_get_by_id (device_info->device);
589
  
590
  fprintf (fp, "(device \"%s\"", device_info->name);
591 592 593 594 595 596 597 598 599 600 601 602 603 604

  switch (gdk_info ? gdk_info->mode : device_info->mode)
    {
    case GDK_MODE_DISABLED:
      mode = "disabled";
      break;
    case GDK_MODE_SCREEN:
      mode = "screen";
      break;
    case GDK_MODE_WINDOW:
      mode = "window";
      break;
    }
  
605
  fprintf (fp, "\n        (mode %s)", mode);
606

607 608
  fprintf (fp, "\n        (axes %d",
	   gdk_info ? gdk_info->num_axes : device_info->num_axes);
609

610
  for (i=0; i< (gdk_info ? gdk_info->num_axes : device_info->num_axes); i++)
611 612
    {
      gchar *axis_type = NULL;	/* Quiet gcc */
613

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
      switch (gdk_info ? gdk_info->axes[i] : device_info->axes[i])
	{
	case GDK_AXIS_IGNORE:
	  axis_type = "ignore";
	  break;
	case GDK_AXIS_X:
	  axis_type = "x";
	  break;
	case GDK_AXIS_Y:
	  axis_type = "y";
	  break;
	case GDK_AXIS_PRESSURE:
	  axis_type = "pressure";
	  break;
	case GDK_AXIS_XTILT:
	  axis_type = "xtilt";
	  break;
	case GDK_AXIS_YTILT:
	  axis_type = "ytilt";
	  break;
634 635 636 637 638
#ifdef GTK_HAVE_SIX_VALUATORS
	case GDK_AXIS_WHEEL:
	  axis_type = "wheel";
	  break;
#endif /* GTK_HAVE_SIX_VALUATORS */
639
	}
640
      fprintf (fp, " %s",axis_type);
641
    }
642
  fprintf (fp,")");
643

644 645
  fprintf (fp, "\n        (keys %d",
	   gdk_info ? gdk_info->num_keys : device_info->num_keys);
646

647
  for (i=0; i < (gdk_info ? gdk_info->num_keys : device_info->num_keys); i++)
648 649 650 651 652
    {
      GdkModifierType modifiers = gdk_info ? gdk_info->keys[i].modifiers :
	device_info->keys[i].modifiers;
      guint keyval = gdk_info ? gdk_info->keys[i].keyval :
	device_info->keys[i].keyval;
653

654 655 656
      if (keyval)
	{
	  /* FIXME: integrate this back with menus_install_accelerator */
657 658
	  gchar accel[64];
	  gchar t2[2];
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675

	  accel[0] = '\0';
	  if (modifiers & GDK_CONTROL_MASK)
	    strcat (accel, "<control>");
	  if (modifiers & GDK_SHIFT_MASK)
	    strcat (accel, "<shift>");
	  if (modifiers & GDK_MOD1_MASK)
	    strcat (accel, "<alt>");
	  
	  t2[0] = keyval;
	  t2[1] = '\0';
	  strcat (accel, t2);
	  fprintf (fp, " \"%s\"",accel);
	}
      else
	fprintf (fp, " \"\"");
    }
676
  fprintf (fp,")");
677

678 679 680
  /* Fixme: hard coded last tool....  see gimprc */
  if (gimp_context_get_tool (device_info->context) >= FIRST_TOOLBOX_TOOL &&
      gimp_context_get_tool (device_info->context) <= LAST_TOOLBOX_TOOL)
681
    {
682 683
      fprintf (fp, "\n        (tool \"%s\")",
	       tool_info[gimp_context_get_tool (device_info->context)].tool_name);
684
    }
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703

  {
    guchar r, g, b;
    gimp_context_get_foreground (device_info->context, &r, &g, &b);
    fprintf (fp, "\n        (foreground %d %d %d)", r, g, b);
  }

  if (gimp_context_get_brush (device_info->context))
    {
      fprintf (fp, "\n        (brush \"%s\")",
	       gimp_context_get_brush (device_info->context)->name);
    }

  if (gimp_context_get_pattern (device_info->context))
    {
      fprintf (fp, "\n        (pattern \"%s\")",
	       gimp_context_get_pattern (device_info->context)->name);
    }

704 705 706 707 708 709
  if (gimp_context_get_gradient (device_info->context))
    {
      fprintf (fp, "\n        (gradient \"%s\")",
	       gimp_context_get_gradient (device_info->context)->name);
    }

710 711 712 713 714 715
  fprintf(fp,")\n");
}

static void
devices_write_rc (void)
{
716 717
  DeviceInfo *device_info;
  gchar *filename;
718 719
  FILE *fp;

720
  device_info = device_info_get_by_id (current_device);
721

722 723 724
  filename = gimp_personal_rc_file ("devicerc");
  fp = fopen (filename, "wb");
  g_free (filename);
725

726 727
  if (!fp)
    return;
728

729
  g_list_foreach (device_info_list, (GFunc) devices_write_rc_device, fp);
730

731
  fclose (fp);
732 733 734
}

void 
735
device_status_create (void)
736 737
{
  DeviceInfo *device_info;
738 739 740
  GtkWidget *label;
  GList *list;
  gint i;
741 742 743 744 745
  
  if (deviceD == NULL)
    {
      deviceD = g_new (DeviceInfoDialog, 1);

746 747 748
      deviceD->shell =
	gimp_dialog_new (_("Device Status"), "device_status",
			 gimp_standard_help_func,
749
			 "dialogs/device_status.html",
750 751 752 753
			 GTK_WIN_POS_NONE,
			 FALSE, FALSE, TRUE,

			 _("Save"), (GtkSignalFunc) devices_write_rc,
754
			 NULL, NULL, NULL, FALSE, FALSE,
755
			 _("Close"), devices_close_callback,
756
			 NULL, NULL, NULL, TRUE, TRUE,
757

758 759 760 761 762
			 NULL);

      dialog_register (deviceD->shell);
      session_set_window_geometry (deviceD->shell, &device_status_session_info,
				   FALSE);
763 764

      deviceD->num_devices = 0;
765 766

      for (list = device_info_list; list; list = g_list_next (list))
767
	{
768
	  if (((DeviceInfo *) list->data)->is_present)
769 770
	    deviceD->num_devices++;
	}
771 772

      /*  devices table  */
773
      deviceD->table = gtk_table_new (deviceD->num_devices, 6, FALSE);
774 775
      gtk_container_set_border_width (GTK_CONTAINER (deviceD->table), 3);
      gtk_container_add (GTK_CONTAINER (GTK_DIALOG (deviceD->shell)->vbox),
776 777 778 779
			 deviceD->table);
      gtk_widget_realize (deviceD->table);
      gtk_widget_show (deviceD->table);

780 781 782 783 784 785
      deviceD->ids        = g_new (guint32, deviceD->num_devices);
      deviceD->frames     = g_new (GtkWidget *, deviceD->num_devices);
      deviceD->tools      = g_new (GtkWidget *, deviceD->num_devices);
      deviceD->colors     = g_new (GtkWidget *, deviceD->num_devices);
      deviceD->brushes    = g_new (GtkWidget *, deviceD->num_devices);
      deviceD->patterns   = g_new (GtkWidget *, deviceD->num_devices);
786
      deviceD->gradients  = g_new (GtkWidget *, deviceD->num_devices);
787
      deviceD->eventboxes = g_new (GtkWidget *, deviceD->num_devices);
788

789
      for (list = device_info_list, i = 0; list; list = g_list_next (list), i++)
790
	{
791 792 793 794
	  if (!((DeviceInfo *) list->data)->is_present)
	    continue;

	  device_info = (DeviceInfo *) list->data;
795 796 797

	  deviceD->ids[i] = device_info->device;

798 799
	  /*  the device name  */

800
	  deviceD->frames[i] = gtk_frame_new (NULL);
801 802 803

	  gtk_frame_set_shadow_type (GTK_FRAME(deviceD->frames[i]),
				     GTK_SHADOW_OUT);
804 805 806 807 808 809 810 811 812
	  gtk_table_attach (GTK_TABLE(deviceD->table), deviceD->frames[i],
			    0, 1, i, i+1,
			    GTK_FILL, GTK_FILL, 2, 0);

	  label = gtk_label_new (device_info->name);
	  gtk_misc_set_padding (GTK_MISC(label), 2, 0);
	  gtk_container_add (GTK_CONTAINER(deviceD->frames[i]), label);
	  gtk_widget_show(label);

813 814
	  /*  the tool  */

815
	  deviceD->eventboxes[i] = gtk_event_box_new();
816

817 818
	  deviceD->tools[i] = gtk_pixmap_new (tool_get_pixmap (RECT_SELECT), 
					      tool_get_mask (RECT_SELECT));
819
	  
820
	  gtk_drag_source_set (deviceD->eventboxes[i],
821
			       GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
822 823 824 825 826 827 828 829 830 831 832 833 834 835
			       tool_target_table, n_tool_targets,
			       GDK_ACTION_COPY);
	  gimp_dnd_tool_source_set (deviceD->eventboxes[i],
				    device_status_drag_tool, 
				    GUINT_TO_POINTER (device_info->device));
 	  gtk_drag_dest_set (deviceD->eventboxes[i],
 			     GTK_DEST_DEFAULT_HIGHLIGHT |
			     GTK_DEST_DEFAULT_MOTION |
 			     GTK_DEST_DEFAULT_DROP,
 			     tool_target_table, n_tool_targets,
 			     GDK_ACTION_COPY); 
 	  gimp_dnd_tool_dest_set (deviceD->eventboxes[i],
				  device_status_drop_tool, 
				  GUINT_TO_POINTER (device_info->device));
836 837 838
	  gtk_container_add (GTK_CONTAINER (deviceD->eventboxes[i]),
			     deviceD->tools[i]);
	  gtk_table_attach (GTK_TABLE (deviceD->table), deviceD->eventboxes[i],
839 840 841
			    1, 2, i, i+1,
			    0, 0, 2, 2);

842 843
	  /*  the foreground color  */

844
	  deviceD->colors[i] = gtk_preview_new (GTK_PREVIEW_COLOR);
845
	  gtk_widget_set_events (deviceD->colors[i], PREVIEW_EVENT_MASK);
846 847
	  gtk_preview_size (GTK_PREVIEW (deviceD->colors[i]),
			    CELL_SIZE, CELL_SIZE);
Sven Neumann's avatar
Sven Neumann committed
848
	  gtk_drag_source_set (deviceD->colors[i],
849
			       GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
Sven Neumann's avatar
Sven Neumann committed
850
			       color_area_target_table, n_color_area_targets,
Sven Neumann's avatar
Sven Neumann committed
851
			       GDK_ACTION_COPY);
852 853
	  gimp_dnd_color_source_set (deviceD->colors[i],
				     device_status_drag_color, 
Sven Neumann's avatar
Sven Neumann committed
854 855 856
				     GUINT_TO_POINTER (device_info->device));
 	  gtk_drag_dest_set (deviceD->colors[i],
 			     GTK_DEST_DEFAULT_HIGHLIGHT |
857
			     GTK_DEST_DEFAULT_MOTION |
Sven Neumann's avatar
Sven Neumann committed
858 859 860 861 862
 			     GTK_DEST_DEFAULT_DROP,
 			     color_area_target_table, n_color_area_targets,
 			     GDK_ACTION_COPY); 
 	  gimp_dnd_color_dest_set (deviceD->colors[i], device_status_drop_color, 
				   GUINT_TO_POINTER (device_info->device));
863 864 865 866
	  gtk_table_attach (GTK_TABLE(deviceD->table), deviceD->colors[i],
			    2, 3, i, i+1,
			    0, 0, 2, 2);

867 868
	  /*  the brush  */

869 870 871
	  deviceD->brushes[i] =
	    gimp_context_preview_new (GCP_BRUSH, 
				      CELL_SIZE, CELL_SIZE,
872
				      FALSE, TRUE,
873 874
				      GTK_SIGNAL_FUNC (device_status_drop_brush),
				      GUINT_TO_POINTER (device_info->device));
875 876 877 878
	  gtk_table_attach (GTK_TABLE(deviceD->table), deviceD->brushes[i],
			    3, 4, i, i+1,
			    0, 0, 2, 2);

879 880
	  /*  the pattern  */

881 882 883
	  deviceD->patterns[i] =
	    gimp_context_preview_new (GCP_PATTERN,
				      CELL_SIZE, CELL_SIZE, 
884
				      FALSE, TRUE,
885 886
				      GTK_SIGNAL_FUNC (device_status_drop_pattern),
				      GUINT_TO_POINTER (device_info->device));
887 888 889 890
	  gtk_table_attach (GTK_TABLE(deviceD->table), deviceD->patterns[i],
			    4, 5, i, i+1,
			    0, 0, 2, 2);

891 892 893 894 895
	  /*  the gradient  */

	  deviceD->gradients[i] =
	    gimp_context_preview_new (GCP_GRADIENT,
				      CELL_SIZE * 2, CELL_SIZE, 
896
				      FALSE, TRUE,
897 898 899 900 901 902
				      GTK_SIGNAL_FUNC (device_status_drop_gradient),
				      GUINT_TO_POINTER (device_info->device));
	  gtk_table_attach (GTK_TABLE(deviceD->table), deviceD->gradients[i],
			    5, 6, i, i+1,
			    0, 0, 2, 2);

903
	  device_status_update (device_info->device);
904 905 906 907 908 909 910 911
	}

      deviceD->current = 0xffffffff; /* random, but doesn't matter */
      device_status_update_current ();

      gtk_widget_show (deviceD->shell);

      gtk_signal_connect (GTK_OBJECT (deviceD->shell), "destroy",
912
			  GTK_SIGNAL_FUNC (device_status_destroy_callback),
913 914 915 916
			  NULL);
    }
  else
    {
917 918
      if (!GTK_WIDGET_MAPPED (deviceD->shell))
	gtk_widget_show (deviceD->shell);
919
      else
920
	gdk_window_raise (deviceD->shell->window);
921 922 923 924 925 926
    }
}

static void
device_status_destroy_callback (void)
{
927 928 929 930 931 932 933
  g_free (deviceD->ids);
  g_free (deviceD->frames);
  g_free (deviceD->tools);
  g_free (deviceD->eventboxes);
  g_free (deviceD->colors);
  g_free (deviceD->brushes);
  g_free (deviceD->patterns);
934
  g_free (deviceD->gradients);
935 936

  g_free (deviceD);
937 938 939 940
  deviceD = NULL;
}

static void
941
devices_close_callback (GtkWidget *widget,
942
			gpointer   data)
943
{
944
  gtk_widget_hide (GTK_WIDGET (data));
945 946
}

947 948 949
void
device_status_free (void)
{                                     
950
  /* Save device status on exit */
951 952
  if (save_device_status)
    devices_write_rc ();
953

954 955 956 957 958 959 960
  if (deviceD)
    {
      session_get_window_info (deviceD->shell, &device_status_session_info);
      device_status_destroy_callback (); 
    }
}

961
static void
962
device_status_update_current (void)
963
{
964
  gint i;
965 966 967

  if (deviceD)
    {
968
      for (i = 0; i < deviceD->num_devices; i++)
969 970 971 972 973 974 975 976
	{
	  if (deviceD->ids[i] == deviceD->current)
	    gtk_frame_set_shadow_type (GTK_FRAME(deviceD->frames[i]), 
				       GTK_SHADOW_OUT);
	  else if (deviceD->ids[i] == current_device)
	    gtk_frame_set_shadow_type (GTK_FRAME(deviceD->frames[i]), 
				       GTK_SHADOW_IN);
	}
977

978 979 980 981 982 983 984 985
      deviceD->current = current_device;
    }
}

void 
device_status_update (guint32 deviceid)
{
  GdkDeviceInfo *gdk_info;
986 987 988 989
  DeviceInfo    *device_info;
  guchar buffer[CELL_SIZE*3];
  gchar  ttbuf[20]; /* [xxx,xxx,xxx] + null */
  gint   i, j;
990

991 992 993 994 995 996 997 998 999 1000
  if (!deviceD || suppress_update)
    return;

  if ((device_info = device_info_get_by_id (deviceid)) == NULL)
    return;

  if ((gdk_info = gdk_device_info_get_by_id (deviceid)) == NULL)
    return;

  for (i = 0; i < deviceD->num_devices; i++)
1001
    {
1002 1003 1004
      if (deviceD->ids[i] == deviceid)
	break;
    }
1005

1006
  g_return_if_fail (i < deviceD->num_devices);
1007

1008 1009 1010 1011 1012 1013 1014 1015
  if (gdk_info->mode == GDK_MODE_DISABLED)
    {
      gtk_widget_hide (deviceD->frames[i]);
      gtk_widget_hide (deviceD->tools[i]);
      gtk_widget_hide (deviceD->eventboxes[i]);
      gtk_widget_hide (deviceD->colors[i]);
      gtk_widget_hide (deviceD->brushes[i]);
      gtk_widget_hide (deviceD->patterns[i]);
1016
      gtk_widget_hide (deviceD->gradients[i]);
1017 1018 1019 1020
    }
  else
    {
      gtk_widget_show (deviceD->frames[i]);
1021

1022 1023
      gtk_pixmap_set (GTK_PIXMAP (deviceD->tools[i]), 
		      tool_get_pixmap (gimp_context_get_tool (device_info->context)),
1024
		      tool_get_mask (gimp_context_get_tool (device_info->context)));
1025 1026 1027 1028 1029

      gtk_widget_draw (deviceD->tools[i], NULL);
      gtk_widget_show (deviceD->tools[i]);
      gtk_widget_show (deviceD->eventboxes[i]);

1030 1031 1032
      gimp_help_set_help_data (deviceD->eventboxes[i],
			       tool_info[(gint) gimp_context_get_tool (device_info->context)].tool_desc,
			       tool_info[(gint) gimp_context_get_tool (device_info->context)].private_tip);
1033 1034

      for (j = 0; j < CELL_SIZE * 3; j += 3)
1035
	{
1036 1037 1038 1039
	  gimp_context_get_foreground (device_info->context,
				       &buffer[j],
				       &buffer[j+1],
				       &buffer[j+2]);
1040 1041
	}

1042 1043 1044 1045 1046
      for (j = 0; j < CELL_SIZE; j++)
	gtk_preview_draw_row (GTK_PREVIEW(deviceD->colors[i]), buffer, 
			      0, j, CELL_SIZE);
      gtk_widget_draw (deviceD->colors[i], NULL);
      gtk_widget_show (deviceD->colors[i]);
1047

1048
      /*  Set the tip to be the RGB value  */
Michael Natterer's avatar
Michael Natterer committed
1049 1050 1051 1052
      g_snprintf (ttbuf, sizeof (ttbuf), "[%3d,%3d,%3d]",
		  buffer[j],
		  buffer[j+1],
		  buffer[j+2]);
1053

1054
      gimp_help_set_help_data (deviceD->colors[i], ttbuf, NULL);
1055 1056 1057 1058 1059 1060 1061

      if (gimp_context_get_brush (device_info->context))
	{
	  gimp_context_preview_update
	    (GIMP_CONTEXT_PREVIEW (deviceD->brushes[i]), 
	     gimp_context_get_brush (device_info->context));
	  gtk_widget_show (deviceD->brushes[i]);
1062
	}
1063

1064 1065 1066 1067 1068 1069 1070
      if (gimp_context_get_pattern (device_info->context))
	{
	  gimp_context_preview_update
	    (GIMP_CONTEXT_PREVIEW (deviceD->patterns[i]), 
	     gimp_context_get_pattern (device_info->context));
	  gtk_widget_show (deviceD->patterns[i]);
	}
1071 1072 1073 1074 1075 1076 1077 1078

      if (gimp_context_get_gradient (device_info->context))
	{
	  gimp_context_preview_update
	    (GIMP_CONTEXT_PREVIEW (deviceD->gradients[i]), 
	     gimp_context_get_gradient (device_info->context));
	  gtk_widget_show (deviceD->gradients[i]);
	}
1079
    }
1080
}
1081

Sven Neumann's avatar
Sven Neumann committed
1082
/*  dnd stuff  */
1083

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
static ToolType
device_status_drag_tool (GtkWidget *widget,
			 gpointer   data)
{
  DeviceInfo *device_info;

  device_info = device_info_get_by_id (GPOINTER_TO_UINT (data));

  if (device_info)
    {
      return gimp_context_get_tool (device_info->context);
    }
  else
    {
      return RECT_SELECT;
    }
}

static void
device_status_drop_tool (GtkWidget *widget,
			 ToolType   tool,
			 gpointer   data)
{
  DeviceInfo *device_info;

  device_info = device_info_get_by_id (GPOINTER_TO_UINT (data));

  if (device_info && device_info->is_present)
    {
      gimp_context_set_tool (device_info->context, tool);
    }
}

Sven Neumann's avatar
Sven Neumann committed
1117 1118 1119 1120 1121 1122 1123
static void
device_status_drag_color (GtkWidget *widget,
			  guchar    *r,
			  guchar    *g,
			  guchar    *b,
			  gpointer   data)
{
1124
  DeviceInfo *device_info;
Sven Neumann's avatar
Sven Neumann committed
1125

1126
  device_info = device_info_get_by_id (GPOINTER_TO_UINT (data));
Sven Neumann's avatar
Sven Neumann committed
1127 1128 1129

  if (device_info)
    {
1130
      gimp_context_get_foreground (device_info->context, r, g, b);
Sven Neumann's avatar
Sven Neumann committed
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
    }
  else
    {
      *r = *g = *b = 0;
    }
}

static void
device_status_drop_color (GtkWidget *widget,
			  guchar     r,
			  guchar     g,
			  guchar     b,
			  gpointer   data)
{
1145
  DeviceInfo *device_info;
Sven Neumann's avatar
Sven Neumann committed
1146

1147
  device_info = device_info_get_by_id (GPOINTER_TO_UINT (data));
Sven Neumann's avatar
Sven Neumann committed
1148 1149 1150

  if (device_info && device_info->is_present)
    {
1151
      gimp_context_set_foreground (device_info->context, r, g, b);
Sven Neumann's avatar
Sven Neumann committed
1152 1153 1154
    }
}

1155
static void
1156 1157 1158
device_status_drop_brush (GtkWidget *widget,
			  GimpBrush *brush,
			  gpointer   data)
1159
{
1160
  DeviceInfo *device_info;
Sven Neumann's avatar
Sven Neumann committed
1161

1162
  device_info = device_info_get_by_id (GPOINTER_TO_UINT (data));
1163 1164 1165

  if (device_info && device_info->is_present)
    {
1166
      gimp_context_set_brush (device_info->context, brush);
1167 1168 1169 1170
    }
}

static void
1171 1172 1173
device_status_drop_pattern (GtkWidget *widget,
			    GPattern  *pattern,
			    gpointer   data)
1174
{
1175
  DeviceInfo *device_info;
1176 1177
  
  device_info = device_info_get_by_id (GPOINTER_TO_UINT (data));
1178

1179 1180 1181 1182 1183
  if (device_info && device_info->is_present)
    {
      gimp_context_set_pattern (device_info->context, pattern);
    }
}
1184

1185 1186 1187 1188 1189 1190
static void
device_status_drop_gradient (GtkWidget  *widget,
			     gradient_t *gradient,
			     gpointer    data)
{
  DeviceInfo *device_info;
Sven Neumann's avatar
Sven Neumann committed
1191
  
1192
  device_info = device_info_get_by_id (GPOINTER_TO_UINT (data));
Sven Neumann's avatar
Sven Neumann committed
1193

1194 1195
  if (device_info && device_info->is_present)
    {
1196
      gimp_context_set_gradient (device_info->context, gradient);
1197 1198
    }
}
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243

/*  context callbacks  */

static void
device_status_color_changed (GimpContext *context,
			     gint         r,
			     gint         g,
			     gint         b,
			     gpointer     data)
{
  DeviceInfo *device_info;

  device_info = device_info_get_by_id (GPOINTER_TO_UINT (data));

  device_status_update (device_info->device);
}

static void
device_status_data_changed (GimpContext *context,
			    gpointer     dummy,
			    gpointer     data)
{
  DeviceInfo *device_info;

  device_info = device_info_get_by_id (GPOINTER_TO_UINT (data));

  device_status_update (device_info->device);
}

static void
device_status_context_connect  (GimpContext *context,
				guint32      deviceid)
{
  gtk_signal_connect (GTK_OBJECT (context), "foreground_changed",
		      GTK_SIGNAL_FUNC (device_status_color_changed),
		      (gpointer) deviceid);
  gtk_signal_connect (GTK_OBJECT (context), "tool_changed",
		      GTK_SIGNAL_FUNC (device_status_data_changed),
		      (gpointer) deviceid);
  gtk_signal_connect (GTK_OBJECT (context), "brush_changed",
		      GTK_SIGNAL_FUNC (device_status_data_changed),
		      (gpointer) deviceid);
  gtk_signal_connect (GTK_OBJECT (context), "pattern_changed",
		      GTK_SIGNAL_FUNC (device_status_data_changed),
		      (gpointer) deviceid);
1244 1245 1246
  gtk_signal_connect (GTK_OBJECT (context), "gradient_changed",
		      GTK_SIGNAL_FUNC (device_status_data_changed),
		      (gpointer) deviceid);
1247
}