~ubuntu-branches/ubuntu/lucid/agg/lucid-proposed

« back to all changes in this revision

Viewing changes to src/ctrl/agg_rbox_ctrl.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Rene Engelhard
  • Date: 2005-11-29 02:02:41 UTC
  • Revision ID: james.westby@ubuntu.com-20051129020241-pmlxls0x6j2qv3nm
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

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