Debian Codegraph @

garglk/winmask.cpp

raw symbols SCIP
1 // Copyright (C) 2010 by Ben Cressey, Chris Spiegel.
2 //
3 // This file is part of Gargoyle.
4 //
5 // Gargoyle is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // Gargoyle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Gargoyle; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 #include <algorithm>
20 #include <cstdlib>
21 #include <new>
22 #include <vector>
23
24 #include "glk.h"
25 #include "garglk.h"
26
27 namespace {
28
29 // storage for hyperlink and selection coordinates
30 struct Mask {
31 bool initialized = false;
32 int hor = 0;
33 int ver = 0;
34 std::vector<std::vector<glui32>> links;
35 rect_t select;
36 };
37
38 }
39
40 static Mask gli_mask;
41
42 // for copy selection
43 bool gli_copyselect = false;
44 bool gli_drawselect = false;
45 bool gli_claimselect = false;
46
47 static int last_x = 0;
48 static int last_y = 0;
49
50 void gli_resize_mask(unsigned int x, unsigned int y)
51 {
52 gli_mask.initialized = true;
53 gli_mask.hor = x + 1;
54 gli_mask.ver = y + 1;
55
56 try {
57 gli_mask.links.resize(gli_mask.hor);
58 for (int i = 0; i < gli_mask.hor; i++) {
59 gli_mask.links[i].resize(gli_mask.ver);
60 std::fill(gli_mask.links[i].begin(), gli_mask.links[i].end(), 0);
61 }
62 } catch (const std::bad_alloc &) {
63 gli_strict_warning("resize_mask: out of memory");
64 gli_mask.initialized = false;
65 gli_mask.hor = 0;
66 gli_mask.ver = 0;
67 return;
68 }
69
70 gli_mask.select.x0 = 0;
71 gli_mask.select.y0 = 0;
72 gli_mask.select.x1 = 0;
73 gli_mask.select.y1 = 0;
74 }
75
76 void gli_put_hyperlink(glui32 linkval, unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1)
77 {
78 int i, k;
79 int tx0 = x0 < x1 ? x0 : x1;
80 int tx1 = x0 < x1 ? x1 : x0;
81 int ty0 = y0 < y1 ? y0 : y1;
82 int ty1 = y0 < y1 ? y1 : y0;
83
84 if (!gli_mask.initialized || gli_mask.hor == 0 || gli_mask.ver == 0) {
85 gli_strict_warning("set_hyperlink: struct not initialized");
86 return;
87 }
88
89 if (tx0 >= gli_mask.hor || tx1 >= gli_mask.hor ||
90 ty0 >= gli_mask.ver || ty1 >= gli_mask.ver) {
91 gli_strict_warning("set_hyperlink: invalid range given");
92 return;
93 }
94
95 for (i = tx0; i < tx1; i++) {
96 for (k = ty0; k < ty1; k++) {
97 gli_mask.links[i][k] = linkval;
98 }
99 }
100 }
101
102 glui32 gli_get_hyperlink(int x, int y)
103 {
104 if (!gli_mask.initialized || gli_mask.hor == 0 || gli_mask.ver == 0) {
105 gli_strict_warning("get_hyperlink: struct not initialized");
106 return 0;
107 }
108
109 if (x >= gli_mask.hor || y >= gli_mask.ver) {
110 gli_strict_warning("get_hyperlink: invalid range given");
111 return 0;
112 }
113
114 return gli_mask.links[x][y];
115 }
116
117 void gli_start_selection(int x, int y)
118 {
119 int tx, ty;
120
121 if (!gli_mask.initialized || gli_mask.hor == 0 || gli_mask.ver == 0) {
122 gli_strict_warning("start_selection: mask not initialized");
123 return;
124 }
125
126 tx = x < gli_mask.hor ? x : gli_mask.hor;
127 ty = y < gli_mask.ver ? y : gli_mask.ver;
128
129 gli_mask.select.x0 = last_x = tx;
130 gli_mask.select.y0 = last_y = ty;
131 gli_mask.select.x1 = 0;
132 gli_mask.select.y1 = 0;
133
134 gli_claimselect = false;
135 gli_force_redraw = true;
136 gli_windows_redraw();
137 }
138
139 void gli_move_selection(int x, int y)
140 {
141 int tx, ty;
142
143 if (std::abs(x - last_x) < 5 && std::abs(y - last_y) < 5) {
144 return;
145 }
146
147 if (!gli_mask.initialized || gli_mask.hor == 0 || gli_mask.ver == 0) {
148 gli_strict_warning("move_selection: mask not initialized");
149 return;
150 }
151
152 tx = x < gli_mask.hor ? x : gli_mask.hor;
153 ty = y < gli_mask.ver ? y : gli_mask.ver;
154
155 gli_mask.select.x1 = last_x = tx;
156 gli_mask.select.y1 = last_y = ty;
157
158 gli_claimselect = false;
159 gli_windows_redraw();
160 }
161
162 void gli_clear_selection()
163 {
164 if (!gli_mask.initialized) {
165 gli_strict_warning("clear_selection: mask not initialized");
166 return;
167 }
168
169 if (gli_mask.select.x0 != 0 || gli_mask.select.x1 != 0
170 || gli_mask.select.y0 != 0 || gli_mask.select.y1 != 0) {
171 gli_force_redraw = true;
172 }
173
174 gli_mask.select.x0 = 0;
175 gli_mask.select.y0 = 0;
176 gli_mask.select.x1 = 0;
177 gli_mask.select.y1 = 0;
178
179 gli_claimselect = false;
180 }
181
182 bool gli_check_selection(int x0, int y0,
183 int x1, int y1)
184 {
185 int cx0, cx1, cy0, cy1;
186
187 cx0 = gli_mask.select.x0 < gli_mask.select.x1
188 ? gli_mask.select.x0
189 : gli_mask.select.x1;
190
191 cx1 = gli_mask.select.x0 < gli_mask.select.x1
192 ? gli_mask.select.x1
193 : gli_mask.select.x0;
194
195 cy0 = gli_mask.select.y0 < gli_mask.select.y1
196 ? gli_mask.select.y0
197 : gli_mask.select.y1;
198
199 cy1 = gli_mask.select.y0 < gli_mask.select.y1
200 ? gli_mask.select.y1
201 : gli_mask.select.y0;
202
203 if (cx0 == 0 || cx1 == 0 || cy0 == 0 || cy1 == 0) {
204 return false;
205 }
206
207 if (cx0 >= x0 && cx0 <= x1
208 && cy0 >= y0 && cy0 <= y1) {
209 return true;
210 }
211
212 if (cx0 >= x0 && cx0 <= x1
213 && cy1 >= y0 && cy1 <= y1) {
214 return true;
215 }
216
217 if (cx1 >= x0 && cx1 <= x1
218 && cy0 >= y0 && cy0 <= y1) {
219 return true;
220 }
221
222 if (cx1 >= x0 && cx1 <= x1
223 && cy1 >= y0 && cy1 <= y1) {
224 return true;
225 }
226
227 return false;
228 }
229
230 bool gli_get_selection(int x0, int y0,
231 int x1, int y1,
232 int *rx0, int *rx1)
233 {
234 int row, upper, lower, above, below;
235 bool row_selected, found_left, found_right;
236 bool from_right, from_below, is_above, is_below;
237 int cx0, cx1, cy0, cy1;
238 int i;
239
240 row = (y0 + y1) / 2;
241 upper = row - (row - y0) / 2;
242 lower = row + (y1 - row) / 2;
243 above = upper - (gli_leading) / 2;
244 below = lower + (gli_leading) / 2;
245
246 cx0 = gli_mask.select.x0 < gli_mask.select.x1
247 ? gli_mask.select.x0
248 : gli_mask.select.x1;
249
250 cx1 = gli_mask.select.x0 < gli_mask.select.x1
251 ? gli_mask.select.x1
252 : gli_mask.select.x0;
253
254 cy0 = gli_mask.select.y0 < gli_mask.select.y1
255 ? gli_mask.select.y0
256 : gli_mask.select.y1;
257
258 cy1 = gli_mask.select.y0 < gli_mask.select.y1
259 ? gli_mask.select.y1
260 : gli_mask.select.y0;
261
262 row_selected = false;
263
264 if ((cy0 >= upper && cy0 <= lower)
265 || (cy1 >= upper && cy1 <= lower)) {
266 row_selected = true;
267 }
268
269 if (row >= cy0 && row <= cy1) {
270 row_selected = true;
271 }
272
273 if (!row_selected) {
274 return false;
275 }
276
277 from_right = (gli_mask.select.x0 != cx0);
278 from_below = (gli_mask.select.y0 != cy0);
279 is_above = (above >= cy0 && above <= cy1);
280 is_below = (below >= cy0 && below <= cy1);
281
282 *rx0 = 0;
283 *rx1 = 0;
284
285 found_left = false;
286 found_right = false;
287
288 if (is_above && is_below) {
289 *rx0 = x0;
290 *rx1 = x1;
291 found_left = true;
292 found_right = true;
293 } else if (!is_above && is_below) {
294 if (from_below) {
295 if (from_right) {
296 *rx0 = cx0;
297 *rx1 = x1;
298 found_left = true;
299 found_right = true;
300 } else {
301 *rx0 = cx1;
302 *rx1 = x1;
303 found_left = true;
304 found_right = true;
305 }
306 } else {
307 if (from_right) {
308 *rx0 = cx1;
309 *rx1 = x1;
310 found_left = true;
311 found_right = true;
312 } else {
313 *rx1 = x1;
314 found_right = true;
315 }
316 }
317 } else if (is_above && !is_below) {
318 if (from_below) {
319 if (from_right) {
320 *rx0 = x0;
321 *rx1 = cx1;
322 found_left = true;
323 found_right = true;
324 } else {
325 *rx0 = x0;
326 *rx1 = cx0;
327 found_left = true;
328 found_right = true;
329 }
330 } else {
331 if (from_right) {
332 if (x0 > cx0) {
333 return false;
334 }
335 *rx0 = x0;
336 *rx1 = cx0;
337 found_left = true;
338 found_right = true;
339 } else {
340 *rx0 = x0;
341 found_left = true;
342 }
343 }
344 }
345
346 if (found_left && found_right) {
347 return true;
348 }
349
350 for (i = x0; i <= x1; i++) {
351 if (i >= cx0 && i <= cx1) {
352 if (!found_left) {
353 *rx0 = i;
354 found_left = true;
355 if (found_right) {
356 return true;
357 }
358 } else {
359 if (!found_right) {
360 *rx1 = i;
361 }
362 }
363 }
364 }
365
366 // XXX
367 // This is the equivalent to how this code was initially checked in,
368 // but it can't be correct. If rx1 is null, this will dereference a
369 // null pointer; but this has _never_ been called with a null
370 // pointer, so the first part is effectively dead code. On top of
371 // it, this will always return true, since rx0 and rx1 are always
372 // non-null pointers: there is one caller to this function and it
373 // always provides values for the pointers. For now just return
374 // true, since that's what the original code effectively did, but
375 // leave this as a marker to come back and try to figure out what
376 // the original intent was.
377 #if 0
378 if (rx0 != nullptr && rx1 == nullptr) {
379 *rx1 = x1;
380 }
381
382 return rx0 != nullptr && rx1 != nullptr;
383 #endif
384
385 return true;
386 }
387
388 void gli_clipboard_copy(const std::vector<glui32> &buf)
389 {
390 winclipstore(buf);
391 }