Merge branch '1790_mc_crash'
[midnight-commander.git] / edit / editwidget.c
blob151270e241150f54b4ca80a6deacfd2458cca9d0
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() */
52 #include "../src/cmddef.h"
54 WEdit *wedit;
55 struct WMenuBar *edit_menubar;
57 int column_highlighting = 0;
59 static cb_ret_t edit_callback (Widget *, widget_msg_t msg, int parm);
61 static char *
62 edit_get_shortcut (int command)
64 const char *ext_map;
65 const char *shortcut = NULL;
67 ext_map = lookup_keymap_shortcut (editor_map, CK_Ext_Mode);
69 if (ext_map != NULL)
70 shortcut = lookup_keymap_shortcut (editor_x_map, command);
71 if (shortcut != NULL)
72 return g_strdup_printf ("%s %s", ext_map, shortcut);
74 shortcut = lookup_keymap_shortcut (editor_map, command);
75 if (shortcut != NULL)
76 return g_strdup (shortcut);
78 return NULL;
81 static int
82 edit_event (Gpm_Event *event, void *data)
84 WEdit *edit = (WEdit *) data;
86 /* Unknown event type */
87 if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
88 return MOU_NORMAL;
90 /* rest of the upper frame, the menu is invisible - call menu */
91 if ((event->type & GPM_DOWN) && (event->y == 1))
92 return edit_menubar->widget.mouse (event, edit_menubar);
94 edit_update_curs_row (edit);
95 edit_update_curs_col (edit);
97 /* Outside editor window */
98 if (event->y <= 1 || event->x <= 0
99 || event->x > edit->num_widget_columns
100 || event->y > edit->num_widget_lines + 1)
101 return MOU_NORMAL;
103 /* Wheel events */
104 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
105 edit_move_up (edit, 2, 1);
106 goto update;
108 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
109 edit_move_down (edit, 2, 1);
110 goto update;
113 /* A lone up mustn't do anything */
114 if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
115 return MOU_NORMAL;
117 if (event->type & (GPM_DOWN | GPM_UP))
118 edit_push_key_press (edit);
120 if (option_cursor_beyond_eol) {
121 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
122 edit_eol(edit, edit->curs1));
123 if ( event->x > line_len ) {
124 edit->over_col = event->x - line_len - option_line_state_width - 1;
125 edit->prev_col = line_len;
126 } else {
127 edit->over_col = 0;
128 edit->prev_col = event->x - option_line_state_width - 1;
130 } else {
131 edit->prev_col = event->x - edit->start_col - option_line_state_width - 1;
134 if (--event->y > (edit->curs_row + 1))
135 edit_move_down (edit, event->y - (edit->curs_row + 1), 0);
136 else if (event->y < (edit->curs_row + 1))
137 edit_move_up (edit, (edit->curs_row + 1) - event->y, 0);
138 else
139 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
141 if (event->type & GPM_DOWN) {
142 edit_mark_cmd (edit, 1); /* reset */
143 edit->highlight = 0;
146 if (!(event->type & GPM_DRAG))
147 edit_mark_cmd (edit, 0);
149 update:
150 edit_find_bracket (edit);
151 edit->force |= REDRAW_COMPLETELY;
152 edit_update_curs_row (edit);
153 edit_update_curs_col (edit);
154 edit_update_screen (edit);
156 return MOU_NORMAL;
159 static void
160 edit_adjust_size (Dlg_head *h)
162 WEdit *edit;
163 WButtonBar *edit_bar;
165 edit = (WEdit *) find_widget_type (h, edit_callback);
166 edit_bar = find_buttonbar (h);
168 widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
169 widget_set_size ((Widget *) edit_bar, LINES - 1, 0, 1, COLS);
170 widget_set_size (&edit_menubar->widget, 0, 0, 1, COLS);
172 #ifdef RESIZABLE_MENUBAR
173 menubar_arrange (edit_menubar);
174 #endif
177 /* Callback for the edit dialog */
178 static cb_ret_t
179 edit_dialog_callback (Dlg_head *h, dlg_msg_t msg, int parm)
181 WEdit *edit;
183 switch (msg) {
184 case DLG_RESIZE:
185 edit_adjust_size (h);
186 return MSG_HANDLED;
188 case DLG_VALIDATE:
189 edit = (WEdit *) find_widget_type (h, edit_callback);
190 if (!edit_ok_to_exit (edit)) {
191 h->running = 1;
193 return MSG_HANDLED;
195 default:
196 return default_dlg_callback (h, msg, parm);
201 edit_file (const char *_file, int line)
203 static int made_directory = 0;
204 Dlg_head *edit_dlg;
205 WButtonBar *edit_bar;
207 if (!made_directory) {
208 char *dir = concat_dir_and_file (home_dir, EDIT_DIR);
209 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
210 g_free (dir);
213 wedit = edit_init (NULL, LINES - 2, COLS, _file, line);
215 if (wedit == NULL)
216 return 0;
218 /* Create a new dialog and add it widgets to it */
219 edit_dlg =
220 create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
221 "[Internal File Editor]", NULL, DLG_WANT_TAB);
223 init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
224 edit_callback, edit_event);
226 widget_want_cursor (wedit->widget, 1);
228 edit_bar = buttonbar_new (1);
229 add_widget (edit_dlg, edit_bar);
231 add_widget (edit_dlg, wedit);
233 edit_dlg->menu_executor = edit_menu_execute;
234 edit_dlg->get_shortcut = edit_get_shortcut;
235 edit_menubar = menubar_new (0, 0, COLS, NULL);
236 add_widget (edit_dlg, edit_menubar);
237 edit_init_menu (edit_menubar);
239 run_dlg (edit_dlg);
241 destroy_dlg (edit_dlg);
243 return 1;
246 const char *
247 edit_get_file_name (const WEdit *edit)
249 return edit->filename;
252 static void edit_my_define (Dlg_head * h, int idx, const char *text,
253 void (*fn) (WEdit *), WEdit * edit)
255 text = edit->labels[idx - 1]? edit->labels[idx - 1] : text;
256 /* function-cast ok */
257 buttonbar_set_label_data (h, idx, text, (buttonbarfn) fn, edit);
261 static void cmd_F1 (WEdit * edit)
263 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (1));
266 static void cmd_F2 (WEdit * edit)
268 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (2));
271 static void cmd_F3 (WEdit * edit)
273 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (3));
276 static void cmd_F4 (WEdit * edit)
278 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (4));
281 static void cmd_F5 (WEdit * edit)
283 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (5));
286 static void cmd_F6 (WEdit * edit)
288 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (6));
291 static void cmd_F7 (WEdit * edit)
293 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (7));
296 static void cmd_F8 (WEdit * edit)
298 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (8));
301 #if 0
302 static void cmd_F9 (WEdit * edit)
304 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (9));
306 #endif
308 static void cmd_F10 (WEdit * edit)
310 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (10));
313 static void
314 edit_labels (WEdit *edit)
316 Dlg_head *h = edit->widget.parent;
318 edit_my_define (h, 1, Q_("ButtonBar|Help"), cmd_F1, edit);
319 edit_my_define (h, 2, Q_("ButtonBar|Save"), cmd_F2, edit);
320 edit_my_define (h, 3, Q_("ButtonBar|Mark"), cmd_F3, edit);
321 edit_my_define (h, 4, Q_("ButtonBar|Replac"), cmd_F4, edit);
322 edit_my_define (h, 5, Q_("ButtonBar|Copy"), cmd_F5, edit);
323 edit_my_define (h, 6, Q_("ButtonBar|Move"), cmd_F6, edit);
324 edit_my_define (h, 7, Q_("ButtonBar|Search"), cmd_F7, edit);
325 edit_my_define (h, 8, Q_("ButtonBar|Delete"), cmd_F8, edit);
326 edit_my_define (h, 9, Q_("ButtonBar|PullDn"), edit_menu_cmd, edit);
327 edit_my_define (h, 10, Q_("ButtonBar|Quit"), cmd_F10, edit);
329 buttonbar_redraw (h);
332 void edit_update_screen (WEdit * e)
334 edit_scroll_screen_over_cursor (e);
336 edit_update_curs_col (e);
337 edit_status (e);
339 /* pop all events for this window for internal handling */
341 if (!is_idle ()) {
342 e->force |= REDRAW_PAGE;
343 return;
345 if (e->force & REDRAW_COMPLETELY)
346 e->force |= REDRAW_PAGE;
347 edit_render_keypress (e);
350 static cb_ret_t
351 edit_callback (Widget *w, widget_msg_t msg, int parm)
353 WEdit *e = (WEdit *) w;
355 switch (msg) {
356 case WIDGET_INIT:
357 e->force |= REDRAW_COMPLETELY;
358 edit_labels (e);
359 return MSG_HANDLED;
361 case WIDGET_DRAW:
362 e->force |= REDRAW_COMPLETELY;
363 e->num_widget_lines = LINES - 2;
364 e->num_widget_columns = COLS;
365 /* fallthrough */
367 case WIDGET_FOCUS:
368 edit_update_screen (e);
369 return MSG_HANDLED;
371 case WIDGET_KEY:
373 int cmd, ch;
375 /* The user may override the access-keys for the menu bar. */
376 if (edit_translate_key (e, parm, &cmd, &ch)) {
377 edit_execute_key_command (e, cmd, ch);
378 edit_update_screen (e);
379 return MSG_HANDLED;
380 } else if (edit_drop_hotkey_menu (e, parm)) {
381 return MSG_HANDLED;
382 } else {
383 return MSG_NOT_HANDLED;
387 case WIDGET_CURSOR:
388 widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
389 e->curs_col + e->start_col + e->over_col +
390 EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width);
391 return MSG_HANDLED;
393 case WIDGET_DESTROY:
394 edit_clean (e);
395 return MSG_HANDLED;
397 default:
398 return default_proc (msg, parm);