Group the Rec and Monitor buttons together. Group the Solo Isolate and Lock buttons...
[ardour.git] / gtk2_ardour / utils.cc
index e749d519dcdc62fe3ee2331f52bd21b8f77166c9..ba151fe3499d62718b2ea080337526e49666f044 100644 (file)
@@ -64,7 +64,9 @@ using namespace Glib;
 using namespace PBD;
 using Gtkmm2ext::Keyboard;
 
-sigc::signal<void>  DPIReset;
+namespace ARDOUR_UI_UTILS {
+       sigc::signal<void>  DPIReset;
+}
 
 #ifdef PLATFORM_WINDOWS
 #define random() rand()
@@ -77,7 +79,7 @@ sigc::signal<void>  DPIReset;
  * @param s true to make sensitive, false to make insensitive
  */
 void
-add_item_with_sensitivity (Menu_Helpers::MenuList& m, Menu_Helpers::MenuElem e, bool s)
+ARDOUR_UI_UTILS::add_item_with_sensitivity (Menu_Helpers::MenuList& m, Menu_Helpers::MenuElem e, bool s)
 {
        m.push_back (e);
        if (!s) {
@@ -87,7 +89,7 @@ add_item_with_sensitivity (Menu_Helpers::MenuList& m, Menu_Helpers::MenuElem e,
 
 
 gint
-just_hide_it (GdkEventAny */*ev*/, Gtk::Window *win)
+ARDOUR_UI_UTILS::just_hide_it (GdkEventAny */*ev*/, Gtk::Window *win)
 {
        win->hide ();
        return 0;
@@ -102,7 +104,7 @@ just_hide_it (GdkEventAny */*ev*/, Gtk::Window *win)
 */
 
 unsigned char*
-xpm2rgb (const char** xpm, uint32_t& w, uint32_t& h)
+ARDOUR_UI_UTILS::xpm2rgb (const char** xpm, uint32_t& w, uint32_t& h)
 {
        static long vals[256], val;
        uint32_t t, x, y, colors, cpp;
@@ -144,7 +146,7 @@ xpm2rgb (const char** xpm, uint32_t& w, uint32_t& h)
 }
 
 unsigned char*
-xpm2rgba (const char** xpm, uint32_t& w, uint32_t& h)
+ARDOUR_UI_UTILS::xpm2rgba (const char** xpm, uint32_t& w, uint32_t& h)
 {
        static long vals[256], val;
        uint32_t t, x, y, colors, cpp;
@@ -221,7 +223,7 @@ xpm2rgba (const char** xpm, uint32_t& w, uint32_t& h)
  * that we might add here later.
  */
 Pango::FontDescription
-sanitized_font (std::string const& name)
+ARDOUR_UI_UTILS::sanitized_font (std::string const& name)
 {
        Pango::FontDescription fd (name);
 
@@ -233,7 +235,7 @@ sanitized_font (std::string const& name)
 }
 
 Pango::FontDescription
-get_font_for_style (string widgetname)
+ARDOUR_UI_UTILS::get_font_for_style (string widgetname)
 {
        Gtk::Window window (WINDOW_TOPLEVEL);
        Gtk::Label foobar;
@@ -262,7 +264,7 @@ get_font_for_style (string widgetname)
 }
 
 uint32_t
-rgba_from_style (string style, uint32_t r, uint32_t g, uint32_t b, uint32_t a, string attr, int state, bool rgba)
+ARDOUR_UI_UTILS::rgba_from_style (string style, uint32_t r, uint32_t g, uint32_t b, uint32_t a, string attr, int state, bool rgba)
 {
        /* In GTK+2, styles aren't set up correctly if the widget is not
           attached to a toplevel window that has a screen pointer.
@@ -321,7 +323,7 @@ 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)
+ARDOUR_UI_UTILS::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);
@@ -368,7 +370,7 @@ rgba_p_from_style (string style, float *r, float *g, float *b, string attr, int
 }
 
 void
-set_color_from_rgb (Gdk::Color& c, uint32_t rgb)
+ARDOUR_UI_UTILS::set_color_from_rgb (Gdk::Color& c, uint32_t rgb)
 {
        /* Gdk::Color color ranges are 16 bit, so scale from 8 bit by
           multiplying by 256.
@@ -377,7 +379,7 @@ set_color_from_rgb (Gdk::Color& c, uint32_t rgb)
 }
 
 void
-set_color_from_rgba (Gdk::Color& c, uint32_t rgba)
+ARDOUR_UI_UTILS::set_color_from_rgba (Gdk::Color& c, uint32_t rgba)
 {
        /* Gdk::Color color ranges are 16 bit, so scale from 8 bit by
           multiplying by 256.
@@ -386,7 +388,7 @@ set_color_from_rgba (Gdk::Color& c, uint32_t rgba)
 }
 
 uint32_t
-gdk_color_to_rgba (Gdk::Color const& c)
+ARDOUR_UI_UTILS::gdk_color_to_rgba (Gdk::Color const& c)
 {
        /* since alpha value is not available from a Gdk::Color, it is
           hardcoded as 0xff (aka 255 or 1.0)
@@ -400,41 +402,9 @@ gdk_color_to_rgba (Gdk::Color const& c)
        return RGBA_TO_UINT (r,g,b,a);
 }
 
-uint32_t
-contrasting_text_color (uint32_t c)
-{
-       double r, g, b, a;
-       ArdourCanvas::color_to_rgba (c, r, g, b, a);
-
-       const double black_r = 0.0;
-       const double black_g = 0.0;
-       const double black_b = 0.0;
-
-       const double white_r = 1.0;
-       const double white_g = 1.0;
-       const double white_b = 1.0;
-
-       /* Use W3C contrast guideline calculation */
-
-       double white_contrast = (max (r, white_r) - min (r, white_r)) +
-               (max (g, white_g) - min (g, white_g)) + 
-               (max (b, white_b) - min (b, white_b));
-
-       double black_contrast = (max (r, black_r) - min (r, black_r)) +
-               (max (g, black_g) - min (g, black_g)) + 
-               (max (b, black_b) - min (b, black_b));
-
-       if (white_contrast > black_contrast) {          
-               /* use white */
-               return ArdourCanvas::rgba_to_color (1.0, 1.0, 1.0, 1.0);
-       } else {
-               /* use black */
-               return ArdourCanvas::rgba_to_color (0.0, 0.0, 0.0, 1.0);
-       }
-}
 
 bool
-relay_key_press (GdkEventKey* ev, Gtk::Window* win)
+ARDOUR_UI_UTILS::relay_key_press (GdkEventKey* ev, Gtk::Window* win)
 {
        PublicEditor& ed (PublicEditor::instance());
 
@@ -450,13 +420,13 @@ relay_key_press (GdkEventKey* ev, Gtk::Window* win)
 }
 
 bool
-forward_key_press (GdkEventKey* ev)
+ARDOUR_UI_UTILS::forward_key_press (GdkEventKey* ev)
 {
-        return PublicEditor::instance().on_key_press_event(ev);
+       return PublicEditor::instance().on_key_press_event(ev);
 }
 
 bool
-emulate_key_event (Gtk::Widget* w, unsigned int keyval)
+ARDOUR_UI_UTILS::emulate_key_event (Gtk::Widget* w, unsigned int keyval)
 {
        GdkDisplay  *display = gtk_widget_get_display (GTK_WIDGET(w->gobj()));
        GdkKeymap   *keymap  = gdk_keymap_get_for_display (display);
@@ -485,7 +455,7 @@ emulate_key_event (Gtk::Widget* w, unsigned int keyval)
 }
 
 bool
-key_press_focus_accelerator_handler (Gtk::Window& window, GdkEventKey* ev)
+ARDOUR_UI_UTILS::key_press_focus_accelerator_handler (Gtk::Window& window, GdkEventKey* ev)
 {
        GtkWindow* win = window.gobj();
        GtkWidget* focus = gtk_window_get_focus (win);
@@ -616,8 +586,8 @@ key_press_focus_accelerator_handler (Gtk::Window& window, GdkEventKey* ev)
                /* no special handling or there are modifiers in effect: accelerate first */
 
                 DEBUG_TRACE (DEBUG::Accelerators, "\tactivate, then propagate\n");
-               DEBUG_TRACE (DEBUG::Accelerators, string_compose ("\tevent send-event:%1 time:%2 length:%3 string:%4 hardware_keycode:%5 group:%6\n",
-                                       ev->send_event, ev->time, ev->length, ev->string, ev->hardware_keycode, ev->group));
+               DEBUG_TRACE (DEBUG::Accelerators, string_compose ("\tevent send-event:%1 time:%2 length:%3 name %7 string:%4 hardware_keycode:%5 group:%6\n",
+                                                                 ev->send_event, ev->time, ev->length, ev->string, ev->hardware_keycode, ev->group, gdk_keyval_name (ev->keyval)));
 
                if (allow_activating) {
                        DEBUG_TRACE (DEBUG::Accelerators, "\tsending to window\n");
@@ -656,7 +626,7 @@ key_press_focus_accelerator_handler (Gtk::Window& window, GdkEventKey* ev)
 }
 
 Glib::RefPtr<Gdk::Pixbuf>
-get_xpm (std::string name)
+ARDOUR_UI_UTILS::get_xpm (std::string name)
 {
        if (!xpm_map[name]) {
 
@@ -666,7 +636,7 @@ get_xpm (std::string name)
 
                std::string data_file_path;
 
-               if(!find_file_in_search_path (spath, name, data_file_path)) {
+               if(!find_file (spath, name, data_file_path)) {
                        fatal << string_compose (_("cannot find XPM file for %1"), name) << endmsg;
                }
 
@@ -681,7 +651,7 @@ get_xpm (std::string name)
 }
 
 vector<string>
-get_icon_sets ()
+ARDOUR_UI_UTILS::get_icon_sets ()
 {
        Searchpath spath(ARDOUR::ardour_data_search_path());
        spath.add_subdirectory_to_paths ("icons");
@@ -693,22 +663,11 @@ get_icon_sets ()
 
                vector<string> entries;
 
-               get_files_in_directory (*s, entries);
+               get_paths (entries, *s, false, false);
 
                for (vector<string>::iterator e = entries.begin(); e != entries.end(); ++e) {
-
-                       string d = *e;
-
-                       /* ignore dotfiles, . and .. */
-
-                       if (d.empty() || d[0] == '.') {
-                               continue;
-                       }
-                       
-                       Glib::ustring path = Glib::build_filename (*s, *e);
-
-                       if (Glib::file_test (path, Glib::FILE_TEST_IS_DIR)) {
-                               r.push_back (Glib::filename_to_utf8 (*e));
+                       if (Glib::file_test (*e, Glib::FILE_TEST_IS_DIR)) {
+                               r.push_back (Glib::filename_to_utf8 (Glib::path_get_basename(*e)));
                        }
                }
        }
@@ -717,7 +676,7 @@ get_icon_sets ()
 }
 
 std::string
-get_icon_path (const char* cname, string icon_set)
+ARDOUR_UI_UTILS::get_icon_path (const char* cname, string icon_set)
 {
        std::string data_file_path;
        string name = cname;
@@ -731,7 +690,7 @@ get_icon_path (const char* cname, string icon_set)
                spath.add_subdirectory_to_paths ("icons");
                spath.add_subdirectory_to_paths (icon_set);
                
-               find_file_in_search_path (spath, name, data_file_path);
+               find_file (spath, name, data_file_path);
        }
        
        if (data_file_path.empty()) {
@@ -743,7 +702,7 @@ get_icon_path (const char* cname, string icon_set)
                Searchpath def (ARDOUR::ardour_data_search_path());
                def.add_subdirectory_to_paths ("icons");
        
-               if (!find_file_in_search_path (def, name, data_file_path)) {
+               if (!find_file (def, name, data_file_path)) {
                        fatal << string_compose (_("cannot find icon image for %1 using %2"), name, spath.to_string()) << endmsg;
                        /*NOTREACHED*/
                }
@@ -753,7 +712,7 @@ get_icon_path (const char* cname, string icon_set)
 }
 
 Glib::RefPtr<Gdk::Pixbuf>
-get_icon (const char* cname, string icon_set)
+ARDOUR_UI_UTILS::get_icon (const char* cname, string icon_set)
 {
        Glib::RefPtr<Gdk::Pixbuf> img;
        try {
@@ -767,6 +726,7 @@ get_icon (const char* cname, string icon_set)
        return img;
 }
 
+namespace ARDOUR_UI_UTILS {
 Glib::RefPtr<Gdk::Pixbuf>
 get_icon (const char* cname)
 {
@@ -781,9 +741,10 @@ get_icon (const char* cname)
 
        return img;
 }
+}
 
 string
-longest (vector<string>& strings)
+ARDOUR_UI_UTILS::longest (vector<string>& strings)
 {
        if (strings.empty()) {
                return string ("");
@@ -811,7 +772,7 @@ longest (vector<string>& strings)
 }
 
 bool
-key_is_legal_for_numeric_entry (guint keyval)
+ARDOUR_UI_UTILS::key_is_legal_for_numeric_entry (guint keyval)
 {
        /* we assume that this does not change over the life of the process 
         */
@@ -894,8 +855,9 @@ key_is_legal_for_numeric_entry (guint keyval)
 
        return false;
 }
+
 void
-set_pango_fontsize ()
+ARDOUR_UI_UTILS::set_pango_fontsize ()
 {
        long val = ARDOUR::Config->get_font_scale();
 
@@ -911,7 +873,7 @@ set_pango_fontsize ()
 }
 
 void
-reset_dpi ()
+ARDOUR_UI_UTILS::reset_dpi ()
 {
        long val = ARDOUR::Config->get_font_scale();
        set_pango_fontsize ();
@@ -923,7 +885,7 @@ reset_dpi ()
 }
 
 void
-resize_window_to_proportion_of_monitor (Gtk::Window* window, int max_width, int max_height)
+ARDOUR_UI_UTILS::resize_window_to_proportion_of_monitor (Gtk::Window* window, int max_width, int max_height)
 {
        Glib::RefPtr<Gdk::Screen> screen = window->get_screen ();
        Gdk::Rectangle monitor_rect;
@@ -938,7 +900,7 @@ resize_window_to_proportion_of_monitor (Gtk::Window* window, int max_width, int
 
 /** Replace _ with __ in a string; for use with menu item text to make underscores displayed correctly */
 string
-escape_underscores (string const & s)
+ARDOUR_UI_UTILS::escape_underscores (string const & s)
 {
        string o;
        string::size_type const N = s.length ();
@@ -956,7 +918,7 @@ escape_underscores (string const & s)
 
 /** Replace < and > with &lt; and &gt; respectively to make < > display correctly in markup strings */
 string
-escape_angled_brackets (string const & s)
+ARDOUR_UI_UTILS::escape_angled_brackets (string const & s)
 {
        string o = s;
        boost::replace_all (o, "<", "&lt;");
@@ -965,7 +927,7 @@ escape_angled_brackets (string const & s)
 }
 
 Gdk::Color
-unique_random_color (list<Gdk::Color>& used_colors)
+ARDOUR_UI_UTILS::unique_random_color (list<Gdk::Color>& used_colors)
 {
        Gdk::Color newcolor;
 
@@ -1006,7 +968,7 @@ unique_random_color (list<Gdk::Color>& used_colors)
 }
 
 string 
-rate_as_string (float r)
+ARDOUR_UI_UTILS::rate_as_string (float r)
 {
        char buf[32];
        if (fmod (r, 1000.0f)) {
@@ -1016,3 +978,28 @@ rate_as_string (float r)
        }
        return buf;
 }
+
+
+string
+ARDOUR_UI_UTILS::track_number_to_string (
+               int64_t tracknumber,
+               std::string sep,
+               std::string postfix
+               )
+{
+       string rv;
+       if (tracknumber > 0) {
+               rv = "<span weight=\"bold\" font_family=\"ArdourMono, Mono\">";
+               rv += PBD::to_string (tracknumber, std::dec);
+               rv += "</span>";
+               rv += sep;
+       }
+       else if (tracknumber < 0) {
+               rv = "<span weight=\"bold\" font_family=\"ArdourMono, Mono\">";
+               rv += PBD::to_string (-tracknumber, std::dec);
+               rv += "</span>";
+               rv += sep;
+       }
+       rv += Glib::Markup::escape_text(postfix);
+       return rv;
+}