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