forked from UplinkCoder/d-tui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtapplication.d
1472 lines (1310 loc) · 35.5 KB
/
tapplication.d
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* D Text User Interface library - TApplication class
*
* Version: $Id$
*
* Author: Kevin Lamonte, <a href="mailto:[email protected]">[email protected]</a>
*
* License: LGPLv3 or later
*
* Copyright: This module is licensed under the GNU Lesser General
* Public License Version 3. Please see the file "COPYING" in this
* directory for more information about the GNU Lesser General Public
* License Version 3.
*
* Copyright (C) 2013 Kevin Lamonte
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 3 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 Lesser General Public
* License along with this program; if not, see
* http://www.gnu.org/licenses/, or write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
module tui.tapplication;
// Description ---------------------------------------------------------------
// Imports -------------------------------------------------------------------
import std.algorithm : sort;
import std.range : reverse = retro;
import core.thread;
import std.conv;
import std.datetime;
import std.math;
import std.socket;
import tui.base;
import tui.codepage;
version(Posix) {
import tui.ecma;
import tui.tterminal;
}
import tui.win32;
import tui.twidget;
import tui.twindow;
import tui.teditor;
import tui.tfileopen;
import tui.tmessagebox;
import tui.tmenu;
import tui.ttimer;
// Defines -------------------------------------------------------------------
// Globals -------------------------------------------------------------------
// Classes -------------------------------------------------------------------
/**
* TApplication sets up a full Text User Interface application.
*/
public class TApplication {
/// Access to the physical screen, keyboard, and mouse.
public Backend backend;
/// Actual mouse coordinate X
private uint mouseX;
/// Actual mouse coordinate Y
private uint mouseY;
/// Fiber for main handleEvent loop
private Fiber primaryEventFiber;
/// Fiber for scondary handleEvent loop
private Fiber secondaryEventFiber;
/// Widget to receive events if secondaryEventFiber is called
private TWidget secondaryEventReceiver;
/// Event queue that will be drained by either primary or secondary Fiber
private TInputEvent [] eventQueue;
/// Windows in this application.
private TWindow [] windows;
/// Top-level menus in this application.
private TMenu [] menus;
/// Stack of activated sub-menus in this application.
private TMenu [] subMenus;
/// Timers that are being ticked.
private TTimer [] timers;
/// The currently acive menu
private TMenu activeMenu = null;
/// Windows and widgets pull colors from this ColorTheme.
public ColorTheme theme;
/// When true, exit the application.
public bool quit = false;
/// When true, repaint the entire screen
public bool repaint = true;
/// When true, just flush updates from the screen.
public bool flush = false;
/// Y coordinate of the top edge of the desktop.
public static immutable int desktopTop = 1;
/// Y coordinate of the bottom edge of the desktop.
public int desktopBottom;
/// Active keyboard accelerators
private TMenuItem[TKeypress] accelerators;
/**
* Public constructor.
*
* Params:
* socket = remote socket to the user, or null if using stdio
*/
public this(Socket socket = null) {
if (socket !is null) {
version(Posix) {
backend = new ECMABackend(socket);
} else {
assert(false);
}
} else {
version(Posix) {
backend = new ECMABackend();
}
version(Windows) {
backend = new Win32ConsoleBackend();
}
}
theme = new ColorTheme();
desktopBottom = backend.screen.getHeight() - 1;
primaryEventFiber = new Fiber(&primaryEventHandler);
}
/// Invert the cell at the mouse pointer position
private void drawMouse() {
CellAttributes attr = backend.screen.getAttrXY(mouseX, mouseY);
attr.foreColor = invertColor(attr.foreColor);
attr.backColor = invertColor(attr.backColor);
backend.screen.putAttrXY(mouseX, mouseY, attr, false);
flush = true;
if (windows.length == 0) {
repaint = true;
}
}
/**
* Draw everything.
*
* Returns:
* escape sequences string that provides the updates to the
* physical screen
*/
final public void drawAll() {
if ((flush) && (!repaint)) {
backend.flushScreen();
flush = false;
return;
}
if (!repaint) {
return;
}
// If true, the cursor is not visible
bool cursor = false;
// Start with a clean screen
backend.screen.clear();
// Draw the background
CellAttributes background = theme.getColor("tapplication.background");
backend.screen.putAll(GraphicsChars.HATCH, background);
// Draw each window in reverse Z order
TWindow [] sorted = windows.dup;
sorted.sort.reverse;
foreach (w; sorted) {
w.drawChildren();
}
// Draw the blank menubar line - reset the screen clipping first so
// it won't trim it out.
backend.screen.resetClipping();
backend.screen.hLineXY(0, 0, backend.screen.getWidth(), ' ',
theme.getColor("tmenu"));
// Now draw the menus.
uint x = 1;
foreach (m; menus) {
CellAttributes menuColor;
CellAttributes menuMnemonicColor;
if (m.active) {
menuColor = theme.getColor("tmenu.highlighted");
menuMnemonicColor = theme.getColor("tmenu.mnemonic.highlighted");
} else {
menuColor = theme.getColor("tmenu");
menuMnemonicColor = theme.getColor("tmenu.mnemonic");
}
// Draw the menu title
backend.screen.hLineXY(x, 0, cast(uint)m.title.length + 2, ' ',
menuColor);
backend.screen.putStrXY(x + 1, 0, m.title, menuColor);
// Draw the highlight character
backend.screen.putCharXY(x + 1 + m.mnemonic.shortcutIdx, 0,
m.mnemonic.shortcut, menuMnemonicColor);
if (m.active) {
m.drawChildren();
// Reset the screen clipping so we can draw the next title.
backend.screen.resetClipping();
}
x += m.title.length + 2;
}
foreach (m; subMenus) {
// Reset the screen clipping so we can draw the next sub-menu.
backend.screen.resetClipping();
m.drawChildren();
}
// Draw the mouse pointer
drawMouse();
// Place the cursor if it is visible
TWidget activeWidget = null;
if (sorted.length > 0) {
activeWidget = sorted[$ - 1].getActiveChild();
if (activeWidget.hasCursor) {
backend.screen.putCursor(true, activeWidget.getCursorAbsoluteX(),
activeWidget.getCursorAbsoluteY());
cursor = true;
}
}
// Kill the cursor
if (cursor == false) {
backend.screen.hideCursor();
}
// Flush the screen contents
backend.flushScreen();
repaint = false;
flush = false;
}
/**
* Add a keyboard accelerator to the global hash
*
* Params:
* item = menu item this accelerator relates to
* keypress = keypress that will dispatch a TMenuEvent
*/
final public void addAccelerator(TMenuItem item, TKeypress keypress) {
assert((keypress in accelerators) is null);
accelerators[keypress] = item;
}
/**
* Add a sub-menu to the list of open sub-menus
*
* Params:
* menu = sub-menu
*/
final public void addSubMenu(TMenu menu) {
subMenus ~= menu;
}
/**
* Add a window to my window list and make it active
*
* Params:
* window = new window to add
*/
final public void addWindow(TWindow window) {
// Do not allow a modal window to spawn a non-modal window
if ((windows.length > 0) && (windows[0].isModal())) {
assert(window.isModal());
}
foreach (w; windows) {
w.active = false;
w.z++;
}
windows ~= window;
window.active = true;
window.z = 0;
}
/**
* Check if there is a system-modal window on top
*
* Returns:
* true if the active window is modal
*/
private bool modalWindowActive() {
if (windows.length == 0) {
return false;
}
return windows[$ - 1].isModal();
}
/**
* Switch to the next window
*
* Params:
* forward = switch to the next window in the list
*/
final public void switchWindow(bool forward) {
// Only switch if there are multiple windows
if (windows.length < 2) {
return;
}
// Swap z/active between active window and the next in the
// list
ptrdiff_t activeWindowI = -1;
for (auto i = 0; i < windows.length; i++) {
if (windows[i].active) {
activeWindowI = i;
break;
}
}
assert(activeWindowI >= 0);
// Do not switch if a window is modal
if (windows[activeWindowI].isModal()) {
return;
}
size_t nextWindowI;
if (forward) {
nextWindowI = (activeWindowI + 1) % windows.length;
} else {
if (activeWindowI == 0) {
nextWindowI = windows.length - 1;
} else {
nextWindowI = activeWindowI - 1;
}
}
windows[activeWindowI].active = false;
windows[activeWindowI].z = windows[nextWindowI].z;
windows[nextWindowI].z = 0;
windows[nextWindowI].active = true;
// Refresh
repaint = true;
}
/**
* Check if a mouse event would hit either the active menu or any
* open sub-menus.
*
* Params:
* mouse = mouse event
*
* Returns:
* true if the mouse would hit
*/
private bool mouseOnMenu(TMouseEvent mouse) {
assert(activeMenu !is null);
TMenu [] menus = subMenus.dup;
menus.reverse;
foreach (m; menus) {
if (m.mouseWouldHit(mouse)) {
return true;
}
}
return activeMenu.mouseWouldHit(mouse);
}
/// See if we need to switch window or activate the menu based on
/// a mouse click
final public void checkSwitchFocus(TMouseEvent mouse) {
if ((mouse.type == TMouseEvent.Type.MOUSE_DOWN) &&
(activeMenu !is null) &&
(mouse.absoluteY != 0) &&
(!mouseOnMenu(mouse))
) {
// They clicked outside the active menu, turn it off
activeMenu.active = false;
activeMenu = null;
foreach (m; subMenus) {
m.active = false;
}
subMenus.length = 0;
// Continue checks
}
// See if they hit the menu bar
if ((mouse.type == TMouseEvent.Type.MOUSE_DOWN) &&
(mouse.mouse1) &&
(!modalWindowActive()) &&
(mouse.absoluteY == 0)
) {
foreach (m; subMenus) {
m.active = false;
}
subMenus.length = 0;
// They selected the menu, go activate it
foreach (m; menus) {
if ((mouse.absoluteX >= m.x) &&
(mouse.absoluteX < m.x + m.title.length + 2)
) {
m.active = true;
activeMenu = m;
} else {
m.active = false;
}
}
repaint = true;
return;
}
// See if they hit the menu bar
if ((mouse.type == TMouseEvent.Type.MOUSE_MOTION) &&
(mouse.mouse1) &&
(activeMenu !is null) &&
(mouse.absoluteY == 0)
) {
TMenu oldMenu = activeMenu;
foreach (m; subMenus) {
m.active = false;
}
subMenus.length = 0;
// See if we should switch menus
foreach (m; menus) {
if ((mouse.absoluteX >= m.x) &&
(mouse.absoluteX < m.x + m.title.length + 2)
) {
m.active = true;
activeMenu = m;
}
}
if (oldMenu !is activeMenu) {
// They switched menus
oldMenu.active = false;
}
repaint = true;
return;
}
// Only switch if there are multiple windows
if (windows.length < 2) {
return;
}
// Switch on the upclick
if (mouse.type != TMouseEvent.Type.MOUSE_UP) {
return;
}
windows.sort;
if (windows[0].isModal()) {
// Modal windows don't switch
return;
}
foreach (w; windows) {
assert(!w.isModal());
if (w.mouseWouldHit(mouse)) {
if (w is windows[0]) {
// Clicked on the same window, nothing to do
return;
}
// We will be switching to another window
assert(windows[0].active);
assert(!w.active);
windows[0].active = false;
windows[0].z = w.z;
w.z = 0;
w.active = true;
repaint = true;
return;
}
}
// Clicked on the background, nothing to do
return;
}
/**
* Close window. Note that the window's destructor is NOT called
* by this method, instead the GC is assumed to do the cleanup.
*
* Params:
* window = the window to remove
*/
final public void closeWindow(TWindow window) {
uint z = window.z;
window.z = -1;
windows.sort;
windows = windows[1 .. $];
TWindow activeWindow = null;
foreach (w; windows) {
if (w.z > z) {
w.z--;
if (w.z == 0) {
w.active = true;
assert(activeWindow is null);
activeWindow = w;
} else {
w.active = false;
}
}
}
// Perform window cleanup
window.onClose();
// Refresh screen
repaint = true;
// Check if we are closing a TMessageBox or similar
if (secondaryEventReceiver !is null) {
assert(secondaryEventFiber !is null);
// Do not send events to the secondaryEventReceiver anymore, the
// window is closed.
secondaryEventReceiver = null;
// Special case: if this is called while executing on a
// secondaryEventFiber, call it so that widgetEventHandler() can
// terminate.
if (secondaryEventFiber.state == Fiber.State.HOLD) {
secondaryEventFiber.call();
}
secondaryEventFiber = null;
// Unfreeze the logic in handleEvent()
if (primaryEventFiber.state == Fiber.State.HOLD) {
primaryEventFiber.call();
}
}
}
/**
* Enable a widget to override the event queue
*
* Params:
* widget = widget that will receive events
*/
final public void enableSecondaryEventReceiver(TWidget widget) {
assert(secondaryEventReceiver is null);
assert(secondaryEventFiber is null);
assert(cast(TMessageBox)widget || cast(TFileOpenBox)widget);
secondaryEventReceiver = widget;
secondaryEventFiber = new Fiber(&widgetEventHandler);
// Refresh
repaint = true;
}
/**
* Post an event to process
*
* Params:
* event = new event to add to the queue
*/
final public void addEvent(TInputEvent event) {
eventQueue ~= event;
}
/**
* Post an event to process and turn off the menu
*
* Params:
* event = new event to add to the queue
*/
final public void addMenuEvent(TInputEvent event) {
eventQueue ~= event;
closeMenu();
}
/**
* Turn off the menu
*/
final public void closeMenu() {
if (activeMenu !is null) {
activeMenu.active = false;
activeMenu = null;
foreach (m; subMenus) {
m.active = false;
}
subMenus.length = 0;
}
repaint = true;
}
/**
* Turn off a menu
*/
final public void closeSubMenu() {
assert(activeMenu !is null);
auto item = subMenus[$ - 1];
assert(item);
item.active = false;
subMenus.length--;
repaint = true;
}
/**
* Switch to the next menu
*/
final public void switchMenu(bool forward = true) {
assert(activeMenu !is null);
foreach (m; subMenus) {
m.active = false;
}
subMenus.length = 0;
for (auto i = 0; i < menus.length; i++) {
if (activeMenu is menus[i]) {
if (forward) {
if (i < menus.length - 1) {
i++;
}
} else {
if (i > 0) {
i--;
}
}
activeMenu.active = false;
activeMenu = menus[i];
activeMenu.active = true;
repaint = true;
return;
}
}
}
/**
* Peek at certain application-level events, add to eventQueue,
* and wake up the consuming Fiber.
*
* Params:
* events the input events to consume
*/
private void metaHandleEvents(TInputEvent [] events) {
foreach (event; events) {
// std.stdio.stderr.writefln("metaHandleEvents event: %s primary %s",
// event, primaryEventFiber.state);
if (quit == true) {
// Do no more processing if the application is already trying to
// exit.
return;
}
// Special application-wide events -------------------------------
// Abort everything
if (auto command = cast(TCommandEvent)event) {
if (command.cmd == cmAbort) {
quit = true;
return;
}
}
// Screen resize
if (auto resize = cast(TResizeEvent)event) {
backend.screen.setDimensions(resize.width, resize.height);
desktopBottom = backend.screen.getHeight() - 1;
repaint = true;
mouseX = 0;
mouseY = 0;
continue;
}
// Peek at the mouse position
if (auto mouse = cast(TMouseEvent)event) {
if ((mouseX != mouse.x) || (mouseY != mouse.y)) {
mouseX = mouse.x;
mouseY = mouse.y;
drawMouse();
}
}
// Put into the main queue
addEvent(event);
// Have one of the two consumer Fibers peel the events off
// the queue.
if (secondaryEventFiber !is null) {
assert(secondaryEventFiber.state == Fiber.State.HOLD);
// Wake up the secondary handler for these events
secondaryEventFiber.call();
} else {
assert(primaryEventFiber.state == Fiber.State.HOLD);
// Wake up the primary handler for these events
primaryEventFiber.call();
}
} // foreach (event; events)
}
/**
* The default event queue function called by primaryFiber
*/
private void primaryEventHandler() {
while (quit == false) {
// Yield if there is nothing to do.
if (eventQueue.length == 0) {
Fiber.yield();
}
// It is possible that eventQueue will shrink to 0 between calls
// to handleEvent, so we must explicitly check every time. We
// cannot use a foreach here.
if (eventQueue.length > 0) {
TInputEvent event = eventQueue[0];
eventQueue = eventQueue[1 .. $];
handleEvent(event);
}
}
}
/**
* The event queue function called by application.secondaryFiber
*/
private void widgetEventHandler() {
// For some reason this assert fires even when I wake inside the
// while(). Not sure if this is a bug in Fibers or a flaw in my
// understanding of them.
// assert(secondaryEventReceiver !is null);
while (secondaryEventReceiver !is null) {
// Yield if there is nothing to do.
if (eventQueue.length == 0) {
Fiber.yield();
}
// It is possible that eventQueue will shrink to 0 between calls
// to handleEvent, so we must explicitly check every time. We
// cannot use a foreach here.
if (eventQueue.length > 0) {
TInputEvent event = eventQueue[0];
eventQueue = eventQueue[1 .. $];
secondaryEventReceiver.handleEvent(event);
}
}
}
/**
* Dispatch one event to the appropriate widget or application-level
* event handler.
*
* Params:
* event the input event to consume
*/
private void handleEvent(TInputEvent event) {
// std.stdio.stderr.writefln("Handle event: %s", event);
// Special application-wide events -----------------------------------
// Peek at the mouse position
if (auto mouse = cast(TMouseEvent)event) {
// See if we need to switch focus to another window or the menu
checkSwitchFocus(mouse);
}
// Handle menu events
if ((activeMenu !is null) && (!cast(TCommandEvent)event)) {
TMenu menu = activeMenu;
if (auto mouse = cast(TMouseEvent)event) {
while (subMenus.length > 0) {
TMenu subMenu = subMenus[$ - 1];
if (subMenu.mouseWouldHit(mouse)) {
break;
}
if ((mouse.type == TMouseEvent.Type.MOUSE_MOTION) &&
(!mouse.mouse1) &&
(!mouse.mouse2) &&
(!mouse.mouse3) &&
(!mouse.mouseWheelUp) &&
(!mouse.mouseWheelDown)
) {
break;
}
// We navigated away from a sub-menu, so close it
closeSubMenu();
}
// Convert the mouse relative x/y to menu coordinates
assert(mouse.x == mouse.absoluteX);
assert(mouse.y == mouse.absoluteY);
if (subMenus.length > 0) {
menu = subMenus[$ - 1];
}
mouse.x -= menu.x;
mouse.y -= menu.y;
}
menu.handleEvent(event);
return;
}
if (auto keypress = cast(TKeypressEvent)event) {
// See if this key matches an accelerator, and if so dispatch the
// menu event.
TKeypress keypressLowercase = toLower(keypress.key);
TMenuItem *item = (keypressLowercase in accelerators);
if (item !is null) {
// Let the menu item dispatch
item.dispatch();
return;
} else {
// Handle the keypress
if (onKeypress(keypress)) {
return;
}
}
}
if (auto cmd = cast(TCommandEvent)event) {
if (onCommand(cmd)) {
return;
}
}
if (auto menu = cast(TMenuEvent)event) {
if (onMenu(menu)) {
return;
}
}
// Dispatch events to the active window -------------------------------
foreach (w; windows) {
if (w.active) {
if (auto mouse = cast(TMouseEvent)event) {
// Convert the mouse relative x/y to window coordinates
assert(mouse.x == mouse.absoluteX);
assert(mouse.y == mouse.absoluteY);
mouse.x -= w.x;
mouse.y -= w.y;
}
// std.stdio.stderr.writefln("TApplication dispatch event: %s", event);
w.handleEvent(event);
break;
}
}
}
/**
* Close all open windows
*/
private void closeAllWindows() {
// Don't do anything if we are in the menu
if (activeMenu !is null) {
return;
}
foreach (w; windows) {
closeWindow(w);
}
}
/**
* Re-layout the open windows as non-overlapping tiles. This produces
* almost the same results as Turbo Pascal 7.0's IDE.
*/
private void tileWindows() {
// Don't do anything if we are in the menu
if (activeMenu !is null) {
return;
}
size_t z = windows.length;
if (z == 0) {
return;
}
size_t a, b;
a = to!size_t(sqrt(to!float(z)));
size_t c = 0;
while (c < a) {
b = (z - c) / a;
if (((a * b) + c) == z) {
break;
}
c++;
}
assert(a > 0);
assert(b > 0);
assert(c < a);
auto newWidth = (backend.screen.getWidth() / a);
auto newHeight1 = ((backend.screen.getHeight() - 1) / b);
auto newHeight2 = ((backend.screen.getHeight() - 1) / (b + c));
// std.stdio.stderr.writefln("Z %s a %s b %s c %s newWidth %s newHeight1 %s newHeight2 %s",
// z, a, b, c, newWidth, newHeight1, newHeight2);
TWindow [] sorted = windows.dup;
sorted.sort.reverse;
for (auto i = 0; i < sorted.length; i++) {
auto logicalX = i / b;
auto logicalY = i % b;
if (i >= ((a - 1) * b)) {
logicalX = a - 1;
logicalY = i - ((a - 1) * b);
}
TWindow w = sorted[i];
w.x = to!int(logicalX * newWidth);
w.width = to!uint(newWidth);
if (i >= ((a - 1) * b)) {
w.y = to!int(logicalY * newHeight2) + 1;
w.height = to!uint(newHeight2);
} else {
w.y = to!int(logicalY * newHeight1) + 1;
w.height = to!uint(newHeight1);
}
}
}
/**
* Re-layout the open windows as overlapping cascaded windows.
*/
private void cascadeWindows() {
// Don't do anything if we are in the menu
if (activeMenu !is null) {
return;
}
uint x = 0;
uint y = 1;
TWindow [] sorted = windows.dup;
sorted.sort.reverse;
foreach (w; sorted) {
w.x = x;
w.y = y;
x++;
y++;
if (x > backend.screen.getWidth()) {
x = 0;
}
if (y >= backend.screen.getHeight()) {
y = 1;
}
}
}
/**
* Method that TApplication subclasses can override to handle menu or
* posted command events.
*
* Params:
* cmd = command event
*
* Returns:
* if true, this event was consumed
*/
protected bool onCommand(TCommandEvent cmd) {
// Default: handle cmExit
if (cmd.cmd == cmExit) {
if (messageBox("Confirmation", "Exit application?",
TMessageBox.Type.YESNO).result == TMessageBox.Result.YES) {
quit = true;
}
// std.stdio.stderr.writefln("onCommand cmExit result: quit = %s", quit);
// std.stdio.stderr.flush();
repaint = true;
return true;
}
/+
version(Posix) {
if (cmd.cmd == cmShell) {
openTerminal(0, 0, TWindow.Flag.RESIZABLE);
repaint = true;
return true;
}