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