update README
[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.Cell;
36import jexer.bits.CellAttributes;
37import jexer.bits.GraphicsChars;
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) {
12b55d76
KL
544 // Do not permit resizing below the status line
545 if (mouse.getAbsoluteY() == application.getDesktopBottom()) {
546 inWindowResize = false;
547 return;
548 }
549
48e27807 550 // Move window over
fca67db0
KL
551 setWidth(resizeWindowWidth + (mouse.getAbsoluteX()
552 - moveWindowMouseX));
553 setHeight(resizeWindowHeight + (mouse.getAbsoluteY()
554 - moveWindowMouseY));
555 if (getX() + getWidth() > getScreen().getWidth()) {
556 setWidth(getScreen().getWidth() - getX());
48e27807 557 }
fca67db0
KL
558 if (getY() + getHeight() > application.getDesktopBottom()) {
559 setY(application.getDesktopBottom() - getHeight() + 1);
48e27807
KL
560 }
561 // Don't cover up the menu bar
fca67db0
KL
562 if (getY() < application.getDesktopTop()) {
563 setY(application.getDesktopTop());
48e27807
KL
564 }
565
566 // Keep within min/max bounds
fca67db0
KL
567 if (getWidth() < minimumWindowWidth) {
568 setWidth(minimumWindowWidth);
48e27807
KL
569 inWindowResize = false;
570 }
fca67db0
KL
571 if (getHeight() < minimumWindowHeight) {
572 setHeight(minimumWindowHeight);
48e27807
KL
573 inWindowResize = false;
574 }
fca67db0
KL
575 if ((maximumWindowWidth > 0)
576 && (getWidth() > maximumWindowWidth)
577 ) {
578 setWidth(maximumWindowWidth);
48e27807
KL
579 inWindowResize = false;
580 }
fca67db0
KL
581 if ((maximumWindowHeight > 0)
582 && (getHeight() > maximumWindowHeight)
583 ) {
584 setHeight(maximumWindowHeight);
48e27807
KL
585 inWindowResize = false;
586 }
587
588 // Pass a resize event to my children
fca67db0
KL
589 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
590 getWidth(), getHeight()));
48e27807
KL
591 return;
592 }
593
2ce6dab2
KL
594 // Give the shortcut bar a shot at this.
595 if (statusBar != null) {
596 statusBar.statusBarMouseMotion(mouse);
597 }
598
48e27807
KL
599 // I didn't take it, pass it on to my children
600 super.onMouseMotion(mouse);
601 }
602
603 /**
604 * Handle keystrokes.
605 *
606 * @param keypress keystroke event
607 */
608 @Override
609 public void onKeypress(final TKeypressEvent keypress) {
610
611 if (inKeyboardResize) {
612
32437017
KL
613 // ESC or ENTER - Exit size/move
614 if (keypress.equals(kbEsc) || keypress.equals(kbEnter)) {
48e27807
KL
615 inKeyboardResize = false;
616 }
617
618 if (keypress.equals(kbLeft)) {
fca67db0
KL
619 if (getX() > 0) {
620 setX(getX() - 1);
48e27807
KL
621 }
622 }
623 if (keypress.equals(kbRight)) {
fca67db0
KL
624 if (getX() < getScreen().getWidth() - 1) {
625 setX(getX() + 1);
48e27807
KL
626 }
627 }
628 if (keypress.equals(kbDown)) {
fca67db0
KL
629 if (getY() < application.getDesktopBottom() - 1) {
630 setY(getY() + 1);
48e27807
KL
631 }
632 }
633 if (keypress.equals(kbUp)) {
fca67db0
KL
634 if (getY() > 1) {
635 setY(getY() - 1);
48e27807
KL
636 }
637 }
3b0a5f8b
KL
638
639 /*
640 * Only permit keyboard resizing if the window was RESIZABLE.
641 */
642 if ((flags & RESIZABLE) != 0) {
643
644 if (keypress.equals(kbShiftLeft)) {
645 if ((getWidth() > minimumWindowWidth)
646 || (minimumWindowWidth <= 0)
647 ) {
648 setWidth(getWidth() - 1);
649 }
48e27807 650 }
3b0a5f8b
KL
651 if (keypress.equals(kbShiftRight)) {
652 if ((getWidth() < maximumWindowWidth)
653 || (maximumWindowWidth <= 0)
654 ) {
655 setWidth(getWidth() + 1);
656 }
48e27807 657 }
3b0a5f8b
KL
658 if (keypress.equals(kbShiftUp)) {
659 if ((getHeight() > minimumWindowHeight)
660 || (minimumWindowHeight <= 0)
661 ) {
662 setHeight(getHeight() - 1);
663 }
48e27807 664 }
3b0a5f8b
KL
665 if (keypress.equals(kbShiftDown)) {
666 if ((getHeight() < maximumWindowHeight)
667 || (maximumWindowHeight <= 0)
668 ) {
669 setHeight(getHeight() + 1);
670 }
48e27807 671 }
48e27807 672
3b0a5f8b
KL
673 // Pass a resize event to my children
674 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
675 getWidth(), getHeight()));
676
677 } // if ((flags & RESIZABLE) != 0)
0d47c546 678
48e27807
KL
679 return;
680 }
681
2ce6dab2
KL
682 // Give the shortcut bar a shot at this.
683 if (statusBar != null) {
684 if (statusBar.statusBarKeypress(keypress)) {
685 return;
686 }
687 }
688
48e27807
KL
689 // These keystrokes will typically not be seen unless a subclass
690 // overrides onMenu() due to how TApplication dispatches
691 // accelerators.
692
92453213 693 if (!(this instanceof TDesktop)) {
48e27807 694
92453213
KL
695 // Ctrl-W - close window
696 if (keypress.equals(kbCtrlW)) {
78a56d5d 697 if ((flags & NOCLOSEBOX) == 0) {
d36057df
KL
698 if ((flags & HIDEONCLOSE) == 0) {
699 // Close window
700 application.closeWindow(this);
701 } else {
702 // Hide window
703 application.hideWindow(this);
704 }
78a56d5d 705 }
92453213
KL
706 return;
707 }
48e27807 708
92453213
KL
709 // F6 - behave like Alt-TAB
710 if (keypress.equals(kbF6)) {
711 application.switchWindow(true);
712 return;
713 }
48e27807 714
92453213
KL
715 // Shift-F6 - behave like Shift-Alt-TAB
716 if (keypress.equals(kbShiftF6)) {
717 application.switchWindow(false);
718 return;
48e27807 719 }
48e27807 720
92453213 721 // F5 - zoom
68c5cd6b 722 if (keypress.equals(kbF5) && ((flags & NOZOOMBOX) == 0)) {
92453213
KL
723 if (maximized) {
724 restore();
725 } else {
726 maximize();
727 }
728 }
729
730 // Ctrl-F5 - size/move
731 if (keypress.equals(kbCtrlF5)) {
732 inKeyboardResize = !inKeyboardResize;
733 }
734
735 } // if (!(this instanceof TDesktop))
48e27807
KL
736
737 // I didn't take it, pass it on to my children
738 super.onKeypress(keypress);
739 }
740
741 /**
742 * Handle posted command events.
743 *
744 * @param command command event
745 */
746 @Override
747 public void onCommand(final TCommandEvent command) {
748
749 // These commands will typically not be seen unless a subclass
750 // overrides onMenu() due to how TApplication dispatches
751 // accelerators.
752
92453213 753 if (!(this instanceof TDesktop)) {
48e27807 754
92453213 755 if (command.equals(cmWindowClose)) {
78a56d5d 756 if ((flags & NOCLOSEBOX) == 0) {
d36057df
KL
757 if ((flags & HIDEONCLOSE) == 0) {
758 // Close window
759 application.closeWindow(this);
760 } else {
761 // Hide window
762 application.hideWindow(this);
763 }
78a56d5d 764 }
92453213
KL
765 return;
766 }
48e27807 767
92453213
KL
768 if (command.equals(cmWindowNext)) {
769 application.switchWindow(true);
770 return;
771 }
48e27807 772
92453213
KL
773 if (command.equals(cmWindowPrevious)) {
774 application.switchWindow(false);
775 return;
776 }
48e27807 777
92453213
KL
778 if (command.equals(cmWindowMove)) {
779 inKeyboardResize = true;
780 return;
781 }
782
68c5cd6b 783 if (command.equals(cmWindowZoom) && ((flags & NOZOOMBOX) == 0)) {
92453213
KL
784 if (maximized) {
785 restore();
786 } else {
787 maximize();
788 }
48e27807 789 }
92453213
KL
790
791 } // if (!(this instanceof TDesktop))
48e27807
KL
792
793 // I didn't take it, pass it on to my children
794 super.onCommand(command);
795 }
796
797 /**
798 * Handle posted menu events.
799 *
800 * @param menu menu event
801 */
802 @Override
803 public void onMenu(final TMenuEvent menu) {
48e27807 804
92453213 805 if (!(this instanceof TDesktop)) {
48e27807 806
92453213 807 if (menu.getId() == TMenu.MID_WINDOW_CLOSE) {
78a56d5d 808 if ((flags & NOCLOSEBOX) == 0) {
d36057df
KL
809 if ((flags & HIDEONCLOSE) == 0) {
810 // Close window
811 application.closeWindow(this);
812 } else {
813 // Hide window
814 application.hideWindow(this);
815 }
78a56d5d 816 }
92453213
KL
817 return;
818 }
48e27807 819
92453213
KL
820 if (menu.getId() == TMenu.MID_WINDOW_NEXT) {
821 application.switchWindow(true);
822 return;
823 }
48e27807 824
92453213
KL
825 if (menu.getId() == TMenu.MID_WINDOW_PREVIOUS) {
826 application.switchWindow(false);
827 return;
48e27807 828 }
92453213
KL
829
830 if (menu.getId() == TMenu.MID_WINDOW_MOVE) {
831 inKeyboardResize = true;
832 return;
833 }
834
68c5cd6b
KL
835 if ((menu.getId() == TMenu.MID_WINDOW_ZOOM)
836 && ((flags & NOZOOMBOX) == 0)
837 ) {
92453213
KL
838 if (maximized) {
839 restore();
840 } else {
841 maximize();
842 }
843 return;
844 }
845
846 } // if (!(this instanceof TDesktop))
48e27807
KL
847
848 // I didn't take it, pass it on to my children
849 super.onMenu(menu);
850 }
851
d36057df
KL
852 // ------------------------------------------------------------------------
853 // TWidget ----------------------------------------------------------------
854 // ------------------------------------------------------------------------
855
856 /**
857 * Get this TWindow's parent TApplication.
858 *
859 * @return this TWindow's parent TApplication
860 */
861 @Override
862 public final TApplication getApplication() {
863 return application;
864 }
865
866 /**
867 * Get the Screen.
868 *
869 * @return the Screen
870 */
871 @Override
872 public final Screen getScreen() {
873 return application.getScreen();
874 }
875
876 /**
877 * Called by TApplication.drawChildren() to render on screen.
878 */
879 @Override
880 public void draw() {
881 // Draw the box and background first.
882 CellAttributes border = getBorder();
883 CellAttributes background = getBackground();
884 int borderType = getBorderType();
885
a69ed767
KL
886 drawBox(0, 0, getWidth(), getHeight(), border, background, borderType,
887 true);
d36057df
KL
888
889 // Draw the title
890 int titleLeft = (getWidth() - title.length() - 2) / 2;
891 putCharXY(titleLeft, 0, ' ', border);
892 putStringXY(titleLeft + 1, 0, title);
893 putCharXY(titleLeft + title.length() + 1, 0, ' ', border);
894
895 if (isActive()) {
896
897 // Draw the close button
898 if ((flags & NOCLOSEBOX) == 0) {
899 putCharXY(2, 0, '[', border);
900 putCharXY(4, 0, ']', border);
901 if (mouseOnClose() && mouse.isMouse1()) {
902 putCharXY(3, 0, GraphicsChars.CP437[0x0F],
903 getBorderControls());
904 } else {
905 putCharXY(3, 0, GraphicsChars.CP437[0xFE],
906 getBorderControls());
907 }
908 }
909
910 // Draw the maximize button
911 if (!isModal() && ((flags & NOZOOMBOX) == 0)) {
912
913 putCharXY(getWidth() - 5, 0, '[', border);
914 putCharXY(getWidth() - 3, 0, ']', border);
915 if (mouseOnMaximize() && mouse.isMouse1()) {
916 putCharXY(getWidth() - 4, 0, GraphicsChars.CP437[0x0F],
917 getBorderControls());
918 } else {
919 if (maximized) {
920 putCharXY(getWidth() - 4, 0, GraphicsChars.CP437[0x12],
921 getBorderControls());
922 } else {
923 putCharXY(getWidth() - 4, 0, GraphicsChars.UPARROW,
924 getBorderControls());
925 }
926 }
927
928 // Draw the resize corner
929 if ((flags & RESIZABLE) != 0) {
930 putCharXY(getWidth() - 2, getHeight() - 1,
931 GraphicsChars.SINGLE_BAR, getBorderControls());
932 putCharXY(getWidth() - 1, getHeight() - 1,
933 GraphicsChars.LRCORNER, getBorderControls());
934 }
935 }
936 }
937 }
938
939 // ------------------------------------------------------------------------
940 // TWindow ----------------------------------------------------------------
941 // ------------------------------------------------------------------------
942
943 /**
944 * Get window title.
945 *
946 * @return window title
947 */
948 public final String getTitle() {
949 return title;
950 }
951
952 /**
953 * Set window title.
954 *
955 * @param title new window title
956 */
957 public final void setTitle(final String title) {
958 this.title = title;
959 }
960
961 /**
962 * Get Z order. Lower number means more in-front.
963 *
964 * @return Z value. Lower number means more in-front.
965 */
966 public final int getZ() {
967 return z;
968 }
969
970 /**
971 * Set Z order. Lower number means more in-front.
972 *
973 * @param z the new Z value. Lower number means more in-front.
974 */
975 public final void setZ(final int z) {
976 this.z = z;
977 }
978
979 /**
980 * Add a keypress to be overridden for this window.
981 *
982 * @param key the key to start taking control of
983 */
984 protected void addShortcutKeypress(final TKeypress key) {
985 keyboardShortcuts.add(key);
986 }
987
988 /**
989 * Remove a keypress to be overridden for this window.
990 *
991 * @param key the key to stop taking control of
992 */
993 protected void removeShortcutKeypress(final TKeypress key) {
994 keyboardShortcuts.remove(key);
995 }
996
997 /**
998 * Remove all keypresses to be overridden for this window.
999 */
1000 protected void clearShortcutKeypresses() {
1001 keyboardShortcuts.clear();
1002 }
1003
1004 /**
1005 * Determine if a keypress is overridden for this window.
1006 *
1007 * @param key the key to check
1008 * @return true if this window wants to process this key on its own
1009 */
1010 public boolean isShortcutKeypress(final TKeypress key) {
1011 return keyboardShortcuts.contains(key);
1012 }
1013
1014 /**
1015 * Get the window's status bar, or null if it does not have one.
1016 *
1017 * @return the status bar, or null
1018 */
1019 public TStatusBar getStatusBar() {
1020 return statusBar;
1021 }
1022
1023 /**
1024 * Set the window's status bar to a new one.
1025 *
1026 * @param text the status bar text
1027 * @return the status bar
1028 */
1029 public TStatusBar newStatusBar(final String text) {
1030 statusBar = new TStatusBar(this, text);
1031 return statusBar;
1032 }
1033
1034 /**
1035 * Set the maximum width for this window.
1036 *
1037 * @param maximumWindowWidth new maximum width
1038 */
1039 public final void setMaximumWindowWidth(final int maximumWindowWidth) {
1040 if ((maximumWindowWidth != -1)
1041 && (maximumWindowWidth < minimumWindowWidth + 1)
1042 ) {
1043 throw new IllegalArgumentException("Maximum window width cannot " +
1044 "be smaller than minimum window width + 1");
1045 }
1046 this.maximumWindowWidth = maximumWindowWidth;
1047 }
1048
1049 /**
1050 * Set the minimum width for this window.
1051 *
1052 * @param minimumWindowWidth new minimum width
1053 */
1054 public final void setMinimumWindowWidth(final int minimumWindowWidth) {
1055 if ((maximumWindowWidth != -1)
1056 && (minimumWindowWidth > maximumWindowWidth - 1)
1057 ) {
1058 throw new IllegalArgumentException("Minimum window width cannot " +
1059 "be larger than maximum window width - 1");
1060 }
1061 this.minimumWindowWidth = minimumWindowWidth;
1062 }
1063
1064 /**
1065 * Set the maximum height for this window.
1066 *
1067 * @param maximumWindowHeight new maximum height
1068 */
1069 public final void setMaximumWindowHeight(final int maximumWindowHeight) {
1070 if ((maximumWindowHeight != -1)
1071 && (maximumWindowHeight < minimumWindowHeight + 1)
1072 ) {
1073 throw new IllegalArgumentException("Maximum window height cannot " +
1074 "be smaller than minimum window height + 1");
1075 }
1076 this.maximumWindowHeight = maximumWindowHeight;
1077 }
1078
1079 /**
1080 * Set the minimum height for this window.
1081 *
1082 * @param minimumWindowHeight new minimum height
1083 */
1084 public final void setMinimumWindowHeight(final int minimumWindowHeight) {
1085 if ((maximumWindowHeight != -1)
1086 && (minimumWindowHeight > maximumWindowHeight - 1)
1087 ) {
1088 throw new IllegalArgumentException("Minimum window height cannot " +
1089 "be larger than maximum window height - 1");
1090 }
1091 this.minimumWindowHeight = minimumWindowHeight;
1092 }
1093
1094 /**
1095 * Recenter the window on-screen.
1096 */
1097 public final void center() {
1098 if ((flags & CENTERED) != 0) {
1099 if (getWidth() < getScreen().getWidth()) {
1100 setX((getScreen().getWidth() - getWidth()) / 2);
1101 } else {
1102 setX(0);
1103 }
1104 setY(((application.getDesktopBottom()
1105 - application.getDesktopTop()) - getHeight()) / 2);
1106 if (getY() < 0) {
1107 setY(0);
1108 }
1109 setY(getY() + application.getDesktopTop());
1110 }
1111 }
1112
1113 /**
1114 * Maximize window.
1115 */
1116 public void maximize() {
1117 if (maximized) {
1118 return;
1119 }
1120
1121 restoreWindowWidth = getWidth();
1122 restoreWindowHeight = getHeight();
1123 restoreWindowX = getX();
1124 restoreWindowY = getY();
1125 setWidth(getScreen().getWidth());
1126 setHeight(application.getDesktopBottom() - 1);
1127 setX(0);
1128 setY(1);
1129 maximized = true;
1130
1131 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(),
1132 getHeight()));
1133 }
1134
1135 /**
1136 * Restore (unmaximize) window.
1137 */
1138 public void restore() {
1139 if (!maximized) {
1140 return;
1141 }
1142
1143 setWidth(restoreWindowWidth);
1144 setHeight(restoreWindowHeight);
1145 setX(restoreWindowX);
1146 setY(restoreWindowY);
1147 maximized = false;
1148
1149 onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(),
1150 getHeight()));
1151 }
1152
1153 /**
1154 * Returns true if this window is hidden.
1155 *
1156 * @return true if this window is hidden, false if the window is shown
1157 */
1158 public final boolean isHidden() {
1159 return hidden;
1160 }
1161
1162 /**
1163 * Returns true if this window is shown.
1164 *
1165 * @return true if this window is shown, false if the window is hidden
1166 */
1167 public final boolean isShown() {
1168 return !hidden;
1169 }
1170
1171 /**
1172 * Hide window. A hidden window will still have its onIdle() called, and
1173 * will also have onClose() called at application exit. Hidden windows
1174 * will not receive any other events.
1175 */
1176 public void hide() {
1177 application.hideWindow(this);
1178 }
1179
1180 /**
1181 * Show window.
1182 */
1183 public void show() {
1184 application.showWindow(this);
1185 }
1186
1187 /**
1188 * Activate window (bring to top and receive events).
1189 */
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 */
1198 public void close() {
1199 application.closeWindow(this);
1200 }
1201
1202 /**
1203 * See if this window is undergoing any movement/resize/etc.
1204 *
1205 * @return true if the window is moving
1206 */
1207 public boolean inMovements() {
1208 if (inWindowResize || inWindowMove || inKeyboardResize) {
1209 return true;
1210 }
1211 return false;
1212 }
1213
1214 /**
1215 * Stop any pending movement/resize/etc.
1216 */
1217 public void stopMovements() {
1218 inWindowResize = false;
1219 inWindowMove = false;
1220 inKeyboardResize = false;
1221 }
1222
1223 /**
1224 * Returns true if this window is modal.
1225 *
1226 * @return true if this window is modal
1227 */
1228 public final boolean isModal() {
1229 if ((flags & MODAL) == 0) {
1230 return false;
1231 }
1232 return true;
1233 }
1234
1235 /**
1236 * Returns true if this window has a close box.
1237 *
1238 * @return true if this window has a close box
1239 */
1240 public final boolean hasCloseBox() {
1241 if ((flags & NOCLOSEBOX) != 0) {
1242 return true;
1243 }
1244 return false;
1245 }
1246
1247 /**
1248 * Returns true if this window has a maximize/zoom box.
1249 *
1250 * @return true if this window has a maximize/zoom box
1251 */
1252 public final boolean hasZoomBox() {
1253 if ((flags & NOZOOMBOX) != 0) {
1254 return true;
9696a8f6
KL
1255 }
1256 return false;
1257 }
1258
1259 /**
1260 * Returns true if this window does not want menus to work while it is
1261 * visible.
1262 *
1263 * @return true if this window does not want menus to work while it is
1264 * visible
1265 */
1266 public final boolean hasOverriddenMenu() {
1267 if ((flags & OVERRIDEMENU) != 0) {
1268 return true;
d36057df
KL
1269 }
1270 return false;
1271 }
1272
1273 /**
1274 * Retrieve the background color.
1275 *
1276 * @return the background color
1277 */
1278 public CellAttributes getBackground() {
1279 if (!isModal()
1280 && (inWindowMove || inWindowResize || inKeyboardResize)
1281 ) {
1282 assert (isActive());
1283 return getTheme().getColor("twindow.background.windowmove");
1284 } else if (isModal() && inWindowMove) {
1285 assert (isActive());
1286 return getTheme().getColor("twindow.background.modal");
1287 } else if (isModal()) {
1288 if (isActive()) {
1289 return getTheme().getColor("twindow.background.modal");
1290 }
1291 return getTheme().getColor("twindow.background.modal.inactive");
1292 } else if (isActive()) {
1293 assert (!isModal());
1294 return getTheme().getColor("twindow.background");
1295 } else {
1296 assert (!isModal());
1297 return getTheme().getColor("twindow.background.inactive");
1298 }
1299 }
1300
1301 /**
1302 * Retrieve the border color.
1303 *
1304 * @return the border color
1305 */
1306 public CellAttributes getBorder() {
1307 if (!isModal()
1308 && (inWindowMove || inWindowResize || inKeyboardResize)
1309 ) {
a69ed767
KL
1310 if (!isActive()) {
1311 // The user's terminal never passed a mouse up event, and now
1312 // another window is active but we never finished a drag.
1313 inWindowMove = false;
1314 inWindowResize = false;
1315 inKeyboardResize = false;
1316 return getTheme().getColor("twindow.border.inactive");
1317 }
1318
d36057df
KL
1319 return getTheme().getColor("twindow.border.windowmove");
1320 } else if (isModal() && inWindowMove) {
1321 assert (isActive());
1322 return getTheme().getColor("twindow.border.modal.windowmove");
1323 } else if (isModal()) {
1324 if (isActive()) {
1325 return getTheme().getColor("twindow.border.modal");
1326 } else {
1327 return getTheme().getColor("twindow.border.modal.inactive");
1328 }
1329 } else if (isActive()) {
1330 assert (!isModal());
1331 return getTheme().getColor("twindow.border");
1332 } else {
1333 assert (!isModal());
1334 return getTheme().getColor("twindow.border.inactive");
1335 }
1336 }
1337
1338 /**
1339 * Retrieve the color used by the window movement/sizing controls.
1340 *
1341 * @return the color used by the zoom box, resize bar, and close box
1342 */
1343 public CellAttributes getBorderControls() {
1344 if (isModal()) {
1345 return getTheme().getColor("twindow.border.modal.windowmove");
1346 }
1347 return getTheme().getColor("twindow.border.windowmove");
1348 }
1349
1350 /**
1351 * Retrieve the border line type.
1352 *
1353 * @return the border line type
1354 */
1355 private int getBorderType() {
1356 if (!isModal()
1357 && (inWindowMove || inWindowResize || inKeyboardResize)
1358 ) {
1359 assert (isActive());
1360 return 1;
1361 } else if (isModal() && inWindowMove) {
1362 assert (isActive());
1363 return 1;
1364 } else if (isModal()) {
1365 if (isActive()) {
1366 return 2;
1367 } else {
1368 return 1;
1369 }
1370 } else if (isActive()) {
1371 return 2;
1372 } else {
1373 return 1;
1374 }
1375 }
1376
978a5d8f
KL
1377 /**
1378 * Returns true if this window does not want the application-wide mouse
1379 * cursor drawn over it.
1380 *
1381 * @return true if this window does not want the application-wide mouse
1382 * cursor drawn over it
1383 */
1384 public final boolean hasHiddenMouse() {
1385 return hideMouse;
1386 }
1387
1388 /**
1389 * Set request to prevent the application-wide mouse cursor from being
1390 * drawn over this window.
1391 *
1392 * @param hideMouse if true, this window does not want the
1393 * application-wide mouse cursor drawn over it
1394 */
1395 public final void setHiddenMouse(final boolean hideMouse) {
1396 this.hideMouse = hideMouse;
1397 }
1398
48e27807 1399}