Chosmetics changes to reflect /doc/mc.1.in, some .TP added in Listing Mode... and...
[midnight-commander.git] / edit / editwidget.c
blob3616d23c493b9d5dc4afc99aa1ff043655832cc4
1 /* editor initialisation and callback handler.
3 Copyright (C) 1996, 1997 the Free Software Foundation
5 Authors: 1996, 1997 Paul Sheer
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
23 #include <config.h>
24 #include "edit.h"
26 WEdit *wedit;
27 WMenu *edit_menubar;
29 int column_highlighting = 0;
31 static int edit_callback (Dlg_head * h, WEdit * edit, int msg, int par);
33 int
34 edit_event (WEdit * edit, Gpm_Event * event, int *result)
36 *result = MOU_NORMAL;
37 edit_update_curs_row (edit);
38 edit_update_curs_col (edit);
40 /* Unknown event type */
41 if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
42 return 0;
44 /* Wheel events */
45 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
46 edit_move_up (edit, 2, 1);
47 goto update;
49 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
50 edit_move_down (edit, 2, 1);
51 goto update;
54 /* Outside editor window */
55 if (event->y <= 1 || event->x <= 0
56 || event->x > edit->num_widget_columns
57 || event->y > edit->num_widget_lines + 1)
58 return 0;
60 /* A lone up mustn't do anything */
61 if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
62 return 1;
64 if (event->type & (GPM_DOWN | GPM_UP))
65 edit_push_key_press (edit);
67 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
69 if (--event->y > (edit->curs_row + 1))
70 edit_cursor_move (edit,
71 edit_move_forward (edit, edit->curs1,
72 event->y - (edit->curs_row +
73 1), 0)
74 - edit->curs1);
76 if (event->y < (edit->curs_row + 1))
77 edit_cursor_move (edit,
78 edit_move_backward (edit, edit->curs1,
79 (edit->curs_row + 1) -
80 event->y) - edit->curs1);
82 edit_cursor_move (edit,
83 (int) edit_move_forward3 (edit, edit->curs1,
84 event->x -
85 edit->start_col - 1,
86 0) - edit->curs1);
88 edit->prev_col = edit_get_col (edit);
90 if (event->type & GPM_DOWN) {
91 edit_mark_cmd (edit, 1); /* reset */
92 edit->highlight = 0;
95 if (!(event->type & GPM_DRAG))
96 edit_mark_cmd (edit, 0);
98 update:
99 edit->force |= REDRAW_COMPLETELY;
100 edit_update_curs_row (edit);
101 edit_update_curs_col (edit);
102 edit_update_screen (edit);
104 return 1;
108 int menubar_event (Gpm_Event * event, WMenu * menubar); /* menu.c */
110 int edit_mouse_event (Gpm_Event * event, void *x)
112 int result;
113 if (edit_event ((WEdit *) x, event, &result))
114 return result;
115 else
116 return menubar_event (event, edit_menubar);
119 static void
120 edit_adjust_size (Dlg_head * h)
122 WEdit *edit;
123 WButtonBar *edit_bar;
125 edit = (WEdit *) find_widget_type (h, (callback_fn) edit_callback);
126 edit_bar = (WButtonBar *) edit->widget.parent->current->next->widget;
127 widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
128 widget_set_size (&edit_bar->widget, LINES - 1, 0, 1, COLS);
129 widget_set_size (&edit_menubar->widget, 0, 0, 1, COLS);
131 #ifdef RESIZABLE_MENUBAR
132 menubar_arrange (edit_menubar);
133 #endif
136 /* Callback for the edit dialog */
137 static int
138 edit_dialog_callback (Dlg_head * h, int id, int msg)
140 switch (msg) {
141 case DLG_RESIZE:
142 edit_adjust_size (h);
143 return MSG_HANDLED;
145 return default_dlg_callback (h, id, msg);
149 edit (const char *_file, int line)
151 static int made_directory = 0;
152 int framed = 0;
153 char *text = 0;
154 Dlg_head *edit_dlg;
155 WButtonBar *edit_bar;
157 if (option_backup_ext_int != -1) {
158 option_backup_ext = malloc (sizeof (int) + 1);
159 option_backup_ext[sizeof (int)] = '\0';
160 memcpy (option_backup_ext, (char *) &option_backup_ext_int,
161 sizeof (int));
163 if (!made_directory) {
164 mkdir (catstrs (home_dir, EDIT_DIR, 0), 0700);
165 made_directory = 1;
167 if (_file) {
168 if (!(*_file)) {
169 _file = 0;
170 text = "";
172 } else
173 text = "";
175 if (!(wedit = edit_init (NULL, LINES - 2, COLS, _file, text, "", 0))) {
176 return 0;
178 wedit->macro_i = -1;
180 /* Create a new dialog and add it widgets to it */
181 edit_dlg =
182 create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
183 "[Internal File Editor]", NULL, DLG_WANT_TAB);
185 init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
186 (callback_fn) edit_callback, (destroy_fn) edit_clean,
187 (mouse_h) edit_mouse_event, 0);
189 widget_want_cursor (wedit->widget, 1);
191 edit_bar = buttonbar_new (1);
193 if (!framed) {
194 switch (edit_key_emulation) {
195 case EDIT_KEY_EMULATION_NORMAL:
196 edit_init_menu_normal (); /* editmenu.c */
197 break;
198 case EDIT_KEY_EMULATION_EMACS:
199 edit_init_menu_emacs (); /* editmenu.c */
200 break;
202 edit_menubar = menubar_new (0, 0, COLS, EditMenuBar, N_menus);
204 add_widget (edit_dlg, wedit);
206 if (!framed)
207 add_widget (edit_dlg, edit_menubar);
209 add_widget (edit_dlg, edit_bar);
210 edit_move_display (wedit, line - 1);
211 edit_move_to_line (wedit, line - 1);
213 run_dlg (edit_dlg);
215 if (!framed)
216 edit_done_menu (); /* editmenu.c */
218 destroy_dlg (edit_dlg);
220 return 1;
223 static void edit_my_define (Dlg_head * h, int idx, char *text,
224 void (*fn) (WEdit *), WEdit * edit)
226 define_label_data (h, (Widget *) edit, idx, text, (buttonbarfn) fn, edit);
230 static void cmd_F1 (WEdit * edit)
232 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (1));
235 static void cmd_F2 (WEdit * edit)
237 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (2));
240 static void cmd_F3 (WEdit * edit)
242 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (3));
245 static void cmd_F4 (WEdit * edit)
247 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (4));
250 static void cmd_F5 (WEdit * edit)
252 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (5));
255 static void cmd_F6 (WEdit * edit)
257 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (6));
260 static void cmd_F7 (WEdit * edit)
262 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (7));
265 static void cmd_F8 (WEdit * edit)
267 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (8));
270 #if 0
271 static void cmd_F9 (WEdit * edit)
273 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (9));
275 #endif
277 static void cmd_F10 (WEdit * edit)
279 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (10));
282 void edit_labels (WEdit * edit)
284 Dlg_head *h = edit->widget.parent;
286 edit_my_define (h, 1, _ ("Help"), cmd_F1, edit);
287 edit_my_define (h, 2, _ ("Save"), cmd_F2, edit);
288 edit_my_define (h, 3, _ ("Mark"), cmd_F3, edit);
289 edit_my_define (h, 4, _ ("Replac"), cmd_F4, edit);
290 edit_my_define (h, 5, _ ("Copy"), cmd_F5, edit);
291 edit_my_define (h, 6, _ ("Move"), cmd_F6, edit);
292 edit_my_define (h, 7, _ ("Search"), cmd_F7, edit);
293 edit_my_define (h, 8, _ ("Delete"), cmd_F8, edit);
294 if (!edit->have_frame)
295 edit_my_define (h, 9, _ ("PullDn"), edit_menu_cmd, edit);
296 edit_my_define (h, 10, _ ("Quit"), cmd_F10, edit);
298 redraw_labels (h, (Widget *) edit);
302 long get_key_state (void)
304 return (long) get_modifier ();
307 void edit_update_screen (WEdit * e)
309 edit_scroll_screen_over_cursor (e);
311 edit_update_curs_col (e);
312 edit_status (e);
314 /* pop all events for this window for internal handling */
316 if (!is_idle ()) {
317 e->force |= REDRAW_PAGE;
318 return;
320 if (e->force & REDRAW_COMPLETELY)
321 e->force |= REDRAW_PAGE;
322 edit_render_keypress (e);
325 static int edit_callback (Dlg_head * h, WEdit * e, int msg, int par)
327 switch (msg) {
328 case WIDGET_INIT:
329 e->force |= REDRAW_COMPLETELY;
330 edit_labels (e);
331 break;
332 case WIDGET_DRAW:
333 e->force |= REDRAW_COMPLETELY;
334 e->num_widget_lines = LINES - 2;
335 e->num_widget_columns = COLS;
336 case WIDGET_FOCUS:
337 edit_update_screen (e);
338 return 1;
339 case WIDGET_KEY:{
340 int cmd, ch;
341 if (edit_drop_hotkey_menu (e, par)) /* first check alt-f, alt-e, alt-s, etc for drop menus */
342 return 1;
343 if (!edit_translate_key (e, 0, par, get_key_state (), &cmd, &ch))
344 return 0;
345 edit_execute_key_command (e, cmd, ch);
346 edit_update_screen (e);
348 return 1;
349 case WIDGET_COMMAND:
350 edit_execute_key_command (e, par, -1);
351 edit_update_screen (e);
352 return 1;
353 case WIDGET_CURSOR:
354 widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET, e->curs_col + e->start_col);
355 return 1;
357 return default_proc (h, msg, par);