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