Add CK_Close action to close current open file.
[midnight-commander.git] / src / editor / editwidget.c
blob38571b5d7f238354a4ed6dd62f4e55c48ecfc10d
1 /*
2 Editor initialisation and callback handler.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007,2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer, 1996, 1997
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file
28 * \brief Source: editor initialisation and callback handler
29 * \author Paul Sheer
30 * \date 1996, 1997
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <sys/stat.h>
43 #include <stdlib.h>
45 #include "lib/global.h"
47 #include "lib/tty/tty.h" /* LINES, COLS */
48 #include "lib/tty/key.h" /* is_idle() */
49 #include "lib/tty/color.h" /* tty_setcolor() */
50 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
51 #include "lib/strutil.h" /* str_term_trim() */
52 #include "lib/util.h" /* mc_build_filename() */
53 #include "lib/widget.h"
54 #include "lib/mcconfig.h"
55 #include "lib/event.h" /* mc_event_raise() */
57 #include "src/keybind-defaults.h"
58 #include "src/main.h" /* home_dir */
59 #include "src/filemanager/cmd.h" /* view_other_cmd(), save_setup_cmd() */
60 #include "src/learn.h" /* learn_keys() */
62 #include "edit-impl.h"
63 #include "editwidget.h"
65 /*** global variables ****************************************************************************/
67 /*** file scope macro definitions ****************************************************************/
69 #define WINDOW_MIN_LINES (2 + 2)
70 #define WINDOW_MIN_COLS (2 + LINE_STATE_WIDTH)
72 /*** file scope type declarations ****************************************************************/
74 /*** file scope variables ************************************************************************/
76 /*** file scope functions ************************************************************************/
78 static cb_ret_t edit_callback (Widget * w, widget_msg_t msg, int parm);
80 /* --------------------------------------------------------------------------------------------- */
81 /**
82 * Show info about editor
85 static void
86 edit_about (void)
88 const char *header = N_("About");
89 const char *button_name = N_("&OK");
90 const char *const version = "MCEdit " VERSION;
91 char text[BUF_LARGE];
93 int win_len, version_len, button_len;
94 int cols, lines;
96 Dlg_head *about_dlg;
98 #ifdef ENABLE_NLS
99 header = _(header);
100 button_name = _(button_name);
101 #endif
103 button_len = str_term_width1 (button_name) + 5;
104 version_len = str_term_width1 (version);
106 g_snprintf (text, sizeof (text),
107 _("Copyright (C) 1996-2012 the Free Software Foundation\n\n"
108 " A user friendly text editor\n"
109 " written for the Midnight Commander"));
111 win_len = str_term_width1 (header);
112 win_len = max (win_len, version_len);
113 win_len = max (win_len, button_len);
115 /* count width and height of text */
116 str_msg_term_size (text, &lines, &cols);
117 lines += 9;
118 cols = max (win_len, cols) + 6;
120 /* dialog */
121 about_dlg = create_dlg (TRUE, 0, 0, lines, cols, dialog_colors, NULL, NULL,
122 "[Internal File Editor]", header, DLG_CENTER | DLG_TRYUP);
124 add_widget (about_dlg, label_new (3, (cols - version_len) / 2, version));
125 add_widget (about_dlg, label_new (5, 3, text));
126 add_widget (about_dlg, button_new (lines - 3, (cols - button_len) / 2,
127 B_ENTER, NORMAL_BUTTON, button_name, NULL));
129 run_dlg (about_dlg);
130 destroy_dlg (about_dlg);
133 /* --------------------------------------------------------------------------------------------- */
135 * Show a help window
138 static void
139 edit_help (void)
141 ev_help_t event_data = { NULL, "[Internal File Editor]" };
142 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
145 /* --------------------------------------------------------------------------------------------- */
147 * Restore saved window size.
149 * @param edit editor object
152 static void
153 edit_restore_size (WEdit * edit)
155 edit->drag_state = MCEDIT_DRAG_NORMAL;
156 widget_set_size ((Widget *) edit, edit->y_prev, edit->x_prev,
157 edit->lines_prev, edit->cols_prev);
158 dlg_redraw (((Widget *) edit)->owner);
161 /* --------------------------------------------------------------------------------------------- */
163 * Move window by one row or column in any direction.
165 * @param edit editor object
166 * @param command direction (CK_Up, CK_Down, CK_Left, CK_Right)
169 static void
170 edit_window_move (WEdit * edit, unsigned long command)
172 Widget *w = (Widget *) edit;
173 Dlg_head *h = w->owner;
175 switch (command)
177 case CK_Up:
178 if (w->y > h->y + 1) /* menubar */
179 w->y--;
180 break;
181 case CK_Down:
182 if (w->y < h->y + h->lines - 2) /* buttonbar */
183 w->y++;
184 break;
185 case CK_Left:
186 if (w->x + w->cols > h->x)
187 w->x--;
188 break;
189 case CK_Right:
190 if (w->x < h->x + h->cols)
191 w->x++;
192 break;
193 default:
194 return;
197 edit->force |= REDRAW_PAGE;
198 dlg_redraw (h);
201 /* --------------------------------------------------------------------------------------------- */
203 * Resize window by one row or column in any direction.
205 * @param edit editor object
206 * @param command direction (CK_Up, CK_Down, CK_Left, CK_Right)
209 static void
210 edit_window_resize (WEdit * edit, unsigned long command)
212 Widget *w = (Widget *) edit;
213 Dlg_head *h = w->owner;
215 switch (command)
217 case CK_Up:
218 if (w->lines > WINDOW_MIN_LINES)
219 w->lines--;
220 break;
221 case CK_Down:
222 if (w->y + w->lines < h->y + h->lines - 1) /* buttonbar */
223 w->lines++;
224 break;
225 case CK_Left:
226 if (w->cols > WINDOW_MIN_COLS)
227 w->cols--;
228 break;
229 case CK_Right:
230 if (w->x + w->cols < h->x + h->cols)
231 w->cols++;
232 break;
233 default:
234 return;
237 edit->force |= REDRAW_COMPLETELY;
238 dlg_redraw (h);
241 /* --------------------------------------------------------------------------------------------- */
243 static char *
244 edit_get_shortcut (unsigned long command)
246 const char *ext_map;
247 const char *shortcut = NULL;
249 shortcut = keybind_lookup_keymap_shortcut (editor_map, command);
250 if (shortcut != NULL)
251 return g_strdup (shortcut);
253 ext_map = keybind_lookup_keymap_shortcut (editor_map, CK_ExtendedKeyMap);
254 if (ext_map != NULL)
255 shortcut = keybind_lookup_keymap_shortcut (editor_x_map, command);
256 if (shortcut != NULL)
257 return g_strdup_printf ("%s %s", ext_map, shortcut);
259 return NULL;
262 /* --------------------------------------------------------------------------------------------- */
264 static char *
265 edit_get_title (const Dlg_head * h, size_t len)
267 const WEdit *edit = find_editor (h);
268 const char *modified = edit->modified ? "(*) " : " ";
269 const char *file_label;
270 char *filename;
272 len -= 4;
274 filename = vfs_path_to_str (edit->filename_vpath);
275 file_label = str_term_trim (filename, len - str_term_width1 (_("Edit: ")));
276 g_free (filename);
278 return g_strconcat (_("Edit: "), modified, file_label, (char *) NULL);
281 /* --------------------------------------------------------------------------------------------- */
283 static int
284 edit_event (Gpm_Event * event, void *data)
286 WEdit *edit = (WEdit *) data;
287 Widget *w = (Widget *) data;
288 Gpm_Event local;
290 if (!mouse_global_in_widget (event, w))
291 return MOU_UNHANDLED;
293 local = mouse_get_local (event, w);
295 /* Unknown event type */
296 if ((event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)) == 0)
297 return MOU_NORMAL;
299 edit_update_curs_row (edit);
300 edit_update_curs_col (edit);
302 if (!EDIT_WITH_FRAME || (local.buttons & GPM_B_LEFT) == 0 || (local.type & GPM_UP) != 0)
303 edit->drag_state = MCEDIT_DRAG_NORMAL;
304 else if (local.y == 1 && edit->drag_state != MCEDIT_DRAG_RESIZE)
306 /* click on the top line (move) */
308 /* start move; save x coordinate of mouse */
309 if ((local.type & GPM_DOWN) != 0)
310 edit->drag_state_start = local.x;
312 /* moving */
313 if ((local.type & (GPM_DOWN | GPM_DRAG)) != 0)
314 edit->drag_state = MCEDIT_DRAG_MOVE;
316 else if (local.y == w->lines && local.x == w->cols)
318 /* click on bottom-right corner (resize) */
319 if ((local.type & (GPM_DOWN | GPM_DRAG)) != 0)
320 edit->drag_state = MCEDIT_DRAG_RESIZE;
323 if (edit->drag_state == MCEDIT_DRAG_NORMAL)
325 gboolean done = TRUE;
327 /* Double click */
328 if ((local.type & (GPM_DOUBLE | GPM_UP)) == (GPM_UP | GPM_DOUBLE))
330 edit_mark_current_word_cmd (edit);
331 goto update;
333 #if 0
334 /* Triple click */
335 if ((local.type & (GPM_TRIPLE | GPM_UP)) == (GPM_UP | GPM_TRIPLE))
337 edit_mark_current_line_cmd (edit);
338 goto update;
340 #endif
341 /* Wheel events */
342 if ((local.buttons & GPM_B_UP) != 0 && (local.type & GPM_DOWN) != 0)
344 edit_move_up (edit, 2, 1);
345 goto update;
347 if ((local.buttons & GPM_B_DOWN) != 0 && (local.type & GPM_DOWN) != 0)
349 edit_move_down (edit, 2, 1);
350 goto update;
353 /* continue handle current event */
354 goto cont;
356 /* handle DRAG mouse event, don't use standard way to continue
357 * event handling outside of widget */
360 int c;
362 c = tty_get_event (event, FALSE, TRUE);
363 if (c == EV_NONE || c != EV_MOUSE)
364 break;
366 local = mouse_get_local (event, w);
368 cont:
369 /* A lone up mustn't do anything */
370 if (edit->mark2 != -1 && (local.type & (GPM_UP | GPM_DRAG)) != 0)
371 return MOU_NORMAL;
373 if ((local.type & (GPM_DOWN | GPM_UP)) != 0)
374 edit_push_key_press (edit);
376 local.x--;
377 if (!option_cursor_beyond_eol)
378 edit->prev_col = local.x - edit->start_col - option_line_state_width - 1;
379 else
381 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
382 edit_eol (edit, edit->curs1));
384 if (local.x > line_len)
386 edit->over_col =
387 local.x - line_len - edit->start_col - option_line_state_width - 1;
388 edit->prev_col = line_len;
390 else
392 edit->over_col = 0;
393 edit->prev_col = local.x - option_line_state_width - edit->start_col - 1;
397 if (EDIT_WITH_FRAME)
398 local.y--;
399 if (local.y > (edit->curs_row + 1))
400 edit_move_down (edit, local.y - (edit->curs_row + 1), 0);
401 else if (local.y < (edit->curs_row + 1))
402 edit_move_up (edit, (edit->curs_row + 1) - local.y, 0);
403 else
404 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
406 if ((local.type & GPM_DOWN) != 0)
408 edit_mark_cmd (edit, 1); /* reset */
409 edit->highlight = 0;
412 done = (local.type & GPM_DRAG) == 0;
413 if (done)
414 edit_mark_cmd (edit, 0);
416 update:
417 edit_find_bracket (edit);
418 edit->force |= REDRAW_COMPLETELY;
419 edit_update_curs_row (edit);
420 edit_update_curs_col (edit);
421 edit_update_screen (edit);
423 while (EDIT_WITH_FRAME && !done);
425 else
426 while (edit->drag_state != MCEDIT_DRAG_NORMAL)
428 int c;
430 c = tty_get_event (event, FALSE, TRUE);
432 if (c == EV_NONE || c != EV_MOUSE)
434 /* redraw frame */
435 edit->drag_state = MCEDIT_DRAG_NORMAL;
436 edit->force |= REDRAW_COMPLETELY;
437 edit_update_screen (edit);
439 else
441 Dlg_head *h = w->owner;
443 if (edit->drag_state == MCEDIT_DRAG_MOVE)
445 int y = event->y - 1;
446 int x = event->x - 1;
448 y = max (y, h->y + 1); /* status line */
449 y = min (y, h->y + h->lines - 2); /* buttonbar */
450 x = max (x, h->x);
451 x = min (x, h->x + h->cols - 1);
452 /* don't use widget_set_size() here to avoid double draw */
453 w->y = y;
454 w->x = x - edit->drag_state_start;
455 edit->force |= REDRAW_PAGE;
457 else if (edit->drag_state == MCEDIT_DRAG_RESIZE)
459 event->y = min (event->y, h->y + h->lines - 1); /* buttonbar */
460 event->x = min (event->x, h->x + h->cols);
461 local = mouse_get_local (event, w);
463 /* don't use widget_set_size() here to avoid double draw */
464 w->lines = max (WINDOW_MIN_LINES, local.y);
465 w->cols = max (WINDOW_MIN_COLS, local.x);
466 edit->force |= REDRAW_COMPLETELY;
469 dlg_redraw (h);
473 return MOU_NORMAL;
476 /* --------------------------------------------------------------------------------------------- */
478 static cb_ret_t
479 edit_dialog_command_execute (Dlg_head * h, unsigned long command)
481 gboolean ret = MSG_HANDLED;
483 switch (command)
485 case CK_Close:
486 /* if there are no opened files anymore, close MC editor */
487 if (edit_widget_is_editor ((Widget *) h->current->data) &&
488 edit_close_cmd ((WEdit *) h->current->data) && find_editor (h) == NULL)
489 dlg_stop (h);
490 break;
491 case CK_Help:
492 edit_help ();
493 /* edit->force |= REDRAW_COMPLETELY; */
494 break;
495 case CK_Menu:
496 edit_menu_cmd (h);
497 break;
498 case CK_Quit:
499 case CK_Cancel:
501 Widget *w = (Widget *) h->current->data;
503 if (!edit_widget_is_editor (w) || ((WEdit *) w)->drag_state == MCEDIT_DRAG_NORMAL)
504 dlg_stop (h);
505 else
506 edit_restore_size ((WEdit *) w);
508 break;
509 case CK_About:
510 edit_about ();
511 break;
512 case CK_Shell:
513 view_other_cmd ();
514 break;
515 case CK_LearnKeys:
516 learn_keys ();
517 break;
518 case CK_WindowMove:
519 case CK_WindowResize:
520 if (edit_widget_is_editor ((Widget *) h->current->data))
521 edit_handle_move_resize ((WEdit *) h->current->data, command);
522 break;
523 case CK_OptionsSaveMode:
524 edit_save_mode_cmd ();
525 break;
526 case CK_SaveSetup:
527 save_setup_cmd ();
528 break;
529 default:
530 ret = MSG_NOT_HANDLED;
531 break;
534 return ret;
537 /* --------------------------------------------------------------------------------------------- */
539 static inline void
540 edit_set_buttonbar (WEdit * edit, WButtonBar * bb)
542 buttonbar_set_label (bb, 1, Q_ ("ButtonBar|Help"), editor_map, NULL);
543 buttonbar_set_label (bb, 2, Q_ ("ButtonBar|Save"), editor_map, (Widget *) edit);
544 buttonbar_set_label (bb, 3, Q_ ("ButtonBar|Mark"), editor_map, (Widget *) edit);
545 buttonbar_set_label (bb, 4, Q_ ("ButtonBar|Replac"), editor_map, (Widget *) edit);
546 buttonbar_set_label (bb, 5, Q_ ("ButtonBar|Copy"), editor_map, (Widget *) edit);
547 buttonbar_set_label (bb, 6, Q_ ("ButtonBar|Move"), editor_map, (Widget *) edit);
548 buttonbar_set_label (bb, 7, Q_ ("ButtonBar|Search"), editor_map, (Widget *) edit);
549 buttonbar_set_label (bb, 8, Q_ ("ButtonBar|Delete"), editor_map, (Widget *) edit);
550 buttonbar_set_label (bb, 9, Q_ ("ButtonBar|PullDn"), editor_map, NULL);
551 buttonbar_set_label (bb, 10, Q_ ("ButtonBar|Quit"), editor_map, NULL);
554 /* --------------------------------------------------------------------------------------------- */
555 /** Callback for the edit dialog */
557 static cb_ret_t
558 edit_dialog_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
560 WEdit *edit;
561 WMenuBar *menubar;
562 WButtonBar *buttonbar;
564 menubar = find_menubar (h);
565 buttonbar = find_buttonbar (h);
567 switch (msg)
569 case DLG_INIT:
570 edit = find_editor (h);
571 edit_set_buttonbar (edit, buttonbar);
572 return MSG_HANDLED;
574 case DLG_DRAW:
575 /* don't use common_dialog_repaint() -- we don't need a frame */
576 tty_setcolor (EDITOR_NORMAL_COLOR);
577 dlg_erase (h);
578 return MSG_HANDLED;
580 case DLG_RESIZE:
581 /* dlg_set_size() is surplus for this case */
582 h->lines = LINES;
583 h->cols = COLS;
584 widget_set_size (&buttonbar->widget, h->lines - 1, h->x, 1, h->cols);
585 widget_set_size (&menubar->widget, h->y, h->x, 1, h->cols);
586 menubar_arrange (menubar);
587 return MSG_HANDLED;
589 case DLG_ACTION:
590 /* shortcut */
591 if (sender == NULL)
592 return edit_dialog_command_execute (h, parm);
593 /* message from menu */
594 if (sender == (Widget *) menubar)
596 if (edit_dialog_command_execute (h, parm) == MSG_HANDLED)
597 return MSG_HANDLED;
598 /* try send command to the current window */
599 return send_message ((Widget *) h->current->data, WIDGET_COMMAND, parm);
601 /* message from buttonbar */
602 if (sender == (Widget *) buttonbar)
604 if (data != NULL)
605 return send_message ((Widget *) data, WIDGET_COMMAND, parm);
606 return edit_dialog_command_execute (h, parm);
608 return MSG_NOT_HANDLED;
610 case DLG_KEY:
612 Widget *w = h->current->data;
613 cb_ret_t ret = MSG_NOT_HANDLED;
615 if (edit_widget_is_editor (w))
617 WEdit *e = (WEdit *) w;
618 unsigned long command;
620 if (!e->extmod)
621 command = keybind_lookup_keymap_command (editor_map, parm);
622 else
624 e->extmod = FALSE;
625 command = keybind_lookup_keymap_command (editor_x_map, parm);
628 if (command != CK_IgnoreKey)
629 ret = edit_dialog_command_execute (h, command);
632 return ret;
635 /* hardcoded menu hotkeys (see edit_drop_hotkey_menu) */
636 case DLG_UNHANDLED_KEY:
637 return edit_drop_hotkey_menu (h, parm) ? MSG_HANDLED : MSG_NOT_HANDLED;
639 case DLG_VALIDATE:
640 h->state = DLG_ACTIVE; /* don't stop the dialog before final decision */
641 edit = find_editor (h);
642 if (edit == NULL)
643 h->state = DLG_CLOSED;
644 else if (edit->drag_state != MCEDIT_DRAG_NORMAL)
645 edit_restore_size (edit);
646 else if (edit_ok_to_exit (edit))
647 h->state = DLG_CLOSED;
648 return MSG_HANDLED;
650 default:
651 return default_dlg_callback (h, sender, msg, parm, data);
655 /* --------------------------------------------------------------------------------------------- */
657 static cb_ret_t
658 edit_callback (Widget * w, widget_msg_t msg, int parm)
660 WEdit *e = (WEdit *) w;
662 switch (msg)
664 case WIDGET_DRAW:
665 e->force |= REDRAW_COMPLETELY;
666 /* fallthrough */
668 case WIDGET_FOCUS:
669 edit_update_screen (e);
670 return MSG_HANDLED;
672 case WIDGET_KEY:
674 int cmd, ch;
675 cb_ret_t ret = MSG_NOT_HANDLED;
677 /* The user may override the access-keys for the menu bar. */
678 if (macro_index == -1 && edit_execute_macro (e, parm))
680 edit_update_screen (e);
681 ret = MSG_HANDLED;
683 else if (edit_translate_key (e, parm, &cmd, &ch))
685 edit_execute_key_command (e, cmd, ch);
686 edit_update_screen (e);
687 ret = MSG_HANDLED;
690 return ret;
693 case WIDGET_COMMAND:
694 /* command from menubar or buttonbar */
695 edit_execute_key_command (e, parm, -1);
696 edit_update_screen (e);
697 return MSG_HANDLED;
699 case WIDGET_CURSOR:
700 widget_move (w, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET + EDIT_WITH_FRAME,
701 e->curs_col + e->start_col + e->over_col +
702 EDIT_TEXT_HORIZONTAL_OFFSET + EDIT_WITH_FRAME + option_line_state_width);
703 return MSG_HANDLED;
705 case WIDGET_DESTROY:
706 edit_clean (e);
707 return MSG_HANDLED;
709 default:
710 return default_proc (msg, parm);
714 /* --------------------------------------------------------------------------------------------- */
715 /*** public functions ****************************************************************************/
716 /* --------------------------------------------------------------------------------------------- */
718 gboolean
719 edit_file (const vfs_path_t * _file_vpath, int line)
721 static gboolean made_directory = FALSE;
722 Dlg_head *edit_dlg;
723 WEdit *wedit;
724 WMenuBar *menubar;
726 if (!made_directory)
728 char *dir;
730 dir = mc_build_filename (mc_config_get_cache_path (), EDIT_DIR, NULL);
731 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
732 g_free (dir);
734 dir = mc_build_filename (mc_config_get_path (), EDIT_DIR, NULL);
735 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
736 g_free (dir);
738 dir = mc_build_filename (mc_config_get_data_path (), EDIT_DIR, NULL);
739 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
740 g_free (dir);
743 wedit = edit_init (NULL, 1, 0, LINES - 2, COLS, _file_vpath, line);
745 if (wedit == NULL)
746 return FALSE;
748 /* Create a new dialog and add it widgets to it */
749 edit_dlg =
750 create_dlg (FALSE, 0, 0, LINES, COLS, NULL, edit_dialog_callback, NULL,
751 "[Internal File Editor]", NULL, DLG_WANT_TAB);
753 edit_dlg->get_shortcut = edit_get_shortcut;
754 edit_dlg->get_title = edit_get_title;
756 menubar = menubar_new (0, 0, COLS, NULL);
757 add_widget (edit_dlg, menubar);
758 edit_init_menu (menubar);
760 init_widget (&wedit->widget, wedit->widget.y, wedit->widget.x,
761 wedit->widget.lines, wedit->widget.cols, edit_callback, edit_event);
762 widget_want_cursor (wedit->widget, TRUE);
764 add_widget (edit_dlg, wedit);
766 add_widget (edit_dlg, buttonbar_new (TRUE));
768 run_dlg (edit_dlg);
770 if (edit_dlg->state == DLG_CLOSED)
771 destroy_dlg (edit_dlg);
773 return TRUE;
776 /* --------------------------------------------------------------------------------------------- */
778 char *
779 edit_get_file_name (const WEdit * edit)
781 return vfs_path_to_str (edit->filename_vpath);
784 /* --------------------------------------------------------------------------------------------- */
786 * Check if widget is an WEdit class.
788 * @param w probably editor object
789 * @return TRUE if widget is an WEdit class, FALSE otherwise
792 WEdit *
793 find_editor (const Dlg_head * h)
795 return (WEdit *) find_widget_type (h, edit_callback);
798 /* --------------------------------------------------------------------------------------------- */
800 gboolean
801 edit_widget_is_editor (const Widget * w)
803 return (w != NULL && w->callback == edit_callback);
806 /* --------------------------------------------------------------------------------------------- */
808 void
809 edit_update_screen (WEdit * e)
811 edit_scroll_screen_over_cursor (e);
812 edit_update_curs_col (e);
814 if (!EDIT_WITH_FRAME)
815 edit_status (e);
816 else
818 if ((e->force & REDRAW_COMPLETELY) != 0)
820 /* draw a frame around edit area */
821 tty_setcolor (EDITOR_NORMAL_COLOR);
822 tty_draw_box (e->widget.y, e->widget.x, e->widget.lines, e->widget.cols, TRUE);
825 edit_info_status (e);
828 /* pop all events for this window for internal handling */
829 if (!is_idle ())
830 e->force |= REDRAW_PAGE;
831 else
833 if (e->force & REDRAW_COMPLETELY)
834 e->force |= REDRAW_PAGE;
835 edit_render_keypress (e);
839 /* --------------------------------------------------------------------------------------------- */
841 * Save current window size.
843 * @param edit editor object
846 void
847 edit_save_size (WEdit * edit)
849 edit->y_prev = edit->widget.y;
850 edit->x_prev = edit->widget.x;
851 edit->lines_prev = edit->widget.lines;
852 edit->cols_prev = edit->widget.cols;
855 /* --------------------------------------------------------------------------------------------- */
857 * Handle move/resize events.
859 * @param edit editor object
860 * @param command action id
861 * @return TRUE if mouse actions was handled, FALSE otherwise
864 gboolean
865 edit_handle_move_resize (WEdit * edit, unsigned long command)
867 gboolean ret = FALSE;
869 switch (edit->drag_state)
871 case MCEDIT_DRAG_NORMAL:
872 /* possible start move/resize */
873 switch (command)
875 case CK_WindowMove:
876 edit->drag_state = MCEDIT_DRAG_MOVE;
877 edit_save_size (edit);
878 ret = TRUE;
879 break;
880 case CK_WindowResize:
881 edit->drag_state = MCEDIT_DRAG_RESIZE;
882 edit_save_size (edit);
883 ret = TRUE;
884 break;
885 default:
886 break;
888 break;
890 case MCEDIT_DRAG_MOVE:
891 switch (command)
893 case CK_WindowResize:
894 edit->drag_state = MCEDIT_DRAG_RESIZE;
895 ret = TRUE;
896 break;
897 case CK_Up:
898 case CK_Down:
899 case CK_Left:
900 case CK_Right:
901 edit_window_move (edit, command);
902 ret = TRUE;
903 break;
904 case CK_Enter:
905 case CK_WindowMove:
906 edit->drag_state = MCEDIT_DRAG_NORMAL;
907 default:
908 ret = TRUE;
909 break;
911 break;
913 case MCEDIT_DRAG_RESIZE:
914 switch (command)
916 case CK_WindowMove:
917 edit->drag_state = MCEDIT_DRAG_MOVE;
918 ret = TRUE;
919 break;
920 case CK_Up:
921 case CK_Down:
922 case CK_Left:
923 case CK_Right:
924 edit_window_resize (edit, command);
925 ret = TRUE;
926 break;
927 case CK_Enter:
928 case CK_WindowResize:
929 edit->drag_state = MCEDIT_DRAG_NORMAL;
930 default:
931 ret = TRUE;
932 break;
934 break;
937 return ret;
940 /* --------------------------------------------------------------------------------------------- */