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