Merge branch '2087_standalone_mcedit_crash'
[kaloumi3.git] / src / editor / editwidget.c
blob4500c97666c01d0803b78ac54840affa8b292888
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 "lib/global.h"
44 #include "lib/tty/tty.h" /* LINES, COLS */
45 #include "lib/tty/key.h" /* is_idle() */
47 #include "edit-impl.h"
48 #include "edit-widget.h"
50 #include "src/dialog.h"
51 #include "src/widget.h" /* ButtonBar */
52 #include "src/menu.h" /* menubar_new() */
53 #include "src/cmddef.h"
55 WEdit *wedit;
56 struct WMenuBar *edit_menubar;
58 int column_highlighting = 0;
60 static cb_ret_t edit_callback (Widget *, widget_msg_t msg, int parm);
62 static char *
63 edit_get_shortcut (unsigned long command)
65 const char *ext_map;
66 const char *shortcut = NULL;
68 shortcut = lookup_keymap_shortcut (editor_map, command);
69 if (shortcut != NULL)
70 return g_strdup (shortcut);
72 ext_map = lookup_keymap_shortcut (editor_map, CK_Ext_Mode);
73 if (ext_map != NULL)
74 shortcut = lookup_keymap_shortcut (editor_x_map, command);
75 if (shortcut != NULL)
76 return g_strdup_printf ("%s %s", ext_map, shortcut);
78 return NULL;
81 static int
82 edit_event (Gpm_Event *event, void *data)
84 WEdit *edit = (WEdit *) data;
86 /* Unknown event type */
87 if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
88 return MOU_NORMAL;
90 /* rest of the upper frame, the menu is invisible - call menu */
91 if ((event->type & GPM_DOWN) && (event->y == 1))
92 return edit_menubar->widget.mouse (event, edit_menubar);
94 edit_update_curs_row (edit);
95 edit_update_curs_col (edit);
97 /* Outside editor window */
98 if (event->y <= 1 || event->x <= 0
99 || event->x > edit->num_widget_columns
100 || event->y > edit->num_widget_lines + 1)
101 return MOU_NORMAL;
103 /* Wheel events */
104 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
105 edit_move_up (edit, 2, 1);
106 goto update;
108 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
109 edit_move_down (edit, 2, 1);
110 goto update;
113 /* A lone up mustn't do anything */
114 if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
115 return MOU_NORMAL;
117 if (event->type & (GPM_DOWN | GPM_UP))
118 edit_push_key_press (edit);
120 if (option_cursor_beyond_eol) {
121 long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
122 edit_eol(edit, edit->curs1));
124 if ( event->x > line_len ) {
125 edit->over_col = event->x - line_len - edit->start_col - option_line_state_width - 1;
126 edit->prev_col = line_len;
127 } else {
128 edit->over_col = 0;
129 edit->prev_col = event->x - option_line_state_width - edit->start_col - 1;
131 } else {
132 edit->prev_col = event->x - edit->start_col - option_line_state_width - 1;
135 if (--event->y > (edit->curs_row + 1))
136 edit_move_down (edit, event->y - (edit->curs_row + 1), 0);
137 else if (event->y < (edit->curs_row + 1))
138 edit_move_up (edit, (edit->curs_row + 1) - event->y, 0);
139 else
140 edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
142 if (event->type & GPM_DOWN) {
143 edit_mark_cmd (edit, 1); /* reset */
144 edit->highlight = 0;
147 if (!(event->type & GPM_DRAG))
148 edit_mark_cmd (edit, 0);
150 update:
151 edit_find_bracket (edit);
152 edit->force |= REDRAW_COMPLETELY;
153 edit_update_curs_row (edit);
154 edit_update_curs_col (edit);
155 edit_update_screen (edit);
157 return MOU_NORMAL;
160 static cb_ret_t
161 edit_command_execute (WEdit *edit, unsigned long command)
163 if (command == CK_Menu)
164 edit_menu_cmd (edit);
165 else {
166 edit_execute_key_command (edit, command, -1);
167 edit_update_screen (edit);
169 return MSG_HANDLED;
172 static inline void
173 edit_set_buttonbar (WEdit *edit, WButtonBar *bb)
175 buttonbar_set_label (bb, 1, Q_("ButtonBar|Help"), editor_map, (Widget *) edit);
176 buttonbar_set_label (bb, 2, Q_("ButtonBar|Save"), editor_map, (Widget *) edit);
177 buttonbar_set_label (bb, 3, Q_("ButtonBar|Mark"), editor_map, (Widget *) edit);
178 buttonbar_set_label (bb, 4, Q_("ButtonBar|Replac"), editor_map, (Widget *) edit);
179 buttonbar_set_label (bb, 5, Q_("ButtonBar|Copy"), editor_map, (Widget *) edit);
180 buttonbar_set_label (bb, 6, Q_("ButtonBar|Move"), editor_map, (Widget *) edit);
181 buttonbar_set_label (bb, 7, Q_("ButtonBar|Search"), editor_map, (Widget *) edit);
182 buttonbar_set_label (bb, 8, Q_("ButtonBar|Delete"), editor_map, (Widget *) edit);
183 buttonbar_set_label (bb, 9, Q_("ButtonBar|PullDn"), editor_map, (Widget *) edit);
184 buttonbar_set_label (bb, 10, Q_("ButtonBar|Quit"), editor_map, (Widget *) edit);
187 /* Callback for the edit dialog */
188 static cb_ret_t
189 edit_dialog_callback (Dlg_head *h, Widget *sender,
190 dlg_msg_t msg, int parm, void *data)
192 WEdit *edit;
193 WMenuBar *menubar;
194 WButtonBar *buttonbar;
196 edit = (WEdit *) find_widget_type (h, edit_callback);
197 menubar = find_menubar (h);
198 buttonbar = find_buttonbar (h);
200 switch (msg) {
201 case DLG_INIT:
202 edit_set_buttonbar (edit, buttonbar);
203 return MSG_HANDLED;
205 case DLG_RESIZE:
206 widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
207 widget_set_size (&buttonbar->widget , LINES - 1, 0, 1, COLS);
208 widget_set_size (&menubar->widget, 0, 0, 1, COLS);
209 menubar_arrange (menubar);
210 return MSG_HANDLED;
212 case DLG_ACTION:
213 if (sender == (Widget *) menubar)
214 return send_message ((Widget *) edit, WIDGET_COMMAND, parm);
215 if (sender == (Widget *) buttonbar)
216 return send_message ((Widget *) edit, WIDGET_COMMAND, parm);
217 return MSG_HANDLED;
219 case DLG_VALIDATE:
220 if (!edit_ok_to_exit (edit))
221 h->running = 1;
222 return MSG_HANDLED;
224 default:
225 return default_dlg_callback (h, sender, msg, parm, data);
230 edit_file (const char *_file, int line)
232 static gboolean made_directory = FALSE;
233 Dlg_head *edit_dlg;
235 if (!made_directory) {
236 char *dir = concat_dir_and_file (home_dir, EDIT_DIR);
237 made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
238 g_free (dir);
241 wedit = edit_init (NULL, LINES - 2, COLS, _file, line);
243 if (wedit == NULL)
244 return 0;
246 /* Create a new dialog and add it widgets to it */
247 edit_dlg =
248 create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
249 "[Internal File Editor]", NULL, DLG_WANT_TAB);
251 edit_dlg->get_shortcut = edit_get_shortcut;
252 edit_menubar = menubar_new (0, 0, COLS, NULL);
253 add_widget (edit_dlg, edit_menubar);
254 edit_init_menu (edit_menubar);
256 init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
257 edit_callback, edit_event);
258 widget_want_cursor (wedit->widget, 1);
260 add_widget (edit_dlg, wedit);
262 add_widget (edit_dlg, buttonbar_new (TRUE));
264 run_dlg (edit_dlg);
266 destroy_dlg (edit_dlg);
268 return 1;
271 const char *
272 edit_get_file_name (const WEdit *edit)
274 return edit->filename;
277 void
278 edit_update_screen (WEdit * e)
280 edit_scroll_screen_over_cursor (e);
282 edit_update_curs_col (e);
283 edit_status (e);
285 /* pop all events for this window for internal handling */
286 if (!is_idle ())
287 e->force |= REDRAW_PAGE;
288 else {
289 if (e->force & REDRAW_COMPLETELY)
290 e->force |= REDRAW_PAGE;
291 edit_render_keypress (e);
295 static cb_ret_t
296 edit_callback (Widget *w, widget_msg_t msg, int parm)
298 WEdit *e = (WEdit *) w;
300 switch (msg) {
301 case WIDGET_DRAW:
302 e->force |= REDRAW_COMPLETELY;
303 e->num_widget_lines = LINES - 2;
304 e->num_widget_columns = COLS;
305 /* fallthrough */
307 case WIDGET_FOCUS:
308 edit_update_screen (e);
309 return MSG_HANDLED;
311 case WIDGET_KEY:
313 int cmd, ch;
314 cb_ret_t ret = MSG_NOT_HANDLED;
316 /* The user may override the access-keys for the menu bar. */
317 if (edit_translate_key (e, parm, &cmd, &ch)) {
318 edit_execute_key_command (e, cmd, ch);
319 edit_update_screen (e);
320 ret = MSG_HANDLED;
321 } else if (edit_drop_hotkey_menu (e, parm))
322 ret = MSG_HANDLED;
324 return ret;
327 case WIDGET_COMMAND:
328 /* command from menubar or buttonbar */
329 return edit_command_execute (e, parm);
331 case WIDGET_CURSOR:
332 widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
333 e->curs_col + e->start_col + e->over_col +
334 EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width);
335 return MSG_HANDLED;
337 case WIDGET_DESTROY:
338 edit_clean (e);
339 return MSG_HANDLED;
341 default:
342 return default_proc (msg, parm);