PolycodeTextEditor.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*
  2. Copyright (C) 2012 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolycodeTextEditor.h"
  20. extern SyntaxHighlightTheme *globalSyntaxTheme;
  21. extern UIGlobalMenu *globalMenu;
  22. void SyntaxHighlightTheme::loadFromFile(String themeName) {
  23. String filePath = "SyntaxThemes/"+themeName+".xml";
  24. this->name =themeName;
  25. Object themeObject;
  26. if(themeObject.loadFromXML(filePath)) {
  27. ObjectEntry *hintingEntry = themeObject.root["useStrongHinting"];
  28. if(hintingEntry) {
  29. useStrongHinting = hintingEntry->boolVal;
  30. }
  31. ObjectEntry *bgColorEntry = themeObject.root["bgColor"];
  32. if(bgColorEntry) {
  33. ObjectEntry *r = (*bgColorEntry)["r"];
  34. ObjectEntry *g = (*bgColorEntry)["g"];
  35. ObjectEntry *b = (*bgColorEntry)["b"];
  36. if(r && g && b) {
  37. bgColor.setColorRGB(r->intVal, g->intVal, b->intVal);
  38. }
  39. }
  40. ObjectEntry *cursorColorEntry = themeObject.root["cursorColor"];
  41. if(cursorColorEntry) {
  42. ObjectEntry *r = (*cursorColorEntry)["r"];
  43. ObjectEntry *g = (*cursorColorEntry)["g"];
  44. ObjectEntry *b = (*cursorColorEntry)["b"];
  45. if(r && g && b) {
  46. cursorColor.setColorRGB(r->intVal, g->intVal, b->intVal);
  47. }
  48. }
  49. ObjectEntry *selectionColorEntry = themeObject.root["selectionColor"];
  50. if(selectionColorEntry) {
  51. ObjectEntry *r = (*selectionColorEntry)["r"];
  52. ObjectEntry *g = (*selectionColorEntry)["g"];
  53. ObjectEntry *b = (*selectionColorEntry)["b"];
  54. if(r && g && b) {
  55. selectionColor.setColorRGB(r->intVal, g->intVal, b->intVal);
  56. }
  57. }
  58. ObjectEntry *lineNumberColorEntry = themeObject.root["lineNumberColor"];
  59. if(lineNumberColorEntry) {
  60. ObjectEntry *r = (*lineNumberColorEntry)["r"];
  61. ObjectEntry *g = (*lineNumberColorEntry)["g"];
  62. ObjectEntry *b = (*lineNumberColorEntry)["b"];
  63. if(r && g && b) {
  64. lineNumberColor.setColorRGB(r->intVal, g->intVal, b->intVal);
  65. }
  66. }
  67. ObjectEntry *textColors = themeObject.root["textColors"];
  68. if(textColors) {
  69. for(int i=0; i < textColors->length && i < 8; i++) {
  70. ObjectEntry *colorEntry = (*textColors)[i];
  71. if(colorEntry) {
  72. ObjectEntry *r = (*colorEntry)["r"];
  73. ObjectEntry *g = (*colorEntry)["g"];
  74. ObjectEntry *b = (*colorEntry)["b"];
  75. if(r && g && b) {
  76. colors[i].setColorRGB(r->intVal, g->intVal, b->intVal);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. PolycodeSyntaxHighlighter::PolycodeSyntaxHighlighter(String extension) {
  84. if(extension == "lua") {
  85. mode = MODE_LUA;
  86. std::vector<String>separators_s = String("[ * [ ] { } ; . , : # ( ) \t \n = + - / \\ ' \"").split(" ");
  87. separators_s.push_back(" ");
  88. for(int i=0; i < separators_s.size(); i++) {
  89. separators.push_back(separators_s[i][0]);
  90. }
  91. keywords = String("for cast safe_cast and require true false class self break do end else elseif function if local nil not or repeat return then until while").split(" ");
  92. } else if(extension == "vert" || extension == "frag") {
  93. mode = MODE_GLSL;
  94. std::vector<String>separators_s = String("[ * [ ] { } ; . , : ( ) \t \n = + - / \\ ' \"").split(" ");
  95. separators_s.push_back(" ");
  96. for(int i=0; i < separators_s.size(); i++) {
  97. separators.push_back(separators_s[i][0]);
  98. }
  99. keywords = String("for cast and true falsebreak do end else if notreturn then until while uniform varying sampler2D sampler3D vec2 vec3 vec4 float in inout void mat2 mat3 mat4 bool int #define #ifdef #elif #else #endif").split(" ");
  100. }
  101. }
  102. PolycodeSyntaxHighlighter::~PolycodeSyntaxHighlighter() {
  103. }
  104. bool PolycodeSyntaxHighlighter::contains(String part, std::vector<String> *list) {
  105. for(int i=0; i < list->size(); i++) {
  106. if((*list)[i] == part)
  107. return true;
  108. }
  109. return false;
  110. }
  111. bool PolycodeSyntaxHighlighter::contains_char(char part, std::vector<char> *list) {
  112. for(int i=0; i < list->size(); i++) {
  113. if((*list)[i] == part)
  114. return true;
  115. }
  116. return false;
  117. }
  118. std::vector<SyntaxHighlightToken> PolycodeSyntaxHighlighter::parseText(String text, SyntaxHighlightToken overrideToken) {
  119. if(mode == MODE_LUA) {
  120. return parseLua(text, overrideToken);
  121. } else {
  122. return parseGLSL(text, overrideToken);
  123. }
  124. }
  125. std::vector<SyntaxHighlightToken> PolycodeSyntaxHighlighter::parseGLSL(String text, SyntaxHighlightToken overrideToken) {
  126. std::vector<SyntaxHighlightToken> tokens;
  127. text = text+"\n";
  128. const int MODE_GENERAL = 0;
  129. const int MODE_COMMENT = 1;
  130. const int MODE_STRING = 2;
  131. const int MODE_METHOD = 3;
  132. const int MODE_KEYWORD = 4;
  133. const int MODE_NUMBER = 5;
  134. const int MODE_MEMBER = 6;
  135. int mode = MODE_GENERAL;
  136. bool isComment = false;
  137. if(text.find_first_of("*/") != -1) {
  138. if(overrideToken.overrideType == SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_LINE || overrideToken.overrideType == SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_START ) {
  139. mode = MODE_COMMENT;
  140. }
  141. }
  142. String line = "";
  143. char lastSeparator = ' ';
  144. for(int i=0; i < text.length(); i++) {
  145. char ch = text[i];
  146. if(contains_char(ch, &separators)) {
  147. unsigned int type = mode;
  148. unsigned int ch_type = mode;
  149. if(ch == '\"' && mode != MODE_COMMENT)
  150. ch_type = MODE_STRING;
  151. if(mode != MODE_STRING && ch == '(' && mode != MODE_COMMENT) {
  152. type = MODE_METHOD;
  153. }
  154. if(mode != MODE_STRING && mode != MODE_COMMENT) {
  155. if(contains(line, &keywords)) {
  156. type = MODE_KEYWORD;
  157. }
  158. }
  159. if(mode != MODE_STRING && !isComment && mode != MODE_COMMENT) {
  160. if(line.isNumber()) {
  161. type = MODE_NUMBER;
  162. } else {
  163. if(lastSeparator == '.' && ch != '.' && ch != ':') {
  164. type = MODE_MEMBER;
  165. }
  166. }
  167. }
  168. if(isComment) {
  169. type = MODE_COMMENT;
  170. ch_type = MODE_COMMENT;
  171. }
  172. if(mode == MODE_COMMENT) {
  173. type = MODE_COMMENT;
  174. ch_type = MODE_COMMENT;
  175. }
  176. if(line != "")
  177. tokens.push_back(SyntaxHighlightToken(line, type));
  178. tokens.push_back(SyntaxHighlightToken(ch, ch_type));
  179. if(ch == '/' && lastSeparator == '/' && mode != MODE_STRING) {
  180. isComment = true;
  181. tokens[tokens.size()-1].type = MODE_COMMENT;
  182. tokens[tokens.size()-2].type = MODE_COMMENT;
  183. }
  184. if(ch == '*' && lastSeparator == '/' && mode != MODE_STRING) {
  185. tokens[tokens.size()-1].type = MODE_COMMENT;
  186. tokens[tokens.size()-2].type = MODE_COMMENT;
  187. mode = MODE_COMMENT;
  188. tokens[tokens.size()-1].overrideType = SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_START;
  189. }
  190. if(ch == '/' && lastSeparator == '*' && mode == MODE_COMMENT) {
  191. if(mode == MODE_COMMENT)
  192. mode = MODE_GENERAL;
  193. if(mode != MODE_STRING)
  194. tokens[tokens.size()-1].overrideType = SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_END;
  195. }
  196. if(ch == '\n' )
  197. isComment = false;
  198. if(ch == '\"' && mode != MODE_COMMENT) {
  199. if(mode == MODE_STRING) {
  200. mode = MODE_GENERAL;
  201. } else {
  202. mode = MODE_STRING;
  203. }
  204. }
  205. line = "";
  206. lastSeparator = ch;
  207. } else {
  208. line.append(ch);
  209. }
  210. }
  211. for(int i=0; i < tokens.size(); i++) {
  212. switch(tokens[i].type) {
  213. case MODE_STRING:
  214. tokens[i].color = globalSyntaxTheme->colors[4];
  215. break;
  216. case MODE_COMMENT:
  217. tokens[i].color = globalSyntaxTheme->colors[1];
  218. break;
  219. case MODE_METHOD:
  220. tokens[i].color = globalSyntaxTheme->colors[3];
  221. break;
  222. case MODE_KEYWORD:
  223. tokens[i].color = globalSyntaxTheme->colors[2];
  224. break;
  225. case MODE_NUMBER:
  226. tokens[i].color = globalSyntaxTheme->colors[6];
  227. break;
  228. case MODE_MEMBER:
  229. tokens[i].color = globalSyntaxTheme->colors[5];
  230. break;
  231. default:
  232. tokens[i].color = globalSyntaxTheme->colors[0];
  233. break;
  234. }
  235. }
  236. return tokens;
  237. }
  238. std::vector<SyntaxHighlightToken> PolycodeSyntaxHighlighter::parseLua(String text, SyntaxHighlightToken overrideToken) {
  239. std::vector<SyntaxHighlightToken> tokens;
  240. text = text+"\n";
  241. const int MODE_GENERAL = 0;
  242. const int MODE_COMMENT = 1;
  243. const int MODE_STRING = 2;
  244. const int MODE_METHOD = 3;
  245. const int MODE_KEYWORD = 4;
  246. const int MODE_NUMBER = 5;
  247. const int MODE_MEMBER = 6;
  248. int mode = MODE_GENERAL;
  249. bool isComment = false;
  250. if(text.find_first_of("]]") != -1) {
  251. if(overrideToken.overrideType == SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_LINE || overrideToken.overrideType == SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_START ) {
  252. mode = MODE_COMMENT;
  253. }
  254. }
  255. String line = "";
  256. char lastSeparator = ' ';
  257. for(int i=0; i < text.length(); i++) {
  258. char ch = text[i];
  259. if(contains_char(ch, &separators)) {
  260. unsigned int type = mode;
  261. unsigned int ch_type = mode;
  262. if(ch == '\"' && mode != MODE_COMMENT)
  263. ch_type = MODE_STRING;
  264. if(mode != MODE_STRING && ch == '(' && mode != MODE_COMMENT) {
  265. type = MODE_METHOD;
  266. }
  267. if(mode != MODE_STRING && mode != MODE_COMMENT) {
  268. if(contains(line, &keywords)) {
  269. type = MODE_KEYWORD;
  270. }
  271. }
  272. if(mode != MODE_STRING && !isComment && mode != MODE_COMMENT) {
  273. if(line.isNumber()) {
  274. type = MODE_NUMBER;
  275. } else {
  276. if(lastSeparator == '.' && ch != '.' && ch != ':') {
  277. type = MODE_MEMBER;
  278. }
  279. }
  280. }
  281. if(isComment) {
  282. type = MODE_COMMENT;
  283. ch_type = MODE_COMMENT;
  284. }
  285. if(mode == MODE_COMMENT) {
  286. type = MODE_COMMENT;
  287. ch_type = MODE_COMMENT;
  288. }
  289. if(line != "")
  290. tokens.push_back(SyntaxHighlightToken(line, type));
  291. tokens.push_back(SyntaxHighlightToken(ch, ch_type));
  292. if(ch == '-' && lastSeparator == '-' && mode != MODE_STRING) {
  293. isComment = true;
  294. tokens[tokens.size()-1].type = MODE_COMMENT;
  295. tokens[tokens.size()-2].type = MODE_COMMENT;
  296. }
  297. if(ch == '[' && lastSeparator == '[' && isComment && mode != MODE_STRING) {
  298. unsigned int old_mode = mode;
  299. mode = MODE_COMMENT;
  300. tokens[tokens.size()-1].overrideType = SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_START;
  301. // ugly hack for ---[[, which is not a block comment
  302. if(tokens.size() > 4) {
  303. if(tokens[tokens.size()-5].text == "-") {
  304. mode = old_mode;
  305. tokens[tokens.size()-1].overrideType = SyntaxHighlightToken::TOKEN_TYPE_NO_OVERRIDE;
  306. }
  307. }
  308. }
  309. if(ch == ']' && lastSeparator == ']') {
  310. if(mode == MODE_COMMENT)
  311. mode = MODE_GENERAL;
  312. if(mode != MODE_STRING)
  313. tokens[tokens.size()-1].overrideType = SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_END;
  314. }
  315. if(ch == '\n' ) {
  316. isComment = false;
  317. mode = MODE_GENERAL;
  318. }
  319. if(ch == '\"' && mode != MODE_COMMENT) {
  320. if(mode == MODE_STRING) {
  321. mode = MODE_GENERAL;
  322. } else {
  323. mode = MODE_STRING;
  324. }
  325. }
  326. line = "";
  327. lastSeparator = ch;
  328. } else {
  329. line.append(ch);
  330. }
  331. }
  332. for(int i=0; i < tokens.size(); i++) {
  333. switch(tokens[i].type) {
  334. case MODE_STRING:
  335. tokens[i].color = globalSyntaxTheme->colors[4];
  336. break;
  337. case MODE_COMMENT:
  338. tokens[i].color = globalSyntaxTheme->colors[1];
  339. break;
  340. case MODE_METHOD:
  341. tokens[i].color = globalSyntaxTheme->colors[3];
  342. break;
  343. case MODE_KEYWORD:
  344. tokens[i].color = globalSyntaxTheme->colors[2];
  345. break;
  346. case MODE_NUMBER:
  347. tokens[i].color = globalSyntaxTheme->colors[6];
  348. break;
  349. case MODE_MEMBER:
  350. tokens[i].color = globalSyntaxTheme->colors[5];
  351. break;
  352. default:
  353. tokens[i].color = globalSyntaxTheme->colors[0];
  354. break;
  355. }
  356. }
  357. return tokens;
  358. }
  359. PolycodeTextEditor::PolycodeTextEditor() : PolycodeEditor(true){
  360. firstTimeResize = true;
  361. editorType = "PolycodeTextEditor";
  362. }
  363. PolycodeTextEditor::~PolycodeTextEditor() {
  364. delete textInput;
  365. delete findBar;
  366. if(syntaxHighligher)
  367. delete syntaxHighligher;
  368. }
  369. bool PolycodeTextEditor::openFile(OSFileEntry filePath) {
  370. isLoading = true;
  371. textInput = new UITextInput(true, 100, 100);
  372. addChild(textInput);
  373. textInput->setBackgroundColor(globalSyntaxTheme->bgColor);
  374. textInput->setCursorColor(globalSyntaxTheme->cursorColor);
  375. textInput->setSelectionColor(globalSyntaxTheme->selectionColor);
  376. textInput->useStrongHinting = globalSyntaxTheme->useStrongHinting;
  377. textInput->setLineNumberColor(globalSyntaxTheme->lineNumberColor);
  378. textInput->enableLineNumbers(true);
  379. textInput->addEventListener(this, UIEvent::CHANGE_EVENT);
  380. findBar = new FindBar();
  381. findBar->visible = false;
  382. addChild(findBar);
  383. findBar->findInput->addEventListener(this, Event::COMPLETE_EVENT);
  384. findBar->findInput->addEventListener(this, Event::CANCEL_EVENT);
  385. findBar->replaceInput->addEventListener(this, Event::CANCEL_EVENT);
  386. findBar->replaceInput->addEventListener(this, Event::COMPLETE_EVENT);
  387. findBar->closeButton->addEventListener(this, UIEvent::CLICK_EVENT);
  388. findBar->replaceAllButton->addEventListener(this, UIEvent::CLICK_EVENT);
  389. findBar->functionList->addEventListener(this, UIEvent::CHANGE_EVENT);
  390. syntaxHighligher = NULL;
  391. if(filePath.extension == "lua" || filePath.extension == "vert" || filePath.extension == "frag") {
  392. syntaxHighligher = new PolycodeSyntaxHighlighter(filePath.extension);
  393. textInput->setSyntaxHighlighter(syntaxHighligher);
  394. } else {
  395. textInput->setTextColor(globalSyntaxTheme->colors[0]);
  396. }
  397. Data *data = new Data();
  398. if(data->loadFromFile(filePath.fullPath)) {
  399. textInput->setText(data->getAsString(String::ENCODING_UTF8).replace("\r\n", "\n"));
  400. }
  401. delete data;
  402. PolycodeEditor::openFile(filePath);
  403. isLoading = false;
  404. return true;
  405. }
  406. ObjectEntry *PolycodeTextEditor::getEditorConfig() {
  407. ObjectEntry *configEntry = new ObjectEntry();
  408. configEntry->addChild("scroll_offset", textInput->getScrollContainer()->getVScrollBar()->getScrollValue());
  409. return configEntry;
  410. }
  411. void PolycodeTextEditor::applyEditorConfig(ObjectEntry *configEntry) {
  412. ObjectEntry *scrollEntry = (*configEntry)["scroll_offset"];
  413. if(scrollEntry) {
  414. textInput->getScrollContainer()->setScrollValue(0.0, scrollEntry->NumberVal);
  415. }
  416. }
  417. void PolycodeTextEditor::handleEvent(Event *event) {
  418. if(event->getDispatcher() == textInput && event->getEventType() == "UIEvent") {
  419. if(!isLoading) {
  420. lastFindString = "";
  421. setHasChanges(true);
  422. }
  423. }
  424. if(event->getDispatcher() == findBar->functionList) {
  425. if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::CHANGE_EVENT) {
  426. FindMatch *match = (FindMatch*)findBar->functionList->getSelectedItem()->data;
  427. textInput->showLine(match->lineNumber, true);
  428. }
  429. }
  430. if(event->getDispatcher() == findBar->replaceAllButton) {
  431. if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::CLICK_EVENT) {
  432. textInput->replaceAll(findBar->findInput->getText(), findBar->replaceInput->getText());
  433. }
  434. }
  435. if(event->getDispatcher() == findBar->closeButton) {
  436. if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::CLICK_EVENT) {
  437. hideFindBar();
  438. }
  439. }
  440. if(event->getDispatcher() == findBar->replaceInput) {
  441. if(event->getEventType() == "") {
  442. if(event->getEventCode() == Event::CANCEL_EVENT) {
  443. hideFindBar();
  444. }
  445. if(event->getEventCode() == Event::COMPLETE_EVENT) {
  446. textInput->findString(findBar->findInput->getText(), true, findBar->replaceInput->getText());
  447. }
  448. }
  449. }
  450. if(event->getDispatcher() == findBar->findInput) {
  451. if(event->getEventType() == "") {
  452. if(event->getEventCode() == Event::CANCEL_EVENT) {
  453. hideFindBar();
  454. }
  455. if(event->getEventCode() == Event::COMPLETE_EVENT) {
  456. if(findBar->findInput->getText() != "") {
  457. if(findBar->findInput->getText() != lastFindString) {
  458. lastFindString = findBar->findInput->getText();
  459. textInput->findString(lastFindString);
  460. } else {
  461. textInput->findNext();
  462. }
  463. }
  464. }
  465. }
  466. }
  467. PolycodeEditor::handleEvent(event);
  468. }
  469. void PolycodeTextEditor::showFindBar() {
  470. findBar->visible = true;
  471. findBar->focusChild(findBar->findInput);
  472. findBar->findInput->selectAll();
  473. lastFindString = "";
  474. for(int i=0; i < findBar->functionList->getNumItems(); i++) {
  475. FindMatch *match = (FindMatch*)findBar->functionList->getItemAtIndex(i)->data;
  476. findBar->functionList->getItemAtIndex(i)->data = NULL;
  477. delete match;
  478. }
  479. findBar->functionList->clearItems();
  480. std::vector<FindMatch> functionMatches = textInput->getFindMatches("function ");
  481. for(int i=0; i < functionMatches.size(); i++) {
  482. FindMatch *match = new FindMatch();
  483. (*match) = functionMatches[i];
  484. findBar->functionList->addComboItem(textInput->getLineText(functionMatches[i].lineNumber).replace("function ", ""), (void*) match);
  485. }
  486. Resize(editorSize.x, editorSize.y);
  487. }
  488. void PolycodeTextEditor::hideFindBar() {
  489. findBar->visible = false;
  490. focusChild(textInput);
  491. Resize(editorSize.x, editorSize.y);
  492. }
  493. void PolycodeTextEditor::highlightLine(unsigned int lineNumber) {
  494. int lineSize = textInput->getLineText(lineNumber-1).length();
  495. textInput->setSelection(lineNumber-1, lineNumber-1, 0, lineSize);
  496. textInput->showLine(lineNumber, false);
  497. }
  498. void PolycodeTextEditor::saveFile() {
  499. Data *data = new Data();
  500. data->setFromString(textInput->getText(), String::ENCODING_UTF8);
  501. data->saveToFile(filePath);
  502. delete data;
  503. setHasChanges(false);
  504. }
  505. void PolycodeTextEditor::Resize(int x, int y) {
  506. findBar->setBarWidth(x);
  507. if(findBar->visible) {
  508. textInput->Resize(x,y-findBar->getHeight());
  509. textInput->setPosition(0,findBar->getHeight());
  510. } else {
  511. textInput->Resize(x,y);
  512. textInput->setPosition(0,0);
  513. }
  514. if(firstTimeResize) {
  515. textInput->doMultilineResize();
  516. firstTimeResize = false;
  517. }
  518. PolycodeEditor::Resize(x,y);
  519. }
  520. FindBar::FindBar() : UIElement() {
  521. barBg = new UIRect(30,30);
  522. barBg->setAnchorPoint(-1.0, -1.0, 0.0);
  523. barBg->color.setColorHexFromString(CoreServices::getInstance()->getConfig()->getStringValue("Polycode", "uiHeaderBgColor"));
  524. addChild(barBg);
  525. setHeight(30);
  526. UILabel *findLabel = new UILabel("FIND", 18, "section");
  527. addChild(findLabel);
  528. findLabel->setColor(1.0, 1.0, 1.0, 0.6);
  529. findLabel->setPosition(10,3);
  530. UILabel *replaceLabel = new UILabel("REPLACE", 18, "section");
  531. addChild(replaceLabel);
  532. replaceLabel->setColor(1.0, 1.0, 1.0, 0.6);
  533. replaceLabel->setPosition(200,3);
  534. processInputEvents = true;
  535. findInput = new UITextInput(false, 120, 12);
  536. addChild(findInput);
  537. findInput->setPosition(60, 4);
  538. replaceInput = new UITextInput(false, 120, 12);
  539. addChild(replaceInput);
  540. replaceInput->setPosition(280, 4);
  541. replaceAllButton = new UIButton("Replace All", 100);
  542. addChild(replaceAllButton);
  543. replaceAllButton->setPosition(420, 3);
  544. UIImage *functionIcon = new UIImage("main/function_icon.png", 11, 17);
  545. addChild(functionIcon);
  546. functionIcon->setPosition(540, 6);
  547. functionList = new UIComboBox(globalMenu, 200);
  548. addChild(functionList);
  549. functionList->setPosition(560, 4);
  550. closeButton = new UIImageButton("main/barClose.png", 1.0, 17, 17);
  551. addChild(closeButton);
  552. }
  553. void FindBar::onKeyDown(PolyKEY key, wchar_t charCode) {
  554. if(key == KEY_TAB) {
  555. focusNextChild();
  556. }
  557. if(key == KEY_ESCAPE) {
  558. }
  559. }
  560. FindBar::~FindBar(){
  561. delete findInput;
  562. delete replaceInput;
  563. delete closeButton;
  564. delete replaceAllButton;
  565. delete barBg;
  566. }
  567. void FindBar::setBarWidth(int width) {
  568. barBg->Resize(width, 30);
  569. closeButton->setPosition(width - 30, 6);
  570. functionList->Resize(width-560-40, functionList->getHeight());
  571. }