| 1 | /* |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (C) 2019 Kevin Lamonte |
| 7 | * |
| 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: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 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. |
| 25 | * |
| 26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 27 | * @version 1 |
| 28 | */ |
| 29 | package jexer; |
| 30 | |
| 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; |
| 37 | |
| 38 | /** |
| 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, |
| 42 | * moved, or resized. |
| 43 | * |
| 44 | * <p> |
| 45 | * Events are passed to TDesktop as follows: |
| 46 | * <ul> |
| 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> |
| 51 | * </ul> |
| 52 | */ |
| 53 | public class TDesktop extends TWindow { |
| 54 | |
| 55 | // ------------------------------------------------------------------------ |
| 56 | // Constructors ----------------------------------------------------------- |
| 57 | // ------------------------------------------------------------------------ |
| 58 | |
| 59 | /** |
| 60 | * Public constructor. |
| 61 | * |
| 62 | * @param parent parent application |
| 63 | */ |
| 64 | public TDesktop(final TApplication parent) { |
| 65 | super(parent, "", 0, 0, parent.getScreen().getWidth(), |
| 66 | parent.getDesktopBottom() - parent.getDesktopTop()); |
| 67 | |
| 68 | setActive(false); |
| 69 | } |
| 70 | |
| 71 | // ------------------------------------------------------------------------ |
| 72 | // Event handlers --------------------------------------------------------- |
| 73 | // ------------------------------------------------------------------------ |
| 74 | |
| 75 | /** |
| 76 | * Handle window/screen resize events. |
| 77 | * |
| 78 | * @param resize resize event |
| 79 | */ |
| 80 | @Override |
| 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())); |
| 88 | } |
| 89 | } |
| 90 | if (resize.getType() == TResizeEvent.Type.SCREEN) { |
| 91 | // Let children see the screen resize |
| 92 | for (TWidget widget: getChildren()) { |
| 93 | widget.onResize(resize); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Handle mouse button presses. |
| 100 | * |
| 101 | * @param mouse mouse button event |
| 102 | */ |
| 103 | @Override |
| 104 | public void onMouseDown(final TMouseEvent mouse) { |
| 105 | this.mouse = mouse; |
| 106 | |
| 107 | // Pass to children |
| 108 | for (TWidget widget: getChildren()) { |
| 109 | if (widget.mouseWouldHit(mouse)) { |
| 110 | // Dispatch to this child, also activate it |
| 111 | activate(widget); |
| 112 | |
| 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); |
| 117 | return; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Handle mouse button releases. |
| 124 | * |
| 125 | * @param mouse mouse button release event |
| 126 | */ |
| 127 | @Override |
| 128 | public void onMouseUp(final TMouseEvent mouse) { |
| 129 | this.mouse = mouse; |
| 130 | |
| 131 | // Pass to children |
| 132 | for (TWidget widget: getChildren()) { |
| 133 | if (widget.mouseWouldHit(mouse)) { |
| 134 | // Dispatch to this child, also activate it |
| 135 | activate(widget); |
| 136 | |
| 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); |
| 141 | return; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Handle mouse movements. |
| 148 | * |
| 149 | * @param mouse mouse motion event |
| 150 | */ |
| 151 | @Override |
| 152 | public void onMouseMotion(final TMouseEvent mouse) { |
| 153 | this.mouse = mouse; |
| 154 | |
| 155 | // Default: do nothing, pass to children instead |
| 156 | super.onMouseMotion(mouse); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Handle keystrokes. |
| 161 | * |
| 162 | * @param keypress keystroke event |
| 163 | */ |
| 164 | @Override |
| 165 | public void onKeypress(final TKeypressEvent keypress) { |
| 166 | // Default: do nothing, pass to children instead |
| 167 | super.onKeypress(keypress); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Handle posted menu events. |
| 172 | * |
| 173 | * @param menu menu event |
| 174 | */ |
| 175 | @Override |
| 176 | public void onMenu(final TMenuEvent menu) { |
| 177 | // Default: do nothing, pass to children instead |
| 178 | super.onMenu(menu); |
| 179 | } |
| 180 | |
| 181 | // ------------------------------------------------------------------------ |
| 182 | // TWindow ---------------------------------------------------------------- |
| 183 | // ------------------------------------------------------------------------ |
| 184 | |
| 185 | /** |
| 186 | * The default TDesktop draws a hatch character across everything. |
| 187 | */ |
| 188 | @Override |
| 189 | public void draw() { |
| 190 | CellAttributes background = getTheme().getColor("tdesktop.background"); |
| 191 | putAll(GraphicsChars.HATCH, background); |
| 192 | |
| 193 | /* |
| 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); |
| 199 | */ |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Hide window. This is a NOP for TDesktop. |
| 204 | */ |
| 205 | @Override |
| 206 | public final void hide() {} |
| 207 | |
| 208 | /** |
| 209 | * Show window. This is a NOP for TDesktop. |
| 210 | */ |
| 211 | @Override |
| 212 | public final void show() {} |
| 213 | |
| 214 | /** |
| 215 | * Called by hide(). This is a NOP for TDesktop. |
| 216 | */ |
| 217 | @Override |
| 218 | public final void onHide() {} |
| 219 | |
| 220 | /** |
| 221 | * Called by show(). This is a NOP for TDesktop. |
| 222 | */ |
| 223 | @Override |
| 224 | public final void onShow() {} |
| 225 | |
| 226 | /** |
| 227 | * Returns true if the mouse is currently on the close button. |
| 228 | * |
| 229 | * @return true if mouse is currently on the close button |
| 230 | */ |
| 231 | @Override |
| 232 | protected final boolean mouseOnClose() { |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Returns true if the mouse is currently on the maximize/restore button. |
| 238 | * |
| 239 | * @return true if the mouse is currently on the maximize/restore button |
| 240 | */ |
| 241 | @Override |
| 242 | protected final boolean mouseOnMaximize() { |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Returns true if the mouse is currently on the resizable lower right |
| 248 | * corner. |
| 249 | * |
| 250 | * @return true if the mouse is currently on the resizable lower right |
| 251 | * corner |
| 252 | */ |
| 253 | @Override |
| 254 | protected final boolean mouseOnResize() { |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | } |