| 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; |
| 42 | import jexer.menu.TMenu; |
| 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 | */ |
| 49 | public class TWindow extends TWidget { |
| 50 | |
| 51 | /** |
| 52 | * Window's parent TApplication. |
| 53 | */ |
| 54 | private TApplication application; |
| 55 | |
| 56 | /** |
| 57 | * Get this TWindow's parent TApplication. |
| 58 | * |
| 59 | * @return this TWindow's parent TApplication |
| 60 | */ |
| 61 | @Override |
| 62 | public final TApplication getApplication() { |
| 63 | return application; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get the Screen. |
| 68 | * |
| 69 | * @return the Screen |
| 70 | */ |
| 71 | @Override |
| 72 | public final Screen getScreen() { |
| 73 | return application.getScreen(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Window title. |
| 78 | */ |
| 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 | } |
| 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 | |
| 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 | |
| 142 | /** |
| 143 | * If true, then the user clicked on the title bar and is moving the |
| 144 | * window. |
| 145 | */ |
| 146 | protected 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 | protected 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 | */ |
| 168 | protected TMouseEvent mouse; |
| 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 | |
| 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 | |
| 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 | |
| 259 | super(); |
| 260 | |
| 261 | // I am my own window and parent |
| 262 | setupForTWindow(this, x, y + application.getDesktopTop(), |
| 263 | width, height); |
| 264 | |
| 265 | // Save fields |
| 266 | this.title = title; |
| 267 | this.application = application; |
| 268 | this.flags = flags; |
| 269 | |
| 270 | // Minimum width/height are 10 and 2 |
| 271 | assert (width >= 10); |
| 272 | assert (getHeight() >= 2); |
| 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) { |
| 291 | if (getWidth() < getScreen().getWidth()) { |
| 292 | setX((getScreen().getWidth() - getWidth()) / 2); |
| 293 | } else { |
| 294 | setX(0); |
| 295 | } |
| 296 | setY(((application.getDesktopBottom() |
| 297 | - application.getDesktopTop()) - getHeight()) / 2); |
| 298 | if (getY() < 0) { |
| 299 | setY(0); |
| 300 | } |
| 301 | setY(getY() + application.getDesktopTop()); |
| 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 | |
| 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) |
| 324 | && (mouse.getAbsoluteY() == getY()) |
| 325 | && (mouse.getAbsoluteX() == getX() + 3) |
| 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() |
| 340 | && (mouse.getAbsoluteY() == getY()) |
| 341 | && (mouse.getAbsoluteX() == getX() + getWidth() - 4) |
| 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) |
| 359 | && (mouse.getAbsoluteY() == getY() + getHeight() - 1) |
| 360 | && ((mouse.getAbsoluteX() == getX() + getWidth() - 1) |
| 361 | || (mouse.getAbsoluteX() == getX() + getWidth() - 2)) |
| 362 | ) { |
| 363 | return true; |
| 364 | } |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Retrieve the background color. |
| 370 | * |
| 371 | * @return the background color |
| 372 | */ |
| 373 | public final CellAttributes getBackground() { |
| 374 | if (!isModal() |
| 375 | && (inWindowMove || inWindowResize || inKeyboardResize) |
| 376 | ) { |
| 377 | assert (isActive()); |
| 378 | return getTheme().getColor("twindow.background.windowmove"); |
| 379 | } else if (isModal() && inWindowMove) { |
| 380 | assert (isActive()); |
| 381 | return getTheme().getColor("twindow.background.modal"); |
| 382 | } else if (isModal()) { |
| 383 | if (isActive()) { |
| 384 | return getTheme().getColor("twindow.background.modal"); |
| 385 | } |
| 386 | return getTheme().getColor("twindow.background.modal.inactive"); |
| 387 | } else if (isActive()) { |
| 388 | assert (!isModal()); |
| 389 | return getTheme().getColor("twindow.background"); |
| 390 | } else { |
| 391 | assert (!isModal()); |
| 392 | return getTheme().getColor("twindow.background.inactive"); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Retrieve the border color. |
| 398 | * |
| 399 | * @return the border color |
| 400 | */ |
| 401 | private CellAttributes getBorder() { |
| 402 | if (!isModal() |
| 403 | && (inWindowMove || inWindowResize || inKeyboardResize) |
| 404 | ) { |
| 405 | assert (isActive()); |
| 406 | return getTheme().getColor("twindow.border.windowmove"); |
| 407 | } else if (isModal() && inWindowMove) { |
| 408 | assert (isActive()); |
| 409 | return getTheme().getColor("twindow.border.modal.windowmove"); |
| 410 | } else if (isModal()) { |
| 411 | if (isActive()) { |
| 412 | return getTheme().getColor("twindow.border.modal"); |
| 413 | } else { |
| 414 | return getTheme().getColor("twindow.border.modal.inactive"); |
| 415 | } |
| 416 | } else if (isActive()) { |
| 417 | assert (!isModal()); |
| 418 | return getTheme().getColor("twindow.border"); |
| 419 | } else { |
| 420 | assert (!isModal()); |
| 421 | return getTheme().getColor("twindow.border.inactive"); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Retrieve the border line type. |
| 427 | * |
| 428 | * @return the border line type |
| 429 | */ |
| 430 | private int getBorderType() { |
| 431 | if (!isModal() |
| 432 | && (inWindowMove || inWindowResize || inKeyboardResize) |
| 433 | ) { |
| 434 | assert (isActive()); |
| 435 | return 1; |
| 436 | } else if (isModal() && inWindowMove) { |
| 437 | assert (isActive()); |
| 438 | return 1; |
| 439 | } else if (isModal()) { |
| 440 | if (isActive()) { |
| 441 | return 2; |
| 442 | } else { |
| 443 | return 1; |
| 444 | } |
| 445 | } else if (isActive()) { |
| 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 application.switchWindow() when this window gets the |
| 462 | * focus, and also by application.addWindow(). |
| 463 | */ |
| 464 | public void onFocus() { |
| 465 | // Default: do nothing |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Called by application.switchWindow() when another window gets the |
| 470 | * focus. |
| 471 | */ |
| 472 | public void onUnfocus() { |
| 473 | // Default: do nothing |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Called by TApplication.drawChildren() to render on screen. |
| 478 | */ |
| 479 | @Override |
| 480 | public void draw() { |
| 481 | // Draw the box and background first. |
| 482 | CellAttributes border = getBorder(); |
| 483 | CellAttributes background = getBackground(); |
| 484 | int borderType = getBorderType(); |
| 485 | |
| 486 | getScreen().drawBox(0, 0, getWidth(), getHeight(), border, |
| 487 | background, borderType, true); |
| 488 | |
| 489 | // Draw the title |
| 490 | int titleLeft = (getWidth() - title.length() - 2) / 2; |
| 491 | putCharXY(titleLeft, 0, ' ', border); |
| 492 | putStringXY(titleLeft + 1, 0, title); |
| 493 | putCharXY(titleLeft + title.length() + 1, 0, ' ', border); |
| 494 | |
| 495 | if (isActive()) { |
| 496 | |
| 497 | // Draw the close button |
| 498 | putCharXY(2, 0, '[', border); |
| 499 | putCharXY(4, 0, ']', border); |
| 500 | if (mouseOnClose() && mouse.isMouse1()) { |
| 501 | putCharXY(3, 0, GraphicsChars.CP437[0x0F], |
| 502 | !isModal() |
| 503 | ? getTheme().getColor("twindow.border.windowmove") |
| 504 | : getTheme().getColor("twindow.border.modal.windowmove")); |
| 505 | } else { |
| 506 | putCharXY(3, 0, GraphicsChars.CP437[0xFE], |
| 507 | !isModal() |
| 508 | ? getTheme().getColor("twindow.border.windowmove") |
| 509 | : getTheme().getColor("twindow.border.modal.windowmove")); |
| 510 | } |
| 511 | |
| 512 | // Draw the maximize button |
| 513 | if (!isModal()) { |
| 514 | |
| 515 | putCharXY(getWidth() - 5, 0, '[', border); |
| 516 | putCharXY(getWidth() - 3, 0, ']', border); |
| 517 | if (mouseOnMaximize() && mouse.isMouse1()) { |
| 518 | putCharXY(getWidth() - 4, 0, GraphicsChars.CP437[0x0F], |
| 519 | getTheme().getColor("twindow.border.windowmove")); |
| 520 | } else { |
| 521 | if (maximized) { |
| 522 | putCharXY(getWidth() - 4, 0, GraphicsChars.CP437[0x12], |
| 523 | getTheme().getColor("twindow.border.windowmove")); |
| 524 | } else { |
| 525 | putCharXY(getWidth() - 4, 0, GraphicsChars.UPARROW, |
| 526 | getTheme().getColor("twindow.border.windowmove")); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | // Draw the resize corner |
| 531 | if ((flags & RESIZABLE) != 0) { |
| 532 | putCharXY(getWidth() - 2, getHeight() - 1, |
| 533 | GraphicsChars.SINGLE_BAR, |
| 534 | getTheme().getColor("twindow.border.windowmove")); |
| 535 | putCharXY(getWidth() - 1, getHeight() - 1, |
| 536 | GraphicsChars.LRCORNER, |
| 537 | getTheme().getColor("twindow.border.windowmove")); |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Handle mouse button presses. |
| 545 | * |
| 546 | * @param mouse mouse button event |
| 547 | */ |
| 548 | @Override |
| 549 | public void onMouseDown(final TMouseEvent mouse) { |
| 550 | this.mouse = mouse; |
| 551 | |
| 552 | inKeyboardResize = false; |
| 553 | |
| 554 | if ((mouse.getAbsoluteY() == getY()) |
| 555 | && mouse.isMouse1() |
| 556 | && (getX() <= mouse.getAbsoluteX()) |
| 557 | && (mouse.getAbsoluteX() < getX() + getWidth()) |
| 558 | && !mouseOnClose() |
| 559 | && !mouseOnMaximize() |
| 560 | ) { |
| 561 | // Begin moving window |
| 562 | inWindowMove = true; |
| 563 | moveWindowMouseX = mouse.getAbsoluteX(); |
| 564 | moveWindowMouseY = mouse.getAbsoluteY(); |
| 565 | oldWindowX = getX(); |
| 566 | oldWindowY = getY(); |
| 567 | if (maximized) { |
| 568 | maximized = false; |
| 569 | } |
| 570 | return; |
| 571 | } |
| 572 | if (mouseOnResize()) { |
| 573 | // Begin window resize |
| 574 | inWindowResize = true; |
| 575 | moveWindowMouseX = mouse.getAbsoluteX(); |
| 576 | moveWindowMouseY = mouse.getAbsoluteY(); |
| 577 | resizeWindowWidth = getWidth(); |
| 578 | resizeWindowHeight = getHeight(); |
| 579 | if (maximized) { |
| 580 | maximized = false; |
| 581 | } |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | // I didn't take it, pass it on to my children |
| 586 | super.onMouseDown(mouse); |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Maximize window. |
| 591 | */ |
| 592 | private void maximize() { |
| 593 | restoreWindowWidth = getWidth(); |
| 594 | restoreWindowHeight = getHeight(); |
| 595 | restoreWindowX = getX(); |
| 596 | restoreWindowY = getY(); |
| 597 | setWidth(getScreen().getWidth()); |
| 598 | setHeight(application.getDesktopBottom() - 1); |
| 599 | setX(0); |
| 600 | setY(1); |
| 601 | maximized = true; |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * Restote (unmaximize) window. |
| 606 | */ |
| 607 | private void restore() { |
| 608 | setWidth(restoreWindowWidth); |
| 609 | setHeight(restoreWindowHeight); |
| 610 | setX(restoreWindowX); |
| 611 | setY(restoreWindowY); |
| 612 | maximized = false; |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * Handle mouse button releases. |
| 617 | * |
| 618 | * @param mouse mouse button release event |
| 619 | */ |
| 620 | @Override |
| 621 | public void onMouseUp(final TMouseEvent mouse) { |
| 622 | this.mouse = mouse; |
| 623 | |
| 624 | if ((inWindowMove) && (mouse.isMouse1())) { |
| 625 | // Stop moving window |
| 626 | inWindowMove = false; |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | if ((inWindowResize) && (mouse.isMouse1())) { |
| 631 | // Stop resizing window |
| 632 | inWindowResize = false; |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | if (mouse.isMouse1() && mouseOnClose()) { |
| 637 | // Close window |
| 638 | application.closeWindow(this); |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | if ((mouse.getAbsoluteY() == getY()) |
| 643 | && mouse.isMouse1() |
| 644 | && mouseOnMaximize()) { |
| 645 | if (maximized) { |
| 646 | // Restore |
| 647 | restore(); |
| 648 | } else { |
| 649 | // Maximize |
| 650 | maximize(); |
| 651 | } |
| 652 | // Pass a resize event to my children |
| 653 | onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, |
| 654 | getWidth(), getHeight())); |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | // I didn't take it, pass it on to my children |
| 659 | super.onMouseUp(mouse); |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * Handle mouse movements. |
| 664 | * |
| 665 | * @param mouse mouse motion event |
| 666 | */ |
| 667 | @Override |
| 668 | public void onMouseMotion(final TMouseEvent mouse) { |
| 669 | this.mouse = mouse; |
| 670 | |
| 671 | if (inWindowMove) { |
| 672 | // Move window over |
| 673 | setX(oldWindowX + (mouse.getAbsoluteX() - moveWindowMouseX)); |
| 674 | setY(oldWindowY + (mouse.getAbsoluteY() - moveWindowMouseY)); |
| 675 | // Don't cover up the menu bar |
| 676 | if (getY() < application.getDesktopTop()) { |
| 677 | setY(application.getDesktopTop()); |
| 678 | } |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | if (inWindowResize) { |
| 683 | // Move window over |
| 684 | setWidth(resizeWindowWidth + (mouse.getAbsoluteX() |
| 685 | - moveWindowMouseX)); |
| 686 | setHeight(resizeWindowHeight + (mouse.getAbsoluteY() |
| 687 | - moveWindowMouseY)); |
| 688 | if (getX() + getWidth() > getScreen().getWidth()) { |
| 689 | setWidth(getScreen().getWidth() - getX()); |
| 690 | } |
| 691 | if (getY() + getHeight() > application.getDesktopBottom()) { |
| 692 | setY(application.getDesktopBottom() - getHeight() + 1); |
| 693 | } |
| 694 | // Don't cover up the menu bar |
| 695 | if (getY() < application.getDesktopTop()) { |
| 696 | setY(application.getDesktopTop()); |
| 697 | } |
| 698 | |
| 699 | // Keep within min/max bounds |
| 700 | if (getWidth() < minimumWindowWidth) { |
| 701 | setWidth(minimumWindowWidth); |
| 702 | inWindowResize = false; |
| 703 | } |
| 704 | if (getHeight() < minimumWindowHeight) { |
| 705 | setHeight(minimumWindowHeight); |
| 706 | inWindowResize = false; |
| 707 | } |
| 708 | if ((maximumWindowWidth > 0) |
| 709 | && (getWidth() > maximumWindowWidth) |
| 710 | ) { |
| 711 | setWidth(maximumWindowWidth); |
| 712 | inWindowResize = false; |
| 713 | } |
| 714 | if ((maximumWindowHeight > 0) |
| 715 | && (getHeight() > maximumWindowHeight) |
| 716 | ) { |
| 717 | setHeight(maximumWindowHeight); |
| 718 | inWindowResize = false; |
| 719 | } |
| 720 | |
| 721 | // Pass a resize event to my children |
| 722 | onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, |
| 723 | getWidth(), getHeight())); |
| 724 | return; |
| 725 | } |
| 726 | |
| 727 | // I didn't take it, pass it on to my children |
| 728 | super.onMouseMotion(mouse); |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * Handle keystrokes. |
| 733 | * |
| 734 | * @param keypress keystroke event |
| 735 | */ |
| 736 | @Override |
| 737 | public void onKeypress(final TKeypressEvent keypress) { |
| 738 | |
| 739 | if (inKeyboardResize) { |
| 740 | |
| 741 | // ESC - Exit size/move |
| 742 | if (keypress.equals(kbEsc)) { |
| 743 | inKeyboardResize = false; |
| 744 | } |
| 745 | |
| 746 | if (keypress.equals(kbLeft)) { |
| 747 | if (getX() > 0) { |
| 748 | setX(getX() - 1); |
| 749 | } |
| 750 | } |
| 751 | if (keypress.equals(kbRight)) { |
| 752 | if (getX() < getScreen().getWidth() - 1) { |
| 753 | setX(getX() + 1); |
| 754 | } |
| 755 | } |
| 756 | if (keypress.equals(kbDown)) { |
| 757 | if (getY() < application.getDesktopBottom() - 1) { |
| 758 | setY(getY() + 1); |
| 759 | } |
| 760 | } |
| 761 | if (keypress.equals(kbUp)) { |
| 762 | if (getY() > 1) { |
| 763 | setY(getY() - 1); |
| 764 | } |
| 765 | } |
| 766 | if (keypress.equals(kbShiftLeft)) { |
| 767 | if ((getWidth() > minimumWindowWidth) |
| 768 | || (minimumWindowWidth <= 0) |
| 769 | ) { |
| 770 | setWidth(getWidth() - 1); |
| 771 | } |
| 772 | } |
| 773 | if (keypress.equals(kbShiftRight)) { |
| 774 | if ((getWidth() < maximumWindowWidth) |
| 775 | || (maximumWindowWidth <= 0) |
| 776 | ) { |
| 777 | setWidth(getWidth() + 1); |
| 778 | } |
| 779 | } |
| 780 | if (keypress.equals(kbShiftUp)) { |
| 781 | if ((getHeight() > minimumWindowHeight) |
| 782 | || (minimumWindowHeight <= 0) |
| 783 | ) { |
| 784 | setHeight(getHeight() - 1); |
| 785 | } |
| 786 | } |
| 787 | if (keypress.equals(kbShiftDown)) { |
| 788 | if ((getHeight() < maximumWindowHeight) |
| 789 | || (maximumWindowHeight <= 0) |
| 790 | ) { |
| 791 | setHeight(getHeight() + 1); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | // Pass a resize event to my children |
| 796 | onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, |
| 797 | getWidth(), getHeight())); |
| 798 | |
| 799 | return; |
| 800 | } |
| 801 | |
| 802 | // These keystrokes will typically not be seen unless a subclass |
| 803 | // overrides onMenu() due to how TApplication dispatches |
| 804 | // accelerators. |
| 805 | |
| 806 | // Ctrl-W - close window |
| 807 | if (keypress.equals(kbCtrlW)) { |
| 808 | application.closeWindow(this); |
| 809 | return; |
| 810 | } |
| 811 | |
| 812 | // F6 - behave like Alt-TAB |
| 813 | if (keypress.equals(kbF6)) { |
| 814 | application.switchWindow(true); |
| 815 | return; |
| 816 | } |
| 817 | |
| 818 | // Shift-F6 - behave like Shift-Alt-TAB |
| 819 | if (keypress.equals(kbShiftF6)) { |
| 820 | application.switchWindow(false); |
| 821 | return; |
| 822 | } |
| 823 | |
| 824 | // F5 - zoom |
| 825 | if (keypress.equals(kbF5)) { |
| 826 | if (maximized) { |
| 827 | restore(); |
| 828 | } else { |
| 829 | maximize(); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | // Ctrl-F5 - size/move |
| 834 | if (keypress.equals(kbCtrlF5)) { |
| 835 | inKeyboardResize = !inKeyboardResize; |
| 836 | } |
| 837 | |
| 838 | // I didn't take it, pass it on to my children |
| 839 | super.onKeypress(keypress); |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * Handle posted command events. |
| 844 | * |
| 845 | * @param command command event |
| 846 | */ |
| 847 | @Override |
| 848 | public void onCommand(final TCommandEvent command) { |
| 849 | |
| 850 | // These commands will typically not be seen unless a subclass |
| 851 | // overrides onMenu() due to how TApplication dispatches |
| 852 | // accelerators. |
| 853 | |
| 854 | if (command.equals(cmWindowClose)) { |
| 855 | application.closeWindow(this); |
| 856 | return; |
| 857 | } |
| 858 | |
| 859 | if (command.equals(cmWindowNext)) { |
| 860 | application.switchWindow(true); |
| 861 | return; |
| 862 | } |
| 863 | |
| 864 | if (command.equals(cmWindowPrevious)) { |
| 865 | application.switchWindow(false); |
| 866 | return; |
| 867 | } |
| 868 | |
| 869 | if (command.equals(cmWindowMove)) { |
| 870 | inKeyboardResize = true; |
| 871 | return; |
| 872 | } |
| 873 | |
| 874 | if (command.equals(cmWindowZoom)) { |
| 875 | if (maximized) { |
| 876 | restore(); |
| 877 | } else { |
| 878 | maximize(); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | // I didn't take it, pass it on to my children |
| 883 | super.onCommand(command); |
| 884 | } |
| 885 | |
| 886 | /** |
| 887 | * Handle posted menu events. |
| 888 | * |
| 889 | * @param menu menu event |
| 890 | */ |
| 891 | @Override |
| 892 | public void onMenu(final TMenuEvent menu) { |
| 893 | if (menu.getId() == TMenu.MID_WINDOW_CLOSE) { |
| 894 | application.closeWindow(this); |
| 895 | return; |
| 896 | } |
| 897 | |
| 898 | if (menu.getId() == TMenu.MID_WINDOW_NEXT) { |
| 899 | application.switchWindow(true); |
| 900 | return; |
| 901 | } |
| 902 | |
| 903 | if (menu.getId() == TMenu.MID_WINDOW_PREVIOUS) { |
| 904 | application.switchWindow(false); |
| 905 | return; |
| 906 | } |
| 907 | |
| 908 | if (menu.getId() == TMenu.MID_WINDOW_MOVE) { |
| 909 | inKeyboardResize = true; |
| 910 | return; |
| 911 | } |
| 912 | |
| 913 | if (menu.getId() == TMenu.MID_WINDOW_ZOOM) { |
| 914 | if (maximized) { |
| 915 | restore(); |
| 916 | } else { |
| 917 | maximize(); |
| 918 | } |
| 919 | return; |
| 920 | } |
| 921 | |
| 922 | // I didn't take it, pass it on to my children |
| 923 | super.onMenu(menu); |
| 924 | } |
| 925 | |
| 926 | // ------------------------------------------------------------------------ |
| 927 | // Passthru for Screen functions ------------------------------------------ |
| 928 | // ------------------------------------------------------------------------ |
| 929 | |
| 930 | /** |
| 931 | * Get the attributes at one location. |
| 932 | * |
| 933 | * @param x column coordinate. 0 is the left-most column. |
| 934 | * @param y row coordinate. 0 is the top-most row. |
| 935 | * @return attributes at (x, y) |
| 936 | */ |
| 937 | public final CellAttributes getAttrXY(final int x, final int y) { |
| 938 | return getScreen().getAttrXY(x, y); |
| 939 | } |
| 940 | |
| 941 | /** |
| 942 | * Set the attributes at one location. |
| 943 | * |
| 944 | * @param x column coordinate. 0 is the left-most column. |
| 945 | * @param y row coordinate. 0 is the top-most row. |
| 946 | * @param attr attributes to use (bold, foreColor, backColor) |
| 947 | */ |
| 948 | public final void putAttrXY(final int x, final int y, |
| 949 | final CellAttributes attr) { |
| 950 | |
| 951 | getScreen().putAttrXY(x, y, attr); |
| 952 | } |
| 953 | |
| 954 | /** |
| 955 | * Set the attributes at one location. |
| 956 | * |
| 957 | * @param x column coordinate. 0 is the left-most column. |
| 958 | * @param y row coordinate. 0 is the top-most row. |
| 959 | * @param attr attributes to use (bold, foreColor, backColor) |
| 960 | * @param clip if true, honor clipping/offset |
| 961 | */ |
| 962 | public final void putAttrXY(final int x, final int y, |
| 963 | final CellAttributes attr, final boolean clip) { |
| 964 | |
| 965 | getScreen().putAttrXY(x, y, attr, clip); |
| 966 | } |
| 967 | |
| 968 | /** |
| 969 | * Fill the entire screen with one character with attributes. |
| 970 | * |
| 971 | * @param ch character to draw |
| 972 | * @param attr attributes to use (bold, foreColor, backColor) |
| 973 | */ |
| 974 | public final void putAll(final char ch, final CellAttributes attr) { |
| 975 | getScreen().putAll(ch, attr); |
| 976 | } |
| 977 | |
| 978 | /** |
| 979 | * Render one character with attributes. |
| 980 | * |
| 981 | * @param x column coordinate. 0 is the left-most column. |
| 982 | * @param y row coordinate. 0 is the top-most row. |
| 983 | * @param ch character + attributes to draw |
| 984 | */ |
| 985 | public final void putCharXY(final int x, final int y, final Cell ch) { |
| 986 | getScreen().putCharXY(x, y, ch); |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * Render one character with attributes. |
| 991 | * |
| 992 | * @param x column coordinate. 0 is the left-most column. |
| 993 | * @param y row coordinate. 0 is the top-most row. |
| 994 | * @param ch character to draw |
| 995 | * @param attr attributes to use (bold, foreColor, backColor) |
| 996 | */ |
| 997 | public final void putCharXY(final int x, final int y, final char ch, |
| 998 | final CellAttributes attr) { |
| 999 | |
| 1000 | getScreen().putCharXY(x, y, ch, attr); |
| 1001 | } |
| 1002 | |
| 1003 | /** |
| 1004 | * Render one character without changing the underlying attributes. |
| 1005 | * |
| 1006 | * @param x column coordinate. 0 is the left-most column. |
| 1007 | * @param y row coordinate. 0 is the top-most row. |
| 1008 | * @param ch character to draw |
| 1009 | */ |
| 1010 | public final void putCharXY(final int x, final int y, final char ch) { |
| 1011 | getScreen().putCharXY(x, y, ch); |
| 1012 | } |
| 1013 | |
| 1014 | /** |
| 1015 | * Render a string. Does not wrap if the string exceeds the line. |
| 1016 | * |
| 1017 | * @param x column coordinate. 0 is the left-most column. |
| 1018 | * @param y row coordinate. 0 is the top-most row. |
| 1019 | * @param str string to draw |
| 1020 | * @param attr attributes to use (bold, foreColor, backColor) |
| 1021 | */ |
| 1022 | public final void putStringXY(final int x, final int y, final String str, |
| 1023 | final CellAttributes attr) { |
| 1024 | |
| 1025 | getScreen().putStringXY(x, y, str, attr); |
| 1026 | } |
| 1027 | |
| 1028 | /** |
| 1029 | * Render a string without changing the underlying attribute. Does not |
| 1030 | * wrap if the string exceeds the line. |
| 1031 | * |
| 1032 | * @param x column coordinate. 0 is the left-most column. |
| 1033 | * @param y row coordinate. 0 is the top-most row. |
| 1034 | * @param str string to draw |
| 1035 | */ |
| 1036 | public final void putStringXY(final int x, final int y, final String str) { |
| 1037 | getScreen().putStringXY(x, y, str); |
| 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * Draw a vertical line from (x, y) to (x, y + n). |
| 1042 | * |
| 1043 | * @param x column coordinate. 0 is the left-most column. |
| 1044 | * @param y row coordinate. 0 is the top-most row. |
| 1045 | * @param n number of characters to draw |
| 1046 | * @param ch character to draw |
| 1047 | * @param attr attributes to use (bold, foreColor, backColor) |
| 1048 | */ |
| 1049 | public final void vLineXY(final int x, final int y, final int n, |
| 1050 | final char ch, final CellAttributes attr) { |
| 1051 | |
| 1052 | getScreen().vLineXY(x, y, n, ch, attr); |
| 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * Draw a horizontal line from (x, y) to (x + n, y). |
| 1057 | * |
| 1058 | * @param x column coordinate. 0 is the left-most column. |
| 1059 | * @param y row coordinate. 0 is the top-most row. |
| 1060 | * @param n number of characters to draw |
| 1061 | * @param ch character to draw |
| 1062 | * @param attr attributes to use (bold, foreColor, backColor) |
| 1063 | */ |
| 1064 | public final void hLineXY(final int x, final int y, final int n, |
| 1065 | final char ch, final CellAttributes attr) { |
| 1066 | |
| 1067 | getScreen().hLineXY(x, y, n, ch, attr); |
| 1068 | } |
| 1069 | |
| 1070 | |
| 1071 | } |