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
25 * \brief Source: editor initialisation and callback handler
34 #include <sys/types.h>
42 #include "../src/global.h"
44 #include "../src/tty/tty.h" /* LINES, COLS */
45 #include "../src/tty/key.h" /* is_idle() */
47 #include "edit-impl.h"
48 #include "edit-widget.h"
50 #include "../src/widget.h" /* buttonbar_redraw() */
51 #include "../src/menu.h" /* menubar_new() */
54 struct WMenu
*edit_menubar
;
56 int column_highlighting
= 0;
58 static cb_ret_t
edit_callback (Widget
*, widget_msg_t msg
, int parm
);
61 edit_event (Gpm_Event
*event
, void *data
)
63 WEdit
*edit
= (WEdit
*) data
;
65 /* Unknown event type */
66 if (!(event
->type
& (GPM_DOWN
| GPM_DRAG
| GPM_UP
)))
69 /* rest of the upper frame, the menu is invisible - call menu */
70 if ((event
->type
& GPM_DOWN
) && (event
->y
== 1))
71 return edit_menubar
->widget
.mouse (event
, edit_menubar
);
73 edit_update_curs_row (edit
);
74 edit_update_curs_col (edit
);
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)
83 if ((event
->buttons
& GPM_B_UP
) && (event
->type
& GPM_DOWN
)) {
84 edit_move_up (edit
, 2, 1);
87 if ((event
->buttons
& GPM_B_DOWN
) && (event
->type
& GPM_DOWN
)) {
88 edit_move_down (edit
, 2, 1);
92 /* A lone up mustn't do anything */
93 if (edit
->mark2
!= -1 && event
->type
& (GPM_UP
| GPM_DRAG
))
96 if (event
->type
& (GPM_DOWN
| GPM_UP
))
97 edit_push_key_press (edit
);
99 if (option_cursor_beyond_eol
) {
100 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
101 edit_eol(edit
, edit
->curs1
));
102 if ( event
->x
> line_len
) {
103 edit
->over_col
= event
->x
- line_len
- option_line_state_width
- 1;
104 edit
->prev_col
= line_len
;
107 edit
->prev_col
= event
->x
- option_line_state_width
- 1;
110 edit
->prev_col
= event
->x
- edit
->start_col
- option_line_state_width
- 1;
113 if (--event
->y
> (edit
->curs_row
+ 1))
114 edit_move_down (edit
, event
->y
- (edit
->curs_row
+ 1), 0);
115 else if (event
->y
< (edit
->curs_row
+ 1))
116 edit_move_up (edit
, (edit
->curs_row
+ 1) - event
->y
, 0);
118 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
120 if (event
->type
& GPM_DOWN
) {
121 edit_mark_cmd (edit
, 1); /* reset */
125 if (!(event
->type
& GPM_DRAG
))
126 edit_mark_cmd (edit
, 0);
129 edit_find_bracket (edit
);
130 edit
->force
|= REDRAW_COMPLETELY
;
131 edit_update_curs_row (edit
);
132 edit_update_curs_col (edit
);
133 edit_update_screen (edit
);
139 edit_adjust_size (Dlg_head
*h
)
142 WButtonBar
*edit_bar
;
144 edit
= (WEdit
*) find_widget_type (h
, edit_callback
);
145 edit_bar
= find_buttonbar (h
);
147 widget_set_size (&edit
->widget
, 0, 0, LINES
- 1, COLS
);
148 widget_set_size ((Widget
*) edit_bar
, LINES
- 1, 0, 1, COLS
);
149 widget_set_size (&edit_menubar
->widget
, 0, 0, 1, COLS
);
151 #ifdef RESIZABLE_MENUBAR
152 menubar_arrange (edit_menubar
);
156 /* Callback for the edit dialog */
158 edit_dialog_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
164 edit_adjust_size (h
);
168 edit
= (WEdit
*) find_widget_type (h
, edit_callback
);
169 if (!edit_ok_to_exit (edit
)) {
175 return default_dlg_callback (h
, msg
, parm
);
180 edit_file (const char *_file
, int line
)
182 static int made_directory
= 0;
184 WButtonBar
*edit_bar
;
186 if (!made_directory
) {
187 char *dir
= concat_dir_and_file (home_dir
, EDIT_DIR
);
188 made_directory
= (mkdir (dir
, 0700) != -1 || errno
== EEXIST
);
192 if (!(wedit
= edit_init (NULL
, LINES
- 2, COLS
, _file
, line
))) {
196 /* Create a new dialog and add it widgets to it */
198 create_dlg (0, 0, LINES
, COLS
, NULL
, edit_dialog_callback
,
199 "[Internal File Editor]", NULL
, DLG_WANT_TAB
);
201 init_widget (&(wedit
->widget
), 0, 0, LINES
- 1, COLS
,
202 edit_callback
, edit_event
);
204 widget_want_cursor (wedit
->widget
, 1);
206 edit_bar
= buttonbar_new (1);
208 edit_menubar
= edit_create_menu ();
210 add_widget (edit_dlg
, edit_bar
);
211 add_widget (edit_dlg
, wedit
);
212 add_widget (edit_dlg
, edit_menubar
);
216 edit_done_menu (edit_menubar
); /* editmenu.c */
218 destroy_dlg (edit_dlg
);
224 edit_get_file_name (const WEdit
*edit
)
226 return edit
->filename
;
229 static void edit_my_define (Dlg_head
* h
, int idx
, const char *text
,
230 void (*fn
) (WEdit
*), WEdit
* edit
)
232 text
= edit
->labels
[idx
- 1]? edit
->labels
[idx
- 1] : text
;
233 /* function-cast ok */
234 buttonbar_set_label_data (h
, idx
, text
, (buttonbarfn
) fn
, edit
);
238 static void cmd_F1 (WEdit
* edit
)
240 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (1));
243 static void cmd_F2 (WEdit
* edit
)
245 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (2));
248 static void cmd_F3 (WEdit
* edit
)
250 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (3));
253 static void cmd_F4 (WEdit
* edit
)
255 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (4));
258 static void cmd_F5 (WEdit
* edit
)
260 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (5));
263 static void cmd_F6 (WEdit
* edit
)
265 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (6));
268 static void cmd_F7 (WEdit
* edit
)
270 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (7));
273 static void cmd_F8 (WEdit
* edit
)
275 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (8));
279 static void cmd_F9 (WEdit
* edit
)
281 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (9));
285 static void cmd_F10 (WEdit
* edit
)
287 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (10));
291 edit_labels (WEdit
*edit
)
293 Dlg_head
*h
= edit
->widget
.parent
;
295 edit_my_define (h
, 1, _("Help"), cmd_F1
, edit
);
296 edit_my_define (h
, 2, _("Save"), cmd_F2
, edit
);
297 edit_my_define (h
, 3, _("Mark"), cmd_F3
, edit
);
298 edit_my_define (h
, 4, _("Replac"), cmd_F4
, edit
);
299 edit_my_define (h
, 5, _("Copy"), cmd_F5
, edit
);
300 edit_my_define (h
, 6, _("Move"), cmd_F6
, edit
);
301 edit_my_define (h
, 7, _("Search"), cmd_F7
, edit
);
302 edit_my_define (h
, 8, _("Delete"), cmd_F8
, edit
);
303 edit_my_define (h
, 9, _("PullDn"), edit_menu_cmd
, edit
);
304 edit_my_define (h
, 10, _("Quit"), cmd_F10
, edit
);
306 buttonbar_redraw (h
);
309 void edit_update_screen (WEdit
* e
)
311 edit_scroll_screen_over_cursor (e
);
313 edit_update_curs_col (e
);
316 /* pop all events for this window for internal handling */
319 e
->force
|= REDRAW_PAGE
;
322 if (e
->force
& REDRAW_COMPLETELY
)
323 e
->force
|= REDRAW_PAGE
;
324 edit_render_keypress (e
);
328 edit_callback (Widget
*w
, widget_msg_t msg
, int parm
)
330 WEdit
*e
= (WEdit
*) w
;
334 e
->force
|= REDRAW_COMPLETELY
;
339 e
->force
|= REDRAW_COMPLETELY
;
340 e
->num_widget_lines
= LINES
- 2;
341 e
->num_widget_columns
= COLS
;
345 edit_update_screen (e
);
352 /* The user may override the access-keys for the menu bar. */
353 if (edit_translate_key (e
, parm
, &cmd
, &ch
)) {
354 edit_execute_key_command (e
, cmd
, ch
);
355 edit_update_screen (e
);
357 } else if (edit_drop_hotkey_menu (e
, parm
)) {
360 return MSG_NOT_HANDLED
;
365 widget_move (&e
->widget
, e
->curs_row
+ EDIT_TEXT_VERTICAL_OFFSET
,
366 e
->curs_col
+ e
->start_col
+ e
->over_col
+
367 EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
);
375 return default_proc (msg
, parm
);