Accommodate newly introduced source(s) in our MSVC project (cairocanvas)
[ardour.git] / libs / gtkmm2ext / utils.cc
index 4cfc0b26a02a5537c59cf76a62418878280417ed..c18023e02008d7429b8d0c5fa24979cbe966b404 100644 (file)
 #include <gtkmm/label.h>
 #include <gtkmm/comboboxtext.h>
 #include <gtkmm/tooltip.h>
+#include <gtkmm/menuitem.h>
 
 #include "gtkmm2ext/utils.h"
+#include "gtkmm2ext/persistent_tooltip.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 
@@ -272,6 +274,18 @@ Gtkmm2ext::pixbuf_from_string(const string& name, const Pango::FontDescription&
                return *empty_pixbuf;
        }
 
+       if (clip_width <= 0 || clip_height <= 0) {
+               /* negative values mean padding around natural size */
+               int width, height;
+               pixel_size (name, font, width, height);
+               if (clip_width <= 0) {
+                       clip_width = width - clip_width;
+               }
+               if (clip_height <= 0) {
+                       clip_height = height - clip_height;
+               }
+       }
+
        Glib::RefPtr<Gdk::Pixbuf> buf = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, true, 8, clip_width, clip_height);
 
        cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, clip_width, clip_height);
@@ -295,6 +309,125 @@ Gtkmm2ext::pixbuf_from_string(const string& name, const Pango::FontDescription&
        return buf;
 }
 
+void
+_position_menu_anchored (int& x, int& y, bool& push_in,
+                                   const Gtk::Menu* const menu,
+                                   Gtk::Widget* const anchor,
+                                   const std::string& selected) {
+       using namespace Gtk;
+       using namespace Gtk::Menu_Helpers;
+
+        /* TODO: lacks support for rotated dropdown buttons */
+
+       if (!anchor->has_screen () || !anchor->get_has_window ()) {
+               return;
+       }
+
+       Gdk::Rectangle monitor;
+       {
+               const int monitor_num = anchor->get_screen ()->get_monitor_at_window (
+                               anchor->get_window ());
+               anchor->get_screen ()->get_monitor_geometry (
+                               (monitor_num < 0) ? 0 : monitor_num, monitor);
+       }
+
+       const Requisition menu_req = menu->size_request();
+       const Gdk::Rectangle allocation = anchor->get_allocation();
+
+       /* The x and y position are handled separately.
+        *
+        * For the x position if the direction is LTR (or RTL), then we try in order:
+        *  a) align the left (right) of the menu with the left (right) of the button
+        *     if there's enough room until the right (left) border of the screen;
+        *  b) align the right (left) of the menu with the right (left) of the button
+        *     if there's enough room until the left (right) border of the screen;
+        *  c) align the right (left) border of the menu with the right (left) border
+        *     of the screen if there's enough space;
+        *  d) align the left (right) border of the menu with the left (right) border
+        *     of the screen, with the rightmost (leftmost) part of the menu that
+        *     overflows the screen.
+        *     XXX We always align left regardless of the direction because if x is
+        *     left of the current monitor, the menu popup code after us notices it
+        *     and enforces that the menu stays in the monitor that's at the left...*/
+
+       anchor->get_window ()->get_origin (x, y);
+
+       if (anchor->get_direction() == TEXT_DIR_RTL) {
+               if (monitor.get_x() <= x + allocation.get_width() - menu_req.width) {
+                       /* a) align menu right and button right */
+                       x += allocation.get_width() - menu_req.width;
+               } else if (x + menu_req.width <= monitor.get_x() + monitor.get_width()) {
+                       /* b) align menu left and button left: nothing to do*/
+               } else if (menu_req.width > monitor.get_width()) {
+                       /* c) align menu left and screen left, guaranteed to fit */
+                       x = monitor.get_x();
+               } else {
+                       /* d) XXX align left or the menu might change monitors */
+                       x = monitor.get_x();
+               }
+       } else { /* LTR */
+               if (x + menu_req.width <= monitor.get_x() + monitor.get_width()) {
+                       /* a) align menu left and button left: nothing to do*/
+               } else if (monitor.get_x() <= x + allocation.get_width() - menu_req.width) {
+                       /* b) align menu right and button right */
+                       x += allocation.get_width() - menu_req.width;
+               } else if (menu_req.width > monitor.get_width()) {
+                       /* c) align menu right and screen right, guaranteed to fit */
+                       x = monitor.get_x() + monitor.get_width() - menu_req.width;
+               } else {
+                       /* d) align left */
+                       x = monitor.get_x();
+               }
+       }
+
+       /* For the y position, try in order:
+        *  a) if there is a menu item with the same text as the button, align it
+        *     with the button, unless that makes the menu overflow the monitor.
+        *  b) align the top of the menu with the bottom of the button if there is
+        *     enough room below the button;
+        *  c) align the bottom of the menu with the top of the button if there is
+        *     enough room above the button;
+        *  d) align the bottom of the menu with the bottom of the monitor if there
+        *     is enough room, but avoid moving the menu to another monitor */
+
+       const MenuList& items = menu->items ();
+       int offset = 0;
+
+       MenuList::const_iterator i = items.begin();
+       for ( ; i != items.end(); ++i) {
+               const Label* label_widget = dynamic_cast<const Label*>(i->get_child());
+               if (label_widget && selected == ((std::string) label_widget->get_label())) {
+                       break;
+               }
+               offset += i->size_request().height;
+       }
+       if (i != items.end() &&
+           y - offset >= monitor.get_y() &&
+           y - offset + menu_req.height <= monitor.get_y() + monitor.get_height()) {
+               y -= offset;
+       } else if (y + allocation.get_height() + menu_req.height <= monitor.get_y() + monitor.get_height()) {
+               y += allocation.get_height(); /* a) */
+       } else if ((y - menu_req.height) >= monitor.get_y()) {
+               y -= menu_req.height; /* b) */
+       } else {
+               y = monitor.get_y() + max(0, monitor.get_height() - menu_req.height);
+       }
+
+       push_in = false;
+}
+
+void
+Gtkmm2ext::anchored_menu_popup (Gtk::Menu* const menu,
+                                Gtk::Widget* const anchor,
+                                const std::string& selected,
+                                guint button, guint32 time) {
+       menu->popup(
+               sigc::bind (sigc::ptr_fun(&_position_menu_anchored),
+                           menu, anchor, selected),
+               button,
+               time);
+}
+
 void
 Gtkmm2ext::set_popdown_strings (Gtk::ComboBoxText& cr, const vector<string>& strings)
 {
@@ -381,30 +514,6 @@ Gtkmm2ext::detach_menu (Gtk::Menu& menu)
        }
 }
 
-bool
-Gtkmm2ext::possibly_translate_mod_to_make_legal_accelerator (GdkModifierType& mod)
-{
-#ifdef GTKOSX
-       /* GTK on OS X is currently (February 2012) setting both
-          the Meta and Mod2 bits in the event modifier state if
-          the Command key is down.
-
-          gtk_accel_groups_activate() does not invoke any of the logic
-          that gtk_window_activate_key() will that sorts out that stupid
-          state of affairs, and as a result it fails to find a match
-          for the key event and the current set of accelerators.
-
-          to fix this, if the meta bit is set, remove the mod2 bit
-          from the modifier. this assumes that our bindings use Primary
-          which will have set the meta bit in the accelerator entry.
-       */
-       if (mod & GDK_META_MASK) {
-               mod = GdkModifierType (mod & ~GDK_MOD2_MASK);
-       }
-#endif
-       return true;
-}
-
 bool
 Gtkmm2ext::possibly_translate_keyval_to_make_legal_accelerator (uint32_t& keyval)
 {
@@ -702,7 +811,7 @@ Gtkmm2ext::window_to_draw_on (Gtk::Widget& w, Gtk::Widget** parent)
 }
 
 int
-Gtkmm2ext::pixel_width (const string& str, Pango::FontDescription& font)
+Gtkmm2ext::pixel_width (const string& str, const Pango::FontDescription& font)
 {
        Glib::RefPtr<Pango::Context> context = Glib::wrap (gdk_pango_context_get());
        Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
@@ -730,7 +839,7 @@ Gtkmm2ext::pixel_width (const string& str, Pango::FontDescription& font)
 }
 
 void
-Gtkmm2ext::pixel_size (const string& str, Pango::FontDescription& font, int& width, int& height)
+Gtkmm2ext::pixel_size (const string& str, const Pango::FontDescription& font, int& width, int& height)
 {
        Gtk::Label foo;
        Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout ("");
@@ -756,9 +865,9 @@ Gtkmm2ext::fit_to_pixels (const string& str, int pixel_width, Pango::FontDescrip
        layout->set_width (pixel_width * PANGO_SCALE);
 
        if (with_ellipses) {
-               layout->set_ellipsize (Pango::ELLIPSIZE_END);
+               layout->set_ellipsize (Pango::ELLIPSIZE_END);
        } else {
-               layout->set_wrap (Pango::WRAP_CHAR);
+               layout->set_wrap (Pango::WRAP_CHAR);
        }
 
        line = layout->get_line (0);
@@ -848,12 +957,14 @@ void
 Gtkmm2ext::enable_tooltips ()
 {
        gtk_rc_parse_string ("gtk-enable-tooltips = 1");
+       PersistentTooltip::set_tooltips_enabled (true);
 }
 
 void
 Gtkmm2ext::disable_tooltips ()
 {
        gtk_rc_parse_string ("gtk-enable-tooltips = 0");
+       PersistentTooltip::set_tooltips_enabled (false);
 }
 
 bool
@@ -967,3 +1078,115 @@ Gtkmm2ext::event_type_string (int event_type)
 
        return "unknown";
 }
+
+std::string
+Gtkmm2ext::markup_escape_text (std::string const& s)
+{
+       return Glib::Markup::escape_text (s);
+}
+
+void
+Gtkmm2ext::add_volume_shortcuts (Gtk::FileChooser& c)
+{
+#ifdef __APPLE__
+       try {
+               /* This is a first order approach, listing all mounted volumes (incl network).
+                * One could use `diskutil` or `mount` to query local disks only, or
+                * something even fancier if deemed appropriate.
+                */
+               Glib::Dir dir("/Volumes");
+               for (Glib::DirIterator di = dir.begin(); di != dir.end(); di++) {
+                       string fullpath = Glib::build_filename ("/Volumes", *di);
+                       if (!Glib::file_test (fullpath, Glib::FILE_TEST_IS_DIR)) continue;
+
+                       try { /* add_shortcut_folder throws an exception if the folder being added already has a shortcut */
+                               c.add_shortcut_folder (fullpath);
+                       }
+                       catch (Glib::Error& e) {
+                               std::cerr << "add_shortcut_folder() threw Glib::Error: " << e.what() << std::endl;
+                       }
+               }
+       }
+       catch (Glib::FileError& e) {
+               std::cerr << "listing /Volumnes failed: " << e.what() << std::endl;
+       }
+#endif
+}
+
+float
+Gtkmm2ext::paned_position_as_fraction (Gtk::Paned& paned, bool h)
+{
+       const guint pos = gtk_paned_get_position (const_cast<GtkPaned*>(static_cast<const Gtk::Paned*>(&paned)->gobj()));
+       return (double) pos / (h ? paned.get_allocation().get_height() : paned.get_allocation().get_width());
+}
+
+void
+Gtkmm2ext::paned_set_position_as_fraction (Gtk::Paned& paned, float fraction, bool h)
+{
+       gint v = (h ? paned.get_allocation().get_height() : paned.get_allocation().get_width());
+
+       if (v < 1) {
+               return;
+       }
+
+       paned.set_position ((guint) floor (fraction * v));
+}
+
+string
+Gtkmm2ext::show_gdk_event_state (int state)
+{
+       string s;
+       if (state & GDK_SHIFT_MASK) {
+               s += "+SHIFT";
+       }
+       if (state & GDK_LOCK_MASK) {
+               s += "+LOCK";
+       }
+       if (state & GDK_CONTROL_MASK) {
+               s += "+CONTROL";
+       }
+       if (state & GDK_MOD1_MASK) {
+               s += "+MOD1";
+       }
+       if (state & GDK_MOD2_MASK) {
+               s += "+MOD2";
+       }
+       if (state & GDK_MOD3_MASK) {
+               s += "+MOD3";
+       }
+       if (state & GDK_MOD4_MASK) {
+               s += "+MOD4";
+       }
+       if (state & GDK_MOD5_MASK) {
+               s += "+MOD5";
+       }
+       if (state & GDK_BUTTON1_MASK) {
+               s += "+BUTTON1";
+       }
+       if (state & GDK_BUTTON2_MASK) {
+               s += "+BUTTON2";
+       }
+       if (state & GDK_BUTTON3_MASK) {
+               s += "+BUTTON3";
+       }
+       if (state & GDK_BUTTON4_MASK) {
+               s += "+BUTTON4";
+       }
+       if (state & GDK_BUTTON5_MASK) {
+               s += "+BUTTON5";
+       }
+       if (state & GDK_SUPER_MASK) {
+               s += "+SUPER";
+       }
+       if (state & GDK_HYPER_MASK) {
+               s += "+HYPER";
+       }
+       if (state & GDK_META_MASK) {
+               s += "+META";
+       }
+       if (state & GDK_RELEASE_MASK) {
+               s += "+RELEASE";
+       }
+
+       return s;
+}