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