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