editor-fltk.nut 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. //
  2. // "$Id: editor.cxx 8864 2011-07-19 04:49:30Z greg.ercolano $"
  3. //
  4. // A simple text editor program for the Fast Light Tool Kit (FLTK).
  5. //
  6. // This program is described in Chapter 4 of the FLTK Programmer's Guide.
  7. //
  8. // Copyright 1998-2010 by Bill Spitzak and others.
  9. //
  10. // This library is free software. Distribution and use rights are outlined in
  11. // the file "COPYING" which should have been included with this file. If this
  12. // file is missing or damaged, see the license at:
  13. //
  14. // http://www.fltk.org/COPYING.php
  15. //
  16. // Please report all bugs and problems on the following page:
  17. //
  18. // http://www.fltk.org/str.php
  19. //
  20. //
  21. // Include necessary headers...
  22. //
  23. /*
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <errno.h>
  29. #ifdef __MWERKS__
  30. # define FL_DLL
  31. #endif
  32. #include <FL/Fl.H>
  33. #include <FL/Fl_Group.H>
  34. #include <FL/Fl_Double_Window.H>
  35. #include <FL/fl_ask.H>
  36. #include <FL/Fl_Native_File_Chooser.H>
  37. #include <FL/Fl_Menu_Bar.H>
  38. #include <FL/Fl_Input.H>
  39. #include <FL/Fl_Button.H>
  40. #include <FL/Fl_Return_Button.H>
  41. #include <FL/Fl_Text_Buffer.H>
  42. #include <FL/Fl_Text_Editor.H>
  43. #include <FL/filename.H>
  44. */
  45. local WIN32 = os.getenv("WINDIR") != null
  46. local __APPLE__ = os.getenv("MAC??") != null
  47. local changed = 0;
  48. local filename = "";
  49. local title="";
  50. local textbuf = null;
  51. // Syntax highlighting stuff...
  52. const TS = 14; // default editor textsize
  53. local stylebuf = null;
  54. local styletable = [ // Style table
  55. [ FL_BLACK, FL_COURIER, TS ], // A - Plain
  56. [ FL_DARK_GREEN, FL_HELVETICA_ITALIC, TS ], // B - Line comments
  57. [ FL_DARK_GREEN, FL_HELVETICA_ITALIC, TS ], // C - Block comments
  58. [ FL_BLUE, FL_COURIER, TS ], // D - Strings
  59. [ FL_DARK_RED, FL_COURIER, TS ], // E - Directives
  60. [ FL_DARK_RED, FL_COURIER_BOLD, TS ], // F - Types
  61. [ FL_BLUE, FL_COURIER_BOLD, TS ], // G - Keywords
  62. ];
  63. local code_keywords = [ // List of known C/C++ keywords...
  64. "and",
  65. "and_eq",
  66. "asm",
  67. "bitand",
  68. "bitor",
  69. "break",
  70. "case",
  71. "catch",
  72. "compl",
  73. "continue",
  74. "default",
  75. "delete",
  76. "do",
  77. "else",
  78. "false",
  79. "for",
  80. "goto",
  81. "if",
  82. "new",
  83. "not",
  84. "not_eq",
  85. "operator",
  86. "or",
  87. "or_eq",
  88. "return",
  89. "switch",
  90. "template",
  91. "this",
  92. "throw",
  93. "true",
  94. "try",
  95. "while",
  96. "xor",
  97. "xor_eq"
  98. ];
  99. local code_types = [ // List of known C/C++ types...
  100. "auto",
  101. "bool",
  102. "char",
  103. "class",
  104. "const",
  105. "const_cast",
  106. "double",
  107. "dynamic_cast",
  108. "enum",
  109. "explicit",
  110. "extern",
  111. "float",
  112. "friend",
  113. "inline",
  114. "int",
  115. "long",
  116. "mutable",
  117. "namespace",
  118. "private",
  119. "protected",
  120. "public",
  121. "register",
  122. "short",
  123. "signed",
  124. "sizeof",
  125. "static",
  126. "static_cast",
  127. "struct",
  128. "template",
  129. "typedef",
  130. "typename",
  131. "union",
  132. "unsigned",
  133. "virtual",
  134. "void",
  135. "volatile"
  136. ];
  137. //
  138. // 'compare_keywords()' - Compare two keywords...
  139. //
  140. //extern "C" {
  141. // int
  142. function compare_keywords(a, b) { return a <=> b; }
  143. //}
  144. //
  145. // 'style_parse()' - Parse text and produce style data.
  146. //
  147. //void
  148. function style_parse(text, style, length) {
  149. local current;
  150. local col;
  151. local last;
  152. local buf, bufptr;
  153. local temp;
  154. local istyle = 0;
  155. local itext = 0;
  156. // Style letters:
  157. //
  158. // A - Plain
  159. // B - Line comments
  160. // C - Block comments
  161. // D - Strings
  162. // E - Directives
  163. // F - Types
  164. // G - Keywords
  165. //~ for (current = style[istyle], col = 0, last = 0; length > 0; length --, itext ++) {
  166. //~ if (current == 'B' || current == 'F' || current == 'G') current = 'A';
  167. //~ if (current == 'A') {
  168. //~ // Check for directives, comments, strings, and keywords...
  169. //~ if (col == 0 && text[itext] == '#') {
  170. //~ // Set style to directive
  171. //~ current = 'E';
  172. //~ } else if (strncmp(text, "//", 2) == 0) {
  173. //~ current = 'B';
  174. //~ for (; length > 0 && text[itext] != '\n'; length --, itext ++) istyle++ = 'B';
  175. //~ if (length == 0) break;
  176. //~ } else if (strncmp(text, "/*", 2) == 0) {
  177. //~ current = 'C';
  178. //~ } else if (strncmp(text, "\\\"", 2) == 0) {
  179. //~ // Quoted quote...
  180. //~ style[istyle++] = current;
  181. //~ style[style++] = current;
  182. //~ itext ++;
  183. //~ length --;
  184. //~ col += 2;
  185. //~ continue;
  186. //~ } else if (text[itext] == '\"') {
  187. //~ current = 'D';
  188. //~ } else if (!last && (islower((text[itext])&255) || text[itext] == '_')) {
  189. //~ // Might be a keyword...
  190. //~ for (temp = text[itext], bufptr = buf;
  191. //~ (islower((temp)&255) || temp == '_') && bufptr < (buf + sizeof(buf) - 1);
  192. //~ *bufptr++ = *temp++);
  193. //~ if (!islower((*temp)&255) && *temp != '_') {
  194. //~ *bufptr = '\0';
  195. //~ bufptr = buf;
  196. //~ if (bsearch(&bufptr, code_types,
  197. //~ sizeof(code_types) / sizeof(code_types[0]),
  198. //~ sizeof(code_types[0]), compare_keywords)) {
  199. //~ while (text < temp) {
  200. //~ style[istyle++] = 'F';
  201. //~ itext ++;
  202. //~ length --;
  203. //~ col ++;
  204. //~ }
  205. //~ itext --;
  206. //~ length ++;
  207. //~ last = 1;
  208. //~ continue;
  209. //~ } else if (bsearch(&bufptr, code_keywords,
  210. //~ sizeof(code_keywords) / sizeof(code_keywords[0]),
  211. //~ sizeof(code_keywords[0]), compare_keywords)) {
  212. //~ while (text < temp) {
  213. //~ style[istyle++] = 'G';
  214. //~ itext ++;
  215. //~ length --;
  216. //~ col ++;
  217. //~ }
  218. //~ itext --;
  219. //~ length ++;
  220. //~ last = 1;
  221. //~ continue;
  222. //~ }
  223. //~ }
  224. //~ }
  225. //~ } else if (current == 'C' && strncmp(text, "*/", 2) == 0) {
  226. //~ // Close a C comment...
  227. //~ style[istyle++] = current;
  228. //~ istyle++ = current;
  229. //~ itext ++;
  230. //~ length --;
  231. //~ current = 'A';
  232. //~ col += 2;
  233. //~ continue;
  234. //~ } else if (current == 'D') {
  235. //~ // Continuing in string...
  236. //~ if (strncmp(text[itext], "\\\"", 2) == 0) {
  237. //~ // Quoted end quote...
  238. //~ style[istyle++] = current;
  239. //~ style[istyle++] = current;
  240. //~ itext ++;
  241. //~ length --;
  242. //~ col += 2;
  243. //~ continue;
  244. //~ } else if (text[itext] == '\"') {
  245. //~ // End quote...
  246. //~ style[istyle++] = current;
  247. //~ col ++;
  248. //~ current = 'A';
  249. //~ continue;
  250. //~ }
  251. //~ }
  252. //~ // Copy style info...
  253. //~ if (current == 'A' && (text[itext] == '{' || text[itext] == '}')) style[style++] = 'G';
  254. //~ else style[istyle++] = current;
  255. //~ col ++;
  256. //~ last = isalnum((text[itext])&255) || text[itext] == '_' || text[itext] == '.';
  257. //~ if (text[itext] == '\n') {
  258. //~ // Reset column and possibly reset the style
  259. //~ col = 0;
  260. //~ if (current == 'B' || current == 'E') current = 'A';
  261. //~ }
  262. //~ }
  263. }
  264. //
  265. // 'style_init()' - Initialize the style buffer...
  266. //
  267. //void
  268. function style_init() {
  269. local style = new blob(textbuf->length()+1);
  270. local text = textbuf->text();
  271. style.memset(0, 'A', textbuf->length());
  272. style[textbuf->length()] = '\0';
  273. if (!stylebuf) stylebuf = new Fl_Text_Buffer(textbuf->length());
  274. style_parse(text, style, textbuf->length());
  275. stylebuf->text(style.tostring());
  276. //delete[] style;
  277. //free(text);
  278. }
  279. //
  280. // 'style_unfinished_cb()' - Update unfinished styles.
  281. //
  282. //void
  283. function style_unfinished_cb(pint, pvoid) {
  284. }
  285. //
  286. // 'style_update()' - Update the style buffer...
  287. //
  288. //void
  289. function style_update(pos, // I - Position of update
  290. nInserted, // I - Number of inserted chars
  291. nDeleted, // I - Number of deleted chars
  292. nRestyled, // I - Number of restyled chars
  293. deletedText,// I - Text that was deleted
  294. cbArg) { // I - Callback data
  295. local start, // Start of text
  296. end; // End of text
  297. local last, // Last style on line
  298. style, // Style data
  299. text; // Text data
  300. // If this is just a selection change, just unselect the style buffer...
  301. if (nInserted == 0 && nDeleted == 0) {
  302. stylebuf->unselect();
  303. return;
  304. }
  305. // Track changes in the text buffer...
  306. if (nInserted > 0) {
  307. // Insert characters into the style buffer...
  308. style = new char[nInserted + 1];
  309. memset(style, 'A', nInserted);
  310. style[nInserted] = '\0';
  311. stylebuf->replace(pos, pos + nDeleted, style);
  312. //delete[] style;
  313. } else {
  314. // Just delete characters in the style buffer...
  315. stylebuf->remove(pos, pos + nDeleted);
  316. }
  317. // Select the area that was just updated to avoid unnecessary
  318. // callbacks...
  319. stylebuf->select(pos, pos + nInserted - nDeleted);
  320. // Re-parse the changed region; we do this by parsing from the
  321. // beginning of the previous line of the changed region to the end of
  322. // the line of the changed region... Then we check the last
  323. // style character and keep updating if we have a multi-line
  324. // comment character...
  325. start = textbuf->line_start(pos);
  326. // if (start > 0) start = textbuf->line_start(start - 1);
  327. end = textbuf->line_end(pos + nInserted);
  328. text = textbuf->text_range(start, end);
  329. style = stylebuf->text_range(start, end);
  330. if (start==end)
  331. last = 0;
  332. else
  333. last = style[end - start - 1];
  334. // printf("start = %d, end = %d, text = \"%s\", style = \"%s\", last='%c'...\n",
  335. // start, end, text, style, last);
  336. style_parse(text, style, end - start);
  337. // printf("new style = \"%s\", new last='%c'...\n",
  338. // style, style[end - start - 1]);
  339. stylebuf->replace(start, end, style);
  340. /*((Fl_Text_Editor *)*/cbArg->redisplay_range(start, end);
  341. if (start==end || last != style[end - start - 1]) {
  342. // printf("Recalculate the rest of the buffer style\n");
  343. // Either the user deleted some text, or the last character
  344. // on the line changed styles, so reparse the
  345. // remainder of the buffer...
  346. //free(text);
  347. //free(style);
  348. end = textbuf->length();
  349. text = textbuf->text_range(start, end);
  350. style = stylebuf->text_range(start, end);
  351. style_parse(text, style, end - start);
  352. stylebuf->replace(start, end, style);
  353. /*((Fl_Text_Editor *)*/cbArg->redisplay_range(start, end);
  354. }
  355. //free(text);
  356. //free(style);
  357. }
  358. // Editor window functions and class...
  359. /*
  360. void save_cb();
  361. void saveas_cb();
  362. void find2_cb(Fl_Widget*, void*);
  363. void replall_cb(Fl_Widget*, void*);
  364. void replace2_cb(Fl_Widget*, void*);
  365. void replcan_cb(Fl_Widget*, void*);
  366. */
  367. class EditorWindow extends Fl_Double_Window {
  368. //public:
  369. replace_dlg = null;
  370. replace_find = null;
  371. replace_with = null;
  372. replace_all = null;
  373. replace_next = null;
  374. replace_cancel = null;
  375. editor = null;
  376. search = null;
  377. constructor(iw, ih, t) {
  378. base.constructor(iw, ih, t);
  379. replace_dlg = new Fl_Window(300, 105, "Replace");
  380. replace_find = new Fl_Input(80, 10, 210, 25, "Find:");
  381. replace_find->align(FL_ALIGN_LEFT);
  382. replace_with = new Fl_Input(80, 40, 210, 25, "Replace:");
  383. replace_with->align(FL_ALIGN_LEFT);
  384. replace_all = new Fl_Button(10, 70, 90, 25, "Replace All");
  385. replace_all->callback(replall_cb, this);
  386. replace_next = new Fl_Return_Button(105, 70, 120, 25, "Replace Next");
  387. replace_next->callback(replace2_cb, this);
  388. replace_cancel = new Fl_Button(230, 70, 60, 25, "Cancel");
  389. replace_cancel->callback(replcan_cb, this);
  390. replace_dlg->end();
  391. replace_dlg->set_non_modal();
  392. editor = 0;
  393. search = "";
  394. }
  395. //destructor() {
  396. // delete replace_dlg;
  397. //}
  398. };
  399. function check_save() {
  400. if (!changed) return 1;
  401. local r = fl_choice("The current file has not been saved.\nWould you like to save it now?",
  402. "Cancel", "Save", "Don't Save");
  403. if (r == 1) {
  404. save_cb(); // Save the file...
  405. return !changed;
  406. }
  407. return (r == 2) ? 1 : 0;
  408. }
  409. local loading = 0;
  410. function load_file(newfile, ipos) {
  411. loading = 1;
  412. local insert = (ipos != -1);
  413. changed = insert;
  414. if (!insert) filename = "";
  415. local r;
  416. if (!insert) r = textbuf->loadfile(newfile);
  417. else r = textbuf->insertfile(newfile, ipos);
  418. changed = changed || textbuf->input_file_was_transcoded;
  419. if (r)
  420. fl_alert("Error reading from file \'%s\':\n%s.", newfile, strerror(errno));
  421. else
  422. if (!insert) filename = newfile;
  423. loading = 0;
  424. //textbuf->call_modify_callbacks();
  425. }
  426. function save_file(newfile) {
  427. if (textbuf->savefile(newfile))
  428. fl_alert("Error writing to file \'%s\':\n%s.", newfile, strerror(errno));
  429. else
  430. filename = newfile;
  431. changed = 0;
  432. textbuf->call_modify_callbacks();
  433. }
  434. function copy_cb(sender, e) {
  435. Fl_Text_Editor.kf_copy(0, e->editor);
  436. }
  437. function cut_cb(sender, e) {
  438. Fl_Text_Editor.kf_cut(0, e->editor);
  439. }
  440. function delete_cb(sender, v) {
  441. textbuf->remove_selection();
  442. }
  443. function find_cb(w, e) {
  444. local val;
  445. val = fl_input("Search String:", e->search);
  446. if (val != null) {
  447. // User entered a string - go find it!
  448. e->search = val;
  449. find2_cb(w, v);
  450. }
  451. }
  452. function find2_cb(w, e) {
  453. if (e->search.len() == 0) {
  454. // Search string is blank; get a new one...
  455. find_cb(w, v);
  456. return;
  457. }
  458. local pos = e->editor->insert_position();
  459. local found = textbuf->search_forward(pos, e->search/*, &pos*/);
  460. if (found) {
  461. // Found a match; select and update the position...
  462. textbuf->select(pos, pos+strlen(e->search));
  463. e->editor->insert_position(pos+e->search.len());
  464. e->editor->show_insert_position();
  465. }
  466. else fl_alert("No occurrences of \'%s\' found!", e->search);
  467. }
  468. function set_title(w) {
  469. if (filename.len() == 0) title = "Untitled";
  470. else {
  471. local slash = filename.rfind("/");
  472. if( WIN32 && slash == null) slash = filename.find("\\");
  473. if (slash != null) title = filename.slice(slash + 1);
  474. else title = filename;
  475. }
  476. if (changed) title += " (modified)";
  477. w->label(title);
  478. }
  479. function changed_cb(iparam, nInserted, nDeleted, iparam2, strparam, w) {
  480. if ((nInserted || nDeleted) && !loading) changed = 1;
  481. set_title(w);
  482. if (loading) w->editor->show_insert_position();
  483. }
  484. function new_cb(sender, udata) {
  485. if (!check_save()) return;
  486. filename = "";
  487. textbuf->select(0, textbuf->length());
  488. textbuf->remove_selection();
  489. changed = 0;
  490. textbuf->call_modify_callbacks();
  491. }
  492. function open_cb(sender, udata) {
  493. if (!check_save()) return;
  494. local fnfc = Fl_Native_File_Chooser();
  495. fnfc.title("Open file");
  496. fnfc.type(Fl_Native_File_Chooser.BROWSE_FILE);
  497. if ( fnfc.show() ) return;
  498. load_file(fnfc.filename(), -1);
  499. }
  500. function insert_cb(sender, w) {
  501. local fnfc = Fl_Native_File_Chooser();
  502. fnfc.title("Insert file");
  503. fnfc.type(Fl_Native_File_Chooser.BROWSE_FILE);
  504. if ( fnfc.show() ) return;
  505. load_file(fnfc.filename(), w->editor->insert_position());
  506. }
  507. function paste_cb(sender, e) {
  508. Fl_Text_Editor.kf_paste(0, e->editor);
  509. }
  510. local num_windows = 0;
  511. function close_cb(sender, w) {
  512. if (num_windows == 1) {
  513. if (!check_save())
  514. return;
  515. }
  516. w->hide();
  517. w->editor->buffer(0);
  518. textbuf->remove_modify_callback(style_update, w->editor);
  519. textbuf->remove_modify_callback(changed_cb, w);
  520. Fl.delete_widget(w);
  521. num_windows--;
  522. if (!num_windows) os.exit(0);
  523. }
  524. function quit_cb(sender, udata) {
  525. if (changed && !check_save())
  526. return;
  527. os.exit(0);
  528. }
  529. function replace_cb(sender, e) {
  530. e->replace_dlg->show();
  531. }
  532. function replace2_cb(sender, e) {
  533. local find = e->replace_find->value();
  534. local replace = e->replace_with->value();
  535. if (find.len() == 0) {
  536. // Search string is blank; get a new one...
  537. e->replace_dlg->show();
  538. return;
  539. }
  540. e->replace_dlg->hide();
  541. local pos = e->editor->insert_position();
  542. local found = textbuf->search_forward(pos, find /*, &pos*/);
  543. if (found) {
  544. // Found a match; update the position and replace text...
  545. textbuf->select(pos, pos+strlen(find));
  546. textbuf->remove_selection();
  547. textbuf->insert(pos, replace);
  548. textbuf->select(pos, pos+replace.len());
  549. e->editor->insert_position(pos+replace.len());
  550. e->editor->show_insert_position();
  551. }
  552. else fl_alert("No occurrences of \'%s\' found!", find);
  553. }
  554. function replall_cb(sender, e) {
  555. local find = e->replace_find->value();
  556. local replace = e->replace_with->value();
  557. find = e->replace_find->value();
  558. if (find.len() == 0) {
  559. // Search string is blank; get a new one...
  560. e->replace_dlg->show();
  561. return;
  562. }
  563. e->replace_dlg->hide();
  564. e->editor->insert_position(0);
  565. local times = 0;
  566. // Loop through the whole string
  567. for (local found = 1; found;) {
  568. local pos = e->editor->insert_position();
  569. found = textbuf->search_forward(pos, find/*, &pos*/);
  570. if (found) {
  571. // Found a match; update the position and replace text...
  572. textbuf->select(pos, pos+strlen(find));
  573. textbuf->remove_selection();
  574. textbuf->insert(pos, replace);
  575. e->editor->insert_position(pos+strlen(replace));
  576. e->editor->show_insert_position();
  577. times++;
  578. }
  579. }
  580. if (times) fl_message("Replaced %d occurrences.", times);
  581. else fl_alert("No occurrences of \'%s\' found!", find);
  582. }
  583. function replcan_cb(sender, e) {
  584. e->replace_dlg->hide();
  585. }
  586. function save_cb() {
  587. if (filename.len() == 0) {
  588. // No filename - get one!
  589. saveas_cb();
  590. return;
  591. }
  592. else save_file(filename);
  593. }
  594. function saveas_cb() {
  595. local fnfc = Fl_Native_File_Chooser();
  596. fnfc.title("Save File As?");
  597. fnfc.type(Fl_Native_File_Chooser.BROWSE_SAVE_FILE);
  598. if ( fnfc.show() ) return;
  599. save_file(fnfc.filename());
  600. }
  601. function view_cb(sender, udata) {
  602. local w = new_view();
  603. w->show();
  604. }
  605. local menuitems = [
  606. [ "&File", 0, 0, 0, FL_SUBMENU ],
  607. [ "&New File", 0, new_cb ],
  608. [ "&Open File...", FL_COMMAND + 'o', open_cb ],
  609. [ "&Insert File...", FL_COMMAND + 'i', insert_cb, 0, FL_MENU_DIVIDER ],
  610. [ "&Save File", FL_COMMAND + 's', save_cb ],
  611. [ "Save File &As...", FL_COMMAND + FL_SHIFT + 's', saveas_cb, 0, FL_MENU_DIVIDER ],
  612. [ "New &View", FL_ALT + (__APPLE__ ? FL_COMMAND : 0) + 'v', view_cb, 0 ],
  613. [ "&Close View", FL_COMMAND + 'w', close_cb, 0, FL_MENU_DIVIDER ],
  614. [ "E&xit", FL_COMMAND + 'q', quit_cb, 0 ],
  615. [ 0 ],
  616. [ "&Edit", 0, 0, 0, FL_SUBMENU ],
  617. [ "Cu&t", FL_COMMAND + 'x', cut_cb ],
  618. [ "&Copy", FL_COMMAND + 'c', copy_cb ],
  619. [ "&Paste", FL_COMMAND + 'v', paste_cb ],
  620. [ "&Delete", 0, delete_cb ],
  621. [ 0 ],
  622. [ "&Search", 0, 0, 0, FL_SUBMENU ],
  623. [ "&Find...", FL_COMMAND + 'f', find_cb ],
  624. [ "F&ind Again", FL_COMMAND + 'g', find2_cb ],
  625. [ "&Replace...", FL_COMMAND + 'r', replace_cb ],
  626. [ "Re&place Again", FL_COMMAND + 't', replace2_cb ],
  627. [ 0 ],
  628. [ 0 ]
  629. ];
  630. function mergerSubMenu(prefix, amenu, curr_pos, last_pos){
  631. for(; curr_pos < last_pos; ++curr_pos){
  632. local item = amenu[curr_pos];
  633. if(!item[0]) break;
  634. if(prefix){
  635. item[0] = prefix + "/" + item[0];
  636. }
  637. if(item.len() > 4 && item[4] & FL_SUBMENU){
  638. curr_pos = mergerSubMenu(item[0], amenu, ++curr_pos, last_pos);
  639. }
  640. }
  641. return curr_pos;
  642. }
  643. mergerSubMenu(null, menuitems, 0, menuitems.len());
  644. function new_view() {
  645. local w = new EditorWindow(660, 400, title);
  646. w->begin();
  647. local m = new Fl_Menu_Bar(0, 0, 660, 30);
  648. m->copy(menuitems, w);
  649. w->editor = new Fl_Text_Editor(0, 30, 660, 370);
  650. w->editor->textfont(FL_COURIER);
  651. w->editor->textsize(TS);
  652. //w->editor->wrap_mode(Fl_Text_Editor::WRAP_AT_BOUNDS, 250);
  653. w->editor->buffer(textbuf);
  654. w->editor->highlight_data(stylebuf, styletable, styletable.len(),
  655. 'A', style_unfinished_cb, 0);
  656. textbuf->text();
  657. style_init();
  658. w->end();
  659. w->resizable(w->editor);
  660. //w->callback(close_cb, w);
  661. //textbuf->add_modify_callback(style_update, w->editor);
  662. //textbuf->add_modify_callback(changed_cb, w);
  663. //textbuf->call_modify_callbacks();
  664. num_windows++;
  665. return w;
  666. }
  667. function main(argc, argv) {
  668. textbuf = new Fl_Text_Buffer();
  669. //textbuf->transcoding_warning_action = NULL;
  670. style_init();
  671. local window = new_view();
  672. //window->show(1, argv);
  673. window->show();
  674. if (argc > 0) load_file(argv[0], -1);
  675. load_file("editor-fltk.nut", -1);
  676. return Fl.run();
  677. }
  678. main(vargv.len(), vargv);
  679. //
  680. // End of "$Id: editor.cxx 8864 2011-07-19 04:49:30Z greg.ercolano $".
  681. //