X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=gtk2_ardour%2Futils.cc;h=72b128c02fa44cebc5b0f4e49d525a3d2c86f69b;hb=bcf885b4be6c20c65b076640df9e3c9a3e52d5b7;hp=fde9d545bbf77149996093ace177dc2c09f8b120;hpb=49ab3aa0b3e78b635c22512b30f44b951c1401f7;p=ardour.git diff --git a/gtk2_ardour/utils.cc b/gtk2_ardour/utils.cc index fde9d545bb..72b128c02f 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,24 +15,33 @@ 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/configuration.h" + +#include "ardour/filesystem_paths.h" #include "ardour_ui.h" +#include "public_editor.h" #include "keyboard.h" #include "utils.h" #include "i18n.h" @@ -43,13 +52,32 @@ using namespace std; using namespace Gtk; using namespace sigc; using namespace Glib; +using namespace PBD; + +sigc::signal DPIReset; + +int +pixel_width (const ustring& str, Pango::FontDescription& font) +{ + Label foo; + Glib::RefPtr layout = foo.create_pango_layout (""); + + layout->set_font_description (font); + layout->set_text (str); + + int width, height; + Gtkmm2ext::get_ink_pixel_size (layout, width, height); + return width; +} ustring -fit_to_pixels (const ustring& str, int pixel_width, Pango::FontDescription& font, int& actual_width) +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; + layout->set_font_description (font); actual_width = 0; @@ -58,9 +86,11 @@ fit_to_pixels (const ustring& str, int pixel_width, Pango::FontDescription& font ustring::iterator last = ustr.end(); --last; /* now points at final entry */ + txt = ustr; + while (!ustr.empty()) { - layout->set_text (ustr); + layout->set_text (txt); int width, height; Gtkmm2ext::get_ink_pixel_size (layout, width, height); @@ -69,19 +99,77 @@ fit_to_pixels (const ustring& str, int pixel_width, Pango::FontDescription& font actual_width = width; break; } - - ustr.erase (last); - --last; + + ustr.erase (last--); + shorter_by++; + + if (with_ellipses && shorter_by > 3) { + txt = ustr; + txt += "..."; + } else { + txt = ustr; + } } - return ustr; + return txt; } +/** 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) +{ + /* XXX hopefully there exists a more efficient way of doing this */ + + bool abbreviated = false; + uint32_t width = 0; + + while (1) { + cairo_text_extents_t ext; + cairo_text_extents (cr, name.c_str(), &ext); + + if (ext.width < avail || name.length() <= 4) { + width = ext.width; + break; + } + + if (abbreviated) { + name = name.substr (0, name.length() - 4) + "..."; + } else { + name = name.substr (0, name.length() - 3) + "..."; + abbreviated = true; + } + } + + return std::make_pair (name, width); +} + + +/** 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) +{ + m.push_back (e); + if (!s) { + m.back().set_sensitive (false); + } +} + + 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: @@ -99,23 +187,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[] // @@ -151,8 +239,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")) { @@ -167,7 +255,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[] // @@ -198,7 +286,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 @@ -209,79 +297,35 @@ get_canvas_points (string who, uint32_t npoints) return new ArdourCanvas::Points (npoints); } -Pango::FontDescription +Pango::FontDescription* get_font_for_style (string widgetname) { Gtk::Window window (WINDOW_TOPLEVEL); Gtk::Label foobar; - Glib::RefPtr