Commit | Line | Data |
---|---|---|
48e27807 KL |
1 | /** |
2 | * Jexer - Java Text User Interface | |
3 | * | |
4 | * License: LGPLv3 or later | |
5 | * | |
6 | * This module is licensed under the GNU Lesser General Public License | |
7 | * Version 3. Please see the file "COPYING" in this directory for more | |
8 | * information about the GNU Lesser General Public License Version 3. | |
9 | * | |
10 | * Copyright (C) 2015 Kevin Lamonte | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU Lesser General Public License | |
14 | * as published by the Free Software Foundation; either version 3 of | |
15 | * the License, or (at your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Lesser General Public | |
23 | * License along with this program; if not, see | |
24 | * http://www.gnu.org/licenses/, or write to the Free Software | |
25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
26 | * 02110-1301 USA | |
27 | * | |
28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
29 | * @version 1 | |
30 | */ | |
31 | package jexer; | |
32 | ||
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 | ||
48e27807 KL |
142 | /** |
143 | * If true, then the user clicked on the title bar and is moving the | |
144 | * window. | |
145 | */ | |
146 | private boolean inWindowMove = false; | |
147 | ||
148 | /** | |
149 | * If true, then the user clicked on the bottom right corner and is | |
150 | * resizing the window. | |
151 | */ | |
152 | private boolean inWindowResize = false; | |
153 | ||
154 | /** | |
155 | * If true, then the user selected "Size/Move" (or hit Ctrl-F5) and is | |
156 | * resizing/moving the window via the keyboard. | |
157 | */ | |
158 | private boolean inKeyboardResize = false; | |
159 | ||
160 | /** | |
161 | * If true, this window is maximized. | |
162 | */ | |
163 | private boolean maximized = false; | |
164 | ||
165 | /** | |
166 | * Remember mouse state. | |
167 | */ | |
928811d8 | 168 | protected TMouseEvent mouse; |
48e27807 KL |
169 | |
170 | // For moving the window. resizing also uses moveWindowMouseX/Y | |
171 | private int moveWindowMouseX; | |
172 | private int moveWindowMouseY; | |
173 | private int oldWindowX; | |
174 | private int oldWindowY; | |
175 | ||
176 | // Resizing | |
177 | private int resizeWindowWidth; | |
178 | private int resizeWindowHeight; | |
179 | private int minimumWindowWidth = 10; | |
180 | private int minimumWindowHeight = 2; | |
181 | private int maximumWindowWidth = -1; | |
182 | private int maximumWindowHeight = -1; | |
183 | ||
184 | // For maximize/restore | |
185 | private int restoreWindowWidth; | |
186 | private int restoreWindowHeight; | |
187 | private int restoreWindowX; | |
188 | private int restoreWindowY; | |
189 | ||
34a42e78 KL |
190 | /** |
191 | * Set the maximum width for this window. | |
192 | * | |
193 | * @param maximumWindowWidth new maximum width | |
194 | */ | |
195 | public final void setMaximumWindowWidth(final int maximumWindowWidth) { | |
196 | this.maximumWindowWidth = maximumWindowWidth; | |
197 | } | |
198 | ||
48e27807 KL |
199 | /** |
200 | * Public constructor. Window will be located at (0, 0). | |
201 | * | |
202 | * @param application TApplication that manages this window | |
203 | * @param title window title, will be centered along the top border | |
204 | * @param width width of window | |
205 | * @param height height of window | |
206 | */ | |
207 | public TWindow(final TApplication application, final String title, | |
208 | final int width, final int height) { | |
209 | ||
210 | this(application, title, 0, 0, width, height, RESIZABLE); | |
211 | } | |
212 | ||
213 | /** | |
214 | * Public constructor. Window will be located at (0, 0). | |
215 | * | |
216 | * @param application TApplication that manages this window | |
217 | * @param title window title, will be centered along the top border | |
218 | * @param width width of window | |
219 | * @param height height of window | |
220 | * @param flags bitmask of RESIZABLE, CENTERED, or MODAL | |
221 | */ | |
222 | public TWindow(final TApplication application, final String title, | |
223 | final int width, final int height, final int flags) { | |
224 | ||
225 | this(application, title, 0, 0, width, height, flags); | |
226 | } | |
227 | ||
228 | /** | |
229 | * Public constructor. | |
230 | * | |
231 | * @param application TApplication that manages this window | |
232 | * @param title window title, will be centered along the top border | |
233 | * @param x column relative to parent | |
234 | * @param y row relative to parent | |
235 | * @param width width of window | |
236 | * @param height height of window | |
237 | */ | |
238 | public TWindow(final TApplication application, final String title, | |
239 | final int x, final int y, final int width, final int height) { | |
240 | ||
241 | this(application, title, x, y, width, height, RESIZABLE); | |
242 | } | |
243 | ||
244 | /** | |
245 | * Public constructor. | |
246 | * | |
247 | * @param application TApplication that manages this window | |
248 | * @param title window title, will be centered along the top border | |
249 | * @param x column relative to parent | |
250 | * @param y row relative to parent | |
251 | * @param width width of window | |
252 | * @param height height of window | |
253 | * @param flags mask of RESIZABLE, CENTERED, or MODAL | |
254 | */ | |
255 | public TWindow(final TApplication application, final String title, | |
256 | final int x, final int y, final int width, final int height, | |
257 | final int flags) { | |
258 | ||
fca67db0 KL |
259 | super(); |
260 | ||
48e27807 | 261 | // I am my own window and parent |
fca67db0 KL |
262 | setupForTWindow(this, x, y + application.getDesktopTop(), |
263 | width, height); | |
48e27807 KL |
264 | |
265 | // Save fields | |
266 | this.title = title; | |
267 | this.application = application; | |
48e27807 KL |
268 | this.flags = flags; |
269 | ||
270 | // Minimum width/height are 10 and 2 | |
271 | assert (width >= 10); | |
fca67db0 | 272 | assert (getHeight() >= 2); |
48e27807 KL |
273 | |
274 | // MODAL implies CENTERED | |
275 | if (isModal()) { | |
276 | this.flags |= CENTERED; | |
277 | } | |
278 | ||
279 | // Center window if specified | |
280 | center(); | |
281 | ||
282 | // Add me to the application | |
283 | application.addWindow(this); | |
284 | } | |
285 | ||
286 | /** | |
287 | * Recenter the window on-screen. | |
288 | */ | |
289 | public final void center() { | |
290 | if ((flags & CENTERED) != 0) { | |
fca67db0 KL |
291 | if (getWidth() < getScreen().getWidth()) { |
292 | setX((getScreen().getWidth() - getWidth()) / 2); | |
48e27807 | 293 | } else { |
fca67db0 | 294 | setX(0); |
48e27807 | 295 | } |
fca67db0 KL |
296 | setY(((application.getDesktopBottom() |
297 | - application.getDesktopTop()) - getHeight()) / 2); | |
298 | if (getY() < 0) { | |
299 | setY(0); | |
48e27807 | 300 | } |
fca67db0 | 301 | setY(getY() + application.getDesktopTop()); |
48e27807 KL |
302 | } |
303 | } | |
304 | ||
305 | /** | |
306 | * Returns true if this window is modal. | |
307 | * | |
308 | * @return true if this window is modal | |
309 | */ | |
310 | public final boolean isModal() { | |
311 | if ((flags & MODAL) == 0) { | |
312 | return false; | |
313 | } | |
314 | return true; | |
315 | } | |
316 | ||
48e27807 KL |
317 | /** |
318 | * Returns true if the mouse is currently on the close button. | |
319 | * | |
320 | * @return true if mouse is currently on the close button | |
321 | */ | |
322 | private boolean mouseOnClose() { | |
323 | if ((mouse != null) | |
fca67db0 KL |
324 | && (mouse.getAbsoluteY() == getY()) |
325 | && (mouse.getAbsoluteX() == getX() + 3) | |
48e27807 KL |
326 | ) { |
327 | return true; | |
328 | } | |
329 | return false; | |
330 | } | |
331 | ||
332 | /** | |
333 | * Returns true if the mouse is currently on the maximize/restore button. | |
334 | * | |
335 | * @return true if the mouse is currently on the maximize/restore button | |
336 | */ | |
337 | private boolean mouseOnMaximize() { | |
338 | if ((mouse != null) | |
339 | && !isModal() | |
fca67db0 KL |
340 | && (mouse.getAbsoluteY() == getY()) |
341 | && (mouse.getAbsoluteX() == getX() + getWidth() - 4) | |
48e27807 KL |
342 | ) { |
343 | return true; | |
344 | } | |
345 | return false; | |
346 | } | |
347 | ||
348 | /** | |
349 | * Returns true if the mouse is currently on the resizable lower right | |
350 | * corner. | |
351 | * | |
352 | * @return true if the mouse is currently on the resizable lower right | |
353 | * corner | |
354 | */ | |
355 | private boolean mouseOnResize() { | |
356 | if (((flags & RESIZABLE) != 0) | |
357 | && !isModal() | |
358 | && (mouse != null) | |
fca67db0 KL |
359 | && (mouse.getAbsoluteY() == getY() + getHeight() - 1) |
360 | && ((mouse.getAbsoluteX() == getX() + getWidth() - 1) | |
361 | || (mouse.getAbsoluteX() == getX() + getWidth() - 2)) | |
48e27807 KL |
362 | ) { |
363 | return true; | |
364 | } | |
365 | return false; | |
366 | } | |
367 | ||
368 | /** | |
369 | * Retrieve the background color. | |
370 | * | |
371 | * @return the background color | |
372 | */ | |
30d336cc | 373 | public final CellAttributes getBackground() { |
48e27807 KL |
374 | if (!isModal() |
375 | && (inWindowMove || inWindowResize || inKeyboardResize) | |
376 | ) { | |
fca67db0 | 377 | assert (getActive()); |
928811d8 | 378 | return getTheme().getColor("twindow.background.windowmove"); |
48e27807 | 379 | } else if (isModal() && inWindowMove) { |
fca67db0 | 380 | assert (getActive()); |
928811d8 | 381 | return getTheme().getColor("twindow.background.modal"); |
48e27807 | 382 | } else if (isModal()) { |
fca67db0 | 383 | if (getActive()) { |
928811d8 | 384 | return getTheme().getColor("twindow.background.modal"); |
48e27807 | 385 | } |
928811d8 | 386 | return getTheme().getColor("twindow.background.modal.inactive"); |
fca67db0 | 387 | } else if (getActive()) { |
48e27807 | 388 | assert (!isModal()); |
928811d8 | 389 | return getTheme().getColor("twindow.background"); |
48e27807 KL |
390 | } else { |
391 | assert (!isModal()); | |
928811d8 | 392 | return getTheme().getColor("twindow.background.inactive"); |
48e27807 KL |
393 | } |
394 | } | |
395 | ||
396 | /** | |
397 | * Retrieve the border color. | |
398 | * | |
399 | * @return the border color | |
400 | */ | |
928811d8 | 401 | private CellAttributes getBorder() { |
48e27807 KL |
402 | if (!isModal() |
403 | && (inWindowMove || inWindowResize || inKeyboardResize) | |
404 | ) { | |
fca67db0 | 405 | assert (getActive()); |
928811d8 | 406 | return getTheme().getColor("twindow.border.windowmove"); |
48e27807 | 407 | } else if (isModal() && inWindowMove) { |
fca67db0 | 408 | assert (getActive()); |
928811d8 | 409 | return getTheme().getColor("twindow.border.modal.windowmove"); |
48e27807 | 410 | } else if (isModal()) { |
fca67db0 | 411 | if (getActive()) { |
928811d8 | 412 | return getTheme().getColor("twindow.border.modal"); |
48e27807 | 413 | } else { |
928811d8 | 414 | return getTheme().getColor("twindow.border.modal.inactive"); |
48e27807 | 415 | } |
fca67db0 | 416 | } else if (getActive()) { |
48e27807 | 417 | assert (!isModal()); |
928811d8 | 418 | return getTheme().getColor("twindow.border"); |
48e27807 KL |
419 | } else { |
420 | assert (!isModal()); | |
928811d8 | 421 | return getTheme().getColor("twindow.border.inactive"); |
48e27807 KL |
422 | } |
423 | } | |
424 | ||
425 | /** | |
426 | * Retrieve the border line type. | |
427 | * | |
428 | * @return the border line type | |
429 | */ | |
928811d8 | 430 | private int getBorderType() { |
48e27807 KL |
431 | if (!isModal() |
432 | && (inWindowMove || inWindowResize || inKeyboardResize) | |
433 | ) { | |
fca67db0 | 434 | assert (getActive()); |
48e27807 KL |
435 | return 1; |
436 | } else if (isModal() && inWindowMove) { | |
fca67db0 | 437 | assert (getActive()); |
48e27807 KL |
438 | return 1; |
439 | } else if (isModal()) { | |
fca67db0 | 440 | if (getActive()) { |
48e27807 KL |
441 | return 2; |
442 | } else { | |
443 | return 1; | |
444 | } | |
fca67db0 | 445 | } else if (getActive()) { |
48e27807 KL |
446 | return 2; |
447 | } else { | |
448 | return 1; | |
449 | } | |
450 | } | |
451 | ||
452 | /** | |
453 | * Subclasses should override this method to cleanup resources. This is | |
454 | * called by application.closeWindow(). | |
455 | */ | |
456 | public void onClose() { | |
457 | // Default: do nothing | |
458 | } | |
459 | ||
460 | /** | |
461 | * Called by TApplication.drawChildren() to render on screen. | |
462 | */ | |
463 | @Override | |
464 | public void draw() { | |
465 | // Draw the box and background first. | |
466 | CellAttributes border = getBorder(); | |
467 | CellAttributes background = getBackground(); | |
468 | int borderType = getBorderType(); | |
469 | ||
fca67db0 | 470 | getScreen().drawBox(0, 0, getWidth(), getHeight(), border, |
48e27807 KL |
471 | background, borderType, true); |
472 | ||
473 | // Draw the title | |
fca67db0 | 474 | int titleLeft = (getWidth() - title.length() - 2) / 2; |
48e27807 KL |
475 | putCharXY(titleLeft, 0, ' ', border); |
476 | putStrXY(titleLeft + 1, 0, title); | |
477 | putCharXY(titleLeft + title.length() + 1, 0, ' ', border); | |
478 | ||
fca67db0 | 479 | if (getActive()) { |
48e27807 KL |
480 | |
481 | // Draw the close button | |
482 | putCharXY(2, 0, '[', border); | |
483 | putCharXY(4, 0, ']', border); | |
484 | if (mouseOnClose() && mouse.getMouse1()) { | |
485 | putCharXY(3, 0, GraphicsChars.CP437[0x0F], | |
486 | !isModal() | |
928811d8 KL |
487 | ? getTheme().getColor("twindow.border.windowmove") |
488 | : getTheme().getColor("twindow.border.modal.windowmove")); | |
48e27807 KL |
489 | } else { |
490 | putCharXY(3, 0, GraphicsChars.CP437[0xFE], | |
491 | !isModal() | |
928811d8 KL |
492 | ? getTheme().getColor("twindow.border.windowmove") |
493 | : getTheme().getColor("twindow.border.modal.windowmove")); | |
48e27807 KL |
494 | } |
495 | ||
496 | // Draw the maximize button | |
497 | if (!isModal()) { | |
498 | ||
fca67db0 KL |
499 | putCharXY(getWidth() - 5, 0, '[', border); |
500 | putCharXY(getWidth() - 3, 0, ']', border); | |
48e27807 | 501 | if (mouseOnMaximize() && mouse.getMouse1()) { |
fca67db0 | 502 | putCharXY(getWidth() - 4, 0, GraphicsChars.CP437[0x0F], |
928811d8 | 503 | getTheme().getColor("twindow.border.windowmove")); |
48e27807 KL |
504 | } else { |
505 | if (maximized) { | |
fca67db0 | 506 | putCharXY(getWidth() - 4, 0, GraphicsChars.CP437[0x12], |
928811d8 | 507 | getTheme().getColor("twindow.border.windowmove")); |
48e27807 | 508 | } else { |
fca67db0 | 509 | putCharXY(getWidth() - 4, 0, GraphicsChars.UPARROW, |
928811d8 | 510 | getTheme().getColor("twindow.border.windowmove")); |
48e27807 KL |
511 | } |
512 | } | |
513 | ||
514 | // Draw the resize corner | |
515 | if ((flags & RESIZABLE) != 0) { | |
928811d8 KL |
516 | putCharXY(getWidth() - 2, getHeight() - 1, |
517 | GraphicsChars.SINGLE_BAR, | |
518 | getTheme().getColor("twindow.border.windowmove")); | |
519 | putCharXY(getWidth() - 1, getHeight() - 1, | |
520 | GraphicsChars.LRCORNER, | |
521 | getTheme().getColor("twindow.border.windowmove")); | |
48e27807 KL |
522 | } |
523 | } | |
524 | } | |
525 | } | |
526 | ||
527 | /** | |
528 | * Handle mouse button presses. | |
529 | * | |
530 | * @param mouse mouse button event | |
531 | */ | |
532 | @Override | |
533 | public void onMouseDown(final TMouseEvent mouse) { | |
534 | this.mouse = mouse; | |
535 | application.setRepaint(); | |
536 | ||
537 | inKeyboardResize = false; | |
538 | ||
fca67db0 | 539 | if ((mouse.getAbsoluteY() == getY()) |
48e27807 | 540 | && mouse.getMouse1() |
fca67db0 KL |
541 | && (getX() <= mouse.getAbsoluteX()) |
542 | && (mouse.getAbsoluteX() < getX() + getWidth()) | |
48e27807 KL |
543 | && !mouseOnClose() |
544 | && !mouseOnMaximize() | |
545 | ) { | |
546 | // Begin moving window | |
547 | inWindowMove = true; | |
548 | moveWindowMouseX = mouse.getAbsoluteX(); | |
549 | moveWindowMouseY = mouse.getAbsoluteY(); | |
fca67db0 KL |
550 | oldWindowX = getX(); |
551 | oldWindowY = getY(); | |
48e27807 KL |
552 | if (maximized) { |
553 | maximized = false; | |
554 | } | |
555 | return; | |
556 | } | |
557 | if (mouseOnResize()) { | |
558 | // Begin window resize | |
559 | inWindowResize = true; | |
560 | moveWindowMouseX = mouse.getAbsoluteX(); | |
561 | moveWindowMouseY = mouse.getAbsoluteY(); | |
fca67db0 KL |
562 | resizeWindowWidth = getWidth(); |
563 | resizeWindowHeight = getHeight(); | |
48e27807 KL |
564 | if (maximized) { |
565 | maximized = false; | |
566 | } | |
567 | return; | |
568 | } | |
569 | ||
570 | // I didn't take it, pass it on to my children | |
571 | super.onMouseDown(mouse); | |
572 | } | |
573 | ||
574 | /** | |
575 | * Maximize window. | |
576 | */ | |
577 | private void maximize() { | |
fca67db0 KL |
578 | restoreWindowWidth = getWidth(); |
579 | restoreWindowHeight = getHeight(); | |
580 | restoreWindowX = getX(); | |
581 | restoreWindowY = getY(); | |
582 | setWidth(getScreen().getWidth()); | |
583 | setHeight(application.getDesktopBottom() - 1); | |
584 | setX(0); | |
585 | setY(1); | |
48e27807 KL |
586 | maximized = true; |
587 | } | |
588 | ||
589 | /** | |
590 | * Restote (unmaximize) window. | |
591 | */ | |
592 | private void restore() { | |
fca67db0 KL |
593 | setWidth(restoreWindowWidth); |
594 | setHeight(restoreWindowHeight); | |
595 | setX(restoreWindowX); | |
596 | setY(restoreWindowY); | |
48e27807 KL |
597 | maximized = false; |
598 | } | |
599 | ||
600 | /** | |
601 | * Handle mouse button releases. | |
602 | * | |
603 | * @param mouse mouse button release event | |
604 | */ | |
605 | @Override | |
606 | public void onMouseUp(final TMouseEvent mouse) { | |
607 | this.mouse = mouse; | |
608 | application.setRepaint(); | |
609 | ||
610 | if ((inWindowMove) && (mouse.getMouse1())) { | |
611 | // Stop moving window | |
612 | inWindowMove = false; | |
613 | return; | |
614 | } | |
615 | ||
616 | if ((inWindowResize) && (mouse.getMouse1())) { | |
617 | // Stop resizing window | |
618 | inWindowResize = false; | |
619 | return; | |
620 | } | |
621 | ||
622 | if (mouse.getMouse1() && mouseOnClose()) { | |
623 | // Close window | |
624 | application.closeWindow(this); | |
625 | return; | |
626 | } | |
627 | ||
fca67db0 KL |
628 | if ((mouse.getAbsoluteY() == getY()) |
629 | && mouse.getMouse1() | |
48e27807 KL |
630 | && mouseOnMaximize()) { |
631 | if (maximized) { | |
632 | // Restore | |
633 | restore(); | |
634 | } else { | |
635 | // Maximize | |
636 | maximize(); | |
637 | } | |
638 | // Pass a resize event to my children | |
fca67db0 KL |
639 | onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, |
640 | getWidth(), getHeight())); | |
48e27807 KL |
641 | return; |
642 | } | |
643 | ||
644 | // I didn't take it, pass it on to my children | |
645 | super.onMouseUp(mouse); | |
646 | } | |
647 | ||
648 | /** | |
649 | * Handle mouse movements. | |
650 | * | |
651 | * @param mouse mouse motion event | |
652 | */ | |
653 | @Override | |
654 | public void onMouseMotion(final TMouseEvent mouse) { | |
655 | this.mouse = mouse; | |
656 | application.setRepaint(); | |
657 | ||
658 | if (inWindowMove) { | |
659 | // Move window over | |
fca67db0 KL |
660 | setX(oldWindowX + (mouse.getAbsoluteX() - moveWindowMouseX)); |
661 | setY(oldWindowY + (mouse.getAbsoluteY() - moveWindowMouseY)); | |
48e27807 | 662 | // Don't cover up the menu bar |
fca67db0 KL |
663 | if (getY() < application.getDesktopTop()) { |
664 | setY(application.getDesktopTop()); | |
48e27807 KL |
665 | } |
666 | return; | |
667 | } | |
668 | ||
669 | if (inWindowResize) { | |
670 | // Move window over | |
fca67db0 KL |
671 | setWidth(resizeWindowWidth + (mouse.getAbsoluteX() |
672 | - moveWindowMouseX)); | |
673 | setHeight(resizeWindowHeight + (mouse.getAbsoluteY() | |
674 | - moveWindowMouseY)); | |
675 | if (getX() + getWidth() > getScreen().getWidth()) { | |
676 | setWidth(getScreen().getWidth() - getX()); | |
48e27807 | 677 | } |
fca67db0 KL |
678 | if (getY() + getHeight() > application.getDesktopBottom()) { |
679 | setY(application.getDesktopBottom() - getHeight() + 1); | |
48e27807 KL |
680 | } |
681 | // Don't cover up the menu bar | |
fca67db0 KL |
682 | if (getY() < application.getDesktopTop()) { |
683 | setY(application.getDesktopTop()); | |
48e27807 KL |
684 | } |
685 | ||
686 | // Keep within min/max bounds | |
fca67db0 KL |
687 | if (getWidth() < minimumWindowWidth) { |
688 | setWidth(minimumWindowWidth); | |
48e27807 KL |
689 | inWindowResize = false; |
690 | } | |
fca67db0 KL |
691 | if (getHeight() < minimumWindowHeight) { |
692 | setHeight(minimumWindowHeight); | |
48e27807 KL |
693 | inWindowResize = false; |
694 | } | |
fca67db0 KL |
695 | if ((maximumWindowWidth > 0) |
696 | && (getWidth() > maximumWindowWidth) | |
697 | ) { | |
698 | setWidth(maximumWindowWidth); | |
48e27807 KL |
699 | inWindowResize = false; |
700 | } | |
fca67db0 KL |
701 | if ((maximumWindowHeight > 0) |
702 | && (getHeight() > maximumWindowHeight) | |
703 | ) { | |
704 | setHeight(maximumWindowHeight); | |
48e27807 KL |
705 | inWindowResize = false; |
706 | } | |
707 | ||
708 | // Pass a resize event to my children | |
fca67db0 KL |
709 | onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, |
710 | getWidth(), getHeight())); | |
48e27807 KL |
711 | return; |
712 | } | |
713 | ||
714 | // I didn't take it, pass it on to my children | |
715 | super.onMouseMotion(mouse); | |
716 | } | |
717 | ||
718 | /** | |
719 | * Handle keystrokes. | |
720 | * | |
721 | * @param keypress keystroke event | |
722 | */ | |
723 | @Override | |
724 | public void onKeypress(final TKeypressEvent keypress) { | |
725 | ||
726 | if (inKeyboardResize) { | |
727 | ||
728 | // ESC - Exit size/move | |
729 | if (keypress.equals(kbEsc)) { | |
730 | inKeyboardResize = false; | |
731 | } | |
732 | ||
733 | if (keypress.equals(kbLeft)) { | |
fca67db0 KL |
734 | if (getX() > 0) { |
735 | setX(getX() - 1); | |
48e27807 KL |
736 | } |
737 | } | |
738 | if (keypress.equals(kbRight)) { | |
fca67db0 KL |
739 | if (getX() < getScreen().getWidth() - 1) { |
740 | setX(getX() + 1); | |
48e27807 KL |
741 | } |
742 | } | |
743 | if (keypress.equals(kbDown)) { | |
fca67db0 KL |
744 | if (getY() < application.getDesktopBottom() - 1) { |
745 | setY(getY() + 1); | |
48e27807 KL |
746 | } |
747 | } | |
748 | if (keypress.equals(kbUp)) { | |
fca67db0 KL |
749 | if (getY() > 1) { |
750 | setY(getY() - 1); | |
48e27807 KL |
751 | } |
752 | } | |
753 | if (keypress.equals(kbShiftLeft)) { | |
a83fea2b KL |
754 | if ((getWidth() > minimumWindowWidth) |
755 | || (minimumWindowWidth <= 0) | |
756 | ) { | |
fca67db0 | 757 | setWidth(getWidth() - 1); |
48e27807 KL |
758 | } |
759 | } | |
760 | if (keypress.equals(kbShiftRight)) { | |
a83fea2b KL |
761 | if ((getWidth() < maximumWindowWidth) |
762 | || (maximumWindowWidth <= 0) | |
763 | ) { | |
fca67db0 | 764 | setWidth(getWidth() + 1); |
48e27807 KL |
765 | } |
766 | } | |
767 | if (keypress.equals(kbShiftUp)) { | |
a83fea2b KL |
768 | if ((getHeight() > minimumWindowHeight) |
769 | || (minimumWindowHeight <= 0) | |
770 | ) { | |
fca67db0 | 771 | setHeight(getHeight() - 1); |
48e27807 KL |
772 | } |
773 | } | |
774 | if (keypress.equals(kbShiftDown)) { | |
a83fea2b KL |
775 | if ((getHeight() < maximumWindowHeight) |
776 | || (maximumWindowHeight <= 0) | |
777 | ) { | |
fca67db0 | 778 | setHeight(getHeight() + 1); |
48e27807 KL |
779 | } |
780 | } | |
781 | ||
782 | return; | |
783 | } | |
784 | ||
785 | // These keystrokes will typically not be seen unless a subclass | |
786 | // overrides onMenu() due to how TApplication dispatches | |
787 | // accelerators. | |
788 | ||
789 | // Ctrl-W - close window | |
790 | if (keypress.equals(kbCtrlW)) { | |
791 | application.closeWindow(this); | |
792 | return; | |
793 | } | |
794 | ||
795 | // F6 - behave like Alt-TAB | |
796 | if (keypress.equals(kbF6)) { | |
797 | application.switchWindow(true); | |
798 | return; | |
799 | } | |
800 | ||
801 | // Shift-F6 - behave like Shift-Alt-TAB | |
802 | if (keypress.equals(kbShiftF6)) { | |
803 | application.switchWindow(false); | |
804 | return; | |
805 | } | |
806 | ||
807 | // F5 - zoom | |
808 | if (keypress.equals(kbF5)) { | |
809 | if (maximized) { | |
810 | restore(); | |
811 | } else { | |
812 | maximize(); | |
813 | } | |
814 | } | |
815 | ||
816 | // Ctrl-F5 - size/move | |
817 | if (keypress.equals(kbCtrlF5)) { | |
818 | inKeyboardResize = !inKeyboardResize; | |
819 | } | |
820 | ||
821 | // I didn't take it, pass it on to my children | |
822 | super.onKeypress(keypress); | |
823 | } | |
824 | ||
825 | /** | |
826 | * Handle posted command events. | |
827 | * | |
828 | * @param command command event | |
829 | */ | |
830 | @Override | |
831 | public void onCommand(final TCommandEvent command) { | |
832 | ||
833 | // These commands will typically not be seen unless a subclass | |
834 | // overrides onMenu() due to how TApplication dispatches | |
835 | // accelerators. | |
836 | ||
837 | if (command.equals(cmWindowClose)) { | |
838 | application.closeWindow(this); | |
839 | return; | |
840 | } | |
841 | ||
842 | if (command.equals(cmWindowNext)) { | |
843 | application.switchWindow(true); | |
844 | return; | |
845 | } | |
846 | ||
847 | if (command.equals(cmWindowPrevious)) { | |
848 | application.switchWindow(false); | |
849 | return; | |
850 | } | |
851 | ||
852 | if (command.equals(cmWindowMove)) { | |
853 | inKeyboardResize = true; | |
854 | return; | |
855 | } | |
856 | ||
857 | if (command.equals(cmWindowZoom)) { | |
858 | if (maximized) { | |
859 | restore(); | |
860 | } else { | |
861 | maximize(); | |
862 | } | |
863 | } | |
864 | ||
865 | // I didn't take it, pass it on to my children | |
866 | super.onCommand(command); | |
867 | } | |
868 | ||
869 | /** | |
870 | * Handle posted menu events. | |
871 | * | |
872 | * @param menu menu event | |
873 | */ | |
874 | @Override | |
875 | public void onMenu(final TMenuEvent menu) { | |
876 | if (menu.getId() == TMenu.MID_WINDOW_CLOSE) { | |
877 | application.closeWindow(this); | |
878 | return; | |
879 | } | |
880 | ||
881 | if (menu.getId() == TMenu.MID_WINDOW_NEXT) { | |
882 | application.switchWindow(true); | |
883 | return; | |
884 | } | |
885 | ||
886 | if (menu.getId() == TMenu.MID_WINDOW_PREVIOUS) { | |
887 | application.switchWindow(false); | |
888 | return; | |
889 | } | |
890 | ||
891 | if (menu.getId() == TMenu.MID_WINDOW_MOVE) { | |
892 | inKeyboardResize = true; | |
893 | return; | |
894 | } | |
895 | ||
896 | if (menu.getId() == TMenu.MID_WINDOW_ZOOM) { | |
897 | if (maximized) { | |
898 | restore(); | |
899 | } else { | |
900 | maximize(); | |
901 | } | |
902 | return; | |
903 | } | |
904 | ||
905 | // I didn't take it, pass it on to my children | |
906 | super.onMenu(menu); | |
907 | } | |
908 | ||
909 | // ------------------------------------------------------------------------ | |
910 | // Passthru for Screen functions ------------------------------------------ | |
911 | // ------------------------------------------------------------------------ | |
912 | ||
913 | /** | |
914 | * Get the attributes at one location. | |
915 | * | |
916 | * @param x column coordinate. 0 is the left-most column. | |
917 | * @param y row coordinate. 0 is the top-most row. | |
918 | * @return attributes at (x, y) | |
919 | */ | |
920 | public final CellAttributes getAttrXY(final int x, final int y) { | |
921 | return getScreen().getAttrXY(x, y); | |
922 | } | |
923 | ||
924 | /** | |
925 | * Set the attributes at one location. | |
926 | * | |
927 | * @param x column coordinate. 0 is the left-most column. | |
928 | * @param y row coordinate. 0 is the top-most row. | |
929 | * @param attr attributes to use (bold, foreColor, backColor) | |
930 | */ | |
931 | public final void putAttrXY(final int x, final int y, | |
932 | final CellAttributes attr) { | |
933 | ||
934 | getScreen().putAttrXY(x, y, attr); | |
935 | } | |
936 | ||
937 | /** | |
938 | * Set the attributes at one location. | |
939 | * | |
940 | * @param x column coordinate. 0 is the left-most column. | |
941 | * @param y row coordinate. 0 is the top-most row. | |
942 | * @param attr attributes to use (bold, foreColor, backColor) | |
943 | * @param clip if true, honor clipping/offset | |
944 | */ | |
945 | public final void putAttrXY(final int x, final int y, | |
946 | final CellAttributes attr, final boolean clip) { | |
947 | ||
948 | getScreen().putAttrXY(x, y, attr, clip); | |
949 | } | |
950 | ||
951 | /** | |
952 | * Fill the entire screen with one character with attributes. | |
953 | * | |
954 | * @param ch character to draw | |
955 | * @param attr attributes to use (bold, foreColor, backColor) | |
956 | */ | |
957 | public final void putAll(final char ch, final CellAttributes attr) { | |
958 | getScreen().putAll(ch, attr); | |
959 | } | |
960 | ||
961 | /** | |
962 | * Render one character with attributes. | |
963 | * | |
964 | * @param x column coordinate. 0 is the left-most column. | |
965 | * @param y row coordinate. 0 is the top-most row. | |
966 | * @param ch character + attributes to draw | |
967 | */ | |
968 | public final void putCharXY(final int x, final int y, final Cell ch) { | |
969 | getScreen().putCharXY(x, y, ch); | |
970 | } | |
971 | ||
972 | /** | |
973 | * Render one character with attributes. | |
974 | * | |
975 | * @param x column coordinate. 0 is the left-most column. | |
976 | * @param y row coordinate. 0 is the top-most row. | |
977 | * @param ch character to draw | |
978 | * @param attr attributes to use (bold, foreColor, backColor) | |
979 | */ | |
980 | public final void putCharXY(final int x, final int y, final char ch, | |
981 | final CellAttributes attr) { | |
982 | ||
983 | getScreen().putCharXY(x, y, ch, attr); | |
984 | } | |
985 | ||
986 | /** | |
987 | * Render one character without changing the underlying attributes. | |
988 | * | |
989 | * @param x column coordinate. 0 is the left-most column. | |
990 | * @param y row coordinate. 0 is the top-most row. | |
991 | * @param ch character to draw | |
992 | */ | |
993 | public final void putCharXY(final int x, final int y, final char ch) { | |
994 | getScreen().putCharXY(x, y, ch); | |
995 | } | |
996 | ||
997 | /** | |
998 | * Render a string. Does not wrap if the string exceeds the line. | |
999 | * | |
1000 | * @param x column coordinate. 0 is the left-most column. | |
1001 | * @param y row coordinate. 0 is the top-most row. | |
1002 | * @param str string to draw | |
1003 | * @param attr attributes to use (bold, foreColor, backColor) | |
1004 | */ | |
1005 | public final void putStrXY(final int x, final int y, final String str, | |
1006 | final CellAttributes attr) { | |
1007 | ||
1008 | getScreen().putStrXY(x, y, str, attr); | |
1009 | } | |
1010 | ||
1011 | /** | |
1012 | * Render a string without changing the underlying attribute. Does not | |
1013 | * wrap if the string exceeds the line. | |
1014 | * | |
1015 | * @param x column coordinate. 0 is the left-most column. | |
1016 | * @param y row coordinate. 0 is the top-most row. | |
1017 | * @param str string to draw | |
1018 | */ | |
1019 | public final void putStrXY(final int x, final int y, final String str) { | |
1020 | getScreen().putStrXY(x, y, str); | |
1021 | } | |
1022 | ||
1023 | /** | |
1024 | * Draw a vertical line from (x, y) to (x, y + n). | |
1025 | * | |
1026 | * @param x column coordinate. 0 is the left-most column. | |
1027 | * @param y row coordinate. 0 is the top-most row. | |
1028 | * @param n number of characters to draw | |
1029 | * @param ch character to draw | |
1030 | * @param attr attributes to use (bold, foreColor, backColor) | |
1031 | */ | |
1032 | public final void vLineXY(final int x, final int y, final int n, | |
1033 | final char ch, final CellAttributes attr) { | |
1034 | ||
1035 | getScreen().vLineXY(x, y, n, ch, attr); | |
1036 | } | |
1037 | ||
1038 | /** | |
1039 | * Draw a horizontal line from (x, y) to (x + n, y). | |
1040 | * | |
1041 | * @param x column coordinate. 0 is the left-most column. | |
1042 | * @param y row coordinate. 0 is the top-most row. | |
1043 | * @param n number of characters to draw | |
1044 | * @param ch character to draw | |
1045 | * @param attr attributes to use (bold, foreColor, backColor) | |
1046 | */ | |
1047 | public final void hLineXY(final int x, final int y, final int n, | |
1048 | final char ch, final CellAttributes attr) { | |
1049 | ||
1050 | getScreen().hLineXY(x, y, n, ch, attr); | |
1051 | } | |
1052 | ||
1053 | ||
1054 | } |