cefaea7f48e22be0e9667cb4f376d84e794d5509
[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, 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         if( use_title >= 0 )
261                 draw_triangle_down_flat(get_w() - margin - resources->popupmenu_triangle_margin,
262                         get_h() / 2 - TRIANGLE_H / 2, 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 // back off x,y just a bit so the menu doesnt deactivate without motion
311                         menu_popup->activate_menu(x-10, y-10, w, h, 0, 1);
312                 popup_down = 1;
313                 if(use_title) draw_title(1);
314         }
315         return 0;
316 }
317
318 int BC_PopupMenu::deactivate_menu()
319 {
320         deactivate();
321         return 0;
322 }
323
324
325 int BC_PopupMenu::reposition_window(int x, int y)
326 {
327         BC_WindowBase::reposition_window(x, y);
328         draw_title(0);
329         return 0;
330 }
331
332 int BC_PopupMenu::focus_out_event()
333 {
334         if( popup_down && !get_button_down() &&
335             !cursor_inside() && !menu_popup->cursor_inside() )
336                 deactivate();
337         return 0;
338 }
339
340
341 int BC_PopupMenu::repeat_event(int64_t duration)
342 {
343         if( status == BUTTON_HI && !tooltip_done &&
344                 tooltip_text && tooltip_text[0] != 0 &&
345                 duration == top_level->get_resources()->tooltip_delay )
346         {
347                 show_tooltip();
348                 tooltip_done = 1;
349                 return 1;
350         }
351         return 0;
352 }
353
354 int BC_PopupMenu::button_press_event()
355 {
356         if(get_buttonpress() == 1 &&
357                 is_event_win() &&
358                 use_title)
359         {
360                 top_level->hide_tooltip();
361                 if(status == BUTTON_HI || status == BUTTON_UP) status = BUTTON_DN;
362                 activate_menu();
363                 draw_title(1);
364                 return 1;
365         }
366
367         // Scrolling section
368         if (is_event_win()
369                 && (get_buttonpress() == 4 || get_buttonpress() == 5)
370                 && menu_popup->total_menuitems() > 1 )
371         {
372                 int theval = -1;
373                 for (int i = 0; i < menu_popup->total_menuitems(); i++) {
374                         if (!strcmp(menu_popup->menu_items.values[i]->get_text(),get_text())) {
375                                 theval=i;
376                                 break;
377                         }
378                 }
379
380                 if (theval == -1)                  theval=0;
381                 else if (get_buttonpress() == 4)   theval--;
382                 else if (get_buttonpress() == 5)   theval++;
383
384                 if (theval < 0)
385                         theval=0;
386                 if (theval >= menu_popup->total_menuitems())
387                         theval = menu_popup->total_menuitems() - 1;
388
389                 BC_MenuItem *tmp = menu_popup->menu_items.values[theval];
390                 set_text(tmp->get_text());
391                 if (!tmp->handle_event())
392                         this->handle_event();
393         }
394         if(popup_down)
395         {
396 // Menu is down so dispatch to popup.
397                 menu_popup->dispatch_button_press();
398                 return 1;
399         }
400
401         return 0;
402 }
403
404 int BC_PopupMenu::button_release_event()
405 {
406 // try the title
407         int result = 0;
408
409         button_releases++;
410
411         if(is_event_win() && use_title)
412         {
413                 hide_tooltip();
414                 if(status == BUTTON_DN)
415                 {
416                         status = BUTTON_HI;
417                         draw_title(1);
418                 }
419         }
420
421         if( !use_title && status == BUTTON_DN ) {
422                 result = 1;
423         }
424         else if(popup_down)
425         {
426 // Menu is down so dispatch to popup.
427                 result = menu_popup->dispatch_button_release();
428         }
429
430         if(popup_down && button_releases >= 2)
431         {
432                 deactivate();
433         }
434
435         if(!result && use_title && cursor_inside() && is_event_win())
436         {
437                 hide_tooltip();
438                 result = 1;
439         }
440         else
441         if(!result && !use_title && popup_down && button_releases < 2)
442         {
443                 result = 1;
444         }
445
446
447         if(!result && popup_down)
448         {
449 // Button was released outside any menu.
450                 deactivate();
451                 result = 1;
452         }
453
454         return result;
455
456
457
458
459
460
461
462
463
464
465         if(popup_down)
466         {
467 // Menu is down so dispatch to popup.
468                 result = menu_popup->dispatch_button_release();
469         }
470
471         if(!result && use_title && cursor_inside() && top_level->event_win == win)
472         {
473 // Inside title
474                 if(button_releases >= 2)
475                 {
476                         highlighted = 1;
477                         deactivate();
478                 }
479                 result = 1;
480         }
481         else
482         if(!result && !use_title && button_releases < 2)
483         {
484 // First release outside a floating menu
485 // Released outside a fictitious title area
486 //              if(top_level->cursor_x < button_press_x - 5 ||
487 //                      top_level->cursor_y < button_press_y - 5 ||
488 //                      top_level->cursor_x > button_press_x + 5 ||
489 //                      top_level->cursor_y > button_press_y + 5)
490                         deactivate();
491                 result = 1;
492         }
493
494         return result;
495 }
496
497 int BC_PopupMenu::translation_event()
498 {
499 //printf("BC_PopupMenu::translation_event 1\n");
500         if(popup_down) menu_popup->dispatch_translation_event();
501         return 0;
502 }
503
504 int BC_PopupMenu::cursor_leave_event()
505 {
506
507         if(status == BUTTON_HI && use_title)
508         {
509                 status = BUTTON_UP;
510                 draw_title(1);
511                 hide_tooltip();
512         }
513
514 // dispatch to popup
515         if( popup_down ) {
516                 if( !get_button_down() && !menu_popup->cursor_inside() )
517                         deactivate_menu();
518                 menu_popup->dispatch_cursor_leave();
519         }
520
521         return 0;
522 }
523
524
525 int BC_PopupMenu::cursor_enter_event()
526 {
527         if(is_event_win() && use_title)
528         {
529                 tooltip_done = 0;
530                 if(top_level->button_down)
531                 {
532                         status = BUTTON_DN;
533                 }
534                 else
535                 if(status == BUTTON_UP)
536                         status = BUTTON_HI;
537                 draw_title(1);
538         }
539
540         return 0;
541 }
542
543 int BC_PopupMenu::cursor_motion_event()
544 {
545         int result = 0;
546
547 // This menu is down.
548         if(popup_down)
549         {
550                 result = menu_popup->dispatch_motion_event();
551         }
552
553         if(!result && use_title && top_level->event_win == win)
554         {
555                 if(highlighted)
556                 {
557                         if(cursor_inside())
558                         {
559                                 highlighted = 0;
560                                 draw_title(1);
561                         }
562                 }
563                 else
564                 {
565                         if(cursor_inside())
566                         {
567                                 highlighted = 1;
568                                 draw_title(1);
569                                 result = 1;
570                         }
571                 }
572         }
573
574         return result;
575 }
576
577 int BC_PopupMenu::drag_start_event()
578 {
579 //printf("BC_PopupMenu::drag_start_event %d\n", popup_down);
580         if(popup_down) return 1;
581         return 0;
582 }
583
584 int BC_PopupMenu::drag_stop_event()
585 {
586         if(popup_down) return 1;
587         return 0;
588 }
589
590 int BC_PopupMenu::drag_motion_event()
591 {
592         if(popup_down) return 1;
593         return 0;
594 }
595
596
597
598
599
600
601