| 1 | /* |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (C) 2017 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. |
| 41 | */ |
| 42 | public class TDesktop extends TWindow { |
| 43 | |
| 44 | /** |
| 45 | * Public constructor. |
| 46 | * |
| 47 | * @param parent parent application |
| 48 | */ |
| 49 | public TDesktop(final TApplication parent) { |
| 50 | |
| 51 | super(parent, "", 0, 0, parent.getScreen().getWidth(), |
| 52 | parent.getScreen().getHeight() - 1); |
| 53 | |
| 54 | setActive(false); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * The default TDesktop draws a hatch character across everything. |
| 59 | */ |
| 60 | @Override |
| 61 | public void draw() { |
| 62 | CellAttributes background = getTheme().getColor("tdesktop.background"); |
| 63 | putAll(GraphicsChars.HATCH, background); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Handle mouse button presses. |
| 68 | * |
| 69 | * @param mouse mouse button event |
| 70 | */ |
| 71 | @Override |
| 72 | public void onMouseDown(final TMouseEvent mouse) { |
| 73 | this.mouse = mouse; |
| 74 | |
| 75 | // Pass to children |
| 76 | for (TWidget widget: getChildren()) { |
| 77 | if (widget.mouseWouldHit(mouse)) { |
| 78 | // Dispatch to this child, also activate it |
| 79 | activate(widget); |
| 80 | |
| 81 | // Set x and y relative to the child's coordinates |
| 82 | mouse.setX(mouse.getAbsoluteX() - widget.getAbsoluteX()); |
| 83 | mouse.setY(mouse.getAbsoluteY() - widget.getAbsoluteY()); |
| 84 | widget.handleEvent(mouse); |
| 85 | return; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Handle mouse button releases. |
| 92 | * |
| 93 | * @param mouse mouse button release event |
| 94 | */ |
| 95 | @Override |
| 96 | public void onMouseUp(final TMouseEvent mouse) { |
| 97 | this.mouse = mouse; |
| 98 | |
| 99 | // Pass to children |
| 100 | for (TWidget widget: getChildren()) { |
| 101 | if (widget.mouseWouldHit(mouse)) { |
| 102 | // Dispatch to this child, also activate it |
| 103 | activate(widget); |
| 104 | |
| 105 | // Set x and y relative to the child's coordinates |
| 106 | mouse.setX(mouse.getAbsoluteX() - widget.getAbsoluteX()); |
| 107 | mouse.setY(mouse.getAbsoluteY() - widget.getAbsoluteY()); |
| 108 | widget.handleEvent(mouse); |
| 109 | return; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Handle mouse movements. |
| 116 | * |
| 117 | * @param mouse mouse motion event |
| 118 | */ |
| 119 | @Override |
| 120 | public void onMouseMotion(final TMouseEvent mouse) { |
| 121 | this.mouse = mouse; |
| 122 | |
| 123 | // Default: do nothing, pass to children instead |
| 124 | super.onMouseMotion(mouse); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Handle keystrokes. |
| 129 | * |
| 130 | * @param keypress keystroke event |
| 131 | */ |
| 132 | @Override |
| 133 | public void onKeypress(final TKeypressEvent keypress) { |
| 134 | // Default: do nothing, pass to children instead |
| 135 | super.onKeypress(keypress); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Handle posted menu events. |
| 140 | * |
| 141 | * @param menu menu event |
| 142 | */ |
| 143 | @Override |
| 144 | public void onMenu(final TMenuEvent menu) { |
| 145 | // Default: do nothing, pass to children instead |
| 146 | super.onMenu(menu); |
| 147 | } |
| 148 | |
| 149 | } |