Debian Codegraph @

garglk/window.cpp

raw symbols SCIP
1 // Copyright (C) 2006-2009 by Tor Andersson, Jesse McGrew.
2 // Copyright (C) 2010 by Ben Cressey, Chris Spiegel.
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 #include <cmath>
22 #include <new>
23
24 #include "optional.hpp"
25
26 #include "glk.h"
27 #include "garglk.h"
28
29 // limit number of text rows/columns
30 #define MAX_TEXT_COLUMNS 255
31 #define MAX_TEXT_ROWS 255
32
33 bool gli_force_redraw = true;
34 bool gli_more_focus = false;
35
36 // Linked list of all windows
37 static window_t *gli_windowlist = nullptr;
38
39 window_t *gli_rootwin = nullptr; // The topmost window.
40 window_t *gli_focuswin = nullptr; // The window selected by the player
41
42 // record whether we've returned a click event
43 bool gli_forceclick = false;
44
45 // Set up the window system. This is called from main().
46 void gli_initialize_windows()
47 {
48 gli_rootwin = nullptr;
49 gli_focuswin = nullptr;
50 }
51
52 // Return the scrollback buffer of the focused window, if that window is
53 // a text buffer. If it's not, find the textbuffer with the longest
54 // scrollback and assume that's the best scrollback buffer, returning
55 // it. If there are no textbuffer windows at all, return nullopt.
56 nonstd::optional<std::vector<char>> gli_get_scrollback() {
57 nonstd::optional<std::vector<char>> text;
58
59 if (gli_focuswin != nullptr && gli_focuswin->type == wintype_TextBuffer) {
60 return gli_get_text(gli_focuswin->winbuffer());
61 }
62
63 for (auto *win = glk_window_iterate(nullptr, nullptr); win != nullptr; win = glk_window_iterate(win, nullptr)) {
64 if (win->type == wintype_TextBuffer) {
65 auto new_text = gli_get_text(win->winbuffer());
66 if (!text.has_value() || new_text.size() > text->size()) {
67 text = new_text;
68 }
69 }
70 }
71
72 return text;
73 }
74
75 static void gli_windows_rearrange()
76 {
77 if (gli_rootwin != nullptr) {
78 rect_t box;
79
80 if (gli_conf_lockcols && gli_cols <= MAX_TEXT_COLUMNS) {
81 // Lock the number of columns
82 int desired_width = gli_wmarginx_save * 2 + gli_cellw * gli_cols;
83 if (desired_width > gli_image_rgb.width()) {
84 gli_wmarginx = gli_wmarginx_save;
85 } else {
86 gli_wmarginx = (gli_image_rgb.width() - gli_cellw * gli_cols) / 2;
87 }
88 } else {
89 // Limit the maximum number of columns
90 int max_width = gli_wmarginx_save * 2 + gli_cellw * MAX_TEXT_COLUMNS;
91 if (max_width < gli_image_rgb.width()) {
92 gli_wmarginx = (gli_image_rgb.width() - gli_cellw * MAX_TEXT_COLUMNS) / 2;
93 } else {
94 gli_wmarginx = gli_wmarginx_save;
95 }
96 }
97
98 if (gli_conf_lockrows && gli_rows <= MAX_TEXT_ROWS) {
99 // Lock the number of rows
100 int desired_height = gli_wmarginy_save * 2 + gli_cellh * gli_rows;
101 if (desired_height > gli_image_rgb.height()) {
102 gli_wmarginy = gli_wmarginy_save;
103 } else {
104 gli_wmarginy = (gli_image_rgb.height() - gli_cellh * gli_rows) / 2;
105 }
106 } else {
107 // Limit the maximum number of rows
108 int max_height = gli_wmarginy_save * 2 + gli_cellh * MAX_TEXT_ROWS;
109 if (max_height < gli_image_rgb.height()) {
110 gli_wmarginy = (gli_image_rgb.height() - gli_cellh * MAX_TEXT_ROWS) / 2;
111 } else {
112 gli_wmarginy = gli_wmarginy_save;
113 }
114 }
115
116 box.x0 = gli_wmarginx;
117 box.y0 = gli_wmarginy;
118 box.x1 = gli_image_rgb.width() - gli_wmarginx;
119 box.y1 = gli_image_rgb.height() - gli_wmarginy;
120 gli_window_rearrange(gli_rootwin, &box);
121 }
122 }
123
124 //
125 // Create, destroy and arrange
126 //
127
128 glk_window_struct::glk_window_struct(glui32 type_, glui32 rock_) :
129 type(type_),
130 rock(rock_),
131 str(gli_stream_open_window(this)),
132 next(gli_windowlist),
133 prev(nullptr)
134 {
135 gli_windowlist = this;
136 if (next != nullptr) {
137 next->prev = this;
138 }
139
140 if (gli_register_obj != nullptr) {
141 disprock = gli_register_obj(this, gidisp_Class_Window);
142 }
143 }
144
145 glk_window_struct::~glk_window_struct()
146 {
147 if (gli_unregister_obj != nullptr) {
148 gli_unregister_obj(this, gidisp_Class_Window, disprock);
149 }
150
151 if (str != nullptr) {
152 gli_delete_stream(str);
153 }
154
155 if (prev != nullptr) {
156 prev->next = next;
157 } else {
158 gli_windowlist = next;
159 }
160
161 if (next != nullptr) {
162 next->prev = prev;
163 }
164 }
165
166 winid_t glk_window_open(winid_t splitwin,
167 glui32 method, glui32 size,
168 glui32 wintype, glui32 rock)
169 {
170 window_t *oldparent;
171 glui32 val;
172
173 if (wintype == wintype_Graphics && !gli_conf_graphics) {
174 return nullptr;
175 }
176
177 gli_force_redraw = true;
178
179 if (gli_rootwin == nullptr) {
180 if (splitwin != nullptr) {
181 gli_strict_warning("window_open: ref must be NULL");
182 return nullptr;
183 }
184
185 // ignore method and size now
186 oldparent = nullptr;
187 }
188
189 else {
190 if (splitwin == nullptr) {
191 gli_strict_warning("window_open: ref must not be NULL");
192 return nullptr;
193 }
194
195 val = (method & winmethod_DivisionMask);
196 if (val != winmethod_Fixed && val != winmethod_Proportional) {
197 gli_strict_warning("window_open: invalid method (not fixed or proportional)");
198 return nullptr;
199 }
200
201 val = (method & winmethod_DirMask);
202 if (val != winmethod_Above && val != winmethod_Below
203 && val != winmethod_Left && val != winmethod_Right) {
204 gli_strict_warning("window_open: invalid method (bad direction)");
205 return nullptr;
206 }
207
208 oldparent = splitwin->parent;
209 if (oldparent != nullptr && oldparent->type != wintype_Pair) {
210 gli_strict_warning("window_open: parent window is not Pair");
211 return nullptr;
212 }
213 }
214
215 // std::unique_ptr is used a couple places in this block just to guard
216 // against leaks. Those instances of it will ultimately release ownership.
217 // This is to mesh with the C ancestry of the code, while still using RAII
218 // to avoid leaks in error cases.
219 try {
220 auto newwin = std::make_unique<window_t>(wintype, rock);
221
222 switch (wintype) {
223 case wintype_Blank:
224 newwin->window = std::make_unique<window_blank_t>(newwin.get());
225 break;
226 case wintype_TextGrid:
227 newwin->window = std::make_unique<window_textgrid_t>(newwin.get());
228 break;
229 case wintype_TextBuffer:
230 newwin->window = std::make_unique<window_textbuffer_t>(newwin.get());
231 break;
232 case wintype_Graphics:
233 newwin->window = std::make_unique<window_graphics_t>(newwin.get());
234 break;
235 case wintype_Pair:
236 gli_strict_warning("window_open: cannot open pair window directly");
237 return nullptr;
238 default:
239 // Unknown window type -- do not print a warning, just return 0
240 // to indicate that it's not possible.
241 return nullptr;
242 }
243
244 if (splitwin == nullptr) {
245 gli_rootwin = newwin.get();
246 } else {
247 // create pairwin, with newwin as the key
248 auto pairwin = std::make_unique<window_t>(wintype_Pair, 0);
249 pairwin->window = std::make_unique<window_pair_t>(pairwin.get(), method, newwin.get(), size);
250
251 pairwin->winpair()->child1 = splitwin;
252 pairwin->winpair()->child2 = newwin.get();
253
254 splitwin->parent = pairwin.get();
255 newwin->parent = pairwin.get();
256 pairwin->parent = oldparent;
257
258 if (oldparent != nullptr) {
259 window_pair_t *dparentwin = oldparent->winpair();
260 if (dparentwin->child1 == splitwin) {
261 dparentwin->child1 = pairwin.get();
262 } else {
263 dparentwin->child2 = pairwin.get();
264 }
265 } else {
266 gli_rootwin = pairwin.get();
267 }
268
269 (void)pairwin.release();
270 }
271
272 gli_windows_rearrange();
273
274 return newwin.release();
275 } catch (const std::bad_alloc &) {
276 gli_strict_warning("window_open: unable to create window (out of memory)");
277 return nullptr;
278 }
279 }
280
281 static void gli_window_close(window_t *win, bool recurse)
282 {
283 window_t *wx;
284
285 if (gli_focuswin == win) {
286 gli_focuswin = nullptr;
287 }
288
289 for (wx = win->parent; wx != nullptr; wx = wx->parent) {
290 if (wx->type == wintype_Pair) {
291 window_pair_t *dwx = wx->winpair();
292 if (dwx->key == win) {
293 dwx->key = nullptr;
294 dwx->keydamage = true;
295 }
296 }
297 }
298
299 if (win->image_loaded) {
300 gli_piclist_decrement();
301 }
302
303 if (win->type == wintype_Pair && recurse) {
304 window_pair_t *dwin = win->winpair();
305 if (dwin->child1 != nullptr) {
306 gli_window_close(dwin->child1, true);
307 }
308 if (dwin->child2 != nullptr) {
309 gli_window_close(dwin->child2, true);
310 }
311 }
312
313 delete win;
314 }
315
316 void glk_window_close(window_t *win, stream_result_t *result)
317 {
318 gli_force_redraw = true;
319
320 if (win == nullptr) {
321 gli_strict_warning("window_close: invalid ref");
322 return;
323 }
324
325 if (win == gli_rootwin || win->parent == nullptr) {
326 // close the root window, which means all windows.
327
328 gli_rootwin = nullptr;
329
330 // begin (simpler) closation
331
332 gli_stream_fill_result(win->str, result);
333 gli_window_close(win, true);
334 }
335
336 else {
337 // have to jigger parent
338 window_t *pairwin, *sibwin, *grandparwin;
339 window_pair_t *dpairwin, *dgrandparwin;
340
341 pairwin = win->parent;
342 dpairwin = pairwin->winpair();
343 if (win == dpairwin->child1) {
344 sibwin = dpairwin->child2;
345 } else if (win == dpairwin->child2) {
346 sibwin = dpairwin->child1;
347 } else {
348 gli_strict_warning("window_close: window tree is corrupted");
349 return;
350 }
351
352 grandparwin = pairwin->parent;
353 if (grandparwin == nullptr) {
354 gli_rootwin = sibwin;
355 sibwin->parent = nullptr;
356 } else {
357 dgrandparwin = grandparwin->winpair();
358 if (dgrandparwin->child1 == pairwin) {
359 dgrandparwin->child1 = sibwin;
360 } else {
361 dgrandparwin->child2 = sibwin;
362 }
363 sibwin->parent = grandparwin;
364 }
365
366 // Begin closation
367
368 gli_stream_fill_result(win->str, result);
369
370 // Close the child window (and descendants), so that key-deletion can
371 // crawl up the tree to the root window.
372 gli_window_close(win, true);
373
374 // This probably isn't necessary, but the child *is* gone, so just
375 // in case.
376 if (win == dpairwin->child1) {
377 dpairwin->child1 = nullptr;
378 } else if (win == dpairwin->child2) {
379 dpairwin->child2 = nullptr;
380 }
381
382 // Now we can delete the parent pair.
383 gli_window_close(pairwin, false);
384
385 // Sort out the arrangements
386 gli_windows_rearrange();
387 }
388 }
389
390 void glk_window_get_arrangement(window_t *win, glui32 *method, glui32 *size,
391 winid_t *keywin)
392 {
393 window_pair_t *dwin;
394 glui32 val;
395
396 if (win == nullptr) {
397 gli_strict_warning("window_get_arrangement: invalid ref");
398 return;
399 }
400
401 if (win->type != wintype_Pair) {
402 gli_strict_warning("window_get_arrangement: not a Pair window");
403 return;
404 }
405
406 dwin = win->winpair();
407
408 val = dwin->dir | dwin->division;
409 if (!dwin->wborder) {
410 val |= winmethod_NoBorder;
411 }
412
413 if (size != nullptr) {
414 *size = dwin->size;
415 if (dwin->key != nullptr && (dwin->key->type == wintype_Graphics) && (dwin->dir == winmethod_Fixed)) {
416 *size = gli_unzoom_int(*size);
417 }
418 }
419 if (keywin != nullptr) {
420 if (dwin->key != nullptr) {
421 *keywin = dwin->key;
422 } else {
423 *keywin = nullptr;
424 }
425 }
426 if (method != nullptr) {
427 *method = val;
428 }
429 }
430
431 void glk_window_set_arrangement(window_t *win, glui32 method, glui32 size, winid_t key)
432 {
433 window_pair_t *dwin;
434 glui32 newdir;
435 bool newvertical, newbackward;
436
437 gli_force_redraw = true;
438
439 if (win == nullptr) {
440 gli_strict_warning("window_set_arrangement: invalid ref");
441 return;
442 }
443
444 if (win->type != wintype_Pair) {
445 gli_strict_warning("window_set_arrangement: not a Pair window");
446 return;
447 }
448
449 if (key != nullptr) {
450 window_t *wx;
451 if (key->type == wintype_Pair) {
452 gli_strict_warning("window_set_arrangement: keywin cannot be a Pair");
453 return;
454 }
455 for (wx = key; wx != nullptr; wx = wx->parent) {
456 if (wx == win) {
457 break;
458 }
459 }
460 if (wx == nullptr) {
461 gli_strict_warning("window_set_arrangement: keywin must be a descendant");
462 return;
463 }
464 }
465
466 dwin = win->winpair();
467
468 newdir = method & winmethod_DirMask;
469 newvertical = (newdir == winmethod_Left || newdir == winmethod_Right);
470 newbackward = (newdir == winmethod_Left || newdir == winmethod_Above);
471 if (key == nullptr) {
472 key = dwin->key;
473 }
474
475 if ((newvertical && !dwin->vertical) || (!newvertical && dwin->vertical)) {
476 if (!dwin->vertical) {
477 gli_strict_warning("window_set_arrangement: split must stay horizontal");
478 } else {
479 gli_strict_warning("window_set_arrangement: split must stay vertical");
480 }
481 return;
482 }
483
484 if (key != nullptr && key->type == wintype_Blank
485 && (method & winmethod_DivisionMask) == winmethod_Fixed) {
486 gli_strict_warning("window_set_arrangement: a Blank window cannot have a fixed size");
487 return;
488 }
489
490 if ((newbackward && !dwin->backward) || (!newbackward && dwin->backward)) {
491 // switch the children
492 window_t *tmpwin = dwin->child1;
493 dwin->child1 = dwin->child2;
494 dwin->child2 = tmpwin;
495 }
496
497 // set up everything else
498 dwin->dir = newdir;
499 dwin->division = method & winmethod_DivisionMask;
500 dwin->key = key;
501 dwin->size = size;
502 if (key != nullptr && (key->type == wintype_Graphics) && (newdir == winmethod_Fixed)) {
503 dwin->size = gli_zoom_int(dwin->size);
504 }
505 dwin->wborder = (method & winmethod_BorderMask) == winmethod_Border;
506
507 dwin->vertical = (dwin->dir == winmethod_Left || dwin->dir == winmethod_Right);
508 dwin->backward = (dwin->dir == winmethod_Left || dwin->dir == winmethod_Above);
509
510 gli_windows_rearrange();
511 }
512
513 void glk_window_get_size(window_t *win, glui32 *width, glui32 *height)
514 {
515 glui32 wid = 0;
516 glui32 hgt = 0;
517
518 if (win == nullptr) {
519 gli_strict_warning("window_get_size: invalid ref");
520 return;
521 }
522
523 switch (win->type) {
524 case wintype_Blank:
525 case wintype_Pair:
526 // always zero
527 break;
528 case wintype_TextGrid:
529 wid = win->bbox.x1 - win->bbox.x0;
530 hgt = win->bbox.y1 - win->bbox.y0;
531 wid = wid / gli_cellw;
532 hgt = hgt / gli_cellh;
533 break;
534 case wintype_TextBuffer:
535 wid = win->bbox.x1 - win->bbox.x0;
536 wid -= std::min<glui32>(wid, gli_tmarginx * 2);
537 hgt = win->bbox.y1 - win->bbox.y0;
538 hgt -= std::min<glui32>(hgt, gli_tmarginy * 2);
539 wid = wid / gli_cellw;
540 hgt = hgt / gli_cellh;
541 break;
542 case wintype_Graphics:
543 wid = gli_unzoom_int(win->bbox.x1 - win->bbox.x0);
544 hgt = gli_unzoom_int(win->bbox.y1 - win->bbox.y0);
545 break;
546 }
547
548 if (width != nullptr) {
549 *width = wid;
550 }
551 if (height != nullptr) {
552 *height = hgt;
553 }
554 }
555
556 //
557 // Family matters
558 //
559
560 winid_t glk_window_iterate(winid_t win, glui32 *rock)
561 {
562 if (win == nullptr) {
563 win = gli_windowlist;
564 } else {
565 win = win->next;
566 }
567
568 if (win != nullptr) {
569 if (rock != nullptr) {
570 *rock = win->rock;
571 }
572 return win;
573 }
574
575 if (rock != nullptr) {
576 *rock = 0;
577 }
578 return nullptr;
579 }
580
581 window_t *gli_window_iterate_treeorder(window_t *win)
582 {
583 if (win == nullptr) {
584 return gli_rootwin;
585 }
586
587 if (win->type == wintype_Pair) {
588 window_pair_t *dwin = win->winpair();
589 if (!dwin->backward) {
590 return dwin->child1;
591 } else {
592 return dwin->child2;
593 }
594 } else {
595 window_t *parwin;
596 window_pair_t *dwin;
597
598 while (win->parent != nullptr) {
599 parwin = win->parent;
600 dwin = parwin->winpair();
601 if (!dwin->backward) {
602 if (win == dwin->child1) {
603 return dwin->child2;
604 }
605 } else {
606 if (win == dwin->child2) {
607 return dwin->child1;
608 }
609 }
610 win = parwin;
611 }
612
613 return nullptr;
614 }
615 }
616
617 glui32 glk_window_get_rock(window_t *win)
618 {
619 if (win == nullptr) {
620 gli_strict_warning("window_get_rock: invalid ref.");
621 return 0;
622 }
623
624 return win->rock;
625 }
626
627 winid_t glk_window_get_root()
628 {
629 if (gli_rootwin == nullptr) {
630 return nullptr;
631 }
632 return gli_rootwin;
633 }
634
635 winid_t glk_window_get_parent(window_t *win)
636 {
637 if (win == nullptr) {
638 gli_strict_warning("window_get_parent: invalid ref");
639 return nullptr;
640 }
641
642 return win->parent;
643 }
644
645 winid_t glk_window_get_sibling(window_t *win)
646 {
647 window_pair_t *dparwin;
648
649 if (win == nullptr) {
650 gli_strict_warning("window_get_sibling: invalid ref");
651 return nullptr;
652 }
653 if (win->parent == nullptr) {
654 return nullptr;
655 }
656
657 dparwin = win->parent->winpair();
658 if (dparwin->child1 == win) {
659 return dparwin->child2;
660 } else if (dparwin->child2 == win) {
661 return dparwin->child1;
662 }
663 return nullptr;
664 }
665
666 glui32 glk_window_get_type(window_t *win)
667 {
668 if (win == nullptr) {
669 gli_strict_warning("window_get_type: invalid ref");
670 return 0;
671 }
672 return win->type;
673 }
674
675 strid_t glk_window_get_stream(window_t *win)
676 {
677 if (win == nullptr) {
678 gli_strict_warning("window_get_stream: invalid ref");
679 return nullptr;
680 }
681
682 return win->str;
683 }
684
685 strid_t glk_window_get_echo_stream(window_t *win)
686 {
687 if (win == nullptr) {
688 gli_strict_warning("window_get_echo_stream: invalid ref");
689 return nullptr;
690 }
691
692 return win->echostr;
693 }
694
695 void glk_window_set_echo_stream(window_t *win, stream_t *str)
696 {
697 if (win == nullptr) {
698 gli_strict_warning("window_set_echo_stream: invalid window id");
699 return;
700 }
701
702 win->echostr = str;
703 }
704
705 void glk_set_window(window_t *win)
706 {
707 if (win == nullptr) {
708 gli_stream_set_current(nullptr);
709 } else {
710 gli_stream_set_current(win->str);
711 }
712 }
713
714 void gli_windows_unechostream(stream_t *str)
715 {
716 window_t *win;
717 for (win = gli_windowlist; win != nullptr; win = win->next) {
718 if (win->echostr == str) {
719 win->echostr = nullptr;
720 }
721 }
722 }
723
724 //
725 // Size changes, rearrangement and redrawing.
726 //
727
728 void gli_window_rearrange(window_t *win, rect_t *box)
729 {
730 switch (win->type) {
731 case wintype_Blank:
732 win_blank_rearrange(win, box);
733 break;
734 case wintype_Pair:
735 win_pair_rearrange(win, box);
736 break;
737 case wintype_TextGrid:
738 win_textgrid_rearrange(win, box);
739 break;
740 case wintype_TextBuffer:
741 win_textbuffer_rearrange(win, box);
742 break;
743 case wintype_Graphics:
744 win_graphics_rearrange(win, box);
745 break;
746 }
747 }
748
749 void gli_windows_size_change(int w, int h, bool post_arrange_event)
750 {
751 gli_image_rgb.resize(w, h, false);
752 gli_resize_mask(gli_image_rgb.width(), gli_image_rgb.height());
753
754 gli_force_redraw = true;
755 gli_windows_rearrange();
756 gli_windows_redraw();
757
758 if (post_arrange_event) {
759 gli_event_store(evtype_Arrange, nullptr, 0, 0);
760 }
761 }
762
763 void gli_window_redraw(window_t *win)
764 {
765 if (gli_force_redraw) {
766 Color color = gli_override_bg.has_value() ? gli_window_color : win->bgcolor;
767 int y0 = win->yadj != 0 ? win->bbox.y0 - win->yadj : win->bbox.y0;
768 gli_draw_rect(win->bbox.x0, y0,
769 win->bbox.x1 - win->bbox.x0,
770 win->bbox.y1 - y0,
771 color);
772 }
773
774 switch (win->type) {
775 case wintype_Blank:
776 win_blank_redraw(win);
777 break;
778 case wintype_Pair:
779 win_pair_redraw(win);
780 break;
781 case wintype_TextGrid:
782 win_textgrid_redraw(win);
783 break;
784 case wintype_TextBuffer:
785 win_textbuffer_redraw(win);
786 break;
787 case wintype_Graphics:
788 win_graphics_redraw(win);
789 break;
790 }
791 }
792
793 void gli_window_refocus(window_t *win)
794 {
795 window_t *focus = win;
796
797 do {
798 if (focus != nullptr && focus->more_request) {
799 gli_focuswin = focus;
800 return;
801 }
802
803 focus = gli_window_iterate_treeorder(focus);
804 } while (focus != win);
805
806 gli_more_focus = false;
807 }
808
809 void gli_windows_redraw()
810 {
811 gli_claimselect = false;
812
813 if (gli_force_redraw) {
814 winrepaint(0, 0, gli_image_rgb.width(), gli_image_rgb.height());
815 gli_draw_clear(gli_window_color);
816 }
817
818 if (gli_rootwin != nullptr) {
819 gli_window_redraw(gli_rootwin);
820 }
821
822 if (gli_more_focus) {
823 gli_window_refocus(gli_focuswin);
824 }
825
826 gli_force_redraw = false;
827 }
828
829 void gli_redraw_rect(int x0, int y0, int x1, int y1)
830 {
831 gli_drawselect = true;
832 winrepaint(x0, y0, x1, y1);
833 }
834
835 //
836 // Input events
837 //
838
839 void glk_request_char_event(window_t *win)
840 {
841 if (win == nullptr) {
842 gli_strict_warning("request_char_event: invalid ref");
843 return;
844 }
845
846 if (win->char_request || win->line_request || win->char_request_uni || win->line_request_uni) {
847 gli_strict_warning("request_char_event: window already has keyboard request");
848 return;
849 }
850
851 switch (win->type) {
852 case wintype_TextBuffer:
853 case wintype_TextGrid:
854 win->char_request = true;
855 break;
856 default:
857 gli_strict_warning("request_char_event: window does not support keyboard input");
858 break;
859 }
860 }
861
862 void glk_request_char_event_uni(window_t *win)
863 {
864 if (win == nullptr) {
865 gli_strict_warning("request_char_event_uni: invalid ref");
866 return;
867 }
868
869 if (win->char_request || win->line_request || win->char_request_uni || win->line_request_uni) {
870 gli_strict_warning("request_char_event_uni: window already has keyboard request");
871 return;
872 }
873
874 switch (win->type) {
875 case wintype_TextBuffer:
876 case wintype_TextGrid:
877 win->char_request_uni = true;
878 break;
879 default:
880 gli_strict_warning("request_char_event_uni: window does not support keyboard input");
881 break;
882 }
883 }
884
885 void glk_request_line_event(window_t *win, char *buf, glui32 maxlen,
886 glui32 initlen)
887 {
888 if (win == nullptr) {
889 gli_strict_warning("request_line_event: invalid ref");
890 return;
891 }
892
893 if (win->char_request || win->line_request || win->char_request_uni || win->line_request_uni) {
894 gli_strict_warning("request_line_event: window already has keyboard request");
895 return;
896 }
897
898 switch (win->type) {
899 case wintype_TextBuffer:
900 win->line_request = true;
901 win_textbuffer_init_line(win, buf, maxlen, initlen);
902 break;
903 case wintype_TextGrid:
904 win->line_request = true;
905 win_textgrid_init_line(win, buf, maxlen, initlen);
906 break;
907 default:
908 gli_strict_warning("request_line_event: window does not support keyboard input");
909 break;
910 }
911 }
912
913 void glk_request_line_event_uni(window_t *win, glui32 *buf, glui32 maxlen,
914 glui32 initlen)
915 {
916 if (win == nullptr) {
917 gli_strict_warning("request_line_event_uni: invalid ref");
918 return;
919 }
920
921 if (win->char_request || win->line_request || win->char_request_uni || win->line_request_uni) {
922 gli_strict_warning("request_line_event_uni: window already has keyboard request");
923 return;
924 }
925
926 switch (win->type) {
927 case wintype_TextBuffer:
928 win->line_request_uni = true;
929 win_textbuffer_init_line_uni(win, buf, maxlen, initlen);
930 break;
931 case wintype_TextGrid:
932 win->line_request_uni = true;
933 win_textgrid_init_line_uni(win, buf, maxlen, initlen);
934 break;
935 default:
936 gli_strict_warning("request_line_event_uni: window does not support keyboard input");
937 break;
938 }
939 }
940
941 void glk_set_echo_line_event(window_t *win, glui32 val)
942 {
943 if (win == nullptr) {
944 gli_strict_warning("set_echo_line_event: invalid ref");
945 return;
946 }
947
948 switch (win->type) {
949 case wintype_TextBuffer:
950 win->echo_line_input = (val != 0);
951 break;
952 default:
953 break;
954 }
955 }
956
957 void glk_set_terminators_line_event(winid_t win, glui32 *keycodes, glui32 count)
958 {
959 if (win == nullptr) {
960 gli_strict_warning("set_terminators_line_event: invalid ref");
961 return;
962 }
963
964 switch (win->type) {
965 case wintype_TextBuffer:
966 case wintype_TextGrid:
967 break;
968 default:
969 gli_strict_warning("set_terminators_line_event: window does not support keyboard input");
970 return;
971 }
972
973 win->line_terminators.clear();
974
975 if (keycodes != nullptr && count != 0) {
976 try {
977 win->line_terminators.resize(count);
978 std::copy(&keycodes[0], &keycodes[count], win->line_terminators.begin());
979 } catch (const std::bad_alloc &) {
980 }
981 }
982 }
983
984 bool gli_window_check_terminator(glui32 ch)
985 {
986 return ch == keycode_Escape || (ch >= keycode_Func12 && ch <= keycode_Func1);
987 }
988
989 void glk_request_mouse_event(window_t *win)
990 {
991 if (win == nullptr) {
992 gli_strict_warning("request_mouse_event: invalid ref");
993 return;
994 }
995
996 switch (win->type) {
997 case wintype_TextGrid:
998 case wintype_Graphics:
999 win->mouse_request = true;
1000 break;
1001 default:
1002 // do nothing
1003 break;
1004 }
1005 }
1006
1007 void glk_request_hyperlink_event(winid_t win)
1008 {
1009 if (win == nullptr) {
1010 gli_strict_warning("request_hyperlink_event: invalid ref");
1011 return;
1012 }
1013
1014 switch (win->type) {
1015 case wintype_TextBuffer:
1016 case wintype_TextGrid:
1017 case wintype_Graphics:
1018 win->hyper_request = true;
1019 break;
1020 default:
1021 // do nothing
1022 break;
1023 }
1024 }
1025
1026 void glk_cancel_char_event(window_t *win)
1027 {
1028 if (win == nullptr) {
1029 gli_strict_warning("cancel_char_event: invalid ref");
1030 return;
1031 }
1032
1033 switch (win->type) {
1034 case wintype_TextBuffer:
1035 case wintype_TextGrid:
1036 win->char_request = false;
1037 win->char_request_uni = false;
1038 break;
1039 default:
1040 // do nothing
1041 break;
1042 }
1043 }
1044
1045 void glk_cancel_line_event(window_t *win, event_t *ev)
1046 {
1047 event_t dummyev;
1048
1049 if (ev == nullptr) {
1050 ev = &dummyev;
1051 }
1052
1053 gli_event_clearevent(ev);
1054
1055 if (win == nullptr) {
1056 gli_strict_warning("cancel_line_event: invalid ref");
1057 return;
1058 }
1059
1060 switch (win->type) {
1061 case wintype_TextBuffer:
1062 if (win->line_request || win->line_request_uni) {
1063 win_textbuffer_cancel_line(win, ev);
1064 }
1065 break;
1066 case wintype_TextGrid:
1067 if (win->line_request || win->line_request_uni) {
1068 win_textgrid_cancel_line(win, ev);
1069 }
1070 break;
1071 default:
1072 // do nothing
1073 break;
1074 }
1075 }
1076
1077 void glk_cancel_mouse_event(window_t *win)
1078 {
1079 if (win == nullptr) {
1080 gli_strict_warning("cancel_mouse_event: invalid ref");
1081 return;
1082 }
1083
1084 switch (win->type) {
1085 case wintype_TextGrid:
1086 case wintype_Graphics:
1087 win->mouse_request = false;
1088 break;
1089 default:
1090 // do nothing
1091 break;
1092 }
1093 }
1094
1095 void glk_cancel_hyperlink_event(winid_t win)
1096 {
1097 if (win == nullptr) {
1098 gli_strict_warning("cancel_hyperlink_event: invalid ref");
1099 return;
1100 }
1101
1102 switch (win->type) {
1103 case wintype_TextBuffer:
1104 case wintype_TextGrid:
1105 case wintype_Graphics:
1106 win->hyper_request = false;
1107 break;
1108 default:
1109 // do nothing
1110 break;
1111 }
1112 }
1113
1114 void gli_window_click(window_t *win, int x, int y)
1115 {
1116 switch (win->type) {
1117 case wintype_Pair:
1118 win_pair_click(win->winpair(), x, y);
1119 break;
1120 case wintype_TextBuffer:
1121 win_textbuffer_click(win->winbuffer(), x, y);
1122 break;
1123 case wintype_TextGrid:
1124 win_textgrid_click(win->wingrid(), x, y);
1125 break;
1126 case wintype_Graphics:
1127 win_graphics_click(win->wingraphics(), x, y);
1128 break;
1129 }
1130 }
1131
1132 // Text output and cursor positioning
1133
1134 void gli_window_put_char_uni(window_t *win, glui32 ch)
1135 {
1136 switch (win->type) {
1137 case wintype_TextBuffer:
1138 win_textbuffer_putchar_uni(win, ch);
1139 break;
1140 case wintype_TextGrid:
1141 win_textgrid_putchar_uni(win, ch);
1142 break;
1143 }
1144 }
1145
1146 bool gli_window_unput_char_uni(window_t *win, glui32 ch)
1147 {
1148 switch (win->type) {
1149 case wintype_TextBuffer:
1150 return win_textbuffer_unputchar_uni(win, ch);
1151 case wintype_TextGrid:
1152 return win_textgrid_unputchar_uni(win, ch);
1153 default:
1154 return false;
1155 }
1156 }
1157
1158 void glk_window_clear(window_t *win)
1159 {
1160 if (win == nullptr) {
1161 gli_strict_warning("window_clear: invalid ref");
1162 return;
1163 }
1164
1165 if (win->line_request || win->line_request_uni) {
1166 if (gli_conf_safeclicks && gli_forceclick) {
1167 glk_cancel_line_event(win, nullptr);
1168 gli_forceclick = false;
1169 } else {
1170 gli_strict_warning("window_clear: window has pending line request");
1171 return;
1172 }
1173 }
1174
1175 switch (win->type) {
1176 case wintype_TextBuffer:
1177 win_textbuffer_clear(win);
1178 break;
1179 case wintype_TextGrid:
1180 win_textgrid_clear(win);
1181 break;
1182 case wintype_Graphics:
1183 win_graphics_erase_rect(win->wingraphics(), true, 0, 0, 0, 0);
1184 break;
1185 }
1186 }
1187
1188 void glk_window_move_cursor(window_t *win, glui32 xpos, glui32 ypos)
1189 {
1190 if (win == nullptr) {
1191 gli_strict_warning("window_move_cursor: invalid ref");
1192 return;
1193 }
1194
1195 switch (win->type) {
1196 case wintype_TextGrid:
1197 win_textgrid_move_cursor(win, xpos, ypos);
1198 break;
1199 default:
1200 gli_strict_warning("window_move_cursor: not a TextGrid window");
1201 break;
1202 }
1203 }
1204
1205 // Graphics and Image drawing
1206
1207 glui32 glk_image_draw_scaled_ext(winid_t win, glui32 image, glsi32 val1, glsi32 val2, glui32 width, glui32 height, glui32 imagerule, glui32 maxwidth)
1208 {
1209 if (win == nullptr) {
1210 gli_strict_warning("image_draw: invalid ref");
1211 return false;
1212 }
1213
1214 if (!gli_conf_graphics) {
1215 return false;
1216 }
1217
1218 auto pic = gli_picture_load(image);
1219 if (pic == nullptr) {
1220 return false;
1221 }
1222
1223 if (!win->image_loaded) {
1224 gli_piclist_increment();
1225 win->image_loaded = true;
1226 }
1227
1228 glui32 window_width = std::round((win->bbox.x1 - win->bbox.x0) / gli_zoom);
1229
1230 switch (imagerule & imagerule_WidthMask) {
1231 case imagerule_WidthOrig:
1232 width = pic->w;
1233 break;
1234 case imagerule_WidthFixed:
1235 // width is used as-is.
1236 break;
1237 case imagerule_WidthRatio:
1238 width = std::round(window_width * (width / 65536.0));
1239 break;
1240 default:
1241 return false;
1242 }
1243
1244 switch (imagerule & imagerule_HeightMask) {
1245 case imagerule_HeightOrig:
1246 height = pic->h;
1247 break;
1248 case imagerule_HeightFixed:
1249 // height is used as-is.
1250 break;
1251 case imagerule_AspectRatio: {
1252 double aspect = static_cast<double>(pic->h) / pic->w;
1253 height = std::round(width * aspect * (height / 65536.0));
1254 break;
1255 }
1256 default:
1257 return false;
1258 }
1259
1260 switch (win->type) {
1261 case wintype_TextBuffer:
1262 return win_textbuffer_draw_picture(pic, win->winbuffer(), window_width, val1, width, height, maxwidth);
1263 case wintype_Graphics:
1264 return win_graphics_draw_picture(pic, win->wingraphics(), val1, val2, width, height);
1265 }
1266
1267 return false;
1268 }
1269
1270 glui32 glk_image_draw(winid_t win, glui32 image, glsi32 val1, glsi32 val2)
1271 {
1272 return glk_image_draw_scaled_ext(win, image, val1, val2, 0, 0, imagerule_WidthOrig | imagerule_HeightOrig, 0x10000);
1273 }
1274
1275 glui32 glk_image_draw_scaled(winid_t win, glui32 image,
1276 glsi32 val1, glsi32 val2, glui32 width, glui32 height)
1277 {
1278 return glk_image_draw_scaled_ext(win, image, val1, val2, width, height, imagerule_WidthFixed | imagerule_HeightFixed, 0x10000);
1279 }
1280
1281 glui32 glk_image_get_info(glui32 image, glui32 *width, glui32 *height)
1282 {
1283 if (!gli_conf_graphics) {
1284 return false;
1285 }
1286
1287 auto pic = gli_picture_load(image);
1288 if (!pic) {
1289 return false;
1290 }
1291
1292 if (width != nullptr) {
1293 *width = pic->w;
1294 }
1295 if (height != nullptr) {
1296 *height = pic->h;
1297 }
1298
1299 return true;
1300 }
1301
1302 void glk_window_flow_break(winid_t win)
1303 {
1304 if (win == nullptr) {
1305 gli_strict_warning("window_erase_rect: invalid ref");
1306 return;
1307 }
1308 if (win->type != wintype_TextBuffer) {
1309 gli_strict_warning("window_flow_break: not a text buffer window");
1310 return;
1311 }
1312 win_textbuffer_flow_break(win->winbuffer());
1313 }
1314
1315 void glk_window_erase_rect(winid_t win,
1316 glsi32 left, glsi32 top, glui32 width, glui32 height)
1317 {
1318 if (win == nullptr) {
1319 gli_strict_warning("window_erase_rect: invalid ref");
1320 return;
1321 }
1322 if (win->type != wintype_Graphics) {
1323 gli_strict_warning("window_erase_rect: not a graphics window");
1324 return;
1325 }
1326 win_graphics_erase_rect(win->wingraphics(), false, left, top, width, height);
1327 }
1328
1329 void glk_window_fill_rect(winid_t win, glui32 color,
1330 glsi32 left, glsi32 top, glui32 width, glui32 height)
1331 {
1332 if (win == nullptr) {
1333 gli_strict_warning("window_fill_rect: invalid ref");
1334 return;
1335 }
1336 if (win->type != wintype_Graphics) {
1337 gli_strict_warning("window_fill_rect: not a graphics window");
1338 return;
1339 }
1340 win_graphics_fill_rect(win->wingraphics(), color, left, top, width, height);
1341 }
1342
1343 void glk_window_set_background_color(winid_t win, glui32 color)
1344 {
1345 if (win == nullptr) {
1346 gli_strict_warning("window_set_background_color: invalid ref");
1347 return;
1348 }
1349 if (win->type != wintype_Graphics) {
1350 gli_strict_warning("window_set_background_color: not a graphics window");
1351 return;
1352 }
1353 win_graphics_set_background_color(win->wingraphics(), color);
1354 }
1355
1356 void attr_t::set(glui32 style_)
1357 {
1358 fgcolor.reset();
1359 bgcolor.reset();
1360 reverse = false;
1361 style = style_;
1362 }
1363
1364 void attr_t::clear()
1365 {
1366 fgcolor.reset();
1367 bgcolor.reset();
1368 reverse = false;
1369 hyper = 0;
1370 style = 0;
1371 }
1372
1373 FontFace attr_t::font(const Styles &styles) const
1374 {
1375 return styles[style].font;
1376 }
1377
1378 static Color zcolor_LightGrey = Color(181, 181, 181);
1379 static nonstd::optional<Color> zcolor_Foreground = Color(0, 0, 0);
1380 static nonstd::optional<Color> zcolor_Background = Color(0, 0, 0);
1381
1382 static Color rgbshift(const Color &rgb)
1383 {
1384 return Color(std::min(rgb[0] + 0x30, 0xff),
1385 std::min(rgb[1] + 0x30, 0xff),
1386 std::min(rgb[2] + 0x30, 0xff));
1387 }
1388
1389 Color attr_t::bg(const Styles &styles) const
1390 {
1391 bool revset = reverse || (styles[style].reverse && !gli_override_reverse);
1392
1393 zcolor_Foreground = fgcolor.has_value() ? fgcolor :
1394 gli_override_fg.has_value() ? gli_override_fg :
1395 nonstd::nullopt;
1396
1397 zcolor_Background = bgcolor.has_value() ? bgcolor :
1398 gli_override_bg.has_value() ? gli_override_bg :
1399 nonstd::nullopt;
1400
1401 if (!revset) {
1402 return zcolor_Background.value_or(styles[style].bg);
1403 } else {
1404 if (zcolor_Foreground.has_value()) {
1405 if (zcolor_Foreground == zcolor_Background) {
1406 return rgbshift(*zcolor_Foreground);
1407 } else {
1408 return *zcolor_Foreground;
1409 }
1410 } else {
1411 if (styles[style].fg == zcolor_Background) {
1412 return zcolor_LightGrey;
1413 } else {
1414 return styles[style].fg;
1415 }
1416 }
1417 }
1418 }
1419
1420 Color attr_t::fg(const Styles &styles) const
1421 {
1422 bool revset = reverse || (styles[style].reverse && !gli_override_reverse);
1423
1424 zcolor_Foreground = fgcolor.has_value() ? fgcolor :
1425 gli_override_fg.has_value() ? gli_override_fg :
1426 nonstd::nullopt;
1427
1428 zcolor_Background = bgcolor.has_value() ? bgcolor :
1429 gli_override_bg.has_value() ? gli_override_bg :
1430 nonstd::nullopt;
1431
1432 if (!revset) {
1433 if (zcolor_Foreground.has_value()) {
1434 if (zcolor_Foreground == zcolor_Background) {
1435 return rgbshift(*zcolor_Foreground);
1436 } else {
1437 return *zcolor_Foreground;
1438 }
1439 } else {
1440 if (styles[style].fg == zcolor_Background) {
1441 return zcolor_LightGrey;
1442 } else {
1443 return styles[style].fg;
1444 }
1445 }
1446 } else {
1447 return zcolor_Background.value_or(styles[style].bg);
1448 }
1449 }