New theme manager, with option to select between dark and light theme. Cleanups...
[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         tips = new Tooltips;
69
70         _active = false;
71
72         if (!theGtkUI) {
73                 theGtkUI = this;
74                 gui_thread = pthread_self ();
75         } else {
76                 fatal << "duplicate UI requested" << endmsg;
77                 /* NOTREACHED */
78         }
79
80         /* add the pipe to the select/poll loop that GDK does */
81
82         gdk_input_add (signal_pipe[0],
83                        GDK_INPUT_READ,
84                        UI::signal_pipe_callback,
85                        this);
86
87         errors = new TextViewer (850,100);
88         errors->text().set_editable (false); 
89         errors->text().set_name ("ErrorText");
90
91         Glib::set_application_name(namestr);
92
93         WindowTitle title(Glib::get_application_name());
94         title += _("Log");
95         errors->set_title (title.get_string());
96
97         errors->dismiss_button().set_name ("ErrorLogCloseButton");
98         errors->signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), (Window *) errors));
99
100         register_thread (pthread_self(), X_("GUI"));
101
102         //load_rcfile (rcfile);
103 }
104
105 UI::~UI ()
106 {
107 }
108
109
110 bool
111 UI::caller_is_ui_thread ()
112 {
113         return pthread_equal (gui_thread, pthread_self());
114 }
115
116 int
117 UI::load_rcfile (string path)
118 {
119         if (path.length() == 0) {
120                 return -1;
121         }
122
123         if (access (path.c_str(), R_OK)) {
124                 error << "UI: couldn't find rc file \"" 
125                       << path
126                       << '"'
127                       << endmsg;
128                 return -1;
129         }
130         
131         RC rc (path.c_str());
132         RC::reset_styles(Gtk::Settings::get_default());
133         return 0;
134 }
135
136 void
137 UI::run (Receiver &old_receiver)
138 {
139         listen_to (error);
140         listen_to (info);
141         listen_to (warning);
142         listen_to (fatal);
143
144         old_receiver.hangup ();
145         starting ();
146         _active = true; 
147         theMain->run ();
148         _active = false;
149         stopping ();
150         hangup ();
151         return;
152 }
153
154 bool
155 UI::running ()
156 {
157         return _active;
158 }
159
160 void
161 UI::kill ()
162 {
163         if (_active) {
164                 pthread_kill (gui_thread, SIGKILL);
165         } 
166 }
167
168 void
169 UI::quit ()
170 {
171         UIRequest *req = get_request (Quit);
172
173         if (req == 0) {
174                 return;
175         }
176
177         send_request (req);
178 }
179
180 static bool idle_quit ()
181 {
182         Main::quit ();
183         return true;
184 }
185
186 void
187 UI::do_quit ()
188 {
189         if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
190                 Main::quit ();
191         } else {
192                 Glib::signal_idle().connect (sigc::ptr_fun (idle_quit));
193         }
194 }
195
196 void
197 UI::touch_display (Touchable *display)
198 {
199         UIRequest *req = get_request (TouchDisplay);
200
201         if (req == 0) {
202                 return;
203         }
204
205         req->display = display;
206
207         send_request (req);
208 }       
209
210 void
211 UI::set_tip (Widget *w, const gchar *tip, const gchar *hlp)
212 {
213         UIRequest *req = get_request (SetTip);
214
215         if (req == 0) {
216                 return;
217         }
218
219         req->widget = w;
220         req->msg = tip;
221         req->msg2 = hlp;
222
223         send_request (req);
224 }
225
226 void
227 UI::set_state (Widget *w, StateType state)
228 {
229         UIRequest *req = get_request (StateChange);
230         
231         if (req == 0) {
232                 return;
233         }
234
235         req->new_state = state;
236         req->widget = w;
237
238         send_request (req);
239 }
240
241 void
242 UI::idle_add (int (*func)(void *), void *arg)
243 {
244         UIRequest *req = get_request (AddIdle);
245
246         if (req == 0) {
247                 return;
248         }
249
250         req->function = func;
251         req->arg = arg;
252
253         send_request (req);
254 }
255
256 /* END abstract_ui interfaces */
257
258 void
259 UI::signal_pipe_callback (void *arg, int fd, GdkInputCondition cond)
260 {
261         char buf[256];
262         
263         /* flush (nonblocking) pipe */
264         
265         while (read (fd, buf, 256) > 0);
266         
267         ((UI *) arg)->handle_ui_requests ();
268 }
269
270 void
271 UI::do_request (UIRequest* req)
272 {
273         if (req->type == ErrorMessage) {
274
275                 process_error_message (req->chn, req->msg);
276                 free (const_cast<char*>(req->msg)); /* it was strdup'ed */
277                 req->msg = 0; /* don't free it again in the destructor */
278
279         } else if (req->type == Quit) {
280
281                 do_quit ();
282
283         } else if (req->type == CallSlot) {
284
285                 req->slot ();
286
287         } else if (req->type == TouchDisplay) {
288
289                 req->display->touch ();
290                 if (req->display->delete_after_touch()) {
291                         delete req->display;
292                 }
293
294         } else if (req->type == StateChange) {
295
296                 req->widget->set_state (req->new_state);
297
298         } else if (req->type == SetTip) {
299
300                 /* XXX need to figure out how this works */
301
302         } else {
303
304                 error << "GtkUI: unknown request type "
305                       << (int) req->type
306                       << endmsg;
307         }              
308 }
309
310 /*======================================================================
311   Error Display
312   ======================================================================*/
313
314 void
315 UI::receive (Transmitter::Channel chn, const char *str)
316 {
317         if (caller_is_ui_thread()) {
318                 process_error_message (chn, str);
319         } else {
320                 UIRequest* req = get_request (ErrorMessage);
321
322                 if (req == 0) {
323                         return;
324                 }
325
326                 req->chn = chn;
327                 req->msg = strdup (str);
328
329                 send_request (req);
330         }
331 }
332
333 #define OLD_STYLE_ERRORS 1
334
335 void
336 UI::process_error_message (Transmitter::Channel chn, const char *str)
337 {
338         RefPtr<Style> style;
339         RefPtr<TextBuffer::Tag> ptag;
340         RefPtr<TextBuffer::Tag> mtag;
341         char *prefix;
342         size_t prefix_len;
343         bool fatal_received = false;
344 #ifndef OLD_STYLE_ERRORS
345         PopUp* popup = new PopUp (WIN_POS_CENTER, 0, true);
346 #endif
347
348         switch (chn) {
349         case Transmitter::Fatal:
350                 prefix = "[FATAL]: ";
351                 ptag = fatal_ptag;
352                 mtag = fatal_mtag;
353                 prefix_len = 9;
354                 fatal_received = true;
355                 break;
356         case Transmitter::Error:
357 #if OLD_STYLE_ERRORS
358                 prefix = "[ERROR]: ";
359                 ptag = error_ptag;
360                 mtag = error_mtag;
361                 prefix_len = 9;
362 #else
363                 popup->set_name ("ErrorMessage");
364                 popup->set_text (str);
365                 popup->touch ();
366                 return;
367 #endif
368                 break;
369         case Transmitter::Info:
370 #if OLD_STYLE_ERRORS    
371                 prefix = "[INFO]: ";
372                 ptag = info_ptag;
373                 mtag = info_mtag;
374                 prefix_len = 8;
375 #else
376                 popup->set_name ("InfoMessage");
377                 popup->set_text (str);
378                 popup->touch ();
379                 return;
380 #endif
381
382                 break;
383         case Transmitter::Warning:
384 #if OLD_STYLE_ERRORS
385                 prefix = "[WARNING]: ";
386                 ptag = warning_ptag;
387                 mtag = warning_mtag;
388                 prefix_len = 11;
389 #else
390                 popup->set_name ("WarningMessage");
391                 popup->set_text (str);
392                 popup->touch ();
393                 return;
394 #endif
395                 break;
396         default:
397                 /* no choice but to use text/console output here */
398                 cerr << "programmer error in UI::check_error_messages (channel = " << chn << ")\n";
399                 ::exit (1);
400         }
401         
402         errors->text().get_buffer()->begin_user_action();
403
404         if (fatal_received) {
405                 handle_fatal (str);
406         } else {
407                 
408                 display_message (prefix, prefix_len, ptag, mtag, str);
409                 
410                 if (!errors->is_visible()) {
411                         toggle_errors();
412                 }
413         }
414
415         errors->text().get_buffer()->end_user_action();
416 }
417
418 void
419 UI::toggle_errors ()
420 {
421         if (!errors->is_visible()) {
422                 errors->set_position (WIN_POS_MOUSE);
423                 errors->show ();
424         } else {
425                 errors->hide ();
426         }
427 }
428
429 void
430 UI::display_message (const char *prefix, gint prefix_len, RefPtr<TextBuffer::Tag> ptag, RefPtr<TextBuffer::Tag> mtag, const char *msg)
431 {
432         RefPtr<TextBuffer> buffer (errors->text().get_buffer());
433
434         buffer->insert_with_tag(buffer->end(), prefix, ptag);
435         buffer->insert_with_tag(buffer->end(), msg, mtag);
436         buffer->insert_with_tag(buffer->end(), "\n", mtag);
437
438         errors->scroll_to_bottom ();
439 }       
440
441 void
442 UI::handle_fatal (const char *message)
443 {
444         Window win (WINDOW_POPUP);
445         VBox packer;
446         Label label (message);
447         Button quit (_("Press To Exit"));
448
449         win.set_default_size (400, 100);
450         
451         string title;
452         title = name();
453         title += ": Fatal Error";
454         win.set_title (title);
455
456         win.set_position (WIN_POS_MOUSE);
457         win.add (packer);
458
459         packer.pack_start (label, true, true);
460         packer.pack_start (quit, false, false);
461         quit.signal_clicked().connect(mem_fun(*this,&UI::quit));
462         
463         win.show_all ();
464         win.set_modal (true);
465
466         theMain->run ();
467         
468         exit (1);
469 }
470
471 void
472 UI::popup_error (const char *text)
473 {
474         PopUp *pup;
475
476         if (!caller_is_ui_thread()) {
477                 error << "non-UI threads can't use UI::popup_error" 
478                       << endmsg;
479                 return;
480         }
481         
482         pup = new PopUp (WIN_POS_MOUSE, 0, true);
483         pup->set_text (text);
484         pup->touch ();
485 }
486
487
488 void
489 UI::flush_pending ()
490 {
491         if (!caller_is_ui_thread()) {
492                 error << "non-UI threads cannot call UI::flush_pending()"
493                       << endmsg;
494                 return;
495         }
496
497         gtk_main_iteration();
498
499         while (gtk_events_pending()) {
500                 gtk_main_iteration();
501         }
502 }
503
504 bool
505 UI::just_hide_it (GdkEventAny *ev, Window *win)
506 {
507         win->hide_all ();
508         return true;
509 }
510
511 Gdk::Color
512 UI::get_color (const string& prompt, bool& picked, const Gdk::Color* initial)
513 {
514         Gdk::Color color;
515
516         ColorSelectionDialog color_dialog (prompt);
517
518         color_dialog.set_modal (true);
519         color_dialog.get_cancel_button()->signal_clicked().connect (bind (mem_fun (*this, &UI::color_selection_done), false));
520         color_dialog.get_ok_button()->signal_clicked().connect (bind (mem_fun (*this, &UI::color_selection_done), true));
521         color_dialog.signal_delete_event().connect (mem_fun (*this, &UI::color_selection_deleted));
522
523         if (initial) {
524                 color_dialog.get_colorsel()->set_current_color (*initial);
525         }
526
527         color_dialog.show_all ();
528         color_picked = false;
529         picked = false;
530
531         Main::run();
532
533         color_dialog.hide_all ();
534
535         if (color_picked) {
536                 Gdk::Color f_rgba = color_dialog.get_colorsel()->get_current_color ();
537                 color.set_red(f_rgba.get_red());
538                 color.set_green(f_rgba.get_green());
539                 color.set_blue(f_rgba.get_blue());
540
541                 picked = true;
542         }
543
544         return color;
545 }
546
547 void
548 UI::color_selection_done (bool status)
549 {
550         color_picked = status;
551         Main::quit ();
552 }
553
554 bool
555 UI::color_selection_deleted (GdkEventAny *ev)
556 {
557         Main::quit ();
558         return true;
559 }