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