2
0

guiMLTextEditCtrl.cpp 12 KB

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