Add a couple of visibility specifiers that were missing
[ardour.git] / libs / gtkmm2ext / gtk_ui.cc
index 318dedd1fc450a4513ea232b61f6f845f5e04b3d..33841cd0beb6821c61a5927c2a848437e1eeb2f7 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/stacktrace.h>
-
-#include <gtkmm2ext/gtk_ui.h>
-#include <gtkmm2ext/textviewer.h>
-#include <gtkmm2ext/popup.h>
-#include <gtkmm2ext/utils.h>
-#include <gtkmm2ext/window_title.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"
 
@@ -49,6 +55,7 @@ using std::map;
 
 UI       *UI::theGtkUI = 0;
 
+BaseUI::RequestType Gtkmm2ext::NullMessage = BaseUI::new_request_type();
 BaseUI::RequestType Gtkmm2ext::ErrorMessage = BaseUI::new_request_type();
 BaseUI::RequestType Gtkmm2ext::TouchDisplay = BaseUI::new_request_type();
 BaseUI::RequestType Gtkmm2ext::StateChange = BaseUI::new_request_type();
@@ -60,11 +67,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;
 
@@ -79,7 +85,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.
@@ -89,11 +95,12 @@ 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);
        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);
 
@@ -102,21 +109,25 @@ 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);
+
+       /* instantiate the Application singleton */
+
+       Application::instance();
 }
 
 UI::~UI ()
 {
+       _receiver.hangup ();
 }
 
-
 bool
 UI::caller_is_ui_thread ()
 {
-       return Thread::self() == run_loop_thread;
+       return Threads::Thread::self() == run_loop_thread;
 }
 
 int
@@ -127,7 +138,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;
@@ -137,7 +148,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
                      << '"'
@@ -146,8 +157,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) {
@@ -183,7 +196,20 @@ UI::load_rcfile (string path, bool themechange)
 
        fatal_widget.set_name ("FatalMessage");
        delete fatal_style;
-       fatal_style = new Glib::RefPtr<Style>(rc.get_style(fatal_widget));
+
+       /* 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));
@@ -194,7 +220,7 @@ UI::load_rcfile (string path, bool themechange)
 
        error_widget.set_name ("ErrorMessage");
        delete error_style;
-       error_style = new Glib::RefPtr<Style>(rc.get_style(error_widget));
+       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));
@@ -205,7 +231,7 @@ UI::load_rcfile (string path, bool themechange)
 
        warning_widget.set_name ("WarningMessage");
        delete warning_style;
-       warning_style = new Glib::RefPtr<Style>(rc.get_style(warning_widget));
+       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));
@@ -216,7 +242,7 @@ UI::load_rcfile (string path, bool themechange)
 
        info_widget.set_name ("InfoMessage");
        delete info_style;
-       info_style = new Glib::RefPtr<Style>(rc.get_style(info_widget));
+       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));
@@ -231,21 +257,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;
 }
 
@@ -297,17 +325,54 @@ 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) {
+               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()) {
+                       string shortcut = ActionManager::get_key_representation (ap, key);
+                       if (!shortcut.empty()) {
+                               replace_all (shortcut, "<", "");
+                               replace_all (shortcut, ">", "-");
+                               msg.append(_("\n\nShortcut: ")).append (shortcut);
+                       }
+               }
+       }
+
        if (req == 0) {
                return;
        }
 
+
        req->widget = w;
-       req->msg = tip;
+       req->msg = msg.c_str();
        req->msg2 = hlp;
 
        send_request (req);
@@ -345,6 +410,23 @@ UI::idle_add (int (*func)(void *), void *arg)
 
 /* END abstract_ui interfaces */
 
+/** 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)
+{
+        PBD::EventLoop::InvalidationRecord* ir = new PBD::EventLoop::InvalidationRecord;
+
+        ir->file = file;
+        ir->line = line;
+
+        trackable.add_destroy_notify_callback (ir, PBD::EventLoop::invalidate_request);
+
+        return ir;
+}
+
 void
 UI::do_request (UIRequest* req)
 {
@@ -379,16 +461,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 {
 
@@ -496,20 +569,46 @@ 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 ()
 {
-       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 {
@@ -561,46 +660,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 ();
+       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 ();
 }
 
-#ifdef GTKOSX
-extern "C" {
-       int gdk_quartz_in_carbon_menu_event_handler ();
-}
-#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;
@@ -615,7 +692,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;
@@ -665,7 +742,7 @@ UI::color_selection_done (bool status)
 }
 
 bool
-UI::color_selection_deleted (GdkEventAny */*ev*/)
+UI::color_selection_deleted (GdkEventAny/*ev*/)
 {
        Main::quit ();
        return true;