1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
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 <gdk-pixbuf/gdk-pixbuf.h>
21 #include <gegl.h>
22 
23 #include "core-types.h"
24 
25 #include "gimp.h"
26 #include "gimpimage.h"
27 #include "gimpguide.h"
28 #include "gimpimage-guides.h"
29 #include "gimpimage-private.h"
30 #include "gimpimage-undo-push.h"
31 
32 #include "gimp-intl.h"
33 
34 
35 /*  public functions  */
36 
37 GimpGuide *
gimp_image_add_hguide(GimpImage * image,gint position,gboolean push_undo)38 gimp_image_add_hguide (GimpImage *image,
39                        gint       position,
40                        gboolean   push_undo)
41 {
42   GimpGuide *guide;
43 
44   g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
45   g_return_val_if_fail (position >= 0 &&
46                         position <= gimp_image_get_height (image), NULL);
47 
48   guide = gimp_guide_new (GIMP_ORIENTATION_HORIZONTAL,
49                           image->gimp->next_guide_ID++);
50 
51   if (push_undo)
52     gimp_image_undo_push_guide (image,
53                                 C_("undo-type", "Add Horizontal Guide"), guide);
54 
55   gimp_image_add_guide (image, guide, position);
56   g_object_unref (G_OBJECT (guide));
57 
58   return guide;
59 }
60 
61 GimpGuide *
gimp_image_add_vguide(GimpImage * image,gint position,gboolean push_undo)62 gimp_image_add_vguide (GimpImage *image,
63                        gint       position,
64                        gboolean   push_undo)
65 {
66   GimpGuide *guide;
67 
68   g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
69   g_return_val_if_fail (position >= 0 &&
70                         position <= gimp_image_get_width (image), NULL);
71 
72   guide = gimp_guide_new (GIMP_ORIENTATION_VERTICAL,
73                           image->gimp->next_guide_ID++);
74 
75   if (push_undo)
76     gimp_image_undo_push_guide (image,
77                                 C_("undo-type", "Add Vertical Guide"), guide);
78 
79   gimp_image_add_guide (image, guide, position);
80   g_object_unref (guide);
81 
82   return guide;
83 }
84 
85 void
gimp_image_add_guide(GimpImage * image,GimpGuide * guide,gint position)86 gimp_image_add_guide (GimpImage *image,
87                       GimpGuide *guide,
88                       gint       position)
89 {
90   GimpImagePrivate *private;
91 
92   g_return_if_fail (GIMP_IS_IMAGE (image));
93   g_return_if_fail (GIMP_IS_GUIDE (guide));
94 
95   private = GIMP_IMAGE_GET_PRIVATE (image);
96 
97   private->guides = g_list_prepend (private->guides, guide);
98 
99   gimp_guide_set_position (guide, position);
100   g_object_ref (guide);
101 
102   gimp_image_guide_added (image, guide);
103 }
104 
105 void
gimp_image_remove_guide(GimpImage * image,GimpGuide * guide,gboolean push_undo)106 gimp_image_remove_guide (GimpImage *image,
107                          GimpGuide *guide,
108                          gboolean   push_undo)
109 {
110   GimpImagePrivate *private;
111 
112   g_return_if_fail (GIMP_IS_IMAGE (image));
113   g_return_if_fail (GIMP_IS_GUIDE (guide));
114 
115   private = GIMP_IMAGE_GET_PRIVATE (image);
116 
117   if (gimp_guide_is_custom (guide))
118     push_undo = FALSE;
119 
120   if (push_undo)
121     gimp_image_undo_push_guide (image, C_("undo-type", "Remove Guide"), guide);
122 
123   private->guides = g_list_remove (private->guides, guide);
124   gimp_aux_item_removed (GIMP_AUX_ITEM (guide));
125 
126   gimp_image_guide_removed (image, guide);
127 
128   gimp_guide_set_position (guide, GIMP_GUIDE_POSITION_UNDEFINED);
129   g_object_unref (guide);
130 }
131 
132 void
gimp_image_move_guide(GimpImage * image,GimpGuide * guide,gint position,gboolean push_undo)133 gimp_image_move_guide (GimpImage *image,
134                        GimpGuide *guide,
135                        gint       position,
136                        gboolean   push_undo)
137 {
138   g_return_if_fail (GIMP_IS_IMAGE (image));
139   g_return_if_fail (GIMP_IS_GUIDE (guide));
140   g_return_if_fail (position >= 0);
141 
142   if (gimp_guide_get_orientation (guide) == GIMP_ORIENTATION_HORIZONTAL)
143     g_return_if_fail (position <= gimp_image_get_height (image));
144   else
145     g_return_if_fail (position <= gimp_image_get_width (image));
146 
147   if (gimp_guide_is_custom (guide))
148     push_undo = FALSE;
149 
150   if (push_undo)
151     gimp_image_undo_push_guide (image, C_("undo-type", "Move Guide"), guide);
152 
153   gimp_guide_set_position (guide, position);
154 
155   gimp_image_guide_moved (image, guide);
156 }
157 
158 GList *
gimp_image_get_guides(GimpImage * image)159 gimp_image_get_guides (GimpImage *image)
160 {
161   g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
162 
163   return GIMP_IMAGE_GET_PRIVATE (image)->guides;
164 }
165 
166 GimpGuide *
gimp_image_get_guide(GimpImage * image,guint32 id)167 gimp_image_get_guide (GimpImage *image,
168                       guint32    id)
169 {
170   GList *guides;
171 
172   g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
173 
174   for (guides = GIMP_IMAGE_GET_PRIVATE (image)->guides;
175        guides;
176        guides = g_list_next (guides))
177     {
178       GimpGuide *guide = guides->data;
179 
180       if (gimp_aux_item_get_ID (GIMP_AUX_ITEM (guide)) == id)
181         return guide;
182     }
183 
184   return NULL;
185 }
186 
187 GimpGuide *
gimp_image_get_next_guide(GimpImage * image,guint32 id,gboolean * guide_found)188 gimp_image_get_next_guide (GimpImage *image,
189                            guint32    id,
190                            gboolean  *guide_found)
191 {
192   GList *guides;
193 
194   g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
195   g_return_val_if_fail (guide_found != NULL, NULL);
196 
197   if (id == 0)
198     *guide_found = TRUE;
199   else
200     *guide_found = FALSE;
201 
202   for (guides = GIMP_IMAGE_GET_PRIVATE (image)->guides;
203        guides;
204        guides = g_list_next (guides))
205     {
206       GimpGuide *guide = guides->data;
207 
208       if (*guide_found) /* this is the first guide after the found one */
209         return guide;
210 
211       if (gimp_aux_item_get_ID (GIMP_AUX_ITEM (guide)) == id) /* found it, next one will be returned */
212         *guide_found = TRUE;
213     }
214 
215   return NULL;
216 }
217