-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage_control.c
300 lines (250 loc) · 5.78 KB
/
page_control.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
/*
* page_control.c
* Page stack control.
*
* 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: Matthew D. Francey
* Eric A. Howe ([email protected])
*/
#include <wlib/rcs.h>
MU_ID("$Mu: mgv/page_control.c 1.22 1998/11/11 05:59:53 $")
#include <Xm/List.h>
#include <wlib/stdlib.h>
#include <wlib/sanity.h>
#include <mine/mgv.h>
#include <mine/page_control.h>
#include <mine/sens.h>
/*
* The typedef for this is in mine/mgv.h to avoid #include loops and such.
*/
#define PGS_MAGIC1 0x50475354 /* 'P' 'G' 'S' 'T' */
#define PGS_MAGIC2 0x54534750 /* 'T' 'S' 'G' 'P' */
struct MGVP_s {
long magic1;
MGV *m;
int *list;
int size; /* current size */
int current; /* current position */
int used; /* number of entries used */
long magic2;
};
/*
* Initial size and increment.
*/
#define INCR 10
static int
pgs_ok(MGVP p)
{
DIEIF(p == NULL);
DIEIF(p->magic1 != PGS_MAGIC1);
DIEIF(p->magic2 != PGS_MAGIC2);
DIEIF(p->m == NULL);
return TRUE;
}
/*
* When the list size (MGVP.size) is this value then
* there is no list at all. This will happen with non-DSC
* documents where we can't switch to arbitrary pages.
* Basically, this value serves as a flag which says "don't do
* anything with p->list".
*/
#define NODSC -1
/*
* This is just a nicer way of using NODSC.
*/
#define haspgstack(p) ((p)->size != NODSC)
static void
show(MGV *m, int page)
{
int top, vis;
Arg a[2];
Cardinal n;
mgv_show(m, page);
if(m->dsc == NULL)
return;
n = 0;
XtSetArg(a[n], XmNtopItemPosition, &top); ++n;
XtSetArg(a[n], XmNvisibleItemCount, &vis); ++n;
wlGetValues(m->pagelist, a, n);
/*
* Convert to Motif List numbering.
*/
++page;
/*
* Check visibility.
*/
if(page < top) {
XmListSetPos(m->pagelist, page);
}
else if(page > top + vis - 1) {
/*
* If we're near the end of the list we want to decrease
* the top position to keep the list's screen area full of
* entries (note: if we XmListSetPos(m->pagelist,
* last_page_pos) the list will end up with one visible
* item, we wish to avoid this situation).
*/
if(page > m->dsc->n_pages - vis)
page = m->dsc->n_pages - vis + 1;
XmListSetPos(m->pagelist, page);
}
}
MGVP
mgv_pgstack_alloc(MGV *m)
{
MGVP p = wl_malloc(sizeof(struct MGVP_s));
p->magic1 = PGS_MAGIC1;
p->magic2 = PGS_MAGIC2;
/*
* If there is no DSC information then there's no
* point in having a page list.
*/
if(m->dsc != NULL) {
p->size = INCR;
p->list = wl_calloc(p->size, sizeof(int));
}
else {
p->size = NODSC;
p->list = NULL;
}
p->used = 0;
p->current = -1;
p->m = m;
wl_assert(pgs_ok(p));
return p;
}
MGVP
mgv_pgstack_free(MGVP p)
{
if(p == NULL)
return p;
wl_assert(pgs_ok(p));
wl_free(p->list);
wl_free(p);
return NULL;
}
static void
resize(MGVP p, int size)
{
wl_assert(pgs_ok(p));
wl_assert(haspgstack(p));
if(p->size >= size + 1)
return;
while(p->size <= size)
p->size += INCR;
p->list = wl_realloc(p->list, p->size*sizeof(int));
}
void
mgv_pgstack_forward(MGVP p)
{
int sens = p->m->sens;
wl_assert(pgs_ok(p));
wl_assert(haspgstack(p));
if(p->current == p->used - 1) {
wl_assert("Cannot go forward at the end of the list!" != NULL);
return;
}
++p->current;
if(p->current == p->used - 1)
sens &= ~MgvSCANFORWARD;
sens |= MgvSCANBACK;
if(p->m->sens != sens)
wl_sens(p->m->senscache, p->m->sens = sens);
show(p->m, p->list[p->current]);
}
void
mgv_pgstack_goto(MGVP p, int page)
{
if(p == NULL)
return;
wl_assert(pgs_ok(p));
/*
* This shouldn't happen but LessTif (as of 1998.08.06) is
* activating accelerators and mnemonics on insensitive widgets.
* Fear not gentle readers, a patch has been submitted and it
* is in the recent LessTif sources.
*/
if(page < 0)
return;
if(haspgstack(p)
&& (p->current == -1 || p->list[p->current] != page)) {
int sens = p->m->sens;
resize(p, p->current + 1);
p->list[++p->current] = page;
p->used = p->current + 1;
if(!(sens & MgvSCANBACK)
&& p->current != 0)
sens |= MgvSCANBACK;
sens &= ~MgvSCANFORWARD;
if(p->m->sens != sens)
wl_sens(p->m->senscache, p->m->sens = sens);
}
show(p->m, page);
}
void
mgv_pgstack_back(MGVP p)
{
int sens = p->m->sens;
wl_assert(pgs_ok(p));
wl_assert(haspgstack(p));
if(p->current <= 0) {
wl_assert("Cannot go back at the beginning of the list!" != NULL);
return;
}
--p->current;
if(p->current == 0)
sens &= ~MgvSCANBACK;
sens |= MgvSCANFORWARD;
if(sens != p->m->sens)
wl_sens(p->m->senscache, p->m->sens = sens);
show(p->m, p->list[p->current]);
}
void
mgv_pgstack_next(MGVP p)
{
int page;
if(p == NULL)
return;
wl_assert(pgs_ok(p));
page = p->m->pageq < 0 ? p->m->page + 1 : p->m->pageq + 1;
mgv_pgstack_goto(p, page);
}
void
mgv_pgstack_prev(MGVP p)
{
int page;
if(p == NULL)
return;
wl_assert(pgs_ok(p));
page = p->m->pageq < 0 ? p->m->page - 1 : p->m->pageq - 1;
mgv_pgstack_goto(p, page);
}
void
mgv_pgstack_prefill(MGVP p, int pages)
{
int i;
wl_assert(pgs_ok(p));
if(!haspgstack(p))
return;
resize(p, pages);
p->used = pages;
for(i = 0; i < pages; ++i)
p->list[i] = i;
p->current = 0;
wl_sens(p->m->senscache, p->m->sens |= MgvSCANFORWARD);
}