TextControls.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * TextControls.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "TextControls.h"
  12. #include "Slider.h"
  13. #include "Images.h"
  14. #include "../CPlayerInterface.h"
  15. #include "../gui/CGuiHandler.h"
  16. #include "../gui/Shortcut.h"
  17. #include "../windows/CMessage.h"
  18. #include "../windows/InfoWindows.h"
  19. #include "../adventureMap/CInGameConsole.h"
  20. #include "../renderSDL/SDL_Extensions.h"
  21. #include "../render/Canvas.h"
  22. #include "../render/Graphics.h"
  23. #include "../render/IFont.h"
  24. #include "../../lib/TextOperations.h"
  25. #ifdef VCMI_ANDROID
  26. #include "lib/CAndroidVMHelper.h"
  27. #endif
  28. std::list<CFocusable*> CFocusable::focusables;
  29. CFocusable * CFocusable::inputWithFocus;
  30. std::string CLabel::visibleText()
  31. {
  32. return text;
  33. }
  34. void CLabel::showAll(Canvas & to)
  35. {
  36. CIntObject::showAll(to);
  37. if(!visibleText().empty())
  38. blitLine(to, pos, visibleText());
  39. }
  40. CLabel::CLabel(int x, int y, EFonts Font, ETextAlignment Align, const ColorRGBA & Color, const std::string & Text)
  41. : CTextContainer(Align, Font, Color), text(Text)
  42. {
  43. setRedrawParent(true);
  44. autoRedraw = true;
  45. pos.x += x;
  46. pos.y += y;
  47. pos.w = pos.h = 0;
  48. if(alignment == ETextAlignment::TOPLEFT) // causes issues for MIDDLE
  49. {
  50. pos.w = (int)graphics->fonts[font]->getStringWidth(visibleText().c_str());
  51. pos.h = (int)graphics->fonts[font]->getLineHeight();
  52. }
  53. }
  54. Point CLabel::getBorderSize()
  55. {
  56. return Point(0, 0);
  57. }
  58. std::string CLabel::getText()
  59. {
  60. return text;
  61. }
  62. void CLabel::setAutoRedraw(bool value)
  63. {
  64. autoRedraw = value;
  65. }
  66. void CLabel::setText(const std::string & Txt)
  67. {
  68. text = Txt;
  69. if(autoRedraw)
  70. {
  71. if(background || !parent)
  72. redraw();
  73. else
  74. parent->redraw();
  75. }
  76. }
  77. void CLabel::setColor(const ColorRGBA & Color)
  78. {
  79. color = Color;
  80. if(autoRedraw)
  81. {
  82. if(background || !parent)
  83. redraw();
  84. else
  85. parent->redraw();
  86. }
  87. }
  88. size_t CLabel::getWidth()
  89. {
  90. return graphics->fonts[font]->getStringWidth(visibleText());;
  91. }
  92. CMultiLineLabel::CMultiLineLabel(Rect position, EFonts Font, ETextAlignment Align, const ColorRGBA & Color, const std::string & Text) :
  93. CLabel(position.x, position.y, Font, Align, Color, Text),
  94. visibleSize(0, 0, position.w, position.h)
  95. {
  96. pos.w = position.w;
  97. pos.h = position.h;
  98. splitText(Text, true);
  99. }
  100. void CMultiLineLabel::setVisibleSize(Rect visibleSize, bool redrawElement)
  101. {
  102. this->visibleSize = visibleSize;
  103. if(redrawElement)
  104. redraw();
  105. }
  106. void CMultiLineLabel::scrollTextBy(int distance)
  107. {
  108. scrollTextTo(visibleSize.y + distance);
  109. }
  110. void CMultiLineLabel::scrollTextTo(int distance, bool redrawAfterScroll)
  111. {
  112. Rect size = visibleSize;
  113. size.y = distance;
  114. setVisibleSize(size, redrawAfterScroll);
  115. }
  116. void CMultiLineLabel::setText(const std::string & Txt)
  117. {
  118. splitText(Txt, false); //setText used below can handle redraw
  119. CLabel::setText(Txt);
  120. }
  121. void CTextContainer::blitLine(Canvas & to, Rect destRect, std::string what)
  122. {
  123. const auto f = graphics->fonts[font];
  124. Point where = destRect.topLeft();
  125. const std::string delimeters = "{}";
  126. auto delimitersCount = std::count_if(what.cbegin(), what.cend(), [&delimeters](char c)
  127. {
  128. return delimeters.find(c) != std::string::npos;
  129. });
  130. //We should count delimiters length from string to correct centering later.
  131. delimitersCount *= f->getStringWidth(delimeters)/2;
  132. // input is rect in which given text should be placed
  133. // calculate proper position for top-left corner of the text
  134. if(alignment == ETextAlignment::TOPLEFT)
  135. {
  136. where.x += getBorderSize().x;
  137. where.y += getBorderSize().y;
  138. }
  139. if(alignment == ETextAlignment::CENTER)
  140. {
  141. where.x += (int(destRect.w) - int(f->getStringWidth(what) - delimitersCount)) / 2;
  142. where.y += (int(destRect.h) - int(f->getLineHeight())) / 2;
  143. }
  144. if(alignment == ETextAlignment::BOTTOMRIGHT)
  145. {
  146. where.x += getBorderSize().x + destRect.w - ((int)f->getStringWidth(what) - delimitersCount);
  147. where.y += getBorderSize().y + destRect.h - (int)f->getLineHeight();
  148. }
  149. size_t begin = 0;
  150. size_t currDelimeter = 0;
  151. do
  152. {
  153. size_t end = what.find_first_of(delimeters[currDelimeter % 2], begin);
  154. if(begin != end)
  155. {
  156. std::string toPrint = what.substr(begin, end - begin);
  157. if(currDelimeter % 2) // Enclosed in {} text - set to yellow
  158. to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint);
  159. else // Non-enclosed text, use default color
  160. to.drawText(where, font, color, ETextAlignment::TOPLEFT, toPrint);
  161. begin = end;
  162. where.x += (int)f->getStringWidth(toPrint);
  163. }
  164. currDelimeter++;
  165. } while(begin++ != std::string::npos);
  166. }
  167. CTextContainer::CTextContainer(ETextAlignment alignment, EFonts font, ColorRGBA color) :
  168. alignment(alignment),
  169. font(font),
  170. color(color)
  171. {
  172. }
  173. void CMultiLineLabel::showAll(Canvas & to)
  174. {
  175. CIntObject::showAll(to);
  176. const auto f = graphics->fonts[font];
  177. // calculate which lines should be visible
  178. int totalLines = static_cast<int>(lines.size());
  179. int beginLine = visibleSize.y;
  180. int endLine = getTextLocation().h + visibleSize.y;
  181. if(beginLine < 0)
  182. beginLine = 0;
  183. else
  184. beginLine /= (int)f->getLineHeight();
  185. if(endLine < 0)
  186. endLine = 0;
  187. else
  188. endLine /= (int)f->getLineHeight();
  189. endLine++;
  190. // and where they should be displayed
  191. Point lineStart = getTextLocation().topLeft() - visibleSize + Point(0, beginLine * (int)f->getLineHeight());
  192. Point lineSize = Point(getTextLocation().w, (int)f->getLineHeight());
  193. CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), getTextLocation()); // to properly trim text that is too big to fit
  194. for(int i = beginLine; i < std::min(totalLines, endLine); i++)
  195. {
  196. if(!lines[i].empty()) //non-empty line
  197. blitLine(to, Rect(lineStart, lineSize), lines[i]);
  198. lineStart.y += (int)f->getLineHeight();
  199. }
  200. }
  201. void CMultiLineLabel::splitText(const std::string & Txt, bool redrawAfter)
  202. {
  203. lines.clear();
  204. const auto f = graphics->fonts[font];
  205. int lineHeight = static_cast<int>(f->getLineHeight());
  206. lines = CMessage::breakText(Txt, pos.w, font);
  207. textSize.y = lineHeight * (int)lines.size();
  208. textSize.x = 0;
  209. for(const std::string & line : lines)
  210. vstd::amax(textSize.x, f->getStringWidth(line.c_str()));
  211. if(redrawAfter)
  212. redraw();
  213. }
  214. Rect CMultiLineLabel::getTextLocation()
  215. {
  216. // this method is needed for vertical alignment alignment of text
  217. // when height of available text is smaller than height of widget
  218. // in this case - we should add proper offset to display text at required position
  219. if(pos.h <= textSize.y)
  220. return pos;
  221. Point textSize(pos.w, (int)graphics->fonts[font]->getLineHeight() * (int)lines.size());
  222. Point textOffset(pos.w - textSize.x, pos.h - textSize.y);
  223. switch(alignment)
  224. {
  225. case ETextAlignment::TOPLEFT: return Rect(pos.topLeft(), textSize);
  226. case ETextAlignment::CENTER: return Rect(pos.topLeft() + textOffset / 2, textSize);
  227. case ETextAlignment::BOTTOMRIGHT: return Rect(pos.topLeft() + textOffset, textSize);
  228. }
  229. assert(0);
  230. return Rect();
  231. }
  232. CLabelGroup::CLabelGroup(EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  233. : font(Font), align(Align), color(Color)
  234. {
  235. defActions = 255 - DISPOSE;
  236. }
  237. void CLabelGroup::add(int x, int y, const std::string & text)
  238. {
  239. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  240. labels.push_back(std::make_shared<CLabel>(x, y, font, align, color, text));
  241. }
  242. size_t CLabelGroup::currentSize() const
  243. {
  244. return labels.size();
  245. }
  246. CTextBox::CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font, ETextAlignment Align, const ColorRGBA & Color) :
  247. sliderStyle(SliderStyle),
  248. slider(nullptr)
  249. {
  250. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  251. label = std::make_shared<CMultiLineLabel>(rect, Font, Align, Color);
  252. setRedrawParent(true);
  253. pos.x += rect.x;
  254. pos.y += rect.y;
  255. pos.h = rect.h;
  256. pos.w = rect.w;
  257. assert(pos.w >= 40); //we need some space
  258. setText(Text);
  259. }
  260. void CTextBox::sliderMoved(int to)
  261. {
  262. label->scrollTextTo(to);
  263. }
  264. void CTextBox::resize(Point newSize)
  265. {
  266. pos.w = newSize.x;
  267. pos.h = newSize.y;
  268. label->pos.w = pos.w;
  269. label->pos.h = pos.h;
  270. slider.reset();
  271. setText(label->getText()); // force refresh
  272. }
  273. void CTextBox::setText(const std::string & text)
  274. {
  275. label->pos.w = pos.w; // reset to default before textSize.y check
  276. label->setText(text);
  277. if(label->textSize.y <= label->pos.h && slider)
  278. {
  279. // slider is no longer needed
  280. slider.reset();
  281. }
  282. else if(slider)
  283. {
  284. // decrease width again if slider still used
  285. label->pos.w = pos.w - 32;
  286. label->setText(text);
  287. slider->setAmount(label->textSize.y);
  288. }
  289. else if(label->textSize.y > label->pos.h)
  290. {
  291. // create slider and update widget
  292. label->pos.w = pos.w - 32;
  293. label->setText(text);
  294. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  295. slider = std::make_shared<CSlider>(Point(pos.w - 32, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
  296. label->pos.h, label->textSize.y, 0, Orientation::VERTICAL, CSlider::EStyle(sliderStyle));
  297. slider->setScrollStep((int)graphics->fonts[label->font]->getLineHeight());
  298. slider->setPanningStep(1);
  299. slider->setScrollBounds(pos - slider->pos.topLeft());
  300. }
  301. }
  302. void CGStatusBar::setEnteringMode(bool on)
  303. {
  304. consoleText.clear();
  305. if (on)
  306. {
  307. //assert(enteringText == false);
  308. alignment = ETextAlignment::TOPLEFT;
  309. GH.startTextInput(pos);
  310. setText(consoleText);
  311. }
  312. else
  313. {
  314. //assert(enteringText == true);
  315. alignment = ETextAlignment::CENTER;
  316. GH.stopTextInput();
  317. setText(hoverText);
  318. }
  319. enteringText = on;
  320. }
  321. void CGStatusBar::setEnteredText(const std::string & text)
  322. {
  323. assert(enteringText == true);
  324. consoleText = text;
  325. setText(text);
  326. }
  327. void CGStatusBar::write(const std::string & Text)
  328. {
  329. hoverText = Text;
  330. if (enteringText == false)
  331. setText(hoverText);
  332. }
  333. void CGStatusBar::clearIfMatching(const std::string & Text)
  334. {
  335. if (hoverText == Text)
  336. clear();
  337. }
  338. void CGStatusBar::clear()
  339. {
  340. write({});
  341. }
  342. CGStatusBar::CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  343. : CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "")
  344. , enteringText(false)
  345. {
  346. addUsedEvents(LCLICK);
  347. background = background_;
  348. addChild(background.get());
  349. pos = background->pos;
  350. getBorderSize();
  351. autoRedraw = false;
  352. }
  353. CGStatusBar::CGStatusBar(int x, int y, std::string name, int maxw)
  354. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  355. , enteringText(false)
  356. {
  357. addUsedEvents(LCLICK);
  358. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  359. auto backgroundImage = std::make_shared<CPicture>(name);
  360. background = backgroundImage;
  361. pos = background->pos;
  362. if((unsigned)maxw < (unsigned)pos.w) //(insigned)-1 > than any correct value of pos.w
  363. {
  364. //execution of this block when maxw is incorrect breaks text centralization (issue #3151)
  365. vstd::amin(pos.w, maxw);
  366. backgroundImage->srcRect = Rect(0, 0, maxw, pos.h);
  367. }
  368. autoRedraw = false;
  369. }
  370. CGStatusBar::~CGStatusBar()
  371. {
  372. assert(GH.statusbar().get() != this);
  373. }
  374. void CGStatusBar::show(Canvas & to)
  375. {
  376. showAll(to);
  377. }
  378. void CGStatusBar::clickPressed(const Point & cursorPosition)
  379. {
  380. if(LOCPLINT && LOCPLINT->cingconsole->isActive())
  381. LOCPLINT->cingconsole->startEnteringText();
  382. }
  383. void CGStatusBar::activate()
  384. {
  385. GH.setStatusbar(shared_from_this());
  386. CIntObject::activate();
  387. }
  388. void CGStatusBar::deactivate()
  389. {
  390. assert(GH.statusbar().get() == this);
  391. GH.setStatusbar(nullptr);
  392. if (enteringText)
  393. LOCPLINT->cingconsole->endEnteringText(false);
  394. CIntObject::deactivate();
  395. }
  396. Point CGStatusBar::getBorderSize()
  397. {
  398. //Width of borders where text should not be printed
  399. static const Point borderSize(5, 1);
  400. switch(alignment)
  401. {
  402. case ETextAlignment::TOPLEFT: return Point(borderSize.x, borderSize.y);
  403. case ETextAlignment::CENTER: return Point(pos.w / 2, pos.h / 2);
  404. case ETextAlignment::BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  405. }
  406. assert(0);
  407. return Point();
  408. }
  409. CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB)
  410. : CLabel(Pos.x, Pos.y, font, ETextAlignment::CENTER),
  411. cb(CB),
  412. CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  413. {
  414. setRedrawParent(true);
  415. pos.h = Pos.h;
  416. pos.w = Pos.w;
  417. background.reset();
  418. addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
  419. #if !defined(VCMI_MOBILE)
  420. giveFocus();
  421. #endif
  422. }
  423. CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const std::string & bgName, const CFunctionList<void(const std::string &)> & CB)
  424. :cb(CB), CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  425. {
  426. pos += Pos.topLeft();
  427. pos.h = Pos.h;
  428. pos.w = Pos.w;
  429. OBJ_CONSTRUCTION;
  430. background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
  431. addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
  432. #if !defined(VCMI_MOBILE)
  433. giveFocus();
  434. #endif
  435. }
  436. CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
  437. :CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  438. {
  439. pos += Pos.topLeft();
  440. OBJ_CONSTRUCTION;
  441. background = std::make_shared<CPicture>(srf, Pos);
  442. pos.w = background->pos.w;
  443. pos.h = background->pos.h;
  444. background->pos = pos;
  445. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  446. #if !defined(VCMI_MOBILE)
  447. giveFocus();
  448. #endif
  449. }
  450. std::atomic<int> CKeyboardFocusListener::usageIndex(0);
  451. CKeyboardFocusListener::CKeyboardFocusListener(CTextInput * textInput)
  452. :textInput(textInput)
  453. {
  454. }
  455. void CKeyboardFocusListener::focusGot()
  456. {
  457. GH.startTextInput(textInput->pos);
  458. usageIndex++;
  459. }
  460. void CKeyboardFocusListener::focusLost()
  461. {
  462. if(0 == --usageIndex)
  463. {
  464. GH.stopTextInput();
  465. }
  466. }
  467. std::string CTextInput::visibleText()
  468. {
  469. return focus ? text + newText + "_" : text;
  470. }
  471. void CTextInput::clickPressed(const Point & cursorPosition)
  472. {
  473. if(!focus)
  474. giveFocus();
  475. }
  476. void CTextInput::keyPressed(EShortcut key)
  477. {
  478. if(!focus)
  479. return;
  480. if(key == EShortcut::GLOBAL_MOVE_FOCUS)
  481. {
  482. moveFocus();
  483. return;
  484. }
  485. bool redrawNeeded = false;
  486. switch(key)
  487. {
  488. case EShortcut::GLOBAL_BACKSPACE:
  489. if(!newText.empty())
  490. {
  491. TextOperations::trimRightUnicode(newText);
  492. redrawNeeded = true;
  493. }
  494. else if(!text.empty())
  495. {
  496. TextOperations::trimRightUnicode(text);
  497. redrawNeeded = true;
  498. }
  499. break;
  500. default:
  501. break;
  502. }
  503. if(redrawNeeded)
  504. {
  505. redraw();
  506. cb(text);
  507. }
  508. }
  509. void CTextInput::showPopupWindow(const Point & cursorPosition)
  510. {
  511. if(!helpBox.empty()) //there is no point to show window with nothing inside...
  512. CRClickPopup::createAndPush(helpBox);
  513. }
  514. void CTextInput::setText(const std::string & nText)
  515. {
  516. setText(nText, false);
  517. }
  518. void CTextInput::setText(const std::string & nText, bool callCb)
  519. {
  520. CLabel::setText(nText);
  521. if(callCb)
  522. cb(text);
  523. }
  524. void CTextInput::setHelpText(const std::string & text)
  525. {
  526. helpBox = text;
  527. }
  528. void CTextInput::textInputed(const std::string & enteredText)
  529. {
  530. if(!focus)
  531. return;
  532. std::string oldText = text;
  533. text += enteredText;
  534. filters(text, oldText);
  535. if(text != oldText)
  536. {
  537. redraw();
  538. cb(text);
  539. }
  540. newText.clear();
  541. }
  542. void CTextInput::textEdited(const std::string & enteredText)
  543. {
  544. if(!focus)
  545. return;
  546. newText = enteredText;
  547. redraw();
  548. cb(text + newText);
  549. }
  550. void CTextInput::filenameFilter(std::string & text, const std::string &)
  551. {
  552. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  553. size_t pos;
  554. while((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  555. text.erase(pos, 1);
  556. }
  557. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  558. {
  559. assert(minValue < maxValue);
  560. if(text.empty())
  561. text = "0";
  562. size_t pos = 0;
  563. if(text[0] == '-') //allow '-' sign as first symbol only
  564. pos++;
  565. while(pos < text.size())
  566. {
  567. if(text[pos] < '0' || text[pos] > '9')
  568. {
  569. text = oldText;
  570. return; //new text is not number.
  571. }
  572. pos++;
  573. }
  574. try
  575. {
  576. int value = boost::lexical_cast<int>(text);
  577. if(value < minValue)
  578. text = std::to_string(minValue);
  579. else if(value > maxValue)
  580. text = std::to_string(maxValue);
  581. }
  582. catch(boost::bad_lexical_cast &)
  583. {
  584. //Should never happen. Unless I missed some cases
  585. logGlobal->warn("Warning: failed to convert %s to number!", text);
  586. text = oldText;
  587. }
  588. }
  589. CFocusable::CFocusable()
  590. :CFocusable(std::make_shared<IFocusListener>())
  591. {
  592. }
  593. CFocusable::CFocusable(std::shared_ptr<IFocusListener> focusListener)
  594. : focusListener(focusListener)
  595. {
  596. focus = false;
  597. focusables.push_back(this);
  598. }
  599. CFocusable::~CFocusable()
  600. {
  601. if(hasFocus())
  602. {
  603. inputWithFocus = nullptr;
  604. focusListener->focusLost();
  605. }
  606. focusables -= this;
  607. }
  608. bool CFocusable::hasFocus() const
  609. {
  610. return inputWithFocus == this;
  611. }
  612. void CFocusable::giveFocus()
  613. {
  614. focus = true;
  615. focusListener->focusGot();
  616. redraw();
  617. if(inputWithFocus)
  618. {
  619. inputWithFocus->focus = false;
  620. inputWithFocus->focusListener->focusLost();
  621. inputWithFocus->redraw();
  622. }
  623. inputWithFocus = this;
  624. }
  625. void CFocusable::moveFocus()
  626. {
  627. auto i = vstd::find(focusables, this),
  628. ourIt = i;
  629. for(i++; i != ourIt; i++)
  630. {
  631. if(i == focusables.end())
  632. i = focusables.begin();
  633. if((*i)->isActive())
  634. {
  635. (*i)->giveFocus();
  636. break;
  637. }
  638. }
  639. }