r370: Heroine Virutal's official release 1.2.1
[cinelerra_cv/mob.git] / hvirtual / guicast / bcbutton.C
bloba10a0e66202583942c04ca5a091db8bc95a9421e
1 #include "bcbutton.h"
2 #include "bcresources.h"
3 #include "bcpixmap.h"
4 #include "colors.h"
5 #include "fonts.h"
6 #include "keys.h"
7 #include "vframe.h"
10 #include <string.h>
11 #include <unistd.h>
13 #define BUTTON_UP 0
14 #define BUTTON_UPHI 1
15 #define BUTTON_DOWNHI 2
17 BC_Button::BC_Button(int x, 
18         int y, 
19         VFrame **data)
20  : BC_SubWindow(x, y, 0, 0, -1)
22         this->data = data;
23         for(int i = 0; i < 3; i++) images[i] = 0;
24         if(!data) printf("BC_Button::BC_Button data == 0\n");
25         status = BUTTON_UP;
26         this->w_argument = 0;
27         underline_number = -1;
28         enabled = 1;
31 BC_Button::BC_Button(int x, 
32         int y, 
33         int w, 
34         VFrame **data)
35  : BC_SubWindow(x, y, 0, 0, -1)
37         this->data = data;
38         this->w_argument = w;
39         for(int i = 0; i < 3; i++) images[i] = 0;
40         if(!data) printf("BC_Button::BC_Button data == 0\n");
41         status = BUTTON_UP;
42         underline_number = -1;
43         enabled = 1;
47 BC_Button::~BC_Button()
49         for(int i = 0; i < 3; i++) if(images[i]) delete images[i];
54 int BC_Button::initialize()
56 // Get the image
57         set_images(data);
59 // Create the subwindow
60         BC_SubWindow::initialize();
62 // Display the bitmap
63         draw_face();
64         return 0;
67 int BC_Button::reposition_window(int x, int y)
69         BC_WindowBase::reposition_window(x, y);
70         draw_face();
71         return 0;
75 int BC_Button::update_bitmaps(VFrame **data)
77         this->data = data;
78         set_images(data);
79         draw_top_background(parent_window, 0, 0, w, h);
80         draw_face();
81         return 0;
84 void BC_Button::enable()
86         enabled = 1;
87         draw_face();
90 void BC_Button::disable()
92         enabled = 0;
93         draw_face();
97 void BC_Button::set_underline(int number)
99         this->underline_number = number;
102 int BC_Button::set_images(VFrame **data)
104         for(int i = 0; i < 3; i++)
105         {
106                 if(images[i]) delete images[i];
107                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
108         }
110         if(w_argument > 0)
111                 w = w_argument;
112         else
113                 w = images[BUTTON_UP]->get_w();
115         h = images[BUTTON_UP]->get_h();
116         return 0;
119 int BC_Button::draw_face()
121         draw_top_background(parent_window, 0, 0, w, h);
122         images[status]->write_drawable(pixmap, 
123                         0, 
124                         0,
125                         w,
126                         h,
127                         0,
128                         0);
129         flash();
130         flush();
131         return 0;
134 int BC_Button::repeat_event(int64_t duration)
136         if(duration == top_level->get_resources()->tooltip_delay &&
137                 tooltip_text[0] != 0 &&
138                 status == BUTTON_UPHI &&
139                 !tooltip_done)
140         {
141                 show_tooltip();
142                 tooltip_done = 1;
143                 return 1;
144         }
145         return 0;
148 int BC_Button::cursor_enter_event()
150         if(top_level->event_win == win && enabled)
151         {
152                 tooltip_done = 0;
153                 if(top_level->button_down)
154                 {
155                         status = BUTTON_DOWNHI;
156                 }
157                 else
158                 if(status == BUTTON_UP) status = BUTTON_UPHI;
159                 draw_face();
160         }
161         return 0;
164 int BC_Button::cursor_leave_event()
166         if(status == BUTTON_UPHI)
167         {
168                 status = BUTTON_UP;
170                 draw_face();
172                 hide_tooltip();
174         }
175         return 0;
178 int BC_Button::button_press_event()
180         if(top_level->event_win == win && get_buttonpress() == 1 && enabled)
181         {
182                 hide_tooltip();
183                 if(status == BUTTON_UPHI || status == BUTTON_UP) status = BUTTON_DOWNHI;
184                 draw_face();
185                 return 1;
186         }
187         return 0;
190 int BC_Button::button_release_event()
192 //printf("BC_Button::button_release_event 1\n");
193         if(top_level->event_win == win)
194         {
195                 hide_tooltip();
196 //printf("BC_Button::button_release_event 2\n");
197                 if(status == BUTTON_DOWNHI) 
198                 {
199 //printf("BC_Button::button_release_event 3\n");
200                         status = BUTTON_UPHI;
201                         draw_face();
203                         if(cursor_inside())
204                         {
205 //printf("BC_Button::button_release_event 4\n");
206                                 handle_event();
207                                 return 1;
208                         }
209                 }
210         }
211         return 0;
214 int BC_Button::cursor_motion_event()
216         if(top_level->button_down && top_level->event_win == win && 
217                 status == BUTTON_DOWNHI && !cursor_inside())
218         {
219                 status = BUTTON_UP;
220                 draw_face();
221         }
222         return 0;
226 BC_OKButton::BC_OKButton(int x, int y)
227  : BC_Button(x, y, 
228         BC_WindowBase::get_resources()->ok_images)
232 BC_OKButton::BC_OKButton(BC_WindowBase *parent_window)
233  : BC_Button(10, 
234         parent_window->get_h() - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10, 
235         BC_WindowBase::get_resources()->ok_images)
239 int BC_OKButton::handle_event()
241         get_top_level()->set_done(0);
242         return 0;
245 int BC_OKButton::resize_event(int w, int h)
247         reposition_window(10,
248                 h - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10);
249         return 1;
252 int BC_OKButton::keypress_event()
254         if(get_keypress() == RETURN) return handle_event();
255         return 0;
258 BC_CancelButton::BC_CancelButton(int x, int y)
259  : BC_Button(x, y, 
260         BC_WindowBase::get_resources()->cancel_images)
264 BC_CancelButton::BC_CancelButton(BC_WindowBase *parent_window)
265  : BC_Button(parent_window->get_w() - BC_WindowBase::get_resources()->cancel_images[0]->get_w() - 10, 
266         parent_window->get_h() - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10, 
267         BC_WindowBase::get_resources()->cancel_images)
271 int BC_CancelButton::handle_event()
273         get_top_level()->set_done(1);
274         return 1;
277 int BC_CancelButton::resize_event(int w,int h)
279         reposition_window(w - BC_WindowBase::get_resources()->cancel_images[0]->get_w() - 10, 
280                 h - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10);
281         return 1;
284 int BC_CancelButton::keypress_event()
286         if(get_keypress() == ESC) return handle_event();
287         return 0;
293 #define LEFT_DN  0
294 #define LEFT_HI  1
295 #define LEFT_UP  2
296 #define MID_DN   3
297 #define MID_HI   4
298 #define MID_UP   5
299 #define RIGHT_DN 6
300 #define RIGHT_HI 7
301 #define RIGHT_UP 8
303 BC_GenericButton::BC_GenericButton(int x, int y, char *text, VFrame **data)
304  : BC_Button(x, 
305         y, 
306         data ? data : BC_WindowBase::get_resources()->generic_button_images)
308         strcpy(this->text, text);
311 BC_GenericButton::BC_GenericButton(int x, int y, int w, char *text, VFrame **data)
312  : BC_Button(x, 
313         y, 
314         w,
315         data ? data : BC_WindowBase::get_resources()->generic_button_images)
317         strcpy(this->text, text);
320 int BC_GenericButton::set_images(VFrame **data)
322         for(int i = 0; i < 3; i++)
323         {
324                 if(images[i]) delete images[i];
325                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
326         }
328         if(w_argument)
329                 w = w_argument;
330         else
331                 w = get_text_width(MEDIUMFONT, text) + images[BUTTON_UP]->get_w() / 3;
334         h = images[BUTTON_UP]->get_h();
335         return 0;
338 int BC_GenericButton::draw_face()
340         int x, y, w;
341         draw_top_background(parent_window, 0, 0, get_w(), get_h());
342         draw_3segmenth(0, 0, get_w(), images[status]);
344         if(enabled)
345                 set_color(get_resources()->default_text_color);
346         else
347                 set_color(get_resources()->disabled_text_color);
348         set_font(MEDIUMFONT);
349         y = (int)((float)get_h() / 2 + get_text_ascent(MEDIUMFONT) / 2 - 2);
350         w = get_text_width(current_font, text, strlen(text));
351         x = get_w() / 2 - w / 2;
352         draw_text(x, 
353                 y, 
354                 text);
356         if(underline_number >= 0)
357         {
358                 y++;
359                 int x1 = get_text_width(current_font, text, underline_number) + x;
360                 int x2 = get_text_width(current_font, text, underline_number + 1) + x;
361                 draw_line(x1, y, x2, y);
362                 draw_line(x1, y + 1, (x2 + x1) / 2, y + 1);
363         }
365         flash();
366         flush();
367         return 0;