Ticket #1414: small refactoring before major modifications.
[midnight-commander.git] / edit / editdraw.c
blobf218f7d12358f4f10d4f5f2008e6b6954697e608
1 /* editor text drawing.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
24 /** \file
25 * \brief Source: editor text drawing
26 * \author Paul Sheer
27 * \date 1996, 1997
30 #include <config.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <sys/stat.h>
41 #include "../src/global.h"
43 #include "edit-impl.h"
44 #include "edit-widget.h"
46 #define MAX_LINE_LEN 1024
48 #include "../src/tty/tty.h" /* tty_printf() */
49 #include "../src/tty/color.h"
50 #include "../src/tty/key.h" /* is_idle() */
52 #include "../src/widget.h" /* buttonbar_redraw() */
53 #include "../src/charsets.h"
54 #include "../src/strutil.h" /* utf string functions */
55 #include "../src/main.h" /* source_codepage */
57 /* Text styles */
58 #define MOD_ABNORMAL (1 << 8)
59 #define MOD_BOLD (1 << 9)
60 #define MOD_MARKED (1 << 10)
61 #define MOD_CURSOR (1 << 11)
62 #define MOD_WHITESPACE (1 << 12)
64 #define FONT_OFFSET_X 0
65 #define FONT_OFFSET_Y 0
66 #define FIXED_FONT 1
67 #define FONT_PIX_PER_LINE 1
68 #define FONT_MEAN_WIDTH 1
70 /* Toggles statusbar draw style */
71 int simple_statusbar = 0;
73 static void status_string (WEdit * edit, char *s, int w)
75 char byte_str[16];
76 unsigned char cur_byte = 0;
77 unsigned int cur_utf = 0;
78 int cw = 1;
81 * If we are at the end of file, print <EOF>,
82 * otherwise print the current character as is (if printable),
83 * as decimal and as hex.
85 if (edit->curs1 < edit->last_byte) {
86 if ( !edit->utf8 ) {
87 cur_byte = edit_get_byte (edit, edit->curs1);
89 g_snprintf (byte_str, sizeof (byte_str), "%4d 0x%03X",
90 (int) cur_byte,
91 (unsigned) cur_byte);
92 } else {
93 cur_utf = edit_get_utf (edit, edit->curs1, &cw);
94 if ( cw > 0 ) {
95 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
96 (unsigned) cur_utf,
97 (unsigned) cur_utf);
98 } else {
99 cur_utf = edit_get_byte (edit, edit->curs1);
100 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
101 (int) cur_utf,
102 (unsigned) cur_utf);
106 } else {
107 strcpy (byte_str, "<EOF> ");
110 /* The field lengths just prevent the status line from shortening too much */
111 if (simple_statusbar)
112 g_snprintf (s, w,
113 "[%c%c%c%c] %2ld %3ld/%3ld (%-4ld/%4ldb)= %s cp:%s",
114 edit->mark1 != edit->mark2 ? ( column_highlighting ? 'C' : 'B') : '-',
115 edit->modified ? 'M' : '-',
116 edit->macro_i < 0 ? '-' : 'R',
117 edit->overwrite == 0 ? '-' : 'O',
118 edit->curs_col,
120 edit->curs_line + 1,
121 edit->total_lines + 1,
123 edit->curs1,
124 edit->last_byte,
125 byte_str,
127 #ifdef HAVE_CHARSET
128 source_codepage >= 0 ? get_codepage_id (source_codepage) : ""
129 #else
131 #endif
133 else
134 g_snprintf (s, w,
135 "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb)= %s %s",
136 edit->mark1 != edit->mark2 ? ( column_highlighting ? 'C' : 'B') : '-',
137 edit->modified ? 'M' : '-',
138 edit->macro_i < 0 ? '-' : 'R',
139 edit->overwrite == 0 ? '-' : 'O',
140 edit->curs_col,
142 edit->start_line + 1,
143 edit->curs_row,
144 edit->curs_line + 1,
145 edit->total_lines + 1,
147 edit->curs1,
148 edit->last_byte,
149 byte_str,
151 #ifdef HAVE_CHARSET
152 source_codepage >= 0 ? get_codepage_id (source_codepage) : ""
153 #else
155 #endif
159 static inline void
160 printwstr (const char *s, int len)
162 if (len > 0)
163 tty_printf ("%-*.*s", len, len, s);
166 /* Draw the status line at the top of the widget. The size of the filename
167 * field varies depending on the width of the screen and the length of
168 * the filename. */
169 void
170 edit_status (WEdit *edit)
172 const int w = edit->widget.cols;
173 const size_t status_size = w + 1;
174 char * const status = g_malloc (status_size);
175 int status_len;
176 const char *fname = "";
177 int fname_len;
178 const int gap = 3; /* between the filename and the status */
179 const int right_gap = 2; /* at the right end of the screen */
180 const int preferred_fname_len = 16;
182 status_string (edit, status, status_size);
183 status_len = (int) str_term_width1 (status);
185 if (edit->filename)
186 fname = edit->filename;
187 fname_len = str_term_width1 (fname);
188 if (fname_len < preferred_fname_len)
189 fname_len = preferred_fname_len;
191 if (fname_len + gap + status_len + right_gap >= w) {
192 if (preferred_fname_len + gap + status_len + right_gap >= w)
193 fname_len = preferred_fname_len;
194 else
195 fname_len = w - (gap + status_len + right_gap);
196 fname = str_trunc (fname, fname_len);
199 widget_move (edit, 0, 0);
200 tty_setcolor (SELECTED_COLOR);
201 printwstr (fname, fname_len + gap);
202 printwstr (status, w - (fname_len + gap));
203 tty_setcolor (EDITOR_NORMAL_COLOR);
205 g_free (status);
208 /* this scrolls the text so that cursor is on the screen */
209 void edit_scroll_screen_over_cursor (WEdit * edit)
211 int p;
212 int outby;
213 int b_extreme, t_extreme, l_extreme, r_extreme;
215 if (edit->num_widget_lines <= 0 || edit->num_widget_columns <= 0)
216 return;
218 r_extreme = EDIT_RIGHT_EXTREME;
219 l_extreme = EDIT_LEFT_EXTREME;
220 b_extreme = EDIT_BOTTOM_EXTREME;
221 t_extreme = EDIT_TOP_EXTREME;
222 if (edit->found_len) {
223 b_extreme = max (edit->num_widget_lines / 4, b_extreme);
224 t_extreme = max (edit->num_widget_lines / 4, t_extreme);
226 if (b_extreme + t_extreme + 1 > edit->num_widget_lines) {
227 int n;
228 n = b_extreme + t_extreme;
229 b_extreme = (b_extreme * (edit->num_widget_lines - 1)) / n;
230 t_extreme = (t_extreme * (edit->num_widget_lines - 1)) / n;
232 if (l_extreme + r_extreme + 1 > edit->num_widget_columns) {
233 int n;
234 n = l_extreme + t_extreme;
235 l_extreme = (l_extreme * (edit->num_widget_columns - 1)) / n;
236 r_extreme = (r_extreme * (edit->num_widget_columns - 1)) / n;
238 p = edit_get_col (edit);
239 edit_update_curs_row (edit);
240 outby = p + edit->start_col - edit->num_widget_columns + 1 + (r_extreme + edit->found_len);
241 if (outby > 0)
242 edit_scroll_right (edit, outby);
243 outby = l_extreme - p - edit->start_col;
244 if (outby > 0)
245 edit_scroll_left (edit, outby);
246 p = edit->curs_row;
247 outby = p - edit->num_widget_lines + 1 + b_extreme;
248 if (outby > 0)
249 edit_scroll_downward (edit, outby);
250 outby = t_extreme - p;
251 if (outby > 0)
252 edit_scroll_upward (edit, outby);
253 edit_update_curs_row (edit);
256 #define edit_move(x,y) widget_move(edit, y, x);
258 struct line_s {
259 unsigned int ch;
260 unsigned int style;
263 static void
264 print_to_widget (WEdit *edit, long row, int start_col, int start_col_real,
265 long end_col, struct line_s line[], char *status)
267 struct line_s *p;
269 int x = start_col_real + EDIT_TEXT_HORIZONTAL_OFFSET;
270 int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
271 int y = row + EDIT_TEXT_VERTICAL_OFFSET;
272 int cols_to_skip = abs (x);
273 unsigned char str[6 + 1];
275 tty_setcolor (EDITOR_NORMAL_COLOR);
276 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1,
277 ' ', end_col + 1 - EDIT_TEXT_HORIZONTAL_OFFSET - x1);
279 if (option_line_state) {
280 int i;
281 for (i = 0; i < LINE_STATE_WIDTH; i++)
282 if (status[i] == '\0')
283 status[i] = ' ';
285 tty_setcolor (LINE_STATE_COLOR);
286 edit_move (x1 + FONT_OFFSET_X - option_line_state_width, y + FONT_OFFSET_Y);
287 tty_print_string (status);
290 edit_move (x1 + FONT_OFFSET_X, y + FONT_OFFSET_Y);
291 p = line;
293 while (p->ch) {
294 int style;
295 unsigned int textchar;
296 int color;
298 if (cols_to_skip) {
299 p++;
300 cols_to_skip--;
301 continue;
304 style = p->style & 0xFF00;
305 textchar = p->ch;
306 color = p->style >> 16;
308 if (style & MOD_ABNORMAL) {
309 /* Non-printable - use black background */
310 color = 0;
313 if (style & MOD_WHITESPACE) {
314 if (style & MOD_MARKED) {
315 textchar = ' ';
316 tty_setcolor (EDITOR_MARKED_COLOR);
317 } else {
318 #if 0
319 if (color != EDITOR_NORMAL_COLOR) {
320 textchar = ' ';
321 tty_lowlevel_setcolor (color);
322 } else
323 #endif
324 tty_setcolor (EDITOR_WHITESPACE_COLOR);
326 } else {
327 if (style & MOD_BOLD) {
328 tty_setcolor (EDITOR_BOLD_COLOR);
329 } else if (style & MOD_MARKED) {
330 tty_setcolor (EDITOR_MARKED_COLOR);
331 } else {
332 tty_lowlevel_setcolor (color);
335 if ( textchar > 255 ) {
336 int res = g_unichar_to_utf8 (textchar, (char *)str);
337 if ( res == 0 ) {
338 str[0] = '.';
339 str[1] = '\0';
340 } else {
341 str[res] = '\0';
343 tty_print_string ((char *) str);
344 } else {
345 tty_print_char (textchar);
347 p++;
351 int visible_tabs = 1, visible_tws = 1;
353 /* b is a pointer to the beginning of the line */
354 static void
355 edit_draw_this_line (WEdit *edit, long b, long row, long start_col,
356 long end_col)
358 struct line_s line[MAX_LINE_LEN];
359 struct line_s *p = line;
361 long m1 = 0, m2 = 0, q, c1, c2;
362 int col, start_col_real;
363 int cw;
364 unsigned int c;
365 int color;
366 int i;
367 int utf8lag = 0;
368 unsigned int cur_line = 0;
369 char line_stat[LINE_STATE_WIDTH + 1];
371 edit_get_syntax_color (edit, b - 1, &color);
372 q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
373 start_col_real = (col =
374 (int) edit_move_forward3 (edit, b, 0,
375 q)) + edit->start_col;
376 c1 = min (edit->column1, edit->column2);
377 c2 = max (edit->column1, edit->column2);
379 if ( option_line_state ) {
380 cur_line = edit->start_line + row;
381 if ( cur_line <= edit->total_lines ) {
382 g_snprintf (line_stat, LINE_STATE_WIDTH + 1, "%7i ", cur_line + 1);
383 } else {
384 memset(line_stat, ' ', LINE_STATE_WIDTH);
385 line_stat[LINE_STATE_WIDTH] = '\0';
389 if (col + 16 > -edit->start_col) {
390 eval_marks (edit, &m1, &m2);
392 if (row <= edit->total_lines - edit->start_line) {
393 long tws = 0;
394 if (tty_use_colors () && visible_tws) {
395 tws = edit_eol (edit, b);
396 while (tws > b && ((c = edit_get_byte (edit, tws - 1)) == ' '
397 || c == '\t'))
398 tws--;
401 while (col <= end_col - edit->start_col) {
402 p->ch = 0;
403 p->style = 0;
404 if (q == edit->curs1)
405 p->style |= MOD_CURSOR;
406 if (q >= m1 && q < m2) {
407 if (column_highlighting) {
408 int x;
409 x = edit_move_forward3 (edit, b, 0, q);
410 if (x >= c1 && x < c2)
411 p->style |= MOD_MARKED;
412 } else
413 p->style |= MOD_MARKED;
415 if (q == edit->bracket)
416 p->style |= MOD_BOLD;
417 if (q >= edit->found_start
418 && q < edit->found_start + edit->found_len)
419 p->style |= MOD_BOLD;
420 cw = 1;
421 if ( !edit->utf8 ) {
422 c = edit_get_byte (edit, q);
423 } else {
424 c = edit_get_utf (edit, q, &cw);
426 /* we don't use bg for mc - fg contains both */
427 edit_get_syntax_color (edit, q, &color);
428 p->style |= color << 16;
429 switch (c) {
430 case '\n':
431 col = (end_col + utf8lag) - edit->start_col + 1; /* quit */
432 p->ch = ' ';
433 p++;
434 break;
435 case '\t':
436 i = TAB_SIZE - ((int) col % TAB_SIZE);
437 col += i;
438 if (tty_use_colors() &&
439 ((visible_tabs || (visible_tws && q >= tws)) && enable_show_tabs_tws)) {
440 if (p->style & MOD_MARKED)
441 c = p->style;
442 else
443 c = p->style | MOD_WHITESPACE;
444 if (i > 2) {
445 p->ch = '<';
446 p->style = c;
447 p++;
448 while (--i > 1) {
449 p->ch = '-';
450 p->style = c;
451 p++;
453 p->ch = '>';
454 p->style = c;
455 p++;
456 } else if (i > 1) {
457 p->ch = '<';
458 p->style = c;
459 p++;
460 p->ch = '>';
461 p->style = c;
462 p++;
463 } else {
464 p->ch = '>';
465 p->style = c;
466 p++;
468 } else if (tty_use_colors() && visible_tws && q >= tws && enable_show_tabs_tws) {
469 p->ch = '.';
470 p->style |= MOD_WHITESPACE;
471 c = p->style & ~MOD_CURSOR;
472 p++;
473 while (--i) {
474 p->ch = ' ';
475 p->style = c;
476 p++;
478 } else {
479 p->ch |= ' ';
480 c = p->style & ~MOD_CURSOR;
481 p++;
482 while (--i) {
483 p->ch = ' ';
484 p->style = c;
485 p++;
488 break;
489 case ' ':
490 if (tty_use_colors() && visible_tws && q >= tws && enable_show_tabs_tws) {
491 p->ch = '.';
492 p->style |= MOD_WHITESPACE;
493 p++;
494 col++;
495 break;
497 /* fallthrough */
498 default:
499 #ifdef HAVE_CHARSET
500 if ( utf8_display ) {
501 if ( !edit->utf8 ) {
502 c = convert_from_8bit_to_utf_c ((unsigned char) c);
504 } else {
505 if ( edit->utf8 ) {
506 c = convert_from_utf_to_current_c (c);
507 } else {
508 #endif
509 c = convert_to_display_c (c);
510 #ifdef HAVE_CHARSET
513 #endif
514 /* Caret notation for control characters */
515 if (c < 32) {
516 p->ch = '^';
517 p->style = MOD_ABNORMAL;
518 p++;
519 p->ch = c + 0x40;
520 p->style = MOD_ABNORMAL;
521 p++;
522 col += 2;
523 break;
525 if (c == 127) {
526 p->ch = '^';
527 p->style = MOD_ABNORMAL;
528 p++;
529 p->ch = '?';
530 p->style = MOD_ABNORMAL;
531 p++;
532 col += 2;
533 break;
535 #ifndef HAVE_CHARSET
536 int utf8_display = 0;
537 #endif
538 if (!edit->utf8) {
539 if ( ( utf8_display && g_unichar_isprint (c) ) ||
540 ( utf8_display == 0 && is_printable (c) ) ) {
541 p->ch = c;
542 p++;
543 } else {
544 p->ch = '.';
545 p->style = MOD_ABNORMAL;
546 p++;
548 } else {
549 if ( g_unichar_isprint (c) ) {
550 p->ch = c;
551 p++;
552 } else {
553 p->ch = '.';
554 p->style = MOD_ABNORMAL;
555 p++;
558 col++;
559 break;
561 q++;
562 if ( cw > 1) {
563 q += cw - 1;
567 } else {
568 start_col_real = start_col = 0;
570 p->ch = 0;
572 print_to_widget (edit, row, start_col, start_col_real, end_col, line, line_stat);
575 #define key_pending(x) (!is_idle())
577 static void edit_draw_this_char (WEdit * edit, long curs, long row)
579 int b = edit_bol (edit, curs);
580 edit_draw_this_line (edit, b, row, 0, edit->num_widget_columns - 1);
583 /* cursor must be in screen for other than REDRAW_PAGE passed in force */
584 static void
585 render_edit_text (WEdit * edit, long start_row, long start_column, long end_row,
586 long end_column)
588 long row = 0, curs_row;
589 static int prev_curs_row = 0;
590 static long prev_curs = 0;
592 int force = edit->force;
593 long b;
596 * If the position of the page has not moved then we can draw the cursor
597 * character only. This will prevent line flicker when using arrow keys.
599 if ((!(force & REDRAW_CHAR_ONLY)) || (force & REDRAW_PAGE)) {
600 if (!(force & REDRAW_IN_BOUNDS)) { /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
601 start_row = 0;
602 end_row = edit->num_widget_lines - 1;
603 start_column = 0;
604 end_column = edit->num_widget_columns - 1;
606 if (force & REDRAW_PAGE) {
607 row = start_row;
608 b = edit_move_forward (edit, edit->start_display, start_row, 0);
609 while (row <= end_row) {
610 if (key_pending (edit))
611 goto exit_render;
612 edit_draw_this_line (edit, b, row, start_column, end_column);
613 b = edit_move_forward (edit, b, 1, 0);
614 row++;
616 } else {
617 curs_row = edit->curs_row;
619 if (force & REDRAW_BEFORE_CURSOR) {
620 if (start_row < curs_row) {
621 long upto = curs_row - 1 <= end_row ? curs_row - 1 : end_row;
622 row = start_row;
623 b = edit->start_display;
624 while (row <= upto) {
625 if (key_pending (edit))
626 goto exit_render;
627 edit_draw_this_line (edit, b, row, start_column, end_column);
628 b = edit_move_forward (edit, b, 1, 0);
632 /* if (force & REDRAW_LINE) ---> default */
633 b = edit_bol (edit, edit->curs1);
634 if (curs_row >= start_row && curs_row <= end_row) {
635 if (key_pending (edit))
636 goto exit_render;
637 edit_draw_this_line (edit, b, curs_row, start_column, end_column);
639 if (force & REDRAW_AFTER_CURSOR) {
640 if (end_row > curs_row) {
641 row = curs_row + 1 < start_row ? start_row : curs_row + 1;
642 b = edit_move_forward (edit, b, 1, 0);
643 while (row <= end_row) {
644 if (key_pending (edit))
645 goto exit_render;
646 edit_draw_this_line (edit, b, row, start_column, end_column);
647 b = edit_move_forward (edit, b, 1, 0);
648 row++;
652 if (force & REDRAW_LINE_ABOVE && curs_row >= 1) {
653 row = curs_row - 1;
654 b = edit_move_backward (edit, edit_bol (edit, edit->curs1), 1);
655 if (row >= start_row && row <= end_row) {
656 if (key_pending (edit))
657 goto exit_render;
658 edit_draw_this_line (edit, b, row, start_column, end_column);
661 if (force & REDRAW_LINE_BELOW && row < edit->num_widget_lines - 1) {
662 row = curs_row + 1;
663 b = edit_bol (edit, edit->curs1);
664 b = edit_move_forward (edit, b, 1, 0);
665 if (row >= start_row && row <= end_row) {
666 if (key_pending (edit))
667 goto exit_render;
668 edit_draw_this_line (edit, b, row, start_column, end_column);
672 } else {
673 if (prev_curs_row < edit->curs_row) { /* with the new text highlighting, we must draw from the top down */
674 edit_draw_this_char (edit, prev_curs, prev_curs_row);
675 edit_draw_this_char (edit, edit->curs1, edit->curs_row);
676 } else {
677 edit_draw_this_char (edit, edit->curs1, edit->curs_row);
678 edit_draw_this_char (edit, prev_curs, prev_curs_row);
682 edit->force = 0;
684 prev_curs_row = edit->curs_row;
685 prev_curs = edit->curs1;
687 exit_render:
688 edit->screen_modified = 0;
689 return;
692 static void
693 edit_render (WEdit * edit, int page, int row_start, int col_start, int row_end, int col_end)
695 if (page) /* if it was an expose event, 'page' would be set */
696 edit->force |= REDRAW_PAGE | REDRAW_IN_BOUNDS;
698 if (edit->force & REDRAW_COMPLETELY)
699 buttonbar_redraw (edit->widget.parent);
700 render_edit_text (edit, row_start, col_start, row_end, col_end);
702 * edit->force != 0 means a key was pending and the redraw
703 * was halted, so next time we must redraw everything in case stuff
704 * was left undrawn from a previous key press.
706 if (edit->force)
707 edit->force |= REDRAW_PAGE;
710 void edit_render_keypress (WEdit * edit)
712 edit_render (edit, 0, 0, 0, 0, 0);