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