gimpimagemaptool.c 31.3 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Michael Natterer's avatar
Michael Natterer committed
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
Michael Natterer's avatar
Michael Natterer committed
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
Michael Natterer's avatar
Michael Natterer committed
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Michael Natterer's avatar
Michael Natterer committed
16 17
 */

18 19 20 21 22 23 24
/* This file contains a base class for tools that implement on canvas
 * preview for non destructive editing. The processing of the pixels can
 * be done either by a gegl op or by a C function (apply_func).
 *
 * For the core side of this, please see /app/core/gimpimagemap.c.
 */

Michael Natterer's avatar
Michael Natterer committed
25 26
#include "config.h"

27 28
#include <string.h>

29
#include <gegl.h>
Michael Natterer's avatar
Michael Natterer committed
30
#include <gtk/gtk.h>
31
#include <gdk/gdkkeysyms.h>
Michael Natterer's avatar
Michael Natterer committed
32

33
#include "libgimpbase/gimpbase.h"
34
#include "libgimpconfig/gimpconfig.h"
35 36
#include "libgimpwidgets/gimpwidgets.h"

Michael Natterer's avatar
Michael Natterer committed
37 38
#include "tools-types.h"

39 40
#include "config/gimpguiconfig.h"

41
#include "core/gimp.h"
42
#include "core/gimpdrawable.h"
43
#include "core/gimperror.h"
44
#include "core/gimpimage.h"
45
#include "core/gimpimage-pick-color.h"
46
#include "core/gimpimagemap.h"
47
#include "core/gimplist.h"
48
#include "core/gimppickable.h"
49
#include "core/gimpprogress.h"
Michael Natterer's avatar
Michael Natterer committed
50
#include "core/gimpprojection.h"
51
#include "core/gimpsettings.h"
52
#include "core/gimptoolinfo.h"
Michael Natterer's avatar
Michael Natterer committed
53

54
#include "widgets/gimpsettingsbox.h"
55
#include "widgets/gimpwidgets-utils.h"
56 57

#include "display/gimpdisplay.h"
58
#include "display/gimpdisplayshell.h"
59
#include "display/gimptoolgui.h"
60

61
#include "gimpcoloroptions.h"
62
#include "gimpimagemaptool.h"
63
#include "gimpimagemaptool-settings.h"
64
#include "gimptoolcontrol.h"
65
#include "tool_manager.h"
66

67
#include "gimp-intl.h"
Michael Natterer's avatar
Michael Natterer committed
68 69 70 71


/*  local function prototypes  */

72 73 74 75 76
static void      gimp_image_map_tool_class_init     (GimpImageMapToolClass *klass);
static void      gimp_image_map_tool_base_init      (GimpImageMapToolClass *klass);

static void      gimp_image_map_tool_init           (GimpImageMapTool *im_tool);

77
static void      gimp_image_map_tool_constructed    (GObject          *object);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
static void      gimp_image_map_tool_finalize       (GObject          *object);

static gboolean  gimp_image_map_tool_initialize     (GimpTool         *tool,
                                                     GimpDisplay      *display,
                                                     GError          **error);
static void      gimp_image_map_tool_control        (GimpTool         *tool,
                                                     GimpToolAction    action,
                                                     GimpDisplay      *display);
static gboolean  gimp_image_map_tool_key_press      (GimpTool         *tool,
                                                     GdkEventKey      *kevent,
                                                     GimpDisplay      *display);
static void      gimp_image_map_tool_options_notify (GimpTool         *tool,
                                                     GimpToolOptions  *options,
                                                     const GParamSpec *pspec);

static gboolean  gimp_image_map_tool_pick_color     (GimpColorTool    *color_tool,
                                                     gint              x,
                                                     gint              y,
96
                                                     const Babl      **sample_format,
97 98
                                                     GimpRGB          *color,
                                                     gint             *color_index);
99 100
static void      gimp_image_map_tool_color_picked   (GimpColorTool    *color_tool,
                                                     GimpColorPickState pick_state,
101 102
                                                     gdouble           x,
                                                     gdouble           y,
103 104 105 106
                                                     const Babl       *sample_format,
                                                     const GimpRGB    *color,
                                                     gint              color_index);

107 108
static void      gimp_image_map_tool_real_reset     (GimpImageMapTool *im_tool);

109 110 111
static void      gimp_image_map_tool_halt           (GimpImageMapTool *im_tool);
static void      gimp_image_map_tool_commit         (GimpImageMapTool *im_tool);

112 113
static void      gimp_image_map_tool_map            (GimpImageMapTool *im_tool);
static void      gimp_image_map_tool_dialog         (GimpImageMapTool *im_tool);
114 115
static void      gimp_image_map_tool_dialog_unmap   (GtkWidget        *dialog,
                                                     GimpImageMapTool *im_tool);
116
static void      gimp_image_map_tool_reset          (GimpImageMapTool *im_tool);
117
static void      gimp_image_map_tool_create_map     (GimpImageMapTool *im_tool);
118 119 120

static void      gimp_image_map_tool_flush          (GimpImageMap     *image_map,
                                                     GimpImageMapTool *im_tool);
121 122 123
static void      gimp_image_map_tool_config_notify  (GObject          *object,
                                                     const GParamSpec *pspec,
                                                     GimpImageMapTool *im_tool);
124

125
static void      gimp_image_map_tool_response       (GimpToolGui      *gui,
126 127 128
                                                     gint              response_id,
                                                     GimpImageMapTool *im_tool);

129 130 131 132 133 134 135 136

static GimpColorToolClass *parent_class = NULL;


GType
gimp_image_map_tool_get_type (void)
{
  static GType type = 0;
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  if (! type)
    {
      const GTypeInfo info =
      {
        sizeof (GimpImageMapToolClass),
        (GBaseInitFunc) gimp_image_map_tool_base_init,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gimp_image_map_tool_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GimpImageMapTool),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gimp_image_map_tool_init,
      };

      type = g_type_register_static (GIMP_TYPE_COLOR_TOOL,
                                     "GimpImageMapTool",
                                     &info, 0);
    }
Michael Natterer's avatar
Michael Natterer committed
157

158 159
  return type;
}
Michael Natterer's avatar
Michael Natterer committed
160 161 162 163 164


static void
gimp_image_map_tool_class_init (GimpImageMapToolClass *klass)
{
165 166 167
  GObjectClass       *object_class     = G_OBJECT_CLASS (klass);
  GimpToolClass      *tool_class       = GIMP_TOOL_CLASS (klass);
  GimpColorToolClass *color_tool_class = GIMP_COLOR_TOOL_CLASS (klass);
168

169 170
  parent_class = g_type_class_peek_parent (klass);

171
  object_class->constructed  = gimp_image_map_tool_constructed;
172 173 174 175 176
  object_class->finalize     = gimp_image_map_tool_finalize;

  tool_class->initialize     = gimp_image_map_tool_initialize;
  tool_class->control        = gimp_image_map_tool_control;
  tool_class->key_press      = gimp_image_map_tool_key_press;
177
  tool_class->options_notify = gimp_image_map_tool_options_notify;
178 179

  color_tool_class->pick     = gimp_image_map_tool_pick_color;
180
  color_tool_class->picked   = gimp_image_map_tool_color_picked;
181

182
  klass->dialog_desc         = NULL;
183 184 185 186 187 188 189
  klass->settings_name       = NULL;
  klass->import_dialog_title = NULL;
  klass->export_dialog_title = NULL;

  klass->get_operation       = NULL;
  klass->map                 = NULL;
  klass->dialog              = NULL;
190
  klass->reset               = gimp_image_map_tool_real_reset;
191
  klass->get_settings_ui     = gimp_image_map_tool_real_get_settings_ui;
192 193
  klass->settings_import     = gimp_image_map_tool_real_settings_import;
  klass->settings_export     = gimp_image_map_tool_real_settings_export;
Michael Natterer's avatar
Michael Natterer committed
194 195
}

196 197 198
static void
gimp_image_map_tool_base_init (GimpImageMapToolClass *klass)
{
199
  klass->recent_settings = gimp_list_new (GIMP_TYPE_SETTINGS, TRUE);
200
  gimp_list_set_sort_func (GIMP_LIST (klass->recent_settings),
201
                           (GCompareFunc) gimp_settings_compare);
202 203
}

Michael Natterer's avatar
Michael Natterer committed
204 205 206
static void
gimp_image_map_tool_init (GimpImageMapTool *image_map_tool)
{
207
  GimpTool *tool = GIMP_TOOL (image_map_tool);
Michael Natterer's avatar
Michael Natterer committed
208

Michael Natterer's avatar
Michael Natterer committed
209 210
  gimp_tool_control_set_scroll_lock (tool->control, TRUE);
  gimp_tool_control_set_preserve    (tool->control, FALSE);
211 212 213 214
  gimp_tool_control_set_dirty_mask  (tool->control,
                                     GIMP_DIRTY_IMAGE           |
                                     GIMP_DIRTY_IMAGE_STRUCTURE |
                                     GIMP_DIRTY_DRAWABLE        |
215 216
                                     GIMP_DIRTY_SELECTION       |
                                     GIMP_DIRTY_ACTIVE_DRAWABLE);
217 218
}

219 220
static void
gimp_image_map_tool_constructed (GObject *object)
221
{
222
  GimpImageMapTool *image_map_tool = GIMP_IMAGE_MAP_TOOL (object);
223

224
  G_OBJECT_CLASS (parent_class)->constructed (object);
225

226
  gimp_image_map_tool_get_operation (image_map_tool);
227 228
}

229 230 231
static void
gimp_image_map_tool_finalize (GObject *object)
{
232
  GimpImageMapTool *image_map_tool = GIMP_IMAGE_MAP_TOOL (object);
233

234 235 236 237 238 239
  if (image_map_tool->operation)
    {
      g_object_unref (image_map_tool->operation);
      image_map_tool->operation = NULL;
    }

240 241 242 243 244 245
  if (image_map_tool->config)
    {
      g_object_unref (image_map_tool->config);
      image_map_tool->config = NULL;
    }

246 247 248 249 250 251
  if (image_map_tool->default_config)
    {
      g_object_unref (image_map_tool->default_config);
      image_map_tool->default_config = NULL;
    }

252 253 254 255 256 257
  if (image_map_tool->undo_desc)
    {
      g_free (image_map_tool->undo_desc);
      image_map_tool->undo_desc = NULL;
    }

258 259 260 261 262 263 264 265 266 267 268 269
  if (image_map_tool->label_group)
    {
      g_object_unref (image_map_tool->label_group);
      image_map_tool->label_group = NULL;
    }

  if (image_map_tool->gui)
    {
      g_object_unref (image_map_tool->gui);
      image_map_tool->gui          = NULL;
      image_map_tool->settings_box = NULL;
    }
270 271 272 273

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

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
static void
gamma_hack (GtkToggleButton  *button,
            GimpImageMapTool *image_map_tool)
{
  if (image_map_tool->image_map)
    {
      GimpTool *tool = GIMP_TOOL (image_map_tool);

      gimp_tool_control_push_preserve (tool->control, TRUE);

      gimp_image_map_tool_create_map (image_map_tool);
      gimp_image_map_set_gamma_hack (image_map_tool->image_map,
                                     gtk_toggle_button_get_active (button));
      gimp_image_map_tool_preview (image_map_tool);

      gimp_tool_control_pop_preserve (tool->control);
    }
}

293 294
#define RESPONSE_RESET 1

295
static gboolean
296 297 298
gimp_image_map_tool_initialize (GimpTool     *tool,
                                GimpDisplay  *display,
                                GError      **error)
299
{
300
  GimpImageMapTool *image_map_tool = GIMP_IMAGE_MAP_TOOL (tool);
301
  GimpToolInfo     *tool_info      = tool->tool_info;
302 303
  GimpImage        *image          = gimp_display_get_image (display);
  GimpDrawable     *drawable       = gimp_image_get_active_drawable (image);
304
  GimpDisplayShell *shell          = gimp_display_get_shell (display);
305

306 307 308
  if (! drawable)
    return FALSE;

309 310 311
  if (gimp_viewable_get_children (GIMP_VIEWABLE (drawable)))
    {
      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
312
			   _("Cannot modify the pixels of layer groups."));
313 314 315
      return FALSE;
    }

316
  if (gimp_item_is_content_locked (GIMP_ITEM (drawable)))
317 318 319 320 321 322
    {
      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
			   _("The active layer's pixels are locked."));
      return FALSE;
    }

323 324 325 326 327 328 329
  if (! gimp_item_is_visible (GIMP_ITEM (drawable)))
    {
      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
                           _("The active layer is not visible."));
      return FALSE;
    }

330 331 332 333
  if (image_map_tool->active_picker)
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (image_map_tool->active_picker),
                                  FALSE);

334 335
  /*  set display so the dialog can be hidden on display destruction  */
  tool->display = display;
336

337 338 339
  if (image_map_tool->config)
    gimp_config_reset (GIMP_CONFIG (image_map_tool->config));

340
  if (! image_map_tool->gui)
341
    {
342 343
      GimpImageMapToolClass *klass;
      GtkWidget             *vbox;
344
      GtkWidget             *combo;
345 346 347
      GtkWidget             *toggle;

      klass = GIMP_IMAGE_MAP_TOOL_GET_CLASS (image_map_tool);
348

349 350
      /*  disabled for at least GIMP 2.8  */
      image_map_tool->overlay = FALSE;
351

352 353 354
      image_map_tool->gui =
        gimp_tool_gui_new (tool_info,
                           klass->dialog_desc,
355 356
                           gtk_widget_get_screen (GTK_WIDGET (shell)),
                           gimp_widget_get_monitor (GTK_WIDGET (shell)),
357
                           image_map_tool->overlay,
358

359 360 361
                           GIMP_STOCK_RESET, RESPONSE_RESET,
                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                           GTK_STOCK_OK,     GTK_RESPONSE_OK,
362

363
                           NULL);
364

365 366 367 368 369 370 371
      gimp_tool_gui_set_default_response (image_map_tool->gui, GTK_RESPONSE_OK);

      gimp_tool_gui_set_alternative_button_order (image_map_tool->gui,
                                                  RESPONSE_RESET,
                                                  GTK_RESPONSE_OK,
                                                  GTK_RESPONSE_CANCEL,
                                                  -1);
372

373
      vbox = gimp_tool_gui_get_vbox (image_map_tool->gui);
374

375
      g_signal_connect_object (image_map_tool->gui, "response",
376 377 378
                               G_CALLBACK (gimp_image_map_tool_response),
                               G_OBJECT (image_map_tool), 0);

379
      if (image_map_tool->config && klass->settings_name)
380
        {
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
          GtkWidget *settings_ui;
          gchar     *settings_filename;
          gchar     *default_folder;

          settings_filename =
            gimp_tool_info_build_options_filename (tool_info, ".settings");

          default_folder =
            g_build_filename (gimp_directory (), klass->settings_name, NULL);

          settings_ui = klass->get_settings_ui (image_map_tool,
                                                klass->recent_settings,
                                                settings_filename,
                                                klass->import_dialog_title,
                                                klass->export_dialog_title,
                                                tool_info->help_id,
                                                default_folder,
                                                &image_map_tool->settings_box);

          g_free (settings_filename);
          g_free (default_folder);

403
          gtk_box_pack_start (GTK_BOX (vbox), settings_ui, FALSE, FALSE, 0);
404 405
          gtk_widget_show (settings_ui);
        }
406

407
      /*  The gamma hack toggle  */
408 409 410 411 412 413 414 415
      toggle = gtk_check_button_new_with_label ("Gamma hack (temp hack, please ignore)");
      gtk_box_pack_end (GTK_BOX (vbox), toggle, FALSE, FALSE, 0);
      gtk_widget_show (toggle);

      g_signal_connect (toggle, "toggled",
                        G_CALLBACK (gamma_hack),
                        image_map_tool);

416
      /*  The preview toggle  */
417 418 419
      toggle = gimp_prop_check_button_new (G_OBJECT (tool_info->tool_options),
                                           "preview",
                                           _("_Preview"));
420
      gtk_box_pack_end (GTK_BOX (vbox), toggle, FALSE, FALSE, 0);
421 422
      gtk_widget_show (toggle);

423 424 425 426 427 428 429
      /*  The area combo  */
      combo = gimp_prop_enum_combo_box_new (G_OBJECT (tool_info->tool_options),
                                            "region",
                                            0, 0);
      gtk_box_pack_end (GTK_BOX (vbox), combo, FALSE, FALSE, 0);
      gtk_widget_show (combo);

430 431 432 433
      /*  Fill in subclass widgets  */
      gimp_image_map_tool_dialog (image_map_tool);
    }

434
  gimp_tool_gui_set_shell (image_map_tool->gui, shell);
435
  gimp_tool_gui_set_viewable (image_map_tool->gui, GIMP_VIEWABLE (drawable));
436

437
  gimp_tool_gui_show (image_map_tool->gui);
438

439
  image_map_tool->drawable = drawable;
440

441
  gimp_image_map_tool_create_map (image_map_tool);
442
  gimp_image_map_tool_preview (image_map_tool);
443

444
  return TRUE;
445 446 447 448
}

static void
gimp_image_map_tool_control (GimpTool       *tool,
449
                             GimpToolAction  action,
450
                             GimpDisplay    *display)
451
{
452
  GimpImageMapTool *image_map_tool = GIMP_IMAGE_MAP_TOOL (tool);
453 454 455

  switch (action)
    {
456 457 458 459 460
    case GIMP_TOOL_ACTION_PAUSE:
    case GIMP_TOOL_ACTION_RESUME:
      break;

    case GIMP_TOOL_ACTION_HALT:
461 462
      gimp_image_map_tool_halt (image_map_tool);
      break;
463

464 465
    case GIMP_TOOL_ACTION_COMMIT:
      gimp_image_map_tool_commit (image_map_tool);
466 467 468
      break;
    }

469
  GIMP_TOOL_CLASS (parent_class)->control (tool, action, display);
470 471
}

472 473 474
static gboolean
gimp_image_map_tool_key_press (GimpTool    *tool,
                               GdkEventKey *kevent,
475
                               GimpDisplay *display)
476 477 478
{
  GimpImageMapTool *image_map_tool = GIMP_IMAGE_MAP_TOOL (tool);

479
  if (image_map_tool->gui && display == tool->display)
480 481 482
    {
      switch (kevent->keyval)
        {
483 484 485
        case GDK_KEY_Return:
        case GDK_KEY_KP_Enter:
        case GDK_KEY_ISO_Enter:
486
          gimp_image_map_tool_response (image_map_tool->gui,
487 488
                                        GTK_RESPONSE_OK,
                                        image_map_tool);
489 490
          return TRUE;

491
        case GDK_KEY_BackSpace:
492
          gimp_image_map_tool_response (image_map_tool->gui,
493 494
                                        RESPONSE_RESET,
                                        image_map_tool);
495 496
          return TRUE;

497
        case GDK_KEY_Escape:
498
          gimp_image_map_tool_response (image_map_tool->gui,
499 500
                                        GTK_RESPONSE_CANCEL,
                                        image_map_tool);
501 502 503 504 505 506 507
          return TRUE;
        }
    }

  return FALSE;
}

508 509 510 511 512 513 514 515 516 517 518 519 520 521
static void
gimp_image_map_tool_options_notify (GimpTool         *tool,
                                    GimpToolOptions  *options,
                                    const GParamSpec *pspec)
{
  GimpImageMapTool *image_map_tool = GIMP_IMAGE_MAP_TOOL (tool);

  if (! strcmp (pspec->name, "preview") &&
      image_map_tool->image_map)
    {
      GimpImageMapOptions *im_options = GIMP_IMAGE_MAP_OPTIONS (options);

      if (im_options->preview)
        {
522
          gimp_tool_control_push_preserve (tool->control, TRUE);
523 524 525

          gimp_image_map_tool_map (image_map_tool);

526
          gimp_tool_control_pop_preserve (tool->control);
527 528 529
        }
      else
        {
530
          gimp_tool_control_push_preserve (tool->control, TRUE);
531

532
          gimp_image_map_abort (image_map_tool->image_map);
533

534
          gimp_tool_control_pop_preserve (tool->control);
535 536
        }
    }
537 538 539 540 541 542 543 544 545 546
  else if (! strcmp (pspec->name, "region") &&
           image_map_tool->image_map)
    {
      gimp_tool_control_push_preserve (tool->control, TRUE);

      gimp_image_map_tool_create_map (image_map_tool);
      gimp_image_map_tool_preview (image_map_tool);

      gimp_tool_control_pop_preserve (tool->control);
    }
547 548
}

549
static gboolean
550 551 552 553 554 555
gimp_image_map_tool_pick_color (GimpColorTool  *color_tool,
                                gint            x,
                                gint            y,
                                const Babl    **sample_format,
                                GimpRGB        *color,
                                gint           *color_index)
556 557
{
  GimpImageMapTool *tool = GIMP_IMAGE_MAP_TOOL (color_tool);
558 559
  gint              off_x, off_y;

560
  gimp_item_get_offset (GIMP_ITEM (tool->drawable), &off_x, &off_y);
561

562
  *sample_format = gimp_drawable_get_format (tool->drawable);
563

564
  return gimp_pickable_pick_color (GIMP_PICKABLE (tool->drawable),
565 566
                                   x - off_x,
                                   y - off_y,
567 568 569
                                   color_tool->options->sample_average,
                                   color_tool->options->average_radius,
                                   color, color_index);
570 571
}

572 573 574
static void
gimp_image_map_tool_color_picked (GimpColorTool      *color_tool,
                                  GimpColorPickState  pick_state,
575 576
                                  gdouble             x,
                                  gdouble             y,
577 578 579 580 581 582 583
                                  const Babl         *sample_format,
                                  const GimpRGB      *color,
                                  gint                color_index)
{
  GimpImageMapTool *tool = GIMP_IMAGE_MAP_TOOL (color_tool);
  gpointer          identifier;

584 585
  g_return_if_fail (GTK_IS_WIDGET (tool->active_picker));

586 587 588 589 590
  identifier = g_object_get_data (G_OBJECT (tool->active_picker),
                                  "picker-identifier");

  GIMP_IMAGE_MAP_TOOL_GET_CLASS (tool)->color_picked (tool,
                                                      identifier,
591
                                                      x, y,
592 593 594 595
                                                      sample_format,
                                                      color);
}

596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
static void
gimp_image_map_tool_real_reset (GimpImageMapTool *im_tool)
{
  if (im_tool->config)
    {
      if (im_tool->default_config)
        {
          gimp_config_copy (GIMP_CONFIG (im_tool->default_config),
                            GIMP_CONFIG (im_tool->config),
                            0);
        }
      else
        {
          gimp_config_reset (GIMP_CONFIG (im_tool->config));
        }
    }
}

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 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
static void
gimp_image_map_tool_halt (GimpImageMapTool *im_tool)
{
  GimpTool *tool = GIMP_TOOL (im_tool);

  if (im_tool->gui)
    gimp_tool_gui_hide (im_tool->gui);

  if (im_tool->image_map)
    {
      gimp_tool_control_push_preserve (tool->control, TRUE);

      gimp_image_map_abort (im_tool->image_map);
      g_object_unref (im_tool->image_map);
      im_tool->image_map = NULL;

      gimp_tool_control_pop_preserve (tool->control);
    }

  tool->drawable = NULL;
}

static void
gimp_image_map_tool_commit (GimpImageMapTool *im_tool)
{
  GimpTool *tool = GIMP_TOOL (im_tool);

  if (im_tool->gui)
    gimp_tool_gui_hide (im_tool->gui);

  if (im_tool->image_map)
    {
      GimpImageMapOptions *options = GIMP_IMAGE_MAP_TOOL_GET_OPTIONS (tool);

      gimp_tool_control_push_preserve (tool->control, TRUE);

      if (! options->preview)
        gimp_image_map_tool_map (im_tool);

      gimp_image_map_commit (im_tool->image_map,
                             GIMP_PROGRESS (tool));
      g_object_unref (im_tool->image_map);
      im_tool->image_map = NULL;

      gimp_tool_control_pop_preserve (tool->control);

      gimp_image_flush (gimp_display_get_image (tool->display));

      if (im_tool->config && im_tool->settings_box)
        {
          GimpGuiConfig *config = GIMP_GUI_CONFIG (tool->tool_info->gimp->config);

          gimp_settings_box_add_current (GIMP_SETTINGS_BOX (im_tool->settings_box),
                                         config->image_map_tool_max_recent);
        }
    }

  tool->display  = NULL;
  tool->drawable = NULL;
}

675
static void
676
gimp_image_map_tool_map (GimpImageMapTool *tool)
677
{
678 679
  if (GIMP_IMAGE_MAP_TOOL_GET_CLASS (tool)->map)
    GIMP_IMAGE_MAP_TOOL_GET_CLASS (tool)->map (tool);
680

681
  gimp_image_map_apply (tool->image_map, NULL);
682 683 684
}

static void
685
gimp_image_map_tool_dialog (GimpImageMapTool *tool)
686
{
687
  GIMP_IMAGE_MAP_TOOL_GET_CLASS (tool)->dialog (tool);
688

689
  g_signal_connect (gimp_tool_gui_get_dialog (tool->gui), "unmap",
690 691 692 693 694 695 696 697 698 699 700
                    G_CALLBACK (gimp_image_map_tool_dialog_unmap),
                    tool);
}

static void
gimp_image_map_tool_dialog_unmap (GtkWidget        *dialog,
                                  GimpImageMapTool *tool)
{
  if (tool->active_picker)
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (tool->active_picker),
                                  FALSE);
701 702 703
}

static void
704 705
gimp_image_map_tool_reset (GimpImageMapTool *tool)
{
706 707 708 709 710 711 712
  if (tool->config)
    g_object_freeze_notify (tool->config);

  GIMP_IMAGE_MAP_TOOL_GET_CLASS (tool)->reset (tool);

  if (tool->config)
    g_object_thaw_notify (tool->config);
713 714
}

715 716
static void
gimp_image_map_tool_create_map (GimpImageMapTool *tool)
717
{
718 719
  GimpImageMapOptions *options   = GIMP_IMAGE_MAP_TOOL_GET_OPTIONS (tool);
  GimpToolInfo        *tool_info = GIMP_TOOL (tool)->tool_info;
720

721 722
  if (tool->image_map)
    {
723
      gimp_image_map_abort (tool->image_map);
724 725 726
      g_object_unref (tool->image_map);
    }

727
  g_assert (tool->operation);
728

729
  tool->image_map = gimp_image_map_new (tool->drawable,
730
                                        tool->undo_desc,
731
                                        tool->operation,
732
                                        gimp_viewable_get_icon_name (GIMP_VIEWABLE (tool_info)));
733

734 735
  gimp_image_map_set_region (tool->image_map, options->region);

736 737 738 739 740
  g_signal_connect (tool->image_map, "flush",
                    G_CALLBACK (gimp_image_map_tool_flush),
                    tool);
}

741 742 743 744
static void
gimp_image_map_tool_flush (GimpImageMap     *image_map,
                           GimpImageMapTool *image_map_tool)
{
745 746
  GimpTool  *tool  = GIMP_TOOL (image_map_tool);
  GimpImage *image = gimp_display_get_image (tool->display);
747

748
  gimp_projection_flush (gimp_image_get_projection (image));
749 750
}

751 752 753 754 755
static void
gimp_image_map_tool_config_notify (GObject          *object,
                                   const GParamSpec *pspec,
                                   GimpImageMapTool *image_map_tool)
{
756
  gimp_image_map_tool_preview (image_map_tool);
757 758
}

759
static void
760
gimp_image_map_tool_response (GimpToolGui      *gui,
761 762
                              gint              response_id,
                              GimpImageMapTool *image_map_tool)
763
{
764
  GimpTool *tool = GIMP_TOOL (image_map_tool);
765

766
  switch (response_id)
767
    {
768 769 770 771
    case RESPONSE_RESET:
      gimp_image_map_tool_reset (image_map_tool);
      gimp_image_map_tool_preview (image_map_tool);
      break;
772

773
    case GTK_RESPONSE_OK:
774
      gimp_tool_control (tool, GIMP_TOOL_ACTION_COMMIT, tool->display);
775
      break;
776

777
    default:
778
      gimp_tool_control (tool, GIMP_TOOL_ACTION_HALT, tool->display);
779 780
      break;
    }
781 782
}

783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
void
gimp_image_map_tool_get_operation (GimpImageMapTool *image_map_tool)
{
  GimpImageMapToolClass *klass;

  g_return_if_fail (GIMP_IS_IMAGE_MAP_TOOL (image_map_tool));

  klass = GIMP_IMAGE_MAP_TOOL_GET_CLASS (image_map_tool);

  if (image_map_tool->image_map)
    {
      gimp_image_map_abort (image_map_tool->image_map);
      g_object_unref (image_map_tool->image_map);
      image_map_tool->image_map = NULL;
    }

  if (image_map_tool->operation)
    {
      g_object_unref (image_map_tool->operation);
      image_map_tool->operation = NULL;
    }

  if (image_map_tool->config)
    {
      g_signal_handlers_disconnect_by_func (image_map_tool->config,
                                            gimp_image_map_tool_config_notify,
                                            image_map_tool);

      g_object_unref (image_map_tool->config);
      image_map_tool->config = NULL;
    }

  if (image_map_tool->undo_desc)
    {
      g_free (image_map_tool->undo_desc);
      image_map_tool->undo_desc = NULL;
    }

  image_map_tool->operation = klass->get_operation (image_map_tool,
                                                    &image_map_tool->config,
                                                    &image_map_tool->undo_desc);

  if (! image_map_tool->undo_desc)
    image_map_tool->undo_desc =
      g_strdup (GIMP_TOOL (image_map_tool)->tool_info->blurb);

  if (image_map_tool->config)
    g_signal_connect_object (image_map_tool->config, "notify",
                             G_CALLBACK (gimp_image_map_tool_config_notify),
                             G_OBJECT (image_map_tool), 0);

  if (GIMP_TOOL (image_map_tool)->drawable)
    gimp_image_map_tool_create_map (image_map_tool);
}

838 839 840 841 842 843 844 845
void
gimp_image_map_tool_preview (GimpImageMapTool *image_map_tool)
{
  GimpTool            *tool;
  GimpImageMapOptions *options;

  g_return_if_fail (GIMP_IS_IMAGE_MAP_TOOL (image_map_tool));

846 847
  tool    = GIMP_TOOL (image_map_tool);
  options = GIMP_IMAGE_MAP_TOOL_GET_OPTIONS (tool);
848

849
  if (image_map_tool->image_map && options->preview)
850
    {
851
      gimp_tool_control_push_preserve (tool->control, TRUE);
852 853 854

      gimp_image_map_tool_map (image_map_tool);

855
      gimp_tool_control_pop_preserve (tool->control);
856 857 858
    }
}

859 860 861 862 863 864 865 866 867 868 869
void
gimp_image_map_tool_edit_as (GimpImageMapTool *im_tool,
                             const gchar      *new_tool_id,
                             GimpConfig       *config)
{
  GimpDisplay  *display;
  GimpContext  *user_context;
  GimpToolInfo *tool_info;
  GimpTool     *new_tool;

  g_return_if_fail (GIMP_IS_IMAGE_MAP_TOOL (im_tool));
870
  g_return_if_fail (new_tool_id != NULL);
871 872 873 874 875 876 877 878 879 880
  g_return_if_fail (GIMP_IS_CONFIG (config));

  display = GIMP_TOOL (im_tool)->display;

  user_context = gimp_get_user_context (display->gimp);

  tool_info = (GimpToolInfo *)
    gimp_container_get_child_by_name (display->gimp->tool_info_list,
                                      new_tool_id);

881
  gimp_tool_control (GIMP_TOOL (im_tool), GIMP_TOOL_ACTION_HALT, display);
882 883 884 885 886
  gimp_context_set_tool (user_context, tool_info);
  tool_manager_initialize_active (display->gimp, display);

  new_tool = tool_manager_get_active (display->gimp);

887 888 889
  GIMP_IMAGE_MAP_TOOL (new_tool)->default_config = g_object_ref (config);

  gimp_image_map_tool_reset (GIMP_IMAGE_MAP_TOOL (new_tool));
890
}
891 892 893 894 895 896

GtkWidget *
gimp_image_map_tool_dialog_get_vbox (GimpImageMapTool *tool)
{
  g_return_val_if_fail (GIMP_IS_IMAGE_MAP_TOOL (tool), NULL);

897
  return gimp_tool_gui_get_vbox (tool->gui);
898 899 900 901 902 903 904
}

GtkSizeGroup *
gimp_image_map_tool_dialog_get_label_group (GimpImageMapTool *tool)
{
  g_return_val_if_fail (GIMP_IS_IMAGE_MAP_TOOL (tool), NULL);

905 906 907
  if (! tool->label_group)
    tool->label_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);

908 909
  return tool->label_group;
}
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938

static void
gimp_image_map_tool_color_picker_toggled (GtkWidget        *widget,
                                          GimpImageMapTool *tool)
{
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
    {
      if (tool->active_picker == widget)
        return;

      if (tool->active_picker)
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (tool->active_picker),
                                      FALSE);

      tool->active_picker = widget;

      gimp_color_tool_enable (GIMP_COLOR_TOOL (tool),
                              GIMP_COLOR_TOOL_GET_OPTIONS (tool));
    }
  else if (tool->active_picker == widget)
    {
      tool->active_picker = NULL;
      gimp_color_tool_disable (GIMP_COLOR_TOOL (tool));
    }
}

GtkWidget *
gimp_image_map_tool_add_color_picker (GimpImageMapTool *tool,
                                      gpointer          identifier,
939
                                      const gchar      *icon_name,
940
                                      const gchar      *tooltip)
941 942 943 944 945
{
  GtkWidget *button;
  GtkWidget *image;

  g_return_val_if_fail (GIMP_IS_IMAGE_MAP_TOOL (tool), NULL);
946
  g_return_val_if_fail (icon_name != NULL, NULL);
947 948 949 950 951

  button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
                         "draw-indicator", FALSE,
                         NULL);

952
  image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
953 954 955 956
  gtk_misc_set_padding (GTK_MISC (image), 2, 2);
  gtk_container_add (GTK_CONTAINER (button), image);
  gtk_widget_show (image);

957 958
  if (tooltip)
    gimp_help_set_help_data (button, tooltip, NULL);
959 960 961 962 963 964 965 966 967

  g_object_set_data (G_OBJECT (button), "picker-identifier", identifier);

  g_signal_connect (button, "toggled",
                    G_CALLBACK (gimp_image_map_tool_color_picker_toggled),
                    tool);

  return button;
}