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
28 #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
->prev_col
= event
->x
- edit
->start_col
- 1;
91 if (--event
->y
> (edit
->curs_row
+ 1))
92 edit_move_down (edit
, event
->y
- (edit
->curs_row
+ 1), 0);
93 else if (event
->y
< (edit
->curs_row
+ 1))
94 edit_move_up (edit
, (edit
->curs_row
+ 1) - event
->y
, 0);
96 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
98 if (event
->type
& GPM_DOWN
) {
99 edit_mark_cmd (edit
, 1); /* reset */
103 if (!(event
->type
& GPM_DRAG
))
104 edit_mark_cmd (edit
, 0);
107 edit_find_bracket (edit
);
108 edit
->force
|= REDRAW_COMPLETELY
;
109 edit_update_curs_row (edit
);
110 edit_update_curs_col (edit
);
111 edit_update_screen (edit
);
118 edit_mouse_event (Gpm_Event
*event
, void *x
)
121 if (edit_event ((WEdit
*) x
, event
, &result
))
124 return (*edit_menubar
->widget
.mouse
) (event
, edit_menubar
);
128 edit_adjust_size (Dlg_head
*h
)
131 WButtonBar
*edit_bar
;
133 edit
= (WEdit
*) find_widget_type (h
, edit_callback
);
134 edit_bar
= find_buttonbar (h
);
136 widget_set_size (&edit
->widget
, 0, 0, LINES
- 1, COLS
);
137 widget_set_size ((Widget
*) edit_bar
, LINES
- 1, 0, 1, COLS
);
138 widget_set_size (&edit_menubar
->widget
, 0, 0, 1, COLS
);
140 #ifdef RESIZABLE_MENUBAR
141 menubar_arrange (edit_menubar
);
145 /* Callback for the edit dialog */
147 edit_dialog_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
153 edit_adjust_size (h
);
157 edit
= (WEdit
*) find_widget_type (h
, edit_callback
);
158 if (!edit_ok_to_exit (edit
)) {
164 return default_dlg_callback (h
, msg
, parm
);
169 edit_file (const char *_file
, int line
)
171 static int made_directory
= 0;
173 WButtonBar
*edit_bar
;
175 if (!made_directory
) {
176 char *dir
= concat_dir_and_file (home_dir
, EDIT_DIR
);
177 made_directory
= (mkdir (dir
, 0700) != -1 || errno
== EEXIST
);
181 if (!(wedit
= edit_init (NULL
, LINES
- 2, COLS
, _file
, line
))) {
185 /* Create a new dialog and add it widgets to it */
187 create_dlg (0, 0, LINES
, COLS
, NULL
, edit_dialog_callback
,
188 "[Internal File Editor]", NULL
, DLG_WANT_TAB
);
190 init_widget (&(wedit
->widget
), 0, 0, LINES
- 1, COLS
,
194 widget_want_cursor (wedit
->widget
, 1);
196 edit_bar
= buttonbar_new (1);
198 edit_menubar
= edit_init_menu ();
200 add_widget (edit_dlg
, edit_bar
);
201 add_widget (edit_dlg
, wedit
);
202 add_widget (edit_dlg
, edit_menubar
);
206 edit_done_menu (edit_menubar
); /* editmenu.c */
208 destroy_dlg (edit_dlg
);
213 static void edit_my_define (Dlg_head
* h
, int idx
, const char *text
,
214 void (*fn
) (WEdit
*), WEdit
* edit
)
216 text
= edit
->labels
[idx
- 1]? edit
->labels
[idx
- 1] : text
;
217 /* function-cast ok */
218 buttonbar_set_label_data (h
, idx
, text
, (buttonbarfn
) fn
, edit
);
222 static void cmd_F1 (WEdit
* edit
)
224 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (1));
227 static void cmd_F2 (WEdit
* edit
)
229 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (2));
232 static void cmd_F3 (WEdit
* edit
)
234 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (3));
237 static void cmd_F4 (WEdit
* edit
)
239 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (4));
242 static void cmd_F5 (WEdit
* edit
)
244 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (5));
247 static void cmd_F6 (WEdit
* edit
)
249 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (6));
252 static void cmd_F7 (WEdit
* edit
)
254 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (7));
257 static void cmd_F8 (WEdit
* edit
)
259 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (8));
263 static void cmd_F9 (WEdit
* edit
)
265 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (9));
269 static void cmd_F10 (WEdit
* edit
)
271 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (10));
275 edit_labels (WEdit
*edit
)
277 Dlg_head
*h
= edit
->widget
.parent
;
279 edit_my_define (h
, 1, _("Help"), cmd_F1
, edit
);
280 edit_my_define (h
, 2, _("Save"), cmd_F2
, edit
);
281 edit_my_define (h
, 3, _("Mark"), cmd_F3
, edit
);
282 edit_my_define (h
, 4, _("Replac"), cmd_F4
, edit
);
283 edit_my_define (h
, 5, _("Copy"), cmd_F5
, edit
);
284 edit_my_define (h
, 6, _("Move"), cmd_F6
, edit
);
285 edit_my_define (h
, 7, _("Search"), cmd_F7
, edit
);
286 edit_my_define (h
, 8, _("Delete"), cmd_F8
, edit
);
287 edit_my_define (h
, 9, _("PullDn"), edit_menu_cmd
, edit
);
288 edit_my_define (h
, 10, _("Quit"), cmd_F10
, edit
);
290 buttonbar_redraw (h
);
293 void edit_update_screen (WEdit
* e
)
295 edit_scroll_screen_over_cursor (e
);
297 edit_update_curs_col (e
);
300 /* pop all events for this window for internal handling */
303 e
->force
|= REDRAW_PAGE
;
306 if (e
->force
& REDRAW_COMPLETELY
)
307 e
->force
|= REDRAW_PAGE
;
308 edit_render_keypress (e
);
312 edit_callback (Widget
*w
, widget_msg_t msg
, int parm
)
314 WEdit
*e
= (WEdit
*) w
;
318 e
->force
|= REDRAW_COMPLETELY
;
323 e
->force
|= REDRAW_COMPLETELY
;
324 e
->num_widget_lines
= LINES
- 2;
325 e
->num_widget_columns
= COLS
;
329 edit_update_screen (e
);
336 /* The user may override the access-keys for the menu bar. */
337 if (edit_translate_key (e
, parm
, &cmd
, &ch
)) {
338 edit_execute_key_command (e
, cmd
, ch
);
339 edit_update_screen (e
);
341 } else if (edit_drop_hotkey_menu (e
, parm
)) {
344 return MSG_NOT_HANDLED
;
349 widget_move (&e
->widget
, e
->curs_row
+ EDIT_TEXT_VERTICAL_OFFSET
,
350 e
->curs_col
+ e
->start_col
);
358 return default_proc (msg
, parm
);