change popupmenu behaviour, few minor fixups
[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 // back off x,y just a bit so the menu doesnt deactivate without motion
316                         menu_popup->activate_menu(x-10, y-10, w, h, 0, 1);
317                 popup_down = 1;
318                 if(use_title) draw_title(1);
319         }
320         return 0;
321 }
322
323 int BC_PopupMenu::deactivate_menu()
324 {
325         deactivate();
326         return 0;
327 }
328
329
330 int BC_PopupMenu::reposition_window(int x, int y)
331 {
332         BC_WindowBase::reposition_window(x, y);
333         draw_title(0);
334         return 0;
335 }
336
337 int BC_PopupMenu::focus_out_event()
338 {
339         if( popup_down && !get_button_down() &&
340             !cursor_inside() && !menu_popup->cursor_inside() )
341                 deactivate();
342         return 0;
343 }
344
345
346 int BC_PopupMenu::repeat_event(int64_t duration)
347 {
348         if( status == BUTTON_HI && !tooltip_done &&
349                 tooltip_text && tooltip_text[0] != 0 &&
350                 duration == top_level->get_resources()->tooltip_delay )
351         {
352                 show_tooltip();
353                 tooltip_done = 1;
354                 return 1;
355         }
356         return 0;
357 }
358
359 int BC_PopupMenu::button_press_event()
360 {
361         if(get_buttonpress() == 1 &&
362                 is_event_win() &&
363                 use_title)
364         {
365                 top_level->hide_tooltip();
366                 if(status == BUTTON_HI || status == BUTTON_UP) status = BUTTON_DN;
367                 activate_menu();
368                 draw_title(1);
369                 return 1;
370         }
371
372         // Scrolling section
373         if (is_event_win()
374                 && (get_buttonpress() == 4 || get_buttonpress() == 5)
375                 && menu_popup->total_menuitems() > 1 )
376         {
377                 int theval = -1;
378                 for (int i = 0; i < menu_popup->total_menuitems(); i++) {
379                         if (!strcmp(menu_popup->menu_items.values[i]->get_text(),get_text())) {
380                                 theval=i;
381                                 break;
382                         }
383                 }
384
385                 if (theval == -1)                  theval=0;
386                 else if (get_buttonpress() == 4)   theval--;
387                 else if (get_buttonpress() == 5)   theval++;
388
389                 if (theval < 0)
390                         theval=0;
391                 if (theval >= menu_popup->total_menuitems())
392                         theval = menu_popup->total_menuitems() - 1;
393
394                 BC_MenuItem *tmp = menu_popup->menu_items.values[theval];
395                 set_text(tmp->get_text());
396                 if (!tmp->handle_event())
397                         this->handle_event();
398         }
399         if(popup_down)
400         {
401 // Menu is down so dispatch to popup.
402                 menu_popup->dispatch_button_press();
403                 return 1;
404         }
405
406         return 0;
407 }
408
409 int BC_PopupMenu::button_release_event()
410 {
411 // try the title
412         int result = 0;
413
414         if(is_event_win() && use_title)
415         {
416                 hide_tooltip();
417                 if(status == BUTTON_DN)
418                 {
419                         status = BUTTON_HI;
420                         draw_title(1);
421                 }
422         }
423
424         if( !use_title && status == BUTTON_DN ) {
425                 result = 1;
426         }
427         else if(popup_down) {
428 // Menu is down so dispatch to popup.
429                 result = menu_popup->dispatch_button_release();
430         }
431 // released outside popup
432         if( !result && popup_down ) {
433                 deactivate();
434                 result = 1;
435         }
436         hide_tooltip();
437
438         return result;
439 }
440
441 int BC_PopupMenu::translation_event()
442 {
443 //printf("BC_PopupMenu::translation_event 1\n");
444         if(popup_down) menu_popup->dispatch_translation_event();
445         return 0;
446 }
447
448 int BC_PopupMenu::cursor_leave_event()
449 {
450
451         if(status == BUTTON_HI && use_title)
452         {
453                 status = BUTTON_UP;
454                 draw_title(1);
455                 hide_tooltip();
456         }
457
458 // dispatch to popup
459         if( popup_down ) {
460                 if( !get_button_down() && !menu_popup->cursor_inside() )
461                         deactivate_menu();
462                 menu_popup->dispatch_cursor_leave();
463         }
464
465         return 0;
466 }
467
468
469 int BC_PopupMenu::cursor_enter_event()
470 {
471         if(is_event_win() && use_title)
472         {
473                 tooltip_done = 0;
474                 if(top_level->button_down)
475                 {
476                         status = BUTTON_DN;
477                 }
478                 else
479                 if(status == BUTTON_UP)
480                         status = BUTTON_HI;
481                 draw_title(1);
482         }
483
484         return 0;
485 }
486
487 int BC_PopupMenu::cursor_motion_event()
488 {
489         int result = 0;
490
491 // This menu is down.
492         if(popup_down)
493         {
494                 result = menu_popup->dispatch_motion_event();
495         }
496
497         if(!result && use_title && top_level->event_win == win)
498         {
499                 if(highlighted)
500                 {
501                         if(cursor_inside())
502                         {
503                                 highlighted = 0;
504                                 draw_title(1);
505                         }
506                 }
507                 else
508                 {
509                         if(cursor_inside())
510                         {
511                                 highlighted = 1;
512                                 draw_title(1);
513                                 result = 1;
514                         }
515                 }
516         }
517
518         return result;
519 }
520
521 int BC_PopupMenu::drag_start_event()
522 {
523 //printf("BC_PopupMenu::drag_start_event %d\n", popup_down);
524         if(popup_down) return 1;
525         return 0;
526 }
527
528 int BC_PopupMenu::drag_stop_event()
529 {
530         if(popup_down) return 1;
531         return 0;
532 }
533
534 int BC_PopupMenu::drag_motion_event()
535 {
536         if(popup_down) return 1;
537         return 0;
538 }
539
540
541
542
543
544
545