Debian Codegraph @

extern/agg24-svn/src/ctrl/agg_rbox_ctrl.cpp

raw
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.4
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
4 //
5 // Permission to copy, use, modify, sell and distribute this software
6 // is granted provided this copyright notice appears in all copies.
7 // This software is provided "as is" without express or implied
8 // warranty, and with no claim as to its suitability for any purpose.
9 //
10 //----------------------------------------------------------------------------
11 // Contact: [email protected]
12 // [email protected]
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
15 //
16 // classes rbox_ctrl_impl, rbox_ctrl
17 //
18 //----------------------------------------------------------------------------
19
20 #include <string.h>
21 #include "ctrl/agg_rbox_ctrl.h"
22
23 namespace agg
24 {
25
26 //------------------------------------------------------------------------
27 rbox_ctrl_impl::rbox_ctrl_impl(double x1, double y1,
28 double x2, double y2, bool flip_y) :
29 ctrl(x1, y1, x2, y2, flip_y),
30 m_border_width(1.0),
31 m_border_extra(0.0),
32 m_text_thickness(1.5),
33 m_text_height(9.0),
34 m_text_width(0.0),
35 m_num_items(0),
36 m_cur_item(-1),
37 m_ellipse_poly(m_ellipse),
38 m_text_poly(m_text),
39 m_idx(0),
40 m_vertex(0)
41 {
42 calc_rbox();
43 }
44
45
46 //------------------------------------------------------------------------
47 void rbox_ctrl_impl::calc_rbox()
48 {
49 m_xs1 = m_x1 + m_border_width;
50 m_ys1 = m_y1 + m_border_width;
51 m_xs2 = m_x2 - m_border_width;
52 m_ys2 = m_y2 - m_border_width;
53 }
54
55
56 //------------------------------------------------------------------------
57 void rbox_ctrl_impl::add_item(const char* text)
58 {
59 if(m_num_items < 32)
60 {
61 m_items[m_num_items].resize(strlen(text) + 1);
62 strcpy(&m_items[m_num_items][0], text);
63 m_num_items++;
64 }
65 }
66
67
68 //------------------------------------------------------------------------
69 void rbox_ctrl_impl::border_width(double t, double extra)
70 {
71 m_border_width = t;
72 m_border_extra = extra;
73 calc_rbox();
74 }
75
76
77 //------------------------------------------------------------------------
78 void rbox_ctrl_impl::text_size(double h, double w)
79 {
80 m_text_width = w;
81 m_text_height = h;
82 }
83
84
85
86 //------------------------------------------------------------------------
87 void rbox_ctrl_impl::rewind(unsigned idx)
88 {
89 m_idx = idx;
90 m_dy = m_text_height * 2.0;
91 m_draw_item = 0;
92
93 switch(idx)
94 {
95 default:
96
97 case 0: // Background
98 m_vertex = 0;
99 m_vx[0] = m_x1 - m_border_extra;
100 m_vy[0] = m_y1 - m_border_extra;
101 m_vx[1] = m_x2 + m_border_extra;
102 m_vy[1] = m_y1 - m_border_extra;
103 m_vx[2] = m_x2 + m_border_extra;
104 m_vy[2] = m_y2 + m_border_extra;
105 m_vx[3] = m_x1 - m_border_extra;
106 m_vy[3] = m_y2 + m_border_extra;
107 break;
108
109 case 1: // Border
110 m_vertex = 0;
111 m_vx[0] = m_x1;
112 m_vy[0] = m_y1;
113 m_vx[1] = m_x2;
114 m_vy[1] = m_y1;
115 m_vx[2] = m_x2;
116 m_vy[2] = m_y2;
117 m_vx[3] = m_x1;
118 m_vy[3] = m_y2;
119 m_vx[4] = m_x1 + m_border_width;
120 m_vy[4] = m_y1 + m_border_width;
121 m_vx[5] = m_x1 + m_border_width;
122 m_vy[5] = m_y2 - m_border_width;
123 m_vx[6] = m_x2 - m_border_width;
124 m_vy[6] = m_y2 - m_border_width;
125 m_vx[7] = m_x2 - m_border_width;
126 m_vy[7] = m_y1 + m_border_width;
127 break;
128
129 case 2: // Text
130 m_text.text(&m_items[0][0]);
131 m_text.start_point(m_xs1 + m_dy * 1.5, m_ys1 + m_dy / 2.0);
132 m_text.size(m_text_height, m_text_width);
133 m_text_poly.width(m_text_thickness);
134 m_text_poly.line_join(round_join);
135 m_text_poly.line_cap(round_cap);
136 m_text_poly.rewind(0);
137 break;
138
139 case 3: // Inactive items
140 m_ellipse.init(m_xs1 + m_dy / 1.3,
141 m_ys1 + m_dy / 1.3,
142 m_text_height / 1.5,
143 m_text_height / 1.5, 32);
144 m_ellipse_poly.width(m_text_thickness);
145 m_ellipse_poly.rewind(0);
146 break;
147
148
149 case 4: // Active Item
150 if(m_cur_item >= 0)
151 {
152 m_ellipse.init(m_xs1 + m_dy / 1.3,
153 m_ys1 + m_dy * m_cur_item + m_dy / 1.3,
154 m_text_height / 2.0,
155 m_text_height / 2.0, 32);
156 m_ellipse.rewind(0);
157 }
158 break;
159
160 }
161 }
162
163
164 //------------------------------------------------------------------------
165 unsigned rbox_ctrl_impl::vertex(double* x, double* y)
166 {
167 unsigned cmd = path_cmd_line_to;
168 switch(m_idx)
169 {
170 case 0:
171 if(m_vertex == 0) cmd = path_cmd_move_to;
172 if(m_vertex >= 4) cmd = path_cmd_stop;
173 *x = m_vx[m_vertex];
174 *y = m_vy[m_vertex];
175 m_vertex++;
176 break;
177
178 case 1:
179 if(m_vertex == 0 || m_vertex == 4) cmd = path_cmd_move_to;
180 if(m_vertex >= 8) cmd = path_cmd_stop;
181 *x = m_vx[m_vertex];
182 *y = m_vy[m_vertex];
183 m_vertex++;
184 break;
185
186 case 2:
187 cmd = m_text_poly.vertex(x, y);
188 if(is_stop(cmd))
189 {
190 m_draw_item++;
191 if(m_draw_item >= m_num_items)
192 {
193 break;
194 }
195 else
196 {
197 m_text.text(&m_items[m_draw_item][0]);
198 m_text.start_point(m_xs1 + m_dy * 1.5,
199 m_ys1 + m_dy * (m_draw_item + 1) - m_dy / 2.0);
200
201 m_text_poly.rewind(0);
202 cmd = m_text_poly.vertex(x, y);
203 }
204 }
205 break;
206
207 case 3:
208 cmd = m_ellipse_poly.vertex(x, y);
209 if(is_stop(cmd))
210 {
211 m_draw_item++;
212 if(m_draw_item >= m_num_items)
213 {
214 break;
215 }
216 else
217 {
218 m_ellipse.init(m_xs1 + m_dy / 1.3,
219 m_ys1 + m_dy * m_draw_item + m_dy / 1.3,
220 m_text_height / 1.5,
221 m_text_height / 1.5, 32);
222 m_ellipse_poly.rewind(0);
223 cmd = m_ellipse_poly.vertex(x, y);
224 }
225 }
226 break;
227
228
229 case 4:
230 if(m_cur_item >= 0)
231 {
232 cmd = m_ellipse.vertex(x, y);
233 }
234 else
235 {
236 cmd = path_cmd_stop;
237 }
238 break;
239
240 default:
241 cmd = path_cmd_stop;
242 break;
243 }
244
245 if(!is_stop(cmd))
246 {
247 transform_xy(x, y);
248 }
249
250 return cmd;
251 }
252
253
254 //------------------------------------------------------------------------
255 bool rbox_ctrl_impl::in_rect(double x, double y) const
256 {
257 inverse_transform_xy(&x, &y);
258 return x >= m_x1 && x <= m_x2 && y >= m_y1 && y <= m_y2;
259 }
260
261
262
263 //------------------------------------------------------------------------
264 bool rbox_ctrl_impl::on_mouse_button_down(double x, double y)
265 {
266 inverse_transform_xy(&x, &y);
267 unsigned i;
268 for(i = 0; i < m_num_items; i++)
269 {
270 double xp = m_xs1 + m_dy / 1.3;
271 double yp = m_ys1 + m_dy * i + m_dy / 1.3;
272 if(calc_distance(x, y, xp, yp) <= m_text_height / 1.5)
273 {
274 m_cur_item = int(i);
275 return true;
276 }
277 }
278 return false;
279 }
280
281
282 //------------------------------------------------------------------------
283 bool rbox_ctrl_impl::on_mouse_move(double, double, bool)
284 {
285 return false;
286 }
287
288 //------------------------------------------------------------------------
289 bool rbox_ctrl_impl::on_mouse_button_up(double, double)
290 {
291 return false;
292 }
293
294 //------------------------------------------------------------------------
295 bool rbox_ctrl_impl::on_arrow_keys(bool left, bool right, bool down, bool up)
296 {
297 if(m_cur_item >= 0)
298 {
299 if(up || right)
300 {
301 m_cur_item++;
302 if(m_cur_item >= int(m_num_items))
303 {
304 m_cur_item = 0;
305 }
306 return true;
307 }
308
309 if(down || left)
310 {
311 m_cur_item--;
312 if(m_cur_item < 0)
313 {
314 m_cur_item = m_num_items - 1;
315 }
316 return true;
317 }
318 }
319 return false;
320 }
321
322
323 }
324
325