Updated italian translatio
[midnight-commander.git] / edit / editwidget.c
blob85e3365ea2dfcb3af0babbd83b82ee02b038d396
1 /* editor initialisation and callback handler.
3 Copyright (C) 1996, 1997 the Free Software Foundation
5 Authors: 1996, 1997 Paul Sheer
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
23 #include <config.h>
24 #include "edit.h"
25 #include "edit-widget.h"
27 #include "../src/tty.h" /* LINES */
28 #include "../src/widget.h" /* redraw_labels() */
29 #include "../src/menu.h" /* menubar_new() */
30 #include "../src/key.h" /* is_idle() */
32 WEdit *wedit;
33 struct WMenu *edit_menubar;
35 int column_highlighting = 0;
37 static cb_ret_t edit_callback (WEdit *edit, widget_msg_t msg, int parm);
39 static int
40 edit_event (WEdit * edit, Gpm_Event * event, int *result)
42 *result = MOU_NORMAL;
43 edit_update_curs_row (edit);
44 edit_update_curs_col (edit);
46 /* Unknown event type */
47 if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
48 return 0;
50 /* Wheel events */
51 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
52 edit_move_up (edit, 2, 1);
53 goto update;
55 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
56 edit_move_down (edit, 2, 1);
57 goto update;
60 /* Outside editor window */
61 if (event->y <= 1 || event->x <= 0
62 || event->x > edit->num_widget_columns
63 || event->y > edit->num_widget_lines + 1)
64 return 0;
66 /* A lone up mustn't do anything */
67 if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
68 return 1;
70 if (event->type & (GPM_DOWN | GPM_UP))
71 edit_push_key_press (edit);
73 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
75 if (--event->y > (edit->curs_row + 1))
76 edit_cursor_move (edit,
77 edit_move_forward (edit, edit->curs1,
78 event->y - (edit->curs_row +
79 1), 0)
80 - edit->curs1);
82 if (event->y < (edit->curs_row + 1))
83 edit_cursor_move (edit,
84 edit_move_backward (edit, edit->curs1,
85 (edit->curs_row + 1) -
86 event->y) - edit->curs1);
88 edit_cursor_move (edit,
89 (int) edit_move_forward3 (edit, edit->curs1,
90 event->x -
91 edit->start_col - 1,
92 0) - edit->curs1);
94 edit->prev_col = edit_get_col (edit);
96 if (event->type & GPM_DOWN) {
97 edit_mark_cmd (edit, 1); /* reset */
98 edit->highlight = 0;
101 if (!(event->type & GPM_DRAG))
102 edit_mark_cmd (edit, 0);
104 update:
105 edit->force |= REDRAW_COMPLETELY;
106 edit_update_curs_row (edit);
107 edit_update_curs_col (edit);
108 edit_update_screen (edit);
110 return 1;
114 static int
115 edit_mouse_event (Gpm_Event *event, void *x)
117 int result;
118 if (edit_event ((WEdit *) x, event, &result))
119 return result;
120 else
121 return (*edit_menubar->widget.mouse) (event, edit_menubar);
124 static void
125 edit_adjust_size (Dlg_head *h)
127 WEdit *edit;
128 WButtonBar *edit_bar;
130 edit = (WEdit *) find_widget_type (h, edit_callback);
131 edit_bar = find_buttonbar (h);
133 widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
134 widget_set_size (&edit_bar->widget, LINES - 1, 0, 1, COLS);
135 widget_set_size (&edit_menubar->widget, 0, 0, 1, COLS);
137 #ifdef RESIZABLE_MENUBAR
138 menubar_arrange (edit_menubar);
139 #endif
142 /* Callback for the edit dialog */
143 static cb_ret_t
144 edit_dialog_callback (Dlg_head *h, dlg_msg_t msg, int parm)
146 WEdit *edit;
148 switch (msg) {
149 case DLG_RESIZE:
150 edit_adjust_size (h);
151 return MSG_HANDLED;
153 case DLG_VALIDATE:
154 edit = (WEdit *) find_widget_type (h, edit_callback);
155 if (!edit_ok_to_exit (edit)) {
156 h->running = 1;
158 return MSG_HANDLED;
160 default:
161 return default_dlg_callback (h, msg, parm);
166 edit (const char *_file, int line)
168 static int made_directory = 0;
169 Dlg_head *edit_dlg;
170 WButtonBar *edit_bar;
172 if (option_backup_ext_int != -1) {
173 option_backup_ext = g_malloc (sizeof (int) + 1);
174 option_backup_ext[sizeof (int)] = '\0';
175 memcpy (option_backup_ext, (char *) &option_backup_ext_int,
176 sizeof (int));
178 if (!made_directory) {
179 mkdir (catstrs (home_dir, EDIT_DIR, 0), 0700);
180 made_directory = 1;
183 if (!(wedit = edit_init (NULL, LINES - 2, COLS, _file, line))) {
184 return 0;
187 /* Create a new dialog and add it widgets to it */
188 edit_dlg =
189 create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
190 "[Internal File Editor]", NULL, DLG_WANT_TAB);
192 init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
193 (callback_fn) edit_callback,
194 (mouse_h) edit_mouse_event);
196 widget_want_cursor (wedit->widget, 1);
198 edit_bar = buttonbar_new (1);
200 switch (edit_key_emulation) {
201 case EDIT_KEY_EMULATION_NORMAL:
202 edit_init_menu_normal (); /* editmenu.c */
203 break;
204 case EDIT_KEY_EMULATION_EMACS:
205 edit_init_menu_emacs (); /* editmenu.c */
206 break;
208 edit_menubar = menubar_new (0, 0, COLS, EditMenuBar, N_menus);
210 add_widget (edit_dlg, edit_bar);
211 add_widget (edit_dlg, wedit);
212 add_widget (edit_dlg, edit_menubar);
214 run_dlg (edit_dlg);
216 edit_done_menu (); /* editmenu.c */
218 destroy_dlg (edit_dlg);
220 return 1;
223 static void edit_my_define (Dlg_head * h, int idx, char *text,
224 void (*fn) (WEdit *), WEdit * edit)
226 define_label_data (h, idx, text, (buttonbarfn) fn, edit);
230 static void cmd_F1 (WEdit * edit)
232 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (1));
235 static void cmd_F2 (WEdit * edit)
237 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (2));
240 static void cmd_F3 (WEdit * edit)
242 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (3));
245 static void cmd_F4 (WEdit * edit)
247 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (4));
250 static void cmd_F5 (WEdit * edit)
252 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (5));
255 static void cmd_F6 (WEdit * edit)
257 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (6));
260 static void cmd_F7 (WEdit * edit)
262 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (7));
265 static void cmd_F8 (WEdit * edit)
267 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (8));
270 #if 0
271 static void cmd_F9 (WEdit * edit)
273 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (9));
275 #endif
277 static void cmd_F10 (WEdit * edit)
279 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (10));
282 static void
283 edit_labels (WEdit *edit)
285 Dlg_head *h = edit->widget.parent;
287 edit_my_define (h, 1, _("Help"), cmd_F1, edit);
288 edit_my_define (h, 2, _("Save"), cmd_F2, edit);
289 edit_my_define (h, 3, _("Mark"), cmd_F3, edit);
290 edit_my_define (h, 4, _("Replac"), cmd_F4, edit);
291 edit_my_define (h, 5, _("Copy"), cmd_F5, edit);
292 edit_my_define (h, 6, _("Move"), cmd_F6, edit);
293 edit_my_define (h, 7, _("Search"), cmd_F7, edit);
294 edit_my_define (h, 8, _("Delete"), cmd_F8, edit);
295 edit_my_define (h, 9, _("PullDn"), edit_menu_cmd, edit);
296 edit_my_define (h, 10, _("Quit"), cmd_F10, edit);
298 redraw_labels (h);
301 void edit_update_screen (WEdit * e)
303 edit_scroll_screen_over_cursor (e);
305 edit_update_curs_col (e);
306 edit_status (e);
308 /* pop all events for this window for internal handling */
310 if (!is_idle ()) {
311 e->force |= REDRAW_PAGE;
312 return;
314 if (e->force & REDRAW_COMPLETELY)
315 e->force |= REDRAW_PAGE;
316 edit_render_keypress (e);
319 static cb_ret_t
320 edit_callback (WEdit *e, widget_msg_t msg, int parm)
322 switch (msg) {
323 case WIDGET_INIT:
324 e->force |= REDRAW_COMPLETELY;
325 edit_labels (e);
326 return MSG_HANDLED;
328 case WIDGET_DRAW:
329 e->force |= REDRAW_COMPLETELY;
330 e->num_widget_lines = LINES - 2;
331 e->num_widget_columns = COLS;
332 /* fallthrough */
334 case WIDGET_FOCUS:
335 edit_update_screen (e);
336 return MSG_HANDLED;
338 case WIDGET_KEY:
340 int cmd, ch;
342 /* first check alt-f, alt-e, alt-s, etc for drop menus */
343 if (edit_drop_hotkey_menu (e, parm))
344 return MSG_HANDLED;
345 if (!edit_translate_key (e, parm, &cmd, &ch))
346 return MSG_NOT_HANDLED;
347 edit_execute_key_command (e, cmd, ch);
348 edit_update_screen (e);
350 return MSG_HANDLED;
352 case WIDGET_CURSOR:
353 widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
354 e->curs_col + e->start_col);
355 return MSG_HANDLED;
357 case WIDGET_DESTROY:
358 edit_clean (e);
359 return MSG_HANDLED;
361 default:
362 return default_proc (msg, parm);