convert from Glib:: to Glib::Threads for all thread-related API
[ardour.git] / libs / gtkmm2ext / gtk_ui.cc
index dcce6b6c2854d611514242dac4aa89a6e5cabe2c..a1b932af59543bf412349510a0c50032b95fe0b5 100644 (file)
@@ -31,7 +31,7 @@
 #include <pbd/touchable.h>
 #include <pbd/failed_constructor.h>
 #include <pbd/pthread_utils.h>
-#include <pbd/stacktrace.h>
+#include <pbd/replace_all.h>
 
 #include <gtkmm2ext/application.h>
 #include <gtkmm2ext/gtk_ui.h>
@@ -40,6 +40,7 @@
 #include <gtkmm2ext/utils.h>
 #include <gtkmm2ext/window_title.h>
 #include <gtkmm2ext/actions.h>
+#include <gtkmm2ext/activatable.h>
 
 #include "i18n.h"
 
@@ -63,11 +64,10 @@ BaseUI::RequestType Gtkmm2ext::AddTimeout = BaseUI::new_request_type();
 
 UI::UI (string namestr, int *argc, char ***argv)
        : AbstractUI<UIRequest> (namestr)
+       , _receiver (*this)
+         
 {
        theMain = new Main (argc, argv);
-#ifndef GTK_NEW_TOOLTIP_API
-       tips = new Tooltips;
-#endif
 
        _active = false;
 
@@ -82,7 +82,7 @@ UI::UI (string namestr, int *argc, char ***argv)
           which is assumed to have called this.
        */
 
-       run_loop_thread = Thread::self();
+       run_loop_thread = Threads::Thread::self();
        
        /* store "this" as the UI-for-thread of this thread, same argument
           as for previous line.
@@ -106,7 +106,7 @@ UI::UI (string namestr, int *argc, char ***argv)
        errors->set_title (title.get_string());
 
        errors->dismiss_button().set_name ("ErrorLogCloseButton");
-       errors->signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), (Window *) errors));
+       errors->signal_delete_event().connect (sigc::bind (sigc::ptr_fun (just_hide_it), (Window *) errors));
        errors->set_type_hint (Gdk::WINDOW_TYPE_HINT_UTILITY);
 
        //load_rcfile (rcfile);
@@ -124,7 +124,7 @@ UI::~UI ()
 bool
 UI::caller_is_ui_thread ()
 {
-       return Thread::self() == run_loop_thread;
+       return Threads::Thread::self() == run_loop_thread;
 }
 
 int
@@ -135,7 +135,7 @@ UI::load_rcfile (string path, bool themechange)
         * 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;
@@ -145,7 +145,7 @@ UI::load_rcfile (string path, bool themechange)
                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
                      << '"'
@@ -154,8 +154,10 @@ UI::load_rcfile (string path, bool themechange)
        }
 
        RC rc (path.c_str());
+       //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) {
@@ -252,10 +254,10 @@ UI::load_rcfile (string path, bool themechange)
 void
 UI::run (Receiver &old_receiver)
 {
-       listen_to (error);
-       listen_to (info);
-       listen_to (warning);
-       listen_to (fatal);
+       _receiver.listen_to (error);
+       _receiver.listen_to (info);
+       _receiver.listen_to (warning);
+       _receiver.listen_to (fatal);
 
        /* stop the old receiver (text/console) once we hit the first idle */
 
@@ -266,7 +268,7 @@ UI::run (Receiver &old_receiver)
        theMain->run ();
        _active = false;
        stopping ();
-       hangup ();
+       _receiver.hangup ();
        return;
 }
 
@@ -338,21 +340,35 @@ UI::set_tip (Widget *w, const gchar *tip, const gchar *hlp)
        std::string msg(tip);
 
        Glib::RefPtr<Gtk::Action> action = w->get_action();
+
+       if (!action) {
+               Gtkmm2ext::Activatable* activatable;
+               if ((activatable = dynamic_cast<Gtkmm2ext::Activatable*>(w))) {
+                       action = activatable->get_related_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());
-                        }
-                }
+               ustring ap = action->get_accel_path();
+               if (!ap.empty()) {
+                       bool has_key = ActionManager::lookup_entry(ap, key);
+                       if (has_key) {
+                               string  abbrev = key.get_abbrev();
+                               if (!abbrev.empty()) {
+                                       replace_all (abbrev, "<", "");
+                                       replace_all (abbrev, ">", "-");
+                                       msg.append(_("\n\nKey: ")).append (abbrev);
+                               }
+                       }
+               }
        }
 
        if (req == 0) {
                return;
        }
 
+
        req->widget = w;
        req->msg = msg.c_str();
        req->msg2 = hlp;
@@ -443,16 +459,7 @@ UI::do_request (UIRequest* req)
 
        } else if (req->type == SetTip) {
 
-#ifdef GTK_NEW_TOOLTIP_API
-               /* even if the installed GTK is up to date,
-                  at present (November 2008) our included
-                  version of gtkmm is not. so use the GTK
-                  API that we've verified has the right function.
-               */
-               gtk_widget_set_tooltip_text (req->widget->gobj(), req->msg);
-#else
-               tips->set_tip (*req->widget, req->msg, "");
-#endif
+               gtk_widget_set_tooltip_markup (req->widget->gobj(), req->msg);
 
        } else {
 
@@ -560,16 +567,35 @@ UI::process_error_message (Transmitter::Channel chn, const char *str)
                handle_fatal (str);
        } else {
 
-               display_message (prefix, prefix_len, ptag, mtag, str);
-
-               if (!errors->is_visible() && chn != Transmitter::Info) {
-                       toggle_errors();
+               if (!ptag || !mtag) {
+                       /* oops, message sent before we set up tags - don't crash */
+                       cerr << prefix << str << endl;
+               } else {
+                       display_message (prefix, prefix_len, ptag, mtag, str);
+                       
+                       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 ()
 {
@@ -634,20 +660,19 @@ UI::handle_fatal (const char *message)
 void
 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 ();
+       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 ();
 }
 
-
 void
 UI::flush_pending ()
 {