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
25 #include "edit-widget.h"
27 #include "../src/tty.h" /* LINES */
28 #include "../src/widget.h" /* redraw_labels() */
29 #include "../src/menu.h" /* menubar_new() */
30 #include "../src/key.h" /* is_idle() */
33 struct WMenu
*edit_menubar
;
35 int column_highlighting
= 0;
37 static cb_ret_t
edit_callback (WEdit
*edit
, widget_msg_t msg
, int parm
);
40 edit_event (WEdit
* edit
, Gpm_Event
* event
, int *result
)
43 edit_update_curs_row (edit
);
44 edit_update_curs_col (edit
);
46 /* Unknown event type */
47 if (!(event
->type
& (GPM_DOWN
| GPM_DRAG
| GPM_UP
)))
51 if ((event
->buttons
& GPM_B_UP
) && (event
->type
& GPM_DOWN
)) {
52 edit_move_up (edit
, 2, 1);
55 if ((event
->buttons
& GPM_B_DOWN
) && (event
->type
& GPM_DOWN
)) {
56 edit_move_down (edit
, 2, 1);
60 /* Outside editor window */
61 if (event
->y
<= 1 || event
->x
<= 0
62 || event
->x
> edit
->num_widget_columns
63 || event
->y
> edit
->num_widget_lines
+ 1)
66 /* A lone up mustn't do anything */
67 if (edit
->mark2
!= -1 && event
->type
& (GPM_UP
| GPM_DRAG
))
70 if (event
->type
& (GPM_DOWN
| GPM_UP
))
71 edit_push_key_press (edit
);
73 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
75 if (--event
->y
> (edit
->curs_row
+ 1))
76 edit_cursor_move (edit
,
77 edit_move_forward (edit
, edit
->curs1
,
78 event
->y
- (edit
->curs_row
+
82 if (event
->y
< (edit
->curs_row
+ 1))
83 edit_cursor_move (edit
,
84 edit_move_backward (edit
, edit
->curs1
,
85 (edit
->curs_row
+ 1) -
86 event
->y
) - edit
->curs1
);
88 edit_cursor_move (edit
,
89 (int) edit_move_forward3 (edit
, edit
->curs1
,
94 edit
->prev_col
= edit_get_col (edit
);
96 if (event
->type
& GPM_DOWN
) {
97 edit_mark_cmd (edit
, 1); /* reset */
101 if (!(event
->type
& GPM_DRAG
))
102 edit_mark_cmd (edit
, 0);
105 edit
->force
|= REDRAW_COMPLETELY
;
106 edit_update_curs_row (edit
);
107 edit_update_curs_col (edit
);
108 edit_update_screen (edit
);
115 edit_mouse_event (Gpm_Event
*event
, void *x
)
118 if (edit_event ((WEdit
*) x
, event
, &result
))
121 return (*edit_menubar
->widget
.mouse
) (event
, edit_menubar
);
125 edit_adjust_size (Dlg_head
*h
)
128 WButtonBar
*edit_bar
;
130 edit
= (WEdit
*) find_widget_type (h
, edit_callback
);
131 edit_bar
= find_buttonbar (h
);
133 widget_set_size (&edit
->widget
, 0, 0, LINES
- 1, COLS
);
134 widget_set_size (&edit_bar
->widget
, LINES
- 1, 0, 1, COLS
);
135 widget_set_size (&edit_menubar
->widget
, 0, 0, 1, COLS
);
137 #ifdef RESIZABLE_MENUBAR
138 menubar_arrange (edit_menubar
);
142 /* Callback for the edit dialog */
144 edit_dialog_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
150 edit_adjust_size (h
);
154 edit
= (WEdit
*) find_widget_type (h
, edit_callback
);
155 if (!edit_ok_to_exit (edit
)) {
161 return default_dlg_callback (h
, msg
, parm
);
166 edit (const char *_file
, int line
)
168 static int made_directory
= 0;
170 WButtonBar
*edit_bar
;
172 if (option_backup_ext_int
!= -1) {
173 option_backup_ext
= g_malloc (sizeof (int) + 1);
174 option_backup_ext
[sizeof (int)] = '\0';
175 memcpy (option_backup_ext
, (char *) &option_backup_ext_int
,
178 if (!made_directory
) {
179 mkdir (catstrs (home_dir
, EDIT_DIR
, 0), 0700);
183 if (!(wedit
= edit_init (NULL
, LINES
- 2, COLS
, _file
, line
))) {
187 /* Create a new dialog and add it widgets to it */
189 create_dlg (0, 0, LINES
, COLS
, NULL
, edit_dialog_callback
,
190 "[Internal File Editor]", NULL
, DLG_WANT_TAB
);
192 init_widget (&(wedit
->widget
), 0, 0, LINES
- 1, COLS
,
193 (callback_fn
) edit_callback
,
194 (mouse_h
) edit_mouse_event
);
196 widget_want_cursor (wedit
->widget
, 1);
198 edit_bar
= buttonbar_new (1);
200 switch (edit_key_emulation
) {
201 case EDIT_KEY_EMULATION_NORMAL
:
202 edit_init_menu_normal (); /* editmenu.c */
204 case EDIT_KEY_EMULATION_EMACS
:
205 edit_init_menu_emacs (); /* editmenu.c */
208 edit_menubar
= menubar_new (0, 0, COLS
, EditMenuBar
, N_menus
);
210 add_widget (edit_dlg
, edit_bar
);
211 add_widget (edit_dlg
, wedit
);
212 add_widget (edit_dlg
, edit_menubar
);
216 edit_done_menu (); /* editmenu.c */
218 destroy_dlg (edit_dlg
);
223 static void edit_my_define (Dlg_head
* h
, int idx
, char *text
,
224 void (*fn
) (WEdit
*), WEdit
* edit
)
226 define_label_data (h
, idx
, text
, (buttonbarfn
) fn
, edit
);
230 static void cmd_F1 (WEdit
* edit
)
232 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (1));
235 static void cmd_F2 (WEdit
* edit
)
237 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (2));
240 static void cmd_F3 (WEdit
* edit
)
242 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (3));
245 static void cmd_F4 (WEdit
* edit
)
247 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (4));
250 static void cmd_F5 (WEdit
* edit
)
252 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (5));
255 static void cmd_F6 (WEdit
* edit
)
257 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (6));
260 static void cmd_F7 (WEdit
* edit
)
262 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (7));
265 static void cmd_F8 (WEdit
* edit
)
267 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (8));
271 static void cmd_F9 (WEdit
* edit
)
273 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (9));
277 static void cmd_F10 (WEdit
* edit
)
279 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (10));
283 edit_labels (WEdit
*edit
)
285 Dlg_head
*h
= edit
->widget
.parent
;
287 edit_my_define (h
, 1, _("Help"), cmd_F1
, edit
);
288 edit_my_define (h
, 2, _("Save"), cmd_F2
, edit
);
289 edit_my_define (h
, 3, _("Mark"), cmd_F3
, edit
);
290 edit_my_define (h
, 4, _("Replac"), cmd_F4
, edit
);
291 edit_my_define (h
, 5, _("Copy"), cmd_F5
, edit
);
292 edit_my_define (h
, 6, _("Move"), cmd_F6
, edit
);
293 edit_my_define (h
, 7, _("Search"), cmd_F7
, edit
);
294 edit_my_define (h
, 8, _("Delete"), cmd_F8
, edit
);
295 edit_my_define (h
, 9, _("PullDn"), edit_menu_cmd
, edit
);
296 edit_my_define (h
, 10, _("Quit"), cmd_F10
, edit
);
301 void edit_update_screen (WEdit
* e
)
303 edit_scroll_screen_over_cursor (e
);
305 edit_update_curs_col (e
);
308 /* pop all events for this window for internal handling */
311 e
->force
|= REDRAW_PAGE
;
314 if (e
->force
& REDRAW_COMPLETELY
)
315 e
->force
|= REDRAW_PAGE
;
316 edit_render_keypress (e
);
320 edit_callback (WEdit
*e
, widget_msg_t msg
, int parm
)
324 e
->force
|= REDRAW_COMPLETELY
;
329 e
->force
|= REDRAW_COMPLETELY
;
330 e
->num_widget_lines
= LINES
- 2;
331 e
->num_widget_columns
= COLS
;
335 edit_update_screen (e
);
342 /* first check alt-f, alt-e, alt-s, etc for drop menus */
343 if (edit_drop_hotkey_menu (e
, parm
))
345 if (!edit_translate_key (e
, parm
, &cmd
, &ch
))
346 return MSG_NOT_HANDLED
;
347 edit_execute_key_command (e
, cmd
, ch
);
348 edit_update_screen (e
);
353 widget_move (&e
->widget
, e
->curs_row
+ EDIT_TEXT_VERTICAL_OFFSET
,
354 e
->curs_col
+ e
->start_col
);
362 return default_proc (msg
, parm
);