code indentation
[midnight-commander.git] / src / editor / editdraw.c
blobf9315963a82e13d2aa471b13a0675c06b4c2a95e
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 */
57 #include "src/main.h" /* macro_index */
59 #include "edit-impl.h"
60 #include "editwidget.h"
62 /*** global variables ****************************************************************************/
64 /* Toggles statusbar draw style */
65 int simple_statusbar = 0;
67 int visible_tabs = 1, visible_tws = 1;
69 /*** file scope macro definitions ****************************************************************/
71 #define MAX_LINE_LEN 1024
73 /* Text styles */
74 #define MOD_ABNORMAL (1 << 8)
75 #define MOD_BOLD (1 << 9)
76 #define MOD_MARKED (1 << 10)
77 #define MOD_CURSOR (1 << 11)
78 #define MOD_WHITESPACE (1 << 12)
80 #define edit_move(x,y) widget_move(edit, y, x);
82 #define key_pending(x) (!is_idle())
84 #define EDITOR_MINIMUM_TERMINAL_WIDTH 30
86 /*** file scope type declarations ****************************************************************/
88 struct line_s
90 unsigned int ch;
91 unsigned int style;
94 /*** file scope variables ************************************************************************/
96 /*** file scope functions ************************************************************************/
98 static inline void
99 printwstr (const char *s, int len)
101 if (len > 0)
102 tty_printf ("%-*.*s", len, len, s);
105 /* --------------------------------------------------------------------------------------------- */
107 static inline void
108 status_string (WEdit * edit, char *s, int w)
110 char byte_str[16];
113 * If we are at the end of file, print <EOF>,
114 * otherwise print the current character as is (if printable),
115 * as decimal and as hex.
117 if (edit->curs1 < edit->last_byte)
119 #ifdef HAVE_CHARSET
120 if (edit->utf8)
122 unsigned int cur_utf = 0;
123 int cw = 1;
125 cur_utf = edit_get_utf (edit, edit->curs1, &cw);
126 if (cw > 0)
128 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
129 (unsigned) cur_utf, (unsigned) cur_utf);
131 else
133 cur_utf = edit_get_byte (edit, edit->curs1);
134 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
135 (int) cur_utf, (unsigned) cur_utf);
138 else
139 #endif
141 unsigned char cur_byte = 0;
143 cur_byte = edit_get_byte (edit, edit->curs1);
144 g_snprintf (byte_str, sizeof (byte_str), "%4d 0x%03X",
145 (int) cur_byte, (unsigned) cur_byte);
148 else
150 strcpy (byte_str, "<EOF> ");
153 /* The field lengths just prevent the status line from shortening too much */
154 if (simple_statusbar)
155 g_snprintf (s, w,
156 "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s",
157 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
158 edit->modified ? 'M' : '-',
159 macro_index < 0 ? '-' : 'R',
160 edit->overwrite == 0 ? '-' : 'O',
161 edit->curs_col + edit->over_col,
162 edit->curs_line + 1,
163 edit->total_lines + 1, (long) edit->curs1, (long) edit->last_byte, 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->curs_line + 1,
179 edit->total_lines + 1, (long) edit->curs1, (long) edit->last_byte, byte_str,
180 #ifdef HAVE_CHARSET
181 mc_global.source_codepage >= 0 ? get_codepage_id (mc_global.source_codepage) :
182 #endif
183 "");
186 /* --------------------------------------------------------------------------------------------- */
188 * Draw the status line at the top of the screen for fullscreen editor window.
190 * @param edit editor object
191 * @param color color pair
194 static inline void
195 edit_status_fullscreen (WEdit * edit, int color)
197 const int w = edit->widget.owner->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 dlg_move (edit->widget.owner, 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 dlg_move (edit->widget.owner, 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 int y, x;
255 int cols = edit->widget.cols;
257 tty_setcolor (STATUSBAR_COLOR);
259 if (cols > 5)
261 const char *fname = N_("NoName");
262 char *full_fname = NULL;
264 if (edit->filename_vpath != NULL)
266 full_fname = vfs_path_to_str (edit->filename_vpath);
267 fname = x_basename (full_fname);
269 #ifdef ENABLE_NLS
270 else
271 fname = _(fname);
272 #endif
274 edit_move (2, 0);
275 tty_printf ("[%s]", str_term_trim (fname, edit->widget.cols - 8 - 6));
276 g_free (full_fname);
279 tty_getyx (&y, &x);
280 x -= edit->widget.x;
281 x += 4;
282 if (x + 6 <= cols - 2 - 6)
284 edit_move (x, 0);
285 tty_printf ("[%c%c%c%c]",
286 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
287 edit->modified ? 'M' : '-',
288 macro_index < 0 ? '-' : 'R', edit->overwrite == 0 ? '-' : 'O');
291 if (cols > 30)
293 edit_move (2, edit->widget.lines - 1);
294 tty_printf ("%3ld %5ld/%ld %6ld/%ld",
295 edit->curs_col + edit->over_col,
296 edit->curs_line + 1, edit->total_lines + 1, edit->curs1, edit->last_byte);
300 * If we are at the end of file, print <EOF>,
301 * otherwise print the current character as is (if printable),
302 * as decimal and as hex.
304 if (cols > 46)
306 edit_move (32, edit->widget.lines - 1);
307 if (edit->curs1 >= edit->last_byte)
308 tty_print_string ("[<EOF> ]");
309 #ifdef HAVE_CHARSET
310 else if (edit->utf8)
312 unsigned int cur_utf;
313 int cw = 1;
315 cur_utf = edit_get_utf (edit, edit->curs1, &cw);
316 if (cw <= 0)
317 cur_utf = edit_get_byte (edit, edit->curs1);
318 tty_printf ("[%05d 0x%04X]", cur_utf, cur_utf);
320 #endif
321 else
323 unsigned char cur_byte;
325 cur_byte = edit_get_byte (edit, edit->curs1);
326 tty_printf ("[%05d 0x%04X]", (unsigned int) cur_byte, (unsigned int) cur_byte);
331 /* --------------------------------------------------------------------------------------------- */
333 * Draw a frame around edit area.
335 * @param edit editor object
336 * @param color color pair
337 * @param active TRUE if editor object is focused
340 static inline void
341 edit_draw_frame (const WEdit * edit, int color, gboolean active)
343 const Widget *w = (const Widget *) edit;
345 /* draw a frame around edit area */
346 tty_setcolor (color);
347 /* draw double frame for active window if skin supports that */
348 tty_draw_box (w->y, w->x, w->lines, w->cols, !active);
349 /* draw a drag marker */
350 if (edit->drag_state == MCEDIT_DRAG_NORMAL)
352 tty_setcolor (EDITOR_FRAME_DRAG);
353 widget_move (w, w->lines - 1, w->cols - 1);
354 tty_print_alt_char (ACS_LRCORNER, TRUE);
358 /* --------------------------------------------------------------------------------------------- */
360 * Draw a window control buttons.
362 * @param edit editor object
363 * @param color color pair
366 static inline void
367 edit_draw_window_icons (const WEdit * edit, int color)
369 const Widget *w = (const Widget *) edit;
370 char tmp[17];
372 tty_setcolor (color);
373 if (edit->fullscreen)
374 dlg_move (w->owner, 0, w->owner->cols - 6);
375 else
376 widget_move (w, 0, edit->widget.cols - 8);
377 g_snprintf (tmp, sizeof (tmp), "[%s][%s]", edit_window_state_char, edit_window_close_char);
378 tty_print_string (tmp);
381 /* --------------------------------------------------------------------------------------------- */
383 static inline void
384 print_to_widget (WEdit * edit, long row, int start_col, int start_col_real,
385 long end_col, struct line_s line[], char *status, int bookmarked)
387 struct line_s *p;
389 int x = start_col_real;
390 int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
391 int y = row + EDIT_TEXT_VERTICAL_OFFSET;
392 int cols_to_skip = abs (x);
393 int i;
394 int wrap_start;
395 int len;
397 if (!edit->fullscreen)
399 x1++;
400 y++;
403 tty_setcolor (EDITOR_NORMAL_COLOR);
404 if (bookmarked != 0)
405 tty_setcolor (bookmarked);
407 len = end_col + 1 - start_col;
408 wrap_start = option_word_wrap_line_length + edit->start_col;
410 if (len > 0 && edit->widget.y + y >= 0)
412 if (!show_right_margin || wrap_start > end_col)
413 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', len);
414 else if (wrap_start < 0)
416 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
417 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', len);
419 else
421 if (wrap_start > 0)
422 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', wrap_start);
424 len -= wrap_start;
425 if (len > 0)
427 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
428 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1 + wrap_start, ' ', len);
433 if (option_line_state)
435 tty_setcolor (LINE_STATE_COLOR);
436 for (i = 0; i < LINE_STATE_WIDTH; i++)
438 edit_move (x1 + i - option_line_state_width, y);
439 if (status[i] == '\0')
440 status[i] = ' ';
441 tty_print_char (status[i]);
445 edit_move (x1, y);
446 i = 1;
447 for (p = line; p->ch != 0; p++)
449 int style;
450 unsigned int textchar;
451 int color;
453 if (cols_to_skip != 0)
455 cols_to_skip--;
456 continue;
459 style = p->style & 0xFF00;
460 textchar = p->ch;
461 color = p->style >> 16;
463 if (style & MOD_ABNORMAL)
465 /* Non-printable - use black background */
466 color = 0;
469 if (style & MOD_WHITESPACE)
471 if (style & MOD_MARKED)
473 textchar = ' ';
474 tty_setcolor (EDITOR_MARKED_COLOR);
476 else
477 tty_setcolor (EDITOR_WHITESPACE_COLOR);
479 else if (style & MOD_BOLD)
480 tty_setcolor (EDITOR_BOLD_COLOR);
481 else if (style & MOD_MARKED)
482 tty_setcolor (EDITOR_MARKED_COLOR);
483 else
484 tty_lowlevel_setcolor (color);
486 if (show_right_margin)
488 if (i > option_word_wrap_line_length + edit->start_col)
489 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
490 i++;
493 tty_print_anychar (textchar);
497 /* --------------------------------------------------------------------------------------------- */
498 /** b is a pointer to the beginning of the line */
500 static void
501 edit_draw_this_line (WEdit * edit, off_t b, long row, long start_col, long end_col)
503 struct line_s line[MAX_LINE_LEN];
504 struct line_s *p = line;
506 off_t m1 = 0, m2 = 0, q;
507 long c1, c2;
508 int col, start_col_real;
509 unsigned int c;
510 int color;
511 int abn_style;
512 int i;
513 unsigned int cur_line = 0;
514 int book_mark = 0;
515 char line_stat[LINE_STATE_WIDTH + 1] = "\0";
517 if (row > edit->widget.lines - 1 - EDIT_TEXT_VERTICAL_OFFSET - 2 * (edit->fullscreen ? 0 : 1))
518 return;
520 if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_COLOR))
521 book_mark = BOOK_MARK_COLOR;
522 else if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_FOUND_COLOR))
523 book_mark = BOOK_MARK_FOUND_COLOR;
525 if (book_mark)
526 abn_style = book_mark << 16;
527 else
528 abn_style = MOD_ABNORMAL;
530 end_col -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
531 if (!edit->fullscreen)
533 const Widget *w = (const Widget *) edit;
535 end_col--;
536 if (w->x + w->cols <= w->owner->cols)
537 end_col--;
540 edit_get_syntax_color (edit, b - 1, &color);
541 q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
542 start_col_real = (col = (int) edit_move_forward3 (edit, b, 0, q)) + edit->start_col;
544 if (option_line_state)
546 cur_line = edit->start_line + row;
547 if (cur_line <= (unsigned int) edit->total_lines)
549 g_snprintf (line_stat, LINE_STATE_WIDTH + 1, "%7i ", cur_line + 1);
551 else
553 memset (line_stat, ' ', LINE_STATE_WIDTH);
554 line_stat[LINE_STATE_WIDTH] = '\0';
556 if (book_mark_query_color (edit, cur_line, BOOK_MARK_COLOR))
558 g_snprintf (line_stat, 2, "*");
562 if (col + 16 > -edit->start_col)
564 eval_marks (edit, &m1, &m2);
566 if (row <= edit->total_lines - edit->start_line)
568 off_t tws = 0;
569 if (tty_use_colors () && visible_tws)
571 tws = edit_eol (edit, b);
572 while (tws > b && ((c = edit_get_byte (edit, tws - 1)) == ' ' || c == '\t'))
573 tws--;
576 while (col <= end_col - edit->start_col)
578 int cw = 1;
579 int tab_over = 0;
580 gboolean wide_width_char = FALSE;
581 gboolean control_char = FALSE;
583 p->ch = 0;
584 p->style = 0;
585 if (q == edit->curs1)
586 p->style |= MOD_CURSOR;
587 if (q >= m1 && q < m2)
589 if (edit->column_highlight)
591 long x;
593 x = (long) edit_move_forward3 (edit, b, 0, q);
594 c1 = min (edit->column1, edit->column2);
595 c2 = max (edit->column1, edit->column2);
596 if (x >= c1 && x < c2)
597 p->style |= MOD_MARKED;
599 else
600 p->style |= MOD_MARKED;
602 if (q == edit->bracket)
603 p->style |= MOD_BOLD;
604 if (q >= edit->found_start && q < (off_t) (edit->found_start + edit->found_len))
605 p->style |= MOD_BOLD;
607 #ifdef HAVE_CHARSET
608 if (edit->utf8)
610 c = edit_get_utf (edit, q, &cw);
612 else
613 #endif
615 c = edit_get_byte (edit, q);
617 /* we don't use bg for mc - fg contains both */
618 if (book_mark)
620 p->style |= book_mark << 16;
622 else
624 edit_get_syntax_color (edit, q, &color);
625 p->style |= color << 16;
627 switch (c)
629 case '\n':
630 col = end_col - edit->start_col + 1; /* quit */
631 break;
632 case '\t':
633 i = TAB_SIZE - ((int) col % TAB_SIZE);
634 tab_over = (end_col - edit->start_col) - (col + i - 1);
635 if (tab_over < 0)
636 i += tab_over;
637 col += i;
638 if (tty_use_colors () &&
639 ((visible_tabs || (visible_tws && q >= tws)) && enable_show_tabs_tws))
641 if (p->style & MOD_MARKED)
642 c = p->style;
643 else if (book_mark)
644 c |= book_mark << 16;
645 else
646 c = p->style | MOD_WHITESPACE;
647 if (i > 2)
649 p->ch = '<';
650 p->style = c;
651 p++;
652 while (--i > 1)
654 p->ch = '-';
655 p->style = c;
656 p++;
658 p->ch = '>';
659 p->style = c;
660 p++;
662 else if (i > 1)
664 p->ch = '<';
665 p->style = c;
666 p++;
667 p->ch = '>';
668 p->style = c;
669 p++;
671 else
673 p->ch = '>';
674 p->style = c;
675 p++;
678 else if (tty_use_colors () && visible_tws && q >= tws && enable_show_tabs_tws)
680 p->ch = '.';
681 p->style |= MOD_WHITESPACE;
682 c = p->style & ~MOD_CURSOR;
683 p++;
684 while (--i)
686 p->ch = ' ';
687 p->style = c;
688 p++;
691 else
693 p->ch |= ' ';
694 c = p->style & ~MOD_CURSOR;
695 p++;
696 while (--i)
698 p->ch = ' ';
699 p->style = c;
700 p++;
703 break;
704 case ' ':
705 if (tty_use_colors () && visible_tws && q >= tws && enable_show_tabs_tws)
707 p->ch = '.';
708 p->style |= MOD_WHITESPACE;
709 p++;
710 col++;
711 break;
713 /* fallthrough */
714 default:
715 #ifdef HAVE_CHARSET
716 if (mc_global.utf8_display)
718 if (!edit->utf8)
720 c = convert_from_8bit_to_utf_c ((unsigned char) c, edit->converter);
722 else
724 if (g_unichar_iswide (c))
726 wide_width_char = TRUE;
727 col++;
731 else if (edit->utf8)
732 c = convert_from_utf_to_current_c (c, edit->converter);
733 else
734 c = convert_to_display_c (c);
735 #endif
737 /* Caret notation for control characters */
738 if (c < 32)
740 p->ch = '^';
741 p->style = abn_style;
742 p++;
743 p->ch = c + 0x40;
744 p->style = abn_style;
745 p++;
746 col += 2;
747 control_char = TRUE;
748 break;
750 if (c == 127)
752 p->ch = '^';
753 p->style = abn_style;
754 p++;
755 p->ch = '?';
756 p->style = abn_style;
757 p++;
758 col += 2;
759 control_char = TRUE;
760 break;
762 #ifdef HAVE_CHARSET
763 if (edit->utf8)
765 if (g_unichar_isprint (c))
766 p->ch = c;
767 else
769 p->ch = '.';
770 p->style = abn_style;
772 p++;
774 else
775 #endif
777 if ((mc_global.utf8_display && g_unichar_isprint (c)) ||
778 (!mc_global.utf8_display && is_printable (c)))
780 p->ch = c;
781 p++;
783 else
785 p->ch = '.';
786 p->style = abn_style;
787 p++;
790 col++;
791 break;
792 } /* case */
794 q++;
795 if (cw > 1)
797 q += cw - 1;
800 if (col > (end_col - edit->start_col + 1))
802 if (wide_width_char)
804 p--;
805 break;
807 if (control_char)
809 p -= 2;
810 break;
816 else
818 start_col_real = start_col = 0;
821 p->ch = 0;
823 print_to_widget (edit, row, start_col, start_col_real, end_col, line, line_stat, book_mark);
826 /* --------------------------------------------------------------------------------------------- */
828 static inline void
829 edit_draw_this_char (WEdit * edit, off_t curs, long row, long start_column, long end_column)
831 off_t b = edit_bol (edit, curs);
833 edit_draw_this_line (edit, b, row, start_column, end_column);
836 /* --------------------------------------------------------------------------------------------- */
837 /** cursor must be in screen for other than REDRAW_PAGE passed in force */
839 static inline void
840 render_edit_text (WEdit * edit, long start_row, long start_column, long end_row, long end_column)
842 static long prev_curs_row = 0;
843 static off_t prev_curs = 0;
845 Widget *w = (Widget *) edit;
846 Dlg_head *h = w->owner;
848 long row = 0, curs_row;
849 int force = edit->force;
850 long b;
851 int y1, x1, y2, x2;
853 int last_line;
854 int last_column;
856 /* draw only visible region */
858 last_line = h->y + h->lines - 1;
860 y1 = w->y;
861 if (y1 > last_line - 1 /* buttonbar */ )
862 return;
864 last_column = h->x + h->cols - 1;
866 x1 = w->x;
867 if (x1 > last_column)
868 return;
870 y2 = w->y + w->lines - 1;
871 if (y2 < h->y + 1 /* menubar */ )
872 return;
874 x2 = w->x + w->cols - 1;
875 if (x2 < h->x)
876 return;
878 if ((force & REDRAW_IN_BOUNDS) == 0)
880 /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
881 /* draw only visible region */
883 if (y2 <= last_line - 1 /* buttonbar */ )
884 end_row = w->lines - 1;
885 else if (y1 >= h->y + 1 /* menubar */ )
886 end_row = h->lines - 1 - y1 - 1;
887 else
888 end_row = start_row + h->lines - 1 - 1;
890 if (x2 <= last_column)
891 end_column = w->cols - 1;
892 else if (x1 >= h->x)
893 end_column = h->cols - 1 - x1;
894 else
895 end_column = start_column + h->cols - 1;
899 * If the position of the page has not moved then we can draw the cursor
900 * character only. This will prevent line flicker when using arrow keys.
902 if ((force & REDRAW_CHAR_ONLY) == 0 || (force & REDRAW_PAGE) != 0)
904 if ((force & REDRAW_PAGE) != 0)
906 row = start_row;
907 b = edit_move_forward (edit, edit->start_display, start_row, 0);
908 while (row <= end_row)
910 if (key_pending (edit))
911 return;
912 edit_draw_this_line (edit, b, row, start_column, end_column);
913 b = edit_move_forward (edit, b, 1, 0);
914 row++;
917 else
919 curs_row = edit->curs_row;
921 if ((force & REDRAW_BEFORE_CURSOR) != 0 && start_row < curs_row)
923 long upto = curs_row - 1 <= end_row ? curs_row - 1 : end_row;
925 row = start_row;
926 b = edit->start_display;
927 while (row <= upto)
929 if (key_pending (edit))
930 return;
931 edit_draw_this_line (edit, b, row, start_column, end_column);
932 b = edit_move_forward (edit, b, 1, 0);
936 /* if (force & REDRAW_LINE) ---> default */
937 b = edit_bol (edit, edit->curs1);
938 if (curs_row >= start_row && curs_row <= end_row)
940 if (key_pending (edit))
941 return;
942 edit_draw_this_line (edit, b, curs_row, start_column, end_column);
945 if ((force & REDRAW_AFTER_CURSOR) != 0 && end_row > curs_row)
947 row = curs_row + 1 < start_row ? start_row : curs_row + 1;
948 b = edit_move_forward (edit, b, 1, 0);
949 while (row <= end_row)
951 if (key_pending (edit))
952 return;
953 edit_draw_this_line (edit, b, row, start_column, end_column);
954 b = edit_move_forward (edit, b, 1, 0);
955 row++;
959 if ((force & REDRAW_LINE_ABOVE) != 0 && curs_row >= 1)
961 row = curs_row - 1;
962 b = edit_move_backward (edit, edit_bol (edit, edit->curs1), 1);
963 if (row >= start_row && row <= end_row)
965 if (key_pending (edit))
966 return;
967 edit_draw_this_line (edit, b, row, start_column, end_column);
971 if ((force & REDRAW_LINE_BELOW) != 0 && row < edit->widget.lines - 1)
973 row = curs_row + 1;
974 b = edit_bol (edit, edit->curs1);
975 b = edit_move_forward (edit, b, 1, 0);
976 if (row >= start_row && row <= end_row)
978 if (key_pending (edit))
979 return;
980 edit_draw_this_line (edit, b, row, start_column, end_column);
985 else if (prev_curs_row < edit->curs_row)
987 /* with the new text highlighting, we must draw from the top down */
988 edit_draw_this_char (edit, prev_curs, prev_curs_row, start_column, end_column);
989 edit_draw_this_char (edit, edit->curs1, edit->curs_row, start_column, end_column);
991 else
993 edit_draw_this_char (edit, edit->curs1, edit->curs_row, start_column, end_column);
994 edit_draw_this_char (edit, prev_curs, prev_curs_row, start_column, end_column);
997 edit->force = 0;
999 prev_curs_row = edit->curs_row;
1000 prev_curs = edit->curs1;
1003 /* --------------------------------------------------------------------------------------------- */
1005 static inline void
1006 edit_render (WEdit * edit, int page, int row_start, int col_start, int row_end, int col_end)
1008 if (page) /* if it was an expose event, 'page' would be set */
1009 edit->force |= REDRAW_PAGE | REDRAW_IN_BOUNDS;
1011 render_edit_text (edit, row_start, col_start, row_end, col_end);
1014 * edit->force != 0 means a key was pending and the redraw
1015 * was halted, so next time we must redraw everything in case stuff
1016 * was left undrawn from a previous key press.
1018 if (edit->force)
1019 edit->force |= REDRAW_PAGE;
1022 /* --------------------------------------------------------------------------------------------- */
1023 /*** public functions ****************************************************************************/
1024 /* --------------------------------------------------------------------------------------------- */
1026 void
1027 edit_status (WEdit * edit, gboolean active)
1029 int color;
1031 if (edit->fullscreen)
1033 color = STATUSBAR_COLOR;
1034 edit_status_fullscreen (edit, color);
1036 else
1038 color = edit->drag_state != MCEDIT_DRAG_NORMAL ? EDITOR_FRAME_DRAG : active ?
1039 EDITOR_FRAME_ACTIVE : EDITOR_FRAME;
1040 edit_draw_frame (edit, color, active);
1041 edit_status_window (edit);
1044 edit_draw_window_icons (edit, color);
1047 /* --------------------------------------------------------------------------------------------- */
1049 /** this scrolls the text so that cursor is on the screen */
1050 void
1051 edit_scroll_screen_over_cursor (WEdit * edit)
1053 long p;
1054 long outby;
1055 int b_extreme, t_extreme, l_extreme, r_extreme;
1057 if (edit->widget.lines <= 0 || edit->widget.cols <= 0)
1058 return;
1060 edit->widget.lines -= EDIT_TEXT_VERTICAL_OFFSET;
1061 edit->widget.cols -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
1063 if (!edit->fullscreen)
1065 edit->widget.x++;
1066 edit->widget.cols -= 2;
1067 edit->widget.y++;
1068 edit->widget.lines -= 2;
1071 r_extreme = EDIT_RIGHT_EXTREME;
1072 l_extreme = EDIT_LEFT_EXTREME;
1073 b_extreme = EDIT_BOTTOM_EXTREME;
1074 t_extreme = EDIT_TOP_EXTREME;
1075 if (edit->found_len != 0)
1077 b_extreme = max (edit->widget.lines / 4, b_extreme);
1078 t_extreme = max (edit->widget.lines / 4, t_extreme);
1080 if (b_extreme + t_extreme + 1 > edit->widget.lines)
1082 int n;
1084 n = b_extreme + t_extreme;
1085 if (n == 0)
1086 n = 1;
1087 b_extreme = (b_extreme * (edit->widget.lines - 1)) / n;
1088 t_extreme = (t_extreme * (edit->widget.lines - 1)) / n;
1090 if (l_extreme + r_extreme + 1 > edit->widget.cols)
1092 int n;
1094 n = l_extreme + t_extreme;
1095 if (n == 0)
1096 n = 1;
1097 l_extreme = (l_extreme * (edit->widget.cols - 1)) / n;
1098 r_extreme = (r_extreme * (edit->widget.cols - 1)) / n;
1100 p = edit_get_col (edit) + edit->over_col;
1101 edit_update_curs_row (edit);
1102 outby = p + edit->start_col - edit->widget.cols + 1 + (r_extreme + edit->found_len);
1103 if (outby > 0)
1104 edit_scroll_right (edit, outby);
1105 outby = l_extreme - p - edit->start_col;
1106 if (outby > 0)
1107 edit_scroll_left (edit, outby);
1108 p = edit->curs_row;
1109 outby = p - edit->widget.lines + 1 + b_extreme;
1110 if (outby > 0)
1111 edit_scroll_downward (edit, outby);
1112 outby = t_extreme - p;
1113 if (outby > 0)
1114 edit_scroll_upward (edit, outby);
1115 edit_update_curs_row (edit);
1117 edit->widget.lines += EDIT_TEXT_VERTICAL_OFFSET;
1118 edit->widget.cols += EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
1119 if (!edit->fullscreen)
1121 edit->widget.x--;
1122 edit->widget.cols += 2;
1123 edit->widget.y--;
1124 edit->widget.lines += 2;
1128 /* --------------------------------------------------------------------------------------------- */
1130 void
1131 edit_render_keypress (WEdit * edit)
1133 edit_render (edit, 0, 0, 0, 0, 0);
1136 /* --------------------------------------------------------------------------------------------- */