r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / guicast / bcbutton.C
blobdbf63a53040ca8f5c1264e9e6b45a6505ad3705c
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;
30 BC_Button::BC_Button(int x, 
31         int y, 
32         int w, 
33         VFrame **data)
34  : BC_SubWindow(x, y, 0, 0, -1)
36         this->data = data;
37         this->w_argument = w;
38         for(int i = 0; i < 3; i++) images[i] = 0;
39         if(!data) printf("BC_Button::BC_Button data == 0\n");
40         status = BUTTON_UP;
41         underline_number = -1;
45 BC_Button::~BC_Button()
47         for(int i = 0; i < 3; i++) if(images[i]) delete images[i];
52 int BC_Button::initialize()
54 // Get the image
55         set_images(data);
57 // Create the subwindow
58         BC_SubWindow::initialize();
60 // Display the bitmap
61         draw_face();
62         return 0;
65 int BC_Button::reposition_window(int x, int y)
67         BC_WindowBase::reposition_window(x, y);
68         draw_face();
69         return 0;
73 int BC_Button::update_bitmaps(VFrame **data)
75         this->data = data;
76         set_images(data);
77         draw_top_background(parent_window, 0, 0, w, h);
78         draw_face();
79         return 0;
82 void BC_Button::set_underline(int number)
84         this->underline_number = number;
87 int BC_Button::set_images(VFrame **data)
89         for(int i = 0; i < 3; i++)
90         {
91                 if(images[i]) delete images[i];
92                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
93         }
95         if(w_argument > 0)
96                 w = w_argument;
97         else
98                 w = images[BUTTON_UP]->get_w();
100         h = images[BUTTON_UP]->get_h();
101         return 0;
104 int BC_Button::draw_face()
106         draw_top_background(parent_window, 0, 0, w, h);
107         images[status]->write_drawable(pixmap, 
108                         0, 
109                         0,
110                         w,
111                         h,
112                         0,
113                         0);
114         flash();
115         return 0;
118 int BC_Button::repeat_event(int64_t duration)
120         if(duration == top_level->get_resources()->tooltip_delay &&
121                 tooltip_text[0] != 0 &&
122                 status == BUTTON_UPHI &&
123                 !tooltip_done)
124         {
125                 show_tooltip();
126                 tooltip_done = 1;
127                 return 1;
128         }
129         return 0;
132 int BC_Button::cursor_enter_event()
134         if(top_level->event_win == win)
135         {
136                 tooltip_done = 0;
137                 if(top_level->button_down)
138                 {
139                         status = BUTTON_DOWNHI;
140                 }
141                 else
142                 if(status == BUTTON_UP) status = BUTTON_UPHI;
143                 draw_face();
144         }
145         return 0;
148 int BC_Button::cursor_leave_event()
150         if(status == BUTTON_UPHI)
151         {
152                 status = BUTTON_UP;
153                 draw_face();
154                 hide_tooltip();
155         }
156         return 0;
159 int BC_Button::button_press_event()
161         if(top_level->event_win == win)
162         {
163                 top_level->hide_tooltip();
164                 if(status == BUTTON_UPHI || status == BUTTON_UP) status = BUTTON_DOWNHI;
165                 draw_face();
166                 return 1;
167         }
168         return 0;
171 int BC_Button::button_release_event()
173 //printf("BC_Button::button_release_event 1\n");
174         if(top_level->event_win == win)
175         {
176                 hide_tooltip();
177 //printf("BC_Button::button_release_event 2\n");
178                 if(status == BUTTON_DOWNHI) 
179                 {
180 //printf("BC_Button::button_release_event 3\n");
181                         status = BUTTON_UPHI;
182                         draw_face();
184                         if(cursor_inside())
185                         {
186 //printf("BC_Button::button_release_event 4\n");
187                                 handle_event();
188                                 return 1;
189                         }
190                 }
191         }
192         return 0;
195 int BC_Button::cursor_motion_event()
197         if(top_level->button_down && top_level->event_win == win && 
198                 status == BUTTON_DOWNHI && !cursor_inside())
199         {
200                 status = BUTTON_UP;
201                 draw_face();
202         }
203         return 0;
207 BC_OKButton::BC_OKButton(int x, int y)
208  : BC_Button(x, y, 
209         BC_WindowBase::get_resources()->ok_images)
213 BC_OKButton::BC_OKButton(BC_WindowBase *parent_window)
214  : BC_Button(10, 
215         parent_window->get_h() - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10, 
216         BC_WindowBase::get_resources()->ok_images)
220 int BC_OKButton::handle_event()
222         get_top_level()->set_done(0);
223         return 0;
226 int BC_OKButton::resize_event(int w, int h)
228         reposition_window(10,
229                 h - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10);
230         return 1;
233 int BC_OKButton::keypress_event()
235         if(get_keypress() == RETURN) return handle_event();
236         return 0;
239 BC_CancelButton::BC_CancelButton(int x, int y)
240  : BC_Button(x, y, 
241         BC_WindowBase::get_resources()->cancel_images)
245 BC_CancelButton::BC_CancelButton(BC_WindowBase *parent_window)
246  : BC_Button(parent_window->get_w() - BC_WindowBase::get_resources()->cancel_images[0]->get_w() - 10, 
247         parent_window->get_h() - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10, 
248         BC_WindowBase::get_resources()->cancel_images)
252 int BC_CancelButton::handle_event()
254         get_top_level()->set_done(1);
255         return 1;
258 int BC_CancelButton::resize_event(int w,int h)
260         reposition_window(w - BC_WindowBase::get_resources()->cancel_images[0]->get_w() - 10, 
261                 h - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10);
262         return 1;
265 int BC_CancelButton::keypress_event()
267         if(get_keypress() == ESC) return handle_event();
268         return 0;
274 #define LEFT_DN  0
275 #define LEFT_HI  1
276 #define LEFT_UP  2
277 #define MID_DN   3
278 #define MID_HI   4
279 #define MID_UP   5
280 #define RIGHT_DN 6
281 #define RIGHT_HI 7
282 #define RIGHT_UP 8
284 BC_GenericButton::BC_GenericButton(int x, int y, char *text, VFrame **data)
285  : BC_Button(x, 
286         y, 
287         data ? data : BC_WindowBase::get_resources()->generic_button_images)
289         strcpy(this->text, text);
292 BC_GenericButton::BC_GenericButton(int x, int y, int w, char *text, VFrame **data)
293  : BC_Button(x, 
294         y, 
295         w,
296         data ? data : BC_WindowBase::get_resources()->generic_button_images)
298         strcpy(this->text, text);
301 int BC_GenericButton::set_images(VFrame **data)
303         for(int i = 0; i < 3; i++)
304         {
305                 if(images[i]) delete images[i];
306                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
307         }
309         if(w_argument)
310                 w = w_argument;
311         else
312                 w = get_text_width(MEDIUMFONT, text) + images[BUTTON_UP]->get_w() / 3;
315         h = images[BUTTON_UP]->get_h();
316         return 0;
319 int BC_GenericButton::draw_face()
321         int x, y, w;
322         draw_top_background(parent_window, 0, 0, get_w(), get_h());
323         draw_3segmenth(0, 0, get_w(), images[status]);
325         set_color(BLACK);
326         set_font(MEDIUMFONT);
327         y = (int)((float)get_h() / 2 + get_text_ascent(MEDIUMFONT) / 2 - 2);
328         w = get_text_width(current_font, text, strlen(text));
329         x = get_w() / 2 - w / 2;
330         draw_text(x, 
331                 y, 
332                 text);
334         if(underline_number >= 0)
335         {
336                 y++;
337                 int x1 = get_text_width(current_font, text, underline_number) + x;
338                 int x2 = get_text_width(current_font, text, underline_number + 1) + x;
339                 draw_line(x1, y, x2, y);
340                 draw_line(x1, y + 1, (x2 + x1) / 2, y + 1);
341         }
343         flash();
344         return 0;