Ticket #2237: Automatic date and version substitution for man pages
[midnight-commander.git] / src / editor / editwidget.c
blobed61fa510d3d6257d451bb1141fe2c345188bdb4
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() */
46 #include "lib/strutil.h" /* str_term_trim() */
48 #include "edit-impl.h"
49 #include "edit-widget.h"
51 #include "src/dialog.h"
52 #include "src/widget.h" /* ButtonBar */
53 #include "src/menu.h" /* menubar_new() */
54 #include "src/cmddef.h"
55 #include "src/keybind.h"
57 static cb_ret_t edit_callback (Widget *, widget_msg_t msg, int parm);
59 static char *
60 edit_get_shortcut (unsigned long command)
62 const char *ext_map;
63 const char *shortcut = NULL;
65 shortcut = lookup_keymap_shortcut (editor_map, command);
66 if (shortcut != NULL)
67 return g_strdup (shortcut);
69 ext_map = lookup_keymap_shortcut (editor_map, CK_Ext_Mode);
70 if (ext_map != NULL)
71 shortcut = lookup_keymap_shortcut (editor_x_map, command);
72 if (shortcut != NULL)
73 return g_strdup_printf ("%s %s", ext_map, shortcut);
75 return NULL;
78 static char *
79 edit_get_title (const Dlg_head *h, size_t len)
81 const WEdit *edit = (const WEdit *) find_widget_type (h, edit_callback);
82 const char *modified = edit->modified ? "(*) " : " ";
83 const char *file_label;
85 len -= 4;
87 file_label = str_term_trim (edit->filename, len - str_term_width1 (_("Edit: ")));
89 return g_strconcat (_("Edit: "), modified, file_label, (char *) NULL);
92 static int
93 edit_event (Gpm_Event * event, void *data)
95 WEdit *edit = (WEdit *) data;
97 /* Unknown event type */
98 if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
99 return MOU_NORMAL;
101 /* rest of the upper frame, the menu is invisible - call menu */
102 if ((event->type & GPM_DOWN) && (event->y == 1))
104 WMenuBar *menubar;
106 menubar = find_menubar (edit->widget.owner);
108 return menubar->widget.mouse (event, menubar);
111 edit_update_curs_row (edit);
112 edit_update_curs_col (edit);
114 /* Outside editor window */
115 if (event->y <= 1 || event->x <= 0
116 || event->x > edit->num_widget_columns || event->y > edit->num_widget_lines + 1)
117 return MOU_NORMAL;
119 /* Wheel events */
120 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN))
122 edit_move_up (edit, 2, 1);
123 goto update;
125 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN))
127 edit_move_down (edit, 2, 1);
128 goto update;
131 /* A lone up mustn't do anything */
132 if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
133 return MOU_NORMAL;
135 if (event->type & (GPM_DOWN | GPM_UP))
136 edit_push_key_press (edit);
138 if (option_cursor_beyond_eol)
140 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
141 edit_eol (edit, edit->curs1));
143 if (event->x > line_len)
145 edit->over_col = event->x - line_len - edit->start_col - option_line_state_width - 1;
146 edit->prev_col = line_len;
148 else
150 edit->over_col = 0;
151 edit->prev_col = event->x - option_line_state_width - edit->start_col - 1;
154 else
156 edit->prev_col = event->x - edit->start_col - option_line_state_width - 1;
159 if (--event->y > (edit->curs_row + 1))
160 edit_move_down (edit, event->y - (edit->curs_row + 1), 0);
161 else if (event->y < (edit->curs_row + 1))
162 edit_move_up (edit, (edit->curs_row + 1) - event->y, 0);
163 else
164 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
166 if (event->type & GPM_DOWN)
168 edit_mark_cmd (edit, 1); /* reset */
169 edit->highlight = 0;
172 if (!(event->type & GPM_DRAG))
173 edit_mark_cmd (edit, 0);
175 update:
176 edit_find_bracket (edit);
177 edit->force |= REDRAW_COMPLETELY;
178 edit_update_curs_row (edit);
179 edit_update_curs_col (edit);
180 edit_update_screen (edit);
182 return MOU_NORMAL;
185 static cb_ret_t
186 edit_command_execute (WEdit * edit, unsigned long command)
188 if (command == CK_Menu)
189 edit_menu_cmd (edit);
190 else
192 edit_execute_key_command (edit, command, -1);
193 edit_update_screen (edit);
195 return MSG_HANDLED;
198 static inline void
199 edit_set_buttonbar (WEdit * edit, WButtonBar * bb)
201 buttonbar_set_label (bb, 1, Q_ ("ButtonBar|Help"), editor_map, (Widget *) edit);
202 buttonbar_set_label (bb, 2, Q_ ("ButtonBar|Save"), editor_map, (Widget *) edit);
203 buttonbar_set_label (bb, 3, Q_ ("ButtonBar|Mark"), editor_map, (Widget *) edit);
204 buttonbar_set_label (bb, 4, Q_ ("ButtonBar|Replac"), editor_map, (Widget *) edit);
205 buttonbar_set_label (bb, 5, Q_ ("ButtonBar|Copy"), editor_map, (Widget *) edit);
206 buttonbar_set_label (bb, 6, Q_ ("ButtonBar|Move"), editor_map, (Widget *) edit);
207 buttonbar_set_label (bb, 7, Q_ ("ButtonBar|Search"), editor_map, (Widget *) edit);
208 buttonbar_set_label (bb, 8, Q_ ("ButtonBar|Delete"), editor_map, (Widget *) edit);
209 buttonbar_set_label (bb, 9, Q_ ("ButtonBar|PullDn"), editor_map, (Widget *) edit);
210 buttonbar_set_label (bb, 10, Q_ ("ButtonBar|Quit"), editor_map, (Widget *) edit);
213 /* Callback for the edit dialog */
214 static cb_ret_t
215 edit_dialog_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
217 WEdit *edit;
218 WMenuBar *menubar;
219 WButtonBar *buttonbar;
221 edit = (WEdit *) find_widget_type (h, edit_callback);
222 menubar = find_menubar (h);
223 buttonbar = find_buttonbar (h);
225 switch (msg)
227 case DLG_INIT:
228 edit_set_buttonbar (edit, buttonbar);
229 return MSG_HANDLED;
231 case DLG_RESIZE:
232 widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
233 widget_set_size (&buttonbar->widget, LINES - 1, 0, 1, COLS);
234 widget_set_size (&menubar->widget, 0, 0, 1, COLS);
235 menubar_arrange (menubar);
236 return MSG_HANDLED;
238 case DLG_ACTION:
239 if (sender == (Widget *) menubar)
240 return send_message ((Widget *) edit, WIDGET_COMMAND, parm);
241 if (sender == (Widget *) buttonbar)
242 return send_message ((Widget *) edit, WIDGET_COMMAND, parm);
243 return MSG_HANDLED;
245 case DLG_VALIDATE:
246 if (!edit_ok_to_exit (edit))
247 h->state = DLG_ACTIVE;
248 else
249 h->state = DLG_CLOSED;
250 return MSG_HANDLED;
252 default:
253 return default_dlg_callback (h, sender, msg, parm, data);
258 edit_file (const char *_file, int line)
260 static gboolean made_directory = FALSE;
261 Dlg_head *edit_dlg;
262 WEdit *wedit;
263 WMenuBar *menubar;
265 if (!made_directory)
267 char *dir = concat_dir_and_file (home_dir, EDIT_DIR);
268 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
269 g_free (dir);
272 wedit = edit_init (NULL, LINES - 2, COLS, _file, line);
274 if (wedit == NULL)
275 return 0;
277 /* Create a new dialog and add it widgets to it */
278 edit_dlg =
279 create_dlg (FALSE, 0, 0, LINES, COLS, NULL, edit_dialog_callback,
280 "[Internal File Editor]", NULL, DLG_WANT_TAB);
282 edit_dlg->get_shortcut = edit_get_shortcut;
283 edit_dlg->get_title = edit_get_title;
285 menubar = menubar_new (0, 0, COLS, NULL);
286 add_widget (edit_dlg, menubar);
287 edit_init_menu (menubar);
289 init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS, edit_callback, edit_event);
290 widget_want_cursor (wedit->widget, 1);
292 add_widget (edit_dlg, wedit);
294 add_widget (edit_dlg, buttonbar_new (TRUE));
296 run_dlg (edit_dlg);
298 if (edit_dlg->state == DLG_CLOSED)
299 destroy_dlg (edit_dlg);
301 return 1;
304 const char *
305 edit_get_file_name (const WEdit * edit)
307 return edit->filename;
310 void
311 edit_update_screen (WEdit * e)
313 edit_scroll_screen_over_cursor (e);
315 edit_update_curs_col (e);
316 edit_status (e);
318 /* pop all events for this window for internal handling */
319 if (!is_idle ())
320 e->force |= REDRAW_PAGE;
321 else
323 if (e->force & REDRAW_COMPLETELY)
324 e->force |= REDRAW_PAGE;
325 edit_render_keypress (e);
329 static cb_ret_t
330 edit_callback (Widget * w, widget_msg_t msg, int parm)
332 WEdit *e = (WEdit *) w;
334 switch (msg)
336 case WIDGET_DRAW:
337 e->force |= REDRAW_COMPLETELY;
338 e->num_widget_lines = LINES - 2;
339 e->num_widget_columns = COLS;
340 /* fallthrough */
342 case WIDGET_FOCUS:
343 edit_update_screen (e);
344 return MSG_HANDLED;
346 case WIDGET_KEY:
348 int cmd, ch;
349 cb_ret_t ret = MSG_NOT_HANDLED;
351 /* The user may override the access-keys for the menu bar. */
352 if (edit_translate_key (e, parm, &cmd, &ch))
354 edit_execute_key_command (e, cmd, ch);
355 edit_update_screen (e);
356 ret = MSG_HANDLED;
358 else if (edit_drop_hotkey_menu (e, parm))
359 ret = MSG_HANDLED;
361 return ret;
364 case WIDGET_COMMAND:
365 /* command from menubar or buttonbar */
366 return edit_command_execute (e, parm);
368 case WIDGET_CURSOR:
369 widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
370 e->curs_col + e->start_col + e->over_col +
371 EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width);
372 return MSG_HANDLED;
374 case WIDGET_DESTROY:
375 edit_clean (e);
376 return MSG_HANDLED;
378 default:
379 return default_proc (msg, parm);