LineEdit.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "Input.h"
  25. #include "LineEdit.h"
  26. #include "Text.h"
  27. #include "UI.h"
  28. #include "UIEvents.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. OBJECTTYPESTATIC(LineEdit);
  33. LineEdit::LineEdit(Context* context) :
  34. BorderImage(context),
  35. lastFont_(0),
  36. lastFontSize_(0),
  37. cursorPosition_(0),
  38. dragBeginCursor_(M_MAX_UNSIGNED),
  39. cursorBlinkRate_(1.0f),
  40. cursorBlinkTimer_(0.0f),
  41. maxLength_(0),
  42. echoCharacter_(0),
  43. cursorMovable_(true),
  44. textSelectable_(true),
  45. textCopyable_(true),
  46. doubleClickInterval_(500)
  47. {
  48. clipChildren_ = true;
  49. enabled_ = true;
  50. focusMode_ = FM_FOCUSABLE_DEFOCUSABLE;
  51. text_ = CreateChild<Text>("LE_Text");
  52. text_->SetInternal(true);
  53. cursor_ = CreateChild<BorderImage>("LE_Cursor");
  54. cursor_->SetInternal(true);
  55. cursor_->SetPriority(1); // Show over text
  56. SubscribeToEvent(this, E_FOCUSED, HANDLER(LineEdit, HandleFocused));
  57. SubscribeToEvent(this, E_DEFOCUSED, HANDLER(LineEdit, HandleDefocused));
  58. SubscribeToEvent(this, E_LAYOUTUPDATED, HANDLER(LineEdit, HandleFocused));
  59. }
  60. LineEdit::~LineEdit()
  61. {
  62. }
  63. void LineEdit::RegisterObject(Context* context)
  64. {
  65. context->RegisterFactory<LineEdit>();
  66. COPY_BASE_ATTRIBUTES(LineEdit, BorderImage);
  67. UPDATE_ATTRIBUTE_DEFAULT_VALUE(LineEdit, "Clip Children", true);
  68. UPDATE_ATTRIBUTE_DEFAULT_VALUE(LineEdit, "Is Enabled", true);
  69. UPDATE_ATTRIBUTE_DEFAULT_VALUE(LineEdit, "Focus Mode", FM_FOCUSABLE_DEFOCUSABLE);
  70. ACCESSOR_ATTRIBUTE(LineEdit, VAR_INT, "Max Length", GetMaxLength, SetMaxLength, unsigned, 0, AM_FILE);
  71. ACCESSOR_ATTRIBUTE(LineEdit, VAR_BOOL, "Is Cursor Movable", IsCursorMovable, SetCursorMovable, bool, true, AM_FILE);
  72. ACCESSOR_ATTRIBUTE(LineEdit, VAR_BOOL, "Is Text Selectable", IsTextSelectable, SetTextSelectable, bool, true, AM_FILE);
  73. ACCESSOR_ATTRIBUTE(LineEdit, VAR_BOOL, "Is Text Copyable", IsTextCopyable, SetTextCopyable, bool, true, AM_FILE);
  74. ACCESSOR_ATTRIBUTE(LineEdit, VAR_FLOAT, "Cursor Blink Rate", GetCursorBlinkRate, SetCursorBlinkRate, float, 1.0f, AM_FILE);
  75. ATTRIBUTE(LineEdit, VAR_INT, "Echo Character", echoCharacter_, 0, AM_FILE);
  76. ACCESSOR_ATTRIBUTE(LineEdit, VAR_FLOAT, "Double Click Interval", GetDoubleClickInterval, SetDoubleClickInterval, float, 0.5f, AM_FILE);
  77. }
  78. void LineEdit::ApplyAttributes()
  79. {
  80. BorderImage::ApplyAttributes();
  81. // Set the text's position to match clipping and indent width, so that text left edge is not left partially hidden
  82. text_->SetPosition(GetIndentWidth() + clipBorder_.left_, clipBorder_.top_);
  83. // Sync the text line
  84. line_ = text_->GetText();
  85. }
  86. void LineEdit::Update(float timeStep)
  87. {
  88. if (cursorBlinkRate_ > 0.0f)
  89. cursorBlinkTimer_ = fmodf(cursorBlinkTimer_ + cursorBlinkRate_ * timeStep, 1.0f);
  90. // Update cursor position if font has changed
  91. if (text_->GetFont() != lastFont_ || text_->GetFontSize() != lastFontSize_)
  92. {
  93. lastFont_ = text_->GetFont();
  94. lastFontSize_ = text_->GetFontSize();
  95. UpdateCursor();
  96. }
  97. bool cursorVisible = HasFocus() ? cursorBlinkTimer_ < 0.5f : false;
  98. cursor_->SetVisible(cursorVisible);
  99. }
  100. void LineEdit::OnClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  101. {
  102. if (buttons & MOUSEB_LEFT && cursorMovable_)
  103. {
  104. if (doubleClickTimer_.GetMSec(true) < doubleClickInterval_)
  105. text_->SetSelection(0);
  106. else
  107. {
  108. unsigned pos = GetCharIndex(position);
  109. if (pos != M_MAX_UNSIGNED)
  110. {
  111. SetCursorPosition(pos);
  112. text_->ClearSelection();
  113. }
  114. }
  115. }
  116. }
  117. void LineEdit::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  118. {
  119. dragBeginCursor_ = GetCharIndex(position);
  120. }
  121. void LineEdit::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  122. {
  123. if (cursorMovable_ && textSelectable_)
  124. {
  125. unsigned start = dragBeginCursor_;
  126. unsigned current = GetCharIndex(position);
  127. if (start != M_MAX_UNSIGNED && current != M_MAX_UNSIGNED)
  128. {
  129. if (start < current)
  130. text_->SetSelection(start, current - start);
  131. else
  132. text_->SetSelection(current, start - current);
  133. SetCursorPosition(current);
  134. }
  135. }
  136. }
  137. bool LineEdit::OnDragDropTest(UIElement* source)
  138. {
  139. if (source)
  140. {
  141. ShortStringHash sourceType = source->GetType();
  142. return sourceType == LineEdit::GetTypeStatic() || sourceType == Text::GetTypeStatic();
  143. }
  144. return false;
  145. }
  146. bool LineEdit::OnDragDropFinish(UIElement* source)
  147. {
  148. if (source)
  149. {
  150. ShortStringHash sourceType = source->GetType();
  151. if (sourceType == LineEdit::GetTypeStatic())
  152. {
  153. LineEdit* sourceLineEdit = static_cast<LineEdit*>(source);
  154. SetText(sourceLineEdit->GetText());
  155. return true;
  156. }
  157. else if (sourceType == Text::GetTypeStatic())
  158. {
  159. Text* sourceText = static_cast<Text*>(source);
  160. SetText(sourceText->GetText());
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. void LineEdit::OnKey(int key, int buttons, int qualifiers)
  167. {
  168. bool changed = false;
  169. bool cursorMoved = false;
  170. switch (key)
  171. {
  172. case 'X':
  173. case 'C':
  174. if (textCopyable_ && qualifiers & QUAL_CTRL)
  175. {
  176. unsigned start = text_->GetSelectionStart();
  177. unsigned length = text_->GetSelectionLength();
  178. if (text_->GetSelectionLength())
  179. GetSubsystem<UI>()->SetClipBoardText(line_.SubstringUTF8(start, length));
  180. if (key == 'X')
  181. {
  182. if (start + length < line_.LengthUTF8())
  183. line_ = line_.SubstringUTF8(0, start) + line_.SubstringUTF8(start + length);
  184. else
  185. line_ = line_.SubstringUTF8(0, start);
  186. text_->ClearSelection();
  187. cursorPosition_ = start;
  188. changed = true;
  189. }
  190. }
  191. break;
  192. case 'V':
  193. if (textCopyable_ && qualifiers & QUAL_CTRL)
  194. {
  195. const String& clipBoard = GetSubsystem<UI>()->GetClipBoardText();
  196. if (!clipBoard.Empty())
  197. {
  198. // Remove selected text first
  199. if(text_->GetSelectionLength() > 0)
  200. {
  201. unsigned start = text_->GetSelectionStart();
  202. unsigned length = text_->GetSelectionLength();
  203. if (start + length < line_.LengthUTF8())
  204. line_ = line_.SubstringUTF8(0, start) + line_.SubstringUTF8(start + length);
  205. else
  206. line_ = line_.SubstringUTF8(0, start);
  207. text_->ClearSelection();
  208. cursorPosition_ = start;
  209. }
  210. if (cursorPosition_ < line_.LengthUTF8())
  211. line_ = line_.SubstringUTF8(0, cursorPosition_) + clipBoard + line_.SubstringUTF8(cursorPosition_);
  212. else
  213. line_ += clipBoard;
  214. cursorPosition_ += clipBoard.LengthUTF8();
  215. changed = true;
  216. }
  217. }
  218. break;
  219. case KEY_HOME:
  220. qualifiers |= QUAL_CTRL;
  221. // Fallthru
  222. case KEY_LEFT:
  223. if (!(qualifiers & QUAL_SHIFT))
  224. text_->ClearSelection();
  225. if (cursorMovable_ && cursorPosition_ > 0)
  226. {
  227. if (textSelectable_ && qualifiers & QUAL_SHIFT && !text_->GetSelectionLength())
  228. dragBeginCursor_ = cursorPosition_;
  229. if (qualifiers & QUAL_CTRL)
  230. cursorPosition_ = 0;
  231. else
  232. --cursorPosition_;
  233. cursorMoved = true;
  234. if (textSelectable_ && qualifiers & QUAL_SHIFT)
  235. {
  236. unsigned start = dragBeginCursor_;
  237. unsigned current = cursorPosition_;
  238. if (start < current)
  239. text_->SetSelection(start, current - start);
  240. else
  241. text_->SetSelection(current, start - current);
  242. }
  243. }
  244. break;
  245. case KEY_END:
  246. qualifiers |= QUAL_CTRL;
  247. // Fallthru
  248. case KEY_RIGHT:
  249. if (!(qualifiers & QUAL_SHIFT))
  250. text_->ClearSelection();
  251. if (cursorMovable_ && cursorPosition_ < line_.LengthUTF8())
  252. {
  253. if (textSelectable_ && qualifiers & QUAL_SHIFT && !text_->GetSelectionLength())
  254. dragBeginCursor_ = cursorPosition_;
  255. if (qualifiers & QUAL_CTRL)
  256. cursorPosition_ = line_.LengthUTF8();
  257. else
  258. ++cursorPosition_;
  259. cursorMoved = true;
  260. if (textSelectable_ && qualifiers & QUAL_SHIFT)
  261. {
  262. unsigned start = dragBeginCursor_;
  263. unsigned current = cursorPosition_;
  264. if (start < current)
  265. text_->SetSelection(start, current - start);
  266. else
  267. text_->SetSelection(current, start - current);
  268. }
  269. }
  270. break;
  271. case KEY_DELETE:
  272. if (!text_->GetSelectionLength())
  273. {
  274. if (cursorPosition_ < line_.LengthUTF8())
  275. {
  276. line_ = line_.SubstringUTF8(0, cursorPosition_) + line_.SubstringUTF8(cursorPosition_ + 1);
  277. changed = true;
  278. }
  279. }
  280. else
  281. {
  282. // If a selection exists, erase it
  283. unsigned start = text_->GetSelectionStart();
  284. unsigned length = text_->GetSelectionLength();
  285. if (start + length < line_.LengthUTF8())
  286. line_ = line_.SubstringUTF8(0, start) + line_.SubstringUTF8(start + length);
  287. else
  288. line_ = line_.SubstringUTF8(0, start);
  289. text_->ClearSelection();
  290. cursorPosition_ = start;
  291. changed = true;
  292. }
  293. break;
  294. case KEY_UP:
  295. case KEY_DOWN:
  296. case KEY_PAGEUP:
  297. case KEY_PAGEDOWN:
  298. {
  299. using namespace UnhandledKey;
  300. VariantMap eventData;
  301. eventData[P_ELEMENT] = (void*)this;
  302. eventData[P_KEY] = key;
  303. eventData[P_BUTTONS] = buttons;
  304. eventData[P_QUALIFIERS] = qualifiers;
  305. SendEvent(E_UNHANDLEDKEY, eventData);
  306. }
  307. return;
  308. case KEY_BACKSPACE:
  309. if (!text_->GetSelectionLength())
  310. {
  311. if (line_.LengthUTF8() && cursorPosition_)
  312. {
  313. if (cursorPosition_ < line_.LengthUTF8())
  314. line_ = line_.SubstringUTF8(0, cursorPosition_ - 1) + line_.SubstringUTF8(cursorPosition_);
  315. else
  316. line_ = line_.SubstringUTF8(0, cursorPosition_ - 1);
  317. --cursorPosition_;
  318. changed = true;
  319. }
  320. }
  321. else
  322. {
  323. // If a selection exists, erase it
  324. unsigned start = text_->GetSelectionStart();
  325. unsigned length = text_->GetSelectionLength();
  326. if (start + length < line_.LengthUTF8())
  327. line_ = line_.SubstringUTF8(0, start) + line_.SubstringUTF8(start + length);
  328. else
  329. line_ = line_.SubstringUTF8(0, start);
  330. text_->ClearSelection();
  331. cursorPosition_ = start;
  332. changed = true;
  333. }
  334. break;
  335. case KEY_RETURN:
  336. case KEY_RETURN2:
  337. case KEY_KP_ENTER:
  338. {
  339. using namespace TextFinished;
  340. VariantMap eventData;
  341. eventData[P_ELEMENT] = (void*)this;
  342. eventData[P_TEXT] = line_;
  343. SendEvent(E_TEXTFINISHED, eventData);
  344. return;
  345. }
  346. break;
  347. }
  348. if (changed)
  349. {
  350. UpdateText();
  351. UpdateCursor();
  352. }
  353. else if (cursorMoved)
  354. UpdateCursor();
  355. }
  356. void LineEdit::OnChar(unsigned c, int buttons, int qualifiers)
  357. {
  358. bool changed = false;
  359. // If only CTRL is held down, do not edit
  360. if ((qualifiers & (QUAL_CTRL | QUAL_ALT)) == QUAL_CTRL)
  361. return;
  362. if (c >= 0x20 && (!maxLength_ || line_.LengthUTF8() < maxLength_))
  363. {
  364. String charStr;
  365. charStr.AppendUTF8(c);
  366. if (!text_->GetSelectionLength())
  367. {
  368. if (cursorPosition_ == line_.LengthUTF8())
  369. line_ += charStr;
  370. else
  371. line_ = line_.SubstringUTF8(0, cursorPosition_) + charStr + line_.SubstringUTF8(cursorPosition_);
  372. ++cursorPosition_;
  373. }
  374. else
  375. {
  376. // If a selection exists, erase it first
  377. unsigned start = text_->GetSelectionStart();
  378. unsigned length = text_->GetSelectionLength();
  379. if (start + length < line_.LengthUTF8())
  380. line_ = line_.SubstringUTF8(0, start) + charStr + line_.SubstringUTF8(start + length);
  381. else
  382. line_ = line_.SubstringUTF8(0, start) + charStr;
  383. cursorPosition_ = start + 1;
  384. }
  385. changed = true;
  386. }
  387. if (changed)
  388. {
  389. text_->ClearSelection();
  390. UpdateText();
  391. UpdateCursor();
  392. }
  393. }
  394. void LineEdit::SetText(const String& text)
  395. {
  396. if (text != line_)
  397. {
  398. line_ = text;
  399. cursorPosition_ = line_.LengthUTF8();
  400. UpdateText();
  401. UpdateCursor();
  402. }
  403. }
  404. void LineEdit::SetCursorPosition(unsigned position)
  405. {
  406. if (position > line_.LengthUTF8() || !cursorMovable_)
  407. position = line_.LengthUTF8();
  408. if (position != cursorPosition_)
  409. {
  410. cursorPosition_ = position;
  411. UpdateCursor();
  412. }
  413. }
  414. void LineEdit::SetCursorBlinkRate(float rate)
  415. {
  416. cursorBlinkRate_ = Max(rate, 0.0f);
  417. if (cursorBlinkRate_ == 0.0f)
  418. cursorBlinkTimer_ = 0.0f; // Cursor does not blink, i.e. always visible
  419. }
  420. void LineEdit::SetMaxLength(unsigned length)
  421. {
  422. maxLength_ = length;
  423. }
  424. void LineEdit::SetEchoCharacter(unsigned c)
  425. {
  426. echoCharacter_ = c;
  427. UpdateText();
  428. }
  429. void LineEdit::SetCursorMovable(bool enable)
  430. {
  431. cursorMovable_ = enable;
  432. }
  433. void LineEdit::SetTextSelectable(bool enable)
  434. {
  435. textSelectable_ = enable;
  436. }
  437. void LineEdit::SetTextCopyable(bool enable)
  438. {
  439. textCopyable_ = enable;
  440. }
  441. void LineEdit::SetDoubleClickInterval(float interval)
  442. {
  443. doubleClickInterval_ = Max((int)(interval * 1000.0f), 0);
  444. }
  445. float LineEdit::GetDoubleClickInterval() const
  446. {
  447. return (float)doubleClickInterval_ / 1000.0f;
  448. }
  449. bool LineEdit::FilterImplicitAttributes(XMLElement& dest) const
  450. {
  451. if (!BorderImage::FilterImplicitAttributes(dest))
  452. return false;
  453. XMLElement childElem = dest.GetChild("element");
  454. if (!childElem)
  455. return false;
  456. if (!RemoveChildXML(childElem, "Name", "LE_Text"))
  457. return false;
  458. if (!RemoveChildXML(childElem, "Position"))
  459. return false;
  460. childElem = childElem.GetNext("element");
  461. if (!childElem)
  462. return false;
  463. if (!RemoveChildXML(childElem, "Name", "LE_Cursor"))
  464. return false;
  465. if (!RemoveChildXML(childElem, "Priority", "1"))
  466. return false;
  467. if (!RemoveChildXML(childElem, "Position"))
  468. return false;
  469. if (!RemoveChildXML(childElem, "Is Visible"))
  470. return false;
  471. return true;
  472. }
  473. void LineEdit::UpdateText()
  474. {
  475. unsigned utf8Length = line_.LengthUTF8();
  476. if (!echoCharacter_)
  477. text_->SetText(line_);
  478. else
  479. {
  480. String echoText;
  481. for (unsigned i = 0; i < utf8Length; ++i)
  482. echoText.AppendUTF8(echoCharacter_);
  483. text_->SetText(echoText);
  484. }
  485. if (cursorPosition_ > utf8Length)
  486. {
  487. cursorPosition_ = utf8Length;
  488. UpdateCursor();
  489. }
  490. using namespace TextChanged;
  491. VariantMap eventData;
  492. eventData[P_ELEMENT] = (void*)this;
  493. eventData[P_TEXT] = line_;
  494. SendEvent(E_TEXTCHANGED, eventData);
  495. }
  496. void LineEdit::UpdateCursor()
  497. {
  498. int x = 0;
  499. const PODVector<IntVector2>& charPositions = text_->GetCharPositions();
  500. if (charPositions.Size())
  501. x = cursorPosition_ < charPositions.Size() ? charPositions[cursorPosition_].x_ : charPositions.Back().x_;
  502. text_->SetPosition(GetIndentWidth() + clipBorder_.left_, clipBorder_.top_);
  503. cursor_->SetPosition(text_->GetPosition() + IntVector2(x, 0));
  504. cursor_->SetSize(cursor_->GetWidth(), text_->GetRowHeight());
  505. // Scroll if necessary
  506. int sx = -GetChildOffset().x_;
  507. int left = clipBorder_.left_;
  508. int right = GetWidth() - clipBorder_.left_ - clipBorder_.right_ - cursor_->GetWidth();
  509. if (x - sx > right)
  510. sx = x - right;
  511. if (x - sx < left)
  512. sx = x - left;
  513. if (sx < 0)
  514. sx = 0;
  515. SetChildOffset(IntVector2(-sx, 0));
  516. // Restart blinking
  517. cursorBlinkTimer_ = 0.0f;
  518. }
  519. unsigned LineEdit::GetCharIndex(const IntVector2& position)
  520. {
  521. IntVector2 screenPosition = ElementToScreen(position);
  522. IntVector2 textPosition = text_->ScreenToElement(screenPosition);
  523. const PODVector<IntVector2>& charPositions = text_->GetCharPositions();
  524. if (textPosition.x_ < 0)
  525. return 0;
  526. for (unsigned i = charPositions.Size() - 1; i < charPositions.Size(); --i)
  527. {
  528. if (textPosition.x_ >= charPositions[i].x_)
  529. return i;
  530. }
  531. return M_MAX_UNSIGNED;
  532. }
  533. void LineEdit::HandleFocused(StringHash eventType, VariantMap& eventData)
  534. {
  535. UpdateCursor();
  536. }
  537. void LineEdit::HandleDefocused(StringHash eventType, VariantMap& eventData)
  538. {
  539. text_->ClearSelection();
  540. }
  541. }