TextControls.cpp 16 KB

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