From 7c96e0252f40fb97daca243090315ee895e55775 Mon Sep 17 00:00:00 2001 From: driAn Date: Tue, 23 Sep 2008 18:58:14 +0000 Subject: [PATCH] restrict number of chars in inputbox git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@2737 e016b1c5-ed68-44aa-a2f7-31cac88cffde --- source/src/console.h | 5 +++-- source/src/menus.cpp | 9 +++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/source/src/console.h b/source/src/console.h index a71059f1..28a558f3 100644 --- a/source/src/console.h +++ b/source/src/console.h @@ -31,9 +31,9 @@ struct consolebuffer struct textinputbuffer { string buf; - int pos; + int pos, max; - textinputbuffer() : pos(-1) + textinputbuffer() : pos(-1), max(0) { buf[0] = '\0'; } @@ -96,6 +96,7 @@ struct textinputbuffer if(unicode) { size_t len = strlen(buf); + if(max && (int)len>=max) break; if(len+1 < sizeof(buf)) { if(pos < 0) buf[len] = unicode; diff --git a/source/src/menus.cpp b/source/src/menus.cpp index caeae599..0b3d690f 100644 --- a/source/src/menus.cpp +++ b/source/src/menus.cpp @@ -187,9 +187,10 @@ struct mitemtextinput : mitemtext char *defaultvalueexp; bool modified; - mitemtextinput(gmenu *parent, char *text, char *value, char *action, char *hoveraction, color *bgcolor) : mitemtext(parent, text, action, hoveraction, bgcolor), defaultvalueexp(value), modified(false) + mitemtextinput(gmenu *parent, char *text, char *value, char *action, char *hoveraction, color *bgcolor, int maxchars) : mitemtext(parent, text, action, hoveraction, bgcolor), defaultvalueexp(value), modified(false) { s_strcpy(input.buf, value); + input.max = maxchars>0 ? maxchars : 15; } virtual int width() { return text_width(input.buf) + text_width(text); } @@ -511,10 +512,10 @@ void menuitem(char *text, char *action, char *hoveraction) lastmenu->items.add(new mitemtext(lastmenu, t, newstring(action[0] ? action : text), hoveraction[0] ? newstring(hoveraction) : NULL, NULL)); } -void menuitemtextinput(char *text, char *value, char *action, char *hoveraction) +void menuitemtextinput(char *text, char *value, char *action, char *hoveraction, char *maxchars) { if(!lastmenu || !text || !value) return; - lastmenu->items.add(new mitemtextinput(lastmenu, newstring(text), newstring(value), action[0] ? newstring(action) : NULL, hoveraction[0] ? newstring(hoveraction) : NULL, NULL)); + lastmenu->items.add(new mitemtextinput(lastmenu, newstring(text), newstring(value), action[0] ? newstring(action) : NULL, hoveraction[0] ? newstring(hoveraction) : NULL, NULL, atoi(maxchars))); } void menuitemslider(char *text, char *min_, char *max_, char *value, char *step, char *display, char *action) @@ -575,7 +576,7 @@ COMMAND(chmenumdl, ARG_6STR); COMMAND(showmenu, ARG_1STR); COMMAND(menuinit, ARG_1STR); COMMAND(menuitem, ARG_3STR); -COMMAND(menuitemtextinput, ARG_4STR); +COMMAND(menuitemtextinput, ARG_5STR); COMMAND(menuitemslider, ARG_7STR); COMMAND(menuitemkeyinput, ARG_4STR); COMMAND(menuitemcheckbox, ARG_3STR);