add a timeout to flush_pending()
[ardour.git] / libs / gtkmm2ext / gtk_ui.cc
index 538bf8d63f5b060bdf78f9c23573673d0424cfc7..c96c31b3dec8b082e62cc16e7bec49d98e4381cc 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,8 @@ using namespace Glib;
 using namespace PBD;
 using std::map;
 
-UI       *UI::theGtkUI = 0;
+UI*   UI::theGtkUI = 0;
+float UI::ui_scale = 1.0;
 
 BaseUI::RequestType Gtkmm2ext::NullMessage = BaseUI::new_request_type();
 BaseUI::RequestType Gtkmm2ext::ErrorMessage = BaseUI::new_request_type();
@@ -67,11 +69,11 @@ BaseUI::RequestType Gtkmm2ext::AddTimeout = BaseUI::new_request_type();
 
 template class AbstractUI<Gtkmm2ext::UIRequest>;
 
-UI::UI (string namestr, int *argc, char ***argv)
-       : AbstractUI<UIRequest> (namestr)
+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);
 
@@ -111,7 +113,7 @@ UI::UI (string namestr, int *argc, char ***argv)
        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");
@@ -364,10 +366,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, ">", "-");
@@ -714,7 +728,7 @@ UI::popup_error (const string& text)
 }
 
 void
-UI::flush_pending ()
+UI::flush_pending (float timeout)
 {
        if (!caller_is_ui_thread()) {
                error << "non-UI threads cannot call UI::flush_pending()"
@@ -722,9 +736,15 @@ 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();
        }
 }