summaryrefslogtreecommitdiff
path: root/src/ui/labelwidget.c
blob: b84e17f43c5c6eaaeeeedec6e7eb81eec8486560 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/* Copyright 2020 Jaakko Keränen <jaakko.keranen@iki.fi>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

#include "labelwidget.h"
#include "text.h"
#include "defs.h"
#include "color.h"
#include "paint.h"
#include "app.h"
#include "util.h"
#include "keys.h"
#include "touch.h"

#include <SDL_version.h>

struct Impl_LabelWidget {
    iWidget widget;
    iString srcLabel;
    iString label;
    int     font;
    int     key;
    int     kmods;
    iChar   icon;
    int     forceFg;
    iString command;
    iClick  click;
    struct {
        uint8_t alignVisual         : 1; /* align according to visible bounds, not font metrics */
        uint8_t noAutoMinHeight     : 1; /* minimum height is not set automatically */
        uint8_t drawAsOutline       : 1; /* draw as outline, filled with background color */
        uint8_t noTopFrame          : 1;
        uint8_t wrap                : 1;
        uint8_t allCaps             : 1;
        uint8_t removeTrailingColon : 1;
    } flags;
};

static iBool isHover_LabelWidget_(const iLabelWidget *d) {
#if defined (iPlatformMobile)
    if (!isHovering_Touch()) {
        return iFalse;
    }
#endif
    return isHover_Widget(d);
}

static iInt2 padding_LabelWidget_(const iLabelWidget *d, int corner) {
    const iWidget *w = constAs_Widget(d);
    const int64_t flags = flags_Widget(w);
    const iInt2 widgetPad = (corner   == 0 ? init_I2(w->padding[0], w->padding[1])
                             : corner == 1 ? init_I2(w->padding[2], w->padding[1])
                             : corner == 2 ? init_I2(w->padding[2], w->padding[3])
                             : init_I2(w->padding[0], w->padding[3]));
#if defined (iPlatformMobile)
    return add_I2(widgetPad,
                  init_I2(flags & tight_WidgetFlag ? 2 * gap_UI : (4 * gap_UI),
                          (flags & extraPadding_WidgetFlag ? 1.5f : 1.0f) * 3 * gap_UI / 2));
#else
    return add_I2(widgetPad,
                  init_I2(flags & tight_WidgetFlag ? 3 * gap_UI / 2 : (3 * gap_UI),
                          gap_UI));
#endif
}

iDefineObjectConstructionArgs(LabelWidget,
                              (const char *label, const char *cmd),
                              label, cmd)

static iBool checkModifiers_(int have, int req) {
    return keyMods_Sym(req) == keyMods_Sym(have);
}

static void trigger_LabelWidget_(const iLabelWidget *d) {
    const iWidget *w = constAs_Widget(d);
    postCommand_Widget(&d->widget, "%s", cstr_String(&d->command));
    if (flags_Widget(w) & radio_WidgetFlag) {
        iForEach(ObjectList, i, children_Widget(w->parent)) {
            setFlags_Widget(i.object, selected_WidgetFlag, d == i.object);
        }
    }
}

static void updateKey_LabelWidget_(iLabelWidget *d) {
    if (!isEmpty_String(&d->command)) {
        const iBinding *bind = findCommand_Keys(cstr_String(&d->command));
        if (bind && bind->id < builtIn_BindingId) {
            d->key   = bind->key;
            d->kmods = bind->mods;
        }
    }
}

static iBool processEvent_LabelWidget_(iLabelWidget *d, const SDL_Event *ev) {
    iWidget *w = &d->widget;
    if (isMetricsChange_UserEvent(ev)) {
        updateSize_LabelWidget(d);
    }
    else if (isCommand_UserEvent(ev, "lang.changed")) {
        const iChar oldIcon = d->icon; /* icon will be retained */
        setText_LabelWidget(d, &d->srcLabel);
        checkIcon_LabelWidget(d); /* strip it */
        d->icon = oldIcon;
        return iFalse;
    }
    else if (isCommand_UserEvent(ev, "bindings.changed")) {
        /* Update the key used to trigger this label. */
        updateKey_LabelWidget_(d);
        return iFalse;
    }
    if (!isEmpty_String(&d->command)) {
#if 0 && defined (iPlatformAppleMobile)
        /* Touch allows activating any button on release. */
        switch (ev->type) {
            case SDL_MOUSEBUTTONUP: {
                const iInt2 mouse = init_I2(ev->button.x, ev->button.y);
                if (contains_Widget(w, mouse)) {
                    trigger_LabelWidget_(d);
                    refresh_Widget(w);
                }
                break;
            }
        }
#endif
        switch (processEvent_Click(&d->click, ev)) {
            case started_ClickResult:
                setFlags_Widget(w, pressed_WidgetFlag, iTrue);
                refresh_Widget(w);
                return iTrue;
            case aborted_ClickResult:
                setFlags_Widget(w, pressed_WidgetFlag, iFalse);
                refresh_Widget(w);
                return iTrue;
//            case double_ClickResult:
            case finished_ClickResult:
                setFlags_Widget(w, pressed_WidgetFlag, iFalse);
                trigger_LabelWidget_(d);
                refresh_Widget(w);
                return iTrue;
            default:
                break;
        }
        switch (ev->type) {
            case SDL_KEYDOWN: {
                const int mods = ev->key.keysym.mod;
                if (d->key && ev->key.keysym.sym == d->key && checkModifiers_(mods, d->kmods)) {
                    trigger_LabelWidget_(d);
                    return iTrue;
                }
                break;
            }
        }
    }
    return processEvent_Widget(&d->widget, ev);
}

static void keyStr_LabelWidget_(const iLabelWidget *d, iString *str) {
    toString_Sym(d->key, d->kmods, str);
}

static void getColors_LabelWidget_(const iLabelWidget *d, int *bg, int *fg, int *frame1, int *frame2) {
    const iWidget *w           = constAs_Widget(d);
    const int64_t  flags       = flags_Widget(w);
    const iBool    isPress     = (flags & pressed_WidgetFlag) != 0;
    const iBool    isSel       = (flags & selected_WidgetFlag) != 0;
    const iBool    isFrameless = (flags & frameless_WidgetFlag) != 0;
    const iBool    isButton    = d->click.button != 0;
    const iBool    isKeyRoot   = (w->root == get_Window()->keyRoot);
    /* Default color state. */
    *bg     = isButton && ~flags & noBackground_WidgetFlag ? (d->widget.bgColor != none_ColorId ?
                                                              d->widget.bgColor : uiBackground_ColorId)
                                                           : none_ColorId;
    *fg     = uiText_ColorId;
    *frame1 = isButton ? uiEmboss1_ColorId : d->widget.frameColor;
    *frame2 = isButton ? uiEmboss2_ColorId : *frame1;
    if (flags & disabled_WidgetFlag && isButton) {
        *fg = uiTextDisabled_ColorId;
    }
    if (isSel) {
        *bg = uiBackgroundSelected_ColorId;
//        if (!isKeyRoot) {
//            *bg = uiEmbossSelected1_ColorId; //uiBackgroundUnfocusedSelection_ColorId;
//        }
        if (!isKeyRoot) {
            *bg = isDark_ColorTheme(colorTheme_App()) ? uiBackgroundUnfocusedSelection_ColorId
                : uiMarked_ColorId ;
        }
        *fg = uiTextSelected_ColorId;
        if (isButton) {
            *frame1 = uiEmbossSelected1_ColorId;
            *frame2 = uiEmbossSelected2_ColorId;
            if (!isKeyRoot) {
                *frame1 = *bg;
            }
        }
    }
    int colorEscape = none_ColorId;
    if (startsWith_String(&d->label, "\v")) {
        colorEscape = cstr_String(&d->label)[1] - asciiBase_ColorEscape; /* TODO: can be two bytes long */
    }
    if (isHover_LabelWidget_(d)) {
        if (isFrameless) {
            *bg = uiBackgroundFramelessHover_ColorId;
            *fg = uiTextFramelessHover_ColorId;
        }
        else {
            /* Frames matching color escaped text. */
            if (colorEscape != none_ColorId) {
                if (isDark_ColorTheme(colorTheme_App())) {
                    *frame1 = colorEscape;
                    *frame2 = darker_Color(*frame1);
                }
                else {
                    *bg = *frame1 = *frame2 = colorEscape;
                    *fg = white_ColorId | permanent_ColorId;
                }
            }
            else if (isSel) {
                *frame1 = uiEmbossSelectedHover1_ColorId;
                *frame2 = uiEmbossSelectedHover2_ColorId;
            }
            else {
                if (isButton) *bg = uiBackgroundHover_ColorId;
                *frame1 = uiEmbossHover1_ColorId;
                *frame2 = uiEmbossHover2_ColorId;
            }
        }
    }
    if (isPress) {
        *bg = uiBackgroundPressed_ColorId | permanent_ColorId;
        if (isButton) {
            *frame1 = uiEmbossPressed1_ColorId;
            *frame2 = colorEscape != none_ColorId ? colorEscape : uiEmbossPressed2_ColorId;
        }
        if (colorEscape == none_ColorId || colorEscape == uiTextAction_ColorId) {
            *fg = uiTextPressed_ColorId | permanent_ColorId;
        }
        else {
            *fg = isDark_ColorTheme(colorTheme_App()) ? white_ColorId : black_ColorId;
        }
    }
    if (d->forceFg >= 0) {
        *fg = d->forceFg;
    }
}

iLocalDef int iconPadding_LabelWidget_(const iLabelWidget *d) {
    const float amount = flags_Widget(constAs_Widget(d)) & extraPadding_WidgetFlag ? 1.5f : 1.15f;
    return d->icon ? iRound(lineHeight_Text(d->font) * amount) : 0;
}

static iRect contentBounds_LabelWidget_(const iLabelWidget *d) {
    iRect content = adjusted_Rect(bounds_Widget(constAs_Widget(d)),
                                  padding_LabelWidget_(d, 0),
                                  neg_I2(padding_LabelWidget_(d, 2)));
    adjustEdges_Rect(&content, 0, 0, 0, iconPadding_LabelWidget_(d));
    return content;
}

static void draw_LabelWidget_(const iLabelWidget *d) {
    const iWidget *w = constAs_Widget(d);
    draw_Widget(w);
    const iBool   isButton = d->click.button != 0;
    const int64_t flags    = flags_Widget(w);
    const iRect   bounds   = bounds_Widget(w);
    iRect         rect     = bounds;
    const iBool   isHover  = isHover_LabelWidget_(d);
    if (isButton) {
        shrink_Rect(&rect, divi_I2(gap2_UI, 4));
        adjustEdges_Rect(&rect, gap_UI / 8, 0, -gap_UI / 8, 0);
    }
    iPaint p;
    init_Paint(&p);
    int bg, fg, frame, frame2;
    getColors_LabelWidget_(d, &bg, &fg, &frame, &frame2);
    const iBool isCaution = startsWith_String(&d->label, uiTextCaution_ColorEscape);
    if (bg >= 0) {
        fillRect_Paint(&p, rect, isCaution && isHover ? uiMarked_ColorId : bg);
    }
    if (~flags & frameless_WidgetFlag) {
        iRect frameRect = adjusted_Rect(rect, zero_I2(), init1_I2(-1));
        if (isButton) {
            iInt2 points[] = {
                bottomLeft_Rect(frameRect),
                topLeft_Rect(frameRect),
                topRight_Rect(frameRect),
                bottomRight_Rect(frameRect),
                bottomLeft_Rect(frameRect)
            };
#if SDL_VERSION_ATLEAST(2, 0, 16)
            if (isOpenGLRenderer_Window()) {
                /* A very curious regression in SDL 2.0.16. */
                points[3].x--;    
            }
#endif
            drawLines_Paint(&p, points + 2, 3, frame2);
            drawLines_Paint(
                &p, points, !isHover && d->flags.noTopFrame ? 2 : 3, frame);
        }
    }
    setClip_Paint(&p, rect);
    const int iconPad = iconPadding_LabelWidget_(d);
    if (d->icon && d->icon != 0x20) { /* no need to draw an empty icon */
        iString str;
        initUnicodeN_String(&str, &d->icon, 1);
        drawCentered_Text(
            d->font,
            (iRect){
                /* The icon position is fine-tuned; c.f. high baseline of Source Sans Pro. */
                add_I2(add_I2(bounds.pos, padding_LabelWidget_(d, 0)),
                       init_I2((flags & extraPadding_WidgetFlag ? -2 : -1.20f) * gap_UI +
                               (deviceType_App() == tablet_AppDeviceType ? -gap_UI : 0),
                               -gap_UI / 8)),
                init_I2(iconPad, lineHeight_Text(d->font)) },
            iTrue,
            isCaution                                            ? uiTextCaution_ColorId
            : flags & (disabled_WidgetFlag | pressed_WidgetFlag) ? fg
            : isHover                                            ? uiIconHover_ColorId
                                                                 : uiIcon_ColorId,
            "%s",
            cstr_String(&str));
        deinit_String(&str);
    }
    if (d->flags.wrap) {
        const iRect cont = contentBounds_LabelWidget_(d); //djusted_Rect(innerBounds_Widget(w), init_I2(iconPad, 0), zero_I2());
        drawWrapRange_Text(
            d->font, topLeft_Rect(cont), width_Rect(cont), fg, range_String(&d->label));
    }
    else if (flags & alignLeft_WidgetFlag) {
        draw_Text(d->font, add_I2(bounds.pos, addX_I2(padding_LabelWidget_(d, 0), iconPad)),
                  fg, cstr_String(&d->label));
        if ((flags & drawKey_WidgetFlag) && d->key) {
            iString str;
            init_String(&str);
            keyStr_LabelWidget_(d, &str);
            drawAlign_Text(uiShortcuts_FontId,
                           add_I2(topRight_Rect(bounds),
                                  addX_I2(negX_I2(padding_LabelWidget_(d, 1)),
                                          deviceType_App() == tablet_AppDeviceType ? gap_UI : 0)),
                           flags & pressed_WidgetFlag ? fg
                           : isCaution                ? uiTextCaution_ColorId
                                                      : uiTextShortcut_ColorId,
                           right_Alignment,
                           cstr_String(&str));
            deinit_String(&str);
        }
    }
    else if (flags & alignRight_WidgetFlag) {
        drawAlign_Text(
            d->font,
            add_I2(topRight_Rect(bounds), negX_I2(padding_LabelWidget_(d, 1))),
            fg,
            right_Alignment,
            cstr_String(&d->label));
    }
    else {
        drawCenteredOutline_Text(
            d->font,
            adjusted_Rect(bounds, add_I2(zero_I2(), init_I2(iconPad, 0)), neg_I2(zero_I2())),
            d->flags.alignVisual,
            d->flags.drawAsOutline ? fg : none_ColorId,
            d->flags.drawAsOutline ? d->widget.bgColor : fg,
            "%s",
            cstr_String(&d->label));
    }
    if (flags & chevron_WidgetFlag) {
        const iRect chRect = rect;
        const int chSize = lineHeight_Text(d->font);
        drawCentered_Text(d->font,
                          (iRect){ addX_I2(topRight_Rect(chRect), -iconPad),
                                   init_I2(chSize, height_Rect(chRect)) },
                          iTrue, uiSeparator_ColorId, rightAngle_Icon);
    }
    unsetClip_Paint(&p);
}

static void sizeChanged_LabelWidget_(iLabelWidget *d) {
    iWidget *w = as_Widget(d);
    if (d->flags.wrap) {
        if (flags_Widget(w) & fixedHeight_WidgetFlag) {
            /* Calculate a new height based on the wrapping. */
            const iRect cont = contentBounds_LabelWidget_(d);
            w->rect.size.y =
                measureWrapRange_Text(d->font, width_Rect(cont), range_String(&d->label))
                    .bounds.size.y +
                padding_LabelWidget_(d, 0).y + padding_LabelWidget_(d, 2).y;
        }
    }
}

iInt2 defaultSize_LabelWidget(const iLabelWidget *d) {
    const iWidget *w = constAs_Widget(d);
    const int64_t flags = flags_Widget(w);
    iInt2 size = add_I2(measure_Text(d->font, cstr_String(&d->label)).bounds.size,
                        add_I2(padding_LabelWidget_(d, 0), padding_LabelWidget_(d, 2)));
    if ((flags & drawKey_WidgetFlag) && d->key) {
        iString str;
        init_String(&str);
        keyStr_LabelWidget_(d, &str);
        size.x += 2 * gap_UI + measure_Text(uiShortcuts_FontId, cstr_String(&str)).bounds.size.x;
        deinit_String(&str);
    }
    size.x += iconPadding_LabelWidget_(d);
    return size;
}

int font_LabelWidget(const iLabelWidget *d) {
    return d->font;
}

void updateSize_LabelWidget(iLabelWidget *d) {
    iWidget *w = as_Widget(d);
    const int64_t flags = flags_Widget(w);
    const iInt2 size = defaultSize_LabelWidget(d);
    if (!d->flags.noAutoMinHeight) {
        w->minSize.y = size.y; /* vertically text must remain visible */
    }
    /* Wrapped text implies that width must be defined by arrangement. */
    if (~flags & fixedWidth_WidgetFlag && !d->flags.wrap) {
        w->rect.size.x = size.x;
    }
    if (~flags & fixedHeight_WidgetFlag) {
        w->rect.size.y = size.y;
    }
}

static void replaceVariables_LabelWidget_(iLabelWidget *d) {
    translate_Lang(&d->label);
    if (d->flags.allCaps) {
        set_String(&d->label, collect_String(upper_String(&d->label)));
    }
    if (d->flags.removeTrailingColon && endsWith_String(&d->label, ":")) {
        removeEnd_String(&d->label, 1);
    }
}

void init_LabelWidget(iLabelWidget *d, const char *label, const char *cmd) {
    iWidget *w = &d->widget;
    init_Widget(w);
    iZap(d->flags);
    d->font = uiLabel_FontId;
    d->forceFg = none_ColorId;
    d->icon = 0;
    initCStr_String(&d->srcLabel, label);
    initCopy_String(&d->label, &d->srcLabel);
    replaceVariables_LabelWidget_(d);
    if (cmd) {
        initCStr_String(&d->command, cmd);
    }
    else {
        setFrameColor_Widget(w, uiFrame_ColorId);
        init_String(&d->command);
    }
    d->key   = 0;
    d->kmods = 0;
    init_Click(&d->click, d, !isEmpty_String(&d->command) ? SDL_BUTTON_LEFT : 0);
    setFlags_Widget(w, hover_WidgetFlag, d->click.button != 0);
    updateSize_LabelWidget(d);
    updateKey_LabelWidget_(d); /* could be bound to another key */
}

void deinit_LabelWidget(iLabelWidget *d) {
    deinit_String(&d->label);
    deinit_String(&d->srcLabel);
    deinit_String(&d->command);
}

void setFont_LabelWidget(iLabelWidget *d, int fontId) {
    d->font = fontId;
    updateSize_LabelWidget(d);
}

void setTextColor_LabelWidget(iLabelWidget *d, int color) {
    if (d && d->forceFg != color) {
        d->forceFg = color;
        refresh_Widget(d);
    }
}

void setText_LabelWidget(iLabelWidget *d, const iString *text) {
    updateText_LabelWidget(d, text);
    updateSize_LabelWidget(d);
    if (isWrapped_LabelWidget(d)) {
        sizeChanged_LabelWidget_(d);
    }
}

void setAlignVisually_LabelWidget(iLabelWidget *d, iBool alignVisual) {
    d->flags.alignVisual = alignVisual;
}

void setNoAutoMinHeight_LabelWidget(iLabelWidget *d, iBool noAutoMinHeight) {
    /* By default all labels use a minimum height determined by the text dimensions. */
    d->flags.noAutoMinHeight = noAutoMinHeight;
    if (noAutoMinHeight) {
        d->widget.minSize.y = 0;
    }
}

void setNoTopFrame_LabelWidget(iLabelWidget *d, iBool noTopFrame) {
    d->flags.noTopFrame = noTopFrame;
}

void setWrap_LabelWidget(iLabelWidget *d, iBool wrap) {
    d->flags.wrap = wrap;
}

void setOutline_LabelWidget(iLabelWidget *d, iBool drawAsOutline) {
    if (d) {
        d->flags.drawAsOutline = drawAsOutline;
    }
}

void setAllCaps_LabelWidget(iLabelWidget *d, iBool allCaps) {
    if (d) {
        d->flags.allCaps = allCaps;
        replaceVariables_LabelWidget_(d);
    }
}

void setRemoveTrailingColon_LabelWidget(iLabelWidget *d, iBool removeTrailingColon) {
    if (d) {
        d->flags.removeTrailingColon = removeTrailingColon;
        replaceVariables_LabelWidget_(d);
    }
}

void updateText_LabelWidget(iLabelWidget *d, const iString *text) {
    set_String(&d->label, text);
    set_String(&d->srcLabel, text);
    replaceVariables_LabelWidget_(d);
    refresh_Widget(&d->widget);
}

void updateTextCStr_LabelWidget(iLabelWidget *d, const char *text) {
    setCStr_String(&d->label, text);
    set_String(&d->srcLabel, &d->label);
    replaceVariables_LabelWidget_(d);
    refresh_Widget(&d->widget);
}

void updateTextAndResizeWidthCStr_LabelWidget(iLabelWidget *d, const char *text) {
    updateTextCStr_LabelWidget(d, text);
    d->widget.rect.size.x = defaultSize_LabelWidget(d).x;
}

void setTextCStr_LabelWidget(iLabelWidget *d, const char *text) {
    setCStr_String(&d->label, text);
    set_String(&d->srcLabel, &d->label);
    replaceVariables_LabelWidget_(d);
    updateSize_LabelWidget(d);
}

void setCommand_LabelWidget(iLabelWidget *d, const iString *command) {
    set_String(&d->command, command);
}

void setIcon_LabelWidget(iLabelWidget *d, iChar icon) {
    if (d->icon != icon) {
        d->icon = icon;
        updateSize_LabelWidget(d);
    }
}

iBool checkIcon_LabelWidget(iLabelWidget *d) {
    if (isEmpty_String(&d->label)) {
        d->icon = 0;
        return iFalse;
    }
    iStringConstIterator iter;
    init_StringConstIterator(&iter, &d->label);
    const iChar icon = iter.value;
    next_StringConstIterator(&iter);
    if (iter.value == ' ' && icon >= 0x100) {
        d->icon = icon;
        remove_Block(&d->label.chars, 0, iter.next - constBegin_String(&d->label));
        return iTrue;
    }
    else {
        d->icon = 0;
    }
    return iFalse;
}

iChar icon_LabelWidget(const iLabelWidget *d) {
    return d->icon;
}

iBool isWrapped_LabelWidget(const iLabelWidget *d) {
    return d->flags.wrap;
}

const iString *text_LabelWidget(const iLabelWidget *d) {
    if (!d) return collectNew_String();
    return &d->label;
}

const iString *sourceText_LabelWidget(const iLabelWidget *d) {
    if (!d) return collectNew_String();
    return &d->srcLabel;
}

const iString *command_LabelWidget(const iLabelWidget *d) {
    return &d->command;
}

iLabelWidget *newKeyMods_LabelWidget(const char *label, int key, int kmods, const char *command) {
    iLabelWidget *d = new_LabelWidget(label, command);
    d->key = key;
    d->kmods = kmods;
    updateKey_LabelWidget_(d); /* could be bound to a different key */
    return d;
}

iLabelWidget *newColor_LabelWidget(const char *text, int color) {
    iLabelWidget *d = new_LabelWidget(format_CStr("%s%s", escape_Color(color), text), NULL);
    setFlags_Widget(as_Widget(d), frameless_WidgetFlag, iTrue);
    return d;
}

iBeginDefineSubclass(LabelWidget, Widget)
    .processEvent = (iAny *) processEvent_LabelWidget_,
    .draw         = (iAny *) draw_LabelWidget_,
    .sizeChanged  = (iAny *) sizeChanged_LabelWidget_,
iEndDefineSubclass(LabelWidget)