forked from mooffie/mc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditwidget.c
377 lines (298 loc) · 9.13 KB
/
editwidget.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* editor initialisation and callback handler.
Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
2007 Free Software Foundation, Inc.
Authors: 1996, 1997 Paul Sheer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/** \file
* \brief Source: editor initialisation and callback handler
* \author Paul Sheer
* \date 1996, 1997
*/
#include <config.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "../src/global.h"
#include "../src/tty/tty.h" /* LINES, COLS */
#include "../src/tty/key.h" /* is_idle() */
#include "edit-impl.h"
#include "edit-widget.h"
#include "../src/widget.h" /* buttonbar_redraw() */
#include "../src/menu.h" /* menubar_new() */
WEdit *wedit;
struct WMenu *edit_menubar;
int column_highlighting = 0;
static cb_ret_t edit_callback (Widget *, widget_msg_t msg, int parm);
static int
edit_event (Gpm_Event *event, void *data)
{
WEdit *edit = (WEdit *) data;
/* Unknown event type */
if (!(event->type & (GPM_DOWN | GPM_DRAG | GPM_UP)))
return MOU_NORMAL;
/* rest of the upper frame, the menu is invisible - call menu */
if ((event->type & GPM_DOWN) && (event->y == 1))
return edit_menubar->widget.mouse (event, edit_menubar);
edit_update_curs_row (edit);
edit_update_curs_col (edit);
/* Outside editor window */
if (event->y <= 1 || event->x <= 0
|| event->x > edit->num_widget_columns
|| event->y > edit->num_widget_lines + 1)
return MOU_NORMAL;
/* Wheel events */
if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
edit_move_up (edit, 2, 1);
goto update;
}
if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
edit_move_down (edit, 2, 1);
goto update;
}
/* A lone up mustn't do anything */
if (edit->mark2 != -1 && event->type & (GPM_UP | GPM_DRAG))
return MOU_NORMAL;
if (event->type & (GPM_DOWN | GPM_UP))
edit_push_key_press (edit);
if (option_cursor_beyond_eol) {
long line_len = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
edit_eol(edit, edit->curs1));
if ( event->x > line_len ) {
edit->over_col = event->x - line_len - option_line_state_width - 1;
edit->prev_col = line_len;
} else {
edit->over_col = 0;
edit->prev_col = event->x - option_line_state_width - 1;
}
} else {
edit->prev_col = event->x - edit->start_col - option_line_state_width - 1;
}
if (--event->y > (edit->curs_row + 1))
edit_move_down (edit, event->y - (edit->curs_row + 1), 0);
else if (event->y < (edit->curs_row + 1))
edit_move_up (edit, (edit->curs_row + 1) - event->y, 0);
else
edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
if (event->type & GPM_DOWN) {
edit_mark_cmd (edit, 1); /* reset */
edit->highlight = 0;
}
if (!(event->type & GPM_DRAG))
edit_mark_cmd (edit, 0);
update:
edit_find_bracket (edit);
edit->force |= REDRAW_COMPLETELY;
edit_update_curs_row (edit);
edit_update_curs_col (edit);
edit_update_screen (edit);
return MOU_NORMAL;
}
static void
edit_adjust_size (Dlg_head *h)
{
WEdit *edit;
WButtonBar *edit_bar;
edit = (WEdit *) find_widget_type (h, edit_callback);
edit_bar = find_buttonbar (h);
widget_set_size (&edit->widget, 0, 0, LINES - 1, COLS);
widget_set_size ((Widget *) edit_bar, LINES - 1, 0, 1, COLS);
widget_set_size (&edit_menubar->widget, 0, 0, 1, COLS);
#ifdef RESIZABLE_MENUBAR
menubar_arrange (edit_menubar);
#endif
}
/* Callback for the edit dialog */
static cb_ret_t
edit_dialog_callback (Dlg_head *h, dlg_msg_t msg, int parm)
{
WEdit *edit;
switch (msg) {
case DLG_RESIZE:
edit_adjust_size (h);
return MSG_HANDLED;
case DLG_VALIDATE:
edit = (WEdit *) find_widget_type (h, edit_callback);
if (!edit_ok_to_exit (edit)) {
h->running = 1;
}
return MSG_HANDLED;
default:
return default_dlg_callback (h, msg, parm);
}
}
int
edit_file (const char *_file, int line)
{
static int made_directory = 0;
Dlg_head *edit_dlg;
WButtonBar *edit_bar;
if (!made_directory) {
char *dir = concat_dir_and_file (home_dir, EDIT_DIR);
made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST);
g_free (dir);
}
if (!(wedit = edit_init (NULL, LINES - 2, COLS, _file, line))) {
return 0;
}
/* Create a new dialog and add it widgets to it */
edit_dlg =
create_dlg (0, 0, LINES, COLS, NULL, edit_dialog_callback,
"[Internal File Editor]", NULL, DLG_WANT_TAB);
init_widget (&(wedit->widget), 0, 0, LINES - 1, COLS,
edit_callback, edit_event);
widget_want_cursor (wedit->widget, 1);
edit_bar = buttonbar_new (1);
edit_menubar = edit_create_menu ();
add_widget (edit_dlg, edit_bar);
add_widget (edit_dlg, wedit);
add_widget (edit_dlg, edit_menubar);
run_dlg (edit_dlg);
edit_done_menu (edit_menubar); /* editmenu.c */
destroy_dlg (edit_dlg);
return 1;
}
const char *
edit_get_file_name (const WEdit *edit)
{
return edit->filename;
}
static void edit_my_define (Dlg_head * h, int idx, const char *text,
void (*fn) (WEdit *), WEdit * edit)
{
text = edit->labels[idx - 1]? edit->labels[idx - 1] : text;
/* function-cast ok */
buttonbar_set_label_data (h, idx, text, (buttonbarfn) fn, edit);
}
static void cmd_F1 (WEdit * edit)
{
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (1));
}
static void cmd_F2 (WEdit * edit)
{
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (2));
}
static void cmd_F3 (WEdit * edit)
{
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (3));
}
static void cmd_F4 (WEdit * edit)
{
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (4));
}
static void cmd_F5 (WEdit * edit)
{
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (5));
}
static void cmd_F6 (WEdit * edit)
{
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (6));
}
static void cmd_F7 (WEdit * edit)
{
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (7));
}
static void cmd_F8 (WEdit * edit)
{
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (8));
}
#if 0
static void cmd_F9 (WEdit * edit)
{
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (9));
}
#endif
static void cmd_F10 (WEdit * edit)
{
send_message ((Widget *) edit, WIDGET_KEY, KEY_F (10));
}
static void
edit_labels (WEdit *edit)
{
Dlg_head *h = edit->widget.parent;
edit_my_define (h, 1, _("Help"), cmd_F1, edit);
edit_my_define (h, 2, _("Save"), cmd_F2, edit);
edit_my_define (h, 3, _("Mark"), cmd_F3, edit);
edit_my_define (h, 4, _("Replac"), cmd_F4, edit);
edit_my_define (h, 5, _("Copy"), cmd_F5, edit);
edit_my_define (h, 6, _("Move"), cmd_F6, edit);
edit_my_define (h, 7, _("Search"), cmd_F7, edit);
edit_my_define (h, 8, _("Delete"), cmd_F8, edit);
edit_my_define (h, 9, _("PullDn"), edit_menu_cmd, edit);
edit_my_define (h, 10, _("Quit"), cmd_F10, edit);
buttonbar_redraw (h);
}
void edit_update_screen (WEdit * e)
{
edit_scroll_screen_over_cursor (e);
edit_update_curs_col (e);
edit_status (e);
/* pop all events for this window for internal handling */
if (!is_idle ()) {
e->force |= REDRAW_PAGE;
return;
}
if (e->force & REDRAW_COMPLETELY)
e->force |= REDRAW_PAGE;
edit_render_keypress (e);
}
static cb_ret_t
edit_callback (Widget *w, widget_msg_t msg, int parm)
{
WEdit *e = (WEdit *) w;
switch (msg) {
case WIDGET_INIT:
e->force |= REDRAW_COMPLETELY;
edit_labels (e);
return MSG_HANDLED;
case WIDGET_DRAW:
e->force |= REDRAW_COMPLETELY;
e->num_widget_lines = LINES - 2;
e->num_widget_columns = COLS;
/* fallthrough */
case WIDGET_FOCUS:
edit_update_screen (e);
return MSG_HANDLED;
case WIDGET_KEY:
{
int cmd, ch;
/* The user may override the access-keys for the menu bar. */
if (edit_translate_key (e, parm, &cmd, &ch)) {
edit_execute_key_command (e, cmd, ch);
edit_update_screen (e);
return MSG_HANDLED;
} else if (edit_drop_hotkey_menu (e, parm)) {
return MSG_HANDLED;
} else {
return MSG_NOT_HANDLED;
}
}
case WIDGET_CURSOR:
widget_move (&e->widget, e->curs_row + EDIT_TEXT_VERTICAL_OFFSET,
e->curs_col + e->start_col + e->over_col +
EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width);
return MSG_HANDLED;
case WIDGET_DESTROY:
edit_clean (e);
return MSG_HANDLED;
default:
return default_proc (msg, parm);
}
}