(edit_find_word_start): return gboolean instead of int.
[midnight-commander.git] / src / editor / editdraw.c
blob22fac69c1b165f427c019d393c8467945c50555e
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];
111 unsigned char cur_byte = 0;
112 unsigned int cur_utf = 0;
113 int cw = 1;
116 * If we are at the end of file, print <EOF>,
117 * otherwise print the current character as is (if printable),
118 * as decimal and as hex.
120 if (edit->curs1 < edit->last_byte)
122 if (!edit->utf8)
124 cur_byte = edit_get_byte (edit, edit->curs1);
126 g_snprintf (byte_str, sizeof (byte_str), "%4d 0x%03X",
127 (int) cur_byte, (unsigned) cur_byte);
129 else
131 cur_utf = edit_get_utf (edit, edit->curs1, &cw);
132 if (cw > 0)
134 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
135 (unsigned) cur_utf, (unsigned) cur_utf);
137 else
139 cur_utf = edit_get_byte (edit, edit->curs1);
140 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
141 (int) cur_utf, (unsigned) cur_utf);
146 else
148 strcpy (byte_str, "<EOF> ");
151 /* The field lengths just prevent the status line from shortening too much */
152 if (simple_statusbar)
153 g_snprintf (s, w,
154 "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s",
155 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
156 edit->modified ? 'M' : '-',
157 macro_index < 0 ? '-' : 'R',
158 edit->overwrite == 0 ? '-' : 'O',
159 edit->curs_col + edit->over_col,
160 edit->curs_line + 1,
161 edit->total_lines + 1, edit->curs1, edit->last_byte, byte_str,
162 #ifdef HAVE_CHARSET
163 mc_global.source_codepage >=
164 0 ? get_codepage_id (mc_global.source_codepage) : ""
165 #else
167 #endif
169 else
170 g_snprintf (s, w,
171 "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb) %s %s",
172 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
173 edit->modified ? 'M' : '-',
174 macro_index < 0 ? '-' : 'R',
175 edit->overwrite == 0 ? '-' : 'O',
176 edit->curs_col + edit->over_col,
177 edit->start_line + 1,
178 edit->curs_row,
179 edit->curs_line + 1,
180 edit->total_lines + 1, edit->curs1, edit->last_byte, byte_str,
181 #ifdef HAVE_CHARSET
182 mc_global.source_codepage >=
183 0 ? get_codepage_id (mc_global.source_codepage) : ""
184 #else
186 #endif
190 /* --------------------------------------------------------------------------------------------- */
192 * Draw the status line at the top of the screen for fullscreen editor window.
194 * @param edit editor object
195 * @param color color pair
198 static inline void
199 edit_status_fullscreen (WEdit * edit, int color)
201 const int w = edit->widget.owner->cols;
202 const size_t status_size = w + 1;
203 char *const status = g_malloc (status_size);
204 int status_len;
205 const char *fname = "";
206 int fname_len;
207 const int gap = 3; /* between the filename and the status */
208 const int right_gap = 5; /* at the right end of the screen */
209 const int preferred_fname_len = 16;
211 status_string (edit, status, status_size);
212 status_len = (int) str_term_width1 (status);
214 if (edit->filename_vpath != NULL)
215 fname = x_basename (vfs_path_get_last_path_str (edit->filename_vpath));
217 fname_len = str_term_width1 (fname);
218 if (fname_len < preferred_fname_len)
219 fname_len = preferred_fname_len;
221 if (fname_len + gap + status_len + right_gap >= w)
223 if (preferred_fname_len + gap + status_len + right_gap >= w)
224 fname_len = preferred_fname_len;
225 else
226 fname_len = w - (gap + status_len + right_gap);
227 fname = str_trunc (fname, fname_len);
230 dlg_move (edit->widget.owner, 0, 0);
231 tty_setcolor (color);
232 printwstr (fname, fname_len + gap);
233 printwstr (status, w - (fname_len + gap));
235 if (simple_statusbar && w > EDITOR_MINIMUM_TERMINAL_WIDTH)
237 size_t percent = 100;
239 if (edit->total_lines + 1 != 0)
240 percent = (edit->curs_line + 1) * 100 / (edit->total_lines + 1);
241 dlg_move (edit->widget.owner, 0, w - 6 - 6);
242 tty_printf (" %3d%%", percent);
245 g_free (status);
248 /* --------------------------------------------------------------------------------------------- */
250 * Draw status line for editor window if window is not in fullscreen mode.
252 * @param edit editor object
255 static inline void
256 edit_status_window (WEdit * edit)
258 int y, x;
259 int cols = edit->widget.cols;
261 tty_setcolor (STATUSBAR_COLOR);
263 if (cols > 5)
265 const char *fname = N_("NoName");
266 char *full_fname = NULL;
268 if (edit->filename_vpath != NULL)
270 full_fname = vfs_path_to_str (edit->filename_vpath);
271 fname = x_basename (full_fname);
273 #ifdef ENABLE_NLS
274 else
275 fname = _(fname);
276 #endif
278 edit_move (2, 0);
279 tty_printf ("[%s]", str_term_trim (fname, edit->widget.cols - 8 - 6));
280 g_free (full_fname);
283 tty_getyx (&y, &x);
284 x -= edit->widget.x;
285 x += 4;
286 if (x + 6 <= cols - 2 - 6)
288 edit_move (x, 0);
289 tty_printf ("[%c%c%c%c]",
290 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
291 edit->modified ? 'M' : '-',
292 macro_index < 0 ? '-' : 'R', edit->overwrite == 0 ? '-' : 'O');
295 if (cols > 30)
297 edit_move (2, edit->widget.lines - 1);
298 tty_printf ("%3ld %5ld/%ld %6ld/%ld",
299 edit->curs_col + edit->over_col,
300 edit->curs_line + 1, edit->total_lines + 1, edit->curs1, edit->last_byte);
304 * If we are at the end of file, print <EOF>,
305 * otherwise print the current character as is (if printable),
306 * as decimal and as hex.
308 if (cols > 46)
310 edit_move (32, edit->widget.lines - 1);
311 if (edit->curs1 >= edit->last_byte)
312 tty_print_string ("[<EOF> ]");
313 #ifdef HAVE_CHARSET
314 else if (edit->utf8)
316 unsigned int cur_utf;
317 int cw = 1;
319 cur_utf = edit_get_utf (edit, edit->curs1, &cw);
320 if (cw <= 0)
321 cur_utf = edit_get_byte (edit, edit->curs1);
322 tty_printf ("[%05d 0x%04X]", cur_utf, cur_utf);
324 #endif
325 else
327 unsigned char cur_byte;
329 cur_byte = edit_get_byte (edit, edit->curs1);
330 tty_printf ("[%05d 0x%04X]", (unsigned int) cur_byte, (unsigned int) cur_byte);
335 /* --------------------------------------------------------------------------------------------- */
337 * Draw a frame around edit area.
339 * @param edit editor object
340 * @param color color pair
341 * @param active TRUE if editor object is focused
344 static inline void
345 edit_draw_frame (const WEdit * edit, int color, gboolean active)
347 const Widget *w = (const Widget *) edit;
349 /* draw a frame around edit area */
350 tty_setcolor (color);
351 /* draw double frame for active window if skin supports that */
352 tty_draw_box (w->y, w->x, w->lines, w->cols, !active);
353 /* draw a drag marker */
354 if (edit->drag_state == MCEDIT_DRAG_NORMAL)
356 tty_setcolor (EDITOR_FRAME_DRAG);
357 widget_move (w, w->lines - 1, w->cols - 1);
358 tty_print_alt_char (ACS_LRCORNER, TRUE);
362 /* --------------------------------------------------------------------------------------------- */
364 * Draw a window control buttons.
366 * @param edit editor object
367 * @param color color pair
370 static inline void
371 edit_draw_window_icons (const WEdit * edit, int color)
373 const Widget *w = (const Widget *) edit;
374 char tmp[17];
376 tty_setcolor (color);
377 if (edit->fullscreen)
378 dlg_move (w->owner, 0, w->owner->cols - 6);
379 else
380 widget_move (w, 0, edit->widget.cols - 8);
381 g_snprintf (tmp, sizeof (tmp), "[%s][%s]", edit_window_state_char, edit_window_close_char);
382 tty_print_string (tmp);
385 /* --------------------------------------------------------------------------------------------- */
387 static inline void
388 print_to_widget (WEdit * edit, long row, int start_col, int start_col_real,
389 long end_col, struct line_s line[], char *status, int bookmarked)
391 struct line_s *p;
393 int x = start_col_real;
394 int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
395 int y = row + EDIT_TEXT_VERTICAL_OFFSET;
396 int cols_to_skip = abs (x);
397 int i;
398 int wrap_start;
399 int len;
401 if (!edit->fullscreen)
403 x1++;
404 y++;
407 tty_setcolor (EDITOR_NORMAL_COLOR);
408 if (bookmarked != 0)
409 tty_setcolor (bookmarked);
411 len = end_col + 1 - start_col;
412 wrap_start = option_word_wrap_line_length + edit->start_col;
414 if (len > 0 && edit->widget.y + y >= 0)
416 if (!show_right_margin || wrap_start > end_col)
417 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', len);
418 else if (wrap_start < 0)
420 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
421 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', len);
423 else
425 if (wrap_start > 0)
426 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', wrap_start);
428 len -= wrap_start;
429 if (len > 0)
431 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
432 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1 + wrap_start, ' ', len);
437 if (option_line_state)
439 tty_setcolor (LINE_STATE_COLOR);
440 for (i = 0; i < LINE_STATE_WIDTH; i++)
442 edit_move (x1 + i - option_line_state_width, y);
443 if (status[i] == '\0')
444 status[i] = ' ';
445 tty_print_char (status[i]);
449 edit_move (x1, y);
450 i = 1;
451 for (p = line; p->ch != 0; p++)
453 int style;
454 unsigned int textchar;
455 int color;
457 if (cols_to_skip != 0)
459 cols_to_skip--;
460 continue;
463 style = p->style & 0xFF00;
464 textchar = p->ch;
465 color = p->style >> 16;
467 if (style & MOD_ABNORMAL)
469 /* Non-printable - use black background */
470 color = 0;
473 if (style & MOD_WHITESPACE)
475 if (style & MOD_MARKED)
477 textchar = ' ';
478 tty_setcolor (EDITOR_MARKED_COLOR);
480 else
481 tty_setcolor (EDITOR_WHITESPACE_COLOR);
483 else if (style & MOD_BOLD)
484 tty_setcolor (EDITOR_BOLD_COLOR);
485 else if (style & MOD_MARKED)
486 tty_setcolor (EDITOR_MARKED_COLOR);
487 else
488 tty_lowlevel_setcolor (color);
490 if (show_right_margin)
492 if (i > option_word_wrap_line_length + edit->start_col)
493 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
494 i++;
497 tty_print_anychar (textchar);
501 /* --------------------------------------------------------------------------------------------- */
502 /** b is a pointer to the beginning of the line */
504 static void
505 edit_draw_this_line (WEdit * edit, long b, long row, long start_col, long end_col)
507 struct line_s line[MAX_LINE_LEN];
508 struct line_s *p = line;
510 long m1 = 0, m2 = 0, q, c1, c2;
511 int col, start_col_real;
512 unsigned int c;
513 int color;
514 int abn_style;
515 int i;
516 int utf8lag = 0;
517 unsigned int cur_line = 0;
518 int book_mark = 0;
519 char line_stat[LINE_STATE_WIDTH + 1] = "\0";
521 if (row > edit->widget.lines - 1 - EDIT_TEXT_VERTICAL_OFFSET - 2 * (edit->fullscreen ? 0 : 1))
522 return;
524 if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_COLOR))
525 book_mark = BOOK_MARK_COLOR;
526 else if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_FOUND_COLOR))
527 book_mark = BOOK_MARK_FOUND_COLOR;
529 if (book_mark)
530 abn_style = book_mark << 16;
531 else
532 abn_style = MOD_ABNORMAL;
534 end_col -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
535 if (!edit->fullscreen)
537 const Widget *w = (const Widget *) edit;
539 end_col--;
540 if (w->x + w->cols <= w->owner->cols)
541 end_col--;
544 edit_get_syntax_color (edit, b - 1, &color);
545 q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
546 start_col_real = (col = (int) edit_move_forward3 (edit, b, 0, q)) + edit->start_col;
548 if (option_line_state)
550 cur_line = edit->start_line + row;
551 if (cur_line <= (unsigned int) edit->total_lines)
553 g_snprintf (line_stat, LINE_STATE_WIDTH + 1, "%7i ", cur_line + 1);
555 else
557 memset (line_stat, ' ', LINE_STATE_WIDTH);
558 line_stat[LINE_STATE_WIDTH] = '\0';
560 if (book_mark_query_color (edit, cur_line, BOOK_MARK_COLOR))
562 g_snprintf (line_stat, 2, "*");
566 if (col + 16 > -edit->start_col)
568 eval_marks (edit, &m1, &m2);
570 if (row <= edit->total_lines - edit->start_line)
572 long tws = 0;
573 if (tty_use_colors () && visible_tws)
575 tws = edit_eol (edit, b);
576 while (tws > b && ((c = edit_get_byte (edit, tws - 1)) == ' ' || c == '\t'))
577 tws--;
580 while (col <= end_col - edit->start_col)
582 int cw = 1;
583 int tab_over = 0;
584 gboolean wide_width_char = FALSE;
585 gboolean control_char = FALSE;
587 p->ch = 0;
588 p->style = 0;
589 if (q == edit->curs1)
590 p->style |= MOD_CURSOR;
591 if (q >= m1 && q < m2)
593 if (edit->column_highlight)
595 int x;
596 x = edit_move_forward3 (edit, b, 0, q);
597 c1 = min (edit->column1, edit->column2);
598 c2 = max (edit->column1, edit->column2);
599 if (x >= c1 && x < c2)
600 p->style |= MOD_MARKED;
602 else
603 p->style |= MOD_MARKED;
605 if (q == edit->bracket)
606 p->style |= MOD_BOLD;
607 if (q >= edit->found_start && q < edit->found_start + edit->found_len)
608 p->style |= MOD_BOLD;
610 if (!edit->utf8)
612 c = edit_get_byte (edit, q);
614 else
616 c = edit_get_utf (edit, q, &cw);
618 /* we don't use bg for mc - fg contains both */
619 if (book_mark)
621 p->style |= book_mark << 16;
623 else
625 edit_get_syntax_color (edit, q, &color);
626 p->style |= color << 16;
628 switch (c)
630 case '\n':
631 col = (end_col + utf8lag) - 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 if (!edit->utf8)
765 if ((mc_global.utf8_display && g_unichar_isprint (c)) ||
766 (!mc_global.utf8_display && is_printable (c)))
768 p->ch = c;
769 p++;
771 else
773 p->ch = '.';
774 p->style = abn_style;
775 p++;
778 else
780 if (g_unichar_isprint (c))
782 p->ch = c;
783 p++;
785 else
787 p->ch = '.';
788 p->style = abn_style;
789 p++;
792 col++;
793 break;
794 } /* case */
796 q++;
797 if (cw > 1)
799 q += cw - 1;
802 if (col > (end_col - edit->start_col + 1))
804 if (wide_width_char)
806 p--;
807 break;
809 if (control_char)
811 p -= 2;
812 break;
818 else
820 start_col_real = start_col = 0;
823 p->ch = 0;
825 print_to_widget (edit, row, start_col, start_col_real, end_col, line, line_stat, book_mark);
828 /* --------------------------------------------------------------------------------------------- */
830 static inline void
831 edit_draw_this_char (WEdit * edit, long curs, long row, long start_column, long end_column)
833 int 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 long prev_curs = 0;
846 Widget *w = (Widget *) edit;
847 Dlg_head *h = w->owner;
849 long row = 0, curs_row;
850 int force = edit->force;
851 long b;
852 int y1, x1, y2, x2;
854 int last_line;
855 int last_column;
857 /* draw only visible region */
859 last_line = h->y + h->lines - 1;
861 y1 = w->y;
862 if (y1 > last_line - 1 /* buttonbar */ )
863 return;
865 last_column = h->x + h->cols - 1;
867 x1 = w->x;
868 if (x1 > last_column)
869 return;
871 y2 = w->y + w->lines - 1;
872 if (y2 < h->y + 1 /* menubar */ )
873 return;
875 x2 = w->x + w->cols - 1;
876 if (x2 < h->x)
877 return;
879 if ((force & REDRAW_IN_BOUNDS) == 0)
881 /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
882 /* draw only visible region */
884 if (y2 <= last_line - 1 /* buttonbar */ )
885 end_row = w->lines - 1;
886 else if (y1 >= h->y + 1 /* menubar */ )
887 end_row = h->lines - 1 - y1 - 1;
888 else
889 end_row = start_row + h->lines - 1 - 1;
891 if (x2 <= last_column)
892 end_column = w->cols - 1;
893 else if (x1 >= h->x)
894 end_column = h->cols - 1 - x1;
895 else
896 end_column = start_column + h->cols - 1;
900 * If the position of the page has not moved then we can draw the cursor
901 * character only. This will prevent line flicker when using arrow keys.
903 if ((force & REDRAW_CHAR_ONLY) == 0 || (force & REDRAW_PAGE) != 0)
905 if ((force & REDRAW_PAGE) != 0)
907 row = start_row;
908 b = edit_move_forward (edit, edit->start_display, start_row, 0);
909 while (row <= end_row)
911 if (key_pending (edit))
912 return;
913 edit_draw_this_line (edit, b, row, start_column, end_column);
914 b = edit_move_forward (edit, b, 1, 0);
915 row++;
918 else
920 curs_row = edit->curs_row;
922 if ((force & REDRAW_BEFORE_CURSOR) != 0 && start_row < curs_row)
924 long upto = curs_row - 1 <= end_row ? curs_row - 1 : end_row;
926 row = start_row;
927 b = edit->start_display;
928 while (row <= upto)
930 if (key_pending (edit))
931 return;
932 edit_draw_this_line (edit, b, row, start_column, end_column);
933 b = edit_move_forward (edit, b, 1, 0);
937 /* if (force & REDRAW_LINE) ---> default */
938 b = edit_bol (edit, edit->curs1);
939 if (curs_row >= start_row && curs_row <= end_row)
941 if (key_pending (edit))
942 return;
943 edit_draw_this_line (edit, b, curs_row, start_column, end_column);
946 if ((force & REDRAW_AFTER_CURSOR) != 0 && end_row > curs_row)
948 row = curs_row + 1 < start_row ? start_row : curs_row + 1;
949 b = edit_move_forward (edit, b, 1, 0);
950 while (row <= end_row)
952 if (key_pending (edit))
953 return;
954 edit_draw_this_line (edit, b, row, start_column, end_column);
955 b = edit_move_forward (edit, b, 1, 0);
956 row++;
960 if ((force & REDRAW_LINE_ABOVE) != 0 && curs_row >= 1)
962 row = curs_row - 1;
963 b = edit_move_backward (edit, edit_bol (edit, edit->curs1), 1);
964 if (row >= start_row && row <= end_row)
966 if (key_pending (edit))
967 return;
968 edit_draw_this_line (edit, b, row, start_column, end_column);
972 if ((force & REDRAW_LINE_BELOW) != 0 && row < edit->widget.lines - 1)
974 row = curs_row + 1;
975 b = edit_bol (edit, edit->curs1);
976 b = edit_move_forward (edit, b, 1, 0);
977 if (row >= start_row && row <= end_row)
979 if (key_pending (edit))
980 return;
981 edit_draw_this_line (edit, b, row, start_column, end_column);
986 else if (prev_curs_row < edit->curs_row)
988 /* with the new text highlighting, we must draw from the top down */
989 edit_draw_this_char (edit, prev_curs, prev_curs_row, start_column, end_column);
990 edit_draw_this_char (edit, edit->curs1, edit->curs_row, start_column, end_column);
992 else
994 edit_draw_this_char (edit, edit->curs1, edit->curs_row, start_column, end_column);
995 edit_draw_this_char (edit, prev_curs, prev_curs_row, start_column, end_column);
998 edit->force = 0;
1000 prev_curs_row = edit->curs_row;
1001 prev_curs = edit->curs1;
1004 /* --------------------------------------------------------------------------------------------- */
1006 static inline void
1007 edit_render (WEdit * edit, int page, int row_start, int col_start, int row_end, int col_end)
1009 if (page) /* if it was an expose event, 'page' would be set */
1010 edit->force |= REDRAW_PAGE | REDRAW_IN_BOUNDS;
1012 render_edit_text (edit, row_start, col_start, row_end, col_end);
1015 * edit->force != 0 means a key was pending and the redraw
1016 * was halted, so next time we must redraw everything in case stuff
1017 * was left undrawn from a previous key press.
1019 if (edit->force)
1020 edit->force |= REDRAW_PAGE;
1023 /* --------------------------------------------------------------------------------------------- */
1024 /*** public functions ****************************************************************************/
1025 /* --------------------------------------------------------------------------------------------- */
1027 void
1028 edit_status (WEdit * edit, gboolean active)
1030 int color;
1032 if (edit->fullscreen)
1034 color = STATUSBAR_COLOR;
1035 edit_status_fullscreen (edit, color);
1037 else
1039 color = edit->drag_state != MCEDIT_DRAG_NORMAL ? EDITOR_FRAME_DRAG : active ?
1040 EDITOR_FRAME_ACTIVE : EDITOR_FRAME;
1041 edit_draw_frame (edit, color, active);
1042 edit_status_window (edit);
1045 edit_draw_window_icons (edit, color);
1048 /* --------------------------------------------------------------------------------------------- */
1050 /** this scrolls the text so that cursor is on the screen */
1051 void
1052 edit_scroll_screen_over_cursor (WEdit * edit)
1054 int p;
1055 int outby;
1056 int b_extreme, t_extreme, l_extreme, r_extreme;
1058 if (edit->widget.lines <= 0 || edit->widget.cols <= 0)
1059 return;
1061 edit->widget.lines -= EDIT_TEXT_VERTICAL_OFFSET;
1062 edit->widget.cols -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
1064 if (!edit->fullscreen)
1066 edit->widget.x++;
1067 edit->widget.cols -= 2;
1068 edit->widget.y++;
1069 edit->widget.lines -= 2;
1072 r_extreme = EDIT_RIGHT_EXTREME;
1073 l_extreme = EDIT_LEFT_EXTREME;
1074 b_extreme = EDIT_BOTTOM_EXTREME;
1075 t_extreme = EDIT_TOP_EXTREME;
1076 if (edit->found_len)
1078 b_extreme = max (edit->widget.lines / 4, b_extreme);
1079 t_extreme = max (edit->widget.lines / 4, t_extreme);
1081 if (b_extreme + t_extreme + 1 > edit->widget.lines)
1083 int n;
1085 n = b_extreme + t_extreme;
1086 if (n == 0)
1087 n = 1;
1088 b_extreme = (b_extreme * (edit->widget.lines - 1)) / n;
1089 t_extreme = (t_extreme * (edit->widget.lines - 1)) / n;
1091 if (l_extreme + r_extreme + 1 > edit->widget.cols)
1093 int n;
1095 n = l_extreme + t_extreme;
1096 if (n == 0)
1097 n = 1;
1098 l_extreme = (l_extreme * (edit->widget.cols - 1)) / n;
1099 r_extreme = (r_extreme * (edit->widget.cols - 1)) / n;
1101 p = edit_get_col (edit) + edit->over_col;
1102 edit_update_curs_row (edit);
1103 outby = p + edit->start_col - edit->widget.cols + 1 + (r_extreme + edit->found_len);
1104 if (outby > 0)
1105 edit_scroll_right (edit, outby);
1106 outby = l_extreme - p - edit->start_col;
1107 if (outby > 0)
1108 edit_scroll_left (edit, outby);
1109 p = edit->curs_row;
1110 outby = p - edit->widget.lines + 1 + b_extreme;
1111 if (outby > 0)
1112 edit_scroll_downward (edit, outby);
1113 outby = t_extreme - p;
1114 if (outby > 0)
1115 edit_scroll_upward (edit, outby);
1116 edit_update_curs_row (edit);
1118 edit->widget.lines += EDIT_TEXT_VERTICAL_OFFSET;
1119 edit->widget.cols += EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
1120 if (!edit->fullscreen)
1122 edit->widget.x--;
1123 edit->widget.cols += 2;
1124 edit->widget.y--;
1125 edit->widget.lines += 2;
1129 /* --------------------------------------------------------------------------------------------- */
1131 void
1132 edit_render_keypress (WEdit * edit)
1134 edit_render (edit, 0, 0, 0, 0, 0);
1137 /* --------------------------------------------------------------------------------------------- */