merge from 2.0-ongoing @ 3581
[ardour.git] / libs / gtkmm2ext / gtk_ui.cc
1 /*
2     Copyright (C) 1999-2005 Paul Barton-Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #include <cmath>
22 #include <fcntl.h>
23 #include <signal.h>
24 #include <unistd.h>
25 #include <cerrno>
26 #include <climits>
27 #include <cctype>
28
29 #include <gtkmm.h>
30 #include <pbd/error.h>
31 #include <pbd/touchable.h>
32 #include <pbd/failed_constructor.h>
33 #include <pbd/pthread_utils.h>
34 #include <pbd/stacktrace.h>
35
36 #include <gtkmm2ext/gtk_ui.h>
37 #include <gtkmm2ext/textviewer.h>
38 #include <gtkmm2ext/popup.h>
39 #include <gtkmm2ext/utils.h>
40 #include <gtkmm2ext/window_title.h>
41
42 #include "i18n.h"
43
44 using namespace Gtkmm2ext;
45 using namespace Gtk;
46 using namespace Glib;
47 using namespace PBD;
48 using std::map;
49
50 pthread_t UI::gui_thread;
51 UI       *UI::theGtkUI = 0;
52
53 BaseUI::RequestType Gtkmm2ext::ErrorMessage = BaseUI::new_request_type();
54 BaseUI::RequestType Gtkmm2ext::Quit = BaseUI::new_request_type();
55 BaseUI::RequestType Gtkmm2ext::TouchDisplay = BaseUI::new_request_type();
56 BaseUI::RequestType Gtkmm2ext::StateChange = BaseUI::new_request_type();
57 BaseUI::RequestType Gtkmm2ext::SetTip = BaseUI::new_request_type();
58 BaseUI::RequestType Gtkmm2ext::AddIdle = BaseUI::new_request_type();
59 BaseUI::RequestType Gtkmm2ext::AddTimeout = BaseUI::new_request_type();
60
61 #include <pbd/abstract_ui.cc>  /* instantiate the template */
62
63
64 UI::UI (string namestr, int *argc, char ***argv) 
65         : AbstractUI<UIRequest> (namestr, true)
66 {
67         theMain = new Main (argc, argv);
68
69         _active = false;
70
71         if (!theGtkUI) {
72                 theGtkUI = this;
73                 gui_thread = pthread_self ();
74         } else {
75                 fatal << "duplicate UI requested" << endmsg;
76                 /* NOTREACHED */
77         }
78
79         /* add the pipe to the select/poll loop that GDK does */
80
81         gdk_input_add (signal_pipe[0],
82                        GDK_INPUT_READ,
83                        UI::signal_pipe_callback,
84                        this);
85
86         errors = new TextViewer (850,100);
87         errors->text().set_editable (false); 
88         errors->text().set_name ("ErrorText");
89
90         Glib::set_application_name(namestr);
91
92         WindowTitle title(Glib::get_application_name());
93         title += _("Log");
94         errors->set_title (title.get_string());
95
96         errors->dismiss_button().set_name ("ErrorLogCloseButton");
97         errors->signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), (Window *) errors));
98
99         register_thread (pthread_self(), X_("GUI"));
100
101         //load_rcfile (rcfile);
102 }
103
104 UI::~UI ()
105 {
106 }
107
108
109 bool
110 UI::caller_is_ui_thread ()
111 {
112         return pthread_equal (gui_thread, pthread_self());
113 }
114
115 int
116 UI::load_rcfile (string path, bool themechange)
117 {
118         if (path.length() == 0) {
119                 return -1;
120         }
121
122         if (access (path.c_str(), R_OK)) {
123                 error << "UI: couldn't find rc file \"" 
124                       << path
125                       << '"'
126                       << endmsg;
127                 return -1;
128         }
129         
130         RC rc (path.c_str());
131         // RC::reset_styles (Gtk::Settings::get_default());
132         gtk_rc_reset_styles (gtk_settings_get_default());
133         theme_changed.emit();
134
135         if (themechange) {
136                 return 0; //Don't continue on every time there is a theme change
137         }
138
139         /* have to pack widgets into a toplevel window so that styles will stick */
140
141         Window temp_window (WINDOW_TOPLEVEL);
142         HBox box;
143         Label a_widget1;
144         Label a_widget2;
145         Label a_widget3;
146         Label a_widget4;
147         RefPtr<Gtk::Style> style;
148         RefPtr<TextBuffer> buffer (errors->text().get_buffer());
149
150         box.pack_start (a_widget1);
151         box.pack_start (a_widget2);
152         box.pack_start (a_widget3);
153         box.pack_start (a_widget4);
154
155         error_ptag = buffer->create_tag();
156         error_mtag = buffer->create_tag();
157         fatal_ptag = buffer->create_tag();
158         fatal_mtag = buffer->create_tag();
159         warning_ptag = buffer->create_tag();
160         warning_mtag = buffer->create_tag();
161         info_ptag = buffer->create_tag();
162         info_mtag = buffer->create_tag();
163
164         a_widget1.set_name ("FatalMessage");
165         a_widget1.ensure_style ();
166         style = a_widget1.get_style();
167
168         fatal_ptag->property_font_desc().set_value(style->get_font());
169         fatal_ptag->property_foreground_gdk().set_value(style->get_fg(STATE_ACTIVE));
170         fatal_ptag->property_background_gdk().set_value(style->get_bg(STATE_ACTIVE));
171         fatal_mtag->property_font_desc().set_value(style->get_font());
172         fatal_mtag->property_foreground_gdk().set_value(style->get_fg(STATE_NORMAL));
173         fatal_mtag->property_background_gdk().set_value(style->get_bg(STATE_NORMAL));
174
175         a_widget2.set_name ("ErrorMessage");
176         a_widget2.ensure_style ();
177         style = a_widget2.get_style();
178
179         error_ptag->property_font_desc().set_value(style->get_font());
180         error_ptag->property_foreground_gdk().set_value(style->get_fg(STATE_ACTIVE));
181         error_ptag->property_background_gdk().set_value(style->get_bg(STATE_ACTIVE));
182         error_mtag->property_font_desc().set_value(style->get_font());
183         error_mtag->property_foreground_gdk().set_value(style->get_fg(STATE_NORMAL));
184         error_mtag->property_background_gdk().set_value(style->get_bg(STATE_NORMAL));
185
186         a_widget3.set_name ("WarningMessage");
187         a_widget3.ensure_style ();
188         style = a_widget3.get_style();
189
190         warning_ptag->property_font_desc().set_value(style->get_font());
191         warning_ptag->property_foreground_gdk().set_value(style->get_fg(STATE_ACTIVE));
192         warning_ptag->property_background_gdk().set_value(style->get_bg(STATE_ACTIVE));
193         warning_mtag->property_font_desc().set_value(style->get_font());
194         warning_mtag->property_foreground_gdk().set_value(style->get_fg(STATE_NORMAL));
195         warning_mtag->property_background_gdk().set_value(style->get_bg(STATE_NORMAL));
196
197         a_widget4.set_name ("InfoMessage");
198         a_widget4.ensure_style ();
199         style = a_widget4.get_style();
200
201         info_ptag->property_font_desc().set_value(style->get_font());
202         info_ptag->property_foreground_gdk().set_value(style->get_fg(STATE_ACTIVE));
203         info_ptag->property_background_gdk().set_value(style->get_bg(STATE_ACTIVE));
204         info_mtag->property_font_desc().set_value(style->get_font());
205         info_mtag->property_foreground_gdk().set_value(style->get_fg(STATE_NORMAL));
206         info_mtag->property_background_gdk().set_value(style->get_bg(STATE_NORMAL));
207
208         return 0;
209 }
210
211 void
212 UI::run (Receiver &old_receiver)
213 {
214         listen_to (error);
215         listen_to (info);
216         listen_to (warning);
217         listen_to (fatal);
218
219         old_receiver.hangup ();
220         starting ();
221         _active = true; 
222         theMain->run ();
223         _active = false;
224         stopping ();
225         hangup ();
226         return;
227 }
228
229 bool
230 UI::running ()
231 {
232         return _active;
233 }
234
235 void
236 UI::kill ()
237 {
238         if (_active) {
239                 pthread_kill (gui_thread, SIGKILL);
240         } 
241 }
242
243 void
244 UI::quit ()
245 {
246         UIRequest *req = get_request (Quit);
247
248         if (req == 0) {
249                 return;
250         }
251
252         send_request (req);
253 }
254
255 static bool idle_quit ()
256 {
257         Main::quit ();
258         return true;
259 }
260
261 void
262 UI::do_quit ()
263 {
264         if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
265                 Main::quit ();
266         } else {
267                 Glib::signal_idle().connect (sigc::ptr_fun (idle_quit));
268         }
269 }
270
271 void
272 UI::touch_display (Touchable *display)
273 {
274         UIRequest *req = get_request (TouchDisplay);
275
276         if (req == 0) {
277                 return;
278         }
279
280         req->display = display;
281
282         send_request (req);
283 }       
284
285 void
286 UI::set_tip (Widget *w, const gchar *tip, const gchar *hlp)
287 {
288         UIRequest *req = get_request (SetTip);
289
290         if (req == 0) {
291                 return;
292         }
293
294         req->widget = w;
295         req->msg = tip;
296         req->msg2 = hlp;
297
298         send_request (req);
299 }
300
301 void
302 UI::set_state (Widget *w, StateType state)
303 {
304         UIRequest *req = get_request (StateChange);
305         
306         if (req == 0) {
307                 return;
308         }
309
310         req->new_state = state;
311         req->widget = w;
312
313         send_request (req);
314 }
315
316 void
317 UI::idle_add (int (*func)(void *), void *arg)
318 {
319         UIRequest *req = get_request (AddIdle);
320
321         if (req == 0) {
322                 return;
323         }
324
325         req->function = func;
326         req->arg = arg;
327
328         send_request (req);
329 }
330
331 /* END abstract_ui interfaces */
332
333 void
334 UI::signal_pipe_callback (void *arg, int fd, GdkInputCondition cond)
335 {
336         char buf[256];
337         
338         /* flush (nonblocking) pipe */
339         
340         while (read (fd, buf, 256) > 0);
341         
342         ((UI *) arg)->handle_ui_requests ();
343 }
344
345 void
346 UI::do_request (UIRequest* req)
347 {
348         if (req->type == ErrorMessage) {
349
350                 process_error_message (req->chn, req->msg);
351                 free (const_cast<char*>(req->msg)); /* it was strdup'ed */
352                 req->msg = 0; /* don't free it again in the destructor */
353
354         } else if (req->type == Quit) {
355
356                 do_quit ();
357
358         } else if (req->type == CallSlot) {
359
360                 req->slot ();
361
362         } else if (req->type == TouchDisplay) {
363
364                 req->display->touch ();
365                 if (req->display->delete_after_touch()) {
366                         delete req->display;
367                 }
368
369         } else if (req->type == StateChange) {
370
371                 req->widget->set_state (req->new_state);
372
373         } else if (req->type == SetTip) {
374
375                 /* XXX need to figure out how this works */
376
377         } else {
378
379                 error << "GtkUI: unknown request type "
380                       << (int) req->type
381                       << endmsg;
382         }              
383 }
384
385 /*======================================================================
386   Error Display
387   ======================================================================*/
388
389 void
390 UI::receive (Transmitter::Channel chn, const char *str)
391 {
392         if (caller_is_ui_thread()) {
393                 process_error_message (chn, str);
394         } else {
395                 UIRequest* req = get_request (ErrorMessage);
396
397                 if (req == 0) {
398                         return;
399                 }
400
401                 req->chn = chn;
402                 req->msg = strdup (str);
403
404                 send_request (req);
405         }
406 }
407
408 #define OLD_STYLE_ERRORS 1
409
410 void
411 UI::process_error_message (Transmitter::Channel chn, const char *str)
412 {
413         RefPtr<Style> style;
414         RefPtr<TextBuffer::Tag> ptag;
415         RefPtr<TextBuffer::Tag> mtag;
416         const char *prefix;
417         size_t prefix_len;
418         bool fatal_received = false;
419 #ifndef OLD_STYLE_ERRORS
420         PopUp* popup = new PopUp (WIN_POS_CENTER, 0, true);
421 #endif
422
423         switch (chn) {
424         case Transmitter::Fatal:
425                 prefix = "[FATAL]: ";
426                 ptag = fatal_ptag;
427                 mtag = fatal_mtag;
428                 prefix_len = 9;
429                 fatal_received = true;
430                 break;
431         case Transmitter::Error:
432 #if OLD_STYLE_ERRORS
433                 prefix = "[ERROR]: ";
434                 ptag = error_ptag;
435                 mtag = error_mtag;
436                 prefix_len = 9;
437 #else
438                 popup->set_name ("ErrorMessage");
439                 popup->set_text (str);
440                 popup->touch ();
441                 return;
442 #endif
443                 break;
444         case Transmitter::Info:
445 #if OLD_STYLE_ERRORS    
446                 prefix = "[INFO]: ";
447                 ptag = info_ptag;
448                 mtag = info_mtag;
449                 prefix_len = 8;
450 #else
451                 popup->set_name ("InfoMessage");
452                 popup->set_text (str);
453                 popup->touch ();
454                 return;
455 #endif
456
457                 break;
458         case Transmitter::Warning:
459 #if OLD_STYLE_ERRORS
460                 prefix = "[WARNING]: ";
461                 ptag = warning_ptag;
462                 mtag = warning_mtag;
463                 prefix_len = 11;
464 #else
465                 popup->set_name ("WarningMessage");
466                 popup->set_text (str);
467                 popup->touch ();
468                 return;
469 #endif
470                 break;
471         default:
472                 /* no choice but to use text/console output here */
473                 cerr << "programmer error in UI::check_error_messages (channel = " << chn << ")\n";
474                 ::exit (1);
475         }
476         
477         errors->text().get_buffer()->begin_user_action();
478
479         if (fatal_received) {
480                 handle_fatal (str);
481         } else {
482                 
483                 display_message (prefix, prefix_len, ptag, mtag, str);
484                 
485                 if (!errors->is_visible()) {
486                         toggle_errors();
487                 }
488         }
489
490         errors->text().get_buffer()->end_user_action();
491 }
492
493 void
494 UI::toggle_errors ()
495 {
496         if (!errors->is_visible()) {
497                 errors->set_position (WIN_POS_MOUSE);
498                 errors->show ();
499         } else {
500                 errors->hide ();
501         }
502 }
503
504 void
505 UI::display_message (const char *prefix, gint prefix_len, RefPtr<TextBuffer::Tag> ptag, RefPtr<TextBuffer::Tag> mtag, const char *msg)
506 {
507         RefPtr<TextBuffer> buffer (errors->text().get_buffer());
508
509         buffer->insert_with_tag(buffer->end(), prefix, ptag);
510         buffer->insert_with_tag(buffer->end(), msg, mtag);
511         buffer->insert_with_tag(buffer->end(), "\n", mtag);
512
513         errors->scroll_to_bottom ();
514 }       
515
516 void
517 UI::handle_fatal (const char *message)
518 {
519         Window win (WINDOW_POPUP);
520         VBox packer;
521         Label label (message);
522         Button quit (_("Press To Exit"));
523
524         win.set_default_size (400, 100);
525         
526         string title;
527         title = name();
528         title += ": Fatal Error";
529         win.set_title (title);
530
531         win.set_position (WIN_POS_MOUSE);
532         win.add (packer);
533
534         packer.pack_start (label, true, true);
535         packer.pack_start (quit, false, false);
536         quit.signal_clicked().connect(mem_fun(*this,&UI::quit));
537         
538         win.show_all ();
539         win.set_modal (true);
540
541         theMain->run ();
542         
543         exit (1);
544 }
545
546 void
547 UI::popup_error (const char *text)
548 {
549         PopUp *pup;
550
551         if (!caller_is_ui_thread()) {
552                 error << "non-UI threads can't use UI::popup_error" 
553                       << endmsg;
554                 return;
555         }
556         
557         pup = new PopUp (WIN_POS_MOUSE, 0, true);
558         pup->set_text (text);
559         pup->touch ();
560 }
561
562 #ifdef GTKOSX
563 extern "C" {
564         int gdk_quartz_in_carbon_menu_event_handler ();
565 }
566 #endif
567
568 void
569 UI::flush_pending ()
570 {
571 #ifdef GTKOSX
572         /* as of february 11th 2008, gtk/osx has a problem in that mac menu events
573            are handled using Carbon with an "internal" event handling system that 
574            doesn't pass things back to the glib/gtk main loop. this makes
575            gtk_main_iteration() block if we call it while in a menu event handler 
576            because glib gets confused and thinks there are two threads running
577            g_main_poll_func(). 
578
579            this hack (relies on code in gtk2_ardour/sync-menu.c) works
580            around that.
581         */
582
583         if (gdk_quartz_in_carbon_menu_event_handler()) {
584                 return;
585         }
586 #endif
587         if (!caller_is_ui_thread()) {
588                 error << "non-UI threads cannot call UI::flush_pending()"
589                       << endmsg;
590                 return;
591         }
592
593         gtk_main_iteration();
594
595         while (gtk_events_pending()) {
596                 gtk_main_iteration();
597         }
598 }
599
600 bool
601 UI::just_hide_it (GdkEventAny *ev, Window *win)
602 {
603         win->hide ();
604         return true;
605 }
606
607 Gdk::Color
608 UI::get_color (const string& prompt, bool& picked, const Gdk::Color* initial)
609 {
610         Gdk::Color color;
611
612         ColorSelectionDialog color_dialog (prompt);
613
614         color_dialog.set_modal (true);
615         color_dialog.get_cancel_button()->signal_clicked().connect (bind (mem_fun (*this, &UI::color_selection_done), false));
616         color_dialog.get_ok_button()->signal_clicked().connect (bind (mem_fun (*this, &UI::color_selection_done), true));
617         color_dialog.signal_delete_event().connect (mem_fun (*this, &UI::color_selection_deleted));
618
619         if (initial) {
620                 color_dialog.get_colorsel()->set_current_color (*initial);
621         }
622
623         color_dialog.show_all ();
624         color_picked = false;
625         picked = false;
626
627         Main::run();
628
629         color_dialog.hide_all ();
630
631         if (color_picked) {
632                 Gdk::Color f_rgba = color_dialog.get_colorsel()->get_current_color ();
633                 color.set_red(f_rgba.get_red());
634                 color.set_green(f_rgba.get_green());
635                 color.set_blue(f_rgba.get_blue());
636
637                 picked = true;
638         }
639
640         return color;
641 }
642
643 void
644 UI::color_selection_done (bool status)
645 {
646         color_picked = status;
647         Main::quit ();
648 }
649
650 bool
651 UI::color_selection_deleted (GdkEventAny *ev)
652 {
653         Main::quit ();
654         return true;
655 }