summaryrefslogtreecommitdiff
path: root/src/ui/labelwidget.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-10-30 15:03:09 +0200
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-10-30 15:03:09 +0200
commit114e52a60c789c5d1d885a994e8e1bd002b7cc93 (patch)
tree85c9e5281cb2f8066f18394a1db4dfe662dc5316 /src/ui/labelwidget.c
parentf22ee7c9ac27fe05649219535a05c21281709607 (diff)
Updating LabelWidget command key
If the key bindings have a key for a command, LabelWidget will use it.
Diffstat (limited to 'src/ui/labelwidget.c')
-rw-r--r--src/ui/labelwidget.c60
1 files changed, 42 insertions, 18 deletions
diff --git a/src/ui/labelwidget.c b/src/ui/labelwidget.c
index 0555bc4d..b4ba79fe 100644
--- a/src/ui/labelwidget.c
+++ b/src/ui/labelwidget.c
@@ -26,6 +26,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
26#include "paint.h" 26#include "paint.h"
27#include "app.h" 27#include "app.h"
28#include "util.h" 28#include "util.h"
29#include "keys.h"
29 30
30iLocalDef iInt2 padding_(int flags) { 31iLocalDef iInt2 padding_(int flags) {
31 return init_I2(flags & tight_WidgetFlag ? 3 * gap_UI / 2 : (3 * gap_UI), gap_UI); 32 return init_I2(flags & tight_WidgetFlag ? 3 * gap_UI / 2 : (3 * gap_UI), gap_UI);
@@ -43,8 +44,8 @@ struct Impl_LabelWidget {
43}; 44};
44 45
45iDefineObjectConstructionArgs(LabelWidget, 46iDefineObjectConstructionArgs(LabelWidget,
46 (const char *label, int key, int kmods, const char *cmd), 47 (const char *label, const char *cmd),
47 label, key, kmods, cmd) 48 label, cmd)
48 49
49static iBool checkModifiers_(int have, int req) { 50static iBool checkModifiers_(int have, int req) {
50 return keyMods_Sym(req) == keyMods_Sym(have); 51 return keyMods_Sym(req) == keyMods_Sym(have);
@@ -60,11 +61,26 @@ static void trigger_LabelWidget_(const iLabelWidget *d) {
60 } 61 }
61} 62}
62 63
64static void updateKey_LabelWidget_(iLabelWidget *d) {
65 if (!isEmpty_String(&d->command)) {
66 const iBinding *bind = findCommand_Keys(cstr_String(&d->command));
67 if (bind) {
68 d->key = bind->key;
69 d->kmods = bind->mods;
70 }
71 }
72}
73
63static iBool processEvent_LabelWidget_(iLabelWidget *d, const SDL_Event *ev) { 74static iBool processEvent_LabelWidget_(iLabelWidget *d, const SDL_Event *ev) {
64 iWidget *w = &d->widget; 75 iWidget *w = &d->widget;
65 if (isCommand_UserEvent(ev, "metrics.changed")) { 76 if (isCommand_UserEvent(ev, "metrics.changed")) {
66 updateSize_LabelWidget(d); 77 updateSize_LabelWidget(d);
67 } 78 }
79 else if (isCommand_UserEvent(ev, "bindings.changed")) {
80 /* Update the key used to trigger this label. */
81 updateKey_LabelWidget_(d);
82 return iFalse;
83 }
68 if (!isEmpty_String(&d->command)) { 84 if (!isEmpty_String(&d->command)) {
69 switch (processEvent_Click(&d->click, ev)) { 85 switch (processEvent_Click(&d->click, ev)) {
70 case started_ClickResult: 86 case started_ClickResult:
@@ -229,6 +245,18 @@ static void draw_LabelWidget_(const iLabelWidget *d) {
229 unsetClip_Paint(&p); 245 unsetClip_Paint(&p);
230} 246}
231 247
248static void sizeChanged_LabelWidget_(iLabelWidget *d) {
249 iWidget *w = as_Widget(d);
250 if (flags_Widget(w) & wrapText_WidgetFlag) {
251 if (flags_Widget(w) & fixedHeight_WidgetFlag) {
252 /* Calculate a new height based on the wrapping. */
253 w->rect.size.y = advanceWrapRange_Text(
254 d->font, innerBounds_Widget(w).size.x, range_String(&d->label))
255 .y;
256 }
257 }
258}
259
232void updateSize_LabelWidget(iLabelWidget *d) { 260void updateSize_LabelWidget(iLabelWidget *d) {
233 iWidget *w = as_Widget(d); 261 iWidget *w = as_Widget(d);
234 const int flags = flags_Widget(w); 262 const int flags = flags_Widget(w);
@@ -249,19 +277,7 @@ void updateSize_LabelWidget(iLabelWidget *d) {
249 } 277 }
250} 278}
251 279
252static void sizeChanged_LabelWidget_(iLabelWidget *d) { 280void init_LabelWidget(iLabelWidget *d, const char *label, const char *cmd) {
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
264void init_LabelWidget(iLabelWidget *d, const char *label, int key, int kmods, const char *cmd) {
265 init_Widget(&d->widget); 281 init_Widget(&d->widget);
266 d->font = uiLabel_FontId; 282 d->font = uiLabel_FontId;
267 initCStr_String(&d->label, label); 283 initCStr_String(&d->label, label);
@@ -271,12 +287,13 @@ void init_LabelWidget(iLabelWidget *d, const char *label, int key, int kmods, co
271 else { 287 else {
272 init_String(&d->command); 288 init_String(&d->command);
273 } 289 }
274 d->key = key; 290 d->key = 0;
275 d->kmods = kmods; 291 d->kmods = 0;
276 init_Click(&d->click, d, !isEmpty_String(&d->command) ? SDL_BUTTON_LEFT : 0); 292 init_Click(&d->click, d, !isEmpty_String(&d->command) ? SDL_BUTTON_LEFT : 0);
277 setFlags_Widget(&d->widget, hover_WidgetFlag, d->click.button != 0); 293 setFlags_Widget(&d->widget, hover_WidgetFlag, d->click.button != 0);
278 d->alignVisual = iFalse; 294 d->alignVisual = iFalse;
279 updateSize_LabelWidget(d); 295 updateSize_LabelWidget(d);
296 updateKey_LabelWidget_(d);
280} 297}
281 298
282void deinit_LabelWidget(iLabelWidget *d) { 299void deinit_LabelWidget(iLabelWidget *d) {
@@ -321,8 +338,15 @@ const iString *command_LabelWidget(const iLabelWidget *d) {
321 return &d->command; 338 return &d->command;
322} 339}
323 340
341iLabelWidget *newKeyMods_LabelWidget(const char *label, int key, int kmods, const char *command) {
342 iLabelWidget *d = new_LabelWidget(label, command);
343 d->key = key;
344 d->kmods = kmods;
345 return d;
346}
347
324iLabelWidget *newColor_LabelWidget(const char *text, int color) { 348iLabelWidget *newColor_LabelWidget(const char *text, int color) {
325 iLabelWidget *d = new_LabelWidget(format_CStr("%s%s", escape_Color(color), text), 0, 0, NULL); 349 iLabelWidget *d = new_LabelWidget(format_CStr("%s%s", escape_Color(color), text), NULL);
326 setFlags_Widget(as_Widget(d), frameless_WidgetFlag, iTrue); 350 setFlags_Widget(as_Widget(d), frameless_WidgetFlag, iTrue);
327 return d; 351 return d;
328} 352}