Merge commit '315633ecce31cd5af92112ab9903f6e7e8ae8df7'
[free-mc.git] / edit / editwidget.c
blob2b248a06980990edb919e5d134457ffc733f4976
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
21 02110-1301, USA.
24 #include <config.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <sys/types.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <string.h>
33 #include <ctype.h>
34 #include <errno.h>
35 #include <sys/stat.h>
37 #include <stdlib.h>
39 #include "../src/global.h"
41 #include "edit.h"
42 #include "edit-widget.h"
44 #include "../src/tty.h" /* LINES */
45 #include "../src/widget.h" /* buttonbar_redraw() */
46 #include "../src/menu.h" /* menubar_new() */
47 #include "../src/key.h" /* is_idle() */
49 WEdit *wedit;
50 struct WMenu *edit_menubar;
52 int column_highlighting = 0;
54 static cb_ret_t edit_callback (Widget *, widget_msg_t msg, int parm);
56 static int
57 edit_event (WEdit * edit, Gpm_Event * event, int *result)
59 *result = MOU_NORMAL;
60 edit_update_curs_row (edit);
61 edit_update_curs_col (edit);
63 /* Unknown event type */
64 if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
65 return 0;
67 /* Wheel events */
68 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
69 edit_move_up (edit, 2, 1);
70 goto update;
72 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
73 edit_move_down (edit, 2, 1);
74 goto update;
77 /* Outside editor window */
78 if (event->y <= 1 || event->x <= 0
79 || event->x > edit->num_widget_columns
80 || event->y > edit->num_widget_lines + 1)
81 return 0;
83 /* A lone up mustn't do anything */
84 if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
85 return 1;
87 if (event->type & (GPM_DOWN | GPM_UP))
88 edit_push_key_press (edit);
90 edit->prev_col = event->x - edit->start_col - 1;
92 if (--event->y > (edit->curs_row + 1))
93 edit_move_down (edit, event->y - (edit->curs_row + 1), 0);
94 else if (event->y < (edit->curs_row + 1))
95 edit_move_up (edit, (edit->curs_row + 1) - event->y, 0);
96 else
97 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
99 if (event->type & GPM_DOWN) {
100 edit_mark_cmd (edit, 1); /* reset */
101 edit->highlight = 0;
104 if (!(event->type & GPM_DRAG))
105 edit_mark_cmd (edit, 0);
107 update:
108 edit_find_bracket (edit);
109 edit->force |= REDRAW_COMPLETELY;
110 edit_update_curs_row (edit);
111 edit_update_curs_col (edit);
112 edit_update_screen (edit);
114 return 1;
118 static int
119 edit_mouse_event (Gpm_Event *event, void *x)
121 int result;
122 if (edit_event ((WEdit *) x, event, &result))
123 return result;
124 else
125 return (*edit_menubar->widget.mouse) (event, edit_menubar);
128 static void
129 edit_adjust_size (Dlg_head *h)
131 WEdit *edit;
132 WButtonBar *edit_bar;
134 edit = (WEdit *) find_widget_type (h, edit_callback);
135 edit_bar = find_buttonbar (h);
137 widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
138 widget_set_size ((Widget *) edit_bar, LINES - 1, 0, 1, COLS);
139 widget_set_size (&edit_menubar->widget, 0, 0, 1, COLS);
141 #ifdef RESIZABLE_MENUBAR
142 menubar_arrange (edit_menubar);
143 #endif
146 /* Callback for the edit dialog */
147 static cb_ret_t
148 edit_dialog_callback (Dlg_head *h, dlg_msg_t msg, int parm)
150 WEdit *edit;
152 switch (msg) {
153 case DLG_RESIZE:
154 edit_adjust_size (h);
155 return MSG_HANDLED;
157 case DLG_VALIDATE:
158 edit = (WEdit *) find_widget_type (h, edit_callback);
159 if (!edit_ok_to_exit (edit)) {
160 h->running = 1;
162 return MSG_HANDLED;
164 default:
165 return default_dlg_callback (h, msg, parm);
170 edit_file (const char *_file, int line)
172 static int made_directory = 0;
173 Dlg_head *edit_dlg;
174 WButtonBar *edit_bar;
176 if (!made_directory) {
177 char *dir = concat_dir_and_file (home_dir, EDIT_DIR);
178 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
179 g_free (dir);
182 if (!(wedit = edit_init (NULL, LINES - 2, COLS, _file, line))) {
183 return 0;
186 /* Create a new dialog and add it widgets to it */
187 edit_dlg =
188 create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
189 "[Internal File Editor]", NULL, DLG_WANT_TAB);
191 init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
192 edit_callback,
193 edit_mouse_event);
195 widget_want_cursor (wedit->widget, 1);
197 edit_bar = buttonbar_new (1);
199 edit_menubar = edit_init_menu ();
201 add_widget (edit_dlg, edit_bar);
202 add_widget (edit_dlg, wedit);
203 add_widget (edit_dlg, edit_menubar);
205 run_dlg (edit_dlg);
207 edit_done_menu (edit_menubar); /* editmenu.c */
209 destroy_dlg (edit_dlg);
211 return 1;
214 static void edit_my_define (Dlg_head * h, int idx, const char *text,
215 void (*fn) (WEdit *), WEdit * edit)
217 text = edit->labels[idx - 1]? edit->labels[idx - 1] : text;
218 /* function-cast ok */
219 buttonbar_set_label_data (h, idx, text, (buttonbarfn) fn, edit);
223 static void cmd_F1 (WEdit * edit)
225 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (1));
228 static void cmd_F2 (WEdit * edit)
230 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (2));
233 static void cmd_F3 (WEdit * edit)
235 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (3));
238 static void cmd_F4 (WEdit * edit)
240 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (4));
243 static void cmd_F5 (WEdit * edit)
245 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (5));
248 static void cmd_F6 (WEdit * edit)
250 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (6));
253 static void cmd_F7 (WEdit * edit)
255 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (7));
258 static void cmd_F8 (WEdit * edit)
260 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (8));
263 #if 0
264 static void cmd_F9 (WEdit * edit)
266 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (9));
268 #endif
270 static void cmd_F10 (WEdit * edit)
272 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (10));
275 static void
276 edit_labels (WEdit *edit)
278 Dlg_head *h = edit->widget.parent;
280 edit_my_define (h, 1, _("Help"), cmd_F1, edit);
281 edit_my_define (h, 2, _("Save"), cmd_F2, edit);
282 edit_my_define (h, 3, _("Mark"), cmd_F3, edit);
283 edit_my_define (h, 4, _("Replac"), cmd_F4, edit);
284 edit_my_define (h, 5, _("Copy"), cmd_F5, edit);
285 edit_my_define (h, 6, _("Move"), cmd_F6, edit);
286 edit_my_define (h, 7, _("Search"), cmd_F7, edit);
287 edit_my_define (h, 8, _("Delete"), cmd_F8, edit);
288 edit_my_define (h, 9, _("PullDn"), edit_menu_cmd, edit);
289 edit_my_define (h, 10, _("Quit"), cmd_F10, edit);
291 buttonbar_redraw (h);
294 void edit_update_screen (WEdit * e)
296 edit_scroll_screen_over_cursor (e);
298 edit_update_curs_col (e);
299 edit_status (e);
301 /* pop all events for this window for internal handling */
303 if (!is_idle ()) {
304 e->force |= REDRAW_PAGE;
305 return;
307 if (e->force & REDRAW_COMPLETELY)
308 e->force |= REDRAW_PAGE;
309 edit_render_keypress (e);
312 static cb_ret_t
313 edit_callback (Widget *w, widget_msg_t msg, int parm)
315 WEdit *e = (WEdit *) w;
317 switch (msg) {
318 case WIDGET_INIT:
319 e->force |= REDRAW_COMPLETELY;
320 edit_labels (e);
321 return MSG_HANDLED;
323 case WIDGET_DRAW:
324 e->force |= REDRAW_COMPLETELY;
325 e->num_widget_lines = LINES - 2;
326 e->num_widget_columns = COLS;
327 /* fallthrough */
329 case WIDGET_FOCUS:
330 edit_update_screen (e);
331 return MSG_HANDLED;
333 case WIDGET_KEY:
335 int cmd, ch;
337 /* The user may override the access-keys for the menu bar. */
338 if (edit_translate_key (e, parm, &cmd, &ch)) {
339 edit_execute_key_command (e, cmd, ch);
340 edit_update_screen (e);
341 return MSG_HANDLED;
342 } else if (edit_drop_hotkey_menu (e, parm)) {
343 return MSG_HANDLED;
344 } else {
345 return MSG_NOT_HANDLED;
349 case WIDGET_CURSOR:
350 widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
351 e->curs_col + e->start_col);
352 return MSG_HANDLED;
354 case WIDGET_DESTROY:
355 edit_clean (e);
356 return MSG_HANDLED;
358 default:
359 return default_proc (msg, parm);