Ticket #1545: Don't escape tilde on cmdline, this is not necessary.
[midnight-commander.git] / edit / editwidget.c
bloba4509068cceb97cecd251255656837d9da04c580
1 /* editor initialisation and callback handler.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
24 /** \file
25 * \brief Source: editor initialisation and callback handler
26 * \author Paul Sheer
27 * \date 1996, 1997
30 #include <config.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <sys/stat.h>
40 #include <stdlib.h>
42 #include "../src/global.h"
44 #include "../src/tty/tty.h" /* LINES, COLS */
45 #include "../src/tty/key.h" /* is_idle() */
47 #include "edit-impl.h"
48 #include "edit-widget.h"
50 #include "../src/widget.h" /* buttonbar_redraw() */
51 #include "../src/menu.h" /* menubar_new() */
53 WEdit *wedit;
54 struct WMenu *edit_menubar;
56 int column_highlighting = 0;
58 static cb_ret_t edit_callback (Widget *, widget_msg_t msg, int parm);
60 static int
61 edit_event (WEdit * edit, Gpm_Event * event, int *result)
63 *result = MOU_NORMAL;
64 edit_update_curs_row (edit);
65 edit_update_curs_col (edit);
67 /* Unknown event type */
68 if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
69 return 0;
71 /* Wheel events */
72 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
73 edit_move_up (edit, 2, 1);
74 goto update;
76 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
77 edit_move_down (edit, 2, 1);
78 goto update;
81 /* Outside editor window */
82 if (event->y <= 1 || event->x <= 0
83 || event->x > edit->num_widget_columns
84 || event->y > edit->num_widget_lines + 1)
85 return 0;
87 /* A lone up mustn't do anything */
88 if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
89 return 1;
91 if (event->type & (GPM_DOWN | GPM_UP))
92 edit_push_key_press (edit);
94 if (option_cursor_beyond_eol) {
95 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
96 edit_eol(edit, edit->curs1));
97 if ( event->x > line_len ) {
98 edit->over_col = event->x - line_len;
99 edit->prev_col = line_len;
100 } else {
101 edit->over_col = 0;
102 edit->prev_col = event->x;
104 } else {
105 edit->prev_col = event->x - edit->start_col - 1;
108 if (--event->y > (edit->curs_row + 1))
109 edit_move_down (edit, event->y - (edit->curs_row + 1), 0);
110 else if (event->y < (edit->curs_row + 1))
111 edit_move_up (edit, (edit->curs_row + 1) - event->y, 0);
112 else
113 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
115 if (event->type & GPM_DOWN) {
116 edit_mark_cmd (edit, 1); /* reset */
117 edit->highlight = 0;
120 if (!(event->type & GPM_DRAG))
121 edit_mark_cmd (edit, 0);
123 update:
124 edit_find_bracket (edit);
125 edit->force |= REDRAW_COMPLETELY;
126 edit_update_curs_row (edit);
127 edit_update_curs_col (edit);
128 edit_update_screen (edit);
130 return 1;
134 static int
135 edit_mouse_event (Gpm_Event *event, void *x)
137 int result;
138 if (edit_event ((WEdit *) x, event, &result))
139 return result;
140 else
141 return (*edit_menubar->widget.mouse) (event, edit_menubar);
144 static void
145 edit_adjust_size (Dlg_head *h)
147 WEdit *edit;
148 WButtonBar *edit_bar;
150 edit = (WEdit *) find_widget_type (h, edit_callback);
151 edit_bar = find_buttonbar (h);
153 widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
154 widget_set_size ((Widget *) edit_bar, LINES - 1, 0, 1, COLS);
155 widget_set_size (&edit_menubar->widget, 0, 0, 1, COLS);
157 #ifdef RESIZABLE_MENUBAR
158 menubar_arrange (edit_menubar);
159 #endif
162 /* Callback for the edit dialog */
163 static cb_ret_t
164 edit_dialog_callback (Dlg_head *h, dlg_msg_t msg, int parm)
166 WEdit *edit;
168 switch (msg) {
169 case DLG_RESIZE:
170 edit_adjust_size (h);
171 return MSG_HANDLED;
173 case DLG_VALIDATE:
174 edit = (WEdit *) find_widget_type (h, edit_callback);
175 if (!edit_ok_to_exit (edit)) {
176 h->running = 1;
178 return MSG_HANDLED;
180 default:
181 return default_dlg_callback (h, msg, parm);
186 edit_file (const char *_file, int line)
188 static int made_directory = 0;
189 Dlg_head *edit_dlg;
190 WButtonBar *edit_bar;
192 if (!made_directory) {
193 char *dir = concat_dir_and_file (home_dir, EDIT_DIR);
194 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
195 g_free (dir);
198 if (!(wedit = edit_init (NULL, LINES - 2, COLS, _file, line))) {
199 return 0;
202 /* Create a new dialog and add it widgets to it */
203 edit_dlg =
204 create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
205 "[Internal File Editor]", NULL, DLG_WANT_TAB);
207 init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
208 edit_callback,
209 edit_mouse_event);
211 widget_want_cursor (wedit->widget, 1);
213 edit_bar = buttonbar_new (1);
215 edit_menubar = edit_create_menu ();
217 add_widget (edit_dlg, edit_bar);
218 add_widget (edit_dlg, wedit);
219 add_widget (edit_dlg, edit_menubar);
221 run_dlg (edit_dlg);
223 edit_done_menu (edit_menubar); /* editmenu.c */
225 destroy_dlg (edit_dlg);
227 return 1;
230 const char *
231 edit_get_file_name (const WEdit *edit)
233 return edit->filename;
236 static void edit_my_define (Dlg_head * h, int idx, const char *text,
237 void (*fn) (WEdit *), WEdit * edit)
239 text = edit->labels[idx - 1]? edit->labels[idx - 1] : text;
240 /* function-cast ok */
241 buttonbar_set_label_data (h, idx, text, (buttonbarfn) fn, edit);
245 static void cmd_F1 (WEdit * edit)
247 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (1));
250 static void cmd_F2 (WEdit * edit)
252 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (2));
255 static void cmd_F3 (WEdit * edit)
257 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (3));
260 static void cmd_F4 (WEdit * edit)
262 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (4));
265 static void cmd_F5 (WEdit * edit)
267 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (5));
270 static void cmd_F6 (WEdit * edit)
272 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (6));
275 static void cmd_F7 (WEdit * edit)
277 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (7));
280 static void cmd_F8 (WEdit * edit)
282 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (8));
285 #if 0
286 static void cmd_F9 (WEdit * edit)
288 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (9));
290 #endif
292 static void cmd_F10 (WEdit * edit)
294 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (10));
297 static void
298 edit_labels (WEdit *edit)
300 Dlg_head *h = edit->widget.parent;
302 edit_my_define (h, 1, _("Help"), cmd_F1, edit);
303 edit_my_define (h, 2, _("Save"), cmd_F2, edit);
304 edit_my_define (h, 3, _("Mark"), cmd_F3, edit);
305 edit_my_define (h, 4, _("Replac"), cmd_F4, edit);
306 edit_my_define (h, 5, _("Copy"), cmd_F5, edit);
307 edit_my_define (h, 6, _("Move"), cmd_F6, edit);
308 edit_my_define (h, 7, _("Search"), cmd_F7, edit);
309 edit_my_define (h, 8, _("Delete"), cmd_F8, edit);
310 edit_my_define (h, 9, _("PullDn"), edit_menu_cmd, edit);
311 edit_my_define (h, 10, _("Quit"), cmd_F10, edit);
313 buttonbar_redraw (h);
316 void edit_update_screen (WEdit * e)
318 edit_scroll_screen_over_cursor (e);
320 edit_update_curs_col (e);
321 edit_status (e);
323 /* pop all events for this window for internal handling */
325 if (!is_idle ()) {
326 e->force |= REDRAW_PAGE;
327 return;
329 if (e->force & REDRAW_COMPLETELY)
330 e->force |= REDRAW_PAGE;
331 edit_render_keypress (e);
334 static cb_ret_t
335 edit_callback (Widget *w, widget_msg_t msg, int parm)
337 WEdit *e = (WEdit *) w;
339 switch (msg) {
340 case WIDGET_INIT:
341 e->force |= REDRAW_COMPLETELY;
342 edit_labels (e);
343 return MSG_HANDLED;
345 case WIDGET_DRAW:
346 e->force |= REDRAW_COMPLETELY;
347 e->num_widget_lines = LINES - 2;
348 e->num_widget_columns = COLS;
349 /* fallthrough */
351 case WIDGET_FOCUS:
352 edit_update_screen (e);
353 return MSG_HANDLED;
355 case WIDGET_KEY:
357 int cmd, ch;
359 /* The user may override the access-keys for the menu bar. */
360 if (edit_translate_key (e, parm, &cmd, &ch)) {
361 edit_execute_key_command (e, cmd, ch);
362 edit_update_screen (e);
363 return MSG_HANDLED;
364 } else if (edit_drop_hotkey_menu (e, parm)) {
365 return MSG_HANDLED;
366 } else {
367 return MSG_NOT_HANDLED;
371 case WIDGET_CURSOR:
372 widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
373 e->curs_col + e->start_col + e->over_col +
374 EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width);
375 return MSG_HANDLED;
377 case WIDGET_DESTROY:
378 edit_clean (e);
379 return MSG_HANDLED;
381 default:
382 return default_proc (msg, parm);