LineEdit.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. // Remove selected text first
  191. if(text_->GetSelectionLength() > 0)
  192. {
  193. unsigned start = text_->GetSelectionStart();
  194. unsigned length = text_->GetSelectionLength();
  195. if (start + length < line_.LengthUTF8())
  196. line_ = line_.SubstringUTF8(0, start) + line_.SubstringUTF8(start + length);
  197. else
  198. line_ = line_.SubstringUTF8(0, start);
  199. text_->ClearSelection();
  200. cursorPosition_ = start;
  201. }
  202. if (cursorPosition_ < line_.LengthUTF8())
  203. line_ = line_.SubstringUTF8(0, cursorPosition_) + clipBoard + line_.SubstringUTF8(cursorPosition_);
  204. else
  205. line_ += clipBoard;
  206. cursorPosition_ += clipBoard.LengthUTF8();
  207. changed = true;
  208. }
  209. }
  210. break;
  211. case KEY_LEFT:
  212. if (!(qualifiers & QUAL_SHIFT))
  213. text_->ClearSelection();
  214. if (cursorMovable_ && cursorPosition_ > 0)
  215. {
  216. if (textSelectable_ && qualifiers & QUAL_SHIFT && !text_->GetSelectionLength())
  217. dragBeginCursor_ = cursorPosition_;
  218. if (qualifiers & QUAL_CTRL)
  219. cursorPosition_ = 0;
  220. else
  221. --cursorPosition_;
  222. cursorMoved = true;
  223. if (textSelectable_ && qualifiers & QUAL_SHIFT)
  224. {
  225. unsigned start = dragBeginCursor_;
  226. unsigned current = cursorPosition_;
  227. if (start < current)
  228. text_->SetSelection(start, current - start);
  229. else
  230. text_->SetSelection(current, start - current);
  231. }
  232. }
  233. break;
  234. case KEY_RIGHT:
  235. if (!(qualifiers & QUAL_SHIFT))
  236. text_->ClearSelection();
  237. if (cursorMovable_ && cursorPosition_ < line_.LengthUTF8())
  238. {
  239. if (textSelectable_ && qualifiers & QUAL_SHIFT && !text_->GetSelectionLength())
  240. dragBeginCursor_ = cursorPosition_;
  241. if (qualifiers & QUAL_CTRL)
  242. cursorPosition_ = line_.LengthUTF8();
  243. else
  244. ++cursorPosition_;
  245. cursorMoved = true;
  246. if (textSelectable_ && qualifiers & QUAL_SHIFT)
  247. {
  248. unsigned start = dragBeginCursor_;
  249. unsigned current = cursorPosition_;
  250. if (start < current)
  251. text_->SetSelection(start, current - start);
  252. else
  253. text_->SetSelection(current, start - current);
  254. }
  255. }
  256. break;
  257. case KEY_HOME:
  258. if (cursorMovable_ && cursorPosition_ > 0)
  259. {
  260. cursorPosition_ = 0;
  261. cursorMoved = true;
  262. }
  263. break;
  264. case KEY_END:
  265. if (cursorMovable_ && cursorPosition_ < line_.Length())
  266. {
  267. cursorPosition_ = line_.LengthUTF8();
  268. cursorMoved = true;
  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. {
  337. using namespace TextFinished;
  338. VariantMap eventData;
  339. eventData[P_ELEMENT] = (void*)this;
  340. eventData[P_TEXT] = line_;
  341. SendEvent(E_TEXTFINISHED, eventData);
  342. return;
  343. }
  344. break;
  345. }
  346. if (changed)
  347. {
  348. UpdateText();
  349. UpdateCursor();
  350. }
  351. else if (cursorMoved)
  352. UpdateCursor();
  353. }
  354. void LineEdit::OnChar(unsigned c, int buttons, int qualifiers)
  355. {
  356. bool changed = false;
  357. // If only CTRL is held down, do not edit
  358. if ((qualifiers & (QUAL_CTRL | QUAL_ALT)) == QUAL_CTRL)
  359. return;
  360. if (c >= 0x20 && (!maxLength_ || line_.LengthUTF8() < maxLength_))
  361. {
  362. String charStr;
  363. charStr.AppendUTF8(c);
  364. if (!text_->GetSelectionLength())
  365. {
  366. if (cursorPosition_ == line_.LengthUTF8())
  367. line_ += charStr;
  368. else
  369. line_ = line_.SubstringUTF8(0, cursorPosition_) + charStr + line_.SubstringUTF8(cursorPosition_);
  370. ++cursorPosition_;
  371. }
  372. else
  373. {
  374. // If a selection exists, erase it first
  375. unsigned start = text_->GetSelectionStart();
  376. unsigned length = text_->GetSelectionLength();
  377. if (start + length < line_.LengthUTF8())
  378. line_ = line_.SubstringUTF8(0, start) + charStr + line_.SubstringUTF8(start + length);
  379. else
  380. line_ = line_.SubstringUTF8(0, start) + charStr;
  381. cursorPosition_ = start + 1;
  382. }
  383. changed = true;
  384. }
  385. if (changed)
  386. {
  387. text_->ClearSelection();
  388. UpdateText();
  389. UpdateCursor();
  390. }
  391. }
  392. void LineEdit::SetText(const String& text)
  393. {
  394. if (text != line_)
  395. {
  396. line_ = text;
  397. cursorPosition_ = line_.LengthUTF8();
  398. UpdateText();
  399. UpdateCursor();
  400. }
  401. }
  402. void LineEdit::SetCursorPosition(unsigned position)
  403. {
  404. if (position > line_.LengthUTF8() || !cursorMovable_)
  405. position = line_.LengthUTF8();
  406. if (position != cursorPosition_)
  407. {
  408. cursorPosition_ = position;
  409. UpdateCursor();
  410. }
  411. }
  412. void LineEdit::SetCursorBlinkRate(float rate)
  413. {
  414. cursorBlinkRate_ = Max(rate, 0.0f);
  415. }
  416. void LineEdit::SetMaxLength(unsigned length)
  417. {
  418. maxLength_ = length;
  419. }
  420. void LineEdit::SetEchoCharacter(unsigned c)
  421. {
  422. echoCharacter_ = c;
  423. UpdateText();
  424. }
  425. void LineEdit::SetCursorMovable(bool enable)
  426. {
  427. cursorMovable_ = enable;
  428. }
  429. void LineEdit::SetTextSelectable(bool enable)
  430. {
  431. textSelectable_ = enable;
  432. }
  433. void LineEdit::SetTextCopyable(bool enable)
  434. {
  435. textCopyable_ = enable;
  436. }
  437. void LineEdit::UpdateText()
  438. {
  439. unsigned utf8Length = line_.LengthUTF8();
  440. if (!echoCharacter_)
  441. text_->SetText(line_);
  442. else
  443. {
  444. String echoText;
  445. for (unsigned i = 0; i < utf8Length; ++i)
  446. echoText.AppendUTF8(echoCharacter_);
  447. text_->SetText(echoText);
  448. }
  449. if (cursorPosition_ > utf8Length)
  450. {
  451. cursorPosition_ = utf8Length;
  452. UpdateCursor();
  453. }
  454. using namespace TextChanged;
  455. VariantMap eventData;
  456. eventData[P_ELEMENT] = (void*)this;
  457. eventData[P_TEXT] = line_;
  458. SendEvent(E_TEXTCHANGED, eventData);
  459. }
  460. void LineEdit::UpdateCursor()
  461. {
  462. int x = 0;
  463. const PODVector<IntVector2>& charPositions = text_->GetCharPositions();
  464. if (charPositions.Size())
  465. x = cursorPosition_ < charPositions.Size() ? charPositions[cursorPosition_].x_ : charPositions.Back().x_;
  466. text_->SetPosition(clipBorder_.left_, clipBorder_.top_);
  467. cursor_->SetPosition(text_->GetPosition() + IntVector2(x, 0));
  468. cursor_->SetSize(cursor_->GetWidth(), text_->GetRowHeight());
  469. // Scroll if necessary
  470. int sx = -GetChildOffset().x_;
  471. int left = clipBorder_.left_;
  472. int right = GetWidth() - clipBorder_.left_ - clipBorder_.right_ - cursor_->GetWidth();
  473. if (x - sx > right)
  474. sx = x - right;
  475. if (x - sx < left)
  476. sx = x - left;
  477. if (sx < 0)
  478. sx = 0;
  479. SetChildOffset(IntVector2(-sx, 0));
  480. // Restart blinking
  481. cursorBlinkTimer_ = 0.0f;
  482. }
  483. unsigned LineEdit::GetCharIndex(const IntVector2& position)
  484. {
  485. IntVector2 screenPosition = ElementToScreen(position);
  486. IntVector2 textPosition = text_->ScreenToElement(screenPosition);
  487. const PODVector<IntVector2>& charPositions = text_->GetCharPositions();
  488. if (textPosition.x_ < 0)
  489. return 0;
  490. for (unsigned i = charPositions.Size() - 1; i < charPositions.Size(); --i)
  491. {
  492. if (textPosition.x_ >= charPositions[i].x_)
  493. return i;
  494. }
  495. return M_MAX_UNSIGNED;
  496. }
  497. void LineEdit::HandleFocused(StringHash eventType, VariantMap& eventData)
  498. {
  499. UpdateCursor();
  500. }
  501. void LineEdit::HandleDefocused(StringHash eventType, VariantMap& eventData)
  502. {
  503. text_->ClearSelection();
  504. }
  505. }