1 /* editor text drawing.
3 Copyright (C) 1996, 1997 the Free Software Foundation
5 Authors: 1996, 1997 Paul Sheer
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include <sys/types.h>
36 #include "../src/global.h"
39 #include "edit-widget.h"
41 #define MAX_LINE_LEN 1024
43 #include "../src/color.h" /* EDITOR_NORMAL_COLOR */
44 #include "../src/tty.h" /* attrset() */
45 #include "../src/widget.h" /* buttonbar_redraw() */
46 #include "../src/key.h" /* is_idle() */
47 #include "../src/charsets.h"
50 #define MOD_ABNORMAL (1 << 8)
51 #define MOD_BOLD (1 << 9)
52 #define MOD_MARKED (1 << 10)
53 #define MOD_CURSOR (1 << 11)
55 #define FONT_OFFSET_X 0
56 #define FONT_OFFSET_Y 0
58 #define FONT_PIX_PER_LINE 1
59 #define FONT_MEAN_WIDTH 1
62 static void status_string (WEdit
* edit
, char *s
, int w
)
67 * If we are at the end of file, print <EOF>,
68 * otherwise print the current character as is (if printable),
69 * as decimal and as hex.
71 if (edit
->curs1
< edit
->last_byte
) {
72 unsigned char cur_byte
= edit_get_byte (edit
, edit
->curs1
);
73 g_snprintf (byte_str
, sizeof (byte_str
), "%c %3d 0x%02X",
74 is_printable (cur_byte
) ? cur_byte
: '.',
78 strcpy (byte_str
, "<EOF>");
81 /* The field lengths just prevent the status line from shortening too much */
83 "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb)= %s",
84 edit
->mark1
!= edit
->mark2
? ( column_highlighting
? 'C' : 'B') : '-',
85 edit
->modified
? 'M' : '-',
86 edit
->macro_i
< 0 ? '-' : 'R',
87 edit
->overwrite
== 0 ? '-' : 'O',
93 edit
->total_lines
+ 1,
101 printwstr (const char *s
, int len
)
104 tty_printf ("%-*.*s", len
, len
, s
);
107 /* Draw the status line at the top of the widget. The size of the filename
108 * field varies depending on the width of the screen and the length of
111 edit_status (WEdit
*edit
)
113 const int w
= edit
->widget
.cols
;
114 const size_t status_size
= w
+ 1;
115 char * const status
= g_malloc (status_size
);
117 const char *fname
= "";
119 const int gap
= 3; /* between the filename and the status */
120 const int right_gap
= 2; /* at the right end of the screen */
121 const int preferred_fname_len
= 16;
123 status_string (edit
, status
, status_size
);
124 status_len
= (int) strlen (status
);
127 fname
= edit
->filename
;
128 fname_len
= strlen(fname
);
129 if (fname_len
< preferred_fname_len
)
130 fname_len
= preferred_fname_len
;
132 if (fname_len
+ gap
+ status_len
+ right_gap
>= w
) {
133 if (preferred_fname_len
+ gap
+ status_len
+ right_gap
>= w
)
134 fname_len
= preferred_fname_len
;
136 fname_len
= w
- (gap
+ status_len
+ right_gap
);
137 fname
= name_trunc (fname
, fname_len
);
140 widget_move (edit
, 0, 0);
141 attrset (SELECTED_COLOR
);
142 printwstr (fname
, fname_len
+ gap
);
143 printwstr (status
, w
- (fname_len
+ gap
));
144 attrset (EDITOR_NORMAL_COLOR
);
149 /* this scrolls the text so that cursor is on the screen */
150 void edit_scroll_screen_over_cursor (WEdit
* edit
)
154 int b_extreme
, t_extreme
, l_extreme
, r_extreme
;
156 if (edit
->num_widget_lines
<= 0 || edit
->num_widget_columns
<= 0)
159 r_extreme
= EDIT_RIGHT_EXTREME
;
160 l_extreme
= EDIT_LEFT_EXTREME
;
161 b_extreme
= EDIT_BOTTOM_EXTREME
;
162 t_extreme
= EDIT_TOP_EXTREME
;
163 if (edit
->found_len
) {
164 b_extreme
= max (edit
->num_widget_lines
/ 4, b_extreme
);
165 t_extreme
= max (edit
->num_widget_lines
/ 4, t_extreme
);
167 if (b_extreme
+ t_extreme
+ 1 > edit
->num_widget_lines
) {
169 n
= b_extreme
+ t_extreme
;
170 b_extreme
= (b_extreme
* (edit
->num_widget_lines
- 1)) / n
;
171 t_extreme
= (t_extreme
* (edit
->num_widget_lines
- 1)) / n
;
173 if (l_extreme
+ r_extreme
+ 1 > edit
->num_widget_columns
) {
175 n
= l_extreme
+ t_extreme
;
176 l_extreme
= (l_extreme
* (edit
->num_widget_columns
- 1)) / n
;
177 r_extreme
= (r_extreme
* (edit
->num_widget_columns
- 1)) / n
;
179 p
= edit_get_col (edit
);
180 edit_update_curs_row (edit
);
181 outby
= p
+ edit
->start_col
- edit
->num_widget_columns
+ 1 + (r_extreme
+ edit
->found_len
);
183 edit_scroll_right (edit
, outby
);
184 outby
= l_extreme
- p
- edit
->start_col
;
186 edit_scroll_left (edit
, outby
);
188 outby
= p
- edit
->num_widget_lines
+ 1 + b_extreme
;
190 edit_scroll_downward (edit
, outby
);
191 outby
= t_extreme
- p
;
193 edit_scroll_upward (edit
, outby
);
194 edit_update_curs_row (edit
);
197 #define set_color(font) attrset (font)
199 #define edit_move(x,y) widget_move(edit, y, x);
201 /* Set colorpair by index, don't interpret S-Lang "emulated attributes" */
203 #define lowlevel_set_color(x) SLsmg_set_color(x & 0x7F)
205 #define lowlevel_set_color(x) attrset(MY_COLOR_PAIR(color))
209 print_to_widget (WEdit
*edit
, long row
, int start_col
, int start_col_real
,
210 long end_col
, unsigned int line
[])
214 int x
= start_col_real
+ EDIT_TEXT_HORIZONTAL_OFFSET
;
215 int x1
= start_col
+ EDIT_TEXT_HORIZONTAL_OFFSET
;
216 int y
= row
+ EDIT_TEXT_VERTICAL_OFFSET
;
217 int cols_to_skip
= abs (x
);
219 set_color (EDITOR_NORMAL_COLOR
);
221 hline (' ', end_col
+ 1 - EDIT_TEXT_HORIZONTAL_OFFSET
- x1
);
223 edit_move (x1
+ FONT_OFFSET_X
, y
+ FONT_OFFSET_Y
);
238 textchar
= *p
& 0xFF;
241 if (style
& MOD_ABNORMAL
) {
242 /* Non-printable - use black background */
246 if (style
& MOD_BOLD
) {
247 set_color (EDITOR_BOLD_COLOR
);
248 } else if (style
& MOD_MARKED
) {
249 set_color (EDITOR_MARKED_COLOR
);
251 lowlevel_set_color (color
);
259 /* b is a pointer to the beginning of the line */
261 edit_draw_this_line (WEdit
*edit
, long b
, long row
, long start_col
,
264 static unsigned int line
[MAX_LINE_LEN
];
265 unsigned int *p
= line
;
266 long m1
= 0, m2
= 0, q
, c1
, c2
;
267 int col
, start_col_real
;
270 int i
, book_mark
= -1;
273 if (!book_mark_query (edit
, edit
->start_line
+ row
, &book_mark
))
277 edit_get_syntax_color (edit
, b
- 1, &color
);
278 q
= edit_move_forward3 (edit
, b
, start_col
- edit
->start_col
, 0);
279 start_col_real
= (col
=
280 (int) edit_move_forward3 (edit
, b
, 0,
281 q
)) + edit
->start_col
;
282 c1
= min (edit
->column1
, edit
->column2
);
283 c2
= max (edit
->column1
, edit
->column2
);
285 if (col
+ 16 > -edit
->start_col
) {
286 eval_marks (edit
, &m1
, &m2
);
288 if (row
<= edit
->total_lines
- edit
->start_line
) {
289 while (col
<= end_col
- edit
->start_col
) {
291 if (q
== edit
->curs1
)
293 if (q
>= m1
&& q
< m2
) {
294 if (column_highlighting
) {
296 x
= edit_move_forward3 (edit
, b
, 0, q
);
297 if (x
>= c1
&& x
< c2
)
302 if (q
== edit
->bracket
)
304 if (q
>= edit
->found_start
305 && q
< edit
->found_start
+ edit
->found_len
)
307 c
= edit_get_byte (edit
, q
);
308 /* we don't use bg for mc - fg contains both */
309 if (book_mark
== -1) {
310 edit_get_syntax_color (edit
, q
, &color
);
313 *p
|= book_mark
<< 16;
318 col
= end_col
- edit
->start_col
+ 1; /* quit */
322 i
= TAB_SIZE
- ((int) col
% TAB_SIZE
);
324 c
= *(p
++) & ~MOD_CURSOR
;
330 c
= convert_to_display_c (c
);
332 /* Caret notation for control characters */
334 *(p
++) = '^' | MOD_ABNORMAL
;
335 *(p
++) = (c
+ 0x40) | MOD_ABNORMAL
;
340 *(p
++) = '^' | MOD_ABNORMAL
;
341 *(p
++) = '?' | MOD_ABNORMAL
;
346 if (is_printable (c
)) {
349 *(p
++) = '.' | MOD_ABNORMAL
;
357 start_col_real
= start_col
= 0;
361 print_to_widget (edit
, row
, start_col
, start_col_real
, end_col
, line
);
364 #define key_pending(x) (!is_idle())
366 static void edit_draw_this_char (WEdit
* edit
, long curs
, long row
)
368 int b
= edit_bol (edit
, curs
);
369 edit_draw_this_line (edit
, b
, row
, 0, edit
->num_widget_columns
- 1);
372 /* cursor must be in screen for other than REDRAW_PAGE passed in force */
374 render_edit_text (WEdit
* edit
, long start_row
, long start_column
, long end_row
,
377 long row
= 0, curs_row
;
378 static int prev_curs_row
= 0;
379 static long prev_curs
= 0;
381 int force
= edit
->force
;
385 * If the position of the page has not moved then we can draw the cursor
386 * character only. This will prevent line flicker when using arrow keys.
388 if ((!(force
& REDRAW_CHAR_ONLY
)) || (force
& REDRAW_PAGE
)) {
389 if (!(force
& REDRAW_IN_BOUNDS
)) { /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
391 end_row
= edit
->num_widget_lines
- 1;
393 end_column
= edit
->num_widget_columns
- 1;
395 if (force
& REDRAW_PAGE
) {
397 b
= edit_move_forward (edit
, edit
->start_display
, start_row
, 0);
398 while (row
<= end_row
) {
399 if (key_pending (edit
))
401 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
402 b
= edit_move_forward (edit
, b
, 1, 0);
406 curs_row
= edit
->curs_row
;
408 if (force
& REDRAW_BEFORE_CURSOR
) {
409 if (start_row
< curs_row
) {
410 long upto
= curs_row
- 1 <= end_row
? curs_row
- 1 : end_row
;
412 b
= edit
->start_display
;
413 while (row
<= upto
) {
414 if (key_pending (edit
))
416 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
417 b
= edit_move_forward (edit
, b
, 1, 0);
421 /* if (force & REDRAW_LINE) ---> default */
422 b
= edit_bol (edit
, edit
->curs1
);
423 if (curs_row
>= start_row
&& curs_row
<= end_row
) {
424 if (key_pending (edit
))
426 edit_draw_this_line (edit
, b
, curs_row
, start_column
, end_column
);
428 if (force
& REDRAW_AFTER_CURSOR
) {
429 if (end_row
> curs_row
) {
430 row
= curs_row
+ 1 < start_row
? start_row
: curs_row
+ 1;
431 b
= edit_move_forward (edit
, b
, 1, 0);
432 while (row
<= end_row
) {
433 if (key_pending (edit
))
435 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
436 b
= edit_move_forward (edit
, b
, 1, 0);
441 if (force
& REDRAW_LINE_ABOVE
&& curs_row
>= 1) {
443 b
= edit_move_backward (edit
, edit_bol (edit
, edit
->curs1
), 1);
444 if (row
>= start_row
&& row
<= end_row
) {
445 if (key_pending (edit
))
447 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
450 if (force
& REDRAW_LINE_BELOW
&& row
< edit
->num_widget_lines
- 1) {
452 b
= edit_bol (edit
, edit
->curs1
);
453 b
= edit_move_forward (edit
, b
, 1, 0);
454 if (row
>= start_row
&& row
<= end_row
) {
455 if (key_pending (edit
))
457 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
462 if (prev_curs_row
< edit
->curs_row
) { /* with the new text highlighting, we must draw from the top down */
463 edit_draw_this_char (edit
, prev_curs
, prev_curs_row
);
464 edit_draw_this_char (edit
, edit
->curs1
, edit
->curs_row
);
466 edit_draw_this_char (edit
, edit
->curs1
, edit
->curs_row
);
467 edit_draw_this_char (edit
, prev_curs
, prev_curs_row
);
473 prev_curs_row
= edit
->curs_row
;
474 prev_curs
= edit
->curs1
;
477 edit
->screen_modified
= 0;
482 edit_render (WEdit
* edit
, int page
, int row_start
, int col_start
, int row_end
, int col_end
)
484 if (page
) /* if it was an expose event, 'page' would be set */
485 edit
->force
|= REDRAW_PAGE
| REDRAW_IN_BOUNDS
;
487 if (edit
->force
& REDRAW_COMPLETELY
)
488 buttonbar_redraw (edit
->widget
.parent
);
489 render_edit_text (edit
, row_start
, col_start
, row_end
, col_end
);
491 * edit->force != 0 means a key was pending and the redraw
492 * was halted, so next time we must redraw everything in case stuff
493 * was left undrawn from a previous key press.
496 edit
->force
|= REDRAW_PAGE
;
499 void edit_render_keypress (WEdit
* edit
)
501 edit_render (edit
, 0, 0, 0, 0, 0);