improve resize flash operation, fixup xv grab/ungrab, fixup label updates
[goodguy/history.git] / cinelerra-5.1 / guicast / bcpopupmenu.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 "bcmenubar.h"
23 #include "bcmenuitem.h"
24 #include "bcmenupopup.h"
25 #include "bcpixmap.h"
26 #include "bcpopupmenu.h"
27 #include "bcresources.h"
28 #include "bcsignals.h"
29 #include "colors.h"
30 #include "fonts.h"
31 #include <string.h>
32 #include "vframe.h"
33
34 #define BUTTON_UP 0
35 #define BUTTON_HI 1
36 #define BUTTON_DN 2
37 #define TOTAL_IMAGES 3
38
39
40 #define TRIANGLE_W 10
41 #define TRIANGLE_H 10
42
43
44 BC_PopupMenu::BC_PopupMenu(int x,
45                 int y,
46                 int w,
47                 const char *text,
48                 int use_title,
49                 VFrame **data,
50                 int margin)
51  : BC_SubWindow(x, y, 0, 0, -1)
52 {
53         highlighted = popup_down = 0;
54         menu_popup = 0;
55         icon = 0;
56         if(margin >= 0)
57                 this->margin = margin;
58         else
59                 this->margin = BC_WindowBase::get_resources()->popupmenu_margin;
60
61         this->use_title = use_title;
62         strcpy(this->text, text);
63         for(int i = 0; i < TOTAL_IMAGES; i++)
64         {
65                 images[i] = 0;
66         }
67         this->data = data;
68         this->w_argument = w;
69         status = BUTTON_UP;
70 }
71
72 BC_PopupMenu::BC_PopupMenu(int x,
73                 int y,
74                 const char *text,
75                 int use_title,
76                 VFrame **data)
77  : BC_SubWindow(x, y, 0, -1, -1)
78 {
79         highlighted = popup_down = 0;
80         menu_popup = 0;
81         icon = 0;
82         this->use_title = use_title;
83         strcpy(this->text, text);
84         for(int i = 0; i < TOTAL_IMAGES; i++)
85         {
86                 images[i] = 0;
87         }
88         this->data = data;
89         this->w_argument = -1;
90         status = BUTTON_UP;
91 }
92
93 BC_PopupMenu::~BC_PopupMenu()
94 {
95         if(menu_popup) delete menu_popup;
96         for(int i = 0; i < TOTAL_IMAGES; i++)
97         {
98                 if(images[i]) delete images[i];
99         }
100 }
101
102 char* BC_PopupMenu::get_text()
103 {
104         return text;
105 }
106
107 void BC_PopupMenu::set_text(const char *text)
108 {
109         if(use_title)
110         {
111                 strcpy(this->text, text);
112                 draw_title(1);
113         }
114 }
115
116 void BC_PopupMenu::set_icon(BC_Pixmap *icon)
117 {
118         if(use_title)
119         {
120                 this->icon = icon;
121                 if(menu_popup) draw_title(1);
122         }
123 }
124
125 int BC_PopupMenu::initialize()
126 {
127         if(use_title)
128         {
129                 if(data)
130                         set_images(data);
131                 else
132                 if(BC_WindowBase::get_resources()->popupmenu_images)
133                         set_images(BC_WindowBase::get_resources()->popupmenu_images);
134                 else
135                         set_images(BC_WindowBase::get_resources()->generic_button_images);
136         }
137         else
138 // Move outside window if no title
139         {
140                 x = -10;
141                 y = -10;
142                 w = 10;
143                 h = 10;
144         }
145
146         BC_SubWindow::initialize();
147
148         menu_popup = new BC_MenuPopup;
149         menu_popup->initialize(top_level,
150                 0,
151                 0,
152                 0,
153                 this);
154
155         if(use_title) draw_title(0);
156
157         return 0;
158 }
159
160 int BC_PopupMenu::set_images(VFrame **data)
161 {
162         BC_Resources *resources = get_resources();
163         for(int i = 0; i < 3; i++)
164         {
165                 if(images[i]) delete images[i];
166                 images[i] = new BC_Pixmap(parent_window, data[i], PIXMAP_ALPHA);
167         }
168
169         if(w_argument >= 0)
170                 w = w_argument +
171                         margin +
172                         resources->popupmenu_triangle_margin;
173         else
174                 w = get_text_width(MEDIUMFONT, text) +
175                         margin +
176                         resources->popupmenu_triangle_margin;
177
178         h = images[BUTTON_UP]->get_h();
179         return 0;
180 }
181
182 int BC_PopupMenu::calculate_w(int w_argument)
183 {
184         return w_argument +
185                 BC_WindowBase::get_resources()->popupmenu_margin +
186                 BC_WindowBase::get_resources()->popupmenu_triangle_margin;
187 }
188
189 int BC_PopupMenu::calculate_h(VFrame **data)
190 {
191         if(data)
192                 ;
193         else
194         if(BC_WindowBase::get_resources()->popupmenu_images)
195                 data = BC_WindowBase::get_resources()->popupmenu_images;
196         else
197                 data = BC_WindowBase::get_resources()->generic_button_images;
198
199
200         return data[BUTTON_UP]->get_h();
201 }
202
203 int BC_PopupMenu::add_item(BC_MenuItem *item)
204 {
205         menu_popup->add_item(item);
206         return 0;
207 }
208
209 int BC_PopupMenu::remove_item(BC_MenuItem *item)
210 {
211         menu_popup->remove_item(item);
212         return 0;
213 }
214
215 int BC_PopupMenu::del_item(BC_MenuItem *item)
216 {
217         menu_popup->del_item(item);
218         return 0;
219 }
220
221 int BC_PopupMenu::total_items()
222 {
223         return menu_popup->total_menuitems();
224         return 0;
225 }
226
227 BC_MenuItem* BC_PopupMenu::get_item(int i)
228 {
229         return menu_popup->menu_items.values[i];
230 }
231
232 int BC_PopupMenu::draw_title(int flush)
233 {
234         if(!use_title) return 0;
235         BC_Resources *resources = get_resources();
236
237 // Background
238         draw_top_background(parent_window, 0, 0, w, h);
239         draw_3segmenth(0, 0, w, images[status]);
240
241 // Overlay text
242         set_color(get_resources()->popup_title_text);
243         int offset = 0;
244         if(status == BUTTON_DN)
245                 offset = 1;
246         if(!icon)
247         {
248                 set_font(MEDIUMFONT);
249                 char truncated[BCTEXTLEN];
250                 int available_w = get_w() - margin * 2 - resources->popupmenu_triangle_margin;
251                 truncate_text(truncated, text, available_w);
252
253                 BC_WindowBase::draw_center_text(
254                         available_w / 2 + margin + offset,
255                         (int)((float)get_h() / 2 + get_text_ascent(MEDIUMFONT) / 2 - 2) + offset,
256                         truncated);
257         }
258
259         if(icon)
260         {
261                 draw_pixmap(icon,
262                         (get_w() - margin * 2 - resources->popupmenu_triangle_margin) / 2 + margin + offset - icon->get_w() / 2 ,
263                         get_h() / 2 - icon->get_h() / 2 + offset);
264         }
265
266         if( use_title >= 0 )
267                 draw_triangle_down_flat(get_w() - margin - resources->popupmenu_triangle_margin,
268                         get_h() / 2 - TRIANGLE_H / 2, TRIANGLE_W, TRIANGLE_H);
269
270         flash(flush);
271         return 0;
272 }
273
274 int BC_PopupMenu::deactivate()
275 {
276         if(popup_down)
277         {
278                 top_level->active_popup_menu = 0;
279                 popup_down = 0;
280                 menu_popup->deactivate_menu();
281
282                 if(use_title) draw_title(1);    // draw the title
283         }
284         return 0;
285 }
286
287 int BC_PopupMenu::activate_menu()
288 {
289         if(!popup_down)
290         {
291                 int x = this->x;
292                 int y = this->y;
293
294                 top_level->deactivate();
295                 top_level->active_popup_menu = this;
296                 if(!use_title)
297                 {
298                         x = top_level->get_abs_cursor_x(0) - get_w();
299                         y = top_level->get_abs_cursor_y(0) - get_h();
300                         button_press_x = top_level->cursor_x;
301                         button_press_y = top_level->cursor_y;
302                 }
303
304                 if(use_title)
305                 {
306                         Window tempwin;
307                         int new_x, new_y;
308                         XTranslateCoordinates(top_level->display,
309                                 win, top_level->rootwin,
310                                 0, 0, &new_x, &new_y, &tempwin);
311                         menu_popup->activate_menu(new_x, new_y,
312                                 w, h, 0, 1);
313                 }
314                 else
315                         menu_popup->activate_menu(x+3, y+3, w, h, 0, 1);
316                 popup_down = 1;
317                 if(use_title) draw_title(1);
318         }
319         return 0;
320 }
321
322 int BC_PopupMenu::deactivate_menu()
323 {
324         deactivate();
325         return 0;
326 }
327
328
329 int BC_PopupMenu::reposition_window(int x, int y)
330 {
331         BC_WindowBase::reposition_window(x, y);
332         draw_title(0);
333         return 0;
334 }
335
336 int BC_PopupMenu::focus_out_event()
337 {
338         if( popup_down && !get_button_down() &&
339             !cursor_inside() && !menu_popup->cursor_inside() )
340                 deactivate();
341         return 0;
342 }
343
344
345 int BC_PopupMenu::repeat_event(int64_t duration)
346 {
347         if( status == BUTTON_HI && !tooltip_done &&
348                 tooltip_text && tooltip_text[0] != 0 &&
349                 duration == top_level->get_resources()->tooltip_delay )
350         {
351                 show_tooltip();
352                 tooltip_done = 1;
353                 return 1;
354         }
355         return 0;
356 }
357
358 int BC_PopupMenu::button_press_event()
359 {
360         if(get_buttonpress() == 1 &&
361                 is_event_win() &&
362                 use_title)
363         {
364                 top_level->hide_tooltip();
365                 if(status == BUTTON_HI || status == BUTTON_UP) status = BUTTON_DN;
366                 activate_menu();
367                 draw_title(1);
368                 return 1;
369         }
370
371         // Scrolling section
372         if (is_event_win()
373                 && (get_buttonpress() == 4 || get_buttonpress() == 5)
374                 && menu_popup->total_menuitems() > 1 )
375         {
376                 int theval = -1;
377                 for (int i = 0; i < menu_popup->total_menuitems(); i++) {
378                         if (!strcmp(menu_popup->menu_items.values[i]->get_text(),get_text())) {
379                                 theval=i;
380                                 break;
381                         }
382                 }
383
384                 if (theval == -1)                  theval=0;
385                 else if (get_buttonpress() == 4)   theval--;
386                 else if (get_buttonpress() == 5)   theval++;
387
388                 if (theval < 0)
389                         theval=0;
390                 if (theval >= menu_popup->total_menuitems())
391                         theval = menu_popup->total_menuitems() - 1;
392
393                 BC_MenuItem *tmp = menu_popup->menu_items.values[theval];
394                 set_text(tmp->get_text());
395                 if (!tmp->handle_event())
396                         this->handle_event();
397         }
398         if(popup_down)
399         {
400 // Menu is down so dispatch to popup.
401                 menu_popup->dispatch_button_press();
402                 return 1;
403         }
404
405         return 0;
406 }
407
408 int BC_PopupMenu::button_release_event()
409 {
410 // try the title
411         int result = 0;
412
413         if(is_event_win() && use_title)
414         {
415                 hide_tooltip();
416                 if(status == BUTTON_DN)
417                 {
418                         status = BUTTON_HI;
419                         draw_title(1);
420                 }
421         }
422
423         if( !use_title && status == BUTTON_DN ) {
424                 result = 1;
425         }
426         else if(popup_down) {
427 // Menu is down so dispatch to popup.
428                 result = menu_popup->dispatch_button_release();
429         }
430 // released outside popup
431         if( !result && popup_down ) {
432                 deactivate();
433                 result = 1;
434         }
435         hide_tooltip();
436
437         return result;
438 }
439
440 int BC_PopupMenu::translation_event()
441 {
442 //printf("BC_PopupMenu::translation_event 1\n");
443         if(popup_down) menu_popup->dispatch_translation_event();
444         return 0;
445 }
446
447 int BC_PopupMenu::cursor_leave_event()
448 {
449
450         if(status == BUTTON_HI && use_title)
451         {
452                 status = BUTTON_UP;
453                 draw_title(1);
454                 hide_tooltip();
455         }
456
457 // dispatch to popup
458         if( popup_down ) {
459                 if( !get_button_down() && !menu_popup->cursor_inside() )
460                         deactivate_menu();
461                 menu_popup->dispatch_cursor_leave();
462         }
463
464         return 0;
465 }
466
467
468 int BC_PopupMenu::cursor_enter_event()
469 {
470         if(is_event_win() && use_title)
471         {
472                 tooltip_done = 0;
473                 if(top_level->button_down)
474                 {
475                         status = BUTTON_DN;
476                 }
477                 else
478                 if(status == BUTTON_UP)
479                         status = BUTTON_HI;
480                 draw_title(1);
481         }
482
483         return 0;
484 }
485
486 int BC_PopupMenu::cursor_motion_event()
487 {
488         int result = 0;
489
490 // This menu is down.
491         if(popup_down)
492         {
493                 result = menu_popup->dispatch_motion_event();
494         }
495
496         if(!result && use_title && top_level->event_win == win)
497         {
498                 if(highlighted)
499                 {
500                         if(cursor_inside())
501                         {
502                                 highlighted = 0;
503                                 draw_title(1);
504                         }
505                 }
506                 else
507                 {
508                         if(cursor_inside())
509                         {
510                                 highlighted = 1;
511                                 draw_title(1);
512                                 result = 1;
513                         }
514                 }
515         }
516
517         return result;
518 }
519
520 int BC_PopupMenu::drag_start_event()
521 {
522 //printf("BC_PopupMenu::drag_start_event %d\n", popup_down);
523         if(popup_down) return 1;
524         return 0;
525 }
526
527 int BC_PopupMenu::drag_stop_event()
528 {
529         if(popup_down) return 1;
530         return 0;
531 }
532
533 int BC_PopupMenu::drag_motion_event()
534 {
535         if(popup_down) return 1;
536         return 0;
537 }
538
539
540
541
542
543
544