add mask color radio btn sel, fix del all mask btn, fix mask dflt kfrm draw name...
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / bcbutton.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcbutton.h"
23 #include "bcpixmap.h"
24 #include "bcresources.h"
25 #include "bcsignals.h"
26 #include "bccolors.h"
27 #include "fonts.h"
28 #include "keys.h"
29 #include "language.h"
30 #include "vframe.h"
31
32
33 #include <string.h>
34 #include <unistd.h>
35
36
37 BC_Button::BC_Button(int x,
38         int y,
39         VFrame **data)
40  : BC_SubWindow(x, y, 0, 0, -1)
41 {
42         this->data = data;
43         for(int i = 0; i < 3; i++) images[i] = 0;
44         if(!data) printf("BC_Button::BC_Button data == 0\n");
45         status = BUTTON_UP;
46         this->w_argument = 0;
47         underline_number = -1;
48         enabled = 1;
49 }
50
51 BC_Button::BC_Button(int x,
52         int y,
53         int w,
54         VFrame **data)
55  : BC_SubWindow(x, y, 0, 0, -1)
56 {
57         this->data = data;
58         this->w_argument = w;
59         for(int i = 0; i < 3; i++) images[i] = 0;
60         if(!data) printf("BC_Button::BC_Button data == 0\n");
61         status = BUTTON_UP;
62         underline_number = -1;
63         enabled = 1;
64 }
65
66
67 BC_Button::~BC_Button()
68 {
69         for(int i = 0; i < 3; i++) if(images[i]) delete images[i];
70 }
71
72
73
74 int BC_Button::initialize()
75 {
76 // Get the image
77         set_images(data);
78
79 // Create the subwindow
80         BC_SubWindow::initialize();
81
82 // Display the bitmap
83         draw_face(0);
84         show_window(0);
85         return 0;
86 }
87
88 int BC_Button::reposition_window(int x, int y)
89 {
90         BC_WindowBase::reposition_window(x, y);
91         draw_face(0);
92         return 0;
93 }
94
95
96 int BC_Button::update_bitmaps(VFrame **data)
97 {
98         this->data = data;
99         set_images(data);
100         draw_top_background(parent_window, 0, 0, w, h);
101         draw_face();
102         return 0;
103 }
104
105 void BC_Button::enable()
106 {
107         enabled = 1;
108         status = BUTTON_UP;
109         draw_face();
110 }
111
112 void BC_Button::disable()
113 {
114         enabled = 0;
115         status = BUTTON_UP;
116         draw_face();
117 }
118
119
120 void BC_Button::set_underline(int number)
121 {
122         this->underline_number = number;
123 }
124
125 int BC_Button::set_images(VFrame **data)
126 {
127         for(int i = 0; i < 3; i++)
128         {
129                 if(images[i]) delete images[i];
130                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
131         }
132
133         if(w_argument > 0)
134                 w = w_argument;
135         else
136                 w = images[BUTTON_UP]->get_w();
137
138         h = images[BUTTON_UP]->get_h();
139         return 0;
140 }
141
142 int BC_Button::draw_face(int flush)
143 {
144         draw_top_background(parent_window, 0, 0, w, h);
145         pixmap->draw_pixmap(images[status],
146                         0,
147                         0,
148                         w,
149                         h,
150                         0,
151                         0);
152         this->flash(flush);
153         return 0;
154 }
155
156 int BC_Button::repeat_event(int64_t duration)
157 {
158         if( status == BUTTON_UPHI && tooltip_text && tooltip_text[0] != 0 &&
159                 duration == top_level->get_resources()->tooltip_delay )
160         {
161                 show_tooltip();
162                 return 1;
163         }
164         return 0;
165 }
166
167 int BC_Button::cursor_enter_event()
168 {
169         if(is_event_win() && enabled)
170         {
171                 if(top_level->button_down) {
172                         status = BUTTON_DOWNHI;
173                 }
174                 else
175                 if(status == BUTTON_UP) status = BUTTON_UPHI;
176                 draw_face();
177         }
178         return 0;
179 }
180
181 int BC_Button::cursor_leave_event()
182 {
183         if(status == BUTTON_UPHI)
184         {
185                 status = BUTTON_UP;
186
187                 draw_face();
188
189                 hide_tooltip();
190
191         }
192         return 0;
193 }
194
195 int BC_Button::button_press_event()
196 {
197         if(is_event_win() && get_buttonpress() == 1 && enabled)
198         {
199                 hide_tooltip();
200                 if(status == BUTTON_UPHI || status == BUTTON_UP) status = BUTTON_DOWNHI;
201                 draw_face();
202                 return 1;
203         }
204         return 0;
205 }
206
207 int BC_Button::button_release_event()
208 {
209         if(is_event_win())
210         {
211                 hide_tooltip();
212                 if(status == BUTTON_DOWNHI)
213                 {
214                         status = BUTTON_UPHI;
215                         draw_face();
216
217                         if(cursor_inside())
218                         {
219                                 handle_event();
220                                 return 1;
221                         }
222                 }
223         }
224         return 0;
225 }
226
227 int BC_Button::cursor_motion_event()
228 {
229         if(top_level->button_down &&
230                 is_event_win() &&
231                 status == BUTTON_DOWNHI &&
232                 !cursor_inside())
233         {
234                 status = BUTTON_UP;
235                 draw_face();
236         }
237         return 0;
238 }
239
240 int BC_Button::get_status()
241 {
242         return status;
243 }
244
245
246
247
248
249
250
251
252
253
254
255 BC_OKButton::BC_OKButton(int x, int y)
256  : BC_Button(x, y,
257         BC_WindowBase::get_resources()->ok_images)
258 {
259 }
260
261 BC_OKButton::BC_OKButton(BC_WindowBase *parent_window, VFrame **images)
262  : BC_Button(10,
263         parent_window->get_h() - images[0]->get_h() - 10,
264         images)
265 {
266         set_tooltip(_("OK"));
267 }
268
269 BC_OKButton::BC_OKButton(BC_WindowBase *parent_window)
270  : BC_Button(10,
271         parent_window->get_h() - BC_WindowBase::get_resources()->ok_images[0]->get_h() - 10,
272         BC_WindowBase::get_resources()->ok_images)
273 {
274         set_tooltip(_("OK"));
275 }
276
277 int BC_OKButton::handle_event()
278 {
279         get_top_level()->set_done(0);
280         return 0;
281 }
282
283 int BC_OKButton::resize_event(int w, int h)
284 {
285         reposition_window(10,
286                 h - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10);
287         return 1;
288 }
289
290 int BC_OKButton::keypress_event()
291 {
292         if(get_keypress() == RETURN) return handle_event();
293         return 0;
294 }
295
296 int BC_OKButton::calculate_h()
297 {
298         return BC_WindowBase::get_resources()->ok_images[0]->get_h();
299 }
300
301 int BC_OKButton::calculate_w()
302 {
303         return BC_WindowBase::get_resources()->ok_images[0]->get_w();
304 }
305
306
307
308
309
310
311
312
313
314
315
316
317
318 BC_CancelButton::BC_CancelButton(int x, int y)
319  : BC_Button(x, y,
320         BC_WindowBase::get_resources()->cancel_images)
321 {
322         set_tooltip(_("Cancel"));
323 }
324
325 BC_CancelButton::BC_CancelButton(BC_WindowBase *parent_window)
326  : BC_Button(parent_window->get_w() - BC_WindowBase::get_resources()->cancel_images[0]->get_w() - 10,
327         parent_window->get_h() - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10,
328         BC_WindowBase::get_resources()->cancel_images)
329 {
330         set_tooltip(_("Cancel"));
331 }
332
333 BC_CancelButton::BC_CancelButton(BC_WindowBase *parent_window, VFrame **images)
334  : BC_Button(parent_window->get_w() - images[0]->get_w() - 10,
335         parent_window->get_h() - images[0]->get_h() - 10,
336         images)
337 {
338         set_tooltip(_("Cancel"));
339 }
340
341 int BC_CancelButton::handle_event()
342 {
343         get_top_level()->set_done(1);
344         return 1;
345 }
346
347 int BC_CancelButton::resize_event(int w,int h)
348 {
349         reposition_window(w - BC_WindowBase::get_resources()->cancel_images[0]->get_w() - 10,
350                 h - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10);
351         return 1;
352 }
353
354 int BC_CancelButton::keypress_event()
355 {
356         if(get_keypress() == ESC) return handle_event();
357         return 0;
358 }
359
360 int BC_CancelButton::calculate_h()
361 {
362         return BC_WindowBase::get_resources()->cancel_images[0]->get_h();
363 }
364
365 int BC_CancelButton::calculate_w()
366 {
367         return BC_WindowBase::get_resources()->cancel_images[0]->get_w();
368 }
369
370
371
372
373
374
375
376
377
378
379 #define LEFT_DN  0
380 #define LEFT_HI  1
381 #define LEFT_UP  2
382 #define MID_DN   3
383 #define MID_HI   4
384 #define MID_UP   5
385 #define RIGHT_DN 6
386 #define RIGHT_HI 7
387 #define RIGHT_UP 8
388
389 BC_GenericButton::BC_GenericButton(int x, int y,
390                 const char *text, VFrame **data, int color)
391  : BC_Button(x, y,
392         data ? data : BC_WindowBase::get_resources()->generic_button_images)
393 {
394         strcpy(this->text, text);
395         this->color = color;
396 }
397
398 BC_GenericButton::BC_GenericButton(int x, int y,
399                 int w, const char *text, VFrame **data, int color)
400  : BC_Button(x, y, w,
401         data ? data : BC_WindowBase::get_resources()->generic_button_images)
402 {
403         strcpy(this->text, text);
404         this->color = color;
405 }
406
407 int BC_GenericButton::set_images(VFrame **data)
408 {
409         BC_Resources *resources = get_resources();
410         for(int i = 0; i < 3; i++)
411         {
412                 if(images[i]) delete images[i];
413                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
414         }
415
416         if(w_argument)
417                 w = w_argument;
418         else
419                 w = get_text_width(MEDIUMFONT, text) +
420                                 resources->generic_button_margin * 2;
421
422
423         h = images[BUTTON_UP]->get_h();
424         return 0;
425 }
426
427 void BC_GenericButton::text_color(int color)
428 {
429         this->color = color;
430 }
431
432 int BC_GenericButton::calculate_w(BC_WindowBase *gui, const char *text)
433 {
434         BC_Resources *resources = gui->get_resources();
435         return gui->get_text_width(MEDIUMFONT, text) +
436                                 resources->generic_button_margin * 2;
437 }
438
439 int BC_GenericButton::calculate_h()
440 {
441         BC_Resources *resources = BC_WindowBase::get_resources();
442         return resources->generic_button_images[0]->get_h();
443 }
444
445 int BC_GenericButton::draw_face(int flush)
446 {
447         draw_top_background(parent_window, 0, 0, get_w(), get_h());
448         draw_3segmenth(0, 0, get_w(), images[status]);
449
450         BC_Resources *resources = get_resources();
451         if(color >= 0 )
452                 set_color(color);
453         else if(enabled)
454                 set_color(resources->default_text_color);
455         else
456                 set_color(resources->disabled_text_color);
457         set_font(MEDIUMFONT);
458
459         int x, y, w;
460         y = (int)((float)get_h() / 2 + get_text_ascent(MEDIUMFONT) / 2 - 2);
461         w = get_text_width(current_font, text, strlen(text)) +
462                 resources->generic_button_margin * 2;
463         x = get_w() / 2 - w / 2 + resources->generic_button_margin;
464         if(status == BUTTON_DOWNHI) { x++; y++; }
465         draw_text(x, y, text);
466
467         if(underline_number >= 0)
468         {
469                 y++;
470                 int x1 = get_text_width(current_font, text, underline_number) +
471                         x;
472                 int x2 = get_text_width(current_font, text, underline_number + 1) +
473                         x;
474
475                 draw_line(x1, y, x2, y);
476                 draw_line(x1, y + 1, (x2 + x1) / 2, y + 1);
477         }
478
479         this->flash(flush);
480         return 0;
481 }
482
483
484
485
486
487 BC_OKTextButton::BC_OKTextButton(BC_WindowBase *parent_window)
488  : BC_GenericButton(10,
489         parent_window->get_h() - BC_GenericButton::calculate_h() - 10,
490         _("OK"))
491 {
492         this->parent_window = parent_window;
493 }
494
495 int BC_OKTextButton::resize_event(int w, int h)
496 {
497         reposition_window(10,
498                 parent_window->get_h() - BC_GenericButton::calculate_h() - 10);
499         return 1;
500 }
501
502 int BC_OKTextButton::handle_event()
503 {
504         get_top_level()->set_done(0);
505         return 0;
506 }
507
508 int BC_OKTextButton::keypress_event()
509 {
510         if(get_keypress() == RETURN) return handle_event();
511         return 0;
512 }
513
514
515
516 BC_CancelTextButton::BC_CancelTextButton(BC_WindowBase *parent_window)
517  : BC_GenericButton(parent_window->get_w() - BC_GenericButton::calculate_w(parent_window, _("Cancel")) - 10,
518         parent_window->get_h() - BC_GenericButton::calculate_h() - 10,
519         _("Cancel"))
520 {
521         this->parent_window = parent_window;
522 }
523
524 int BC_CancelTextButton::resize_event(int w, int h)
525 {
526         reposition_window(parent_window->get_w() - BC_GenericButton::calculate_w(parent_window, _("Cancel")) - 10,
527                 parent_window->get_h() - BC_GenericButton::calculate_h() - 10);
528         return 1;
529 }
530
531 int BC_CancelTextButton::handle_event()
532 {
533         get_top_level()->set_done(1);
534         return 1;
535 }
536
537 int BC_CancelTextButton::keypress_event()
538 {
539         if(get_keypress() == ESC) return handle_event();
540         return 0;
541 }
542
543
544
545