1 /* $Id: polyselect.c,v 1.2 2004/03/13 03:31:45 meffie Exp $
2  *
3  * GNU Paint
4  * Copyright 2000-2003, 2007  Li-Cheng (Andy) Tai
5  *
6  * Authors: Li-Cheng (Andy) Tai <atai@gnu.org>
7  *          Michael A. Meffie III <meffiem@neo.rr.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 3
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be
15  * useful, but WITHOUT ANY WARRANTY; without even the implied
16  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
17  * PURPOSE. See the GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26 
27 #include "polyselect.h"
28 #include "selection.h"
29 #include "debug.h"
30 
31 typedef struct _gpaint_polygon_select
32 {
33     gpaint_tool       tool;
34     gpaint_point      anchor;
35     gpaint_point      drag;
36     gpaint_selection *selection;
37 } gpaint_polygon_select;
38 
39 #define GPAINT_POLYGON_SELECT(tool) ((gpaint_polygon_select*)(tool))
40 
41 static void polygon_select_destroy(gpaint_tool * tool);
42 static void polygon_select_select(gpaint_tool * tool);
43 static void polygon_select_deselect(gpaint_tool * tool);
44 static void polygon_select_button_press(gpaint_tool * tool, int x, int y);
45 static void polygon_select_motion(gpaint_tool * tool, int x, int y);
46 static void polygon_select_button_release(gpaint_tool * tool, int x, int y);
47 
48 gpaint_tool *
polygon_select_create(const char * name)49 polygon_select_create(const char *name)
50 {
51     gpaint_polygon_select *polygon_select = g_new0(gpaint_polygon_select, 1);
52     gpaint_tool *tool = GPAINT_TOOL(polygon_select);
53 
54     tool->name = name;
55     tool->cursor = gdk_cursor_new(GDK_CROSSHAIR);
56     tool->destroy        = polygon_select_destroy;
57     tool->select         = polygon_select_select;
58     tool->deselect       = polygon_select_deselect;
59     tool->button_press   = polygon_select_button_press;
60     tool->button_release = polygon_select_button_release;
61     tool->motion         = polygon_select_motion;
62     return tool;
63 }
64 
65 static void
polygon_select_destroy(gpaint_tool * tool)66 polygon_select_destroy(gpaint_tool *tool)
67 {
68     debug_fn();
69     gdk_cursor_destroy(tool->cursor);
70     g_free(tool);
71 }
72 
73 static void
polygon_select_select(gpaint_tool * tool)74 polygon_select_select(gpaint_tool *tool)
75 {
76     gpaint_polygon_select *polygon_select = GPAINT_POLYGON_SELECT(tool);
77     polygon_select->selection = tool->canvas->selection;
78     selection_enable_flash(polygon_select->selection);
79 }
80 
81 static void
polygon_select_deselect(gpaint_tool * tool)82 polygon_select_deselect(gpaint_tool *tool)
83 {
84     gpaint_polygon_select *polygon_select = GPAINT_POLYGON_SELECT(tool);
85     selection_remove_points(polygon_select->selection);
86 }
87 
88 static void
polygon_select_focus_change(gpaint_tool * tool)89 polygon_select_focus_change(gpaint_tool *tool)
90 {
91     gpaint_polygon_select *polygon_select = GPAINT_POLYGON_SELECT(tool);
92     debug_fn();
93     if (tool->canvas->has_focus)
94     {
95         selection_enable_flash(polygon_select->selection);
96     }
97     else
98     {
99         selection_disable_flash(polygon_select->selection);
100     }
101 }
102 
103 static void
polygon_select_button_press(gpaint_tool * tool,int x,int y)104 polygon_select_button_press(gpaint_tool * tool, int x, int y)
105 {
106     gpaint_polygon_select *ps = GPAINT_POLYGON_SELECT(tool);
107     debug_fn();
108 
109     if (drawing_in_bounds(tool->drawing, x, y))
110     {
111         if (selection_num_points(ps->selection))
112         {
113             selection_change_last(ps->selection, x, y);
114         }
115         else
116         {
117             selection_add_point(ps->selection, x, y);
118         }
119         set_point(&ps->anchor, x, y);
120     }
121 }
122 
123 static void
polygon_select_motion(gpaint_tool * tool,int x,int y)124 polygon_select_motion(gpaint_tool * tool, int x, int y)
125 {
126     gpaint_polygon_select *ps = GPAINT_POLYGON_SELECT(tool);
127     debug_fn();
128 
129     if (drawing_in_bounds(tool->drawing, x, y))
130     {
131         if (is_point_defined(&ps->anchor))
132         {
133             if (is_point_defined(&ps->drag))
134             {
135                 selection_change_last(ps->selection, x, y);
136                 selection_draw_flash(ps->selection);
137             }
138             set_point(&ps->drag, x, y);
139         }
140     }
141 }
142 
143 static void
polygon_select_button_release(gpaint_tool * tool,int x,int y)144 polygon_select_button_release(gpaint_tool * tool, int x, int y)
145 {
146     gpaint_polygon_select *ps = GPAINT_POLYGON_SELECT(tool);
147     debug_fn();
148 
149     if (drawing_in_bounds(tool->drawing, x, y))
150     {
151         if (is_point_defined(&ps->drag))
152         {
153             selection_change_last(ps->selection, x, y);
154         }
155         else
156         {
157             selection_add_point(ps->selection, x, y);
158         }
159         selection_close_loop(ps->selection);
160         selection_draw_flash(ps->selection);
161     }
162 }
163