84277a8c2acc8dba6d46ff018ddbfbdc232404ce
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
implements Comparable
<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 * Set visible cursor flag.
318 * @param hasCursor if true, this widget has a cursor
320 public final void setHasCursor(final boolean hasCursor
) {
321 this.hasCursor
= hasCursor
;
325 * See if this widget has a visible cursor.
327 * @return if true, this widget has a visible cursor
329 public final boolean visibleCursor() {
334 * Cursor column position in relative coordinates.
336 private int cursorX
= 0;
339 * Get cursor X value.
341 * @return cursor column position in relative coordinates
343 public final int getCursorX() {
348 * Set cursor X value.
350 * @param cursorX column position in relative coordinates
352 public final void setCursorX(final int cursorX
) {
353 this.cursorX
= cursorX
;
357 * Cursor row position in relative coordinates.
359 private int cursorY
= 0;
362 * Get cursor Y value.
364 * @return cursor row position in relative coordinates
366 public final int getCursorY() {
371 * Set cursor Y value.
373 * @param cursorY row position in relative coordinates
375 public final void setCursorY(final int cursorY
) {
376 this.cursorY
= cursorY
;
380 * Comparison operator sorts on tabOrder for TWidgets and z for TWindows.
382 * @param that another TWidget or TWindow instance
383 * @return difference between this.tabOrder and that.tabOrder, or
384 * difference between this.z and that.z
387 public final int compareTo(final TWidget that
) {
388 if ((this instanceof TWindow
)
389 && (that
instanceof TWindow
)
391 return (((TWindow
) this).getZ() - ((TWindow
) that
).getZ());
393 return (this.tabOrder
- that
.tabOrder
);
397 * See if this widget should render with the active color.
399 * @return true if this widget is active and all of its parents are
402 public final boolean getAbsoluteActive() {
403 if (parent
== this) {
406 return (active
&& parent
.getAbsoluteActive());
410 * Returns the cursor X position.
412 * @return absolute screen column number for the cursor's X position
414 public final int getCursorAbsoluteX() {
416 return getAbsoluteX() + cursorX
;
420 * Returns the cursor Y position.
422 * @return absolute screen row number for the cursor's Y position
424 public final int getCursorAbsoluteY() {
426 return getAbsoluteY() + cursorY
;
430 * Compute my absolute X position as the sum of my X plus all my parent's
433 * @return absolute screen column number for my X position
435 public final int getAbsoluteX() {
436 assert (parent
!= null);
437 if (parent
== this) {
440 if ((parent
instanceof TWindow
) && !(parent
instanceof TMenu
)) {
441 // Widgets on a TWindow have (0,0) as their top-left, but this is
442 // actually the TWindow's (1,1).
443 return parent
.getAbsoluteX() + x
+ 1;
445 return parent
.getAbsoluteX() + x
;
449 * Compute my absolute Y position as the sum of my Y plus all my parent's
452 * @return absolute screen row number for my Y position
454 public final int getAbsoluteY() {
455 assert (parent
!= null);
456 if (parent
== this) {
459 if ((parent
instanceof TWindow
) && !(parent
instanceof TMenu
)) {
460 // Widgets on a TWindow have (0,0) as their top-left, but this is
461 // actually the TWindow's (1,1).
462 return parent
.getAbsoluteY() + y
+ 1;
464 return parent
.getAbsoluteY() + y
;
468 * Get the global color theme.
470 * @return the ColorTheme
472 public final ColorTheme
getTheme() {
473 return window
.getApplication().getTheme();
477 * Draw my specific widget. When called, the screen rectangle I draw
478 * into is already setup (offset and clipping).
481 // Default widget draws nothing.
485 * Called by parent to render to TWindow.
487 public final void drawChildren() {
488 // Set my clipping rectangle
489 assert (window
!= null);
490 assert (window
.getScreen() != null);
491 Screen screen
= window
.getScreen();
493 screen
.setClipRight(width
);
494 screen
.setClipBottom(height
);
496 int absoluteRightEdge
= window
.getAbsoluteX() + screen
.getWidth();
497 int absoluteBottomEdge
= window
.getAbsoluteY() + screen
.getHeight();
498 if (!(this instanceof TWindow
) && !(this instanceof TVScroller
)) {
499 absoluteRightEdge
-= 1;
501 if (!(this instanceof TWindow
) && !(this instanceof THScroller
)) {
502 absoluteBottomEdge
-= 1;
504 int myRightEdge
= getAbsoluteX() + width
;
505 int myBottomEdge
= getAbsoluteY() + height
;
506 if (getAbsoluteX() > absoluteRightEdge
) {
508 screen
.setClipRight(0);
509 } else if (myRightEdge
> absoluteRightEdge
) {
510 screen
.setClipRight(screen
.getClipRight()
511 - myRightEdge
- absoluteRightEdge
);
513 if (getAbsoluteY() > absoluteBottomEdge
) {
515 screen
.setClipBottom(0);
516 } else if (myBottomEdge
> absoluteBottomEdge
) {
517 screen
.setClipBottom(screen
.getClipBottom()
518 - myBottomEdge
- absoluteBottomEdge
);
522 screen
.setOffsetX(getAbsoluteX());
523 screen
.setOffsetY(getAbsoluteY());
528 // Continue down the chain
529 for (TWidget widget
: children
) {
530 widget
.drawChildren();
535 * Default constructor for subclasses.
537 protected TWidget() {
538 children
= new LinkedList
<TWidget
>();
542 * Protected constructor.
544 * @param parent parent widget
546 protected TWidget(final TWidget parent
) {
551 * Protected constructor used by subclasses that are disabled by default.
553 * @param parent parent widget
554 * @param enabled if true assume enabled
556 protected TWidget(final TWidget parent
, final boolean enabled
) {
557 this.enabled
= enabled
;
558 this.parent
= parent
;
559 this.window
= parent
.window
;
560 children
= new LinkedList
<TWidget
>();
561 parent
.addChild(this);
565 * Add a child widget to my list of children. We set its tabOrder to 0
566 * and increment the tabOrder of all other children.
568 * @param child TWidget to add
570 private void addChild(final TWidget child
) {
574 && !(child
instanceof THScroller
)
575 && !(child
instanceof TVScroller
)
577 for (TWidget widget
: children
) {
578 widget
.active
= false;
583 for (int i
= 0; i
< children
.size(); i
++) {
584 children
.get(i
).tabOrder
= i
;
589 * Switch the active child.
591 * @param child TWidget to activate
593 public final void activate(final TWidget child
) {
594 assert (child
.enabled
);
595 if ((child
instanceof THScroller
)
596 || (child
instanceof TVScroller
)
601 if (child
!= activeChild
) {
602 if (activeChild
!= null) {
603 activeChild
.active
= false;
611 * Switch the active child.
613 * @param tabOrder tabOrder of the child to activate. If that child
614 * isn't enabled, then the next enabled child will be activated.
616 public final void activate(final int tabOrder
) {
617 if (activeChild
== null) {
620 TWidget child
= null;
621 for (TWidget widget
: children
) {
623 && !(widget
instanceof THScroller
)
624 && !(widget
instanceof TVScroller
)
625 && (widget
.tabOrder
>= tabOrder
)
631 if ((child
!= null) && (child
!= activeChild
)) {
632 activeChild
.active
= false;
633 assert (child
.enabled
);
640 * Switch the active widget with the next in the tab order.
642 * @param forward if true, then switch to the next enabled widget in the
643 * list, otherwise switch to the previous enabled widget in the list
645 public final void switchWidget(final boolean forward
) {
647 // Only switch if there are multiple enabled widgets
648 if ((children
.size() < 2) || (activeChild
== null)) {
652 int tabOrder
= activeChild
.tabOrder
;
661 // If at the end, pass the switch to my parent.
662 if ((!forward
) && (parent
!= this)) {
663 parent
.switchWidget(forward
);
667 tabOrder
= children
.size() - 1;
668 } else if (tabOrder
== children
.size()) {
669 // If at the end, pass the switch to my parent.
670 if ((forward
) && (parent
!= this)) {
671 parent
.switchWidget(forward
);
677 if (activeChild
.tabOrder
== tabOrder
) {
681 } while ((!children
.get(tabOrder
).enabled
)
682 && !(children
.get(tabOrder
) instanceof THScroller
)
683 && !(children
.get(tabOrder
) instanceof TVScroller
));
685 assert (children
.get(tabOrder
).enabled
);
687 activeChild
.active
= false;
688 children
.get(tabOrder
).active
= true;
689 activeChild
= children
.get(tabOrder
);
692 window
.getApplication().setRepaint();
696 * Returns my active widget.
698 * @return widget that is active, or this if no children
700 public TWidget
getActiveChild() {
701 if ((this instanceof THScroller
)
702 || (this instanceof TVScroller
)
707 for (TWidget widget
: children
) {
709 return widget
.getActiveChild();
712 // No active children, return me
717 * Method that subclasses can override to handle keystrokes.
719 * @param keypress keystroke event
721 public void onKeypress(final TKeypressEvent keypress
) {
723 if ((children
.size() == 0)
725 // || (cast(TTreeView)this)
726 // || (cast(TText)this)
730 // tab / shift-tab - switch to next/previous widget
731 // right-arrow or down-arrow: same as tab
732 // left-arrow or up-arrow: same as shift-tab
733 if ((keypress
.equals(kbTab
))
734 || (keypress
.equals(kbRight
))
735 || (keypress
.equals(kbDown
))
737 parent
.switchWidget(true);
739 } else if ((keypress
.equals(kbShiftTab
))
740 || (keypress
.equals(kbBackTab
))
741 || (keypress
.equals(kbLeft
))
742 || (keypress
.equals(kbUp
))
744 parent
.switchWidget(false);
749 // If I have any buttons on me AND this is an Alt-key that matches
750 // its mnemonic, send it an Enter keystroke
751 for (TWidget widget
: children
) {
755 if (TButton button = cast(TButton)w) {
756 if (button.enabled &&
757 !keypress.key.isKey &&
759 !keypress.key.ctrl &&
760 (toLowercase(button.mnemonic.shortcut) == toLowercase(keypress.key.ch))) {
762 w.handleEvent(new TKeypressEvent(kbEnter));
769 // Dispatch the keypress to an active widget
770 for (TWidget widget
: children
) {
772 window
.getApplication().setRepaint();
773 widget
.handleEvent(keypress
);
780 * Method that subclasses can override to handle mouse button presses.
782 * @param mouse mouse button event
784 public void onMouseDown(final TMouseEvent mouse
) {
785 // Default: do nothing, pass to children instead
786 for (TWidget widget
: children
) {
787 if (widget
.mouseWouldHit(mouse
)) {
788 // Dispatch to this child, also activate it
791 // Set x and y relative to the child's coordinates
792 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
793 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
794 widget
.handleEvent(mouse
);
801 * Method that subclasses can override to handle mouse button releases.
803 * @param mouse mouse button event
805 public void onMouseUp(final TMouseEvent mouse
) {
806 // Default: do nothing, pass to children instead
807 for (TWidget widget
: children
) {
808 if (widget
.mouseWouldHit(mouse
)) {
809 // Dispatch to this child, also activate it
812 // Set x and y relative to the child's coordinates
813 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
814 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
815 widget
.handleEvent(mouse
);
822 * Method that subclasses can override to handle mouse movements.
824 * @param mouse mouse motion event
826 public void onMouseMotion(final TMouseEvent mouse
) {
827 // Default: do nothing, pass it on to ALL of my children. This way
828 // the children can see the mouse "leaving" their area.
829 for (TWidget widget
: children
) {
830 // Set x and y relative to the child's coordinates
831 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
832 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
833 widget
.handleEvent(mouse
);
838 * Method that subclasses can override to handle window/screen resize
841 * @param resize resize event
843 public void onResize(final TResizeEvent resize
) {
844 // Default: do nothing, pass to children instead
845 for (TWidget widget
: children
) {
846 widget
.onResize(resize
);
851 * Method that subclasses can override to handle posted command events.
853 * @param command command event
855 public void onCommand(final TCommandEvent command
) {
856 // Default: do nothing, pass to children instead
857 for (TWidget widget
: children
) {
858 widget
.onCommand(command
);
863 * Method that subclasses can override to handle menu or posted menu
866 * @param menu menu event
868 public void onMenu(final TMenuEvent menu
) {
869 // Default: do nothing, pass to children instead
870 for (TWidget widget
: children
) {
876 * Method that subclasses can override to do processing when the UI is
879 public void onIdle() {
880 // Default: do nothing, pass to children instead
881 for (TWidget widget
: children
) {
887 * Consume event. Subclasses that want to intercept all events in one go
888 * can override this method.
890 * @param event keyboard, mouse, resize, command, or menu event
892 public void handleEvent(final TInputEvent event
) {
893 // System.err.printf("TWidget (%s) event: %s\n", this.getClass().getName(),
898 // System.err.println(" -- discard --");
902 if (event
instanceof TKeypressEvent
) {
903 onKeypress((TKeypressEvent
) event
);
904 } else if (event
instanceof TMouseEvent
) {
906 TMouseEvent mouse
= (TMouseEvent
) event
;
908 switch (mouse
.getType()) {
919 onMouseMotion(mouse
);
923 throw new IllegalArgumentException("Invalid mouse event type: "
926 } else if (event
instanceof TResizeEvent
) {
927 onResize((TResizeEvent
) event
);
928 } else if (event
instanceof TCommandEvent
) {
929 onCommand((TCommandEvent
) event
);
930 } else if (event
instanceof TMenuEvent
) {
931 onMenu((TMenuEvent
) event
);
939 * Check if a mouse press/release event coordinate is contained in this
942 * @param mouse a mouse-based event
943 * @return whether or not a mouse click would be sent to this widget
945 public final boolean mouseWouldHit(final TMouseEvent mouse
) {
951 if ((mouse
.getAbsoluteX() >= getAbsoluteX())
952 && (mouse
.getAbsoluteX() < getAbsoluteX() + width
)
953 && (mouse
.getAbsoluteY() >= getAbsoluteY())
954 && (mouse
.getAbsoluteY() < getAbsoluteY() + height
)
962 * Convenience function to add a label to this container/window.
965 * @param x column relative to parent
966 * @param y row relative to parent
967 * @return the new label
969 public final TLabel
addLabel(final String text
, final int x
, final int y
) {
970 return addLabel(text
, x
, y
, "tlabel");
974 * Convenience function to add a label to this container/window.
977 * @param x column relative to parent
978 * @param y row relative to parent
979 * @param colorKey ColorTheme key color to use for foreground text.
980 * Default is "tlabel"
981 * @return the new label
983 public final TLabel
addLabel(final String text
, final int x
, final int y
,
984 final String colorKey
) {
986 return new TLabel(this, text
, x
, y
, colorKey
);
990 * Convenience function to add a button to this container/window.
992 * @param text label on the button
993 * @param x column relative to parent
994 * @param y row relative to parent
995 * @param action to call when button is pressed
996 * @return the new button
998 public final TButton
addButton(final String text
, final int x
, final int y
,
999 final TAction action
) {
1001 return new TButton(this, text
, x
, y
, action
);
1005 * Convenience function to add a checkbox to this container/window.
1007 * @param x column relative to parent
1008 * @param y row relative to parent
1009 * @param label label to display next to (right of) the checkbox
1010 * @param checked initial check state
1011 * @return the new checkbox
1013 public final TCheckbox
addCheckbox(final int x
, final int y
,
1014 final String label
, final boolean checked
) {
1016 return new TCheckbox(this, x
, y
, label
, checked
);
1020 * Convenience function to add a progress bar to this container/window.
1022 * @param x column relative to parent
1023 * @param y row relative to parent
1024 * @param width width of progress bar
1025 * @param value initial value of percent complete
1026 * @return the new progress bar
1028 public final TProgressBar
addProgressBar(final int x
, final int y
,
1029 final int width
, final int value
) {
1031 return new TProgressBar(this, x
, y
, width
, value
);
1035 * Convenience function to add a radio button group to this
1038 * @param x column relative to parent
1039 * @param y row relative to parent
1040 * @param label label to display on the group box
1041 * @return the new radio button group
1043 public final TRadioGroup
addRadioGroup(final int x
, final int y
,
1044 final String label
) {
1046 return new TRadioGroup(this, x
, y
, label
);
1050 * Convenience function to add a text field to this container/window.
1052 * @param x column relative to parent
1053 * @param y row relative to parent
1054 * @param width visible text width
1055 * @param fixed if true, the text cannot exceed the display width
1056 * @return the new text field
1058 public final TField
addField(final int x
, final int y
,
1059 final int width
, final boolean fixed
) {
1061 return new TField(this, x
, y
, width
, fixed
);
1065 * Convenience function to add a text field to this container/window.
1067 * @param x column relative to parent
1068 * @param y row relative to parent
1069 * @param width visible text width
1070 * @param fixed if true, the text cannot exceed the display width
1071 * @param text initial text, default is empty string
1072 * @return the new text field
1074 public final TField
addField(final int x
, final int y
,
1075 final int width
, final boolean fixed
, final String text
) {
1077 return new TField(this, x
, y
, width
, fixed
, text
);
1081 * Convenience function to add a text field to this container/window.
1083 * @param x column relative to parent
1084 * @param y row relative to parent
1085 * @param width visible text width
1086 * @param fixed if true, the text cannot exceed the display width
1087 * @param text initial text, default is empty string
1088 * @param enterAction function to call when enter key is pressed
1089 * @param updateAction function to call when the text is updated
1090 * @return the new text field
1092 public final TField
addField(final int x
, final int y
,
1093 final int width
, final boolean fixed
, final String text
,
1094 final TAction enterAction
, final TAction updateAction
) {
1096 return new TField(this, x
, y
, width
, fixed
, text
, enterAction
,
1101 * Convenience function to add a scrollable text box to this
1104 * @param text text on the screen
1105 * @param x column relative to parent
1106 * @param y row relative to parent
1107 * @param width width of text area
1108 * @param height height of text area
1109 * @param colorKey ColorTheme key color to use for foreground text
1110 * @return the new text box
1112 public TText
addText(final String text
, final int x
,
1113 final int y
, final int width
, final int height
, final String colorKey
) {
1115 return new TText(this, text
, x
, y
, width
, height
, colorKey
);
1119 * Convenience function to add a scrollable text box to this
1122 * @param text text on the screen
1123 * @param x column relative to parent
1124 * @param y row relative to parent
1125 * @param width width of text area
1126 * @param height height of text area
1127 * @return the new text box
1129 public TText
addText(final String text
, final int x
, final int y
,
1130 final int width
, final int height
) {
1132 return new TText(this, text
, x
, y
, width
, height
, "ttext");