6312d087c84e12258a0d89db4e6624af9576f797
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
.event
.TCommandEvent
;
37 import jexer
.event
.TInputEvent
;
38 import jexer
.event
.TKeypressEvent
;
39 import jexer
.event
.TMenuEvent
;
40 import jexer
.event
.TMouseEvent
;
41 import jexer
.event
.TResizeEvent
;
42 import jexer
.io
.Screen
;
43 import static jexer
.TKeypress
.*;
46 * TWidget is the base class of all objects that can be drawn on screen or
47 * handle user input events.
49 public abstract class TWidget
{
52 * Every widget has a parent widget that it may be "contained" in. For
53 * example, a TWindow might contain several TTextFields, or a TComboBox
54 * may contain a TScrollBar.
56 protected TWidget parent
= null;
59 * Child widgets that this widget contains.
61 private List
<TWidget
> children
;
64 * The currently active child widget that will receive keypress events.
66 private TWidget activeChild
= null;
69 * If true, this widget will receive events.
71 protected boolean active
= false;
74 * The window that this widget draws to.
76 protected TWindow window
= null;
79 * Absolute X position of the top-left corner.
84 * Absolute Y position of the top-left corner.
91 protected int width
= 0;
96 protected int height
= 0;
99 * My tab order inside a window or containing widget.
101 private int tabOrder
= 0;
104 * If true, this widget can be tabbed to or receive events.
106 private boolean enabled
= true;
111 * @return if true, this widget can be tabbed to or receive events
113 public final boolean getEnabled() {
120 * @param enabled if true, this widget can be tabbed to or receive events
122 public final void setEnabled(final boolean enabled
) {
123 this.enabled
= enabled
;
126 // TODO: get this working after scrollers are going again
128 if (enabled == false) {
130 // See if there are any active siblings to switch to
131 boolean foundSibling = false;
132 if (parent !is null) {
133 foreach (w; parent.children) {
135 (!cast(THScroller)this) &&
136 (!cast(TVScroller)this)
144 parent.activeChild = null;
152 * If true, this widget has a cursor.
154 private boolean hasCursor
= false;
157 * Cursor column position in relative coordinates.
159 private int cursorX
= 0;
162 * Cursor row position in relative coordinates.
164 private int cursorY
= 0;
167 * Comparison operator sorts on tabOrder.
169 * @param that another TWidget instance
170 * @return difference between this.tabOrder and that.tabOrder
172 public final int compare(final TWidget that
) {
173 return (this.tabOrder
- that
.tabOrder
);
177 * See if this widget should render with the active color.
179 * @return true if this widget is active and all of its parents are
182 public final boolean getAbsoluteActive() {
183 if (parent
== this) {
186 return (active
&& parent
.getAbsoluteActive());
190 * Returns the cursor X position.
192 * @return absolute screen column number for the cursor's X position
194 public final int getCursorAbsoluteX() {
196 return getAbsoluteX() + cursorX
;
200 * Returns the cursor Y position.
202 * @return absolute screen row number for the cursor's Y position
204 public final int getCursorAbsoluteY() {
206 return getAbsoluteY() + cursorY
;
210 * Compute my absolute X position as the sum of my X plus all my parent's
213 * @return absolute screen column number for my X position
215 public final int getAbsoluteX() {
216 assert (parent
!= null);
217 if (parent
== this) {
220 if ((parent
instanceof TWindow
) && !(parent
instanceof TMenu
)) {
221 // Widgets on a TWindow have (0,0) as their top-left, but this is
222 // actually the TWindow's (1,1).
223 return parent
.getAbsoluteX() + x
+ 1;
225 return parent
.getAbsoluteX() + x
;
229 * Compute my absolute Y position as the sum of my Y plus all my parent's
232 * @return absolute screen row number for my Y position
234 public final int getAbsoluteY() {
235 assert (parent
!= null);
236 if (parent
== this) {
239 if ((parent
instanceof TWindow
) && !(parent
instanceof TMenu
)) {
240 // Widgets on a TWindow have (0,0) as their top-left, but this is
241 // actually the TWindow's (1,1).
242 return parent
.getAbsoluteY() + y
+ 1;
244 return parent
.getAbsoluteY() + y
;
248 * Draw my specific widget. When called, the screen rectangle I draw
249 * into is already setup (offset and clipping).
252 // Default widget draws nothing.
256 * Called by parent to render to TWindow.
258 public final void drawChildren() {
259 // Set my clipping rectangle
260 assert (window
!= null);
261 assert (window
.getScreen() != null);
262 Screen screen
= window
.getScreen();
264 screen
.setClipRight(width
);
265 screen
.setClipBottom(height
);
267 int absoluteRightEdge
= window
.getAbsoluteX() + screen
.getWidth();
268 int absoluteBottomEdge
= window
.getAbsoluteY() + screen
.getHeight();
269 if (!(this instanceof TWindow
) && !(this instanceof TVScroller
)) {
270 absoluteRightEdge
-= 1;
272 if (!(this instanceof TWindow
) && !(this instanceof THScroller
)) {
273 absoluteBottomEdge
-= 1;
275 int myRightEdge
= getAbsoluteX() + width
;
276 int myBottomEdge
= getAbsoluteY() + height
;
277 if (getAbsoluteX() > absoluteRightEdge
) {
279 screen
.setClipRight(0);
280 } else if (myRightEdge
> absoluteRightEdge
) {
281 screen
.setClipRight(screen
.getClipRight()
282 - myRightEdge
- absoluteRightEdge
);
284 if (getAbsoluteY() > absoluteBottomEdge
) {
286 screen
.setClipBottom(0);
287 } else if (myBottomEdge
> absoluteBottomEdge
) {
288 screen
.setClipBottom(screen
.getClipBottom()
289 - myBottomEdge
- absoluteBottomEdge
);
293 screen
.setOffsetX(getAbsoluteX());
294 screen
.setOffsetY(getAbsoluteY());
299 // Continue down the chain
300 for (TWidget widget
: children
) {
301 widget
.drawChildren();
306 * Subclasses need this constructor to setup children.
308 protected TWidget() {
309 children
= new LinkedList
<TWidget
>();
313 * Protected constructor.
315 * @param parent parent widget
317 protected TWidget(final TWidget parent
) {
318 this.parent
= parent
;
319 this.window
= parent
.window
;
321 parent
.addChild(this);
325 * Add a child widget to my list of children. We set its tabOrder to 0
326 * and increment the tabOrder of all other children.
328 * @param child TWidget to add
330 private void addChild(final TWidget child
) {
334 && !(child
instanceof THScroller
)
335 && !(child
instanceof TVScroller
)
337 for (TWidget widget
: children
) {
338 widget
.active
= false;
343 for (int i
= 0; i
< children
.size(); i
++) {
344 children
.get(i
).tabOrder
= i
;
349 * Switch the active child.
351 * @param child TWidget to activate
353 public final void activate(final TWidget child
) {
354 assert (child
.enabled
);
355 if ((child
instanceof THScroller
)
356 || (child
instanceof TVScroller
)
361 if (child
!= activeChild
) {
362 if (activeChild
!= null) {
363 activeChild
.active
= false;
371 * Switch the active child.
373 * @param tabOrder tabOrder of the child to activate. If that child
374 * isn't enabled, then the next enabled child will be activated.
376 public final void activate(final int tabOrder
) {
377 if (activeChild
== null) {
380 TWidget child
= null;
381 for (TWidget widget
: children
) {
383 && !(widget
instanceof THScroller
)
384 && !(widget
instanceof TVScroller
)
385 && (widget
.tabOrder
>= tabOrder
)
391 if ((child
!= null) && (child
!= activeChild
)) {
392 activeChild
.active
= false;
393 assert (child
.enabled
);
400 * Switch the active widget with the next in the tab order.
402 * @param forward if true, then switch to the next enabled widget in the
403 * list, otherwise switch to the previous enabled widget in the list
405 public final void switchWidget(final boolean forward
) {
407 // Only switch if there are multiple enabled widgets
408 if ((children
.size() < 2) || (activeChild
== null)) {
412 int tabOrder
= activeChild
.tabOrder
;
421 // If at the end, pass the switch to my parent.
422 if ((!forward
) && (parent
!= this)) {
423 parent
.switchWidget(forward
);
427 tabOrder
= children
.size() - 1;
428 } else if (tabOrder
== children
.size()) {
429 // If at the end, pass the switch to my parent.
430 if ((forward
) && (parent
!= this)) {
431 parent
.switchWidget(forward
);
437 if (activeChild
.tabOrder
== tabOrder
) {
441 } while ((!children
.get(tabOrder
).enabled
)
442 && !(children
.get(tabOrder
) instanceof THScroller
)
443 && !(children
.get(tabOrder
) instanceof TVScroller
));
445 assert (children
.get(tabOrder
).enabled
);
447 activeChild
.active
= false;
448 children
.get(tabOrder
).active
= true;
449 activeChild
= children
.get(tabOrder
);
452 window
.getApplication().setRepaint();
456 * Returns my active widget.
458 * @return widget that is active, or this if no children
460 public final TWidget
getActiveChild() {
461 if ((this instanceof THScroller
)
462 || (this instanceof TVScroller
)
467 for (TWidget widget
: children
) {
469 return widget
.getActiveChild();
472 // No active children, return me
477 * Method that subclasses can override to handle keystrokes.
479 * @param keypress keystroke event
481 public void onKeypress(final TKeypressEvent keypress
) {
483 if ((children
.size() == 0)
484 // || (cast(TTreeView)this)
485 // || (cast(TText)this)
489 // tab / shift-tab - switch to next/previous widget
490 // right-arrow or down-arrow: same as tab
491 // left-arrow or up-arrow: same as shift-tab
492 if ((keypress
.equals(kbTab
))
493 || (keypress
.equals(kbRight
))
494 || (keypress
.equals(kbDown
))
496 parent
.switchWidget(true);
498 } else if ((keypress
.equals(kbShiftTab
))
499 || (keypress
.equals(kbBackTab
))
500 || (keypress
.equals(kbLeft
))
501 || (keypress
.equals(kbUp
))
503 parent
.switchWidget(false);
508 // If I have any buttons on me AND this is an Alt-key that matches
509 // its mnemonic, send it an Enter keystroke
510 for (TWidget widget
: children
) {
514 if (TButton button = cast(TButton)w) {
515 if (button.enabled &&
516 !keypress.key.isKey &&
518 !keypress.key.ctrl &&
519 (toLowercase(button.mnemonic.shortcut) == toLowercase(keypress.key.ch))) {
521 w.handleEvent(new TKeypressEvent(kbEnter));
528 // Dispatch the keypress to an active widget
529 for (TWidget widget
: children
) {
531 window
.getApplication().setRepaint();
532 widget
.handleEvent(keypress
);
539 * Method that subclasses can override to handle mouse button presses.
541 * @param mouse mouse button event
543 public void onMouseDown(final TMouseEvent mouse
) {
544 // Default: do nothing, pass to children instead
545 for (TWidget widget
: children
) {
546 if (widget
.mouseWouldHit(mouse
)) {
547 // Dispatch to this child, also activate it
550 // Set x and y relative to the child's coordinates
551 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
552 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
553 widget
.handleEvent(mouse
);
560 * Method that subclasses can override to handle mouse button releases.
562 * @param mouse mouse button event
564 public void onMouseUp(final TMouseEvent mouse
) {
565 // Default: do nothing, pass to children instead
566 for (TWidget widget
: children
) {
567 if (widget
.mouseWouldHit(mouse
)) {
568 // Dispatch to this child, also activate it
571 // Set x and y relative to the child's coordinates
572 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
573 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
574 widget
.handleEvent(mouse
);
581 * Method that subclasses can override to handle mouse movements.
583 * @param mouse mouse motion event
585 public void onMouseMotion(final TMouseEvent mouse
) {
586 // Default: do nothing, pass it on to ALL of my children. This way
587 // the children can see the mouse "leaving" their area.
588 for (TWidget widget
: children
) {
589 // Set x and y relative to the child's coordinates
590 mouse
.setX(mouse
.getAbsoluteX() - widget
.getAbsoluteX());
591 mouse
.setY(mouse
.getAbsoluteY() - widget
.getAbsoluteY());
592 widget
.handleEvent(mouse
);
597 * Method that subclasses can override to handle window/screen resize
600 * @param resize resize event
602 public void onResize(final TResizeEvent resize
) {
603 // Default: do nothing, pass to children instead
604 for (TWidget widget
: children
) {
605 widget
.onResize(resize
);
610 * Method that subclasses can override to handle posted command events.
612 * @param command command event
614 public void onCommand(final TCommandEvent command
) {
615 // Default: do nothing, pass to children instead
616 for (TWidget widget
: children
) {
617 widget
.onCommand(command
);
622 * Method that subclasses can override to handle menu or posted menu
625 * @param menu menu event
627 public void onMenu(final TMenuEvent menu
) {
628 // Default: do nothing, pass to children instead
629 for (TWidget widget
: children
) {
635 * Method that subclasses can override to do processing when the UI is
638 public void onIdle() {
639 // Default: do nothing, pass to children instead
640 for (TWidget widget
: children
) {
646 * Consume event. Subclasses that want to intercept all events in one go
647 * can override this method.
649 * @param event keyboard, mouse, resize, command, or menu event
651 public void handleEvent(final TInputEvent event
) {
652 // System.err.printf("TWidget (%s) event: %s\n", this.getClass().getName(),
657 // System.err.println(" -- discard --");
661 if (event
instanceof TKeypressEvent
) {
662 onKeypress((TKeypressEvent
) event
);
663 } else if (event
instanceof TMouseEvent
) {
665 TMouseEvent mouse
= (TMouseEvent
) event
;
667 switch (mouse
.getType()) {
678 onMouseMotion(mouse
);
682 throw new IllegalArgumentException("Invalid mouse event type: "
685 } else if (event
instanceof TResizeEvent
) {
686 onResize((TResizeEvent
) event
);
687 } else if (event
instanceof TCommandEvent
) {
688 onCommand((TCommandEvent
) event
);
689 } else if (event
instanceof TMenuEvent
) {
690 onMenu((TMenuEvent
) event
);
698 * Check if a mouse press/release event coordinate is contained in this
701 * @param mouse a mouse-based event
702 * @return whether or not a mouse click would be sent to this widget
704 public final boolean mouseWouldHit(final TMouseEvent mouse
) {
710 if ((mouse
.getAbsoluteX() >= getAbsoluteX())
711 && (mouse
.getAbsoluteX() < getAbsoluteX() + width
)
712 && (mouse
.getAbsoluteY() >= getAbsoluteY())
713 && (mouse
.getAbsoluteY() < getAbsoluteY() + height
)