TextField.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  1. #include "TextField.h"
  2. #include <math.h>
  3. #include "AlphaField.h"
  4. #include "Border.h"
  5. #include "Font.h"
  6. #include "Globals.h"
  7. #include "Image.h"
  8. #include "KeyboardEvent.h"
  9. #include "MouseEvent.h"
  10. #include "Scroll.h"
  11. #include "Text.h"
  12. #include "ToolTip.h"
  13. #include "Window.h"
  14. using namespace Framework;
  15. #ifndef test
  16. # define test
  17. bool TextStyle::equals(const TextStyle& rhs)
  18. {
  19. return fontSize == rhs.fontSize && fontColor == rhs.fontColor
  20. && selectedColor == rhs.selectedColor
  21. && selectedBackcroundColor == rhs.selectedBackcroundColor
  22. && underlined == rhs.underlined && selected == rhs.selected
  23. && interactParam == rhs.interactParam
  24. && rendererIndex == rhs.rendererIndex;
  25. }
  26. #endif
  27. TextField::TextStyleManager::TextStyleManager()
  28. : ReferenceCounter(),
  29. renderer(new RCArray<TextRenderer>()),
  30. index(0),
  31. styleIndex(0),
  32. text(0)
  33. {
  34. current.beginIndex = 0;
  35. current.fontColor = 0xFFFFFFFF;
  36. current.fontSize = 12;
  37. current.selected = 0;
  38. current.selectedColor = 0xFFFFFFFF;
  39. current.selectedBackcroundColor = 0xFF0000FF;
  40. current.underlined = 0;
  41. current.interactParam = 0;
  42. current.rendererIndex = 0;
  43. textStyle.add(current);
  44. }
  45. TextField::TextStyleManager::~TextStyleManager()
  46. {
  47. if (renderer) renderer->release();
  48. if (text) text->release();
  49. }
  50. // Sets the style of a text section
  51. // begin: the start position of the section
  52. // end: the end position of the section (not included)
  53. void TextField::TextStyleManager::setTextStyle(
  54. int begin, int end, TextStyle style)
  55. {
  56. if (begin < 0 || begin > end || begin > text->getLength()) return;
  57. int sc = textStyle.getEntryCount();
  58. int index = -1;
  59. TextStyle s = textStyle.get(0);
  60. // search to the correct position in the style array
  61. for (int i = 0; i < sc; i++)
  62. {
  63. if (textStyle.get(i).beginIndex >= begin)
  64. {
  65. index = i;
  66. if (textStyle.get(i).beginIndex > begin)
  67. s = textStyle.get(i - 1);
  68. else
  69. {
  70. s = textStyle.get(i);
  71. textStyle.remove(i);
  72. sc--;
  73. }
  74. break;
  75. }
  76. }
  77. style.beginIndex = begin;
  78. s.beginIndex = end;
  79. if (index < 0)
  80. { // append styles at the end
  81. textStyle.add(style);
  82. textStyle.add(s);
  83. }
  84. else
  85. { // insert into the middle of the style array
  86. textStyle.add(style, index);
  87. for (int i = index + 1; i < sc + 1; i++)
  88. { // remove styles that were overwritten
  89. if (textStyle.get(i).beginIndex <= end
  90. && textStyle.get(i).beginIndex > begin)
  91. {
  92. s = textStyle.get(i);
  93. textStyle.remove(i);
  94. i--;
  95. sc--;
  96. }
  97. }
  98. s.beginIndex = end;
  99. textStyle.add(s, index + 1);
  100. }
  101. cleanupStyles();
  102. }
  103. // Removes a text section
  104. // begin: the index of the first affected character
  105. // end: the index of the first character after the section
  106. void TextField::TextStyleManager::removeText(int begin, int end)
  107. {
  108. int sc = textStyle.getEntryCount();
  109. for (int i = 1; i < sc; i++)
  110. {
  111. TextStyle s = textStyle.get(i);
  112. if (s.beginIndex >= begin && s.beginIndex < end)
  113. {
  114. textStyle.remove(i);
  115. i--;
  116. sc--;
  117. }
  118. if (s.beginIndex >= end)
  119. {
  120. s.beginIndex -= end - begin;
  121. textStyle.set(s, i);
  122. }
  123. }
  124. text->remove(begin, end);
  125. cleanupStyles();
  126. }
  127. // Inserts text at a specific position
  128. // pos: the position of the new text section
  129. // text: the new text
  130. void TextField::TextStyleManager::insertText(int pos, const char* text)
  131. {
  132. int len = textLength(text);
  133. this->text->insert(pos, text);
  134. int sc = textStyle.getEntryCount();
  135. for (int i = 0; i < sc; i++)
  136. {
  137. TextStyle s = textStyle.get(i);
  138. if (s.beginIndex > pos)
  139. {
  140. s.beginIndex += len;
  141. textStyle.set(s, i);
  142. }
  143. }
  144. cleanupStyles();
  145. }
  146. // Removes unnecessary duplicate styles
  147. void TextField::TextStyleManager::cleanupStyles()
  148. {
  149. int sc = textStyle.getEntryCount();
  150. TextStyle last = textStyle.get(0);
  151. for (int i = 1; i < sc; i++)
  152. {
  153. if (textStyle.get(i).beginIndex == last.beginIndex)
  154. {
  155. last = textStyle.get(i);
  156. textStyle.remove(i - 1);
  157. i--;
  158. sc--;
  159. continue;
  160. }
  161. if (textStyle.get(i).equals(last)
  162. || (text && textStyle.get(i).beginIndex > text->getLength()))
  163. {
  164. textStyle.remove(i);
  165. i--;
  166. sc--;
  167. }
  168. else
  169. last = textStyle.get(i);
  170. }
  171. }
  172. // returns a reference to the style object
  173. TextStyle& TextField::TextStyleManager::currentStyle()
  174. {
  175. return current;
  176. }
  177. // returns the current text renderer
  178. TextRenderer* TextField::TextStyleManager::zCurrentRenderer()
  179. {
  180. if (renderer->getEntryCount() == 0) return 0;
  181. TextRenderer* tr = renderer->z(current.rendererIndex);
  182. if (!tr) tr = renderer->z(0);
  183. if (tr) tr->setFontSize(current.fontSize);
  184. return tr;
  185. }
  186. // changes the content of the style object to the style of the next character
  187. bool TextField::TextStyleManager::nextStyle()
  188. {
  189. index++;
  190. if (textStyle.getEntryCount() > styleIndex + 1
  191. && index >= textStyle.get(styleIndex + 1).beginIndex)
  192. current = textStyle.get(styleIndex++ + 1);
  193. return text && index < text->getLength();
  194. }
  195. // changes the content of the style object to the style of the specified
  196. // character
  197. // index: the index of the character to jump to
  198. // returns 0 if the character does not exist
  199. bool TextField::TextStyleManager::stepTo(int index)
  200. {
  201. resetIteration();
  202. while (this->index < index && nextStyle())
  203. ;
  204. return text && this->index == index;
  205. }
  206. // changes the content of the style object to the style of the first character
  207. void TextField::TextStyleManager::resetIteration()
  208. {
  209. index = 0;
  210. styleIndex = 0;
  211. current = textStyle.get(0);
  212. }
  213. // Returns the style of a specific character\n// index: The index of the
  214. // character
  215. TextStyle TextField::TextStyleManager::getTextStyle(int index) const
  216. {
  217. TextStyle last = textStyle.get(0);
  218. int ind = 0;
  219. for (auto i = textStyle.begin(); i && ind <= index; ind++)
  220. {
  221. if (i._.beginIndex <= ind)
  222. {
  223. last = i;
  224. i++;
  225. }
  226. }
  227. return last;
  228. }
  229. // Contents of the TextField class from TextField.h
  230. // Constructor
  231. TextField::TextField()
  232. : DrawableBackground(),
  233. tm(new TextStyleManager()),
  234. autoLineBreakSpacing(0),
  235. showChar(0),
  236. cpos(0),
  237. tickVal(0),
  238. mouseKlick(0)
  239. {
  240. charEvent = 0;
  241. horizontalScrollBar = new HScrollBar();
  242. vertikalScrollBar = new VScrollBar();
  243. this->setMouseEvent(_ret1ME);
  244. this->setKeyboardEvent(_ret1TE);
  245. }
  246. // Destructor
  247. TextField::~TextField()
  248. {
  249. tm->release();
  250. if (autoLineBreakSpacing) autoLineBreakSpacing->release();
  251. }
  252. void TextField::doMouseEvent(MouseEvent& me, bool userRet) // Mouse event
  253. {
  254. if (!userRet) return;
  255. if (hasStyleNot(Style::Editable))
  256. {
  257. int rbr = 0;
  258. if (border) rbr = border->getRWidth();
  259. if (((vertikalScrollBar && hasStyle(Style::VScroll))
  260. || (horizontalScrollBar && hasStyle(Style::HScroll)))
  261. && me.mx > rbr && me.mx < gr.x - rbr && me.my > rbr
  262. && me.my < gr.y - rbr)
  263. {
  264. me.processed |= vertikalScrollBar->doMouseMessage(
  265. gr.x - rbr - 15, rbr, 15, gr.y - rbr * 2, me);
  266. me.processed |= horizontalScrollBar->doMouseMessage(rbr,
  267. gr.y - rbr * 2 - 15,
  268. gr.x - rbr * 2
  269. - ((vertikalScrollBar && hasStyle(Style::VScroll)) ? 15
  270. : 0),
  271. 15,
  272. me);
  273. }
  274. if (me.mx >= 0 && me.mx <= gr.x && me.my >= 0 && me.my <= gr.y
  275. && !me.processed && hasStyle(Style::Visible))
  276. {
  277. int scrollHi = (vertikalScrollBar && hasStyle(Style::VScroll))
  278. ? vertikalScrollBar->getScroll()
  279. : 0;
  280. int scrollBr = (horizontalScrollBar && hasStyle(Style::HScroll))
  281. ? horizontalScrollBar->getScroll()
  282. : 0;
  283. int xxx = me.mx - rbr + scrollBr;
  284. int yyy = me.my - rbr + scrollHi;
  285. int mouseChar = getTextIndexAt(xxx, yyy);
  286. if (mouseChar >= 0)
  287. {
  288. TextStyle s = tm->getTextStyle(mouseChar);
  289. if (charEvent && s.interactParam)
  290. charEvent(mouseChar, s.interactParam, me);
  291. }
  292. if (charEvent) me.processed = 1;
  293. }
  294. mouseKlick = 0;
  295. return;
  296. }
  297. if (!me.processed)
  298. {
  299. if (hasStyleNot(Style::Focus))
  300. {
  301. mouseKlick = 0;
  302. if (me.id == Framework::ME_PLeft) addStyle(Style::Focus);
  303. }
  304. int rbr = 0;
  305. if (border) rbr = border->getRWidth();
  306. if (vertikalScrollBar && hasStyle(Style::VScroll))
  307. {
  308. if (vertikalScrollBar->doMouseMessage(
  309. gr.x - rbr - 15, rbr, 15, gr.y - rbr * 2, me))
  310. {
  311. me.processed = 1;
  312. return;
  313. }
  314. }
  315. if (horizontalScrollBar && hasStyle(Style::HScroll))
  316. {
  317. if (horizontalScrollBar->doMouseMessage(rbr,
  318. gr.y - rbr - 15,
  319. gr.x - rbr * 2
  320. - ((vertikalScrollBar && hasStyle(Style::VScroll)) ? 15
  321. : 0),
  322. 15,
  323. me))
  324. {
  325. me.processed = 1;
  326. return;
  327. }
  328. }
  329. bool shift = getKeyState(T_Shift);
  330. bool strg = getKeyState(T_Strg);
  331. int tbr = getTextWidth();
  332. int thi = getTextHeight();
  333. int scrollHi = (vertikalScrollBar && hasStyle(Style::VScroll))
  334. ? vertikalScrollBar->getScroll()
  335. : 0;
  336. int scrollBr = (horizontalScrollBar && hasStyle(Style::HScroll))
  337. ? horizontalScrollBar->getScroll()
  338. : 0;
  339. int xxx = me.mx - rbr + scrollBr;
  340. int yyy = me.my - rbr + scrollHi;
  341. int mouseChar = getTextIndexAt(xxx, yyy);
  342. int scrollWidth = (vertikalScrollBar && hasStyle(Style::VScroll)) * 15;
  343. int scrollHeight
  344. = (horizontalScrollBar && hasStyle(Style::HScroll)) * 15;
  345. if (hasStyle(Style::HCenter))
  346. xxx -= (((gr.x - scrollWidth) / 2) - tbr / 2) - rbr;
  347. if (hasStyle(Style::VCenter) && hasStyleNot(Style::VScroll))
  348. yyy -= (((gr.y - scrollHeight) / 2) - thi / 2) - rbr;
  349. if (mouseChar >= 0)
  350. {
  351. TextStyle s = tm->getTextStyle(mouseChar);
  352. if (charEvent) charEvent(mouseChar, s.interactParam, me);
  353. }
  354. if (me.mx < gr.x - rbr - 15)
  355. {
  356. if (tm->renderer)
  357. {
  358. int ncpos = getCurserPosAt(xxx, yyy);
  359. if (me.id == Framework::ME_PLeft)
  360. {
  361. if (ncpos != -1)
  362. {
  363. if (shift && cpos != ncpos)
  364. addSelahl(MIN(cpos, ncpos), MAX(cpos, ncpos));
  365. else if (!shift && !mouseKlick && !strg)
  366. deselectSelahl();
  367. cpos = ncpos;
  368. rend = 1;
  369. if (vertikalScrollBar && hasStyle(Style::VScroll))
  370. updateVScroll();
  371. if (horizontalScrollBar && hasStyle(Style::HScroll))
  372. updateHScroll();
  373. }
  374. mouseKlick = 1;
  375. }
  376. if (me.id == ME_Move && mouseKlick)
  377. {
  378. if (ncpos != -1)
  379. {
  380. rend = 1;
  381. if (cpos != ncpos)
  382. invertSelection(MIN(cpos, ncpos), MAX(cpos, ncpos));
  383. cpos = ncpos;
  384. if (vertikalScrollBar && hasStyle(Style::VScroll))
  385. updateVScroll(cpos);
  386. if (horizontalScrollBar && hasStyle(Style::HScroll))
  387. updateHScroll(cpos);
  388. }
  389. }
  390. if (me.id == ME_RLeft)
  391. {
  392. if (ncpos != -1)
  393. {
  394. rend = 1;
  395. if (cpos != ncpos)
  396. invertSelection(MIN(cpos, ncpos), MAX(cpos, ncpos));
  397. cpos = ncpos;
  398. if (vertikalScrollBar && hasStyle(Style::VScroll))
  399. updateVScroll(cpos);
  400. if (horizontalScrollBar && hasStyle(Style::HScroll))
  401. updateHScroll(cpos);
  402. }
  403. mouseKlick = 0;
  404. }
  405. }
  406. }
  407. }
  408. me.processed = 1;
  409. }
  410. int TextField::getTextHeight() const
  411. {
  412. if (!tm || !tm->text) return 0;
  413. tm->resetIteration();
  414. int th = 0;
  415. int len = tm->text->getLength();
  416. const char* text = tm->text->getText();
  417. Text txtWithLineBreaks;
  418. if (hasStyle(Style::AutoLineBreak))
  419. {
  420. txtWithLineBreaks = addLineBreaksToText(tm->text->getText(),
  421. autoLineBreakSpacing ? autoLineBreakSpacing->getText() : "");
  422. text = txtWithLineBreaks;
  423. len = txtWithLineBreaks.getLength();
  424. }
  425. int max = 0;
  426. int distance = 0;
  427. for (int i = 0; i < len; i++)
  428. {
  429. if (text[i] == '\n')
  430. {
  431. th += max + distance;
  432. distance = 0;
  433. max = 0;
  434. tm->nextStyle();
  435. continue;
  436. }
  437. TextRenderer* r = tm->zCurrentRenderer();
  438. if (r)
  439. {
  440. int tmp = r->getRowHeight();
  441. max = max >= tmp ? max : tmp;
  442. if (max == tmp) distance = r->getLineSpacing();
  443. }
  444. tm->nextStyle();
  445. }
  446. if (max > 0) th += max;
  447. return th;
  448. }
  449. int TextField::getTextWidth() const
  450. {
  451. if (!tm || !tm->text) return 0;
  452. tm->resetIteration();
  453. int maxBr = 0;
  454. int len = tm->text->getLength();
  455. const char* text = tm->text->getText();
  456. Text txtWithLineBreaks;
  457. if (hasStyle(Style::AutoLineBreak))
  458. {
  459. txtWithLineBreaks = addLineBreaksToText(tm->text->getText(),
  460. autoLineBreakSpacing ? autoLineBreakSpacing->getText() : "");
  461. text = txtWithLineBreaks;
  462. len = txtWithLineBreaks.getLength();
  463. }
  464. int lineBr = 0;
  465. char buff[] = {0, 0};
  466. for (int i = 0; i < len; i++)
  467. {
  468. buff[0] = text[i];
  469. if (text[i] == '\n')
  470. {
  471. maxBr = maxBr >= lineBr ? maxBr : lineBr;
  472. lineBr = 0;
  473. tm->nextStyle();
  474. continue;
  475. }
  476. TextRenderer* r = tm->zCurrentRenderer();
  477. if (r) lineBr += r->getTextWidth(buff);
  478. tm->nextStyle();
  479. }
  480. if (lineBr > 0) maxBr = maxBr >= lineBr ? maxBr : lineBr;
  481. return maxBr;
  482. }
  483. // charEvent: a function that is called when the mouse is on a
  484. // specific character and the interactParam in the style != 0
  485. // call: charEvent( charIndex, interactParam, mouseEreignis );
  486. void TextField::setCharEvent(
  487. std::function<void(int, int, MouseEvent me)> charEvent)
  488. {
  489. this->charEvent = charEvent;
  490. }
  491. void TextField::setText(Text* txt) // sets the displayed text
  492. {
  493. rwLock.lockWrite();
  494. if (!tm->text) tm->text = new Text();
  495. tm->text->setText(*txt);
  496. if (hasStyle(Style::VScroll)) updateVScroll();
  497. if (hasStyle(Style::HScroll)) updateHScroll();
  498. rwLock.unlockWrite();
  499. rend = 1;
  500. txt->release();
  501. }
  502. void TextField::setTextZ(Text* txt) // sets a pointer to the displayed text
  503. {
  504. rwLock.lockWrite();
  505. if (tm->text) tm->text->release();
  506. tm->text = txt;
  507. if (hasStyle(Style::VScroll)) updateVScroll();
  508. if (hasStyle(Style::HScroll)) updateHScroll();
  509. rend = 1;
  510. rwLock.unlockWrite();
  511. }
  512. void TextField::setText(const char* txt) // sets the displayed text
  513. {
  514. rwLock.lockWrite();
  515. if (!tm->text) tm->text = new Text();
  516. tm->text->setText(txt);
  517. if (hasStyle(Style::VScroll)) updateVScroll();
  518. if (hasStyle(Style::HScroll)) updateHScroll();
  519. rend = 1;
  520. rwLock.unlockWrite();
  521. }
  522. // sets the text with styles
  523. // txt: the text
  524. // format: \x1: enables underline
  525. // \x2FF: sets the font size for the following text.
  526. // FF is a two-digit hex value
  527. // \x3AARRGGBB: sets the font color.
  528. // AARRGGBB is an 8-digit hex value with two characters each
  529. // for alpha, red, green and blue
  530. // \x4AARRGGBB: sets the color of the selected text.
  531. // AARRGGBB is an 8-digit hex value with two characters each
  532. // for alpha, red, green and blue
  533. // \x5AARRGGBB: sets the background color of the selected text.
  534. // AARRGGBB is an 8-digit hex value with two characters each
  535. // for alpha, red, green and blue
  536. // \x6FF: sets text renderer index.
  537. // FF is a two-digit hex value
  538. // \x7: deactivates underline
  539. // \x8FFFFFFFF: set interact param.
  540. // FFFFFFFF is an 8-digit hex value
  541. void TextField::setFormattedText(const char* txt)
  542. {
  543. rwLock.lockWrite();
  544. if (!tm->text) tm->text = new Text();
  545. tm->textStyle.clear();
  546. TextStyle current;
  547. current.beginIndex = 0;
  548. current.fontColor = 0xFFFFFFFF;
  549. current.fontSize = 12;
  550. current.selected = 0;
  551. current.selectedColor = 0xFFFFFFFF;
  552. current.selectedBackcroundColor = 0xFF0000FF;
  553. current.underlined = 0;
  554. current.interactParam = 0;
  555. current.rendererIndex = 0;
  556. tm->textStyle.add(current);
  557. Text result = "";
  558. for (int i = 0; 1; i++)
  559. {
  560. bool br = 0;
  561. current.beginIndex = result.getLength();
  562. switch (txt[i])
  563. {
  564. case 0:
  565. br = 1;
  566. break;
  567. case 1:
  568. current.underlined = 1;
  569. tm->textStyle.add(current);
  570. break;
  571. case 2:
  572. {
  573. Text p = "0x";
  574. p.append(txt + i + 1, 2);
  575. current.fontSize = (unsigned char)(int)p;
  576. tm->textStyle.add(current);
  577. i += 2;
  578. break;
  579. }
  580. case 3:
  581. {
  582. Text p = "0x";
  583. p.append(txt + i + 1, 8);
  584. current.fontColor = (int)p;
  585. tm->textStyle.add(current);
  586. i += 8;
  587. }
  588. break;
  589. case 4:
  590. {
  591. Text p = "0x";
  592. p.append(txt + i + 1, 8);
  593. current.selectedColor = (int)p;
  594. tm->textStyle.add(current);
  595. i += 8;
  596. break;
  597. }
  598. case 5:
  599. {
  600. Text p = "0x";
  601. p.append(txt + i + 1, 8);
  602. current.selectedBackcroundColor = (int)p;
  603. tm->textStyle.add(current);
  604. i += 8;
  605. break;
  606. }
  607. case 6:
  608. {
  609. Text p = "0x";
  610. p.append(txt + i + 1, 2);
  611. current.rendererIndex = (unsigned char)(int)p;
  612. tm->textStyle.add(current);
  613. i += 2;
  614. }
  615. break;
  616. case 7:
  617. current.underlined = 0;
  618. tm->textStyle.add(current);
  619. break;
  620. case 8:
  621. {
  622. Text p = "0x";
  623. p.append(txt + i + 1, 8);
  624. current.interactParam = (int)p;
  625. tm->textStyle.add(current);
  626. i += 8;
  627. }
  628. break;
  629. default:
  630. result.append(txt[i]);
  631. }
  632. if (br) break;
  633. }
  634. tm->text->setText(result);
  635. tm->cleanupStyles();
  636. if (hasStyle(Style::VScroll)) updateVScroll();
  637. if (hasStyle(Style::HScroll)) updateHScroll();
  638. rend = 1;
  639. rwLock.unlockWrite();
  640. }
  641. // inserts line breaks so that the text does not exceed the width of the text
  642. // field
  643. void TextField::addLineBreaks(const char* spacing)
  644. {
  645. if (!tm->text) return;
  646. setFormattedText(addLineBreaksToText(tm->text->getText(), spacing));
  647. }
  648. Text TextField::addLineBreaksToText(
  649. const char* txt, const char* spacing, bool includeFormat) const
  650. {
  651. int lastPos = -1;
  652. int lastPos2 = -1;
  653. int x = 0;
  654. Text result = "";
  655. int len = (int)strlen(txt);
  656. int maxBr = getWidth();
  657. if (hasStyle(Style::VScroll) && vertikalScrollBar) maxBr -= 15;
  658. tm->resetIteration();
  659. TextStyle last;
  660. last.beginIndex = 0;
  661. last.fontColor = 0xFFFFFFFF;
  662. last.fontSize = 12;
  663. last.selected = 0;
  664. last.selectedColor = 0xFFFFFFFF;
  665. last.selectedBackcroundColor = 0xFF0000FF;
  666. last.underlined = 0;
  667. last.interactParam = 0;
  668. last.rendererIndex = 0;
  669. for (int i = 0; i < len; ++i)
  670. {
  671. if (last.fontSize != tm->current.fontSize && includeFormat)
  672. {
  673. Text param("\x2");
  674. param.appendHex((char)tm->current.fontSize);
  675. result += param;
  676. last.fontSize = tm->current.fontSize;
  677. }
  678. if (last.fontColor != tm->current.fontColor && includeFormat)
  679. {
  680. Text param("\x3");
  681. param.appendHex(tm->current.fontColor);
  682. result += param;
  683. last.fontColor = tm->current.fontColor;
  684. }
  685. if (last.selectedColor != tm->current.selectedColor && includeFormat)
  686. {
  687. Text param("\x4");
  688. param.appendHex(tm->current.selectedColor);
  689. result += param;
  690. last.selectedColor = tm->current.selectedColor;
  691. }
  692. if (last.selectedBackcroundColor != tm->current.selectedBackcroundColor
  693. && includeFormat)
  694. {
  695. Text param("\x5");
  696. param.appendHex(tm->current.selectedBackcroundColor);
  697. result += param;
  698. last.selectedBackcroundColor = tm->current.selectedBackcroundColor;
  699. }
  700. if (last.underlined != tm->current.underlined && includeFormat)
  701. {
  702. char tmp[2] = {tm->current.underlined ? (char)1 : (char)7, 0};
  703. result += tmp;
  704. last.underlined = tm->current.underlined;
  705. }
  706. if (last.interactParam != tm->current.interactParam && includeFormat)
  707. {
  708. Text param("\x8");
  709. param.appendHex(tm->current.interactParam);
  710. result += param;
  711. last.interactParam = tm->current.interactParam;
  712. }
  713. if (last.rendererIndex != tm->current.rendererIndex && includeFormat)
  714. {
  715. Text param("\x6");
  716. param.appendHex((char)tm->current.rendererIndex);
  717. result += param;
  718. last.rendererIndex = tm->current.rendererIndex;
  719. }
  720. if (txt[i] == ' ')
  721. {
  722. lastPos = i;
  723. lastPos2 = result.getLength();
  724. x += tm->zCurrentRenderer()->getTextWidth(" ");
  725. result += " ";
  726. tm->nextStyle();
  727. continue;
  728. }
  729. if (txt[i] == '\t')
  730. {
  731. lastPos = i;
  732. lastPos2 = result.getLength();
  733. x += tm->zCurrentRenderer()->getTextWidth("\t");
  734. result += "\t";
  735. tm->nextStyle();
  736. continue;
  737. }
  738. if (txt[i] == '\n')
  739. {
  740. x = 0;
  741. lastPos = -1;
  742. lastPos2 = -1;
  743. result += "\n";
  744. tm->nextStyle();
  745. continue;
  746. }
  747. char buff[2] = {txt[i], 0};
  748. x += tm->zCurrentRenderer()->getTextWidth(buff);
  749. result += buff;
  750. if (x > maxBr && lastPos > -1)
  751. {
  752. result.remove(lastPos2, result.getLength());
  753. result += "\n";
  754. result += spacing;
  755. x = tm->zCurrentRenderer()->getTextWidth(spacing);
  756. i = lastPos;
  757. tm->stepTo(lastPos);
  758. lastPos = -1;
  759. lastPos2 = -1;
  760. last = tm->currentStyle();
  761. }
  762. tm->nextStyle();
  763. }
  764. return result;
  765. }
  766. void TextField::setAutoLineBreakSpacing(const char* spacing)
  767. {
  768. if (!autoLineBreakSpacing) autoLineBreakSpacing = new Text();
  769. autoLineBreakSpacing->setText(spacing);
  770. }
  771. // Sets the style of a text section
  772. // begin: the start position of the section
  773. // end: the end position of the section (not included)
  774. void TextField::setTextStyle(int begin, int end, TextStyle style)
  775. {
  776. tm->setTextStyle(begin, end, style);
  777. }
  778. void TextField::addRow(const char* zeile) // appends a line
  779. {
  780. if (tm->text)
  781. {
  782. Text* txt = new Text(zeile);
  783. if (zeile[txt->getLength() - 1] != '\n') txt->append("\n");
  784. TextRenderer* r = tm->renderer->z(0);
  785. if (tm->textStyle.get(tm->textStyle.getEntryCount() - 1).rendererIndex
  786. < tm->renderer->getEntryCount())
  787. r = tm->renderer->z(
  788. tm->textStyle.get(tm->textStyle.getEntryCount() - 1)
  789. .rendererIndex);
  790. if (r)
  791. {
  792. bool vs = vertikalScrollBar && hasStyle(Style::VScroll);
  793. int rbr
  794. = (border && hasStyle(Style::Border)) ? border->getRWidth() : 0;
  795. r->setFontSize(
  796. tm->textStyle.get(tm->textStyle.getEntryCount() - 1).fontSize);
  797. r->formatText(txt, gr.x - ((int)vs * 15) - rbr * 2);
  798. }
  799. rwLock.lockWrite();
  800. tm->text->append(txt->getText());
  801. rwLock.unlockWrite();
  802. txt->release();
  803. if (hasStyle(Style::VScroll)) updateVScroll();
  804. if (hasStyle(Style::HScroll)) updateHScroll();
  805. rend = 1;
  806. }
  807. }
  808. // Appends a line to the text
  809. // zeile: The new line
  810. // color: The color of the line
  811. void TextField::addRow(const char* zeile, int color)
  812. {
  813. if (tm->text)
  814. {
  815. Text* txt = new Text(zeile);
  816. if (zeile[txt->getLength() - 1] != '\n') txt->append("\n");
  817. TextRenderer* r = tm->renderer->z(0);
  818. if (tm->textStyle.get(tm->textStyle.getEntryCount() - 1).rendererIndex
  819. < tm->renderer->getEntryCount())
  820. r = tm->renderer->z(
  821. tm->textStyle.get(tm->textStyle.getEntryCount() - 1)
  822. .rendererIndex);
  823. if (r)
  824. {
  825. bool vs = vertikalScrollBar && hasStyle(Style::VScroll);
  826. int rbr
  827. = (border && hasStyle(Style::Border)) ? border->getRWidth() : 0;
  828. r->setFontSize(
  829. tm->textStyle.get(tm->textStyle.getEntryCount() - 1).fontSize);
  830. r->formatText(txt, gr.x - ((int)vs * 15) - rbr * 2);
  831. }
  832. rwLock.lockWrite();
  833. tm->text->append(txt->getText());
  834. setFontColor(tm->text->getLength() - txt->getLength(),
  835. tm->text->getLength(),
  836. color);
  837. rwLock.unlockWrite();
  838. txt->release();
  839. if (hasStyle(Style::VScroll)) updateVScroll();
  840. if (hasStyle(Style::HScroll)) updateHScroll();
  841. rend = 1;
  842. }
  843. }
  844. // Deselects all text sections
  845. void TextField::deselectSelahl()
  846. {
  847. for (int i = 0; i < tm->textStyle.getEntryCount(); i++)
  848. {
  849. TextStyle s = tm->textStyle.get(i);
  850. if (s.selected)
  851. {
  852. s.selected = 0;
  853. tm->textStyle.set(s, i);
  854. }
  855. }
  856. tm->cleanupStyles();
  857. }
  858. void TextField::setSelection(int pos1, int pos2) // sets the selected text
  859. {
  860. deselectSelahl();
  861. TextStyle s = tm->getTextStyle(pos1);
  862. s.selected = 1;
  863. tm->setTextStyle(pos1, pos2, s);
  864. }
  865. void TextField::setSelection(Point& selection)
  866. {
  867. deselectSelahl();
  868. TextStyle s = tm->getTextStyle(selection.x);
  869. s.selected = 1;
  870. tm->setTextStyle(selection.x, selection.y, s);
  871. rend = 1;
  872. }
  873. void TextField::addSelahl(int pos1, int pos2)
  874. {
  875. TextStyle s = tm->getTextStyle(pos1);
  876. s.selected = 1;
  877. tm->setTextStyle(pos1, pos2, s);
  878. }
  879. void TextField::addSelahl(Point& selection)
  880. {
  881. TextStyle s = tm->getTextStyle(selection.x);
  882. s.selected = 1;
  883. tm->setTextStyle(selection.x, selection.y, s);
  884. rend = 1;
  885. }
  886. // Sets the selected text section
  887. // begin: The cursor position in the text
  888. // end: The position in the text up to which the text should be colored
  889. void TextField::invertSelection(int begin, int end)
  890. {
  891. for (int i = begin; i < end; i++)
  892. {
  893. TextStyle s = tm->getTextStyle(i);
  894. s.selected = !s.selected;
  895. tm->setTextStyle(i, i + 1, s);
  896. rend = 1;
  897. }
  898. }
  899. // replaces all selected text sections with a text
  900. // text: the new text
  901. void TextField::replaceSelection(const char* text)
  902. {
  903. tm->cleanupStyles();
  904. int sa = tm->textStyle.getEntryCount();
  905. int last = tm->text->getLength();
  906. int si = 0;
  907. for (int i = sa - 1; i >= 0; i--)
  908. {
  909. TextStyle s = tm->textStyle.get(i);
  910. si = i;
  911. if (s.selected)
  912. {
  913. if ((i > 0 && !tm->textStyle.get(i - 1).selected) || i == 0)
  914. {
  915. s.selected = false;
  916. tm->textStyle.set(s, si);
  917. tm->removeText(s.beginIndex, last);
  918. tm->insertText(s.beginIndex, text);
  919. }
  920. }
  921. else
  922. last = s.beginIndex;
  923. }
  924. }
  925. void TextField::setTextRendererZ(TextRenderer* textRd)
  926. {
  927. if (tm->renderer)
  928. tm->renderer->clear();
  929. else
  930. tm->renderer = new RCArray<TextRenderer>();
  931. tm->renderer->add(textRd);
  932. rend = 1;
  933. }
  934. // Adds a TextRenderer
  935. // textRd: The text renderer
  936. void TextField::addTextRendererZ(TextRenderer* textRd)
  937. {
  938. if (!tm->renderer) tm->renderer = new RCArray<TextRenderer>();
  939. tm->renderer->add(textRd);
  940. }
  941. // Sets the used TextRenderers
  942. // textRd: The text renderers
  943. void TextField::setTextRendererZ(RCArray<TextRenderer>* textRd)
  944. {
  945. if (tm->renderer) tm->renderer->release();
  946. tm->renderer = textRd;
  947. }
  948. void TextField::setFontZ(Font* font) // sets a pointer to the font
  949. {
  950. if (!tm->renderer) tm->renderer = new RCArray<TextRenderer>();
  951. if (!tm->renderer->getEntryCount())
  952. tm->renderer->add(new TextRenderer(font));
  953. else
  954. tm->renderer->z(0)->setFontZ(font);
  955. rend = 1;
  956. }
  957. // Sets a pointer to the font
  958. // rendererIndex: The index of the renderer whose font should be set
  959. // font: The font to be used for text drawing.
  960. void TextField::setFontZ(int rendererIndex, Font* font)
  961. {
  962. if (!tm->renderer) tm->renderer = new RCArray<TextRenderer>();
  963. if (tm->renderer->getEntryCount() <= rendererIndex)
  964. tm->renderer->add(new TextRenderer(font), rendererIndex);
  965. else
  966. tm->renderer->z(rendererIndex)->setFontZ(font);
  967. }
  968. void TextField::setFontSize(unsigned char gr) // sets the font size
  969. {
  970. TextStyle s = tm->textStyle.get(0);
  971. s.fontSize = gr;
  972. tm->textStyle.set(s, 0);
  973. rend = 1;
  974. }
  975. // Sets the font size (default: 12)
  976. // begin: The index of the first affected character
  977. // end: The index of the first unaffected character
  978. // gr: The font size to be used for text drawing
  979. void TextField::setFontSize(int begin, int end, unsigned char gr)
  980. {
  981. TextStyle s = tm->getTextStyle(begin);
  982. s.fontSize = gr;
  983. tm->setTextStyle(begin, end, s);
  984. rend = 1;
  985. }
  986. void TextField::setFontColor(int fc) // sets the font color
  987. {
  988. TextStyle s = tm->textStyle.get(0);
  989. s.fontColor = fc;
  990. tm->textStyle.set(s, 0);
  991. rend = 1;
  992. }
  993. // Sets the font color
  994. // begin: The index of the first affected character
  995. // end: The index of the first unaffected character
  996. // fc: The color to be used for text drawing
  997. void TextField::setFontColor(int begin, int end, int fc)
  998. {
  999. TextStyle s = tm->getTextStyle(begin);
  1000. s.fontColor = fc;
  1001. tm->setTextStyle(begin, end, s);
  1002. rend = 1;
  1003. }
  1004. void TextField::setSchowChar(unsigned char c) // for password field *
  1005. {
  1006. showChar = c;
  1007. rend = 1;
  1008. }
  1009. void TextField::setVScrollToRow(int zeile) // scrolls to line
  1010. {
  1011. if (vertikalScrollBar && tm->renderer && tm->renderer->getEntryCount()
  1012. && tm->text && hasStyle(Style::Multiline))
  1013. {
  1014. rwLock.lockWrite();
  1015. tm->resetIteration();
  1016. int len = tm->text->getLength();
  1017. int y = 0;
  1018. int lnum = 0;
  1019. const char* text = tm->text->getText();
  1020. Text txtWithLineBreaks;
  1021. if (hasStyle(Style::AutoLineBreak))
  1022. {
  1023. txtWithLineBreaks = addLineBreaksToText(tm->text->getText(),
  1024. autoLineBreakSpacing ? autoLineBreakSpacing->getText() : "");
  1025. text = txtWithLineBreaks;
  1026. len = txtWithLineBreaks.getLength();
  1027. }
  1028. int max = 0;
  1029. for (int i = 0; i < len && lnum < zeile; i++)
  1030. {
  1031. if (text[i] == '\n')
  1032. {
  1033. lnum++;
  1034. y += max;
  1035. max = 0;
  1036. tm->nextStyle();
  1037. continue;
  1038. }
  1039. TextRenderer* r = tm->zCurrentRenderer();
  1040. if (r)
  1041. {
  1042. int tmp = r->getLineSpacing() + r->getRowHeight();
  1043. max = max >= tmp ? max : tmp;
  1044. }
  1045. tm->nextStyle();
  1046. }
  1047. rwLock.unlockWrite();
  1048. vertikalScrollBar->scroll(y);
  1049. rend = 1;
  1050. }
  1051. }
  1052. void TextField::updateVScroll(int pos) // scrolls down
  1053. {
  1054. if (pos == -1) pos = cpos;
  1055. if (vertikalScrollBar)
  1056. {
  1057. int sPos = 0;
  1058. int hi = 0;
  1059. int sPosZH = 0;
  1060. if (tm->text && tm->renderer)
  1061. {
  1062. if (hasStyleNot(Style::Multiline)) tm->text->remove('\n');
  1063. hi = gr.y;
  1064. if (hasStyle(Style::Border) && border)
  1065. hi -= border->getRWidth() * 2;
  1066. if (hasStyle(Style::HScroll) && horizontalScrollBar) hi -= 15;
  1067. int th = 0;
  1068. rwLock.lockWrite();
  1069. tm->resetIteration();
  1070. int len = tm->text->getLength();
  1071. const char* text = tm->text->getText();
  1072. Text txtWithLineBreaks;
  1073. if (hasStyle(Style::AutoLineBreak))
  1074. {
  1075. txtWithLineBreaks = addLineBreaksToText(tm->text->getText(),
  1076. autoLineBreakSpacing ? autoLineBreakSpacing->getText()
  1077. : "");
  1078. text = txtWithLineBreaks;
  1079. len = txtWithLineBreaks.getLength();
  1080. }
  1081. int max = 0;
  1082. int lastMax = 0;
  1083. for (int i = 0; i < len; i++)
  1084. {
  1085. if (text[i] == '\n')
  1086. {
  1087. if (i <= pos)
  1088. {
  1089. sPos += max;
  1090. sPosZH = max;
  1091. }
  1092. th += max;
  1093. lastMax = max;
  1094. max = 0;
  1095. tm->nextStyle();
  1096. continue;
  1097. }
  1098. TextRenderer* r = tm->zCurrentRenderer();
  1099. if (r)
  1100. {
  1101. int tmp = r->getLineSpacing() + r->getRowHeight();
  1102. max = max >= tmp ? max : tmp;
  1103. }
  1104. tm->nextStyle();
  1105. }
  1106. if (max != lastMax && max > 0)
  1107. {
  1108. th += max;
  1109. lastMax = max;
  1110. }
  1111. th += lastMax;
  1112. rwLock.unlockWrite();
  1113. vertikalScrollBar->update(th, hi);
  1114. }
  1115. if (sPos - sPosZH < vertikalScrollBar->getScroll())
  1116. vertikalScrollBar->scroll(sPos - sPosZH);
  1117. if (sPos + sPosZH > vertikalScrollBar->getScroll()
  1118. + vertikalScrollBar->getScrollData()->anzeige)
  1119. vertikalScrollBar->scroll(sPos + sPosZH * 2 - hi);
  1120. rend = 1;
  1121. }
  1122. }
  1123. void TextField::updateHScroll(int pos) // scrolls to cursor position
  1124. {
  1125. if (pos == -1) pos = cpos;
  1126. rwLock.lockWrite();
  1127. if (horizontalScrollBar && tm->text && tm->renderer)
  1128. {
  1129. if (hasStyleNot(Style::Multiline)) tm->text->remove('\n');
  1130. int br = gr.x;
  1131. if (hasStyle(Style::Border) && border) br -= border->getRWidth() * 2;
  1132. if (hasStyle(Style::VScroll) && vertikalScrollBar) br -= 15;
  1133. tm->resetIteration();
  1134. int maxBr = 0;
  1135. int len = tm->text->getLength();
  1136. const char* text = tm->text->getText();
  1137. int lineBr = 0;
  1138. char buff[] = {0, 0};
  1139. int cbr = 0;
  1140. for (int i = 0; i < len; i++)
  1141. {
  1142. buff[0] = text[i];
  1143. if (text[i] == '\n')
  1144. {
  1145. maxBr = maxBr >= lineBr ? maxBr : lineBr;
  1146. lineBr = 0;
  1147. tm->nextStyle();
  1148. continue;
  1149. }
  1150. TextRenderer* r = tm->zCurrentRenderer();
  1151. if (r)
  1152. {
  1153. lineBr += r->getTextWidth(buff);
  1154. if (i <= pos) cbr = lineBr;
  1155. }
  1156. tm->nextStyle();
  1157. }
  1158. maxBr = maxBr >= lineBr ? maxBr : lineBr;
  1159. horizontalScrollBar->update(maxBr, br);
  1160. if (cbr > horizontalScrollBar->getScroll()
  1161. + horizontalScrollBar->getScrollData()->anzeige)
  1162. horizontalScrollBar->scroll(cbr - br);
  1163. if (cbr < horizontalScrollBar->getScroll())
  1164. horizontalScrollBar->scroll(cbr);
  1165. }
  1166. rwLock.unlockWrite();
  1167. }
  1168. // Returns the width in pixels needed to fully display the current text with
  1169. // the current styles
  1170. int TextField::getNeededWidth()
  1171. {
  1172. int maxBr = 0;
  1173. rwLock.lockRead();
  1174. if (tm->text && tm->renderer) maxBr = getTextWidth();
  1175. rwLock.unlockRead();
  1176. bool vs = vertikalScrollBar && hasStyle(Style::VScroll);
  1177. bool r = border && hasStyle(Style::Border);
  1178. return maxBr + (r ? border->getRWidth() * 2 : 0) + (vs ? 15 : 0);
  1179. }
  1180. // Returns the height in pixels needed to fully display the current text with
  1181. // the current styles
  1182. int TextField::getNeededHeight()
  1183. {
  1184. int th = 0;
  1185. rwLock.lockRead();
  1186. if (tm->text && tm->renderer) th = getTextHeight();
  1187. rwLock.unlockRead();
  1188. bool hs = horizontalScrollBar && hasStyle(Style::HScroll);
  1189. bool r = border && hasStyle(Style::Border);
  1190. return th + (r ? border->getRWidth() * 2 : 0) + (hs ? 15 : 0);
  1191. }
  1192. bool TextField::tick(double tickval) // tick
  1193. {
  1194. if (hasStyle(Style::Focus))
  1195. {
  1196. if (tickVal < 0.5 && tickVal + tickval >= 0.5) rend = 1;
  1197. if (tickVal >= 0.5 && tickVal + tickval >= 1) rend = 1;
  1198. tickVal += tickval;
  1199. if (tickVal >= 1) tickVal -= 1;
  1200. }
  1201. return DrawableBackground::tick(tickval);
  1202. }
  1203. void TextField::doKeyboardEvent(KeyboardEvent& te)
  1204. {
  1205. bool ntakc = !te.processed;
  1206. if (te.processed || hasStyleNot(Style::Focus)) return;
  1207. if (!tak) return;
  1208. getThis();
  1209. if (tak(takParam, this, te))
  1210. {
  1211. if (hasStyleNot(Style::Editable))
  1212. {
  1213. release();
  1214. return;
  1215. }
  1216. if (te.id == TE_Press)
  1217. {
  1218. bool shift = getKeyState(T_Shift);
  1219. bool strg = getKeyState(T_Strg);
  1220. switch (te.virtualKey)
  1221. {
  1222. case T_Entf:
  1223. if (!tm->getTextStyle(cpos).selected)
  1224. tm->removeText(cpos, cpos + 1);
  1225. else
  1226. {
  1227. cpos = tm->getTextStyle(cpos).beginIndex;
  1228. while (cpos > 0 && tm->getTextStyle(cpos - 1).selected)
  1229. cpos = tm->getTextStyle(cpos - 1).beginIndex;
  1230. }
  1231. replaceSelection("");
  1232. deselectSelahl();
  1233. rend = 1;
  1234. break;
  1235. case T_BackSpace:
  1236. if (!tm->getTextStyle(cpos).selected)
  1237. {
  1238. tm->removeText(cpos - 1, cpos);
  1239. cpos--;
  1240. }
  1241. else
  1242. {
  1243. cpos = tm->getTextStyle(cpos).beginIndex;
  1244. while (cpos > 0 && tm->getTextStyle(cpos - 1).selected)
  1245. cpos = tm->getTextStyle(cpos - 1).beginIndex;
  1246. }
  1247. replaceSelection("");
  1248. deselectSelahl();
  1249. rend = 1;
  1250. break;
  1251. case T_Enter:
  1252. if (hasStyle(TextField::Style::Multiline))
  1253. {
  1254. if (!tm->getTextStyle(cpos).selected)
  1255. tm->insertText(cpos, "\n");
  1256. else
  1257. {
  1258. cpos = tm->getTextStyle(cpos).beginIndex;
  1259. while (cpos > 0 && tm->getTextStyle(cpos - 1).selected)
  1260. cpos = tm->getTextStyle(cpos - 1).beginIndex;
  1261. }
  1262. replaceSelection("\n");
  1263. ++cpos;
  1264. rend = 1;
  1265. }
  1266. break;
  1267. case T_Links:
  1268. if (shift)
  1269. {
  1270. if (strg)
  1271. {
  1272. int tmp = tm->text->getLKick(cpos);
  1273. invertSelection(tmp, cpos);
  1274. cpos = tmp;
  1275. }
  1276. else
  1277. {
  1278. invertSelection(cpos - 1, cpos);
  1279. --cpos;
  1280. }
  1281. }
  1282. else
  1283. {
  1284. if (strg)
  1285. cpos = tm->text->getLKick(cpos);
  1286. else
  1287. --cpos;
  1288. deselectSelahl();
  1289. }
  1290. rend = 1;
  1291. break;
  1292. case T_Oben:
  1293. {
  1294. int tmp = tm->text->getOKick(cpos);
  1295. invertSelection(tmp, cpos);
  1296. cpos = tmp;
  1297. if (!shift) deselectSelahl();
  1298. rend = 1;
  1299. break;
  1300. }
  1301. case T_Rechts:
  1302. if (shift)
  1303. {
  1304. if (strg)
  1305. {
  1306. int tmp = tm->text->getRKick(cpos);
  1307. invertSelection(cpos, tmp);
  1308. cpos = tmp;
  1309. }
  1310. else
  1311. {
  1312. invertSelection(cpos, cpos + 1);
  1313. ++cpos;
  1314. }
  1315. }
  1316. else
  1317. {
  1318. if (strg)
  1319. cpos = tm->text->getRKick(cpos);
  1320. else
  1321. ++cpos;
  1322. deselectSelahl();
  1323. }
  1324. rend = 1;
  1325. break;
  1326. case T_Unten:
  1327. {
  1328. int tmp = tm->text->getUKick(cpos);
  1329. invertSelection(cpos, tmp);
  1330. cpos = tmp;
  1331. if (!shift) deselectSelahl();
  1332. rend = 1;
  1333. break;
  1334. }
  1335. default:
  1336. if (strg && te.id == TE_Press)
  1337. {
  1338. if (te.virtualKey == 'c' || te.virtualKey == 'C')
  1339. {
  1340. int sa = tm->textStyle.getEntryCount();
  1341. int length = 0;
  1342. for (int i = 0; i < sa; i++)
  1343. {
  1344. TextStyle s = tm->textStyle.get(i);
  1345. if (s.selected)
  1346. {
  1347. int max = tm->text->getLength();
  1348. if (i < sa - 1)
  1349. max = tm->textStyle.get(i + 1).beginIndex;
  1350. length += max - s.beginIndex;
  1351. }
  1352. }
  1353. if (length)
  1354. {
  1355. char* txt = new char[length + 1];
  1356. txt[length] = 0;
  1357. int index = 0;
  1358. for (int i = 0; i < sa; i++)
  1359. {
  1360. TextStyle s = tm->textStyle.get(i);
  1361. if (s.selected)
  1362. {
  1363. int max = tm->text->getLength();
  1364. if (i < sa - 1)
  1365. max = tm->textStyle.get(i + 1)
  1366. .beginIndex;
  1367. memcpy(txt + index,
  1368. tm->text->getText() + s.beginIndex,
  1369. max - s.beginIndex);
  1370. index += max - s.beginIndex;
  1371. }
  1372. }
  1373. TextKopieren(txt);
  1374. delete[] txt;
  1375. }
  1376. else
  1377. TextKopieren(tm->text->getText());
  1378. }
  1379. if (te.virtualKey == 'v' || te.virtualKey == 'V')
  1380. {
  1381. const char* txt = TextInsert();
  1382. if (!tm->getTextStyle(cpos).selected)
  1383. tm->insertText(cpos, txt);
  1384. else
  1385. {
  1386. cpos = tm->getTextStyle(cpos).beginIndex;
  1387. while (
  1388. cpos > 0 && tm->getTextStyle(cpos - 1).selected)
  1389. cpos = tm->getTextStyle(cpos - 1).beginIndex;
  1390. }
  1391. replaceSelection(txt);
  1392. cpos += textLength(txt);
  1393. rend = 1;
  1394. }
  1395. break;
  1396. }
  1397. if (isWritable(te.key[0]))
  1398. {
  1399. if (!tm->getTextStyle(cpos).selected)
  1400. tm->insertText(cpos, te.key);
  1401. else
  1402. {
  1403. cpos = tm->getTextStyle(cpos).beginIndex;
  1404. while (cpos > 0 && tm->getTextStyle(cpos - 1).selected)
  1405. cpos = tm->getTextStyle(cpos - 1).beginIndex;
  1406. }
  1407. replaceSelection(te.key);
  1408. ++cpos;
  1409. rend = 1;
  1410. }
  1411. break;
  1412. }
  1413. }
  1414. if (cpos < 0) cpos = 0;
  1415. if (cpos > tm->text->getLength()) cpos = tm->text->getLength();
  1416. if (hasStyle(Style::VScroll)) updateVScroll(cpos);
  1417. if (hasStyle(Style::HScroll)) updateHScroll(cpos);
  1418. te.processed = 1;
  1419. }
  1420. if (ntakc && te.processed && nTak) te.processed = nTak(ntakParam, this, te);
  1421. release();
  1422. }
  1423. void TextField::render(Image& zRObj) // draws to zRObj
  1424. {
  1425. if (hasStyleNot(Style::Visible)) return;
  1426. DrawableBackground::render(zRObj);
  1427. if (!tm->text || !tm->renderer) return;
  1428. rwLock.lockRead();
  1429. if (!zRObj.setDrawOptions(innenPosition, innenSize))
  1430. {
  1431. rwLock.unlockRead();
  1432. return;
  1433. }
  1434. if (hasStyleNot(Style::Multiline)) tm->text->remove('\n');
  1435. int tbr = getTextWidth();
  1436. int thi = getTextHeight();
  1437. int xxx = 0;
  1438. int yyy = 0;
  1439. int Width = innenSize.x;
  1440. int height = innenSize.y;
  1441. bool hs = horizontalScrollBar && hasStyle(Style::HScroll);
  1442. bool vs = vertikalScrollBar && hasStyle(Style::VScroll);
  1443. if (vs) yyy -= vertikalScrollBar->getScroll();
  1444. if (hs) xxx -= horizontalScrollBar->getScroll();
  1445. if (hasStyle(Style::HCenter) && !hs) xxx = (Width / 2) - tbr / 2;
  1446. int x = xxx;
  1447. int len = tm->text->getLength();
  1448. const char* text = tm->text->getText();
  1449. Text txtWithLineBreaks;
  1450. if (hasStyle(Style::AutoLineBreak))
  1451. {
  1452. txtWithLineBreaks = addLineBreaksToText(tm->text->getText(),
  1453. autoLineBreakSpacing ? autoLineBreakSpacing->getText() : "",
  1454. 0);
  1455. text = txtWithLineBreaks;
  1456. len = txtWithLineBreaks.getLength();
  1457. }
  1458. tm->resetIteration();
  1459. if (thi == 0)
  1460. {
  1461. thi = tm->zCurrentRenderer()->getRowHeight();
  1462. }
  1463. if (hasStyle(Style::VCenter) && !vs) yyy = (height / 2) - thi / 2;
  1464. int y = yyy;
  1465. TextStyle& style = tm->currentStyle();
  1466. int maxLH = 0;
  1467. int corrector = 0;
  1468. int realLen = tm->text->getLength();
  1469. for (int i = 0; i <= len; i++)
  1470. {
  1471. int oldX = x;
  1472. if (i < len && tm->zCurrentRenderer())
  1473. tm->zCurrentRenderer()->renderChar(x,
  1474. y,
  1475. isWritable(showChar) ? showChar : text[i],
  1476. zRObj,
  1477. style.selected ? style.selectedColor : style.fontColor,
  1478. style.underlined,
  1479. style.selected,
  1480. style.selectedBackcroundColor);
  1481. if (i - corrector == cpos && tickVal <= 0.5 && hasStyle(Style::Focus)
  1482. && hasStyle(Style::Editable) && tm->zCurrentRenderer())
  1483. zRObj.drawLineV(
  1484. oldX, y, tm->zCurrentRenderer()->getRowHeight(), 0xFFFF5555);
  1485. if (i >= realLen || text[i] != tm->text->getText()[i])
  1486. {
  1487. corrector++;
  1488. }
  1489. if (tm->zCurrentRenderer())
  1490. {
  1491. int tmp = tm->zCurrentRenderer()->getRowHeight()
  1492. + tm->zCurrentRenderer()->getLineSpacing();
  1493. maxLH = tmp > maxLH ? tmp : maxLH;
  1494. }
  1495. if (i < len && text[i] == '\n')
  1496. {
  1497. x = xxx;
  1498. y += maxLH;
  1499. }
  1500. tm->nextStyle();
  1501. }
  1502. zRObj.releaseDrawOptions();
  1503. rwLock.unlockRead();
  1504. }
  1505. // Constant
  1506. Text* TextField::getText() const // returns the text
  1507. {
  1508. if (!tm->text) return 0;
  1509. return dynamic_cast<Text*>(tm->text->getThis());
  1510. }
  1511. Text* TextField::zText() const // returns the text without reference counter
  1512. {
  1513. return tm->text;
  1514. }
  1515. Font* TextField::getFont() const // returns the font
  1516. {
  1517. tm->resetIteration();
  1518. return tm->zCurrentRenderer() ? tm->zCurrentRenderer()->getFont() : 0;
  1519. }
  1520. Font* TextField::zFont() const // returns the font without reference counter
  1521. {
  1522. tm->resetIteration();
  1523. return tm->zCurrentRenderer() ? tm->zCurrentRenderer()->zFont() : 0;
  1524. }
  1525. // Returns the font.
  1526. // rendererIndex: The index of the renderer whose font should be returned
  1527. // return: 0, if the font was not set
  1528. Font* TextField::getFont(int rendererIndex) const
  1529. {
  1530. if (tm->renderer && tm->renderer->z(rendererIndex))
  1531. return tm->renderer->z(rendererIndex)->getFont();
  1532. return 0;
  1533. }
  1534. // Returns the font without increased reference counter
  1535. // rendererIndex: The index of the renderer whose font should be returned
  1536. // return: 0, if the font was not set
  1537. Font* TextField::zFont(int rendererIndex) const
  1538. {
  1539. if (tm->renderer && tm->renderer->z(rendererIndex))
  1540. return tm->renderer->z(rendererIndex)->zFont();
  1541. return 0;
  1542. }
  1543. TextRenderer* TextField::getTextRenderer() const
  1544. {
  1545. tm->resetIteration();
  1546. return dynamic_cast<TextRenderer*>(tm->zCurrentRenderer()->getThis());
  1547. }
  1548. TextRenderer* TextField::zTextRenderer() const
  1549. {
  1550. tm->resetIteration();
  1551. return tm->zCurrentRenderer();
  1552. }
  1553. // Returns the TextRenderer.
  1554. // index: The index of the renderer to be returned
  1555. // return: 0, if the TextRenderer was not set
  1556. TextRenderer* TextField::getTextRenderer(int index) const
  1557. {
  1558. if (tm->renderer && tm->renderer->z(index)) return tm->renderer->get(index);
  1559. return 0;
  1560. }
  1561. // Returns the TextRenderer without increased reference counter
  1562. // index: The index of the renderer to be returned
  1563. // return: 0, if the TextRenderer was not set
  1564. TextRenderer* TextField::zTextRenderer(int index) const
  1565. {
  1566. if (tm->renderer && tm->renderer->z(index)) return tm->renderer->z(index);
  1567. return 0;
  1568. }
  1569. unsigned char TextField::getFontSize() const // returns the font size
  1570. {
  1571. tm->resetIteration();
  1572. return tm->current.fontSize;
  1573. }
  1574. // Returns the font size
  1575. // index: The index of the character
  1576. unsigned char TextField::getFontSize(int index) const
  1577. {
  1578. tm->resetIteration();
  1579. return tm->current.fontSize;
  1580. }
  1581. int TextField::getFontColor() const // returns the font color
  1582. {
  1583. tm->resetIteration();
  1584. return tm->current.fontColor;
  1585. }
  1586. // Returns the font color in A8R8G8B8 format
  1587. // index: The index of the character
  1588. int TextField::getFontColor(int index) const
  1589. {
  1590. return tm->getTextStyle(index).fontColor;
  1591. }
  1592. unsigned char TextField::getShowChar() const // returns the display character
  1593. {
  1594. return showChar;
  1595. }
  1596. int TextField::getCursorPos() const
  1597. {
  1598. return cpos;
  1599. }
  1600. // Returns 1 if the character is selected
  1601. // index: The index of the character
  1602. bool TextField::isCharSelected(int index) const
  1603. {
  1604. return tm->getTextStyle(index).selected;
  1605. }
  1606. // Returns the index of the character located under the mouse
  1607. // mx: the x position of the mouse relative to the text field position
  1608. // my: the y position of the mouse relative to the text field position
  1609. // return: -1, if no character is at the position
  1610. int TextField::getTextIndexAt(int mx, int my) const
  1611. {
  1612. if (!tm || !tm->text || !tm->zCurrentRenderer()) return -1;
  1613. int tbr = getTextWidth();
  1614. int thi = getTextHeight();
  1615. int xxx = 0;
  1616. int yyy = 0;
  1617. int Width = innenSize.x;
  1618. int height = innenSize.y;
  1619. bool hs = horizontalScrollBar && hasStyle(Style::HScroll);
  1620. bool vs = vertikalScrollBar && hasStyle(Style::VScroll);
  1621. if (vs) yyy -= vertikalScrollBar->getScroll();
  1622. if (hs) xxx -= horizontalScrollBar->getScroll();
  1623. if (hasStyle(Style::HCenter) && !hs) xxx = (Width / 2) - tbr / 2;
  1624. if (hasStyle(Style::VCenter) && !vs) yyy = (height / 2) - thi / 2;
  1625. int x = xxx;
  1626. int y = yyy;
  1627. int len = tm->text->getLength();
  1628. const char* text = tm->text->getText();
  1629. Text txtWithLineBreaks;
  1630. if (hasStyle(Style::AutoLineBreak))
  1631. {
  1632. txtWithLineBreaks = addLineBreaksToText(tm->text->getText(),
  1633. autoLineBreakSpacing ? autoLineBreakSpacing->getText() : "");
  1634. text = txtWithLineBreaks;
  1635. len = txtWithLineBreaks.getLength();
  1636. }
  1637. tm->resetIteration();
  1638. int corrector = 0;
  1639. int maxLH = 0;
  1640. int realLen = tm->text->getLength();
  1641. for (int i = 0; i < len; i++)
  1642. {
  1643. if (i >= realLen || text[i] != tm->text->getText()[i])
  1644. {
  1645. corrector++;
  1646. }
  1647. char buff[2] = {isWritable(showChar) ? (char)showChar : text[i], 0};
  1648. int tmpx = tm->zCurrentRenderer()->getTextWidth(buff);
  1649. int tmpy = tm->zCurrentRenderer()->getRowHeight();
  1650. if (mx >= x && mx < x + tmpx && my >= y && my < y + tmpy)
  1651. return i - corrector;
  1652. if (mx < x + tmpx && my < y + tmpy) return -1;
  1653. x += tmpx;
  1654. tmpy += tm->zCurrentRenderer()->getLineSpacing();
  1655. maxLH = tmpy > maxLH ? tmpy : maxLH;
  1656. if (text[i] == '\n')
  1657. {
  1658. x = xxx;
  1659. y += maxLH;
  1660. }
  1661. tm->nextStyle();
  1662. }
  1663. return -1;
  1664. }
  1665. // Returns the index of the character before which the cursor is placed when
  1666. // the mouse is clicked
  1667. // mx: the x position of the mouse relative to the text field position
  1668. // my: the y position of the mouse relative to the text field position
  1669. int TextField::getCurserPosAt(int mx, int my) const
  1670. {
  1671. if (!tm || !tm->text || !tm->zCurrentRenderer())
  1672. return tm->text->getLength();
  1673. int tbr = getTextWidth();
  1674. int thi = getTextHeight();
  1675. int xxx = 0;
  1676. int yyy = 0;
  1677. int Width = innenSize.x;
  1678. int height = innenSize.y;
  1679. bool hs = horizontalScrollBar && hasStyle(Style::HScroll);
  1680. bool vs = vertikalScrollBar && hasStyle(Style::VScroll);
  1681. if (vs) yyy -= vertikalScrollBar->getScroll();
  1682. if (hs) xxx -= horizontalScrollBar->getScroll();
  1683. if (hasStyle(Style::HCenter) && !hs) xxx = (Width / 2) - tbr / 2;
  1684. if (hasStyle(Style::VCenter) && !vs) yyy = (height / 2) - thi / 2;
  1685. int x = xxx;
  1686. int y = yyy;
  1687. int len = tm->text->getLength();
  1688. const char* text = tm->text->getText();
  1689. Text txtWithLineBreaks;
  1690. if (hasStyle(Style::AutoLineBreak))
  1691. {
  1692. txtWithLineBreaks = addLineBreaksToText(tm->text->getText(),
  1693. autoLineBreakSpacing ? autoLineBreakSpacing->getText() : "");
  1694. text = txtWithLineBreaks;
  1695. len = txtWithLineBreaks.getLength();
  1696. }
  1697. tm->resetIteration();
  1698. int maxLH = 0;
  1699. int realLen = tm->text->getLength();
  1700. int corrector = 0;
  1701. for (int i = 0; i < len; i++)
  1702. {
  1703. if (i >= realLen || text[i] != tm->text->getText()[i])
  1704. {
  1705. corrector++;
  1706. }
  1707. int tmpx = tm->zCurrentRenderer()->getCharWidth(
  1708. isWritable(showChar) ? showChar : text[i]);
  1709. int tmpy = tm->zCurrentRenderer()->getRowHeight()
  1710. + tm->zCurrentRenderer()->getLineSpacing();
  1711. if (mx < x + tmpx / 2
  1712. && my < y + tmpy - tm->zCurrentRenderer()->getLineSpacing() / 2)
  1713. return i - corrector;
  1714. x += tmpx + tm->zCurrentRenderer()->getCharSpacing();
  1715. maxLH = tmpy > maxLH ? tmpy : maxLH;
  1716. if (text[i] == '\n')
  1717. {
  1718. if (my >= y - tm->zCurrentRenderer()->getLineSpacing() / 2
  1719. && my < y + maxLH
  1720. - tm->zCurrentRenderer()->getLineSpacing() / 2)
  1721. return i - corrector;
  1722. x = xxx;
  1723. y += maxLH;
  1724. }
  1725. tm->nextStyle();
  1726. }
  1727. return tm->text->getLength();
  1728. }
  1729. // Returns the style of a specific character
  1730. // index: The index of the character
  1731. TextStyle TextField::getTextStyle(int index) const
  1732. {
  1733. return tm->getTextStyle(index);
  1734. }
  1735. // returns the string that is inserted after each line break when using
  1736. // the AutoLineBreak style
  1737. Text TextField::getAutoLineBreakSpacing() const
  1738. {
  1739. return autoLineBreakSpacing ? *autoLineBreakSpacing : Text("");
  1740. }
  1741. Drawable* TextField::duplicate() const // Creates a copy of the drawing
  1742. {
  1743. TextField* obj = new TextField();
  1744. obj->setPosition(pos);
  1745. obj->setSize(gr);
  1746. obj->setMouseEventParameter(makParam);
  1747. obj->setKeyboardEventParameter(takParam);
  1748. obj->setMouseEvent(mak);
  1749. obj->setKeyboardEvent(tak);
  1750. if (toolTip) obj->setToolTipZ((ToolTip*)toolTip->duplicate());
  1751. obj->setStyle(style);
  1752. obj->tm->renderer->release();
  1753. obj->tm->renderer
  1754. = dynamic_cast<RCArray<TextRenderer>*>(tm->renderer->getThis());
  1755. obj->tm->textStyle.clear();
  1756. for (const auto& i : tm->textStyle)
  1757. obj->tm->textStyle.add(i);
  1758. obj->tm->index = tm->index;
  1759. obj->tm->styleIndex = tm->styleIndex;
  1760. obj->tm->current = tm->current;
  1761. if (tm->text) obj->setText(tm->text->getText());
  1762. obj->setBackgroundColor(backgroundColor);
  1763. if (backgroundFeld)
  1764. obj->setAlphaFieldZ((AlphaField*)backgroundFeld->duplicate());
  1765. if (border) obj->setBorderZ((Border*)border->duplicate());
  1766. if (backgroundImage)
  1767. obj->setBackgroundImage(
  1768. dynamic_cast<Image*>(backgroundImage->getThis()));
  1769. if (vertikalScrollBar)
  1770. {
  1771. obj->setVerticalClickScroll(vertikalScrollBar->getClickScroll());
  1772. obj->setVerticalScrollPos(vertikalScrollBar->getScroll());
  1773. obj->setVerticalScrollColor(
  1774. vertikalScrollBar->getColor(), vertikalScrollBar->getBgColor());
  1775. }
  1776. if (horizontalScrollBar)
  1777. {
  1778. obj->setHorizontalClickScroll(horizontalScrollBar->getClickScroll());
  1779. obj->setHorizontalScrollPos(horizontalScrollBar->getScroll());
  1780. obj->setHorizontalScrollColor(
  1781. horizontalScrollBar->getColor(), horizontalScrollBar->getBgColor());
  1782. }
  1783. if (autoLineBreakSpacing)
  1784. {
  1785. obj->setAutoLineBreakSpacing(*autoLineBreakSpacing);
  1786. }
  1787. obj->setSchowChar(showChar);
  1788. return obj;
  1789. }