Skip to content

Commit

Permalink
Added text estimation suppot for tk::Button widget
Browse files Browse the repository at this point in the history
  • Loading branch information
sadko4u committed Sep 18, 2024
1 parent c85412d commit 8893557
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*******************************************************************************

=== 1.0.24 ===
* Added text estimation suppot for tk::Button widget.
* Added aggregate size computation (enabled by default) for tk::TabControl widget.
* Fixed bug in bookmak editor (set first/last actions) of the tk::FileDialog widget.
* Updated build scripts.
Expand Down
18 changes: 18 additions & 0 deletions include/lsp-plug.in/tk/widgets/simple/Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ namespace lsp
S_HOVER = (1 << 10)
};

typedef struct estimation_t
{
LSPString text;
float scaling;
float fscaling;
ssize_t min_width;
ssize_t min_height;
ws::font_parameters_t fp;
ws::text_parameters_t tp;
} estimation_t;

protected:
size_t nState;
size_t nBMask;
Expand Down Expand Up @@ -140,8 +151,11 @@ namespace lsp
prop::Position sTextDownShift;
prop::Position sTextPressedShift;

lltl::parray<prop::String> vEstimations; // Estimation string

protected:
void update_mode(button_mode_t mode);
void estimate_string_size(estimation_t *e, tk::String *s);

static status_t slot_on_change(Widget *sender, void *ptr, void *data);
static status_t slot_on_submit(Widget *sender, void *ptr, void *data);
Expand Down Expand Up @@ -169,6 +183,10 @@ namespace lsp

virtual status_t init() override;

public:
void clear_text_estimations();
tk::String *add_text_estimation();

public:
LSP_TK_PROPERTY(Color, color, &sColor)
LSP_TK_PROPERTY(Color, text_color, &sTextColor)
Expand Down
75 changes: 63 additions & 12 deletions src/main/widgets/simple/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ namespace lsp
Button::~Button()
{
nFlags |= FINALIZED;
clear_text_estimations();
}

status_t Button::init()
Expand Down Expand Up @@ -372,6 +373,53 @@ namespace lsp
radius);
}

void Button::clear_text_estimations()
{
size_t removed = 0;
for (lltl::iterator<prop::String> it = vEstimations.values(); it; ++it)
{
prop::String *s = it.get();
if (s != NULL)
{
++removed;
delete s;
}
}
vEstimations.clear();
if (removed > 0)
query_resize();
}

tk::String *Button::add_text_estimation()
{
prop::String *s = new prop::String(&sProperties);
if (s == NULL)
return NULL;
s->bind(&sStyle, pDisplay->dictionary());

if (vEstimations.add(s))
return s;

delete s;
return NULL;
}

void Button::estimate_string_size(estimation_t *e, tk::String *s)
{
if (s == NULL)
return;

// Form the text string
s->format(&e->text);
sTextAdjust.apply(&e->text);

// Estimate sizes
sFont.get_multitext_parameters(pDisplay, &e->tp, e->fscaling, &e->text);

e->min_width = lsp_max(e->min_width, ceilf(e->tp.Width));
e->min_height = lsp_max(e->min_height, ceilf(lsp_max(e->tp.Height, e->fp.Height)));
}

void Button::draw(ws::ISurface *s)
{
ws::IGradient *g = NULL;
Expand Down Expand Up @@ -610,7 +658,6 @@ namespace lsp
ws::rectangle_t xr;

float scaling = lsp_max(0.0f, sScaling.get());
float fscaling = lsp_max(0.0f, scaling * sFontScaling.get());

xr.nWidth = 0;
xr.nHeight = 0;
Expand All @@ -621,17 +668,21 @@ namespace lsp

if ((text.length() > 0) && (!sTextClip.get()))
{
ws::font_parameters_t fp;
ws::text_parameters_t tp;

sFont.get_parameters(pDisplay, fscaling, &fp);
sFont.get_multitext_parameters(pDisplay, &tp, fscaling, &text);

ssize_t tminw = ceil(tp.Width);
ssize_t tminh = ceil(lsp_max(tp.Height, fp.Height));

xr.nWidth = lsp_max(xr.nWidth, tminw);
xr.nHeight = lsp_max(xr.nHeight, tminh);
// Form the estimation parameters
estimation_t e;
e.scaling = scaling;
e.fscaling = lsp_max(0.0f, e.scaling * sFontScaling.get());
e.min_width = 0;
e.min_height = 0;
sFont.get_parameters(pDisplay, e.fscaling, &e.fp);

// Estimate the size of the label
for (lltl::iterator<prop::String> it = vEstimations.values(); it; ++it)
estimate_string_size(&e, it.get());
estimate_string_size(&e, &sText);

xr.nWidth = lsp_max(xr.nWidth, e.min_width);
xr.nHeight = lsp_max(xr.nHeight, e.min_height);

sTextPadding.add(&xr, scaling);
}
Expand Down

0 comments on commit 8893557

Please sign in to comment.