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