History

fixed bug in rendering menu rows where state was longer than 5 characters

ZappoMan committed 13 years ago

Commit e429dfe1 parent 37322fc2 files · +20 −25Browse files

Showing the changes to interface/src/MenuRow.cpp (1 of 2 files). Show all

Unified · Split

interface/src/MenuRow.cpp+18−25
@@ -24,20 +24,18 @@ MenuRow::MenuRow() : · 23 unmodified lines
2424 MenuRow::MenuRow(const char * columnName, MenuRowCallback callback) :
2525 callback(callback),
2626 stateNameCallback(NULL) {
27− int length = std::min(MAX_COLUMN_NAME - 5,(int) strlen(columnName));
28− strncpy(this->rowName, columnName, length);
29− memcpy(this->rowName + length, " \0", 5);
27+ this->nameLength = strlen(columnName);
28+ strncpy(this->rowName, columnName, MAX_COLUMN_NAME); // copy the base name
29+ strncpy(this->rowName, this->getName(), MAX_COLUMN_NAME); // now add in state
3030 rowWidth = 0;
3131 }
3232
3333 MenuRow::MenuRow(const char * columnName, MenuRowCallback callback, MenuStateNameCallback stateNameCallback) :
3434 callback(callback),
3535 stateNameCallback(stateNameCallback) {
36− int length = std::min(MAX_COLUMN_NAME - 5,(int) strlen(columnName));
37− strncpy(this->rowName, columnName, length);
38−
39− // note: it would be good to also include the initial state
40− memcpy(this->rowName + length, " \0", 5);
36+ this->nameLength = strlen(columnName);
37+ strncpy(this->rowName, columnName, MAX_COLUMN_NAME);
38+ strncpy(this->rowName, this->getName(), MAX_COLUMN_NAME); // now add in state
4139 rowWidth = 0;
4240 }
4341
@@ -48,32 +46,27 @@ void MenuRow::call() { · 4 unmodified lines
4846 callback(MENU_ROW_PICKED);
4947 }
5048
51−char* MenuRow::getName() {
52− int length = (int) strlen(this->rowName) - 4;
49+const char* MenuRow::getStateName() {
5350 int currentValue = callback(MENU_ROW_GET_VALUE);
54−
51+ const char* stateName;
5552 // If the MenuRow has a custom stateNameCallback function, then call it to get a string
5653 // to display in the menu. Otherwise, use the default implementation.
5754 if (stateNameCallback != NULL) {
58− const char* stateName = stateNameCallback(currentValue);
59− int stateNameLength = strlen(stateName);
60− printf("MenuRow::getName() stateName=%s stateNameLength=%d\n",stateName,stateNameLength);
61−
62− // XXXBHG - BUG!!! - only 5 characters?? someplace else hard coded? for some reason, we end up
63− // with memory corruption down the line, if we allow these states to be longer than 5 characters
64− // including the NULL termination.
65− strncpy(this->rowName + length, stateName,4); // only 4 chars
66− memcpy(this->rowName + length + 5, "\0", 0); // null terminate!!
67−
55+ stateName = stateNameCallback(currentValue);
6856 } else {
6957 if (currentValue == 0) {
70− memcpy(this->rowName + length, " OFF\0", 5);
58+ stateName = " OFF ";
7159 } else if (currentValue == 1) {
72− memcpy(this->rowName + length, " ON \0", 5);
60+ stateName = " ON ";
7361 } else {
74− memcpy(this->rowName + length, " \0", 5);
62+ stateName = " ";
7563 }
76− }
64+ }
65+ return stateName;
66+}
67+
68+char* MenuRow::getName() {
69+ strcpy(this->rowName + nameLength, getStateName());
7770 return this->rowName;
7871 }
7972