1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995-2002 Spencer Kimball, Peter Mattis and others
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include <gegl.h>
21 #include <gtk/gtk.h>
22 
23 #include "libgimpmath/gimpmath.h"
24 
25 #include "tools-types.h"
26 
27 #include "core/gimp.h"
28 #include "core/gimpcontainer.h"
29 #include "core/gimpimage.h"
30 #include "core/gimpprogress.h"
31 #include "core/gimptoolinfo.h"
32 
33 #include "display/gimpdisplay.h"
34 #include "display/gimpdisplayshell.h"
35 #include "display/gimpdisplayshell-cursor.h"
36 #include "display/gimpstatusbar.h"
37 
38 #include "gimptool.h"
39 #include "gimptool-progress.h"
40 #include "gimptoolcontrol.h"
41 
42 #include "gimp-log.h"
43 #include "gimp-intl.h"
44 
45 
46 /* #define DEBUG_ACTIVE_STATE 1 */
47 
48 
49 enum
50 {
51   PROP_0,
52   PROP_TOOL_INFO
53 };
54 
55 
56 static void            gimp_tool_constructed         (GObject          *object);
57 static void            gimp_tool_dispose             (GObject          *object);
58 static void            gimp_tool_finalize            (GObject          *object);
59 static void            gimp_tool_set_property        (GObject          *object,
60                                                       guint             property_id,
61                                                       const GValue     *value,
62                                                       GParamSpec       *pspec);
63 static void            gimp_tool_get_property        (GObject          *object,
64                                                       guint             property_id,
65                                                       GValue           *value,
66                                                       GParamSpec       *pspec);
67 
68 static gboolean        gimp_tool_real_has_display    (GimpTool         *tool,
69                                                       GimpDisplay      *display);
70 static GimpDisplay   * gimp_tool_real_has_image      (GimpTool         *tool,
71                                                       GimpImage        *image);
72 static gboolean        gimp_tool_real_initialize     (GimpTool         *tool,
73                                                       GimpDisplay      *display,
74                                                       GError          **error);
75 static void            gimp_tool_real_control        (GimpTool         *tool,
76                                                       GimpToolAction    action,
77                                                       GimpDisplay      *display);
78 static void            gimp_tool_real_button_press   (GimpTool         *tool,
79                                                       const GimpCoords *coords,
80                                                       guint32           time,
81                                                       GdkModifierType   state,
82                                                       GimpButtonPressType press_type,
83                                                       GimpDisplay      *display);
84 static void            gimp_tool_real_button_release (GimpTool         *tool,
85                                                       const GimpCoords *coords,
86                                                       guint32           time,
87                                                       GdkModifierType   state,
88                                                       GimpButtonReleaseType release_type,
89                                                       GimpDisplay      *display);
90 static void            gimp_tool_real_motion         (GimpTool         *tool,
91                                                       const GimpCoords *coords,
92                                                       guint32           time,
93                                                       GdkModifierType   state,
94                                                       GimpDisplay      *display);
95 static gboolean        gimp_tool_real_key_press      (GimpTool         *tool,
96                                                       GdkEventKey      *kevent,
97                                                       GimpDisplay      *display);
98 static gboolean        gimp_tool_real_key_release    (GimpTool         *tool,
99                                                       GdkEventKey      *kevent,
100                                                       GimpDisplay      *display);
101 static void            gimp_tool_real_modifier_key   (GimpTool         *tool,
102                                                       GdkModifierType   key,
103                                                       gboolean          press,
104                                                       GdkModifierType   state,
105                                                       GimpDisplay      *display);
106 static void       gimp_tool_real_active_modifier_key (GimpTool         *tool,
107                                                       GdkModifierType   key,
108                                                       gboolean          press,
109                                                       GdkModifierType   state,
110                                                       GimpDisplay      *display);
111 static void            gimp_tool_real_oper_update    (GimpTool         *tool,
112                                                       const GimpCoords *coords,
113                                                       GdkModifierType   state,
114                                                       gboolean          proximity,
115                                                       GimpDisplay      *display);
116 static void            gimp_tool_real_cursor_update  (GimpTool         *tool,
117                                                       const GimpCoords *coords,
118                                                       GdkModifierType   state,
119                                                       GimpDisplay      *display);
120 static const gchar   * gimp_tool_real_can_undo       (GimpTool         *tool,
121                                                       GimpDisplay      *display);
122 static const gchar   * gimp_tool_real_can_redo       (GimpTool         *tool,
123                                                       GimpDisplay      *display);
124 static gboolean        gimp_tool_real_undo           (GimpTool         *tool,
125                                                       GimpDisplay      *display);
126 static gboolean        gimp_tool_real_redo           (GimpTool         *tool,
127                                                       GimpDisplay      *display);
128 static GimpUIManager * gimp_tool_real_get_popup      (GimpTool         *tool,
129                                                       const GimpCoords *coords,
130                                                       GdkModifierType   state,
131                                                       GimpDisplay      *display,
132                                                       const gchar     **ui_path);
133 static void            gimp_tool_real_options_notify (GimpTool         *tool,
134                                                       GimpToolOptions  *options,
135                                                       const GParamSpec *pspec);
136 
137 static void            gimp_tool_options_notify      (GimpToolOptions  *options,
138                                                       const GParamSpec *pspec,
139                                                       GimpTool         *tool);
140 static void            gimp_tool_clear_status        (GimpTool         *tool);
141 static void            gimp_tool_release             (GimpTool         *tool);
142 
143 
144 G_DEFINE_TYPE_WITH_CODE (GimpTool, gimp_tool, GIMP_TYPE_OBJECT,
145                          G_IMPLEMENT_INTERFACE (GIMP_TYPE_PROGRESS,
146                                                 gimp_tool_progress_iface_init))
147 
148 #define parent_class gimp_tool_parent_class
149 
150 static gint global_tool_ID = 1;
151 
152 
153 static void
gimp_tool_class_init(GimpToolClass * klass)154 gimp_tool_class_init (GimpToolClass *klass)
155 {
156   GObjectClass *object_class = G_OBJECT_CLASS (klass);
157 
158   object_class->constructed  = gimp_tool_constructed;
159   object_class->dispose      = gimp_tool_dispose;
160   object_class->finalize     = gimp_tool_finalize;
161   object_class->set_property = gimp_tool_set_property;
162   object_class->get_property = gimp_tool_get_property;
163 
164   klass->has_display         = gimp_tool_real_has_display;
165   klass->has_image           = gimp_tool_real_has_image;
166   klass->initialize          = gimp_tool_real_initialize;
167   klass->control             = gimp_tool_real_control;
168   klass->button_press        = gimp_tool_real_button_press;
169   klass->button_release      = gimp_tool_real_button_release;
170   klass->motion              = gimp_tool_real_motion;
171   klass->key_press           = gimp_tool_real_key_press;
172   klass->key_release         = gimp_tool_real_key_release;
173   klass->modifier_key        = gimp_tool_real_modifier_key;
174   klass->active_modifier_key = gimp_tool_real_active_modifier_key;
175   klass->oper_update         = gimp_tool_real_oper_update;
176   klass->cursor_update       = gimp_tool_real_cursor_update;
177   klass->can_undo            = gimp_tool_real_can_undo;
178   klass->can_redo            = gimp_tool_real_can_redo;
179   klass->undo                = gimp_tool_real_undo;
180   klass->redo                = gimp_tool_real_redo;
181   klass->get_popup           = gimp_tool_real_get_popup;
182   klass->options_notify      = gimp_tool_real_options_notify;
183 
184   g_object_class_install_property (object_class, PROP_TOOL_INFO,
185                                    g_param_spec_object ("tool-info",
186                                                         NULL, NULL,
187                                                         GIMP_TYPE_TOOL_INFO,
188                                                         GIMP_PARAM_READWRITE |
189                                                         G_PARAM_CONSTRUCT_ONLY));
190 }
191 
192 static void
gimp_tool_init(GimpTool * tool)193 gimp_tool_init (GimpTool *tool)
194 {
195   tool->tool_info             = NULL;
196   tool->ID                    = global_tool_ID++;
197   tool->control               = g_object_new (GIMP_TYPE_TOOL_CONTROL, NULL);
198   tool->display               = NULL;
199   tool->drawable              = NULL;
200   tool->focus_display         = NULL;
201   tool->modifier_state        = 0;
202   tool->active_modifier_state = 0;
203   tool->button_press_state    = 0;
204 }
205 
206 static void
gimp_tool_constructed(GObject * object)207 gimp_tool_constructed (GObject *object)
208 {
209   GimpTool *tool = GIMP_TOOL (object);
210 
211   G_OBJECT_CLASS (parent_class)->constructed (object);
212 
213   gimp_assert (GIMP_IS_TOOL_INFO (tool->tool_info));
214 
215   g_signal_connect_object (gimp_tool_get_options (tool), "notify",
216                            G_CALLBACK (gimp_tool_options_notify),
217                            tool, 0);
218 }
219 
220 static void
gimp_tool_dispose(GObject * object)221 gimp_tool_dispose (GObject *object)
222 {
223   GimpTool *tool = GIMP_TOOL (object);
224 
225   gimp_tool_control (tool, GIMP_TOOL_ACTION_HALT, tool->display);
226 
227   G_OBJECT_CLASS (parent_class)->dispose (object);
228 }
229 
230 static void
gimp_tool_finalize(GObject * object)231 gimp_tool_finalize (GObject *object)
232 {
233   GimpTool *tool = GIMP_TOOL (object);
234 
235   g_clear_object (&tool->tool_info);
236   g_clear_object (&tool->control);
237   g_clear_pointer (&tool->label,     g_free);
238   g_clear_pointer (&tool->undo_desc, g_free);
239   g_clear_pointer (&tool->icon_name, g_free);
240   g_clear_pointer (&tool->help_id,   g_free);
241 
242   G_OBJECT_CLASS (parent_class)->finalize (object);
243 }
244 
245 static void
gimp_tool_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)246 gimp_tool_set_property (GObject      *object,
247                         guint         property_id,
248                         const GValue *value,
249                         GParamSpec   *pspec)
250 {
251   GimpTool *tool = GIMP_TOOL (object);
252 
253   switch (property_id)
254     {
255     case PROP_TOOL_INFO:
256       tool->tool_info = g_value_dup_object (value);
257       break;
258 
259     default:
260       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
261       break;
262     }
263 }
264 
265 static void
gimp_tool_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)266 gimp_tool_get_property (GObject    *object,
267                         guint       property_id,
268                         GValue     *value,
269                         GParamSpec *pspec)
270 {
271   GimpTool *tool = GIMP_TOOL (object);
272 
273   switch (property_id)
274     {
275     case PROP_TOOL_INFO:
276       g_value_set_object (value, tool->tool_info);
277       break;
278 
279     default:
280       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
281       break;
282     }
283 }
284 
285 
286 /*  standard member functions  */
287 
288 static gboolean
gimp_tool_real_has_display(GimpTool * tool,GimpDisplay * display)289 gimp_tool_real_has_display (GimpTool    *tool,
290                             GimpDisplay *display)
291 {
292   return (display == tool->display ||
293           g_list_find (tool->status_displays, display));
294 }
295 
296 static GimpDisplay *
gimp_tool_real_has_image(GimpTool * tool,GimpImage * image)297 gimp_tool_real_has_image (GimpTool  *tool,
298                           GimpImage *image)
299 {
300   if (tool->display)
301     {
302       if (image && gimp_display_get_image (tool->display) == image)
303         return tool->display;
304 
305       /*  NULL image means any display  */
306       if (! image)
307         return tool->display;
308     }
309 
310   return NULL;
311 }
312 
313 static gboolean
gimp_tool_real_initialize(GimpTool * tool,GimpDisplay * display,GError ** error)314 gimp_tool_real_initialize (GimpTool     *tool,
315                            GimpDisplay  *display,
316                            GError      **error)
317 {
318   return TRUE;
319 }
320 
321 static void
gimp_tool_real_control(GimpTool * tool,GimpToolAction action,GimpDisplay * display)322 gimp_tool_real_control (GimpTool       *tool,
323                         GimpToolAction  action,
324                         GimpDisplay    *display)
325 {
326   switch (action)
327     {
328     case GIMP_TOOL_ACTION_PAUSE:
329     case GIMP_TOOL_ACTION_RESUME:
330       break;
331 
332     case GIMP_TOOL_ACTION_HALT:
333       tool->display  = NULL;
334       tool->drawable = NULL;
335       break;
336 
337     case GIMP_TOOL_ACTION_COMMIT:
338       break;
339     }
340 }
341 
342 static void
gimp_tool_real_button_press(GimpTool * tool,const GimpCoords * coords,guint32 time,GdkModifierType state,GimpButtonPressType press_type,GimpDisplay * display)343 gimp_tool_real_button_press (GimpTool            *tool,
344                              const GimpCoords    *coords,
345                              guint32              time,
346                              GdkModifierType      state,
347                              GimpButtonPressType  press_type,
348                              GimpDisplay         *display)
349 {
350   if (press_type == GIMP_BUTTON_PRESS_NORMAL)
351     {
352       GimpImage *image = gimp_display_get_image (display);
353 
354       tool->display  = display;
355       tool->drawable = gimp_image_get_active_drawable (image);
356 
357       gimp_tool_control_activate (tool->control);
358     }
359 }
360 
361 static void
gimp_tool_real_button_release(GimpTool * tool,const GimpCoords * coords,guint32 time,GdkModifierType state,GimpButtonReleaseType release_type,GimpDisplay * display)362 gimp_tool_real_button_release (GimpTool              *tool,
363                                const GimpCoords      *coords,
364                                guint32                time,
365                                GdkModifierType        state,
366                                GimpButtonReleaseType  release_type,
367                                GimpDisplay           *display)
368 {
369   gimp_tool_control_halt (tool->control);
370 }
371 
372 static void
gimp_tool_real_motion(GimpTool * tool,const GimpCoords * coords,guint32 time,GdkModifierType state,GimpDisplay * display)373 gimp_tool_real_motion (GimpTool         *tool,
374                        const GimpCoords *coords,
375                        guint32           time,
376                        GdkModifierType   state,
377                        GimpDisplay      *display)
378 {
379 }
380 
381 static gboolean
gimp_tool_real_key_press(GimpTool * tool,GdkEventKey * kevent,GimpDisplay * display)382 gimp_tool_real_key_press (GimpTool    *tool,
383                           GdkEventKey *kevent,
384                           GimpDisplay *display)
385 {
386   return FALSE;
387 }
388 
389 static gboolean
gimp_tool_real_key_release(GimpTool * tool,GdkEventKey * kevent,GimpDisplay * display)390 gimp_tool_real_key_release (GimpTool    *tool,
391                             GdkEventKey *kevent,
392                             GimpDisplay *display)
393 {
394   return FALSE;
395 }
396 
397 static void
gimp_tool_real_modifier_key(GimpTool * tool,GdkModifierType key,gboolean press,GdkModifierType state,GimpDisplay * display)398 gimp_tool_real_modifier_key (GimpTool        *tool,
399                              GdkModifierType  key,
400                              gboolean         press,
401                              GdkModifierType  state,
402                              GimpDisplay     *display)
403 {
404 }
405 
406 static void
gimp_tool_real_active_modifier_key(GimpTool * tool,GdkModifierType key,gboolean press,GdkModifierType state,GimpDisplay * display)407 gimp_tool_real_active_modifier_key (GimpTool        *tool,
408                                     GdkModifierType  key,
409                                     gboolean         press,
410                                     GdkModifierType  state,
411                                     GimpDisplay     *display)
412 {
413 }
414 
415 static void
gimp_tool_real_oper_update(GimpTool * tool,const GimpCoords * coords,GdkModifierType state,gboolean proximity,GimpDisplay * display)416 gimp_tool_real_oper_update (GimpTool         *tool,
417                             const GimpCoords *coords,
418                             GdkModifierType   state,
419                             gboolean          proximity,
420                             GimpDisplay      *display)
421 {
422 }
423 
424 static void
gimp_tool_real_cursor_update(GimpTool * tool,const GimpCoords * coords,GdkModifierType state,GimpDisplay * display)425 gimp_tool_real_cursor_update (GimpTool         *tool,
426                               const GimpCoords *coords,
427                               GdkModifierType   state,
428                               GimpDisplay      *display)
429 {
430   gimp_tool_set_cursor (tool, display,
431                         gimp_tool_control_get_cursor (tool->control),
432                         gimp_tool_control_get_tool_cursor (tool->control),
433                         gimp_tool_control_get_cursor_modifier (tool->control));
434 }
435 
436 static const gchar *
gimp_tool_real_can_undo(GimpTool * tool,GimpDisplay * display)437 gimp_tool_real_can_undo (GimpTool    *tool,
438                          GimpDisplay *display)
439 {
440   return NULL;
441 }
442 
443 static const gchar *
gimp_tool_real_can_redo(GimpTool * tool,GimpDisplay * display)444 gimp_tool_real_can_redo (GimpTool    *tool,
445                          GimpDisplay *display)
446 {
447   return NULL;
448 }
449 
450 static gboolean
gimp_tool_real_undo(GimpTool * tool,GimpDisplay * display)451 gimp_tool_real_undo (GimpTool    *tool,
452                      GimpDisplay *display)
453 {
454   return FALSE;
455 }
456 
457 static gboolean
gimp_tool_real_redo(GimpTool * tool,GimpDisplay * display)458 gimp_tool_real_redo (GimpTool    *tool,
459                      GimpDisplay *display)
460 {
461   return FALSE;
462 }
463 
464 static GimpUIManager *
gimp_tool_real_get_popup(GimpTool * tool,const GimpCoords * coords,GdkModifierType state,GimpDisplay * display,const gchar ** ui_path)465 gimp_tool_real_get_popup (GimpTool         *tool,
466                           const GimpCoords *coords,
467                           GdkModifierType   state,
468                           GimpDisplay      *display,
469                           const gchar     **ui_path)
470 {
471   *ui_path = NULL;
472 
473   return NULL;
474 }
475 
476 static void
gimp_tool_real_options_notify(GimpTool * tool,GimpToolOptions * options,const GParamSpec * pspec)477 gimp_tool_real_options_notify (GimpTool         *tool,
478                                GimpToolOptions  *options,
479                                const GParamSpec *pspec)
480 {
481 }
482 
483 
484 /*  public functions  */
485 
486 GimpToolOptions *
gimp_tool_get_options(GimpTool * tool)487 gimp_tool_get_options (GimpTool *tool)
488 {
489   g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);
490   g_return_val_if_fail (GIMP_IS_TOOL_INFO (tool->tool_info), NULL);
491 
492   return tool->tool_info->tool_options;
493 }
494 
495 void
gimp_tool_set_label(GimpTool * tool,const gchar * label)496 gimp_tool_set_label (GimpTool    *tool,
497                      const gchar *label)
498 {
499   g_return_if_fail (GIMP_IS_TOOL (tool));
500 
501   g_free (tool->label);
502   tool->label = g_strdup (label);
503 }
504 
505 const gchar *
gimp_tool_get_label(GimpTool * tool)506 gimp_tool_get_label (GimpTool *tool)
507 {
508   g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);
509 
510   if (tool->label)
511     return tool->label;
512 
513   return tool->tool_info->label;
514 }
515 
516 void
gimp_tool_set_undo_desc(GimpTool * tool,const gchar * undo_desc)517 gimp_tool_set_undo_desc (GimpTool    *tool,
518                          const gchar *undo_desc)
519 {
520   g_return_if_fail (GIMP_IS_TOOL (tool));
521 
522   g_free (tool->undo_desc);
523   tool->undo_desc = g_strdup (undo_desc);
524 }
525 
526 const gchar *
gimp_tool_get_undo_desc(GimpTool * tool)527 gimp_tool_get_undo_desc (GimpTool *tool)
528 {
529   g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);
530 
531   if (tool->undo_desc)
532     return tool->undo_desc;
533 
534   return tool->tool_info->label;
535 }
536 
537 void
gimp_tool_set_icon_name(GimpTool * tool,const gchar * icon_name)538 gimp_tool_set_icon_name (GimpTool    *tool,
539                          const gchar *icon_name)
540 {
541   g_return_if_fail (GIMP_IS_TOOL (tool));
542 
543   g_free (tool->icon_name);
544   tool->icon_name = g_strdup (icon_name);
545 }
546 
547 const gchar *
gimp_tool_get_icon_name(GimpTool * tool)548 gimp_tool_get_icon_name (GimpTool *tool)
549 {
550   g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);
551 
552   if (tool->icon_name)
553     return tool->icon_name;
554 
555   return gimp_viewable_get_icon_name (GIMP_VIEWABLE (tool->tool_info));
556 }
557 
558 void
gimp_tool_set_help_id(GimpTool * tool,const gchar * help_id)559 gimp_tool_set_help_id (GimpTool    *tool,
560                        const gchar *help_id)
561 {
562   g_return_if_fail (GIMP_IS_TOOL (tool));
563 
564   g_free (tool->help_id);
565   tool->help_id = g_strdup (help_id);
566 }
567 
568 const gchar *
gimp_tool_get_help_id(GimpTool * tool)569 gimp_tool_get_help_id (GimpTool *tool)
570 {
571   g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);
572 
573   if (tool->help_id)
574     return tool->help_id;
575 
576   return tool->tool_info->help_id;
577 }
578 
579 gboolean
gimp_tool_has_display(GimpTool * tool,GimpDisplay * display)580 gimp_tool_has_display (GimpTool    *tool,
581                        GimpDisplay *display)
582 {
583   g_return_val_if_fail (GIMP_IS_TOOL (tool), FALSE);
584   g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);
585 
586   return GIMP_TOOL_GET_CLASS (tool)->has_display (tool, display);
587 }
588 
589 GimpDisplay *
gimp_tool_has_image(GimpTool * tool,GimpImage * image)590 gimp_tool_has_image (GimpTool  *tool,
591                      GimpImage *image)
592 {
593   GimpDisplay *display;
594 
595   g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);
596   g_return_val_if_fail (image == NULL || GIMP_IS_IMAGE (image), NULL);
597 
598   display = GIMP_TOOL_GET_CLASS (tool)->has_image (tool, image);
599 
600   /*  check status displays last because they don't affect the tool
601    *  itself (unlike tool->display or draw_tool->display)
602    */
603   if (! display && tool->status_displays)
604     {
605       GList *list;
606 
607       for (list = tool->status_displays; list; list = g_list_next (list))
608         {
609           GimpDisplay *status_display = list->data;
610 
611           if (gimp_display_get_image (status_display) == image)
612             return status_display;
613         }
614 
615       /*  NULL image means any display  */
616       if (! image)
617         return tool->status_displays->data;
618     }
619 
620   return display;
621 }
622 
623 gboolean
gimp_tool_initialize(GimpTool * tool,GimpDisplay * display)624 gimp_tool_initialize (GimpTool    *tool,
625                       GimpDisplay *display)
626 {
627   GError *error = NULL;
628 
629   g_return_val_if_fail (GIMP_IS_TOOL (tool), FALSE);
630   g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);
631 
632   if (! GIMP_TOOL_GET_CLASS (tool)->initialize (tool, display, &error))
633     {
634       if (error)
635         {
636           gimp_tool_message_literal (tool, display, error->message);
637           g_clear_error (&error);
638         }
639 
640       return FALSE;
641     }
642 
643   return TRUE;
644 }
645 
646 void
gimp_tool_control(GimpTool * tool,GimpToolAction action,GimpDisplay * display)647 gimp_tool_control (GimpTool       *tool,
648                    GimpToolAction  action,
649                    GimpDisplay    *display)
650 {
651   g_return_if_fail (GIMP_IS_TOOL (tool));
652 
653   g_object_ref (tool);
654 
655   switch (action)
656     {
657     case GIMP_TOOL_ACTION_PAUSE:
658       if (! gimp_tool_control_is_paused (tool->control))
659         GIMP_TOOL_GET_CLASS (tool)->control (tool, action, display);
660 
661       gimp_tool_control_pause (tool->control);
662       break;
663 
664     case GIMP_TOOL_ACTION_RESUME:
665       if (gimp_tool_control_is_paused (tool->control))
666         {
667           gimp_tool_control_resume (tool->control);
668 
669           if (! gimp_tool_control_is_paused (tool->control))
670             GIMP_TOOL_GET_CLASS (tool)->control (tool, action, display);
671         }
672       else
673         {
674           g_warning ("gimp_tool_control: unable to RESUME tool with "
675                      "tool->control->paused_count == 0");
676         }
677       break;
678 
679     case GIMP_TOOL_ACTION_COMMIT:
680       gimp_tool_release (tool);
681 
682       GIMP_TOOL_GET_CLASS (tool)->control (tool, action, display);
683 
684       /*  always HALT after COMMIT here and not in each tool individually;
685        *  some tools interact with their subclasses (e.g. filter tool
686        *  and operation tool), and it's essential that COMMIT runs
687        *  through the entire class hierarchy before HALT
688        */
689       action = GIMP_TOOL_ACTION_HALT;
690 
691       /* pass through */
692     case GIMP_TOOL_ACTION_HALT:
693       gimp_tool_release (tool);
694 
695       GIMP_TOOL_GET_CLASS (tool)->control (tool, action, display);
696 
697       if (gimp_tool_control_is_active (tool->control))
698         gimp_tool_control_halt (tool->control);
699 
700       gimp_tool_clear_status (tool);
701       break;
702     }
703 
704   g_object_unref (tool);
705 }
706 
707 void
gimp_tool_button_press(GimpTool * tool,const GimpCoords * coords,guint32 time,GdkModifierType state,GimpButtonPressType press_type,GimpDisplay * display)708 gimp_tool_button_press (GimpTool            *tool,
709                         const GimpCoords    *coords,
710                         guint32              time,
711                         GdkModifierType      state,
712                         GimpButtonPressType  press_type,
713                         GimpDisplay         *display)
714 {
715   g_return_if_fail (GIMP_IS_TOOL (tool));
716   g_return_if_fail (coords != NULL);
717   g_return_if_fail (GIMP_IS_DISPLAY (display));
718 
719   GIMP_TOOL_GET_CLASS (tool)->button_press (tool, coords, time, state,
720                                             press_type, display);
721 
722   if (press_type == GIMP_BUTTON_PRESS_NORMAL &&
723       gimp_tool_control_is_active (tool->control))
724     {
725       tool->button_press_state    = state;
726       tool->active_modifier_state = state;
727 
728       tool->last_pointer_coords = *coords;
729       tool->last_pointer_time   = time - g_get_monotonic_time () / 1000;
730       tool->last_pointer_state  = state;
731 
732       if (gimp_tool_control_get_wants_click (tool->control))
733         {
734           tool->in_click_distance   = TRUE;
735           tool->got_motion_event    = FALSE;
736           tool->button_press_coords = *coords;
737           tool->button_press_time   = time;
738         }
739       else
740         {
741           tool->in_click_distance   = FALSE;
742         }
743     }
744 }
745 
746 static gboolean
gimp_tool_check_click_distance(GimpTool * tool,const GimpCoords * coords,guint32 time,GimpDisplay * display)747 gimp_tool_check_click_distance (GimpTool         *tool,
748                                 const GimpCoords *coords,
749                                 guint32           time,
750                                 GimpDisplay      *display)
751 {
752   GimpDisplayShell *shell;
753   gint              double_click_time;
754   gint              double_click_distance;
755 
756   if (! tool->in_click_distance)
757     return FALSE;
758 
759   shell = gimp_display_get_shell (display);
760 
761   g_object_get (gtk_widget_get_settings (GTK_WIDGET (shell)),
762                 "gtk-double-click-time",     &double_click_time,
763                 "gtk-double-click-distance", &double_click_distance,
764                 NULL);
765 
766   if ((time - tool->button_press_time) > double_click_time)
767     {
768       tool->in_click_distance = FALSE;
769     }
770   else
771     {
772       GimpDisplayShell *shell = gimp_display_get_shell (display);
773       gdouble           dx;
774       gdouble           dy;
775 
776       dx = SCALEX (shell, tool->button_press_coords.x - coords->x);
777       dy = SCALEY (shell, tool->button_press_coords.y - coords->y);
778 
779       if ((SQR (dx) + SQR (dy)) > SQR (double_click_distance))
780         {
781           tool->in_click_distance = FALSE;
782         }
783     }
784 
785   return tool->in_click_distance;
786 }
787 
788 void
gimp_tool_button_release(GimpTool * tool,const GimpCoords * coords,guint32 time,GdkModifierType state,GimpDisplay * display)789 gimp_tool_button_release (GimpTool         *tool,
790                           const GimpCoords *coords,
791                           guint32           time,
792                           GdkModifierType   state,
793                           GimpDisplay      *display)
794 {
795   GimpButtonReleaseType release_type = GIMP_BUTTON_RELEASE_NORMAL;
796   GimpCoords            my_coords;
797 
798   g_return_if_fail (GIMP_IS_TOOL (tool));
799   g_return_if_fail (coords != NULL);
800   g_return_if_fail (GIMP_IS_DISPLAY (display));
801   g_return_if_fail (gimp_tool_control_is_active (tool->control) == TRUE);
802 
803   g_object_ref (tool);
804 
805   tool->last_pointer_state = 0;
806 
807   my_coords = *coords;
808 
809   if (state & GDK_BUTTON3_MASK)
810     {
811       release_type = GIMP_BUTTON_RELEASE_CANCEL;
812     }
813   else if (gimp_tool_control_get_wants_click (tool->control))
814     {
815       if (gimp_tool_check_click_distance (tool, coords, time, display))
816         {
817           release_type = GIMP_BUTTON_RELEASE_CLICK;
818           my_coords    = tool->button_press_coords;
819 
820           if (tool->got_motion_event)
821             {
822               /*  if there has been a motion() since button_press(),
823                *  synthesize a motion() back to the recorded press
824                *  coordinates
825                */
826               GIMP_TOOL_GET_CLASS (tool)->motion (tool, &my_coords, time,
827                                                   state & GDK_BUTTON1_MASK,
828                                                   display);
829             }
830         }
831       else if (! tool->got_motion_event)
832         {
833           release_type = GIMP_BUTTON_RELEASE_NO_MOTION;
834         }
835     }
836 
837   GIMP_TOOL_GET_CLASS (tool)->button_release (tool, &my_coords, time, state,
838                                               release_type, display);
839 
840   g_warn_if_fail (gimp_tool_control_is_active (tool->control) == FALSE);
841 
842   if (tool->active_modifier_state                            != 0 &&
843       gimp_tool_control_get_active_modifiers (tool->control) !=
844       GIMP_TOOL_ACTIVE_MODIFIERS_SAME)
845     {
846       gimp_tool_control_activate (tool->control);
847 
848       gimp_tool_set_active_modifier_state (tool, 0, display);
849 
850       gimp_tool_control_halt (tool->control);
851     }
852 
853   tool->button_press_state    = 0;
854   tool->active_modifier_state = 0;
855 
856   g_object_unref (tool);
857 }
858 
859 void
gimp_tool_motion(GimpTool * tool,const GimpCoords * coords,guint32 time,GdkModifierType state,GimpDisplay * display)860 gimp_tool_motion (GimpTool         *tool,
861                   const GimpCoords *coords,
862                   guint32           time,
863                   GdkModifierType   state,
864                   GimpDisplay      *display)
865 {
866   g_return_if_fail (GIMP_IS_TOOL (tool));
867   g_return_if_fail (coords != NULL);
868   g_return_if_fail (GIMP_IS_DISPLAY (display));
869   g_return_if_fail (gimp_tool_control_is_active (tool->control) == TRUE);
870 
871   tool->got_motion_event = TRUE;
872 
873   tool->last_pointer_coords = *coords;
874   tool->last_pointer_time   = time - g_get_monotonic_time () / 1000;
875   tool->last_pointer_state  = state;
876 
877   GIMP_TOOL_GET_CLASS (tool)->motion (tool, coords, time, state, display);
878 }
879 
880 void
gimp_tool_set_focus_display(GimpTool * tool,GimpDisplay * display)881 gimp_tool_set_focus_display (GimpTool    *tool,
882                              GimpDisplay *display)
883 {
884   g_return_if_fail (GIMP_IS_TOOL (tool));
885   g_return_if_fail (display == NULL || GIMP_IS_DISPLAY (display));
886   g_return_if_fail (gimp_tool_control_is_active (tool->control) == FALSE);
887 
888   GIMP_LOG (TOOL_FOCUS, "tool: %p  focus_display: %p  tool->focus_display: %p",
889             tool, display, tool->focus_display);
890 
891   if (display != tool->focus_display)
892     {
893       if (tool->focus_display)
894         {
895           if (tool->active_modifier_state != 0)
896             {
897               gimp_tool_control_activate (tool->control);
898 
899               gimp_tool_set_active_modifier_state (tool, 0, tool->focus_display);
900 
901               gimp_tool_control_halt (tool->control);
902             }
903 
904           if (tool->modifier_state != 0)
905             gimp_tool_set_modifier_state (tool, 0, tool->focus_display);
906         }
907 
908       tool->focus_display = display;
909     }
910 }
911 
912 gboolean
gimp_tool_key_press(GimpTool * tool,GdkEventKey * kevent,GimpDisplay * display)913 gimp_tool_key_press (GimpTool    *tool,
914                      GdkEventKey *kevent,
915                      GimpDisplay *display)
916 {
917   g_return_val_if_fail (GIMP_IS_TOOL (tool), FALSE);
918   g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);
919   g_return_val_if_fail (display == tool->focus_display, FALSE);
920   g_return_val_if_fail (gimp_tool_control_is_active (tool->control) == FALSE,
921                         FALSE);
922 
923   return GIMP_TOOL_GET_CLASS (tool)->key_press (tool, kevent, display);
924 }
925 
926 gboolean
gimp_tool_key_release(GimpTool * tool,GdkEventKey * kevent,GimpDisplay * display)927 gimp_tool_key_release (GimpTool    *tool,
928                        GdkEventKey *kevent,
929                        GimpDisplay *display)
930 {
931   g_return_val_if_fail (GIMP_IS_TOOL (tool), FALSE);
932   g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);
933   g_return_val_if_fail (display == tool->focus_display, FALSE);
934   g_return_val_if_fail (gimp_tool_control_is_active (tool->control) == FALSE,
935                         FALSE);
936 
937   return GIMP_TOOL_GET_CLASS (tool)->key_release (tool, kevent, display);
938 }
939 
940 static void
gimp_tool_modifier_key(GimpTool * tool,GdkModifierType key,gboolean press,GdkModifierType state,GimpDisplay * display)941 gimp_tool_modifier_key (GimpTool        *tool,
942                         GdkModifierType  key,
943                         gboolean         press,
944                         GdkModifierType  state,
945                         GimpDisplay     *display)
946 {
947   g_return_if_fail (GIMP_IS_TOOL (tool));
948   g_return_if_fail (GIMP_IS_DISPLAY (display));
949   g_return_if_fail (display == tool->focus_display);
950 
951   GIMP_TOOL_GET_CLASS (tool)->modifier_key (tool, key, press, state, display);
952 }
953 
954 static void
gimp_tool_active_modifier_key(GimpTool * tool,GdkModifierType key,gboolean press,GdkModifierType state,GimpDisplay * display)955 gimp_tool_active_modifier_key (GimpTool        *tool,
956                                GdkModifierType  key,
957                                gboolean         press,
958                                GdkModifierType  state,
959                                GimpDisplay     *display)
960 {
961   g_return_if_fail (GIMP_IS_TOOL (tool));
962   g_return_if_fail (GIMP_IS_DISPLAY (display));
963   g_return_if_fail (display == tool->focus_display);
964 
965   GIMP_TOOL_GET_CLASS (tool)->active_modifier_key (tool, key, press, state,
966                                                    display);
967 }
968 
969 static gboolean
state_changed(GdkModifierType old_state,GdkModifierType new_state,GdkModifierType modifier,gboolean * press)970 state_changed (GdkModifierType  old_state,
971                GdkModifierType  new_state,
972                GdkModifierType  modifier,
973                gboolean        *press)
974 {
975   if ((old_state & modifier) != (new_state & modifier))
976     {
977       *press = (new_state & modifier) ? TRUE : FALSE;
978 
979       return TRUE;
980     }
981 
982   return FALSE;
983 }
984 
985 void
gimp_tool_set_modifier_state(GimpTool * tool,GdkModifierType state,GimpDisplay * display)986 gimp_tool_set_modifier_state (GimpTool        *tool,
987                               GdkModifierType  state,
988                               GimpDisplay     *display)
989 {
990   gboolean press;
991 
992   g_return_if_fail (GIMP_IS_TOOL (tool));
993   g_return_if_fail (GIMP_IS_DISPLAY (display));
994   g_return_if_fail (gimp_tool_control_is_active (tool->control) == FALSE);
995 
996   GIMP_LOG (TOOL_FOCUS, "tool: %p  display: %p  tool->focus_display: %p",
997             tool, display, tool->focus_display);
998 
999   g_return_if_fail (display == tool->focus_display);
1000 
1001   if (state_changed (tool->modifier_state, state, GDK_SHIFT_MASK, &press))
1002     {
1003       gimp_tool_modifier_key (tool, GDK_SHIFT_MASK,
1004                               press, state,
1005                               display);
1006     }
1007 
1008   if (state_changed (tool->modifier_state, state, GDK_CONTROL_MASK, &press))
1009     {
1010       gimp_tool_modifier_key (tool, GDK_CONTROL_MASK,
1011                               press, state,
1012                               display);
1013     }
1014 
1015   if (state_changed (tool->modifier_state, state, GDK_MOD1_MASK, &press))
1016     {
1017       gimp_tool_modifier_key (tool, GDK_MOD1_MASK,
1018                               press, state,
1019                               display);
1020     }
1021 
1022   if (state_changed (tool->modifier_state, state, GDK_MOD2_MASK, &press))
1023     {
1024       gimp_tool_modifier_key (tool, GDK_MOD2_MASK,
1025                               press, state,
1026                               display);
1027     }
1028 
1029   tool->modifier_state = state;
1030 }
1031 
1032 void
gimp_tool_set_active_modifier_state(GimpTool * tool,GdkModifierType state,GimpDisplay * display)1033 gimp_tool_set_active_modifier_state (GimpTool        *tool,
1034                                      GdkModifierType  state,
1035                                      GimpDisplay     *display)
1036 {
1037   GimpToolActiveModifiers active_modifiers;
1038   gboolean                press;
1039 
1040   g_return_if_fail (GIMP_IS_TOOL (tool));
1041   g_return_if_fail (GIMP_IS_DISPLAY (display));
1042   g_return_if_fail (gimp_tool_control_is_active (tool->control) == TRUE);
1043 
1044   GIMP_LOG (TOOL_FOCUS, "tool: %p  display: %p  tool->focus_display: %p",
1045             tool, display, tool->focus_display);
1046 
1047   g_return_if_fail (display == tool->focus_display);
1048 
1049   active_modifiers = gimp_tool_control_get_active_modifiers (tool->control);
1050 
1051   if (state_changed (tool->active_modifier_state, state, GDK_SHIFT_MASK,
1052                      &press))
1053     {
1054 #ifdef DEBUG_ACTIVE_STATE
1055       g_printerr ("%s: SHIFT %s\n", G_STRFUNC,
1056                   press ? "pressed" : "released");
1057 #endif
1058 
1059       switch (active_modifiers)
1060         {
1061         case GIMP_TOOL_ACTIVE_MODIFIERS_OFF:
1062           break;
1063 
1064         case GIMP_TOOL_ACTIVE_MODIFIERS_SAME:
1065           gimp_tool_modifier_key (tool, GDK_SHIFT_MASK,
1066                                   press, state,
1067                                   display);
1068           break;
1069 
1070         case GIMP_TOOL_ACTIVE_MODIFIERS_SEPARATE:
1071           if (! press && (tool->button_press_state & GDK_SHIFT_MASK))
1072             {
1073               tool->button_press_state &= ~GDK_SHIFT_MASK;
1074             }
1075           else
1076             {
1077               gimp_tool_active_modifier_key (tool, GDK_SHIFT_MASK,
1078                                              press, state,
1079                                              display);
1080             }
1081           break;
1082         }
1083     }
1084 
1085   if (state_changed (tool->active_modifier_state, state, GDK_CONTROL_MASK,
1086                      &press))
1087     {
1088 #ifdef DEBUG_ACTIVE_STATE
1089       g_printerr ("%s: CONTROL %s\n", G_STRFUNC,
1090                   press ? "pressed" : "released");
1091 #endif
1092 
1093       switch (active_modifiers)
1094         {
1095         case GIMP_TOOL_ACTIVE_MODIFIERS_OFF:
1096           break;
1097 
1098         case GIMP_TOOL_ACTIVE_MODIFIERS_SAME:
1099           gimp_tool_modifier_key (tool, GDK_CONTROL_MASK,
1100                                   press, state,
1101                                   display);
1102           break;
1103 
1104         case GIMP_TOOL_ACTIVE_MODIFIERS_SEPARATE:
1105           if (! press && (tool->button_press_state & GDK_CONTROL_MASK))
1106             {
1107               tool->button_press_state &= ~GDK_CONTROL_MASK;
1108             }
1109           else
1110             {
1111               gimp_tool_active_modifier_key (tool, GDK_CONTROL_MASK,
1112                                              press, state,
1113                                              display);
1114             }
1115           break;
1116         }
1117     }
1118 
1119   if (state_changed (tool->active_modifier_state, state, GDK_MOD1_MASK,
1120                      &press))
1121     {
1122 #ifdef DEBUG_ACTIVE_STATE
1123       g_printerr ("%s: ALT %s\n", G_STRFUNC,
1124                   press ? "pressed" : "released");
1125 #endif
1126 
1127       switch (active_modifiers)
1128         {
1129         case GIMP_TOOL_ACTIVE_MODIFIERS_OFF:
1130           break;
1131 
1132         case GIMP_TOOL_ACTIVE_MODIFIERS_SAME:
1133           gimp_tool_modifier_key (tool, GDK_MOD1_MASK,
1134                                   press, state,
1135                                   display);
1136           break;
1137 
1138         case GIMP_TOOL_ACTIVE_MODIFIERS_SEPARATE:
1139           if (! press && (tool->button_press_state & GDK_MOD1_MASK))
1140             {
1141               tool->button_press_state &= ~GDK_MOD1_MASK;
1142             }
1143           else
1144             {
1145               gimp_tool_active_modifier_key (tool, GDK_MOD1_MASK,
1146                                              press, state,
1147                                              display);
1148             }
1149           break;
1150         }
1151     }
1152 
1153   if (state_changed (tool->active_modifier_state, state, GDK_MOD2_MASK,
1154                      &press))
1155     {
1156 #ifdef DEBUG_ACTIVE_STATE
1157       g_printerr ("%s: MOD2 %s\n", G_STRFUNC,
1158                   press ? "pressed" : "released");
1159 #endif
1160 
1161       switch (active_modifiers)
1162         {
1163         case GIMP_TOOL_ACTIVE_MODIFIERS_OFF:
1164           break;
1165 
1166         case GIMP_TOOL_ACTIVE_MODIFIERS_SAME:
1167           gimp_tool_modifier_key (tool, GDK_MOD2_MASK,
1168                                   press, state,
1169                                   display);
1170           break;
1171 
1172         case GIMP_TOOL_ACTIVE_MODIFIERS_SEPARATE:
1173           if (! press && (tool->button_press_state & GDK_MOD2_MASK))
1174             {
1175               tool->button_press_state &= ~GDK_MOD2_MASK;
1176             }
1177           else
1178             {
1179               gimp_tool_active_modifier_key (tool, GDK_MOD2_MASK,
1180                                              press, state,
1181                                              display);
1182             }
1183           break;
1184         }
1185     }
1186 
1187   tool->active_modifier_state = state;
1188 
1189   if (active_modifiers == GIMP_TOOL_ACTIVE_MODIFIERS_SAME)
1190     tool->modifier_state = state;
1191 }
1192 
1193 void
gimp_tool_oper_update(GimpTool * tool,const GimpCoords * coords,GdkModifierType state,gboolean proximity,GimpDisplay * display)1194 gimp_tool_oper_update (GimpTool         *tool,
1195                        const GimpCoords *coords,
1196                        GdkModifierType   state,
1197                        gboolean          proximity,
1198                        GimpDisplay      *display)
1199 {
1200   g_return_if_fail (GIMP_IS_TOOL (tool));
1201   g_return_if_fail (coords != NULL);
1202   g_return_if_fail (GIMP_IS_DISPLAY (display));
1203   g_return_if_fail (gimp_tool_control_is_active (tool->control) == FALSE);
1204 
1205   GIMP_TOOL_GET_CLASS (tool)->oper_update (tool, coords, state, proximity,
1206                                            display);
1207 
1208   if (G_UNLIKELY (gimp_image_is_empty (gimp_display_get_image (display)) &&
1209                   ! gimp_tool_control_get_handle_empty_image (tool->control)))
1210     {
1211       gimp_tool_replace_status (tool, display,
1212                                 "%s",
1213                                 _("Can't work on an empty image, "
1214                                   "add a layer first"));
1215     }
1216 }
1217 
1218 void
gimp_tool_cursor_update(GimpTool * tool,const GimpCoords * coords,GdkModifierType state,GimpDisplay * display)1219 gimp_tool_cursor_update (GimpTool         *tool,
1220                          const GimpCoords *coords,
1221                          GdkModifierType   state,
1222                          GimpDisplay      *display)
1223 {
1224   g_return_if_fail (GIMP_IS_TOOL (tool));
1225   g_return_if_fail (coords != NULL);
1226   g_return_if_fail (GIMP_IS_DISPLAY (display));
1227   g_return_if_fail (gimp_tool_control_is_active (tool->control) == FALSE);
1228 
1229   GIMP_TOOL_GET_CLASS (tool)->cursor_update (tool, coords, state, display);
1230 }
1231 
1232 const gchar *
gimp_tool_can_undo(GimpTool * tool,GimpDisplay * display)1233 gimp_tool_can_undo (GimpTool    *tool,
1234                     GimpDisplay *display)
1235 {
1236   g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);
1237   g_return_val_if_fail (GIMP_IS_DISPLAY (display), NULL);
1238 
1239   if (display == tool->display)
1240     return GIMP_TOOL_GET_CLASS (tool)->can_undo (tool, display);
1241 
1242   return NULL;
1243 }
1244 
1245 const gchar *
gimp_tool_can_redo(GimpTool * tool,GimpDisplay * display)1246 gimp_tool_can_redo (GimpTool    *tool,
1247                     GimpDisplay *display)
1248 {
1249   g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);
1250   g_return_val_if_fail (GIMP_IS_DISPLAY (display), NULL);
1251 
1252   if (display == tool->display)
1253     return GIMP_TOOL_GET_CLASS (tool)->can_redo (tool, display);
1254 
1255   return NULL;
1256 }
1257 
1258 gboolean
gimp_tool_undo(GimpTool * tool,GimpDisplay * display)1259 gimp_tool_undo (GimpTool    *tool,
1260                 GimpDisplay *display)
1261 {
1262   g_return_val_if_fail (GIMP_IS_TOOL (tool), FALSE);
1263   g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);
1264 
1265   if (gimp_tool_can_undo (tool, display))
1266     return GIMP_TOOL_GET_CLASS (tool)->undo (tool, display);
1267 
1268   return FALSE;
1269 }
1270 
1271 gboolean
gimp_tool_redo(GimpTool * tool,GimpDisplay * display)1272 gimp_tool_redo (GimpTool    *tool,
1273                 GimpDisplay *display)
1274 {
1275   g_return_val_if_fail (GIMP_IS_TOOL (tool), FALSE);
1276   g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);
1277 
1278   if (gimp_tool_can_redo (tool, display))
1279     return GIMP_TOOL_GET_CLASS (tool)->redo (tool, display);
1280 
1281   return FALSE;
1282 }
1283 
1284 GimpUIManager *
gimp_tool_get_popup(GimpTool * tool,const GimpCoords * coords,GdkModifierType state,GimpDisplay * display,const gchar ** ui_path)1285 gimp_tool_get_popup (GimpTool         *tool,
1286                      const GimpCoords *coords,
1287                      GdkModifierType   state,
1288                      GimpDisplay      *display,
1289                      const gchar     **ui_path)
1290 {
1291   g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);
1292   g_return_val_if_fail (coords != NULL, NULL);
1293   g_return_val_if_fail (GIMP_IS_DISPLAY (display), NULL);
1294   g_return_val_if_fail (ui_path != NULL, NULL);
1295 
1296   return GIMP_TOOL_GET_CLASS (tool)->get_popup (tool, coords, state, display,
1297                                                 ui_path);
1298 }
1299 
1300 void
gimp_tool_push_status(GimpTool * tool,GimpDisplay * display,const gchar * format,...)1301 gimp_tool_push_status (GimpTool    *tool,
1302                        GimpDisplay *display,
1303                        const gchar *format,
1304                        ...)
1305 {
1306   GimpDisplayShell *shell;
1307   const gchar      *icon_name;
1308   va_list           args;
1309 
1310   g_return_if_fail (GIMP_IS_TOOL (tool));
1311   g_return_if_fail (GIMP_IS_DISPLAY (display));
1312   g_return_if_fail (format != NULL);
1313 
1314   shell = gimp_display_get_shell (display);
1315 
1316   icon_name = gimp_viewable_get_icon_name (GIMP_VIEWABLE (tool->tool_info));
1317 
1318   va_start (args, format);
1319 
1320   gimp_statusbar_push_valist (gimp_display_shell_get_statusbar (shell),
1321                               G_OBJECT_TYPE_NAME (tool), icon_name,
1322                               format, args);
1323 
1324   va_end (args);
1325 
1326   tool->status_displays = g_list_remove (tool->status_displays, display);
1327   tool->status_displays = g_list_prepend (tool->status_displays, display);
1328 }
1329 
1330 void
gimp_tool_push_status_coords(GimpTool * tool,GimpDisplay * display,GimpCursorPrecision precision,const gchar * title,gdouble x,const gchar * separator,gdouble y,const gchar * help)1331 gimp_tool_push_status_coords (GimpTool            *tool,
1332                               GimpDisplay         *display,
1333                               GimpCursorPrecision  precision,
1334                               const gchar         *title,
1335                               gdouble              x,
1336                               const gchar         *separator,
1337                               gdouble              y,
1338                               const gchar         *help)
1339 {
1340   GimpDisplayShell *shell;
1341   const gchar      *icon_name;
1342 
1343   g_return_if_fail (GIMP_IS_TOOL (tool));
1344   g_return_if_fail (GIMP_IS_DISPLAY (display));
1345 
1346   shell = gimp_display_get_shell (display);
1347 
1348   icon_name = gimp_viewable_get_icon_name (GIMP_VIEWABLE (tool->tool_info));
1349 
1350   gimp_statusbar_push_coords (gimp_display_shell_get_statusbar (shell),
1351                               G_OBJECT_TYPE_NAME (tool), icon_name,
1352                               precision, title, x, separator, y,
1353                               help);
1354 
1355   tool->status_displays = g_list_remove (tool->status_displays, display);
1356   tool->status_displays = g_list_prepend (tool->status_displays, display);
1357 }
1358 
1359 void
gimp_tool_push_status_length(GimpTool * tool,GimpDisplay * display,const gchar * title,GimpOrientationType axis,gdouble value,const gchar * help)1360 gimp_tool_push_status_length (GimpTool            *tool,
1361                               GimpDisplay         *display,
1362                               const gchar         *title,
1363                               GimpOrientationType  axis,
1364                               gdouble              value,
1365                               const gchar         *help)
1366 {
1367   GimpDisplayShell *shell;
1368   const gchar      *icon_name;
1369 
1370   g_return_if_fail (GIMP_IS_TOOL (tool));
1371   g_return_if_fail (GIMP_IS_DISPLAY (display));
1372 
1373   shell = gimp_display_get_shell (display);
1374 
1375   icon_name = gimp_viewable_get_icon_name (GIMP_VIEWABLE (tool->tool_info));
1376 
1377   gimp_statusbar_push_length (gimp_display_shell_get_statusbar (shell),
1378                               G_OBJECT_TYPE_NAME (tool), icon_name,
1379                               title, axis, value, help);
1380 
1381   tool->status_displays = g_list_remove (tool->status_displays, display);
1382   tool->status_displays = g_list_prepend (tool->status_displays, display);
1383 }
1384 
1385 void
gimp_tool_replace_status(GimpTool * tool,GimpDisplay * display,const gchar * format,...)1386 gimp_tool_replace_status (GimpTool    *tool,
1387                           GimpDisplay *display,
1388                           const gchar *format,
1389                           ...)
1390 {
1391   GimpDisplayShell *shell;
1392   const gchar      *icon_name;
1393   va_list           args;
1394 
1395   g_return_if_fail (GIMP_IS_TOOL (tool));
1396   g_return_if_fail (GIMP_IS_DISPLAY (display));
1397   g_return_if_fail (format != NULL);
1398 
1399   shell = gimp_display_get_shell (display);
1400 
1401   icon_name = gimp_viewable_get_icon_name (GIMP_VIEWABLE (tool->tool_info));
1402 
1403   va_start (args, format);
1404 
1405   gimp_statusbar_replace_valist (gimp_display_shell_get_statusbar (shell),
1406                                  G_OBJECT_TYPE_NAME (tool), icon_name,
1407                                  format, args);
1408 
1409   va_end (args);
1410 
1411   tool->status_displays = g_list_remove (tool->status_displays, display);
1412   tool->status_displays = g_list_prepend (tool->status_displays, display);
1413 }
1414 
1415 void
gimp_tool_pop_status(GimpTool * tool,GimpDisplay * display)1416 gimp_tool_pop_status (GimpTool    *tool,
1417                       GimpDisplay *display)
1418 {
1419   GimpDisplayShell *shell;
1420 
1421   g_return_if_fail (GIMP_IS_TOOL (tool));
1422   g_return_if_fail (GIMP_IS_DISPLAY (display));
1423 
1424   shell = gimp_display_get_shell (display);
1425 
1426   gimp_statusbar_pop (gimp_display_shell_get_statusbar (shell),
1427                       G_OBJECT_TYPE_NAME (tool));
1428 
1429   tool->status_displays = g_list_remove (tool->status_displays, display);
1430 }
1431 
1432 void
gimp_tool_message(GimpTool * tool,GimpDisplay * display,const gchar * format,...)1433 gimp_tool_message (GimpTool    *tool,
1434                    GimpDisplay *display,
1435                    const gchar *format,
1436                    ...)
1437 {
1438   va_list args;
1439 
1440   g_return_if_fail (GIMP_IS_TOOL (tool));
1441   g_return_if_fail (GIMP_IS_DISPLAY (display));
1442   g_return_if_fail (format != NULL);
1443 
1444   va_start (args, format);
1445 
1446   gimp_message_valist (display->gimp, G_OBJECT (display),
1447                        GIMP_MESSAGE_WARNING, format, args);
1448 
1449   va_end (args);
1450 }
1451 
1452 void
gimp_tool_message_literal(GimpTool * tool,GimpDisplay * display,const gchar * message)1453 gimp_tool_message_literal (GimpTool    *tool,
1454                            GimpDisplay *display,
1455                            const gchar *message)
1456 {
1457   g_return_if_fail (GIMP_IS_TOOL (tool));
1458   g_return_if_fail (GIMP_IS_DISPLAY (display));
1459   g_return_if_fail (message != NULL);
1460 
1461   gimp_message_literal (display->gimp, G_OBJECT (display),
1462                         GIMP_MESSAGE_WARNING, message);
1463 }
1464 
1465 void
gimp_tool_set_cursor(GimpTool * tool,GimpDisplay * display,GimpCursorType cursor,GimpToolCursorType tool_cursor,GimpCursorModifier modifier)1466 gimp_tool_set_cursor (GimpTool           *tool,
1467                       GimpDisplay        *display,
1468                       GimpCursorType      cursor,
1469                       GimpToolCursorType  tool_cursor,
1470                       GimpCursorModifier  modifier)
1471 {
1472   g_return_if_fail (GIMP_IS_TOOL (tool));
1473   g_return_if_fail (GIMP_IS_DISPLAY (display));
1474 
1475   gimp_display_shell_set_cursor (gimp_display_get_shell (display),
1476                                  cursor, tool_cursor, modifier);
1477 }
1478 
1479 
1480 /*  private functions  */
1481 
1482 static void
gimp_tool_options_notify(GimpToolOptions * options,const GParamSpec * pspec,GimpTool * tool)1483 gimp_tool_options_notify (GimpToolOptions  *options,
1484                           const GParamSpec *pspec,
1485                           GimpTool         *tool)
1486 {
1487   GIMP_TOOL_GET_CLASS (tool)->options_notify (tool, options, pspec);
1488 }
1489 
1490 static void
gimp_tool_clear_status(GimpTool * tool)1491 gimp_tool_clear_status (GimpTool *tool)
1492 {
1493   g_return_if_fail (GIMP_IS_TOOL (tool));
1494 
1495   while (tool->status_displays)
1496     gimp_tool_pop_status (tool, tool->status_displays->data);
1497 }
1498 
1499 static void
gimp_tool_release(GimpTool * tool)1500 gimp_tool_release (GimpTool *tool)
1501 {
1502   if (tool->last_pointer_state &&
1503       gimp_tool_control_is_active (tool->control))
1504     {
1505       gimp_tool_button_release (
1506         tool,
1507         &tool->last_pointer_coords,
1508         tool->last_pointer_time + g_get_monotonic_time () / 1000,
1509         tool->last_pointer_state,
1510         tool->display);
1511     }
1512 }
1513