add link_color property of label defn, passed to font::ttext

This commit permits link_color to be specified in any label defn,
at the same place that link_aware is specified. It will be passed
through to tcanvas and finally font::ttext in a similar manner as
link awareness, and used for the color parameter in any link
formatting. We choose a default value of #ffff00 everywhere,
matching the definition currently in the help browser for cross-
references.
This commit is contained in:
Chris Beck 2014-10-17 05:06:51 -04:00
parent de1c04e187
commit 5f3dbe1d98
11 changed files with 87 additions and 6 deletions

View file

@ -24,6 +24,7 @@
text_font_style = {FONT_STYLE}
link_aware = true
link_color = #ffff00
[state_enabled]
@ -41,6 +42,7 @@
text = "(text)"
text_markup = "(text_markup)"
text_link_aware = "(text_link_aware)"
text_link_color = "(text_link_color)"
[/text]
[/draw]
@ -63,6 +65,7 @@
text = "(text)"
text_markup = "(text_markup)"
text_link_aware = "(text_link_aware)"
text_link_color = "(text_link_color)"
[/text]
[/draw]

View file

@ -371,6 +371,11 @@
type="f_bool"
default=false
[/key]
[key]
name="text_link_color"
type="string"
default="#ffff00"
[/key]
[key]
name="w"
type="f_unsigned"
@ -690,6 +695,11 @@
type="bool"
default="false"
[/key]
[key]
name="link_color"
type="string"
default="#ffff00"
[/key]
[/tag]
[/tag]
[tag]

View file

@ -1273,6 +1273,9 @@ private:
/** The link aware switch of the text. */
tformula<bool> link_aware_;
/** The link color of the text. */
tformula<std::string> link_color_;
/** The maximum width for the text. */
tformula<int> maximum_width_;
@ -1308,6 +1311,8 @@ private:
* text_markup & f_bool & false & Can the text have mark-up? $
* text_link_aware & f_bool & false &
* Is the text link aware? $
* text_link_color & f_string & "#ffff00" &
* The color of links in the text $
* maximum_width & f_int & -1 & The maximum width the text is allowed to
* be. $
* maximum_height & f_int & -1 & The maximum height the text is allowed
@ -1342,6 +1347,7 @@ ttext::ttext(const config& cfg)
, text_(cfg["text"])
, text_markup_(cfg["text_markup"], false)
, link_aware_(cfg["text_link_aware"], false)
, link_color_(cfg["text_link_color"], "#ffff00")
, maximum_width_(cfg["maximum_width"], -1)
, characters_per_line_(cfg["text_characters_per_line"])
, maximum_height_(cfg["maximum_height"], -1)
@ -1371,7 +1377,8 @@ void ttext::draw(surface& canvas,
static font::ttext text_renderer;
text_renderer.set_link_aware(link_aware_(variables));
text_renderer.set_link_aware(link_aware_(variables))
.set_link_color(link_color_(variables));
text_renderer.set_text(text, text_markup_(variables));
text_renderer.set_font_size(font_size_)

View file

@ -67,6 +67,7 @@ tlabel_definition::tlabel_definition(const config& cfg)
tlabel_definition::tresolution::tresolution(const config& cfg)
: tresolution_definition_(cfg)
, link_aware(cfg["link_aware"].to_bool(false))
, link_color(cfg["link_color"].str().size() > 0 ? cfg["link_color"].str() : "#ffff00")
{
// Note the order should be the same as the enum tstate is label.hpp.
state.push_back(tstate_definition(cfg.child("state_enabled")));

View file

@ -30,6 +30,7 @@ struct tlabel_definition : public tcontrol_definition
explicit tresolution(const config& cfg);
bool link_aware;
std::string link_color;
};
};

View file

@ -354,6 +354,7 @@ void tcontrol::update_canvas()
canvas.set_variable("text", variant(label_));
canvas.set_variable("text_markup", variant(use_markup_));
canvas.set_variable("text_link_aware", variant(get_link_aware()));
canvas.set_variable("text_link_color", variant(get_link_color()));
canvas.set_variable("text_alignment",
variant(encode_text_alignment(text_alignment_)));
canvas.set_variable("text_maximum_width", variant(max_width));
@ -438,7 +439,9 @@ tpoint tcontrol::get_best_text_size(const tpoint& minimum_size,
const tpoint border(config_->text_extra_width, config_->text_extra_height);
tpoint size = minimum_size - border;
renderer_.set_link_aware(get_link_aware());
renderer_.set_link_aware(get_link_aware())
.set_link_color(get_link_color());
renderer_.set_text(label_, use_markup_);
renderer_.set_font_size(config_->text_font_size);

View file

@ -156,6 +156,19 @@ public:
return false;
}
/**
* Returns the color string to be used with links.
*
* This value is used to call @ref ttext::set_link_color
* (indirectly).
*
* @returns The link color string. This impl returns "#ffff00".
*
*/
virtual std::string get_link_color() const
{
return "#ffff00";
}
/**
* See @ref twidget::layout_initialise.

View file

@ -43,6 +43,7 @@ tlabel::tlabel()
, can_wrap_(false)
, characters_per_line_(0)
, link_aware_(false)
, link_color_("#ffff00")
{
connect_signal<event::LEFT_BUTTON_CLICK>(boost::bind(&tlabel::signal_handler_left_button_click, this, _2, _3));
connect_signal<event::RIGHT_BUTTON_CLICK>(boost::bind(&tlabel::signal_handler_right_button_click, this, _2, _3));
@ -63,6 +64,11 @@ bool tlabel::get_link_aware() const
return link_aware_;
}
std::string tlabel::get_link_color() const
{
return link_color_;
}
void tlabel::set_active(const bool active)
{
if(get_active() != active) {
@ -101,6 +107,16 @@ void tlabel::set_link_aware(bool link_aware)
set_is_dirty(true);
}
void tlabel::set_link_color(const std::string & color)
{
if(color == link_color_) {
return;
}
link_color_ = color;
update_canvas();
set_is_dirty(true);
}
void tlabel::set_state(const tstate state)
{
if(state != state_) {
@ -126,6 +142,7 @@ void tlabel::load_config_extra()
assert(conf);
set_link_aware(conf->link_aware);
set_link_color(conf->link_color);
}

View file

@ -35,6 +35,9 @@ public:
/** See @ref tcontrol::get_link_aware. */
virtual bool get_link_aware() const OVERRIDE;
/** See @ref tcontrol::get_link_aware. */
virtual std::string get_link_color() const OVERRIDE;
/** See @ref tcontrol::set_active. */
virtual void set_active(const bool active) OVERRIDE;
@ -58,6 +61,8 @@ public:
void set_link_aware(bool l);
void set_link_color(const std::string & color);
private:
/**
* Possible states of the widget.
@ -96,6 +101,11 @@ private:
*/
bool link_aware_;
/**
* What color links will be rendered in.
*/
std::string link_color_;
/** See @ref tcontrol::get_control_type. */
virtual const std::string& get_control_type() const OVERRIDE;

View file

@ -537,6 +537,17 @@ ttext& ttext::set_link_aware(bool b)
return *this;
}
ttext& ttext::set_link_color(const std::string & color)
{
if(color != link_color_) {
link_color_ = color;
calculation_dirty_ = true;
surface_dirty_ = true;
}
return *this;
}
namespace {
/** Small helper class to make sure the font object is destroyed properly. */
@ -786,8 +797,6 @@ void ttext::create_surface_buffer(const size_t size) const
memset(surface_buffer_, 0, size);
}
static std::string handle_token(const std::string & token);
bool ttext::set_markup(const std::string & text) {
if (!link_aware_) {
return set_markup_helper(text);
@ -819,10 +828,10 @@ static bool looks_like_url(const std::string & str)
return (str.size() >= 8) && ((str.substr(0,7) == "http://") || (str.substr(0,8) == "https://"));
}
static std::string handle_token(const std::string & token)
std::string ttext::handle_token(const std::string & token) const
{
if (looks_like_url(token)) {
return "<span underline=\'single\' color=\'#8888ff\'>" + token + "</span>";
return "<span underline=\'single\' color=\'" + link_color_ + "\'>" + token + "</span>";
} else {
return token;
}

View file

@ -243,6 +243,8 @@ public:
bool link_aware() const { return link_aware_; }
ttext& set_link_aware(bool b);
ttext& set_link_color(const std::string & color);
private:
/***** ***** ***** ***** Pango variables ***** ***** ***** *****/
@ -271,6 +273,9 @@ private:
/** Are hyperlinks in the text marked-up, and will get_link return them. */
bool link_aware_;
/** The color to render links in. */
std::string link_color_;
/** The font size to draw. */
unsigned font_size_;
@ -397,6 +402,8 @@ private:
bool set_markup(const std::string& text);
bool set_markup_helper(const std::string & text);
std::string handle_token(const std::string & token) const;
};
} // namespace font