Updated italian translatio
[midnight-commander.git] / edit / editdraw.c
blobd55cee90df05590af50ed747f72cf352b2afa1e5
1 /* editor text drawing.
3 Copyright (C) 1996, 1997 the Free Software Foundation
5 Authors: 1996, 1997 Paul Sheer
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
23 #include <config.h>
24 #include "edit.h"
25 #include "edit-widget.h"
27 #define MAX_LINE_LEN 1024
29 #include "../src/color.h" /* EDITOR_NORMAL_COLOR */
30 #include "../src/tty.h" /* attrset() */
31 #include "../src/widget.h" /* redraw_labels() */
32 #include "../src/key.h" /* is_idle() */
33 #include "../src/charsets.h"
35 /* Text styles */
36 #define MOD_ABNORMAL (1 << 8)
37 #define MOD_BOLD (1 << 9)
38 #define MOD_MARKED (1 << 10)
39 #define MOD_CURSOR (1 << 11)
41 #define FONT_OFFSET_X 0
42 #define FONT_OFFSET_Y 0
43 #define FIXED_FONT 1
44 #define FONT_PIX_PER_LINE 1
45 #define FONT_MEAN_WIDTH 1
48 static void status_string (WEdit * edit, char *s, int w, int fill)
50 char byte_str[16];
53 * If we are at the end of file, print <EOF>,
54 * otherwise print the current character as is (if printable),
55 * as decimal and as hex.
57 if (edit->curs1 < edit->last_byte) {
58 unsigned char cur_byte = edit_get_byte (edit, edit->curs1);
59 g_snprintf (byte_str, sizeof(byte_str), "%c %3d 0x%02X",
60 is_printable(cur_byte) ? cur_byte : '.',
61 cur_byte,
62 cur_byte);
63 } else {
64 strcpy(byte_str, "<EOF>");
67 /* The field lengths just prevent the status line from shortening too much */
68 g_snprintf (s, w,
69 "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb)= %s",
70 edit->mark1 != edit->mark2 ? ( column_highlighting ? 'C' : 'B') : '-',
71 edit->modified ? 'M' : '-',
72 edit->macro_i < 0 ? '-' : 'R',
73 edit->overwrite == 0 ? '-' : 'O',
74 edit->curs_col,
76 edit->start_line + 1,
77 edit->curs_row,
78 edit->curs_line + 1,
79 edit->total_lines + 1,
81 edit->curs1,
82 edit->last_byte,
83 byte_str);
86 /* how to get as much onto the status line as is numerically possible :) */
87 void
88 edit_status (WEdit *edit)
90 int w, i, t;
91 char *s;
92 w = edit->widget.cols;
93 s = g_malloc (w + 16);
94 if (w < 4)
95 w = 4;
96 memset (s, ' ', w);
97 attrset (SELECTED_COLOR);
98 if (w > 4) {
99 widget_move (edit, 0, 0);
100 if (edit->filename) {
101 i = w > 24 ? 18 : w - 6;
102 i = i < 13 ? 13 : i;
103 strcpy (s, name_trunc (edit->filename, i));
104 i = strlen (s);
105 s[i] = ' ';
107 t = w - 20;
108 if (t > 1) /* g_snprintf() must write at least '\000' */
109 status_string (edit, s + 20, t + 1 /* for '\000' */ , ' ');
111 s[w] = 0;
113 printw ("%-*s", w, s);
114 attrset (EDITOR_NORMAL_COLOR);
115 g_free (s);
118 /* this scrolls the text so that cursor is on the screen */
119 void edit_scroll_screen_over_cursor (WEdit * edit)
121 int p;
122 int outby;
123 int b_extreme, t_extreme, l_extreme, r_extreme;
125 if (edit->num_widget_lines <= 0 || edit->num_widget_columns <= 0)
126 return;
128 r_extreme = EDIT_RIGHT_EXTREME;
129 l_extreme = EDIT_LEFT_EXTREME;
130 b_extreme = EDIT_BOTTOM_EXTREME;
131 t_extreme = EDIT_TOP_EXTREME;
132 if (edit->found_len) {
133 b_extreme = max (edit->num_widget_lines / 4, b_extreme);
134 t_extreme = max (edit->num_widget_lines / 4, t_extreme);
136 if (b_extreme + t_extreme + 1 > edit->num_widget_lines) {
137 int n;
138 n = b_extreme + t_extreme;
139 b_extreme = (b_extreme * (edit->num_widget_lines - 1)) / n;
140 t_extreme = (t_extreme * (edit->num_widget_lines - 1)) / n;
142 if (l_extreme + r_extreme + 1 > edit->num_widget_columns) {
143 int n;
144 n = l_extreme + t_extreme;
145 l_extreme = (l_extreme * (edit->num_widget_columns - 1)) / n;
146 r_extreme = (r_extreme * (edit->num_widget_columns - 1)) / n;
148 p = edit_get_col (edit);
149 edit_update_curs_row (edit);
150 outby = p + edit->start_col - edit->num_widget_columns + 1 + (r_extreme + edit->found_len);
151 if (outby > 0)
152 edit_scroll_right (edit, outby);
153 outby = l_extreme - p - edit->start_col;
154 if (outby > 0)
155 edit_scroll_left (edit, outby);
156 p = edit->curs_row;
157 outby = p - edit->num_widget_lines + 1 + b_extreme;
158 if (outby > 0)
159 edit_scroll_downward (edit, outby);
160 outby = t_extreme - p;
161 if (outby > 0)
162 edit_scroll_upward (edit, outby);
163 edit_update_curs_row (edit);
166 #define set_color(font) attrset (font)
168 #define edit_move(x,y) widget_move(edit, y, x);
170 /* Set colorpair by index, don't interpret S-Lang "emulated attributes" */
171 #ifdef HAVE_SLANG
172 #define lowlevel_set_color(x) SLsmg_set_color(x & 0x7F)
173 #else
174 #define lowlevel_set_color(x) attrset(MY_COLOR_PAIR(color))
175 #endif
177 static void
178 print_to_widget (WEdit *edit, long row, int start_col, int start_col_real,
179 long end_col, unsigned int line[])
181 unsigned int *p;
183 int x = start_col_real + EDIT_TEXT_HORIZONTAL_OFFSET;
184 int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET;
185 int y = row + EDIT_TEXT_VERTICAL_OFFSET;
187 set_color (EDITOR_NORMAL_COLOR);
188 edit_move (x1, y);
189 hline (' ', end_col + 1 - EDIT_TEXT_HORIZONTAL_OFFSET - x1);
191 edit_move (x + FONT_OFFSET_X, y + FONT_OFFSET_Y);
192 p = line;
194 while (*p) {
195 int style = *p & 0xFF00;
196 int textchar = *p & 0xFF;
197 int color = *p >> 16;
199 if (style & MOD_ABNORMAL) {
200 /* Non-printable - use black background */
201 color = 0;
204 if (style & MOD_BOLD) {
205 set_color (EDITOR_BOLD_COLOR);
206 } else if (style & MOD_MARKED) {
207 set_color (EDITOR_MARKED_COLOR);
208 } else {
209 lowlevel_set_color (color);
212 addch (textchar);
213 p++;
217 /* b is a pointer to the beginning of the line */
218 static void
219 edit_draw_this_line (WEdit *edit, long b, long row, long start_col,
220 long end_col)
222 static unsigned int line[MAX_LINE_LEN];
223 unsigned int *p = line;
224 long m1 = 0, m2 = 0, q, c1, c2;
225 int col, start_col_real;
226 unsigned int c;
227 int color;
228 int i, book_mark = -1;
230 #if 0
231 if (!book_mark_query (edit, edit->start_line + row, &book_mark))
232 book_mark = -1;
233 #endif
235 edit_get_syntax_color (edit, b - 1, &color);
236 q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
237 start_col_real = (col =
238 (int) edit_move_forward3 (edit, b, 0,
239 q)) + edit->start_col;
240 c1 = min (edit->column1, edit->column2);
241 c2 = max (edit->column1, edit->column2);
243 if (col + 16 > -edit->start_col) {
244 eval_marks (edit, &m1, &m2);
246 if (row <= edit->total_lines - edit->start_line) {
247 while (col <= end_col - edit->start_col) {
248 *p = 0;
249 if (q == edit->curs1)
250 *p |= MOD_CURSOR;
251 if (q >= m1 && q < m2) {
252 if (column_highlighting) {
253 int x;
254 x = edit_move_forward3 (edit, b, 0, q);
255 if (x >= c1 && x < c2)
256 *p |= MOD_MARKED;
257 } else
258 *p |= MOD_MARKED;
260 if (q == edit->bracket)
261 *p |= MOD_BOLD;
262 if (q >= edit->found_start
263 && q < edit->found_start + edit->found_len)
264 *p |= MOD_BOLD;
265 c = edit_get_byte (edit, q);
266 /* we don't use bg for mc - fg contains both */
267 if (book_mark == -1) {
268 edit_get_syntax_color (edit, q, &color);
269 *p |= color << 16;
270 } else {
271 *p |= book_mark << 16;
273 q++;
274 switch (c) {
275 case '\n':
276 col = end_col - edit->start_col + 1; /* quit */
277 *(p++) |= ' ';
278 break;
279 case '\t':
280 i = TAB_SIZE - ((int) col % TAB_SIZE);
281 *p |= ' ';
282 c = *(p++) & ~MOD_CURSOR;
283 col += i;
284 while (--i)
285 *(p++) = c;
286 break;
287 default:
288 c = convert_to_display_c (c);
290 /* Caret notation for control characters */
291 if (c < 32) {
292 *(p++) = '^' | MOD_ABNORMAL;
293 *(p++) = (c + 0x40) | MOD_ABNORMAL;
294 col += 2;
295 break;
297 if (c == 127) {
298 *(p++) = '^' | MOD_ABNORMAL;
299 *(p++) = '?' | MOD_ABNORMAL;
300 col += 2;
301 break;
304 if (is_printable (c)) {
305 *(p++) |= c;
306 } else {
307 *(p++) = '.' | MOD_ABNORMAL;
309 col++;
310 break;
314 } else {
315 start_col_real = start_col = 0;
317 *p = 0;
319 print_to_widget (edit, row, start_col, start_col_real, end_col, line);
322 #define key_pending(x) (!is_idle())
324 static void edit_draw_this_char (WEdit * edit, long curs, long row)
326 int b = edit_bol (edit, curs);
327 edit_draw_this_line (edit, b, row, 0, edit->num_widget_columns - 1);
330 /* cursor must be in screen for other than REDRAW_PAGE passed in force */
331 static void
332 render_edit_text (WEdit * edit, long start_row, long start_column, long end_row,
333 long end_column)
335 long row = 0, curs_row;
336 static int prev_curs_row = 0;
337 static int prev_start_col = 0;
338 static long prev_curs = 0;
339 static long prev_start = -1;
341 int force = edit->force;
342 long b;
345 * If the position of the page has not moved then we can draw the cursor
346 * character only. This will prevent line flicker when using arrow keys.
348 if ((!(force & REDRAW_CHAR_ONLY)) || (force & REDRAW_PAGE)) {
349 if (!(force & REDRAW_IN_BOUNDS)) { /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
350 start_row = 0;
351 end_row = edit->num_widget_lines - 1;
352 start_column = 0;
353 end_column = edit->num_widget_columns - 1;
355 if (force & REDRAW_PAGE) {
356 row = start_row;
357 b = edit_move_forward (edit, edit->start_display, start_row, 0);
358 while (row <= end_row) {
359 if (key_pending (edit))
360 goto exit_render;
361 edit_draw_this_line (edit, b, row, start_column, end_column);
362 b = edit_move_forward (edit, b, 1, 0);
363 row++;
365 } else {
366 curs_row = edit->curs_row;
368 if (force & REDRAW_BEFORE_CURSOR) {
369 if (start_row < curs_row) {
370 long upto = curs_row - 1 <= end_row ? curs_row - 1 : end_row;
371 row = start_row;
372 b = edit->start_display;
373 while (row <= upto) {
374 if (key_pending (edit))
375 goto exit_render;
376 edit_draw_this_line (edit, b, row, start_column, end_column);
377 b = edit_move_forward (edit, b, 1, 0);
381 /* if (force & REDRAW_LINE) ---> default */
382 b = edit_bol (edit, edit->curs1);
383 if (curs_row >= start_row && curs_row <= end_row) {
384 if (key_pending (edit))
385 goto exit_render;
386 edit_draw_this_line (edit, b, curs_row, start_column, end_column);
388 if (force & REDRAW_AFTER_CURSOR) {
389 if (end_row > curs_row) {
390 row = curs_row + 1 < start_row ? start_row : curs_row + 1;
391 b = edit_move_forward (edit, b, 1, 0);
392 while (row <= end_row) {
393 if (key_pending (edit))
394 goto exit_render;
395 edit_draw_this_line (edit, b, row, start_column, end_column);
396 b = edit_move_forward (edit, b, 1, 0);
397 row++;
401 if (force & REDRAW_LINE_ABOVE && curs_row >= 1) {
402 row = curs_row - 1;
403 b = edit_move_backward (edit, edit_bol (edit, edit->curs1), 1);
404 if (row >= start_row && row <= end_row) {
405 if (key_pending (edit))
406 goto exit_render;
407 edit_draw_this_line (edit, b, row, start_column, end_column);
410 if (force & REDRAW_LINE_BELOW && row < edit->num_widget_lines - 1) {
411 row = curs_row + 1;
412 b = edit_bol (edit, edit->curs1);
413 b = edit_move_forward (edit, b, 1, 0);
414 if (row >= start_row && row <= end_row) {
415 if (key_pending (edit))
416 goto exit_render;
417 edit_draw_this_line (edit, b, row, start_column, end_column);
421 } else {
422 if (prev_curs_row < edit->curs_row) { /* with the new text highlighting, we must draw from the top down */
423 edit_draw_this_char (edit, prev_curs, prev_curs_row);
424 edit_draw_this_char (edit, edit->curs1, edit->curs_row);
425 } else {
426 edit_draw_this_char (edit, edit->curs1, edit->curs_row);
427 edit_draw_this_char (edit, prev_curs, prev_curs_row);
431 edit->force = 0;
433 prev_curs_row = edit->curs_row;
434 prev_curs = edit->curs1;
435 prev_start_col = edit->start_col;
436 exit_render:
437 edit->screen_modified = 0;
438 prev_start = edit->start_line;
439 return;
442 static void
443 edit_render (WEdit * edit, int page, int row_start, int col_start, int row_end, int col_end)
445 if (page) /* if it was an expose event, 'page' would be set */
446 edit->force |= REDRAW_PAGE | REDRAW_IN_BOUNDS;
448 if (edit->force & REDRAW_COMPLETELY)
449 redraw_labels (edit->widget.parent);
450 render_edit_text (edit, row_start, col_start, row_end, col_end);
452 * edit->force != 0 means a key was pending and the redraw
453 * was halted, so next time we must redraw everything in case stuff
454 * was left undrawn from a previous key press.
456 if (edit->force)
457 edit->force |= REDRAW_PAGE;
460 void edit_render_keypress (WEdit * edit)
462 edit_render (edit, 0, 0, 0, 0, 0);