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

This commit is contained in:
ZappoMan 2013-04-13 15:57:30 -07:00
parent 37322fcb73
commit e429dfe61f
2 changed files with 20 additions and 25 deletions

View file

@ -24,20 +24,18 @@ MenuRow::MenuRow() :
MenuRow::MenuRow(const char * columnName, MenuRowCallback callback) :
callback(callback),
stateNameCallback(NULL) {
int length = std::min(MAX_COLUMN_NAME - 5,(int) strlen(columnName));
strncpy(this->rowName, columnName, length);
memcpy(this->rowName + length, " \0", 5);
this->nameLength = strlen(columnName);
strncpy(this->rowName, columnName, MAX_COLUMN_NAME); // copy the base name
strncpy(this->rowName, this->getName(), MAX_COLUMN_NAME); // now add in state
rowWidth = 0;
}
MenuRow::MenuRow(const char * columnName, MenuRowCallback callback, MenuStateNameCallback stateNameCallback) :
callback(callback),
stateNameCallback(stateNameCallback) {
int length = std::min(MAX_COLUMN_NAME - 5,(int) strlen(columnName));
strncpy(this->rowName, columnName, length);
// note: it would be good to also include the initial state
memcpy(this->rowName + length, " \0", 5);
this->nameLength = strlen(columnName);
strncpy(this->rowName, columnName, MAX_COLUMN_NAME);
strncpy(this->rowName, this->getName(), MAX_COLUMN_NAME); // now add in state
rowWidth = 0;
}
@ -48,32 +46,27 @@ void MenuRow::call() {
callback(MENU_ROW_PICKED);
}
char* MenuRow::getName() {
int length = (int) strlen(this->rowName) - 4;
const char* MenuRow::getStateName() {
int currentValue = callback(MENU_ROW_GET_VALUE);
const char* stateName;
// If the MenuRow has a custom stateNameCallback function, then call it to get a string
// to display in the menu. Otherwise, use the default implementation.
if (stateNameCallback != NULL) {
const char* stateName = stateNameCallback(currentValue);
int stateNameLength = strlen(stateName);
printf("MenuRow::getName() stateName=%s stateNameLength=%d\n",stateName,stateNameLength);
// XXXBHG - BUG!!! - only 5 characters?? someplace else hard coded? for some reason, we end up
// with memory corruption down the line, if we allow these states to be longer than 5 characters
// including the NULL termination.
strncpy(this->rowName + length, stateName,4); // only 4 chars
memcpy(this->rowName + length + 5, "\0", 0); // null terminate!!
stateName = stateNameCallback(currentValue);
} else {
if (currentValue == 0) {
memcpy(this->rowName + length, " OFF\0", 5);
stateName = " OFF ";
} else if (currentValue == 1) {
memcpy(this->rowName + length, " ON \0", 5);
stateName = " ON ";
} else {
memcpy(this->rowName + length, " \0", 5);
stateName = " ";
}
}
}
return stateName;
}
char* MenuRow::getName() {
strcpy(this->rowName + nameLength, getStateName());
return this->rowName;
}

View file

@ -26,9 +26,11 @@ public:
~MenuRow();
void call();
char * getName();
const char* getStateName();
int getWidth(float scale, int mono, int leftPosition);
int getWidth();
private:
int nameLength;
char rowName[MAX_COLUMN_NAME];
int rowWidth;
MenuRowCallback callback;