LineEdit.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. dragStartCursor_(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. }
  55. LineEdit::~LineEdit()
  56. {
  57. }
  58. void LineEdit::RegisterObject(Context* context)
  59. {
  60. context->RegisterFactory<LineEdit>();
  61. }
  62. void LineEdit::SetStyle(const XMLElement& element)
  63. {
  64. BorderImage::SetStyle(element);
  65. if (element.HasChild("maxlength"))
  66. SetMaxLength(element.GetChild("maxlength").GetInt("value"));
  67. if (element.HasChild("cursormovable"))
  68. SetCursorMovable(element.GetChild("cursormovable").GetBool("enable"));
  69. if (element.HasChild("textselectable"))
  70. SetTextSelectable(element.GetChild("textselectable").GetBool("enable"));
  71. if (element.HasChild("textcopyable"))
  72. SetTextCopyable(element.GetChild("textcopyable").GetBool("enable"));
  73. XMLElement textElem = element.GetChild("text");
  74. if (textElem)
  75. {
  76. if (textElem.HasAttribute("value"))
  77. SetText(textElem.GetString("value"));
  78. text_->SetStyle(textElem);
  79. }
  80. XMLElement cursorElem = element.GetChild("cursor");
  81. if (cursorElem)
  82. cursor_->SetStyle(cursorElem);
  83. if (element.HasChild("cursorposition"))
  84. SetCursorPosition(element.GetChild("cursorposition").GetInt("value"));
  85. if (element.HasChild("cursorblinkrate"))
  86. SetCursorBlinkRate(element.GetChild("cursorblinkrate").GetFloat("value"));
  87. if (element.HasChild("echocharacter"))
  88. {
  89. String text = element.GetChild("echocharacter").GetString("value");
  90. if (text.Length())
  91. SetEchoCharacter(text[0]);
  92. }
  93. // Set the text's position to match clipping, so that text left edge is not left partially hidden
  94. text_->SetPosition(clipBorder_.left_, clipBorder_.top_);
  95. }
  96. void LineEdit::Update(float timeStep)
  97. {
  98. if (cursorBlinkRate_ > 0.0f)
  99. cursorBlinkTimer_ = fmodf(cursorBlinkTimer_ + cursorBlinkRate_ * timeStep, 1.0f);
  100. else
  101. cursorBlinkTimer_ = 0.0f;
  102. // Update cursor position if font has changed
  103. if (text_->GetFont() != lastFont_ || text_->GetFontSize() != lastFontSize_)
  104. {
  105. lastFont_ = text_->GetFont();
  106. lastFontSize_ = text_->GetFontSize();
  107. UpdateCursor();
  108. }
  109. bool cursorVisible = false;
  110. if (HasFocus())
  111. cursorVisible = cursorBlinkTimer_ < 0.5f;
  112. cursor_->SetVisible(cursorVisible);
  113. }
  114. void LineEdit::OnClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  115. {
  116. if (buttons & MOUSEB_LEFT && cursorMovable_)
  117. {
  118. unsigned pos = GetCharIndex(position);
  119. if (pos != M_MAX_UNSIGNED)
  120. {
  121. SetCursorPosition(pos);
  122. text_->ClearSelection();
  123. }
  124. }
  125. }
  126. void LineEdit::OnDragStart(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  127. {
  128. dragStartCursor_ = GetCharIndex(position);
  129. }
  130. void LineEdit::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  131. {
  132. if (cursorMovable_ && textSelectable_)
  133. {
  134. unsigned start = dragStartCursor_;
  135. unsigned current = GetCharIndex(position);
  136. if (start != M_MAX_UNSIGNED && current != M_MAX_UNSIGNED)
  137. {
  138. if (start < current)
  139. text_->SetSelection(start, current - start);
  140. else
  141. text_->SetSelection(current, start - current);
  142. SetCursorPosition(current);
  143. }
  144. }
  145. }
  146. bool LineEdit::OnDragDropTest(UIElement* source)
  147. {
  148. return (dynamic_cast<LineEdit*>(source) != 0);
  149. }
  150. bool LineEdit::OnDragDropFinish(UIElement* source)
  151. {
  152. LineEdit* sourceLineEdit = dynamic_cast<LineEdit*>(source);
  153. if (sourceLineEdit)
  154. {
  155. SetText(sourceLineEdit->GetText());
  156. return true;
  157. }
  158. else
  159. return false;
  160. }
  161. void LineEdit::OnKey(int key, int buttons, int qualifiers)
  162. {
  163. bool changed = false;
  164. bool cursorMoved = false;
  165. switch (key)
  166. {
  167. case 'X':
  168. case 'C':
  169. if (textCopyable_ && qualifiers & QUAL_CTRL)
  170. {
  171. unsigned start = text_->GetSelectionStart();
  172. unsigned length = text_->GetSelectionLength();
  173. if (text_->GetSelectionLength())
  174. GetSubsystem<UI>()->SetClipBoardText(line_.Substring(start, length));
  175. if (key == 'X')
  176. {
  177. if (start + length < line_.Length())
  178. line_ = line_.Substring(0, start) + line_.Substring(start + length);
  179. else
  180. line_ = line_.Substring(0, start);
  181. text_->ClearSelection();
  182. cursorPosition_ = start;
  183. changed = true;
  184. }
  185. }
  186. break;
  187. case 'V':
  188. if (textCopyable_ && qualifiers & QUAL_CTRL)
  189. {
  190. const String& clipBoard = GetSubsystem<UI>()->GetClipBoardText();
  191. if (!clipBoard.Empty())
  192. {
  193. if (cursorPosition_ < line_.Length())
  194. line_ = line_.Substring(0, cursorPosition_) + clipBoard + line_.Substring(cursorPosition_);
  195. else
  196. line_ += clipBoard;
  197. cursorPosition_ += clipBoard.Length();
  198. changed = true;
  199. }
  200. }
  201. break;
  202. case KEY_LEFT:
  203. if (!(qualifiers & QUAL_SHIFT))
  204. text_->ClearSelection();
  205. if (cursorMovable_ && cursorPosition_ > 0)
  206. {
  207. if (textSelectable_ && qualifiers & QUAL_SHIFT && !text_->GetSelectionLength())
  208. dragStartCursor_ = cursorPosition_;
  209. if (qualifiers & QUAL_CTRL)
  210. cursorPosition_ = 0;
  211. else
  212. --cursorPosition_;
  213. cursorMoved = true;
  214. if (textSelectable_ && qualifiers & QUAL_SHIFT)
  215. {
  216. unsigned start = dragStartCursor_;
  217. unsigned current = cursorPosition_;
  218. if (start < current)
  219. text_->SetSelection(start, current - start);
  220. else
  221. text_->SetSelection(current, start - current);
  222. }
  223. }
  224. break;
  225. case KEY_RIGHT:
  226. if (!(qualifiers & QUAL_SHIFT))
  227. text_->ClearSelection();
  228. if (cursorMovable_ && cursorPosition_ < line_.Length())
  229. {
  230. if (textSelectable_ && qualifiers & QUAL_SHIFT && !text_->GetSelectionLength())
  231. dragStartCursor_ = cursorPosition_;
  232. if (qualifiers & QUAL_CTRL)
  233. cursorPosition_ = line_.Length();
  234. else
  235. ++cursorPosition_;
  236. cursorMoved = true;
  237. if (textSelectable_ && qualifiers & QUAL_SHIFT)
  238. {
  239. unsigned start = dragStartCursor_;
  240. unsigned current = cursorPosition_;
  241. if (start < current)
  242. text_->SetSelection(start, current - start);
  243. else
  244. text_->SetSelection(current, start - current);
  245. }
  246. }
  247. break;
  248. case KEY_HOME:
  249. if (cursorMovable_ && cursorPosition_ > 0)
  250. {
  251. cursorPosition_ = 0;
  252. cursorMoved = true;
  253. }
  254. break;
  255. case KEY_END:
  256. if (cursorMovable_ && cursorPosition_ < line_.Length())
  257. {
  258. cursorPosition_ = line_.Length();
  259. cursorMoved = true;
  260. }
  261. break;
  262. case KEY_DELETE:
  263. if (!text_->GetSelectionLength())
  264. {
  265. if (cursorPosition_ < line_.Length())
  266. {
  267. line_ = line_.Substring(0, cursorPosition_) + line_.Substring(cursorPosition_ + 1);
  268. changed = true;
  269. }
  270. }
  271. else
  272. {
  273. // If a selection exists, erase it
  274. unsigned start = text_->GetSelectionStart();
  275. unsigned length = text_->GetSelectionLength();
  276. if (start + length < line_.Length())
  277. line_ = line_.Substring(0, start) + line_.Substring(start + length);
  278. else
  279. line_ = line_.Substring(0, start);
  280. text_->ClearSelection();
  281. cursorPosition_ = start;
  282. changed = true;
  283. }
  284. break;
  285. case KEY_UP:
  286. case KEY_DOWN:
  287. case KEY_PAGEUP:
  288. case KEY_PAGEDOWN:
  289. {
  290. using namespace UnhandledKey;
  291. VariantMap eventData;
  292. eventData[P_ELEMENT] = (void*)this;
  293. eventData[P_KEY] = key;
  294. eventData[P_BUTTONS] = buttons;
  295. eventData[P_QUALIFIERS] = qualifiers;
  296. SendEvent(E_UNHANDLEDKEY, eventData);
  297. }
  298. return;
  299. case KEY_BACKSPACE:
  300. if (!text_->GetSelectionLength())
  301. {
  302. if (line_.Length() && cursorPosition_)
  303. {
  304. if (cursorPosition_ < line_.Length())
  305. line_ = line_.Substring(0, cursorPosition_ - 1) + line_.Substring(cursorPosition_);
  306. else
  307. line_ = line_.Substring(0, cursorPosition_ - 1);
  308. --cursorPosition_;
  309. changed = true;
  310. }
  311. }
  312. else
  313. {
  314. // If a selection exists, erase it
  315. unsigned start = text_->GetSelectionStart();
  316. unsigned length = text_->GetSelectionLength();
  317. if (start + length < line_.Length())
  318. line_ = line_.Substring(0, start) + line_.Substring(start + length);
  319. else
  320. line_ = line_.Substring(0, start);
  321. text_->ClearSelection();
  322. cursorPosition_ = start;
  323. changed = true;
  324. }
  325. break;
  326. case KEY_RETURN:
  327. {
  328. using namespace TextFinished;
  329. VariantMap eventData;
  330. eventData[P_ELEMENT] = (void*)this;
  331. eventData[P_TEXT] = line_;
  332. SendEvent(E_TEXTFINISHED, eventData);
  333. return;
  334. }
  335. break;
  336. }
  337. if (changed)
  338. {
  339. UpdateText();
  340. UpdateCursor();
  341. }
  342. else if (cursorMoved)
  343. UpdateCursor();
  344. }
  345. void LineEdit::OnChar(unsigned char c, int buttons, int qualifiers)
  346. {
  347. bool changed = false;
  348. // If only CTRL is held down, do not edit
  349. if ((qualifiers & (QUAL_CTRL | QUAL_ALT)) == QUAL_CTRL)
  350. return;
  351. if (c >= 0x20 && (!maxLength_ || line_.Length() < maxLength_))
  352. {
  353. String charStr;
  354. charStr.Resize(1);
  355. charStr[0] = c;
  356. if (!text_->GetSelectionLength())
  357. {
  358. if (cursorPosition_ == line_.Length())
  359. line_ += charStr;
  360. else
  361. line_ = line_.Substring(0, cursorPosition_) + charStr + line_.Substring(cursorPosition_);
  362. ++cursorPosition_;
  363. }
  364. else
  365. {
  366. // If a selection exists, erase it first
  367. unsigned start = text_->GetSelectionStart();
  368. unsigned length = text_->GetSelectionLength();
  369. if (start + length < line_.Length())
  370. line_ = line_.Substring(0, start) + charStr + line_.Substring(start + length);
  371. else
  372. line_ = line_.Substring(0, start) + charStr;
  373. cursorPosition_ = start + 1;
  374. }
  375. changed = true;
  376. }
  377. if (changed)
  378. {
  379. text_->ClearSelection();
  380. UpdateText();
  381. UpdateCursor();
  382. }
  383. }
  384. void LineEdit::OnFocus()
  385. {
  386. UpdateCursor();
  387. }
  388. void LineEdit::OnDefocus()
  389. {
  390. text_->ClearSelection();
  391. }
  392. void LineEdit::SetText(const String& text)
  393. {
  394. if (text != line_)
  395. {
  396. line_ = text;
  397. cursorPosition_ = line_.Length();
  398. UpdateText();
  399. UpdateCursor();
  400. }
  401. }
  402. void LineEdit::SetCursorPosition(unsigned position)
  403. {
  404. if (position > line_.Length() || !cursorMovable_)
  405. position = line_.Length();
  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(char 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. if (!echoCharacter_)
  440. text_->SetText(line_);
  441. else
  442. {
  443. String echoText;
  444. echoText.Resize(line_.Length());
  445. for (unsigned i = 0; i < line_.Length(); ++i)
  446. echoText[i] = echoCharacter_;
  447. text_->SetText(echoText);
  448. }
  449. if (cursorPosition_ > line_.Length())
  450. {
  451. cursorPosition_ = line_.Length();
  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. }