r1015: Add a shortcut 'e' that toggles between selection and drag&drop edit modes.
[cinelerra_cv/ct.git] / guicast / bcbutton.C
blobad447dc01148236508fc8818165011246a22deb4
1 #include "bcbutton.h"
2 #include "bcpixmap.h"
3 #include "bcresources.h"
4 #include "bcsignals.h"
5 #include "colors.h"
6 #include "fonts.h"
7 #include "keys.h"
8 #include "language.h"
9 #include "vframe.h"
12 #include <string.h>
13 #include <unistd.h>
15 #define BUTTON_UP 0
16 #define BUTTON_UPHI 1
17 #define BUTTON_DOWNHI 2
19 BC_Button::BC_Button(int x, 
20         int y, 
21         VFrame **data)
22  : BC_SubWindow(x, y, 0, 0, -1)
24         this->data = data;
25         for(int i = 0; i < 3; i++) images[i] = 0;
26         if(!data) printf("BC_Button::BC_Button data == 0\n");
27         status = BUTTON_UP;
28         this->w_argument = 0;
29         underline_number = -1;
30         enabled = 1;
33 BC_Button::BC_Button(int x, 
34         int y, 
35         int w, 
36         VFrame **data)
37  : BC_SubWindow(x, y, 0, 0, -1)
39         this->data = data;
40         this->w_argument = w;
41         for(int i = 0; i < 3; i++) images[i] = 0;
42         if(!data) printf("BC_Button::BC_Button data == 0\n");
43         status = BUTTON_UP;
44         underline_number = -1;
45         enabled = 1;
49 BC_Button::~BC_Button()
51         for(int i = 0; i < 3; i++) if(images[i]) delete images[i];
56 int BC_Button::initialize()
58 // Get the image
59         set_images(data);
61 // Create the subwindow
62         BC_SubWindow::initialize();
64 // Display the bitmap
65         draw_face();
66         return 0;
69 int BC_Button::reposition_window(int x, int y)
71         BC_WindowBase::reposition_window(x, y);
72         draw_face();
73         return 0;
77 int BC_Button::update_bitmaps(VFrame **data)
79         this->data = data;
80         set_images(data);
81         draw_top_background(parent_window, 0, 0, w, h);
82         draw_face();
83         return 0;
86 void BC_Button::enable()
88         enabled = 1;
89         draw_face();
92 void BC_Button::disable()
94         enabled = 0;
95         draw_face();
99 void BC_Button::set_underline(int number)
101         this->underline_number = number;
104 int BC_Button::set_images(VFrame **data)
106         for(int i = 0; i < 3; i++)
107         {
108                 if(images[i]) delete images[i];
109                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
110         }
112         if(w_argument > 0)
113                 w = w_argument;
114         else
115                 w = images[BUTTON_UP]->get_w();
117         h = images[BUTTON_UP]->get_h();
118         return 0;
121 int BC_Button::draw_face()
123         draw_top_background(parent_window, 0, 0, w, h);
124         pixmap->draw_pixmap(images[status], 
125                         0, 
126                         0,
127                         w,
128                         h,
129                         0,
130                         0);
131         flash();
132         return 0;
135 int BC_Button::repeat_event(int64_t duration)
137         if(duration == top_level->get_resources()->tooltip_delay &&
138                 tooltip_text[0] != 0 &&
139                 status == BUTTON_UPHI &&
140                 !tooltip_done)
141         {
142                 show_tooltip();
143                 tooltip_done = 1;
144                 return 1;
145         }
146         return 0;
149 int BC_Button::cursor_enter_event()
151         if(top_level->event_win == win && enabled)
152         {
153                 tooltip_done = 0;
154                 if(top_level->button_down)
155                 {
156                         status = BUTTON_DOWNHI;
157                 }
158                 else
159                 if(status == BUTTON_UP) status = BUTTON_UPHI;
160                 draw_face();
161         }
162         return 0;
165 int BC_Button::cursor_leave_event()
167         if(status == BUTTON_UPHI)
168         {
169                 status = BUTTON_UP;
171                 draw_face();
173                 hide_tooltip();
175         }
176         return 0;
179 int BC_Button::button_press_event()
181         if(top_level->event_win == win && get_buttonpress() == 1 && enabled)
182         {
183                 hide_tooltip();
184                 if(status == BUTTON_UPHI || status == BUTTON_UP) status = BUTTON_DOWNHI;
185                 draw_face();
186                 return 1;
187         }
188         return 0;
191 int BC_Button::button_release_event()
193         if(top_level->event_win == win)
194         {
195                 hide_tooltip();
196                 if(status == BUTTON_DOWNHI) 
197                 {
198                         status = BUTTON_UPHI;
199                         draw_face();
201                         if(cursor_inside())
202                         {
203                                 handle_event();
204                                 return 1;
205                         }
206                 }
207         }
208         return 0;
211 int BC_Button::cursor_motion_event()
213         if(top_level->button_down && top_level->event_win == win && 
214                 status == BUTTON_DOWNHI && !cursor_inside())
215         {
216                 status = BUTTON_UP;
217                 draw_face();
218         }
219         return 0;
232 BC_OKButton::BC_OKButton(int x, int y)
233  : BC_Button(x, y, 
234         BC_WindowBase::get_resources()->ok_images)
238 BC_OKButton::BC_OKButton(BC_WindowBase *parent_window, VFrame **images)
239  : BC_Button(10, 
240         parent_window->get_h() - images[0]->get_h() - 10, 
241         images)
243         set_tooltip("OK");
246 BC_OKButton::BC_OKButton(BC_WindowBase *parent_window)
247  : BC_Button(10, 
248         parent_window->get_h() - BC_WindowBase::get_resources()->ok_images[0]->get_h() - 10, 
249         BC_WindowBase::get_resources()->ok_images)
251         set_tooltip("OK");
254 int BC_OKButton::handle_event()
256         get_top_level()->set_done(0);
257         return 0;
260 int BC_OKButton::resize_event(int w, int h)
262         reposition_window(10,
263                 h - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10);
264         return 1;
267 int BC_OKButton::keypress_event()
269         if(get_keypress() == RETURN) return handle_event();
270         return 0;
273 int BC_OKButton::calculate_h()
275         return BC_WindowBase::get_resources()->ok_images[0]->get_h();
278 int BC_OKButton::calculate_w()
280         return BC_WindowBase::get_resources()->ok_images[0]->get_w();
295 BC_CancelButton::BC_CancelButton(int x, int y)
296  : BC_Button(x, y, 
297         BC_WindowBase::get_resources()->cancel_images)
299         set_tooltip("Cancel");
302 BC_CancelButton::BC_CancelButton(BC_WindowBase *parent_window)
303  : BC_Button(parent_window->get_w() - BC_WindowBase::get_resources()->cancel_images[0]->get_w() - 10, 
304         parent_window->get_h() - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10, 
305         BC_WindowBase::get_resources()->cancel_images)
307         set_tooltip("Cancel");
310 BC_CancelButton::BC_CancelButton(BC_WindowBase *parent_window, VFrame **images)
311  : BC_Button(parent_window->get_w() - images[0]->get_w() - 10, 
312         parent_window->get_h() - images[0]->get_h() - 10, 
313         images)
315         set_tooltip("Cancel");
318 int BC_CancelButton::handle_event()
320         get_top_level()->set_done(1);
321         return 1;
324 int BC_CancelButton::resize_event(int w,int h)
326         reposition_window(w - BC_WindowBase::get_resources()->cancel_images[0]->get_w() - 10, 
327                 h - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10);
328         return 1;
331 int BC_CancelButton::keypress_event()
333         if(get_keypress() == ESC) return handle_event();
334         return 0;
337 int BC_CancelButton::calculate_h()
339         return BC_WindowBase::get_resources()->cancel_images[0]->get_h();
342 int BC_CancelButton::calculate_w()
344         return BC_WindowBase::get_resources()->cancel_images[0]->get_w();
356 #define LEFT_DN  0
357 #define LEFT_HI  1
358 #define LEFT_UP  2
359 #define MID_DN   3
360 #define MID_HI   4
361 #define MID_UP   5
362 #define RIGHT_DN 6
363 #define RIGHT_HI 7
364 #define RIGHT_UP 8
366 BC_GenericButton::BC_GenericButton(int x, int y, char *text, VFrame **data)
367  : BC_Button(x, 
368         y, 
369         data ? data : BC_WindowBase::get_resources()->generic_button_images)
371         strcpy(this->text, text);
374 BC_GenericButton::BC_GenericButton(int x, int y, int w, char *text, VFrame **data)
375  : BC_Button(x, 
376         y, 
377         w,
378         data ? data : BC_WindowBase::get_resources()->generic_button_images)
380         strcpy(this->text, text);
383 int BC_GenericButton::set_images(VFrame **data)
385         BC_Resources *resources = get_resources();
386         for(int i = 0; i < 3; i++)
387         {
388                 if(images[i]) delete images[i];
389                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
390         }
392         if(w_argument)
393                 w = w_argument;
394         else
395                 w = get_text_width(MEDIUMFONT, text) + 
396                                 resources->generic_button_margin * 2;
399         h = images[BUTTON_UP]->get_h();
400         return 0;
403 int BC_GenericButton::calculate_w(BC_WindowBase *gui, char *text)
405         BC_Resources *resources = gui->get_resources();
406         return gui->get_text_width(MEDIUMFONT, text) + 
407                                 resources->generic_button_margin * 2;
410 int BC_GenericButton::calculate_h()
412         BC_Resources *resources = BC_WindowBase::get_resources();
413         return resources->generic_button_images[0]->get_h();
416 int BC_GenericButton::draw_face()
418         draw_top_background(parent_window, 0, 0, get_w(), get_h());
419         draw_3segmenth(0, 0, get_w(), images[status]);
421         if(enabled)
422                 set_color(get_resources()->default_text_color);
423         else
424                 set_color(get_resources()->disabled_text_color);
425         set_font(MEDIUMFONT);
427         int x, y, w;
428         BC_Resources *resources = get_resources();
429         y = (int)((float)get_h() / 2 + get_text_ascent(MEDIUMFONT) / 2 - 2);
430         w = get_text_width(current_font, text, strlen(text)) + 
431                 resources->generic_button_margin * 2;
432         x = get_w() / 2 - w / 2 + resources->generic_button_margin;
433         if(status == BUTTON_DOWNHI)
434         {
435                 x++;
436                 y++;
437         }
438         draw_text(x, 
439                 y, 
440                 text);
442         if(underline_number >= 0)
443         {
444                 y++;
445                 int x1 = get_text_width(current_font, text, underline_number) + 
446                         x + 
447                         resources->toggle_text_margin;
448                 int x2 = get_text_width(current_font, text, underline_number + 1) + 
449                         x +
450                         resources->toggle_text_margin;
451                 draw_line(x1, y, x2, y);
452                 draw_line(x1, y + 1, (x2 + x1) / 2, y + 1);
453         }
455         flash();
456         return 0;
463 BC_OKTextButton::BC_OKTextButton(BC_WindowBase *parent_window)
464  : BC_GenericButton(10,
465         parent_window->get_h() - BC_GenericButton::calculate_h() - 10,
466         _("OK"))
468         this->parent_window = parent_window;
471 int BC_OKTextButton::resize_event(int w, int h)
473         reposition_window(10,
474                 parent_window->get_h() - BC_GenericButton::calculate_h() - 10);
475         return 1;
478 int BC_OKTextButton::handle_event()
480         get_top_level()->set_done(0);
481         return 0;
484 int BC_OKTextButton::keypress_event()
486         if(get_keypress() == RETURN) return handle_event();
487         return 0;
492 BC_CancelTextButton::BC_CancelTextButton(BC_WindowBase *parent_window)
493  : BC_GenericButton(parent_window->get_w() - BC_GenericButton::calculate_w(parent_window, _("Cancel")) - 10,
494         parent_window->get_h() - BC_GenericButton::calculate_h() - 10,
495         _("Cancel"))
497         this->parent_window = parent_window;
500 int BC_CancelTextButton::resize_event(int w, int h)
502         reposition_window(parent_window->get_w() - BC_GenericButton::calculate_w(parent_window, _("Cancel")) - 10,
503                 parent_window->get_h() - BC_GenericButton::calculate_h() - 10);
504         return 1;
507 int BC_CancelTextButton::handle_event()
509         get_top_level()->set_done(1);
510         return 1;
513 int BC_CancelTextButton::keypress_event()
515         if(get_keypress() == ESC) return handle_event();
516         return 0;