Merge branch 'master' into windows+cc
[ardour.git] / gtk2_ardour / utils.cc
index 4df17936449ef73f391b29539f38be3145e78e58..00e94b0dd96b6878584162b1eeb119d7567a8b7c 100644 (file)
@@ -28,6 +28,7 @@
 #include <clocale>
 #include <cstring>
 #include <cctype>
+#include <cmath>
 #include <fstream>
 #include <list>
 #include <sys/stat.h>
@@ -287,6 +288,53 @@ rgba_from_style (string style, uint32_t r, uint32_t g, uint32_t b, uint32_t a, s
        }
 }
 
+bool
+rgba_p_from_style (string style, float *r, float *g, float *b, string attr, int state)
+{
+       static Gtk::Window* window = 0;
+       assert (r && g && b);
+
+       if (window == 0) {
+               window = new Window (WINDOW_TOPLEVEL);
+       }
+
+       Gtk::EventBox foo;
+
+       window->add (foo);
+
+       foo.set_name (style);
+       foo.ensure_style ();
+
+       GtkRcStyle* rc = foo.get_style()->gobj()->rc_style;
+
+       if (!rc) {
+               warning << string_compose (_("missing RGBA style for \"%1\""), style) << endl;
+               return false;
+       }
+       if (attr == "fg") {
+               *r = rc->fg[state].red / 65535.0;
+               *g = rc->fg[state].green / 65535.0;
+               *b = rc->fg[state].blue / 65535.0;
+       } else if (attr == "bg") {
+               *r = rc->bg[state].red / 65535.0;
+               *g = rc->bg[state].green / 65535.0;
+               *b = rc->bg[state].blue / 65535.0;
+       } else if (attr == "base") {
+               *r = rc->base[state].red / 65535.0;
+               *g = rc->base[state].green / 65535.0;
+               *b = rc->base[state].blue / 65535.0;
+       } else if (attr == "text") {
+               *r = rc->text[state].red / 65535.0;
+               *g = rc->text[state].green / 65535.0;
+               *b = rc->text[state].blue / 65535.0;
+       } else {
+               return false;
+       }
+
+       window->remove ();
+       return true;
+}
+
 void
 set_color (Gdk::Color& c, int rgb)
 {
@@ -296,8 +344,14 @@ set_color (Gdk::Color& c, int rgb)
 bool
 relay_key_press (GdkEventKey* ev, Gtk::Window* win)
 {
+       PublicEditor& ed (PublicEditor::instance());
+
        if (!key_press_focus_accelerator_handler (*win, ev)) {
-               return PublicEditor::instance().on_key_press_event(ev);
+               if (&ed == 0) {
+                       /* early key press in pre-main-window-dialogs, no editor yet */
+                       return false;
+               }
+               return ed.on_key_press_event(ev);
        } else {
                return true;
        }
@@ -514,7 +568,7 @@ get_xpm (std::string name)
 {
        if (!xpm_map[name]) {
 
-               SearchPath spath(ARDOUR::ardour_data_search_path());
+               Searchpath spath(ARDOUR::ardour_data_search_path());
 
                spath.add_subdirectory_to_paths("pixmaps");
 
@@ -540,7 +594,7 @@ get_icon_path (const char* cname)
        string name = cname;
        name += X_(".png");
 
-       SearchPath spath(ARDOUR::ardour_data_search_path());
+       Searchpath spath(ARDOUR::ardour_data_search_path());
 
        spath.add_subdirectory_to_paths("icons");
 
@@ -687,7 +741,9 @@ set_pango_fontsize ()
 
        /* FT2 rendering - used by GnomeCanvas, sigh */
 
+#ifndef WIN32
        pango_ft2_font_map_set_resolution ((PangoFT2FontMap*) pango_ft2_font_map_new(), val/1024, val/1024);
+#endif
 
        /* Cairo rendering, in case there is any */
 
@@ -788,3 +844,15 @@ unique_random_color (list<Gdk::Color>& used_colors)
                /* XXX need throttle here to make sure we don't spin for ever */
        }
 }
+
+string 
+rate_as_string (float r)
+{
+       char buf[32];
+       if (fmod (r, 1000.0f)) {
+               snprintf (buf, sizeof (buf), "%.1f kHz", r/1000.0);
+       } else {
+               snprintf (buf, sizeof (buf), "%.0f kHz", r/1000.0);
+       }
+       return buf;
+}