Align switch statement with case labels to reduce indentation in EngineDialog
[ardour.git] / libs / gtkmm2ext / gtk_ui.cc
index 92756c47827c5aaf550bfcd66bbfea4123049b9e..d1601468591945033b5954fd07887ef28eed3d1c 100644 (file)
 #include <cctype>
 
 #include <gtkmm.h>
-#include <pbd/error.h>
-#include <pbd/touchable.h>
-#include <pbd/failed_constructor.h>
-#include <pbd/pthread_utils.h>
-#include <pbd/replace_all.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 <gtkmm2ext/activatable.h>
+
+#include "pbd/error.h"
+#include "pbd/touchable.h"
+#include "pbd/failed_constructor.h"
+#include "pbd/pthread_utils.h"
+#include "pbd/replace_all.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 "gtkmm2ext/activatable.h"
+#include "gtkmm2ext/actions.h"
+#include "gtkmm2ext/gui_thread.h"
 
 #include "i18n.h"
 
@@ -62,26 +65,33 @@ BaseUI::RequestType Gtkmm2ext::AddTimeout = BaseUI::new_request_type();
 
 #include "pbd/abstract_ui.cc"  /* instantiate the template */
 
+template class AbstractUI<Gtkmm2ext::UIRequest>;
+
 UI::UI (string namestr, int *argc, char ***argv)
        : AbstractUI<UIRequest> (namestr)
+       , _receiver (*this)
+       , errors (0)
+
 {
        theMain = new Main (argc, argv);
 
+       pthread_set_name ("gui");
+
        _active = false;
 
        if (!theGtkUI) {
                theGtkUI = this;
        } else {
                fatal << "duplicate UI requested" << endmsg;
-               /* NOTREACHED */
+               abort(); /* NOTREACHED */
        }
 
        /* the GUI event loop runs in the main thread of the app,
           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.
        */
@@ -90,7 +100,7 @@ UI::UI (string namestr, int *argc, char ***argv)
 
        /* attach our request source to the default main context */
 
-       request_channel.ios()->attach (MainContext::get_default());
+       attach_request_source ();
 
        errors = new TextViewer (800,600);
        errors->text().set_editable (false);
@@ -116,13 +126,14 @@ UI::UI (string namestr, int *argc, char ***argv)
 
 UI::~UI ()
 {
+       _receiver.hangup ();
+       delete (errors);
 }
 
-
 bool
 UI::caller_is_ui_thread ()
 {
-       return Thread::self() == run_loop_thread;
+       return Threads::Thread::self() == run_loop_thread;
 }
 
 int
@@ -131,9 +142,9 @@ 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... 
+        * 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;
@@ -252,21 +263,23 @@ 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 */
 
        Glib::signal_idle().connect (bind_return (mem_fun (old_receiver, &Receiver::hangup), false));
 
-       starting ();
+       if (starting ()) {
+               return;
+       }
+
        _active = true;
        theMain->run ();
        _active = false;
-       stopping ();
-       hangup ();
+
        return;
 }
 
@@ -350,14 +363,11 @@ UI::set_tip (Widget *w, const gchar *tip, const gchar *hlp)
                Gtk::AccelKey key;
                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);
-                               }
+                       string shortcut = ActionManager::get_key_representation (ap, key);
+                       if (!shortcut.empty()) {
+                               replace_all (shortcut, "<", "");
+                               replace_all (shortcut, ">", "-");
+                               msg.append(_("\n\nShortcut: ")).append (shortcut);
                        }
                }
        }
@@ -471,9 +481,41 @@ UI::do_request (UIRequest* req)
   Error Display
   ======================================================================*/
 
+void
+UI::dump_errors (std::ostream& ostr)
+{
+       Glib::Threads::Mutex::Lock lm (error_lock);
+       ostr << endl << X_("Errors/Messages:") << endl;
+       for (list<string>::const_iterator i = error_stack.begin(); i != error_stack.end(); ++i) {
+               ostr << *i << endl;
+       }
+       ostr << endl;
+}
+
 void
 UI::receive (Transmitter::Channel chn, const char *str)
 {
+       {
+               Glib::Threads::Mutex::Lock lm (error_lock);
+               switch (chn) {
+               case Transmitter::Fatal:
+                       error_stack.push_back (string (X_("FATAL: ")) + str);
+                       break;
+               case Transmitter::Error:
+                       error_stack.push_back (string (X_("ERROR: ")) + str);
+                       break;
+               case Transmitter::Warning:
+                       error_stack.push_back (string (X_("WARNING: ")) + str);
+                       break;
+               case Transmitter::Info:
+                       error_stack.push_back (string (X_("INFO: ")) + str);
+                       break;
+               case Transmitter::Throw:
+                       error_stack.push_back (string (X_("THROW: ")) + str);
+                       break;
+               }
+       }
+
        if (caller_is_ui_thread()) {
                process_error_message (chn, str);
        } else {
@@ -565,10 +607,11 @@ 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) {
-                       show_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);
                }
        }
 
@@ -598,7 +641,7 @@ UI::toggle_errors ()
        }
 
        Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
-       
+
        if (tact->get_active()) {
                errors->set_position (WIN_POS_MOUSE);
                errors->show ();
@@ -683,7 +726,7 @@ UI::flush_pending ()
 }
 
 bool
-UI::just_hide_it (GdkEventAny */*ev*/, Window *win)
+UI::just_hide_it (GdkEventAny/*ev*/, Window *win)
 {
        win->hide ();
        return true;
@@ -733,7 +776,7 @@ UI::color_selection_done (bool status)
 }
 
 bool
-UI::color_selection_deleted (GdkEventAny */*ev*/)
+UI::color_selection_deleted (GdkEventAny/*ev*/)
 {
        Main::quit ();
        return true;