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., 59 Temple Place, Suite 330, Boston, MA
25 #include "edit-widget.h"
27 #define MAX_LINE_LEN 1024
29 #include "../src/color.h" /* EDITOR_NORMAL_COLOR */
30 #include "../src/tty.h" /* attrset() */
31 #include "../src/widget.h" /* redraw_labels() */
32 #include "../src/key.h" /* is_idle() */
33 #include "../src/charsets.h"
36 #define MOD_ABNORMAL (1 << 8)
37 #define MOD_BOLD (1 << 9)
38 #define MOD_MARKED (1 << 10)
39 #define MOD_CURSOR (1 << 11)
41 #define FONT_OFFSET_X 0
42 #define FONT_OFFSET_Y 0
44 #define FONT_PIX_PER_LINE 1
45 #define FONT_MEAN_WIDTH 1
48 static void status_string (WEdit
* edit
, char *s
, int w
, int fill
)
53 * If we are at the end of file, print <EOF>,
54 * otherwise print the current character as is (if printable),
55 * as decimal and as hex.
57 if (edit
->curs1
< edit
->last_byte
) {
58 unsigned char cur_byte
= edit_get_byte (edit
, edit
->curs1
);
59 g_snprintf (byte_str
, sizeof(byte_str
), "%c %3d 0x%02X",
60 is_printable(cur_byte
) ? cur_byte
: '.',
64 strcpy(byte_str
, "<EOF>");
67 /* The field lengths just prevent the status line from shortening too much */
69 "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb)= %s",
70 edit
->mark1
!= edit
->mark2
? ( column_highlighting
? 'C' : 'B') : '-',
71 edit
->modified
? 'M' : '-',
72 edit
->macro_i
< 0 ? '-' : 'R',
73 edit
->overwrite
== 0 ? '-' : 'O',
79 edit
->total_lines
+ 1,
86 /* how to get as much onto the status line as is numerically possible :) */
88 edit_status (WEdit
*edit
)
92 w
= edit
->widget
.cols
;
93 s
= g_malloc (w
+ 16);
97 attrset (SELECTED_COLOR
);
99 widget_move (edit
, 0, 0);
100 if (edit
->filename
) {
101 i
= w
> 24 ? 18 : w
- 6;
103 strcpy (s
, name_trunc (edit
->filename
, i
));
108 if (t
> 1) /* g_snprintf() must write at least '\000' */
109 status_string (edit
, s
+ 20, t
+ 1 /* for '\000' */ , ' ');
113 printw ("%-*s", w
, s
);
114 attrset (EDITOR_NORMAL_COLOR
);
118 /* this scrolls the text so that cursor is on the screen */
119 void edit_scroll_screen_over_cursor (WEdit
* edit
)
123 int b_extreme
, t_extreme
, l_extreme
, r_extreme
;
125 if (edit
->num_widget_lines
<= 0 || edit
->num_widget_columns
<= 0)
128 r_extreme
= EDIT_RIGHT_EXTREME
;
129 l_extreme
= EDIT_LEFT_EXTREME
;
130 b_extreme
= EDIT_BOTTOM_EXTREME
;
131 t_extreme
= EDIT_TOP_EXTREME
;
132 if (edit
->found_len
) {
133 b_extreme
= max (edit
->num_widget_lines
/ 4, b_extreme
);
134 t_extreme
= max (edit
->num_widget_lines
/ 4, t_extreme
);
136 if (b_extreme
+ t_extreme
+ 1 > edit
->num_widget_lines
) {
138 n
= b_extreme
+ t_extreme
;
139 b_extreme
= (b_extreme
* (edit
->num_widget_lines
- 1)) / n
;
140 t_extreme
= (t_extreme
* (edit
->num_widget_lines
- 1)) / n
;
142 if (l_extreme
+ r_extreme
+ 1 > edit
->num_widget_columns
) {
144 n
= l_extreme
+ t_extreme
;
145 l_extreme
= (l_extreme
* (edit
->num_widget_columns
- 1)) / n
;
146 r_extreme
= (r_extreme
* (edit
->num_widget_columns
- 1)) / n
;
148 p
= edit_get_col (edit
);
149 edit_update_curs_row (edit
);
150 outby
= p
+ edit
->start_col
- edit
->num_widget_columns
+ 1 + (r_extreme
+ edit
->found_len
);
152 edit_scroll_right (edit
, outby
);
153 outby
= l_extreme
- p
- edit
->start_col
;
155 edit_scroll_left (edit
, outby
);
157 outby
= p
- edit
->num_widget_lines
+ 1 + b_extreme
;
159 edit_scroll_downward (edit
, outby
);
160 outby
= t_extreme
- p
;
162 edit_scroll_upward (edit
, outby
);
163 edit_update_curs_row (edit
);
166 #define set_color(font) attrset (font)
168 #define edit_move(x,y) widget_move(edit, y, x);
170 /* Set colorpair by index, don't interpret S-Lang "emulated attributes" */
172 #define lowlevel_set_color(x) SLsmg_set_color(x & 0x7F)
174 #define lowlevel_set_color(x) attrset(MY_COLOR_PAIR(color))
178 print_to_widget (WEdit
*edit
, long row
, int start_col
, int start_col_real
,
179 long end_col
, unsigned int line
[])
183 int x
= start_col_real
+ EDIT_TEXT_HORIZONTAL_OFFSET
;
184 int x1
= start_col
+ EDIT_TEXT_HORIZONTAL_OFFSET
;
185 int y
= row
+ EDIT_TEXT_VERTICAL_OFFSET
;
187 set_color (EDITOR_NORMAL_COLOR
);
189 hline (' ', end_col
+ 1 - EDIT_TEXT_HORIZONTAL_OFFSET
- x1
);
191 edit_move (x
+ FONT_OFFSET_X
, y
+ FONT_OFFSET_Y
);
195 int style
= *p
& 0xFF00;
196 int textchar
= *p
& 0xFF;
197 int color
= *p
>> 16;
199 if (style
& MOD_ABNORMAL
) {
200 /* Non-printable - use black background */
204 if (style
& MOD_BOLD
) {
205 set_color (EDITOR_BOLD_COLOR
);
206 } else if (style
& MOD_MARKED
) {
207 set_color (EDITOR_MARKED_COLOR
);
209 lowlevel_set_color (color
);
217 /* b is a pointer to the beginning of the line */
219 edit_draw_this_line (WEdit
*edit
, long b
, long row
, long start_col
,
222 static unsigned int line
[MAX_LINE_LEN
];
223 unsigned int *p
= line
;
224 long m1
= 0, m2
= 0, q
, c1
, c2
;
225 int col
, start_col_real
;
228 int i
, book_mark
= -1;
231 if (!book_mark_query (edit
, edit
->start_line
+ row
, &book_mark
))
235 edit_get_syntax_color (edit
, b
- 1, &color
);
236 q
= edit_move_forward3 (edit
, b
, start_col
- edit
->start_col
, 0);
237 start_col_real
= (col
=
238 (int) edit_move_forward3 (edit
, b
, 0,
239 q
)) + edit
->start_col
;
240 c1
= min (edit
->column1
, edit
->column2
);
241 c2
= max (edit
->column1
, edit
->column2
);
243 if (col
+ 16 > -edit
->start_col
) {
244 eval_marks (edit
, &m1
, &m2
);
246 if (row
<= edit
->total_lines
- edit
->start_line
) {
247 while (col
<= end_col
- edit
->start_col
) {
249 if (q
== edit
->curs1
)
251 if (q
>= m1
&& q
< m2
) {
252 if (column_highlighting
) {
254 x
= edit_move_forward3 (edit
, b
, 0, q
);
255 if (x
>= c1
&& x
< c2
)
260 if (q
== edit
->bracket
)
262 if (q
>= edit
->found_start
263 && q
< edit
->found_start
+ edit
->found_len
)
265 c
= edit_get_byte (edit
, q
);
266 /* we don't use bg for mc - fg contains both */
267 if (book_mark
== -1) {
268 edit_get_syntax_color (edit
, q
, &color
);
271 *p
|= book_mark
<< 16;
276 col
= end_col
- edit
->start_col
+ 1; /* quit */
280 i
= TAB_SIZE
- ((int) col
% TAB_SIZE
);
282 c
= *(p
++) & ~MOD_CURSOR
;
288 c
= convert_to_display_c (c
);
290 /* Caret notation for control characters */
292 *(p
++) = '^' | MOD_ABNORMAL
;
293 *(p
++) = (c
+ 0x40) | MOD_ABNORMAL
;
298 *(p
++) = '^' | MOD_ABNORMAL
;
299 *(p
++) = '?' | MOD_ABNORMAL
;
304 if (is_printable (c
)) {
307 *(p
++) = '.' | MOD_ABNORMAL
;
315 start_col_real
= start_col
= 0;
319 print_to_widget (edit
, row
, start_col
, start_col_real
, end_col
, line
);
322 #define key_pending(x) (!is_idle())
324 static void edit_draw_this_char (WEdit
* edit
, long curs
, long row
)
326 int b
= edit_bol (edit
, curs
);
327 edit_draw_this_line (edit
, b
, row
, 0, edit
->num_widget_columns
- 1);
330 /* cursor must be in screen for other than REDRAW_PAGE passed in force */
332 render_edit_text (WEdit
* edit
, long start_row
, long start_column
, long end_row
,
335 long row
= 0, curs_row
;
336 static int prev_curs_row
= 0;
337 static int prev_start_col
= 0;
338 static long prev_curs
= 0;
339 static long prev_start
= -1;
341 int force
= edit
->force
;
345 * If the position of the page has not moved then we can draw the cursor
346 * character only. This will prevent line flicker when using arrow keys.
348 if ((!(force
& REDRAW_CHAR_ONLY
)) || (force
& REDRAW_PAGE
)) {
349 if (!(force
& REDRAW_IN_BOUNDS
)) { /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
351 end_row
= edit
->num_widget_lines
- 1;
353 end_column
= edit
->num_widget_columns
- 1;
355 if (force
& REDRAW_PAGE
) {
357 b
= edit_move_forward (edit
, edit
->start_display
, start_row
, 0);
358 while (row
<= end_row
) {
359 if (key_pending (edit
))
361 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
362 b
= edit_move_forward (edit
, b
, 1, 0);
366 curs_row
= edit
->curs_row
;
368 if (force
& REDRAW_BEFORE_CURSOR
) {
369 if (start_row
< curs_row
) {
370 long upto
= curs_row
- 1 <= end_row
? curs_row
- 1 : end_row
;
372 b
= edit
->start_display
;
373 while (row
<= upto
) {
374 if (key_pending (edit
))
376 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
377 b
= edit_move_forward (edit
, b
, 1, 0);
381 /* if (force & REDRAW_LINE) ---> default */
382 b
= edit_bol (edit
, edit
->curs1
);
383 if (curs_row
>= start_row
&& curs_row
<= end_row
) {
384 if (key_pending (edit
))
386 edit_draw_this_line (edit
, b
, curs_row
, start_column
, end_column
);
388 if (force
& REDRAW_AFTER_CURSOR
) {
389 if (end_row
> curs_row
) {
390 row
= curs_row
+ 1 < start_row
? start_row
: curs_row
+ 1;
391 b
= edit_move_forward (edit
, b
, 1, 0);
392 while (row
<= end_row
) {
393 if (key_pending (edit
))
395 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
396 b
= edit_move_forward (edit
, b
, 1, 0);
401 if (force
& REDRAW_LINE_ABOVE
&& curs_row
>= 1) {
403 b
= edit_move_backward (edit
, edit_bol (edit
, edit
->curs1
), 1);
404 if (row
>= start_row
&& row
<= end_row
) {
405 if (key_pending (edit
))
407 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
410 if (force
& REDRAW_LINE_BELOW
&& row
< edit
->num_widget_lines
- 1) {
412 b
= edit_bol (edit
, edit
->curs1
);
413 b
= edit_move_forward (edit
, b
, 1, 0);
414 if (row
>= start_row
&& row
<= end_row
) {
415 if (key_pending (edit
))
417 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
422 if (prev_curs_row
< edit
->curs_row
) { /* with the new text highlighting, we must draw from the top down */
423 edit_draw_this_char (edit
, prev_curs
, prev_curs_row
);
424 edit_draw_this_char (edit
, edit
->curs1
, edit
->curs_row
);
426 edit_draw_this_char (edit
, edit
->curs1
, edit
->curs_row
);
427 edit_draw_this_char (edit
, prev_curs
, prev_curs_row
);
433 prev_curs_row
= edit
->curs_row
;
434 prev_curs
= edit
->curs1
;
435 prev_start_col
= edit
->start_col
;
437 edit
->screen_modified
= 0;
438 prev_start
= edit
->start_line
;
443 edit_render (WEdit
* edit
, int page
, int row_start
, int col_start
, int row_end
, int col_end
)
445 if (page
) /* if it was an expose event, 'page' would be set */
446 edit
->force
|= REDRAW_PAGE
| REDRAW_IN_BOUNDS
;
448 if (edit
->force
& REDRAW_COMPLETELY
)
449 redraw_labels (edit
->widget
.parent
);
450 render_edit_text (edit
, row_start
, col_start
, row_end
, col_end
);
452 * edit->force != 0 means a key was pending and the redraw
453 * was halted, so next time we must redraw everything in case stuff
454 * was left undrawn from a previous key press.
457 edit
->force
|= REDRAW_PAGE
;
460 void edit_render_keypress (WEdit
* edit
)
462 edit_render (edit
, 0, 0, 0, 0, 0);