Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
48e27807 KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
48e27807 | 5 | * |
e16dda65 | 6 | * Copyright (C) 2016 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 | */ | |
29 | package jexer; | |
30 | ||
5dfd1c11 KL |
31 | import java.util.HashSet; |
32 | ||
48e27807 KL |
33 | import jexer.bits.Cell; |
34 | import jexer.bits.CellAttributes; | |
35 | import jexer.bits.GraphicsChars; | |
36 | import jexer.event.TCommandEvent; | |
37 | import jexer.event.TKeypressEvent; | |
38 | import jexer.event.TMenuEvent; | |
39 | import jexer.event.TMouseEvent; | |
40 | import jexer.event.TResizeEvent; | |
41 | import jexer.io.Screen; | |
928811d8 | 42 | import jexer.menu.TMenu; |
48e27807 KL |
43 | import static jexer.TCommand.*; |
44 | import static jexer.TKeypress.*; | |
45 | ||
46 | /** | |
47 | * TWindow is the top-level container and drawing surface for other widgets. | |
48 | */ | |
2b9c27db | 49 | public class TWindow extends TWidget { |
48e27807 KL |
50 | |
51 | /** | |
52 | * Window's parent TApplication. | |
53 | */ | |
fca67db0 | 54 | private TApplication application; |
48e27807 KL |
55 | |
56 | /** | |
57 | * Get this TWindow's parent TApplication. | |
58 | * | |
59 | * @return this TWindow's parent TApplication | |
60 | */ | |
928811d8 | 61 | @Override |
48e27807 KL |
62 | public final TApplication getApplication() { |
63 | return application; | |
64 | } | |
65 | ||
66 | /** | |
67 | * Get the Screen. | |
68 | * | |
69 | * @return the Screen | |
70 | */ | |
928811d8 | 71 | @Override |
48e27807 KL |
72 | public final Screen getScreen() { |
73 | return application.getScreen(); | |
74 | } | |
75 | ||
76 | /** | |
77 | * Window title. | |
78 | */ | |
fca67db0 KL |
79 | private String title = ""; |
80 | ||
81 | /** | |
82 | * Get window title. | |
83 | * | |
84 | * @return window title | |
85 | */ | |
86 | public final String getTitle() { | |
87 | return title; | |
88 | } | |
89 | ||
90 | /** | |
91 | * Set window title. | |
92 | * | |
93 | * @param title new window title | |
94 | */ | |
95 | public final void setTitle(final String title) { | |
96 | this.title = title; | |
97 | } | |
48e27807 KL |
98 | |
99 | /** | |
100 | * Window is resizable (default yes). | |
101 | */ | |
102 | public static final int RESIZABLE = 0x01; | |
103 | ||
104 | /** | |
105 | * Window is modal (default no). | |
106 | */ | |
107 | public static final int MODAL = 0x02; | |
108 | ||
109 | /** | |
110 | * Window is centered (default no). | |
111 | */ | |
112 | public static final int CENTERED = 0x04; | |
113 | ||
114 | /** | |
115 | * Window flags. | |
116 | */ | |
117 | private int flags = RESIZABLE; | |
118 | ||
119 | /** | |
120 | * Z order. Lower number means more in-front. | |
121 | */ | |
122 | private int z = 0; | |
123 | ||
a06459bd KL |
124 | /** |
125 | * Get Z order. Lower number means more in-front. | |
126 | * | |
127 | * @return Z value. Lower number means more in-front. | |
128 | */ | |
129 | public final int getZ() { | |
130 | return z; | |
131 | } | |
132 | ||
133 | /** | |
134 | * Set Z order. Lower number means more in-front. | |
135 | * | |
136 | * @param z the new Z value. Lower number means more in-front. | |
137 | */ | |
138 | public final void setZ(final int z) { | |
139 | this.z = z; | |
140 | } | |
141 | ||
5dfd1c11 KL |
142 | /** |
143 | * Window's keyboard shortcuts. Any key in this set will be passed to | |
144 | * the window directly rather than processed through the menu | |
145 | * accelerators. | |
146 | */ | |
147 | private HashSet<TKeypress> keyboardShortcuts = new HashSet<TKeypress>(); | |
148 | ||
149 | /** | |
150 | * Add a keypress to be overridden for this window. | |
151 | * | |
152 | * @param key the key to start taking control of | |
153 | */ | |
154 | protected void addShortcutKeypress(final TKeypress key) { | |
155 | keyboardShortcuts.add(key); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Remove a keypress to be overridden for this window. | |
160 | * | |
161 | * @param key the key to stop taking control of | |
162 | */ | |
163 | protected void removeShortcutKeypress(final TKeypress key) { | |
164 | keyboardShortcuts.remove(key); | |
165 | } | |
166 | ||
167 | /** | |
168 | * Remove all keypresses to be overridden for this window. | |
169 | */ | |
170 | protected void clearShortcutKeypresses() { | |
171 | keyboardShortcuts.clear(); | |
172 | } | |
173 | ||
174 | /** | |
175 | * Determine if a keypress is overridden for this window. | |
176 | * | |
177 | * @param key the key to check | |
178 | * @return true if this window wants to process this key on its own | |
179 | */ | |
180 | public boolean isShortcutKeypress(final TKeypress key) { | |
181 | return keyboardShortcuts.contains(key); | |
182 | } | |
183 | ||
48e27807 KL |
184 | /** |
185 | * If true, then the user clicked on the title bar and is moving the | |
186 | * window. | |
187 | */ | |
bd8d51fa | 188 | protected boolean inWindowMove = false; |
48e27807 KL |
189 | |
190 | /** | |
191 | * If true, then the user clicked on the bottom right corner and is | |
192 | * resizing the window. | |
193 | */ | |
bd8d51fa | 194 | protected boolean inWindowResize = false; |
48e27807 KL |
195 | |
196 | /** | |
197 | * If true, then the user selected "Size/Move" (or hit Ctrl-F5) and is | |
198 | * resizing/moving the window via the keyboard. | |
199 | */ | |
200 | private boolean inKeyboardResize = false; | |
201 | ||
202 | /** | |
203 | * If true, this window is maximized. | |
204 | */ | |
205 | private boolean maximized = false; | |
206 | ||
207 | /** | |
208 | * Remember mouse state. | |
209 | */ | |
928811d8 | 210 | protected TMouseEvent mouse; |
48e27807 KL |
211 | |
212 | // For moving the window. resizing also uses moveWindowMouseX/Y | |
213 | private int moveWindowMouseX; | |
214 | private int moveWindowMouseY; | |
215 | private int oldWindowX; | |
216 | private int oldWindowY; | |
217 | ||
218 | // Resizing | |
219 | private int resizeWindowWidth; | |
220 | private int resizeWindowHeight; | |
221 | private int minimumWindowWidth = 10; | |
222 | private int minimumWindowHeight = 2; | |
223 | private int maximumWindowWidth = -1; | |
224 | private int maximumWindowHeight = -1; | |
225 | ||
226 | // For maximize/restore | |
227 | private int restoreWindowWidth; | |
228 | private int restoreWindowHeight; | |
229 | private int restoreWindowX; | |
230 | private int restoreWindowY; | |
231 | ||
34a42e78 KL |
232 | /** |
233 | * Set the maximum width for this window. | |
234 | * | |
235 | * @param maximumWindowWidth new maximum width | |
236 | */ | |
237 | public final void setMaximumWindowWidth(final int maximumWindowWidth) { | |
238 | this.maximumWindowWidth = maximumWindowWidth; | |
239 | } | |
240 | ||
48e27807 KL |
241 | /** |
242 | * Public constructor. Window will be located at (0, 0). | |
243 | * | |
244 | * @param application TApplication that manages this window | |
245 | * @param title window title, will be centered along the top border | |
246 | * @param width width of window | |
247 | * @param height height of window | |
248 | */ | |
249 | public TWindow(final TApplication application, final String title, | |
250 | final int width, final int height) { | |
251 | ||
252 | this(application, title, 0, 0, width, height, RESIZABLE); | |
253 | } | |
254 | ||
255 | /** | |
256 | * Public constructor. Window will be located at (0, 0). | |
257 | * | |
258 | * @param application TApplication that manages this window | |
259 | * @param title window title, will be centered along the top border | |
260 | * @param width width of window | |
261 | * @param height height of window | |
262 | * @param flags bitmask of RESIZABLE, CENTERED, or MODAL | |
263 | */ | |
264 | public TWindow(final TApplication application, final String title, | |
265 | final int width, final int height, final int flags) { | |
266 | ||
267 | this(application, title, 0, 0, width, height, flags); | |
268 | } | |
269 | ||
270 | /** | |
271 | * Public constructor. | |
272 | * | |
273 | * @param application TApplication that manages this window | |
274 | * @param title window title, will be centered along the top border | |
275 | * @param x column relative to parent | |
276 | * @param y row relative to parent | |
277 | * @param width width of window | |
278 | * @param height height of window | |
279 | */ | |
280 | public TWindow(final TApplication application, final String title, | |
281 | final int x, final int y, final int width, final int height) { | |
282 | ||
283 | this(application, title, x, y, width, height, RESIZABLE); | |
284 | } | |
285 | ||
286 | /** | |
287 | * Public constructor. | |
288 | * | |
289 | * @param application TApplication that manages this window | |
290 | * @param title window title, will be centered along the top border | |
291 | * @param x column relative to parent | |
292 | * @param y row relative to parent | |
293 | * @param width width of window | |
294 | * @param height height of window | |
295 | * @param flags mask of RESIZABLE, CENTERED, or MODAL | |
296 | */ | |
297 | public TWindow(final TApplication application, final String title, | |
298 | final int x, final int y, final int width, final int height, | |
299 | final int flags) { | |
300 | ||
fca67db0 KL |
301 | super(); |
302 | ||
48e27807 | 303 | // I am my own window and parent |
fca67db0 KL |
304 | setupForTWindow(this, x, y + application.getDesktopTop(), |
305 | width, height); | |
48e27807 KL |
306 | |
307 | // Save fields | |
308 | this.title = title; | |
309 | this.application = application; | |
48e27807 KL |
310 | this.flags = flags; |
311 | ||
312 | // Minimum width/height are 10 and 2 | |
313 | assert (width >= 10); | |
fca67db0 | 314 | assert (getHeight() >= 2); |
48e27807 KL |
315 | |
316 | // MODAL implies CENTERED | |
317 | if (isModal()) { | |
318 | this.flags |= CENTERED; | |
319 | } | |
320 | ||
321 | // Center window if specified | |
322 | center(); | |
323 | ||
324 | // Add me to the application | |
325 | application.addWindow(this); | |
326 | } | |
327 | ||
328 | /** | |
329 | * Recenter the window on-screen. | |
330 | */ | |
331 | public final void center() { | |
332 | if ((flags & CENTERED) != 0) { | |
fca67db0 KL |
333 | if (getWidth() < getScreen().getWidth()) { |
334 | setX((getScreen().getWidth() - getWidth()) / 2); | |
48e27807 | 335 | } else { |
fca67db0 | 336 | setX(0); |
48e27807 | 337 | } |
fca67db0 KL |
338 | setY(((application.getDesktopBottom() |
339 | - application.getDesktopTop()) - getHeight()) / 2); | |
340 | if (getY() < 0) { | |
341 | setY(0); | |
48e27807 | 342 | } |
fca67db0 | 343 | setY(getY() + application.getDesktopTop()); |
48e27807 KL |
344 | } |
345 | } | |
346 | ||
347 | /** | |
348 | * Returns true if this window is modal. | |
349 | * | |
350 | * @return true if this window is modal | |
351 | */ | |
352 | public final boolean isModal() { | |
353 | if ((flags & MODAL) == 0) { | |
354 | return false; | |
355 | } | |
356 | return true; | |
357 | } | |
358 | ||
48e27807 KL |
359 | /** |
360 | * Returns true if the mouse is currently on the close button. | |
361 | * | |
362 | * @return true if mouse is currently on the close button | |
363 | */ | |
364 | private boolean mouseOnClose() { | |
365 | if ((mouse != null) | |
fca67db0 KL |
366 | && (mouse.getAbsoluteY() == getY()) |
367 | && (mouse.getAbsoluteX() == getX() + 3) | |
48e27807 KL |
368 | ) { |
369 | return true; | |
370 | } | |
371 | return false; | |
372 | } | |
373 | ||
374 | /** | |
375 | * Returns true if the mouse is currently on the maximize/restore button. | |
376 | * | |
377 | * @return true if the mouse is currently on the maximize/restore button | |
378 | */ | |
379 | private boolean mouseOnMaximize() { | |
380 | if ((mouse != null) | |
381 | && !isModal() | |
fca67db0 KL |
382 | && (mouse.getAbsoluteY() == getY()) |
383 | && (mouse.getAbsoluteX() == getX() + getWidth() - 4) | |
48e27807 KL |
384 | ) { |
385 | return true; | |
386 | } | |
387 | return false; | |
388 | } | |
389 | ||
390 | /** | |
391 | * Returns true if the mouse is currently on the resizable lower right | |
392 | * corner. | |
393 | * | |
394 | * @return true if the mouse is currently on the resizable lower right | |
395 | * corner | |
396 | */ | |
397 | private boolean mouseOnResize() { | |
398 | if (((flags & RESIZABLE) != 0) | |
399 | && !isModal() | |
400 | && (mouse != null) | |
fca67db0 KL |
401 | && (mouse.getAbsoluteY() == getY() + getHeight() - 1) |
402 | && ((mouse.getAbsoluteX() == getX() + getWidth() - 1) | |
403 | || (mouse.getAbsoluteX() == getX() + getWidth() - 2)) | |
48e27807 KL |
404 | ) { |
405 | return true; | |
406 | } | |
407 | return false; | |
408 | } | |
409 | ||
410 | /** | |
411 | * Retrieve the background color. | |
412 | * | |
413 | * @return the background color | |
414 | */ | |
30d336cc | 415 | public final CellAttributes getBackground() { |
48e27807 KL |
416 | if (!isModal() |
417 | && (inWindowMove || inWindowResize || inKeyboardResize) | |
418 | ) { | |
7c870d89 | 419 | assert (isActive()); |
928811d8 | 420 | return getTheme().getColor("twindow.background.windowmove"); |
48e27807 | 421 | } else if (isModal() && inWindowMove) { |
7c870d89 | 422 | assert (isActive()); |
928811d8 | 423 | return getTheme().getColor("twindow.background.modal"); |
48e27807 | 424 | } else if (isModal()) { |
7c870d89 | 425 | if (isActive()) { |
928811d8 | 426 | return getTheme().getColor("twindow.background.modal"); |
48e27807 | 427 | } |
928811d8 | 428 | return getTheme().getColor("twindow.background.modal.inactive"); |
7c870d89 | 429 | } else if (isActive()) { |
48e27807 | 430 | assert (!isModal()); |
928811d8 | 431 | return getTheme().getColor("twindow.background"); |
48e27807 KL |
432 | } else { |
433 | assert (!isModal()); | |
928811d8 | 434 | return getTheme().getColor("twindow.background.inactive"); |
48e27807 KL |
435 | } |
436 | } | |
437 | ||
438 | /** | |
439 | * Retrieve the border color. | |
440 | * | |
441 | * @return the border color | |
442 | */ | |
3649b921 | 443 | public CellAttributes getBorder() { |
48e27807 KL |
444 | if (!isModal() |
445 | && (inWindowMove || inWindowResize || inKeyboardResize) | |
446 | ) { | |
7c870d89 | 447 | assert (isActive()); |
928811d8 | 448 | return getTheme().getColor("twindow.border.windowmove"); |
48e27807 | 449 | } else if (isModal() && inWindowMove) { |
7c870d89 | 450 | assert (isActive()); |
928811d8 | 451 | return getTheme().getColor("twindow.border.modal.windowmove"); |
48e27807 | 452 | } else if (isModal()) { |
7c870d89 | 453 | if (isActive()) { |
928811d8 | 454 | return getTheme().getColor("twindow.border.modal"); |
48e27807 | 455 | } else { |
928811d8 | 456 | return getTheme().getColor("twindow.border.modal.inactive"); |
48e27807 | 457 | } |
7c870d89 | 458 | } else if (isActive()) { |
48e27807 | 459 | assert (!isModal()); |
928811d8 | 460 | return getTheme().getColor("twindow.border"); |
48e27807 KL |
461 | } else { |
462 | assert (!isModal()); | |
928811d8 | 463 | return getTheme().getColor("twindow.border.inactive"); |
48e27807 KL |
464 | } |
465 | } | |
466 | ||
467 | /** | |
468 | * Retrieve the border line type. | |
469 | * | |
470 | * @return the border line type | |
471 | */ | |
928811d8 | 472 | private int getBorderType() { |
48e27807 KL |
473 | if (!isModal() |
474 | && (inWindowMove || inWindowResize || inKeyboardResize) | |
475 | ) { | |
7c870d89 | 476 | assert (isActive()); |
48e27807 KL |
477 | return 1; |
478 | } else if (isModal() && inWindowMove) { | |
7c870d89 | 479 | assert (isActive()); |
48e27807 KL |
480 | return 1; |
481 | } else if (isModal()) { | |
7c870d89 | 482 | if (isActive()) { |
48e27807 KL |
483 | return 2; |
484 | } else { | |
485 | return 1; | |
486 | } | |
7c870d89 | 487 | } else if (isActive()) { |
48e27807 KL |
488 | return 2; |
489 | } else { | |
490 | return 1; | |
491 | } | |
492 | } | |
493 | ||
494 | /** | |
495 | * Subclasses should override this method to cleanup resources. This is | |
496 | * called by application.closeWindow(). | |
497 | */ | |
498 | public void onClose() { | |
499 | // Default: do nothing | |
500 | } | |
501 | ||
efb7af1f KL |
502 | /** |
503 | * Called by application.switchWindow() when this window gets the | |
504 | * focus, and also by application.addWindow(). | |
505 | */ | |
506 | public void onFocus() { | |
507 | // Default: do nothing | |
508 | } | |
509 | ||
510 | /** | |
511 | * Called by application.switchWindow() when another window gets the | |
512 | * focus. | |
513 | */ | |
514 | public void onUnfocus() { | |
515 | // Default: do nothing | |
516 | } | |
517 | ||
48e27807 KL |
518 | /** |
519 | * Called by TApplication.drawChildren() to render on screen. | |
520 | */ | |
521 | @Override | |
522 | public void draw() { | |
523 | // Draw the box and background first. | |
524 | CellAttributes border = getBorder(); | |
525 | CellAttributes background = getBackground(); | |
526 | int borderType = getBorderType(); | |
527 | ||
fca67db0 | 528 | getScreen().drawBox(0, 0, getWidth(), getHeight(), border, |
48e27807 KL |
529 | background, borderType, true); |
530 | ||
531 | // Draw the title | |
fca67db0 | 532 | int titleLeft = (getWidth() - title.length() - 2) / 2; |
48e27807 | 533 | putCharXY(titleLeft, 0, ' ', border); |
0d47c546 | 534 | putStringXY(titleLeft + 1, 0, title); |
48e27807 KL |
535 | putCharXY(titleLeft + title.length() + 1, 0, ' ', border); |
536 | ||
7c870d89 | 537 | if (isActive()) { |
48e27807 KL |
538 | |
539 | // Draw the close button | |
540 | putCharXY(2, 0, '[', border); | |
541 | putCharXY(4, 0, ']', border); | |
7c870d89 | 542 | if (mouseOnClose() && mouse.isMouse1()) { |
48e27807 KL |
543 | putCharXY(3, 0, GraphicsChars.CP437[0x0F], |
544 | !isModal() | |
928811d8 KL |
545 | ? getTheme().getColor("twindow.border.windowmove") |
546 | : getTheme().getColor("twindow.border.modal.windowmove")); | |
48e27807 KL |
547 | } else { |
548 | putCharXY(3, 0, GraphicsChars.CP437[0xFE], | |
549 | !isModal() | |
928811d8 KL |
550 | ? getTheme().getColor("twindow.border.windowmove") |
551 | : getTheme().getColor("twindow.border.modal.windowmove")); | |
48e27807 KL |
552 | } |
553 | ||
554 | // Draw the maximize button | |
555 | if (!isModal()) { | |
556 | ||
fca67db0 KL |
557 | putCharXY(getWidth() - 5, 0, '[', border); |
558 | putCharXY(getWidth() - 3, 0, ']', border); | |
7c870d89 | 559 | if (mouseOnMaximize() && mouse.isMouse1()) { |
fca67db0 | 560 | putCharXY(getWidth() - 4, 0, GraphicsChars.CP437[0x0F], |
928811d8 | 561 | getTheme().getColor("twindow.border.windowmove")); |
48e27807 KL |
562 | } else { |
563 | if (maximized) { | |
fca67db0 | 564 | putCharXY(getWidth() - 4, 0, GraphicsChars.CP437[0x12], |
928811d8 | 565 | getTheme().getColor("twindow.border.windowmove")); |
48e27807 | 566 | } else { |
fca67db0 | 567 | putCharXY(getWidth() - 4, 0, GraphicsChars.UPARROW, |
928811d8 | 568 | getTheme().getColor("twindow.border.windowmove")); |
48e27807 KL |
569 | } |
570 | } | |
571 | ||
572 | // Draw the resize corner | |
573 | if ((flags & RESIZABLE) != 0) { | |
928811d8 KL |
574 | putCharXY(getWidth() - 2, getHeight() - 1, |
575 | GraphicsChars.SINGLE_BAR, | |
576 | getTheme().getColor("twindow.border.windowmove")); | |
577 | putCharXY(getWidth() - 1, getHeight() - 1, | |
578 | GraphicsChars.LRCORNER, | |
579 | getTheme().getColor("twindow.border.windowmove")); | |
48e27807 KL |
580 | } |
581 | } | |
582 | } | |
583 | } | |
584 | ||
585 | /** | |
586 | * Handle mouse button presses. | |
587 | * | |
588 | * @param mouse mouse button event | |
589 | */ | |
590 | @Override | |
591 | public void onMouseDown(final TMouseEvent mouse) { | |
592 | this.mouse = mouse; | |
48e27807 KL |
593 | |
594 | inKeyboardResize = false; | |
595 | ||
fca67db0 | 596 | if ((mouse.getAbsoluteY() == getY()) |
7c870d89 | 597 | && mouse.isMouse1() |
fca67db0 KL |
598 | && (getX() <= mouse.getAbsoluteX()) |
599 | && (mouse.getAbsoluteX() < getX() + getWidth()) | |
48e27807 KL |
600 | && !mouseOnClose() |
601 | && !mouseOnMaximize() | |
602 | ) { | |
603 | // Begin moving window | |
604 | inWindowMove = true; | |
605 | moveWindowMouseX = mouse.getAbsoluteX(); | |
606 | moveWindowMouseY = mouse.getAbsoluteY(); | |
fca67db0 KL |
607 | oldWindowX = getX(); |
608 | oldWindowY = getY(); | |
48e27807 KL |
609 | if (maximized) { |
610 | maximized = false; | |
611 | } | |
612 | return; | |
613 | } | |
614 | if (mouseOnResize()) { | |
615 | // Begin window resize | |
616 | inWindowResize = true; | |
617 | moveWindowMouseX = mouse.getAbsoluteX(); | |
618 | moveWindowMouseY = mouse.getAbsoluteY(); | |
fca67db0 KL |
619 | resizeWindowWidth = getWidth(); |
620 | resizeWindowHeight = getHeight(); | |
48e27807 KL |
621 | if (maximized) { |
622 | maximized = false; | |
623 | } | |
624 | return; | |
625 | } | |
626 | ||
627 | // I didn't take it, pass it on to my children | |
628 | super.onMouseDown(mouse); | |
629 | } | |
630 | ||
631 | /** | |
632 | * Maximize window. | |
633 | */ | |
634 | private void maximize() { | |
fca67db0 KL |
635 | restoreWindowWidth = getWidth(); |
636 | restoreWindowHeight = getHeight(); | |
637 | restoreWindowX = getX(); | |
638 | restoreWindowY = getY(); | |
639 | setWidth(getScreen().getWidth()); | |
640 | setHeight(application.getDesktopBottom() - 1); | |
641 | setX(0); | |
642 | setY(1); | |
48e27807 KL |
643 | maximized = true; |
644 | } | |
645 | ||
646 | /** | |
647 | * Restote (unmaximize) window. | |
648 | */ | |
649 | private void restore() { | |
fca67db0 KL |
650 | setWidth(restoreWindowWidth); |
651 | setHeight(restoreWindowHeight); | |
652 | setX(restoreWindowX); | |
653 | setY(restoreWindowY); | |
48e27807 KL |
654 | maximized = false; |
655 | } | |
656 | ||
657 | /** | |
658 | * Handle mouse button releases. | |
659 | * | |
660 | * @param mouse mouse button release event | |
661 | */ | |
662 | @Override | |
663 | public void onMouseUp(final TMouseEvent mouse) { | |
664 | this.mouse = mouse; | |
48e27807 | 665 | |
7c870d89 | 666 | if ((inWindowMove) && (mouse.isMouse1())) { |
48e27807 KL |
667 | // Stop moving window |
668 | inWindowMove = false; | |
669 | return; | |
670 | } | |
671 | ||
7c870d89 | 672 | if ((inWindowResize) && (mouse.isMouse1())) { |
48e27807 KL |
673 | // Stop resizing window |
674 | inWindowResize = false; | |
675 | return; | |
676 | } | |
677 | ||
7c870d89 | 678 | if (mouse.isMouse1() && mouseOnClose()) { |
48e27807 KL |
679 | // Close window |
680 | application.closeWindow(this); | |
681 | return; | |
682 | } | |
683 | ||
fca67db0 | 684 | if ((mouse.getAbsoluteY() == getY()) |
7c870d89 | 685 | && mouse.isMouse1() |
48e27807 KL |
686 | && mouseOnMaximize()) { |
687 | if (maximized) { | |
688 | // Restore | |
689 | restore(); | |
690 | } else { | |
691 | // Maximize | |
692 | maximize(); | |
693 | } | |
694 | // Pass a resize event to my children | |
fca67db0 KL |
695 | onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, |
696 | getWidth(), getHeight())); | |
48e27807 KL |
697 | return; |
698 | } | |
699 | ||
700 | // I didn't take it, pass it on to my children | |
701 | super.onMouseUp(mouse); | |
702 | } | |
703 | ||
704 | /** | |
705 | * Handle mouse movements. | |
706 | * | |
707 | * @param mouse mouse motion event | |
708 | */ | |
709 | @Override | |
710 | public void onMouseMotion(final TMouseEvent mouse) { | |
711 | this.mouse = mouse; | |
48e27807 KL |
712 | |
713 | if (inWindowMove) { | |
714 | // Move window over | |
fca67db0 KL |
715 | setX(oldWindowX + (mouse.getAbsoluteX() - moveWindowMouseX)); |
716 | setY(oldWindowY + (mouse.getAbsoluteY() - moveWindowMouseY)); | |
48e27807 | 717 | // Don't cover up the menu bar |
fca67db0 KL |
718 | if (getY() < application.getDesktopTop()) { |
719 | setY(application.getDesktopTop()); | |
48e27807 KL |
720 | } |
721 | return; | |
722 | } | |
723 | ||
724 | if (inWindowResize) { | |
725 | // Move window over | |
fca67db0 KL |
726 | setWidth(resizeWindowWidth + (mouse.getAbsoluteX() |
727 | - moveWindowMouseX)); | |
728 | setHeight(resizeWindowHeight + (mouse.getAbsoluteY() | |
729 | - moveWindowMouseY)); | |
730 | if (getX() + getWidth() > getScreen().getWidth()) { | |
731 | setWidth(getScreen().getWidth() - getX()); | |
48e27807 | 732 | } |
fca67db0 KL |
733 | if (getY() + getHeight() > application.getDesktopBottom()) { |
734 | setY(application.getDesktopBottom() - getHeight() + 1); | |
48e27807 KL |
735 | } |
736 | // Don't cover up the menu bar | |
fca67db0 KL |
737 | if (getY() < application.getDesktopTop()) { |
738 | setY(application.getDesktopTop()); | |
48e27807 KL |
739 | } |
740 | ||
741 | // Keep within min/max bounds | |
fca67db0 KL |
742 | if (getWidth() < minimumWindowWidth) { |
743 | setWidth(minimumWindowWidth); | |
48e27807 KL |
744 | inWindowResize = false; |
745 | } | |
fca67db0 KL |
746 | if (getHeight() < minimumWindowHeight) { |
747 | setHeight(minimumWindowHeight); | |
48e27807 KL |
748 | inWindowResize = false; |
749 | } | |
fca67db0 KL |
750 | if ((maximumWindowWidth > 0) |
751 | && (getWidth() > maximumWindowWidth) | |
752 | ) { | |
753 | setWidth(maximumWindowWidth); | |
48e27807 KL |
754 | inWindowResize = false; |
755 | } | |
fca67db0 KL |
756 | if ((maximumWindowHeight > 0) |
757 | && (getHeight() > maximumWindowHeight) | |
758 | ) { | |
759 | setHeight(maximumWindowHeight); | |
48e27807 KL |
760 | inWindowResize = false; |
761 | } | |
762 | ||
763 | // Pass a resize event to my children | |
fca67db0 KL |
764 | onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, |
765 | getWidth(), getHeight())); | |
48e27807 KL |
766 | return; |
767 | } | |
768 | ||
769 | // I didn't take it, pass it on to my children | |
770 | super.onMouseMotion(mouse); | |
771 | } | |
772 | ||
773 | /** | |
774 | * Handle keystrokes. | |
775 | * | |
776 | * @param keypress keystroke event | |
777 | */ | |
778 | @Override | |
779 | public void onKeypress(final TKeypressEvent keypress) { | |
780 | ||
781 | if (inKeyboardResize) { | |
782 | ||
32437017 KL |
783 | // ESC or ENTER - Exit size/move |
784 | if (keypress.equals(kbEsc) || keypress.equals(kbEnter)) { | |
48e27807 KL |
785 | inKeyboardResize = false; |
786 | } | |
787 | ||
788 | if (keypress.equals(kbLeft)) { | |
fca67db0 KL |
789 | if (getX() > 0) { |
790 | setX(getX() - 1); | |
48e27807 KL |
791 | } |
792 | } | |
793 | if (keypress.equals(kbRight)) { | |
fca67db0 KL |
794 | if (getX() < getScreen().getWidth() - 1) { |
795 | setX(getX() + 1); | |
48e27807 KL |
796 | } |
797 | } | |
798 | if (keypress.equals(kbDown)) { | |
fca67db0 KL |
799 | if (getY() < application.getDesktopBottom() - 1) { |
800 | setY(getY() + 1); | |
48e27807 KL |
801 | } |
802 | } | |
803 | if (keypress.equals(kbUp)) { | |
fca67db0 KL |
804 | if (getY() > 1) { |
805 | setY(getY() - 1); | |
48e27807 KL |
806 | } |
807 | } | |
808 | if (keypress.equals(kbShiftLeft)) { | |
a83fea2b KL |
809 | if ((getWidth() > minimumWindowWidth) |
810 | || (minimumWindowWidth <= 0) | |
811 | ) { | |
fca67db0 | 812 | setWidth(getWidth() - 1); |
48e27807 KL |
813 | } |
814 | } | |
815 | if (keypress.equals(kbShiftRight)) { | |
a83fea2b KL |
816 | if ((getWidth() < maximumWindowWidth) |
817 | || (maximumWindowWidth <= 0) | |
818 | ) { | |
fca67db0 | 819 | setWidth(getWidth() + 1); |
48e27807 KL |
820 | } |
821 | } | |
822 | if (keypress.equals(kbShiftUp)) { | |
a83fea2b KL |
823 | if ((getHeight() > minimumWindowHeight) |
824 | || (minimumWindowHeight <= 0) | |
825 | ) { | |
fca67db0 | 826 | setHeight(getHeight() - 1); |
48e27807 KL |
827 | } |
828 | } | |
829 | if (keypress.equals(kbShiftDown)) { | |
a83fea2b KL |
830 | if ((getHeight() < maximumWindowHeight) |
831 | || (maximumWindowHeight <= 0) | |
832 | ) { | |
fca67db0 | 833 | setHeight(getHeight() + 1); |
48e27807 KL |
834 | } |
835 | } | |
836 | ||
0d47c546 KL |
837 | // Pass a resize event to my children |
838 | onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, | |
839 | getWidth(), getHeight())); | |
840 | ||
48e27807 KL |
841 | return; |
842 | } | |
843 | ||
844 | // These keystrokes will typically not be seen unless a subclass | |
845 | // overrides onMenu() due to how TApplication dispatches | |
846 | // accelerators. | |
847 | ||
848 | // Ctrl-W - close window | |
849 | if (keypress.equals(kbCtrlW)) { | |
850 | application.closeWindow(this); | |
851 | return; | |
852 | } | |
853 | ||
854 | // F6 - behave like Alt-TAB | |
855 | if (keypress.equals(kbF6)) { | |
856 | application.switchWindow(true); | |
857 | return; | |
858 | } | |
859 | ||
860 | // Shift-F6 - behave like Shift-Alt-TAB | |
861 | if (keypress.equals(kbShiftF6)) { | |
862 | application.switchWindow(false); | |
863 | return; | |
864 | } | |
865 | ||
866 | // F5 - zoom | |
867 | if (keypress.equals(kbF5)) { | |
868 | if (maximized) { | |
869 | restore(); | |
870 | } else { | |
871 | maximize(); | |
872 | } | |
873 | } | |
874 | ||
875 | // Ctrl-F5 - size/move | |
876 | if (keypress.equals(kbCtrlF5)) { | |
877 | inKeyboardResize = !inKeyboardResize; | |
878 | } | |
879 | ||
880 | // I didn't take it, pass it on to my children | |
881 | super.onKeypress(keypress); | |
882 | } | |
883 | ||
884 | /** | |
885 | * Handle posted command events. | |
886 | * | |
887 | * @param command command event | |
888 | */ | |
889 | @Override | |
890 | public void onCommand(final TCommandEvent command) { | |
891 | ||
892 | // These commands will typically not be seen unless a subclass | |
893 | // overrides onMenu() due to how TApplication dispatches | |
894 | // accelerators. | |
895 | ||
896 | if (command.equals(cmWindowClose)) { | |
897 | application.closeWindow(this); | |
898 | return; | |
899 | } | |
900 | ||
901 | if (command.equals(cmWindowNext)) { | |
902 | application.switchWindow(true); | |
903 | return; | |
904 | } | |
905 | ||
906 | if (command.equals(cmWindowPrevious)) { | |
907 | application.switchWindow(false); | |
908 | return; | |
909 | } | |
910 | ||
911 | if (command.equals(cmWindowMove)) { | |
912 | inKeyboardResize = true; | |
913 | return; | |
914 | } | |
915 | ||
916 | if (command.equals(cmWindowZoom)) { | |
917 | if (maximized) { | |
918 | restore(); | |
919 | } else { | |
920 | maximize(); | |
921 | } | |
922 | } | |
923 | ||
924 | // I didn't take it, pass it on to my children | |
925 | super.onCommand(command); | |
926 | } | |
927 | ||
928 | /** | |
929 | * Handle posted menu events. | |
930 | * | |
931 | * @param menu menu event | |
932 | */ | |
933 | @Override | |
934 | public void onMenu(final TMenuEvent menu) { | |
935 | if (menu.getId() == TMenu.MID_WINDOW_CLOSE) { | |
936 | application.closeWindow(this); | |
937 | return; | |
938 | } | |
939 | ||
940 | if (menu.getId() == TMenu.MID_WINDOW_NEXT) { | |
941 | application.switchWindow(true); | |
942 | return; | |
943 | } | |
944 | ||
945 | if (menu.getId() == TMenu.MID_WINDOW_PREVIOUS) { | |
946 | application.switchWindow(false); | |
947 | return; | |
948 | } | |
949 | ||
950 | if (menu.getId() == TMenu.MID_WINDOW_MOVE) { | |
951 | inKeyboardResize = true; | |
952 | return; | |
953 | } | |
954 | ||
955 | if (menu.getId() == TMenu.MID_WINDOW_ZOOM) { | |
956 | if (maximized) { | |
957 | restore(); | |
958 | } else { | |
959 | maximize(); | |
960 | } | |
961 | return; | |
962 | } | |
963 | ||
964 | // I didn't take it, pass it on to my children | |
965 | super.onMenu(menu); | |
966 | } | |
967 | ||
968 | // ------------------------------------------------------------------------ | |
969 | // Passthru for Screen functions ------------------------------------------ | |
970 | // ------------------------------------------------------------------------ | |
971 | ||
972 | /** | |
973 | * Get the attributes at one location. | |
974 | * | |
975 | * @param x column coordinate. 0 is the left-most column. | |
976 | * @param y row coordinate. 0 is the top-most row. | |
977 | * @return attributes at (x, y) | |
978 | */ | |
979 | public final CellAttributes getAttrXY(final int x, final int y) { | |
980 | return getScreen().getAttrXY(x, y); | |
981 | } | |
982 | ||
983 | /** | |
984 | * Set the attributes at one location. | |
985 | * | |
986 | * @param x column coordinate. 0 is the left-most column. | |
987 | * @param y row coordinate. 0 is the top-most row. | |
988 | * @param attr attributes to use (bold, foreColor, backColor) | |
989 | */ | |
990 | public final void putAttrXY(final int x, final int y, | |
991 | final CellAttributes attr) { | |
992 | ||
993 | getScreen().putAttrXY(x, y, attr); | |
994 | } | |
995 | ||
996 | /** | |
997 | * Set the attributes at one location. | |
998 | * | |
999 | * @param x column coordinate. 0 is the left-most column. | |
1000 | * @param y row coordinate. 0 is the top-most row. | |
1001 | * @param attr attributes to use (bold, foreColor, backColor) | |
1002 | * @param clip if true, honor clipping/offset | |
1003 | */ | |
1004 | public final void putAttrXY(final int x, final int y, | |
1005 | final CellAttributes attr, final boolean clip) { | |
1006 | ||
1007 | getScreen().putAttrXY(x, y, attr, clip); | |
1008 | } | |
1009 | ||
1010 | /** | |
1011 | * Fill the entire screen with one character with attributes. | |
1012 | * | |
1013 | * @param ch character to draw | |
1014 | * @param attr attributes to use (bold, foreColor, backColor) | |
1015 | */ | |
1016 | public final void putAll(final char ch, final CellAttributes attr) { | |
1017 | getScreen().putAll(ch, attr); | |
1018 | } | |
1019 | ||
1020 | /** | |
1021 | * Render one character with attributes. | |
1022 | * | |
1023 | * @param x column coordinate. 0 is the left-most column. | |
1024 | * @param y row coordinate. 0 is the top-most row. | |
1025 | * @param ch character + attributes to draw | |
1026 | */ | |
1027 | public final void putCharXY(final int x, final int y, final Cell ch) { | |
1028 | getScreen().putCharXY(x, y, ch); | |
1029 | } | |
1030 | ||
1031 | /** | |
1032 | * Render one character with attributes. | |
1033 | * | |
1034 | * @param x column coordinate. 0 is the left-most column. | |
1035 | * @param y row coordinate. 0 is the top-most row. | |
1036 | * @param ch character to draw | |
1037 | * @param attr attributes to use (bold, foreColor, backColor) | |
1038 | */ | |
1039 | public final void putCharXY(final int x, final int y, final char ch, | |
1040 | final CellAttributes attr) { | |
1041 | ||
1042 | getScreen().putCharXY(x, y, ch, attr); | |
1043 | } | |
1044 | ||
1045 | /** | |
1046 | * Render one character without changing the underlying attributes. | |
1047 | * | |
1048 | * @param x column coordinate. 0 is the left-most column. | |
1049 | * @param y row coordinate. 0 is the top-most row. | |
1050 | * @param ch character to draw | |
1051 | */ | |
1052 | public final void putCharXY(final int x, final int y, final char ch) { | |
1053 | getScreen().putCharXY(x, y, ch); | |
1054 | } | |
1055 | ||
1056 | /** | |
1057 | * Render a string. Does not wrap if the string exceeds the line. | |
1058 | * | |
1059 | * @param x column coordinate. 0 is the left-most column. | |
1060 | * @param y row coordinate. 0 is the top-most row. | |
1061 | * @param str string to draw | |
1062 | * @param attr attributes to use (bold, foreColor, backColor) | |
1063 | */ | |
0d47c546 | 1064 | public final void putStringXY(final int x, final int y, final String str, |
48e27807 KL |
1065 | final CellAttributes attr) { |
1066 | ||
0d47c546 | 1067 | getScreen().putStringXY(x, y, str, attr); |
48e27807 KL |
1068 | } |
1069 | ||
1070 | /** | |
1071 | * Render a string without changing the underlying attribute. Does not | |
1072 | * wrap if the string exceeds the line. | |
1073 | * | |
1074 | * @param x column coordinate. 0 is the left-most column. | |
1075 | * @param y row coordinate. 0 is the top-most row. | |
1076 | * @param str string to draw | |
1077 | */ | |
0d47c546 KL |
1078 | public final void putStringXY(final int x, final int y, final String str) { |
1079 | getScreen().putStringXY(x, y, str); | |
48e27807 KL |
1080 | } |
1081 | ||
1082 | /** | |
1083 | * Draw a vertical line from (x, y) to (x, y + n). | |
1084 | * | |
1085 | * @param x column coordinate. 0 is the left-most column. | |
1086 | * @param y row coordinate. 0 is the top-most row. | |
1087 | * @param n number of characters to draw | |
1088 | * @param ch character to draw | |
1089 | * @param attr attributes to use (bold, foreColor, backColor) | |
1090 | */ | |
1091 | public final void vLineXY(final int x, final int y, final int n, | |
1092 | final char ch, final CellAttributes attr) { | |
1093 | ||
1094 | getScreen().vLineXY(x, y, n, ch, attr); | |
1095 | } | |
1096 | ||
1097 | /** | |
1098 | * Draw a horizontal line from (x, y) to (x + n, y). | |
1099 | * | |
1100 | * @param x column coordinate. 0 is the left-most column. | |
1101 | * @param y row coordinate. 0 is the top-most row. | |
1102 | * @param n number of characters to draw | |
1103 | * @param ch character to draw | |
1104 | * @param attr attributes to use (bold, foreColor, backColor) | |
1105 | */ | |
1106 | public final void hLineXY(final int x, final int y, final int n, | |
1107 | final char ch, final CellAttributes attr) { | |
1108 | ||
1109 | getScreen().hLineXY(x, y, n, ch, attr); | |
1110 | } | |
1111 | ||
1112 | ||
1113 | } |