LineEdit.cpp 16 KB

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