Ticket #3716: various checkboxes not responding.
[midnight-commander.git] / src / editor / editdraw.c
blob899e3a830bd859f2d17c929a4fb25ac9789f524b
1 /*
2 Editor text drawing.
4 Copyright (C) 1996-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Paul Sheer, 1996, 1997
9 Andrew Borodin <aborodin@vmail.ru> 2012, 2013
10 Slava Zanko <slavazanko@gmail.com>, 2013
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file
29 * \brief Source: editor text drawing
30 * \author Paul Sheer
31 * \date 1996, 1997
34 #include <config.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <sys/stat.h>
45 #include "lib/global.h"
46 #include "lib/tty/tty.h" /* tty_printf() */
47 #include "lib/tty/key.h" /* is_idle() */
48 #include "lib/skin.h"
49 #include "lib/strutil.h" /* utf string functions */
50 #include "lib/util.h" /* is_printable() */
51 #include "lib/widget.h"
52 #ifdef HAVE_CHARSET
53 #include "lib/charsets.h"
54 #endif
56 #include "src/setup.h" /* edit_tab_spacing */
58 #include "edit-impl.h"
59 #include "editwidget.h"
61 /*** global variables ****************************************************************************/
63 /* Toggles statusbar draw style */
64 int simple_statusbar = 0;
66 int visible_tabs = 1, visible_tws = 1;
68 /*** file scope macro definitions ****************************************************************/
70 #define MAX_LINE_LEN 1024
72 /* Text styles */
73 #define MOD_ABNORMAL (1 << 8)
74 #define MOD_BOLD (1 << 9)
75 #define MOD_MARKED (1 << 10)
76 #define MOD_CURSOR (1 << 11)
77 #define MOD_WHITESPACE (1 << 12)
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
85 /*** file scope type declarations ****************************************************************/
87 typedef struct
89 unsigned int ch;
90 unsigned int style;
91 } line_s;
93 /*** file scope variables ************************************************************************/
95 /*** file scope functions ************************************************************************/
97 static inline void
98 printwstr (const char *s, int len)
100 if (len > 0)
101 tty_printf ("%-*.*s", len, len, s);
104 /* --------------------------------------------------------------------------------------------- */
106 static inline void
107 status_string (WEdit * edit, char *s, int w)
109 char byte_str[16];
112 * If we are at the end of file, print <EOF>,
113 * otherwise print the current character as is (if printable),
114 * as decimal and as hex.
116 if (edit->buffer.curs1 < edit->buffer.size)
118 #ifdef HAVE_CHARSET
119 if (edit->utf8)
121 unsigned int cur_utf = 0;
122 int char_length = 1;
124 cur_utf = edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &char_length);
125 if (char_length > 0)
127 g_snprintf (byte_str, sizeof (byte_str), "%04u 0x%03X",
128 (unsigned) cur_utf, (unsigned) cur_utf);
130 else
132 cur_utf = edit_buffer_get_current_byte (&edit->buffer);
133 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
134 (int) cur_utf, (unsigned) cur_utf);
137 else
138 #endif
140 unsigned char cur_byte = 0;
142 cur_byte = edit_buffer_get_current_byte (&edit->buffer);
143 g_snprintf (byte_str, sizeof (byte_str), "%4d 0x%03X",
144 (int) cur_byte, (unsigned) cur_byte);
147 else
149 strcpy (byte_str, "<EOF> ");
152 /* The field lengths just prevent the status line from shortening too much */
153 if (simple_statusbar)
154 g_snprintf (s, w,
155 "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s",
156 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
157 edit->modified ? 'M' : '-',
158 macro_index < 0 ? '-' : 'R',
159 edit->overwrite == 0 ? '-' : 'O',
160 edit->curs_col + edit->over_col,
161 edit->buffer.curs_line + 1,
162 edit->buffer.lines + 1, (long) edit->buffer.curs1, (long) edit->buffer.size,
163 byte_str,
164 #ifdef HAVE_CHARSET
165 mc_global.source_codepage >= 0 ? get_codepage_id (mc_global.source_codepage) :
166 #endif
167 "");
168 else
169 g_snprintf (s, w,
170 "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb) %s %s",
171 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
172 edit->modified ? 'M' : '-',
173 macro_index < 0 ? '-' : 'R',
174 edit->overwrite == 0 ? '-' : 'O',
175 edit->curs_col + edit->over_col,
176 edit->start_line + 1,
177 edit->curs_row,
178 edit->buffer.curs_line + 1,
179 edit->buffer.lines + 1, (long) edit->buffer.curs1, (long) edit->buffer.size,
180 byte_str,
181 #ifdef HAVE_CHARSET
182 mc_global.source_codepage >= 0 ? get_codepage_id (mc_global.source_codepage) :
183 #endif
184 "");
187 /* --------------------------------------------------------------------------------------------- */
189 * Draw the status line at the top of the screen for fullscreen editor window.
191 * @param edit editor object
192 * @param color color pair
195 static inline void
196 edit_status_fullscreen (WEdit * edit, int color)
198 Widget *h = WIDGET (WIDGET (edit)->owner);
199 const int w = h->cols;
200 const size_t status_size = w + 1;
201 char *const status = g_malloc (status_size);
202 int status_len;
203 const char *fname = "";
204 int fname_len;
205 const int gap = 3; /* between the filename and the status */
206 const int right_gap = 5; /* at the right end of the screen */
207 const int preferred_fname_len = 16;
209 status_string (edit, status, status_size);
210 status_len = (int) str_term_width1 (status);
212 if (edit->filename_vpath != NULL)
214 fname = vfs_path_get_last_path_str (edit->filename_vpath);
216 if (!option_state_full_filename)
217 fname = x_basename (fname);
220 fname_len = str_term_width1 (fname);
221 if (fname_len < preferred_fname_len)
222 fname_len = preferred_fname_len;
224 if (fname_len + gap + status_len + right_gap >= w)
226 if (preferred_fname_len + gap + status_len + right_gap >= w)
227 fname_len = preferred_fname_len;
228 else
229 fname_len = w - (gap + status_len + right_gap);
230 fname = str_trunc (fname, fname_len);
233 widget_move (h, 0, 0);
234 tty_setcolor (color);
235 printwstr (fname, fname_len + gap);
236 printwstr (status, w - (fname_len + gap));
238 if (simple_statusbar && w > EDITOR_MINIMUM_TERMINAL_WIDTH)
240 int percent;
242 percent = edit_buffer_calc_percent (&edit->buffer, edit->buffer.curs1);
243 widget_move (h, 0, w - 6 - 6);
244 tty_printf (" %3d%%", percent);
247 g_free (status);
250 /* --------------------------------------------------------------------------------------------- */
252 * Draw status line for editor window if window is not in fullscreen mode.
254 * @param edit editor object
257 static inline void
258 edit_status_window (WEdit * edit)
260 Widget *w = WIDGET (edit);
261 int y, x;
262 int cols = w->cols;
264 tty_setcolor (STATUSBAR_COLOR);
266 if (cols > 5)
268 const char *fname = N_("NoName");
270 if (edit->filename_vpath != NULL)
272 fname = vfs_path_get_last_path_str (edit->filename_vpath);
274 if (!option_state_full_filename)
275 fname = x_basename (fname);
277 #ifdef ENABLE_NLS
278 else
279 fname = _(fname);
280 #endif
282 edit_move (2, 0);
283 tty_printf ("[%s]", str_term_trim (fname, w->cols - 8 - 6));
286 tty_getyx (&y, &x);
287 x -= w->x;
288 x += 4;
289 if (x + 6 <= cols - 2 - 6)
291 edit_move (x, 0);
292 tty_printf ("[%c%c%c%c]",
293 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
294 edit->modified ? 'M' : '-',
295 macro_index < 0 ? '-' : 'R', edit->overwrite == 0 ? '-' : 'O');
298 if (cols > 30)
300 edit_move (2, w->lines - 1);
301 tty_printf ("%3ld %5ld/%ld %6ld/%ld",
302 edit->curs_col + edit->over_col,
303 edit->buffer.curs_line + 1, edit->buffer.lines + 1, (long) edit->buffer.curs1,
304 (long) edit->buffer.size);
308 * If we are at the end of file, print <EOF>,
309 * otherwise print the current character as is (if printable),
310 * as decimal and as hex.
312 if (cols > 46)
314 edit_move (32, w->lines - 1);
315 if (edit->buffer.curs1 >= edit->buffer.size)
316 tty_print_string ("[<EOF> ]");
317 #ifdef HAVE_CHARSET
318 else if (edit->utf8)
320 unsigned int cur_utf;
321 int char_length = 1;
323 cur_utf = edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &char_length);
324 if (char_length <= 0)
325 cur_utf = edit_buffer_get_current_byte (&edit->buffer);
326 tty_printf ("[%05u 0x%04X]", cur_utf, cur_utf);
328 #endif
329 else
331 unsigned char cur_byte;
333 cur_byte = edit_buffer_get_current_byte (&edit->buffer);
334 tty_printf ("[%05u 0x%04X]", (unsigned int) cur_byte, (unsigned int) cur_byte);
339 /* --------------------------------------------------------------------------------------------- */
341 * Draw a frame around edit area.
343 * @param edit editor object
344 * @param color color pair
345 * @param active TRUE if editor object is focused
348 static inline void
349 edit_draw_frame (const WEdit * edit, int color, gboolean active)
351 const Widget *w = CONST_WIDGET (edit);
353 /* draw a frame around edit area */
354 tty_setcolor (color);
355 /* draw double frame for active window if skin supports that */
356 tty_draw_box (w->y, w->x, w->lines, w->cols, !active);
357 /* draw a drag marker */
358 if (edit->drag_state == MCEDIT_DRAG_NONE)
360 tty_setcolor (EDITOR_FRAME_DRAG);
361 widget_move (w, w->lines - 1, w->cols - 1);
362 tty_print_alt_char (ACS_LRCORNER, TRUE);
366 /* --------------------------------------------------------------------------------------------- */
368 * Draw a window control buttons.
370 * @param edit editor object
371 * @param color color pair
374 static inline void
375 edit_draw_window_icons (const WEdit * edit, int color)
377 const Widget *w = CONST_WIDGET (edit);
378 char tmp[17];
380 tty_setcolor (color);
381 if (edit->fullscreen)
382 widget_move (w->owner, 0, WIDGET (w->owner)->cols - 6);
383 else
384 widget_move (w, 0, w->cols - 8);
385 g_snprintf (tmp, sizeof (tmp), "[%s][%s]", edit_window_state_char, edit_window_close_char);
386 tty_print_string (tmp);
389 /* --------------------------------------------------------------------------------------------- */
391 static inline void
392 print_to_widget (WEdit * edit, long row, int start_col, int start_col_real,
393 long end_col, line_s line[], char *status, int bookmarked)
395 Widget *w = WIDGET (edit);
397 line_s *p;
399 int x = start_col_real;
400 int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
401 int y = row + EDIT_TEXT_VERTICAL_OFFSET;
402 int cols_to_skip = abs (x);
403 int i;
404 int wrap_start;
405 int len;
407 if (!edit->fullscreen)
409 x1++;
410 y++;
413 tty_setcolor (EDITOR_NORMAL_COLOR);
414 if (bookmarked != 0)
415 tty_setcolor (bookmarked);
417 len = end_col + 1 - start_col;
418 wrap_start = option_word_wrap_line_length + edit->start_col;
420 if (len > 0 && w->y + y >= 0)
422 if (!show_right_margin || wrap_start > end_col)
423 tty_draw_hline (w->y + y, w->x + x1, ' ', len);
424 else if (wrap_start < 0)
426 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
427 tty_draw_hline (w->y + y, w->x + x1, ' ', len);
429 else
431 if (wrap_start > 0)
432 tty_draw_hline (w->y + y, w->x + x1, ' ', wrap_start);
434 len -= wrap_start;
435 if (len > 0)
437 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
438 tty_draw_hline (w->y + y, w->x + x1 + wrap_start, ' ', len);
443 if (option_line_state)
445 tty_setcolor (LINE_STATE_COLOR);
446 for (i = 0; i < LINE_STATE_WIDTH; i++)
448 edit_move (x1 + i - option_line_state_width, y);
449 if (status[i] == '\0')
450 status[i] = ' ';
451 tty_print_char (status[i]);
455 edit_move (x1, y);
456 i = 1;
457 for (p = line; p->ch != 0; p++)
459 int style;
460 unsigned int textchar;
461 int color;
463 if (cols_to_skip != 0)
465 cols_to_skip--;
466 continue;
469 style = p->style & 0xFF00;
470 textchar = p->ch;
471 color = p->style >> 16;
473 if (style & MOD_ABNORMAL)
475 /* Non-printable - use black background */
476 color = 0;
479 if (style & MOD_WHITESPACE)
481 if (style & MOD_MARKED)
483 textchar = ' ';
484 tty_setcolor (EDITOR_MARKED_COLOR);
486 else
487 tty_setcolor (EDITOR_WHITESPACE_COLOR);
489 else if (style & MOD_BOLD)
490 tty_setcolor (EDITOR_BOLD_COLOR);
491 else if (style & MOD_MARKED)
492 tty_setcolor (EDITOR_MARKED_COLOR);
493 else
494 tty_lowlevel_setcolor (color);
496 if (show_right_margin)
498 if (i > option_word_wrap_line_length + edit->start_col)
499 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
500 i++;
503 tty_print_anychar (textchar);
507 /* --------------------------------------------------------------------------------------------- */
508 /** b is a pointer to the beginning of the line */
510 static void
511 edit_draw_this_line (WEdit * edit, off_t b, long row, long start_col, long end_col)
513 Widget *w = WIDGET (edit);
515 line_s line[MAX_LINE_LEN];
516 line_s *p = line;
518 off_t m1 = 0, m2 = 0, q;
519 int col, start_col_real;
520 int color;
521 int abn_style;
522 int book_mark = 0;
523 char line_stat[LINE_STATE_WIDTH + 1] = "\0";
525 if (row > w->lines - 1 - EDIT_TEXT_VERTICAL_OFFSET - 2 * (edit->fullscreen ? 0 : 1))
526 return;
528 if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_COLOR))
529 book_mark = BOOK_MARK_COLOR;
530 else if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_FOUND_COLOR))
531 book_mark = BOOK_MARK_FOUND_COLOR;
533 if (book_mark)
534 abn_style = book_mark << 16;
535 else
536 abn_style = MOD_ABNORMAL;
538 end_col -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
539 if (!edit->fullscreen)
541 end_col--;
542 if (w->x + w->cols <= WIDGET (w->owner)->cols)
543 end_col--;
546 color = edit_get_syntax_color (edit, b - 1);
547 q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
548 start_col_real = (col = (int) edit_move_forward3 (edit, b, 0, q)) + edit->start_col;
550 if (option_line_state)
552 long cur_line;
554 cur_line = edit->start_line + row;
555 if (cur_line <= edit->buffer.lines)
557 g_snprintf (line_stat, sizeof (line_stat), "%7ld ", cur_line + 1);
559 else
561 memset (line_stat, ' ', LINE_STATE_WIDTH);
562 line_stat[LINE_STATE_WIDTH] = '\0';
564 if (book_mark_query_color (edit, cur_line, BOOK_MARK_COLOR))
566 g_snprintf (line_stat, 2, "*");
570 if (col + 16 > -edit->start_col)
572 eval_marks (edit, &m1, &m2);
574 if (row <= edit->buffer.lines - edit->start_line)
576 off_t tws = 0;
577 if (tty_use_colors () && visible_tws)
579 unsigned int c;
581 tws = edit_buffer_get_eol (&edit->buffer, b);
582 while (tws > b
583 && ((c = edit_buffer_get_byte (&edit->buffer, tws - 1)) == ' ' || c == '\t'))
584 tws--;
587 while (col <= end_col - edit->start_col)
589 int char_length = 1;
590 unsigned int c;
591 gboolean wide_width_char = FALSE;
592 gboolean control_char = FALSE;
594 p->ch = 0;
595 p->style = 0;
596 if (q == edit->buffer.curs1)
597 p->style |= MOD_CURSOR;
598 if (q >= m1 && q < m2)
600 if (edit->column_highlight)
602 long x;
603 long c1, c2;
605 x = (long) edit_move_forward3 (edit, b, 0, q);
606 c1 = MIN (edit->column1, edit->column2);
607 c2 = MAX (edit->column1, edit->column2);
608 if (x >= c1 && x < c2)
609 p->style |= MOD_MARKED;
611 else
612 p->style |= MOD_MARKED;
614 if (q == edit->bracket)
615 p->style |= MOD_BOLD;
616 if (q >= edit->found_start && q < (off_t) (edit->found_start + edit->found_len))
617 p->style |= MOD_BOLD;
619 #ifdef HAVE_CHARSET
620 if (edit->utf8)
621 c = edit_buffer_get_utf (&edit->buffer, q, &char_length);
622 else
623 #endif
624 c = edit_buffer_get_byte (&edit->buffer, q);
626 /* we don't use bg for mc - fg contains both */
627 if (book_mark)
629 p->style |= book_mark << 16;
631 else
633 color = edit_get_syntax_color (edit, q);
634 p->style |= color << 16;
636 switch (c)
638 case '\n':
639 col = end_col - edit->start_col + 1; /* quit */
640 break;
641 case '\t':
643 int tab_over;
644 int i;
646 i = TAB_SIZE - ((int) col % TAB_SIZE);
647 tab_over = (end_col - edit->start_col) - (col + i - 1);
648 if (tab_over < 0)
649 i += tab_over;
650 col += i;
651 if (tty_use_colors () &&
652 ((visible_tabs || (visible_tws && q >= tws)) && enable_show_tabs_tws))
654 if (p->style & MOD_MARKED)
655 c = p->style;
656 else if (book_mark)
657 c |= book_mark << 16;
658 else
659 c = p->style | MOD_WHITESPACE;
660 if (i > 2)
662 p->ch = '<';
663 p->style = c;
664 p++;
665 while (--i > 1)
667 p->ch = '-';
668 p->style = c;
669 p++;
671 p->ch = '>';
672 p->style = c;
673 p++;
675 else if (i > 1)
677 p->ch = '<';
678 p->style = c;
679 p++;
680 p->ch = '>';
681 p->style = c;
682 p++;
684 else
686 p->ch = '>';
687 p->style = c;
688 p++;
691 else if (tty_use_colors () && visible_tws && q >= tws
692 && enable_show_tabs_tws)
694 p->ch = '.';
695 p->style |= MOD_WHITESPACE;
696 c = p->style & ~MOD_CURSOR;
697 p++;
698 while (--i)
700 p->ch = ' ';
701 p->style = c;
702 p++;
705 else
707 p->ch |= ' ';
708 c = p->style & ~MOD_CURSOR;
709 p++;
710 while (--i)
712 p->ch = ' ';
713 p->style = c;
714 p++;
718 break;
719 case ' ':
720 if (tty_use_colors () && visible_tws && q >= tws && enable_show_tabs_tws)
722 p->ch = '.';
723 p->style |= MOD_WHITESPACE;
724 p++;
725 col++;
726 break;
728 /* fallthrough */
729 default:
730 #ifdef HAVE_CHARSET
731 if (mc_global.utf8_display)
733 if (!edit->utf8)
735 c = convert_from_8bit_to_utf_c ((unsigned char) c, edit->converter);
737 else
739 if (g_unichar_iswide (c))
741 wide_width_char = TRUE;
742 col++;
746 else if (edit->utf8)
747 c = convert_from_utf_to_current_c (c, edit->converter);
748 else
749 c = convert_to_display_c (c);
750 #endif
752 /* Caret notation for control characters */
753 if (c < 32)
755 p->ch = '^';
756 p->style = abn_style;
757 p++;
758 p->ch = c + 0x40;
759 p->style = abn_style;
760 p++;
761 col += 2;
762 control_char = TRUE;
763 break;
765 if (c == 127)
767 p->ch = '^';
768 p->style = abn_style;
769 p++;
770 p->ch = '?';
771 p->style = abn_style;
772 p++;
773 col += 2;
774 control_char = TRUE;
775 break;
777 #ifdef HAVE_CHARSET
778 if (edit->utf8)
780 if (g_unichar_isprint (c))
781 p->ch = c;
782 else
784 p->ch = '.';
785 p->style = abn_style;
787 p++;
789 else
790 #endif
792 if ((mc_global.utf8_display && g_unichar_isprint (c)) ||
793 (!mc_global.utf8_display && is_printable (c)))
795 p->ch = c;
796 p++;
798 else
800 p->ch = '.';
801 p->style = abn_style;
802 p++;
805 col++;
806 break;
807 } /* case */
809 q++;
810 if (char_length > 1)
811 q += char_length - 1;
813 if (col > (end_col - edit->start_col + 1))
815 if (wide_width_char)
817 p--;
818 break;
820 if (control_char)
822 p -= 2;
823 break;
829 else
831 start_col_real = start_col = 0;
834 p->ch = 0;
836 print_to_widget (edit, row, start_col, start_col_real, end_col, line, line_stat, book_mark);
839 /* --------------------------------------------------------------------------------------------- */
841 static inline void
842 edit_draw_this_char (WEdit * edit, off_t curs, long row, long start_column, long end_column)
844 off_t b = edit_buffer_get_bol (&edit->buffer, curs);
846 edit_draw_this_line (edit, b, row, start_column, end_column);
849 /* --------------------------------------------------------------------------------------------- */
850 /** cursor must be in screen for other than REDRAW_PAGE passed in force */
852 static inline void
853 render_edit_text (WEdit * edit, long start_row, long start_column, long end_row, long end_column)
855 static long prev_curs_row = 0;
856 static off_t prev_curs = 0;
858 Widget *w = WIDGET (edit);
859 Widget *wh = WIDGET (w->owner);
861 int force = edit->force;
862 int y1, x1, y2, x2;
864 int last_line;
865 int last_column;
867 /* draw only visible region */
869 last_line = wh->y + wh->lines - 1;
871 y1 = w->y;
872 if (y1 > last_line - 1 /* buttonbar */ )
873 return;
875 last_column = wh->x + wh->cols - 1;
877 x1 = w->x;
878 if (x1 > last_column)
879 return;
881 y2 = w->y + w->lines - 1;
882 if (y2 < wh->y + 1 /* menubar */ )
883 return;
885 x2 = w->x + w->cols - 1;
886 if (x2 < wh->x)
887 return;
889 if ((force & REDRAW_IN_BOUNDS) == 0)
891 /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
892 /* draw only visible region */
894 if (y2 <= last_line - 1 /* buttonbar */ )
895 end_row = w->lines - 1;
896 else if (y1 >= wh->y + 1 /* menubar */ )
897 end_row = wh->lines - 1 - y1 - 1;
898 else
899 end_row = start_row + wh->lines - 1 - 1;
901 if (x2 <= last_column)
902 end_column = w->cols - 1;
903 else if (x1 >= wh->x)
904 end_column = wh->cols - 1 - x1;
905 else
906 end_column = start_column + wh->cols - 1;
910 * If the position of the page has not moved then we can draw the cursor
911 * character only. This will prevent line flicker when using arrow keys.
913 if ((force & REDRAW_CHAR_ONLY) == 0 || (force & REDRAW_PAGE) != 0)
915 long row = 0;
916 long b;
918 if ((force & REDRAW_PAGE) != 0)
920 row = start_row;
921 b = edit_buffer_get_forward_offset (&edit->buffer, edit->start_display, start_row, 0);
922 while (row <= end_row)
924 if (key_pending (edit))
925 return;
926 edit_draw_this_line (edit, b, row, start_column, end_column);
927 b = edit_buffer_get_forward_offset (&edit->buffer, b, 1, 0);
928 row++;
931 else
933 long curs_row = edit->curs_row;
935 if ((force & REDRAW_BEFORE_CURSOR) != 0 && start_row < curs_row)
937 long upto = curs_row - 1 <= end_row ? curs_row - 1 : end_row;
939 row = start_row;
940 b = edit->start_display;
941 while (row <= upto)
943 if (key_pending (edit))
944 return;
945 edit_draw_this_line (edit, b, row, start_column, end_column);
946 b = edit_buffer_get_forward_offset (&edit->buffer, b, 1, 0);
950 /* if (force & REDRAW_LINE) ---> default */
951 b = edit_buffer_get_current_bol (&edit->buffer);
952 if (curs_row >= start_row && curs_row <= end_row)
954 if (key_pending (edit))
955 return;
956 edit_draw_this_line (edit, b, curs_row, start_column, end_column);
959 if ((force & REDRAW_AFTER_CURSOR) != 0 && end_row > curs_row)
961 row = curs_row + 1 < start_row ? start_row : curs_row + 1;
962 b = edit_buffer_get_forward_offset (&edit->buffer, b, 1, 0);
963 while (row <= end_row)
965 if (key_pending (edit))
966 return;
967 edit_draw_this_line (edit, b, row, start_column, end_column);
968 b = edit_buffer_get_forward_offset (&edit->buffer, b, 1, 0);
969 row++;
973 if ((force & REDRAW_LINE_ABOVE) != 0 && curs_row >= 1)
975 row = curs_row - 1;
976 b = edit_buffer_get_backward_offset (&edit->buffer,
977 edit_buffer_get_current_bol (&edit->buffer),
979 if (row >= start_row && row <= end_row)
981 if (key_pending (edit))
982 return;
983 edit_draw_this_line (edit, b, row, start_column, end_column);
987 if ((force & REDRAW_LINE_BELOW) != 0 && row < w->lines - 1)
989 row = curs_row + 1;
990 b = edit_buffer_get_current_bol (&edit->buffer);
991 b = edit_buffer_get_forward_offset (&edit->buffer, b, 1, 0);
992 if (row >= start_row && row <= end_row)
994 if (key_pending (edit))
995 return;
996 edit_draw_this_line (edit, b, row, start_column, end_column);
1001 else if (prev_curs_row < edit->curs_row)
1003 /* with the new text highlighting, we must draw from the top down */
1004 edit_draw_this_char (edit, prev_curs, prev_curs_row, start_column, end_column);
1005 edit_draw_this_char (edit, edit->buffer.curs1, edit->curs_row, start_column, end_column);
1007 else
1009 edit_draw_this_char (edit, edit->buffer.curs1, edit->curs_row, start_column, end_column);
1010 edit_draw_this_char (edit, prev_curs, prev_curs_row, start_column, end_column);
1013 edit->force = 0;
1015 prev_curs_row = edit->curs_row;
1016 prev_curs = edit->buffer.curs1;
1019 /* --------------------------------------------------------------------------------------------- */
1021 static inline void
1022 edit_render (WEdit * edit, int page, int row_start, int col_start, int row_end, int col_end)
1024 if (page) /* if it was an expose event, 'page' would be set */
1025 edit->force |= REDRAW_PAGE | REDRAW_IN_BOUNDS;
1027 render_edit_text (edit, row_start, col_start, row_end, col_end);
1030 * edit->force != 0 means a key was pending and the redraw
1031 * was halted, so next time we must redraw everything in case stuff
1032 * was left undrawn from a previous key press.
1034 if (edit->force)
1035 edit->force |= REDRAW_PAGE;
1038 /* --------------------------------------------------------------------------------------------- */
1039 /*** public functions ****************************************************************************/
1040 /* --------------------------------------------------------------------------------------------- */
1042 void
1043 edit_status (WEdit * edit, gboolean active)
1045 int color;
1047 if (edit->fullscreen)
1049 color = STATUSBAR_COLOR;
1050 edit_status_fullscreen (edit, color);
1052 else
1054 color = edit->drag_state != MCEDIT_DRAG_NONE ? EDITOR_FRAME_DRAG : active ?
1055 EDITOR_FRAME_ACTIVE : EDITOR_FRAME;
1056 edit_draw_frame (edit, color, active);
1057 edit_status_window (edit);
1060 edit_draw_window_icons (edit, color);
1063 /* --------------------------------------------------------------------------------------------- */
1065 /** this scrolls the text so that cursor is on the screen */
1066 void
1067 edit_scroll_screen_over_cursor (WEdit * edit)
1069 Widget *w = WIDGET (edit);
1071 long p;
1072 long outby;
1073 int b_extreme, t_extreme, l_extreme, r_extreme;
1075 if (w->lines <= 0 || w->cols <= 0)
1076 return;
1078 w->lines -= EDIT_TEXT_VERTICAL_OFFSET;
1079 w->cols -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
1081 if (!edit->fullscreen)
1083 w->x++;
1084 w->cols -= 2;
1085 w->y++;
1086 w->lines -= 2;
1089 r_extreme = EDIT_RIGHT_EXTREME;
1090 l_extreme = EDIT_LEFT_EXTREME;
1091 b_extreme = EDIT_BOTTOM_EXTREME;
1092 t_extreme = EDIT_TOP_EXTREME;
1093 if (edit->found_len != 0)
1095 b_extreme = MAX (w->lines / 4, b_extreme);
1096 t_extreme = MAX (w->lines / 4, t_extreme);
1098 if (b_extreme + t_extreme + 1 > w->lines)
1100 int n;
1102 n = b_extreme + t_extreme;
1103 if (n == 0)
1104 n = 1;
1105 b_extreme = (b_extreme * (w->lines - 1)) / n;
1106 t_extreme = (t_extreme * (w->lines - 1)) / n;
1108 if (l_extreme + r_extreme + 1 > w->cols)
1110 int n;
1112 n = l_extreme + t_extreme;
1113 if (n == 0)
1114 n = 1;
1115 l_extreme = (l_extreme * (w->cols - 1)) / n;
1116 r_extreme = (r_extreme * (w->cols - 1)) / n;
1118 p = edit_get_col (edit) + edit->over_col;
1119 edit_update_curs_row (edit);
1120 outby = p + edit->start_col - w->cols + 1 + (r_extreme + edit->found_len);
1121 if (outby > 0)
1122 edit_scroll_right (edit, outby);
1123 outby = l_extreme - p - edit->start_col;
1124 if (outby > 0)
1125 edit_scroll_left (edit, outby);
1126 p = edit->curs_row;
1127 outby = p - w->lines + 1 + b_extreme;
1128 if (outby > 0)
1129 edit_scroll_downward (edit, outby);
1130 outby = t_extreme - p;
1131 if (outby > 0)
1132 edit_scroll_upward (edit, outby);
1133 edit_update_curs_row (edit);
1135 w->lines += EDIT_TEXT_VERTICAL_OFFSET;
1136 w->cols += EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
1137 if (!edit->fullscreen)
1139 w->x--;
1140 w->cols += 2;
1141 w->y--;
1142 w->lines += 2;
1146 /* --------------------------------------------------------------------------------------------- */
1148 void
1149 edit_render_keypress (WEdit * edit)
1151 edit_render (edit, 0, 0, 0, 0, 0);
1154 /* --------------------------------------------------------------------------------------------- */