summaryrefslogtreecommitdiff
path: root/src/ui/labelwidget.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui/labelwidget.c')
-rw-r--r--src/ui/labelwidget.c36
1 files changed, 35 insertions, 1 deletions
diff --git a/src/ui/labelwidget.c b/src/ui/labelwidget.c
index ea70977c..c3bc4392 100644
--- a/src/ui/labelwidget.c
+++ b/src/ui/labelwidget.c
@@ -40,6 +40,7 @@ iLocalDef iInt2 padding_(int64_t flags) {
40 40
41struct Impl_LabelWidget { 41struct Impl_LabelWidget {
42 iWidget widget; 42 iWidget widget;
43 iString srcLabel;
43 iString label; 44 iString label;
44 int font; 45 int font;
45 int key; 46 int key;
@@ -357,12 +358,40 @@ void updateSize_LabelWidget(iLabelWidget *d) {
357 } 358 }
358} 359}
359 360
361static void replaceVariables_LabelWidget_(iLabelWidget *d) {
362 for (const char *label = cstr_String(&d->label); *label; ) {
363 iRangecc id;
364 id.start = strstr(label, "${");
365 if (!id.start) {
366 break;
367 }
368 id.start += 2;
369 id.end = strchr(id.start, '}');
370 iAssert(id.end != NULL);
371 /* TODO: Add a lookup that doesn't allocate anything; Lang can handle it. */
372 const size_t len = size_Range(&id);
373 char *key = malloc(len + 1);
374 memcpy(key, id.start, len);
375 key[len] = 0;
376 const char *text = cstr_Lang(key);
377 const size_t textLen = strlen(text);
378 free(key);
379 /* Replace it. */
380 size_t startPos = id.start - cstr_String(&d->label) - 2;
381 remove_Block(&d->label.chars, startPos, len + 3);
382 insertData_Block(&d->label.chars, startPos, text, textLen);
383 label = cstr_String(&d->label) + startPos + textLen;
384 }
385}
386
360void init_LabelWidget(iLabelWidget *d, const char *label, const char *cmd) { 387void init_LabelWidget(iLabelWidget *d, const char *label, const char *cmd) {
361 init_Widget(&d->widget); 388 init_Widget(&d->widget);
362 d->font = uiLabel_FontId; 389 d->font = uiLabel_FontId;
363 d->forceFg = none_ColorId; 390 d->forceFg = none_ColorId;
364 d->icon = 0; 391 d->icon = 0;
365 initCStr_String(&d->label, label); 392 initCStr_String(&d->srcLabel, label);
393 initCopy_String(&d->label, &d->srcLabel);
394 replaceVariables_LabelWidget_(d);
366 if (cmd) { 395 if (cmd) {
367 initCStr_String(&d->command, cmd); 396 initCStr_String(&d->command, cmd);
368 } 397 }
@@ -381,6 +410,7 @@ void init_LabelWidget(iLabelWidget *d, const char *label, const char *cmd) {
381 410
382void deinit_LabelWidget(iLabelWidget *d) { 411void deinit_LabelWidget(iLabelWidget *d) {
383 deinit_String(&d->label); 412 deinit_String(&d->label);
413 deinit_String(&d->srcLabel);
384 deinit_String(&d->command); 414 deinit_String(&d->command);
385} 415}
386 416
@@ -407,11 +437,15 @@ void setAlignVisually_LabelWidget(iLabelWidget *d, iBool alignVisual) {
407 437
408void updateText_LabelWidget(iLabelWidget *d, const iString *text) { 438void updateText_LabelWidget(iLabelWidget *d, const iString *text) {
409 set_String(&d->label, text); 439 set_String(&d->label, text);
440 set_String(&d->srcLabel, text);
441 replaceVariables_LabelWidget_(d);
410 refresh_Widget(&d->widget); 442 refresh_Widget(&d->widget);
411} 443}
412 444
413void updateTextCStr_LabelWidget(iLabelWidget *d, const char *text) { 445void updateTextCStr_LabelWidget(iLabelWidget *d, const char *text) {
414 setCStr_String(&d->label, text); 446 setCStr_String(&d->label, text);
447 set_String(&d->srcLabel, &d->label);
448 replaceVariables_LabelWidget_(d);
415 refresh_Widget(&d->widget); 449 refresh_Widget(&d->widget);
416} 450}
417 451