LineEdit.cpp 17 KB

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