1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimpcanvasguide.c
5  * Copyright (C) 2010 Michael Natterer <mitch@gimp.org>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #include "config.h"
22 
23 #include <gegl.h>
24 #include <gtk/gtk.h>
25 
26 #include "libgimpbase/gimpbase.h"
27 #include "libgimpmath/gimpmath.h"
28 
29 #include "display-types.h"
30 
31 #include "gimpcanvas-style.h"
32 #include "gimpcanvasguide.h"
33 #include "gimpdisplayshell.h"
34 
35 
36 enum
37 {
38   PROP_0,
39   PROP_ORIENTATION,
40   PROP_POSITION,
41   PROP_STYLE
42 };
43 
44 
45 typedef struct _GimpCanvasGuidePrivate GimpCanvasGuidePrivate;
46 
47 struct _GimpCanvasGuidePrivate
48 {
49   GimpOrientationType  orientation;
50   gint                 position;
51 
52   GimpGuideStyle       style;
53 };
54 
55 #define GET_PRIVATE(guide) \
56         ((GimpCanvasGuidePrivate *) gimp_canvas_guide_get_instance_private ((GimpCanvasGuide *) (guide)))
57 
58 
59 /*  local function prototypes  */
60 
61 static void             gimp_canvas_guide_set_property (GObject        *object,
62                                                         guint           property_id,
63                                                         const GValue   *value,
64                                                         GParamSpec     *pspec);
65 static void             gimp_canvas_guide_get_property (GObject        *object,
66                                                         guint           property_id,
67                                                         GValue         *value,
68                                                         GParamSpec     *pspec);
69 static void             gimp_canvas_guide_draw         (GimpCanvasItem *item,
70                                                         cairo_t        *cr);
71 static cairo_region_t * gimp_canvas_guide_get_extents  (GimpCanvasItem *item);
72 static void             gimp_canvas_guide_stroke       (GimpCanvasItem *item,
73                                                         cairo_t        *cr);
74 
75 
G_DEFINE_TYPE_WITH_PRIVATE(GimpCanvasGuide,gimp_canvas_guide,GIMP_TYPE_CANVAS_ITEM)76 G_DEFINE_TYPE_WITH_PRIVATE (GimpCanvasGuide, gimp_canvas_guide,
77                             GIMP_TYPE_CANVAS_ITEM)
78 
79 #define parent_class gimp_canvas_guide_parent_class
80 
81 
82 static void
83 gimp_canvas_guide_class_init (GimpCanvasGuideClass *klass)
84 {
85   GObjectClass        *object_class = G_OBJECT_CLASS (klass);
86   GimpCanvasItemClass *item_class   = GIMP_CANVAS_ITEM_CLASS (klass);
87 
88   object_class->set_property = gimp_canvas_guide_set_property;
89   object_class->get_property = gimp_canvas_guide_get_property;
90 
91   item_class->draw           = gimp_canvas_guide_draw;
92   item_class->get_extents    = gimp_canvas_guide_get_extents;
93   item_class->stroke         = gimp_canvas_guide_stroke;
94 
95   g_object_class_install_property (object_class, PROP_ORIENTATION,
96                                    g_param_spec_enum ("orientation", NULL, NULL,
97                                                       GIMP_TYPE_ORIENTATION_TYPE,
98                                                       GIMP_ORIENTATION_HORIZONTAL,
99                                                       GIMP_PARAM_READWRITE));
100 
101   g_object_class_install_property (object_class, PROP_POSITION,
102                                    g_param_spec_int ("position", NULL, NULL,
103                                                      -GIMP_MAX_IMAGE_SIZE,
104                                                      GIMP_MAX_IMAGE_SIZE, 0,
105                                                      GIMP_PARAM_READWRITE));
106 
107   g_object_class_install_property (object_class, PROP_STYLE,
108                                    g_param_spec_enum ("style", NULL, NULL,
109                                                       GIMP_TYPE_GUIDE_STYLE,
110                                                       GIMP_GUIDE_STYLE_NONE,
111                                                       GIMP_PARAM_READWRITE));
112 }
113 
114 static void
gimp_canvas_guide_init(GimpCanvasGuide * guide)115 gimp_canvas_guide_init (GimpCanvasGuide *guide)
116 {
117 }
118 
119 static void
gimp_canvas_guide_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)120 gimp_canvas_guide_set_property (GObject      *object,
121                                 guint         property_id,
122                                 const GValue *value,
123                                 GParamSpec   *pspec)
124 {
125   GimpCanvasGuidePrivate *private = GET_PRIVATE (object);
126 
127   switch (property_id)
128     {
129     case PROP_ORIENTATION:
130       private->orientation = g_value_get_enum (value);
131       break;
132     case PROP_POSITION:
133       private->position = g_value_get_int (value);
134       break;
135     case PROP_STYLE:
136       private->style = g_value_get_enum (value);
137       break;
138 
139     default:
140       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
141       break;
142     }
143 }
144 
145 static void
gimp_canvas_guide_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)146 gimp_canvas_guide_get_property (GObject    *object,
147                                 guint       property_id,
148                                 GValue     *value,
149                                 GParamSpec *pspec)
150 {
151   GimpCanvasGuidePrivate *private = GET_PRIVATE (object);
152 
153   switch (property_id)
154     {
155     case PROP_ORIENTATION:
156       g_value_set_enum (value, private->orientation);
157       break;
158     case PROP_POSITION:
159       g_value_set_int (value, private->position);
160       break;
161     case PROP_STYLE:
162       g_value_set_enum (value, private->style);
163       break;
164 
165     default:
166       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
167       break;
168     }
169 }
170 
171 static void
gimp_canvas_guide_transform(GimpCanvasItem * item,gdouble * x1,gdouble * y1,gdouble * x2,gdouble * y2)172 gimp_canvas_guide_transform (GimpCanvasItem *item,
173                              gdouble        *x1,
174                              gdouble        *y1,
175                              gdouble        *x2,
176                              gdouble        *y2)
177 {
178   GimpCanvasGuidePrivate *private = GET_PRIVATE (item);
179   GtkWidget              *canvas  = gimp_canvas_item_get_canvas (item);
180   GtkAllocation           allocation;
181   gint                    max_outside;
182   gint                    x, y;
183 
184   gtk_widget_get_allocation (canvas, &allocation);
185 
186   max_outside = allocation.width + allocation.height;
187 
188   *x1 = -max_outside;
189   *y1 = -max_outside;
190   *x2 = allocation.width  + max_outside;
191   *y2 = allocation.height + max_outside;
192 
193   switch (private->orientation)
194     {
195     case GIMP_ORIENTATION_HORIZONTAL:
196       gimp_canvas_item_transform_xy (item, 0, private->position, &x, &y);
197       *y1 = *y2 = y + 0.5;
198       break;
199 
200     case GIMP_ORIENTATION_VERTICAL:
201       gimp_canvas_item_transform_xy (item, private->position, 0, &x, &y);
202       *x1 = *x2 = x + 0.5;
203       break;
204 
205     case GIMP_ORIENTATION_UNKNOWN:
206       return;
207     }
208 }
209 
210 static void
gimp_canvas_guide_draw(GimpCanvasItem * item,cairo_t * cr)211 gimp_canvas_guide_draw (GimpCanvasItem *item,
212                         cairo_t        *cr)
213 {
214   gdouble x1, y1;
215   gdouble x2, y2;
216 
217   gimp_canvas_guide_transform (item, &x1, &y1, &x2, &y2);
218 
219   cairo_move_to (cr, x1, y1);
220   cairo_line_to (cr, x2, y2);
221 
222   _gimp_canvas_item_stroke (item, cr);
223 }
224 
225 static cairo_region_t *
gimp_canvas_guide_get_extents(GimpCanvasItem * item)226 gimp_canvas_guide_get_extents (GimpCanvasItem *item)
227 {
228   cairo_rectangle_int_t rectangle;
229   gdouble               x1, y1;
230   gdouble               x2, y2;
231 
232   gimp_canvas_guide_transform (item, &x1, &y1, &x2, &y2);
233 
234   rectangle.x      = MIN (x1, x2) - 1.5;
235   rectangle.y      = MIN (y1, y2) - 1.5;
236   rectangle.width  = ABS (x2 - x1) + 3.0;
237   rectangle.height = ABS (y2 - y1) + 3.0;
238 
239   return cairo_region_create_rectangle (&rectangle);
240 }
241 
242 static void
gimp_canvas_guide_stroke(GimpCanvasItem * item,cairo_t * cr)243 gimp_canvas_guide_stroke (GimpCanvasItem *item,
244                           cairo_t        *cr)
245 {
246   GimpCanvasGuidePrivate *private = GET_PRIVATE (item);
247 
248   if (private->style != GIMP_GUIDE_STYLE_NONE)
249     {
250       GimpDisplayShell *shell = gimp_canvas_item_get_shell (item);
251 
252       gimp_canvas_set_guide_style (gimp_canvas_item_get_canvas (item), cr,
253                                    private->style,
254                                    gimp_canvas_item_get_highlight (item),
255                                    shell->offset_x, shell->offset_y);
256       cairo_stroke (cr);
257     }
258   else
259     {
260       GIMP_CANVAS_ITEM_CLASS (parent_class)->stroke (item, cr);
261     }
262 }
263 
264 GimpCanvasItem *
gimp_canvas_guide_new(GimpDisplayShell * shell,GimpOrientationType orientation,gint position,GimpGuideStyle style)265 gimp_canvas_guide_new (GimpDisplayShell    *shell,
266                        GimpOrientationType  orientation,
267                        gint                 position,
268                        GimpGuideStyle       style)
269 {
270   g_return_val_if_fail (GIMP_IS_DISPLAY_SHELL (shell), NULL);
271 
272   return g_object_new (GIMP_TYPE_CANVAS_GUIDE,
273                        "shell",        shell,
274                        "orientation",  orientation,
275                        "position",     position,
276                        "style",        style,
277                        NULL);
278 }
279 
280 void
gimp_canvas_guide_set(GimpCanvasItem * guide,GimpOrientationType orientation,gint position)281 gimp_canvas_guide_set (GimpCanvasItem      *guide,
282                        GimpOrientationType  orientation,
283                        gint                 position)
284 {
285   g_return_if_fail (GIMP_IS_CANVAS_GUIDE (guide));
286 
287   gimp_canvas_item_begin_change (guide);
288 
289   g_object_set (guide,
290                 "orientation", orientation,
291                 "position",    position,
292                 NULL);
293 
294   gimp_canvas_item_end_change (guide);
295 }
296