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., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include <sys/types.h>
38 #include "../src/global.h"
41 #include "edit-widget.h"
43 #include "../src/tty.h" /* LINES */
44 #include "../src/widget.h" /* buttonbar_redraw() */
45 #include "../src/menu.h" /* menubar_new() */
46 #include "../src/key.h" /* is_idle() */
49 struct WMenu
*edit_menubar
;
51 int column_highlighting
= 0;
53 static cb_ret_t
edit_callback (Widget
*, widget_msg_t msg
, int parm
);
56 edit_event (WEdit
* edit
, Gpm_Event
* event
, int *result
)
59 edit_update_curs_row (edit
);
60 edit_update_curs_col (edit
);
62 /* Unknown event type */
63 if (!(event
->type
& (GPM_DOWN
| GPM_DRAG
| GPM_UP
)))
67 if ((event
->buttons
& GPM_B_UP
) && (event
->type
& GPM_DOWN
)) {
68 edit_move_up (edit
, 2, 1);
71 if ((event
->buttons
& GPM_B_DOWN
) && (event
->type
& GPM_DOWN
)) {
72 edit_move_down (edit
, 2, 1);
76 /* Outside editor window */
77 if (event
->y
<= 1 || event
->x
<= 0
78 || event
->x
> edit
->num_widget_columns
79 || event
->y
> edit
->num_widget_lines
+ 1)
82 /* A lone up mustn't do anything */
83 if (edit
->mark2
!= -1 && event
->type
& (GPM_UP
| GPM_DRAG
))
86 if (event
->type
& (GPM_DOWN
| GPM_UP
))
87 edit_push_key_press (edit
);
89 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
91 if (--event
->y
> (edit
->curs_row
+ 1))
92 edit_cursor_move (edit
,
93 edit_move_forward (edit
, edit
->curs1
,
94 event
->y
- (edit
->curs_row
+
98 if (event
->y
< (edit
->curs_row
+ 1))
99 edit_cursor_move (edit
,
100 edit_move_backward (edit
, edit
->curs1
,
101 (edit
->curs_row
+ 1) -
102 event
->y
) - edit
->curs1
);
104 edit_cursor_move (edit
,
105 (int) edit_move_forward3 (edit
, edit
->curs1
,
110 edit
->prev_col
= edit_get_col (edit
);
112 if (event
->type
& GPM_DOWN
) {
113 edit_mark_cmd (edit
, 1); /* reset */
117 if (!(event
->type
& GPM_DRAG
))
118 edit_mark_cmd (edit
, 0);
121 edit
->force
|= REDRAW_COMPLETELY
;
122 edit_update_curs_row (edit
);
123 edit_update_curs_col (edit
);
124 edit_update_screen (edit
);
131 edit_mouse_event (Gpm_Event
*event
, void *x
)
134 if (edit_event ((WEdit
*) x
, event
, &result
))
137 return (*edit_menubar
->widget
.mouse
) (event
, edit_menubar
);
141 edit_adjust_size (Dlg_head
*h
)
144 WButtonBar
*edit_bar
;
146 edit
= (WEdit
*) find_widget_type (h
, edit_callback
);
147 edit_bar
= find_buttonbar (h
);
149 widget_set_size (&edit
->widget
, 0, 0, LINES
- 1, COLS
);
150 widget_set_size ((Widget
*) edit_bar
, LINES
- 1, 0, 1, COLS
);
151 widget_set_size (&edit_menubar
->widget
, 0, 0, 1, COLS
);
153 #ifdef RESIZABLE_MENUBAR
154 menubar_arrange (edit_menubar
);
158 /* Callback for the edit dialog */
160 edit_dialog_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
166 edit_adjust_size (h
);
170 edit
= (WEdit
*) find_widget_type (h
, edit_callback
);
171 if (!edit_ok_to_exit (edit
)) {
177 return default_dlg_callback (h
, msg
, parm
);
182 edit_file (const char *_file
, int line
)
184 static int made_directory
= 0;
186 WButtonBar
*edit_bar
;
188 if (!made_directory
) {
189 char *dir
= concat_dir_and_file (home_dir
, EDIT_DIR
);
190 made_directory
= (mkdir (dir
, 0700) != -1 || errno
== EEXIST
);
194 if (!(wedit
= edit_init (NULL
, LINES
- 2, COLS
, _file
, line
))) {
198 /* Create a new dialog and add it widgets to it */
200 create_dlg (0, 0, LINES
, COLS
, NULL
, edit_dialog_callback
,
201 "[Internal File Editor]", NULL
, DLG_WANT_TAB
);
203 init_widget (&(wedit
->widget
), 0, 0, LINES
- 1, COLS
,
207 widget_want_cursor (wedit
->widget
, 1);
209 edit_bar
= buttonbar_new (1);
211 edit_menubar
= edit_init_menu ();
213 add_widget (edit_dlg
, edit_bar
);
214 add_widget (edit_dlg
, wedit
);
215 add_widget (edit_dlg
, edit_menubar
);
219 edit_done_menu (edit_menubar
); /* editmenu.c */
221 destroy_dlg (edit_dlg
);
226 static void edit_my_define (Dlg_head
* h
, int idx
, const char *text
,
227 void (*fn
) (WEdit
*), WEdit
* edit
)
229 text
= edit
->labels
[idx
- 1]? edit
->labels
[idx
- 1] : text
;
230 /* function-cast ok */
231 buttonbar_set_label_data (h
, idx
, text
, (buttonbarfn
) fn
, edit
);
235 static void cmd_F1 (WEdit
* edit
)
237 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (1));
240 static void cmd_F2 (WEdit
* edit
)
242 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (2));
245 static void cmd_F3 (WEdit
* edit
)
247 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (3));
250 static void cmd_F4 (WEdit
* edit
)
252 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (4));
255 static void cmd_F5 (WEdit
* edit
)
257 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (5));
260 static void cmd_F6 (WEdit
* edit
)
262 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (6));
265 static void cmd_F7 (WEdit
* edit
)
267 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (7));
270 static void cmd_F8 (WEdit
* edit
)
272 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (8));
276 static void cmd_F9 (WEdit
* edit
)
278 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (9));
282 static void cmd_F10 (WEdit
* edit
)
284 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (10));
288 edit_labels (WEdit
*edit
)
290 Dlg_head
*h
= edit
->widget
.parent
;
292 edit_my_define (h
, 1, _("Help"), cmd_F1
, edit
);
293 edit_my_define (h
, 2, _("Save"), cmd_F2
, edit
);
294 edit_my_define (h
, 3, _("Mark"), cmd_F3
, edit
);
295 edit_my_define (h
, 4, _("Replac"), cmd_F4
, edit
);
296 edit_my_define (h
, 5, _("Copy"), cmd_F5
, edit
);
297 edit_my_define (h
, 6, _("Move"), cmd_F6
, edit
);
298 edit_my_define (h
, 7, _("Search"), cmd_F7
, edit
);
299 edit_my_define (h
, 8, _("Delete"), cmd_F8
, edit
);
300 edit_my_define (h
, 9, _("PullDn"), edit_menu_cmd
, edit
);
301 edit_my_define (h
, 10, _("Quit"), cmd_F10
, edit
);
303 buttonbar_redraw (h
);
306 void edit_update_screen (WEdit
* e
)
308 edit_scroll_screen_over_cursor (e
);
310 edit_update_curs_col (e
);
313 /* pop all events for this window for internal handling */
316 e
->force
|= REDRAW_PAGE
;
319 if (e
->force
& REDRAW_COMPLETELY
)
320 e
->force
|= REDRAW_PAGE
;
321 edit_render_keypress (e
);
325 edit_callback (Widget
*w
, widget_msg_t msg
, int parm
)
327 WEdit
*e
= (WEdit
*) w
;
331 e
->force
|= REDRAW_COMPLETELY
;
336 e
->force
|= REDRAW_COMPLETELY
;
337 e
->num_widget_lines
= LINES
- 2;
338 e
->num_widget_columns
= COLS
;
342 edit_update_screen (e
);
349 /* The user may override the access-keys for the menu bar. */
350 if (edit_translate_key (e
, parm
, &cmd
, &ch
)) {
351 edit_execute_key_command (e
, cmd
, ch
);
352 edit_update_screen (e
);
354 } else if (edit_drop_hotkey_menu (e
, parm
)) {
357 return MSG_NOT_HANDLED
;
362 widget_move (&e
->widget
, e
->curs_row
+ EDIT_TEXT_VERTICAL_OFFSET
,
363 e
->curs_col
+ e
->start_col
);
371 return default_proc (msg
, parm
);