guiTextCtrl.cpp
Engine/source/gui/controls/guiTextCtrl.cpp
Public Functions
ConsoleDocClass(GuiTextCtrl , "@brief GUI <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> object this displays <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> single line of text, without <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">TorqueML.\n\n</a>" " @<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" " <a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> <a href="/coding/class/classguitextctrl/">GuiTextCtrl</a>()\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " {\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " text=\"Hello World\";\n" " textID = \"\"STR_HELLO\"\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " maxlength = \"1024\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " //Properties not specific <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> this <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> have been omitted from this <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">example.\n</a>" " };\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" "@see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">GuiControl\n</a>" "@see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Localization\n\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">GuiCore\n</a>" )
DefineEngineMethod(GuiTextCtrl , setText , void , (const char *text) , "@brief Sets the text in the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">control.\n\n</a>" "@param text Text <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> display in the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">control.\n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "// Set the text <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> show in the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">control\n</a>" "% text)
DefineEngineMethod(GuiTextCtrl , setTextID , void , (const char *textID) , "@brief Maps the text ctrl <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> variable used in localization, rather than raw <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">text.\n\n</a>" " @param textID Name of variable text should be mapped <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">to\n</a>" " @<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "//Inform the <a href="/coding/class/classguitextctrl/">GuiTextCtrl</a> <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> of the textID <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">use\n</a>" "%thisGuiTextCtrl.setTextID(\"STR_QUIT\");\n" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" "@see <a href="/coding/class/classguicontrol/">GuiControl</a>" "@see Localization" )
Detailed Description
Public Functions
ConsoleDocClass(GuiTextCtrl , "@brief GUI <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> object this displays <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> single line of text, without <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">TorqueML.\n\n</a>" " @<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" " <a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> <a href="/coding/class/classguitextctrl/">GuiTextCtrl</a>()\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " {\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " text=\"Hello World\";\n" " textID = \"\"STR_HELLO\"\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " maxlength = \"1024\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " //Properties not specific <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> this <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> have been omitted from this <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">example.\n</a>" " };\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" "@see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">GuiControl\n</a>" "@see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Localization\n\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">GuiCore\n</a>" )
DefineEngineMethod(GuiTextCtrl , setText , void , (const char *text) , "@brief Sets the text in the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">control.\n\n</a>" "@param text Text <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> display in the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">control.\n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "// Set the text <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> show in the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">control\n</a>" "% text)
DefineEngineMethod(GuiTextCtrl , setTextID , void , (const char *textID) , "@brief Maps the text ctrl <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> variable used in localization, rather than raw <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">text.\n\n</a>" " @param textID Name of variable text should be mapped <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">to\n</a>" " @<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "//Inform the <a href="/coding/class/classguitextctrl/">GuiTextCtrl</a> <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> of the textID <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">use\n</a>" "%thisGuiTextCtrl.setTextID(\"STR_QUIT\");\n" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" "@see <a href="/coding/class/classguicontrol/">GuiControl</a>" "@see Localization" )
IMPLEMENT_CONOBJECT(GuiTextCtrl )
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "gui/controls/guiTextCtrl.h" 26 27#include "gui/core/guiDefaultControlRender.h" 28#include "console/consoleTypes.h" 29#include "console/console.h" 30#include "core/color.h" 31#include "i18n/lang.h" 32#include "gfx/gfxDrawUtil.h" 33#include "console/engineAPI.h" 34 35IMPLEMENT_CONOBJECT( GuiTextCtrl ); 36 37ConsoleDocClass( GuiTextCtrl, 38 "@brief GUI control object this displays a single line of text, without TorqueML.\n\n" 39 40 "@tsexample\n" 41 " new GuiTextCtrl()\n" 42 " {\n" 43 " text = \"Hello World\";\n" 44 " textID = \"\"STR_HELLO\"\";\n" 45 " maxlength = \"1024\";\n" 46 " //Properties not specific to this control have been omitted from this example.\n" 47 " };\n" 48 "@endtsexample\n\n" 49 50 "@see GuiControl\n" 51 "@see Localization\n\n" 52 "@ingroup GuiCore\n" 53); 54 55GuiTextCtrl::GuiTextCtrl() 56{ 57 //default fonts 58 mInitialText = StringTable->EmptyString(); 59 mInitialTextID = StringTable->EmptyString(); 60 mText[0] = '\0'; 61 mMaxStrLen = GuiTextCtrl::MAX_STRING_LENGTH; 62} 63 64DefineEngineMethod( GuiTextCtrl, setText, void, (const char* text),, 65 "@brief Sets the text in the control.\n\n" 66 "@param text Text to display in the control.\n" 67 "@tsexample\n" 68 "// Set the text to show in the control\n" 69 "%text = \"Gideon - Destroyer of World\";\n\n" 70 "// Inform the GuiTextCtrl control to change its text to the defined value\n" 71 "%thisGuiTextCtrl.setText(%text);\n" 72 "@endtsexample\n\n" 73 "@see GuiControl") 74{ 75 object->setText( text ); 76} 77 78DefineEngineMethod( GuiTextCtrl, setTextID, void, (const char* textID),, 79 "@brief Maps the text ctrl to a variable used in localization, rather than raw text.\n\n" 80 "@param textID Name of variable text should be mapped to\n" 81 "@tsexample\n" 82 "// Inform the GuiTextCtrl control of the textID to use\n" 83 "%thisGuiTextCtrl.setTextID(\"STR_QUIT\");\n" 84 "@endtsexample\n\n" 85 "@see GuiControl" 86 "@see Localization") 87{ 88 object->setTextID( textID ); 89} 90 91void GuiTextCtrl::initPersistFields() 92{ 93 addProtectedField("text", TypeCaseString, Offset(mInitialText, GuiTextCtrl), setText, getTextProperty, 94 "The text to show on the control."); 95 96 addField( "textID", TypeString, Offset( mInitialTextID, GuiTextCtrl ), 97 "Maps the text of this control to a variable used in localization, rather than raw text."); 98 99 addField( "maxLength", TypeS32, Offset( mMaxStrLen, GuiTextCtrl ), 100 "Defines the maximum length of the text. The default is 1024." ); 101 102 Parent::initPersistFields(); 103} 104 105bool GuiTextCtrl::onAdd() 106{ 107 if(!Parent::onAdd()) 108 return false; 109 110 dStrncpy(mText, (UTF8*)mInitialText, MAX_STRING_LENGTH); 111 mText[MAX_STRING_LENGTH] = '\0'; 112 113 114 return true; 115} 116 117void GuiTextCtrl::inspectPostApply() 118{ 119 Parent::inspectPostApply(); 120 if(mInitialTextID && *mInitialTextID != 0) 121 setTextID(mInitialTextID); 122 else if( mConsoleVariable[ 0 ] ) 123 setText( getVariable() ); 124 else 125 setText(mInitialText); 126} 127 128bool GuiTextCtrl::onWake() 129{ 130 if ( !Parent::onWake() ) 131 return false; 132 133 if( !mProfile->mFont ) 134 { 135 Con::errorf( "GuiTextCtrl::onWake() - no valid font in profile '%s'", mProfile->getName() ); 136 return false; 137 } 138 if(mInitialTextID && *mInitialTextID != 0) 139 setTextID(mInitialTextID); 140 141 if ( mConsoleVariable[0] ) 142 { 143 const char *txt = Con::getVariable( mConsoleVariable ); 144 if ( txt ) 145 { 146 if ( dStrlen( txt ) > mMaxStrLen ) 147 { 148 char* buf = new char[mMaxStrLen + 1]; 149 dStrncpy( buf, txt, mMaxStrLen ); 150 buf[mMaxStrLen] = 0; 151 setScriptValue( buf ); 152 delete [] buf; 153 } 154 else 155 setScriptValue( txt ); 156 } 157 } 158 159 //resize 160 autoResize(); 161 162 return true; 163} 164 165void GuiTextCtrl::autoResize() 166{ 167 if( mProfile->mAutoSizeWidth || mProfile->mAutoSizeHeight) 168 { 169 if( !mProfile->mFont ) 170 { 171 mProfile->loadFont(); 172 if( !mProfile->mFont ) 173 return; 174 } 175 176 Point2I newExtents = getExtent(); 177 if ( mProfile->mAutoSizeWidth ) 178 newExtents.x = mProfile->mFont->getStrWidth((const UTF8 *) mText ); 179 if ( mProfile->mAutoSizeHeight ) 180 newExtents.y = mProfile->mFont->getHeight() + 4; 181 182 setExtent( newExtents ); 183 } 184} 185 186void GuiTextCtrl::setText(const char *txt) 187{ 188 //make sure we don't call this before onAdd(); 189 if( !mProfile ) 190 return; 191 192 // The txt pointer is sometimes the same as the mText pointer, so make sure 193 // we don't call strncpy with overlapping src and dest. 194 if (txt && txt != mText) 195 dStrncpy(mText, (UTF8*)txt, MAX_STRING_LENGTH); 196 mText[MAX_STRING_LENGTH] = '\0'; 197 198 setVariable((char*)mText); 199 setUpdate(); 200 201 autoResize(); 202} 203 204void GuiTextCtrl::setTextID(const char *id) 205{ 206 S32 n = Con::getIntVariable(id, -1); 207 if(n != -1) 208 { 209 mInitialTextID = StringTable->insert(id); 210 setTextID(n); 211 } 212} 213void GuiTextCtrl::setTextID(S32 id) 214{ 215 const UTF8 *str = getGUIString(id); 216 if(str) 217 setText((const char*)str); 218 //mInitialTextID = id; 219} 220 221void GuiTextCtrl::onPreRender() 222{ 223 Parent::onPreRender(); 224 225 const char * var = getVariable(); 226 if(var && var[0] && dStricmp((char*)mText, var)) 227 setText(var); 228} 229 230void GuiTextCtrl::onRender(Point2I offset, const RectI &updateRect) 231{ 232 renderBorder( RectI( offset, getExtent() ), mProfile ); 233 234 GFX->getDrawUtil()->setBitmapModulation( mProfile->mFontColor ); 235 renderJustifiedText(offset, getExtent(), (char*)mText); 236 237 //render the child controls 238 renderChildControls(offset, updateRect); 239} 240 241const char *GuiTextCtrl::getScriptValue() 242{ 243 return getText(); 244} 245 246void GuiTextCtrl::setScriptValue(const char *val) 247{ 248 setText(val); 249} 250