LineEdit.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  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 deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // 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 FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "Input.h"
  26. #include "LineEdit.h"
  27. #include "Text.h"
  28. #include "UI.h"
  29. #include "UIEvents.h"
  30. #include "DebugNew.h"
  31. OBJECTTYPESTATIC(LineEdit);
  32. LineEdit::LineEdit(Context* context) :
  33. BorderImage(context),
  34. lastFont_(0),
  35. lastFontSize_(0),
  36. cursorPosition_(0),
  37. dragStartCursor_(M_MAX_UNSIGNED),
  38. cursorBlinkRate_(1.0f),
  39. cursorBlinkTimer_(0.0f),
  40. maxLength_(0),
  41. echoCharacter_(0),
  42. cursorMovable_(true),
  43. textSelectable_(true),
  44. textCopyable_(true)
  45. {
  46. clipChildren_ = true;
  47. active_ = true;
  48. focusMode_ = FM_FOCUSABLE_DEFOCUSABLE;
  49. text_ = new Text(context_);
  50. cursor_ = new BorderImage(context_);
  51. AddChild(text_);
  52. AddChild(cursor_);
  53. // Show cursor on top of text
  54. cursor_->SetPriority(1);
  55. }
  56. LineEdit::~LineEdit()
  57. {
  58. }
  59. void LineEdit::RegisterObject(Context* context)
  60. {
  61. context->RegisterFactory<LineEdit>();
  62. }
  63. void LineEdit::SetStyle(const XMLElement& element)
  64. {
  65. BorderImage::SetStyle(element);
  66. if (element.HasChildElement("maxlength"))
  67. SetMaxLength(element.GetChildElement("maxlength").GetInt("value"));
  68. if (element.HasChildElement("cursormovable"))
  69. SetCursorMovable(element.GetChildElement("cursormovable").GetBool("enable"));
  70. if (element.HasChildElement("textselectable"))
  71. SetTextSelectable(element.GetChildElement("textselectable").GetBool("enable"));
  72. if (element.HasChildElement("textcopyable"))
  73. SetTextCopyable(element.GetChildElement("textcopyable").GetBool("enable"));
  74. XMLElement textElem = element.GetChildElement("text");
  75. if (textElem)
  76. {
  77. if (textElem.HasAttribute("value"))
  78. SetText(textElem.GetString("value"));
  79. text_->SetStyle(textElem);
  80. }
  81. XMLElement cursorElem = element.GetChildElement("cursor");
  82. if (cursorElem)
  83. cursor_->SetStyle(cursorElem);
  84. if (element.HasChildElement("cursorposition"))
  85. SetCursorPosition(element.GetChildElement("cursorposition").GetInt("value"));
  86. if (element.HasChildElement("cursorblinkrate"))
  87. SetCursorBlinkRate(element.GetChildElement("cursorblinkrate").GetFloat("value"));
  88. if (element.HasChildElement("echocharacter"))
  89. {
  90. String text = element.GetChildElement("echocharacter").GetString("value");
  91. if (text.Length())
  92. SetEchoCharacter(text[0]);
  93. }
  94. }
  95. void LineEdit::Update(float timeStep)
  96. {
  97. if (cursorBlinkRate_ > 0.0f)
  98. cursorBlinkTimer_ = fmodf(cursorBlinkTimer_ + cursorBlinkRate_ * timeStep, 1.0f);
  99. else
  100. cursorBlinkTimer_ = 0.0f;
  101. // Update cursor position if font has changed
  102. if ((text_->GetFont() != lastFont_) || (text_->GetFontSize() != lastFontSize_))
  103. {
  104. lastFont_ = text_->GetFont();
  105. lastFontSize_ = text_->GetFontSize();
  106. UpdateCursor();
  107. }
  108. bool cursorVisible = false;
  109. if (focus_)
  110. cursorVisible = cursorBlinkTimer_ < 0.5f;
  111. cursor_->SetVisible(cursorVisible);
  112. }
  113. void LineEdit::OnClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  114. {
  115. if ((buttons & MOUSEB_LEFT) && (cursorMovable_))
  116. {
  117. unsigned pos = GetCharIndex(position);
  118. if (pos != M_MAX_UNSIGNED)
  119. {
  120. SetCursorPosition(pos);
  121. text_->ClearSelection();
  122. }
  123. }
  124. }
  125. void LineEdit::OnDragStart(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  126. {
  127. dragStartCursor_ = GetCharIndex(position);
  128. }
  129. void LineEdit::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  130. {
  131. if ((cursorMovable_) && (textSelectable_))
  132. {
  133. unsigned start = dragStartCursor_;
  134. unsigned current = GetCharIndex(position);
  135. if ((start != M_MAX_UNSIGNED) && (current != M_MAX_UNSIGNED))
  136. {
  137. if (start < current)
  138. text_->SetSelection(start, current - start);
  139. else
  140. text_->SetSelection(current, start - current);
  141. SetCursorPosition(current);
  142. }
  143. }
  144. }
  145. bool LineEdit::OnDragDropTest(UIElement* source)
  146. {
  147. return (dynamic_cast<LineEdit*>(source) != 0);
  148. }
  149. bool LineEdit::OnDragDropFinish(UIElement* source)
  150. {
  151. LineEdit* sourceLineEdit = dynamic_cast<LineEdit*>(source);
  152. if (sourceLineEdit)
  153. {
  154. SetText(sourceLineEdit->GetText());
  155. return true;
  156. }
  157. else
  158. return false;
  159. }
  160. void LineEdit::OnKey(int key, int buttons, int qualifiers)
  161. {
  162. bool changed = false;
  163. bool cursorMoved = false;
  164. switch (key)
  165. {
  166. case 'X':
  167. case 'C':
  168. if ((textCopyable_) && (qualifiers & QUAL_CTRL))
  169. {
  170. unsigned start = text_->GetSelectionStart();
  171. unsigned length = text_->GetSelectionLength();
  172. if (text_->GetSelectionLength())
  173. GetSubsystem<UI>()->SetClipBoardText(line_.Substring(start, length));
  174. if (key == 'X')
  175. {
  176. if (start + length < line_.Length())
  177. line_ = line_.Substring(0, start) + line_.Substring(start + length);
  178. else
  179. line_ = line_.Substring(0, start);
  180. text_->ClearSelection();
  181. cursorPosition_ = start;
  182. changed = true;
  183. }
  184. }
  185. break;
  186. case 'V':
  187. if ((textCopyable_) && (qualifiers & QUAL_CTRL))
  188. {
  189. const String& clipBoard = GetSubsystem<UI>()->GetClipBoardText();
  190. if (!clipBoard.Empty())
  191. {
  192. if (cursorPosition_ < line_.Length())
  193. line_ = line_.Substring(0, cursorPosition_) + clipBoard + line_.Substring(cursorPosition_);
  194. else
  195. line_ += clipBoard;
  196. cursorPosition_ += clipBoard.Length();
  197. changed = true;
  198. }
  199. }
  200. break;
  201. case KEY_LEFT:
  202. if (!(qualifiers & QUAL_SHIFT))
  203. text_->ClearSelection();
  204. if ((cursorMovable_) && (cursorPosition_ > 0))
  205. {
  206. if ((textSelectable_) && (qualifiers & QUAL_SHIFT) && (!text_->GetSelectionLength()))
  207. dragStartCursor_ = cursorPosition_;
  208. if (qualifiers & QUAL_CTRL)
  209. cursorPosition_ = 0;
  210. else
  211. --cursorPosition_;
  212. cursorMoved = true;
  213. if ((textSelectable_) && (qualifiers & QUAL_SHIFT))
  214. {
  215. unsigned start = dragStartCursor_;
  216. unsigned current = cursorPosition_;
  217. if (start < current)
  218. text_->SetSelection(start, current - start);
  219. else
  220. text_->SetSelection(current, start - current);
  221. }
  222. }
  223. break;
  224. case KEY_RIGHT:
  225. if (!(qualifiers & QUAL_SHIFT))
  226. text_->ClearSelection();
  227. if ((cursorMovable_) && (cursorPosition_ < line_.Length()))
  228. {
  229. if ((textSelectable_) && (qualifiers & QUAL_SHIFT) && (!text_->GetSelectionLength()))
  230. dragStartCursor_ = cursorPosition_;
  231. if (qualifiers & QUAL_CTRL)
  232. cursorPosition_ = line_.Length();
  233. else
  234. ++cursorPosition_;
  235. cursorMoved = true;
  236. if ((textSelectable_) && (qualifiers & QUAL_SHIFT))
  237. {
  238. unsigned start = dragStartCursor_;
  239. unsigned current = cursorPosition_;
  240. if (start < current)
  241. text_->SetSelection(start, current - start);
  242. else
  243. text_->SetSelection(current, start - current);
  244. }
  245. }
  246. break;
  247. case KEY_HOME:
  248. if ((cursorMovable_) && (cursorPosition_ > 0))
  249. {
  250. cursorPosition_ = 0;
  251. cursorMoved = true;
  252. }
  253. break;
  254. case KEY_END:
  255. if ((cursorMovable_) && (cursorPosition_ < line_.Length()))
  256. {
  257. cursorPosition_ = line_.Length();
  258. cursorMoved = true;
  259. }
  260. break;
  261. case KEY_DELETE:
  262. if (!text_->GetSelectionLength())
  263. {
  264. if (cursorPosition_ < line_.Length())
  265. {
  266. line_ = line_.Substring(0, cursorPosition_) + line_.Substring(cursorPosition_ + 1);
  267. changed = true;
  268. }
  269. }
  270. else
  271. {
  272. // If a selection exists, erase it
  273. unsigned start = text_->GetSelectionStart();
  274. unsigned length = text_->GetSelectionLength();
  275. if (start + length < line_.Length())
  276. line_ = line_.Substring(0, start) + line_.Substring(start + length);
  277. else
  278. line_ = line_.Substring(0, start);
  279. text_->ClearSelection();
  280. cursorPosition_ = start;
  281. changed = true;
  282. }
  283. break;
  284. case KEY_UP:
  285. case KEY_DOWN:
  286. case KEY_PAGEUP:
  287. case KEY_PAGEDOWN:
  288. {
  289. using namespace UnhandledKey;
  290. VariantMap eventData;
  291. eventData[P_ELEMENT] = (void*)this;
  292. eventData[P_KEY] = key;
  293. eventData[P_BUTTONS] = buttons;
  294. eventData[P_QUALIFIERS] = qualifiers;
  295. SendEvent(E_UNHANDLEDKEY, eventData);
  296. }
  297. return;
  298. }
  299. if (changed)
  300. {
  301. UpdateText();
  302. UpdateCursor();
  303. }
  304. else if (cursorMoved)
  305. UpdateCursor();
  306. }
  307. void LineEdit::OnChar(unsigned char c, int buttons, int qualifiers)
  308. {
  309. bool changed = false;
  310. // If only CTRL is held down, do not edit
  311. if ((qualifiers & (QUAL_CTRL | QUAL_ALT)) == QUAL_CTRL)
  312. return;
  313. if (c == '\b')
  314. {
  315. if (!text_->GetSelectionLength())
  316. {
  317. if ((line_.Length()) && (cursorPosition_))
  318. {
  319. if (cursorPosition_ < line_.Length())
  320. line_ = line_.Substring(0, cursorPosition_ - 1) + line_.Substring(cursorPosition_);
  321. else
  322. line_ = line_.Substring(0, cursorPosition_ - 1);
  323. --cursorPosition_;
  324. changed = true;
  325. }
  326. }
  327. else
  328. {
  329. // If a selection exists, erase it
  330. unsigned start = text_->GetSelectionStart();
  331. unsigned length = text_->GetSelectionLength();
  332. if (start + length < line_.Length())
  333. line_ = line_.Substring(0, start) + line_.Substring(start + length);
  334. else
  335. line_ = line_.Substring(0, start);
  336. text_->ClearSelection();
  337. cursorPosition_ = start;
  338. changed = true;
  339. }
  340. }
  341. else if (c == '\r')
  342. {
  343. using namespace TextFinished;
  344. VariantMap eventData;
  345. eventData[P_ELEMENT] = (void*)this;
  346. eventData[P_TEXT] = line_;
  347. SendEvent(E_TEXTFINISHED, eventData);
  348. return;
  349. }
  350. else if ((c >= 0x20) && ((!maxLength_) || (line_.Length() < maxLength_)))
  351. {
  352. String charStr;
  353. charStr.Resize(1);
  354. charStr[0] = c;
  355. if (!text_->GetSelectionLength())
  356. {
  357. if (cursorPosition_ == line_.Length())
  358. line_ += charStr;
  359. else
  360. line_ = line_.Substring(0, cursorPosition_) + charStr + line_.Substring(cursorPosition_);
  361. ++cursorPosition_;
  362. }
  363. else
  364. {
  365. // If a selection exists, erase it first
  366. unsigned start = text_->GetSelectionStart();
  367. unsigned length = text_->GetSelectionLength();
  368. if (start + length < line_.Length())
  369. line_ = line_.Substring(0, start) + charStr + line_.Substring(start + length);
  370. else
  371. line_ = line_.Substring(0, start) + charStr;
  372. cursorPosition_ = start + 1;
  373. }
  374. changed = true;
  375. }
  376. if (changed)
  377. {
  378. text_->ClearSelection();
  379. UpdateText();
  380. UpdateCursor();
  381. }
  382. }
  383. void LineEdit::OnFocus()
  384. {
  385. UpdateCursor();
  386. }
  387. void LineEdit::OnDefocus()
  388. {
  389. text_->ClearSelection();
  390. }
  391. void LineEdit::SetText(const String& text)
  392. {
  393. if (text != line_)
  394. {
  395. line_ = text;
  396. cursorPosition_ = line_.Length();
  397. UpdateText();
  398. UpdateCursor();
  399. }
  400. }
  401. void LineEdit::SetCursorPosition(unsigned position)
  402. {
  403. if ((position > line_.Length()) || (!cursorMovable_))
  404. position = line_.Length();
  405. if (position != cursorPosition_)
  406. {
  407. cursorPosition_ = position;
  408. UpdateCursor();
  409. }
  410. }
  411. void LineEdit::SetCursorBlinkRate(float rate)
  412. {
  413. cursorBlinkRate_ = Max(rate, 0.0f);
  414. }
  415. void LineEdit::SetMaxLength(unsigned length)
  416. {
  417. maxLength_ = length;
  418. }
  419. void LineEdit::SetEchoCharacter(char c)
  420. {
  421. echoCharacter_ = c;
  422. UpdateText();
  423. }
  424. void LineEdit::SetCursorMovable(bool enable)
  425. {
  426. cursorMovable_ = enable;
  427. }
  428. void LineEdit::SetTextSelectable(bool enable)
  429. {
  430. textSelectable_ = enable;
  431. }
  432. void LineEdit::SetTextCopyable(bool enable)
  433. {
  434. textCopyable_ = enable;
  435. }
  436. void LineEdit::UpdateText()
  437. {
  438. if (!echoCharacter_)
  439. text_->SetText(line_);
  440. else
  441. {
  442. String echoText;
  443. echoText.Resize(line_.Length());
  444. for (unsigned i = 0; i < line_.Length(); ++i)
  445. echoText[i] = echoCharacter_;
  446. text_->SetText(echoText);
  447. }
  448. if (cursorPosition_ > line_.Length())
  449. {
  450. cursorPosition_ = line_.Length();
  451. UpdateCursor();
  452. }
  453. using namespace TextChanged;
  454. VariantMap eventData;
  455. eventData[P_ELEMENT] = (void*)this;
  456. eventData[P_TEXT] = line_;
  457. SendEvent(E_TEXTCHANGED, eventData);
  458. }
  459. void LineEdit::UpdateCursor()
  460. {
  461. int x = 0;
  462. const PODVector<IntVector2>& charPositions = text_->GetCharPositions();
  463. if (charPositions.Size())
  464. x = cursorPosition_ < charPositions.Size() ? charPositions[cursorPosition_].x_ : charPositions.Back().x_;
  465. cursor_->SetPosition(text_->GetPosition() + IntVector2(x, 0));
  466. cursor_->SetSize(cursor_->GetWidth(), text_->GetRowHeight());
  467. // Scroll if necessary
  468. int sx = -GetChildOffset().x_;
  469. int left = clipBorder_.left_;
  470. int right = GetWidth() - clipBorder_.left_ - clipBorder_.right_ - cursor_->GetWidth();
  471. if (x - sx > right)
  472. sx = x - right;
  473. if (x - sx < left)
  474. sx = x - left;
  475. SetChildOffset(IntVector2(-sx, 0));
  476. // Restart blinking
  477. cursorBlinkTimer_ = 0.0f;
  478. }
  479. unsigned LineEdit::GetCharIndex(const IntVector2& position)
  480. {
  481. IntVector2 screenPosition = ElementToScreen(position);
  482. IntVector2 textPosition = text_->ScreenToElement(screenPosition);
  483. const PODVector<IntVector2>& charPositions = text_->GetCharPositions();
  484. if (textPosition.x_ < 0)
  485. return 0;
  486. for (unsigned i = charPositions.Size() - 1; i < charPositions.Size(); --i)
  487. {
  488. if (textPosition.x_ >= charPositions[i].x_)
  489. return i;
  490. }
  491. return M_MAX_UNSIGNED;
  492. }