initial commit
[goodguy/history.git] / cinelerra-5.0 / 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, w, -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 = 0;
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, 0);
212         return 0;
213 }
214
215 int BC_PopupMenu::total_items()
216 {
217         return menu_popup->total_menuitems();
218         return 0;
219 }
220
221 BC_MenuItem* BC_PopupMenu::get_item(int i)
222 {
223         return menu_popup->menu_items.values[i];
224 }
225
226 int BC_PopupMenu::draw_title(int flush)
227 {
228         if(!use_title) return 0;
229         BC_Resources *resources = get_resources();
230
231 // Background
232         draw_top_background(parent_window, 0, 0, w, h);
233         draw_3segmenth(0, 0, w, images[status]);
234
235 // Overlay text
236         set_color(get_resources()->popup_title_text);
237         int offset = 0;
238         if(status == BUTTON_DN)
239                 offset = 1;
240         if(!icon)
241         {
242                 set_font(MEDIUMFONT);
243                 char truncated[BCTEXTLEN];
244                 int available_w = get_w() - margin * 2 - resources->popupmenu_triangle_margin;
245                 truncate_text(truncated, text, available_w);
246
247                 BC_WindowBase::draw_center_text(
248                         available_w / 2 + margin + offset,
249                         (int)((float)get_h() / 2 + get_text_ascent(MEDIUMFONT) / 2 - 2) + offset,
250                         truncated);
251         }
252
253         if(icon)
254         {
255                 draw_pixmap(icon,
256                         (get_w() - margin * 2 - resources->popupmenu_triangle_margin) / 2 + margin + offset - icon->get_w() / 2 ,
257                         get_h() / 2 - icon->get_h() / 2 + offset);
258         }
259
260         draw_triangle_down_flat(get_w() - margin - resources->popupmenu_triangle_margin,
261                 get_h() / 2 - TRIANGLE_H / 2,
262                 TRIANGLE_W, TRIANGLE_H);
263
264         flash(flush);
265         return 0;
266 }
267
268 int BC_PopupMenu::deactivate()
269 {
270         if(popup_down)
271         {
272                 top_level->active_popup_menu = 0;
273                 popup_down = 0;
274                 menu_popup->deactivate_menu();
275
276                 if(use_title) draw_title(1);    // draw the title
277         }
278         return 0;
279 }
280
281 int BC_PopupMenu::activate_menu()
282 {
283         if(!popup_down)
284         {
285                 int x = this->x;
286                 int y = this->y;
287
288                 top_level->deactivate();
289                 top_level->active_popup_menu = this;
290                 if(!use_title)
291                 {
292                         x = top_level->get_abs_cursor_x(0) - get_w();
293                         y = top_level->get_abs_cursor_y(0) - get_h();
294                         button_press_x = top_level->cursor_x;
295                         button_press_y = top_level->cursor_y;
296                 }
297
298                 button_releases = 0;
299                 if(use_title)
300                 {
301                         Window tempwin;
302                         int new_x, new_y;
303                         XTranslateCoordinates(top_level->display,
304                                 win, top_level->rootwin,
305                                 0, 0, &new_x, &new_y, &tempwin);
306                         menu_popup->activate_menu(new_x, new_y,
307                                 w, h, 0, 1);
308                 }
309                 else
310                         menu_popup->activate_menu(x, y, w, h, 0, 1);
311                 popup_down = 1;
312                 if(use_title) draw_title(1);
313         }
314         return 0;
315 }
316
317 int BC_PopupMenu::deactivate_menu()
318 {
319         deactivate();
320         return 0;
321 }
322
323
324 int BC_PopupMenu::reposition_window(int x, int y)
325 {
326         BC_WindowBase::reposition_window(x, y);
327         draw_title(0);
328         return 0;
329 }
330
331 int BC_PopupMenu::focus_out_event()
332 {
333         if( popup_down && !menu_popup->cursor_inside() )
334                 deactivate();
335         return 0;
336 }
337
338
339 int BC_PopupMenu::repeat_event(int64_t duration)
340 {
341         if( status == BUTTON_HI && !tooltip_done &&
342                 tooltip_text && tooltip_text[0] != 0 &&
343                 duration == top_level->get_resources()->tooltip_delay )
344         {
345                 show_tooltip();
346                 tooltip_done = 1;
347                 return 1;
348         }
349         return 0;
350 }
351
352 int BC_PopupMenu::button_press_event()
353 {
354         if(get_buttonpress() == 1 &&
355                 is_event_win() &&
356                 use_title)
357         {
358                 top_level->hide_tooltip();
359                 if(status == BUTTON_HI || status == BUTTON_UP) status = BUTTON_DN;
360                 activate_menu();
361                 draw_title(1);
362                 return 1;
363         }
364
365         // Scrolling section
366         if (is_event_win()
367                 && (get_buttonpress() == 4 || get_buttonpress() == 5)
368                 && menu_popup->total_menuitems() > 1 )
369         {
370                 int theval = -1;
371                 for (int i = 0; i < menu_popup->total_menuitems(); i++) {
372                         if (!strcmp(menu_popup->menu_items.values[i]->get_text(),get_text())) {
373                                 theval=i;
374                                 break;
375                         }
376                 }
377
378                 if (theval == -1)                  theval=0;
379                 else if (get_buttonpress() == 4)   theval--;
380                 else if (get_buttonpress() == 5)   theval++;
381
382                 if (theval < 0)
383                         theval=0;
384                 if (theval >= menu_popup->total_menuitems())
385                         theval = menu_popup->total_menuitems() - 1;
386
387                 BC_MenuItem *tmp = menu_popup->menu_items.values[theval];
388                 set_text(tmp->get_text());
389                 if (!tmp->handle_event())
390                         this->handle_event();
391         }
392         if(popup_down)
393         {
394 // Menu is down so dispatch to popup.
395                 menu_popup->dispatch_button_press();
396                 return 1;
397         }
398
399         return 0;
400 }
401
402 int BC_PopupMenu::button_release_event()
403 {
404 // try the title
405         int result = 0;
406
407         button_releases++;
408
409         if(is_event_win() && use_title)
410         {
411                 hide_tooltip();
412                 if(status == BUTTON_DN)
413                 {
414                         status = BUTTON_HI;
415                         draw_title(1);
416                 }
417         }
418
419         if(popup_down)
420         {
421 // Menu is down so dispatch to popup.
422                 result = menu_popup->dispatch_button_release();
423         }
424
425         if(popup_down && button_releases >= 2)
426         {
427                 deactivate();
428         }
429
430         if(!result && use_title && cursor_inside() && is_event_win())
431         {
432                 hide_tooltip();
433                 result = 1;
434         }
435         else
436         if(!result && !use_title && popup_down && button_releases < 2)
437         {
438                 result = 1;
439         }
440
441
442         if(!result && popup_down)
443         {
444 // Button was released outside any menu.
445                 deactivate();
446                 result = 1;
447         }
448
449         return result;
450
451
452
453
454
455
456
457
458
459
460         if(popup_down)
461         {
462 // Menu is down so dispatch to popup.
463                 result = menu_popup->dispatch_button_release();
464         }
465
466         if(!result && use_title && cursor_inside() && top_level->event_win == win)
467         {
468 // Inside title
469                 if(button_releases >= 2)
470                 {
471                         highlighted = 1;
472                         deactivate();
473                 }
474                 result = 1;
475         }
476         else
477         if(!result && !use_title && button_releases < 2)
478         {
479 // First release outside a floating menu
480 // Released outside a fictitious title area
481 //              if(top_level->cursor_x < button_press_x - 5 ||
482 //                      top_level->cursor_y < button_press_y - 5 ||
483 //                      top_level->cursor_x > button_press_x + 5 ||
484 //                      top_level->cursor_y > button_press_y + 5)
485                         deactivate();
486                 result = 1;
487         }
488
489         return result;
490 }
491
492 int BC_PopupMenu::translation_event()
493 {
494 //printf("BC_PopupMenu::translation_event 1\n");
495         if(popup_down) menu_popup->dispatch_translation_event();
496         return 0;
497 }
498
499 int BC_PopupMenu::cursor_leave_event()
500 {
501
502         if(status == BUTTON_HI && use_title)
503         {
504                 status = BUTTON_UP;
505                 draw_title(1);
506                 hide_tooltip();
507         }
508
509 // dispatch to popup
510         if(popup_down)
511         {
512                 menu_popup->dispatch_cursor_leave();
513         }
514
515         return 0;
516 }
517
518
519 int BC_PopupMenu::cursor_enter_event()
520 {
521         if(is_event_win() && use_title)
522         {
523                 tooltip_done = 0;
524                 if(top_level->button_down)
525                 {
526                         status = BUTTON_DN;
527                 }
528                 else
529                 if(status == BUTTON_UP)
530                         status = BUTTON_HI;
531                 draw_title(1);
532         }
533
534         return 0;
535 }
536
537 int BC_PopupMenu::cursor_motion_event()
538 {
539         int result = 0;
540
541 // This menu is down.
542         if(popup_down)
543         {
544                 result = menu_popup->dispatch_motion_event();
545         }
546
547         if(!result && use_title && top_level->event_win == win)
548         {
549                 if(highlighted)
550                 {
551                         if(cursor_inside())
552                         {
553                                 highlighted = 0;
554                                 draw_title(1);
555                         }
556                 }
557                 else
558                 {
559                         if(cursor_inside())
560                         {
561                                 highlighted = 1;
562                                 draw_title(1);
563                                 result = 1;
564                         }
565                 }
566         }
567
568         return result;
569 }
570
571 int BC_PopupMenu::drag_start_event()
572 {
573 //printf("BC_PopupMenu::drag_start_event %d\n", popup_down);
574         if(popup_down) return 1;
575         return 0;
576 }
577
578 int BC_PopupMenu::drag_stop_event()
579 {
580         if(popup_down) return 1;
581         return 0;
582 }
583
584 int BC_PopupMenu::drag_motion_event()
585 {
586         if(popup_down) return 1;
587         return 0;
588 }
589
590
591
592
593
594
595