remove cruft (unused UI::ui_scale)
[ardour.git] / libs / gtkmm2ext / gtk_ui.cc
index fc94657f6df96f432f84ee1752f46f3b1612e03d..c2210bba934714a12f58255befdd0954140e28d4 100644 (file)
@@ -35,6 +35,7 @@
 #include "pbd/replace_all.h"
 
 #include "gtkmm2ext/application.h"
+#include "gtkmm2ext/bindings.h"
 #include "gtkmm2ext/gtk_ui.h"
 #include "gtkmm2ext/textviewer.h"
 #include "gtkmm2ext/popup.h"
@@ -45,7 +46,7 @@
 #include "gtkmm2ext/actions.h"
 #include "gtkmm2ext/gui_thread.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace Gtkmm2ext;
 using namespace Gtk;
@@ -53,7 +54,7 @@ using namespace Glib;
 using namespace PBD;
 using std::map;
 
-UI       *UI::theGtkUI = 0;
+UI*   UI::theGtkUI = 0;
 
 BaseUI::RequestType Gtkmm2ext::NullMessage = BaseUI::new_request_type();
 BaseUI::RequestType Gtkmm2ext::ErrorMessage = BaseUI::new_request_type();
@@ -65,20 +66,25 @@ BaseUI::RequestType Gtkmm2ext::AddTimeout = BaseUI::new_request_type();
 
 #include "pbd/abstract_ui.cc"  /* instantiate the template */
 
-UI::UI (string namestr, int *argc, char ***argv)
-       : AbstractUI<UIRequest> (namestr)
+template class AbstractUI<Gtkmm2ext::UIRequest>;
+
+UI::UI (string application_name, string thread_name, int *argc, char ***argv)
+       : AbstractUI<UIRequest> (thread_name)
        , _receiver (*this)
-         
+       , global_bindings (0)
+       , 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,
@@ -86,23 +92,27 @@ UI::UI (string namestr, int *argc, char ***argv)
        */
 
        run_loop_thread = Threads::Thread::self();
-       
+
        /* store "this" as the UI-for-thread of this thread, same argument
           as for previous line.
        */
 
        set_event_loop_for_thread (this);
 
+       /* we will be receiving requests */
+
+       EventLoop::register_request_buffer_factory ("gui", request_buffer_factory);
+
        /* 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);
        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);
+       Glib::set_application_name (application_name);
 
        WindowTitle title(Glib::get_application_name());
        title += _("Log");
@@ -122,6 +132,7 @@ UI::UI (string namestr, int *argc, char ***argv)
 UI::~UI ()
 {
        _receiver.hangup ();
+       delete (errors);
 }
 
 bool
@@ -136,9 +147,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;
@@ -354,10 +365,22 @@ UI::set_tip (Widget *w, const gchar *tip, const gchar *hlp)
        }
 
        if (action) {
-               Gtk::AccelKey key;
-               ustring ap = action->get_accel_path();
-               if (!ap.empty()) {
-                       string shortcut = ActionManager::get_key_representation (ap, key);
+               Bindings* bindings = (Bindings*) w->get_data ("ardour-bindings");
+               if (!bindings) {
+                       Gtk::Window* win = (Gtk::Window*) w->get_toplevel();
+                       if (win) {
+                               bindings = (Bindings*) win->get_data ("ardour-bindings");
+                       }
+               }
+
+               if (!bindings) {
+                       bindings = global_bindings;
+               }
+
+               if (bindings) {
+                       Bindings::Operation op;
+                       KeyboardKey kb = bindings->get_binding_for_action (action, op);
+                       string shortcut = kb.display_label ();
                        if (!shortcut.empty()) {
                                replace_all (shortcut, "<", "");
                                replace_all (shortcut, ">", "-");
@@ -443,7 +466,7 @@ UI::do_request (UIRequest* req)
        } else if (req->type == CallSlot) {
 #ifndef NDEBUG
                if (getenv ("DEBUG_THREADED_SIGNALS")) {
-                       cerr << "call slot for " << name() << endl;
+                       cerr << "call slot for " << event_loop_name() << endl;
                }
 #endif
                req->the_slot ();
@@ -475,9 +498,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 {
@@ -574,10 +629,6 @@ UI::process_error_message (Transmitter::Channel chn, const char *str)
                        cerr << prefix << str << endl;
                } else {
                        display_message (prefix, prefix_len, ptag, mtag, str);
-                       
-                       if (!errors->is_visible() && chn != Transmitter::Info) {
-                               show_errors ();
-                       }
                }
        }
 
@@ -607,7 +658,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 ();
@@ -670,13 +721,13 @@ UI::popup_error (const string& text)
 
        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_wmclass (X_("error"), Glib::get_application_name());
        msg.set_position (WIN_POS_MOUSE);
        msg.run ();
 }
 
 void
-UI::flush_pending ()
+UI::flush_pending (float timeout)
 {
        if (!caller_is_ui_thread()) {
                error << "non-UI threads cannot call UI::flush_pending()"
@@ -684,56 +735,26 @@ UI::flush_pending ()
                return;
        }
 
+       int64_t end = g_get_monotonic_time () + timeout * 1e6;
+
        gtk_main_iteration();
 
        while (gtk_events_pending()) {
+               if (timeout > 0 && end < g_get_monotonic_time ()) {
+                       cerr << "UI::flush_pending timed out after " << timeout << "s.\n";
+                       break;
+               }
                gtk_main_iteration();
        }
 }
 
 bool
-UI::just_hide_it (GdkEventAny */*ev*/, Window *win)
+UI::just_hide_it (GdkEventAny/*ev*/, Window *win)
 {
        win->hide ();
        return true;
 }
 
-Gdk::Color
-UI::get_color (const string& prompt, bool& picked, const Gdk::Color* initial)
-{
-       Gdk::Color color;
-
-       ColorSelectionDialog color_dialog (prompt);
-
-       color_dialog.set_modal (true);
-       color_dialog.get_cancel_button()->signal_clicked().connect (bind (mem_fun (*this, &UI::color_selection_done), false));
-       color_dialog.get_ok_button()->signal_clicked().connect (bind (mem_fun (*this, &UI::color_selection_done), true));
-       color_dialog.signal_delete_event().connect (mem_fun (*this, &UI::color_selection_deleted));
-
-       if (initial) {
-               color_dialog.get_colorsel()->set_current_color (*initial);
-       }
-
-       color_dialog.show_all ();
-       color_picked = false;
-       picked = false;
-
-       Main::run();
-
-       color_dialog.hide_all ();
-
-       if (color_picked) {
-               Gdk::Color f_rgba = color_dialog.get_colorsel()->get_current_color ();
-               color.set_red(f_rgba.get_red());
-               color.set_green(f_rgba.get_green());
-               color.set_blue(f_rgba.get_blue());
-
-               picked = true;
-       }
-
-       return color;
-}
-
 void
 UI::color_selection_done (bool status)
 {
@@ -742,7 +763,7 @@ UI::color_selection_done (bool status)
 }
 
 bool
-UI::color_selection_deleted (GdkEventAny */*ev*/)
+UI::color_selection_deleted (GdkEventAny/*ev*/)
 {
        Main::quit ();
        return true;