editdraw.c 13 KB

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