2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import jexer
.bits
.CellAttributes
;
32 import jexer
.bits
.GraphicsChars
;
33 import jexer
.event
.TKeypressEvent
;
34 import jexer
.event
.TMenuEvent
;
35 import jexer
.event
.TMouseEvent
;
36 import jexer
.event
.TResizeEvent
;
39 * TDesktop is a special-class window that is drawn underneath everything
40 * else. Like a TWindow, it can contain widgets and perform "background"
41 * processing via onIdle(). But unlike a TWindow, it cannot be hidden,
45 * Events are passed to TDesktop as follows:
47 * <li>Mouse events are seen if they do not cover any other windows.</li>
48 * <li>Keypress events are seen if no other windows are open.</li>
49 * <li>Menu events are seen if no other windows are open.</li>
50 * <li>Command events are seen if no other windows are open.</li>
53 public class TDesktop
extends TWindow
{
55 // ------------------------------------------------------------------------
56 // Constructors -----------------------------------------------------------
57 // ------------------------------------------------------------------------
62 * @param parent parent application
64 public TDesktop(final TApplication parent
) {
65 super(parent
, "", 0, 0, parent
.getScreen().getWidth(),
66 parent
.getDesktopBottom() - parent
.getDesktopTop());
71 // ------------------------------------------------------------------------
72 // Event handlers ---------------------------------------------------------
73 // ------------------------------------------------------------------------
76 * Handle window/screen resize events.
78 * @param resize resize event
81 public void onResize(final TResizeEvent resize
) {
82 if (getChildren().size() == 1) {
83 TWidget child
= getChildren().get(0);
84 if (!(child
instanceof TWindow
)) {
85 // Only one child, resize it to match my size.
86 child
.onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
87 getWidth(), getHeight()));
90 if (resize
.getType() == TResizeEvent
.Type
.SCREEN
) {
91 // Let children see the screen resize
92 for (TWidget widget
: getChildren()) {
93 widget
.onResize(resize
);
99 * Handle mouse button presses.
101 * @param mouse mouse button event
104 public void onMouseDown(final TMouseEvent mouse
) {
108 for (TWidget widget
: getChildren()) {
109 if (widget
.mouseWouldHit(mouse
)) {
110 // Dispatch to this child, also activate it
113 // Set x and y relative to the child's coordinates
114 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
115 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
116 widget
.handleEvent(mouse
);
123 * Handle mouse button releases.
125 * @param mouse mouse button release event
128 public void onMouseUp(final TMouseEvent mouse
) {
132 for (TWidget widget
: getChildren()) {
133 if (widget
.mouseWouldHit(mouse
)) {
134 // Dispatch to this child, also activate it
137 // Set x and y relative to the child's coordinates
138 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
139 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
140 widget
.handleEvent(mouse
);
147 * Handle mouse movements.
149 * @param mouse mouse motion event
152 public void onMouseMotion(final TMouseEvent mouse
) {
155 // Default: do nothing, pass to children instead
156 super.onMouseMotion(mouse
);
162 * @param keypress keystroke event
165 public void onKeypress(final TKeypressEvent keypress
) {
166 // Default: do nothing, pass to children instead
167 super.onKeypress(keypress
);
171 * Handle posted menu events.
173 * @param menu menu event
176 public void onMenu(final TMenuEvent menu
) {
177 // Default: do nothing, pass to children instead
181 // ------------------------------------------------------------------------
182 // TWindow ----------------------------------------------------------------
183 // ------------------------------------------------------------------------
186 * The default TDesktop draws a hatch character across everything.
190 CellAttributes background
= getTheme().getColor("tdesktop.background");
191 putAll(GraphicsChars
.HATCH
, background
);
194 // For debugging, let's see where the desktop bounds really are.
195 putCharXY(0, 0, '0', background);
196 putCharXY(getWidth() - 1, 0, '1', background);
197 putCharXY(0, getHeight() - 1, '2', background);
198 putCharXY(getWidth() - 1, getHeight() - 1, '3', background);
203 * Hide window. This is a NOP for TDesktop.
206 public final void hide() {}
209 * Show window. This is a NOP for TDesktop.
212 public final void show() {}
215 * Called by hide(). This is a NOP for TDesktop.
218 public final void onHide() {}
221 * Called by show(). This is a NOP for TDesktop.
224 public final void onShow() {}
227 * Returns true if the mouse is currently on the close button.
229 * @return true if mouse is currently on the close button
232 protected final boolean mouseOnClose() {
237 * Returns true if the mouse is currently on the maximize/restore button.
239 * @return true if the mouse is currently on the maximize/restore button
242 protected final boolean mouseOnMaximize() {
247 * Returns true if the mouse is currently on the resizable lower right
250 * @return true if the mouse is currently on the resizable lower right
254 protected final boolean mouseOnResize() {