diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-08-02 17:56:02 +0300 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-08-02 17:56:02 +0300 |
commit | f3d083d3f150c1e940c4086f39165fb55935344a (patch) | |
tree | d5d65702d4b80112095859e61635a95801c2d5c0 /src/ui/labelwidget.c | |
parent | a346dd6450b9c6636110d1c882d28ca4dc82573d (diff) |
Flag for tight label padding; aligning labels visually
Diffstat (limited to 'src/ui/labelwidget.c')
-rw-r--r-- | src/ui/labelwidget.c | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/ui/labelwidget.c b/src/ui/labelwidget.c index d3eaed11..c246c0c4 100644 --- a/src/ui/labelwidget.c +++ b/src/ui/labelwidget.c | |||
@@ -5,7 +5,9 @@ | |||
5 | #include "app.h" | 5 | #include "app.h" |
6 | #include "util.h" | 6 | #include "util.h" |
7 | 7 | ||
8 | iLocalDef iInt2 padding_(void) { return init_I2(3 * gap_UI, gap_UI); } | 8 | iLocalDef iInt2 padding_(int flags) { |
9 | return init_I2(flags & tight_WidgetFlag ? 3 * gap_UI / 2 : (3 * gap_UI), gap_UI); | ||
10 | } | ||
9 | 11 | ||
10 | struct Impl_LabelWidget { | 12 | struct Impl_LabelWidget { |
11 | iWidget widget; | 13 | iWidget widget; |
@@ -14,6 +16,7 @@ struct Impl_LabelWidget { | |||
14 | int key; | 16 | int key; |
15 | int kmods; | 17 | int kmods; |
16 | iString command; | 18 | iString command; |
19 | iBool alignVisual; /* align according to visible bounds, not typography */ | ||
17 | iClick click; | 20 | iClick click; |
18 | }; | 21 | }; |
19 | 22 | ||
@@ -191,13 +194,13 @@ static void draw_LabelWidget_(const iLabelWidget *d) { | |||
191 | } | 194 | } |
192 | setClip_Paint(&p, rect); | 195 | setClip_Paint(&p, rect); |
193 | if (flags & alignLeft_WidgetFlag) { | 196 | if (flags & alignLeft_WidgetFlag) { |
194 | draw_Text(d->font, add_I2(bounds.pos, padding_()), fg, cstr_String(&d->label)); | 197 | draw_Text(d->font, add_I2(bounds.pos, padding_(flags)), fg, cstr_String(&d->label)); |
195 | if ((flags & drawKey_WidgetFlag) && d->key) { | 198 | if ((flags & drawKey_WidgetFlag) && d->key) { |
196 | iString str; | 199 | iString str; |
197 | init_String(&str); | 200 | init_String(&str); |
198 | keyStr_LabelWidget_(d, &str); | 201 | keyStr_LabelWidget_(d, &str); |
199 | drawAlign_Text(uiShortcuts_FontId, | 202 | drawAlign_Text(uiShortcuts_FontId, |
200 | add_I2(topRight_Rect(bounds), negX_I2(padding_())), | 203 | add_I2(topRight_Rect(bounds), negX_I2(padding_(flags))), |
201 | flags & pressed_WidgetFlag ? fg : cyan_ColorId, | 204 | flags & pressed_WidgetFlag ? fg : cyan_ColorId, |
202 | right_Alignment, | 205 | right_Alignment, |
203 | cstr_String(&str)); | 206 | cstr_String(&str)); |
@@ -207,13 +210,13 @@ static void draw_LabelWidget_(const iLabelWidget *d) { | |||
207 | else if (flags & alignRight_WidgetFlag) { | 210 | else if (flags & alignRight_WidgetFlag) { |
208 | drawAlign_Text( | 211 | drawAlign_Text( |
209 | d->font, | 212 | d->font, |
210 | add_I2(topRight_Rect(bounds), negX_I2(padding_())), | 213 | add_I2(topRight_Rect(bounds), negX_I2(padding_(flags))), |
211 | fg, | 214 | fg, |
212 | right_Alignment, | 215 | right_Alignment, |
213 | cstr_String(&d->label)); | 216 | cstr_String(&d->label)); |
214 | } | 217 | } |
215 | else { | 218 | else { |
216 | drawCentered_Text(d->font, bounds, fg, cstr_String(&d->label)); | 219 | drawCentered_Text(d->font, bounds, d->alignVisual, fg, cstr_String(&d->label)); |
217 | } | 220 | } |
218 | clearClip_Paint(&p); | 221 | clearClip_Paint(&p); |
219 | } | 222 | } |
@@ -221,7 +224,7 @@ static void draw_LabelWidget_(const iLabelWidget *d) { | |||
221 | void updateSize_LabelWidget(iLabelWidget *d) { | 224 | void updateSize_LabelWidget(iLabelWidget *d) { |
222 | iWidget *w = as_Widget(d); | 225 | iWidget *w = as_Widget(d); |
223 | const int flags = flags_Widget(w); | 226 | const int flags = flags_Widget(w); |
224 | iInt2 size = add_I2(measure_Text(d->font, cstr_String(&d->label)), muli_I2(padding_(), 2)); | 227 | iInt2 size = add_I2(measure_Text(d->font, cstr_String(&d->label)), muli_I2(padding_(flags), 2)); |
225 | if ((flags & drawKey_WidgetFlag) && d->key) { | 228 | if ((flags & drawKey_WidgetFlag) && d->key) { |
226 | iString str; | 229 | iString str; |
227 | init_String(&str); | 230 | init_String(&str); |
@@ -251,6 +254,7 @@ void init_LabelWidget(iLabelWidget *d, const char *label, int key, int kmods, co | |||
251 | d->kmods = kmods; | 254 | d->kmods = kmods; |
252 | init_Click(&d->click, d, !isEmpty_String(&d->command) ? SDL_BUTTON_LEFT : 0); | 255 | init_Click(&d->click, d, !isEmpty_String(&d->command) ? SDL_BUTTON_LEFT : 0); |
253 | setFlags_Widget(&d->widget, hover_WidgetFlag, d->click.button != 0); | 256 | setFlags_Widget(&d->widget, hover_WidgetFlag, d->click.button != 0); |
257 | d->alignVisual = iFalse; | ||
254 | updateSize_LabelWidget(d); | 258 | updateSize_LabelWidget(d); |
255 | } | 259 | } |
256 | 260 | ||
@@ -269,6 +273,10 @@ void setText_LabelWidget(iLabelWidget *d, const iString *text) { | |||
269 | updateSize_LabelWidget(d); | 273 | updateSize_LabelWidget(d); |
270 | } | 274 | } |
271 | 275 | ||
276 | void setAlignVisually_LabelWidget(iLabelWidget *d, iBool alignVisual) { | ||
277 | d->alignVisual = alignVisual; | ||
278 | } | ||
279 | |||
272 | void updateText_LabelWidget(iLabelWidget *d, const iString *text) { | 280 | void updateText_LabelWidget(iLabelWidget *d, const iString *text) { |
273 | set_String(&d->label, text); | 281 | set_String(&d->label, text); |
274 | refresh_Widget(&d->widget); | 282 | refresh_Widget(&d->widget); |