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 (WEdit
* edit
, Gpm_Event
* event
, int *result
)
64 edit_update_curs_row (edit
);
65 edit_update_curs_col (edit
);
67 /* Unknown event type */
68 if (!(event
->type
& (GPM_DOWN
| GPM_DRAG
| GPM_UP
)))
72 if ((event
->buttons
& GPM_B_UP
) && (event
->type
& GPM_DOWN
)) {
73 edit_move_up (edit
, 2, 1);
76 if ((event
->buttons
& GPM_B_DOWN
) && (event
->type
& GPM_DOWN
)) {
77 edit_move_down (edit
, 2, 1);
81 /* Outside editor window */
82 if (event
->y
<= 1 || event
->x
<= 0
83 || event
->x
> edit
->num_widget_columns
84 || event
->y
> edit
->num_widget_lines
+ 1)
87 /* A lone up mustn't do anything */
88 if (edit
->mark2
!= -1 && event
->type
& (GPM_UP
| GPM_DRAG
))
91 if (event
->type
& (GPM_DOWN
| GPM_UP
))
92 edit_push_key_press (edit
);
94 if (option_cursor_beyond_eol
) {
95 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
96 edit_eol(edit
, edit
->curs1
));
97 if ( event
->x
> line_len
) {
98 edit
->over_col
= event
->x
- line_len
- option_line_state_width
- 1;
99 edit
->prev_col
= line_len
;
102 edit
->prev_col
= event
->x
- option_line_state_width
- 1;
105 edit
->prev_col
= event
->x
- edit
->start_col
- option_line_state_width
- 1;
108 if (--event
->y
> (edit
->curs_row
+ 1))
109 edit_move_down (edit
, event
->y
- (edit
->curs_row
+ 1), 0);
110 else if (event
->y
< (edit
->curs_row
+ 1))
111 edit_move_up (edit
, (edit
->curs_row
+ 1) - event
->y
, 0);
113 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
115 if (event
->type
& GPM_DOWN
) {
116 edit_mark_cmd (edit
, 1); /* reset */
120 if (!(event
->type
& GPM_DRAG
))
121 edit_mark_cmd (edit
, 0);
124 edit_find_bracket (edit
);
125 edit
->force
|= REDRAW_COMPLETELY
;
126 edit_update_curs_row (edit
);
127 edit_update_curs_col (edit
);
128 edit_update_screen (edit
);
135 edit_mouse_event (Gpm_Event
*event
, void *x
)
138 if (edit_event ((WEdit
*) x
, event
, &result
))
141 return (*edit_menubar
->widget
.mouse
) (event
, edit_menubar
);
145 edit_adjust_size (Dlg_head
*h
)
148 WButtonBar
*edit_bar
;
150 edit
= (WEdit
*) find_widget_type (h
, edit_callback
);
151 edit_bar
= find_buttonbar (h
);
153 widget_set_size (&edit
->widget
, 0, 0, LINES
- 1, COLS
);
154 widget_set_size ((Widget
*) edit_bar
, LINES
- 1, 0, 1, COLS
);
155 widget_set_size (&edit_menubar
->widget
, 0, 0, 1, COLS
);
157 #ifdef RESIZABLE_MENUBAR
158 menubar_arrange (edit_menubar
);
162 /* Callback for the edit dialog */
164 edit_dialog_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
170 edit_adjust_size (h
);
174 edit
= (WEdit
*) find_widget_type (h
, edit_callback
);
175 if (!edit_ok_to_exit (edit
)) {
181 return default_dlg_callback (h
, msg
, parm
);
186 edit_file (const char *_file
, int line
)
188 static int made_directory
= 0;
190 WButtonBar
*edit_bar
;
192 if (!made_directory
) {
193 char *dir
= concat_dir_and_file (home_dir
, EDIT_DIR
);
194 made_directory
= (mkdir (dir
, 0700) != -1 || errno
== EEXIST
);
198 if (!(wedit
= edit_init (NULL
, LINES
- 2, COLS
, _file
, line
))) {
202 /* Create a new dialog and add it widgets to it */
204 create_dlg (0, 0, LINES
, COLS
, NULL
, edit_dialog_callback
,
205 "[Internal File Editor]", NULL
, DLG_WANT_TAB
);
207 init_widget (&(wedit
->widget
), 0, 0, LINES
- 1, COLS
,
211 widget_want_cursor (wedit
->widget
, 1);
213 edit_bar
= buttonbar_new (1);
215 edit_menubar
= edit_create_menu ();
217 add_widget (edit_dlg
, edit_bar
);
218 add_widget (edit_dlg
, wedit
);
219 add_widget (edit_dlg
, edit_menubar
);
223 edit_done_menu (edit_menubar
); /* editmenu.c */
225 destroy_dlg (edit_dlg
);
231 edit_get_file_name (const WEdit
*edit
)
233 return edit
->filename
;
236 static void edit_my_define (Dlg_head
* h
, int idx
, const char *text
,
237 void (*fn
) (WEdit
*), WEdit
* edit
)
239 text
= edit
->labels
[idx
- 1]? edit
->labels
[idx
- 1] : text
;
240 /* function-cast ok */
241 buttonbar_set_label_data (h
, idx
, text
, (buttonbarfn
) fn
, edit
);
245 static void cmd_F1 (WEdit
* edit
)
247 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (1));
250 static void cmd_F2 (WEdit
* edit
)
252 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (2));
255 static void cmd_F3 (WEdit
* edit
)
257 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (3));
260 static void cmd_F4 (WEdit
* edit
)
262 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (4));
265 static void cmd_F5 (WEdit
* edit
)
267 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (5));
270 static void cmd_F6 (WEdit
* edit
)
272 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (6));
275 static void cmd_F7 (WEdit
* edit
)
277 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (7));
280 static void cmd_F8 (WEdit
* edit
)
282 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (8));
286 static void cmd_F9 (WEdit
* edit
)
288 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (9));
292 static void cmd_F10 (WEdit
* edit
)
294 send_message ((Widget
*) edit
, WIDGET_KEY
, KEY_F (10));
298 edit_labels (WEdit
*edit
)
300 Dlg_head
*h
= edit
->widget
.parent
;
302 edit_my_define (h
, 1, _("Help"), cmd_F1
, edit
);
303 edit_my_define (h
, 2, _("Save"), cmd_F2
, edit
);
304 edit_my_define (h
, 3, _("Mark"), cmd_F3
, edit
);
305 edit_my_define (h
, 4, _("Replac"), cmd_F4
, edit
);
306 edit_my_define (h
, 5, _("Copy"), cmd_F5
, edit
);
307 edit_my_define (h
, 6, _("Move"), cmd_F6
, edit
);
308 edit_my_define (h
, 7, _("Search"), cmd_F7
, edit
);
309 edit_my_define (h
, 8, _("Delete"), cmd_F8
, edit
);
310 edit_my_define (h
, 9, _("PullDn"), edit_menu_cmd
, edit
);
311 edit_my_define (h
, 10, _("Quit"), cmd_F10
, edit
);
313 buttonbar_redraw (h
);
316 void edit_update_screen (WEdit
* e
)
318 edit_scroll_screen_over_cursor (e
);
320 edit_update_curs_col (e
);
323 /* pop all events for this window for internal handling */
326 e
->force
|= REDRAW_PAGE
;
329 if (e
->force
& REDRAW_COMPLETELY
)
330 e
->force
|= REDRAW_PAGE
;
331 edit_render_keypress (e
);
335 edit_callback (Widget
*w
, widget_msg_t msg
, int parm
)
337 WEdit
*e
= (WEdit
*) w
;
341 e
->force
|= REDRAW_COMPLETELY
;
346 e
->force
|= REDRAW_COMPLETELY
;
347 e
->num_widget_lines
= LINES
- 2;
348 e
->num_widget_columns
= COLS
;
352 edit_update_screen (e
);
359 /* The user may override the access-keys for the menu bar. */
360 if (edit_translate_key (e
, parm
, &cmd
, &ch
)) {
361 edit_execute_key_command (e
, cmd
, ch
);
362 edit_update_screen (e
);
364 } else if (edit_drop_hotkey_menu (e
, parm
)) {
367 return MSG_NOT_HANDLED
;
372 widget_move (&e
->widget
, e
->curs_row
+ EDIT_TEXT_VERTICAL_OFFSET
,
373 e
->curs_col
+ e
->start_col
+ e
->over_col
+
374 EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
);
382 return default_proc (msg
, parm
);