Ticket #2876: code cleanup before 4.8.6 release.
[midnight-commander.git] / src / editor / editdraw.c
blob2d119f6d1797f8ae8b35b24da2291238b922935a
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
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, (long) edit->curs1, (long) edit->last_byte, byte_str,
181 #ifdef HAVE_CHARSET
182 mc_global.source_codepage >= 0 ? get_codepage_id (mc_global.source_codepage) :
183 #endif
188 /* --------------------------------------------------------------------------------------------- */
190 * Draw the status line at the top of the screen for fullscreen editor window.
192 * @param edit editor object
193 * @param color color pair
196 static inline void
197 edit_status_fullscreen (WEdit * edit, int color)
199 const int w = edit->widget.owner->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)
213 fname = x_basename (vfs_path_get_last_path_str (edit->filename_vpath));
215 fname_len = str_term_width1 (fname);
216 if (fname_len < preferred_fname_len)
217 fname_len = preferred_fname_len;
219 if (fname_len + gap + status_len + right_gap >= w)
221 if (preferred_fname_len + gap + status_len + right_gap >= w)
222 fname_len = preferred_fname_len;
223 else
224 fname_len = w - (gap + status_len + right_gap);
225 fname = str_trunc (fname, fname_len);
228 dlg_move (edit->widget.owner, 0, 0);
229 tty_setcolor (color);
230 printwstr (fname, fname_len + gap);
231 printwstr (status, w - (fname_len + gap));
233 if (simple_statusbar && w > EDITOR_MINIMUM_TERMINAL_WIDTH)
235 size_t percent = 100;
237 if (edit->total_lines + 1 != 0)
238 percent = (edit->curs_line + 1) * 100 / (edit->total_lines + 1);
239 dlg_move (edit->widget.owner, 0, w - 6 - 6);
240 tty_printf (" %3d%%", percent);
243 g_free (status);
246 /* --------------------------------------------------------------------------------------------- */
248 * Draw status line for editor window if window is not in fullscreen mode.
250 * @param edit editor object
253 static inline void
254 edit_status_window (WEdit * edit)
256 int y, x;
257 int cols = edit->widget.cols;
259 tty_setcolor (STATUSBAR_COLOR);
261 if (cols > 5)
263 const char *fname = N_("NoName");
264 char *full_fname = NULL;
266 if (edit->filename_vpath != NULL)
268 full_fname = vfs_path_to_str (edit->filename_vpath);
269 fname = x_basename (full_fname);
271 #ifdef ENABLE_NLS
272 else
273 fname = _(fname);
274 #endif
276 edit_move (2, 0);
277 tty_printf ("[%s]", str_term_trim (fname, edit->widget.cols - 8 - 6));
278 g_free (full_fname);
281 tty_getyx (&y, &x);
282 x -= edit->widget.x;
283 x += 4;
284 if (x + 6 <= cols - 2 - 6)
286 edit_move (x, 0);
287 tty_printf ("[%c%c%c%c]",
288 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
289 edit->modified ? 'M' : '-',
290 macro_index < 0 ? '-' : 'R', edit->overwrite == 0 ? '-' : 'O');
293 if (cols > 30)
295 edit_move (2, edit->widget.lines - 1);
296 tty_printf ("%3ld %5ld/%ld %6ld/%ld",
297 edit->curs_col + edit->over_col,
298 edit->curs_line + 1, edit->total_lines + 1, edit->curs1, edit->last_byte);
302 * If we are at the end of file, print <EOF>,
303 * otherwise print the current character as is (if printable),
304 * as decimal and as hex.
306 if (cols > 46)
308 edit_move (32, edit->widget.lines - 1);
309 if (edit->curs1 >= edit->last_byte)
310 tty_print_string ("[<EOF> ]");
311 #ifdef HAVE_CHARSET
312 else if (edit->utf8)
314 unsigned int cur_utf;
315 int cw = 1;
317 cur_utf = edit_get_utf (edit, edit->curs1, &cw);
318 if (cw <= 0)
319 cur_utf = edit_get_byte (edit, edit->curs1);
320 tty_printf ("[%05d 0x%04X]", cur_utf, cur_utf);
322 #endif
323 else
325 unsigned char cur_byte;
327 cur_byte = edit_get_byte (edit, edit->curs1);
328 tty_printf ("[%05d 0x%04X]", (unsigned int) cur_byte, (unsigned int) cur_byte);
333 /* --------------------------------------------------------------------------------------------- */
335 * Draw a frame around edit area.
337 * @param edit editor object
338 * @param color color pair
339 * @param active TRUE if editor object is focused
342 static inline void
343 edit_draw_frame (const WEdit * edit, int color, gboolean active)
345 const Widget *w = (const Widget *) edit;
347 /* draw a frame around edit area */
348 tty_setcolor (color);
349 /* draw double frame for active window if skin supports that */
350 tty_draw_box (w->y, w->x, w->lines, w->cols, !active);
351 /* draw a drag marker */
352 if (edit->drag_state == MCEDIT_DRAG_NORMAL)
354 tty_setcolor (EDITOR_FRAME_DRAG);
355 widget_move (w, w->lines - 1, w->cols - 1);
356 tty_print_alt_char (ACS_LRCORNER, TRUE);
360 /* --------------------------------------------------------------------------------------------- */
362 * Draw a window control buttons.
364 * @param edit editor object
365 * @param color color pair
368 static inline void
369 edit_draw_window_icons (const WEdit * edit, int color)
371 const Widget *w = (const Widget *) edit;
372 char tmp[17];
374 tty_setcolor (color);
375 if (edit->fullscreen)
376 dlg_move (w->owner, 0, w->owner->cols - 6);
377 else
378 widget_move (w, 0, edit->widget.cols - 8);
379 g_snprintf (tmp, sizeof (tmp), "[%s][%s]", edit_window_state_char, edit_window_close_char);
380 tty_print_string (tmp);
383 /* --------------------------------------------------------------------------------------------- */
385 static inline void
386 print_to_widget (WEdit * edit, long row, int start_col, int start_col_real,
387 long end_col, struct line_s line[], char *status, int bookmarked)
389 struct line_s *p;
391 int x = start_col_real;
392 int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
393 int y = row + EDIT_TEXT_VERTICAL_OFFSET;
394 int cols_to_skip = abs (x);
395 int i;
396 int wrap_start;
397 int len;
399 if (!edit->fullscreen)
401 x1++;
402 y++;
405 tty_setcolor (EDITOR_NORMAL_COLOR);
406 if (bookmarked != 0)
407 tty_setcolor (bookmarked);
409 len = end_col + 1 - start_col;
410 wrap_start = option_word_wrap_line_length + edit->start_col;
412 if (len > 0 && edit->widget.y + y >= 0)
414 if (!show_right_margin || wrap_start > end_col)
415 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', len);
416 else if (wrap_start < 0)
418 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
419 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', len);
421 else
423 if (wrap_start > 0)
424 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', wrap_start);
426 len -= wrap_start;
427 if (len > 0)
429 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
430 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1 + wrap_start, ' ', len);
435 if (option_line_state)
437 tty_setcolor (LINE_STATE_COLOR);
438 for (i = 0; i < LINE_STATE_WIDTH; i++)
440 edit_move (x1 + i - option_line_state_width, y);
441 if (status[i] == '\0')
442 status[i] = ' ';
443 tty_print_char (status[i]);
447 edit_move (x1, y);
448 i = 1;
449 for (p = line; p->ch != 0; p++)
451 int style;
452 unsigned int textchar;
453 int color;
455 if (cols_to_skip != 0)
457 cols_to_skip--;
458 continue;
461 style = p->style & 0xFF00;
462 textchar = p->ch;
463 color = p->style >> 16;
465 if (style & MOD_ABNORMAL)
467 /* Non-printable - use black background */
468 color = 0;
471 if (style & MOD_WHITESPACE)
473 if (style & MOD_MARKED)
475 textchar = ' ';
476 tty_setcolor (EDITOR_MARKED_COLOR);
478 else
479 tty_setcolor (EDITOR_WHITESPACE_COLOR);
481 else if (style & MOD_BOLD)
482 tty_setcolor (EDITOR_BOLD_COLOR);
483 else if (style & MOD_MARKED)
484 tty_setcolor (EDITOR_MARKED_COLOR);
485 else
486 tty_lowlevel_setcolor (color);
488 if (show_right_margin)
490 if (i > option_word_wrap_line_length + edit->start_col)
491 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
492 i++;
495 tty_print_anychar (textchar);
499 /* --------------------------------------------------------------------------------------------- */
500 /** b is a pointer to the beginning of the line */
502 static void
503 edit_draw_this_line (WEdit * edit, off_t b, long row, long start_col, long end_col)
505 struct line_s line[MAX_LINE_LEN];
506 struct line_s *p = line;
508 off_t m1 = 0, m2 = 0, q;
509 long c1, c2;
510 int col, start_col_real;
511 unsigned int c;
512 int color;
513 int abn_style;
514 int i;
515 unsigned int cur_line = 0;
516 int book_mark = 0;
517 char line_stat[LINE_STATE_WIDTH + 1] = "\0";
519 if (row > edit->widget.lines - 1 - EDIT_TEXT_VERTICAL_OFFSET - 2 * (edit->fullscreen ? 0 : 1))
520 return;
522 if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_COLOR))
523 book_mark = BOOK_MARK_COLOR;
524 else if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_FOUND_COLOR))
525 book_mark = BOOK_MARK_FOUND_COLOR;
527 if (book_mark)
528 abn_style = book_mark << 16;
529 else
530 abn_style = MOD_ABNORMAL;
532 end_col -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
533 if (!edit->fullscreen)
535 const Widget *w = (const Widget *) edit;
537 end_col--;
538 if (w->x + w->cols <= w->owner->cols)
539 end_col--;
542 edit_get_syntax_color (edit, b - 1, &color);
543 q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
544 start_col_real = (col = (int) edit_move_forward3 (edit, b, 0, q)) + edit->start_col;
546 if (option_line_state)
548 cur_line = edit->start_line + row;
549 if (cur_line <= (unsigned int) edit->total_lines)
551 g_snprintf (line_stat, LINE_STATE_WIDTH + 1, "%7i ", cur_line + 1);
553 else
555 memset (line_stat, ' ', LINE_STATE_WIDTH);
556 line_stat[LINE_STATE_WIDTH] = '\0';
558 if (book_mark_query_color (edit, cur_line, BOOK_MARK_COLOR))
560 g_snprintf (line_stat, 2, "*");
564 if (col + 16 > -edit->start_col)
566 eval_marks (edit, &m1, &m2);
568 if (row <= edit->total_lines - edit->start_line)
570 off_t tws = 0;
571 if (tty_use_colors () && visible_tws)
573 tws = edit_eol (edit, b);
574 while (tws > b && ((c = edit_get_byte (edit, tws - 1)) == ' ' || c == '\t'))
575 tws--;
578 while (col <= end_col - edit->start_col)
580 int cw = 1;
581 int tab_over = 0;
582 gboolean wide_width_char = FALSE;
583 gboolean control_char = FALSE;
585 p->ch = 0;
586 p->style = 0;
587 if (q == edit->curs1)
588 p->style |= MOD_CURSOR;
589 if (q >= m1 && q < m2)
591 if (edit->column_highlight)
593 long x;
595 x = (long) edit_move_forward3 (edit, b, 0, q);
596 c1 = min (edit->column1, edit->column2);
597 c2 = max (edit->column1, edit->column2);
598 if (x >= c1 && x < c2)
599 p->style |= MOD_MARKED;
601 else
602 p->style |= MOD_MARKED;
604 if (q == edit->bracket)
605 p->style |= MOD_BOLD;
606 if (q >= edit->found_start && q < (off_t) (edit->found_start + edit->found_len))
607 p->style |= MOD_BOLD;
609 #ifdef HAVE_CHARSET
610 if (edit->utf8)
612 c = edit_get_utf (edit, q, &cw);
614 else
615 #endif
617 c = edit_get_byte (edit, q);
619 /* we don't use bg for mc - fg contains both */
620 if (book_mark)
622 p->style |= book_mark << 16;
624 else
626 edit_get_syntax_color (edit, q, &color);
627 p->style |= color << 16;
629 switch (c)
631 case '\n':
632 col = end_col - edit->start_col + 1; /* quit */
633 break;
634 case '\t':
635 i = TAB_SIZE - ((int) col % TAB_SIZE);
636 tab_over = (end_col - edit->start_col) - (col + i - 1);
637 if (tab_over < 0)
638 i += tab_over;
639 col += i;
640 if (tty_use_colors () &&
641 ((visible_tabs || (visible_tws && q >= tws)) && enable_show_tabs_tws))
643 if (p->style & MOD_MARKED)
644 c = p->style;
645 else if (book_mark)
646 c |= book_mark << 16;
647 else
648 c = p->style | MOD_WHITESPACE;
649 if (i > 2)
651 p->ch = '<';
652 p->style = c;
653 p++;
654 while (--i > 1)
656 p->ch = '-';
657 p->style = c;
658 p++;
660 p->ch = '>';
661 p->style = c;
662 p++;
664 else if (i > 1)
666 p->ch = '<';
667 p->style = c;
668 p++;
669 p->ch = '>';
670 p->style = c;
671 p++;
673 else
675 p->ch = '>';
676 p->style = c;
677 p++;
680 else if (tty_use_colors () && visible_tws && q >= tws && enable_show_tabs_tws)
682 p->ch = '.';
683 p->style |= MOD_WHITESPACE;
684 c = p->style & ~MOD_CURSOR;
685 p++;
686 while (--i)
688 p->ch = ' ';
689 p->style = c;
690 p++;
693 else
695 p->ch |= ' ';
696 c = p->style & ~MOD_CURSOR;
697 p++;
698 while (--i)
700 p->ch = ' ';
701 p->style = c;
702 p++;
705 break;
706 case ' ':
707 if (tty_use_colors () && visible_tws && q >= tws && enable_show_tabs_tws)
709 p->ch = '.';
710 p->style |= MOD_WHITESPACE;
711 p++;
712 col++;
713 break;
715 /* fallthrough */
716 default:
717 #ifdef HAVE_CHARSET
718 if (mc_global.utf8_display)
720 if (!edit->utf8)
722 c = convert_from_8bit_to_utf_c ((unsigned char) c, edit->converter);
724 else
726 if (g_unichar_iswide (c))
728 wide_width_char = TRUE;
729 col++;
733 else if (edit->utf8)
734 c = convert_from_utf_to_current_c (c, edit->converter);
735 else
736 c = convert_to_display_c (c);
737 #endif
739 /* Caret notation for control characters */
740 if (c < 32)
742 p->ch = '^';
743 p->style = abn_style;
744 p++;
745 p->ch = c + 0x40;
746 p->style = abn_style;
747 p++;
748 col += 2;
749 control_char = TRUE;
750 break;
752 if (c == 127)
754 p->ch = '^';
755 p->style = abn_style;
756 p++;
757 p->ch = '?';
758 p->style = abn_style;
759 p++;
760 col += 2;
761 control_char = TRUE;
762 break;
764 #ifdef HAVE_CHARSET
765 if (edit->utf8)
767 if (g_unichar_isprint (c))
768 p->ch = c;
769 else
771 p->ch = '.';
772 p->style = abn_style;
774 p++;
776 else
777 #endif
779 if ((mc_global.utf8_display && g_unichar_isprint (c)) ||
780 (!mc_global.utf8_display && is_printable (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, off_t curs, long row, long start_column, long end_column)
833 off_t b = edit_bol (edit, curs);
835 edit_draw_this_line (edit, b, row, start_column, end_column);
838 /* --------------------------------------------------------------------------------------------- */
839 /** cursor must be in screen for other than REDRAW_PAGE passed in force */
841 static inline void
842 render_edit_text (WEdit * edit, long start_row, long start_column, long end_row, long end_column)
844 static long prev_curs_row = 0;
845 static off_t prev_curs = 0;
847 Widget *w = (Widget *) edit;
848 Dlg_head *h = w->owner;
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 = h->y + h->lines - 1;
862 y1 = w->y;
863 if (y1 > last_line - 1 /* buttonbar */ )
864 return;
866 last_column = h->x + h->cols - 1;
868 x1 = w->x;
869 if (x1 > last_column)
870 return;
872 y2 = w->y + w->lines - 1;
873 if (y2 < h->y + 1 /* menubar */ )
874 return;
876 x2 = w->x + w->cols - 1;
877 if (x2 < h->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 >= h->y + 1 /* menubar */ )
888 end_row = h->lines - 1 - y1 - 1;
889 else
890 end_row = start_row + h->lines - 1 - 1;
892 if (x2 <= last_column)
893 end_column = w->cols - 1;
894 else if (x1 >= h->x)
895 end_column = h->cols - 1 - x1;
896 else
897 end_column = start_column + h->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 /* --------------------------------------------------------------------------------------------- */