update terminals list
[nikiroo-utils.git] / src / jexer / TWindow.java
CommitLineData
daa4106c 1/*
48e27807
KL
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
48e27807 5 *
a69ed767 6 * Copyright (C) 2019 Kevin Lamonte
48e27807 7 *
e16dda65
KL
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
48e27807 14 *
e16dda65
KL
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
48e27807 17 *
e16dda65
KL
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
48e27807
KL
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer;
30
5dfd1c11 31import java.util.HashSet;
d36057df 32import java.util.Set;
5dfd1c11 33
42873e30 34import jexer.backend.Screen;
48e27807
KL
35import jexer.bits.CellAttributes;
36import jexer.bits.GraphicsChars;
37import jexer.event.TCommandEvent;
38import jexer.event.TKeypressEvent;
39import jexer.event.TMenuEvent;
40import jexer.event.TMouseEvent;
41import jexer.event.TResizeEvent;
928811d8 42import jexer.menu.TMenu;
48e27807
KL
43import static jexer.TCommand.*;
44import static jexer.TKeypress.*;
45
46/**
47 * TWindow is the top-level container and drawing surface for other widgets.
48 */
2b9c27db 49public class TWindow extends TWidget {
48e27807 50
2ce6dab2 51 // ------------------------------------------------------------------------
d36057df 52 // Constants --------------------------------------------------------------
2ce6dab2
KL
53 // ------------------------------------------------------------------------
54
48e27807 55 /**
2ce6dab2 56 * Window is resizable (default yes).
48e27807 57 */
2ce6dab2 58 public static final int RESIZABLE = 0x01;
48e27807
KL
59
60 /**
2ce6dab2 61 * Window is modal (default no).
48e27807 62 */
2ce6dab2 63 public static final int MODAL = 0x02;
48e27807
KL
64
65 /**
2ce6dab2 66 * Window is centered (default no).
48e27807 67 */
2ce6dab2
KL
68 public static final int CENTERED = 0x04;
69
78a56d5d
KL
70 /**
71 * Window has no close box (default no). Window can still be closed via
72 * TApplication.closeWindow() and TWindow.close().
73 */
74 public static final int NOCLOSEBOX = 0x08;
75
68c5cd6b
KL
76 /**
77 * Window has no maximize box (default no).
78 */
79 public static final int NOZOOMBOX = 0x10;
80
48e27807 81 /**
d36057df
KL
82 * Window is placed at absolute position (no smart placement) (default
83 * no).
fca67db0 84 */
d36057df 85 public static final int ABSOLUTEXY = 0x20;
fca67db0
KL
86
87 /**
d36057df
KL
88 * Hitting the closebox with the mouse calls TApplication.hideWindow()
89 * rather than TApplication.closeWindow() (default no).
fca67db0 90 */
d36057df 91 public static final int HIDEONCLOSE = 0x40;
48e27807 92
9696a8f6
KL
93 /**
94 * Menus cannot be used when this window is active (default no).
95 */
96 public static final int OVERRIDEMENU = 0x80;
97
2ce6dab2 98 // ------------------------------------------------------------------------
d36057df 99 // Variables --------------------------------------------------------------
2ce6dab2 100 // ------------------------------------------------------------------------
48e27807
KL
101
102 /**
d36057df 103 * Window flags. Note package private access.
48e27807 104 */
d36057df 105 int flags = RESIZABLE;
48e27807
KL
106
107 /**
d36057df 108 * Window title.
48e27807 109 */
d36057df 110 private String title = "";
48e27807
KL
111
112 /**
d36057df 113 * Window's parent TApplication.
48e27807 114 */
d36057df 115 private TApplication application;
48e27807
KL
116
117 /**
118 * Z order. Lower number means more in-front.
119 */
120 private int z = 0;
121
5dfd1c11
KL
122 /**
123 * Window's keyboard shortcuts. Any key in this set will be passed to
124 * the window directly rather than processed through the menu
125 * accelerators.
126 */
d36057df 127 private Set<TKeypress> keyboardShortcuts = new HashSet<TKeypress>();
2ce6dab2 128
48e27807
KL
129 /**
130 * If true, then the user clicked on the title bar and is moving the
131 * window.
132 */
bd8d51fa 133 protected boolean inWindowMove = false;
48e27807
KL
134
135 /**
136 * If true, then the user clicked on the bottom right corner and is
137 * resizing the window.
138 */
bd8d51fa 139 protected boolean inWindowResize = false;
48e27807
KL
140
141 /**
142 * If true, then the user selected "Size/Move" (or hit Ctrl-F5) and is
143 * resizing/moving the window via the keyboard.
144 */
d36057df 145 protected boolean inKeyboardResize = false;
48e27807
KL
146
147 /**
148 * If true, this window is maximized.
149 */
150 private boolean maximized = false;
151
152 /**
153 * Remember mouse state.
154 */
928811d8 155 protected TMouseEvent mouse;
48e27807
KL
156
157 // For moving the window. resizing also uses moveWindowMouseX/Y
158 private int moveWindowMouseX;
159 private int moveWindowMouseY;
160 private int oldWindowX;
161 private int oldWindowY;
162
163 // Resizing
164 private int resizeWindowWidth;
165 private int resizeWindowHeight;
166 private int minimumWindowWidth = 10;
167 private int minimumWindowHeight = 2;
168 private int maximumWindowWidth = -1;
169 private int maximumWindowHeight = -1;
170
171 // For maximize/restore
172 private int restoreWindowWidth;
173 private int restoreWindowHeight;
174 private int restoreWindowX;
175 private int restoreWindowY;
176
34a42e78 177 /**
d36057df
KL
178 * Hidden flag. A hidden window will still have its onIdle() called, and
179 * will also have onClose() called at application exit. Note package
180 * private access: TApplication will force hidden false if a modal window
181 * is active.
34a42e78 182 */
d36057df 183 boolean hidden = false;
34a42e78 184
71a389c9 185 /**
d36057df
KL
186 * A window may have a status bar associated with it. TApplication will
187 * draw this status bar last, and will also route events to it first
188 * before the window.
71a389c9 189 */
d36057df
KL
190 protected TStatusBar statusBar = null;
191
978a5d8f
KL
192 /**
193 * A window may request that TApplication NOT draw the mouse cursor over
194 * it by setting this to true. This is currently only used within Jexer
195 * by TTerminalWindow so that only the bottom-most instance of nested
196 * Jexer's draws the mouse within its application window. But perhaps
197 * other applications can use it, so public getter/setter is provided.
198 */
199 private boolean hideMouse = false;
200
d36057df
KL
201 // ------------------------------------------------------------------------
202 // Constructors -----------------------------------------------------------
203 // ------------------------------------------------------------------------
71a389c9
KL
204
205 /**
d36057df 206 * Public constructor. Window will be located at (0, 0).
71a389c9 207 *
d36057df
KL
208 * @param application TApplication that manages this window
209 * @param title window title, will be centered along the top border
210 * @param width width of window
211 * @param height height of window
71a389c9 212 */
d36057df
KL
213 public TWindow(final TApplication application, final String title,
214 final int width, final int height) {
215
216 this(application, title, 0, 0, width, height, RESIZABLE);
71a389c9
KL
217 }
218
219 /**
d36057df 220 * Public constructor. Window will be located at (0, 0).
71a389c9 221 *
d36057df
KL
222 * @param application TApplication that manages this window
223 * @param title window title, will be centered along the top border
224 * @param width width of window
225 * @param height height of window
226 * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
71a389c9 227 */
d36057df
KL
228 public TWindow(final TApplication application, final String title,
229 final int width, final int height, final int flags) {
71a389c9 230
d36057df 231 this(application, title, 0, 0, width, height, flags);
2ce6dab2
KL
232 }
233
234 /**
d36057df
KL
235 * Public constructor.
236 *
237 * @param application TApplication that manages this window
238 * @param title window title, will be centered along the top border
239 * @param x column relative to parent
240 * @param y row relative to parent
241 * @param width width of window
242 * @param height height of window
2ce6dab2 243 */
d36057df
KL
244 public TWindow(final TApplication application, final String title,
245 final int x, final int y, final int width, final int height) {
92453213 246
d36057df 247 this(application, title, x, y, width, height, RESIZABLE);
48e27807
KL
248 }
249
250 /**
251 * Public constructor.
252 *
253 * @param application TApplication that manages this window
254 * @param title window title, will be centered along the top border
255 * @param x column relative to parent
256 * @param y row relative to parent
257 * @param width width of window
258 * @param height height of window
259 * @param flags mask of RESIZABLE, CENTERED, or MODAL
260 */
261 public TWindow(final TApplication application, final String title,
262 final int x, final int y, final int width, final int height,
263 final int flags) {
264
fca67db0
KL
265 super();
266
48e27807 267 // I am my own window and parent
fca67db0
KL
268 setupForTWindow(this, x, y + application.getDesktopTop(),
269 width, height);
48e27807
KL
270
271 // Save fields
272 this.title = title;
273 this.application = application;
48e27807
KL
274 this.flags = flags;
275
276 // Minimum width/height are 10 and 2
277 assert (width >= 10);
fca67db0 278 assert (getHeight() >= 2);
48e27807
KL
279
280 // MODAL implies CENTERED
281 if (isModal()) {
282 this.flags |= CENTERED;
283 }
284
285 // Center window if specified
286 center();
287
288 // Add me to the application
d36057df 289 application.addWindowToApplication(this);
48e27807
KL
290 }
291
2ce6dab2 292 // ------------------------------------------------------------------------
d36057df 293 // Event handlers ---------------------------------------------------------
2ce6dab2 294 // ------------------------------------------------------------------------
48e27807 295
fe0770f9 296 /**
d36057df 297 * Returns true if the mouse is currently on the close button.
fe0770f9 298 *
d36057df 299 * @return true if mouse is currently on the close button
fe0770f9 300 */
d36057df
KL
301 protected boolean mouseOnClose() {
302 if ((flags & NOCLOSEBOX) != 0) {
303 return false;
304 }
305 if ((mouse != null)
306 && (mouse.getAbsoluteY() == getY())
307 && (mouse.getAbsoluteX() == getX() + 3)
308 ) {
fe0770f9
KL
309 return true;
310 }
311 return false;
312 }
313
314 /**
d36057df 315 * Returns true if the mouse is currently on the maximize/restore button.
48e27807 316 *
d36057df 317 * @return true if the mouse is currently on the maximize/restore button
48e27807 318 */
d36057df
KL
319 protected boolean mouseOnMaximize() {
320 if ((flags & NOZOOMBOX) != 0) {
48e27807
KL
321 return false;
322 }
d36057df
KL
323 if ((mouse != null)
324 && !isModal()
325 && (mouse.getAbsoluteY() == getY())
326 && (mouse.getAbsoluteX() == getX() + getWidth() - 4)
327 ) {
328 return true;
329 }
330 return false;
48e27807
KL
331 }
332
78a56d5d 333 /**
d36057df
KL
334 * Returns true if the mouse is currently on the resizable lower right
335 * corner.
78a56d5d 336 *
d36057df
KL
337 * @return true if the mouse is currently on the resizable lower right
338 * corner
78a56d5d 339 */
d36057df
KL
340 protected boolean mouseOnResize() {
341 if (((flags & RESIZABLE) != 0)
342 && !isModal()
343 && (mouse != null)
344 && (mouse.getAbsoluteY() == getY() + getHeight() - 1)
345 && ((mouse.getAbsoluteX() == getX() + getWidth() - 1)
346 || (mouse.getAbsoluteX() == getX() + getWidth() - 2))
347 ) {
78a56d5d
KL
348 return true;
349 }
350 return false;
351 }
352
a69ed767
KL
353 /**
354 * Subclasses should override this method to perform any user prompting
355 * before they are offscreen. Note that unlike other windowing toolkits,
356 * windows can NOT use this function in some manner to avoid being
357 * closed. This is called by application.closeWindow().
358 */
359 protected void onPreClose() {
360 // Default: do nothing.
361 }
362
68c5cd6b 363 /**
d36057df
KL
364 * Subclasses should override this method to cleanup resources. This is
365 * called by application.closeWindow().
68c5cd6b 366 */
a69ed767
KL
367 protected void onClose() {
368 // Default: perform widget-specific cleanup.
369 for (TWidget w: getChildren()) {
370 w.close();
371 }
68c5cd6b
KL
372 }
373
48e27807 374 /**
d36057df
KL
375 * Called by application.switchWindow() when this window gets the
376 * focus, and also by application.addWindow().
48e27807 377 */
a69ed767 378 protected void onFocus() {
d36057df 379 // Default: do nothing
48e27807
KL
380 }
381
382 /**
d36057df
KL
383 * Called by application.switchWindow() when another window gets the
384 * focus.
48e27807 385 */
a69ed767 386 protected void onUnfocus() {
d36057df 387 // Default: do nothing
48e27807
KL
388 }
389
390 /**
d36057df 391 * Called by application.hideWindow().
92453213 392 */
a69ed767 393 protected void onHide() {
92453213
KL
394 // Default: do nothing
395 }
396
397 /**
398 * Called by application.showWindow().
399 */
a69ed767 400 protected void onShow() {
92453213
KL
401 // Default: do nothing
402 }
403
48e27807
KL
404 /**
405 * Handle mouse button presses.
406 *
407 * @param mouse mouse button event
408 */
409 @Override
410 public void onMouseDown(final TMouseEvent mouse) {
411 this.mouse = mouse;
48e27807
KL
412
413 inKeyboardResize = false;
a69ed767
KL
414 inWindowMove = false;
415 inWindowResize = false;
48e27807 416
fca67db0 417 if ((mouse.getAbsoluteY() == getY())
7c870d89 418 && mouse.isMouse1()
fca67db0
KL
419 && (getX() <= mouse.getAbsoluteX())
420 && (mouse.getAbsoluteX() < getX() + getWidth())
48e27807
KL
421 && !mouseOnClose()
422 && !mouseOnMaximize()
423 ) {
424 // Begin moving window
425 inWindowMove = true;
426 moveWindowMouseX = mouse.getAbsoluteX();
427 moveWindowMouseY = mouse.getAbsoluteY();
fca67db0
KL
428 oldWindowX = getX();
429 oldWindowY = getY();
48e27807
KL
430 if (maximized) {
431 maximized = false;
432 }
433 return;
434 }
435 if (mouseOnResize()) {
436 // Begin window resize
437 inWindowResize = true;
438 moveWindowMouseX = mouse.getAbsoluteX();
439 moveWindowMouseY = mouse.getAbsoluteY();
fca67db0
KL
440 resizeWindowWidth = getWidth();
441 resizeWindowHeight = getHeight();
48e27807
KL
442 if (maximized) {
443 maximized = false;
444 }
445 return;
446 }
447
2ce6dab2
KL
448 // Give the shortcut bar a shot at this.
449 if (statusBar != null) {
450 if (statusBar.statusBarMouseDown(mouse)) {
451 return;
452 }
453 }
454
48e27807
KL
455 // I didn't take it, pass it on to my children
456 super.onMouseDown(mouse);
457 }
458
48e27807
KL
459 /**
460 * Handle mouse button releases.
461 *
462 * @param mouse mouse button release event
463 */
464 @Override
465 public void onMouseUp(final TMouseEvent mouse) {
466 this.mouse = mouse;
48e27807 467
7c870d89 468 if ((inWindowMove) && (mouse.isMouse1())) {
48e27807
KL
469 // Stop moving window
470 inWindowMove = false;
471 return;
472 }
473
7c870d89 474 if ((inWindowResize) && (mouse.isMouse1())) {
48e27807
KL
475 // Stop resizing window
476 inWindowResize = false;
477 return;
478 }
479
7c870d89 480 if (mouse.isMouse1() && mouseOnClose()) {
d36057df
KL
481 if ((flags & HIDEONCLOSE) == 0) {
482 // Close window
483 application.closeWindow(this);
484 } else {
485 // Hide window
486 application.hideWindow(this);
487 }
48e27807
KL
488 return;
489 }
490
fca67db0 491 if ((mouse.getAbsoluteY() == getY())
7c870d89 492 && mouse.isMouse1()
48e27807
KL
493 && mouseOnMaximize()) {
494 if (maximized) {
495 // Restore
496 restore();
497 } else {
498 // Maximize
499 maximize();
500 }
501 // Pass a resize event to my children
fca67db0
KL
502 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
503 getWidth(), getHeight()));
48e27807
KL
504 return;
505 }
506
2ce6dab2
KL
507 // Give the shortcut bar a shot at this.
508 if (statusBar != null) {
509 if (statusBar.statusBarMouseUp(mouse)) {
510 return;
511 }
512 }
513
48e27807
KL
514 // I didn't take it, pass it on to my children
515 super.onMouseUp(mouse);
516 }
517
518 /**
519 * Handle mouse movements.
520 *
521 * @param mouse mouse motion event
522 */
523 @Override
524 public void onMouseMotion(final TMouseEvent mouse) {
525 this.mouse = mouse;
48e27807
KL
526
527 if (inWindowMove) {
528 // Move window over
fca67db0
KL
529 setX(oldWindowX + (mouse.getAbsoluteX() - moveWindowMouseX));
530 setY(oldWindowY + (mouse.getAbsoluteY() - moveWindowMouseY));
48e27807 531 // Don't cover up the menu bar
fca67db0
KL
532 if (getY() < application.getDesktopTop()) {
533 setY(application.getDesktopTop());
48e27807 534 }
2ce6dab2
KL
535 // Don't go below the status bar
536 if (getY() >= application.getDesktopBottom()) {
537 setY(application.getDesktopBottom() - 1);
538 }
48e27807
KL
539 return;
540 }
541
542 if (inWindowResize) {
12b55d76
KL
543 // Do not permit resizing below the status line
544 if (mouse.getAbsoluteY() == application.getDesktopBottom()) {
545 inWindowResize = false;
546 return;
547 }
548
48e27807 549 // Move window over
fca67db0
KL
550 setWidth(resizeWindowWidth + (mouse.getAbsoluteX()
551 - moveWindowMouseX));
552 setHeight(resizeWindowHeight + (mouse.getAbsoluteY()
553 - moveWindowMouseY));
554 if (getX() + getWidth() > getScreen().getWidth()) {
555 setWidth(getScreen().getWidth() - getX());
48e27807 556 }
fca67db0
KL
557 if (getY() + getHeight() > application.getDesktopBottom()) {
558 setY(application.getDesktopBottom() - getHeight() + 1);
48e27807
KL
559 }
560 // Don't cover up the menu bar
fca67db0
KL
561 if (getY() < application.getDesktopTop()) {
562 setY(application.getDesktopTop());
48e27807
KL
563 }
564
565 // Keep within min/max bounds
fca67db0
KL
566 if (getWidth() < minimumWindowWidth) {
567 setWidth(minimumWindowWidth);
48e27807
KL
568 inWindowResize = false;
569 }
fca67db0
KL
570 if (getHeight() < minimumWindowHeight) {
571 setHeight(minimumWindowHeight);
48e27807
KL
572 inWindowResize = false;
573 }
fca67db0
KL
574 if ((maximumWindowWidth > 0)
575 && (getWidth() > maximumWindowWidth)
576 ) {
577 setWidth(maximumWindowWidth);
48e27807
KL
578 inWindowResize = false;
579 }
fca67db0
KL
580 if ((maximumWindowHeight > 0)
581 && (getHeight() > maximumWindowHeight)
582 ) {
583 setHeight(maximumWindowHeight);
48e27807
KL
584 inWindowResize = false;
585 }
586
587 // Pass a resize event to my children
fca67db0
KL
588 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
589 getWidth(), getHeight()));
48e27807
KL
590 return;
591 }
592
2ce6dab2
KL
593 // Give the shortcut bar a shot at this.
594 if (statusBar != null) {
595 statusBar.statusBarMouseMotion(mouse);
596 }
597
48e27807
KL
598 // I didn't take it, pass it on to my children
599 super.onMouseMotion(mouse);
600 }
601
602 /**
603 * Handle keystrokes.
604 *
605 * @param keypress keystroke event
606 */
607 @Override
608 public void onKeypress(final TKeypressEvent keypress) {
609
610 if (inKeyboardResize) {
611
32437017
KL
612 // ESC or ENTER - Exit size/move
613 if (keypress.equals(kbEsc) || keypress.equals(kbEnter)) {
48e27807
KL
614 inKeyboardResize = false;
615 }
616
617 if (keypress.equals(kbLeft)) {
fca67db0
KL
618 if (getX() > 0) {
619 setX(getX() - 1);
48e27807
KL
620 }
621 }
622 if (keypress.equals(kbRight)) {
fca67db0
KL
623 if (getX() < getScreen().getWidth() - 1) {
624 setX(getX() + 1);
48e27807
KL
625 }
626 }
627 if (keypress.equals(kbDown)) {
fca67db0
KL
628 if (getY() < application.getDesktopBottom() - 1) {
629 setY(getY() + 1);
48e27807
KL
630 }
631 }
632 if (keypress.equals(kbUp)) {
fca67db0
KL
633 if (getY() > 1) {
634 setY(getY() - 1);
48e27807
KL
635 }
636 }
3b0a5f8b
KL
637
638 /*
639 * Only permit keyboard resizing if the window was RESIZABLE.
640 */
641 if ((flags & RESIZABLE) != 0) {
642
643 if (keypress.equals(kbShiftLeft)) {
644 if ((getWidth() > minimumWindowWidth)
645 || (minimumWindowWidth <= 0)
646 ) {
647 setWidth(getWidth() - 1);
648 }
48e27807 649 }
3b0a5f8b
KL
650 if (keypress.equals(kbShiftRight)) {
651 if ((getWidth() < maximumWindowWidth)
652 || (maximumWindowWidth <= 0)
653 ) {
654 setWidth(getWidth() + 1);
655 }
48e27807 656 }
3b0a5f8b
KL
657 if (keypress.equals(kbShiftUp)) {
658 if ((getHeight() > minimumWindowHeight)
659 || (minimumWindowHeight <= 0)
660 ) {
661 setHeight(getHeight() - 1);
662 }
48e27807 663 }
3b0a5f8b
KL
664 if (keypress.equals(kbShiftDown)) {
665 if ((getHeight() < maximumWindowHeight)
666 || (maximumWindowHeight <= 0)
667 ) {
668 setHeight(getHeight() + 1);
669 }
48e27807 670 }
48e27807 671
3b0a5f8b
KL
672 // Pass a resize event to my children
673 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
674 getWidth(), getHeight()));
675
676 } // if ((flags & RESIZABLE) != 0)
0d47c546 677
48e27807
KL
678 return;
679 }
680
2ce6dab2
KL
681 // Give the shortcut bar a shot at this.
682 if (statusBar != null) {
683 if (statusBar.statusBarKeypress(keypress)) {
684 return;
685 }
686 }
687
48e27807
KL
688 // These keystrokes will typically not be seen unless a subclass
689 // overrides onMenu() due to how TApplication dispatches
690 // accelerators.
691
92453213 692 if (!(this instanceof TDesktop)) {
48e27807 693
92453213
KL
694 // Ctrl-W - close window
695 if (keypress.equals(kbCtrlW)) {
78a56d5d 696 if ((flags & NOCLOSEBOX) == 0) {
d36057df
KL
697 if ((flags & HIDEONCLOSE) == 0) {
698 // Close window
699 application.closeWindow(this);
700 } else {
701 // Hide window
702 application.hideWindow(this);
703 }
78a56d5d 704 }
92453213
KL
705 return;
706 }
48e27807 707
92453213
KL
708 // F6 - behave like Alt-TAB
709 if (keypress.equals(kbF6)) {
710 application.switchWindow(true);
711 return;
712 }
48e27807 713
92453213
KL
714 // Shift-F6 - behave like Shift-Alt-TAB
715 if (keypress.equals(kbShiftF6)) {
716 application.switchWindow(false);
717 return;
48e27807 718 }
48e27807 719
92453213 720 // F5 - zoom
68c5cd6b 721 if (keypress.equals(kbF5) && ((flags & NOZOOMBOX) == 0)) {
92453213
KL
722 if (maximized) {
723 restore();
724 } else {
725 maximize();
726 }
727 }
728
729 // Ctrl-F5 - size/move
730 if (keypress.equals(kbCtrlF5)) {
731 inKeyboardResize = !inKeyboardResize;
732 }
733
734 } // if (!(this instanceof TDesktop))
48e27807
KL
735
736 // I didn't take it, pass it on to my children
737 super.onKeypress(keypress);
738 }
739
740 /**
741 * Handle posted command events.
742 *
743 * @param command command event
744 */
745 @Override
746 public void onCommand(final TCommandEvent command) {
747
748 // These commands will typically not be seen unless a subclass
749 // overrides onMenu() due to how TApplication dispatches
750 // accelerators.
751
92453213 752 if (!(this instanceof TDesktop)) {
48e27807 753
92453213 754 if (command.equals(cmWindowClose)) {
78a56d5d 755 if ((flags & NOCLOSEBOX) == 0) {
d36057df
KL
756 if ((flags & HIDEONCLOSE) == 0) {
757 // Close window
758 application.closeWindow(this);
759 } else {
760 // Hide window
761 application.hideWindow(this);
762 }
78a56d5d 763 }
92453213
KL
764 return;
765 }
48e27807 766
92453213
KL
767 if (command.equals(cmWindowNext)) {
768 application.switchWindow(true);
769 return;
770 }
48e27807 771
92453213
KL
772 if (command.equals(cmWindowPrevious)) {
773 application.switchWindow(false);
774 return;
775 }
48e27807 776
92453213
KL
777 if (command.equals(cmWindowMove)) {
778 inKeyboardResize = true;
779 return;
780 }
781
68c5cd6b 782 if (command.equals(cmWindowZoom) && ((flags & NOZOOMBOX) == 0)) {
92453213
KL
783 if (maximized) {
784 restore();
785 } else {
786 maximize();
787 }
48e27807 788 }
92453213
KL
789
790 } // if (!(this instanceof TDesktop))
48e27807
KL
791
792 // I didn't take it, pass it on to my children
793 super.onCommand(command);
794 }
795
796 /**
797 * Handle posted menu events.
798 *
799 * @param menu menu event
800 */
801 @Override
802 public void onMenu(final TMenuEvent menu) {
48e27807 803
92453213 804 if (!(this instanceof TDesktop)) {
48e27807 805
92453213 806 if (menu.getId() == TMenu.MID_WINDOW_CLOSE) {
78a56d5d 807 if ((flags & NOCLOSEBOX) == 0) {
d36057df
KL
808 if ((flags & HIDEONCLOSE) == 0) {
809 // Close window
810 application.closeWindow(this);
811 } else {
812 // Hide window
813 application.hideWindow(this);
814 }
78a56d5d 815 }
92453213
KL
816 return;
817 }
48e27807 818
92453213
KL
819 if (menu.getId() == TMenu.MID_WINDOW_NEXT) {
820 application.switchWindow(true);
821 return;
822 }
48e27807 823
92453213
KL
824 if (menu.getId() == TMenu.MID_WINDOW_PREVIOUS) {
825 application.switchWindow(false);
826 return;
48e27807 827 }
92453213
KL
828
829 if (menu.getId() == TMenu.MID_WINDOW_MOVE) {
830 inKeyboardResize = true;
831 return;
832 }
833
68c5cd6b
KL
834 if ((menu.getId() == TMenu.MID_WINDOW_ZOOM)
835 && ((flags & NOZOOMBOX) == 0)
836 ) {
92453213
KL
837 if (maximized) {
838 restore();
839 } else {
840 maximize();
841 }
842 return;
843 }
844
845 } // if (!(this instanceof TDesktop))
48e27807
KL
846
847 // I didn't take it, pass it on to my children
848 super.onMenu(menu);
849 }
850
d36057df
KL
851 // ------------------------------------------------------------------------
852 // TWidget ----------------------------------------------------------------
853 // ------------------------------------------------------------------------
854
855 /**
856 * Get this TWindow's parent TApplication.
857 *
858 * @return this TWindow's parent TApplication
859 */
860 @Override
861 public final TApplication getApplication() {
862 return application;
863 }
864
865 /**
866 * Get the Screen.
867 *
868 * @return the Screen
869 */
870 @Override
871 public final Screen getScreen() {
872 return application.getScreen();
873 }
874
875 /**
876 * Called by TApplication.drawChildren() to render on screen.
877 */
878 @Override
879 public void draw() {
880 // Draw the box and background first.
881 CellAttributes border = getBorder();
882 CellAttributes background = getBackground();
883 int borderType = getBorderType();
884
a69ed767
KL
885 drawBox(0, 0, getWidth(), getHeight(), border, background, borderType,
886 true);
d36057df
KL
887
888 // Draw the title
889 int titleLeft = (getWidth() - title.length() - 2) / 2;
890 putCharXY(titleLeft, 0, ' ', border);
891 putStringXY(titleLeft + 1, 0, title);
892 putCharXY(titleLeft + title.length() + 1, 0, ' ', border);
893
894 if (isActive()) {
895
896 // Draw the close button
897 if ((flags & NOCLOSEBOX) == 0) {
898 putCharXY(2, 0, '[', border);
899 putCharXY(4, 0, ']', border);
900 if (mouseOnClose() && mouse.isMouse1()) {
901 putCharXY(3, 0, GraphicsChars.CP437[0x0F],
902 getBorderControls());
903 } else {
904 putCharXY(3, 0, GraphicsChars.CP437[0xFE],
905 getBorderControls());
906 }
907 }
908
909 // Draw the maximize button
910 if (!isModal() && ((flags & NOZOOMBOX) == 0)) {
911
912 putCharXY(getWidth() - 5, 0, '[', border);
913 putCharXY(getWidth() - 3, 0, ']', border);
914 if (mouseOnMaximize() && mouse.isMouse1()) {
915 putCharXY(getWidth() - 4, 0, GraphicsChars.CP437[0x0F],
916 getBorderControls());
917 } else {
918 if (maximized) {
919 putCharXY(getWidth() - 4, 0, GraphicsChars.CP437[0x12],
920 getBorderControls());
921 } else {
922 putCharXY(getWidth() - 4, 0, GraphicsChars.UPARROW,
923 getBorderControls());
924 }
925 }
926
927 // Draw the resize corner
928 if ((flags & RESIZABLE) != 0) {
929 putCharXY(getWidth() - 2, getHeight() - 1,
930 GraphicsChars.SINGLE_BAR, getBorderControls());
931 putCharXY(getWidth() - 1, getHeight() - 1,
932 GraphicsChars.LRCORNER, getBorderControls());
933 }
934 }
935 }
936 }
937
938 // ------------------------------------------------------------------------
939 // TWindow ----------------------------------------------------------------
940 // ------------------------------------------------------------------------
941
942 /**
943 * Get window title.
944 *
945 * @return window title
946 */
947 public final String getTitle() {
948 return title;
949 }
950
951 /**
952 * Set window title.
953 *
954 * @param title new window title
955 */
956 public final void setTitle(final String title) {
957 this.title = title;
958 }
959
960 /**
961 * Get Z order. Lower number means more in-front.
962 *
963 * @return Z value. Lower number means more in-front.
964 */
965 public final int getZ() {
966 return z;
967 }
968
969 /**
970 * Set Z order. Lower number means more in-front.
971 *
972 * @param z the new Z value. Lower number means more in-front.
973 */
974 public final void setZ(final int z) {
975 this.z = z;
976 }
977
978 /**
979 * Add a keypress to be overridden for this window.
980 *
981 * @param key the key to start taking control of
982 */
983 protected void addShortcutKeypress(final TKeypress key) {
984 keyboardShortcuts.add(key);
985 }
986
987 /**
988 * Remove a keypress to be overridden for this window.
989 *
990 * @param key the key to stop taking control of
991 */
992 protected void removeShortcutKeypress(final TKeypress key) {
993 keyboardShortcuts.remove(key);
994 }
995
996 /**
997 * Remove all keypresses to be overridden for this window.
998 */
999 protected void clearShortcutKeypresses() {
1000 keyboardShortcuts.clear();
1001 }
1002
1003 /**
1004 * Determine if a keypress is overridden for this window.
1005 *
1006 * @param key the key to check
1007 * @return true if this window wants to process this key on its own
1008 */
1009 public boolean isShortcutKeypress(final TKeypress key) {
1010 return keyboardShortcuts.contains(key);
1011 }
1012
1013 /**
1014 * Get the window's status bar, or null if it does not have one.
1015 *
1016 * @return the status bar, or null
1017 */
1018 public TStatusBar getStatusBar() {
1019 return statusBar;
1020 }
1021
1022 /**
1023 * Set the window's status bar to a new one.
1024 *
1025 * @param text the status bar text
1026 * @return the status bar
1027 */
1028 public TStatusBar newStatusBar(final String text) {
1029 statusBar = new TStatusBar(this, text);
1030 return statusBar;
1031 }
1032
1033 /**
1034 * Set the maximum width for this window.
1035 *
1036 * @param maximumWindowWidth new maximum width
1037 */
1038 public final void setMaximumWindowWidth(final int maximumWindowWidth) {
1039 if ((maximumWindowWidth != -1)
1040 && (maximumWindowWidth < minimumWindowWidth + 1)
1041 ) {
1042 throw new IllegalArgumentException("Maximum window width cannot " +
1043 "be smaller than minimum window width + 1");
1044 }
1045 this.maximumWindowWidth = maximumWindowWidth;
1046 }
1047
1048 /**
1049 * Set the minimum width for this window.
1050 *
1051 * @param minimumWindowWidth new minimum width
1052 */
1053 public final void setMinimumWindowWidth(final int minimumWindowWidth) {
1054 if ((maximumWindowWidth != -1)
1055 && (minimumWindowWidth > maximumWindowWidth - 1)
1056 ) {
1057 throw new IllegalArgumentException("Minimum window width cannot " +
1058 "be larger than maximum window width - 1");
1059 }
1060 this.minimumWindowWidth = minimumWindowWidth;
1061 }
1062
1063 /**
1064 * Set the maximum height for this window.
1065 *
1066 * @param maximumWindowHeight new maximum height
1067 */
1068 public final void setMaximumWindowHeight(final int maximumWindowHeight) {
1069 if ((maximumWindowHeight != -1)
1070 && (maximumWindowHeight < minimumWindowHeight + 1)
1071 ) {
1072 throw new IllegalArgumentException("Maximum window height cannot " +
1073 "be smaller than minimum window height + 1");
1074 }
1075 this.maximumWindowHeight = maximumWindowHeight;
1076 }
1077
1078 /**
1079 * Set the minimum height for this window.
1080 *
1081 * @param minimumWindowHeight new minimum height
1082 */
1083 public final void setMinimumWindowHeight(final int minimumWindowHeight) {
1084 if ((maximumWindowHeight != -1)
1085 && (minimumWindowHeight > maximumWindowHeight - 1)
1086 ) {
1087 throw new IllegalArgumentException("Minimum window height cannot " +
1088 "be larger than maximum window height - 1");
1089 }
1090 this.minimumWindowHeight = minimumWindowHeight;
1091 }
1092
1093 /**
1094 * Recenter the window on-screen.
1095 */
1096 public final void center() {
1097 if ((flags & CENTERED) != 0) {
1098 if (getWidth() < getScreen().getWidth()) {
1099 setX((getScreen().getWidth() - getWidth()) / 2);
1100 } else {
1101 setX(0);
1102 }
1103 setY(((application.getDesktopBottom()
1104 - application.getDesktopTop()) - getHeight()) / 2);
1105 if (getY() < 0) {
1106 setY(0);
1107 }
1108 setY(getY() + application.getDesktopTop());
1109 }
1110 }
1111
1112 /**
1113 * Maximize window.
1114 */
1115 public void maximize() {
1116 if (maximized) {
1117 return;
1118 }
1119
1120 restoreWindowWidth = getWidth();
1121 restoreWindowHeight = getHeight();
1122 restoreWindowX = getX();
1123 restoreWindowY = getY();
1124 setWidth(getScreen().getWidth());
1125 setHeight(application.getDesktopBottom() - 1);
1126 setX(0);
1127 setY(1);
1128 maximized = true;
1129
1130 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(),
1131 getHeight()));
1132 }
1133
1134 /**
1135 * Restore (unmaximize) window.
1136 */
1137 public void restore() {
1138 if (!maximized) {
1139 return;
1140 }
1141
1142 setWidth(restoreWindowWidth);
1143 setHeight(restoreWindowHeight);
1144 setX(restoreWindowX);
1145 setY(restoreWindowY);
1146 maximized = false;
1147
1148 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(),
1149 getHeight()));
1150 }
1151
1152 /**
1153 * Returns true if this window is hidden.
1154 *
1155 * @return true if this window is hidden, false if the window is shown
1156 */
1157 public final boolean isHidden() {
1158 return hidden;
1159 }
1160
1161 /**
1162 * Returns true if this window is shown.
1163 *
1164 * @return true if this window is shown, false if the window is hidden
1165 */
1166 public final boolean isShown() {
1167 return !hidden;
1168 }
1169
1170 /**
1171 * Hide window. A hidden window will still have its onIdle() called, and
1172 * will also have onClose() called at application exit. Hidden windows
1173 * will not receive any other events.
1174 */
1175 public void hide() {
1176 application.hideWindow(this);
1177 }
1178
1179 /**
1180 * Show window.
1181 */
1182 public void show() {
1183 application.showWindow(this);
1184 }
1185
1186 /**
1187 * Activate window (bring to top and receive events).
1188 */
5218e73c 1189 @Override
d36057df
KL
1190 public void activate() {
1191 application.activateWindow(this);
1192 }
1193
1194 /**
1195 * Close window. Note that windows without a close box can still be
1196 * closed by calling the close() method.
1197 */
c88c4ced 1198 @Override
d36057df
KL
1199 public void close() {
1200 application.closeWindow(this);
1201 }
1202
1203 /**
1204 * See if this window is undergoing any movement/resize/etc.
1205 *
1206 * @return true if the window is moving
1207 */
1208 public boolean inMovements() {
1209 if (inWindowResize || inWindowMove || inKeyboardResize) {
1210 return true;
1211 }
1212 return false;
1213 }
1214
1215 /**
1216 * Stop any pending movement/resize/etc.
1217 */
1218 public void stopMovements() {
1219 inWindowResize = false;
1220 inWindowMove = false;
1221 inKeyboardResize = false;
1222 }
1223
1224 /**
1225 * Returns true if this window is modal.
1226 *
1227 * @return true if this window is modal
1228 */
1229 public final boolean isModal() {
1230 if ((flags & MODAL) == 0) {
1231 return false;
1232 }
1233 return true;
1234 }
1235
1236 /**
1237 * Returns true if this window has a close box.
1238 *
1239 * @return true if this window has a close box
1240 */
1241 public final boolean hasCloseBox() {
1242 if ((flags & NOCLOSEBOX) != 0) {
1243 return true;
1244 }
1245 return false;
1246 }
1247
1248 /**
1249 * Returns true if this window has a maximize/zoom box.
1250 *
1251 * @return true if this window has a maximize/zoom box
1252 */
1253 public final boolean hasZoomBox() {
1254 if ((flags & NOZOOMBOX) != 0) {
1255 return true;
9696a8f6
KL
1256 }
1257 return false;
1258 }
1259
1260 /**
1261 * Returns true if this window does not want menus to work while it is
1262 * visible.
1263 *
1264 * @return true if this window does not want menus to work while it is
1265 * visible
1266 */
1267 public final boolean hasOverriddenMenu() {
1268 if ((flags & OVERRIDEMENU) != 0) {
1269 return true;
d36057df
KL
1270 }
1271 return false;
1272 }
1273
1274 /**
1275 * Retrieve the background color.
1276 *
1277 * @return the background color
1278 */
1279 public CellAttributes getBackground() {
1280 if (!isModal()
1281 && (inWindowMove || inWindowResize || inKeyboardResize)
1282 ) {
1283 assert (isActive());
1284 return getTheme().getColor("twindow.background.windowmove");
1285 } else if (isModal() && inWindowMove) {
1286 assert (isActive());
1287 return getTheme().getColor("twindow.background.modal");
1288 } else if (isModal()) {
1289 if (isActive()) {
1290 return getTheme().getColor("twindow.background.modal");
1291 }
1292 return getTheme().getColor("twindow.background.modal.inactive");
1293 } else if (isActive()) {
1294 assert (!isModal());
1295 return getTheme().getColor("twindow.background");
1296 } else {
1297 assert (!isModal());
1298 return getTheme().getColor("twindow.background.inactive");
1299 }
1300 }
1301
1302 /**
1303 * Retrieve the border color.
1304 *
1305 * @return the border color
1306 */
1307 public CellAttributes getBorder() {
1308 if (!isModal()
1309 && (inWindowMove || inWindowResize || inKeyboardResize)
1310 ) {
a69ed767
KL
1311 if (!isActive()) {
1312 // The user's terminal never passed a mouse up event, and now
1313 // another window is active but we never finished a drag.
1314 inWindowMove = false;
1315 inWindowResize = false;
1316 inKeyboardResize = false;
1317 return getTheme().getColor("twindow.border.inactive");
1318 }
1319
d36057df
KL
1320 return getTheme().getColor("twindow.border.windowmove");
1321 } else if (isModal() && inWindowMove) {
1322 assert (isActive());
1323 return getTheme().getColor("twindow.border.modal.windowmove");
1324 } else if (isModal()) {
1325 if (isActive()) {
1326 return getTheme().getColor("twindow.border.modal");
1327 } else {
1328 return getTheme().getColor("twindow.border.modal.inactive");
1329 }
1330 } else if (isActive()) {
1331 assert (!isModal());
1332 return getTheme().getColor("twindow.border");
1333 } else {
1334 assert (!isModal());
1335 return getTheme().getColor("twindow.border.inactive");
1336 }
1337 }
1338
1339 /**
1340 * Retrieve the color used by the window movement/sizing controls.
1341 *
1342 * @return the color used by the zoom box, resize bar, and close box
1343 */
1344 public CellAttributes getBorderControls() {
1345 if (isModal()) {
1346 return getTheme().getColor("twindow.border.modal.windowmove");
1347 }
1348 return getTheme().getColor("twindow.border.windowmove");
1349 }
1350
1351 /**
1352 * Retrieve the border line type.
1353 *
1354 * @return the border line type
1355 */
1356 private int getBorderType() {
1357 if (!isModal()
1358 && (inWindowMove || inWindowResize || inKeyboardResize)
1359 ) {
1360 assert (isActive());
1361 return 1;
1362 } else if (isModal() && inWindowMove) {
1363 assert (isActive());
1364 return 1;
1365 } else if (isModal()) {
1366 if (isActive()) {
1367 return 2;
1368 } else {
1369 return 1;
1370 }
1371 } else if (isActive()) {
1372 return 2;
1373 } else {
1374 return 1;
1375 }
1376 }
1377
978a5d8f
KL
1378 /**
1379 * Returns true if this window does not want the application-wide mouse
1380 * cursor drawn over it.
1381 *
1382 * @return true if this window does not want the application-wide mouse
1383 * cursor drawn over it
1384 */
1385 public final boolean hasHiddenMouse() {
1386 return hideMouse;
1387 }
1388
1389 /**
1390 * Set request to prevent the application-wide mouse cursor from being
1391 * drawn over this window.
1392 *
1393 * @param hideMouse if true, this window does not want the
1394 * application-wide mouse cursor drawn over it
1395 */
1396 public final void setHiddenMouse(final boolean hideMouse) {
1397 this.hideMouse = hideMouse;
1398 }
1399
48e27807 1400}