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;
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())
83 #define EDITOR_MINIMUM_TERMINAL_WIDTH 30
86 /*** file scope type declarations ****************************************************************/
94 /*** file scope variables ************************************************************************/
96 /*** file scope functions ************************************************************************/
97 /* --------------------------------------------------------------------------------------------- */
100 status_string (WEdit
* edit
, char *s
, int w
)
103 unsigned char cur_byte
= 0;
104 unsigned int cur_utf
= 0;
108 * If we are at the end of file, print <EOF>,
109 * otherwise print the current character as is (if printable),
110 * as decimal and as hex.
112 if (edit
->curs1
< edit
->last_byte
)
116 cur_byte
= edit_get_byte (edit
, edit
->curs1
);
118 g_snprintf (byte_str
, sizeof (byte_str
), "%4d 0x%03X",
119 (int) cur_byte
, (unsigned) cur_byte
);
123 cur_utf
= edit_get_utf (edit
, edit
->curs1
, &cw
);
126 g_snprintf (byte_str
, sizeof (byte_str
), "%04d 0x%03X",
127 (unsigned) cur_utf
, (unsigned) cur_utf
);
131 cur_utf
= edit_get_byte (edit
, edit
->curs1
);
132 g_snprintf (byte_str
, sizeof (byte_str
), "%04d 0x%03X",
133 (int) cur_utf
, (unsigned) cur_utf
);
140 strcpy (byte_str
, "<EOF> ");
143 /* The field lengths just prevent the status line from shortening too much */
144 if (simple_statusbar
)
146 "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s",
147 edit
->mark1
!= edit
->mark2
? (edit
->column_highlight
? 'C' : 'B') : '-',
148 edit
->modified
? 'M' : '-',
149 macro_index
< 0 ? '-' : 'R',
150 edit
->overwrite
== 0 ? '-' : 'O',
151 edit
->curs_col
+ edit
->over_col
,
153 edit
->total_lines
+ 1, edit
->curs1
, edit
->last_byte
, byte_str
,
155 mc_global
.source_codepage
>=
156 0 ? get_codepage_id (mc_global
.source_codepage
) : ""
163 "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb) %s %s",
164 edit
->mark1
!= edit
->mark2
? (edit
->column_highlight
? 'C' : 'B') : '-',
165 edit
->modified
? 'M' : '-',
166 macro_index
< 0 ? '-' : 'R',
167 edit
->overwrite
== 0 ? '-' : 'O',
168 edit
->curs_col
+ edit
->over_col
,
169 edit
->start_line
+ 1,
172 edit
->total_lines
+ 1, edit
->curs1
, edit
->last_byte
, byte_str
,
174 mc_global
.source_codepage
>=
175 0 ? get_codepage_id (mc_global
.source_codepage
) : ""
182 /* --------------------------------------------------------------------------------------------- */
185 printwstr (const char *s
, int len
)
188 tty_printf ("%-*.*s", len
, len
, s
);
191 /* --------------------------------------------------------------------------------------------- */
194 print_to_widget (WEdit
* edit
, long row
, int start_col
, int start_col_real
,
195 long end_col
, struct line_s line
[], char *status
, int bookmarked
)
199 int x
= start_col_real
;
200 int x1
= start_col
+ EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
201 int y
= row
+ EDIT_TEXT_VERTICAL_OFFSET
;
202 int cols_to_skip
= abs (x
);
207 tty_setcolor (EDITOR_NORMAL_COLOR
);
209 tty_setcolor (bookmarked
);
211 len
= end_col
+ 1 - start_col
;
212 wrap_start
= option_word_wrap_line_length
+ edit
->start_col
;
214 if (len
> 0 && edit
->widget
.y
+ y
>= 0)
216 if (!show_right_margin
|| wrap_start
> end_col
)
217 tty_draw_hline (edit
->widget
.y
+ y
, edit
->widget
.x
+ x1
, ' ', len
);
218 else if (wrap_start
< 0)
220 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR
);
221 tty_draw_hline (edit
->widget
.y
+ y
, edit
->widget
.x
+ x1
, ' ', len
);
226 tty_draw_hline (edit
->widget
.y
+ y
, edit
->widget
.x
+ x1
, ' ', wrap_start
);
231 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR
);
232 tty_draw_hline (edit
->widget
.y
+ y
, edit
->widget
.x
+ x1
+ wrap_start
, ' ', len
);
237 if (option_line_state
)
239 tty_setcolor (LINE_STATE_COLOR
);
240 for (i
= 0; i
< LINE_STATE_WIDTH
; i
++)
242 edit_move (x1
+ i
+ FONT_OFFSET_X
- option_line_state_width
, y
+ FONT_OFFSET_Y
);
243 if (status
[i
] == '\0')
245 tty_print_char (status
[i
]);
249 edit_move (x1
+ FONT_OFFSET_X
, y
+ FONT_OFFSET_Y
);
255 unsigned int textchar
;
265 style
= p
->style
& 0xFF00;
267 color
= p
->style
>> 16;
269 if (style
& MOD_ABNORMAL
)
271 /* Non-printable - use black background */
275 if (style
& MOD_WHITESPACE
)
277 if (style
& MOD_MARKED
)
280 tty_setcolor (EDITOR_MARKED_COLOR
);
285 if (color
!= EDITOR_NORMAL_COLOR
)
288 tty_lowlevel_setcolor (color
);
292 tty_setcolor (EDITOR_WHITESPACE_COLOR
);
297 if (style
& MOD_BOLD
)
299 tty_setcolor (EDITOR_BOLD_COLOR
);
301 else if (style
& MOD_MARKED
)
303 tty_setcolor (EDITOR_MARKED_COLOR
);
307 tty_lowlevel_setcolor (color
);
310 if (show_right_margin
)
312 if (i
> option_word_wrap_line_length
+ edit
->start_col
)
313 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR
);
316 tty_print_anychar (textchar
);
321 /* --------------------------------------------------------------------------------------------- */
322 /** b is a pointer to the beginning of the line */
325 edit_draw_this_line (WEdit
* edit
, long b
, long row
, long start_col
, long end_col
)
327 struct line_s line
[MAX_LINE_LEN
];
328 struct line_s
*p
= line
;
330 long m1
= 0, m2
= 0, q
, c1
, c2
;
331 int col
, start_col_real
;
337 unsigned int cur_line
= 0;
339 char line_stat
[LINE_STATE_WIDTH
+ 1] = "\0";
341 if (row
> edit
->widget
.lines
- 1 - EDIT_TEXT_VERTICAL_OFFSET
)
344 if (book_mark_query_color (edit
, edit
->start_line
+ row
, BOOK_MARK_COLOR
))
345 book_mark
= BOOK_MARK_COLOR
;
346 else if (book_mark_query_color (edit
, edit
->start_line
+ row
, BOOK_MARK_FOUND_COLOR
))
347 book_mark
= BOOK_MARK_FOUND_COLOR
;
350 abn_style
= book_mark
<< 16;
352 abn_style
= MOD_ABNORMAL
;
354 end_col
-= EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
356 edit_get_syntax_color (edit
, b
- 1, &color
);
357 q
= edit_move_forward3 (edit
, b
, start_col
- edit
->start_col
, 0);
358 start_col_real
= (col
= (int) edit_move_forward3 (edit
, b
, 0, q
)) + edit
->start_col
;
359 if (option_line_state
)
361 cur_line
= edit
->start_line
+ row
;
362 if (cur_line
<= (unsigned int) edit
->total_lines
)
364 g_snprintf (line_stat
, LINE_STATE_WIDTH
+ 1, "%7i ", cur_line
+ 1);
368 memset (line_stat
, ' ', LINE_STATE_WIDTH
);
369 line_stat
[LINE_STATE_WIDTH
] = '\0';
371 if (book_mark_query_color (edit
, cur_line
, BOOK_MARK_COLOR
))
373 g_snprintf (line_stat
, 2, "*");
377 if (col
+ 16 > -edit
->start_col
)
379 eval_marks (edit
, &m1
, &m2
);
381 if (row
<= edit
->total_lines
- edit
->start_line
)
384 if (tty_use_colors () && visible_tws
)
386 tws
= edit_eol (edit
, b
);
387 while (tws
> b
&& ((c
= edit_get_byte (edit
, tws
- 1)) == ' ' || c
== '\t'))
391 while (col
<= end_col
- edit
->start_col
)
397 if (q
== edit
->curs1
)
398 p
->style
|= MOD_CURSOR
;
399 if (q
>= m1
&& q
< m2
)
401 if (edit
->column_highlight
)
404 x
= edit_move_forward3 (edit
, b
, 0, q
);
405 c1
= min (edit
->column1
, edit
->column2
);
406 c2
= max (edit
->column1
, edit
->column2
);
407 if (x
>= c1
&& x
< c2
)
408 p
->style
|= MOD_MARKED
;
411 p
->style
|= MOD_MARKED
;
413 if (q
== edit
->bracket
)
414 p
->style
|= MOD_BOLD
;
415 if (q
>= edit
->found_start
&& q
< edit
->found_start
+ edit
->found_len
)
416 p
->style
|= MOD_BOLD
;
420 c
= edit_get_byte (edit
, q
);
424 c
= edit_get_utf (edit
, q
, &cw
);
426 /* we don't use bg for mc - fg contains both */
429 p
->style
|= book_mark
<< 16;
433 edit_get_syntax_color (edit
, q
, &color
);
434 p
->style
|= color
<< 16;
439 col
= (end_col
+ utf8lag
) - edit
->start_col
+ 1; /* quit */
442 i
= TAB_SIZE
- ((int) col
% TAB_SIZE
);
444 if (tty_use_colors () &&
445 ((visible_tabs
|| (visible_tws
&& q
>= tws
)) && enable_show_tabs_tws
))
447 if (p
->style
& MOD_MARKED
)
450 c
|= book_mark
<< 16;
452 c
= p
->style
| MOD_WHITESPACE
;
484 else if (tty_use_colors () && visible_tws
&& q
>= tws
&& enable_show_tabs_tws
)
487 p
->style
|= MOD_WHITESPACE
;
488 c
= p
->style
& ~MOD_CURSOR
;
500 c
= p
->style
& ~MOD_CURSOR
;
511 if (tty_use_colors () && visible_tws
&& q
>= tws
&& enable_show_tabs_tws
)
514 p
->style
|= MOD_WHITESPACE
;
522 if (mc_global
.utf8_display
)
526 c
= convert_from_8bit_to_utf_c ((unsigned char) c
, edit
->converter
);
530 c
= convert_from_utf_to_current_c (c
, edit
->converter
);
533 c
= convert_to_display_c (c
);
535 /* Caret notation for control characters */
539 p
->style
= abn_style
;
542 p
->style
= abn_style
;
550 p
->style
= abn_style
;
553 p
->style
= abn_style
;
560 if ((mc_global
.utf8_display
&& g_unichar_isprint (c
)) ||
561 (!mc_global
.utf8_display
&& is_printable (c
)))
569 p
->style
= abn_style
;
575 if (g_unichar_isprint (c
))
583 p
->style
= abn_style
;
601 start_col_real
= start_col
= 0;
606 print_to_widget (edit
, row
, start_col
, start_col_real
, end_col
, line
, line_stat
, book_mark
);
609 /* --------------------------------------------------------------------------------------------- */
612 edit_draw_this_char (WEdit
* edit
, long curs
, long row
, long start_column
, long end_column
)
614 int b
= edit_bol (edit
, curs
);
615 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
618 /* --------------------------------------------------------------------------------------------- */
619 /** cursor must be in screen for other than REDRAW_PAGE passed in force */
622 render_edit_text (WEdit
* edit
, long start_row
, long start_column
, long end_row
, long end_column
)
624 static long prev_curs_row
= 0;
625 static long prev_curs
= 0;
627 Widget
*w
= (Widget
*) edit
;
628 Dlg_head
*h
= w
->owner
;
630 long row
= 0, curs_row
;
631 int force
= edit
->force
;
638 /* draw only visible region */
640 last_line
= h
->y
+ h
->lines
- 1;
643 if (y1
> last_line
- 1 /* buttonbar */ )
646 last_column
= h
->x
+ h
->cols
- 1;
649 if (x1
> last_column
)
652 y2
= w
->y
+ w
->lines
- 1;
653 if (y2
< h
->y
+ 1 /* menubar */ )
656 x2
= w
->x
+ w
->cols
- 1;
660 if ((force
& REDRAW_IN_BOUNDS
) == 0)
662 /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
663 /* draw only visible region */
665 if (y2
<= last_line
- 1 /* buttonbar */ )
666 end_row
= w
->lines
- 1;
667 else if (y1
>= h
->y
+ 1 /* menubar */ )
668 end_row
= h
->lines
- 1 - y1
- 1;
670 end_row
= start_row
+ h
->lines
- 1 - 1;
672 if (x2
<= last_column
)
673 end_column
= w
->cols
- 1;
675 end_column
= h
->cols
- 1 - x1
;
677 end_column
= start_column
+ h
->cols
- 1;
681 * If the position of the page has not moved then we can draw the cursor
682 * character only. This will prevent line flicker when using arrow keys.
684 if ((force
& REDRAW_CHAR_ONLY
) == 0 || (force
& REDRAW_PAGE
) != 0)
686 if ((force
& REDRAW_PAGE
) != 0)
689 b
= edit_move_forward (edit
, edit
->start_display
, start_row
, 0);
690 while (row
<= end_row
)
692 if (key_pending (edit
))
694 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
695 b
= edit_move_forward (edit
, b
, 1, 0);
701 curs_row
= edit
->curs_row
;
703 if ((force
& REDRAW_BEFORE_CURSOR
) != 0 && start_row
< curs_row
)
705 long upto
= curs_row
- 1 <= end_row
? curs_row
- 1 : end_row
;
708 b
= edit
->start_display
;
711 if (key_pending (edit
))
713 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
714 b
= edit_move_forward (edit
, b
, 1, 0);
718 /* if (force & REDRAW_LINE) ---> default */
719 b
= edit_bol (edit
, edit
->curs1
);
720 if (curs_row
>= start_row
&& curs_row
<= end_row
)
722 if (key_pending (edit
))
724 edit_draw_this_line (edit
, b
, curs_row
, start_column
, end_column
);
727 if ((force
& REDRAW_AFTER_CURSOR
) != 0 && end_row
> curs_row
)
729 row
= curs_row
+ 1 < start_row
? start_row
: curs_row
+ 1;
730 b
= edit_move_forward (edit
, b
, 1, 0);
731 while (row
<= end_row
)
733 if (key_pending (edit
))
735 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
736 b
= edit_move_forward (edit
, b
, 1, 0);
741 if ((force
& REDRAW_LINE_ABOVE
) != 0 && curs_row
>= 1)
744 b
= edit_move_backward (edit
, edit_bol (edit
, edit
->curs1
), 1);
745 if (row
>= start_row
&& row
<= end_row
)
747 if (key_pending (edit
))
749 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
753 if ((force
& REDRAW_LINE_BELOW
) != 0 && row
< edit
->widget
.lines
- 1)
756 b
= edit_bol (edit
, edit
->curs1
);
757 b
= edit_move_forward (edit
, b
, 1, 0);
758 if (row
>= start_row
&& row
<= end_row
)
760 if (key_pending (edit
))
762 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
767 else if (prev_curs_row
< edit
->curs_row
)
769 /* with the new text highlighting, we must draw from the top down */
770 edit_draw_this_char (edit
, prev_curs
, prev_curs_row
, start_column
, end_column
);
771 edit_draw_this_char (edit
, edit
->curs1
, edit
->curs_row
, start_column
, end_column
);
775 edit_draw_this_char (edit
, edit
->curs1
, edit
->curs_row
, start_column
, end_column
);
776 edit_draw_this_char (edit
, prev_curs
, prev_curs_row
, start_column
, end_column
);
781 prev_curs_row
= edit
->curs_row
;
782 prev_curs
= edit
->curs1
;
785 /* --------------------------------------------------------------------------------------------- */
788 edit_render (WEdit
* edit
, int page
, int row_start
, int col_start
, int row_end
, int col_end
)
790 if (page
) /* if it was an expose event, 'page' would be set */
791 edit
->force
|= REDRAW_PAGE
| REDRAW_IN_BOUNDS
;
793 if (edit
->force
& REDRAW_COMPLETELY
)
794 buttonbar_redraw (find_buttonbar (edit
->widget
.owner
));
795 render_edit_text (edit
, row_start
, col_start
, row_end
, col_end
);
797 * edit->force != 0 means a key was pending and the redraw
798 * was halted, so next time we must redraw everything in case stuff
799 * was left undrawn from a previous key press.
802 edit
->force
|= REDRAW_PAGE
;
805 /* --------------------------------------------------------------------------------------------- */
806 /*** public functions ****************************************************************************/
807 /* --------------------------------------------------------------------------------------------- */
809 /** Draw the status line at the top of the screen. The size of the filename
810 * field varies depending on the width of the screen and the length of
813 edit_status (WEdit
* edit
)
815 const int w
= edit
->widget
.owner
->cols
;
816 const size_t status_size
= w
+ 1;
817 char *const status
= g_malloc (status_size
);
819 const char *fname
= "";
821 const int gap
= 3; /* between the filename and the status */
822 const int right_gap
= 5; /* at the right end of the screen */
823 const int preferred_fname_len
= 16;
825 status_string (edit
, status
, status_size
);
826 status_len
= (int) str_term_width1 (status
);
829 fname
= edit
->filename
;
830 fname_len
= str_term_width1 (fname
);
831 if (fname_len
< preferred_fname_len
)
832 fname_len
= preferred_fname_len
;
834 if (fname_len
+ gap
+ status_len
+ right_gap
>= w
)
836 if (preferred_fname_len
+ gap
+ status_len
+ right_gap
>= w
)
837 fname_len
= preferred_fname_len
;
839 fname_len
= w
- (gap
+ status_len
+ right_gap
);
840 fname
= str_trunc (fname
, fname_len
);
843 dlg_move (edit
->widget
.owner
, 0, 0);
844 tty_setcolor (STATUSBAR_COLOR
);
845 printwstr (fname
, fname_len
+ gap
);
846 printwstr (status
, w
- (fname_len
+ gap
));
848 if (simple_statusbar
&& w
> EDITOR_MINIMUM_TERMINAL_WIDTH
)
850 size_t percent
= 100;
852 if (edit
->total_lines
+ 1 != 0)
853 percent
= (edit
->curs_line
+ 1) * 100 / (edit
->total_lines
+ 1);
854 dlg_move (edit
->widget
.owner
, 0, w
- 5);
855 tty_printf (" %3d%%", percent
);
861 /* --------------------------------------------------------------------------------------------- */
862 /** this scrolls the text so that cursor is on the screen */
864 edit_scroll_screen_over_cursor (WEdit
* edit
)
868 int b_extreme
, t_extreme
, l_extreme
, r_extreme
;
870 if (edit
->widget
.lines
<= 0 || edit
->widget
.cols
<= 0)
873 edit
->widget
.cols
-= EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
874 edit
->widget
.lines
-= EDIT_TEXT_VERTICAL_OFFSET
;
876 r_extreme
= EDIT_RIGHT_EXTREME
;
877 l_extreme
= EDIT_LEFT_EXTREME
;
878 b_extreme
= EDIT_BOTTOM_EXTREME
;
879 t_extreme
= EDIT_TOP_EXTREME
;
882 b_extreme
= max (edit
->widget
.lines
/ 4, b_extreme
);
883 t_extreme
= max (edit
->widget
.lines
/ 4, t_extreme
);
885 if (b_extreme
+ t_extreme
+ 1 > edit
->widget
.lines
)
889 n
= b_extreme
+ t_extreme
;
892 b_extreme
= (b_extreme
* (edit
->widget
.lines
- 1)) / n
;
893 t_extreme
= (t_extreme
* (edit
->widget
.lines
- 1)) / n
;
895 if (l_extreme
+ r_extreme
+ 1 > edit
->widget
.cols
)
899 n
= l_extreme
+ t_extreme
;
902 l_extreme
= (l_extreme
* (edit
->widget
.cols
- 1)) / n
;
903 r_extreme
= (r_extreme
* (edit
->widget
.cols
- 1)) / n
;
905 p
= edit_get_col (edit
) + edit
->over_col
;
906 edit_update_curs_row (edit
);
907 outby
= p
+ edit
->start_col
- edit
->widget
.cols
+ 1 + (r_extreme
+ edit
->found_len
);
909 edit_scroll_right (edit
, outby
);
910 outby
= l_extreme
- p
- edit
->start_col
;
912 edit_scroll_left (edit
, outby
);
914 outby
= p
- edit
->widget
.lines
+ 1 + b_extreme
;
916 edit_scroll_downward (edit
, outby
);
917 outby
= t_extreme
- p
;
919 edit_scroll_upward (edit
, outby
);
920 edit_update_curs_row (edit
);
922 edit
->widget
.lines
+= EDIT_TEXT_VERTICAL_OFFSET
;
923 edit
->widget
.cols
+= EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
926 /* --------------------------------------------------------------------------------------------- */
929 edit_render_keypress (WEdit
* edit
)
931 edit_render (edit
, 0, 0, 0, 0, 0);
934 /* --------------------------------------------------------------------------------------------- */