tool_manager.c 18.7 KB
Newer Older
Nate Summers's avatar
doh!    
Nate Summers committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <gtk/gtk.h>

23
24
#include "libgimpwidgets/gimpwidgets.h"

Michael Natterer's avatar
Michael Natterer committed
25
#include "tools-types.h"
Nate Summers's avatar
doh!    
Nate Summers committed
26

27
#include "core/gimp.h"
28
29
30
31
#include "core/gimpcontainer.h"
#include "core/gimpcontext.h"
#include "core/gimpimage.h"
#include "core/gimplist.h"
Michael Natterer's avatar
Michael Natterer committed
32
#include "core/gimptoolinfo.h"
33

34
35
#include "libgimptool/gimptoolcontrol.h"

36
37
#include "display/gimpdisplay.h"

38
#include "gimpairbrushtool.h"
39
40
#include "gimpclonetool.h"
#include "gimpconvolvetool.h"
41
42
#include "gimpdodgeburntool.h"
#include "gimperasertool.h"
Michael Natterer's avatar
Michael Natterer committed
43
#include "gimppaintbrushtool.h"
44
#include "gimppenciltool.h"
Michael Natterer's avatar
Michael Natterer committed
45
#include "gimprectselecttool.h"
46
#include "gimpsmudgetool.h"
Michael Natterer's avatar
Michael Natterer committed
47
48
49
50
#include "gimptoolcontrol.h"
#include "tool_manager.h"
#include "tool_options.h"
#include "tools.h"
51

52
#include "app_procs.h"
53

Nate Summers's avatar
doh!    
Nate Summers committed
54
55
56
#include "libgimp/gimpintl.h"


57
58
59
60
#define PAINT_OPTIONS_MASK GIMP_CONTEXT_OPACITY_MASK    | \
                           GIMP_CONTEXT_PAINT_MODE_MASK | \
                           GIMP_CONTEXT_BRUSH_MASK      | \
                           GIMP_CONTEXT_GRADIENT_MASK
Michael Natterer's avatar
Michael Natterer committed
61
62


63
typedef struct _GimpToolManager GimpToolManager;
Nate Summers's avatar
doh!    
Nate Summers committed
64

65
66
67
68
struct _GimpToolManager
{
  GimpTool    *active_tool;
  GSList      *tool_stack;
Nate Summers's avatar
doh!    
Nate Summers committed
69

70
  GimpContext *global_tool_context;
Michael Natterer's avatar
Michael Natterer committed
71
72

  GQuark       image_dirty_handler_id;
73
};
74
75


Michael Natterer's avatar
Michael Natterer committed
76
/*  local function prototypes  */
Nate Summers's avatar
doh!    
Nate Summers committed
77

Michael Natterer's avatar
Michael Natterer committed
78
79
80
81
82
83
84
85
static GimpToolManager * tool_manager_get (Gimp            *gimp);
static void              tool_manager_set (Gimp            *gimp,
					   GimpToolManager *tool_manager);
static void   tool_manager_tool_changed   (GimpContext     *user_context,
					   GimpToolInfo    *tool_info,
					   gpointer         data);
static void   tool_manager_image_dirty    (GimpImage       *gimage,
					   gpointer         data);
Michael Natterer's avatar
Michael Natterer committed
86
87
88
89
90
91


/*  public functions  */

void
tool_manager_init (Gimp *gimp)
Nate Summers's avatar
doh!    
Nate Summers committed
92
{
93
94
95
96
  GimpToolManager *tool_manager;
  GimpContext     *user_context;
  GimpContext     *tool_context;

97
98
  g_return_if_fail (GIMP_IS_GIMP (gimp));

99
100
  tool_manager = g_new0 (GimpToolManager, 1);

Michael Natterer's avatar
Michael Natterer committed
101
102
103
104
  tool_manager->active_tool            = NULL;
  tool_manager->tool_stack             = NULL;
  tool_manager->global_tool_context    = NULL;
  tool_manager->image_dirty_handler_id = 0;
105
106

  tool_manager_set (gimp, tool_manager);
Michael Natterer's avatar
Michael Natterer committed
107

Michael Natterer's avatar
Michael Natterer committed
108
109
  tool_manager->image_dirty_handler_id =
    gimp_container_add_handler (gimp->images, "dirty",
110
				G_CALLBACK (tool_manager_image_dirty),
Michael Natterer's avatar
Michael Natterer committed
111
112
				tool_manager);

Michael Natterer's avatar
Michael Natterer committed
113
114
  user_context = gimp_get_user_context (gimp);

115
116
117
  g_signal_connect (G_OBJECT (user_context), "tool_changed",
		    G_CALLBACK (tool_manager_tool_changed),
		    tool_manager);
Michael Natterer's avatar
Michael Natterer committed
118
119
120
121

  /*  Create a context to store the paint options of the
   *  global paint options mode
   */
122
123
124
  tool_manager->global_tool_context = gimp_context_new (gimp,
                                                        "Global Tool Context",
                                                        user_context);
Michael Natterer's avatar
Michael Natterer committed
125
126

  /*  TODO: add foreground, background, brush, pattern, gradient  */
127
128
  gimp_context_define_properties (tool_manager->global_tool_context,
				  PAINT_OPTIONS_MASK, FALSE);
Michael Natterer's avatar
Michael Natterer committed
129
130
131
132

  /* register internal tools */
  tools_init (gimp);

133
134
  gimp_tool_info_set_standard (gimp, tool_manager_get_info_by_type (gimp, GIMP_TYPE_RECT_SELECT_TOOL));

135
  if (tool_manager->active_tool)
Michael Natterer's avatar
Michael Natterer committed
136
    {
Michael Natterer's avatar
Michael Natterer committed
137
      tool_context = tool_manager->active_tool->tool_info->context;
138
139
140
141
142

      if (tool_context)
	{
	  gimp_context_set_parent (tool_context, user_context);
	}
Michael Natterer's avatar
Michael Natterer committed
143
144
145
146
147
    }

  gimp_container_thaw (gimp->tool_info_list);
}

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
void
tool_manager_restore (Gimp *gimp)
{
  GimpToolManager *tool_manager;
  GimpToolInfo    *tool_info;
  GList           *list;

  g_return_if_fail (GIMP_IS_GIMP (gimp));

  tool_manager = tool_manager_get (gimp);

  for (list = GIMP_LIST (gimp->tool_info_list)->list;
       list;
       list = g_list_next (list))
    {
      tool_info = GIMP_TOOL_INFO (list->data);

      if (tool_info->options_new_func)
        {
          tool_info->tool_options = tool_info->options_new_func (tool_info);
        }
      else
        {
          tool_info->tool_options = tool_options_new (tool_info);
        }
    }
}

Michael Natterer's avatar
Michael Natterer committed
176
177
178
void
tool_manager_exit (Gimp *gimp)
{
179
180
  GimpToolManager *tool_manager;

181
182
  g_return_if_fail (GIMP_IS_GIMP (gimp));

183
  tool_manager = tool_manager_get (gimp);
184
  tool_manager_set (gimp, NULL);
185

186
  g_object_unref (G_OBJECT (tool_manager->global_tool_context));
187

Michael Natterer's avatar
Michael Natterer committed
188
189
190
  gimp_container_remove_handler (gimp->images,
				 tool_manager->image_dirty_handler_id);

191
192
  if (tool_manager->active_tool)
    g_object_unref (G_OBJECT (tool_manager->active_tool));
193

194
  g_free (tool_manager);
Michael Natterer's avatar
Michael Natterer committed
195
196
}

197
198
199
200
201
GimpTool *
tool_manager_get_active (Gimp *gimp)
{
  GimpToolManager *tool_manager;

202
203
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);

204
205
206
207
208
  tool_manager = tool_manager_get (gimp);

  return tool_manager->active_tool;
}

Nate Summers's avatar
doh!    
Nate Summers committed
209
void
210
211
tool_manager_select_tool (Gimp     *gimp,
			  GimpTool *tool)
Nate Summers's avatar
doh!    
Nate Summers committed
212
{
213
214
  GimpToolManager *tool_manager;

Michael Natterer's avatar
Michael Natterer committed
215
  g_return_if_fail (GIMP_IS_GIMP (gimp));
216
217
  g_return_if_fail (GIMP_IS_TOOL (tool));

218
219
220
  tool_manager = tool_manager_get (gimp);

  if (tool_manager->active_tool)
221
    {
222
223
224
225
226
227
      if (tool_manager->active_tool->gdisp)
        {
          tool_manager_control_active (gimp,
                                       HALT,
                                       tool_manager->active_tool->gdisp);
        }
228
229
230

      g_object_unref (G_OBJECT (tool_manager->active_tool));
    }
Nate Summers's avatar
doh!    
Nate Summers committed
231

232
  tool_manager->active_tool = tool;
233
234

  g_object_ref (G_OBJECT (tool_manager->active_tool));
Nate Summers's avatar
doh!    
Nate Summers committed
235
236
}

237
void
238
239
tool_manager_push_tool (Gimp     *gimp,
			GimpTool *tool)
240
{
241
242
  GimpToolManager *tool_manager;

Michael Natterer's avatar
Michael Natterer committed
243
  g_return_if_fail (GIMP_IS_GIMP (gimp));
244
245
  g_return_if_fail (GIMP_IS_TOOL (tool));

246
247
248
  tool_manager = tool_manager_get (gimp);

  if (tool_manager->active_tool)
249
    {
250
251
      tool_manager->tool_stack = g_slist_prepend (tool_manager->tool_stack,
						  tool_manager->active_tool);
252
253

      g_object_ref (G_OBJECT (tool_manager->tool_stack->data));
254
255
    }

256
  tool_manager_select_tool (gimp, tool);
257
258
259
}

void
260
tool_manager_pop_tool (Gimp *gimp)
261
{
262
263
  GimpToolManager *tool_manager;

264
265
  g_return_if_fail (GIMP_IS_GIMP (gimp));

266
267
268
  tool_manager = tool_manager_get (gimp);

  if (tool_manager->tool_stack)
269
    {
270
271
      tool_manager_select_tool (gimp,
				GIMP_TOOL (tool_manager->tool_stack->data));
272

273
274
      g_object_unref (G_OBJECT (tool_manager->tool_stack->data));

275
276
      tool_manager->tool_stack = g_slist_remove (tool_manager->tool_stack,
						 tool_manager->active_tool);
277
278
279
    }
}

280
void
281
282
tool_manager_initialize_active (Gimp        *gimp,
                                GimpDisplay *gdisp)
283
{
284
  GimpToolManager *tool_manager;
285
  GimpTool        *tool;
286

287
  g_return_if_fail (GIMP_IS_GIMP (gimp));
288
  g_return_if_fail (GIMP_IS_DISPLAY (gdisp));
289

290
  tool_manager = tool_manager_get (gimp);
Michael Natterer's avatar
Michael Natterer committed
291

292
  if (tool_manager->active_tool)
Michael Natterer's avatar
Michael Natterer committed
293
    {
294
      tool = tool_manager->active_tool;
295

296
      gimp_tool_initialize (tool, gdisp);
297

298
      tool->drawable = gimp_image_active_drawable (gdisp->gimage);
299
300
301
    }
}

Nate Summers's avatar
doh!    
Nate Summers committed
302
void
303
304
305
tool_manager_control_active (Gimp           *gimp,
			     GimpToolAction  action,
			     GimpDisplay    *gdisp)
Nate Summers's avatar
doh!    
Nate Summers committed
306
{
307
308
  GimpToolManager *tool_manager;

309
310
  g_return_if_fail (GIMP_IS_GIMP (gimp));

311
312
  tool_manager = tool_manager_get (gimp);

313
314
315
  if (! tool_manager->active_tool)
    return;

316
  if (gdisp && tool_manager->active_tool->gdisp == gdisp)
317
    {
318
319
320
      gimp_tool_control (tool_manager->active_tool,
                         action,
                         gdisp);
321
322
323
    }
  else if (action == HALT)
    {
324
325
      /* sets paused_count to 0 -- is this ok? */
      gimp_tool_control_halt (tool_manager->active_tool->control);
326
    }
Nate Summers's avatar
doh!    
Nate Summers committed
327
328
}

329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
void
tool_manager_button_press_active (Gimp            *gimp,
                                  GimpCoords      *coords,
                                  guint32          time,
                                  GdkModifierType  state,
                                  GimpDisplay     *gdisp)
{
  GimpToolManager *tool_manager;

  g_return_if_fail (GIMP_IS_GIMP (gimp));

  tool_manager = tool_manager_get (gimp);

  if (tool_manager->active_tool)
    {
      gimp_tool_button_press (tool_manager->active_tool,
                              coords, time, state,
                              gdisp);
    }
}

void
tool_manager_button_release_active (Gimp            *gimp,
                                    GimpCoords      *coords,
                                    guint32          time,
                                    GdkModifierType  state,
                                    GimpDisplay     *gdisp)
{
  GimpToolManager *tool_manager;

  g_return_if_fail (GIMP_IS_GIMP (gimp));

  tool_manager = tool_manager_get (gimp);

  if (tool_manager->active_tool)
    {
      gimp_tool_button_release (tool_manager->active_tool,
                                coords, time, state,
                                gdisp);
    }
}

void
tool_manager_motion_active (Gimp            *gimp,
                            GimpCoords      *coords,
                            guint32          time,
                            GdkModifierType  state,
                            GimpDisplay     *gdisp)
{
  GimpToolManager *tool_manager;

  g_return_if_fail (GIMP_IS_GIMP (gimp));

  tool_manager = tool_manager_get (gimp);

  if (tool_manager->active_tool)
    {
      gimp_tool_motion (tool_manager->active_tool,
                        coords, time, state,
                        gdisp);
    }
}

void
tool_manager_arrow_key_active (Gimp        *gimp,
                               GdkEventKey *kevent,
                               GimpDisplay *gdisp)
{
  GimpToolManager *tool_manager;

  g_return_if_fail (GIMP_IS_GIMP (gimp));

  tool_manager = tool_manager_get (gimp);

  if (tool_manager->active_tool)
    {
      gimp_tool_arrow_key (tool_manager->active_tool,
                           kevent,
                           gdisp);
    }
}

void
tool_manager_modifier_key_active (Gimp            *gimp,
                                  GdkModifierType  key,
                                  gboolean         press,
                                  GdkModifierType  state,
                                  GimpDisplay     *gdisp)
{
  GimpToolManager *tool_manager;

  g_return_if_fail (GIMP_IS_GIMP (gimp));

  tool_manager = tool_manager_get (gimp);

  if (tool_manager->active_tool)
    {
      gimp_tool_modifier_key (tool_manager->active_tool,
                              key, press, state,
                              gdisp);
    }
}

void
tool_manager_oper_update_active (Gimp            *gimp,
                                 GimpCoords      *coords,
                                 GdkModifierType  state,
                                 GimpDisplay     *gdisp)
{
  GimpToolManager *tool_manager;

  g_return_if_fail (GIMP_IS_GIMP (gimp));

  tool_manager = tool_manager_get (gimp);

  if (tool_manager->active_tool)
    {
      gimp_tool_oper_update (tool_manager->active_tool,
                             coords, state,
                             gdisp);
    }
}

void
tool_manager_cursor_update_active (Gimp            *gimp,
                                   GimpCoords      *coords,
                                   GdkModifierType  state,
                                   GimpDisplay     *gdisp)
{
  GimpToolManager *tool_manager;

  g_return_if_fail (GIMP_IS_GIMP (gimp));

  tool_manager = tool_manager_get (gimp);

  if (tool_manager->active_tool)
    {
      gimp_tool_cursor_update (tool_manager->active_tool,
                               coords, state,
                               gdisp);
    }
}

472
void
Nate Summers's avatar
Nate Summers committed
473
tool_manager_register_tool (GType                   tool_type,
474
475
476
477
478
479
480
481
482
                            GimpToolOptionsNewFunc  options_new_func,
			    gboolean                tool_context,
			    const gchar            *identifier,
			    const gchar            *blurb,
			    const gchar            *help,
			    const gchar            *menu_path,
			    const gchar            *menu_accel,
			    const gchar            *help_domain,
			    const gchar            *help_data,
Nate Summers's avatar
Nate Summers committed
483
			    const gchar            *stock_id,
484
			    gpointer                data)
485
{
486
  Gimp            *gimp = (Gimp *) data;
487
488
  GimpToolManager *tool_manager;
  GimpToolInfo    *tool_info;
489
  const gchar     *paint_core_name;
Michael Natterer's avatar
Michael Natterer committed
490
491
492
  GtkIconSet      *icon_set;
  GtkStyle        *style;
  GdkPixbuf       *pixbuf;
493

494
495
496
  g_return_if_fail (GIMP_IS_GIMP (gimp));
  g_return_if_fail (g_type_is_a (tool_type, GIMP_TYPE_TOOL));

497
498
  if (tool_type == GIMP_TYPE_PENCIL_TOOL)
    {
499
      paint_core_name = "GimpPencil";
500
501
502
    }
  else if (tool_type == GIMP_TYPE_PAINTBRUSH_TOOL)
    {
503
      paint_core_name = "GimpPaintbrush";
504
505
506
    }
  else if (tool_type == GIMP_TYPE_ERASER_TOOL)
    {
507
      paint_core_name = "GimpEraser";
508
509
510
    }
  else if (tool_type == GIMP_TYPE_AIRBRUSH_TOOL)
    {
511
      paint_core_name = "GimpAirbrush";
512
513
514
    }
  else if (tool_type == GIMP_TYPE_CLONE_TOOL)
    {
515
      paint_core_name = "GimpClone";
516
517
518
    }
  else if (tool_type == GIMP_TYPE_CONVOLVE_TOOL)
    {
519
      paint_core_name = "GimpConvolve";
520
521
522
    }
  else if (tool_type == GIMP_TYPE_SMUDGE_TOOL)
    {
523
      paint_core_name = "GimpSmudge";
524
525
526
    }
  else if (tool_type == GIMP_TYPE_DODGEBURN_TOOL)
    {
527
528
529
530
531
      paint_core_name = "GimpDodgeBurn";
    }
  else
    {
      paint_core_name = "GimpPaintbrush";
532
    }
533

Michael Natterer's avatar
Michael Natterer committed
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
  icon_set = gtk_icon_factory_lookup_default (stock_id);

#ifdef __GNUC__
#warning FIXME: remove gtk_widget_get_default_style()
#endif
  style = gtk_widget_get_default_style ();

  pixbuf = gtk_icon_set_render_icon (icon_set,
				     style,
				     GTK_TEXT_DIR_LTR,
				     GTK_STATE_NORMAL,
				     GTK_ICON_SIZE_BUTTON,
				     NULL,
				     NULL);

549
  tool_manager = tool_manager_get (gimp);
550

Michael Natterer's avatar
Michael Natterer committed
551
552
  tool_info = gimp_tool_info_new (gimp,
                                  tool_manager->global_tool_context,
Michael Natterer's avatar
Michael Natterer committed
553
				  tool_type,
554
				  tool_context,
555
556
557
				  identifier,
				  blurb,
				  help,
558
559
560
561
				  menu_path,
				  menu_accel,
				  help_domain,
				  help_data,
562
                                  paint_core_name,
Michael Natterer's avatar
Michael Natterer committed
563
564
565
566
				  stock_id,
				  pixbuf);

  g_object_unref (G_OBJECT (pixbuf));
567

568
  tool_info->options_new_func = options_new_func;
569

570
  gimp_container_add (gimp->tool_info_list, GIMP_OBJECT (tool_info));
571
  g_object_unref (G_OBJECT (tool_info));
572
573
}

574
GimpToolInfo *
575
576
tool_manager_get_info_by_type (Gimp  *gimp,
			       GType  tool_type)
577
578
579
580
{
  GimpToolInfo *tool_info;
  GList        *list;

581
582
583
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (g_type_is_a (tool_type, GIMP_TYPE_TOOL), NULL);

Michael Natterer's avatar
Michael Natterer committed
584
  for (list = GIMP_LIST (gimp->tool_info_list)->list;
585
586
587
588
589
590
591
592
593
594
595
       list;
       list = g_list_next (list))
    {
      tool_info = (GimpToolInfo *) list->data;

      if (tool_info->tool_type == tool_type)
	return tool_info;
    }

  return NULL;
}
596
597

const gchar *
598
tool_manager_active_get_help_data (Gimp *gimp)
599
{
600
601
  GimpToolManager *tool_manager;

602
603
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);

604
  tool_manager = tool_manager_get (gimp);
605

606
607
608
609
  if (tool_manager->active_tool)
    {
      return tool_manager->active_tool->tool_info->help_data;
    }
610

611
  return NULL;
612
613
614
615
616
}

void
tool_manager_help_func (const gchar *help_data)
{
617
  gimp_standard_help_func (tool_manager_active_get_help_data (the_gimp));
618
}
Michael Natterer's avatar
Michael Natterer committed
619
620
621
622


/*  private functions  */

623
#define TOOL_MANAGER_DATA_KEY "gimp-tool-manager"
Michael Natterer's avatar
Michael Natterer committed
624

625
626
627
static GimpToolManager *
tool_manager_get (Gimp *gimp)
{
628
  return g_object_get_data (G_OBJECT (gimp), TOOL_MANAGER_DATA_KEY);
629
}
Michael Natterer's avatar
Michael Natterer committed
630

631
632
633
634
static void
tool_manager_set (Gimp            *gimp,
		  GimpToolManager *tool_manager)
{
635
  g_object_set_data (G_OBJECT (gimp), TOOL_MANAGER_DATA_KEY,
636
                     tool_manager);
Michael Natterer's avatar
Michael Natterer committed
637
638
639
640
641
642
643
}

static void
tool_manager_tool_changed (GimpContext  *user_context,
			   GimpToolInfo *tool_info,
			   gpointer      data)
{
644
645
646
  GimpToolManager *tool_manager;
  GimpTool        *new_tool     = NULL;
  GimpContext     *tool_context = NULL;
Michael Natterer's avatar
Michael Natterer committed
647
648
649
650

  if (! tool_info)
    return;

651
652
  tool_manager = (GimpToolManager *) data;

Michael Natterer's avatar
Michael Natterer committed
653
  /* FIXME: gimp_busy HACK */
654
  if (user_context->gimp->busy)
Michael Natterer's avatar
Michael Natterer committed
655
656
657
658
    {
      /*  there may be contexts waiting for the user_context's "tool_changed"
       *  signal, so stop emitting it.
       */
659
      g_signal_stop_emission_by_name (G_OBJECT (user_context), "tool_changed");
Michael Natterer's avatar
Michael Natterer committed
660

661
      if (G_TYPE_FROM_INSTANCE (tool_manager->active_tool) !=
662
	  tool_info->tool_type)
Michael Natterer's avatar
Michael Natterer committed
663
	{
664
665
666
	  g_signal_handlers_block_by_func (G_OBJECT (user_context),
					   tool_manager_tool_changed,
					   data);
Michael Natterer's avatar
Michael Natterer committed
667
668
669

	  /*  explicitly set the current tool  */
	  gimp_context_set_tool (user_context,
Michael Natterer's avatar
Michael Natterer committed
670
                                 tool_manager->active_tool->tool_info);
Michael Natterer's avatar
Michael Natterer committed
671

672
673
674
	  g_signal_handlers_unblock_by_func (G_OBJECT (user_context),
					     tool_manager_tool_changed,
					     data);
Michael Natterer's avatar
Michael Natterer committed
675
676
677
678
679
	}

      return;
    }

Michael Natterer's avatar
Michael Natterer committed
680
  if (g_type_is_a (tool_info->tool_type, GIMP_TYPE_TOOL))
Michael Natterer's avatar
Michael Natterer committed
681
    {
Michael Natterer's avatar
Michael Natterer committed
682
      new_tool = g_object_new (tool_info->tool_type, NULL);
Michael Natterer's avatar
Michael Natterer committed
683
684

      new_tool->tool_info = tool_info;
Michael Natterer's avatar
Michael Natterer committed
685
686
687
    }
  else
    {
Michael Natterer's avatar
Michael Natterer committed
688
      g_warning ("%s(): tool_info->tool_type is no GimpTool subclass",
Michael Natterer's avatar
Michael Natterer committed
689
690
691
692
		 G_GNUC_FUNCTION);
      return;
    }

693
694
  if (tool_manager->active_tool &&
      (tool_context = tool_manager->active_tool->tool_info->context))
Michael Natterer's avatar
Michael Natterer committed
695
    {
696
697
      gimp_context_unset_parent (tool_context);
    }
Michael Natterer's avatar
Michael Natterer committed
698

699
700
701
702
703
  if ((tool_context = tool_info->context))
    {
      gimp_context_copy_properties (tool_context, user_context,
                                    PAINT_OPTIONS_MASK);
      gimp_context_set_parent (tool_context, user_context);
Michael Natterer's avatar
Michael Natterer committed
704
705
    }

706
  tool_manager_select_tool (user_context->gimp, new_tool);
707
708

  g_object_unref (G_OBJECT (new_tool));
Michael Natterer's avatar
Michael Natterer committed
709
}
Michael Natterer's avatar
Michael Natterer committed
710
711
712
713
714
715
716
717
718

static void
tool_manager_image_dirty (GimpImage *gimage,
			  gpointer   data)
{
  GimpToolManager *tool_manager;

  tool_manager = (GimpToolManager *) data;

719
720
  if (tool_manager->active_tool &&
      ! gimp_tool_control_preserve (tool_manager->active_tool->control))
Michael Natterer's avatar
Michael Natterer committed
721
    {
722
723
724
      GimpDisplay *gdisp;

      gdisp = tool_manager->active_tool->gdisp;
Michael Natterer's avatar
Michael Natterer committed
725
726
727

      if (gdisp)
	{
728
729
730
731
          /*  create a new one, deleting the current
           */
          gimp_context_tool_changed (gimp_get_user_context (gimage->gimp));

Michael Natterer's avatar
Michael Natterer committed
732
	  if (gdisp->gimage == gimage)
733
            {
734
              GimpTool *tool;
735

736
737
738
739
740
741
742
743
744
              tool = tool_manager->active_tool;

              if (gimp_image_active_drawable (gdisp->gimage) ||
                  gimp_tool_control_handles_empty_image (tool->control))
                {
                  tool_manager_initialize_active (gimage->gimp, gdisp);

                  tool->gdisp = gdisp;
                }
745
            }
Michael Natterer's avatar
Michael Natterer committed
746
747
748
	}
    }
}