TextControls.cpp 19 KB

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