2
0

guiMLTextEditCtrl.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "gui/controls/guiMLTextEditCtrl.h"
  23. #include "gui/containers/guiScrollCtrl.h"
  24. #include "gui/core/guiCanvas.h"
  25. #include "console/consoleTypes.h"
  26. #include "core/frameAllocator.h"
  27. #include "core/stringBuffer.h"
  28. #include "gfx/gfxDrawUtil.h"
  29. #include "console/engineAPI.h"
  30. IMPLEMENT_CONOBJECT(GuiMLTextEditCtrl);
  31. ConsoleDocClass( GuiMLTextEditCtrl,
  32. "@brief A text entry control that accepts the Gui Markup Language ('ML') tags and multiple lines.\n\n"
  33. "@tsexample\n"
  34. "new GuiMLTextEditCtrl()\n"
  35. " {\n"
  36. " lineSpacing = \"2\";\n"
  37. " allowColorChars = \"0\";\n"
  38. " maxChars = \"-1\";\n"
  39. " deniedSound = \"DeniedSoundProfile\";\n"
  40. " text = \"\";\n"
  41. " escapeCommand = \"onEscapeScriptFunction();\";\n"
  42. " //Properties not specific to this control have been omitted from this example.\n"
  43. " };\n"
  44. "@endtsexample\n\n"
  45. "@see GuiMLTextCtrl\n"
  46. "@see GuiControl\n\n"
  47. "@ingroup GuiControls\n"
  48. );
  49. //--------------------------------------------------------------------------
  50. GuiMLTextEditCtrl::GuiMLTextEditCtrl()
  51. {
  52. mEscapeCommand = StringTable->insert( "" );
  53. mIsEditCtrl = true;
  54. mActive = true;
  55. mVertMoveAnchorValid = false;
  56. }
  57. //--------------------------------------------------------------------------
  58. GuiMLTextEditCtrl::~GuiMLTextEditCtrl()
  59. {
  60. }
  61. //--------------------------------------------------------------------------
  62. bool GuiMLTextEditCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
  63. {
  64. // We don't want to get any smaller than our parent:
  65. Point2I newExt = newExtent;
  66. GuiControl* parent = getParent();
  67. if ( parent )
  68. newExt.y = getMax( parent->getHeight(), newExt.y );
  69. return Parent::resize( newPosition, newExt );
  70. }
  71. //--------------------------------------------------------------------------
  72. void GuiMLTextEditCtrl::initPersistFields()
  73. {
  74. addField( "escapeCommand", TypeString, Offset( mEscapeCommand, GuiMLTextEditCtrl ), "Script function to run whenever the 'escape' key is pressed when this control is in focus.\n");
  75. Parent::initPersistFields();
  76. }
  77. //--------------------------------------------------------------------------
  78. void GuiMLTextEditCtrl::setFirstResponder()
  79. {
  80. Parent::setFirstResponder();
  81. GuiCanvas *root = getRoot();
  82. if (root != NULL)
  83. {
  84. root->enableKeyboardTranslation();
  85. // If the native OS accelerator keys are not disabled
  86. // then some key events like Delete, ctrl+V, etc may
  87. // not make it down to us.
  88. root->setNativeAcceleratorsEnabled( false );
  89. }
  90. }
  91. void GuiMLTextEditCtrl::onLoseFirstResponder()
  92. {
  93. GuiCanvas *root = getRoot();
  94. if (root != NULL)
  95. {
  96. root->setNativeAcceleratorsEnabled( true );
  97. root->disableKeyboardTranslation();
  98. }
  99. // Redraw the control:
  100. setUpdate();
  101. }
  102. //--------------------------------------------------------------------------
  103. bool GuiMLTextEditCtrl::onWake()
  104. {
  105. if( !Parent::onWake() )
  106. return false;
  107. getRoot()->enableKeyboardTranslation();
  108. return true;
  109. }
  110. //--------------------------------------------------------------------------
  111. // Key events...
  112. bool GuiMLTextEditCtrl::onKeyDown(const GuiEvent& event)
  113. {
  114. if ( !isActive() )
  115. return false;
  116. setUpdate();
  117. //handle modifiers first...
  118. if (event.modifier & SI_PRIMARY_CTRL)
  119. {
  120. switch(event.keyCode)
  121. {
  122. //copy/cut
  123. case KEY_C:
  124. case KEY_X:
  125. {
  126. //make sure we actually have something selected
  127. if (mSelectionActive)
  128. {
  129. copyToClipboard(mSelectionStart, mSelectionEnd);
  130. //if we're cutting, also delete the selection
  131. if (event.keyCode == KEY_X)
  132. {
  133. mSelectionActive = false;
  134. deleteChars(mSelectionStart, mSelectionEnd);
  135. mCursorPosition = mSelectionStart;
  136. }
  137. else
  138. mCursorPosition = mSelectionEnd + 1;
  139. }
  140. return true;
  141. }
  142. //paste
  143. case KEY_V:
  144. {
  145. const char *clipBuf = Platform::getClipboard();
  146. if (dStrlen(clipBuf) > 0)
  147. {
  148. // Normal ascii keypress. Go ahead and add the chars...
  149. if (mSelectionActive == true)
  150. {
  151. mSelectionActive = false;
  152. deleteChars(mSelectionStart, mSelectionEnd);
  153. mCursorPosition = mSelectionStart;
  154. }
  155. insertChars(clipBuf, dStrlen(clipBuf), mCursorPosition);
  156. }
  157. return true;
  158. }
  159. default:
  160. break;
  161. }
  162. }
  163. else if ( event.modifier & SI_SHIFT )
  164. {
  165. switch ( event.keyCode )
  166. {
  167. case KEY_TAB:
  168. return( Parent::onKeyDown( event ) );
  169. default:
  170. break;
  171. }
  172. }
  173. else if ( event.modifier == 0 )
  174. {
  175. switch (event.keyCode)
  176. {
  177. // Escape:
  178. case KEY_ESCAPE:
  179. if ( mEscapeCommand[0] )
  180. {
  181. Con::evaluate( mEscapeCommand );
  182. return( true );
  183. }
  184. return( Parent::onKeyDown( event ) );
  185. // Deletion
  186. case KEY_BACKSPACE:
  187. case KEY_DELETE:
  188. handleDeleteKeys(event);
  189. return true;
  190. // Cursor movement
  191. case KEY_LEFT:
  192. case KEY_RIGHT:
  193. case KEY_UP:
  194. case KEY_DOWN:
  195. case KEY_HOME:
  196. case KEY_END:
  197. handleMoveKeys(event);
  198. return true;
  199. // Special chars...
  200. case KEY_TAB:
  201. // insert 3 spaces
  202. if (mSelectionActive == true)
  203. {
  204. mSelectionActive = false;
  205. deleteChars(mSelectionStart, mSelectionEnd);
  206. mCursorPosition = mSelectionStart;
  207. }
  208. insertChars( "\t", 1, mCursorPosition );
  209. return true;
  210. case KEY_RETURN:
  211. // insert carriage return
  212. if (mSelectionActive == true)
  213. {
  214. mSelectionActive = false;
  215. deleteChars(mSelectionStart, mSelectionEnd);
  216. mCursorPosition = mSelectionStart;
  217. }
  218. insertChars( "\n", 1, mCursorPosition );
  219. return true;
  220. default:
  221. break;
  222. }
  223. }
  224. if ( (mFont && mFont->isValidChar(event.ascii)) || (!mFont && event.ascii != 0) )
  225. {
  226. // Normal ascii keypress. Go ahead and add the chars...
  227. if (mSelectionActive == true)
  228. {
  229. mSelectionActive = false;
  230. deleteChars(mSelectionStart, mSelectionEnd);
  231. mCursorPosition = mSelectionStart;
  232. }
  233. UTF8 *outString = NULL;
  234. U32 outStringLen = 0;
  235. #ifdef TORQUE_UNICODE
  236. UTF16 inData[2] = { event.ascii, 0 };
  237. StringBuffer inBuff(inData);
  238. FrameTemp<UTF8> outBuff(4);
  239. inBuff.getCopy8(outBuff, 4);
  240. outString = outBuff;
  241. outStringLen = dStrlen(outBuff);
  242. #else
  243. char ascii = char(event.ascii);
  244. outString = &ascii;
  245. outStringLen = 1;
  246. #endif
  247. insertChars(outString, outStringLen, mCursorPosition);
  248. mVertMoveAnchorValid = false;
  249. return true;
  250. }
  251. // Otherwise, let the parent have the event...
  252. return Parent::onKeyDown(event);
  253. }
  254. //--------------------------------------
  255. void GuiMLTextEditCtrl::handleDeleteKeys(const GuiEvent& event)
  256. {
  257. if ( isSelectionActive() )
  258. {
  259. mSelectionActive = false;
  260. deleteChars(mSelectionStart, mSelectionEnd+1);
  261. mCursorPosition = mSelectionStart;
  262. }
  263. else
  264. {
  265. switch ( event.keyCode )
  266. {
  267. case KEY_BACKSPACE:
  268. if (mCursorPosition != 0)
  269. {
  270. // delete one character left
  271. deleteChars(mCursorPosition-1, mCursorPosition);
  272. setUpdate();
  273. }
  274. break;
  275. case KEY_DELETE:
  276. if (mCursorPosition != mTextBuffer.length())
  277. {
  278. // delete one character right
  279. deleteChars(mCursorPosition, mCursorPosition+1);
  280. setUpdate();
  281. }
  282. break;
  283. default:
  284. AssertFatal(false, "Unknown key code received!");
  285. }
  286. }
  287. }
  288. //--------------------------------------
  289. void GuiMLTextEditCtrl::handleMoveKeys(const GuiEvent& event)
  290. {
  291. if ( event.modifier & SI_SHIFT )
  292. return;
  293. mSelectionActive = false;
  294. switch ( event.keyCode )
  295. {
  296. case KEY_LEFT:
  297. mVertMoveAnchorValid = false;
  298. // move one left
  299. if ( mCursorPosition != 0 )
  300. {
  301. mCursorPosition--;
  302. setUpdate();
  303. }
  304. break;
  305. case KEY_RIGHT:
  306. mVertMoveAnchorValid = false;
  307. // move one right
  308. if ( mCursorPosition != mTextBuffer.length() )
  309. {
  310. mCursorPosition++;
  311. setUpdate();
  312. }
  313. break;
  314. case KEY_UP:
  315. case KEY_DOWN:
  316. {
  317. Line* walk;
  318. for ( walk = mLineList; walk->next; walk = walk->next )
  319. {
  320. if ( mCursorPosition <= ( walk->textStart + walk->len ) )
  321. break;
  322. }
  323. if ( !walk )
  324. return;
  325. if ( event.keyCode == KEY_UP )
  326. {
  327. if ( walk == mLineList )
  328. return;
  329. }
  330. else if ( walk->next == NULL )
  331. return;
  332. Point2I newPos;
  333. newPos.set( 0, walk->y );
  334. // Find the x-position:
  335. if ( !mVertMoveAnchorValid )
  336. {
  337. Point2I cursorTopP, cursorBottomP;
  338. ColorI color;
  339. getCursorPositionAndColor(cursorTopP, cursorBottomP, color);
  340. mVertMoveAnchor = cursorTopP.x;
  341. mVertMoveAnchorValid = true;
  342. }
  343. newPos.x = mVertMoveAnchor;
  344. // Set the new y-position:
  345. if (event.keyCode == KEY_UP)
  346. newPos.y--;
  347. else
  348. newPos.y += (walk->height + 1);
  349. if (setCursorPosition(getTextPosition(newPos)))
  350. mVertMoveAnchorValid = false;
  351. break;
  352. }
  353. case KEY_HOME:
  354. case KEY_END:
  355. {
  356. mVertMoveAnchorValid = false;
  357. Line* walk;
  358. for (walk = mLineList; walk->next; walk = walk->next)
  359. {
  360. if (mCursorPosition <= (walk->textStart + walk->len))
  361. break;
  362. }
  363. if (walk)
  364. {
  365. if (event.keyCode == KEY_HOME)
  366. {
  367. //place the cursor at the beginning of the first atom if there is one
  368. if (walk->atomList)
  369. mCursorPosition = walk->atomList->textStart;
  370. else
  371. mCursorPosition = walk->textStart;
  372. }
  373. else
  374. {
  375. mCursorPosition = walk->textStart;
  376. mCursorPosition += walk->len;
  377. }
  378. setUpdate();
  379. }
  380. break;
  381. }
  382. default:
  383. AssertFatal(false, "Unknown move key code was received!");
  384. }
  385. ensureCursorOnScreen();
  386. }
  387. //--------------------------------------------------------------------------
  388. void GuiMLTextEditCtrl::onRender(Point2I offset, const RectI& updateRect)
  389. {
  390. Parent::onRender(offset, updateRect);
  391. // We are the first responder, draw our cursor in the appropriate position...
  392. if (isFirstResponder())
  393. {
  394. Point2I top, bottom;
  395. ColorI color;
  396. getCursorPositionAndColor(top, bottom, color);
  397. GFX->getDrawUtil()->drawLine(top + offset, bottom + offset, mProfile->mCursorColor);
  398. }
  399. }