diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-03-22 17:28:05 +0200 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-03-22 17:28:05 +0200 |
commit | 36ad6cd20a07aecf69e92e9fa724beef14be536a (patch) | |
tree | e2b130724be663b48ae7949327ffca158c35bae1 /src/ui/labelwidget.c | |
parent | 157f0be146bf8122a70dcf5940f1033a6462c34c (diff) |
Basic language string mechanism
Added a set of English strings. Lang can load a language.
LabelWidget can replace IDs in the label.
IssueID #192
Diffstat (limited to 'src/ui/labelwidget.c')
-rw-r--r-- | src/ui/labelwidget.c | 36 |
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 | ||
41 | struct Impl_LabelWidget { | 41 | struct 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 | ||
361 | static 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 | |||
360 | void init_LabelWidget(iLabelWidget *d, const char *label, const char *cmd) { | 387 | void 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 | ||
382 | void deinit_LabelWidget(iLabelWidget *d) { | 411 | void 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 | ||
408 | void updateText_LabelWidget(iLabelWidget *d, const iString *text) { | 438 | void 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 | ||
413 | void updateTextCStr_LabelWidget(iLabelWidget *d, const char *text) { | 445 | void 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 | ||