r717: Made the highlighted text color of the menus WHITE
[cinelerra_cv/mob.git] / guicast / bcbutton.C
blob7136d818b9339c5e75120bbd291b34c48f23c12c
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 "language.h"
8 #include "vframe.h"
11 #include <string.h>
12 #include <unistd.h>
14 #define BUTTON_UP 0
15 #define BUTTON_UPHI 1
16 #define BUTTON_DOWNHI 2
18 BC_Button::BC_Button(int x, 
19         int y, 
20         VFrame **data)
21  : BC_SubWindow(x, y, 0, 0, -1)
23         this->data = data;
24         for(int i = 0; i < 3; i++) images[i] = 0;
25         if(!data) printf("BC_Button::BC_Button data == 0\n");
26         status = BUTTON_UP;
27         this->w_argument = 0;
28         underline_number = -1;
29         enabled = 1;
32 BC_Button::BC_Button(int x, 
33         int y, 
34         int w, 
35         VFrame **data)
36  : BC_SubWindow(x, y, 0, 0, -1)
38         this->data = data;
39         this->w_argument = w;
40         for(int i = 0; i < 3; i++) images[i] = 0;
41         if(!data) printf("BC_Button::BC_Button data == 0\n");
42         status = BUTTON_UP;
43         underline_number = -1;
44         enabled = 1;
48 BC_Button::~BC_Button()
50         for(int i = 0; i < 3; i++) if(images[i]) delete images[i];
55 int BC_Button::initialize()
57 // Get the image
58         set_images(data);
60 // Create the subwindow
61         BC_SubWindow::initialize();
63 // Display the bitmap
64         draw_face();
65         return 0;
68 int BC_Button::reposition_window(int x, int y)
70         BC_WindowBase::reposition_window(x, y);
71         draw_face();
72         return 0;
76 int BC_Button::update_bitmaps(VFrame **data)
78         this->data = data;
79         set_images(data);
80         draw_top_background(parent_window, 0, 0, w, h);
81         draw_face();
82         return 0;
85 void BC_Button::enable()
87         enabled = 1;
88         draw_face();
91 void BC_Button::disable()
93         enabled = 0;
94         draw_face();
98 void BC_Button::set_underline(int number)
100         this->underline_number = number;
103 int BC_Button::set_images(VFrame **data)
105         for(int i = 0; i < 3; i++)
106         {
107                 if(images[i]) delete images[i];
108                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
109         }
111         if(w_argument > 0)
112                 w = w_argument;
113         else
114                 w = images[BUTTON_UP]->get_w();
116         h = images[BUTTON_UP]->get_h();
117         return 0;
120 int BC_Button::draw_face()
122         draw_top_background(parent_window, 0, 0, w, h);
123         images[status]->write_drawable(pixmap, 
124                         0, 
125                         0,
126                         w,
127                         h,
128                         0,
129                         0);
130         flash();
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;
235 BC_OKButton::BC_OKButton(int x, int y)
236  : BC_Button(x, y, 
237         BC_WindowBase::get_resources()->ok_images)
241 BC_OKButton::BC_OKButton(BC_WindowBase *parent_window, VFrame **images)
242  : BC_Button(10, 
243         parent_window->get_h() - images[0]->get_h() - 10, 
244         images)
246         set_tooltip("OK");
249 BC_OKButton::BC_OKButton(BC_WindowBase *parent_window)
250  : BC_Button(10, 
251         parent_window->get_h() - BC_WindowBase::get_resources()->ok_images[0]->get_h() - 10, 
252         BC_WindowBase::get_resources()->ok_images)
254         set_tooltip("OK");
257 int BC_OKButton::handle_event()
259         get_top_level()->set_done(0);
260         return 0;
263 int BC_OKButton::resize_event(int w, int h)
265         reposition_window(10,
266                 h - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10);
267         return 1;
270 int BC_OKButton::keypress_event()
272         if(get_keypress() == RETURN) return handle_event();
273         return 0;
276 int BC_OKButton::calculate_h()
278         return BC_WindowBase::get_resources()->ok_images[0]->get_h();
281 int BC_OKButton::calculate_w()
283         return BC_WindowBase::get_resources()->ok_images[0]->get_w();
298 BC_CancelButton::BC_CancelButton(int x, int y)
299  : BC_Button(x, y, 
300         BC_WindowBase::get_resources()->cancel_images)
302         set_tooltip("Cancel");
305 BC_CancelButton::BC_CancelButton(BC_WindowBase *parent_window)
306  : BC_Button(parent_window->get_w() - BC_WindowBase::get_resources()->cancel_images[0]->get_w() - 10, 
307         parent_window->get_h() - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10, 
308         BC_WindowBase::get_resources()->cancel_images)
310         set_tooltip("Cancel");
313 BC_CancelButton::BC_CancelButton(BC_WindowBase *parent_window, VFrame **images)
314  : BC_Button(parent_window->get_w() - images[0]->get_w() - 10, 
315         parent_window->get_h() - images[0]->get_h() - 10, 
316         images)
318         set_tooltip("Cancel");
321 int BC_CancelButton::handle_event()
323         get_top_level()->set_done(1);
324         return 1;
327 int BC_CancelButton::resize_event(int w,int h)
329         reposition_window(w - BC_WindowBase::get_resources()->cancel_images[0]->get_w() - 10, 
330                 h - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10);
331         return 1;
334 int BC_CancelButton::keypress_event()
336         if(get_keypress() == ESC) return handle_event();
337         return 0;
340 int BC_CancelButton::calculate_h()
342         return BC_WindowBase::get_resources()->cancel_images[0]->get_h();
345 int BC_CancelButton::calculate_w()
347         return BC_WindowBase::get_resources()->cancel_images[0]->get_w();
359 #define LEFT_DN  0
360 #define LEFT_HI  1
361 #define LEFT_UP  2
362 #define MID_DN   3
363 #define MID_HI   4
364 #define MID_UP   5
365 #define RIGHT_DN 6
366 #define RIGHT_HI 7
367 #define RIGHT_UP 8
369 BC_GenericButton::BC_GenericButton(int x, int y, char *text, VFrame **data)
370  : BC_Button(x, 
371         y, 
372         data ? data : BC_WindowBase::get_resources()->generic_button_images)
374         strcpy(this->text, text);
377 BC_GenericButton::BC_GenericButton(int x, int y, int w, char *text, VFrame **data)
378  : BC_Button(x, 
379         y, 
380         w,
381         data ? data : BC_WindowBase::get_resources()->generic_button_images)
383         strcpy(this->text, text);
386 int BC_GenericButton::set_images(VFrame **data)
388         BC_Resources *resources = get_resources();
389         for(int i = 0; i < 3; i++)
390         {
391                 if(images[i]) delete images[i];
392                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
393         }
395         if(w_argument)
396                 w = w_argument;
397         else
398                 w = get_text_width(MEDIUMFONT, text) + 
399                                 resources->generic_button_margin * 2;
402         h = images[BUTTON_UP]->get_h();
403         return 0;
406 int BC_GenericButton::calculate_w(BC_WindowBase *gui, char *text)
408         BC_Resources *resources = gui->get_resources();
409         return gui->get_text_width(MEDIUMFONT, text) + 
410                                 resources->generic_button_margin * 2;
413 int BC_GenericButton::calculate_h()
415         BC_Resources *resources = BC_WindowBase::get_resources();
416         return resources->generic_button_images[0]->get_h();
419 int BC_GenericButton::draw_face()
421         draw_top_background(parent_window, 0, 0, get_w(), get_h());
422         draw_3segmenth(0, 0, get_w(), images[status]);
424         if(enabled)
425                 set_color(get_resources()->default_text_color);
426         else
427                 set_color(get_resources()->disabled_text_color);
428         set_font(MEDIUMFONT);
430         int x, y, w;
431         BC_Resources *resources = get_resources();
432         y = (int)((float)get_h() / 2 + get_text_ascent(MEDIUMFONT) / 2 - 2);
433         w = get_text_width(current_font, text, strlen(text)) + 
434                 resources->generic_button_margin * 2;
435         x = get_w() / 2 - w / 2 + resources->generic_button_margin;
436         if(status == BUTTON_DOWNHI)
437         {
438                 x++;
439                 y++;
440         }
441         draw_text(x, 
442                 y, 
443                 text);
445         if(underline_number >= 0)
446         {
447                 y++;
448                 int x1 = get_text_width(current_font, text, underline_number) + 
449                         x + 
450                         resources->toggle_text_margin;
451                 int x2 = get_text_width(current_font, text, underline_number + 1) + 
452                         x +
453                         resources->toggle_text_margin;
454                 draw_line(x1, y, x2, y);
455                 draw_line(x1, y + 1, (x2 + x1) / 2, y + 1);
456         }
458         flash();
459         return 0;
466 BC_OKTextButton::BC_OKTextButton(BC_WindowBase *parent_window)
467  : BC_GenericButton(10,
468         parent_window->get_h() - BC_GenericButton::calculate_h() - 10,
469         _("OK"))
471         this->parent_window = parent_window;
474 int BC_OKTextButton::resize_event(int w, int h)
476         reposition_window(10,
477                 parent_window->get_h() - BC_GenericButton::calculate_h() - 10);
478         return 1;
481 int BC_OKTextButton::handle_event()
483         get_top_level()->set_done(0);
484         return 0;
487 int BC_OKTextButton::keypress_event()
489         if(get_keypress() == RETURN) return handle_event();
490         return 0;
495 BC_CancelTextButton::BC_CancelTextButton(BC_WindowBase *parent_window)
496  : BC_GenericButton(parent_window->get_w() - BC_GenericButton::calculate_w(parent_window, _("Cancel")) - 10,
497         parent_window->get_h() - BC_GenericButton::calculate_h() - 10,
498         _("Cancel"))
500         this->parent_window = parent_window;
503 int BC_CancelTextButton::resize_event(int w, int h)
505         reposition_window(parent_window->get_w() - BC_GenericButton::calculate_w(parent_window, _("Cancel")) - 10,
506                 parent_window->get_h() - BC_GenericButton::calculate_h() - 10);
507         return 1;
510 int BC_CancelTextButton::handle_event()
512         get_top_level()->set_done(1);
513         return 1;
516 int BC_CancelTextButton::keypress_event()
518         if(get_keypress() == ESC) return handle_event();
519         return 0;