guiMLTextEditCtrl.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. docsURL;
  75. addField( "escapeCommand", TypeString, Offset( mEscapeCommand, GuiMLTextEditCtrl ), "Script function to run whenever the 'escape' key is pressed when this control is in focus.\n");
  76. Parent::initPersistFields();
  77. }
  78. //--------------------------------------------------------------------------
  79. void GuiMLTextEditCtrl::setFirstResponder()
  80. {
  81. Parent::setFirstResponder();
  82. GuiCanvas *root = getRoot();
  83. if (root != NULL)
  84. {
  85. root->enableKeyboardTranslation();
  86. // If the native OS accelerator keys are not disabled
  87. // then some key events like Delete, ctrl+V, etc may
  88. // not make it down to us.
  89. root->setNativeAcceleratorsEnabled( false );
  90. }
  91. }
  92. void GuiMLTextEditCtrl::onLoseFirstResponder()
  93. {
  94. GuiCanvas *root = getRoot();
  95. if (root != NULL)
  96. {
  97. root->setNativeAcceleratorsEnabled( true );
  98. root->disableKeyboardTranslation();
  99. }
  100. // Redraw the control:
  101. setUpdate();
  102. }
  103. //--------------------------------------------------------------------------
  104. bool GuiMLTextEditCtrl::onWake()
  105. {
  106. if( !Parent::onWake() )
  107. return false;
  108. getRoot()->enableKeyboardTranslation();
  109. return true;
  110. }
  111. //--------------------------------------------------------------------------
  112. // Key events...
  113. bool GuiMLTextEditCtrl::onKeyDown(const GuiEvent& event)
  114. {
  115. if ( !isActive() )
  116. return false;
  117. setUpdate();
  118. //handle modifiers first...
  119. if (event.modifier & SI_PRIMARY_CTRL)
  120. {
  121. switch(event.keyCode)
  122. {
  123. //copy/cut
  124. case KEY_C:
  125. case KEY_X:
  126. {
  127. //make sure we actually have something selected
  128. if (mSelectionActive)
  129. {
  130. copyToClipboard(mSelectionStart, mSelectionEnd);
  131. //if we're cutting, also delete the selection
  132. if (event.keyCode == KEY_X)
  133. {
  134. mSelectionActive = false;
  135. deleteChars(mSelectionStart, mSelectionEnd);
  136. mCursorPosition = mSelectionStart;
  137. }
  138. else
  139. mCursorPosition = mSelectionEnd + 1;
  140. }
  141. return true;
  142. }
  143. //paste
  144. case KEY_V:
  145. {
  146. const char *clipBuf = Platform::getClipboard();
  147. if (dStrlen(clipBuf) > 0)
  148. {
  149. // Normal ascii keypress. Go ahead and add the chars...
  150. if (mSelectionActive == true)
  151. {
  152. mSelectionActive = false;
  153. deleteChars(mSelectionStart, mSelectionEnd);
  154. mCursorPosition = mSelectionStart;
  155. }
  156. insertChars(clipBuf, dStrlen(clipBuf), mCursorPosition);
  157. }
  158. return true;
  159. }
  160. default:
  161. break;
  162. }
  163. }
  164. else if ( event.modifier & SI_SHIFT )
  165. {
  166. switch ( event.keyCode )
  167. {
  168. case KEY_TAB:
  169. return( Parent::onKeyDown( event ) );
  170. default:
  171. break;
  172. }
  173. }
  174. else if ( event.modifier == 0 )
  175. {
  176. switch (event.keyCode)
  177. {
  178. // Escape:
  179. case KEY_ESCAPE:
  180. if ( mEscapeCommand[0] )
  181. {
  182. Con::evaluate( mEscapeCommand );
  183. return( true );
  184. }
  185. return( Parent::onKeyDown( event ) );
  186. // Deletion
  187. case KEY_BACKSPACE:
  188. case KEY_DELETE:
  189. handleDeleteKeys(event);
  190. return true;
  191. // Cursor movement
  192. case KEY_LEFT:
  193. case KEY_RIGHT:
  194. case KEY_UP:
  195. case KEY_DOWN:
  196. case KEY_HOME:
  197. case KEY_END:
  198. handleMoveKeys(event);
  199. return true;
  200. // Special chars...
  201. case KEY_TAB:
  202. // insert 3 spaces
  203. if (mSelectionActive == true)
  204. {
  205. mSelectionActive = false;
  206. deleteChars(mSelectionStart, mSelectionEnd);
  207. mCursorPosition = mSelectionStart;
  208. }
  209. insertChars( "\t", 1, mCursorPosition );
  210. return true;
  211. case KEY_RETURN:
  212. // insert carriage return
  213. if (mSelectionActive == true)
  214. {
  215. mSelectionActive = false;
  216. deleteChars(mSelectionStart, mSelectionEnd);
  217. mCursorPosition = mSelectionStart;
  218. }
  219. insertChars( "\n", 1, mCursorPosition );
  220. return true;
  221. default:
  222. break;
  223. }
  224. }
  225. if ( (mFont && mFont->isValidChar(event.ascii)) || (!mFont && event.ascii != 0) )
  226. {
  227. // Normal ascii keypress. Go ahead and add the chars...
  228. if (mSelectionActive == true)
  229. {
  230. mSelectionActive = false;
  231. deleteChars(mSelectionStart, mSelectionEnd);
  232. mCursorPosition = mSelectionStart;
  233. }
  234. UTF8 *outString = NULL;
  235. U32 outStringLen = 0;
  236. #ifdef TORQUE_UNICODE
  237. UTF16 inData[2] = { event.ascii, 0 };
  238. StringBuffer inBuff(inData);
  239. FrameTemp<UTF8> outBuff(4);
  240. inBuff.getCopy8(outBuff, 4);
  241. outString = outBuff;
  242. outStringLen = dStrlen(outBuff);
  243. #else
  244. char ascii = char(event.ascii);
  245. outString = &ascii;
  246. outStringLen = 1;
  247. #endif
  248. insertChars(outString, outStringLen, mCursorPosition);
  249. mVertMoveAnchorValid = false;
  250. return true;
  251. }
  252. // Otherwise, let the parent have the event...
  253. return Parent::onKeyDown(event);
  254. }
  255. //--------------------------------------
  256. void GuiMLTextEditCtrl::handleDeleteKeys(const GuiEvent& event)
  257. {
  258. if ( isSelectionActive() )
  259. {
  260. mSelectionActive = false;
  261. deleteChars(mSelectionStart, mSelectionEnd+1);
  262. mCursorPosition = mSelectionStart;
  263. }
  264. else
  265. {
  266. switch ( event.keyCode )
  267. {
  268. case KEY_BACKSPACE:
  269. if (mCursorPosition != 0)
  270. {
  271. // delete one character left
  272. deleteChars(mCursorPosition-1, mCursorPosition);
  273. setUpdate();
  274. }
  275. break;
  276. case KEY_DELETE:
  277. if (mCursorPosition != mTextBuffer.length())
  278. {
  279. // delete one character right
  280. deleteChars(mCursorPosition, mCursorPosition+1);
  281. setUpdate();
  282. }
  283. break;
  284. default:
  285. AssertFatal(false, "Unknown key code received!");
  286. }
  287. }
  288. }
  289. //--------------------------------------
  290. void GuiMLTextEditCtrl::handleMoveKeys(const GuiEvent& event)
  291. {
  292. if ( event.modifier & SI_SHIFT )
  293. return;
  294. mSelectionActive = false;
  295. switch ( event.keyCode )
  296. {
  297. case KEY_LEFT:
  298. mVertMoveAnchorValid = false;
  299. // move one left
  300. if ( mCursorPosition != 0 )
  301. {
  302. mCursorPosition--;
  303. setUpdate();
  304. }
  305. break;
  306. case KEY_RIGHT:
  307. mVertMoveAnchorValid = false;
  308. // move one right
  309. if ( mCursorPosition != mTextBuffer.length() )
  310. {
  311. mCursorPosition++;
  312. setUpdate();
  313. }
  314. break;
  315. case KEY_UP:
  316. case KEY_DOWN:
  317. {
  318. Line* walk;
  319. for ( walk = mLineList; walk->next; walk = walk->next )
  320. {
  321. if ( mCursorPosition <= ( walk->textStart + walk->len ) )
  322. break;
  323. }
  324. if ( !walk )
  325. return;
  326. if ( event.keyCode == KEY_UP )
  327. {
  328. if ( walk == mLineList )
  329. return;
  330. }
  331. else if ( walk->next == NULL )
  332. return;
  333. Point2I newPos;
  334. newPos.set( 0, walk->y );
  335. // Find the x-position:
  336. if ( !mVertMoveAnchorValid )
  337. {
  338. Point2I cursorTopP, cursorBottomP;
  339. ColorI color;
  340. getCursorPositionAndColor(cursorTopP, cursorBottomP, color);
  341. mVertMoveAnchor = cursorTopP.x;
  342. mVertMoveAnchorValid = true;
  343. }
  344. newPos.x = mVertMoveAnchor;
  345. // Set the new y-position:
  346. if (event.keyCode == KEY_UP)
  347. newPos.y--;
  348. else
  349. newPos.y += (walk->height + 1);
  350. if (setCursorPosition(getTextPosition(newPos)))
  351. mVertMoveAnchorValid = false;
  352. break;
  353. }
  354. case KEY_HOME:
  355. case KEY_END:
  356. {
  357. mVertMoveAnchorValid = false;
  358. Line* walk;
  359. for (walk = mLineList; walk->next; walk = walk->next)
  360. {
  361. if (mCursorPosition <= (walk->textStart + walk->len))
  362. break;
  363. }
  364. if (walk)
  365. {
  366. if (event.keyCode == KEY_HOME)
  367. {
  368. //place the cursor at the beginning of the first atom if there is one
  369. if (walk->atomList)
  370. mCursorPosition = walk->atomList->textStart;
  371. else
  372. mCursorPosition = walk->textStart;
  373. }
  374. else
  375. {
  376. mCursorPosition = walk->textStart;
  377. mCursorPosition += walk->len;
  378. }
  379. setUpdate();
  380. }
  381. break;
  382. }
  383. default:
  384. AssertFatal(false, "Unknown move key code was received!");
  385. }
  386. ensureCursorOnScreen();
  387. }
  388. //--------------------------------------------------------------------------
  389. void GuiMLTextEditCtrl::onRender(Point2I offset, const RectI& updateRect)
  390. {
  391. Parent::onRender(offset, updateRect);
  392. // We are the first responder, draw our cursor in the appropriate position...
  393. if (isFirstResponder())
  394. {
  395. Point2I top, bottom;
  396. ColorI color;
  397. getCursorPositionAndColor(top, bottom, color);
  398. GFX->getDrawUtil()->drawLine(top + offset, bottom + offset, mProfile->mCursorColor);
  399. }
  400. }