Added modal flag to the dialog structure.
[midnight-commander.git] / src / editor / editwidget.c
blobb4c95621a3da5cf0654df7cf0eb62dacf90290f2
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 "lib/global.h"
44 #include "lib/tty/tty.h" /* LINES, COLS */
45 #include "lib/tty/key.h" /* is_idle() */
47 #include "edit-impl.h"
48 #include "edit-widget.h"
50 #include "src/dialog.h"
51 #include "src/widget.h" /* ButtonBar */
52 #include "src/menu.h" /* menubar_new() */
53 #include "src/cmddef.h"
54 #include "src/keybind.h"
56 int column_highlighting = 0;
58 static cb_ret_t edit_callback (Widget *, widget_msg_t msg, int parm);
60 static char *
61 edit_get_shortcut (unsigned long command)
63 const char *ext_map;
64 const char *shortcut = NULL;
66 shortcut = lookup_keymap_shortcut (editor_map, command);
67 if (shortcut != NULL)
68 return g_strdup (shortcut);
70 ext_map = lookup_keymap_shortcut (editor_map, CK_Ext_Mode);
71 if (ext_map != NULL)
72 shortcut = lookup_keymap_shortcut (editor_x_map, command);
73 if (shortcut != NULL)
74 return g_strdup_printf ("%s %s", ext_map, shortcut);
76 return NULL;
79 static int
80 edit_event (Gpm_Event * event, void *data)
82 WEdit *edit = (WEdit *) data;
84 /* Unknown event type */
85 if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
86 return MOU_NORMAL;
88 /* rest of the upper frame, the menu is invisible - call menu */
89 if ((event->type & GPM_DOWN) && (event->y == 1))
91 WMenuBar *menubar;
93 menubar = find_menubar (edit->widget.owner);
95 return menubar->widget.mouse (event, menubar);
98 edit_update_curs_row (edit);
99 edit_update_curs_col (edit);
101 /* Outside editor window */
102 if (event->y <= 1 || event->x <= 0
103 || event->x > edit->num_widget_columns || event->y > edit->num_widget_lines + 1)
104 return MOU_NORMAL;
106 /* Wheel events */
107 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN))
109 edit_move_up (edit, 2, 1);
110 goto update;
112 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN))
114 edit_move_down (edit, 2, 1);
115 goto update;
118 /* A lone up mustn't do anything */
119 if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
120 return MOU_NORMAL;
122 if (event->type & (GPM_DOWN | GPM_UP))
123 edit_push_key_press (edit);
125 if (option_cursor_beyond_eol)
127 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
128 edit_eol (edit, edit->curs1));
130 if (event->x > line_len)
132 edit->over_col = event->x - line_len - edit->start_col - option_line_state_width - 1;
133 edit->prev_col = line_len;
135 else
137 edit->over_col = 0;
138 edit->prev_col = event->x - option_line_state_width - edit->start_col - 1;
141 else
143 edit->prev_col = event->x - edit->start_col - option_line_state_width - 1;
146 if (--event->y > (edit->curs_row + 1))
147 edit_move_down (edit, event->y - (edit->curs_row + 1), 0);
148 else if (event->y < (edit->curs_row + 1))
149 edit_move_up (edit, (edit->curs_row + 1) - event->y, 0);
150 else
151 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
153 if (event->type & GPM_DOWN)
155 edit_mark_cmd (edit, 1); /* reset */
156 edit->highlight = 0;
159 if (!(event->type & GPM_DRAG))
160 edit_mark_cmd (edit, 0);
162 update:
163 edit_find_bracket (edit);
164 edit->force |= REDRAW_COMPLETELY;
165 edit_update_curs_row (edit);
166 edit_update_curs_col (edit);
167 edit_update_screen (edit);
169 return MOU_NORMAL;
172 static cb_ret_t
173 edit_command_execute (WEdit * edit, unsigned long command)
175 if (command == CK_Menu)
176 edit_menu_cmd (edit);
177 else
179 edit_execute_key_command (edit, command, -1);
180 edit_update_screen (edit);
182 return MSG_HANDLED;
185 static inline void
186 edit_set_buttonbar (WEdit * edit, WButtonBar * bb)
188 buttonbar_set_label (bb, 1, Q_ ("ButtonBar|Help"), editor_map, (Widget *) edit);
189 buttonbar_set_label (bb, 2, Q_ ("ButtonBar|Save"), editor_map, (Widget *) edit);
190 buttonbar_set_label (bb, 3, Q_ ("ButtonBar|Mark"), editor_map, (Widget *) edit);
191 buttonbar_set_label (bb, 4, Q_ ("ButtonBar|Replac"), editor_map, (Widget *) edit);
192 buttonbar_set_label (bb, 5, Q_ ("ButtonBar|Copy"), editor_map, (Widget *) edit);
193 buttonbar_set_label (bb, 6, Q_ ("ButtonBar|Move"), editor_map, (Widget *) edit);
194 buttonbar_set_label (bb, 7, Q_ ("ButtonBar|Search"), editor_map, (Widget *) edit);
195 buttonbar_set_label (bb, 8, Q_ ("ButtonBar|Delete"), editor_map, (Widget *) edit);
196 buttonbar_set_label (bb, 9, Q_ ("ButtonBar|PullDn"), editor_map, (Widget *) edit);
197 buttonbar_set_label (bb, 10, Q_ ("ButtonBar|Quit"), editor_map, (Widget *) edit);
200 /* Callback for the edit dialog */
201 static cb_ret_t
202 edit_dialog_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
204 WEdit *edit;
205 WMenuBar *menubar;
206 WButtonBar *buttonbar;
208 edit = (WEdit *) find_widget_type (h, edit_callback);
209 menubar = find_menubar (h);
210 buttonbar = find_buttonbar (h);
212 switch (msg)
214 case DLG_INIT:
215 edit_set_buttonbar (edit, buttonbar);
216 return MSG_HANDLED;
218 case DLG_RESIZE:
219 widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
220 widget_set_size (&buttonbar->widget, LINES - 1, 0, 1, COLS);
221 widget_set_size (&menubar->widget, 0, 0, 1, COLS);
222 menubar_arrange (menubar);
223 return MSG_HANDLED;
225 case DLG_ACTION:
226 if (sender == (Widget *) menubar)
227 return send_message ((Widget *) edit, WIDGET_COMMAND, parm);
228 if (sender == (Widget *) buttonbar)
229 return send_message ((Widget *) edit, WIDGET_COMMAND, parm);
230 return MSG_HANDLED;
232 case DLG_VALIDATE:
233 if (!edit_ok_to_exit (edit))
234 h->state = DLG_ACTIVE;
235 else
236 h->state = DLG_CLOSED;
237 return MSG_HANDLED;
239 default:
240 return default_dlg_callback (h, sender, msg, parm, data);
245 edit_file (const char *_file, int line)
247 static gboolean made_directory = FALSE;
248 Dlg_head *edit_dlg;
249 WEdit *wedit;
250 WMenuBar *menubar;
252 if (!made_directory)
254 char *dir = concat_dir_and_file (home_dir, EDIT_DIR);
255 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
256 g_free (dir);
259 wedit = edit_init (NULL, LINES - 2, COLS, _file, line);
261 if (wedit == NULL)
262 return 0;
264 /* Create a new dialog and add it widgets to it */
265 edit_dlg =
266 create_dlg (FALSE, 0, 0, LINES, COLS, NULL, edit_dialog_callback,
267 "[Internal File Editor]", NULL, DLG_WANT_TAB);
269 edit_dlg->get_shortcut = edit_get_shortcut;
270 menubar = menubar_new (0, 0, COLS, NULL);
271 add_widget (edit_dlg, menubar);
272 edit_init_menu (menubar);
274 init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS, edit_callback, edit_event);
275 widget_want_cursor (wedit->widget, 1);
277 add_widget (edit_dlg, wedit);
279 add_widget (edit_dlg, buttonbar_new (TRUE));
281 run_dlg (edit_dlg);
283 destroy_dlg (edit_dlg);
285 return 1;
288 const char *
289 edit_get_file_name (const WEdit * edit)
291 return edit->filename;
294 void
295 edit_update_screen (WEdit * e)
297 edit_scroll_screen_over_cursor (e);
299 edit_update_curs_col (e);
300 edit_status (e);
302 /* pop all events for this window for internal handling */
303 if (!is_idle ())
304 e->force |= REDRAW_PAGE;
305 else
307 if (e->force & REDRAW_COMPLETELY)
308 e->force |= REDRAW_PAGE;
309 edit_render_keypress (e);
313 static cb_ret_t
314 edit_callback (Widget * w, widget_msg_t msg, int parm)
316 WEdit *e = (WEdit *) w;
318 switch (msg)
320 case WIDGET_DRAW:
321 e->force |= REDRAW_COMPLETELY;
322 e->num_widget_lines = LINES - 2;
323 e->num_widget_columns = COLS;
324 /* fallthrough */
326 case WIDGET_FOCUS:
327 edit_update_screen (e);
328 return MSG_HANDLED;
330 case WIDGET_KEY:
332 int cmd, ch;
333 cb_ret_t ret = MSG_NOT_HANDLED;
335 /* The user may override the access-keys for the menu bar. */
336 if (edit_translate_key (e, parm, &cmd, &ch))
338 edit_execute_key_command (e, cmd, ch);
339 edit_update_screen (e);
340 ret = MSG_HANDLED;
342 else if (edit_drop_hotkey_menu (e, parm))
343 ret = MSG_HANDLED;
345 return ret;
348 case WIDGET_COMMAND:
349 /* command from menubar or buttonbar */
350 return edit_command_execute (e, parm);
352 case WIDGET_CURSOR:
353 widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
354 e->curs_col + e->start_col + e->over_col +
355 EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width);
356 return MSG_HANDLED;
358 case WIDGET_DESTROY:
359 edit_clean (e);
360 return MSG_HANDLED;
362 default:
363 return default_proc (msg, parm);