gui.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. #ifndef __MARIO_GUI
  2. #define __MARIO_GUI
  3. #include <allegro.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. // Returns state of a single bit which is on 'pos' position in tmp_b byte
  7. bool checkBit(BYTE tmp_b, int pos)
  8. {
  9. if ( tmp_b & ( 1 << pos ))
  10. {
  11. return true;
  12. }
  13. else
  14. {
  15. return false;
  16. }
  17. }
  18. void draw_SEG_digit(BITMAP *buf, int x, int y, int color, int size, BYTE segm_code)
  19. {
  20. int darkc, normc;
  21. normc = color;
  22. darkc = makecol(getr(normc) / 4, getg(normc) / 4, getb(normc) / 4);
  23. if ( checkBit(segm_code, 1))
  24. {
  25. color = normc;
  26. }
  27. else
  28. {
  29. color = darkc;
  30. }
  31. vline(buf, x + size, y, y + size, color);
  32. if ( checkBit(segm_code, 2))
  33. {
  34. color = normc;
  35. }
  36. else
  37. {
  38. color = darkc;
  39. }
  40. vline(buf, x + size, y + size, y + 2 * size, color);
  41. if ( checkBit(segm_code, 4))
  42. {
  43. color = normc;
  44. }
  45. else
  46. {
  47. color = darkc;
  48. }
  49. vline(buf, x, y + 2 * size, y + size, color);
  50. if ( checkBit(segm_code, 5))
  51. {
  52. color = normc;
  53. }
  54. else
  55. {
  56. color = darkc;
  57. }
  58. vline(buf, x, y + size, y, color);
  59. if ( checkBit(segm_code, 6))
  60. {
  61. color = normc;
  62. }
  63. else
  64. {
  65. color = darkc;
  66. }
  67. hline(buf, x, y + size, x + size, color);
  68. if ( checkBit(segm_code, 0))
  69. {
  70. color = normc;
  71. }
  72. else
  73. {
  74. color = darkc;
  75. }
  76. hline(buf, x, y, x + size, color);
  77. if ( checkBit(segm_code, 3))
  78. {
  79. color = normc;
  80. }
  81. else
  82. {
  83. color = darkc;
  84. }
  85. hline(buf, x + size, y + 2 * size, x, color);
  86. if ( checkBit(segm_code, 7))
  87. {
  88. color = normc;
  89. }
  90. else
  91. {
  92. color = darkc;
  93. }
  94. putpixel(buf, x + size + 2, y + 2 * size, color);
  95. }
  96. void draw_LED_bin(BITMAP *buf, int x, int y, int color, BYTE code)
  97. {
  98. int darkc, normc;
  99. int i;
  100. normc = color;
  101. darkc = makecol(getr(normc) / 4, getg(normc) / 4, getb(normc) / 4);
  102. for ( i = 0 ; i < 8 ; i++ )
  103. {
  104. if ( checkBit(code, 7 - i))
  105. {
  106. color = normc;
  107. }
  108. else
  109. {
  110. color = darkc;
  111. }
  112. circlefill(buf, x + 7 * i, y, 2, color);
  113. }
  114. }
  115. void change_char(char *string, char ch, int pos)
  116. {
  117. string[pos - 1] = ch;
  118. }
  119. void insert_char(char *string, char ch, int pos, int lenght)
  120. {
  121. for ( int c = lenght ; c > pos ; c-- )
  122. {
  123. string[c - 1] = string[c - 2];
  124. }
  125. string[pos - 1] = ch;
  126. }
  127. void cut_char(char *string, int pos, int lenght)
  128. {
  129. for ( int c = pos ; c < lenght ; c++ )
  130. {
  131. string[c - 1] = string[c];
  132. }
  133. string[lenght - 1] = (char) ( 32 );
  134. }
  135. void change_ext(char *s)
  136. {
  137. int c;
  138. for ( c = 254 ; c >= 0 ; c-- )
  139. {
  140. if ( s[c] == '.' )
  141. {
  142. s[c + 1] = 'h';
  143. s[c + 2] = 'e';
  144. s[c + 3] = 'x';
  145. break;
  146. }
  147. }
  148. return;
  149. }
  150. void GetText(char *text, BITMAP *buf, int x, int y, int w, int h, int lenght, char *title)
  151. {
  152. int c;
  153. BITMAP *surface = create_bitmap(w, h);
  154. BITMAP *cache = create_bitmap(w, h);
  155. blit(buf, cache, x, y, 0, 0, w, h);
  156. clear(surface);
  157. hline(surface, 2, surface->h - 2, surface->w - 2, makecol(255, 255, 255));
  158. vline(surface, 2, 2, surface->h - 2, makecol(255, 255, 255));
  159. vline(surface, surface->w - 2, 2, surface->h - 2, makecol(255, 255, 255));
  160. for ( c = 2 ; c <= 2 + 10 ; c += 2 )
  161. {
  162. hline(surface, 2, c, surface->w - 2, makecol(255, 255, 255));
  163. }
  164. textprintf_centre(surface, mainf, surface->w / 2, 4, makecol(255, 255, 255), " %s ", title);
  165. text[0] = 32;
  166. for ( c = 1 ; c <= lenght - 1 ; c++ )
  167. {
  168. text[c] = 32;
  169. }
  170. text[lenght - 1] = 0;
  171. clear_keybuf();
  172. int pos = 1;
  173. WORD key_p;
  174. WORD key_ch;
  175. bool flag;
  176. while ( key[KEY_ENTER] )
  177. {
  178. }
  179. while ( !key[KEY_ENTER] )
  180. {
  181. textprintf(surface, mainf, 15, surface->h / 2, makecol(255, 255, 255), "%s", text);
  182. hline(surface, 15 + ( pos - 1 ) * 8, surface->h / 2 + 8, 15 + pos * 8, makecol(255, 255, 255));
  183. blit(surface, buf, 0, 0, x, y, w, h);
  184. hline(surface, 15 + ( pos - 1 ) * 8, surface->h / 2 + 8, 15 + pos * 8, makecol(0, 0, 0));
  185. key_p = readkey();
  186. key_ch = key_p & 0xff;
  187. key_p = key_p >> 8;
  188. flag = false;
  189. switch ( key_p )
  190. {
  191. case KEY_BACKSPACE:
  192. if ( pos > 1 )
  193. {
  194. pos--;
  195. cut_char(text, pos, lenght);
  196. text[lenght - 2] = 32;
  197. text[lenght - 1] = 0;
  198. }
  199. flag = true;
  200. break;
  201. case KEY_DEL:
  202. cut_char(text, pos, lenght);
  203. text[lenght - 2] = 32;
  204. text[lenght - 1] = 0;
  205. flag = true;
  206. break;
  207. case KEY_LEFT:
  208. if ( pos > 1 )
  209. { pos--; }
  210. flag = true;
  211. break;
  212. case KEY_RIGHT:
  213. if ( pos < lenght )
  214. { pos++; }
  215. flag = true;
  216. break;
  217. }
  218. if ( !key[KEY_ENTER] && key_ch != 0 && pos < lenght && !flag )
  219. {
  220. insert_char(text, key_ch, pos, lenght);
  221. pos++;
  222. text[lenght - 1] = 0;
  223. }
  224. }
  225. text[lenght - 1] = 0;
  226. blit(cache, buf, 0, 0, x, y, w, h);
  227. destroy_bitmap(cache);
  228. destroy_bitmap(surface);
  229. }
  230. void ShowMessage(char *text, BITMAP *buf, int x, int y, int w, int h, char *title)
  231. {
  232. BITMAP *surface = create_bitmap(w, h);
  233. BITMAP *cache = create_bitmap(w, h);
  234. blit(buf, cache, x, y, 0, 0, w, h);
  235. clear(surface);
  236. hline(surface, 2, surface->h - 2, surface->w - 2, makecol(255, 255, 255));
  237. vline(surface, 2, 2, surface->h - 2, makecol(255, 255, 255));
  238. vline(surface, surface->w - 2, 2, surface->h - 2, makecol(255, 255, 255));
  239. for ( int c = 2 ; c <= 2 + 10 ; c += 2 )
  240. {
  241. hline(surface, 2, c, surface->w - 2, makecol(255, 255, 255));
  242. }
  243. textprintf_centre(surface, mainf, surface->w / 2, 4, makecol(255, 255, 255), " %s ", title);
  244. textprintf(surface, mainf, 15, surface->h / 2, makecol(255, 255, 255), "%s", text);
  245. textprintf_centre(surface, mainf, surface->w / 2, surface->h - 16, makecol(255, 0, 0), "Press ENTER");
  246. blit(surface, buf, 0, 0, x, y, w, h);
  247. while ( key[KEY_ENTER] )
  248. {
  249. }
  250. while ( !key[KEY_ENTER] )
  251. {
  252. }
  253. blit(cache, buf, 0, 0, x, y, w, h);
  254. destroy_bitmap(cache);
  255. destroy_bitmap(surface);
  256. }
  257. bool QuestionBox(char *text, BITMAP *buf, int x, int y, int w, int h, char *title, int _color)
  258. {
  259. BITMAP *surface = create_bitmap(w, h);
  260. BITMAP *cache = create_bitmap(w, h);
  261. int len, c, line = 0, pos = 0;
  262. blit(buf, cache, x, y, 0, 0, w, h);
  263. clear(surface);
  264. hline(surface, 2, surface->h - 2, surface->w - 2, makecol(255, 255, 255));
  265. vline(surface, 2, 2, surface->h - 2, makecol(255, 255, 255));
  266. vline(surface, surface->w - 2, 2, surface->h - 2, makecol(255, 255, 255));
  267. for ( c = 2 ; c <= 2 + 10 ; c += 2 )
  268. {
  269. hline(surface, 2, c, surface->w - 2, makecol(255, 255, 255));
  270. }
  271. textprintf_centre(surface, mainf, surface->w / 2, 4, makecol(255, 255, 255), " %s ", title);
  272. textprintf_centre(surface, mainf, surface->w / 2, surface->h - 16, makecol(255, 0, 0),
  273. "Press ENTER (OK) or ESC (Cancel)");
  274. len = (int) strlen(text);
  275. for ( c = 0 ; c < len ; c++ )
  276. {
  277. if ( text[c] == 10 )
  278. {
  279. line++;
  280. pos = 0;
  281. }
  282. else
  283. {
  284. if ( text[c] != 13 )
  285. {
  286. if ( text[c] > 126 || text[c] < 32 )
  287. { break; }
  288. textprintf(surface, mainf, 15 + pos * 8, 20 + line * 10, _color, "%c", text[c]);
  289. pos++;
  290. }
  291. }
  292. }
  293. blit(surface, buf, 0, 0, x, y, w, h);
  294. while ( key[KEY_ENTER] || key[KEY_ESC] )
  295. {
  296. }
  297. while ( !( key[KEY_ENTER] || key[KEY_ESC] ))
  298. {
  299. }
  300. if ( key[KEY_ENTER] )
  301. {
  302. return true;
  303. }
  304. else
  305. {
  306. return false;
  307. }
  308. blit(cache, buf, 0, 0, x, y, w, h);
  309. destroy_bitmap(cache);
  310. destroy_bitmap(surface);
  311. }
  312. void ShowMsgEx(char *text, BITMAP *buf, int x, int y, int w, int h, char *title, int _color)
  313. {
  314. BITMAP *surface = create_bitmap(w, h);
  315. BITMAP *cache = create_bitmap(w, h);
  316. int len, c, line = 0, pos = 0;
  317. blit(buf, cache, x, y, 0, 0, w, h);
  318. clear(surface);
  319. hline(surface, 2, surface->h - 2, surface->w - 2, makecol(255, 255, 255));
  320. vline(surface, 2, 2, surface->h - 2, makecol(255, 255, 255));
  321. vline(surface, surface->w - 2, 2, surface->h - 2, makecol(255, 255, 255));
  322. for ( c = 2 ; c <= 2 + 10 ; c += 2 )
  323. {
  324. hline(surface, 2, c, surface->w - 2, makecol(255, 255, 255));
  325. }
  326. textprintf_centre(surface, mainf, surface->w / 2, 4, makecol(255, 255, 255), " %s ", title);
  327. textprintf_centre(surface, mainf, surface->w / 2, surface->h - 16, makecol(255, 0, 0), "Press ENTER");
  328. len = (int) strlen(text);
  329. for ( c = 0 ; c < len ; c++ )
  330. {
  331. if ( text[c] == 10 )
  332. {
  333. line++;
  334. pos = 0;
  335. }
  336. else
  337. {
  338. if ( text[c] != 13 )
  339. {
  340. if ( text[c] > 126 || text[c] < 32 )
  341. { break; }
  342. textprintf(surface, mainf, 15 + pos * 8, 20 + line * 10, _color, "%c", text[c]);
  343. pos++;
  344. }
  345. }
  346. }
  347. blit(surface, buf, 0, 0, x, y, w, h);
  348. while ( key[KEY_ENTER] )
  349. {
  350. }
  351. while ( !key[KEY_ENTER] )
  352. {
  353. }
  354. blit(cache, buf, 0, 0, x, y, w, h);
  355. destroy_bitmap(cache);
  356. destroy_bitmap(surface);
  357. }
  358. class code_editor
  359. {
  360. private:
  361. char *text_edit[65536];
  362. int lines;
  363. int cl, cp;
  364. int scl, scp;
  365. int _w, _h;
  366. public:
  367. code_editor(void);
  368. ~code_editor(void);
  369. bool alloc_new_line(void); // ret. true on success
  370. bool insert_new_line(int ln);
  371. void delete_last(void);
  372. void cut_line(int ln);
  373. void insert_char(int ln, char ch);
  374. void replace_char(int ln, char ch);
  375. void backspace(void);
  376. void del_ch(int ln);
  377. void draw_cr(void);
  378. void clear_cr(void);
  379. void load_source(char *filename);
  380. void save_source(char *filename);
  381. int compile(int *err); // out-num errors, in-table of int's
  382. // thet represtents lines with errors
  383. // (automactly allocated)
  384. int left, top;
  385. int tab_size;
  386. BITMAP *surface;
  387. void process(void);
  388. void disp(void);
  389. };
  390. void code_editor::load_source(char *filename)
  391. {
  392. FILE *strin;
  393. int c, i, tp, j = 0;
  394. strin = NULL;
  395. strin = fopen(filename, "rb");
  396. lines = 0;
  397. if ( strin == NULL )
  398. { ShowMessage("Can't open the file", screen, 200, 200, 200, 60, "Message!"); }
  399. else
  400. {
  401. //for (c=0;c<65535;c++)
  402. //{
  403. // if (text_edit[c]!=NULL) delete[] text_edit[c];
  404. // else break;
  405. //}
  406. while ( !feof(strin))
  407. {
  408. alloc_new_line();
  409. for ( i = 0 ; ( i < 256 ) && !feof(strin) ; i++ )
  410. {
  411. c = fgetc(strin);
  412. if ( c == 9 ) //TAB
  413. {
  414. tp = i / tab_size;
  415. tp++;
  416. tp *= 8;
  417. for ( i ; i <= tp ; i++ )
  418. { text_edit[j][i] = 32; }
  419. i--;
  420. }
  421. else
  422. {
  423. if (( c == 10 ) || ( c == 13 ))
  424. {
  425. if ( c == 13 )
  426. { fgetc(strin); }
  427. for ( i ; i < 256 ; i++ )
  428. {
  429. text_edit[j][i] = 10;
  430. }
  431. }
  432. else
  433. { text_edit[j][i] = c == -1 ? 10 : c; }
  434. }
  435. }
  436. j++;
  437. }
  438. lines = j;
  439. fclose(strin);
  440. }
  441. cp = 0;
  442. cl = 0;
  443. scp = 0;
  444. scl = 0;
  445. disp();
  446. }
  447. void code_editor::disp(void)
  448. {
  449. int i, j, cnti, cntj = 0;
  450. for ( j = cl - scl ; j < cl - scl + _h + 1 && cntj < lines ; j++ )
  451. {
  452. cnti = 0;
  453. bool endf = false;
  454. for ( i = cp - scp ; i < cp - scp + _w && !endf ; i++ )
  455. {
  456. if ( text_edit[j][i] == 10 || text_edit[j][i] == 0 || text_edit[j][i] == 13 )
  457. { endf = true; }
  458. else
  459. {
  460. textprintf(surface, mainf, left + cnti * 8, top + cntj * 10, makecol(255, 255, 255), "%c",
  461. text_edit[j][i]);
  462. cnti++;
  463. }
  464. }
  465. for ( i ; i < cp - scp + _w + 1 ; i++ )
  466. {
  467. textprintf(surface, mainf, left + cnti * 8, top + cntj * 10, makecol(255, 255, 255), " ");
  468. cnti++;
  469. }
  470. cntj++;
  471. }
  472. if ( cntj < _h )
  473. { rectfill(surface, left, top + cntj * 10, surface->w - left, surface->h - left, 0); }
  474. }
  475. void code_editor::process(void)
  476. {
  477. BITMAP *cache;
  478. int c, i;
  479. bool exit_ed = false;
  480. char *filen;
  481. filen = new char[256];
  482. cache = create_bitmap(SCREEN_W, SCREEN_H);
  483. blit(screen, cache, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
  484. clear(surface);
  485. hline(surface, 2, surface->h - 2, surface->w - 2, makecol(255, 255, 255));
  486. vline(surface, 2, 2, surface->h - 2, makecol(255, 255, 255));
  487. vline(surface, surface->w - 2, 2, surface->h - 2, makecol(255, 255, 255));
  488. for ( c = 2 ; c <= 2 + 10 ; c += 2 )
  489. {
  490. hline(surface, 2, c, surface->w - 2, makecol(255, 255, 255));
  491. }
  492. textprintf_centre(surface, mainf, surface->w / 2, 4, makecol(255, 255, 255), " Source editor ");
  493. lines = 0;
  494. alloc_new_line();
  495. WORD key_p;
  496. WORD key_ch;
  497. while ( !exit_ed )
  498. {
  499. key_p = readkey();
  500. key_ch = key_p & 0xff;
  501. key_p = key_p >> 8;
  502. switch ( key_p )
  503. {
  504. case KEY_BACKSPACE:
  505. if ( cp > 0 )
  506. {
  507. clear_cr();
  508. if ( scp > 0 )
  509. { scp--; }
  510. cp--;
  511. backspace();
  512. }
  513. else
  514. {
  515. if ( cl > 0 )
  516. {
  517. clear_cr();
  518. for ( c = 0 ; ( c < 256 ) && ( text_edit[cl - 1][c] != 10 ) ; c++ )
  519. {
  520. }
  521. cp = c;
  522. scp = cp;
  523. if ( scp >= _w )
  524. { scp = _w - 1; }
  525. for ( c ; c < 256 ; c++ )
  526. {
  527. text_edit[cl - 1][c] = text_edit[cl][c - cp];
  528. if ( text_edit[cl][c - cp] == 10 )
  529. { break; }
  530. }
  531. delete[]text_edit[cl];
  532. for ( i = cl ; i < lines ; i++ )
  533. { text_edit[i] = text_edit[i + 1]; }
  534. lines--;
  535. text_edit[lines] = NULL;
  536. cl--;
  537. if ( scl > 0 )
  538. { scl--; }
  539. }
  540. }
  541. break;
  542. case KEY_DEL:
  543. backspace();
  544. break;
  545. case KEY_ENTER:
  546. if ( cl < 65536 )
  547. {
  548. clear_cr();
  549. cl++;
  550. if ( scl < _h )
  551. { scl++; }
  552. if ( !insert_new_line(cl))
  553. {
  554. ShowMessage("Out of memory", surface, 200, 100, 200, 60, "Error!");
  555. }
  556. else
  557. {
  558. for ( c = cp ; c < 256 ; c++ )
  559. {
  560. text_edit[cl][c - cp] = text_edit[cl - 1][c];
  561. text_edit[cl - 1][c] = 10;
  562. }
  563. cp = 0;
  564. scp = 0;
  565. }
  566. }
  567. else
  568. { ShowMessage("Source can't has more then 65536 lines of code!!!", surface, 200, 100, 200, 60, "Error!"); }
  569. break;
  570. case KEY_END:
  571. clear_cr();
  572. for ( cp = 0 ; cp < 256 && text_edit[cl][cp] != 10 ; cp++ )
  573. {
  574. }
  575. scp = cp;
  576. if ( scp >= _w )
  577. { scp = _w - 1; }
  578. break;
  579. case KEY_HOME:
  580. clear_cr();
  581. cp = 0;
  582. scp = 0;
  583. break;
  584. case KEY_LEFT:
  585. if ( cp > 0 )
  586. {
  587. clear_cr();
  588. cp--;
  589. if ( scp > 0 )
  590. { scp--; }
  591. }
  592. else
  593. {
  594. if ( cl > 0 )
  595. {
  596. clear_cr();
  597. for ( c = 0 ; ( c < 256 ) && ( text_edit[cl - 1][c] != 10 ) ; c++ )
  598. {
  599. }
  600. cp = c;
  601. scp = cp;
  602. if ( scp >= _w )
  603. { scp = _w - 1; }
  604. cl--;
  605. if ( scl > 0 )
  606. { scl--; }
  607. }
  608. }
  609. break;
  610. case KEY_RIGHT:
  611. if ( cp < 256 && text_edit[cl][cp] != 10 )
  612. {
  613. clear_cr();
  614. cp++;
  615. if ( scp < _w )
  616. { scp++; }
  617. }
  618. else
  619. {
  620. if ( cl + 1 < lines )
  621. {
  622. clear_cr();
  623. cp = 0;
  624. scp = cp;
  625. if ( scp >= _w )
  626. { scp = _w - 1; }
  627. cl++;
  628. if ( scl < _h )
  629. { scl++; }
  630. }
  631. }
  632. break;
  633. case KEY_UP:
  634. if ( cl > 0 )
  635. {
  636. clear_cr();
  637. cl--;
  638. if ( scl > 0 )
  639. { scl--; }
  640. for ( c = 0 ; c < 256 && text_edit[cl][c] != 10 ; c++ )
  641. {
  642. }
  643. if ( c - 1 < cp )
  644. {
  645. cp = c;
  646. scp = cp;
  647. if ( scp >= _w )
  648. { scp = _w - 1; }
  649. }
  650. }
  651. break;
  652. case KEY_DOWN:
  653. if ( cl < 65536 && cl < ( lines - 1 ))
  654. {
  655. clear_cr();
  656. cl++;
  657. if ( scl < _h )
  658. { scl++; }
  659. for ( c = 0 ; c < 256 && text_edit[cl][c] != 10 ; c++ )
  660. {
  661. }
  662. if ( c - 1 < cp )
  663. {
  664. cp = c;
  665. scp = cp;
  666. if ( scp >= _w )
  667. { scp = _w - 1; }
  668. }
  669. }
  670. break;
  671. case KEY_ESC:
  672. if ( QuestionBox("Are you sure want to EXIT?", screen, 200, 200, 400, 60, "Confirm!", makecol(255, 0, 0)))
  673. {
  674. exit_ed = true;
  675. for ( c = 0 ; c < lines ; c++ )
  676. {
  677. if ( text_edit[c] != NULL )
  678. {
  679. delete[] text_edit[c];
  680. }
  681. }
  682. lines = 0;
  683. }
  684. else
  685. {
  686. rest(500);
  687. clear_keybuf();
  688. }
  689. break;
  690. case KEY_F3:
  691. GetText(filen, screen, 100, 200, 600, 60, 256, " Enter source code file name ");
  692. for ( c = 254 ; filen[c] == ' ' || filen[c] == '\r' ; c-- )
  693. {
  694. filen[c] = 0;
  695. }
  696. load_source(filen);
  697. break;
  698. default:
  699. if ( key_ch != 0 && cp < 256 )
  700. {
  701. insert_char(cl, key_ch);
  702. clear_cr();
  703. cp++;
  704. if ( scp < _w )
  705. { scp++; }
  706. key_ch = 0;
  707. }
  708. break;
  709. }
  710. if ( !exit_ed )
  711. {
  712. disp();
  713. draw_cr();
  714. }
  715. textprintf(surface, mainf, 1, 1, makecol(255, 255, 255), " p%d l%d sp%d sl%d c%d ln%d", cp, cl, scp, scl,
  716. text_edit[cl][cp], lines);
  717. blit(surface, screen, 0, 0, 0, 0, surface->w, surface->h);
  718. }
  719. blit(cache, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
  720. destroy_bitmap(cache);
  721. delete[] filen;
  722. return;
  723. }
  724. void code_editor::insert_char(int ln, char ch)
  725. {
  726. int i;
  727. for ( i = 256 ; i > cp ; i-- )
  728. {
  729. text_edit[cl][i] = text_edit[cl][i - 1];
  730. }
  731. text_edit[cl][cp] = ch;
  732. }
  733. void code_editor::backspace(void)
  734. {
  735. int i;
  736. for ( i = cp ; i < 256 ; i++ )
  737. {
  738. text_edit[cl][i] = text_edit[cl][i + 1];
  739. }
  740. }
  741. void code_editor::draw_cr(void)
  742. {
  743. hline(surface, left + 8 * scp, top + 10 * scl + 9, left + 8 * scp + 8, makecol(255, 255, 255));
  744. }
  745. void code_editor::clear_cr(void)
  746. {
  747. hline(surface, left + 8 * scp, top + 10 * scl + 9, left + 8 * scp + 8, 0);
  748. }
  749. code_editor::code_editor(void)
  750. {
  751. int i;
  752. for ( i = 0 ; i < 65536 ; i++ )
  753. {
  754. text_edit[i] = NULL;
  755. }
  756. lines = 0;
  757. top = 24;
  758. left = 11;
  759. cl = 0;
  760. cp = 0;
  761. scl = 0;
  762. scp = 0;
  763. surface = create_bitmap(SCREEN_W, SCREEN_H - 100);
  764. _w = ( surface->w - left * 2 ) / 8 - 1;
  765. _h = ( surface->h - top - left ) / 10 - 1;
  766. tab_size = 8;
  767. }
  768. code_editor::~code_editor(void)
  769. {
  770. }
  771. bool code_editor::alloc_new_line(void)
  772. {
  773. int i;
  774. if ( lines < 65536 )
  775. {
  776. text_edit[lines] = new char[256];
  777. for ( i = 0 ; i < 256 ; i++ )
  778. { text_edit[lines][i] = 10; }
  779. lines++;
  780. return text_edit[lines] == NULL ? false : true;
  781. }
  782. return false;
  783. }
  784. bool code_editor::insert_new_line(int ln)
  785. {
  786. int i;
  787. if ( lines < 65536 )
  788. {
  789. for ( i = lines ; i > ln ; i-- )
  790. { text_edit[i] = text_edit[i - 1]; }
  791. text_edit[ln] = NULL;
  792. text_edit[ln] = new char[256];
  793. for ( i = 0 ; i < 256 ; i++ )
  794. { text_edit[ln][i] = 10; }
  795. lines++;
  796. if ( text_edit[ln] == NULL )
  797. {
  798. for ( i = ln ; i < lines ; i++ )
  799. { text_edit[i] = text_edit[i + 1]; }
  800. lines--;
  801. return false;
  802. }
  803. else
  804. { return true; }
  805. }
  806. return false;
  807. }
  808. void code_editor::delete_last(void)
  809. {
  810. lines--;
  811. delete[] text_edit[lines];
  812. text_edit[lines] = NULL;
  813. }
  814. void code_editor::cut_line(int ln)
  815. {
  816. int i;
  817. if ( text_edit[ln] != NULL )
  818. {
  819. for ( i = ln ; i < lines ; i++ )
  820. { text_edit[i] = text_edit[i + 1]; }
  821. lines--;
  822. }
  823. }
  824. #endif // __MARIO_GUI