X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=gtk2_ardour%2Futils.cc;h=a52969268adb0b18b74efaab1f3e15d8b621f801;hb=e55e3fde7cec89c49e01046a2db276b2d8f30958;hp=8998b56229ecc371fdd5d52d59a26fcd450b7ac7;hpb=2b49b2433b6dc06a52b0848af62373c747f18ac9;p=ardour.git diff --git a/gtk2_ardour/utils.cc b/gtk2_ardour/utils.cc index 8998b56229..a52969268a 100644 --- a/gtk2_ardour/utils.cc +++ b/gtk2_ardour/utils.cc @@ -15,19 +15,29 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id$ */ +#include // for fontmap resolution control for GnomeCanvas +#include // for fontmap resolution control for GnomeCanvas + #include #include +#include +#include #include +#include #include #include #include #include #include +#include + #include +#include + +#include #include "ardour_ui.h" #include "keyboard.h" @@ -39,155 +49,73 @@ using namespace std; using namespace Gtk; using namespace sigc; +using namespace Glib; +using namespace PBD; -string -short_version (string orig, string::size_type target_length) -{ - /* this tries to create a recognizable abbreviation - of "orig" by removing characters until we meet - a certain target length. - - note that we deliberately leave digits in the result - without modification. - */ - - - string::size_type pos; - - /* remove white-space and punctuation, starting at end */ - - while (orig.length() > target_length) { - if ((pos = orig.find_last_of (_("\"\n\t ,<.>/?:;'[{}]~`!@#$%^&*()_-+="))) == string::npos) { - break; - } - orig.replace (pos, 1, ""); - } - - /* remove lower-case vowels, starting at end */ - - while (orig.length() > target_length) { - if ((pos = orig.find_last_of (_("aeiou"))) == string::npos) { - break; - } - orig.replace (pos, 1, ""); - } - - /* remove upper-case vowels, starting at end */ - - while (orig.length() > target_length) { - if ((pos = orig.find_last_of (_("AEIOU"))) == string::npos) { - break; - } - orig.replace (pos, 1, ""); - } - - /* remove lower-case consonants, starting at end */ - - while (orig.length() > target_length) { - if ((pos = orig.find_last_of (_("bcdfghjklmnpqrtvwxyz"))) == string::npos) { - break; - } - orig.replace (pos, 1, ""); - } +sigc::signal DPIReset; - /* remove upper-case consonants, starting at end */ +int +pixel_width (const ustring& str, Pango::FontDescription& font) +{ + Label foo; + Glib::RefPtr layout = foo.create_pango_layout (""); - while (orig.length() > target_length) { - if ((pos = orig.find_last_of (_("BCDFGHJKLMNPQRTVWXYZ"))) == string::npos) { - break; - } - orig.replace (pos, 1, ""); - } + layout->set_font_description (font); + layout->set_text (str); - /* whatever the length is now, use it */ - - return orig; + int width, height; + Gtkmm2ext::get_ink_pixel_size (layout, width, height); + return width; } -string -fit_to_pixels (string str, int pixel_width, string font) +ustring +fit_to_pixels (const ustring& str, int pixel_width, Pango::FontDescription& font, int& actual_width, bool with_ellipses) { 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) { - Glib::RefPtr layout = foo.create_pango_layout (cstr); + Glib::RefPtr layout = foo.create_pango_layout (""); + ustring::size_type shorter_by = 0; + ustring txt; - layout->set_font_description (fontdesc); - layout->get_pixel_size (width, height); + layout->set_font_description (font); - if (width < (pixel_width)) { - break; - } + actual_width = 0; - --namelen; - cstr[namelen] = '\0'; + ustring ustr = str; + ustring::iterator last = ustr.end(); + --last; /* now points at final entry */ - } + txt = ustr; - return cstr; -} + while (!ustr.empty()) { -int -atoi (const string& s) -{ - return atoi (s.c_str()); -} - -double -atof (const string& s) -{ - return atof (s.c_str()); -} - -void -strip_whitespace_edges (string& str) -{ - string::size_type i; - string::size_type len; - string::size_type s; + layout->set_text (txt); - len = str.length(); + int width, height; + Gtkmm2ext::get_ink_pixel_size (layout, width, height); - for (i = 0; i < len; ++i) { - if (isgraph (str[i])) { + if (width < pixel_width) { + actual_width = width; break; } - } - - s = i; + + ustr.erase (last--); + shorter_by++; - for (i = len - 1; i >= 0; --i) { - if (isgraph (str[i])) { - break; + if (with_ellipses && shorter_by > 3) { + txt = ustr; + txt += "..."; + } else { + txt = ustr; } } - str = str.substr (s, (i - s) + 1); -} - -vector -internationalize (const char **array) -{ - vector v; - - for (uint32_t i = 0; array[i]; ++i) { - v.push_back (_(array[i])); - } - - return v; + return txt; } gint just_hide_it (GdkEventAny *ev, Gtk::Window *win) { - win->hide_all (); + win->hide (); return TRUE; } @@ -215,7 +143,7 @@ xpm2rgb (const char** xpm, uint32_t& w, uint32_t& h) return 0; } - savergb = rgb = (unsigned char*)art_alloc (h * w * 3); + savergb = rgb = (unsigned char*) malloc (h * w * 3); // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION for (t = 0; t < colors; ++t) { @@ -258,7 +186,7 @@ xpm2rgba (const char** xpm, uint32_t& w, uint32_t& h) return 0; } - savergb = rgb = (unsigned char*)art_alloc (h * w * 4); + savergb = rgb = (unsigned char*) malloc (h * w * 4); // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION @@ -316,195 +244,497 @@ get_canvas_points (string who, uint32_t npoints) return new ArdourCanvas::Points (npoints); } -int -channel_combo_get_channel_count (Gtk::ComboBoxText& combo) +Pango::FontDescription* +get_font_for_style (string widgetname) { - string str = combo.get_active_text(); - int chns; - - if (str == _("mono")) { - return 1; - } else if (str == _("stereo")) { - return 2; - } else if ((chns = atoi (str)) != 0) { - return chns; - } else { - return 0; - } + Gtk::Window window (WINDOW_TOPLEVEL); + Gtk::Label foobar; + Glib::RefPtr style; + + window.add (foobar); + foobar.set_name (widgetname); + foobar.ensure_style(); + + style = foobar.get_style (); + + Glib::RefPtr layout = foobar.get_layout(); + + PangoFontDescription *pfd = (PangoFontDescription *)pango_layout_get_font_description((PangoLayout *)layout->gobj()); + + if (!pfd) { + + /* layout inherited its font description from a PangoContext */ + + PangoContext* ctxt = (PangoContext*) pango_layout_get_context ((PangoLayout*) layout->gobj()); + pfd = pango_context_get_font_description (ctxt); + return new Pango::FontDescription (pfd, true); /* make a copy */ + } + + return new Pango::FontDescription (pfd, true); /* make a copy */ } -static int32_t -int_from_hex (char hic, char loc) +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) { - int hi; /* hi byte */ - int lo; /* low byte */ + /* In GTK+2, styles aren't set up correctly if the widget is not + attached to a toplevel window that has a screen pointer. + */ - hi = (int) hic; + static Gtk::Window* window = 0; - if( ('0'<=hi) && (hi<='9') ) { - hi -= '0'; - } else if( ('a'<= hi) && (hi<= 'f') ) { - hi -= ('a'-10); - } else if( ('A'<=hi) && (hi<='F') ) { - hi -= ('A'-10); + if (window == 0) { + window = new Window (WINDOW_TOPLEVEL); } + + Gtk::Label foo; - lo = (int) loc; + window->add (foo); + + foo.set_name (style); + foo.ensure_style (); - if( ('0'<=lo) && (lo<='9') ) { - lo -= '0'; - } else if( ('a'<=lo) && (lo<='f') ) { - lo -= ('a'-10); - } else if( ('A'<=lo) && (lo<='F') ) { - lo -= ('A'-10); + GtkRcStyle* waverc = foo.get_style()->gobj()->rc_style; + + if (waverc) { + if (attr == "fg") { + r = waverc->fg[state].red / 257; + g = waverc->fg[state].green / 257; + b = waverc->fg[state].blue / 257; + + /* what a hack ... "a" is for "active" */ + if (state == Gtk::STATE_NORMAL && rgba) { + a = waverc->fg[GTK_STATE_ACTIVE].red / 257; + } + } else if (attr == "bg") { + r = g = b = 0; + r = waverc->bg[state].red / 257; + g = waverc->bg[state].green / 257; + b = waverc->bg[state].blue / 257; + } else if (attr == "base") { + r = waverc->base[state].red / 257; + g = waverc->base[state].green / 257; + b = waverc->base[state].blue / 257; + } else if (attr == "text") { + r = waverc->text[state].red / 257; + g = waverc->text[state].green / 257; + b = waverc->text[state].blue / 257; + } + } else { + warning << string_compose (_("missing RGBA style for \"%1\""), style) << endl; } - return lo + (16 * hi); + window->remove (); + + if (state == Gtk::STATE_NORMAL && rgba) { + return (uint32_t) RGBA_TO_UINT(r,g,b,a); + } else { + return (uint32_t) RGB_TO_UINT(r,g,b); + } } -void -url_decode (string& url) + +Gdk::Color +color_from_style (string widget_style_name, int state, string attr) { - string::iterator last; - string::iterator next; + GtkStyle* style; - for (string::iterator i = url.begin(); i != url.end(); ++i) { - if ((*i) == '+') { - *i = ' '; - } + style = gtk_rc_get_style_by_paths (gtk_settings_get_default(), + widget_style_name.c_str(), + 0, G_TYPE_NONE); + + if (!style) { + error << string_compose (_("no style found for %1, using red"), style) << endmsg; + return Gdk::Color ("red"); } - if (url.length() <= 3) { - return; + cerr << "got style for " << widget_style_name << endl; + + if (attr == "fg") { + return Gdk::Color (&style->fg[state]); } - last = url.end(); + if (attr == "bg") { + cerr << "returning color from bg\n"; + return Gdk::Color (&style->bg[state]); + } - --last; /* points at last char */ - --last; /* points at last char - 1 */ + if (attr == "light") { + return Gdk::Color (&style->light[state]); + } - for (string::iterator i = url.begin(); i != last; ) { + if (attr == "dark") { + return Gdk::Color (&style->dark[state]); + } - if (*i == '%') { + if (attr == "mid") { + return Gdk::Color (&style->mid[state]); + } - next = i; + if (attr == "text") { + return Gdk::Color (&style->text[state]); + } - url.erase (i); - - i = next; - ++next; - - if (isxdigit (*i) && isxdigit (*next)) { - /* replace first digit with char */ - *i = int_from_hex (*i,*next); - ++i; /* points at 2nd of 2 digits */ - url.erase (i); - } - } else { - ++i; - } + if (attr == "base") { + return Gdk::Color (&style->base[state]); } + + if (attr == "text_aa") { + return Gdk::Color (&style->text_aa[state]); + } + + error << string_compose (_("unknown style attribute %1 requested for color; using \"red\""), attr) << endmsg; + return Gdk::Color ("red"); } -Pango::FontDescription -get_font_for_style (string widgetname) + +bool +canvas_item_visible (ArdourCanvas::Item* item) { - Gtk::Label foobar; - Glib::RefPtr