Just a fuzzy trim...
[midnight-commander.git] / edit / editwidget.c
blobb7b50c6f196865a16a7d8469480351a5c2d72d7b
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
20 02111-1307, USA.
23 #include <config.h>
24 #include "edit.h"
26 WEdit *wedit;
27 WButtonBar *edit_bar;
28 Dlg_head *edit_dlg;
29 WMenu *edit_menubar;
31 int column_highlighting = 0;
33 static int edit_callback (Dlg_head * h, WEdit * edit, int msg, int par);
35 static int edit_mode_callback (struct Dlg_head *h, int id, int msg)
37 return 0;
40 int edit_event (WEdit * edit, Gpm_Event * event, int *result)
42 *result = MOU_NORMAL;
43 edit_update_curs_row (edit);
44 edit_update_curs_col (edit);
45 if (event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)) {
46 if (event->y > 1 && event->x > 0
47 && event->x <= edit->num_widget_columns
48 && event->y <= edit->num_widget_lines + 1) {
49 if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
50 return 1; /* a lone up mustn't do anything */
51 if (event->type & (GPM_DOWN | GPM_UP))
52 edit_push_key_press (edit);
53 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
54 if (--event->y > (edit->curs_row + 1))
55 edit_cursor_move (edit,
56 edit_move_forward (edit, edit->curs1, event->y - (edit->curs_row + 1), 0)
57 - edit->curs1);
58 if (event->y < (edit->curs_row + 1))
59 edit_cursor_move (edit,
60 +edit_move_backward (edit, edit->curs1, (edit->curs_row + 1) - event->y)
61 - edit->curs1);
62 edit_cursor_move (edit, (int) edit_move_forward3 (edit, edit->curs1,
63 event->x - edit->start_col - 1, 0) - edit->curs1);
64 edit->prev_col = edit_get_col (edit);
65 if (event->type & GPM_DOWN) {
66 edit_mark_cmd (edit, 1); /* reset */
67 edit->highlight = 0;
69 if (!(event->type & GPM_DRAG))
70 edit_mark_cmd (edit, 0);
71 edit->force |= REDRAW_COMPLETELY;
72 edit_update_curs_row (edit);
73 edit_update_curs_col (edit);
74 edit_update_screen (edit);
75 return 1;
78 return 0;
83 int menubar_event (Gpm_Event * event, WMenu * menubar); /* menu.c */
85 int edit_mouse_event (Gpm_Event * event, void *x)
87 int result;
88 if (edit_event ((WEdit *) x, event, &result))
89 return result;
90 else
91 return menubar_event (event, edit_menubar);
94 extern Menu EditMenuBar[5];
96 int edit (const char *_file, int line)
98 static int made_directory = 0;
99 int framed = 0;
100 int midnight_colors[4];
101 char *text = 0;
103 if (option_backup_ext_int != -1) {
104 option_backup_ext = malloc (sizeof (int) + 1);
105 option_backup_ext[sizeof (int)] = '\0';
106 memcpy (option_backup_ext, (char *) &option_backup_ext_int, sizeof (int));
108 if (!made_directory) {
109 mkdir (catstrs (home_dir, EDIT_DIR, 0), 0700);
110 made_directory = 1;
112 if (_file) {
113 if (!(*_file)) {
114 _file = 0;
115 text = "";
117 } else
118 text = "";
120 if (!(wedit = edit_init (NULL, LINES - 2, COLS, _file, text, "", 0))) {
121 return 0;
123 wedit->macro_i = -1;
125 /* Create a new dialog and add it widgets to it */
126 edit_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors,
127 edit_mode_callback, "[Internal File Editor]",
128 "edit",
129 DLG_NONE);
131 edit_dlg->raw = 1; /*so that tab = '\t' key works */
133 init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
134 (callback_fn) edit_callback,
135 (destroy_fn) edit_clean,
136 (mouse_h) edit_mouse_event, 0);
138 widget_want_cursor (wedit->widget, 1);
140 edit_bar = buttonbar_new (1);
142 if (!framed) {
143 switch (edit_key_emulation) {
144 case EDIT_KEY_EMULATION_NORMAL:
145 edit_init_menu_normal (); /* editmenu.c */
146 break;
147 case EDIT_KEY_EMULATION_EMACS:
148 edit_init_menu_emacs (); /* editmenu.c */
149 break;
151 edit_menubar = menubar_new (0, 0, COLS, EditMenuBar, N_menus);
153 add_widget (edit_dlg, wedit);
155 if (!framed)
156 add_widget (edit_dlg, edit_menubar);
158 add_widget (edit_dlg, edit_bar);
159 edit_move_display (wedit, line - 1);
160 edit_move_to_line (wedit, line - 1);
162 run_dlg (edit_dlg);
164 if (!framed)
165 edit_done_menu (); /* editmenu.c */
167 destroy_dlg (edit_dlg);
169 return 1;
172 static void edit_my_define (Dlg_head * h, int idx, char *text,
173 void (*fn) (WEdit *), WEdit * edit)
175 define_label_data (h, (Widget *) edit, idx, text, (buttonbarfn) fn, edit);
179 static void cmd_F1 (WEdit * edit)
181 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (1));
184 static void cmd_F2 (WEdit * edit)
186 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (2));
189 static void cmd_F3 (WEdit * edit)
191 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (3));
194 static void cmd_F4 (WEdit * edit)
196 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (4));
199 static void cmd_F5 (WEdit * edit)
201 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (5));
204 static void cmd_F6 (WEdit * edit)
206 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (6));
209 static void cmd_F7 (WEdit * edit)
211 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (7));
214 static void cmd_F8 (WEdit * edit)
216 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (8));
219 #if 0
220 static void cmd_F9 (WEdit * edit)
222 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (9));
224 #endif
226 static void cmd_F10 (WEdit * edit)
228 send_message (edit->widget.parent, (Widget *) edit, WIDGET_KEY, KEY_F (10));
231 void edit_labels (WEdit * edit)
233 Dlg_head *h = edit->widget.parent;
235 edit_my_define (h, 1, _ ("Help"), cmd_F1, edit);
236 edit_my_define (h, 2, _ ("Save"), cmd_F2, edit);
237 edit_my_define (h, 3, _ ("Mark"), cmd_F3, edit);
238 edit_my_define (h, 4, _ ("Replac"), cmd_F4, edit);
239 edit_my_define (h, 5, _ ("Copy"), cmd_F5, edit);
240 edit_my_define (h, 6, _ ("Move"), cmd_F6, edit);
241 edit_my_define (h, 7, _ ("Search"), cmd_F7, edit);
242 edit_my_define (h, 8, _ ("Delete"), cmd_F8, edit);
243 if (!edit->have_frame)
244 edit_my_define (h, 9, _ ("PullDn"), edit_menu_cmd, edit);
245 edit_my_define (h, 10, _ ("Quit"), cmd_F10, edit);
247 redraw_labels (h, (Widget *) edit);
251 long get_key_state (void)
253 return (long) get_modifier ();
256 void edit_adjust_size (Dlg_head * h)
258 WEdit *edit;
259 WButtonBar *edit_bar;
261 edit = (WEdit *) find_widget_type (h, (callback_fn) edit_callback);
262 edit_bar = (WButtonBar *) edit->widget.parent->current->next->widget;
263 widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
264 widget_set_size (&edit_bar->widget, LINES - 1, 0, 1, COLS);
265 widget_set_size (&edit_menubar->widget, 0, 0, 1, COLS);
267 #ifdef RESIZABLE_MENUBAR
268 menubar_arrange (edit_menubar);
269 #endif
272 void edit_update_screen (WEdit * e)
274 edit_scroll_screen_over_cursor (e);
276 edit_update_curs_col (e);
277 edit_status (e);
279 /* pop all events for this window for internal handling */
281 if (!is_idle ()) {
282 e->force |= REDRAW_PAGE;
283 return;
285 if (e->force & REDRAW_COMPLETELY)
286 e->force |= REDRAW_PAGE;
287 edit_render_keypress (e);
290 static int edit_callback (Dlg_head * h, WEdit * e, int msg, int par)
292 switch (msg) {
293 case WIDGET_INIT:
294 e->force |= REDRAW_COMPLETELY;
295 edit_labels (e);
296 break;
297 case WIDGET_DRAW:
298 e->force |= REDRAW_COMPLETELY;
299 e->num_widget_lines = LINES - 2;
300 e->num_widget_columns = COLS;
301 case WIDGET_FOCUS:
302 edit_update_screen (e);
303 return 1;
304 case WIDGET_KEY:{
305 int cmd, ch;
306 if (edit_drop_hotkey_menu (e, par)) /* first check alt-f, alt-e, alt-s, etc for drop menus */
307 return 1;
308 if (!edit_translate_key (e, 0, par, get_key_state (), &cmd, &ch))
309 return 0;
310 edit_execute_key_command (e, cmd, ch);
311 edit_update_screen (e);
313 return 1;
314 case WIDGET_COMMAND:
315 edit_execute_key_command (e, par, -1);
316 edit_update_screen (e);
317 return 1;
318 case WIDGET_CURSOR:
319 widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET, e->curs_col + e->start_col);
320 return 1;
322 return default_proc (h, msg, par);