Upgrade to waf 1.6.7 and autowaf r52.
[ardour.git] / libs / gtkmm2ext / gtk_ui.cc
index 1620226ccc9b05876ec54846f82f2b89a69b559c..35517e9604de825f14e4cac1c425df5a017b7140 100644 (file)
 #include <pbd/pthread_utils.h>
 #include <pbd/stacktrace.h>
 
+#include <gtkmm2ext/application.h>
 #include <gtkmm2ext/gtk_ui.h>
 #include <gtkmm2ext/textviewer.h>
 #include <gtkmm2ext/popup.h>
 #include <gtkmm2ext/utils.h>
 #include <gtkmm2ext/window_title.h>
+#include <gtkmm2ext/actions.h>
 
 #include "i18n.h"
 
@@ -47,22 +49,20 @@ using namespace Glib;
 using namespace PBD;
 using std::map;
 
-pthread_t UI::gui_thread;
 UI       *UI::theGtkUI = 0;
 
+BaseUI::RequestType Gtkmm2ext::NullMessage = BaseUI::new_request_type();
 BaseUI::RequestType Gtkmm2ext::ErrorMessage = BaseUI::new_request_type();
-BaseUI::RequestType Gtkmm2ext::Quit = BaseUI::new_request_type();
 BaseUI::RequestType Gtkmm2ext::TouchDisplay = BaseUI::new_request_type();
 BaseUI::RequestType Gtkmm2ext::StateChange = BaseUI::new_request_type();
 BaseUI::RequestType Gtkmm2ext::SetTip = BaseUI::new_request_type();
 BaseUI::RequestType Gtkmm2ext::AddIdle = BaseUI::new_request_type();
 BaseUI::RequestType Gtkmm2ext::AddTimeout = BaseUI::new_request_type();
 
-#include <pbd/abstract_ui.cc>  /* instantiate the template */
-
+#include "pbd/abstract_ui.cc"  /* instantiate the template */
 
 UI::UI (string namestr, int *argc, char ***argv)
-       : AbstractUI<UIRequest> (namestr, true)
+       : AbstractUI<UIRequest> (namestr)
 {
        theMain = new Main (argc, argv);
 #ifndef GTK_NEW_TOOLTIP_API
@@ -73,22 +73,31 @@ UI::UI (string namestr, int *argc, char ***argv)
 
        if (!theGtkUI) {
                theGtkUI = this;
-               gui_thread = pthread_self ();
        } else {
                fatal << "duplicate UI requested" << endmsg;
                /* NOTREACHED */
        }
 
-       /* add the pipe to the select/poll loop that GDK does */
+       /* the GUI event loop runs in the main thread of the app,
+          which is assumed to have called this.
+       */
+
+       run_loop_thread = Thread::self();
+       
+       /* store "this" as the UI-for-thread of this thread, same argument
+          as for previous line.
+       */
 
-       gdk_input_add (signal_pipe[0],
-                      GDK_INPUT_READ,
-                      UI::signal_pipe_callback,
-                      this);
+       set_event_loop_for_thread (this);
 
-       errors = new TextViewer (850,100);
+       /* attach our request source to the default main context */
+
+       request_channel.ios()->attach (MainContext::get_default());
+
+       errors = new TextViewer (800,600);
        errors->text().set_editable (false);
        errors->text().set_name ("ErrorText");
+       errors->signal_unmap().connect (sigc::bind (sigc::ptr_fun (&ActionManager::uncheck_toggleaction), X_("<Actions>/Editor/toggle-log-window")));
 
        Glib::set_application_name(namestr);
 
@@ -100,9 +109,11 @@ UI::UI (string namestr, int *argc, char ***argv)
        errors->signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), (Window *) errors));
        errors->set_type_hint (Gdk::WINDOW_TYPE_HINT_UTILITY);
 
-       register_thread (pthread_self(), X_("GUI"));
-
        //load_rcfile (rcfile);
+
+       /* instantiate the Application singleton */
+
+       Application::instance();
 }
 
 UI::~UI ()
@@ -113,17 +124,28 @@ UI::~UI ()
 bool
 UI::caller_is_ui_thread ()
 {
-       return pthread_equal (gui_thread, pthread_self());
+       return Thread::self() == run_loop_thread;
 }
 
 int
 UI::load_rcfile (string path, bool themechange)
 {
+       /* Yes, pointers to Glib::RefPtr.  If these are not kept around,
+        * a segfault somewhere deep in the wonderfully robust glib will result.
+        * This does not occur if wiget.get_style is used instead of rc.get_style below,
+        * except that doesn't actually work... 
+        */
+       
+       static Glib::RefPtr<Style>* fatal_style   = 0;
+       static Glib::RefPtr<Style>* error_style   = 0;
+       static Glib::RefPtr<Style>* warning_style = 0;
+       static Glib::RefPtr<Style>* info_style    = 0;
+
        if (path.length() == 0) {
                return -1;
        }
 
-       if (access (path.c_str(), R_OK)) {
+       if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
                error << "UI: couldn't find rc file \""
                      << path
                      << '"'
@@ -132,8 +154,10 @@ UI::load_rcfile (string path, bool themechange)
        }
 
        RC rc (path.c_str());
-       // RC::reset_styles (Gtk::Settings::get_default());
+       //this is buggy in gtkmm for some reason, so use C
+       //RC::reset_styles (Gtk::Settings::get_default());
        gtk_rc_reset_styles (gtk_settings_get_default());
+
        theme_changed.emit();
 
        if (themechange) {
@@ -143,18 +167,20 @@ UI::load_rcfile (string path, bool themechange)
        /* have to pack widgets into a toplevel window so that styles will stick */
 
        Window temp_window (WINDOW_TOPLEVEL);
+       temp_window.ensure_style ();
+
        HBox box;
-       Label a_widget1;
-       Label a_widget2;
-       Label a_widget3;
-       Label a_widget4;
+       Label fatal_widget;
+       Label error_widget;
+       Label warning_widget;
+       Label info_widget;
        RefPtr<Gtk::Style> style;
        RefPtr<TextBuffer> buffer (errors->text().get_buffer());
 
-       box.pack_start (a_widget1);
-       box.pack_start (a_widget2);
-       box.pack_start (a_widget3);
-       box.pack_start (a_widget4);
+       box.pack_start (fatal_widget);
+       box.pack_start (error_widget);
+       box.pack_start (warning_widget);
+       box.pack_start (info_widget);
 
        error_ptag = buffer->create_tag();
        error_mtag = buffer->create_tag();
@@ -165,49 +191,62 @@ UI::load_rcfile (string path, bool themechange)
        info_ptag = buffer->create_tag();
        info_mtag = buffer->create_tag();
 
-       a_widget1.set_name ("FatalMessage");
-       a_widget1.ensure_style ();
-       style = a_widget1.get_style();
-
-       fatal_ptag->property_font_desc().set_value(style->get_font());
-       fatal_ptag->property_foreground_gdk().set_value(style->get_fg(STATE_ACTIVE));
-       fatal_ptag->property_background_gdk().set_value(style->get_bg(STATE_ACTIVE));
-       fatal_mtag->property_font_desc().set_value(style->get_font());
-       fatal_mtag->property_foreground_gdk().set_value(style->get_fg(STATE_NORMAL));
-       fatal_mtag->property_background_gdk().set_value(style->get_bg(STATE_NORMAL));
-
-       a_widget2.set_name ("ErrorMessage");
-       a_widget2.ensure_style ();
-       style = a_widget2.get_style();
-
-       error_ptag->property_font_desc().set_value(style->get_font());
-       error_ptag->property_foreground_gdk().set_value(style->get_fg(STATE_ACTIVE));
-       error_ptag->property_background_gdk().set_value(style->get_bg(STATE_ACTIVE));
-       error_mtag->property_font_desc().set_value(style->get_font());
-       error_mtag->property_foreground_gdk().set_value(style->get_fg(STATE_NORMAL));
-       error_mtag->property_background_gdk().set_value(style->get_bg(STATE_NORMAL));
-
-       a_widget3.set_name ("WarningMessage");
-       a_widget3.ensure_style ();
-       style = a_widget3.get_style();
-
-       warning_ptag->property_font_desc().set_value(style->get_font());
-       warning_ptag->property_foreground_gdk().set_value(style->get_fg(STATE_ACTIVE));
-       warning_ptag->property_background_gdk().set_value(style->get_bg(STATE_ACTIVE));
-       warning_mtag->property_font_desc().set_value(style->get_font());
-       warning_mtag->property_foreground_gdk().set_value(style->get_fg(STATE_NORMAL));
-       warning_mtag->property_background_gdk().set_value(style->get_bg(STATE_NORMAL));
-
-       a_widget4.set_name ("InfoMessage");
-       a_widget4.ensure_style ();
-       style = a_widget4.get_style();
-
-       info_ptag->property_font_desc().set_value(style->get_font());
-       info_ptag->property_foreground_gdk().set_value(style->get_fg(STATE_ACTIVE));
-       info_ptag->property_background_gdk().set_value(style->get_bg(STATE_ACTIVE));
-       info_mtag->property_font_desc().set_value(style->get_font());
-       info_mtag->property_foreground_gdk().set_value(style->get_fg(STATE_NORMAL));
-       info_mtag->property_background_gdk().set_value(style->get_bg(STATE_NORMAL));
+       fatal_widget.set_name ("FatalMessage");
+       delete fatal_style;
+
+       /* This next line and the similar ones below are sketchily
+        * guessed to fix #2885.  I think maybe that problems occur
+        * because with gtk_rc_get_style (to quote its docs) "no
+        * refcount is added to the returned style".  So I've switched
+        * this to use Glib::wrap with take_copy == true, which requires
+        * all the nasty casts and calls to plain-old-C GTK.
+        *
+        * At worst I think this causes a memory leak; at least it appears
+        * to fix the bug.
+        *
+        * I could be wrong about any or all of the above.
+        */
+       fatal_style = new Glib::RefPtr<Style> (Glib::wrap (gtk_rc_get_style (reinterpret_cast<GtkWidget*> (fatal_widget.gobj())), true));
+
+       fatal_ptag->property_font_desc().set_value((*fatal_style)->get_font());
+       fatal_ptag->property_foreground_gdk().set_value((*fatal_style)->get_fg(STATE_ACTIVE));
+       fatal_ptag->property_background_gdk().set_value((*fatal_style)->get_bg(STATE_ACTIVE));
+       fatal_mtag->property_font_desc().set_value((*fatal_style)->get_font());
+       fatal_mtag->property_foreground_gdk().set_value((*fatal_style)->get_fg(STATE_NORMAL));
+       fatal_mtag->property_background_gdk().set_value((*fatal_style)->get_bg(STATE_NORMAL));
+
+       error_widget.set_name ("ErrorMessage");
+       delete error_style;
+       error_style = new Glib::RefPtr<Style> (Glib::wrap (gtk_rc_get_style (reinterpret_cast<GtkWidget*> (error_widget.gobj())), true));
+
+       error_ptag->property_font_desc().set_value((*error_style)->get_font());
+       error_ptag->property_foreground_gdk().set_value((*error_style)->get_fg(STATE_ACTIVE));
+       error_ptag->property_background_gdk().set_value((*error_style)->get_bg(STATE_ACTIVE));
+       error_mtag->property_font_desc().set_value((*error_style)->get_font());
+       error_mtag->property_foreground_gdk().set_value((*error_style)->get_fg(STATE_NORMAL));
+       error_mtag->property_background_gdk().set_value((*error_style)->get_bg(STATE_NORMAL));
+
+       warning_widget.set_name ("WarningMessage");
+       delete warning_style;
+       warning_style = new Glib::RefPtr<Style> (Glib::wrap (gtk_rc_get_style (reinterpret_cast<GtkWidget*> (warning_widget.gobj())), true));
+
+       warning_ptag->property_font_desc().set_value((*warning_style)->get_font());
+       warning_ptag->property_foreground_gdk().set_value((*warning_style)->get_fg(STATE_ACTIVE));
+       warning_ptag->property_background_gdk().set_value((*warning_style)->get_bg(STATE_ACTIVE));
+       warning_mtag->property_font_desc().set_value((*warning_style)->get_font());
+       warning_mtag->property_foreground_gdk().set_value((*warning_style)->get_fg(STATE_NORMAL));
+       warning_mtag->property_background_gdk().set_value((*warning_style)->get_bg(STATE_NORMAL));
+
+       info_widget.set_name ("InfoMessage");
+       delete info_style;
+       info_style = new Glib::RefPtr<Style> (Glib::wrap (gtk_rc_get_style (reinterpret_cast<GtkWidget*> (info_widget.gobj())), true));
+
+       info_ptag->property_font_desc().set_value((*info_style)->get_font());
+       info_ptag->property_foreground_gdk().set_value((*info_style)->get_fg(STATE_ACTIVE));
+       info_ptag->property_background_gdk().set_value((*info_style)->get_bg(STATE_ACTIVE));
+       info_mtag->property_font_desc().set_value((*info_style)->get_font());
+       info_mtag->property_foreground_gdk().set_value((*info_style)->get_fg(STATE_NORMAL));
+       info_mtag->property_background_gdk().set_value((*info_style)->get_bg(STATE_NORMAL));
 
        return 0;
 }
@@ -239,14 +278,6 @@ UI::running ()
        return _active;
 }
 
-void
-UI::kill ()
-{
-       if (_active) {
-               pthread_kill (gui_thread, SIGKILL);
-       }
-}
-
 void
 UI::quit ()
 {
@@ -289,17 +320,43 @@ UI::touch_display (Touchable *display)
        send_request (req);
 }
 
+void
+UI::set_tip (Widget &w, const gchar *tip)
+{
+       set_tip(&w, tip, "");
+}
+
+void
+UI::set_tip (Widget &w, const std::string& tip)
+{
+       set_tip(&w, tip.c_str(), "");
+}
+
 void
 UI::set_tip (Widget *w, const gchar *tip, const gchar *hlp)
 {
        UIRequest *req = get_request (SetTip);
 
+       std::string msg(tip);
+
+       Glib::RefPtr<Gtk::Action> action = w->get_action();
+       if (action) {
+               Gtk::AccelKey key;
+                ustring ap = action->get_accel_path();
+                if (!ap.empty()) {
+                        bool has_key = ActionManager::lookup_entry(ap, key);
+                        if (has_key && key.get_abbrev() != "") {
+                                msg.append("\n\n Key: ").append(key.get_abbrev());
+                        }
+                }
+       }
+
        if (req == 0) {
                return;
        }
 
        req->widget = w;
-       req->msg = tip;
+       req->msg = msg.c_str();
        req->msg2 = hlp;
 
        send_request (req);
@@ -337,16 +394,21 @@ UI::idle_add (int (*func)(void *), void *arg)
 
 /* END abstract_ui interfaces */
 
-void
-UI::signal_pipe_callback (void *arg, int fd, GdkInputCondition /*cond*/)
+/** Create a PBD::EventLoop::InvalidationRecord and attach a callback
+ *  to a given sigc::trackable so that PBD::EventLoop::invalidate_request
+ *  is called when that trackable is destroyed.
+ */
+PBD::EventLoop::InvalidationRecord*
+__invalidator (sigc::trackable& trackable, const char* file, int line)
 {
-       char buf[256];
+        PBD::EventLoop::InvalidationRecord* ir = new PBD::EventLoop::InvalidationRecord;
 
-       /* flush (nonblocking) pipe */
+        ir->file = file;
+        ir->line = line;
 
-       while (read (fd, buf, 256) > 0) {}
+        trackable.add_destroy_notify_callback (ir, PBD::EventLoop::invalidate_request);
 
-       ((UI *) arg)->handle_ui_requests ();
+        return ir;
 }
 
 void
@@ -363,8 +425,12 @@ UI::do_request (UIRequest* req)
                do_quit ();
 
        } else if (req->type == CallSlot) {
-
-               req->slot ();
+#ifndef NDEBUG
+               if (getenv ("DEBUG_THREADED_SIGNALS")) {
+                       cerr << "call slot for " << name() << endl;
+               }
+#endif
+               req->the_slot ();
 
        } else if (req->type == TouchDisplay) {
 
@@ -498,18 +564,39 @@ UI::process_error_message (Transmitter::Channel chn, const char *str)
 
                display_message (prefix, prefix_len, ptag, mtag, str);
 
-               if (!errors->is_visible()) {
-                       toggle_errors();
+               if (!errors->is_visible() && chn != Transmitter::Info) {
+                       show_errors ();
                }
        }
 
        errors->text().get_buffer()->end_user_action();
 }
 
+void
+UI::show_errors ()
+{
+       Glib::RefPtr<Action> act = ActionManager::get_action (X_("Editor"), X_("toggle-log-window"));
+       if (!act) {
+               return;
+       }
+
+       Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
+        if (tact) {
+                tact->set_active ();
+        }
+}
+
 void
 UI::toggle_errors ()
 {
-       if (!errors->is_visible()) {
+       Glib::RefPtr<Action> act = ActionManager::get_action (X_("Editor"), X_("toggle-log-window"));
+       if (!act) {
+               return;
+       }
+
+       Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
+       
+       if (tact->get_active()) {
                errors->set_position (WIN_POS_MOUSE);
                errors->show ();
        } else {
@@ -539,10 +626,9 @@ UI::handle_fatal (const char *message)
 
        win.set_default_size (400, 100);
 
-       string title;
-       title = name();
+       WindowTitle title(Glib::get_application_name());
        title += ": Fatal Error";
-       win.set_title (title);
+       win.set_title (title.get_string());
 
        win.set_position (WIN_POS_MOUSE);
        win.set_border_width (12);
@@ -562,46 +648,24 @@ UI::handle_fatal (const char *message)
 }
 
 void
-UI::popup_error (const char *text)
+UI::popup_error (const string& text)
 {
-       PopUp *pup;
-
        if (!caller_is_ui_thread()) {
                error << "non-UI threads can't use UI::popup_error"
                      << endmsg;
                return;
        }
 
-       pup = new PopUp (WIN_POS_MOUSE, 0, true);
-       pup->set_text (text);
-       pup->touch ();
-}
-
-#ifdef GTKOSX
-extern "C" {
-       int gdk_quartz_in_carbon_menu_event_handler ();
+       MessageDialog msg (text);
+       msg.set_title (string_compose (_("I'm sorry %1, I can't do that"), g_get_user_name()));
+       msg.set_wmclass (X_("error"), name());
+       msg.set_position (WIN_POS_MOUSE);
+       msg.run ();
 }
-#endif
 
 void
 UI::flush_pending ()
 {
-#ifdef GTKOSX
-       /* as of february 11th 2008, gtk/osx has a problem in that mac menu events
-          are handled using Carbon with an "internal" event handling system that
-          doesn't pass things back to the glib/gtk main loop. this makes
-          gtk_main_iteration() block if we call it while in a menu event handler
-          because glib gets confused and thinks there are two threads running
-          g_main_poll_func().
-
-          this hack (relies on code in gtk2_ardour/sync-menu.c) works
-          around that.
-       */
-
-       if (gdk_quartz_in_carbon_menu_event_handler()) {
-               return;
-       }
-#endif
        if (!caller_is_ui_thread()) {
                error << "non-UI threads cannot call UI::flush_pending()"
                      << endmsg;