17f7849631f5bd087f536cf48f4068bf211447b7
2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
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.
10 * Copyright (C) 2015 Kevin Lamonte
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.
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.
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
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import java
.util
.List
;
34 import java
.util
.LinkedList
;
36 import jexer
.bits
.ColorTheme
;
37 import jexer
.event
.TCommandEvent
;
38 import jexer
.event
.TInputEvent
;
39 import jexer
.event
.TKeypressEvent
;
40 import jexer
.event
.TMenuEvent
;
41 import jexer
.event
.TMouseEvent
;
42 import jexer
.event
.TResizeEvent
;
43 import jexer
.io
.Screen
;
44 import jexer
.menu
.TMenu
;
45 import static jexer
.TKeypress
.*;
48 * TWidget is the base class of all objects that can be drawn on screen or
49 * handle user input events.
51 public abstract class TWidget
{
54 * Every widget has a parent widget that it may be "contained" in. For
55 * example, a TWindow might contain several TTextFields, or a TComboBox
56 * may contain a TScrollBar.
58 private TWidget parent
= null;
63 * @return parent widget
65 public final TWidget
getParent() {
70 * Backdoor access for TWindow's constructor. ONLY TWindow USES THIS.
72 * @param window the top-level window
73 * @param x column relative to parent
74 * @param y row relative to parent
75 * @param width width of window
76 * @param height height of window
78 protected final void setupForTWindow(final TWindow window
,
79 final int x
, final int y
, final int width
, final int height
) {
90 * Request full repaint on next screen refresh.
92 protected final void setRepaint() {
93 window
.getApplication().setRepaint();
97 * Get this TWidget's parent TApplication.
99 * @return the parent TApplication
101 public TApplication
getApplication() {
102 return window
.getApplication();
110 public Screen
getScreen() {
111 return window
.getScreen();
115 * Child widgets that this widget contains.
117 private List
<TWidget
> children
;
120 * Get the list of child widgets that this widget contains.
122 * @return the list of child widgets
124 public List
<TWidget
> getChildren() {
129 * The currently active child widget that will receive keypress events.
131 private TWidget activeChild
= null;
134 * If true, this widget will receive events.
136 private boolean active
= false;
141 * @return if true, this widget will receive events
143 public final boolean getActive() {
150 * @param active if true, this widget will receive events
152 public final void setActive(final boolean active
) {
153 this.active
= active
;
157 * The window that this widget draws to.
159 private TWindow window
= null;
162 * Get the window this widget is on.
166 public final TWindow
getWindow() {
171 * Absolute X position of the top-left corner.
178 * @return absolute X position of the top-left corner
180 public final int getX() {
187 * @param x absolute X position of the top-left corner
189 public final void setX(final int x
) {
194 * Absolute Y position of the top-left corner.
201 * @return absolute Y position of the top-left corner
203 public final int getY() {
210 * @param y absolute Y position of the top-left corner
212 public final void setY(final int y
) {
219 private int width
= 0;
224 * @return widget width
226 public final int getWidth() {
233 * @param width new widget width
235 public final void setWidth(final int width
) {
242 private int height
= 0;
247 * @return widget height
249 public final int getHeight() {
256 * @param height new widget height
258 public final void setHeight(final int height
) {
259 this.height
= height
;
263 * My tab order inside a window or containing widget.
265 private int tabOrder
= 0;
268 * If true, this widget can be tabbed to or receive events.
270 private boolean enabled
= true;
275 * @return if true, this widget can be tabbed to or receive events
277 public final boolean getEnabled() {
284 * @param enabled if true, this widget can be tabbed to or receive events
286 public final void setEnabled(final boolean enabled
) {
287 this.enabled
= enabled
;
290 // See if there are any active siblings to switch to
291 boolean foundSibling
= false;
292 if (parent
!= null) {
293 for (TWidget w
: parent
.children
) {
295 && !(this instanceof THScroller
)
296 && !(this instanceof TVScroller
)
304 parent
.activeChild
= null;
311 * If true, this widget has a cursor.
313 private boolean hasCursor
= false;
316 * See if this widget has a visible cursor.
318 * @return if true, this widget has a visible cursor
320 public final boolean visibleCursor() {
325 * Cursor column position in relative coordinates.
327 private int cursorX
= 0;
330 * Cursor row position in relative coordinates.
332 private int cursorY
= 0;
335 * Comparison operator sorts on tabOrder.
337 * @param that another TWidget instance
338 * @return difference between this.tabOrder and that.tabOrder
340 public final int compare(final TWidget that
) {
341 return (this.tabOrder
- that
.tabOrder
);
345 * See if this widget should render with the active color.
347 * @return true if this widget is active and all of its parents are
350 public final boolean getAbsoluteActive() {
351 if (parent
== this) {
354 return (active
&& parent
.getAbsoluteActive());
358 * Returns the cursor X position.
360 * @return absolute screen column number for the cursor's X position
362 public final int getCursorAbsoluteX() {
364 return getAbsoluteX() + cursorX
;
368 * Returns the cursor Y position.
370 * @return absolute screen row number for the cursor's Y position
372 public final int getCursorAbsoluteY() {
374 return getAbsoluteY() + cursorY
;
378 * Compute my absolute X position as the sum of my X plus all my parent's
381 * @return absolute screen column number for my X position
383 public final int getAbsoluteX() {
384 assert (parent
!= null);
385 if (parent
== this) {
388 if ((parent
instanceof TWindow
) && !(parent
instanceof TMenu
)) {
389 // Widgets on a TWindow have (0,0) as their top-left, but this is
390 // actually the TWindow's (1,1).
391 return parent
.getAbsoluteX() + x
+ 1;
393 return parent
.getAbsoluteX() + x
;
397 * Compute my absolute Y position as the sum of my Y plus all my parent's
400 * @return absolute screen row number for my Y position
402 public final int getAbsoluteY() {
403 assert (parent
!= null);
404 if (parent
== this) {
407 if ((parent
instanceof TWindow
) && !(parent
instanceof TMenu
)) {
408 // Widgets on a TWindow have (0,0) as their top-left, but this is
409 // actually the TWindow's (1,1).
410 return parent
.getAbsoluteY() + y
+ 1;
412 return parent
.getAbsoluteY() + y
;
416 * Get the global color theme.
418 * @return the ColorTheme
420 public final ColorTheme
getTheme() {
421 return window
.getApplication().getTheme();
425 * Draw my specific widget. When called, the screen rectangle I draw
426 * into is already setup (offset and clipping).
429 // Default widget draws nothing.
433 * Called by parent to render to TWindow.
435 public final void drawChildren() {
436 // Set my clipping rectangle
437 assert (window
!= null);
438 assert (window
.getScreen() != null);
439 Screen screen
= window
.getScreen();
441 screen
.setClipRight(width
);
442 screen
.setClipBottom(height
);
444 int absoluteRightEdge
= window
.getAbsoluteX() + screen
.getWidth();
445 int absoluteBottomEdge
= window
.getAbsoluteY() + screen
.getHeight();
446 if (!(this instanceof TWindow
) && !(this instanceof TVScroller
)) {
447 absoluteRightEdge
-= 1;
449 if (!(this instanceof TWindow
) && !(this instanceof THScroller
)) {
450 absoluteBottomEdge
-= 1;
452 int myRightEdge
= getAbsoluteX() + width
;
453 int myBottomEdge
= getAbsoluteY() + height
;
454 if (getAbsoluteX() > absoluteRightEdge
) {
456 screen
.setClipRight(0);
457 } else if (myRightEdge
> absoluteRightEdge
) {
458 screen
.setClipRight(screen
.getClipRight()
459 - myRightEdge
- absoluteRightEdge
);
461 if (getAbsoluteY() > absoluteBottomEdge
) {
463 screen
.setClipBottom(0);
464 } else if (myBottomEdge
> absoluteBottomEdge
) {
465 screen
.setClipBottom(screen
.getClipBottom()
466 - myBottomEdge
- absoluteBottomEdge
);
470 screen
.setOffsetX(getAbsoluteX());
471 screen
.setOffsetY(getAbsoluteY());
476 // Continue down the chain
477 for (TWidget widget
: children
) {
478 widget
.drawChildren();
483 * Default constructor for subclasses.
485 protected TWidget() {
486 children
= new LinkedList
<TWidget
>();
490 * Protected constructor.
492 * @param parent parent widget
494 protected TWidget(final TWidget parent
) {
499 * Protected constructor used by subclasses that are disabled by default.
501 * @param parent parent widget
502 * @param enabled if true assume enabled
504 protected TWidget(final TWidget parent
, final boolean enabled
) {
505 this.enabled
= enabled
;
506 this.parent
= parent
;
507 this.window
= parent
.window
;
508 children
= new LinkedList
<TWidget
>();
509 parent
.addChild(this);
513 * Add a child widget to my list of children. We set its tabOrder to 0
514 * and increment the tabOrder of all other children.
516 * @param child TWidget to add
518 private void addChild(final TWidget child
) {
522 && !(child
instanceof THScroller
)
523 && !(child
instanceof TVScroller
)
525 for (TWidget widget
: children
) {
526 widget
.active
= false;
531 for (int i
= 0; i
< children
.size(); i
++) {
532 children
.get(i
).tabOrder
= i
;
537 * Switch the active child.
539 * @param child TWidget to activate
541 public final void activate(final TWidget child
) {
542 assert (child
.enabled
);
543 if ((child
instanceof THScroller
)
544 || (child
instanceof TVScroller
)
549 if (child
!= activeChild
) {
550 if (activeChild
!= null) {
551 activeChild
.active
= false;
559 * Switch the active child.
561 * @param tabOrder tabOrder of the child to activate. If that child
562 * isn't enabled, then the next enabled child will be activated.
564 public final void activate(final int tabOrder
) {
565 if (activeChild
== null) {
568 TWidget child
= null;
569 for (TWidget widget
: children
) {
571 && !(widget
instanceof THScroller
)
572 && !(widget
instanceof TVScroller
)
573 && (widget
.tabOrder
>= tabOrder
)
579 if ((child
!= null) && (child
!= activeChild
)) {
580 activeChild
.active
= false;
581 assert (child
.enabled
);
588 * Switch the active widget with the next in the tab order.
590 * @param forward if true, then switch to the next enabled widget in the
591 * list, otherwise switch to the previous enabled widget in the list
593 public final void switchWidget(final boolean forward
) {
595 // Only switch if there are multiple enabled widgets
596 if ((children
.size() < 2) || (activeChild
== null)) {
600 int tabOrder
= activeChild
.tabOrder
;
609 // If at the end, pass the switch to my parent.
610 if ((!forward
) && (parent
!= this)) {
611 parent
.switchWidget(forward
);
615 tabOrder
= children
.size() - 1;
616 } else if (tabOrder
== children
.size()) {
617 // If at the end, pass the switch to my parent.
618 if ((forward
) && (parent
!= this)) {
619 parent
.switchWidget(forward
);
625 if (activeChild
.tabOrder
== tabOrder
) {
629 } while ((!children
.get(tabOrder
).enabled
)
630 && !(children
.get(tabOrder
) instanceof THScroller
)
631 && !(children
.get(tabOrder
) instanceof TVScroller
));
633 assert (children
.get(tabOrder
).enabled
);
635 activeChild
.active
= false;
636 children
.get(tabOrder
).active
= true;
637 activeChild
= children
.get(tabOrder
);
640 window
.getApplication().setRepaint();
644 * Returns my active widget.
646 * @return widget that is active, or this if no children
648 public TWidget
getActiveChild() {
649 if ((this instanceof THScroller
)
650 || (this instanceof TVScroller
)
655 for (TWidget widget
: children
) {
657 return widget
.getActiveChild();
660 // No active children, return me
665 * Method that subclasses can override to handle keystrokes.
667 * @param keypress keystroke event
669 public void onKeypress(final TKeypressEvent keypress
) {
671 if ((children
.size() == 0)
673 // || (cast(TTreeView)this)
674 // || (cast(TText)this)
678 // tab / shift-tab - switch to next/previous widget
679 // right-arrow or down-arrow: same as tab
680 // left-arrow or up-arrow: same as shift-tab
681 if ((keypress
.equals(kbTab
))
682 || (keypress
.equals(kbRight
))
683 || (keypress
.equals(kbDown
))
685 parent
.switchWidget(true);
687 } else if ((keypress
.equals(kbShiftTab
))
688 || (keypress
.equals(kbBackTab
))
689 || (keypress
.equals(kbLeft
))
690 || (keypress
.equals(kbUp
))
692 parent
.switchWidget(false);
697 // If I have any buttons on me AND this is an Alt-key that matches
698 // its mnemonic, send it an Enter keystroke
699 for (TWidget widget
: children
) {
703 if (TButton button = cast(TButton)w) {
704 if (button.enabled &&
705 !keypress.key.isKey &&
707 !keypress.key.ctrl &&
708 (toLowercase(button.mnemonic.shortcut) == toLowercase(keypress.key.ch))) {
710 w.handleEvent(new TKeypressEvent(kbEnter));
717 // Dispatch the keypress to an active widget
718 for (TWidget widget
: children
) {
720 window
.getApplication().setRepaint();
721 widget
.handleEvent(keypress
);
728 * Method that subclasses can override to handle mouse button presses.
730 * @param mouse mouse button event
732 public void onMouseDown(final TMouseEvent mouse
) {
733 // Default: do nothing, pass to children instead
734 for (TWidget widget
: children
) {
735 if (widget
.mouseWouldHit(mouse
)) {
736 // Dispatch to this child, also activate it
739 // Set x and y relative to the child's coordinates
740 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
741 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
742 widget
.handleEvent(mouse
);
749 * Method that subclasses can override to handle mouse button releases.
751 * @param mouse mouse button event
753 public void onMouseUp(final TMouseEvent mouse
) {
754 // Default: do nothing, pass to children instead
755 for (TWidget widget
: children
) {
756 if (widget
.mouseWouldHit(mouse
)) {
757 // Dispatch to this child, also activate it
760 // Set x and y relative to the child's coordinates
761 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
762 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
763 widget
.handleEvent(mouse
);
770 * Method that subclasses can override to handle mouse movements.
772 * @param mouse mouse motion event
774 public void onMouseMotion(final TMouseEvent mouse
) {
775 // Default: do nothing, pass it on to ALL of my children. This way
776 // the children can see the mouse "leaving" their area.
777 for (TWidget widget
: children
) {
778 // Set x and y relative to the child's coordinates
779 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
780 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
781 widget
.handleEvent(mouse
);
786 * Method that subclasses can override to handle window/screen resize
789 * @param resize resize event
791 public void onResize(final TResizeEvent resize
) {
792 // Default: do nothing, pass to children instead
793 for (TWidget widget
: children
) {
794 widget
.onResize(resize
);
799 * Method that subclasses can override to handle posted command events.
801 * @param command command event
803 public void onCommand(final TCommandEvent command
) {
804 // Default: do nothing, pass to children instead
805 for (TWidget widget
: children
) {
806 widget
.onCommand(command
);
811 * Method that subclasses can override to handle menu or posted menu
814 * @param menu menu event
816 public void onMenu(final TMenuEvent menu
) {
817 // Default: do nothing, pass to children instead
818 for (TWidget widget
: children
) {
824 * Method that subclasses can override to do processing when the UI is
827 public void onIdle() {
828 // Default: do nothing, pass to children instead
829 for (TWidget widget
: children
) {
835 * Consume event. Subclasses that want to intercept all events in one go
836 * can override this method.
838 * @param event keyboard, mouse, resize, command, or menu event
840 public void handleEvent(final TInputEvent event
) {
841 // System.err.printf("TWidget (%s) event: %s\n", this.getClass().getName(),
846 // System.err.println(" -- discard --");
850 if (event
instanceof TKeypressEvent
) {
851 onKeypress((TKeypressEvent
) event
);
852 } else if (event
instanceof TMouseEvent
) {
854 TMouseEvent mouse
= (TMouseEvent
) event
;
856 switch (mouse
.getType()) {
867 onMouseMotion(mouse
);
871 throw new IllegalArgumentException("Invalid mouse event type: "
874 } else if (event
instanceof TResizeEvent
) {
875 onResize((TResizeEvent
) event
);
876 } else if (event
instanceof TCommandEvent
) {
877 onCommand((TCommandEvent
) event
);
878 } else if (event
instanceof TMenuEvent
) {
879 onMenu((TMenuEvent
) event
);
887 * Check if a mouse press/release event coordinate is contained in this
890 * @param mouse a mouse-based event
891 * @return whether or not a mouse click would be sent to this widget
893 public final boolean mouseWouldHit(final TMouseEvent mouse
) {
899 if ((mouse
.getAbsoluteX() >= getAbsoluteX())
900 && (mouse
.getAbsoluteX() < getAbsoluteX() + width
)
901 && (mouse
.getAbsoluteY() >= getAbsoluteY())
902 && (mouse
.getAbsoluteY() < getAbsoluteY() + height
)
910 * Convenience function to add a label to this container/window.
913 * @param x column relative to parent
914 * @param y row relative to parent
915 * @return the new label
917 public final TLabel
addLabel(final String text
, final int x
, final int y
) {
918 return addLabel(text
, x
, y
, "tlabel");
922 * Convenience function to add a label to this container/window.
925 * @param x column relative to parent
926 * @param y row relative to parent
927 * @param colorKey ColorTheme key color to use for foreground text.
928 * Default is "tlabel"
929 * @return the new label
931 public final TLabel
addLabel(final String text
, final int x
, final int y
,
932 final String colorKey
) {
934 return new TLabel(this, text
, x
, y
, colorKey
);
938 * Convenience function to add a button to this container/window.
940 * @param text label on the button
941 * @param x column relative to parent
942 * @param y row relative to parent
943 * @param action to call when button is pressed
944 * @return the new button
946 public final TButton
addButton(final String text
, final int x
, final int y
,
947 final TAction action
) {
949 return new TButton(this, text
, x
, y
, action
);