-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursor.c
283 lines (241 loc) · 5.74 KB
/
cursor.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
/*
* cursor.c
*
* CAVE WALL - Colored Ascii Visual Editor Without Any Loathsome Limitations
* Cursor movement routines
*
* Copyright (C) 2002 Michal Miszewski <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <curses.h>
#include <stdlib.h>
#include "config.h"
#include "cursor.h"
int actx = 0, acty = 0;
short mode = 0; /* cursor movement mode */
chtype oldchar; /* character under the cursor */
extern struct config
{
int defcolor, defbgnd, defattr, defmode, fromzero,
key_up, key_down, key_left, key_right,
key_leftborder, key_rightborder, key_upborder, key_downborder,
key_delch, key_backspc, key_addattribs,
key_export, key_import, key_color, key_bgnd, key_attrib, key_mode, key_clear, key_block, key_paste, key_undo, key_menu, key_unknown;
} conf;
#ifdef HIGHLIGHT
/* highlights block selection */
void highlight_block(void)
{
static int xmin,xmax,ymin,ymax;
int x,y;
static bool highlight=false;
if (!highlight)
{
if (xFIN!=-1)
{
xmin=xSTART;
xmax=xFIN;
ymin=ySTART;
ymax=yFIN;
}
else
{
if (xSTART>actx)
{
xmin=actx;
xmax=xSTART;
}
else
{
xmin=xSTART;
xmax=actx;
}
if (ySTART>acty)
{
ymin=acty;
ymax=ySTART;
}
else
{
ymin=ySTART;
ymax=acty;
}
}
if ((xmin<leftline && xmax<leftline) || (ymin<topline && ymax<topline) || (xmin>leftline+xsize) || (ymin>topline+ysize-1))
return;
if (xmin<leftline)
xmin=leftline;
if (ymin<topline)
ymin=topline;
if (xmax>leftline+xsize)
xmax=leftline+xsize;
if (ymax>topline+ysize-1)
ymax=topline+ysize-1;
highlight=true;
}
else
highlight=false;
for (x=xmin; x<=xmax; x++)
for (y=ymin; y<=ymax; y++)
mvwaddch(editp,y,x,mvwinch(editp,y,x) ^ A_STANDOUT);
return;
}
#endif
/* refreshes edit pad */
void editprefresh(void)
{
touchwin(editp);
#ifdef HIGHLIGHT
if (blockmode)
{
highlight_block();
prefresh(editp,topline,leftline,0,0,ysize-2,xsize-1);
highlight_block();
}
else
#endif
prefresh(editp,topline,leftline,0,0,ysize-2,xsize-1);
}
/* puts the cursor on selected position */
void put_cursor(int y, int x)
{
acty = y;
actx = x;
oldchar = mvwinch(editp, y, x);
mvwaddch(editp, y, x, oldchar | A_REVERSE);
}
/* hides the cursor (for import/export.c routines) */
void cursor_up(void)
{
mvwaddch(editp, acty, actx, oldchar);
wrefresh(editp);
}
/* shows the cursor (for import/export.c routines) */
void cursor_down(void)
{
put_cursor(acty, actx);
editprefresh();
}
/* moves the cursor to selected position */
void mv_cursor(int y, int x)
{
mvwaddch(editp, acty, actx, oldchar); /* restore non-inverted char */
put_cursor(y, x);
editprefresh();
}
/* moves the cursor up */
void move_up(void)
{
if (acty > 0) mv_cursor(acty - 1, actx);
if (acty < topline) editp_scroll(-1,0);
}
/* moves the cursor down */
void move_down(void)
{
if (acty < 999)
{
mv_cursor(acty + 1, actx);
if (acty > topline+ysize-2) editp_scroll(1,0);
}
}
/* moves the cursor left */
void move_left(void)
{
if (actx > 0) mv_cursor(acty, actx - 1);
if (actx < leftline) editp_scroll(0,-1);
}
/* moves the cursor right */
void move_right(void)
{
if (actx < 999)
{
mv_cursor(acty, actx + 1);
if (actx > leftline+xsize-1) editp_scroll(0,1);
}
}
#ifdef RANDOM
/* generates random number between min and max */
int randomize(int min, int max)
{
int ret;
srandom(fgetc(rnd));
ret = ((random() % (max - min + 1)) + min);
return ret;
}
#endif
/* steps using method selected in 'mode' */
void step(void)
{
int one = mode;
#ifdef RANDOM
if (mode == 9) one = randomize(1, 8); /* the move_rnd mode */
#endif
switch (one)
{
case 1: { move_right(); break; }
case 2: { move_left(); break; }
case 3: { move_up(); break; }
case 4: { move_down(); break; }
case 5: { move_up(); move_left(); break; }
case 6: { move_up(); move_right(); break; }
case 7: { move_down(); move_left(); break; }
case 8: { move_down(); move_right(); break; }
}
}
/* steps backwards using method selected in 'mode' */
void step_bwd(void)
{
int one = mode;
#ifdef RANDOM
if (mode == 9) one = randomize(1, 8); /* the move_rnd mode */
#endif
switch (one)
{
case 1: { move_left(); break; }
case 2: { move_right(); break; }
case 3: { move_down(); break; }
case 4: { move_up(); break; }
case 5: { move_down(); move_right(); break; }
case 6: { move_down(); move_left(); break; }
case 7: { move_up(); move_right(); break; }
case 8: { move_up(); move_left(); break; }
}
}
/* gets the start-up values from the conf structure */
void cursor_load_defaults(void)
{
mode = conf.defmode;
}
/* makes editp wider or higher by 1 screen */
void change_editp_size(int down, int right)
{
if ((right==0) && (down==0))
return;
truexsize+=right;
trueysize+=down;
if (truexsize>1000) truexsize=1000;
if (trueysize>1000) trueysize=1000;
if (truexsize<=xsize) truexsize=xsize+1;
if (trueysize<=ysize-1) trueysize=ysize;
wresize(editp,trueysize,truexsize);
return;
}
/* changes topline and leftline coordinates and checks if we are not oustide the edit pad (if so it calls change_editp_size) */
void editp_scroll(int dlines, int rlines)
{
int resizex=0, resizey=0;
leftline+=rlines;
topline+=dlines;
if (topline<0) topline=0;
if (leftline<0) leftline=0;
if (topline>1000-ysize+1) topline=1000-ysize+1;
if (leftline>1000-xsize) leftline=1000-xsize;
if (topline+ysize-1>=trueysize) resizey=ysize-1;
if (leftline+xsize>=truexsize) resizex=xsize;
change_editp_size(resizey, resizex);
editprefresh();
return;
}