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 "lib/global.h"
42 #include "lib/tty/tty.h" /* tty_printf() */
43 #include "lib/tty/key.h" /* is_idle() */
45 #include "lib/strutil.h" /* utf string functions */
46 #include "lib/util.h" /* is_printable() */
47 #include "lib/widget.h" /* buttonbar_redraw() */
48 #include "lib/charsets.h"
50 #include "src/setup.h" /* edit_tab_spacing */
52 #include "edit-impl.h"
53 #include "edit-widget.h"
55 /*** global variables ****************************************************************************/
57 /* Toggles statusbar draw style */
58 int simple_statusbar
= 0;
60 int visible_tabs
= 1, visible_tws
= 1, visible_eol
= 0;
62 /*** file scope macro definitions ****************************************************************/
64 #define MAX_LINE_LEN 1024
67 #define MOD_ABNORMAL (1 << 8)
68 #define MOD_BOLD (1 << 9)
69 #define MOD_MARKED (1 << 10)
70 #define MOD_CURSOR (1 << 11)
71 #define MOD_WHITESPACE (1 << 12)
73 #define FONT_OFFSET_X 0
74 #define FONT_OFFSET_Y 0
76 #define FONT_PIX_PER_LINE 1
77 #define FONT_MEAN_WIDTH 1
79 #define edit_move(x,y) widget_move(edit, y, x);
81 #define key_pending(x) (!is_idle())
84 /*** file scope type declarations ****************************************************************/
92 /*** file scope variables ************************************************************************/
94 /*** file scope functions ************************************************************************/
95 /* --------------------------------------------------------------------------------------------- */
98 status_string (WEdit
* edit
, char *s
, int w
)
101 unsigned char cur_byte
= 0;
102 unsigned int cur_utf
= 0;
106 * If we are at the end of file, print <EOF>,
107 * otherwise print the current character as is (if printable),
108 * as decimal and as hex.
110 if (edit
->curs1
< edit
->last_byte
)
114 cur_byte
= edit_get_byte (edit
, edit
->curs1
);
116 g_snprintf (byte_str
, sizeof (byte_str
), "%4d 0x%03X",
117 (int) cur_byte
, (unsigned) cur_byte
);
121 cur_utf
= edit_get_utf (edit
, edit
->curs1
, &cw
);
124 g_snprintf (byte_str
, sizeof (byte_str
), "%04d 0x%03X",
125 (unsigned) cur_utf
, (unsigned) cur_utf
);
129 cur_utf
= edit_get_byte (edit
, edit
->curs1
);
130 g_snprintf (byte_str
, sizeof (byte_str
), "%04d 0x%03X",
131 (int) cur_utf
, (unsigned) cur_utf
);
138 strcpy (byte_str
, "<EOF> ");
141 /* The field lengths just prevent the status line from shortening too much */
142 if (simple_statusbar
)
144 "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s",
145 edit
->mark1
!= edit
->mark2
? (edit
->column_highlight
? 'C' : 'B') : '-',
146 edit
->modified
? 'M' : '-',
147 macro_index
< 0 ? '-' : 'R',
148 edit
->overwrite
== 0 ? '-' : 'O',
149 edit
->curs_col
+ edit
->over_col
,
151 edit
->total_lines
+ 1, edit
->curs1
, edit
->last_byte
, byte_str
,
153 mc_global
.source_codepage
>= 0 ? get_codepage_id (mc_global
.source_codepage
) : ""
160 "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb) %s %s",
161 edit
->mark1
!= edit
->mark2
? (edit
->column_highlight
? 'C' : 'B') : '-',
162 edit
->modified
? 'M' : '-',
163 macro_index
< 0 ? '-' : 'R',
164 edit
->overwrite
== 0 ? '-' : 'O',
165 edit
->curs_col
+ edit
->over_col
,
166 edit
->start_line
+ 1,
169 edit
->total_lines
+ 1, edit
->curs1
, edit
->last_byte
, byte_str
,
171 mc_global
.source_codepage
>= 0 ? get_codepage_id (mc_global
.source_codepage
) : ""
178 /* --------------------------------------------------------------------------------------------- */
181 printwstr (const char *s
, int len
)
184 tty_printf ("%-*.*s", len
, len
, s
);
187 /* --------------------------------------------------------------------------------------------- */
190 print_to_widget (WEdit
* edit
, long row
, int start_col
, int start_col_real
,
191 long end_col
, struct line_s line
[], char *status
, int bookmarked
)
195 int x
= start_col_real
;
196 int x1
= start_col
+ EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
197 int y
= row
+ EDIT_TEXT_VERTICAL_OFFSET
;
198 int cols_to_skip
= abs (x
);
201 tty_setcolor (EDITOR_NORMAL_COLOR
);
203 tty_setcolor (bookmarked
);
205 if (!show_right_margin
)
207 tty_draw_hline (edit
->widget
.y
+ y
, edit
->widget
.x
+ x1
, ' ', end_col
+ 1 - start_col
);
209 else if (edit
->start_col
< option_word_wrap_line_length
)
211 tty_draw_hline (edit
->widget
.y
+ y
,
212 edit
->widget
.x
+ x1
, ' ', option_word_wrap_line_length
- edit
->start_col
);
214 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR
);
215 tty_draw_hline (edit
->widget
.y
+ y
,
216 edit
->widget
.x
+ x1
+ option_word_wrap_line_length
+ edit
->start_col
,
217 ' ', end_col
+ 1 - start_col
);
220 if (option_line_state
)
222 for (i
= 0; i
< LINE_STATE_WIDTH
; i
++)
223 if (status
[i
] == '\0')
226 tty_setcolor (LINE_STATE_COLOR
);
227 edit_move (x1
+ FONT_OFFSET_X
- option_line_state_width
, y
+ FONT_OFFSET_Y
);
228 tty_print_string (status
);
231 edit_move (x1
+ FONT_OFFSET_X
, y
+ FONT_OFFSET_Y
);
237 unsigned int textchar
;
247 style
= p
->style
& 0xFF00;
249 color
= p
->style
>> 16;
251 if (style
& MOD_ABNORMAL
)
253 /* Non-printable - use black background */
257 if (style
& MOD_WHITESPACE
)
259 if (style
& MOD_MARKED
)
262 tty_setcolor (EDITOR_MARKED_COLOR
);
267 if (color
!= EDITOR_NORMAL_COLOR
)
270 tty_lowlevel_setcolor (color
);
274 tty_setcolor (EDITOR_WHITESPACE_COLOR
);
279 if (style
& MOD_BOLD
)
281 tty_setcolor (EDITOR_BOLD_COLOR
);
283 else if (style
& MOD_MARKED
)
285 tty_setcolor (EDITOR_MARKED_COLOR
);
289 tty_lowlevel_setcolor (color
);
292 if (show_right_margin
)
294 if (i
> option_word_wrap_line_length
+ edit
->start_col
)
295 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR
);
298 tty_print_anychar (textchar
);
303 /* --------------------------------------------------------------------------------------------- */
304 /** b is a pointer to the beginning of the line */
307 edit_draw_this_line (WEdit
* edit
, long b
, long row
, long start_col
, long end_col
)
309 struct line_s line
[MAX_LINE_LEN
];
310 struct line_s
*p
= line
;
312 long m1
= 0, m2
= 0, q
, c1
, c2
;
313 int col
, start_col_real
;
319 unsigned int cur_line
= 0;
321 char line_stat
[LINE_STATE_WIDTH
+ 1];
323 if (row
> edit
->widget
.lines
- EDIT_TEXT_VERTICAL_OFFSET
)
326 if (book_mark_query_color (edit
, edit
->start_line
+ row
, BOOK_MARK_COLOR
))
327 book_mark
= BOOK_MARK_COLOR
;
328 else if (book_mark_query_color (edit
, edit
->start_line
+ row
, BOOK_MARK_FOUND_COLOR
))
329 book_mark
= BOOK_MARK_FOUND_COLOR
;
332 abn_style
= book_mark
<< 16;
334 abn_style
= MOD_ABNORMAL
;
336 end_col
-= EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
338 edit_get_syntax_color (edit
, b
- 1, &color
);
339 q
= edit_move_forward3 (edit
, b
, start_col
- edit
->start_col
, 0);
340 start_col_real
= (col
= (int) edit_move_forward3 (edit
, b
, 0, q
)) + edit
->start_col
;
341 if (option_line_state
)
343 cur_line
= edit
->start_line
+ row
;
344 if (cur_line
<= (unsigned int) edit
->total_lines
)
346 g_snprintf (line_stat
, LINE_STATE_WIDTH
+ 1, "%7i ", cur_line
+ 1);
350 memset (line_stat
, ' ', LINE_STATE_WIDTH
);
351 line_stat
[LINE_STATE_WIDTH
] = '\0';
353 if (book_mark_query_color (edit
, cur_line
, BOOK_MARK_COLOR
))
355 g_snprintf (line_stat
, 2, "*");
359 if (col
+ 16 > -edit
->start_col
)
361 eval_marks (edit
, &m1
, &m2
);
363 if (row
<= edit
->total_lines
- edit
->start_line
)
366 if (tty_use_colors () && visible_tws
)
368 tws
= edit_eol (edit
, b
);
369 while (tws
> b
&& ((c
= edit_get_byte (edit
, tws
- 1)) == ' ' || c
== '\t'))
373 while (col
<= end_col
- edit
->start_col
)
379 if (q
== edit
->curs1
)
380 p
->style
|= MOD_CURSOR
;
381 if (q
>= m1
&& q
< m2
)
383 if (edit
->column_highlight
)
386 x
= edit_move_forward3 (edit
, b
, 0, q
);
387 c1
= min (edit
->column1
, edit
->column2
);
388 c2
= max (edit
->column1
, edit
->column2
);
389 if (x
>= c1
&& x
< c2
)
390 p
->style
|= MOD_MARKED
;
393 p
->style
|= MOD_MARKED
;
395 if (q
== edit
->bracket
)
396 p
->style
|= MOD_BOLD
;
397 if (q
>= edit
->found_start
&& q
< edit
->found_start
+ edit
->found_len
)
398 p
->style
|= MOD_BOLD
;
402 c
= edit_get_byte (edit
, q
);
406 c
= edit_get_utf (edit
, q
, &cw
);
408 /* we don't use bg for mc - fg contains both */
411 p
->style
|= book_mark
<< 16;
415 edit_get_syntax_color (edit
, q
, &color
);
416 p
->style
|= color
<< 16;
421 col
= (end_col
+ utf8lag
) - edit
->start_col
+ 1; /* quit */
422 if (tty_use_colors() && visible_eol
) {
423 p
->ch
= widget_edit_eol_char
;
424 p
->style
|= MOD_WHITESPACE
;
430 i
= TAB_SIZE
- ((int) col
% TAB_SIZE
);
432 if (tty_use_colors () &&
433 ((visible_tabs
|| (visible_tws
&& q
>= tws
)) && enable_show_tabs_tws
))
435 if (p
->style
& MOD_MARKED
)
438 c
|= book_mark
<< 16;
440 c
= p
->style
| MOD_WHITESPACE
;
443 p
->ch
= widget_edit_tab_first
;
448 p
->ch
= widget_edit_tab_middle
;
452 p
->ch
= widget_edit_tab_last
;
458 p
->ch
= widget_edit_tab_first
;
461 p
->ch
= widget_edit_tab_last
;
467 p
->ch
= widget_edit_tab_last
;
472 else if (tty_use_colors () && visible_tws
&& q
>= tws
&& enable_show_tabs_tws
)
475 p
->style
|= MOD_WHITESPACE
;
476 c
= p
->style
& ~MOD_CURSOR
;
488 c
= p
->style
& ~MOD_CURSOR
;
499 if (tty_use_colors () && visible_tws
&& q
>= tws
&& enable_show_tabs_tws
)
502 p
->style
|= MOD_WHITESPACE
;
510 if (mc_global
.utf8_display
)
514 c
= convert_from_8bit_to_utf_c ((unsigned char) c
, edit
->converter
);
518 c
= convert_from_utf_to_current_c (c
, edit
->converter
);
521 c
= convert_to_display_c (c
);
523 /* Caret notation for control characters */
527 p
->style
= abn_style
;
530 p
->style
= abn_style
;
538 p
->style
= abn_style
;
541 p
->style
= abn_style
;
548 if ((mc_global
.utf8_display
&& g_unichar_isprint (c
)) ||
549 (!mc_global
.utf8_display
&& is_printable (c
)))
557 p
->style
= abn_style
;
563 if (g_unichar_isprint (c
))
571 p
->style
= abn_style
;
589 start_col_real
= start_col
= 0;
594 print_to_widget (edit
, row
, start_col
, start_col_real
, end_col
, line
, line_stat
, book_mark
);
597 /* --------------------------------------------------------------------------------------------- */
600 edit_draw_this_char (WEdit
* edit
, long curs
, long row
)
602 int b
= edit_bol (edit
, curs
);
603 edit_draw_this_line (edit
, b
, row
, 0, edit
->widget
.cols
- 1);
606 /* --------------------------------------------------------------------------------------------- */
607 /** cursor must be in screen for other than REDRAW_PAGE passed in force */
610 render_edit_text (WEdit
* edit
, long start_row
, long start_column
, long end_row
, long end_column
)
612 long row
= 0, curs_row
;
613 static long prev_curs_row
= 0;
614 static long prev_curs
= 0;
616 int force
= edit
->force
;
620 * If the position of the page has not moved then we can draw the cursor
621 * character only. This will prevent line flicker when using arrow keys.
623 if ((!(force
& REDRAW_CHAR_ONLY
)) || (force
& REDRAW_PAGE
))
625 if (!(force
& REDRAW_IN_BOUNDS
))
626 { /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
628 end_row
= edit
->widget
.lines
- 1;
630 end_column
= edit
->widget
.cols
- 1;
632 if (force
& REDRAW_PAGE
)
635 b
= edit_move_forward (edit
, edit
->start_display
, start_row
, 0);
636 while (row
<= end_row
)
638 if (key_pending (edit
))
640 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
641 b
= edit_move_forward (edit
, b
, 1, 0);
647 curs_row
= edit
->curs_row
;
649 if (force
& REDRAW_BEFORE_CURSOR
)
651 if (start_row
< curs_row
)
653 long upto
= curs_row
- 1 <= end_row
? curs_row
- 1 : end_row
;
655 b
= edit
->start_display
;
658 if (key_pending (edit
))
660 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
661 b
= edit_move_forward (edit
, b
, 1, 0);
665 /* if (force & REDRAW_LINE) ---> default */
666 b
= edit_bol (edit
, edit
->curs1
);
667 if (curs_row
>= start_row
&& curs_row
<= end_row
)
669 if (key_pending (edit
))
671 edit_draw_this_line (edit
, b
, curs_row
, start_column
, end_column
);
673 if (force
& REDRAW_AFTER_CURSOR
)
675 if (end_row
> curs_row
)
677 row
= curs_row
+ 1 < start_row
? start_row
: curs_row
+ 1;
678 b
= edit_move_forward (edit
, b
, 1, 0);
679 while (row
<= end_row
)
681 if (key_pending (edit
))
683 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
684 b
= edit_move_forward (edit
, b
, 1, 0);
689 if (force
& REDRAW_LINE_ABOVE
&& curs_row
>= 1)
692 b
= edit_move_backward (edit
, edit_bol (edit
, edit
->curs1
), 1);
693 if (row
>= start_row
&& row
<= end_row
)
695 if (key_pending (edit
))
697 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
700 if (force
& REDRAW_LINE_BELOW
&& row
< edit
->widget
.lines
- 1)
703 b
= edit_bol (edit
, edit
->curs1
);
704 b
= edit_move_forward (edit
, b
, 1, 0);
705 if (row
>= start_row
&& row
<= end_row
)
707 if (key_pending (edit
))
709 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
716 if (prev_curs_row
< edit
->curs_row
)
717 { /* with the new text highlighting, we must draw from the top down */
718 edit_draw_this_char (edit
, prev_curs
, prev_curs_row
);
719 edit_draw_this_char (edit
, edit
->curs1
, edit
->curs_row
);
723 edit_draw_this_char (edit
, edit
->curs1
, edit
->curs_row
);
724 edit_draw_this_char (edit
, prev_curs
, prev_curs_row
);
730 prev_curs_row
= edit
->curs_row
;
731 prev_curs
= edit
->curs1
;
734 /* --------------------------------------------------------------------------------------------- */
737 edit_render (WEdit
* edit
, int page
, int row_start
, int col_start
, int row_end
, int col_end
)
739 if (page
) /* if it was an expose event, 'page' would be set */
740 edit
->force
|= REDRAW_PAGE
| REDRAW_IN_BOUNDS
;
742 if (edit
->force
& REDRAW_COMPLETELY
)
743 buttonbar_redraw (find_buttonbar (edit
->widget
.owner
));
744 render_edit_text (edit
, row_start
, col_start
, row_end
, col_end
);
746 * edit->force != 0 means a key was pending and the redraw
747 * was halted, so next time we must redraw everything in case stuff
748 * was left undrawn from a previous key press.
751 edit
->force
|= REDRAW_PAGE
;
754 /* --------------------------------------------------------------------------------------------- */
755 /*** public functions ****************************************************************************/
756 /* --------------------------------------------------------------------------------------------- */
758 /** Draw the status line at the top of the widget. The size of the filename
759 * field varies depending on the width of the screen and the length of
762 edit_status (WEdit
* edit
)
764 const int w
= edit
->widget
.cols
;
765 const size_t status_size
= w
+ 1;
766 char *const status
= g_malloc (status_size
);
768 const char *fname
= "";
770 const int gap
= 3; /* between the filename and the status */
771 const int right_gap
= 5; /* at the right end of the screen */
772 const int preferred_fname_len
= 16;
774 status_string (edit
, status
, status_size
);
775 status_len
= (int) str_term_width1 (status
);
778 fname
= edit
->filename
;
779 fname_len
= str_term_width1 (fname
);
780 if (fname_len
< preferred_fname_len
)
781 fname_len
= preferred_fname_len
;
783 if (fname_len
+ gap
+ status_len
+ right_gap
>= w
)
785 if (preferred_fname_len
+ gap
+ status_len
+ right_gap
>= w
)
786 fname_len
= preferred_fname_len
;
788 fname_len
= w
- (gap
+ status_len
+ right_gap
);
789 fname
= str_trunc (fname
, fname_len
);
792 widget_move (edit
, 0, 0);
793 tty_setcolor (STATUSBAR_COLOR
);
794 printwstr (fname
, fname_len
+ gap
);
795 printwstr (status
, w
- (fname_len
+ gap
));
797 if (simple_statusbar
&& edit
->widget
.cols
> 30)
800 if (edit
->total_lines
+ 1 != 0)
801 percent
= (edit
->curs_line
+ 1) * 100 / (edit
->total_lines
+ 1);
804 widget_move (edit
, 0, edit
->widget
.cols
- 5);
805 tty_printf (" %3d%%", percent
);
811 /* --------------------------------------------------------------------------------------------- */
812 /** this scrolls the text so that cursor is on the screen */
814 edit_scroll_screen_over_cursor (WEdit
* edit
)
818 int b_extreme
, t_extreme
, l_extreme
, r_extreme
;
820 if (edit
->widget
.lines
<= 0 || edit
->widget
.cols
<= 0)
823 edit
->widget
.cols
-= EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
824 edit
->widget
.lines
-= EDIT_TEXT_VERTICAL_OFFSET
- 1;
826 r_extreme
= EDIT_RIGHT_EXTREME
;
827 l_extreme
= EDIT_LEFT_EXTREME
;
828 b_extreme
= EDIT_BOTTOM_EXTREME
;
829 t_extreme
= EDIT_TOP_EXTREME
;
832 b_extreme
= max (edit
->widget
.lines
/ 4, b_extreme
);
833 t_extreme
= max (edit
->widget
.lines
/ 4, t_extreme
);
835 if (b_extreme
+ t_extreme
+ 1 > edit
->widget
.lines
)
838 n
= b_extreme
+ t_extreme
;
839 b_extreme
= (b_extreme
* (edit
->widget
.lines
- 1)) / n
;
840 t_extreme
= (t_extreme
* (edit
->widget
.lines
- 1)) / n
;
842 if (l_extreme
+ r_extreme
+ 1 > edit
->widget
.cols
)
845 n
= l_extreme
+ t_extreme
;
846 l_extreme
= (l_extreme
* (edit
->widget
.cols
- 1)) / n
;
847 r_extreme
= (r_extreme
* (edit
->widget
.cols
- 1)) / n
;
849 p
= edit_get_col (edit
) + edit
->over_col
;
850 edit_update_curs_row (edit
);
851 outby
= p
+ edit
->start_col
- edit
->widget
.cols
+ 1 + (r_extreme
+ edit
->found_len
);
853 edit_scroll_right (edit
, outby
);
854 outby
= l_extreme
- p
- edit
->start_col
;
856 edit_scroll_left (edit
, outby
);
858 outby
= p
- edit
->widget
.lines
+ 1 + b_extreme
;
860 edit_scroll_downward (edit
, outby
);
861 outby
= t_extreme
- p
;
863 edit_scroll_upward (edit
, outby
);
864 edit_update_curs_row (edit
);
866 edit
->widget
.lines
+= EDIT_TEXT_VERTICAL_OFFSET
- 1;
867 edit
->widget
.cols
+= EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
870 /* --------------------------------------------------------------------------------------------- */
873 edit_render_keypress (WEdit
* edit
)
875 edit_render (edit
, 0, 0, 0, 0, 0);
878 /* --------------------------------------------------------------------------------------------- */