2 Copyright (C) 1999-2005 Paul Barton-Davis
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.
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.
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.
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>
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>
44 using namespace Gtkmm2ext;
50 pthread_t UI::gui_thread;
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();
61 #include <pbd/abstract_ui.cc> /* instantiate the template */
64 UI::UI (string namestr, int *argc, char ***argv)
65 : AbstractUI<UIRequest> (namestr, true)
67 theMain = new Main (argc, argv);
74 gui_thread = pthread_self ();
76 fatal << "duplicate UI requested" << endmsg;
80 /* add the pipe to the select/poll loop that GDK does */
82 gdk_input_add (signal_pipe[0],
84 UI::signal_pipe_callback,
87 errors = new TextViewer (850,100);
88 errors->text().set_editable (false);
89 errors->text().set_name ("ErrorText");
91 Glib::set_application_name(namestr);
93 WindowTitle title(Glib::get_application_name());
95 errors->set_title (title.get_string());
97 errors->dismiss_button().set_name ("ErrorLogCloseButton");
98 errors->signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), (Window *) errors));
100 register_thread (pthread_self(), X_("GUI"));
102 //load_rcfile (rcfile);
111 UI::caller_is_ui_thread ()
113 return pthread_equal (gui_thread, pthread_self());
117 UI::load_rcfile (string path)
119 if (path.length() == 0) {
123 if (access (path.c_str(), R_OK)) {
124 error << "UI: couldn't find rc file \""
131 RC rc (path.c_str());
132 RC::reset_styles(Gtk::Settings::get_default());
137 UI::run (Receiver &old_receiver)
144 old_receiver.hangup ();
164 pthread_kill (gui_thread, SIGKILL);
171 UIRequest *req = get_request (Quit);
180 static bool idle_quit ()
189 if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
192 Glib::signal_idle().connect (sigc::ptr_fun (idle_quit));
197 UI::touch_display (Touchable *display)
199 UIRequest *req = get_request (TouchDisplay);
205 req->display = display;
211 UI::set_tip (Widget *w, const gchar *tip, const gchar *hlp)
213 UIRequest *req = get_request (SetTip);
227 UI::set_state (Widget *w, StateType state)
229 UIRequest *req = get_request (StateChange);
235 req->new_state = state;
242 UI::idle_add (int (*func)(void *), void *arg)
244 UIRequest *req = get_request (AddIdle);
250 req->function = func;
256 /* END abstract_ui interfaces */
259 UI::signal_pipe_callback (void *arg, int fd, GdkInputCondition cond)
263 /* flush (nonblocking) pipe */
265 while (read (fd, buf, 256) > 0);
267 ((UI *) arg)->handle_ui_requests ();
271 UI::do_request (UIRequest* req)
273 if (req->type == ErrorMessage) {
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 */
279 } else if (req->type == Quit) {
283 } else if (req->type == CallSlot) {
287 } else if (req->type == TouchDisplay) {
289 req->display->touch ();
290 if (req->display->delete_after_touch()) {
294 } else if (req->type == StateChange) {
296 req->widget->set_state (req->new_state);
298 } else if (req->type == SetTip) {
300 /* XXX need to figure out how this works */
304 error << "GtkUI: unknown request type "
310 /*======================================================================
312 ======================================================================*/
315 UI::receive (Transmitter::Channel chn, const char *str)
317 if (caller_is_ui_thread()) {
318 process_error_message (chn, str);
320 UIRequest* req = get_request (ErrorMessage);
327 req->msg = strdup (str);
333 #define OLD_STYLE_ERRORS 1
336 UI::process_error_message (Transmitter::Channel chn, const char *str)
339 RefPtr<TextBuffer::Tag> ptag;
340 RefPtr<TextBuffer::Tag> mtag;
343 bool fatal_received = false;
344 #ifndef OLD_STYLE_ERRORS
345 PopUp* popup = new PopUp (WIN_POS_CENTER, 0, true);
349 case Transmitter::Fatal:
350 prefix = "[FATAL]: ";
354 fatal_received = true;
356 case Transmitter::Error:
358 prefix = "[ERROR]: ";
363 popup->set_name ("ErrorMessage");
364 popup->set_text (str);
369 case Transmitter::Info:
376 popup->set_name ("InfoMessage");
377 popup->set_text (str);
383 case Transmitter::Warning:
385 prefix = "[WARNING]: ";
390 popup->set_name ("WarningMessage");
391 popup->set_text (str);
397 /* no choice but to use text/console output here */
398 cerr << "programmer error in UI::check_error_messages (channel = " << chn << ")\n";
402 errors->text().get_buffer()->begin_user_action();
404 if (fatal_received) {
408 display_message (prefix, prefix_len, ptag, mtag, str);
410 if (!errors->is_visible()) {
415 errors->text().get_buffer()->end_user_action();
421 if (!errors->is_visible()) {
422 errors->set_position (WIN_POS_MOUSE);
430 UI::display_message (const char *prefix, gint prefix_len, RefPtr<TextBuffer::Tag> ptag, RefPtr<TextBuffer::Tag> mtag, const char *msg)
432 RefPtr<TextBuffer> buffer (errors->text().get_buffer());
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);
438 errors->scroll_to_bottom ();
442 UI::handle_fatal (const char *message)
444 Window win (WINDOW_POPUP);
446 Label label (message);
447 Button quit (_("Press To Exit"));
449 win.set_default_size (400, 100);
453 title += ": Fatal Error";
454 win.set_title (title);
456 win.set_position (WIN_POS_MOUSE);
459 packer.pack_start (label, true, true);
460 packer.pack_start (quit, false, false);
461 quit.signal_clicked().connect(mem_fun(*this,&UI::quit));
464 win.set_modal (true);
472 UI::popup_error (const char *text)
476 if (!caller_is_ui_thread()) {
477 error << "non-UI threads can't use UI::popup_error"
482 pup = new PopUp (WIN_POS_MOUSE, 0, true);
483 pup->set_text (text);
491 if (!caller_is_ui_thread()) {
492 error << "non-UI threads cannot call UI::flush_pending()"
497 gtk_main_iteration();
499 while (gtk_events_pending()) {
500 gtk_main_iteration();
505 UI::just_hide_it (GdkEventAny *ev, Window *win)
512 UI::get_color (const string& prompt, bool& picked, const Gdk::Color* initial)
516 ColorSelectionDialog color_dialog (prompt);
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));
524 color_dialog.get_colorsel()->set_current_color (*initial);
527 color_dialog.show_all ();
528 color_picked = false;
533 color_dialog.hide_all ();
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());
548 UI::color_selection_done (bool status)
550 color_picked = status;
555 UI::color_selection_deleted (GdkEventAny *ev)