Debian Codegraph @

garglk/wingrid.cpp

raw symbols SCIP
1 // Copyright (C) 2006-2009 by Tor Andersson, Jesse McGrew.
2 // Copyright (C) 2010 by Ben Cressey, Chris Spiegel, Jörg Walter.
3 //
4 // This file is part of Gargoyle.
5 //
6 // Gargoyle is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // Gargoyle is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Gargoyle; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
20 #include <algorithm>
21
22 #include "glk.h"
23 #include "garglk.h"
24
25 // A grid of characters. We store the window as a list of lines.
26 // Within a line, just store an array of characters and an array
27 // of style bytes, the same size.
28
29 static void touch(window_textgrid_t *dwin, int line)
30 {
31 window_t *win = dwin->owner;
32 int y = win->bbox.y0 + line * gli_leading;
33 dwin->lines[line].dirty = true;
34 winrepaint(win->bbox.x0, y, win->bbox.x1, y + gli_leading);
35 }
36
37 void win_textgrid_rearrange(window_t *win, rect_t *box)
38 {
39 int newwid, newhgt;
40 int k;
41 window_textgrid_t *dwin = win->wingrid();
42 dwin->owner->bbox = *box;
43
44 newwid = (box->x1 - box->x0) / gli_cellw;
45 newhgt = (box->y1 - box->y0) / gli_cellh;
46
47 if (newwid == dwin->width && newhgt == dwin->height) {
48 return;
49 }
50
51 for (k = dwin->height; k < newhgt; k++) {
52 dwin->lines[k].chars.fill(' ');
53 dwin->lines[k].attrs.fill(attr_t{});
54 }
55
56 dwin->owner->attr.clear();
57 dwin->width = newwid;
58 dwin->height = newhgt;
59
60 for (k = 0; k < dwin->height; k++) {
61 touch(dwin, k);
62 std::fill(dwin->lines[k].chars.begin() + dwin->width, dwin->lines[k].chars.end(), ' ');
63 auto *attr_end = (&dwin->lines[k].attrs[0]) + dwin->lines[k].attrs.size();
64 for (auto *attr = &dwin->lines[k].attrs[dwin->width]; attr < attr_end; ++attr) {
65 attr->clear();
66 }
67 }
68 }
69
70 void win_textgrid_redraw(window_t *win) {
71 window_textgrid_t *dwin = win->wingrid();
72 int x0 = win->bbox.x0;
73 int y0 = win->bbox.y0;
74
75 // Draw a run of identically-styled cells over the interval [start,end) at (x,y).
76 // Return the width of the drawn area.
77 auto draw_run = [&dwin](const tgline_t *ln, int start, int end, int x, int y) -> int {
78 glui32 link = ln->attrs[start].hyper;
79 auto font = ln->attrs[start].font(dwin->styles);
80 Color fgcolor = link != 0 ? gli_link_color
81 : ln->attrs[start].fg(dwin->styles);
82 Color bgcolor = ln->attrs[start].bg(dwin->styles);
83 int w = (end - start) * gli_cellw;
84
85 gli_draw_rect(x, y, w, gli_leading, bgcolor);
86
87 if (link != 0) {
88 if (gli_underline_hyperlinks) {
89 gli_draw_rect(x, y + gli_baseline + 1, w, 1, gli_link_color);
90 }
91 gli_put_hyperlink(link, x, y, x + w, y + gli_leading);
92 }
93
94 for (int i = start; i < end; i++) {
95 gli_draw_string_uni(x * GLI_SUBPIX, y + gli_baseline, font, fgcolor, &ln->chars[i], 1, -1);
96 x += gli_cellw;
97 }
98
99 return w;
100 };
101
102 for (int i = 0; i < dwin->height; i++) {
103 tgline_t *ln = &dwin->lines[i];
104 if (!ln->dirty && !gli_force_redraw) {
105 continue;
106 }
107 ln->dirty = false;
108
109 int x = x0;
110 int y = y0 + i * gli_leading;
111
112 // Clear any stored hyperlink coordinates.
113 gli_put_hyperlink(0, x0, y, x0 + gli_cellw * dwin->width, y + gli_leading);
114
115 // Draw runs of contiguous cells that share the same styling.
116 for (int start = 0, end = 1; end <= dwin->width; end++) {
117 if (end == dwin->width || ln->attrs[end] != ln->attrs[start]) {
118 x += draw_run(ln, start, end, x, y);
119 start = end;
120 }
121 }
122 }
123 }
124
125 void win_textgrid_putchar_uni(window_t *win, glui32 ch)
126 {
127 window_textgrid_t *dwin = win->wingrid();
128 tgline_t *ln;
129
130 // Canonicalize the cursor position. That is, the cursor may have been
131 // left outside the window area; wrap it if necessary.
132 if (dwin->curx < 0) {
133 dwin->curx = 0;
134 } else if (dwin->curx >= dwin->width) {
135 dwin->curx = 0;
136 dwin->cury++;
137 }
138 if (dwin->cury < 0) {
139 dwin->cury = 0;
140 } else if (dwin->cury >= dwin->height) {
141 return; // outside the window
142 }
143
144 if (ch == '\n') {
145 // a newline just moves the cursor.
146 dwin->cury++;
147 dwin->curx = 0;
148 return;
149 }
150
151 touch(dwin, dwin->cury);
152
153 ln = &(dwin->lines[dwin->cury]);
154 ln->chars[dwin->curx] = ch;
155 ln->attrs[dwin->curx] = win->attr;
156
157 dwin->curx++;
158 // We can leave the cursor outside the window, since it will be
159 // canonicalized next time a character is printed.
160 }
161
162 bool win_textgrid_unputchar_uni(window_t *win, glui32 ch)
163 {
164 window_textgrid_t *dwin = win->wingrid();
165 tgline_t *ln;
166 int oldx = dwin->curx, oldy = dwin->cury;
167
168 // Move the cursor back.
169 if (dwin->curx >= dwin->width) {
170 dwin->curx = dwin->width - 1;
171 } else {
172 dwin->curx--;
173 }
174
175 // Canonicalize the cursor position. That is, the cursor may have been
176 // left outside the window area; wrap it if necessary.
177 if (dwin->curx < 0) {
178 dwin->curx = dwin->width - 1;
179 dwin->cury--;
180 }
181 if (dwin->cury < 0) {
182 dwin->cury = 0;
183 } else if (dwin->cury >= dwin->height) {
184 return false; // outside the window
185 }
186
187 if (ch == '\n') {
188 // a newline just moves the cursor.
189 if (dwin->curx == dwin->width - 1) {
190 return true; // deleted a newline
191 }
192 dwin->curx = oldx;
193 dwin->cury = oldy;
194 return false; // it wasn't there
195 }
196
197 ln = &(dwin->lines[dwin->cury]);
198 if (glk_char_to_upper(ln->chars[dwin->curx]) == glk_char_to_upper(ch)) {
199 ln->chars[dwin->curx] = ' ';
200 ln->attrs[dwin->curx].clear();
201 touch(dwin, dwin->cury);
202 return true; // deleted the char
203 } else {
204 dwin->curx = oldx;
205 dwin->cury = oldy;
206 return false; // it wasn't there
207 }
208 }
209
210 void win_textgrid_clear(window_t *win)
211 {
212 window_textgrid_t *dwin = win->wingrid();
213 int k;
214
215 win->attr.fgcolor = gli_override_fg;
216 win->attr.bgcolor = gli_override_bg;
217 win->attr.reverse = false;
218
219 for (k = 0; k < dwin->height; k++) {
220 touch(dwin, k);
221 dwin->lines[k].chars.fill(' ');
222 dwin->lines[k].attrs.fill(attr_t{});
223 }
224
225 dwin->curx = 0;
226 dwin->cury = 0;
227 }
228
229 void win_textgrid_move_cursor(window_t *win, int xpos, int ypos)
230 {
231 window_textgrid_t *dwin = win->wingrid();
232
233 // If the values are negative, they're really huge positive numbers --
234 // remember that they were cast from glui32. So set them huge and
235 // let canonicalization take its course.
236 if (xpos < 0) {
237 xpos = 32767;
238 }
239 if (ypos < 0) {
240 ypos = 32767;
241 }
242
243 dwin->curx = xpos;
244 dwin->cury = ypos;
245 }
246
247 void win_textgrid_click(window_textgrid_t *dwin, int sx, int sy)
248 {
249 window_t *win = dwin->owner;
250 int x = sx - win->bbox.x0;
251 int y = sy - win->bbox.y0;
252
253 if (win->line_request || win->char_request
254 || win->line_request_uni || win->char_request_uni
255 || win->more_request || win->scroll_request) {
256 gli_focuswin = win;
257 }
258
259 if (win->mouse_request) {
260 gli_event_store(evtype_MouseInput, win, x / gli_cellw, y / gli_leading);
261 win->mouse_request = false;
262 if (gli_conf_safeclicks) {
263 gli_forceclick = true;
264 }
265 }
266
267 if (win->hyper_request) {
268 glui32 linkval = gli_get_hyperlink(sx, sy);
269 if (linkval != 0) {
270 gli_event_store(evtype_Hyperlink, win, linkval, 0);
271 win->hyper_request = false;
272 if (gli_conf_safeclicks) {
273 gli_forceclick = true;
274 }
275 }
276 }
277 }
278
279 // Prepare the window for line input.
280 static void win_textgrid_init_impl(window_t *win, void *buf, int maxlen, int initlen, bool unicode)
281 {
282 window_textgrid_t *dwin = win->wingrid();
283
284 dwin->inunicode = unicode;
285 dwin->inoriglen = maxlen;
286 if (maxlen > (dwin->width - dwin->curx)) {
287 maxlen = (dwin->width - dwin->curx);
288 }
289
290 dwin->inbuf = buf;
291 dwin->inmax = maxlen;
292 dwin->inlen = 0;
293 dwin->incurs = 0;
294 dwin->inorgx = dwin->curx;
295 dwin->inorgy = dwin->cury;
296 dwin->origattr = win->attr;
297 win->attr.set(style_Input);
298
299 if (initlen > maxlen) {
300 initlen = maxlen;
301 }
302
303 if (initlen != 0) {
304 int ix;
305 tgline_t *ln = &(dwin->lines[dwin->inorgy]);
306
307 for (ix = 0; ix < initlen; ix++) {
308 ln->attrs[dwin->inorgx + ix].set(style_Input);
309 if (unicode) {
310 ln->chars[dwin->inorgx + ix] = (static_cast<glui32 *>(buf))[ix];
311 } else {
312 ln->chars[dwin->inorgx + ix] = (static_cast<unsigned char *>(buf))[ix];
313 }
314 }
315
316 dwin->incurs += initlen;
317 dwin->inlen += initlen;
318 dwin->curx = dwin->inorgx + dwin->incurs;
319 dwin->cury = dwin->inorgy;
320
321 touch(dwin, dwin->inorgy);
322 }
323
324 if (gli_register_arr != nullptr) {
325 dwin->inarrayrock = (*gli_register_arr)(dwin->inbuf, dwin->inoriglen, const_cast<char *>(unicode ? "&+#!Iu" : "&+#!Cn"));
326 }
327 }
328
329 void win_textgrid_init_line(window_t *win, char *buf, int maxlen, int initlen)
330 {
331 win_textgrid_init_impl(win, buf, maxlen, initlen, false);
332 }
333
334 void win_textgrid_init_line_uni(window_t *win, glui32 *buf, int maxlen, int initlen)
335 {
336 win_textgrid_init_impl(win, buf, maxlen, initlen, true);
337 }
338
339 static void accept_or_cancel(window_t *win, event_t *ev, bool echo)
340 {
341 int ix;
342 void *inbuf;
343 int inoriglen;
344 bool inunicode;
345 gidispatch_rock_t inarrayrock;
346 window_textgrid_t *dwin = win->wingrid();
347 tgline_t *ln = &(dwin->lines[dwin->inorgy]);
348
349 if (dwin->inbuf == nullptr) {
350 return;
351 }
352
353 inbuf = dwin->inbuf;
354 inoriglen = dwin->inoriglen;
355 inarrayrock = dwin->inarrayrock;
356 inunicode = dwin->inunicode;
357
358 if (!inunicode) {
359 for (ix = 0; ix < dwin->inlen; ix++) {
360 glui32 ch = ln->chars[dwin->inorgx + ix];
361 if (ch > 0xff) {
362 ch = '?';
363 }
364 (static_cast<char *>(inbuf))[ix] = static_cast<char>(ch);
365 }
366 if (echo && win->echostr != nullptr) {
367 gli_stream_echo_line(win->echostr, static_cast<char *>(inbuf), dwin->inlen);
368 }
369 } else {
370 for (ix = 0; ix < dwin->inlen; ix++) {
371 (static_cast<glui32 *>(inbuf))[ix] = ln->chars[dwin->inorgx + ix];
372 }
373 if (echo && win->echostr != nullptr) {
374 gli_stream_echo_line_uni(win->echostr, static_cast<glui32 *>(inbuf), dwin->inlen);
375 }
376 }
377
378 dwin->cury = dwin->inorgy + 1;
379 dwin->curx = 0;
380 win->attr = dwin->origattr;
381
382 ev->type = evtype_LineInput;
383 ev->win = win;
384 ev->val1 = dwin->inlen;
385 ev->val2 = 0;
386
387 win->line_request = false;
388 win->line_request_uni = false;
389 dwin->inbuf = nullptr;
390 dwin->inoriglen = 0;
391 dwin->inmax = 0;
392 dwin->inorgx = 0;
393 dwin->inorgy = 0;
394
395 if (gli_unregister_arr != nullptr) {
396 const char *typedesc = (inunicode ? "&+#!Iu" : "&+#!Cn");
397 (*gli_unregister_arr)(inbuf, inoriglen, const_cast<char *>(typedesc), inarrayrock);
398 }
399 }
400
401 // Abort line input, storing whatever's been typed so far.
402 void win_textgrid_cancel_line(window_t *win, event_t *ev)
403 {
404 accept_or_cancel(win, ev, false);
405 }
406
407 // Keybinding functions.
408
409 // Any key, during character input. Ends character input.
410 void gcmd_grid_accept_readchar(window_t *win, glui32 arg)
411 {
412 glui32 key;
413
414 switch (arg) {
415 case keycode_Erase:
416 key = keycode_Delete;
417 break;
418 case keycode_MouseWheelUp:
419 case keycode_MouseWheelDown:
420 return;
421 default:
422 key = arg;
423 }
424
425 if (key > 0xff && key < (0xffffffff - keycode_MAXVAL + 1)) {
426 if (!(win->char_request_uni) || key > 0x10ffff) {
427 key = keycode_Unknown;
428 }
429 }
430
431 win->char_request = false;
432 win->char_request_uni = false;
433 gli_event_store(evtype_CharInput, win, key, 0);
434 }
435
436 // Return or enter, during line input. Ends line input.
437 static void acceptline(window_t *win, glui32 keycode)
438 {
439 event_t ev;
440
441 accept_or_cancel(win, &ev, true);
442
443 if (!win->line_terminators.empty() && keycode != keycode_Return) {
444 ev.val2 = keycode;
445 }
446
447 gli_event_store(ev.type, ev.win, ev.val1, ev.val2);
448 }
449
450 // Any regular key, during line input.
451 void gcmd_grid_accept_readline(window_t *win, glui32 arg)
452 {
453 int ix;
454 window_textgrid_t *dwin = win->wingrid();
455 tgline_t *ln = &(dwin->lines[dwin->inorgy]);
456
457 if (dwin->inbuf == nullptr) {
458 return;
459 }
460
461 if (!win->line_terminators.empty() && gli_window_check_terminator(arg)) {
462 if (std::find(win->line_terminators.begin(), win->line_terminators.end(), arg) != win->line_terminators.end()) {
463 acceptline(win, arg);
464 return;
465 }
466 }
467
468 switch (arg) {
469
470 // Delete keys, during line input.
471
472 case keycode_Delete:
473 if (dwin->inlen <= 0) {
474 return;
475 }
476 if (dwin->incurs <= 0) {
477 return;
478 }
479 for (ix = dwin->incurs; ix < dwin->inlen; ix++) {
480 ln->chars[dwin->inorgx + ix - 1] = ln->chars[dwin->inorgx + ix];
481 }
482 ln->chars[dwin->inorgx + dwin->inlen - 1] = ' ';
483 dwin->incurs--;
484 dwin->inlen--;
485 break;
486
487 case keycode_Erase:
488 if (dwin->inlen <= 0) {
489 return;
490 }
491 if (dwin->incurs >= dwin->inlen) {
492 return;
493 }
494 for (ix = dwin->incurs; ix < dwin->inlen - 1; ix++) {
495 ln->chars[dwin->inorgx + ix] = ln->chars[dwin->inorgx + ix + 1];
496 }
497 ln->chars[dwin->inorgx + dwin->inlen - 1] = ' ';
498 dwin->inlen--;
499 break;
500
501 case keycode_Escape:
502 if (dwin->inlen <= 0) {
503 return;
504 }
505 for (ix = 0; ix < dwin->inlen; ix++) {
506 ln->chars[dwin->inorgx + ix] = ' ';
507 }
508 dwin->inlen = 0;
509 dwin->incurs = 0;
510 break;
511
512 // Cursor movement keys, during line input.
513
514 case keycode_Left:
515 if (dwin->incurs <= 0) {
516 return;
517 }
518 dwin->incurs--;
519 break;
520
521 case keycode_Right:
522 if (dwin->incurs >= dwin->inlen) {
523 return;
524 }
525 dwin->incurs++;
526 break;
527
528 case keycode_Home:
529 if (dwin->incurs <= 0) {
530 return;
531 }
532 dwin->incurs = 0;
533 break;
534
535 case keycode_End:
536 if (dwin->incurs >= dwin->inlen) {
537 return;
538 }
539 dwin->incurs = dwin->inlen;
540 break;
541
542 case keycode_Return:
543 acceptline(win, arg);
544 return;
545
546 default:
547 if (dwin->inlen >= dwin->inmax) {
548 return;
549 }
550
551 if (arg < 32 || arg > 0x10ffff) {
552 return;
553 }
554
555 if (gli_conf_caps && (arg > 0x60 && arg < 0x7b)) {
556 arg -= 0x20;
557 }
558
559 for (ix = dwin->inlen; ix > dwin->incurs; ix--) {
560 ln->chars[dwin->inorgx + ix] = ln->chars[dwin->inorgx + ix - 1];
561 }
562 ln->attrs[dwin->inorgx + dwin->inlen].set(style_Input);
563 ln->chars[dwin->inorgx + dwin->incurs] = arg;
564
565 dwin->incurs++;
566 dwin->inlen++;
567 }
568
569 dwin->curx = dwin->inorgx + dwin->incurs;
570 dwin->cury = dwin->inorgy;
571
572 touch(dwin, dwin->inorgy);
573 }