* es.po: Updated Spanish translation.
[free-mc.git] / edit / editwidget.c
blob031be7b1c10914bdfc7c989a0485bda2422d9e74
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., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.
23 #include <config.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <sys/types.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #include <string.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #include <sys/stat.h>
36 #include <stdlib.h>
38 #include "../src/global.h"
40 #include "edit.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() */
48 WEdit *wedit;
49 struct WMenu *edit_menubar;
51 int column_highlighting = 0;
53 static cb_ret_t edit_callback (Widget *, widget_msg_t msg, int parm);
55 static int
56 edit_event (WEdit * edit, Gpm_Event * event, int *result)
58 *result = MOU_NORMAL;
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)))
64 return 0;
66 /* Wheel events */
67 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
68 edit_move_up (edit, 2, 1);
69 goto update;
71 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
72 edit_move_down (edit, 2, 1);
73 goto update;
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)
80 return 0;
82 /* A lone up mustn't do anything */
83 if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
84 return 1;
86 if (event->type & (GPM_DOWN | GPM_UP))
87 edit_push_key_press (edit);
89 edit_cursor_move (edit, edit_bol (edit, edit->curs1) - edit->curs1);
91 if (--event->y > (edit->curs_row + 1))
92 edit_cursor_move (edit,
93 edit_move_forward (edit, edit->curs1,
94 event->y - (edit->curs_row +
95 1), 0)
96 - edit->curs1);
98 if (event->y < (edit->curs_row + 1))
99 edit_cursor_move (edit,
100 edit_move_backward (edit, edit->curs1,
101 (edit->curs_row + 1) -
102 event->y) - edit->curs1);
104 edit_cursor_move (edit,
105 (int) edit_move_forward3 (edit, edit->curs1,
106 event->x -
107 edit->start_col - 1,
108 0) - edit->curs1);
110 edit->prev_col = edit_get_col (edit);
112 if (event->type & GPM_DOWN) {
113 edit_mark_cmd (edit, 1); /* reset */
114 edit->highlight = 0;
117 if (!(event->type & GPM_DRAG))
118 edit_mark_cmd (edit, 0);
120 update:
121 edit->force |= REDRAW_COMPLETELY;
122 edit_update_curs_row (edit);
123 edit_update_curs_col (edit);
124 edit_update_screen (edit);
126 return 1;
130 static int
131 edit_mouse_event (Gpm_Event *event, void *x)
133 int result;
134 if (edit_event ((WEdit *) x, event, &result))
135 return result;
136 else
137 return (*edit_menubar->widget.mouse) (event, edit_menubar);
140 static void
141 edit_adjust_size (Dlg_head *h)
143 WEdit *edit;
144 WButtonBar *edit_bar;
146 edit = (WEdit *) find_widget_type (h, edit_callback);
147 edit_bar = find_buttonbar (h);
149 widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
150 widget_set_size ((Widget *) edit_bar, LINES - 1, 0, 1, COLS);
151 widget_set_size (&edit_menubar->widget, 0, 0, 1, COLS);
153 #ifdef RESIZABLE_MENUBAR
154 menubar_arrange (edit_menubar);
155 #endif
158 /* Callback for the edit dialog */
159 static cb_ret_t
160 edit_dialog_callback (Dlg_head *h, dlg_msg_t msg, int parm)
162 WEdit *edit;
164 switch (msg) {
165 case DLG_RESIZE:
166 edit_adjust_size (h);
167 return MSG_HANDLED;
169 case DLG_VALIDATE:
170 edit = (WEdit *) find_widget_type (h, edit_callback);
171 if (!edit_ok_to_exit (edit)) {
172 h->running = 1;
174 return MSG_HANDLED;
176 default:
177 return default_dlg_callback (h, msg, parm);
182 edit_file (const char *_file, int line)
184 static int made_directory = 0;
185 Dlg_head *edit_dlg;
186 WButtonBar *edit_bar;
188 if (!made_directory) {
189 char *dir = concat_dir_and_file (home_dir, EDIT_DIR);
190 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
191 g_free (dir);
194 if (!(wedit = edit_init (NULL, LINES - 2, COLS, _file, line))) {
195 return 0;
198 /* Create a new dialog and add it widgets to it */
199 edit_dlg =
200 create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
201 "[Internal File Editor]", NULL, DLG_WANT_TAB);
203 init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
204 edit_callback,
205 edit_mouse_event);
207 widget_want_cursor (wedit->widget, 1);
209 edit_bar = buttonbar_new (1);
211 edit_menubar = edit_init_menu ();
213 add_widget (edit_dlg, edit_bar);
214 add_widget (edit_dlg, wedit);
215 add_widget (edit_dlg, edit_menubar);
217 run_dlg (edit_dlg);
219 edit_done_menu (edit_menubar); /* editmenu.c */
221 destroy_dlg (edit_dlg);
223 return 1;
226 static void edit_my_define (Dlg_head * h, int idx, const char *text,
227 void (*fn) (WEdit *), WEdit * edit)
229 text = edit->labels[idx - 1]? edit->labels[idx - 1] : text;
230 /* function-cast ok */
231 buttonbar_set_label_data (h, idx, text, (buttonbarfn) fn, edit);
235 static void cmd_F1 (WEdit * edit)
237 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (1));
240 static void cmd_F2 (WEdit * edit)
242 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (2));
245 static void cmd_F3 (WEdit * edit)
247 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (3));
250 static void cmd_F4 (WEdit * edit)
252 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (4));
255 static void cmd_F5 (WEdit * edit)
257 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (5));
260 static void cmd_F6 (WEdit * edit)
262 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (6));
265 static void cmd_F7 (WEdit * edit)
267 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (7));
270 static void cmd_F8 (WEdit * edit)
272 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (8));
275 #if 0
276 static void cmd_F9 (WEdit * edit)
278 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (9));
280 #endif
282 static void cmd_F10 (WEdit * edit)
284 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (10));
287 static void
288 edit_labels (WEdit *edit)
290 Dlg_head *h = edit->widget.parent;
292 edit_my_define (h, 1, _("Help"), cmd_F1, edit);
293 edit_my_define (h, 2, _("Save"), cmd_F2, edit);
294 edit_my_define (h, 3, _("Mark"), cmd_F3, edit);
295 edit_my_define (h, 4, _("Replac"), cmd_F4, edit);
296 edit_my_define (h, 5, _("Copy"), cmd_F5, edit);
297 edit_my_define (h, 6, _("Move"), cmd_F6, edit);
298 edit_my_define (h, 7, _("Search"), cmd_F7, edit);
299 edit_my_define (h, 8, _("Delete"), cmd_F8, edit);
300 edit_my_define (h, 9, _("PullDn"), edit_menu_cmd, edit);
301 edit_my_define (h, 10, _("Quit"), cmd_F10, edit);
303 buttonbar_redraw (h);
306 void edit_update_screen (WEdit * e)
308 edit_scroll_screen_over_cursor (e);
310 edit_update_curs_col (e);
311 edit_status (e);
313 /* pop all events for this window for internal handling */
315 if (!is_idle ()) {
316 e->force |= REDRAW_PAGE;
317 return;
319 if (e->force & REDRAW_COMPLETELY)
320 e->force |= REDRAW_PAGE;
321 edit_render_keypress (e);
324 static cb_ret_t
325 edit_callback (Widget *w, widget_msg_t msg, int parm)
327 WEdit *e = (WEdit *) w;
329 switch (msg) {
330 case WIDGET_INIT:
331 e->force |= REDRAW_COMPLETELY;
332 edit_labels (e);
333 return MSG_HANDLED;
335 case WIDGET_DRAW:
336 e->force |= REDRAW_COMPLETELY;
337 e->num_widget_lines = LINES - 2;
338 e->num_widget_columns = COLS;
339 /* fallthrough */
341 case WIDGET_FOCUS:
342 edit_update_screen (e);
343 return MSG_HANDLED;
345 case WIDGET_KEY:
347 int cmd, ch;
349 /* The user may override the access-keys for the menu bar. */
350 if (edit_translate_key (e, parm, &cmd, &ch)) {
351 edit_execute_key_command (e, cmd, ch);
352 edit_update_screen (e);
353 return MSG_HANDLED;
354 } else if (edit_drop_hotkey_menu (e, parm)) {
355 return MSG_HANDLED;
356 } else {
357 return MSG_NOT_HANDLED;
361 case WIDGET_CURSOR:
362 widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
363 e->curs_col + e->start_col);
364 return MSG_HANDLED;
366 case WIDGET_DESTROY:
367 edit_clean (e);
368 return MSG_HANDLED;
370 default:
371 return default_proc (msg, parm);