Ticket #2919: widget system improvements and unifications.
[midnight-commander.git] / src / editor / editdraw.c
blob7f55ab789b7aefa32b7ed0e31862319d78f05be6
1 /*
2 Editor text drawing.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2011, 2012
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer, 1996, 1997
10 Andrew Borodin <aborodin@vmail.ru> 2012
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 struct line_s
89 unsigned int ch;
90 unsigned int style;
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->curs1 < edit->last_byte)
118 #ifdef HAVE_CHARSET
119 if (edit->utf8)
121 unsigned int cur_utf = 0;
122 int cw = 1;
124 cur_utf = edit_get_utf (edit, edit->curs1, &cw);
125 if (cw > 0)
127 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
128 (unsigned) cur_utf, (unsigned) cur_utf);
130 else
132 cur_utf = edit_get_byte (edit, edit->curs1);
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_get_byte (edit, edit->curs1);
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->curs_line + 1,
162 edit->total_lines + 1, (long) edit->curs1, (long) edit->last_byte, byte_str,
163 #ifdef HAVE_CHARSET
164 mc_global.source_codepage >= 0 ? get_codepage_id (mc_global.source_codepage) :
165 #endif
166 "");
167 else
168 g_snprintf (s, w,
169 "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb) %s %s",
170 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
171 edit->modified ? 'M' : '-',
172 macro_index < 0 ? '-' : 'R',
173 edit->overwrite == 0 ? '-' : 'O',
174 edit->curs_col + edit->over_col,
175 edit->start_line + 1,
176 edit->curs_row,
177 edit->curs_line + 1,
178 edit->total_lines + 1, (long) edit->curs1, (long) edit->last_byte, byte_str,
179 #ifdef HAVE_CHARSET
180 mc_global.source_codepage >= 0 ? get_codepage_id (mc_global.source_codepage) :
181 #endif
182 "");
185 /* --------------------------------------------------------------------------------------------- */
187 * Draw the status line at the top of the screen for fullscreen editor window.
189 * @param edit editor object
190 * @param color color pair
193 static inline void
194 edit_status_fullscreen (WEdit * edit, int color)
196 Widget *h = WIDGET (WIDGET (edit)->owner);
197 const int w = h->cols;
198 const size_t status_size = w + 1;
199 char *const status = g_malloc (status_size);
200 int status_len;
201 const char *fname = "";
202 int fname_len;
203 const int gap = 3; /* between the filename and the status */
204 const int right_gap = 5; /* at the right end of the screen */
205 const int preferred_fname_len = 16;
207 status_string (edit, status, status_size);
208 status_len = (int) str_term_width1 (status);
210 if (edit->filename_vpath != NULL)
211 fname = x_basename (vfs_path_get_last_path_str (edit->filename_vpath));
213 fname_len = str_term_width1 (fname);
214 if (fname_len < preferred_fname_len)
215 fname_len = preferred_fname_len;
217 if (fname_len + gap + status_len + right_gap >= w)
219 if (preferred_fname_len + gap + status_len + right_gap >= w)
220 fname_len = preferred_fname_len;
221 else
222 fname_len = w - (gap + status_len + right_gap);
223 fname = str_trunc (fname, fname_len);
226 widget_move (h, 0, 0);
227 tty_setcolor (color);
228 printwstr (fname, fname_len + gap);
229 printwstr (status, w - (fname_len + gap));
231 if (simple_statusbar && w > EDITOR_MINIMUM_TERMINAL_WIDTH)
233 size_t percent = 100;
235 if (edit->total_lines + 1 != 0)
236 percent = (edit->curs_line + 1) * 100 / (edit->total_lines + 1);
237 widget_move (h, 0, w - 6 - 6);
238 tty_printf (" %3d%%", percent);
241 g_free (status);
244 /* --------------------------------------------------------------------------------------------- */
246 * Draw status line for editor window if window is not in fullscreen mode.
248 * @param edit editor object
251 static inline void
252 edit_status_window (WEdit * edit)
254 Widget *w = WIDGET (edit);
255 int y, x;
256 int cols = w->cols;
258 tty_setcolor (STATUSBAR_COLOR);
260 if (cols > 5)
262 const char *fname = N_("NoName");
263 char *full_fname = NULL;
265 if (edit->filename_vpath != NULL)
267 full_fname = vfs_path_to_str (edit->filename_vpath);
268 fname = x_basename (full_fname);
270 #ifdef ENABLE_NLS
271 else
272 fname = _(fname);
273 #endif
275 edit_move (2, 0);
276 tty_printf ("[%s]", str_term_trim (fname, w->cols - 8 - 6));
277 g_free (full_fname);
280 tty_getyx (&y, &x);
281 x -= w->x;
282 x += 4;
283 if (x + 6 <= cols - 2 - 6)
285 edit_move (x, 0);
286 tty_printf ("[%c%c%c%c]",
287 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
288 edit->modified ? 'M' : '-',
289 macro_index < 0 ? '-' : 'R', edit->overwrite == 0 ? '-' : 'O');
292 if (cols > 30)
294 edit_move (2, w->lines - 1);
295 tty_printf ("%3ld %5ld/%ld %6ld/%ld",
296 edit->curs_col + edit->over_col,
297 edit->curs_line + 1, edit->total_lines + 1, edit->curs1, edit->last_byte);
301 * If we are at the end of file, print <EOF>,
302 * otherwise print the current character as is (if printable),
303 * as decimal and as hex.
305 if (cols > 46)
307 edit_move (32, w->lines - 1);
308 if (edit->curs1 >= edit->last_byte)
309 tty_print_string ("[<EOF> ]");
310 #ifdef HAVE_CHARSET
311 else if (edit->utf8)
313 unsigned int cur_utf;
314 int cw = 1;
316 cur_utf = edit_get_utf (edit, edit->curs1, &cw);
317 if (cw <= 0)
318 cur_utf = edit_get_byte (edit, edit->curs1);
319 tty_printf ("[%05d 0x%04X]", cur_utf, cur_utf);
321 #endif
322 else
324 unsigned char cur_byte;
326 cur_byte = edit_get_byte (edit, edit->curs1);
327 tty_printf ("[%05d 0x%04X]", (unsigned int) cur_byte, (unsigned int) cur_byte);
332 /* --------------------------------------------------------------------------------------------- */
334 * Draw a frame around edit area.
336 * @param edit editor object
337 * @param color color pair
338 * @param active TRUE if editor object is focused
341 static inline void
342 edit_draw_frame (const WEdit * edit, int color, gboolean active)
344 const Widget *w = (const Widget *) edit;
346 /* draw a frame around edit area */
347 tty_setcolor (color);
348 /* draw double frame for active window if skin supports that */
349 tty_draw_box (w->y, w->x, w->lines, w->cols, !active);
350 /* draw a drag marker */
351 if (edit->drag_state == MCEDIT_DRAG_NORMAL)
353 tty_setcolor (EDITOR_FRAME_DRAG);
354 widget_move (w, w->lines - 1, w->cols - 1);
355 tty_print_alt_char (ACS_LRCORNER, TRUE);
359 /* --------------------------------------------------------------------------------------------- */
361 * Draw a window control buttons.
363 * @param edit editor object
364 * @param color color pair
367 static inline void
368 edit_draw_window_icons (const WEdit * edit, int color)
370 const Widget *w = WIDGET (edit);
371 char tmp[17];
373 tty_setcolor (color);
374 if (edit->fullscreen)
375 widget_move (w->owner, 0, WIDGET (w->owner)->cols - 6);
376 else
377 widget_move (w, 0, w->cols - 8);
378 g_snprintf (tmp, sizeof (tmp), "[%s][%s]", edit_window_state_char, edit_window_close_char);
379 tty_print_string (tmp);
382 /* --------------------------------------------------------------------------------------------- */
384 static inline void
385 print_to_widget (WEdit * edit, long row, int start_col, int start_col_real,
386 long end_col, struct line_s line[], char *status, int bookmarked)
388 struct line_s *p;
390 int x = start_col_real;
391 int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
392 int y = row + EDIT_TEXT_VERTICAL_OFFSET;
393 int cols_to_skip = abs (x);
394 int i;
395 int wrap_start;
396 int len;
398 if (!edit->fullscreen)
400 x1++;
401 y++;
404 tty_setcolor (EDITOR_NORMAL_COLOR);
405 if (bookmarked != 0)
406 tty_setcolor (bookmarked);
408 len = end_col + 1 - start_col;
409 wrap_start = option_word_wrap_line_length + edit->start_col;
411 if (len > 0 && edit->widget.y + y >= 0)
413 if (!show_right_margin || wrap_start > end_col)
414 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', len);
415 else if (wrap_start < 0)
417 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
418 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', len);
420 else
422 if (wrap_start > 0)
423 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', wrap_start);
425 len -= wrap_start;
426 if (len > 0)
428 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
429 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1 + wrap_start, ' ', len);
434 if (option_line_state)
436 tty_setcolor (LINE_STATE_COLOR);
437 for (i = 0; i < LINE_STATE_WIDTH; i++)
439 edit_move (x1 + i - option_line_state_width, y);
440 if (status[i] == '\0')
441 status[i] = ' ';
442 tty_print_char (status[i]);
446 edit_move (x1, y);
447 i = 1;
448 for (p = line; p->ch != 0; p++)
450 int style;
451 unsigned int textchar;
452 int color;
454 if (cols_to_skip != 0)
456 cols_to_skip--;
457 continue;
460 style = p->style & 0xFF00;
461 textchar = p->ch;
462 color = p->style >> 16;
464 if (style & MOD_ABNORMAL)
466 /* Non-printable - use black background */
467 color = 0;
470 if (style & MOD_WHITESPACE)
472 if (style & MOD_MARKED)
474 textchar = ' ';
475 tty_setcolor (EDITOR_MARKED_COLOR);
477 else
478 tty_setcolor (EDITOR_WHITESPACE_COLOR);
480 else if (style & MOD_BOLD)
481 tty_setcolor (EDITOR_BOLD_COLOR);
482 else if (style & MOD_MARKED)
483 tty_setcolor (EDITOR_MARKED_COLOR);
484 else
485 tty_lowlevel_setcolor (color);
487 if (show_right_margin)
489 if (i > option_word_wrap_line_length + edit->start_col)
490 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
491 i++;
494 tty_print_anychar (textchar);
498 /* --------------------------------------------------------------------------------------------- */
499 /** b is a pointer to the beginning of the line */
501 static void
502 edit_draw_this_line (WEdit * edit, off_t b, long row, long start_col, long end_col)
504 Widget *w = WIDGET (edit);
506 struct line_s line[MAX_LINE_LEN];
507 struct line_s *p = line;
509 off_t m1 = 0, m2 = 0, q;
510 long c1, c2;
511 int col, start_col_real;
512 unsigned int c;
513 int color;
514 int abn_style;
515 int i;
516 unsigned int cur_line = 0;
517 int book_mark = 0;
518 char line_stat[LINE_STATE_WIDTH + 1] = "\0";
520 if (row > w->lines - 1 - EDIT_TEXT_VERTICAL_OFFSET - 2 * (edit->fullscreen ? 0 : 1))
521 return;
523 if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_COLOR))
524 book_mark = BOOK_MARK_COLOR;
525 else if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_FOUND_COLOR))
526 book_mark = BOOK_MARK_FOUND_COLOR;
528 if (book_mark)
529 abn_style = book_mark << 16;
530 else
531 abn_style = MOD_ABNORMAL;
533 end_col -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
534 if (!edit->fullscreen)
536 end_col--;
537 if (w->x + w->cols <= WIDGET (w->owner)->cols)
538 end_col--;
541 color = edit_get_syntax_color (edit, b - 1);
542 q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
543 start_col_real = (col = (int) edit_move_forward3 (edit, b, 0, q)) + edit->start_col;
545 if (option_line_state)
547 cur_line = edit->start_line + row;
548 if (cur_line <= (unsigned int) edit->total_lines)
550 g_snprintf (line_stat, LINE_STATE_WIDTH + 1, "%7i ", cur_line + 1);
552 else
554 memset (line_stat, ' ', LINE_STATE_WIDTH);
555 line_stat[LINE_STATE_WIDTH] = '\0';
557 if (book_mark_query_color (edit, cur_line, BOOK_MARK_COLOR))
559 g_snprintf (line_stat, 2, "*");
563 if (col + 16 > -edit->start_col)
565 eval_marks (edit, &m1, &m2);
567 if (row <= edit->total_lines - edit->start_line)
569 off_t tws = 0;
570 if (tty_use_colors () && visible_tws)
572 tws = edit_eol (edit, b);
573 while (tws > b && ((c = edit_get_byte (edit, tws - 1)) == ' ' || c == '\t'))
574 tws--;
577 while (col <= end_col - edit->start_col)
579 int cw = 1;
580 int tab_over = 0;
581 gboolean wide_width_char = FALSE;
582 gboolean control_char = FALSE;
584 p->ch = 0;
585 p->style = 0;
586 if (q == edit->curs1)
587 p->style |= MOD_CURSOR;
588 if (q >= m1 && q < m2)
590 if (edit->column_highlight)
592 long x;
594 x = (long) edit_move_forward3 (edit, b, 0, q);
595 c1 = min (edit->column1, edit->column2);
596 c2 = max (edit->column1, edit->column2);
597 if (x >= c1 && x < c2)
598 p->style |= MOD_MARKED;
600 else
601 p->style |= MOD_MARKED;
603 if (q == edit->bracket)
604 p->style |= MOD_BOLD;
605 if (q >= edit->found_start && q < (off_t) (edit->found_start + edit->found_len))
606 p->style |= MOD_BOLD;
608 #ifdef HAVE_CHARSET
609 if (edit->utf8)
611 c = edit_get_utf (edit, q, &cw);
613 else
614 #endif
616 c = edit_get_byte (edit, q);
618 /* we don't use bg for mc - fg contains both */
619 if (book_mark)
621 p->style |= book_mark << 16;
623 else
625 color = edit_get_syntax_color (edit, q);
626 p->style |= color << 16;
628 switch (c)
630 case '\n':
631 col = end_col - edit->start_col + 1; /* quit */
632 break;
633 case '\t':
634 i = TAB_SIZE - ((int) col % TAB_SIZE);
635 tab_over = (end_col - edit->start_col) - (col + i - 1);
636 if (tab_over < 0)
637 i += tab_over;
638 col += i;
639 if (tty_use_colors () &&
640 ((visible_tabs || (visible_tws && q >= tws)) && enable_show_tabs_tws))
642 if (p->style & MOD_MARKED)
643 c = p->style;
644 else if (book_mark)
645 c |= book_mark << 16;
646 else
647 c = p->style | MOD_WHITESPACE;
648 if (i > 2)
650 p->ch = '<';
651 p->style = c;
652 p++;
653 while (--i > 1)
655 p->ch = '-';
656 p->style = c;
657 p++;
659 p->ch = '>';
660 p->style = c;
661 p++;
663 else if (i > 1)
665 p->ch = '<';
666 p->style = c;
667 p++;
668 p->ch = '>';
669 p->style = c;
670 p++;
672 else
674 p->ch = '>';
675 p->style = c;
676 p++;
679 else if (tty_use_colors () && visible_tws && q >= tws && enable_show_tabs_tws)
681 p->ch = '.';
682 p->style |= MOD_WHITESPACE;
683 c = p->style & ~MOD_CURSOR;
684 p++;
685 while (--i)
687 p->ch = ' ';
688 p->style = c;
689 p++;
692 else
694 p->ch |= ' ';
695 c = p->style & ~MOD_CURSOR;
696 p++;
697 while (--i)
699 p->ch = ' ';
700 p->style = c;
701 p++;
704 break;
705 case ' ':
706 if (tty_use_colors () && visible_tws && q >= tws && enable_show_tabs_tws)
708 p->ch = '.';
709 p->style |= MOD_WHITESPACE;
710 p++;
711 col++;
712 break;
714 /* fallthrough */
715 default:
716 #ifdef HAVE_CHARSET
717 if (mc_global.utf8_display)
719 if (!edit->utf8)
721 c = convert_from_8bit_to_utf_c ((unsigned char) c, edit->converter);
723 else
725 if (g_unichar_iswide (c))
727 wide_width_char = TRUE;
728 col++;
732 else if (edit->utf8)
733 c = convert_from_utf_to_current_c (c, edit->converter);
734 else
735 c = convert_to_display_c (c);
736 #endif
738 /* Caret notation for control characters */
739 if (c < 32)
741 p->ch = '^';
742 p->style = abn_style;
743 p++;
744 p->ch = c + 0x40;
745 p->style = abn_style;
746 p++;
747 col += 2;
748 control_char = TRUE;
749 break;
751 if (c == 127)
753 p->ch = '^';
754 p->style = abn_style;
755 p++;
756 p->ch = '?';
757 p->style = abn_style;
758 p++;
759 col += 2;
760 control_char = TRUE;
761 break;
763 #ifdef HAVE_CHARSET
764 if (edit->utf8)
766 if (g_unichar_isprint (c))
767 p->ch = c;
768 else
770 p->ch = '.';
771 p->style = abn_style;
773 p++;
775 else
776 #endif
778 if ((mc_global.utf8_display && g_unichar_isprint (c)) ||
779 (!mc_global.utf8_display && is_printable (c)))
781 p->ch = c;
782 p++;
784 else
786 p->ch = '.';
787 p->style = abn_style;
788 p++;
791 col++;
792 break;
793 } /* case */
795 q++;
796 if (cw > 1)
798 q += cw - 1;
801 if (col > (end_col - edit->start_col + 1))
803 if (wide_width_char)
805 p--;
806 break;
808 if (control_char)
810 p -= 2;
811 break;
817 else
819 start_col_real = start_col = 0;
822 p->ch = 0;
824 print_to_widget (edit, row, start_col, start_col_real, end_col, line, line_stat, book_mark);
827 /* --------------------------------------------------------------------------------------------- */
829 static inline void
830 edit_draw_this_char (WEdit * edit, off_t curs, long row, long start_column, long end_column)
832 off_t b = edit_bol (edit, curs);
834 edit_draw_this_line (edit, b, row, start_column, end_column);
837 /* --------------------------------------------------------------------------------------------- */
838 /** cursor must be in screen for other than REDRAW_PAGE passed in force */
840 static inline void
841 render_edit_text (WEdit * edit, long start_row, long start_column, long end_row, long end_column)
843 static long prev_curs_row = 0;
844 static off_t prev_curs = 0;
846 Widget *w = (Widget *) edit;
847 Dlg_head *h = w->owner;
848 Widget *wh = WIDGET (h);
850 long row = 0, curs_row;
851 int force = edit->force;
852 long b;
853 int y1, x1, y2, x2;
855 int last_line;
856 int last_column;
858 /* draw only visible region */
860 last_line = wh->y + wh->lines - 1;
862 y1 = w->y;
863 if (y1 > last_line - 1 /* buttonbar */ )
864 return;
866 last_column = wh->x + wh->cols - 1;
868 x1 = w->x;
869 if (x1 > last_column)
870 return;
872 y2 = w->y + w->lines - 1;
873 if (y2 < wh->y + 1 /* menubar */ )
874 return;
876 x2 = w->x + w->cols - 1;
877 if (x2 < wh->x)
878 return;
880 if ((force & REDRAW_IN_BOUNDS) == 0)
882 /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
883 /* draw only visible region */
885 if (y2 <= last_line - 1 /* buttonbar */ )
886 end_row = w->lines - 1;
887 else if (y1 >= wh->y + 1 /* menubar */ )
888 end_row = wh->lines - 1 - y1 - 1;
889 else
890 end_row = start_row + wh->lines - 1 - 1;
892 if (x2 <= last_column)
893 end_column = w->cols - 1;
894 else if (x1 >= wh->x)
895 end_column = wh->cols - 1 - x1;
896 else
897 end_column = start_column + wh->cols - 1;
901 * If the position of the page has not moved then we can draw the cursor
902 * character only. This will prevent line flicker when using arrow keys.
904 if ((force & REDRAW_CHAR_ONLY) == 0 || (force & REDRAW_PAGE) != 0)
906 if ((force & REDRAW_PAGE) != 0)
908 row = start_row;
909 b = edit_move_forward (edit, edit->start_display, start_row, 0);
910 while (row <= end_row)
912 if (key_pending (edit))
913 return;
914 edit_draw_this_line (edit, b, row, start_column, end_column);
915 b = edit_move_forward (edit, b, 1, 0);
916 row++;
919 else
921 curs_row = edit->curs_row;
923 if ((force & REDRAW_BEFORE_CURSOR) != 0 && start_row < curs_row)
925 long upto = curs_row - 1 <= end_row ? curs_row - 1 : end_row;
927 row = start_row;
928 b = edit->start_display;
929 while (row <= upto)
931 if (key_pending (edit))
932 return;
933 edit_draw_this_line (edit, b, row, start_column, end_column);
934 b = edit_move_forward (edit, b, 1, 0);
938 /* if (force & REDRAW_LINE) ---> default */
939 b = edit_bol (edit, edit->curs1);
940 if (curs_row >= start_row && curs_row <= end_row)
942 if (key_pending (edit))
943 return;
944 edit_draw_this_line (edit, b, curs_row, start_column, end_column);
947 if ((force & REDRAW_AFTER_CURSOR) != 0 && end_row > curs_row)
949 row = curs_row + 1 < start_row ? start_row : curs_row + 1;
950 b = edit_move_forward (edit, b, 1, 0);
951 while (row <= end_row)
953 if (key_pending (edit))
954 return;
955 edit_draw_this_line (edit, b, row, start_column, end_column);
956 b = edit_move_forward (edit, b, 1, 0);
957 row++;
961 if ((force & REDRAW_LINE_ABOVE) != 0 && curs_row >= 1)
963 row = curs_row - 1;
964 b = edit_move_backward (edit, edit_bol (edit, edit->curs1), 1);
965 if (row >= start_row && row <= end_row)
967 if (key_pending (edit))
968 return;
969 edit_draw_this_line (edit, b, row, start_column, end_column);
973 if ((force & REDRAW_LINE_BELOW) != 0 && row < edit->widget.lines - 1)
975 row = curs_row + 1;
976 b = edit_bol (edit, edit->curs1);
977 b = edit_move_forward (edit, b, 1, 0);
978 if (row >= start_row && row <= end_row)
980 if (key_pending (edit))
981 return;
982 edit_draw_this_line (edit, b, row, start_column, end_column);
987 else if (prev_curs_row < edit->curs_row)
989 /* with the new text highlighting, we must draw from the top down */
990 edit_draw_this_char (edit, prev_curs, prev_curs_row, start_column, end_column);
991 edit_draw_this_char (edit, edit->curs1, edit->curs_row, start_column, end_column);
993 else
995 edit_draw_this_char (edit, edit->curs1, edit->curs_row, start_column, end_column);
996 edit_draw_this_char (edit, prev_curs, prev_curs_row, start_column, end_column);
999 edit->force = 0;
1001 prev_curs_row = edit->curs_row;
1002 prev_curs = edit->curs1;
1005 /* --------------------------------------------------------------------------------------------- */
1007 static inline void
1008 edit_render (WEdit * edit, int page, int row_start, int col_start, int row_end, int col_end)
1010 if (page) /* if it was an expose event, 'page' would be set */
1011 edit->force |= REDRAW_PAGE | REDRAW_IN_BOUNDS;
1013 render_edit_text (edit, row_start, col_start, row_end, col_end);
1016 * edit->force != 0 means a key was pending and the redraw
1017 * was halted, so next time we must redraw everything in case stuff
1018 * was left undrawn from a previous key press.
1020 if (edit->force)
1021 edit->force |= REDRAW_PAGE;
1024 /* --------------------------------------------------------------------------------------------- */
1025 /*** public functions ****************************************************************************/
1026 /* --------------------------------------------------------------------------------------------- */
1028 void
1029 edit_status (WEdit * edit, gboolean active)
1031 int color;
1033 if (edit->fullscreen)
1035 color = STATUSBAR_COLOR;
1036 edit_status_fullscreen (edit, color);
1038 else
1040 color = edit->drag_state != MCEDIT_DRAG_NORMAL ? EDITOR_FRAME_DRAG : active ?
1041 EDITOR_FRAME_ACTIVE : EDITOR_FRAME;
1042 edit_draw_frame (edit, color, active);
1043 edit_status_window (edit);
1046 edit_draw_window_icons (edit, color);
1049 /* --------------------------------------------------------------------------------------------- */
1051 /** this scrolls the text so that cursor is on the screen */
1052 void
1053 edit_scroll_screen_over_cursor (WEdit * edit)
1055 long p;
1056 long outby;
1057 int b_extreme, t_extreme, l_extreme, r_extreme;
1059 if (edit->widget.lines <= 0 || edit->widget.cols <= 0)
1060 return;
1062 edit->widget.lines -= EDIT_TEXT_VERTICAL_OFFSET;
1063 edit->widget.cols -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
1065 if (!edit->fullscreen)
1067 edit->widget.x++;
1068 edit->widget.cols -= 2;
1069 edit->widget.y++;
1070 edit->widget.lines -= 2;
1073 r_extreme = EDIT_RIGHT_EXTREME;
1074 l_extreme = EDIT_LEFT_EXTREME;
1075 b_extreme = EDIT_BOTTOM_EXTREME;
1076 t_extreme = EDIT_TOP_EXTREME;
1077 if (edit->found_len != 0)
1079 b_extreme = max (edit->widget.lines / 4, b_extreme);
1080 t_extreme = max (edit->widget.lines / 4, t_extreme);
1082 if (b_extreme + t_extreme + 1 > edit->widget.lines)
1084 int n;
1086 n = b_extreme + t_extreme;
1087 if (n == 0)
1088 n = 1;
1089 b_extreme = (b_extreme * (edit->widget.lines - 1)) / n;
1090 t_extreme = (t_extreme * (edit->widget.lines - 1)) / n;
1092 if (l_extreme + r_extreme + 1 > edit->widget.cols)
1094 int n;
1096 n = l_extreme + t_extreme;
1097 if (n == 0)
1098 n = 1;
1099 l_extreme = (l_extreme * (edit->widget.cols - 1)) / n;
1100 r_extreme = (r_extreme * (edit->widget.cols - 1)) / n;
1102 p = edit_get_col (edit) + edit->over_col;
1103 edit_update_curs_row (edit);
1104 outby = p + edit->start_col - edit->widget.cols + 1 + (r_extreme + edit->found_len);
1105 if (outby > 0)
1106 edit_scroll_right (edit, outby);
1107 outby = l_extreme - p - edit->start_col;
1108 if (outby > 0)
1109 edit_scroll_left (edit, outby);
1110 p = edit->curs_row;
1111 outby = p - edit->widget.lines + 1 + b_extreme;
1112 if (outby > 0)
1113 edit_scroll_downward (edit, outby);
1114 outby = t_extreme - p;
1115 if (outby > 0)
1116 edit_scroll_upward (edit, outby);
1117 edit_update_curs_row (edit);
1119 edit->widget.lines += EDIT_TEXT_VERTICAL_OFFSET;
1120 edit->widget.cols += EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
1121 if (!edit->fullscreen)
1123 edit->widget.x--;
1124 edit->widget.cols += 2;
1125 edit->widget.y--;
1126 edit->widget.lines += 2;
1130 /* --------------------------------------------------------------------------------------------- */
1132 void
1133 edit_render_keypress (WEdit * edit)
1135 edit_render (edit, 0, 0, 0, 0, 0);
1138 /* --------------------------------------------------------------------------------------------- */