13
0

Add support for Pango Markup to ArdourButton

This commit is contained in:
Robin Gareus 2016-12-10 15:52:48 +01:00
parent 6a429cfb7c
commit b552ed886f
2 changed files with 30 additions and 12 deletions

View File

@ -59,6 +59,7 @@ ArdourButton::Element ArdourButton::just_led_default_elements = ArdourButton::El
ArdourButton::ArdourButton (Element e) ArdourButton::ArdourButton (Element e)
: _sizing_text("") : _sizing_text("")
, _markup (false)
, _elements (e) , _elements (e)
, _icon (Gtkmm2ext::ArdourIcon::NoIcon) , _icon (Gtkmm2ext::ArdourIcon::NoIcon)
, _tweaks (Tweaks (0)) , _tweaks (Tweaks (0))
@ -68,7 +69,7 @@ ArdourButton::ArdourButton (Element e)
, _text_width (0) , _text_width (0)
, _text_height (0) , _text_height (0)
, _diameter (0) , _diameter (0)
, _corner_radius (2.5) , _corner_radius (3.5)
, _corner_mask (0xf) , _corner_mask (0xf)
, _angle(0) , _angle(0)
, _xalign(.5) , _xalign(.5)
@ -104,7 +105,9 @@ ArdourButton::ArdourButton (Element e)
ArdourButton::ArdourButton (const std::string& str, Element e) ArdourButton::ArdourButton (const std::string& str, Element e)
: _sizing_text("") : _sizing_text("")
, _markup (false)
, _elements (e) , _elements (e)
, _icon (Gtkmm2ext::ArdourIcon::NoIcon)
, _tweaks (Tweaks (0)) , _tweaks (Tweaks (0))
, _char_pixel_width (0) , _char_pixel_width (0)
, _char_pixel_height (0) , _char_pixel_height (0)
@ -112,7 +115,7 @@ ArdourButton::ArdourButton (const std::string& str, Element e)
, _text_width (0) , _text_width (0)
, _text_height (0) , _text_height (0)
, _diameter (0) , _diameter (0)
, _corner_radius (2.5) , _corner_radius (3.5)
, _corner_mask (0xf) , _corner_mask (0xf)
, _angle(0) , _angle(0)
, _xalign(.5) , _xalign(.5)
@ -178,18 +181,29 @@ ArdourButton::set_layout_font (const Pango::FontDescription& fd)
} }
void void
ArdourButton::set_text (const std::string& str) ArdourButton::set_text_internal () {
assert (_layout);
if (_markup) {
_layout->set_markup (_text);
} else {
_layout->set_text (_text);
}
}
void
ArdourButton::set_text (const std::string& str, bool markup)
{ {
if (_text == str) { if (_text == str && _markup == markup) {
return; return;
} }
_text = str; _text = str;
_markup = markup;
if (!is_realized()) { if (!is_realized()) {
return; return;
} }
ensure_layout (); ensure_layout ();
if (_layout && _layout->get_text() != _text) { if (_layout && _layout->get_text() != _text) {
_layout->set_text (_text); set_text_internal ();
/* on_size_request() will fill in _text_width/height /* on_size_request() will fill in _text_width/height
* so queue it even if _sizing_text != "" */ * so queue it even if _sizing_text != "" */
queue_resize (); queue_resize ();
@ -549,9 +563,9 @@ ArdourButton::on_realize()
ensure_layout (); ensure_layout ();
if (_layout) { if (_layout) {
if (_layout->get_text() != _text) { if (_layout->get_text() != _text) {
_layout->set_text (_text); set_text_internal ();
queue_resize ();
} }
queue_resize ();
} }
} }
@ -572,7 +586,8 @@ ArdourButton::on_size_request (Gtk::Requisition* req)
if (_elements & Text) { if (_elements & Text) {
ensure_layout(); ensure_layout();
_layout->set_text (_text); set_text_internal ();
/* render() needs the size of the displayed text */ /* render() needs the size of the displayed text */
_layout->get_pixel_size (_text_width, _text_height); _layout->get_pixel_size (_text_width, _text_height);
@ -597,7 +612,7 @@ ArdourButton::on_size_request (Gtk::Requisition* req)
req->width += sizing_text_width; req->width += sizing_text_width;
if (!_sizing_text.empty()) { if (!_sizing_text.empty()) {
_layout->set_text (_text); /* restore display text */ set_text_internal (); /* restore display text */
} }
} }
@ -1200,7 +1215,7 @@ ArdourButton::recalc_char_pixel_geometry ()
Glib::ustring gx(x); Glib::ustring gx(x);
_char_avg_pixel_width = w / (float)gx.size(); _char_avg_pixel_width = w / (float)gx.size();
_char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width)); _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
_layout->set_text (_text); set_text_internal (); /* restore display text */
} }
void void

View File

@ -78,8 +78,9 @@ class ArdourButton : public CairoWidget , public Gtkmm2ext::Activatable
void set_corner_radius (float); void set_corner_radius (float);
void set_text (const std::string&); void set_text (const std::string&, bool markup = false);
const std::string& get_text () {return _text;} const std::string& get_text () { return _text; }
bool get_markup () const { return _markup; }
void set_angle (const double); void set_angle (const double);
void set_alignment (const float, const float); void set_alignment (const float, const float);
void get_alignment (float& xa, float& ya) {xa = _xalign; ya = _yalign;}; void get_alignment (float& xa, float& ya) {xa = _xalign; ya = _yalign;};
@ -143,11 +144,13 @@ class ArdourButton : public CairoWidget , public Gtkmm2ext::Activatable
Glib::RefPtr<Gdk::Pixbuf> _pixbuf; Glib::RefPtr<Gdk::Pixbuf> _pixbuf;
std::string _text; std::string _text;
std::string _sizing_text; std::string _sizing_text;
bool _markup;
Element _elements; Element _elements;
Gtkmm2ext::ArdourIcon::Icon _icon; Gtkmm2ext::ArdourIcon::Icon _icon;
Tweaks _tweaks; Tweaks _tweaks;
BindingProxy binding_proxy; BindingProxy binding_proxy;
void set_text_internal ();
void recalc_char_pixel_geometry (); void recalc_char_pixel_geometry ();
unsigned int _char_pixel_width; unsigned int _char_pixel_width;
unsigned int _char_pixel_height; unsigned int _char_pixel_height;