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
25 * \brief Source: editor text drawing
34 #include <sys/types.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 */
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
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
)
76 unsigned char cur_byte
= 0;
77 unsigned int cur_utf
= 0;
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
) {
87 cur_byte
= edit_get_byte (edit
, edit
->curs1
);
89 g_snprintf (byte_str
, sizeof (byte_str
), "%4d 0x%03X",
93 cur_utf
= edit_get_utf (edit
, edit
->curs1
, &cw
);
95 g_snprintf (byte_str
, sizeof (byte_str
), "%04d 0x%03X",
99 cur_utf
= edit_get_byte (edit
, edit
->curs1
);
100 g_snprintf (byte_str
, sizeof (byte_str
), "%04d 0x%03X",
107 strcpy (byte_str
, "<EOF> ");
110 /* The field lengths just prevent the status line from shortening too much */
111 if (simple_statusbar
)
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',
121 edit
->total_lines
+ 1,
128 source_codepage
>= 0 ? get_codepage_id (source_codepage
) : ""
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',
142 edit
->start_line
+ 1,
145 edit
->total_lines
+ 1,
152 source_codepage
>= 0 ? get_codepage_id (source_codepage
) : ""
160 printwstr (const char *s
, int len
)
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
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
);
176 const char *fname
= "";
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
);
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
;
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
);
208 /* this scrolls the text so that cursor is on the screen */
209 void edit_scroll_screen_over_cursor (WEdit
* edit
)
213 int b_extreme
, t_extreme
, l_extreme
, r_extreme
;
215 if (edit
->num_widget_lines
<= 0 || edit
->num_widget_columns
<= 0)
218 edit
->num_widget_columns
-= EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
219 edit
->num_widget_lines
-= EDIT_TEXT_VERTICAL_OFFSET
- 1;
221 r_extreme
= EDIT_RIGHT_EXTREME
;
222 l_extreme
= EDIT_LEFT_EXTREME
;
223 b_extreme
= EDIT_BOTTOM_EXTREME
;
224 t_extreme
= EDIT_TOP_EXTREME
;
225 if (edit
->found_len
) {
226 b_extreme
= max (edit
->num_widget_lines
/ 4, b_extreme
);
227 t_extreme
= max (edit
->num_widget_lines
/ 4, t_extreme
);
229 if (b_extreme
+ t_extreme
+ 1 > edit
->num_widget_lines
) {
231 n
= b_extreme
+ t_extreme
;
232 b_extreme
= (b_extreme
* (edit
->num_widget_lines
- 1)) / n
;
233 t_extreme
= (t_extreme
* (edit
->num_widget_lines
- 1)) / n
;
235 if (l_extreme
+ r_extreme
+ 1 > edit
->num_widget_columns
) {
237 n
= l_extreme
+ t_extreme
;
238 l_extreme
= (l_extreme
* (edit
->num_widget_columns
- 1)) / n
;
239 r_extreme
= (r_extreme
* (edit
->num_widget_columns
- 1)) / n
;
241 p
= edit_get_col (edit
) + edit
->over_col
;
242 edit_update_curs_row (edit
);
243 outby
= p
+ edit
->start_col
- edit
->num_widget_columns
+ 1 + (r_extreme
+ edit
->found_len
);
245 edit_scroll_right (edit
, outby
);
246 outby
= l_extreme
- p
- edit
->start_col
;
248 edit_scroll_left (edit
, outby
);
250 outby
= p
- edit
->num_widget_lines
+ 1 + b_extreme
;
252 edit_scroll_downward (edit
, outby
);
253 outby
= t_extreme
- p
;
255 edit_scroll_upward (edit
, outby
);
256 edit_update_curs_row (edit
);
258 edit
->num_widget_lines
+= EDIT_TEXT_VERTICAL_OFFSET
- 1;
259 edit
->num_widget_columns
+= EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
262 #define edit_move(x,y) widget_move(edit, y, x);
270 print_to_widget (WEdit
*edit
, long row
, int start_col
, int start_col_real
,
271 long end_col
, struct line_s line
[], char *status
)
275 int x
= start_col_real
;
276 int x1
= start_col
+ EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
277 int y
= row
+ EDIT_TEXT_VERTICAL_OFFSET
;
278 int cols_to_skip
= abs (x
);
279 unsigned char str
[6 + 1];
281 tty_setcolor (EDITOR_NORMAL_COLOR
);
282 tty_draw_hline (edit
->widget
.y
+ y
, edit
->widget
.x
+ x1
,
283 ' ', end_col
+ 1 - start_col
);
285 if (option_line_state
) {
287 for (i
= 0; i
< LINE_STATE_WIDTH
; i
++)
288 if (status
[i
] == '\0')
291 tty_setcolor (LINE_STATE_COLOR
);
292 edit_move (x1
+ FONT_OFFSET_X
- option_line_state_width
, y
+ FONT_OFFSET_Y
);
293 tty_print_string (status
);
296 edit_move (x1
+ FONT_OFFSET_X
, y
+ FONT_OFFSET_Y
);
301 unsigned int textchar
;
310 style
= p
->style
& 0xFF00;
312 color
= p
->style
>> 16;
314 if (style
& MOD_ABNORMAL
) {
315 /* Non-printable - use black background */
319 if (style
& MOD_WHITESPACE
) {
320 if (style
& MOD_MARKED
) {
322 tty_setcolor (EDITOR_MARKED_COLOR
);
325 if (color
!= EDITOR_NORMAL_COLOR
) {
327 tty_lowlevel_setcolor (color
);
330 tty_setcolor (EDITOR_WHITESPACE_COLOR
);
333 if (style
& MOD_BOLD
) {
334 tty_setcolor (EDITOR_BOLD_COLOR
);
335 } else if (style
& MOD_MARKED
) {
336 tty_setcolor (EDITOR_MARKED_COLOR
);
338 tty_lowlevel_setcolor (color
);
341 tty_print_anychar (textchar
);
346 int visible_tabs
= 1, visible_tws
= 1;
348 /* b is a pointer to the beginning of the line */
350 edit_draw_this_line (WEdit
*edit
, long b
, long row
, long start_col
,
353 struct line_s line
[MAX_LINE_LEN
];
354 struct line_s
*p
= line
;
356 long m1
= 0, m2
= 0, q
, c1
, c2
;
357 int col
, start_col_real
;
364 unsigned int cur_line
= 0;
366 char line_stat
[LINE_STATE_WIDTH
+ 1];
368 if (row
> edit
->num_widget_lines
- EDIT_TEXT_VERTICAL_OFFSET
) {
371 if (book_mark_query_color(edit
, edit
->start_line
+ row
, BOOK_MARK_COLOR
)) {
372 book_mark
= BOOK_MARK_COLOR
;
373 } else if (book_mark_query_color(edit
, edit
->start_line
+ row
, BOOK_MARK_FOUND_COLOR
)) {
374 book_mark
= BOOK_MARK_FOUND_COLOR
;
378 abn_style
= book_mark
<< 16;
380 abn_style
= MOD_ABNORMAL
;
382 end_col
-= EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
384 edit_get_syntax_color (edit
, b
- 1, &color
);
385 q
= edit_move_forward3 (edit
, b
, start_col
- edit
->start_col
, 0);
386 start_col_real
= (col
=
387 (int) edit_move_forward3 (edit
, b
, 0,
388 q
)) + edit
->start_col
;
389 if ( option_line_state
) {
390 cur_line
= edit
->start_line
+ row
;
391 if ( cur_line
<= edit
->total_lines
) {
392 g_snprintf (line_stat
, LINE_STATE_WIDTH
+ 1, "%7i ", cur_line
+ 1);
394 memset(line_stat
, ' ', LINE_STATE_WIDTH
);
395 line_stat
[LINE_STATE_WIDTH
] = '\0';
397 if (book_mark_query_color (edit
, cur_line
, BOOK_MARK_COLOR
)){
398 g_snprintf (line_stat
, 2, "*");
402 if (col
+ 16 > -edit
->start_col
) {
403 eval_marks (edit
, &m1
, &m2
);
405 if (row
<= edit
->total_lines
- edit
->start_line
) {
407 if (tty_use_colors () && visible_tws
) {
408 tws
= edit_eol (edit
, b
);
409 while (tws
> b
&& ((c
= edit_get_byte (edit
, tws
- 1)) == ' '
414 while (col
<= end_col
- edit
->start_col
) {
417 if (q
== edit
->curs1
)
418 p
->style
|= MOD_CURSOR
;
419 if (q
>= m1
&& q
< m2
) {
420 if (column_highlighting
) {
422 x
= edit_move_forward3 (edit
, b
, 0, q
);
423 c1
= min (edit
->column1
, edit
->column2
);
424 c2
= max (edit
->column1
, edit
->column2
);
425 if (x
>= c1
&& x
< c2
)
426 p
->style
|= MOD_MARKED
;
428 p
->style
|= MOD_MARKED
;
430 if (q
== edit
->bracket
)
431 p
->style
|= MOD_BOLD
;
432 if (q
>= edit
->found_start
433 && q
< edit
->found_start
+ edit
->found_len
)
434 p
->style
|= MOD_BOLD
;
437 c
= edit_get_byte (edit
, q
);
439 c
= edit_get_utf (edit
, q
, &cw
);
441 /* we don't use bg for mc - fg contains both */
443 p
->style
|= book_mark
<< 16;
445 edit_get_syntax_color (edit
, q
, &color
);
446 p
->style
|= color
<< 16;
450 col
= (end_col
+ utf8lag
) - edit
->start_col
+ 1; /* quit */
453 i
= TAB_SIZE
- ((int) col
% TAB_SIZE
);
455 if (tty_use_colors() &&
456 ((visible_tabs
|| (visible_tws
&& q
>= tws
)) && enable_show_tabs_tws
)) {
457 if (p
->style
& MOD_MARKED
)
460 c
|= book_mark
<< 16;
462 c
= p
->style
| MOD_WHITESPACE
;
487 } else if (tty_use_colors() && visible_tws
&& q
>= tws
&& enable_show_tabs_tws
) {
489 p
->style
|= MOD_WHITESPACE
;
490 c
= p
->style
& ~MOD_CURSOR
;
499 c
= p
->style
& ~MOD_CURSOR
;
509 if (tty_use_colors() && visible_tws
&& q
>= tws
&& enable_show_tabs_tws
) {
511 p
->style
|= MOD_WHITESPACE
;
519 if ( utf8_display
) {
521 c
= convert_from_8bit_to_utf_c ((unsigned char) c
, edit
->converter
);
525 c
= convert_from_utf_to_current_c (c
, edit
->converter
);
528 c
= convert_to_display_c (c
);
533 /* Caret notation for control characters */
536 p
->style
= abn_style
;
539 p
->style
= abn_style
;
546 p
->style
= abn_style
;
549 p
->style
= abn_style
;
555 int utf8_display
= 0;
558 if ( ( utf8_display
&& g_unichar_isprint (c
) ) ||
559 ( utf8_display
== 0 && is_printable (c
) ) ) {
564 p
->style
= abn_style
;
568 if ( g_unichar_isprint (c
) ) {
573 p
->style
= abn_style
;
587 start_col_real
= start_col
= 0;
591 print_to_widget (edit
, row
, start_col
, start_col_real
, end_col
, line
, line_stat
);
594 #define key_pending(x) (!is_idle())
596 static void edit_draw_this_char (WEdit
* edit
, long curs
, long row
)
598 int b
= edit_bol (edit
, curs
);
599 edit_draw_this_line (edit
, b
, row
, 0, edit
->num_widget_columns
- 1);
602 /* cursor must be in screen for other than REDRAW_PAGE passed in force */
604 render_edit_text (WEdit
* edit
, long start_row
, long start_column
, long end_row
,
607 long row
= 0, curs_row
;
608 static int prev_curs_row
= 0;
609 static long prev_curs
= 0;
611 int force
= edit
->force
;
615 * If the position of the page has not moved then we can draw the cursor
616 * character only. This will prevent line flicker when using arrow keys.
618 if ((!(force
& REDRAW_CHAR_ONLY
)) || (force
& REDRAW_PAGE
)) {
619 if (!(force
& REDRAW_IN_BOUNDS
)) { /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
621 end_row
= edit
->num_widget_lines
- 1;
623 end_column
= edit
->num_widget_columns
- 1;
625 if (force
& REDRAW_PAGE
) {
627 b
= edit_move_forward (edit
, edit
->start_display
, start_row
, 0);
628 while (row
<= end_row
) {
629 if (key_pending (edit
))
631 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
632 b
= edit_move_forward (edit
, b
, 1, 0);
636 curs_row
= edit
->curs_row
;
638 if (force
& REDRAW_BEFORE_CURSOR
) {
639 if (start_row
< curs_row
) {
640 long upto
= curs_row
- 1 <= end_row
? curs_row
- 1 : end_row
;
642 b
= edit
->start_display
;
643 while (row
<= upto
) {
644 if (key_pending (edit
))
646 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
647 b
= edit_move_forward (edit
, b
, 1, 0);
651 /* if (force & REDRAW_LINE) ---> default */
652 b
= edit_bol (edit
, edit
->curs1
);
653 if (curs_row
>= start_row
&& curs_row
<= end_row
) {
654 if (key_pending (edit
))
656 edit_draw_this_line (edit
, b
, curs_row
, start_column
, end_column
);
658 if (force
& REDRAW_AFTER_CURSOR
) {
659 if (end_row
> curs_row
) {
660 row
= curs_row
+ 1 < start_row
? start_row
: curs_row
+ 1;
661 b
= edit_move_forward (edit
, b
, 1, 0);
662 while (row
<= end_row
) {
663 if (key_pending (edit
))
665 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
666 b
= edit_move_forward (edit
, b
, 1, 0);
671 if (force
& REDRAW_LINE_ABOVE
&& curs_row
>= 1) {
673 b
= edit_move_backward (edit
, edit_bol (edit
, edit
->curs1
), 1);
674 if (row
>= start_row
&& row
<= end_row
) {
675 if (key_pending (edit
))
677 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
680 if (force
& REDRAW_LINE_BELOW
&& row
< edit
->num_widget_lines
- 1) {
682 b
= edit_bol (edit
, edit
->curs1
);
683 b
= edit_move_forward (edit
, b
, 1, 0);
684 if (row
>= start_row
&& row
<= end_row
) {
685 if (key_pending (edit
))
687 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
692 if (prev_curs_row
< edit
->curs_row
) { /* with the new text highlighting, we must draw from the top down */
693 edit_draw_this_char (edit
, prev_curs
, prev_curs_row
);
694 edit_draw_this_char (edit
, edit
->curs1
, edit
->curs_row
);
696 edit_draw_this_char (edit
, edit
->curs1
, edit
->curs_row
);
697 edit_draw_this_char (edit
, prev_curs
, prev_curs_row
);
703 prev_curs_row
= edit
->curs_row
;
704 prev_curs
= edit
->curs1
;
707 edit
->screen_modified
= 0;
712 edit_render (WEdit
* edit
, int page
, int row_start
, int col_start
, int row_end
, int col_end
)
714 if (page
) /* if it was an expose event, 'page' would be set */
715 edit
->force
|= REDRAW_PAGE
| REDRAW_IN_BOUNDS
;
717 if (edit
->force
& REDRAW_COMPLETELY
)
718 buttonbar_redraw (edit
->widget
.parent
);
719 render_edit_text (edit
, row_start
, col_start
, row_end
, col_end
);
721 * edit->force != 0 means a key was pending and the redraw
722 * was halted, so next time we must redraw everything in case stuff
723 * was left undrawn from a previous key press.
726 edit
->force
|= REDRAW_PAGE
;
729 void edit_render_keypress (WEdit
* edit
)
731 edit_render (edit
, 0, 0, 0, 0, 0);