Removed type SHELL_ESCAPED_STR in favour of plain char*
[midnight-commander.git] / edit / editwidget.c
blob8fb8c147194fde43c7ff570966b6263906f32196
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 #include <config.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <sys/types.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <string.h>
33 #include <ctype.h>
34 #include <errno.h>
35 #include <sys/stat.h>
36 #include <stdlib.h>
38 #include <mhl/memory.h>
39 #include <mhl/string.h>
41 #include "../src/global.h"
43 #include "edit.h"
44 #include "edit-widget.h"
46 #include "../src/tty.h" /* LINES */
47 #include "../src/widget.h" /* buttonbar_redraw() */
48 #include "../src/menu.h" /* menubar_new() */
49 #include "../src/key.h" /* is_idle() */
51 WEdit *wedit;
52 struct WMenu *edit_menubar;
54 int column_highlighting = 0;
56 static cb_ret_t edit_callback (Widget *, widget_msg_t msg, int parm);
58 static int
59 edit_event (WEdit * edit, Gpm_Event * event, int *result)
61 *result = MOU_NORMAL;
62 edit_update_curs_row (edit);
63 edit_update_curs_col (edit);
65 /* Unknown event type */
66 if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
67 return 0;
69 /* Wheel events */
70 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
71 edit_move_up (edit, 2, 1);
72 goto update;
74 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
75 edit_move_down (edit, 2, 1);
76 goto update;
79 /* Outside editor window */
80 if (event->y <= 1 || event->x <= 0
81 || event->x > edit->num_widget_columns
82 || event->y > edit->num_widget_lines + 1)
83 return 0;
85 /* A lone up mustn't do anything */
86 if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
87 return 1;
89 if (event->type & (GPM_DOWN | GPM_UP))
90 edit_push_key_press (edit);
92 edit->prev_col = event->x - edit->start_col - 1;
94 if (--event->y > (edit->curs_row + 1))
95 edit_move_down (edit, event->y - (edit->curs_row + 1), 0);
96 else if (event->y < (edit->curs_row + 1))
97 edit_move_up (edit, (edit->curs_row + 1) - event->y, 0);
98 else
99 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
101 if (event->type & GPM_DOWN) {
102 edit_mark_cmd (edit, 1); /* reset */
103 edit->highlight = 0;
106 if (!(event->type & GPM_DRAG))
107 edit_mark_cmd (edit, 0);
109 update:
110 edit_find_bracket (edit);
111 edit->force |= REDRAW_COMPLETELY;
112 edit_update_curs_row (edit);
113 edit_update_curs_col (edit);
114 edit_update_screen (edit);
116 return 1;
120 static int
121 edit_mouse_event (Gpm_Event *event, void *x)
123 int result;
124 if (edit_event ((WEdit *) x, event, &result))
125 return result;
126 else
127 return (*edit_menubar->widget.mouse) (event, edit_menubar);
130 static void
131 edit_adjust_size (Dlg_head *h)
133 WEdit *edit;
134 WButtonBar *edit_bar;
136 edit = (WEdit *) find_widget_type (h, edit_callback);
137 edit_bar = find_buttonbar (h);
139 widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
140 widget_set_size ((Widget *) edit_bar, LINES - 1, 0, 1, COLS);
141 widget_set_size (&edit_menubar->widget, 0, 0, 1, COLS);
143 #ifdef RESIZABLE_MENUBAR
144 menubar_arrange (edit_menubar);
145 #endif
148 /* Callback for the edit dialog */
149 static cb_ret_t
150 edit_dialog_callback (Dlg_head *h, dlg_msg_t msg, int parm)
152 WEdit *edit;
154 switch (msg) {
155 case DLG_RESIZE:
156 edit_adjust_size (h);
157 return MSG_HANDLED;
159 case DLG_VALIDATE:
160 edit = (WEdit *) find_widget_type (h, edit_callback);
161 if (!edit_ok_to_exit (edit)) {
162 h->running = 1;
164 return MSG_HANDLED;
166 default:
167 return default_dlg_callback (h, msg, parm);
172 edit_file (const char *_file, int line)
174 static int made_directory = 0;
175 Dlg_head *edit_dlg;
176 WButtonBar *edit_bar;
178 if (!made_directory) {
179 char *dir = mhl_str_dir_plus_file (home_dir, EDIT_DIR);
180 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
181 g_free (dir);
184 if (!(wedit = edit_init (NULL, LINES - 2, COLS, _file, line))) {
185 return 0;
188 /* Create a new dialog and add it widgets to it */
189 edit_dlg =
190 create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
191 "[Internal File Editor]", NULL, DLG_WANT_TAB);
193 init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
194 edit_callback,
195 edit_mouse_event);
197 widget_want_cursor (wedit->widget, 1);
199 edit_bar = buttonbar_new (1);
201 edit_menubar = edit_init_menu ();
203 add_widget (edit_dlg, edit_bar);
204 add_widget (edit_dlg, wedit);
205 add_widget (edit_dlg, edit_menubar);
207 run_dlg (edit_dlg);
209 edit_done_menu (edit_menubar); /* editmenu.c */
211 destroy_dlg (edit_dlg);
213 return 1;
216 static void edit_my_define (Dlg_head * h, int idx, const char *text,
217 void (*fn) (WEdit *), WEdit * edit)
219 text = edit->labels[idx - 1]? edit->labels[idx - 1] : text;
220 /* function-cast ok */
221 buttonbar_set_label_data (h, idx, text, (buttonbarfn) fn, edit);
225 static void cmd_F1 (WEdit * edit)
227 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (1));
230 static void cmd_F2 (WEdit * edit)
232 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (2));
235 static void cmd_F3 (WEdit * edit)
237 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (3));
240 static void cmd_F4 (WEdit * edit)
242 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (4));
245 static void cmd_F5 (WEdit * edit)
247 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (5));
250 static void cmd_F6 (WEdit * edit)
252 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (6));
255 static void cmd_F7 (WEdit * edit)
257 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (7));
260 static void cmd_F8 (WEdit * edit)
262 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (8));
265 #if 0
266 static void cmd_F9 (WEdit * edit)
268 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (9));
270 #endif
272 static void cmd_F10 (WEdit * edit)
274 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (10));
277 static void
278 edit_labels (WEdit *edit)
280 Dlg_head *h = edit->widget.parent;
282 edit_my_define (h, 1, _("Help"), cmd_F1, edit);
283 edit_my_define (h, 2, _("Save"), cmd_F2, edit);
284 edit_my_define (h, 3, _("Mark"), cmd_F3, edit);
285 edit_my_define (h, 4, _("Replac"), cmd_F4, edit);
286 edit_my_define (h, 5, _("Copy"), cmd_F5, edit);
287 edit_my_define (h, 6, _("Move"), cmd_F6, edit);
288 edit_my_define (h, 7, _("Search"), cmd_F7, edit);
289 edit_my_define (h, 8, _("Delete"), cmd_F8, edit);
290 edit_my_define (h, 9, _("PullDn"), edit_menu_cmd, edit);
291 edit_my_define (h, 10, _("Quit"), cmd_F10, edit);
293 buttonbar_redraw (h);
296 void edit_update_screen (WEdit * e)
298 edit_scroll_screen_over_cursor (e);
300 edit_update_curs_col (e);
301 edit_status (e);
303 /* pop all events for this window for internal handling */
305 if (!is_idle ()) {
306 e->force |= REDRAW_PAGE;
307 return;
309 if (e->force & REDRAW_COMPLETELY)
310 e->force |= REDRAW_PAGE;
311 edit_render_keypress (e);
314 static cb_ret_t
315 edit_callback (Widget *w, widget_msg_t msg, int parm)
317 WEdit *e = (WEdit *) w;
319 switch (msg) {
320 case WIDGET_INIT:
321 e->force |= REDRAW_COMPLETELY;
322 edit_labels (e);
323 return MSG_HANDLED;
325 case WIDGET_DRAW:
326 e->force |= REDRAW_COMPLETELY;
327 e->num_widget_lines = LINES - 2;
328 e->num_widget_columns = COLS;
329 /* fallthrough */
331 case WIDGET_FOCUS:
332 edit_update_screen (e);
333 return MSG_HANDLED;
335 case WIDGET_KEY:
337 int cmd, ch;
339 /* The user may override the access-keys for the menu bar. */
340 if (edit_translate_key (e, parm, &cmd, &ch)) {
341 edit_execute_key_command (e, cmd, ch);
342 edit_update_screen (e);
343 return MSG_HANDLED;
344 } else if (edit_drop_hotkey_menu (e, parm)) {
345 return MSG_HANDLED;
346 } else {
347 return MSG_NOT_HANDLED;
351 case WIDGET_CURSOR:
352 widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
353 e->curs_col + e->start_col);
354 return MSG_HANDLED;
356 case WIDGET_DESTROY:
357 edit_clean (e);
358 return MSG_HANDLED;
360 default:
361 return default_proc (msg, parm);