X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=gtk2_ardour%2Futils.cc;h=aa57a3b773cb87a60f3ab79fd6a9542958477e93;hb=e69aca28426dd17a0f82ea01c7c98e217b4fdcc3;hp=91f69a444ec63aea7e1eb7da313357cbeb92f386;hpb=af707897735a34ab1a82c7307dc61b12c14027a9;p=ardour.git diff --git a/gtk2_ardour/utils.cc b/gtk2_ardour/utils.cc index 91f69a444e..aa57a3b773 100644 --- a/gtk2_ardour/utils.cc +++ b/gtk2_ardour/utils.cc @@ -1,7 +1,7 @@ /* - Copyright (C) 2003 Paul Davis + Copyright (C) 2003 Paul Davis - This program is free software; you can redistribute it and/or modify + This program is free software; you an redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. @@ -15,156 +15,162 @@ 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 "pbd/file_utils.h" + #include -#include +#include "ardour/configuration.h" +#include "ardour/rc_configuration.h" + +#include "ardour/filesystem_paths.h" #include "ardour_ui.h" +#include "public_editor.h" #include "keyboard.h" #include "utils.h" #include "i18n.h" #include "rgb_macros.h" #include "canvas_impl.h" +#include "gui_thread.h" using namespace std; using namespace Gtk; -using namespace sigc; using namespace Glib; +using namespace PBD; +using Gtkmm2ext::Keyboard; -string -short_version (string orig, string::size_type target_length) +sigc::signal DPIReset; + +int +pixel_width (const ustring& str, Pango::FontDescription& font) { - /* this tries to create a recognizable abbreviation - of "orig" by removing characters until we meet - a certain target length. + Label foo; + Glib::RefPtr layout = foo.create_pango_layout (""); - note that we deliberately leave digits in the result - without modification. - */ + layout->set_font_description (font); + layout->set_text (str); + int width, height; + Gtkmm2ext::get_ink_pixel_size (layout, width, height); + return width; +} - string::size_type pos; +ustring +fit_to_pixels (const ustring& str, int pixel_width, Pango::FontDescription& font, int& actual_width, bool with_ellipses) +{ + Label foo; + Glib::RefPtr layout = foo.create_pango_layout (""); + ustring::size_type shorter_by = 0; + ustring txt; - /* remove white-space and punctuation, starting at end */ + layout->set_font_description (font); - while (orig.length() > target_length) { - if ((pos = orig.find_last_of (_("\"\n\t ,<.>/?:;'[{}]~`!@#$%^&*()_-+="))) == string::npos) { - break; - } - orig.replace (pos, 1, ""); - } + actual_width = 0; - /* remove lower-case vowels, starting at end */ + ustring ustr = str; + ustring::iterator last = ustr.end(); + --last; /* now points at final entry */ - while (orig.length() > target_length) { - if ((pos = orig.find_last_of (_("aeiou"))) == string::npos) { - break; - } - orig.replace (pos, 1, ""); - } + txt = ustr; - /* remove upper-case vowels, starting at end */ + while (!ustr.empty()) { - while (orig.length() > target_length) { - if ((pos = orig.find_last_of (_("AEIOU"))) == string::npos) { - break; - } - orig.replace (pos, 1, ""); - } + layout->set_text (txt); - /* remove lower-case consonants, starting at end */ + int width, height; + Gtkmm2ext::get_ink_pixel_size (layout, width, height); - while (orig.length() > target_length) { - if ((pos = orig.find_last_of (_("bcdfghjklmnpqrtvwxyz"))) == string::npos) { + if (width < pixel_width) { + actual_width = width; break; } - orig.replace (pos, 1, ""); - } - /* remove upper-case consonants, starting at end */ + ustr.erase (last--); + shorter_by++; - while (orig.length() > target_length) { - if ((pos = orig.find_last_of (_("BCDFGHJKLMNPQRTVWXYZ"))) == string::npos) { - break; + if (with_ellipses && shorter_by > 3) { + txt = ustr; + txt += "..."; + } else { + txt = ustr; } - orig.replace (pos, 1, ""); } - /* whatever the length is now, use it */ - - return orig; + return txt; } -string -fit_to_pixels (const string & str, int pixel_width, const string & font) +/** Try to fit a string into a given horizontal space by ellipsizing it. + * @param cr Cairo context in which the text will be plotted. + * @param name Text. + * @param avail Available horizontal space. + * @return (Text, possibly ellipsized) and (horizontal size of text) + */ + +std::pair +fit_to_pixels (cairo_t* cr, std::string name, double avail) { - 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); + /* XXX hopefully there exists a more efficient way of doing this */ + + bool abbreviated = false; + uint32_t width = 0; - layout->set_font_description (fontdesc); - layout->get_pixel_size (width, height); + while (1) { + cairo_text_extents_t ext; + cairo_text_extents (cr, name.c_str(), &ext); - if (width < (pixel_width)) { + if (ext.width < avail || name.length() <= 4) { + width = ext.width; break; } - --namelen; - cstr[namelen] = '\0'; - + if (abbreviated) { + name = name.substr (0, name.length() - 4) + "..."; + } else { + name = name.substr (0, name.length() - 3) + "..."; + abbreviated = true; + } } - return cstr; + return std::make_pair (name, width); } -int -atoi (const string& s) -{ - return atoi (s.c_str()); -} - -double -atof (const string& s) -{ - return atof (s.c_str()); -} -vector -internationalize (const char **array) +/** Add an element to a menu, settings its sensitivity. + * @param m Menu to add to. + * @param e Element to add. + * @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) { - vector v; - - for (uint32_t i = 0; array[i]; ++i) { - v.push_back (_(array[i])); + m.push_back (e); + if (!s) { + m.back().set_sensitive (false); } - - return v; } + gint -just_hide_it (GdkEventAny *ev, Gtk::Window *win) +just_hide_it (GdkEventAny */*ev*/, Gtk::Window *win) { - win->hide_all (); - return TRUE; + win->hide (); + return 0; } /* xpm2rgb copied from nixieclock, which bore the legend: @@ -182,23 +188,23 @@ xpm2rgb (const char** xpm, uint32_t& w, uint32_t& h) uint32_t t, x, y, colors, cpp; unsigned char c; unsigned char *savergb, *rgb; - + // PARSE HEADER - + if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) { error << string_compose (_("bad XPM header %1"), xpm[0]) << endmsg; 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) { sscanf (xpm[t+1], "%c c #%lx", &c, &val); vals[c] = val; } - + // COLORMAP -> RGB CONVERSION // Get low 3 bytes from vals[] // @@ -234,8 +240,8 @@ 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 if (strstr (xpm[1], "None")) { @@ -250,7 +256,7 @@ xpm2rgba (const char** xpm, uint32_t& w, uint32_t& h) sscanf (xpm[t+1], "%c c #%lx", &c, &val); vals[c] = val; } - + // COLORMAP -> RGB CONVERSION // Get low 3 bytes from vals[] // @@ -281,7 +287,7 @@ xpm2rgba (const char** xpm, uint32_t& w, uint32_t& h) } ArdourCanvas::Points* -get_canvas_points (string who, uint32_t npoints) +get_canvas_points (string /*who*/, uint32_t npoints) { // cerr << who << ": wants " << npoints << " canvas points" << endl; #ifdef TRAP_EXCESSIVE_POINT_REQUESTS @@ -292,152 +298,35 @@ get_canvas_points (string who, uint32_t npoints) return new ArdourCanvas::Points (npoints); } -static int32_t -int_from_hex (char hic, char loc) -{ - int hi; /* hi byte */ - int lo; /* low byte */ - - hi = (int) hic; - - 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); - } - - lo = (int) loc; - - 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); - } - - return lo + (16 * hi); -} - -void -url_decode (string& url) -{ - string::iterator last; - string::iterator next; - - for (string::iterator i = url.begin(); i != url.end(); ++i) { - if ((*i) == '+') { - *i = ' '; - } - } - - if (url.length() <= 3) { - return; - } - - last = url.end(); - - --last; /* points at last char */ - --last; /* points at last char - 1 */ - - for (string::iterator i = url.begin(); i != last; ) { - - if (*i == '%') { - - next = i; - - 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; - } - } -} - -Pango::FontDescription +Pango::FontDescription* get_font_for_style (string widgetname) { Gtk::Window window (WINDOW_TOPLEVEL); Gtk::Label foobar; - Glib::RefPtr