Skip to content

Commit a895897

Browse files
committed
UI: fix label display issue
1 parent b97d3a9 commit a895897

File tree

1 file changed

+66
-74
lines changed

1 file changed

+66
-74
lines changed

src/ui/inputs.cpp

Lines changed: 66 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
extern System *g_system;
2020

21-
FormList *activeList = NULL;
22-
FormInput *focusInput = NULL;
23-
FormEditInput *focusEdit = NULL;
21+
FormList *activeList = nullptr;
22+
FormInput *focusInput = nullptr;
23+
FormEditInput *focusEdit = nullptr;
2424
int background = DEFAULT_BACKGROUND;
2525
int foreground = DEFAULT_FOREGROUND;
2626

@@ -33,9 +33,9 @@ FormEditInput *get_focus_edit() {
3333

3434
bool form_ui::optionSelected(int index) {
3535
bool result = false;
36-
if (activeList != NULL) {
36+
if (activeList != nullptr) {
3737
activeList->optionSelected(index);
38-
activeList = NULL;
38+
activeList = nullptr;
3939
result = false;
4040
}
4141
return result;
@@ -48,20 +48,20 @@ void set_input_defaults(int fg, int bg) {
4848

4949
int get_color(var_p_t value, int def) {
5050
int result = def;
51-
if (value != NULL && value->type == V_INT) {
51+
if (value != nullptr && value->type == V_INT) {
5252
result = value->v.i;
5353
if (result < 0) {
5454
result = -result;
5555
} else if (result < 16) {
5656
result = colors[result];
5757
}
58-
} else if (value != NULL && value->type == V_STR &&
58+
} else if (value != nullptr && value->type == V_STR &&
5959
value->v.p.length) {
6060
const char *n = value->v.p.ptr;
6161
if (n[0] == '0' && n[1] == 'x' && n[2]) {
62-
result = strtol(n + 2, NULL, 16);
62+
result = strtol(n + 2, nullptr, 16);
6363
} else if (n[0] == '#' && n[1]) {
64-
result = strtol(n + 1, NULL, 16);
64+
result = strtol(n + 1, nullptr, 16);
6565
} else if (strcasecmp(n, "black") == 0) {
6666
result = DEFAULT_BACKGROUND;
6767
} else if (strcasecmp(n, "red") == 0) {
@@ -121,7 +121,7 @@ FormInput::FormInput(int x, int y, int w, int h) :
121121

122122
FormInput::~FormInput() {
123123
if (focusInput == this) {
124-
focusInput = NULL;
124+
focusInput = nullptr;
125125
}
126126
}
127127

@@ -133,23 +133,23 @@ void FormInput::construct(var_p_t form, var_p_t field, int id) {
133133
_id = id;
134134

135135
var_p_t v_id = map_get(field, FORM_INPUT_ID);
136-
if (v_id == NULL) {
136+
if (v_id == nullptr) {
137137
map_add_var(field, FORM_INPUT_ID, _id);
138138
} else {
139139
v_setint(v_id, _id);
140140
}
141141

142142
var_p_t v_onclick = map_get(field, FORM_INPUT_ONCLICK);
143-
if (v_onclick != NULL && v_onclick->type == V_PTR) {
143+
if (v_onclick != nullptr && v_onclick->type == V_PTR) {
144144
_onclick = v_onclick->v.ap.p;
145145
}
146146

147147
const char *caption = getText();
148-
MAExtent extent = maGetTextSize(caption != NULL && caption[0] ? caption : "Z");
148+
MAExtent extent = maGetTextSize(caption != nullptr && caption[0] ? caption : "Z");
149149
int textW = EXTENT_X(extent);
150150
int textH = EXTENT_Y(extent);
151151

152-
if (_width <= 0 && caption != NULL) {
152+
if (_width <= 0 && caption != nullptr) {
153153
_width = textW + padding(false);
154154
}
155155

@@ -238,25 +238,23 @@ void FormInput::drawLink(const char *caption, int dx, int dy, int sw, int chw) {
238238
}
239239

240240
void FormInput::drawText(const char *caption, int dx, int dy, int sw, int chw) {
241-
int strWidth = chw * (caption == NULL ? 0 : strlen(caption));
242-
int width = sw - dx;
243-
if (width > _width) {
244-
width = _width;
245-
}
246-
if (caption == NULL) {
247-
// nothing to draw
248-
} else if (strWidth > width) {
249-
int len = width / chw;
250-
if (len > 0) {
251-
char *buffer = new char[len + 1];
252-
strncpy(buffer, caption, len - 1);
253-
buffer[len - 1] = '~';
254-
buffer[len] = 0;
255-
maDrawText(dx, dy, buffer, len);
256-
delete [] buffer;
241+
if (caption != nullptr) {
242+
unsigned strWidth = chw * strlen(caption);
243+
int width = sw - dx;
244+
if (width > _width) {
245+
width = _width;
246+
}
247+
if (strWidth > width) {
248+
int len = width / chw;
249+
if (len > 0) {
250+
maDrawText(dx, dy, caption, len - 1);
251+
if (caption[len - 1] != ' ') {
252+
maDrawText(dx + ((len - 1) * chw), dy, "~", 1);
253+
}
254+
}
255+
} else if (strWidth) {
256+
maDrawText(dx, dy, caption, strlen(caption));
257257
}
258-
} else if (strWidth) {
259-
maDrawText(dx, dy, caption, strlen(caption));
260258
}
261259
}
262260

@@ -282,10 +280,10 @@ void FormInput::setHelpTextColor() {
282280

283281
// returns the field var attached to the field
284282
var_p_t FormInput::getField(var_p_t form) {
285-
var_p_t result = NULL;
283+
var_p_t result = nullptr;
286284
if (form->type == V_MAP) {
287285
var_p_t inputs = map_get(form, FORM_INPUTS);
288-
if (inputs != NULL && inputs->type == V_ARRAY) {
286+
if (inputs != nullptr && inputs->type == V_ARRAY) {
289287
for (unsigned i = 0; i < v_asize(inputs) && !result; i++) {
290288
var_p_t elem = v_elem(inputs, i);
291289
if (elem->type == V_MAP && (_id == map_get_int(elem, FORM_INPUT_ID, -1))) {
@@ -302,19 +300,19 @@ bool FormInput::updateUI(var_p_t form, var_p_t field) {
302300
bool updated = false;
303301

304302
var_p_t var = map_get(field, FORM_INPUT_LABEL);
305-
if (var == NULL) {
303+
if (var == nullptr) {
306304
var = map_get(form, FORM_INPUT_LABEL);
307305
}
308-
if (var != NULL) {
306+
if (var != nullptr) {
309307
setText(v_getstr(var));
310308
updated = true;
311309
}
312310

313311
var_p_t v_bg = map_get(field, FORM_INPUT_BG);
314-
if (v_bg == NULL) {
312+
if (v_bg == nullptr) {
315313
v_bg = map_get(form, FORM_INPUT_BG);
316314
}
317-
if (v_bg != NULL) {
315+
if (v_bg != nullptr) {
318316
int bg = get_color(v_bg, _bg);
319317
if (bg != _bg) {
320318
updated = true;
@@ -323,10 +321,10 @@ bool FormInput::updateUI(var_p_t form, var_p_t field) {
323321
}
324322

325323
var_p_t v_fg = map_get(field, FORM_INPUT_FG);
326-
if (v_fg == NULL) {
324+
if (v_fg == nullptr) {
327325
v_fg = map_get(form, FORM_INPUT_FG);
328326
}
329-
if (v_fg != NULL) {
327+
if (v_fg != nullptr) {
330328
int fg = get_color(v_fg, _fg);
331329
if (fg != _fg) {
332330
updated = true;
@@ -361,10 +359,10 @@ bool FormInput::edit(int key, int screenWidth, int charWidth) {
361359
// set the widget value onto the form value
362360
void FormInput::updateForm(var_p_t form) {
363361
var_p_t field = getField(form);
364-
if (field != NULL) {
362+
if (field != nullptr) {
365363
var_p_t inputValue = map_get(field, FORM_INPUT_VALUE);
366364
var_p_t value = map_get(form, FORM_VALUE);
367-
if (value != NULL && inputValue != NULL) {
365+
if (value != nullptr && inputValue != nullptr) {
368366
v_set(value, inputValue);
369367
}
370368
}
@@ -375,9 +373,9 @@ bool FormInput::hasFocus() const {
375373
}
376374

377375
void FormInput::setFocus(bool focus) {
378-
focusEdit = NULL;
376+
focusEdit = nullptr;
379377
if (!_noFocus && focus == (focusInput != this)) {
380-
focusInput = focus ? this : NULL;
378+
focusInput = focus ? this : nullptr;
381379
g_system->getOutput()->setDirty();
382380
}
383381
}
@@ -401,22 +399,16 @@ FormLabel::FormLabel(const char *caption, int x, int y, int w, int h) :
401399
bool FormLabel::updateUI(var_p_t form, var_p_t field) {
402400
bool updated = FormInput::updateUI(form, field);
403401
var_p_t var = map_get(field, FORM_INPUT_LABEL);
404-
if (var != NULL && var->type == V_STR && !_label.equals(var->v.p.ptr)) {
402+
if (var != nullptr && var->type == V_STR && !_label.equals(var->v.p.ptr)) {
405403
_label = var->v.p.ptr;
406404
updated = true;
407405
}
408406
return updated;
409407
}
410408

411409
void FormLabel::draw(int x, int y, int w, int h, int chw) {
412-
int len = _label.length();
413-
if (len > 0) {
414-
if (len * chw >= _width) {
415-
len = _width / chw;
416-
}
417-
maSetColor(_fg);
418-
maDrawText(x, y, _label.c_str(), len);
419-
}
410+
maSetColor(_fg);
411+
drawText(_label.c_str(), x, y, w, chw);
420412
}
421413

422414
//
@@ -446,7 +438,7 @@ FormEditInput::FormEditInput(int x, int y, int w, int h) :
446438

447439
FormEditInput::~FormEditInput() {
448440
if (focusEdit == this) {
449-
focusEdit = NULL;
441+
focusEdit = nullptr;
450442
}
451443
}
452444

@@ -488,8 +480,8 @@ int FormEditInput::getControlKey(int key) {
488480

489481
void FormEditInput::setFocus(bool focus) {
490482
if (!_noFocus && focus == (focusInput != this)) {
491-
focusInput = focus ? this : NULL;
492-
focusEdit = focus ? this : NULL;
483+
focusInput = focus ? this : nullptr;
484+
focusEdit = focus ? this : nullptr;
493485
g_system->getOutput()->setDirty();
494486
}
495487
}
@@ -501,15 +493,15 @@ FormLineInput::FormLineInput(const char *value, const char *help, int size, bool
501493
int x, int y, int w, int h) :
502494
FormEditInput(x, y, w, h),
503495
_help(help),
504-
_buffer(NULL),
496+
_buffer(nullptr),
505497
_size(size),
506498
_scroll(0),
507499
_mark(-1),
508500
_point(0),
509501
_grow(grow) {
510502
_buffer = new char[_size + 1];
511503
_buffer[0] = '\0';
512-
if (value != NULL && value[0]) {
504+
if (value != nullptr && value[0]) {
513505
int len = MIN(strlen(value), (unsigned)_size);
514506
memcpy(_buffer, value, len);
515507
_buffer[len] = '\0';
@@ -519,7 +511,7 @@ FormLineInput::FormLineInput(const char *value, const char *help, int size, bool
519511

520512
FormLineInput::~FormLineInput() {
521513
delete [] _buffer;
522-
_buffer = NULL;
514+
_buffer = nullptr;
523515
}
524516

525517
void FormLineInput::clicked(int x, int y, bool pressed) {
@@ -583,7 +575,7 @@ void FormLineInput::draw(int x, int y, int w, int h, int chw) {
583575

584576
bool FormLineInput::edit(int key, int screenWidth, int charWidth) {
585577
int wChars;
586-
int len = _buffer == NULL ? 0 : strlen(_buffer);
578+
int len = _buffer == nullptr ? 0 : strlen(_buffer);
587579

588580
key = getControlKey(key);
589581
switch (key) {
@@ -694,14 +686,14 @@ bool FormLineInput::edit(int key, int screenWidth, int charWidth) {
694686

695687
void FormLineInput::selectAll() {
696688
_point = 0;
697-
_mark = _buffer == NULL ? -0 : strlen(_buffer);
689+
_mark = _buffer == nullptr ? -0 : strlen(_buffer);
698690
}
699691

700692
void FormLineInput::updateField(var_p_t form) {
701693
var_p_t field = getField(form);
702-
if (field != NULL) {
694+
if (field != nullptr) {
703695
var_p_t value = map_get(field, FORM_INPUT_VALUE);
704-
if (value != NULL) {
696+
if (value != nullptr) {
705697
v_setstr(value, _buffer);
706698
}
707699
}
@@ -710,7 +702,7 @@ void FormLineInput::updateField(var_p_t form) {
710702
bool FormLineInput::updateUI(var_p_t form, var_p_t field) {
711703
bool updated = FormInput::updateUI(form, field);
712704
var_p_t var = map_get(field, FORM_INPUT_VALUE);
713-
if (var != NULL) {
705+
if (var != nullptr) {
714706
const char *value = v_getstr(var);
715707
if (value && strcmp(value, _buffer) != 0) {
716708
int len = MIN(strlen(value), (unsigned)_size);
@@ -724,7 +716,7 @@ bool FormLineInput::updateUI(var_p_t form, var_p_t field) {
724716
}
725717

726718
char *FormLineInput::copy(bool cut) {
727-
char *result = NULL;
719+
char *result = nullptr;
728720
int len = strlen(_buffer);
729721
int start = MIN(_mark, _point);
730722
if (_mark != -1 && start < len) {
@@ -759,7 +751,7 @@ void FormLineInput::cut() {
759751
void FormLineInput::paste(const char *text) {
760752
int len = strlen(_buffer);
761753
int avail = _size - (len + 1);
762-
if (text != NULL && avail > 0) {
754+
if (text != nullptr && avail > 0) {
763755
int index = _scroll + _point;
764756
int count = strlen(text);
765757
if (count > avail) {
@@ -881,10 +873,10 @@ FormList::FormList(ListModel *model, int x, int y, int w, int h) :
881873

882874
FormList::~FormList() {
883875
if (activeList == this) {
884-
activeList = NULL;
876+
activeList = nullptr;
885877
}
886878
delete _model;
887-
_model = NULL;
879+
_model = nullptr;
888880
}
889881

890882
void FormList::optionSelected(int index) {
@@ -912,7 +904,7 @@ void FormList::selectIndex(int index) {
912904
bool FormList::updateUI(var_p_t form, var_p_t field) {
913905
bool updated = FormInput::updateUI(form, field);
914906
var_p_t var = map_get(field, FORM_INPUT_VALUE);
915-
if (var != NULL && (var->type == V_ARRAY || var->type == V_STR)) {
907+
if (var != nullptr && (var->type == V_ARRAY || var->type == V_STR)) {
916908
// update list control with new array or string variable
917909
_model->clear();
918910
_model->create(var);
@@ -921,7 +913,7 @@ bool FormList::updateUI(var_p_t form, var_p_t field) {
921913

922914
// set the selectedIndex
923915
var = map_get(field, FORM_INPUT_INDEX);
924-
if (var != NULL) {
916+
if (var != nullptr) {
925917
selectIndex(v_getint(var));
926918
updated = true;
927919
}
@@ -934,7 +926,7 @@ void FormList::updateForm(var_p_t form) {
934926

935927
// set the form value
936928
var_p_t value = map_get(form, FORM_VALUE);
937-
if (value == NULL) {
929+
if (value == nullptr) {
938930
value = map_add_var(form, FORM_VALUE, 0);
939931
}
940932
if (selected) {
@@ -945,9 +937,9 @@ void FormList::updateForm(var_p_t form) {
945937

946938
// set the selectedIndex
947939
var_p_t field = getField(form);
948-
if (field != NULL) {
940+
if (field != nullptr) {
949941
value = map_get(field, FORM_INPUT_INDEX);
950-
if (value != NULL) {
942+
if (value != nullptr) {
951943
v_setint(value, _model->selected());
952944
}
953945
}
@@ -1062,7 +1054,7 @@ bool FormListBox::selected(MAPoint2d pt, int offsX, int offsY, bool &redraw) {
10621054
_topIndex++;
10631055
redraw = true;
10641056
} else {
1065-
activeList = NULL;
1057+
activeList = nullptr;
10661058
}
10671059
return result;
10681060
}

0 commit comments

Comments
 (0)