r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / guicast / bctoggle.C
blobd4e8c88f898849d323b5ec61d7c899fb7930714c
1 #include "bcpixmap.h"
2 #include "bcresources.h"
3 #include "bctoggle.h"
4 #include "colors.h"
5 #include "fonts.h"
8 BC_Toggle::BC_Toggle(int x, int y, 
9                 VFrame **data, 
10                 int value, 
11                 char *caption,
12                 int bottom_justify, 
13                 int font,
14                 int color)
15  : BC_SubWindow(x, y, 0, 0, -1)
17         this->data = data;
18         images[0] = images[1] = images[2] = images[3] = images[4] = 0;
19         status = value ? TOGGLE_CHECKED : TOGGLE_UP;
20         this->value = value;
21         this->caption = caption;
22         this->bottom_justify = bottom_justify;
23         this->font = font;
24         this->color = color;
25         select_drag = 0;
29 BC_Toggle::~BC_Toggle()
31         delete images[0];
32         delete images[1];
33         delete images[2];
34         delete images[3];
35         delete images[4];
39 int BC_Toggle::initialize()
41 // Get the image
42         set_images(data);
43         w = images[0]->get_w();
44         h = images[0]->get_h();
45         toggle_x = 0;
46         toggle_y = 0;
47         text_y = 0;
48         text_x = w + 5;
50 // Expand subwindow for text
51         if(has_caption())
52         {
53                 text_w = get_text_width(MEDIUMFONT, caption);
54                 text_h = get_text_height(MEDIUMFONT);
55                 if(text_h > h)
56                 {
57                         toggle_y = (text_h - h) >> 1;
58                         h = text_h;
59                 }
60                 else
61                         text_y = (h - text_h) >> 1;
63                 if(bottom_justify)
64                 {
65                         text_y = h - text_h;
66                         text_line = h - get_text_descent(MEDIUMFONT);
67                 }
68                 else
69                         text_line = text_y + get_text_ascent(MEDIUMFONT);
70                 
71                 w = text_x + text_w;
72         }
74 // Create the subwindow
75         BC_SubWindow::initialize();
77 // Display the bitmap
78         draw_face();
79         return 0;
82 int BC_Toggle::set_images(VFrame **data)
84         if(images)
85         {
86                 delete images[0];
87                 delete images[1];
88                 delete images[2];
89                 delete images[3];
90                 delete images[4];
91         }
93         images[0] = new BC_Pixmap(top_level, data[0], PIXMAP_ALPHA);
94         images[1] = new BC_Pixmap(top_level, data[1], PIXMAP_ALPHA);
95         images[2] = new BC_Pixmap(top_level, data[2], PIXMAP_ALPHA);
96         images[3] = new BC_Pixmap(top_level, data[3], PIXMAP_ALPHA);
97         images[4] = new BC_Pixmap(top_level, data[4], PIXMAP_ALPHA);
98         return 0;
101 void BC_Toggle::set_select_drag(int value)
103         this->select_drag = value;
106 int BC_Toggle::draw_face()
108         draw_top_background(parent_window, 0, 0, get_w(), get_h());
109         if(has_caption())
110         {
111                 if(status == TOGGLE_UPHI || status == TOGGLE_DOWN || status == TOGGLE_CHECKEDHI)
112                 {
113                         set_color(LTGREY);
114                         draw_box(text_x, text_line - get_text_ascent(MEDIUMFONT), get_w() - text_x, get_text_height(MEDIUMFONT));
115                 }
117                 set_opaque();
118                 set_color(get_resources()->text_default);
119                 set_font(font);
120                 set_color(color);
121                 draw_text(text_x, text_line, caption);
122         }
124         draw_pixmap(images[status]);
125         flash();
126         return 0;
130 int BC_Toggle::repeat_event(int64_t duration)
132         if(duration == top_level->get_resources()->tooltip_delay &&
133                 tooltip_text[0] != 0 &&
134                 (status == TOGGLE_UPHI || status == TOGGLE_CHECKEDHI) &&
135                 !tooltip_done)
136         {
137                 show_tooltip();
138                 tooltip_done = 1;
139                 return 1;
140         }
141         return 0;
144 int BC_Toggle::cursor_enter_event()
146         if(top_level->event_win == win)
147         {
148                 tooltip_done = 0;
149                 if(top_level->button_down)
150                         status = TOGGLE_DOWN;
151                 else
152                         status = value ? TOGGLE_CHECKEDHI : TOGGLE_UPHI;
153                 draw_face();
154         }
155         return 0;
158 int BC_Toggle::cursor_leave_event()
160         hide_tooltip();
161         if(!value)
162         {
163                 status = TOGGLE_UP;
164                 draw_face();
165         }
166         else
167         {
168                 status = TOGGLE_CHECKED;
169                 draw_face();
170         }
171         return 0;
174 int BC_Toggle::button_press_event()
176         hide_tooltip();
177         if(top_level->event_win == win && get_buttonpress() == 1)
178         {
179                 status = TOGGLE_DOWN;
180                 top_level->toggle_value = !this->value;
181                 draw_face();
182                 return 1;
183         }
184         return 0;
187 int BC_Toggle::button_release_event()
189         hide_tooltip();
190         if(top_level->event_win == win && status == TOGGLE_DOWN)
191         {
192                 if(!value)
193                 {
194                         status = TOGGLE_CHECKEDHI;
195                         value = 1;
196                 }
197                 else
198                 {
199                         status = TOGGLE_UPHI;
200                         value = 0;
201                 }
203                 draw_face();
204                 return handle_event();
205         }
206         return 0;
209 int BC_Toggle::cursor_motion_event()
211         if(select_drag) return 0;
212         if(top_level->button_down && top_level->event_win == win && !cursor_inside())
213         {
214                 if(status == TOGGLE_DOWN)
215                 {
216                         status = TOGGLE_UP;
217                         draw_face();
218                 }
219                 else
220                 if(status == TOGGLE_UPHI)
221                 {
222                         status = TOGGLE_CHECKEDHI;
223                         draw_face();
224                 }
225         }
226         return 0;
229 int BC_Toggle::get_value()
231         return value;
234 int BC_Toggle::set_value(int value, int draw)
236         if(value != this->value)
237         {
238                 this->value = value;
239                 if(value) 
240                 {
241                         switch(status)
242                         {
243                                 case TOGGLE_UP:
244                                         status = TOGGLE_CHECKED;
245                                         break;
246                                 case TOGGLE_UPHI:
247                                         status = TOGGLE_CHECKEDHI;
248                                         break;
249                         }
250                 }
251                 else
252                 switch(status)
253                 {
254                         case TOGGLE_CHECKED:
255                                 status = TOGGLE_UP;
256                                 break;
257                         case TOGGLE_CHECKEDHI:
258                                 status = TOGGLE_UPHI;
259                                 break;
260                 }
261                 if(draw) draw_face();
262         }
263         return 0;
266 int BC_Toggle::update(int value, int draw)
268         return set_value(value, draw);
271 void BC_Toggle::reposition_window(int x, int y)
273         BC_WindowBase::reposition_window(x, y);
274         draw_face();
278 int BC_Toggle::has_caption()
280         return (caption != 0 && caption[0] != 0);
283 BC_Radial::BC_Radial(int x, 
284         int y, 
285         int value, 
286         char *caption, 
287         int font,
288         int color)
289  : BC_Toggle(x, 
290         y, 
291         BC_WindowBase::get_resources()->radial_images, 
292         value, 
293         caption, 
294         0, 
295         font,
296         color)
300 BC_CheckBox::BC_CheckBox(int x, 
301         int y, 
302         int value, 
303         char *caption, 
304         int font,
305         int color)
306  : BC_Toggle(x, 
307         y, 
308         BC_WindowBase::get_resources()->checkbox_images, 
309         value, 
310         caption, 
311         1, 
312         font,
313         color)
315         this->value = 0;
318 BC_CheckBox::BC_CheckBox(int x, 
319         int y, 
320         int *value, 
321         char *caption, 
322         int font,
323         int color)
324  : BC_Toggle(x, 
325         y, 
326         BC_WindowBase::get_resources()->checkbox_images, 
327         *value, 
328         caption, 
329         1, 
330         font,
331         color)
333         this->value = value;
336 int BC_CheckBox::handle_event()
338         *value = get_value();
339         return 1;
345 BC_Label::BC_Label(int x, 
346         int y, 
347         int value, 
348         int font,
349         int color)
350  : BC_Toggle(x, 
351         y, 
352         BC_WindowBase::get_resources()->label_images, 
353         value, 
354         "", 
355         0, 
356         font,
357         color)