size-based scrolling and right-click pages for editor notebook
[ardour.git] / gtk2_ardour / utils.cc
index a1e317abd45c344543d1ae3e70b2dd3bbfc89fac..290339cad86e93bc0309e01a8b18104bb5f6baae 100644 (file)
 #include <gtkmm2ext/utils.h>
 
 #include "ardour_ui.h"
+#include "keyboard.h"
 #include "utils.h"
 #include "i18n.h"
 #include "rgb_macros.h"
+#include "canvas_impl.h"
 
 using namespace std;
 using namespace Gtk;
+using namespace sigc;
 
 string
 short_version (string orig, string::size_type target_length)
@@ -102,27 +105,22 @@ short_version (string orig, string::size_type target_length)
 }
 
 string
-fit_to_pixels (string str, int32_t pixel_width, Gdk_Font& font)
+fit_to_pixels (ARDOUR::stringcr_t str, int pixel_width, ARDOUR::stringcr_t font)
 {
-       gint width;
-       gint lbearing;
-       gint rbearing;
-       gint ascent;
-       gint descent;
-
+       Label foo;
+       int width;
+       int height;
+       Pango::FontDescription fontdesc (font);
+       
        int namelen = str.length();
        char cstr[namelen+1];
        strcpy (cstr, str.c_str());
        
        while (namelen) {
-               
-               gdk_string_extents (font,
-                                   cstr,
-                                   &lbearing,
-                                   &rbearing,
-                                   &width,
-                                   &ascent,
-                                   &descent);
+               Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout (cstr);
+
+               layout->set_font_description (fontdesc);
+               layout->get_pixel_size (width, height);
 
                if (width < (pixel_width)) {
                        break;
@@ -189,17 +187,10 @@ internationalize (const char **array)
 gint
 just_hide_it (GdkEventAny *ev, Gtk::Window *win)
 {
-       ARDOUR_UI::instance()->allow_focus (false);
        win->hide_all ();
        return TRUE;
 }
 
-void
-allow_keyboard_focus (bool yn)
-{
-       ARDOUR_UI::instance()->allow_focus (yn);
-}
-
 /* xpm2rgb copied from nixieclock, which bore the legend:
 
     nixieclock - a nixie desktop timepiece
@@ -219,7 +210,7 @@ xpm2rgb (const char** xpm, uint32_t& w, uint32_t& h)
        // PARSE HEADER
        
        if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
-               error << compose (_("bad XPM header %1"), xpm[0])
+               error << string_compose (_("bad XPM header %1"), xpm[0])
                      << endmsg;
                return 0;
        }
@@ -262,7 +253,7 @@ xpm2rgba (const char** xpm, uint32_t& w, uint32_t& h)
        // PARSE HEADER
 
        if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
-               error << compose (_("bad XPM header %1"), xpm[0])
+               error << string_compose (_("bad XPM header %1"), xpm[0])
                      << endmsg;
                return 0;
        }
@@ -313,7 +304,7 @@ xpm2rgba (const char** xpm, uint32_t& w, uint32_t& h)
        return (savergb);
 }
 
-GtkCanvasPoints*
+ArdourCanvas::Points*
 get_canvas_points (string who, uint32_t npoints)
 {
        // cerr << who << ": wants " << npoints << " canvas points" << endl;
@@ -322,13 +313,13 @@ get_canvas_points (string who, uint32_t npoints)
                abort ();
        }
 #endif
-       return gtk_canvas_points_new (npoints);
+       return new ArdourCanvas::Points (npoints);
 }
 
 int
-channel_combo_get_channel_count (Gtk::Combo& combo)
+channel_combo_get_channel_count (Gtk::ComboBoxText& combo)
 {
-       string str = combo.get_entry()->get_text();
+       string str = combo.get_active_text();
        int chns;
 
        if (str == _("mono")) {
@@ -415,26 +406,21 @@ url_decode (string& url)
        }
 }
 
-string
+Pango::FontDescription
 get_font_for_style (string widgetname)
 {
+       Gtk::Window window (WINDOW_TOPLEVEL);
        Gtk::Label foobar;
+       Glib::RefPtr<Style> style;
 
+       window.add (foobar);
        foobar.set_name (widgetname);
        foobar.ensure_style();
 
-       if (foobar.get_style() == 0 || foobar.get_style()->gobj()->rc_style == 0 || foobar.get_style()->gobj()->rc_style->font_name == 0) {
-               return "fixed";
-       }
-
-       string str = foobar.get_style()->gobj()->rc_style->font_name;
-
-       if (str.empty()) {
-               return "fixed"; // standard X Window fallback font
-       } else {
-               return str;
-       }
+       style = foobar.get_style ();
+       return style->get_font();
 }
+
 gint
 pane_handler (GdkEventButton* ev, Gtk::Paned* pane)
 {
@@ -446,13 +432,13 @@ pane_handler (GdkEventButton* ev, Gtk::Paned* pane)
 
                gint pos;
                gint cmp;
-
-               pos = Gtkmm2ext::gtk_paned_get_position (pane->gobj());
+               
+               pos = pane->get_position ();
 
                if (dynamic_cast<VPaned*>(pane)) {
-                       cmp = pane->height();
+                       cmp = pane->get_height();
                } else {
-                       cmp = pane->width();
+                       cmp = pane->get_width();
                }
 
                /* we have to use approximations here because we can't predict the
@@ -481,9 +467,9 @@ pane_handler (GdkEventButton* ev, Gtk::Paned* pane)
                                pane->set_position (1);
                        } else {
                                if (dynamic_cast<VPaned*>(pane)) {
-                                       pane->set_position (pane->height());
+                                       pane->set_position (pane->get_height());
                                } else {
-                                       pane->set_position (pane->width());
+                                       pane->set_position (pane->get_width());
                                }
                        }
                }
@@ -496,8 +482,20 @@ pane_handler (GdkEventButton* ev, Gtk::Paned* pane)
 uint32_t
 rgba_from_style (string style, uint32_t r, uint32_t g, uint32_t b, uint32_t a)
 {
+       /* In GTK+2, styles aren't set up correctly if the widget is not
+          attached to a toplevel window that has a screen pointer.
+       */
+
+       static Gtk::Window* window = 0;
+
+       if (window == 0) {
+               window = new Window (WINDOW_TOPLEVEL);
+       }
+
        Gtk::Label foo;
        
+       window->add (foo);
+
        foo.set_name (style);
        foo.ensure_style ();
        
@@ -513,8 +511,16 @@ rgba_from_style (string style, uint32_t r, uint32_t g, uint32_t b, uint32_t a)
                a = waverc->fg[GTK_STATE_ACTIVE].red / 257; 
 
        } else {
-               warning << compose (_("missing RGBA style for \"%1\""), style) << endl;
+               warning << string_compose (_("missing RGBA style for \"%1\""), style) << endl;
        }
+
+       window->remove ();
        
        return (uint32_t) RGBA_TO_UINT(r,g,b,a);
 }
+
+bool 
+canvas_item_visible (ArdourCanvas::Item* item)
+{
+       return (item->gobj()->object.flags & GNOME_CANVAS_ITEM_VISIBLE) ? true : false;
+}