90a4681db0ddd6d28750315449cdafeb3dc7cce8
[goodguy/history.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 "colors.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         draw_face();
109 }
110
111 void BC_Button::disable()
112 {
113         enabled = 0;
114         status = BUTTON_UP;
115         draw_face();
116 }
117
118
119 void BC_Button::set_underline(int number)
120 {
121         this->underline_number = number;
122 }
123
124 int BC_Button::set_images(VFrame **data)
125 {
126         for(int i = 0; i < 3; i++)
127         {
128                 if(images[i]) delete images[i];
129                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
130         }
131
132         if(w_argument > 0)
133                 w = w_argument;
134         else
135                 w = images[BUTTON_UP]->get_w();
136
137         h = images[BUTTON_UP]->get_h();
138         return 0;
139 }
140
141 int BC_Button::draw_face(int flush)
142 {
143         draw_top_background(parent_window, 0, 0, w, h);
144         pixmap->draw_pixmap(images[status],
145                         0,
146                         0,
147                         w,
148                         h,
149                         0,
150                         0);
151         this->flash(flush);
152         return 0;
153 }
154
155 int BC_Button::repeat_event(int64_t duration)
156 {
157         if( status == BUTTON_UPHI && !tooltip_done &&
158                 tooltip_text && tooltip_text[0] != 0 &&
159                 duration == top_level->get_resources()->tooltip_delay )
160         {
161                 show_tooltip();
162                 tooltip_done = 1;
163                 return 1;
164         }
165         return 0;
166 }
167
168 int BC_Button::cursor_enter_event()
169 {
170         if(is_event_win() && enabled)
171         {
172                 tooltip_done = 0;
173                 if(top_level->button_down)
174                 {
175                         status = BUTTON_DOWNHI;
176                 }
177                 else
178                 if(status == BUTTON_UP) status = BUTTON_UPHI;
179                 draw_face();
180         }
181         return 0;
182 }
183
184 int BC_Button::cursor_leave_event()
185 {
186         if(status == BUTTON_UPHI)
187         {
188                 status = BUTTON_UP;
189
190                 draw_face();
191
192                 hide_tooltip();
193
194         }
195         return 0;
196 }
197
198 int BC_Button::button_press_event()
199 {
200         if(is_event_win() && get_buttonpress() == 1 && enabled)
201         {
202                 hide_tooltip();
203                 if(status == BUTTON_UPHI || status == BUTTON_UP) status = BUTTON_DOWNHI;
204                 draw_face();
205                 return 1;
206         }
207         return 0;
208 }
209
210 int BC_Button::button_release_event()
211 {
212         if(is_event_win())
213         {
214                 hide_tooltip();
215                 if(status == BUTTON_DOWNHI)
216                 {
217                         status = BUTTON_UPHI;
218                         draw_face();
219
220                         if(cursor_inside())
221                         {
222                                 handle_event();
223                                 return 1;
224                         }
225                 }
226         }
227         return 0;
228 }
229
230 int BC_Button::cursor_motion_event()
231 {
232         if(top_level->button_down &&
233                 is_event_win() &&
234                 status == BUTTON_DOWNHI &&
235                 !cursor_inside())
236         {
237                 status = BUTTON_UP;
238                 draw_face();
239         }
240         return 0;
241 }
242
243 int BC_Button::get_status()
244 {
245         return status;
246 }
247
248
249
250
251
252
253
254
255
256
257
258 BC_OKButton::BC_OKButton(int x, int y)
259  : BC_Button(x, y,
260         BC_WindowBase::get_resources()->ok_images)
261 {
262 }
263
264 BC_OKButton::BC_OKButton(BC_WindowBase *parent_window, VFrame **images)
265  : BC_Button(10,
266         parent_window->get_h() - images[0]->get_h() - 10,
267         images)
268 {
269         set_tooltip(_("OK"));
270 }
271
272 BC_OKButton::BC_OKButton(BC_WindowBase *parent_window)
273  : BC_Button(10,
274         parent_window->get_h() - BC_WindowBase::get_resources()->ok_images[0]->get_h() - 10,
275         BC_WindowBase::get_resources()->ok_images)
276 {
277         set_tooltip(_("OK"));
278 }
279
280 int BC_OKButton::handle_event()
281 {
282         get_top_level()->set_done(0);
283         return 0;
284 }
285
286 int BC_OKButton::resize_event(int w, int h)
287 {
288         reposition_window(10,
289                 h - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10);
290         return 1;
291 }
292
293 int BC_OKButton::keypress_event()
294 {
295         if(get_keypress() == RETURN) return handle_event();
296         return 0;
297 }
298
299 int BC_OKButton::calculate_h()
300 {
301         return BC_WindowBase::get_resources()->ok_images[0]->get_h();
302 }
303
304 int BC_OKButton::calculate_w()
305 {
306         return BC_WindowBase::get_resources()->ok_images[0]->get_w();
307 }
308
309
310
311
312
313
314
315
316
317
318
319
320
321 BC_CancelButton::BC_CancelButton(int x, int y)
322  : BC_Button(x, y,
323         BC_WindowBase::get_resources()->cancel_images)
324 {
325         set_tooltip(_("Cancel"));
326 }
327
328 BC_CancelButton::BC_CancelButton(BC_WindowBase *parent_window)
329  : BC_Button(parent_window->get_w() - BC_WindowBase::get_resources()->cancel_images[0]->get_w() - 10,
330         parent_window->get_h() - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10,
331         BC_WindowBase::get_resources()->cancel_images)
332 {
333         set_tooltip(_("Cancel"));
334 }
335
336 BC_CancelButton::BC_CancelButton(BC_WindowBase *parent_window, VFrame **images)
337  : BC_Button(parent_window->get_w() - images[0]->get_w() - 10,
338         parent_window->get_h() - images[0]->get_h() - 10,
339         images)
340 {
341         set_tooltip(_("Cancel"));
342 }
343
344 int BC_CancelButton::handle_event()
345 {
346         get_top_level()->set_done(1);
347         return 1;
348 }
349
350 int BC_CancelButton::resize_event(int w,int h)
351 {
352         reposition_window(w - BC_WindowBase::get_resources()->cancel_images[0]->get_w() - 10,
353                 h - BC_WindowBase::get_resources()->cancel_images[0]->get_h() - 10);
354         return 1;
355 }
356
357 int BC_CancelButton::keypress_event()
358 {
359         if(get_keypress() == ESC) return handle_event();
360         return 0;
361 }
362
363 int BC_CancelButton::calculate_h()
364 {
365         return BC_WindowBase::get_resources()->cancel_images[0]->get_h();
366 }
367
368 int BC_CancelButton::calculate_w()
369 {
370         return BC_WindowBase::get_resources()->cancel_images[0]->get_w();
371 }
372
373
374
375
376
377
378
379
380
381
382 #define LEFT_DN  0
383 #define LEFT_HI  1
384 #define LEFT_UP  2
385 #define MID_DN   3
386 #define MID_HI   4
387 #define MID_UP   5
388 #define RIGHT_DN 6
389 #define RIGHT_HI 7
390 #define RIGHT_UP 8
391
392 BC_GenericButton::BC_GenericButton(int x, int y,
393                 const char *text, VFrame **data, int color)
394  : BC_Button(x, y,
395         data ? data : BC_WindowBase::get_resources()->generic_button_images)
396 {
397         strcpy(this->text, text);
398         this->color = color;
399 }
400
401 BC_GenericButton::BC_GenericButton(int x, int y,
402                 int w, const char *text, VFrame **data, int color)
403  : BC_Button(x, y, w,
404         data ? data : BC_WindowBase::get_resources()->generic_button_images)
405 {
406         strcpy(this->text, text);
407         this->color = color;
408 }
409
410 int BC_GenericButton::set_images(VFrame **data)
411 {
412         BC_Resources *resources = get_resources();
413         for(int i = 0; i < 3; i++)
414         {
415                 if(images[i]) delete images[i];
416                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
417         }
418
419         if(w_argument)
420                 w = w_argument;
421         else
422                 w = get_text_width(MEDIUMFONT, text) +
423                                 resources->generic_button_margin * 2;
424
425
426         h = images[BUTTON_UP]->get_h();
427         return 0;
428 }
429
430 void BC_GenericButton::text_color(int color)
431 {
432         this->color = color;
433 }
434
435 int BC_GenericButton::calculate_w(BC_WindowBase *gui, const char *text)
436 {
437         BC_Resources *resources = gui->get_resources();
438         return gui->get_text_width(MEDIUMFONT, text) +
439                                 resources->generic_button_margin * 2;
440 }
441
442 int BC_GenericButton::calculate_h()
443 {
444         BC_Resources *resources = BC_WindowBase::get_resources();
445         return resources->generic_button_images[0]->get_h();
446 }
447
448 int BC_GenericButton::draw_face(int flush)
449 {
450         draw_top_background(parent_window, 0, 0, get_w(), get_h());
451         draw_3segmenth(0, 0, get_w(), images[status]);
452
453         BC_Resources *resources = get_resources();
454         if(color >= 0 )
455                 set_color(color);
456         else if(enabled)
457                 set_color(resources->default_text_color);
458         else
459                 set_color(resources->disabled_text_color);
460         set_font(MEDIUMFONT);
461
462         int x, y, w;
463         y = (int)((float)get_h() / 2 + get_text_ascent(MEDIUMFONT) / 2 - 2);
464         w = get_text_width(current_font, text, strlen(text)) +
465                 resources->generic_button_margin * 2;
466         x = get_w() / 2 - w / 2 + resources->generic_button_margin;
467         if(status == BUTTON_DOWNHI) { x++; y++; }
468         draw_text(x, y, text);
469
470         if(underline_number >= 0)
471         {
472                 y++;
473                 int x1 = get_text_width(current_font, text, underline_number) +
474                         x;
475                 int x2 = get_text_width(current_font, text, underline_number + 1) +
476                         x;
477
478                 draw_line(x1, y, x2, y);
479                 draw_line(x1, y + 1, (x2 + x1) / 2, y + 1);
480         }
481
482         this->flash(flush);
483         return 0;
484 }
485
486
487
488
489
490 BC_OKTextButton::BC_OKTextButton(BC_WindowBase *parent_window)
491  : BC_GenericButton(10,
492         parent_window->get_h() - BC_GenericButton::calculate_h() - 10,
493         _("OK"))
494 {
495         this->parent_window = parent_window;
496 }
497
498 int BC_OKTextButton::resize_event(int w, int h)
499 {
500         reposition_window(10,
501                 parent_window->get_h() - BC_GenericButton::calculate_h() - 10);
502         return 1;
503 }
504
505 int BC_OKTextButton::handle_event()
506 {
507         get_top_level()->set_done(0);
508         return 0;
509 }
510
511 int BC_OKTextButton::keypress_event()
512 {
513         if(get_keypress() == RETURN) return handle_event();
514         return 0;
515 }
516
517
518
519 BC_CancelTextButton::BC_CancelTextButton(BC_WindowBase *parent_window)
520  : BC_GenericButton(parent_window->get_w() - BC_GenericButton::calculate_w(parent_window, _("Cancel")) - 10,
521         parent_window->get_h() - BC_GenericButton::calculate_h() - 10,
522         _("Cancel"))
523 {
524         this->parent_window = parent_window;
525 }
526
527 int BC_CancelTextButton::resize_event(int w, int h)
528 {
529         reposition_window(parent_window->get_w() - BC_GenericButton::calculate_w(parent_window, _("Cancel")) - 10,
530                 parent_window->get_h() - BC_GenericButton::calculate_h() - 10);
531         return 1;
532 }
533
534 int BC_CancelTextButton::handle_event()
535 {
536         get_top_level()->set_done(1);
537         return 1;
538 }
539
540 int BC_CancelTextButton::keypress_event()
541 {
542         if(get_keypress() == ESC) return handle_event();
543         return 0;
544 }
545
546
547
548