LineEdit.cpp 17 KB

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