LineEdit.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. ACCESSOR_ATTRIBUTE(LineEdit, VAR_INT, "Max Length", GetMaxLength, SetMaxLength, unsigned, 0, AM_FILE);
  68. ACCESSOR_ATTRIBUTE(LineEdit, VAR_BOOL, "Is Cursor Movable", IsCursorMovable, SetCursorMovable, bool, true, AM_FILE);
  69. ACCESSOR_ATTRIBUTE(LineEdit, VAR_BOOL, "Is Text Selectable", IsTextSelectable, SetTextSelectable, bool, true, AM_FILE);
  70. ACCESSOR_ATTRIBUTE(LineEdit, VAR_BOOL, "Is Text Copyable", IsTextCopyable, SetTextCopyable, bool, true, AM_FILE);
  71. ACCESSOR_ATTRIBUTE(LineEdit, VAR_FLOAT, "Cursor Blink Rate", GetCursorBlinkRate, SetCursorBlinkRate, float, 1.0f, AM_FILE);
  72. ATTRIBUTE(LineEdit, VAR_INT, "Echo Character", echoCharacter_, 0, AM_FILE);
  73. ACCESSOR_ATTRIBUTE(LineEdit, VAR_FLOAT, "Double Click Interval", GetDoubleClickInterval, SetDoubleClickInterval, float, 0.5f, AM_FILE);
  74. }
  75. void LineEdit::ApplyAttributes()
  76. {
  77. BorderImage::ApplyAttributes();
  78. // Set the text's position to match clipping and indent width, so that text left edge is not left partially hidden
  79. text_->SetPosition(GetIndentWidth() + clipBorder_.left_, clipBorder_.top_);
  80. // Sync the text line
  81. line_ = text_->GetText();
  82. }
  83. void LineEdit::Update(float timeStep)
  84. {
  85. if (cursorBlinkRate_ > 0.0f)
  86. cursorBlinkTimer_ = fmodf(cursorBlinkTimer_ + cursorBlinkRate_ * timeStep, 1.0f);
  87. // Update cursor position if font has changed
  88. if (text_->GetFont() != lastFont_ || text_->GetFontSize() != lastFontSize_)
  89. {
  90. lastFont_ = text_->GetFont();
  91. lastFontSize_ = text_->GetFontSize();
  92. UpdateCursor();
  93. }
  94. bool cursorVisible = HasFocus() ? cursorBlinkTimer_ < 0.5f : false;
  95. cursor_->SetVisible(cursorVisible);
  96. }
  97. void LineEdit::OnClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  98. {
  99. if (buttons & MOUSEB_LEFT && cursorMovable_)
  100. {
  101. if (doubleClickTimer_.GetMSec(true) < doubleClickInterval_)
  102. text_->SetSelection(0);
  103. else
  104. {
  105. unsigned pos = GetCharIndex(position);
  106. if (pos != M_MAX_UNSIGNED)
  107. {
  108. SetCursorPosition(pos);
  109. text_->ClearSelection();
  110. }
  111. }
  112. }
  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. void LineEdit::SetDoubleClickInterval(float interval)
  439. {
  440. doubleClickInterval_ = Max((int)(interval * 1000.0f), 0);
  441. }
  442. float LineEdit::GetDoubleClickInterval() const
  443. {
  444. return (float)doubleClickInterval_ / 1000.0f;
  445. }
  446. void LineEdit::UpdateText()
  447. {
  448. unsigned utf8Length = line_.LengthUTF8();
  449. if (!echoCharacter_)
  450. text_->SetText(line_);
  451. else
  452. {
  453. String echoText;
  454. for (unsigned i = 0; i < utf8Length; ++i)
  455. echoText.AppendUTF8(echoCharacter_);
  456. text_->SetText(echoText);
  457. }
  458. if (cursorPosition_ > utf8Length)
  459. {
  460. cursorPosition_ = utf8Length;
  461. UpdateCursor();
  462. }
  463. using namespace TextChanged;
  464. VariantMap eventData;
  465. eventData[P_ELEMENT] = (void*)this;
  466. eventData[P_TEXT] = line_;
  467. SendEvent(E_TEXTCHANGED, eventData);
  468. }
  469. void LineEdit::UpdateCursor()
  470. {
  471. int x = 0;
  472. const PODVector<IntVector2>& charPositions = text_->GetCharPositions();
  473. if (charPositions.Size())
  474. x = cursorPosition_ < charPositions.Size() ? charPositions[cursorPosition_].x_ : charPositions.Back().x_;
  475. text_->SetPosition(GetIndentWidth() + clipBorder_.left_, clipBorder_.top_);
  476. cursor_->SetPosition(text_->GetPosition() + IntVector2(x, 0));
  477. cursor_->SetSize(cursor_->GetWidth(), text_->GetRowHeight());
  478. // Scroll if necessary
  479. int sx = -GetChildOffset().x_;
  480. int left = clipBorder_.left_;
  481. int right = GetWidth() - clipBorder_.left_ - clipBorder_.right_ - cursor_->GetWidth();
  482. if (x - sx > right)
  483. sx = x - right;
  484. if (x - sx < left)
  485. sx = x - left;
  486. if (sx < 0)
  487. sx = 0;
  488. SetChildOffset(IntVector2(-sx, 0));
  489. // Restart blinking
  490. cursorBlinkTimer_ = 0.0f;
  491. }
  492. unsigned LineEdit::GetCharIndex(const IntVector2& position)
  493. {
  494. IntVector2 screenPosition = ElementToScreen(position);
  495. IntVector2 textPosition = text_->ScreenToElement(screenPosition);
  496. const PODVector<IntVector2>& charPositions = text_->GetCharPositions();
  497. if (textPosition.x_ < 0)
  498. return 0;
  499. for (unsigned i = charPositions.Size() - 1; i < charPositions.Size(); --i)
  500. {
  501. if (textPosition.x_ >= charPositions[i].x_)
  502. return i;
  503. }
  504. return M_MAX_UNSIGNED;
  505. }
  506. void LineEdit::HandleFocused(StringHash eventType, VariantMap& eventData)
  507. {
  508. UpdateCursor();
  509. }
  510. void LineEdit::HandleDefocused(StringHash eventType, VariantMap& eventData)
  511. {
  512. text_->ClearSelection();
  513. }
  514. }