Aggressive use WIDGET macro.
[midnight-commander.git] / src / editor / editwidget.c
blob6dbb56ac6bc7ad920c1d4f8eca7694d7bbd1dc1c
1 /*
2 Editor initialisation and callback handler.
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 initialisation and callback handler
30 * \author Paul Sheer
31 * \date 1996, 1997
34 #include <config.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>
44 #include <stdlib.h>
46 #include "lib/global.h"
48 #include "lib/tty/tty.h" /* LINES, COLS */
49 #include "lib/tty/key.h" /* is_idle() */
50 #include "lib/tty/color.h" /* tty_setcolor() */
51 #include "lib/skin.h"
52 #include "lib/strutil.h" /* str_term_trim() */
53 #include "lib/util.h" /* mc_build_filename() */
54 #include "lib/widget.h"
55 #include "lib/mcconfig.h"
56 #include "lib/event.h" /* mc_event_raise() */
58 #include "src/keybind-defaults.h"
59 #include "src/setup.h" /* home_dir */
60 #include "src/filemanager/cmd.h" /* view_other_cmd(), save_setup_cmd() */
61 #include "src/learn.h" /* learn_keys() */
62 #include "src/args.h" /* mcedit_arg_t */
64 #include "edit-impl.h"
65 #include "editwidget.h"
66 #ifdef HAVE_ASPELL
67 #include "spell.h"
68 #endif
70 /*** global variables ****************************************************************************/
72 char *edit_window_state_char = NULL;
73 char *edit_window_close_char = NULL;
75 /*** file scope macro definitions ****************************************************************/
77 #define WINDOW_MIN_LINES (2 + 2)
78 #define WINDOW_MIN_COLS (2 + LINE_STATE_WIDTH + 2)
80 /*** file scope type declarations ****************************************************************/
82 /*** file scope variables ************************************************************************/
83 static unsigned int edit_dlg_init_refcounter = 0;
85 /*** file scope functions ************************************************************************/
87 static cb_ret_t edit_callback (Widget * w, widget_msg_t msg, int parm);
88 static cb_ret_t edit_dialog_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm,
89 void *data);
91 /* --------------------------------------------------------------------------------------------- */
92 /**
93 * Init the 'edit' subsystem
96 static void
97 edit_dlg_init (void)
99 if (edit_dlg_init_refcounter == 0)
101 edit_window_state_char = mc_skin_get ("editor", "window-state-char", "*");
102 edit_window_close_char = mc_skin_get ("editor", "window-close-char", "X");
104 #ifdef HAVE_ASPELL
105 aspell_init ();
106 #endif
109 edit_dlg_init_refcounter++;
112 /* --------------------------------------------------------------------------------------------- */
114 * Deinit the 'edit' subsystem
117 static void
118 edit_dlg_deinit (void)
120 if (edit_dlg_init_refcounter != 0)
121 edit_dlg_init_refcounter--;
123 if (edit_dlg_init_refcounter == 0)
125 g_free (edit_window_state_char);
126 g_free (edit_window_close_char);
128 #ifdef HAVE_ASPELL
129 aspell_clean ();
130 #endif
134 /* --------------------------------------------------------------------------------------------- */
136 * Show info about editor
139 static void
140 edit_about (void)
142 const char *header = N_("About");
143 const char *button_name = N_("&OK");
144 const char *const version = "MCEdit " VERSION;
145 char text[BUF_LARGE];
147 int win_len, version_len, button_len;
148 int cols, lines;
150 Dlg_head *about_dlg;
152 #ifdef ENABLE_NLS
153 header = _(header);
154 button_name = _(button_name);
155 #endif
157 button_len = str_term_width1 (button_name) + 5;
158 version_len = str_term_width1 (version);
160 g_snprintf (text, sizeof (text),
161 _("Copyright (C) 1996-2012 the Free Software Foundation\n\n"
162 " A user friendly text editor\n"
163 " written for the Midnight Commander"));
165 win_len = str_term_width1 (header);
166 win_len = max (win_len, version_len);
167 win_len = max (win_len, button_len);
169 /* count width and height of text */
170 str_msg_term_size (text, &lines, &cols);
171 lines += 9;
172 cols = max (win_len, cols) + 6;
174 /* dialog */
175 about_dlg = create_dlg (TRUE, 0, 0, lines, cols, dialog_colors, NULL, NULL,
176 "[Internal File Editor]", header, DLG_CENTER | DLG_TRYUP);
178 add_widget (about_dlg, label_new (3, (cols - version_len) / 2, version));
179 add_widget (about_dlg, label_new (5, 3, text));
180 add_widget (about_dlg, button_new (lines - 3, (cols - button_len) / 2,
181 B_ENTER, NORMAL_BUTTON, button_name, NULL));
183 run_dlg (about_dlg);
184 destroy_dlg (about_dlg);
187 /* --------------------------------------------------------------------------------------------- */
189 * Show a help window
192 static void
193 edit_help (void)
195 ev_help_t event_data = { NULL, "[Internal File Editor]" };
196 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
199 /* --------------------------------------------------------------------------------------------- */
201 * Callback for the iteration of objects in the 'editors' array.
202 * Resize the editor window.
204 * @param data probably WEdit object
205 * @param user_data unused
208 static void
209 edit_dialog_resize_cb (void *data, void *user_data)
211 Widget *w = WIDGET (data);
213 (void) user_data;
215 if (edit_widget_is_editor (w) && ((WEdit *) w)->fullscreen)
217 Widget *wh = WIDGET (w->owner);
219 w->lines = wh->lines - 2;
220 w->cols = wh->cols;
224 /* --------------------------------------------------------------------------------------------- */
226 * Restore saved window size.
228 * @param edit editor object
231 static void
232 edit_restore_size (WEdit * edit)
234 edit->drag_state = MCEDIT_DRAG_NORMAL;
235 widget_set_size (WIDGET (edit), edit->y_prev, edit->x_prev, edit->lines_prev, edit->cols_prev);
236 dlg_redraw (WIDGET (edit)->owner);
239 /* --------------------------------------------------------------------------------------------- */
241 * Move window by one row or column in any direction.
243 * @param edit editor object
244 * @param command direction (CK_Up, CK_Down, CK_Left, CK_Right)
247 static void
248 edit_window_move (WEdit * edit, unsigned long command)
250 Widget *w = WIDGET (edit);
251 Widget *wh = WIDGET (w->owner);
253 switch (command)
255 case CK_Up:
256 if (w->y > wh->y + 1) /* menubar */
257 w->y--;
258 break;
259 case CK_Down:
260 if (w->y < wh->y + wh->lines - 2) /* buttonbar */
261 w->y++;
262 break;
263 case CK_Left:
264 if (w->x + w->cols > wh->x)
265 w->x--;
266 break;
267 case CK_Right:
268 if (w->x < wh->x + wh->cols)
269 w->x++;
270 break;
271 default:
272 return;
275 edit->force |= REDRAW_PAGE;
276 dlg_redraw (w->owner);
279 /* --------------------------------------------------------------------------------------------- */
281 * Resize window by one row or column in any direction.
283 * @param edit editor object
284 * @param command direction (CK_Up, CK_Down, CK_Left, CK_Right)
287 static void
288 edit_window_resize (WEdit * edit, unsigned long command)
290 Widget *w = WIDGET (edit);
291 Widget *wh = WIDGET (w->owner);
293 switch (command)
295 case CK_Up:
296 if (w->lines > WINDOW_MIN_LINES)
297 w->lines--;
298 break;
299 case CK_Down:
300 if (w->y + w->lines < wh->y + wh->lines - 1) /* buttonbar */
301 w->lines++;
302 break;
303 case CK_Left:
304 if (w->cols > WINDOW_MIN_COLS)
305 w->cols--;
306 break;
307 case CK_Right:
308 if (w->x + w->cols < wh->x + wh->cols)
309 w->cols++;
310 break;
311 default:
312 return;
315 edit->force |= REDRAW_COMPLETELY;
316 dlg_redraw (w->owner);
319 /* --------------------------------------------------------------------------------------------- */
321 * Get hotkey by number.
323 * @param n number
324 * @return hotkey
327 static unsigned char
328 get_hotkey (int n)
330 return (n <= 9) ? '0' + n : 'a' + n - 10;
333 /* --------------------------------------------------------------------------------------------- */
335 static void
336 edit_window_list (const Dlg_head * h)
338 const size_t offset = 2; /* skip menu and buttonbar */
339 const size_t dlg_num = g_list_length (h->widgets) - offset;
340 int lines, cols;
341 Listbox *listbox;
342 GList *w;
343 int i = 0;
344 int rv;
346 lines = min ((size_t) (LINES * 2 / 3), dlg_num);
347 cols = COLS * 2 / 3;
349 listbox = create_listbox_window (lines, cols, _("Open files"), "[Open files]");
351 for (w = h->widgets; w != NULL; w = g_list_next (w))
352 if (edit_widget_is_editor (WIDGET (w->data)))
354 WEdit *e = (WEdit *) w->data;
355 char *fname;
357 if (e->filename_vpath == NULL)
358 fname = g_strdup_printf ("%c [%s]", e->modified ? '*' : ' ', _("NoName"));
359 else
361 char *fname2;
363 fname2 = vfs_path_to_str (e->filename_vpath);
364 fname = g_strdup_printf ("%c%s", e->modified ? '*' : ' ', fname2);
365 g_free (fname2);
368 listbox_add_item (listbox->list, LISTBOX_APPEND_AT_END, get_hotkey (i++),
369 str_term_trim (fname, WIDGET (listbox->list)->cols - 2), NULL);
370 g_free (fname);
373 rv = g_list_position (h->widgets, h->current) - offset;
374 listbox_select_entry (listbox->list, rv);
375 rv = run_listbox (listbox);
376 if (rv >= 0)
378 w = g_list_nth (h->widgets, rv + offset);
379 dlg_set_top_widget (w->data);
383 /* --------------------------------------------------------------------------------------------- */
385 static char *
386 edit_get_shortcut (unsigned long command)
388 const char *ext_map;
389 const char *shortcut = NULL;
391 shortcut = keybind_lookup_keymap_shortcut (editor_map, command);
392 if (shortcut != NULL)
393 return g_strdup (shortcut);
395 ext_map = keybind_lookup_keymap_shortcut (editor_map, CK_ExtendedKeyMap);
396 if (ext_map != NULL)
397 shortcut = keybind_lookup_keymap_shortcut (editor_x_map, command);
398 if (shortcut != NULL)
399 return g_strdup_printf ("%s %s", ext_map, shortcut);
401 return NULL;
404 /* --------------------------------------------------------------------------------------------- */
406 static char *
407 edit_get_title (const Dlg_head * h, size_t len)
409 const WEdit *edit = find_editor (h);
410 const char *modified = edit->modified ? "(*) " : " ";
411 const char *file_label;
412 char *filename;
414 len -= 4;
416 filename = vfs_path_to_str (edit->filename_vpath);
417 if (filename == NULL)
418 filename = g_strdup (_("[NoName]"));
419 file_label = str_term_trim (filename, len - str_term_width1 (_("Edit: ")));
420 g_free (filename);
422 return g_strconcat (_("Edit: "), modified, file_label, (char *) NULL);
425 /* --------------------------------------------------------------------------------------------- */
427 * Handle mouse events of editor window
429 * @param event mouse event
430 * @param data editor window
431 * @return MOU_NORMAL if event was handled, MOU_UNHANDLED otherwise
434 static int
435 edit_event (Gpm_Event * event, void *data)
437 WEdit *edit = (WEdit *) data;
438 Widget *w = WIDGET (data);
439 Gpm_Event local;
441 if (!mouse_global_in_widget (event, w))
442 return MOU_UNHANDLED;
444 local = mouse_get_local (event, w);
446 /* Unknown event type */
447 if ((event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)) == 0)
448 return MOU_NORMAL;
450 dlg_set_top_widget (w);
452 edit_update_curs_row (edit);
453 edit_update_curs_col (edit);
455 if (edit->fullscreen || (local.buttons & GPM_B_LEFT) == 0 || (local.type & GPM_UP) != 0)
456 edit->drag_state = MCEDIT_DRAG_NORMAL;
457 else if (local.y == 1 && edit->drag_state != MCEDIT_DRAG_RESIZE)
459 /* click on the top line (move) */
460 int dx = edit->fullscreen ? 0 : 2;
462 if (local.x == w->cols - dx - 1)
464 edit_dialog_callback (w->owner, NULL, DLG_ACTION, CK_Close, NULL);
465 return MOU_NORMAL;
468 if (local.x == w->cols - dx - 4)
470 edit_toggle_fullscreen (edit);
471 return MOU_NORMAL;
474 if ((local.type & (GPM_DOWN | GPM_DRAG)) != 0)
476 /* move if not fullscreen */
477 edit->drag_state_start = local.x;
478 edit->drag_state = MCEDIT_DRAG_MOVE;
479 edit->force |= REDRAW_COMPLETELY;
480 edit_update_screen (edit);
483 else if (!edit->fullscreen && local.y == w->lines && local.x == w->cols)
485 /* click on bottom-right corner (resize) */
486 if ((local.type & (GPM_DOWN | GPM_DRAG)) != 0)
488 edit->drag_state = MCEDIT_DRAG_RESIZE;
489 edit->force |= REDRAW_COMPLETELY;
490 edit_update_screen (edit);
494 if (edit->drag_state == MCEDIT_DRAG_NORMAL)
496 gboolean done = TRUE;
498 /* Double click */
499 if ((local.type & (GPM_DOUBLE | GPM_UP)) == (GPM_UP | GPM_DOUBLE))
501 edit_mark_current_word_cmd (edit);
502 goto update;
504 #if 0
505 /* Triple click */
506 if ((local.type & (GPM_TRIPLE | GPM_UP)) == (GPM_UP | GPM_TRIPLE))
508 edit_mark_current_line_cmd (edit);
509 goto update;
511 #endif
512 /* Wheel events */
513 if ((local.buttons & GPM_B_UP) != 0 && (local.type & GPM_DOWN) != 0)
515 edit_move_up (edit, 2, 1);
516 goto update;
518 if ((local.buttons & GPM_B_DOWN) != 0 && (local.type & GPM_DOWN) != 0)
520 edit_move_down (edit, 2, 1);
521 goto update;
524 /* continue handle current event */
525 goto cont;
527 /* handle DRAG mouse event, don't use standard way to continue
528 * event handling outside of widget */
531 int c;
533 c = tty_get_event (event, FALSE, TRUE);
534 if (c == EV_NONE || c != EV_MOUSE)
535 break;
537 local = mouse_get_local (event, w);
539 cont:
540 /* A lone up mustn't do anything */
541 if (edit->mark2 != -1 && (local.type & (GPM_UP | GPM_DRAG)) != 0)
542 return MOU_NORMAL;
544 if ((local.type & (GPM_DOWN | GPM_UP)) != 0)
545 edit_push_key_press (edit);
547 if (!edit->fullscreen)
548 local.x--;
549 if (!option_cursor_beyond_eol)
550 edit->prev_col = local.x - edit->start_col - option_line_state_width - 1;
551 else
553 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
554 edit_eol (edit, edit->curs1));
556 if (local.x > line_len)
558 edit->over_col =
559 local.x - line_len - edit->start_col - option_line_state_width - 1;
560 edit->prev_col = line_len;
562 else
564 edit->over_col = 0;
565 edit->prev_col = local.x - option_line_state_width - edit->start_col - 1;
569 if (!edit->fullscreen)
570 local.y--;
571 if (local.y > (edit->curs_row + 1))
572 edit_move_down (edit, local.y - (edit->curs_row + 1), 0);
573 else if (local.y < (edit->curs_row + 1))
574 edit_move_up (edit, (edit->curs_row + 1) - local.y, 0);
575 else
576 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
578 if ((local.type & GPM_DOWN) != 0)
580 edit_mark_cmd (edit, TRUE); /* reset */
581 edit->highlight = 0;
584 done = (local.type & GPM_DRAG) == 0;
585 if (done)
586 edit_mark_cmd (edit, FALSE);
588 update:
589 edit_find_bracket (edit);
590 edit->force |= REDRAW_COMPLETELY;
591 edit_update_curs_row (edit);
592 edit_update_curs_col (edit);
593 edit_update_screen (edit);
595 while (!edit->fullscreen && !done);
597 else
598 while (edit->drag_state != MCEDIT_DRAG_NORMAL)
600 int c;
601 int y;
603 c = tty_get_event (event, FALSE, TRUE);
604 y = event->y - 1;
606 if (c == EV_NONE || c != EV_MOUSE)
608 /* redraw frame */
609 edit->drag_state = MCEDIT_DRAG_NORMAL;
610 edit->force |= REDRAW_COMPLETELY;
611 edit_update_screen (edit);
613 else if (y == w->y && (event->type & (GPM_DOUBLE | GPM_UP)) == (GPM_DOUBLE | GPM_UP))
615 /* double click on top line (toggle fullscreen) */
616 edit_toggle_fullscreen (edit);
617 edit->drag_state = MCEDIT_DRAG_NORMAL;
618 edit->force |= REDRAW_COMPLETELY;
619 edit_update_screen (edit);
621 else if ((event->type & (GPM_DRAG | GPM_DOWN)) == 0)
623 /* redraw frame */
624 edit->drag_state = MCEDIT_DRAG_NORMAL;
625 edit->force |= REDRAW_COMPLETELY;
626 edit_update_screen (edit);
628 else if (!edit->fullscreen)
630 Widget *h = WIDGET (w->owner);
632 if (edit->drag_state == MCEDIT_DRAG_MOVE)
634 int x = event->x - 1;
636 y = max (y, h->y + 1); /* status line */
637 y = min (y, h->y + h->lines - 2); /* buttonbar */
638 x = max (x, h->x);
639 x = min (x, h->x + h->cols - 1);
640 /* don't use widget_set_size() here to avoid double draw */
641 w->y = y;
642 w->x = x - edit->drag_state_start;
643 edit->force |= REDRAW_COMPLETELY;
645 else if (edit->drag_state == MCEDIT_DRAG_RESIZE)
647 event->y = min (event->y, h->y + h->lines - 1); /* buttonbar */
648 event->x = min (event->x, h->x + h->cols);
649 local = mouse_get_local (event, w);
651 /* don't use widget_set_size() here to avoid double draw */
652 w->lines = max (WINDOW_MIN_LINES, local.y);
653 w->cols = max (WINDOW_MIN_COLS, local.x);
654 edit->force |= REDRAW_COMPLETELY;
657 dlg_redraw (w->owner);
661 return MOU_NORMAL;
664 /* --------------------------------------------------------------------------------------------- */
666 * Handle mouse events of editor screen.
668 * @param event mouse event
669 * @param data editor screen
670 * @return MOU_NORMAL if event was handled, MOU_UNHANDLED otherwise
673 static int
674 edit_dialog_event (Gpm_Event * event, void *data)
676 Dlg_head *h = (Dlg_head *) data;
677 Widget *w;
678 Widget *wh = WIDGET (h);
679 int ret = MOU_UNHANDLED;
681 w = WIDGET (find_menubar (h));
683 if (event->y == wh->y + 1 && (event->type & GPM_DOWN) != 0 && !((WMenuBar *) w)->is_active)
685 /* menubar */
687 GList *l;
688 GList *top = NULL;
689 int x;
691 /* Try find top fullscreen window */
692 for (l = h->widgets; l != NULL; l = g_list_next (l))
693 if (edit_widget_is_editor (WIDGET (l->data)) && ((WEdit *) l->data)->fullscreen)
694 top = l;
696 /* Handle fullscreen/close buttons in the top line */
697 x = wh->x + wh->cols + 1 - 6;
699 if (top != NULL && event->x >= x)
701 WEdit *e;
703 e = (WEdit *) top->data;
704 x = event->x - x;
706 if (top != h->current)
708 /* Window is not active. Activate it */
709 dlg_set_top_widget (e);
712 /* Handle buttons */
713 if (x <= 2)
714 edit_toggle_fullscreen (e);
715 else
716 edit_dialog_callback (h, NULL, DLG_ACTION, CK_Close, NULL);
718 ret = MOU_NORMAL;
721 if (ret == MOU_UNHANDLED)
722 dlg_select_widget (w);
725 return ret;
728 /* --------------------------------------------------------------------------------------------- */
730 static cb_ret_t
731 edit_dialog_command_execute (Dlg_head * h, unsigned long command)
733 Widget *wh = WIDGET (h);
734 gboolean ret = MSG_HANDLED;
736 switch (command)
738 case CK_EditNew:
739 edit_add_window (h, wh->y + 1, wh->x, wh->lines - 2, wh->cols, NULL, 0);
740 break;
741 case CK_EditFile:
742 edit_load_cmd (h);
743 break;
744 case CK_EditSyntaxFile:
745 edit_load_syntax_file (h);
746 break;
747 case CK_EditUserMenu:
748 edit_load_menu_file (h);
749 break;
750 case CK_Close:
751 /* if there are no opened files anymore, close MC editor */
752 if (edit_widget_is_editor (WIDGET (h->current->data)) &&
753 edit_close_cmd ((WEdit *) h->current->data) && find_editor (h) == NULL)
754 dlg_stop (h);
755 break;
756 case CK_Help:
757 edit_help ();
758 /* edit->force |= REDRAW_COMPLETELY; */
759 break;
760 case CK_Menu:
761 edit_menu_cmd (h);
762 break;
763 case CK_Quit:
764 case CK_Cancel:
766 Widget *w = WIDGET (h->current->data);
768 if (!edit_widget_is_editor (w) || ((WEdit *) w)->drag_state == MCEDIT_DRAG_NORMAL)
769 dlg_stop (h);
770 else
771 edit_restore_size ((WEdit *) w);
773 break;
774 case CK_About:
775 edit_about ();
776 break;
777 case CK_SyntaxOnOff:
778 edit_syntax_onoff_cmd (h);
779 break;
780 case CK_ShowTabTws:
781 edit_show_tabs_tws_cmd (h);
782 break;
783 case CK_ShowMargin:
784 edit_show_margin_cmd (h);
785 break;
786 case CK_ShowNumbers:
787 edit_show_numbers_cmd (h);
788 break;
789 case CK_Refresh:
790 edit_refresh_cmd ();
791 break;
792 case CK_Shell:
793 view_other_cmd ();
794 break;
795 case CK_LearnKeys:
796 learn_keys ();
797 break;
798 case CK_WindowMove:
799 case CK_WindowResize:
800 if (edit_widget_is_editor (WIDGET (h->current->data)))
801 edit_handle_move_resize ((WEdit *) h->current->data, command);
802 break;
803 case CK_WindowList:
804 edit_window_list (h);
805 break;
806 case CK_WindowNext:
807 dlg_one_down (h);
808 dlg_set_top_widget (h->current->data);
809 break;
810 case CK_WindowPrev:
811 dlg_one_up (h);
812 dlg_set_top_widget (h->current->data);
813 break;
814 case CK_Options:
815 edit_options_dialog (h);
816 break;
817 case CK_OptionsSaveMode:
818 edit_save_mode_cmd ();
819 break;
820 case CK_SaveSetup:
821 save_setup_cmd ();
822 break;
823 default:
824 ret = MSG_NOT_HANDLED;
825 break;
828 return ret;
831 /* --------------------------------------------------------------------------------------------- */
833 static inline void
834 edit_quit (Dlg_head * h)
836 GList *l;
837 WEdit *e = NULL;
839 h->state = DLG_ACTIVE; /* don't stop the dialog before final decision */
841 for (l = h->widgets; l != NULL; l = g_list_next (l))
842 if (edit_widget_is_editor (WIDGET (l->data)))
844 e = (WEdit *) l->data;
846 if (e->drag_state != MCEDIT_DRAG_NORMAL)
848 edit_restore_size (e);
849 return;
852 if (e->modified)
854 dlg_select_widget (e);
856 if (!edit_ok_to_exit (e))
857 return;
861 /* no editors in dialog at all or no any file required to be saved */
862 if (e == NULL || l == NULL)
863 h->state = DLG_CLOSED;
866 /* --------------------------------------------------------------------------------------------- */
868 static inline void
869 edit_set_buttonbar (WEdit * edit, WButtonBar * bb)
871 buttonbar_set_label (bb, 1, Q_ ("ButtonBar|Help"), editor_map, NULL);
872 buttonbar_set_label (bb, 2, Q_ ("ButtonBar|Save"), editor_map, WIDGET (edit));
873 buttonbar_set_label (bb, 3, Q_ ("ButtonBar|Mark"), editor_map, WIDGET (edit));
874 buttonbar_set_label (bb, 4, Q_ ("ButtonBar|Replac"), editor_map, WIDGET (edit));
875 buttonbar_set_label (bb, 5, Q_ ("ButtonBar|Copy"), editor_map, WIDGET (edit));
876 buttonbar_set_label (bb, 6, Q_ ("ButtonBar|Move"), editor_map, WIDGET (edit));
877 buttonbar_set_label (bb, 7, Q_ ("ButtonBar|Search"), editor_map, WIDGET (edit));
878 buttonbar_set_label (bb, 8, Q_ ("ButtonBar|Delete"), editor_map, WIDGET (edit));
879 buttonbar_set_label (bb, 9, Q_ ("ButtonBar|PullDn"), editor_map, NULL);
880 buttonbar_set_label (bb, 10, Q_ ("ButtonBar|Quit"), editor_map, NULL);
883 /* --------------------------------------------------------------------------------------------- */
884 /** Callback for the edit dialog */
886 static cb_ret_t
887 edit_dialog_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
889 WMenuBar *menubar;
890 WButtonBar *buttonbar;
891 Widget *wh = WIDGET (h);
893 switch (msg)
895 case DLG_INIT:
896 edit_dlg_init ();
897 return MSG_HANDLED;
899 case DLG_DRAW:
900 /* don't use common_dialog_repaint() -- we don't need a frame */
901 tty_setcolor (EDITOR_BACKGROUND);
902 dlg_erase (h);
903 return MSG_HANDLED;
905 case DLG_RESIZE:
906 menubar = find_menubar (h);
907 buttonbar = find_buttonbar (h);
908 /* dlg_set_size() is surplus for this case */
909 wh->lines = LINES;
910 wh->cols = COLS;
911 widget_set_size (WIDGET (buttonbar), wh->lines - 1, wh->x, 1, wh->cols);
912 widget_set_size (WIDGET (menubar), wh->y, wh->x, 1, wh->cols);
913 menubar_arrange (menubar);
914 g_list_foreach (h->widgets, (GFunc) edit_dialog_resize_cb, NULL);
915 return MSG_HANDLED;
917 case DLG_ACTION:
918 /* shortcut */
919 if (sender == NULL)
920 return edit_dialog_command_execute (h, parm);
921 /* message from menu */
922 menubar = find_menubar (h);
923 if (sender == WIDGET (menubar))
925 if (edit_dialog_command_execute (h, parm) == MSG_HANDLED)
926 return MSG_HANDLED;
927 /* try send command to the current window */
928 return send_message (WIDGET (h->current->data), WIDGET_COMMAND, parm);
930 /* message from buttonbar */
931 buttonbar = find_buttonbar (h);
932 if (sender == WIDGET (buttonbar))
934 if (data != NULL)
935 return send_message (WIDGET (data), WIDGET_COMMAND, parm);
936 return edit_dialog_command_execute (h, parm);
938 return MSG_NOT_HANDLED;
940 case DLG_KEY:
942 Widget *w = h->current->data;
943 cb_ret_t ret = MSG_NOT_HANDLED;
945 if (edit_widget_is_editor (w))
947 WEdit *e = (WEdit *) w;
948 unsigned long command;
950 if (!e->extmod)
951 command = keybind_lookup_keymap_command (editor_map, parm);
952 else
954 e->extmod = FALSE;
955 command = keybind_lookup_keymap_command (editor_x_map, parm);
958 if (command != CK_IgnoreKey)
959 ret = edit_dialog_command_execute (h, command);
962 return ret;
965 /* hardcoded menu hotkeys (see edit_drop_hotkey_menu) */
966 case DLG_UNHANDLED_KEY:
967 return edit_drop_hotkey_menu (h, parm) ? MSG_HANDLED : MSG_NOT_HANDLED;
969 case DLG_VALIDATE:
970 edit_quit (h);
971 return MSG_HANDLED;
973 case DLG_END:
974 edit_dlg_deinit ();
975 return MSG_HANDLED;
977 default:
978 return default_dlg_callback (h, sender, msg, parm, data);
982 /* --------------------------------------------------------------------------------------------- */
984 static cb_ret_t
985 edit_callback (Widget * w, widget_msg_t msg, int parm)
987 WEdit *e = (WEdit *) w;
989 switch (msg)
991 case WIDGET_FOCUS:
992 edit_set_buttonbar (e, find_buttonbar (w->owner));
993 /* fall through */
995 case WIDGET_DRAW:
996 e->force |= REDRAW_COMPLETELY;
997 edit_update_screen (e);
998 return MSG_HANDLED;
1000 case WIDGET_UNFOCUS:
1001 /* redraw frame and status */
1002 edit_status (e, FALSE);
1003 return MSG_HANDLED;
1005 case WIDGET_KEY:
1007 int cmd, ch;
1008 cb_ret_t ret = MSG_NOT_HANDLED;
1010 /* The user may override the access-keys for the menu bar. */
1011 if (macro_index == -1 && edit_execute_macro (e, parm))
1013 edit_update_screen (e);
1014 ret = MSG_HANDLED;
1016 else if (edit_translate_key (e, parm, &cmd, &ch))
1018 edit_execute_key_command (e, cmd, ch);
1019 edit_update_screen (e);
1020 ret = MSG_HANDLED;
1023 return ret;
1026 case WIDGET_COMMAND:
1027 /* command from menubar or buttonbar */
1028 edit_execute_key_command (e, parm, -1);
1029 edit_update_screen (e);
1030 return MSG_HANDLED;
1032 case WIDGET_CURSOR:
1034 int y, x;
1036 y = (e->fullscreen ? 0 : 1) + EDIT_TEXT_VERTICAL_OFFSET + e->curs_row;
1037 x = (e->fullscreen ? 0 : 1) + EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width +
1038 e->curs_col + e->start_col + e->over_col;
1040 widget_move (w, y, x);
1041 return MSG_HANDLED;
1044 case WIDGET_DESTROY:
1045 edit_clean (e);
1046 return MSG_HANDLED;
1048 default:
1049 return default_proc (msg, parm);
1053 /* --------------------------------------------------------------------------------------------- */
1054 /*** public functions ****************************************************************************/
1055 /* --------------------------------------------------------------------------------------------- */
1057 * Edit one file.
1059 * @param file_vpath file object
1060 * @param line line number
1061 * @return TRUE if no errors was occured, FALSE otherwise
1064 gboolean
1065 edit_file (const vfs_path_t * file_vpath, long line)
1067 mcedit_arg_t arg = { (vfs_path_t *) file_vpath, line };
1068 GList *files;
1069 gboolean ok;
1071 files = g_list_prepend (NULL, &arg);
1072 ok = edit_files (files);
1073 g_list_free (files);
1075 return ok;
1078 /* --------------------------------------------------------------------------------------------- */
1080 gboolean
1081 edit_files (const GList * files)
1083 static gboolean made_directory = FALSE;
1084 Dlg_head *edit_dlg;
1085 WMenuBar *menubar;
1086 const GList *file;
1087 gboolean ok = FALSE;
1089 if (!made_directory)
1091 char *dir;
1093 dir = mc_build_filename (mc_config_get_cache_path (), EDIT_DIR, NULL);
1094 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
1095 g_free (dir);
1097 dir = mc_build_filename (mc_config_get_path (), EDIT_DIR, NULL);
1098 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
1099 g_free (dir);
1101 dir = mc_build_filename (mc_config_get_data_path (), EDIT_DIR, NULL);
1102 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
1103 g_free (dir);
1106 /* Create a new dialog and add it widgets to it */
1107 edit_dlg =
1108 create_dlg (FALSE, 0, 0, LINES, COLS, NULL, edit_dialog_callback, edit_dialog_event,
1109 "[Internal File Editor]", NULL, DLG_WANT_TAB);
1111 edit_dlg->get_shortcut = edit_get_shortcut;
1112 edit_dlg->get_title = edit_get_title;
1114 menubar = menubar_new (0, 0, COLS, NULL);
1115 add_widget (edit_dlg, menubar);
1116 edit_init_menu (menubar);
1118 add_widget (edit_dlg, buttonbar_new (TRUE));
1120 for (file = files; file != NULL; file = g_list_next (file))
1122 Widget *w = WIDGET (edit_dlg);
1123 mcedit_arg_t *f = (mcedit_arg_t *) file->data;
1124 gboolean f_ok;
1126 f_ok = edit_add_window (edit_dlg, w->y + 1, w->x, w->lines - 2, w->cols, f->file_vpath,
1127 f->line_number);
1128 /* at least one file has been opened succefully */
1129 ok = ok || f_ok;
1132 if (ok)
1133 run_dlg (edit_dlg);
1135 if (!ok || edit_dlg->state == DLG_CLOSED)
1136 destroy_dlg (edit_dlg);
1138 return ok;
1141 /* --------------------------------------------------------------------------------------------- */
1143 char *
1144 edit_get_file_name (const WEdit * edit)
1146 return vfs_path_to_str (edit->filename_vpath);
1149 /* --------------------------------------------------------------------------------------------- */
1151 WEdit *
1152 find_editor (const Dlg_head * h)
1154 if (edit_widget_is_editor (WIDGET (h->current->data)))
1155 return (WEdit *) h->current->data;
1156 return (WEdit *) find_widget_type (h, edit_callback);
1159 /* --------------------------------------------------------------------------------------------- */
1161 * Check if widget is an WEdit class.
1163 * @param w probably editor object
1164 * @return TRUE if widget is an WEdit class, FALSE otherwise
1167 gboolean
1168 edit_widget_is_editor (const Widget * w)
1170 return (w != NULL && w->callback == edit_callback);
1173 /* --------------------------------------------------------------------------------------------- */
1175 void
1176 edit_update_screen (WEdit * e)
1178 Dlg_head *h = WIDGET (e)->owner;
1180 edit_scroll_screen_over_cursor (e);
1181 edit_update_curs_col (e);
1183 edit_status (e, (e->force & REDRAW_COMPLETELY) != 0 && (void *) e == h->current->data);
1185 /* pop all events for this window for internal handling */
1186 if (!is_idle ())
1187 e->force |= REDRAW_PAGE;
1188 else
1190 if ((e->force & REDRAW_COMPLETELY) != 0)
1191 e->force |= REDRAW_PAGE;
1192 edit_render_keypress (e);
1195 buttonbar_redraw (find_buttonbar (h));
1198 /* --------------------------------------------------------------------------------------------- */
1200 * Save current window size.
1202 * @param edit editor object
1205 void
1206 edit_save_size (WEdit * edit)
1208 Widget *w = WIDGET (edit);
1210 edit->y_prev = w->y;
1211 edit->x_prev = w->x;
1212 edit->lines_prev = w->lines;
1213 edit->cols_prev = w->cols;
1216 /* --------------------------------------------------------------------------------------------- */
1218 * Create new editor window and insert it into editor screen.
1220 * @param h editor dialog (screen)
1221 * @param y y coordinate
1222 * @param x x coordinate
1223 * @param lines window height
1224 * @param cols window width
1225 * @param f file object
1226 * @param fline line number in file
1227 * @return TRUE if new window was successfully created and inserted into editor screen,
1228 * FALSE otherwise
1231 gboolean
1232 edit_add_window (Dlg_head * h, int y, int x, int lines, int cols, const vfs_path_t * f, long fline)
1234 WEdit *edit;
1235 Widget *w;
1237 edit = edit_init (NULL, y, x, lines, cols, f, fline);
1238 if (edit == NULL)
1239 return FALSE;
1241 w = WIDGET (edit);
1242 w->callback = edit_callback;
1243 w->mouse = edit_event;
1244 widget_want_cursor (w, TRUE);
1246 add_widget (h, w);
1247 dlg_redraw (h);
1249 return TRUE;
1252 /* --------------------------------------------------------------------------------------------- */
1254 * Handle move/resize events.
1256 * @param edit editor object
1257 * @param command action id
1258 * @return TRUE if mouse actions was handled, FALSE otherwise
1261 gboolean
1262 edit_handle_move_resize (WEdit * edit, unsigned long command)
1264 gboolean ret = FALSE;
1266 if (edit->fullscreen)
1268 edit->drag_state = MCEDIT_DRAG_NORMAL;
1269 return ret;
1272 switch (edit->drag_state)
1274 case MCEDIT_DRAG_NORMAL:
1275 /* possible start move/resize */
1276 switch (command)
1278 case CK_WindowMove:
1279 edit->drag_state = MCEDIT_DRAG_MOVE;
1280 edit_save_size (edit);
1281 ret = TRUE;
1282 break;
1283 case CK_WindowResize:
1284 edit->drag_state = MCEDIT_DRAG_RESIZE;
1285 edit_save_size (edit);
1286 ret = TRUE;
1287 break;
1288 default:
1289 break;
1291 break;
1293 case MCEDIT_DRAG_MOVE:
1294 switch (command)
1296 case CK_WindowResize:
1297 edit->drag_state = MCEDIT_DRAG_RESIZE;
1298 ret = TRUE;
1299 break;
1300 case CK_Up:
1301 case CK_Down:
1302 case CK_Left:
1303 case CK_Right:
1304 edit_window_move (edit, command);
1305 ret = TRUE;
1306 break;
1307 case CK_Enter:
1308 case CK_WindowMove:
1309 edit->drag_state = MCEDIT_DRAG_NORMAL;
1310 /* redraw frame and status */
1311 edit_status (edit, TRUE);
1312 default:
1313 ret = TRUE;
1314 break;
1316 break;
1318 case MCEDIT_DRAG_RESIZE:
1319 switch (command)
1321 case CK_WindowMove:
1322 edit->drag_state = MCEDIT_DRAG_MOVE;
1323 ret = TRUE;
1324 break;
1325 case CK_Up:
1326 case CK_Down:
1327 case CK_Left:
1328 case CK_Right:
1329 edit_window_resize (edit, command);
1330 ret = TRUE;
1331 break;
1332 case CK_Enter:
1333 case CK_WindowResize:
1334 edit->drag_state = MCEDIT_DRAG_NORMAL;
1335 /* redraw frame and status */
1336 edit_status (edit, TRUE);
1337 default:
1338 ret = TRUE;
1339 break;
1341 break;
1344 return ret;
1347 /* --------------------------------------------------------------------------------------------- */
1349 * Toggle window fuulscreen mode.
1351 * @param edit editor object
1354 void
1355 edit_toggle_fullscreen (WEdit * edit)
1357 edit->fullscreen = !edit->fullscreen;
1358 edit->force = REDRAW_COMPLETELY;
1360 if (!edit->fullscreen)
1361 edit_restore_size (edit);
1362 else
1364 Widget *w = WIDGET (edit);
1365 Widget *h = WIDGET (w->owner);
1367 edit_save_size (edit);
1368 widget_set_size (w, h->y + 1, h->x, h->lines - 2, h->cols);
1369 edit->force |= REDRAW_PAGE;
1370 edit_update_screen (edit);
1374 /* --------------------------------------------------------------------------------------------- */