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