LineEdit.cpp 17 KB

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