PageRenderTime 35ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/SRC/TW/EDIT.C

http://thai-writer.googlecode.com/
C | 780 lines | 504 code | 101 blank | 175 comment | 141 complexity | 4dec2ff30567957f5923267b2ada3b77 MD5 | raw file
  1. #include "inc.h"
  2. #include "..\thai\grphc.h"
  3. /* ------------------------------------------------------------------------ */
  4. /* From attribute given, find string of ascii control code. */
  5. /* ------------------------------------------------------------------------ */
  6. void findstrcode(char fontcode[], font_attr attr)
  7. {
  8. int i = 0;
  9. if((attr & ONELINEATTR) != 0)
  10. {
  11. fontcode[i] = ONELINECODE;
  12. i++;
  13. }
  14. if((attr & SUPERATTR) != 0)
  15. {
  16. fontcode[i] = SUPERCODE;
  17. i++;
  18. }
  19. if((attr & SUBATTR) != 0)
  20. {
  21. fontcode[i] = SUBCODE;
  22. i++;
  23. }
  24. if((attr & ITALICATTR) != 0)
  25. {
  26. fontcode[i] = ITALICCODE;
  27. i++;
  28. }
  29. if((attr & BOLDATTR) != 0)
  30. {
  31. fontcode[i] = BOLDCODE;
  32. i++;
  33. }
  34. if((attr & TWOLINEATTR) != 0)
  35. {
  36. fontcode[i] = TWOLINECODE;
  37. i++;
  38. }
  39. if((attr & ENLARGEATTR) != 0)
  40. {
  41. fontcode[i] = ENLARGECODE;
  42. i++;
  43. }
  44. fontcode[i] = '\0';
  45. }
  46. /* ------------------------------------------------------------------------ */
  47. /* Find line number from line pointer given. */
  48. /* ------------------------------------------------------------------------ */
  49. unsigned findlineno(struct line_node *line)
  50. {
  51. register unsigned linenumber = 1;
  52. struct line_node *templine;
  53. templine = sentinel -> next;
  54. while(templine != line)
  55. {
  56. templine = templine -> next;
  57. linenumber++;
  58. }
  59. return(linenumber);
  60. }
  61. /* ------------------------------------------------------------------------ */
  62. /* Find line pointer from line number given. */
  63. /* ------------------------------------------------------------------------ */
  64. struct line_node *linepointer(unsigned linenum)
  65. {
  66. struct line_node *line;
  67. line = sentinel -> next;
  68. while(linenum-- > 1)
  69. {
  70. line = line -> next;
  71. }
  72. return(line);
  73. }
  74. /* ------------------------------------------------------------------------ */
  75. /* Display line from link list at row given. First column to be displayed */
  76. /* is in global variable "firstcol". */
  77. /* ------------------------------------------------------------------------ */
  78. void displine(struct line_node *line, unsigned y, unsigned linenum)
  79. {
  80. register unsigned x = 0;
  81. int count = firstcol;
  82. font_attr tempfont, curfont = 0;
  83. char *st;
  84. st = line -> text;
  85. y += wind.row;
  86. while((count > 0) && (*st != '\0'))
  87. {
  88. if(whatlevel(*st) == MIDDLE)
  89. {
  90. if(*st < 32)
  91. {
  92. togglefont(&curfont, *st);
  93. }
  94. else
  95. {
  96. if((curfont & ENLARGEATTR) == ENLARGEATTR)
  97. {
  98. count--;
  99. }
  100. count--;
  101. }
  102. }
  103. st++;
  104. }
  105. if((count < 0) && ((curfont & ENLARGEATTR) == ENLARGEATTR))
  106. {
  107. x++;
  108. }
  109. while((*st != '\0') && (x < (wind.length - 2)))
  110. {
  111. if(*st < 32)
  112. {
  113. togglefont(&curfont, *st);
  114. }
  115. else
  116. {
  117. tempfont = curfont;
  118. if(haveblock() && inblock(linenum, x + firstcol) && dispblock)
  119. {
  120. curfont = curfont | REVERSEATTR;
  121. }
  122. if(whatlevel(*st) != MIDDLE)
  123. {
  124. if(x > 0)
  125. {
  126. if((curfont & ENLARGEATTR) == ENLARGEATTR)
  127. {
  128. if(x >= 2)
  129. {
  130. prchar(*st, curfont, wind.col + x - 2, y);
  131. }
  132. }
  133. else
  134. {
  135. prchar(*st, curfont, wind.col + x - 1, y);
  136. }
  137. }
  138. }
  139. else
  140. {
  141. if((*st != ' ') || (curfont != 0))
  142. {
  143. if((curfont & ENLARGEATTR) != ENLARGEATTR)
  144. {
  145. prchar(*st, curfont, wind.col + x, y);
  146. x++;
  147. }
  148. else
  149. {
  150. prchar(*st, curfont, wind.col + x, y);
  151. x += 2;
  152. }
  153. }
  154. else /* if it's blank and normal attribute,use prblank to speed up */
  155. {
  156. prblank(wind.col + x, y);
  157. x++;
  158. }
  159. }
  160. curfont = tempfont;
  161. }
  162. st++;
  163. }
  164. clrline(wind.col + x, y, wind.col + wind.length - 1);
  165. if(line -> wrap == NO)
  166. {
  167. prchar('<', 0, wind.col + wind.length - 1, y);
  168. }
  169. if(*st != '\0')
  170. {
  171. prchar('+', 0, wind.col + wind.length - 1, y);
  172. }
  173. y -= wind.row;
  174. /*
  175. if(graphbuff[y] != NULL)
  176. paintlinegraph(graphbuff[y], y);
  177. */
  178. }
  179. /* ------------------------------------------------------------------------ */
  180. /* Display page break line at row given. */
  181. /* ------------------------------------------------------------------------ */
  182. void disppagebreak(unsigned y)
  183. {
  184. register unsigned i = 0, j;
  185. y += wind.row;
  186. j = wind.col + wind.length - 1;
  187. while(i != j)
  188. {
  189. prchar('-', 0, wind.col + i, y);
  190. i++;
  191. }
  192. prchar('P', 0, i, y);
  193. }
  194. /*
  195. void set_graphic_buffer(void)
  196. {
  197. unsigned count, i, j, m, n;
  198. unsigned linenum = lineno, linenum2;
  199. struct line_node *templine;
  200. char *gr;
  201. templine = curpage;
  202. while(templine != curline) */ /* find lineno of curpage */
  203. /* {
  204. linenum--;
  205. templine = templine -> next;
  206. }
  207. j = 0;
  208. while(j < wind.width)
  209. {
  210. if(graphbuff[j] != NULL)
  211. free(graphbuff[j]);
  212. graphbuff[j] = NULL;
  213. j++;
  214. }
  215. return;
  216. templine = curpage;
  217. count = 0;
  218. while((templine -> graph == NULL) && (count != 66) && (templine -> previous != sentinel))
  219. {
  220. templine = templine -> previous;
  221. count++;
  222. }
  223. if(templine -> graph != NULL)
  224. {
  225. gr = templine -> graph;
  226. gr += strlen(gr) + 4;
  227. while((count != 0) && (*gr != 0x80))
  228. {
  229. m = 0;
  230. while((m != 20) && (*gr != 0x80))
  231. {
  232. gr += strlen(gr) + 1;
  233. m++;
  234. }
  235. count--;
  236. }
  237. linenum2 = linenum;
  238. i = 0;
  239. while((*gr != 0x80) && (i < wind.width))
  240. {
  241. m = 0;
  242. n = 0;
  243. while((m != 20) && (gr[n] != 0x80))
  244. {
  245. n += strlen(gr + n) + 1;
  246. m++;
  247. }
  248. graphbuff[i] = (char *) malloc(n);
  249. memcpy(graphbuff[i],gr,n);
  250. gr += n;
  251. i++;
  252. linenum2++;
  253. if((pagebreak == YES) && (((linenum2 - 1) % lineperpage) == 0))
  254. {
  255. if(i != wind.width)
  256. i++;
  257. }
  258. }
  259. }
  260. templine = curpage;
  261. i = 0;
  262. while((i < wind.width) && (templine != sentinel))
  263. {
  264. if(templine -> graph != NULL)
  265. {
  266. j = i;
  267. linenum2 = linenum;
  268. gr = templine -> graph;
  269. gr += strlen(templine -> graph) + 4;
  270. while((*gr != 0x80) && (j < wind.width))
  271. {
  272. m = 0;
  273. n = 0;
  274. while((m != 20) && (gr[n] != 0x80))
  275. {
  276. n += strlen(gr + n) + 1;
  277. m++;
  278. }
  279. graphbuff[j] = (char *) malloc(n);
  280. memcpy(graphbuff[j], gr, n);
  281. gr += n;
  282. linenum2++;
  283. j++;
  284. if((pagebreak == YES) && (((linenum2 - 1) % lineperpage) == 0))
  285. {
  286. if(j != wind.width)
  287. j++;
  288. }
  289. }
  290. }
  291. templine = templine -> next;
  292. i++;
  293. linenum++;
  294. if((pagebreak == YES) && (((linenum - 1) % lineperpage) == 0))
  295. {
  296. if(i != wind.width)
  297. i++;
  298. }
  299. }
  300. }
  301. */
  302. /* ------------------------------------------------------------------------ */
  303. /* Display all line in page from linked list. Stop display if keyboard is */
  304. /* pressed,then set flag "pagecomplete" to NO, else "pagecomplete" is set */
  305. /* to YES. */
  306. /* ------------------------------------------------------------------------ */
  307. void showpage(void)
  308. {
  309. register unsigned y = 0, linenum = lineno;
  310. struct line_node *temppage = curpage;
  311. /* set_graphic_buffer(); */
  312. while(temppage != curline) /* find lineno of curpage */
  313. {
  314. linenum--;
  315. temppage = temppage -> next;
  316. }
  317. temppage = curpage;
  318. while((y != wind.width) && (temppage != sentinel))
  319. {
  320. if(keypressed())
  321. {
  322. pagecomplete = NO;
  323. return;
  324. }
  325. if(temppage != curline)
  326. {
  327. displine(temppage, y, linenum); /* display line from linked list */
  328. }
  329. else
  330. {
  331. refreshline(0, y); /* display line from workline */
  332. }
  333. temppage = temppage -> next;
  334. linenum++;
  335. y++;
  336. if((pagebreak == YES) && (((linenum - 1) % lineperpage) == 0))
  337. {
  338. if(y != wind.width)
  339. {
  340. disppagebreak(y);
  341. y++;
  342. }
  343. }
  344. }
  345. if(temppage == sentinel)
  346. {
  347. while(y != wind.width)
  348. {
  349. if(keypressed())
  350. {
  351. pagecomplete = NO;
  352. return;
  353. }
  354. clrline(wind.col, wind.row + y, wind.col + wind.length - 1);
  355. prchar('.', 0, wind.col + wind.length - 1, wind.row + y);
  356. y++;
  357. }
  358. }
  359. writetab();
  360. pagecomplete = YES;
  361. return;
  362. }
  363. /* ------------------------------------------------------------------------ */
  364. /* Show all lines in page,ignore pressing keyboard. */
  365. /* ------------------------------------------------------------------------ */
  366. void showpageall(void)
  367. {
  368. if(filename[0] != '\0')
  369. {
  370. do
  371. {
  372. showpage();
  373. if(!pagecomplete)
  374. {
  375. ebioskey(0);
  376. }
  377. }
  378. while(!pagecomplete);
  379. }
  380. else
  381. {
  382. cls();
  383. }
  384. }
  385. /* ------------------------------------------------------------------------ */
  386. /* Find value of current row from global variable "curpage" and "curline". */
  387. /* ------------------------------------------------------------------------ */
  388. unsigned findrow(void)
  389. {
  390. register unsigned row = 0, linenum = lineno;
  391. struct line_node *temppage = curpage;
  392. while(temppage != curline)
  393. {
  394. linenum--;
  395. temppage = temppage -> next;
  396. }
  397. temppage = curpage;
  398. while(temppage != curline)
  399. {
  400. if((pagebreak == YES) && (((linenum - 1) % lineperpage) == (lineperpage - 1)))
  401. {
  402. row++;
  403. }
  404. temppage = temppage -> next;
  405. linenum++;
  406. row++;
  407. }
  408. return(row);
  409. }
  410. /* ------------------------------------------------------------------------ */
  411. /* Adjust column to right position. */
  412. /* ------------------------------------------------------------------------ */
  413. void adjustcol(unsigned *x)
  414. {
  415. if((*x + firstcol) >= strlen(workline.middle))
  416. {
  417. if(firstcol > (strlen(workline.middle) - 1))
  418. {
  419. firstcol = strlen(workline.middle) - 1;
  420. pagecomplete = NO;
  421. }
  422. *x = strlen(workline.middle) - 1 - firstcol;
  423. if(*x > (wind.length - 2))
  424. {
  425. firstcol += *x - (wind.length - 2);
  426. pagecomplete = NO;
  427. }
  428. }
  429. if(*x > 0)
  430. {
  431. if(workline.below[*x + firstcol + 1] == ENLARGEATTR)
  432. (*x)--;
  433. }
  434. else
  435. {
  436. if((workline.below[*x + firstcol + 1] == ENLARGEATTR) && (firstcol != 0))
  437. {
  438. firstcol--;
  439. pagecomplete = NO;
  440. }
  441. }
  442. }
  443. /* ------------------------------------------------------------------------ */
  444. /* Load line from linked list to workline. */
  445. /* ------------------------------------------------------------------------ */
  446. void loadtoline(char *address)
  447. {
  448. register unsigned index = 1;
  449. font_attr curfont, oldfont;
  450. workline.middle[0] = ' ';
  451. while(index <= (MAXCOL + 1))
  452. {
  453. workline.topest[index] = ' ';
  454. workline.upper[index] = ' ';
  455. workline.middle[index] = ' ';
  456. workline.below[index] = ' ';
  457. workline.attr[index] = 0x00;
  458. index++;
  459. }
  460. index = 0;
  461. curfont = 0;
  462. oldfont = 0;
  463. while((*address != '\0') && (index <= MAXCOL))
  464. {
  465. if(*address < 32)
  466. {
  467. togglefont(&curfont,*address);
  468. }
  469. else
  470. {
  471. switch(whatlevel(*address))
  472. {
  473. case MIDDLE : index++;
  474. if((oldfont & ENLARGEATTR) == ENLARGEATTR)
  475. {
  476. workline.attr[index] = oldfont;
  477. workline.middle[index] = workline.middle[index - 1];
  478. workline.below[index] = ENLARGEATTR;
  479. index++;
  480. }
  481. workline.middle[index] = (*address);
  482. workline.attr[index] = curfont;
  483. break;
  484. case UPPER : workline.upper[index] = (*address);
  485. break;
  486. case TOPEST : workline.topest[index] = (*address);
  487. break;
  488. case BELOW : workline.below[index] = (*address);
  489. break;
  490. }
  491. oldfont = curfont;
  492. }
  493. address++;
  494. }
  495. index++;
  496. if((oldfont & ENLARGEATTR) == ENLARGEATTR)
  497. {
  498. workline.attr[index] = oldfont;
  499. workline.middle[index] = workline.middle[index - 1];
  500. workline.below[index] = ENLARGEATTR;
  501. index++;
  502. }
  503. workline.middle[index] = '\0';
  504. }
  505. /* ------------------------------------------------------------------------ */
  506. /* Store line from workline to linked list */
  507. /* ------------------------------------------------------------------------ */
  508. void storeline(struct line_node *curline)
  509. {
  510. unsigned count, col;
  511. static char oneline[MAXCOL * 5];
  512. char oldfont, fontcode[9];
  513. char *keep_ptr;
  514. count = 0;
  515. col = 1;
  516. oldfont = 0;
  517. while((workline.middle[col] != '\0') && (col <= MAXCOL))
  518. {
  519. if(workline.attr[col] != oldfont)
  520. {
  521. if(oldfont != 0)
  522. {
  523. findstrcode(fontcode, oldfont);
  524. strcpy(&oneline[count], fontcode);
  525. count += strlen(fontcode);
  526. }
  527. findstrcode(fontcode, workline.attr[col]);
  528. strcpy(&oneline[count], fontcode);
  529. count += strlen(fontcode);
  530. }
  531. oldfont = workline.attr[col];
  532. oneline[count] = workline.middle[col];
  533. count++;
  534. if(workline.below[col] != ' ')
  535. {
  536. oneline[count] = workline.below[col];
  537. count++;
  538. }
  539. if(workline.upper[col] != ' ')
  540. {
  541. oneline[count] = workline.upper[col];
  542. count++;
  543. }
  544. if(workline.topest[col] != ' ')
  545. {
  546. oneline[count] = workline.topest[col];
  547. count++;
  548. }
  549. if((oldfont & ENLARGEATTR) == ENLARGEATTR)
  550. {
  551. col++;
  552. }
  553. col++;
  554. }
  555. if(oldfont != 0)
  556. {
  557. findstrcode(fontcode, oldfont);
  558. strcpy(&oneline[count], fontcode);
  559. count += strlen(fontcode);
  560. }
  561. oneline[count] = '\0';
  562. keep_ptr = curline -> text;
  563. curline -> text = (char *) malloc(count + 1);
  564. if(curline -> text != NULL)
  565. {
  566. if(keep_ptr != NULL)
  567. free(keep_ptr);
  568. strcpy(curline -> text, oneline);
  569. }
  570. else
  571. {
  572. curline -> text = keep_ptr;
  573. dispstr("Internal error in storeline", 1, 1, 2);
  574. getch();
  575. }
  576. }
  577. /* ------------------------------------------------------------------------ */
  578. /* Display line from workline at row given.Start display from column given.*/
  579. /* ------------------------------------------------------------------------ */
  580. void refreshline(unsigned x, unsigned y)
  581. {
  582. register int i, len;
  583. char attr;
  584. len = wind.length - 2;
  585. y += wind.row;
  586. if(x == 0)
  587. {
  588. if(workline.below[firstcol + x + 1] == ENLARGEATTR)
  589. {
  590. prchar(' ', workline.attr[firstcol + x + 1], wind.col + x, y);
  591. x++;
  592. }
  593. }
  594. else
  595. {
  596. if(workline.below[firstcol+x] != ENLARGEATTR)
  597. {
  598. x--;
  599. }
  600. else
  601. {
  602. if(x >= 2)
  603. {
  604. x = x - 2;
  605. }
  606. }
  607. }
  608. i = firstcol + x + 1;
  609. while((x < len) && (workline.middle[i] != '\0'))
  610. {
  611. attr = workline.attr[i];
  612. if(haveblock() && inblock(lineno, i - 1) && dispblock)
  613. {
  614. attr = attr | REVERSEATTR;
  615. }
  616. if((workline.middle[i] != ' ') || (attr != 0))
  617. {
  618. prchar(workline.middle[i], attr, wind.col + x, y);
  619. if(workline.below[i] != ' ')
  620. prchar(workline.below[i], attr, wind.col + x, y);
  621. if(workline.upper[i] != ' ')
  622. prchar(workline.upper[i], attr, wind.col + x, y);
  623. if(workline.topest[i] != ' ')
  624. prchar(workline.topest[i], attr, wind.col + x, y);
  625. }
  626. else
  627. {
  628. prblank(wind.col + x, y);
  629. }
  630. if((workline.attr[i] & ENLARGEATTR) == ENLARGEATTR)
  631. {
  632. x++;
  633. }
  634. x++;
  635. i = firstcol + x + 1;
  636. }
  637. prblank(wind.col + wind.length - 2, y);
  638. if(workline.middle[i] == '\0')
  639. {
  640. for(; x < len; x++)
  641. prblank(wind.col + x, y);
  642. if(curline -> wrap == NO)
  643. {
  644. prchar('<', 0, wind.col + wind.length - 1, y);
  645. }
  646. else
  647. {
  648. prblank(wind.col + wind.length - 1, y);
  649. }
  650. }
  651. else
  652. {
  653. prchar('+', 0, wind.col + wind.length - 1, y);
  654. }
  655. y -= wind.row;
  656. /*
  657. if(graphbuff[y] != NULL)
  658. {
  659. paintlinegraph(graphbuff[y], y);
  660. }
  661. */
  662. }