diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-10-29 14:10:20 +0200 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-10-29 14:10:20 +0200 |
commit | 0257f0b53d9a6baed3719159b721abdc31d44715 (patch) | |
tree | 60f16f89177d69718b621c75ad7c024eb42a6e5d /src/ui/labelwidget.c | |
parent | b12399bf941f9de46798e84a818c9cffded6debe (diff) |
LabelWidget: Flag for wrapping text
Widgets now have an optional virtual method that gets called when the size of the widget changes during arranging. Wrapped text in LabelWidget uses this to update its height.
Diffstat (limited to 'src/ui/labelwidget.c')
-rw-r--r-- | src/ui/labelwidget.c | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/src/ui/labelwidget.c b/src/ui/labelwidget.c index f23091d8..0555bc4d 100644 --- a/src/ui/labelwidget.c +++ b/src/ui/labelwidget.c | |||
@@ -166,10 +166,10 @@ static void getColors_LabelWidget_(const iLabelWidget *d, int *bg, int *fg, int | |||
166 | static void draw_LabelWidget_(const iLabelWidget *d) { | 166 | static void draw_LabelWidget_(const iLabelWidget *d) { |
167 | const iWidget *w = constAs_Widget(d); | 167 | const iWidget *w = constAs_Widget(d); |
168 | draw_Widget(w); | 168 | draw_Widget(w); |
169 | const iBool isButton = d->click.button != 0; | 169 | const iBool isButton = d->click.button != 0; |
170 | const int flags = flags_Widget(w); | 170 | const int64_t flags = flags_Widget(w); |
171 | const iRect bounds = bounds_Widget(w); | 171 | const iRect bounds = bounds_Widget(w); |
172 | iRect rect = bounds; | 172 | iRect rect = bounds; |
173 | if (isButton) { | 173 | if (isButton) { |
174 | shrink_Rect(&rect, divi_I2(gap2_UI, 4)); | 174 | shrink_Rect(&rect, divi_I2(gap2_UI, 4)); |
175 | adjustEdges_Rect(&rect, gap_UI / 8, 0, -gap_UI / 8, 0); | 175 | adjustEdges_Rect(&rect, gap_UI / 8, 0, -gap_UI / 8, 0); |
@@ -196,7 +196,12 @@ static void draw_LabelWidget_(const iLabelWidget *d) { | |||
196 | } | 196 | } |
197 | } | 197 | } |
198 | setClip_Paint(&p, rect); | 198 | setClip_Paint(&p, rect); |
199 | if (flags & alignLeft_WidgetFlag) { | 199 | if (flags & wrapText_WidgetFlag) { |
200 | const iRect inner = innerBounds_Widget(w); | ||
201 | const int wrap = inner.size.x; | ||
202 | drawWrapRange_Text(d->font, topLeft_Rect(inner), wrap, fg, range_String(&d->label)); | ||
203 | } | ||
204 | else if (flags & alignLeft_WidgetFlag) { | ||
200 | draw_Text(d->font, add_I2(bounds.pos, padding_(flags)), fg, cstr_String(&d->label)); | 205 | draw_Text(d->font, add_I2(bounds.pos, padding_(flags)), fg, cstr_String(&d->label)); |
201 | if ((flags & drawKey_WidgetFlag) && d->key) { | 206 | if ((flags & drawKey_WidgetFlag) && d->key) { |
202 | iString str; | 207 | iString str; |
@@ -235,7 +240,8 @@ void updateSize_LabelWidget(iLabelWidget *d) { | |||
235 | size.x += 2 * gap_UI + measure_Text(uiShortcuts_FontId, cstr_String(&str)).x; | 240 | size.x += 2 * gap_UI + measure_Text(uiShortcuts_FontId, cstr_String(&str)).x; |
236 | deinit_String(&str); | 241 | deinit_String(&str); |
237 | } | 242 | } |
238 | if (~flags & fixedWidth_WidgetFlag) { | 243 | /* Wrapped text implies that width must be defined by arrangement. */ |
244 | if (!(flags & (fixedWidth_WidgetFlag | wrapText_WidgetFlag))) { | ||
239 | w->rect.size.x = size.x; | 245 | w->rect.size.x = size.x; |
240 | } | 246 | } |
241 | if (~flags & fixedHeight_WidgetFlag) { | 247 | if (~flags & fixedHeight_WidgetFlag) { |
@@ -243,6 +249,18 @@ void updateSize_LabelWidget(iLabelWidget *d) { | |||
243 | } | 249 | } |
244 | } | 250 | } |
245 | 251 | ||
252 | static void sizeChanged_LabelWidget_(iLabelWidget *d) { | ||
253 | iWidget *w = as_Widget(d); | ||
254 | if (flags_Widget(w) & wrapText_WidgetFlag) { | ||
255 | if (flags_Widget(w) & fixedHeight_WidgetFlag) { | ||
256 | /* Calculate a new height based on the wrapping. */ | ||
257 | w->rect.size.y = advanceWrapRange_Text( | ||
258 | d->font, innerBounds_Widget(w).size.x, range_String(&d->label)) | ||
259 | .y; | ||
260 | } | ||
261 | } | ||
262 | } | ||
263 | |||
246 | void init_LabelWidget(iLabelWidget *d, const char *label, int key, int kmods, const char *cmd) { | 264 | void init_LabelWidget(iLabelWidget *d, const char *label, int key, int kmods, const char *cmd) { |
247 | init_Widget(&d->widget); | 265 | init_Widget(&d->widget); |
248 | d->font = uiLabel_FontId; | 266 | d->font = uiLabel_FontId; |
@@ -312,4 +330,5 @@ iLabelWidget *newColor_LabelWidget(const char *text, int color) { | |||
312 | iBeginDefineSubclass(LabelWidget, Widget) | 330 | iBeginDefineSubclass(LabelWidget, Widget) |
313 | .processEvent = (iAny *) processEvent_LabelWidget_, | 331 | .processEvent = (iAny *) processEvent_LabelWidget_, |
314 | .draw = (iAny *) draw_LabelWidget_, | 332 | .draw = (iAny *) draw_LabelWidget_, |
333 | .sizeChanged = (iAny *) sizeChanged_LabelWidget_, | ||
315 | iEndDefineSubclass(LabelWidget) | 334 | iEndDefineSubclass(LabelWidget) |