TextControls.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. label->scrollTextTo(0);
  339. }
  340. else if(slider)
  341. {
  342. // decrease width again if slider still used
  343. label->pos.w = pos.w - 16;
  344. assert(label->pos.w > 0);
  345. label->setText(text);
  346. slider->setAmount(label->textSize.y);
  347. }
  348. else if(label->textSize.y > label->pos.h)
  349. {
  350. // create slider and update widget
  351. label->pos.w = pos.w - 16;
  352. assert(label->pos.w > 0);
  353. label->setText(text);
  354. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  355. slider = std::make_shared<CSlider>(Point(pos.w - 16, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
  356. label->pos.h, label->textSize.y, 0, Orientation::VERTICAL, CSlider::EStyle(sliderStyle));
  357. slider->setScrollStep((int)graphics->fonts[label->font]->getLineHeight());
  358. slider->setPanningStep(1);
  359. slider->setScrollBounds(pos - slider->pos.topLeft());
  360. }
  361. }
  362. void CGStatusBar::setEnteringMode(bool on)
  363. {
  364. consoleText.clear();
  365. if (on)
  366. {
  367. //assert(enteringText == false);
  368. alignment = ETextAlignment::TOPLEFT;
  369. GH.startTextInput(pos);
  370. setText(consoleText);
  371. }
  372. else
  373. {
  374. //assert(enteringText == true);
  375. alignment = ETextAlignment::CENTER;
  376. GH.stopTextInput();
  377. setText(hoverText);
  378. }
  379. enteringText = on;
  380. }
  381. void CGStatusBar::setEnteredText(const std::string & text)
  382. {
  383. assert(enteringText == true);
  384. consoleText = text;
  385. setText(text);
  386. }
  387. void CGStatusBar::write(const std::string & Text)
  388. {
  389. hoverText = Text;
  390. if (enteringText == false)
  391. setText(hoverText);
  392. }
  393. void CGStatusBar::clearIfMatching(const std::string & Text)
  394. {
  395. if (hoverText == Text)
  396. clear();
  397. }
  398. void CGStatusBar::clear()
  399. {
  400. write({});
  401. }
  402. CGStatusBar::CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  403. : CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "", background_->pos.w)
  404. , enteringText(false)
  405. {
  406. addUsedEvents(LCLICK);
  407. background = background_;
  408. addChild(background.get());
  409. pos = background->pos;
  410. getBorderSize();
  411. autoRedraw = false;
  412. }
  413. CGStatusBar::CGStatusBar(int x, int y)
  414. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  415. , enteringText(false)
  416. {
  417. // without background
  418. addUsedEvents(LCLICK);
  419. }
  420. CGStatusBar::CGStatusBar(int x, int y, const ImagePath & name, int maxw)
  421. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  422. , enteringText(false)
  423. {
  424. addUsedEvents(LCLICK);
  425. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  426. auto backgroundImage = std::make_shared<CPicture>(name);
  427. background = backgroundImage;
  428. pos = background->pos;
  429. if((unsigned)maxw < (unsigned)pos.w) //(insigned)-1 > than any correct value of pos.w
  430. {
  431. //execution of this block when maxw is incorrect breaks text centralization (issue #3151)
  432. vstd::amin(pos.w, maxw);
  433. backgroundImage->srcRect = Rect(0, 0, maxw, pos.h);
  434. }
  435. autoRedraw = false;
  436. }
  437. CGStatusBar::~CGStatusBar()
  438. {
  439. assert(GH.statusbar().get() != this);
  440. }
  441. void CGStatusBar::show(Canvas & to)
  442. {
  443. showAll(to);
  444. }
  445. void CGStatusBar::clickPressed(const Point & cursorPosition)
  446. {
  447. if(LOCPLINT && LOCPLINT->cingconsole->isActive())
  448. LOCPLINT->cingconsole->startEnteringText();
  449. }
  450. void CGStatusBar::activate()
  451. {
  452. GH.setStatusbar(shared_from_this());
  453. CIntObject::activate();
  454. }
  455. void CGStatusBar::deactivate()
  456. {
  457. assert(GH.statusbar().get() == this);
  458. GH.setStatusbar(nullptr);
  459. if (enteringText)
  460. LOCPLINT->cingconsole->endEnteringText(false);
  461. CIntObject::deactivate();
  462. }
  463. Point CGStatusBar::getBorderSize()
  464. {
  465. //Width of borders where text should not be printed
  466. static const Point borderSize(5, 1);
  467. switch(alignment)
  468. {
  469. case ETextAlignment::TOPLEFT: return Point(borderSize.x, borderSize.y);
  470. case ETextAlignment::TOPCENTER: return Point(pos.w / 2, borderSize.y);
  471. case ETextAlignment::CENTER: return Point(pos.w / 2, pos.h / 2);
  472. case ETextAlignment::BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  473. }
  474. assert(0);
  475. return Point();
  476. }
  477. CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB, ETextAlignment alignment, bool giveFocusToInput)
  478. : CLabel(Pos.x, Pos.y, font, alignment),
  479. cb(CB)
  480. {
  481. setRedrawParent(true);
  482. pos.h = Pos.h;
  483. pos.w = Pos.w;
  484. maxWidth = Pos.w;
  485. background.reset();
  486. addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
  487. #if !defined(VCMI_MOBILE)
  488. if(giveFocusToInput)
  489. giveFocus();
  490. #endif
  491. }
  492. CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName, const CFunctionList<void(const std::string &)> & CB)
  493. :cb(CB)
  494. {
  495. pos += Pos.topLeft();
  496. pos.h = Pos.h;
  497. pos.w = Pos.w;
  498. maxWidth = Pos.w;
  499. OBJ_CONSTRUCTION;
  500. background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
  501. addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
  502. #if !defined(VCMI_MOBILE)
  503. giveFocus();
  504. #endif
  505. }
  506. CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
  507. {
  508. pos += Pos.topLeft();
  509. OBJ_CONSTRUCTION;
  510. background = std::make_shared<CPicture>(srf, Pos);
  511. pos.w = background->pos.w;
  512. pos.h = background->pos.h;
  513. maxWidth = Pos.w;
  514. background->pos = pos;
  515. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  516. #if !defined(VCMI_MOBILE)
  517. giveFocus();
  518. #endif
  519. }
  520. std::atomic<int> CFocusable::usageIndex(0);
  521. void CFocusable::focusGot()
  522. {
  523. GH.startTextInput(pos);
  524. usageIndex++;
  525. }
  526. void CFocusable::focusLost()
  527. {
  528. if(0 == --usageIndex)
  529. {
  530. GH.stopTextInput();
  531. }
  532. }
  533. std::string CTextInput::visibleText()
  534. {
  535. return focus ? text + newText + "_" : text;
  536. }
  537. void CTextInput::clickPressed(const Point & cursorPosition)
  538. {
  539. if(!focus)
  540. giveFocus();
  541. }
  542. void CTextInput::keyPressed(EShortcut key)
  543. {
  544. if(!focus)
  545. return;
  546. if(key == EShortcut::GLOBAL_MOVE_FOCUS)
  547. {
  548. moveFocus();
  549. return;
  550. }
  551. bool redrawNeeded = false;
  552. switch(key)
  553. {
  554. case EShortcut::GLOBAL_BACKSPACE:
  555. if(!newText.empty())
  556. {
  557. TextOperations::trimRightUnicode(newText);
  558. redrawNeeded = true;
  559. }
  560. else if(!text.empty())
  561. {
  562. TextOperations::trimRightUnicode(text);
  563. redrawNeeded = true;
  564. }
  565. break;
  566. default:
  567. break;
  568. }
  569. if(redrawNeeded)
  570. {
  571. redraw();
  572. cb(text);
  573. }
  574. }
  575. void CTextInput::showPopupWindow(const Point & cursorPosition)
  576. {
  577. if(!helpBox.empty()) //there is no point to show window with nothing inside...
  578. CRClickPopup::createAndPush(helpBox);
  579. }
  580. void CTextInput::setText(const std::string & nText)
  581. {
  582. setText(nText, false);
  583. }
  584. void CTextInput::setText(const std::string & nText, bool callCb)
  585. {
  586. CLabel::setText(nText);
  587. if(callCb)
  588. cb(text);
  589. }
  590. void CTextInput::setHelpText(const std::string & text)
  591. {
  592. helpBox = text;
  593. }
  594. void CTextInput::textInputed(const std::string & enteredText)
  595. {
  596. if(!focus)
  597. return;
  598. std::string oldText = text;
  599. setText(getText() + enteredText);
  600. filters(text, oldText);
  601. if(text != oldText)
  602. {
  603. redraw();
  604. cb(text);
  605. }
  606. newText.clear();
  607. }
  608. void CTextInput::textEdited(const std::string & enteredText)
  609. {
  610. if(!focus)
  611. return;
  612. newText = enteredText;
  613. redraw();
  614. cb(text + newText);
  615. }
  616. void CTextInput::filenameFilter(std::string & text, const std::string &)
  617. {
  618. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  619. size_t pos;
  620. while((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  621. text.erase(pos, 1);
  622. }
  623. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  624. {
  625. assert(minValue < maxValue);
  626. if(text.empty())
  627. text = "0";
  628. size_t pos = 0;
  629. if(text[0] == '-') //allow '-' sign as first symbol only
  630. pos++;
  631. while(pos < text.size())
  632. {
  633. if(text[pos] < '0' || text[pos] > '9')
  634. {
  635. text = oldText;
  636. return; //new text is not number.
  637. }
  638. pos++;
  639. }
  640. try
  641. {
  642. int value = boost::lexical_cast<int>(text);
  643. if(value < minValue)
  644. text = std::to_string(minValue);
  645. else if(value > maxValue)
  646. text = std::to_string(maxValue);
  647. }
  648. catch(boost::bad_lexical_cast &)
  649. {
  650. //Should never happen. Unless I missed some cases
  651. logGlobal->warn("Warning: failed to convert %s to number!", text);
  652. text = oldText;
  653. }
  654. }
  655. CFocusable::CFocusable()
  656. {
  657. focus = false;
  658. focusables.push_back(this);
  659. }
  660. CFocusable::~CFocusable()
  661. {
  662. if(hasFocus())
  663. {
  664. inputWithFocus = nullptr;
  665. focusLost();
  666. }
  667. focusables -= this;
  668. }
  669. bool CFocusable::hasFocus() const
  670. {
  671. return inputWithFocus == this;
  672. }
  673. void CFocusable::giveFocus()
  674. {
  675. focus = true;
  676. focusGot();
  677. redraw();
  678. if(inputWithFocus)
  679. {
  680. inputWithFocus->focus = false;
  681. inputWithFocus->focusLost();
  682. inputWithFocus->redraw();
  683. }
  684. inputWithFocus = this;
  685. }
  686. void CFocusable::moveFocus()
  687. {
  688. auto i = vstd::find(focusables, this),
  689. ourIt = i;
  690. for(i++; i != ourIt; i++)
  691. {
  692. if(i == focusables.end())
  693. i = focusables.begin();
  694. if (*i == this)
  695. return;
  696. if((*i)->isActive())
  697. {
  698. (*i)->giveFocus();
  699. break;
  700. }
  701. }
  702. }
  703. void CFocusable::removeFocus()
  704. {
  705. if(this == inputWithFocus)
  706. {
  707. focus = false;
  708. focusLost();
  709. redraw();
  710. inputWithFocus = nullptr;
  711. }
  712. }