Skip to content

Commit 969d295

Browse files
committed
Added text size estimation feature to the tk::Label widget
1 parent 4668234 commit 969d295

File tree

3 files changed

+94
-37
lines changed

3 files changed

+94
-37
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
=== 1.0.21 ===
66
* Added empty text property for edit.
7+
* Added text size estimation feature to the tk::Label widget.
78
* Fixed illegal memory access when destroying Box, Grid and Menu widgets.
89
* Added tab.pointer property to the tk::TabControl.
910
* Updated build scripts.

include/lsp-plug.in/tk/widgets/simple/Label.h

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3-
* (C) 2020 Vladimir Sadovnikov <[email protected]>
2+
* Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/>
3+
* (C) 2024 Vladimir Sadovnikov <[email protected]>
44
*
55
* This file is part of lsp-tk-lib
66
* Created on: 6 июл. 2017 г.
@@ -65,6 +65,16 @@ namespace lsp
6565
F_MOUSE_IGN = 1 << 2,
6666
};
6767

68+
typedef struct estimation_t
69+
{
70+
LSPString text;
71+
float scaling;
72+
float fscaling;
73+
ws::size_limit_t *r;
74+
ws::font_parameters_t fp;
75+
ws::text_parameters_t tp;
76+
} estimation_t;
77+
6878
protected:
6979
size_t nMFlags;
7080
size_t nState;
@@ -79,21 +89,24 @@ namespace lsp
7989
prop::SizeConstraints sConstraints; // Size constraints
8090
prop::Padding sIPadding; // Internal padding
8191
prop::WidgetPtr<Menu> sPopup; // Popup menu
92+
lltl::parray<prop::String> vEstimations; // Estimation string
8293

8394
protected:
8495
static status_t slot_on_submit(Widget *sender, void *ptr, void *data);
8596
static status_t slot_on_before_popup(Widget *sender, void *ptr, void *data);
8697
static status_t slot_on_popup(Widget *sender, void *ptr, void *data);
8798

8899
protected:
89-
virtual void size_request(ws::size_limit_t *r);
90-
virtual void property_changed(Property *prop);
100+
void estimate_string_size(estimation_t *e, tk::String *s);
101+
102+
virtual void size_request(ws::size_limit_t *r) override;
103+
virtual void property_changed(Property *prop) override;
91104

92105
public:
93106
explicit Label(Display *dpy);
94-
virtual ~Label();
107+
virtual ~Label() override;
95108

96-
virtual status_t init();
109+
virtual status_t init() override;
97110

98111
public:
99112
LSP_TK_PROPERTY(TextLayout, text_layout, &sTextLayout)
@@ -108,22 +121,20 @@ namespace lsp
108121
LSP_TK_PROPERTY(WidgetPtr<Menu>, popup, &sPopup)
109122

110123
public:
111-
virtual void draw(ws::ISurface *s);
124+
void clear_text_estimations();
125+
tk::String *add_text_estimation();
112126

113-
virtual status_t on_mouse_in(const ws::event_t *e);
127+
public:
128+
virtual void draw(ws::ISurface *s) override;
114129

130+
public:
131+
virtual status_t on_mouse_in(const ws::event_t *e);
115132
virtual status_t on_mouse_out(const ws::event_t *e);
116-
117133
virtual status_t on_mouse_move(const ws::event_t *e);
118-
119134
virtual status_t on_mouse_down(const ws::event_t *e);
120-
121135
virtual status_t on_mouse_up(const ws::event_t *e);
122-
123136
virtual status_t on_before_popup(Menu *menu);
124-
125137
virtual status_t on_popup(Menu *menu);
126-
127138
virtual status_t on_submit();
128139
};
129140

src/main/widgets/simple/Label.cpp

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/>
3-
* (C) 2023 Vladimir Sadovnikov <[email protected]>
2+
* Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/>
3+
* (C) 2024 Vladimir Sadovnikov <[email protected]>
44
*
55
* This file is part of lsp-tk-lib
66
* Created on: 6 июл. 2017 г.
@@ -19,10 +19,12 @@
1919
* along with lsp-tk-lib. If not, see <https://www.gnu.org/licenses/>.
2020
*/
2121

22-
#include <lsp-plug.in/tk/tk.h>
22+
#include <lsp-plug.in/common/debug.h>
2323
#include <lsp-plug.in/stdlib/math.h>
24+
#include <lsp-plug.in/stdlib/stdio.h>
25+
#include <lsp-plug.in/tk/tk.h>
26+
2427
#include <private/tk/style/BuiltinStyle.h>
25-
#include <lsp-plug.in/common/debug.h>
2628

2729
namespace lsp
2830
{
@@ -77,6 +79,7 @@ namespace lsp
7779
Label::~Label()
7880
{
7981
nFlags |= FINALIZED;
82+
clear_text_estimations();
8083
}
8184

8285
status_t Label::init()
@@ -216,6 +219,53 @@ namespace lsp
216219
}
217220
}
218221

222+
void Label::clear_text_estimations()
223+
{
224+
size_t removed = 0;
225+
for (lltl::iterator<prop::String> it = vEstimations.values(); it; ++it)
226+
{
227+
prop::String *s = it.get();
228+
if (s != NULL)
229+
{
230+
++removed;
231+
delete s;
232+
}
233+
}
234+
vEstimations.clear();
235+
if (removed > 0)
236+
query_resize();
237+
}
238+
239+
tk::String *Label::add_text_estimation()
240+
{
241+
prop::String *s = new prop::String(&sProperties);
242+
if (s == NULL)
243+
return NULL;
244+
s->bind(&sStyle, pDisplay->dictionary());
245+
246+
if (vEstimations.add(s))
247+
return s;
248+
249+
delete s;
250+
return NULL;
251+
}
252+
253+
void Label::estimate_string_size(estimation_t *e, tk::String *s)
254+
{
255+
if (s == NULL)
256+
return;
257+
258+
// Form the text string
259+
s->format(&e->text);
260+
sTextAdjust.apply(&e->text);
261+
262+
// Estimate sizes
263+
sFont.get_multitext_parameters(pDisplay, &e->tp, e->fscaling, &e->text);
264+
265+
e->r->nMinWidth = lsp_max(e->r->nMinWidth, ceilf(e->tp.Width));
266+
e->r->nMinHeight = lsp_max(e->r->nMinHeight, ceilf(lsp_max(e->tp.Height, e->fp.Height)));
267+
}
268+
219269
void Label::size_request(ws::size_limit_t *r)
220270
{
221271
r->nMinWidth = 0;
@@ -225,26 +275,21 @@ namespace lsp
225275
r->nPreWidth = -1;
226276
r->nPreHeight = -1;
227277

228-
// Form the text string
229-
LSPString text;
230-
sText.format(&text);
231-
sTextAdjust.apply(&text);
232-
233-
// Estimate sizes
234-
float scaling = lsp_max(0.0f, sScaling.get());
235-
float fscaling = lsp_max(0.0f, scaling * sFontScaling.get());
236-
ws::font_parameters_t fp;
237-
ws::text_parameters_t tp;
238-
239-
sFont.get_parameters(pDisplay, fscaling, &fp);
240-
sFont.get_multitext_parameters(pDisplay, &tp, fscaling, &text);
241-
242-
r->nMinWidth = ceil(tp.Width);
243-
r->nMinHeight = ceil(lsp_max(tp.Height, fp.Height));
244-
278+
// Form the estimation parameters
279+
estimation_t e;
280+
e.scaling = lsp_max(0.0f, sScaling.get());
281+
e.fscaling = lsp_max(0.0f, e.scaling * sFontScaling.get());
282+
e.r = r;
283+
sFont.get_parameters(pDisplay, e.fscaling, &e.fp);
284+
285+
// Estimate the size of the label
286+
for (lltl::iterator<prop::String> it = vEstimations.values(); it; ++it)
287+
estimate_string_size(&e, it.get());
288+
estimate_string_size(&e, &sText);
289+
245290
// Apply size constraints
246-
sConstraints.apply(r, scaling);
247-
sIPadding.add(r, scaling);
291+
sConstraints.apply(r, e.scaling);
292+
sIPadding.add(r, e.scaling);
248293
}
249294

250295
status_t Label::on_mouse_in(const ws::event_t *e)

0 commit comments

Comments
 (0)