Skip to content

Commit

Permalink
Merge pull request #300 from cbeck88/clickable_labels
Browse files Browse the repository at this point in the history
Clickable hyperlinks in gui2 labels
  • Loading branch information
cbeck88 committed Oct 19, 2014
2 parents 007da9f + 536c200 commit 455f6f7
Show file tree
Hide file tree
Showing 11 changed files with 430 additions and 8 deletions.
7 changes: 7 additions & 0 deletions data/gui/default/widget/label_default.cfg
Expand Up @@ -23,6 +23,9 @@
text_font_size = {FONT_SIZE}
text_font_style = {FONT_STYLE}

link_aware = true
link_color = #ffff00

[state_enabled]

[draw]
Expand All @@ -38,6 +41,8 @@
color = {FONT_COLOR_ENABLED}
text = "(text)"
text_markup = "(text_markup)"
text_link_aware = "(text_link_aware)"
text_link_color = "(text_link_color)"
[/text]

[/draw]
Expand All @@ -59,6 +64,8 @@
color = {FONT_COLOR_DISABLED}
text = "(text)"
text_markup = "(text_markup)"
text_link_aware = "(text_link_aware)"
text_link_color = "(text_link_color)"
[/text]

[/draw]
Expand Down
20 changes: 20 additions & 0 deletions data/gui/schema.cfg
Expand Up @@ -366,6 +366,16 @@
type="f_bool"
default=false
[/key]
[key]
name="text_link_aware"
type="f_bool"
default=false
[/key]
[key]
name="text_link_color"
type="string"
default="#ffff00"
[/key]
[key]
name="w"
type="f_unsigned"
Expand Down Expand Up @@ -680,6 +690,16 @@
max="1"
super="generic/state"
[/tag]
[key]
name="link_aware"
type="bool"
default="false"
[/key]
[key]
name="link_color"
type="string"
default="#ffff00"
[/key]
[/tag]
[/tag]
[tag]
Expand Down
15 changes: 15 additions & 0 deletions src/gui/auxiliary/canvas.cpp
Expand Up @@ -1270,6 +1270,12 @@ class ttext : public tcanvas::tshape
/** The text markup switch of the text. */
tformula<bool> text_markup_;

/** 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_;

Expand Down Expand Up @@ -1303,6 +1309,10 @@ class ttext : public tcanvas::tshape
* color & color & "" & The color of the text. $
* text & f_tstring & "" & The text to draw (translatable). $
* 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
Expand Down Expand Up @@ -1336,6 +1346,8 @@ ttext::ttext(const config& cfg)
, color_(decode_color(cfg["color"]))
, 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)
Expand Down Expand Up @@ -1364,6 +1376,9 @@ void ttext::draw(surface& canvas,
}

static font::ttext text_renderer;

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_)
Expand Down
14 changes: 14 additions & 0 deletions src/gui/auxiliary/widget_definition/label.cpp
Expand Up @@ -41,12 +41,24 @@ tlabel_definition::tlabel_definition(const config& cfg)
* The reason is that labels are often used as visual indication of the state
* of the widget it labels.
*
* Note: The above is outdated, if "link_aware" is enabled then there is interaction.
*
*
* The following states exist:
* * state_enabled, the label is enabled.
* * state_disabled, the label is disabled.
* @begin{parent}{name="gui/"}
* @begin{tag}{name="label_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
* @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
* @begin{table}{config}
* link_aware & f_bool & false & Whether the label is link aware. This means
* it is rendered with links highlighted,
* and responds to click events on those
* links. $
* link_color & string & #ffff00 & The color to render links with. This
* string will be used verbatim in pango
* markup for each link. $
* @end{table}
* @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
* @end{tag}{name="state_enabled"}
* @begin{tag}{name="state_disabled"}{min=0}{max=1}{super="generic/state"}
Expand All @@ -57,6 +69,8 @@ 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")));
Expand Down
3 changes: 3 additions & 0 deletions src/gui/auxiliary/widget_definition/label.hpp
Expand Up @@ -28,6 +28,9 @@ struct tlabel_definition : public tcontrol_definition
struct tresolution : public tresolution_definition_
{
explicit tresolution(const config& cfg);

bool link_aware;
std::string link_color;
};
};

Expand Down
25 changes: 25 additions & 0 deletions src/gui/widgets/control.cpp
Expand Up @@ -173,6 +173,16 @@ unsigned tcontrol::get_characters_per_line() const
return 0;
}

bool tcontrol::get_link_aware() const
{
return false;
}

std::string tcontrol::get_link_color() const
{
return "#ffff00";
}

void tcontrol::layout_initialise(const bool full_initialisation)
{
// Inherited.
Expand Down Expand Up @@ -353,6 +363,8 @@ 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));
Expand Down Expand Up @@ -437,6 +449,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())
.set_link_color(get_link_color());

renderer_.set_text(label_, use_markup_);

renderer_.set_font_size(config_->text_font_size);
Expand Down Expand Up @@ -543,4 +558,14 @@ void tcontrol::signal_handler_notify_remove_tooltip(const event::tevent event,
handled = true;
}

std::string tcontrol::get_label_token(const gui2::tpoint & position, const char * delim) const
{
return renderer_.get_token(position, delim);
}

std::string tcontrol::get_label_link(const gui2::tpoint & position) const
{
return renderer_.get_link(position);
}

} // namespace gui2
28 changes: 28 additions & 0 deletions src/gui/widgets/control.hpp
Expand Up @@ -141,6 +141,29 @@ class tcontrol : public twidget
*/
virtual unsigned get_characters_per_line() const;

/**
* Returns whether the label should be link_aware, in
* in rendering and in searching for links with get_link.
*
* This value is used to call @ref ttext::set_link_aware
* (indirectly).
*
* @returns The link aware status. This impl always
* always returns false.
*/
virtual bool get_link_aware() const;

/**
* 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;

/**
* See @ref twidget::layout_initialise.
*
Expand Down Expand Up @@ -421,6 +444,11 @@ class tcontrol : public twidget
int x_offset,
int y_offset) OVERRIDE;

/** Exposes font::ttext::get_token, for the text label of this control */
std::string get_label_token(const gui2::tpoint & position, const char * delimiters = " \n\r\t") const;

std::string get_label_link(const gui2::tpoint & position) const;

private:
#ifdef GUI2_EXPERIMENTAL_LISTBOX
/**
Expand Down

0 comments on commit 455f6f7

Please sign in to comment.