guiMLTextEditCtrl.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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/guiMLTextEditCtrl.h"
  23. #include "gui/containers/guiScrollCtrl.h"
  24. #include "graphics/dgl.h"
  25. #include "console/consoleTypes.h"
  26. #include "platform/event.h"
  27. #include "memory/frameAllocator.h"
  28. #include "string/stringBuffer.h"
  29. IMPLEMENT_CONOBJECT(GuiMLTextEditCtrl);
  30. //--------------------------------------------------------------------------
  31. GuiMLTextEditCtrl::GuiMLTextEditCtrl()
  32. {
  33. mEscapeCommand = StringTable->EmptyString;
  34. mIsEditCtrl = true;
  35. mActive = true;
  36. mVertMoveAnchorValid = false;
  37. }
  38. //--------------------------------------------------------------------------
  39. GuiMLTextEditCtrl::~GuiMLTextEditCtrl()
  40. {
  41. }
  42. //--------------------------------------------------------------------------
  43. void GuiMLTextEditCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
  44. {
  45. // We don't want to get any smaller than our parent:
  46. Point2I newExt = newExtent;
  47. GuiControl* parent = getParent();
  48. if ( parent )
  49. newExt.y = getMax( parent->mBounds.extent.y, newExt.y );
  50. Parent::resize( newPosition, newExt );
  51. }
  52. //--------------------------------------------------------------------------
  53. void GuiMLTextEditCtrl::initPersistFields()
  54. {
  55. Parent::initPersistFields();
  56. addField( "escapeCommand", TypeString, Offset( mEscapeCommand, GuiMLTextEditCtrl ) );
  57. }
  58. //--------------------------------------------------------------------------
  59. // Key events...
  60. bool GuiMLTextEditCtrl::onKeyDown(const GuiEvent& event)
  61. {
  62. setUpdate();
  63. //handle modifiers first...
  64. if (event.modifier & SI_CTRL)
  65. {
  66. switch(event.keyCode)
  67. {
  68. //copy/cut
  69. case KEY_C:
  70. case KEY_X:
  71. {
  72. //make sure we actually have something selected
  73. if (mSelectionActive)
  74. {
  75. copyToClipboard(mSelectionStart, mSelectionEnd);
  76. //if we're cutting, also delete the selection
  77. if (event.keyCode == KEY_X)
  78. {
  79. mSelectionActive = false;
  80. deleteChars(mSelectionStart, mSelectionEnd);
  81. mCursorPosition = mSelectionStart;
  82. }
  83. else
  84. mCursorPosition = mSelectionEnd + 1;
  85. }
  86. return true;
  87. }
  88. //paste
  89. case KEY_V:
  90. {
  91. const char *clipBuf = Platform::getClipboard();
  92. if (dStrlen(clipBuf) > 0)
  93. {
  94. // Normal ascii keypress. Go ahead and add the chars...
  95. if (mSelectionActive == true)
  96. {
  97. mSelectionActive = false;
  98. deleteChars(mSelectionStart, mSelectionEnd);
  99. mCursorPosition = mSelectionStart;
  100. }
  101. insertChars(clipBuf, dStrlen(clipBuf), mCursorPosition);
  102. }
  103. return true;
  104. }
  105. }
  106. }
  107. else if ( event.modifier & SI_SHIFT )
  108. {
  109. switch ( event.keyCode )
  110. {
  111. case KEY_TAB:
  112. return( Parent::onKeyDown( event ) );
  113. }
  114. }
  115. else if ( event.modifier == 0 )
  116. {
  117. switch (event.keyCode)
  118. {
  119. // Escape:
  120. case KEY_ESCAPE:
  121. if ( mEscapeCommand[0] )
  122. {
  123. Con::evaluate( mEscapeCommand );
  124. return( true );
  125. }
  126. return( Parent::onKeyDown( event ) );
  127. // Deletion
  128. case KEY_BACKSPACE:
  129. case KEY_DELETE:
  130. handleDeleteKeys(event);
  131. return true;
  132. // Cursor movement
  133. case KEY_LEFT:
  134. case KEY_RIGHT:
  135. case KEY_UP:
  136. case KEY_DOWN:
  137. case KEY_HOME:
  138. case KEY_END:
  139. handleMoveKeys(event);
  140. return true;
  141. // Special chars...
  142. case KEY_TAB:
  143. // insert 3 spaces
  144. if (mSelectionActive == true)
  145. {
  146. mSelectionActive = false;
  147. deleteChars(mSelectionStart, mSelectionEnd);
  148. mCursorPosition = mSelectionStart;
  149. }
  150. insertChars( "\t", 1, mCursorPosition );
  151. return true;
  152. case KEY_RETURN:
  153. // insert carriage return
  154. if (mSelectionActive == true)
  155. {
  156. mSelectionActive = false;
  157. deleteChars(mSelectionStart, mSelectionEnd);
  158. mCursorPosition = mSelectionStart;
  159. }
  160. insertChars( "\n", 1, mCursorPosition );
  161. return true;
  162. }
  163. }
  164. if (event.ascii != 0)
  165. {
  166. // Normal ascii keypress. Go ahead and add the chars...
  167. if (mSelectionActive == true)
  168. {
  169. mSelectionActive = false;
  170. deleteChars(mSelectionStart, mSelectionEnd);
  171. mCursorPosition = mSelectionStart;
  172. }
  173. UTF8 *outString = NULL;
  174. U32 outStringLen = 0;
  175. #ifdef TORQUE_UNICODE
  176. UTF16 inData[2] = { event.ascii, 0 };
  177. StringBuffer inBuff(inData);
  178. FrameTemp<UTF8> outBuff(4);
  179. //[neo, 5/7/2007]: StringBuffer::get() is gone baby, gone...
  180. //inBuff.get(outBuff, 4);
  181. inBuff.getCopy8(outBuff,4);
  182. outString = outBuff;
  183. outStringLen = dStrlen(outBuff);
  184. #else
  185. char ascii = char(event.ascii);
  186. outString = &ascii;
  187. outStringLen = 1;
  188. #endif
  189. insertChars(outString, outStringLen, mCursorPosition);
  190. mVertMoveAnchorValid = false;
  191. return true;
  192. }
  193. // Otherwise, let the parent have the event...
  194. return Parent::onKeyDown(event);
  195. }
  196. //--------------------------------------
  197. void GuiMLTextEditCtrl::handleDeleteKeys(const GuiEvent& event)
  198. {
  199. if ( isSelectionActive() )
  200. {
  201. mSelectionActive = false;
  202. deleteChars(mSelectionStart, mSelectionEnd+1);
  203. mCursorPosition = mSelectionStart;
  204. }
  205. else
  206. {
  207. switch ( event.keyCode )
  208. {
  209. case KEY_BACKSPACE:
  210. if (mCursorPosition != 0)
  211. {
  212. // delete one character left
  213. deleteChars(mCursorPosition-1, mCursorPosition);
  214. setUpdate();
  215. }
  216. break;
  217. case KEY_DELETE:
  218. if (mCursorPosition != mTextBuffer.length())
  219. {
  220. // delete one character right
  221. deleteChars(mCursorPosition, mCursorPosition+1);
  222. setUpdate();
  223. }
  224. break;
  225. default:
  226. AssertFatal(false, "Unknown key code received!");
  227. }
  228. }
  229. }
  230. //--------------------------------------
  231. void GuiMLTextEditCtrl::handleMoveKeys(const GuiEvent& event)
  232. {
  233. if ( event.modifier & SI_SHIFT )
  234. return;
  235. mSelectionActive = false;
  236. switch ( event.keyCode )
  237. {
  238. case KEY_LEFT:
  239. mVertMoveAnchorValid = false;
  240. // move one left
  241. if ( mCursorPosition != 0 )
  242. {
  243. mCursorPosition--;
  244. setUpdate();
  245. }
  246. break;
  247. case KEY_RIGHT:
  248. mVertMoveAnchorValid = false;
  249. // move one right
  250. if ( mCursorPosition != mTextBuffer.length() )
  251. {
  252. mCursorPosition++;
  253. setUpdate();
  254. }
  255. break;
  256. case KEY_UP:
  257. case KEY_DOWN:
  258. {
  259. Line* walk;
  260. for ( walk = mLineList; walk->next; walk = walk->next )
  261. {
  262. if ( mCursorPosition <= ( walk->textStart + walk->len ) )
  263. break;
  264. }
  265. if ( !walk )
  266. return;
  267. if ( event.keyCode == KEY_UP )
  268. {
  269. if ( walk == mLineList )
  270. return;
  271. }
  272. else if ( walk->next == NULL )
  273. return;
  274. Point2I newPos;
  275. newPos.set( 0, walk->y );
  276. // Find the x-position:
  277. if ( !mVertMoveAnchorValid )
  278. {
  279. Point2I cursorTopP, cursorBottomP;
  280. ColorI color;
  281. getCursorPositionAndColor(cursorTopP, cursorBottomP, color);
  282. mVertMoveAnchor = cursorTopP.x;
  283. mVertMoveAnchorValid = true;
  284. }
  285. newPos.x = mVertMoveAnchor;
  286. // Set the new y-position:
  287. if (event.keyCode == KEY_UP)
  288. newPos.y--;
  289. else
  290. newPos.y += (walk->height + 1);
  291. if (setCursorPosition(getTextPosition(newPos)))
  292. mVertMoveAnchorValid = false;
  293. break;
  294. }
  295. case KEY_HOME:
  296. case KEY_END:
  297. {
  298. mVertMoveAnchorValid = false;
  299. Line* walk;
  300. for (walk = mLineList; walk->next; walk = walk->next)
  301. {
  302. if (mCursorPosition <= (walk->textStart + walk->len))
  303. break;
  304. }
  305. if (walk)
  306. {
  307. if (event.keyCode == KEY_HOME)
  308. {
  309. //place the cursor at the beginning of the first atom if there is one
  310. if (walk->atomList)
  311. mCursorPosition = walk->atomList->textStart;
  312. else
  313. mCursorPosition = walk->textStart;
  314. }
  315. else
  316. {
  317. mCursorPosition = walk->textStart;
  318. mCursorPosition += walk->len;
  319. }
  320. setUpdate();
  321. }
  322. break;
  323. }
  324. default:
  325. AssertFatal(false, "Unknown move key code was received!");
  326. }
  327. ensureCursorOnScreen();
  328. }
  329. //--------------------------------------------------------------------------
  330. void GuiMLTextEditCtrl::onRender(Point2I offset, const RectI& updateRect)
  331. {
  332. Parent::onRender(offset, updateRect);
  333. // We are the first responder, draw our cursor in the appropriate position...
  334. if (isFirstResponder())
  335. {
  336. Point2I top, bottom;
  337. ColorI color;
  338. getCursorPositionAndColor(top, bottom, color);
  339. dglDrawLine(top + offset, bottom + offset, mProfile->mCursorColor);
  340. }
  341. }