-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscroll.c
285 lines (259 loc) · 6.96 KB
/
scroll.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
/*
* scroll.c
* Page scrolling and panning actions.
*
* Copyright (C) 1998 Eric A. Howe
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Authors: Eric A. Howe ([email protected])
*/
#include <wlib/rcs.h>
MU_ID("$Mu: mgv/scroll.c 1.22 1998/12/29 02:37:59 $")
#include <string.h>
#include <X11/IntrinsicP.h>
#include <Xm/XmStrDefs.h>
#include <wlib/stdlib.h>
#include <wlib/wlib.h>
#include <wlib/sanity.h>
#include <wlib/scrollbar.h>
#include <wlib/util.h>
#include <wlib/display.h>
#include <mine/mgv.h>
#include <mine/error.h>
#include <mine/scroll.h>
#include <mine/strings.h>
#include <mine/page_control.h>
#include <mine/sens.h>
#include <mine/center.h>
#include <mine/uiutil.h>
/*
* Don't look kids, this function is being naughty under the covers.
*
* There is some overlap between this and the scrolling for the mgvScroll
* action but that can't really be helped without making more mess.
*/
static void
pan(MGV *m, int dx, int dy)
{
WLSBV h, v;
Widget hw = NULL;
Widget vw = NULL;
Position x, y;
Arg a[2];
Cardinal n;
hw = wl_getsbvalues(m->main, "*view*HorScrollBar", &h);
if(h.value + dx < h.min)
dx = h.min - h.value;
else if(h.value + dx > h.max - h.slider)
dx = h.max - h.slider - h.value;
vw = wl_getsbvalues(m->main, "*view*VertScrollBar", &v);
if(v.value + dy < v.min)
dy = v.min - v.value;
else if(v.value + dy > v.max - v.slider)
dy = v.max - v.slider - v.value;
h.value += dx;
v.value += dy;
wl_setsbvalues(hw, &h, False);
wl_setsbvalues(vw, &v, False);
n = 0;
XtSetArg(a[n], XmNx, &x); ++n;
XtSetArg(a[n], XmNy, &y); ++n;
wlGetValues(m->ghost, a, n);
XtMoveWidget(m->ghost, x - dx, y - dy);
}
void
mgv_action_pan(Widget w, XEvent *ev, String *argv, Cardinal *argc)
{
MGV *m;
char *cmd;
USEUP(argc);
m = mgv_userdata(w);
cmd = wl_lstrdup(*argv);
if(strcmp(cmd, "begin") == 0) {
m->startx = ev->xbutton.x;
m->starty = ev->xbutton.y;
}
else if(strcmp(cmd, "end") == 0) {
m->startx = m->curx = -1;
m->starty = m->cury = -1;
}
else if(strcmp(cmd, "pan") == 0) {
m->curx = ev->xbutton.x;
m->cury = ev->xbutton.y;
pan(m, m->startx - m->curx, m->starty - m->cury);
}
else {
mgv_warn(m->ghost, wl_s(w, MgvSbadArgument), "mgvPan", *argv);
}
wl_free(cmd);
}
/*
* scrolling action
* You're not supposed to fiddle with the scrollbars in a scrolled window
* but it works so screw the OSF.
*/
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
void
mgv_action_scroll(Widget w, XEvent *ev, String *argv, Cardinal *argc)
{
MGV *m;
char *action = NULL;
char *magical = NULL;
int way, force, value, incr, dir;
WLSBV v;
USEUP(argc); USEUP(ev);
m = mgv_userdata(w);
if(*argc >= 1)
action = wl_lstrdup(argv[0]);
if(*argc >= 2)
magical = wl_lstrdup(argv[1]);
force = 1;
if(*argc >= 3) {
if((force = atoi(argv[2])) < 0)
force *= -1;
else if(force == 0)
force = 1;
}
memset((void *)&v, '\0', sizeof(v));
if(strcmp(action, "down") == 0) {
dir = 1;
way = DOWN;
w = wl_getsbvalues(m->main, "*view*VertScrollBar", &v);
}
else if(strcmp(action, "up") == 0) {
dir = -1;
way = UP;
w = wl_getsbvalues(m->main, "*view*VertScrollBar", &v);
}
else if(strcmp(action, "right") == 0) {
dir = 1;
way = RIGHT;
w = wl_getsbvalues(m->main, "*view*HorScrollBar", &v);
}
else if(strcmp(action, "left") == 0) {
dir = -1;
way = LEFT;
w = wl_getsbvalues(m->main, "*view*HorScrollBar", &v);
}
else {
XBell(wl_display(w), 100);
wl_free(action);
wl_free(magical);
return;
}
/*
* Yep, I got bitten by this when I was using the wrong parent
* widget in the wl_getsbvalues() calls above.
*/
wl_assert(w != NULL);
/*
* Check magic scrolling.
* We abuse the MgvS things a little bit here but it gives
* a fairly simple solution. We also query the scrollbar values
* again after we move to the new page since the page size can
* change.
*
* This chunk is a little convoluted because of the magic resistance.
*/
if((way == UP || way == DOWN)
&& (m->bits & MgvMAGICSCROLL)
&& strcmp(magical, "magical") == 0) {
/*
* Some would consider this wonderful little construction
* an abuse--I call it a valid and wholesome technique.
*/
int hehe = (way == UP) + 2*(m->last_magic > 0)
+ 4*(m->last_magic == 0);
switch(hehe) {
case 0: case 4: m->last_magic -= force; break;
case 1: m->last_magic = 0; break;
case 2: m->last_magic = 0; break;
case 3: case 5: m->last_magic += force; break;
default:
/* can't happen (unless your compiler is busted) */
wl_assert("busted compiler!" != NULL);
break;
}
if(way == UP
&& v.value == v.min
&& (m->sens & MgvSNOTFIRST)) {
if(m->last_magic < m->magic_resistance)
return;
mgv_pgstack_prev(m->pgstack);
if(m->center == mgv_center_center)
return;
v.value = v.max - v.slider;
wl_setsbvalues(w, &v, True);
m->last_magic = 0;
wl_free(action);
wl_free(magical);
return;
}
if(way == DOWN
&& v.value + v.slider == v.max
&& (m->sens & MgvSNOTLAST)) {
if(m->last_magic > -m->magic_resistance)
return;
mgv_pgstack_next(m->pgstack);
if(m->center == mgv_center_center)
return;
v.value = 0;
wl_setsbvalues(w, &v, True);
m->last_magic = 0;
wl_free(action);
wl_free(magical);
return;
}
}
m->last_magic = 0;
/*
* We move m->scrollfactor of the _slider_ size (the slider
* represents one screen full in the scrollbar's coordinate
* system). This makes things easy and sensible for both me,
* the programmer, and you, the not-me's (I could have said
* "users" but I'm a user too so that would be a misleading
* thing to say).
*
* Once we have the final value (unadjusted for page size
* limits), we can compute the individual increments based
* on the m->smoothness value (which is the percentage of
* the total increment that each little hop should be).
*/
value = v.slider * m->scrollfactor;
if((incr = value / m->smoothness) <= 0)
incr = 1;
incr *= dir;
value = v.value + dir*value;
if(value < v.min)
value = v.min;
else if(value > v.max - v.slider)
value = v.max - v.slider;
if((m->bits & MgvSMOOTH) && incr < 0) {
while((v.value += incr) > value)
wl_setsbvalues(w, &v, True);
}
else if((m->bits & MgvSMOOTH) && incr > 0) {
while((v.value += incr) < value)
wl_setsbvalues(w, &v, True);
}
v.value = value;
wl_setsbvalues(w, &v, True);
wl_free(action);
wl_free(magical);
}