editdraw.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* editor text drawing.
  2. Copyright (C) 1996, 1997 the Free Software Foundation
  3. Authors: 1996, 1997 Paul Sheer
  4. $Id$
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307, USA.
  17. */
  18. #include <config.h>
  19. #include "edit.h"
  20. #define MAX_LINE_LEN 1024
  21. #ifdef HAVE_CHARSET
  22. #include "src/charsets.h"
  23. #endif
  24. extern int column_highlighting;
  25. static void status_string (WEdit * edit, char *s, int w, int fill, int font_width)
  26. {
  27. char byte_str[16];
  28. /*
  29. * If we are at the end of file, print <EOF>,
  30. * otherwise print the current character as is (if printable),
  31. * as decimal and as hex.
  32. */
  33. if (edit->curs1 < edit->last_byte) {
  34. unsigned char cur_byte = edit_get_byte (edit, edit->curs1);
  35. g_snprintf (byte_str, sizeof(byte_str), "%c %3d 0x%02X",
  36. is_printable(cur_byte) ? cur_byte : '.',
  37. cur_byte,
  38. cur_byte);
  39. } else {
  40. strcpy(byte_str, "<EOF>");
  41. }
  42. /* The field lengths just prevent the status line from shortening too much */
  43. g_snprintf (s, w,
  44. "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb)= %s",
  45. edit->mark1 != edit->mark2 ? ( column_highlighting ? 'C' : 'B') : '-',
  46. edit->modified ? 'M' : '-',
  47. edit->macro_i < 0 ? '-' : 'R',
  48. edit->overwrite == 0 ? '-' : 'O',
  49. edit->curs_col / font_width,
  50. edit->start_line + 1,
  51. edit->curs_row,
  52. edit->curs_line + 1,
  53. edit->total_lines + 1,
  54. edit->curs1,
  55. edit->last_byte,
  56. byte_str);
  57. }
  58. /* how to get as much onto the status line as is numerically possible :) */
  59. void edit_status (WEdit * edit)
  60. {
  61. int w, i, t;
  62. char *s;
  63. w = edit->widget.cols - (edit->have_frame * 2);
  64. s = malloc (w + 15);
  65. if (w < 4)
  66. w = 4;
  67. memset (s, ' ', w);
  68. attrset (SELECTED_COLOR);
  69. if (w > 4) {
  70. widget_move (edit, edit->have_frame, edit->have_frame);
  71. i = w > 24 ? 18 : w - 6;
  72. i = i < 13 ? 13 : i;
  73. strcpy (s, (char *) name_trunc (edit->filename ? edit->filename : "", i));
  74. i = strlen (s);
  75. s[i] = ' ';
  76. t = w - 20;
  77. if (t < 0)
  78. t = 0;
  79. status_string (edit, s + 20, t, ' ', 1);
  80. }
  81. s[w] = 0;
  82. printw ("%-*s", w, s);
  83. attrset (NORMAL_COLOR);
  84. free (s);
  85. }
  86. /* result is boolean */
  87. int cursor_in_screen (WEdit * edit, long row)
  88. {
  89. if (row < 0 || row >= edit->num_widget_lines)
  90. return 0;
  91. else
  92. return 1;
  93. }
  94. /* returns rows from the first displayed line to the cursor */
  95. int cursor_from_display_top (WEdit * edit)
  96. {
  97. if (edit->curs1 < edit->start_display)
  98. return -edit_move_forward (edit, edit->curs1, 0, edit->start_display);
  99. else
  100. return edit_move_forward (edit, edit->start_display, 0, edit->curs1);
  101. }
  102. /* returns how far the cursor is out of the screen */
  103. int cursor_out_of_screen (WEdit * edit)
  104. {
  105. int row = cursor_from_display_top (edit);
  106. if (row >= edit->num_widget_lines)
  107. return row - edit->num_widget_lines + 1;
  108. if (row < 0)
  109. return row;
  110. return 0;
  111. }
  112. /* this scrolls the text so that cursor is on the screen */
  113. void edit_scroll_screen_over_cursor (WEdit * edit)
  114. {
  115. int p;
  116. int outby;
  117. int b_extreme, t_extreme, l_extreme, r_extreme;
  118. r_extreme = EDIT_RIGHT_EXTREME;
  119. l_extreme = EDIT_LEFT_EXTREME;
  120. b_extreme = EDIT_BOTTOM_EXTREME;
  121. t_extreme = EDIT_TOP_EXTREME;
  122. if (edit->found_len) {
  123. b_extreme = max (edit->num_widget_lines / 4, b_extreme);
  124. t_extreme = max (edit->num_widget_lines / 4, t_extreme);
  125. }
  126. if (b_extreme + t_extreme + 1 > edit->num_widget_lines) {
  127. int n;
  128. n = b_extreme + t_extreme;
  129. b_extreme = (b_extreme * (edit->num_widget_lines - 1)) / n;
  130. t_extreme = (t_extreme * (edit->num_widget_lines - 1)) / n;
  131. }
  132. if (l_extreme + r_extreme + 1 > edit->num_widget_columns) {
  133. int n;
  134. n = l_extreme + t_extreme;
  135. l_extreme = (l_extreme * (edit->num_widget_columns - 1)) / n;
  136. r_extreme = (r_extreme * (edit->num_widget_columns - 1)) / n;
  137. }
  138. p = edit_get_col (edit);
  139. edit_update_curs_row (edit);
  140. outby = p + edit->start_col - edit->num_widget_columns + 1 + (r_extreme + edit->found_len);
  141. if (outby > 0)
  142. edit_scroll_right (edit, outby);
  143. outby = l_extreme - p - edit->start_col;
  144. if (outby > 0)
  145. edit_scroll_left (edit, outby);
  146. p = edit->curs_row;
  147. outby = p - edit->num_widget_lines + 1 + b_extreme;
  148. if (outby > 0)
  149. edit_scroll_downward (edit, outby);
  150. outby = t_extreme - p;
  151. if (outby > 0)
  152. edit_scroll_upward (edit, outby);
  153. edit_update_curs_row (edit);
  154. }
  155. #define set_color(font) attrset (font)
  156. #define edit_move(x,y) widget_move(edit, y, x);
  157. static void print_to_widget (WEdit * edit, long row, int start_col, float start_col_real, long end_col, unsigned int line[])
  158. {
  159. int x = (float) start_col_real + EDIT_TEXT_HORIZONTAL_OFFSET;
  160. int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET;
  161. int y = row + EDIT_TEXT_VERTICAL_OFFSET;
  162. set_color (EDITOR_NORMAL_COLOR);
  163. edit_move (x1, y);
  164. hline (' ', end_col + 1 - EDIT_TEXT_HORIZONTAL_OFFSET - x1);
  165. edit_move (x + FONT_OFFSET_X, y + FONT_OFFSET_Y);
  166. {
  167. unsigned int *p = line;
  168. int textchar = ' ';
  169. long style;
  170. while (*p) {
  171. style = *p >> 8;
  172. textchar = *p & 0xFF;
  173. #ifdef HAVE_SYNTAXH
  174. if (!(style & (0xFF - MOD_ABNORMAL - MOD_CURSOR)))
  175. SLsmg_set_color ((*p & 0x007F0000) >> 16);
  176. #endif
  177. if (style & MOD_ABNORMAL)
  178. textchar = '.';
  179. if (style & MOD_HIGHLIGHTED) {
  180. set_color (EDITOR_BOLD_COLOR);
  181. } else if (style & MOD_MARKED) {
  182. set_color (EDITOR_MARKED_COLOR);
  183. }
  184. if (style & MOD_UNDERLINED) {
  185. set_color (EDITOR_UNDERLINED_COLOR);
  186. }
  187. if (style & MOD_BOLD) {
  188. set_color (EDITOR_BOLD_COLOR);
  189. }
  190. addch (textchar);
  191. p++;
  192. }
  193. }
  194. }
  195. /* b is a pointer to the beginning of the line */
  196. static void edit_draw_this_line (WEdit * edit, long b, long row, long start_col, long end_col)
  197. {
  198. static unsigned int line[MAX_LINE_LEN];
  199. unsigned int *p = line;
  200. long m1 = 0, m2 = 0, q, c1, c2;
  201. int col, start_col_real;
  202. unsigned int c;
  203. int fg, bg;
  204. int i, book_mark = -1;
  205. #if 0
  206. if (!book_mark_query (edit, edit->start_line + row, &book_mark))
  207. book_mark = -1;
  208. #endif
  209. edit_get_syntax_color (edit, b - 1, &fg, &bg);
  210. q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
  211. start_col_real = (col = (int) edit_move_forward3 (edit, b, 0, q)) + edit->start_col;
  212. c1 = min (edit->column1, edit->column2);
  213. c2 = max (edit->column1, edit->column2);
  214. if (col + 16 > -edit->start_col) {
  215. eval_marks (edit, &m1, &m2);
  216. if (row <= edit->total_lines - edit->start_line) {
  217. while (col <= end_col - edit->start_col) {
  218. *p = 0;
  219. if (q == edit->curs1)
  220. *p |= MOD_CURSOR * 256;
  221. if (q >= m1 && q < m2) {
  222. if (column_highlighting) {
  223. int x;
  224. x = edit_move_forward3 (edit, b, 0, q);
  225. if (x >= c1 && x < c2)
  226. *p |= MOD_MARKED * 256;
  227. } else
  228. *p |= MOD_MARKED * 256;
  229. }
  230. if (q == edit->bracket)
  231. *p |= MOD_BOLD * 256;
  232. if (q >= edit->found_start && q < edit->found_start + edit->found_len)
  233. *p |= MOD_HIGHLIGHTED * 256;
  234. c = edit_get_byte (edit, q);
  235. /* we don't use bg for mc - fg contains both */
  236. if (book_mark == -1) {
  237. edit_get_syntax_color (edit, q, &fg, &bg);
  238. *p |= fg << 16;
  239. } else {
  240. *p |= book_mark << 16;
  241. }
  242. q++;
  243. switch (c) {
  244. case '\n':
  245. col = end_col - edit->start_col + 1; /* quit */
  246. *(p++) |= ' ';
  247. break;
  248. case '\t':
  249. i = TAB_SIZE - ((int) col % TAB_SIZE);
  250. *p |= ' ';
  251. c = *(p++) & (0xFFFFFFFF - MOD_CURSOR * 256);
  252. col += i;
  253. while (--i)
  254. *(p++) = c;
  255. break;
  256. case '\r':
  257. break;
  258. default:
  259. #ifdef HAVE_CHARSET
  260. if (c >= 0 && c <= 255)
  261. c = conv_displ[ c ];
  262. #endif
  263. if (is_printable (c)) {
  264. *(p++) |= c;
  265. } else {
  266. *(p++) = '.';
  267. *p |= (256 * MOD_ABNORMAL);
  268. }
  269. col++;
  270. break;
  271. }
  272. }
  273. }
  274. } else {
  275. start_col_real = start_col = 0;
  276. }
  277. *p = 0;
  278. print_to_widget (edit, row, start_col, start_col_real, end_col, line);
  279. }
  280. #define key_pending(x) (!is_idle())
  281. static void edit_draw_this_char (WEdit * edit, long curs, long row)
  282. {
  283. int b = edit_bol (edit, curs);
  284. edit_draw_this_line (edit, b, row, 0, edit->num_widget_columns - 1);
  285. }
  286. /* cursor must be in screen for other than REDRAW_PAGE passed in force */
  287. void render_edit_text (WEdit * edit, long start_row, long start_column, long end_row,
  288. long end_column)
  289. {
  290. long row = 0, curs_row;
  291. static int prev_curs_row = 0;
  292. static int prev_start_col = 0;
  293. static long prev_curs = 0;
  294. static long prev_start = -1;
  295. int force = edit->force;
  296. long b;
  297. CPushFont ("editor", 0);
  298. /*
  299. * If the position of the page has not moved then we can draw the cursor
  300. * character only. This will prevent line flicker when using arrow keys.
  301. */
  302. if ((!(force & REDRAW_CHAR_ONLY)) || (force & REDRAW_PAGE)) {
  303. if (!(force & REDRAW_IN_BOUNDS)) { /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
  304. start_row = 0;
  305. end_row = edit->num_widget_lines - 1;
  306. start_column = 0;
  307. end_column = edit->num_widget_columns - 1;
  308. }
  309. if (force & REDRAW_PAGE) {
  310. row = start_row;
  311. b = edit_move_forward (edit, edit->start_display, start_row, 0);
  312. while (row <= end_row) {
  313. if (key_pending (edit))
  314. goto exit_render;
  315. edit_draw_this_line (edit, b, row, start_column, end_column);
  316. b = edit_move_forward (edit, b, 1, 0);
  317. row++;
  318. }
  319. } else {
  320. curs_row = edit->curs_row;
  321. if (force & REDRAW_BEFORE_CURSOR) {
  322. if (start_row < curs_row) {
  323. long upto = curs_row - 1 <= end_row ? curs_row - 1 : end_row;
  324. row = start_row;
  325. b = edit->start_display;
  326. while (row <= upto) {
  327. if (key_pending (edit))
  328. goto exit_render;
  329. edit_draw_this_line (edit, b, row, start_column, end_column);
  330. b = edit_move_forward (edit, b, 1, 0);
  331. }
  332. }
  333. }
  334. /* if (force & REDRAW_LINE) ---> default */
  335. b = edit_bol (edit, edit->curs1);
  336. if (curs_row >= start_row && curs_row <= end_row) {
  337. if (key_pending (edit))
  338. goto exit_render;
  339. edit_draw_this_line (edit, b, curs_row, start_column, end_column);
  340. }
  341. if (force & REDRAW_AFTER_CURSOR) {
  342. if (end_row > curs_row) {
  343. row = curs_row + 1 < start_row ? start_row : curs_row + 1;
  344. b = edit_move_forward (edit, b, 1, 0);
  345. while (row <= end_row) {
  346. if (key_pending (edit))
  347. goto exit_render;
  348. edit_draw_this_line (edit, b, row, start_column, end_column);
  349. b = edit_move_forward (edit, b, 1, 0);
  350. row++;
  351. }
  352. }
  353. }
  354. if (force & REDRAW_LINE_ABOVE && curs_row >= 1) {
  355. row = curs_row - 1;
  356. b = edit_move_backward (edit, edit_bol (edit, edit->curs1), 1);
  357. if (row >= start_row && row <= end_row) {
  358. if (key_pending (edit))
  359. goto exit_render;
  360. edit_draw_this_line (edit, b, row, start_column, end_column);
  361. }
  362. }
  363. if (force & REDRAW_LINE_BELOW && row < edit->num_widget_lines - 1) {
  364. row = curs_row + 1;
  365. b = edit_bol (edit, edit->curs1);
  366. b = edit_move_forward (edit, b, 1, 0);
  367. if (row >= start_row && row <= end_row) {
  368. if (key_pending (edit))
  369. goto exit_render;
  370. edit_draw_this_line (edit, b, row, start_column, end_column);
  371. }
  372. }
  373. }
  374. } else {
  375. if (prev_curs_row < edit->curs_row) { /* with the new text highlighting, we must draw from the top down */
  376. edit_draw_this_char (edit, prev_curs, prev_curs_row);
  377. edit_draw_this_char (edit, edit->curs1, edit->curs_row);
  378. } else {
  379. edit_draw_this_char (edit, edit->curs1, edit->curs_row);
  380. edit_draw_this_char (edit, prev_curs, prev_curs_row);
  381. }
  382. }
  383. edit->force = 0;
  384. prev_curs_row = edit->curs_row;
  385. prev_curs = edit->curs1;
  386. prev_start_col = edit->start_col;
  387. exit_render:
  388. edit->screen_modified = 0;
  389. prev_start = edit->start_line;
  390. CPopFont ();
  391. return;
  392. }
  393. void edit_set_space_width (int s);
  394. extern int option_long_whitespace;
  395. void edit_render (WEdit * edit, int page, int row_start, int col_start, int row_end, int col_end)
  396. {
  397. if (page) /* if it was an expose event, 'page' would be set */
  398. edit->force |= REDRAW_PAGE | REDRAW_IN_BOUNDS;
  399. if (edit->force & REDRAW_COMPLETELY)
  400. redraw_labels (edit->widget.parent, (Widget *) edit);
  401. render_edit_text (edit, row_start, col_start, row_end, col_end);
  402. /*
  403. * edit->force != 0 means a key was pending and the redraw
  404. * was halted, so next time we must redraw everything in case stuff
  405. * was left undrawn from a previous key press.
  406. */
  407. if (edit->force)
  408. edit->force |= REDRAW_PAGE;
  409. }
  410. void edit_render_keypress (WEdit * edit)
  411. {
  412. edit_render (edit, 0, 0, 0, 0, 0);
  413. }