13
0

move 2 other non-ardour-specific utility functions into gtkmm2ext

git-svn-id: svn://localhost/ardour2/branches/3.0@10932 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
Paul Davis 2011-12-07 13:08:00 +00:00
parent 2f837b66f7
commit bb737997ae
4 changed files with 6 additions and 102 deletions

View File

@ -17,6 +17,8 @@
*/
#include "gtkmm2ext/utils.h"
#include "ardour/route_group.h"
#include "editor_group_tabs.h"
#include "editor.h"
@ -96,7 +98,7 @@ EditorGroupTabs::draw_tab (cairo_t* cr, Tab const & tab) const
cairo_fill (cr);
if (tab.group) {
pair<string, double> const f = fit_to_pixels (cr, tab.group->name(), tab.to - tab.from - arc_radius * 2);
pair<string, double> const f = Gtkmm2ext::fit_to_pixels (cr, tab.group->name(), tab.to - tab.from - arc_radius * 2);
cairo_text_extents_t ext;
cairo_text_extents (cr, tab.group->name().c_str(), &ext);

View File

@ -19,6 +19,8 @@
#include <boost/foreach.hpp>
#include "gtkmm2ext/utils.h"
#include "ardour/route_group.h"
#include "ardour/session.h"
#include "mixer_group_tabs.h"
@ -104,7 +106,7 @@ MixerGroupTabs::draw_tab (cairo_t* cr, Tab const & tab) const
cairo_fill (cr);
if (tab.group) {
pair<string, double> const f = fit_to_pixels (cr, tab.group->name(), tab.to - tab.from - arc_radius * 2);
pair<string, double> const f = Gtkmm2ext::fit_to_pixels (cr, tab.group->name(), tab.to - tab.from - arc_radius * 2);
cairo_text_extents_t ext;
cairo_text_extents (cr, tab.group->name().c_str(), &ext);

View File

@ -63,99 +63,6 @@ using Gtkmm2ext::Keyboard;
sigc::signal<void> DPIReset;
int
pixel_width (const string& str, Pango::FontDescription& font)
{
Label foo;
Glib::RefPtr<Pango::Layout> 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;
}
string
fit_to_pixels (const string& str, int pixel_width, Pango::FontDescription& font, int& actual_width, bool with_ellipses)
{
Label foo;
Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout ("");
string::size_type shorter_by = 0;
string txt;
layout->set_font_description (font);
actual_width = 0;
string ustr = str;
string::iterator last = ustr.end();
--last; /* now points at final entry */
txt = ustr;
while (!ustr.empty()) {
layout->set_text (txt);
int width, height;
Gtkmm2ext::get_ink_pixel_size (layout, width, height);
if (width < pixel_width) {
actual_width = width;
break;
}
ustr.erase (last--);
shorter_by++;
if (with_ellipses && shorter_by > 3) {
txt = ustr;
txt += "...";
} else {
txt = 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<std::string, double>
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.

View File

@ -44,14 +44,7 @@ namespace Gtk {
extern sigc::signal<void> DPIReset;
std::string fit_to_pixels (const std::string&, int pixel_width, Pango::FontDescription& font, int& actual_width, bool with_ellipses = false);
std::pair<std::string, double> fit_to_pixels (cairo_t *, std::string, double);
int pixel_width (const std::string& str, Pango::FontDescription& font);
gint just_hide_it (GdkEventAny*, Gtk::Window*);
void allow_keyboard_focus (bool);
void add_item_with_sensitivity (Gtk::Menu_Helpers::MenuList &, Gtk::Menu_Helpers::MenuElem, bool);
unsigned char* xpm2rgb (const char** xpm, uint32_t& w, uint32_t& h);