Merge branch '1556_find_all_i18n'
[pantumic.git] / edit / editwidget.c
blob0f7b996fe03b3393f3d73c407fd59d5b01a516d2
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 /** \file
25 * \brief Source: editor initialisation and callback handler
26 * \author Paul Sheer
27 * \date 1996, 1997
30 #include <config.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <sys/stat.h>
40 #include <stdlib.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() */
53 WEdit *wedit;
54 struct WMenu *edit_menubar;
56 int column_highlighting = 0;
58 static cb_ret_t edit_callback (Widget *, widget_msg_t msg, int parm);
60 static int
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)))
67 return MOU_NORMAL;
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)
80 return MOU_NORMAL;
82 /* Wheel events */
83 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
84 edit_move_up (edit, 2, 1);
85 goto update;
87 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
88 edit_move_down (edit, 2, 1);
89 goto update;
92 /* A lone up mustn't do anything */
93 if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
94 return MOU_NORMAL;
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;
105 } else {
106 edit->over_col = 0;
107 edit->prev_col = event->x - option_line_state_width - 1;
109 } else {
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);
117 else
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 */
122 edit->highlight = 0;
125 if (!(event->type & GPM_DRAG))
126 edit_mark_cmd (edit, 0);
128 update:
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);
135 return MOU_NORMAL;
138 static void
139 edit_adjust_size (Dlg_head *h)
141 WEdit *edit;
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);
153 #endif
156 /* Callback for the edit dialog */
157 static cb_ret_t
158 edit_dialog_callback (Dlg_head *h, dlg_msg_t msg, int parm)
160 WEdit *edit;
162 switch (msg) {
163 case DLG_RESIZE:
164 edit_adjust_size (h);
165 return MSG_HANDLED;
167 case DLG_VALIDATE:
168 edit = (WEdit *) find_widget_type (h, edit_callback);
169 if (!edit_ok_to_exit (edit)) {
170 h->running = 1;
172 return MSG_HANDLED;
174 default:
175 return default_dlg_callback (h, msg, parm);
180 edit_file (const char *_file, int line)
182 static int made_directory = 0;
183 Dlg_head *edit_dlg;
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);
189 g_free (dir);
192 if (!(wedit = edit_init (NULL, LINES - 2, COLS, _file, line))) {
193 return 0;
196 /* Create a new dialog and add it widgets to it */
197 edit_dlg =
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);
214 run_dlg (edit_dlg);
216 edit_done_menu (edit_menubar); /* editmenu.c */
218 destroy_dlg (edit_dlg);
220 return 1;
223 const char *
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));
278 #if 0
279 static void cmd_F9 (WEdit * edit)
281 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (9));
283 #endif
285 static void cmd_F10 (WEdit * edit)
287 send_message ((Widget *) edit, WIDGET_KEY, KEY_F (10));
290 static void
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);
314 edit_status (e);
316 /* pop all events for this window for internal handling */
318 if (!is_idle ()) {
319 e->force |= REDRAW_PAGE;
320 return;
322 if (e->force & REDRAW_COMPLETELY)
323 e->force |= REDRAW_PAGE;
324 edit_render_keypress (e);
327 static cb_ret_t
328 edit_callback (Widget *w, widget_msg_t msg, int parm)
330 WEdit *e = (WEdit *) w;
332 switch (msg) {
333 case WIDGET_INIT:
334 e->force |= REDRAW_COMPLETELY;
335 edit_labels (e);
336 return MSG_HANDLED;
338 case WIDGET_DRAW:
339 e->force |= REDRAW_COMPLETELY;
340 e->num_widget_lines = LINES - 2;
341 e->num_widget_columns = COLS;
342 /* fallthrough */
344 case WIDGET_FOCUS:
345 edit_update_screen (e);
346 return MSG_HANDLED;
348 case WIDGET_KEY:
350 int cmd, ch;
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);
356 return MSG_HANDLED;
357 } else if (edit_drop_hotkey_menu (e, parm)) {
358 return MSG_HANDLED;
359 } else {
360 return MSG_NOT_HANDLED;
364 case WIDGET_CURSOR:
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);
368 return MSG_HANDLED;
370 case WIDGET_DESTROY:
371 edit_clean (e);
372 return MSG_HANDLED;
374 default:
375 return default_proc (msg, parm);