LineEdit.cpp 19 KB

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