rich_text_label.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. /*************************************************************************/
  2. /* rich_text_label.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "rich_text_label.h"
  30. #include "scene/scene_string_names.h"
  31. #include "os/keyboard.h"
  32. #include "os/os.h"
  33. RichTextLabel::Item *RichTextLabel::_get_next_item(Item* p_item) {
  34. if (p_item->subitems.size()) {
  35. return p_item->subitems.front()->get();
  36. } else if (!p_item->parent) {
  37. return NULL;
  38. } else if (p_item->E->next()) {
  39. return p_item->E->next()->get();
  40. } else {
  41. //go up until something with a next is found
  42. while (p_item->parent && !p_item->E->next()) {
  43. p_item=p_item->parent;
  44. }
  45. if (p_item && p_item->parent)
  46. return p_item->E->next()->get();
  47. else
  48. return NULL;
  49. }
  50. return NULL;
  51. }
  52. void RichTextLabel::_process_line(int &y, int p_width, int p_line, ProcessMode p_mode,const Ref<Font> &p_base_font,const Color &p_base_color,const Point2i& p_click_pos,Item **r_click_item,int *r_click_char,bool *r_outside) {
  53. RID ci;
  54. if (r_outside)
  55. *r_outside=false;
  56. if (p_mode==PROCESS_DRAW) {
  57. ci=get_canvas_item();
  58. if (r_click_item)
  59. *r_click_item=NULL;
  60. }
  61. Line &l = lines[p_line];
  62. Item *it = l.from;
  63. int line_ofs=0;
  64. int margin=_find_margin(it,p_base_font);
  65. Align align=_find_align(it);;
  66. int line=0;
  67. if (p_mode!=PROCESS_CACHE) {
  68. ERR_FAIL_INDEX(line,l.offset_caches.size());
  69. line_ofs = l.offset_caches[line];
  70. }
  71. int wofs=margin;
  72. if (p_mode!=PROCESS_CACHE && align!=ALIGN_FILL)
  73. wofs+=line_ofs;
  74. int begin=wofs;
  75. Ref<Font> cfont = _find_font(it);
  76. if (cfont.is_null())
  77. cfont=p_base_font;
  78. //line height should be the font height for the first time, this ensures that an empty line will never have zero height and succesive newlines are displayed
  79. int line_height=cfont->get_height();
  80. Variant meta;
  81. #define NEW_LINE \
  82. {\
  83. if (p_mode!=PROCESS_CACHE) {\
  84. line++;\
  85. if (line < l.offset_caches.size())\
  86. line_ofs=l.offset_caches[line];\
  87. wofs=margin;\
  88. if (align!=ALIGN_FILL)\
  89. wofs+=line_ofs;\
  90. } else {\
  91. int used=wofs-margin;\
  92. switch(align) {\
  93. case ALIGN_LEFT: l.offset_caches.push_back(0); break;\
  94. case ALIGN_CENTER: l.offset_caches.push_back(((p_width-margin)-used)/2); break;\
  95. case ALIGN_RIGHT: l.offset_caches.push_back(((p_width-margin)-used)); break;\
  96. case ALIGN_FILL: l.offset_caches.push_back(p_width-wofs); break;\
  97. }\
  98. l.height_caches.push_back(line_height);\
  99. }\
  100. y+=line_height+get_constant(SceneStringNames::get_singleton()->line_separation);\
  101. line_height=0;\
  102. wofs=begin;\
  103. if (p_mode!=PROCESS_CACHE) {\
  104. lh=line<l.height_caches.size()?l.height_caches[line]:1;\
  105. }\
  106. if (p_mode==PROCESS_POINTER && r_click_item && p_click_pos.y>=y && p_click_pos.y<=y+lh && p_click_pos.x<wofs) {\
  107. if (r_outside) *r_outside=true;\
  108. *r_click_item=it;\
  109. *r_click_char=rchar;\
  110. return;\
  111. }\
  112. }
  113. #define ENSURE_WIDTH(m_width) \
  114. if (wofs + m_width > p_width) {\
  115. if (p_mode==PROCESS_POINTER && r_click_item && p_click_pos.y>=y && p_click_pos.y<=y+lh && p_click_pos.x>wofs) {\
  116. if (r_outside) *r_outside=true; \
  117. *r_click_item=it;\
  118. *r_click_char=rchar;\
  119. return;\
  120. }\
  121. NEW_LINE\
  122. }
  123. #define ADVANCE(m_width) \
  124. {\
  125. if (p_mode==PROCESS_POINTER && r_click_item && p_click_pos.y>=y && p_click_pos.y<=y+lh && p_click_pos.x>=wofs && p_click_pos.x<wofs+m_width) {\
  126. if (r_outside) *r_outside=false; \
  127. *r_click_item=it;\
  128. *r_click_char=rchar;\
  129. return;\
  130. }\
  131. wofs+=m_width;\
  132. }
  133. #define CHECK_HEIGHT( m_height ) \
  134. if (m_height > line_height) {\
  135. line_height=m_height;\
  136. }
  137. Color selection_fg;
  138. Color selection_bg;
  139. if (p_mode==PROCESS_DRAW) {
  140. selection_fg = get_color("font_color_selected");
  141. selection_bg = get_color("selection_color");
  142. }
  143. int rchar=0;
  144. int lh=0;
  145. while (it) {
  146. switch(it->type) {
  147. case ITEM_TEXT: {
  148. ItemText *text = static_cast<ItemText*>(it);
  149. Ref<Font> font=_find_font(it);
  150. if (font.is_null())
  151. font=p_base_font;
  152. const CharType *c = text->text.c_str();
  153. const CharType *cf=c;
  154. int fh=font->get_height();
  155. int ascent = font->get_ascent();
  156. Color color;
  157. bool underline=false;
  158. if (p_mode==PROCESS_DRAW) {
  159. color=_find_color(text,p_base_color);
  160. underline=_find_underline(text);
  161. if (_find_meta(text,&meta)) {
  162. underline=true;
  163. }
  164. }
  165. rchar=0;
  166. while(*c) {
  167. int end=0;
  168. int w=0;
  169. lh=0;
  170. if (p_mode!=PROCESS_CACHE) {
  171. lh=line<l.height_caches.size()?l.height_caches[line]:1;
  172. }
  173. while (c[end]!=0 && !(end && c[end-1]==' ' && c[end]!=' ')) {
  174. int cw = font->get_char_size(c[end],c[end+1]).width;
  175. w+=cw;
  176. end++;
  177. }
  178. ENSURE_WIDTH(w);
  179. {
  180. int ofs=0;
  181. for(int i=0;i<end;i++) {
  182. int pofs=wofs+ofs;
  183. if (p_mode==PROCESS_POINTER && r_click_char && p_click_pos.y>=y && p_click_pos.y<=y+lh) {
  184. //int o = (wofs+w)-p_click_pos.x;
  185. int cw=font->get_char_size(c[i],c[i+1]).x;
  186. if (p_click_pos.x-cw/2>pofs) {
  187. rchar=int((&c[i])-cf);
  188. //print_line("GOT: "+itos(rchar));
  189. //if (i==end-1 && p_click_pos.x+cw/2 > pofs)
  190. // rchar++;
  191. //int o = (wofs+w)-p_click_pos.x;
  192. // if (o>cw/2)
  193. // rchar++;
  194. }
  195. ofs+=cw;
  196. } else if (p_mode==PROCESS_DRAW) {
  197. bool selected=false;
  198. if (selection.active) {
  199. int cofs = (&c[i])-cf;
  200. if ((text->index > selection.from->index || (text->index == selection.from->index && cofs >=selection.from_char)) && (text->index < selection.to->index || (text->index == selection.to->index && cofs <=selection.to_char))) {
  201. selected=true;
  202. }
  203. }
  204. int cw;
  205. if (selected) {
  206. cw = font->get_char_size(c[i],c[i+1]).x;
  207. draw_rect(Rect2(pofs,y,cw,lh),selection_bg);
  208. font->draw_char(ci,Point2(pofs,y+lh-(fh-ascent)),c[i],c[i+1],selection_fg);
  209. } else {
  210. cw=font->draw_char(ci,Point2(pofs,y+lh-(fh-ascent)),c[i],c[i+1],color);
  211. }
  212. if (underline) {
  213. Color uc=color;
  214. uc.a*=0.3;
  215. VS::get_singleton()->canvas_item_add_line(ci,Point2(pofs,y+ascent+2),Point2(pofs+cw,y+ascent+2),uc);
  216. }
  217. ofs+=cw;
  218. }
  219. }
  220. }
  221. ADVANCE(w);
  222. CHECK_HEIGHT(fh); //must be done somewhere
  223. c=&c[end];
  224. }
  225. } break;
  226. case ITEM_IMAGE: {
  227. lh=0;
  228. if (p_mode!=PROCESS_CACHE)
  229. lh = line<l.height_caches.size()?l.height_caches[line]:1;
  230. ItemImage *img = static_cast<ItemImage*>(it);
  231. Ref<Font> font=_find_font(it);
  232. if (font.is_null())
  233. font=p_base_font;
  234. if (p_mode==PROCESS_POINTER && r_click_char)
  235. *r_click_char=0;
  236. ENSURE_WIDTH( img->image->get_width() );
  237. if (p_mode==PROCESS_DRAW) {
  238. img->image->draw(ci,Point2(wofs,y+lh-font->get_descent()-img->image->get_height()));
  239. }
  240. ADVANCE( img->image->get_width() );
  241. CHECK_HEIGHT( (img->image->get_height()+font->get_descent()) );
  242. } break;
  243. case ITEM_NEWLINE: {
  244. lh=0;
  245. if (p_mode!=PROCESS_CACHE)
  246. lh = line<l.height_caches.size()?l.height_caches[line]:1;
  247. #if 0
  248. if (p_mode==PROCESS_POINTER && r_click_item ) {
  249. //previous last "wrapped" line
  250. int pl = line-1;
  251. if (pl<0 || lines[pl].height_caches.size()==0)
  252. break;
  253. int py=lines[pl].offset_caches[ lines[pl].offset_caches.size() -1 ];
  254. int ph=lines[pl].height_caches[ lines[pl].height_caches.size() -1 ];
  255. print_line("py: "+itos(py));
  256. print_line("ph: "+itos(ph));
  257. rchar=0;
  258. if (p_click_pos.y>=py && p_click_pos.y<=py+ph) {
  259. if (r_outside) *r_outside=true;
  260. *r_click_item=it;
  261. *r_click_char=rchar;
  262. return;
  263. }
  264. }
  265. #endif
  266. } break;
  267. default: {}
  268. }
  269. Item *itp = it;
  270. it = _get_next_item(it);
  271. if (p_mode == PROCESS_POINTER && r_click_item && itp && !it && p_click_pos.y>y+lh) {
  272. //at the end of all, return this
  273. if (r_outside) *r_outside=true;
  274. *r_click_item=itp;
  275. *r_click_char=rchar;
  276. return;
  277. }
  278. if (it && (p_line+1 < lines.size()) && lines[p_line+1].from==it) {
  279. if (p_mode==PROCESS_POINTER && r_click_item && p_click_pos.y>=y && p_click_pos.y<=y+lh) {
  280. //went to next line, but pointer was on the previous one
  281. if (r_outside) *r_outside=true;
  282. *r_click_item=itp;
  283. *r_click_char=rchar;
  284. return;
  285. }
  286. break;
  287. }
  288. }
  289. NEW_LINE;
  290. #undef NEW_LINE
  291. #undef ENSURE_WIDTH
  292. #undef ADVANCE
  293. #undef CHECK_HEIGHT
  294. }
  295. void RichTextLabel::_scroll_changed(double) {
  296. if (updating_scroll)
  297. return;
  298. if (scroll_follow && vscroll->get_val()>=(vscroll->get_max()-vscroll->get_page()))
  299. scroll_following=true;
  300. else
  301. scroll_following=false;
  302. update();
  303. }
  304. void RichTextLabel::_update_scroll() {
  305. int total_height=0;
  306. if (lines.size())
  307. total_height=lines[lines.size()-1].height_accum_cache;
  308. bool exceeds = total_height > get_size().height && scroll_active;
  309. if (exceeds!=scroll_visible) {
  310. if (exceeds) {
  311. scroll_visible=true;
  312. first_invalid_line=0;
  313. scroll_w=vscroll->get_combined_minimum_size().width;
  314. vscroll->show();
  315. vscroll->set_anchor_and_margin( MARGIN_LEFT, ANCHOR_END,scroll_w);
  316. _validate_line_caches();
  317. } else {
  318. scroll_visible=false;
  319. vscroll->hide();
  320. scroll_w=0;
  321. _validate_line_caches();
  322. }
  323. }
  324. }
  325. void RichTextLabel::_notification(int p_what) {
  326. switch (p_what) {
  327. case NOTIFICATION_RESIZED: {
  328. first_invalid_line=0; //invalidate ALL
  329. update();
  330. } break;
  331. case NOTIFICATION_DRAW: {
  332. _validate_line_caches();
  333. _update_scroll();
  334. RID ci=get_canvas_item();
  335. Size2 size = get_size();
  336. VisualServer::get_singleton()->canvas_item_set_clip(ci,true);
  337. if (has_focus()) {
  338. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci,true);
  339. draw_style_box(get_stylebox("focus"),Rect2(Point2(),size));
  340. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci,false);
  341. }
  342. int ofs = vscroll->get_val();
  343. //todo, change to binary search
  344. int from_line = 0;
  345. while (from_line<lines.size()) {
  346. if (lines[from_line].height_accum_cache>=ofs)
  347. break;
  348. from_line++;
  349. }
  350. if (from_line>=lines.size())
  351. break; //nothing to draw
  352. int y = (lines[from_line].height_accum_cache - lines[from_line].height_cache) - ofs;
  353. Ref<Font> base_font=get_font("default_font");
  354. Color base_color=get_color("default_color");
  355. while (y<size.height && from_line<lines.size()) {
  356. _process_line(y,size.width-scroll_w,from_line,PROCESS_DRAW,base_font,base_color);
  357. from_line++;
  358. }
  359. }
  360. }
  361. }
  362. void RichTextLabel::_find_click(const Point2i& p_click,Item **r_click_item,int *r_click_char,bool *r_outside) {
  363. if (r_click_item)
  364. *r_click_item=NULL;
  365. Size2 size = get_size();
  366. int ofs = vscroll->get_val();
  367. //todo, change to binary search
  368. int from_line = 0;
  369. while (from_line<lines.size()) {
  370. if (lines[from_line].height_accum_cache>=ofs)
  371. break;
  372. from_line++;
  373. }
  374. if (from_line>=lines.size())
  375. return;
  376. int y = (lines[from_line].height_accum_cache - lines[from_line].height_cache) - ofs;
  377. Ref<Font> base_font=get_font("default_font");
  378. Color base_color=get_color("default_color");
  379. while (y<size.height && from_line<lines.size()) {
  380. _process_line(y,size.width-scroll_w,from_line,PROCESS_POINTER,base_font,base_color,p_click,r_click_item,r_click_char,r_outside);
  381. if (r_click_item && *r_click_item)
  382. return;
  383. from_line++;
  384. }
  385. }
  386. Control::CursorShape RichTextLabel::get_cursor_shape(const Point2& p_pos) const {
  387. if (!underline_meta || selection.click)
  388. return CURSOR_ARROW;
  389. if (first_invalid_line<lines.size())
  390. return CURSOR_ARROW; //invalid
  391. int line=0;
  392. Item *item=NULL;
  393. ((RichTextLabel*)(this))->_find_click(p_pos,&item,&line);
  394. if (item && ((RichTextLabel*)(this))->_find_meta(item,NULL))
  395. return CURSOR_POINTING_HAND;
  396. return CURSOR_ARROW;
  397. }
  398. void RichTextLabel::_input_event(InputEvent p_event) {
  399. switch(p_event.type) {
  400. case InputEvent::MOUSE_BUTTON: {
  401. if (first_invalid_line<lines.size())
  402. return;
  403. const InputEventMouseButton& b = p_event.mouse_button;
  404. if (b.button_index==BUTTON_LEFT) {
  405. if (true) {
  406. if (b.pressed && !b.doubleclick) {
  407. int line=0;
  408. Item *item=NULL;
  409. bool outside;
  410. _find_click(Point2i(b.x,b.y),&item,&line,&outside);
  411. if (item) {
  412. Variant meta;
  413. if (!outside && _find_meta(item,&meta)) {
  414. //meta clicked
  415. emit_signal("meta_clicked",meta);
  416. } else if (selection.enabled) {
  417. selection.click=item;
  418. selection.click_char=line;
  419. }
  420. }
  421. } else if (!b.pressed) {
  422. selection.click=NULL;
  423. }
  424. }
  425. }
  426. if (b.button_index==BUTTON_WHEEL_UP) {
  427. if (scroll_active)
  428. vscroll->set_val( vscroll->get_val()-vscroll->get_page()/8 );
  429. }
  430. if (b.button_index==BUTTON_WHEEL_DOWN) {
  431. if (scroll_active)
  432. vscroll->set_val( vscroll->get_val()+vscroll->get_page()/8 );
  433. }
  434. } break;
  435. case InputEvent::KEY: {
  436. const InputEventKey &k=p_event.key;
  437. if (k.pressed) {
  438. bool handled=true;
  439. switch(k.scancode) {
  440. case KEY_PAGEUP: {
  441. if (vscroll->is_visible())
  442. vscroll->set_val( vscroll->get_val() - vscroll->get_page() );
  443. } break;
  444. case KEY_PAGEDOWN: {
  445. if (vscroll->is_visible())
  446. vscroll->set_val( vscroll->get_val() + vscroll->get_page() );
  447. } break;
  448. case KEY_UP: {
  449. if (vscroll->is_visible())
  450. vscroll->set_val( vscroll->get_val() - get_font("default_font")->get_height() );
  451. } break;
  452. case KEY_DOWN: {
  453. if (vscroll->is_visible())
  454. vscroll->set_val( vscroll->get_val() + get_font("default_font")->get_height() );
  455. } break;
  456. case KEY_HOME: {
  457. if (vscroll->is_visible())
  458. vscroll->set_val( 0 );
  459. } break;
  460. case KEY_END: {
  461. if (vscroll->is_visible())
  462. vscroll->set_val( vscroll->get_max() );
  463. } break;
  464. case KEY_INSERT:
  465. case KEY_C: {
  466. if (k.mod.command) {
  467. selection_copy();
  468. } else {
  469. handled=false;
  470. }
  471. } break;
  472. default: handled=false;
  473. }
  474. if (handled)
  475. accept_event();
  476. }
  477. } break;
  478. case InputEvent::MOUSE_MOTION: {
  479. if (first_invalid_line<lines.size())
  480. return;
  481. const InputEventMouseMotion& m = p_event.mouse_motion;
  482. if (selection.click) {
  483. int line=0;
  484. Item *item=NULL;
  485. _find_click(Point2i(m.x,m.y),&item,&line);
  486. if (!item)
  487. return; // do not update
  488. selection.from=selection.click;
  489. selection.from_char=selection.click_char;
  490. selection.to=item;
  491. selection.to_char=line;
  492. bool swap=false;
  493. if (selection.from->index > selection.to->index )
  494. swap=true;
  495. else if (selection.from->index == selection.to->index) {
  496. if (selection.from_char > selection.to_char)
  497. swap=true;
  498. else if (selection.from_char == selection.to_char) {
  499. selection.active=false;
  500. return;
  501. }
  502. }
  503. if (swap) {
  504. SWAP( selection.from, selection.to );
  505. SWAP( selection.from_char, selection.to_char );
  506. }
  507. selection.active=true;
  508. update();
  509. }
  510. } break;
  511. }
  512. }
  513. Ref<Font> RichTextLabel::_find_font(Item *p_item) {
  514. Item *fontitem=p_item;
  515. while(fontitem) {
  516. if (fontitem->type==ITEM_FONT) {
  517. ItemFont *fi = static_cast<ItemFont*>(fontitem);
  518. return fi->font;
  519. }
  520. fontitem=fontitem->parent;
  521. }
  522. return Ref<Font>();
  523. }
  524. int RichTextLabel::_find_margin(Item *p_item,const Ref<Font>& p_base_font) {
  525. Item *item=p_item;
  526. int margin=0;
  527. while(item) {
  528. if (item->type==ITEM_INDENT) {
  529. Ref<Font> font=_find_font(item);
  530. if (font.is_null())
  531. font=p_base_font;
  532. ItemIndent *indent = static_cast<ItemIndent*>(item);
  533. margin+=indent->level*tab_size*font->get_char_size(' ').width;
  534. } else if (item->type==ITEM_LIST) {
  535. Ref<Font> font=_find_font(item);
  536. if (font.is_null())
  537. font=p_base_font;
  538. }
  539. item=item->parent;
  540. }
  541. return margin;
  542. }
  543. RichTextLabel::Align RichTextLabel::_find_align(Item *p_item) {
  544. Item *item=p_item;
  545. while(item) {
  546. if (item->type==ITEM_ALIGN) {
  547. ItemAlign *align = static_cast<ItemAlign*>(item);
  548. return align->align;
  549. }
  550. item=item->parent;
  551. }
  552. return default_align;
  553. }
  554. Color RichTextLabel::_find_color(Item *p_item,const Color& p_default_color) {
  555. Item *item=p_item;
  556. while(item) {
  557. if (item->type==ITEM_COLOR) {
  558. ItemColor *color = static_cast<ItemColor*>(item);
  559. return color->color;
  560. }
  561. item=item->parent;
  562. }
  563. return p_default_color;
  564. }
  565. bool RichTextLabel::_find_underline(Item *p_item) {
  566. Item *item=p_item;
  567. while(item) {
  568. if (item->type==ITEM_UNDERLINE) {
  569. return true;
  570. }
  571. item=item->parent;
  572. }
  573. return false;
  574. }
  575. bool RichTextLabel::_find_meta(Item *p_item,Variant *r_meta) {
  576. Item *item=p_item;
  577. while(item) {
  578. if (item->type==ITEM_META) {
  579. ItemMeta *meta = static_cast<ItemMeta*>(item);
  580. if (r_meta)
  581. *r_meta=meta->meta;
  582. return true;
  583. }
  584. item=item->parent;
  585. }
  586. return false;
  587. }
  588. void RichTextLabel::_validate_line_caches() {
  589. if (first_invalid_line==lines.size())
  590. return;
  591. //validate invalid lines!s
  592. Size2 size = get_size();
  593. Ref<Font> base_font=get_font("default_font");
  594. for(int i=first_invalid_line;i<lines.size();i++) {
  595. int y=0;
  596. _process_line(y,size.width-scroll_w,i,PROCESS_CACHE,base_font,Color());
  597. lines[i].height_cache=y;
  598. lines[i].height_accum_cache=y;
  599. if (i>0)
  600. lines[i].height_accum_cache+=lines[i-1].height_accum_cache;
  601. }
  602. int total_height=0;
  603. if (lines.size())
  604. total_height=lines[lines.size()-1].height_accum_cache;
  605. first_invalid_line=lines.size();
  606. updating_scroll=true;
  607. vscroll->set_max(total_height);
  608. vscroll->set_page(size.height);
  609. if (scroll_follow && scroll_following)
  610. vscroll->set_val(total_height-size.height);
  611. updating_scroll=false;
  612. }
  613. void RichTextLabel::_invalidate_current_line() {
  614. if (lines.size()-1 <= first_invalid_line) {
  615. first_invalid_line=lines.size()-1;
  616. update();
  617. }
  618. }
  619. void RichTextLabel::add_text(const String& p_text) {
  620. int pos=0;
  621. while (pos<p_text.length()) {
  622. int end=p_text.find("\n",pos);
  623. String line;
  624. bool eol=false;
  625. if (end==-1) {
  626. end=p_text.length();
  627. } else {
  628. eol=true;
  629. }
  630. if (pos==0 && end==p_text.length())
  631. line=p_text;
  632. else
  633. line=p_text.substr(pos,end-pos);
  634. if (line.length()>0) {
  635. if (current->subitems.size() && current->subitems.back()->get()->type==ITEM_TEXT) {
  636. //append text condition!
  637. ItemText *ti = static_cast<ItemText*>(current->subitems.back()->get());
  638. ti->text+=line;
  639. _invalidate_current_line();
  640. } else {
  641. //append item condition
  642. ItemText *item = memnew( ItemText );
  643. item->text=line;
  644. _add_item(item,false);
  645. }
  646. }
  647. if (eol) {
  648. ItemNewline *item = memnew( ItemNewline );
  649. item->line=lines.size();
  650. _add_item(item,false);
  651. lines.resize(lines.size()+1);
  652. lines[lines.size()-1].from=item;
  653. _invalidate_current_line();
  654. }
  655. pos=end+1;
  656. }
  657. }
  658. void RichTextLabel::_add_item(Item *p_item, bool p_enter) {
  659. p_item->parent=current;
  660. p_item->E=current->subitems.push_back(p_item);
  661. p_item->index=current_idx++;
  662. if (p_enter)
  663. current=p_item;
  664. if (lines[lines.size()-1].from==NULL) {
  665. lines[lines.size()-1].from=p_item;
  666. }
  667. _invalidate_current_line();
  668. }
  669. void RichTextLabel::add_image(const Ref<Texture>& p_image) {
  670. ERR_FAIL_COND(p_image.is_null());
  671. ItemImage *item = memnew( ItemImage );
  672. item->image=p_image;
  673. _add_item(item,false);
  674. }
  675. void RichTextLabel::add_newline() {
  676. ItemNewline *item = memnew( ItemNewline );
  677. item->line=lines.size();
  678. lines.resize(lines.size()+1);
  679. _add_item(item,false);
  680. }
  681. void RichTextLabel::push_font(const Ref<Font>& p_font) {
  682. ERR_FAIL_COND(p_font.is_null());
  683. ItemFont *item = memnew( ItemFont );
  684. item->font=p_font;
  685. _add_item(item,true);
  686. }
  687. void RichTextLabel::push_color(const Color& p_color) {
  688. ItemColor *item = memnew( ItemColor );
  689. item->color=p_color;
  690. _add_item(item,true);
  691. }
  692. void RichTextLabel::push_underline() {
  693. ItemUnderline *item = memnew( ItemUnderline );
  694. _add_item(item,true);
  695. }
  696. void RichTextLabel::push_align(Align p_align) {
  697. lines.resize(lines.size()+1);
  698. ItemAlign *item = memnew( ItemAlign );
  699. item->align=p_align;
  700. _add_item(item,true);
  701. ItemNewline *itemnl = memnew( ItemNewline );
  702. itemnl->line=lines.size()-1;
  703. _add_item(itemnl,false);
  704. }
  705. void RichTextLabel::push_indent(int p_level) {
  706. ERR_FAIL_COND(p_level<0);
  707. lines.resize(lines.size()+1);
  708. ItemIndent *item = memnew( ItemIndent );
  709. item->level=p_level;
  710. _add_item(item,true);
  711. ItemNewline *itemnl = memnew( ItemNewline );
  712. itemnl->line=lines.size()-1;
  713. _add_item(itemnl,false);
  714. }
  715. void RichTextLabel::push_list(ListType p_list) {
  716. ERR_FAIL_INDEX(p_list,3);
  717. ItemList *item = memnew( ItemList );
  718. item->list_type=p_list;
  719. _add_item(item,true);
  720. }
  721. void RichTextLabel::push_meta(const Variant& p_meta) {
  722. ItemMeta *item = memnew( ItemMeta );
  723. item->meta=p_meta;
  724. _add_item(item,true);
  725. }
  726. void RichTextLabel::pop() {
  727. ERR_FAIL_COND(!current->parent);
  728. current=current->parent;
  729. }
  730. void RichTextLabel::clear() {
  731. main->_clear_children();
  732. current=main;
  733. lines.clear();
  734. lines.resize(1);
  735. first_invalid_line=0;
  736. update();
  737. selection.click=NULL;
  738. selection.active=false;
  739. current_idx=1;
  740. }
  741. void RichTextLabel::set_tab_size(int p_spaces) {
  742. tab_size=p_spaces;
  743. first_invalid_line=0;
  744. update();
  745. }
  746. int RichTextLabel::get_tab_size() const {
  747. return tab_size;
  748. }
  749. void RichTextLabel::set_meta_underline(bool p_underline) {
  750. underline_meta=p_underline;
  751. update();
  752. }
  753. bool RichTextLabel::is_meta_underlined() const {
  754. return underline_meta;
  755. }
  756. void RichTextLabel::set_offset(int p_pixel) {
  757. vscroll->set_val(p_pixel);
  758. }
  759. void RichTextLabel::set_scroll_active(bool p_active) {
  760. if (scroll_active==p_active)
  761. return;
  762. scroll_active=p_active;
  763. update();
  764. }
  765. bool RichTextLabel::is_scroll_active() const {
  766. return scroll_active;
  767. }
  768. void RichTextLabel::set_scroll_follow(bool p_follow) {
  769. scroll_follow=p_follow;
  770. if (!vscroll->is_visible() || vscroll->get_val()>=(vscroll->get_max()-vscroll->get_page()))
  771. scroll_following=true;
  772. }
  773. bool RichTextLabel::is_scroll_following() const {
  774. return scroll_follow;
  775. }
  776. Error RichTextLabel::parse_bbcode(const String& p_bbcode) {
  777. clear();
  778. return append_bbcode(p_bbcode);
  779. }
  780. Error RichTextLabel::append_bbcode(const String& p_bbcode) {
  781. int pos = 0;
  782. List<String> tag_stack;
  783. Ref<Font> base_font=get_font("default_font");
  784. Color base_color=get_color("default_color");
  785. while(pos < p_bbcode.length()) {
  786. int brk_pos = p_bbcode.find("[",pos);
  787. if (brk_pos<0)
  788. brk_pos=p_bbcode.length();
  789. if (brk_pos > pos) {
  790. add_text(p_bbcode.substr(pos,brk_pos-pos));
  791. }
  792. if (brk_pos==p_bbcode.length())
  793. break; //nothing else o add
  794. int brk_end = p_bbcode.find("]",brk_pos+1);
  795. if (brk_end==-1) {
  796. //no close, add the rest
  797. add_text(p_bbcode.substr(brk_pos,p_bbcode.length()-brk_pos));
  798. break;
  799. }
  800. String tag = p_bbcode.substr(brk_pos+1,brk_end-brk_pos-1);
  801. if (tag.begins_with("/")) {
  802. bool tag_ok = tag_stack.size() && tag_stack.front()->get()==tag.substr(1,tag.length());
  803. if (!tag_ok) {
  804. add_text("[");
  805. pos++;
  806. continue;
  807. }
  808. tag_stack.pop_front();
  809. pos=brk_end+1;
  810. if (tag!="/img")
  811. pop();
  812. } else if (tag=="b") {
  813. //use bold font
  814. push_font(base_font);
  815. pos=brk_end+1;
  816. tag_stack.push_front(tag);
  817. } else if (tag=="i") {
  818. //use italics font
  819. push_font(base_font);
  820. pos=brk_end+1;
  821. tag_stack.push_front(tag);
  822. } else if (tag=="code") {
  823. //use monospace font
  824. push_font(base_font);
  825. pos=brk_end+1;
  826. tag_stack.push_front(tag);
  827. } else if (tag=="u") {
  828. //use underline
  829. push_underline();
  830. pos=brk_end+1;
  831. tag_stack.push_front(tag);
  832. } else if (tag=="s") {
  833. //use strikethrough (not supported underline instead)
  834. push_underline();
  835. pos=brk_end+1;
  836. tag_stack.push_front(tag);
  837. } else if (tag=="url") {
  838. //use strikethrough (not supported underline instead)
  839. int end=p_bbcode.find("[",brk_end);
  840. if (end==-1)
  841. end=p_bbcode.length();
  842. String url = p_bbcode.substr(brk_end+1,end-brk_end-1);
  843. push_meta(url);
  844. pos=brk_end+1;
  845. tag_stack.push_front(tag);
  846. } else if (tag.begins_with("url=")) {
  847. String url = tag.substr(4,tag.length());
  848. push_meta(url);
  849. pos=brk_end+1;
  850. tag_stack.push_front("url");
  851. } else if (tag=="img") {
  852. //use strikethrough (not supported underline instead)
  853. int end=p_bbcode.find("[",brk_end);
  854. if (end==-1)
  855. end=p_bbcode.length();
  856. String image = p_bbcode.substr(brk_end+1,end-brk_end-1);
  857. Ref<Texture> texture = ResourceLoader::load(image,"Texture");
  858. if (texture.is_valid())
  859. add_image(texture);
  860. pos=end;
  861. tag_stack.push_front(tag);
  862. } else if (tag.begins_with("color=")) {
  863. String col = tag.substr(6,tag.length());
  864. Color color;
  865. if (col.begins_with("#"))
  866. color=Color::html(col);
  867. else if (col=="aqua")
  868. color=Color::html("#00FFFF");
  869. else if (col=="black")
  870. color=Color::html("#000000");
  871. else if (col=="blue")
  872. color=Color::html("#0000FF");
  873. else if (col=="fuchsia")
  874. color=Color::html("#FF00FF");
  875. else if (col=="gray" || col=="grey")
  876. color=Color::html("#808080");
  877. else if (col=="green")
  878. color=Color::html("#008000");
  879. else if (col=="lime")
  880. color=Color::html("#00FF00");
  881. else if (col=="maroon")
  882. color=Color::html("#800000");
  883. else if (col=="navy")
  884. color=Color::html("#000080");
  885. else if (col=="olive")
  886. color=Color::html("#808000");
  887. else if (col=="purple")
  888. color=Color::html("#800080");
  889. else if (col=="red")
  890. color=Color::html("#FF0000");
  891. else if (col=="silver")
  892. color=Color::html("#C0C0C0");
  893. else if (col=="teal")
  894. color=Color::html("#008008");
  895. else if (col=="white")
  896. color=Color::html("#FFFFFF");
  897. else if (col=="yellow")
  898. color=Color::html("#FFFF00");
  899. else
  900. color=base_color;
  901. push_color(color);
  902. pos=brk_end+1;
  903. tag_stack.push_front("color");
  904. } else if (tag.begins_with("font=")) {
  905. String fnt = tag.substr(5,tag.length());
  906. Ref<Font> font = ResourceLoader::load(fnt,"Font");
  907. if (font.is_valid())
  908. push_font(font);
  909. else
  910. push_font(base_font);
  911. pos=brk_end+1;
  912. tag_stack.push_front("font");
  913. } else {
  914. add_text("["); //ignore
  915. pos=brk_pos+1;
  916. }
  917. }
  918. return OK;
  919. }
  920. void RichTextLabel::scroll_to_line(int p_line) {
  921. ERR_FAIL_INDEX(p_line,lines.size());
  922. _validate_line_caches();
  923. vscroll->set_val(lines[p_line].height_accum_cache);
  924. }
  925. int RichTextLabel::get_line_count() const {
  926. return lines.size();
  927. }
  928. void RichTextLabel::set_selection_enabled(bool p_enabled) {
  929. selection.enabled=p_enabled;
  930. if (!p_enabled) {
  931. if (selection.active) {
  932. selection.active=false;
  933. update();
  934. }
  935. set_focus_mode(FOCUS_NONE);
  936. } else {
  937. set_focus_mode(FOCUS_ALL);
  938. }
  939. }
  940. bool RichTextLabel::search(const String& p_string,bool p_from_selection) {
  941. ERR_FAIL_COND_V(!selection.enabled,false);
  942. Item *it=main;
  943. int charidx=0;
  944. if (p_from_selection && selection.active && selection.enabled) {
  945. it=selection.to;
  946. charidx=selection.to_char+1;
  947. }
  948. int line=-1;
  949. while(it) {
  950. if (it->type==ITEM_TEXT) {
  951. ItemText *t = static_cast<ItemText*>(it);
  952. int sp = t->text.find(p_string,charidx);
  953. if (sp!=-1) {
  954. selection.from=it;
  955. selection.from_char=sp;
  956. selection.to=it;
  957. selection.to_char=sp+p_string.length()-1;
  958. selection.active=true;
  959. update();
  960. if (line==-1) {
  961. while(it) {
  962. if (it->type==ITEM_NEWLINE) {
  963. line=static_cast<ItemNewline*>(it)->line;
  964. break;
  965. }
  966. it=_get_next_item(it);
  967. }
  968. if (!it)
  969. line=lines.size()-1;
  970. }
  971. scroll_to_line(line-2);
  972. return true;
  973. }
  974. } else if (it->type==ITEM_NEWLINE) {
  975. line=static_cast<ItemNewline*>(it)->line;
  976. }
  977. it=_get_next_item(it);
  978. charidx=0;
  979. }
  980. return false;
  981. }
  982. void RichTextLabel::selection_copy() {
  983. if (!selection.enabled)
  984. return;
  985. String text;
  986. RichTextLabel::Item *item=selection.from;
  987. while(item) {
  988. if (item->type==ITEM_TEXT) {
  989. String itext = static_cast<ItemText*>(item)->text;
  990. if (item==selection.from && item==selection.to) {
  991. text+=itext.substr(selection.from_char,selection.to_char-selection.from_char+1);
  992. } else if (item==selection.from) {
  993. text+=itext.substr(selection.from_char,itext.size());
  994. } else if (item==selection.to) {
  995. text+=itext.substr(0,selection.to_char+1);
  996. } else {
  997. text+=itext;
  998. }
  999. } else if (item->type==ITEM_NEWLINE) {
  1000. text+="\n";
  1001. }
  1002. if (item==selection.to)
  1003. break;
  1004. item=_get_next_item(item);
  1005. }
  1006. if (text!="") {
  1007. OS::get_singleton()->set_clipboard(text);
  1008. print_line("COPY: "+text);
  1009. }
  1010. }
  1011. bool RichTextLabel::is_selection_enabled() const {
  1012. return selection.enabled;
  1013. }
  1014. void RichTextLabel::_bind_methods() {
  1015. ObjectTypeDB::bind_method(_MD("_input_event"),&RichTextLabel::_input_event);
  1016. ObjectTypeDB::bind_method(_MD("_scroll_changed"),&RichTextLabel::_scroll_changed);
  1017. ObjectTypeDB::bind_method(_MD("add_text","text"),&RichTextLabel::add_text);
  1018. ObjectTypeDB::bind_method(_MD("add_image","image:Texture"),&RichTextLabel::add_image);
  1019. ObjectTypeDB::bind_method(_MD("newline"),&RichTextLabel::add_newline);
  1020. ObjectTypeDB::bind_method(_MD("push_font","font"),&RichTextLabel::push_font);
  1021. ObjectTypeDB::bind_method(_MD("push_color","color"),&RichTextLabel::push_color);
  1022. ObjectTypeDB::bind_method(_MD("push_align","align"),&RichTextLabel::push_align);
  1023. ObjectTypeDB::bind_method(_MD("push_indent","level"),&RichTextLabel::push_indent);
  1024. ObjectTypeDB::bind_method(_MD("push_list","type"),&RichTextLabel::push_list);
  1025. ObjectTypeDB::bind_method(_MD("push_meta","data"),&RichTextLabel::push_meta);
  1026. ObjectTypeDB::bind_method(_MD("push_underline"),&RichTextLabel::push_underline);
  1027. ObjectTypeDB::bind_method(_MD("pop"),&RichTextLabel::pop);
  1028. ObjectTypeDB::bind_method(_MD("clear"),&RichTextLabel::clear);
  1029. ObjectTypeDB::bind_method(_MD("set_meta_underline","enable"),&RichTextLabel::set_meta_underline);
  1030. ObjectTypeDB::bind_method(_MD("is_meta_underlined"),&RichTextLabel::is_meta_underlined);
  1031. ObjectTypeDB::bind_method(_MD("set_scroll_active","active"),&RichTextLabel::set_scroll_active);
  1032. ObjectTypeDB::bind_method(_MD("is_scroll_active"),&RichTextLabel::is_scroll_active);
  1033. ObjectTypeDB::bind_method(_MD("set_scroll_follow","follow"),&RichTextLabel::set_scroll_follow);
  1034. ObjectTypeDB::bind_method(_MD("is_scroll_following"),&RichTextLabel::is_scroll_following);
  1035. ObjectTypeDB::bind_method(_MD("set_tab_size","spaces"),&RichTextLabel::set_tab_size);
  1036. ObjectTypeDB::bind_method(_MD("get_tab_size"),&RichTextLabel::get_tab_size);
  1037. ObjectTypeDB::bind_method(_MD("set_selection_enabled","enabled"),&RichTextLabel::set_selection_enabled);
  1038. ObjectTypeDB::bind_method(_MD("is_selection_enabled"),&RichTextLabel::is_selection_enabled);
  1039. ObjectTypeDB::bind_method(_MD("parse_bbcode", "bbcode"),&RichTextLabel::parse_bbcode);
  1040. ObjectTypeDB::bind_method(_MD("append_bbcode", "bbcode"),&RichTextLabel::append_bbcode);
  1041. ADD_SIGNAL( MethodInfo("meta_clicked",PropertyInfo(Variant::NIL,"meta")));
  1042. BIND_CONSTANT( ALIGN_LEFT );
  1043. BIND_CONSTANT( ALIGN_CENTER );
  1044. BIND_CONSTANT( ALIGN_RIGHT );
  1045. BIND_CONSTANT( ALIGN_FILL );
  1046. BIND_CONSTANT( LIST_NUMBERS );
  1047. BIND_CONSTANT( LIST_LETTERS );
  1048. BIND_CONSTANT( LIST_DOTS );
  1049. BIND_CONSTANT( ITEM_MAIN );
  1050. BIND_CONSTANT( ITEM_TEXT );
  1051. BIND_CONSTANT( ITEM_IMAGE );
  1052. BIND_CONSTANT( ITEM_NEWLINE );
  1053. BIND_CONSTANT( ITEM_FONT );
  1054. BIND_CONSTANT( ITEM_COLOR );
  1055. BIND_CONSTANT( ITEM_UNDERLINE );
  1056. BIND_CONSTANT( ITEM_ALIGN );
  1057. BIND_CONSTANT( ITEM_INDENT );
  1058. BIND_CONSTANT( ITEM_LIST );
  1059. BIND_CONSTANT( ITEM_META );
  1060. }
  1061. RichTextLabel::RichTextLabel() {
  1062. main = memnew( ItemMain );
  1063. main->index=0;
  1064. current=main;
  1065. lines.resize(1);
  1066. lines[0].from=main;
  1067. first_invalid_line=0;
  1068. tab_size=4;
  1069. default_align=ALIGN_LEFT;
  1070. underline_meta=true;
  1071. scroll_visible=false;
  1072. scroll_follow=false;
  1073. scroll_following=false;
  1074. updating_scroll=false;
  1075. scroll_active=true;
  1076. scroll_w=0;
  1077. vscroll = memnew( VScrollBar );
  1078. add_child(vscroll);
  1079. vscroll->set_drag_slave(String(".."));
  1080. vscroll->set_step(1);
  1081. vscroll->set_anchor_and_margin( MARGIN_TOP, ANCHOR_BEGIN, 0);
  1082. vscroll->set_anchor_and_margin( MARGIN_BOTTOM, ANCHOR_END, 0);
  1083. vscroll->set_anchor_and_margin( MARGIN_RIGHT, ANCHOR_END, 0);
  1084. vscroll->connect("value_changed",this,"_scroll_changed");
  1085. vscroll->set_step(1);
  1086. vscroll->hide();
  1087. current_idx=1;
  1088. selection.click=NULL;
  1089. selection.active=false;
  1090. selection.enabled=false;
  1091. }
  1092. RichTextLabel::~RichTextLabel() {
  1093. memdelete( main );
  1094. }