TTreeView compiles
[fanfix.git] / src / jexer / TWidget.java
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 java.util.List;
34 import java.util.LinkedList;
35
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.*;
46
47 /**
48 * TWidget is the base class of all objects that can be drawn on screen or
49 * handle user input events.
50 */
51 public abstract class TWidget implements Comparable<TWidget> {
52
53 /**
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.
57 */
58 private TWidget parent = null;
59
60 /**
61 * Get parent widget.
62 *
63 * @return parent widget
64 */
65 public final TWidget getParent() {
66 return parent;
67 }
68
69 /**
70 * Backdoor access for TWindow's constructor. ONLY TWindow USES THIS.
71 *
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
77 */
78 protected final void setupForTWindow(final TWindow window,
79 final int x, final int y, final int width, final int height) {
80
81 this.parent = window;
82 this.window = window;
83 this.x = x;
84 this.y = y;
85 this.width = width;
86 this.height = height;
87 }
88
89 /**
90 * Get this TWidget's parent TApplication.
91 *
92 * @return the parent TApplication
93 */
94 public TApplication getApplication() {
95 return window.getApplication();
96 }
97
98 /**
99 * Get the Screen.
100 *
101 * @return the Screen
102 */
103 public Screen getScreen() {
104 return window.getScreen();
105 }
106
107 /**
108 * Child widgets that this widget contains.
109 */
110 private List<TWidget> children;
111
112 /**
113 * Get the list of child widgets that this widget contains.
114 *
115 * @return the list of child widgets
116 */
117 public List<TWidget> getChildren() {
118 return children;
119 }
120
121 /**
122 * The currently active child widget that will receive keypress events.
123 */
124 private TWidget activeChild = null;
125
126 /**
127 * If true, this widget will receive events.
128 */
129 private boolean active = false;
130
131 /**
132 * Get active flag.
133 *
134 * @return if true, this widget will receive events
135 */
136 public final boolean isActive() {
137 return active;
138 }
139
140 /**
141 * Set active flag.
142 *
143 * @param active if true, this widget will receive events
144 */
145 public final void setActive(final boolean active) {
146 this.active = active;
147 }
148
149 /**
150 * The window that this widget draws to.
151 */
152 private TWindow window = null;
153
154 /**
155 * Get the window this widget is on.
156 *
157 * @return the window
158 */
159 public final TWindow getWindow() {
160 return window;
161 }
162
163 /**
164 * Absolute X position of the top-left corner.
165 */
166 private int x = 0;
167
168 /**
169 * Get X position.
170 *
171 * @return absolute X position of the top-left corner
172 */
173 public final int getX() {
174 return x;
175 }
176
177 /**
178 * Set X position.
179 *
180 * @param x absolute X position of the top-left corner
181 */
182 public final void setX(final int x) {
183 this.x = x;
184 }
185
186 /**
187 * Absolute Y position of the top-left corner.
188 */
189 private int y = 0;
190
191 /**
192 * Get Y position.
193 *
194 * @return absolute Y position of the top-left corner
195 */
196 public final int getY() {
197 return y;
198 }
199
200 /**
201 * Set Y position.
202 *
203 * @param y absolute Y position of the top-left corner
204 */
205 public final void setY(final int y) {
206 this.y = y;
207 }
208
209 /**
210 * Width.
211 */
212 private int width = 0;
213
214 /**
215 * Get the width.
216 *
217 * @return widget width
218 */
219 public final int getWidth() {
220 return this.width;
221 }
222
223 /**
224 * Change the width.
225 *
226 * @param width new widget width
227 */
228 public final void setWidth(final int width) {
229 this.width = width;
230 }
231
232 /**
233 * Height.
234 */
235 private int height = 0;
236
237 /**
238 * Get the height.
239 *
240 * @return widget height
241 */
242 public final int getHeight() {
243 return this.height;
244 }
245
246 /**
247 * Change the height.
248 *
249 * @param height new widget height
250 */
251 public final void setHeight(final int height) {
252 this.height = height;
253 }
254
255 /**
256 * My tab order inside a window or containing widget.
257 */
258 private int tabOrder = 0;
259
260 /**
261 * If true, this widget can be tabbed to or receive events.
262 */
263 private boolean enabled = true;
264
265 /**
266 * Get enabled flag.
267 *
268 * @return if true, this widget can be tabbed to or receive events
269 */
270 public final boolean isEnabled() {
271 return enabled;
272 }
273
274 /**
275 * Set enabled flag.
276 *
277 * @param enabled if true, this widget can be tabbed to or receive events
278 */
279 public final void setEnabled(final boolean enabled) {
280 this.enabled = enabled;
281 if (!enabled) {
282 active = false;
283 // See if there are any active siblings to switch to
284 boolean foundSibling = false;
285 if (parent != null) {
286 for (TWidget w: parent.children) {
287 if ((w.enabled)
288 && !(this instanceof THScroller)
289 && !(this instanceof TVScroller)
290 ) {
291 parent.activate(w);
292 foundSibling = true;
293 break;
294 }
295 }
296 if (!foundSibling) {
297 parent.activeChild = null;
298 }
299 }
300 }
301 }
302
303 /**
304 * If true, this widget has a cursor.
305 */
306 private boolean cursorVisible = false;
307
308 /**
309 * Set visible cursor flag.
310 *
311 * @param cursorVisible if true, this widget has a cursor
312 */
313 public final void setCursorVisible(final boolean cursorVisible) {
314 this.cursorVisible = cursorVisible;
315 }
316
317 /**
318 * See if this widget has a visible cursor.
319 *
320 * @return if true, this widget has a visible cursor
321 */
322 public final boolean isCursorVisible() {
323 return cursorVisible;
324 }
325
326 /**
327 * Cursor column position in relative coordinates.
328 */
329 private int cursorX = 0;
330
331 /**
332 * Get cursor X value.
333 *
334 * @return cursor column position in relative coordinates
335 */
336 public final int getCursorX() {
337 return cursorX;
338 }
339
340 /**
341 * Set cursor X value.
342 *
343 * @param cursorX column position in relative coordinates
344 */
345 public final void setCursorX(final int cursorX) {
346 this.cursorX = cursorX;
347 }
348
349 /**
350 * Cursor row position in relative coordinates.
351 */
352 private int cursorY = 0;
353
354 /**
355 * Get cursor Y value.
356 *
357 * @return cursor row position in relative coordinates
358 */
359 public final int getCursorY() {
360 return cursorY;
361 }
362
363 /**
364 * Set cursor Y value.
365 *
366 * @param cursorY row position in relative coordinates
367 */
368 public final void setCursorY(final int cursorY) {
369 this.cursorY = cursorY;
370 }
371
372 /**
373 * Comparison operator sorts on:
374 * <ul>
375 * <li>tabOrder for TWidgets</li>
376 * <li>z for TWindows</li>
377 * <li>text for TTreeItems</li>
378 * </ul>
379 *
380 * @param that another TWidget, TWindow, or TTreeItem instance
381 * @return difference between this.tabOrder and that.tabOrder, or
382 * difference between this.z and that.z, or String.compareTo(text)
383 */
384 @Override
385 public final int compareTo(final TWidget that) {
386 if ((this instanceof TWindow)
387 && (that instanceof TWindow)
388 ) {
389 return (((TWindow) this).getZ() - ((TWindow) that).getZ());
390 }
391 if ((this instanceof TTreeItem)
392 && (that instanceof TTreeItem)
393 ) {
394 return (((TTreeItem) this).getText().compareTo(
395 ((TTreeItem) that).getText()));
396 }
397 return (this.tabOrder - that.tabOrder);
398 }
399
400 /**
401 * See if this widget should render with the active color.
402 *
403 * @return true if this widget is active and all of its parents are
404 * active.
405 */
406 public final boolean isAbsoluteActive() {
407 if (parent == this) {
408 return active;
409 }
410 return (active && parent.isAbsoluteActive());
411 }
412
413 /**
414 * Returns the cursor X position.
415 *
416 * @return absolute screen column number for the cursor's X position
417 */
418 public final int getCursorAbsoluteX() {
419 assert (cursorVisible);
420 return getAbsoluteX() + cursorX;
421 }
422
423 /**
424 * Returns the cursor Y position.
425 *
426 * @return absolute screen row number for the cursor's Y position
427 */
428 public final int getCursorAbsoluteY() {
429 assert (cursorVisible);
430 return getAbsoluteY() + cursorY;
431 }
432
433 /**
434 * Compute my absolute X position as the sum of my X plus all my parent's
435 * X's.
436 *
437 * @return absolute screen column number for my X position
438 */
439 public final int getAbsoluteX() {
440 assert (parent != null);
441 if (parent == this) {
442 return x;
443 }
444 if ((parent instanceof TWindow) && !(parent instanceof TMenu)) {
445 // Widgets on a TWindow have (0,0) as their top-left, but this is
446 // actually the TWindow's (1,1).
447 return parent.getAbsoluteX() + x + 1;
448 }
449 return parent.getAbsoluteX() + x;
450 }
451
452 /**
453 * Compute my absolute Y position as the sum of my Y plus all my parent's
454 * Y's.
455 *
456 * @return absolute screen row number for my Y position
457 */
458 public final int getAbsoluteY() {
459 assert (parent != null);
460 if (parent == this) {
461 return y;
462 }
463 if ((parent instanceof TWindow) && !(parent instanceof TMenu)) {
464 // Widgets on a TWindow have (0,0) as their top-left, but this is
465 // actually the TWindow's (1,1).
466 return parent.getAbsoluteY() + y + 1;
467 }
468 return parent.getAbsoluteY() + y;
469 }
470
471 /**
472 * Get the global color theme.
473 *
474 * @return the ColorTheme
475 */
476 public final ColorTheme getTheme() {
477 return window.getApplication().getTheme();
478 }
479
480 /**
481 * Draw my specific widget. When called, the screen rectangle I draw
482 * into is already setup (offset and clipping).
483 */
484 public void draw() {
485 // Default widget draws nothing.
486 }
487
488 /**
489 * Called by parent to render to TWindow.
490 */
491 public final void drawChildren() {
492 // Set my clipping rectangle
493 assert (window != null);
494 assert (getScreen() != null);
495 Screen screen = getScreen();
496
497 screen.setClipRight(width);
498 screen.setClipBottom(height);
499
500 int absoluteRightEdge = window.getAbsoluteX() + window.getWidth();
501 int absoluteBottomEdge = window.getAbsoluteY() + window.getHeight();
502 if (!(this instanceof TWindow) && !(this instanceof TVScroller)) {
503 absoluteRightEdge -= 1;
504 }
505 if (!(this instanceof TWindow) && !(this instanceof THScroller)) {
506 absoluteBottomEdge -= 1;
507 }
508 int myRightEdge = getAbsoluteX() + width;
509 int myBottomEdge = getAbsoluteY() + height;
510 if (getAbsoluteX() > absoluteRightEdge) {
511 // I am offscreen
512 screen.setClipRight(0);
513 } else if (myRightEdge > absoluteRightEdge) {
514 screen.setClipRight(screen.getClipRight()
515 - (myRightEdge - absoluteRightEdge));
516 }
517 if (getAbsoluteY() > absoluteBottomEdge) {
518 // I am offscreen
519 screen.setClipBottom(0);
520 } else if (myBottomEdge > absoluteBottomEdge) {
521 screen.setClipBottom(screen.getClipBottom()
522 - (myBottomEdge - absoluteBottomEdge));
523 }
524
525 // Set my offset
526 screen.setOffsetX(getAbsoluteX());
527 screen.setOffsetY(getAbsoluteY());
528
529 // Draw me
530 draw();
531
532 // Continue down the chain
533 for (TWidget widget: children) {
534 widget.drawChildren();
535 }
536 }
537
538 /**
539 * Default constructor for subclasses.
540 */
541 protected TWidget() {
542 children = new LinkedList<TWidget>();
543 }
544
545 /**
546 * Protected constructor.
547 *
548 * @param parent parent widget
549 */
550 protected TWidget(final TWidget parent) {
551 this(parent, true);
552 }
553
554 /**
555 * Protected constructor.
556 *
557 * @param parent parent widget
558 * @param x column relative to parent
559 * @param y row relative to parent
560 * @param width width of widget
561 * @param height height of widget
562 */
563 protected TWidget(final TWidget parent, final int x, final int y,
564 final int width, final int height) {
565
566 this(parent, true, x, y, width, height);
567 }
568
569 /**
570 * Protected constructor used by subclasses that are disabled by default.
571 *
572 * @param parent parent widget
573 * @param enabled if true assume enabled
574 */
575 protected TWidget(final TWidget parent, final boolean enabled) {
576 this.enabled = enabled;
577 this.parent = parent;
578 this.window = parent.window;
579 children = new LinkedList<TWidget>();
580 parent.addChild(this);
581 }
582
583 /**
584 * Protected constructor used by subclasses that are disabled by default.
585 *
586 * @param parent parent widget
587 * @param enabled if true assume enabled
588 * @param x column relative to parent
589 * @param y row relative to parent
590 * @param width width of widget
591 * @param height height of widget
592 */
593 protected TWidget(final TWidget parent, final boolean enabled,
594 final int x, final int y, final int width, final int height) {
595
596 this.enabled = enabled;
597 this.parent = parent;
598 this.window = parent.window;
599 children = new LinkedList<TWidget>();
600 parent.addChild(this);
601
602 this.x = x;
603 this.y = y;
604 this.width = width;
605 this.height = height;
606 }
607
608 /**
609 * Add a child widget to my list of children. We set its tabOrder to 0
610 * and increment the tabOrder of all other children.
611 *
612 * @param child TWidget to add
613 */
614 private void addChild(final TWidget child) {
615 children.add(child);
616
617 if ((child.enabled)
618 && !(child instanceof THScroller)
619 && !(child instanceof TVScroller)
620 ) {
621 for (TWidget widget: children) {
622 widget.active = false;
623 }
624 child.active = true;
625 activeChild = child;
626 }
627 for (int i = 0; i < children.size(); i++) {
628 children.get(i).tabOrder = i;
629 }
630 }
631
632 /**
633 * Switch the active child.
634 *
635 * @param child TWidget to activate
636 */
637 public final void activate(final TWidget child) {
638 assert (child.enabled);
639 if ((child instanceof THScroller)
640 || (child instanceof TVScroller)
641 ) {
642 return;
643 }
644
645 if (child != activeChild) {
646 if (activeChild != null) {
647 activeChild.active = false;
648 }
649 child.active = true;
650 activeChild = child;
651 }
652 }
653
654 /**
655 * Switch the active child.
656 *
657 * @param tabOrder tabOrder of the child to activate. If that child
658 * isn't enabled, then the next enabled child will be activated.
659 */
660 public final void activate(final int tabOrder) {
661 if (activeChild == null) {
662 return;
663 }
664 TWidget child = null;
665 for (TWidget widget: children) {
666 if ((widget.enabled)
667 && !(widget instanceof THScroller)
668 && !(widget instanceof TVScroller)
669 && (widget.tabOrder >= tabOrder)
670 ) {
671 child = widget;
672 break;
673 }
674 }
675 if ((child != null) && (child != activeChild)) {
676 activeChild.active = false;
677 assert (child.enabled);
678 child.active = true;
679 activeChild = child;
680 }
681 }
682
683 /**
684 * Switch the active widget with the next in the tab order.
685 *
686 * @param forward if true, then switch to the next enabled widget in the
687 * list, otherwise switch to the previous enabled widget in the list
688 */
689 public final void switchWidget(final boolean forward) {
690
691 // Only switch if there are multiple enabled widgets
692 if ((children.size() < 2) || (activeChild == null)) {
693 return;
694 }
695
696 int tabOrder = activeChild.tabOrder;
697 do {
698 if (forward) {
699 tabOrder++;
700 } else {
701 tabOrder--;
702 }
703 if (tabOrder < 0) {
704
705 // If at the end, pass the switch to my parent.
706 if ((!forward) && (parent != this)) {
707 parent.switchWidget(forward);
708 return;
709 }
710
711 tabOrder = children.size() - 1;
712 } else if (tabOrder == children.size()) {
713 // If at the end, pass the switch to my parent.
714 if ((forward) && (parent != this)) {
715 parent.switchWidget(forward);
716 return;
717 }
718
719 tabOrder = 0;
720 }
721 if (activeChild.tabOrder == tabOrder) {
722 // We wrapped around
723 break;
724 }
725 } while ((!children.get(tabOrder).enabled)
726 && !(children.get(tabOrder) instanceof THScroller)
727 && !(children.get(tabOrder) instanceof TVScroller));
728
729 assert (children.get(tabOrder).enabled);
730
731 activeChild.active = false;
732 children.get(tabOrder).active = true;
733 activeChild = children.get(tabOrder);
734 }
735
736 /**
737 * Returns my active widget.
738 *
739 * @return widget that is active, or this if no children
740 */
741 public TWidget getActiveChild() {
742 if ((this instanceof THScroller)
743 || (this instanceof TVScroller)
744 ) {
745 return parent;
746 }
747
748 for (TWidget widget: children) {
749 if (widget.active) {
750 return widget.getActiveChild();
751 }
752 }
753 // No active children, return me
754 return this;
755 }
756
757 /**
758 * Method that subclasses can override to handle keystrokes.
759 *
760 * @param keypress keystroke event
761 */
762 public void onKeypress(final TKeypressEvent keypress) {
763
764 if ((children.size() == 0)
765 // TODO
766 // || (cast(TTreeView)this)
767 || (this instanceof TText)
768 ) {
769
770 // Defaults:
771 // tab / shift-tab - switch to next/previous widget
772 // right-arrow or down-arrow: same as tab
773 // left-arrow or up-arrow: same as shift-tab
774 if ((keypress.equals(kbTab))
775 || (keypress.equals(kbRight))
776 || (keypress.equals(kbDown))
777 ) {
778 parent.switchWidget(true);
779 return;
780 } else if ((keypress.equals(kbShiftTab))
781 || (keypress.equals(kbBackTab))
782 || (keypress.equals(kbLeft))
783 || (keypress.equals(kbUp))
784 ) {
785 parent.switchWidget(false);
786 return;
787 }
788 }
789
790 // If I have any buttons on me AND this is an Alt-key that matches
791 // its mnemonic, send it an Enter keystroke
792 for (TWidget widget: children) {
793 if (widget instanceof TButton) {
794 TButton button = (TButton) widget;
795 if (button.isEnabled()
796 && !keypress.getKey().isFnKey()
797 && keypress.getKey().isAlt()
798 && !keypress.getKey().isCtrl()
799 && (Character.toLowerCase(button.getMnemonic().getShortcut())
800 == Character.toLowerCase(keypress.getKey().getChar()))
801 ) {
802
803 widget.handleEvent(new TKeypressEvent(kbEnter));
804 return;
805 }
806 }
807 }
808
809 // Dispatch the keypress to an active widget
810 for (TWidget widget: children) {
811 if (widget.active) {
812 widget.handleEvent(keypress);
813 return;
814 }
815 }
816 }
817
818 /**
819 * Method that subclasses can override to handle mouse button presses.
820 *
821 * @param mouse mouse button event
822 */
823 public void onMouseDown(final TMouseEvent mouse) {
824 // Default: do nothing, pass to children instead
825 for (TWidget widget: children) {
826 if (widget.mouseWouldHit(mouse)) {
827 // Dispatch to this child, also activate it
828 activate(widget);
829
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);
834 return;
835 }
836 }
837 }
838
839 /**
840 * Method that subclasses can override to handle mouse button releases.
841 *
842 * @param mouse mouse button event
843 */
844 public void onMouseUp(final TMouseEvent mouse) {
845 // Default: do nothing, pass to children instead
846 for (TWidget widget: children) {
847 if (widget.mouseWouldHit(mouse)) {
848 // Dispatch to this child, also activate it
849 activate(widget);
850
851 // Set x and y relative to the child's coordinates
852 mouse.setX(mouse.getAbsoluteX() - widget.getAbsoluteX());
853 mouse.setY(mouse.getAbsoluteY() - widget.getAbsoluteY());
854 widget.handleEvent(mouse);
855 return;
856 }
857 }
858 }
859
860 /**
861 * Method that subclasses can override to handle mouse movements.
862 *
863 * @param mouse mouse motion event
864 */
865 public void onMouseMotion(final TMouseEvent mouse) {
866 // Default: do nothing, pass it on to ALL of my children. This way
867 // the children can see the mouse "leaving" their area.
868 for (TWidget widget: children) {
869 // Set x and y relative to the child's coordinates
870 mouse.setX(mouse.getAbsoluteX() - widget.getAbsoluteX());
871 mouse.setY(mouse.getAbsoluteY() - widget.getAbsoluteY());
872 widget.handleEvent(mouse);
873 }
874 }
875
876 /**
877 * Method that subclasses can override to handle window/screen resize
878 * events.
879 *
880 * @param resize resize event
881 */
882 public void onResize(final TResizeEvent resize) {
883 // Default: do nothing, pass to children instead
884 for (TWidget widget: children) {
885 widget.onResize(resize);
886 }
887 }
888
889 /**
890 * Method that subclasses can override to handle posted command events.
891 *
892 * @param command command event
893 */
894 public void onCommand(final TCommandEvent command) {
895 // Default: do nothing, pass to children instead
896 for (TWidget widget: children) {
897 widget.onCommand(command);
898 }
899 }
900
901 /**
902 * Method that subclasses can override to handle menu or posted menu
903 * events.
904 *
905 * @param menu menu event
906 */
907 public void onMenu(final TMenuEvent menu) {
908 // Default: do nothing, pass to children instead
909 for (TWidget widget: children) {
910 widget.onMenu(menu);
911 }
912 }
913
914 /**
915 * Method that subclasses can override to do processing when the UI is
916 * idle.
917 */
918 public void onIdle() {
919 // Default: do nothing, pass to children instead
920 for (TWidget widget: children) {
921 widget.onIdle();
922 }
923 }
924
925 /**
926 * Consume event. Subclasses that want to intercept all events in one go
927 * can override this method.
928 *
929 * @param event keyboard, mouse, resize, command, or menu event
930 */
931 public void handleEvent(final TInputEvent event) {
932 // System.err.printf("TWidget (%s) event: %s\n", this.getClass().getName(),
933 // event);
934
935 if (!enabled) {
936 // Discard event
937 // System.err.println(" -- discard --");
938 return;
939 }
940
941 if (event instanceof TKeypressEvent) {
942 onKeypress((TKeypressEvent) event);
943 } else if (event instanceof TMouseEvent) {
944
945 TMouseEvent mouse = (TMouseEvent) event;
946
947 switch (mouse.getType()) {
948
949 case MOUSE_DOWN:
950 onMouseDown(mouse);
951 break;
952
953 case MOUSE_UP:
954 onMouseUp(mouse);
955 break;
956
957 case MOUSE_MOTION:
958 onMouseMotion(mouse);
959 break;
960
961 default:
962 throw new IllegalArgumentException("Invalid mouse event type: "
963 + mouse.getType());
964 }
965 } else if (event instanceof TResizeEvent) {
966 onResize((TResizeEvent) event);
967 } else if (event instanceof TCommandEvent) {
968 onCommand((TCommandEvent) event);
969 } else if (event instanceof TMenuEvent) {
970 onMenu((TMenuEvent) event);
971 }
972
973 // Do nothing else
974 return;
975 }
976
977 /**
978 * Check if a mouse press/release event coordinate is contained in this
979 * widget.
980 *
981 * @param mouse a mouse-based event
982 * @return whether or not a mouse click would be sent to this widget
983 */
984 public final boolean mouseWouldHit(final TMouseEvent mouse) {
985
986 if (!enabled) {
987 return false;
988 }
989
990 if ((mouse.getAbsoluteX() >= getAbsoluteX())
991 && (mouse.getAbsoluteX() < getAbsoluteX() + width)
992 && (mouse.getAbsoluteY() >= getAbsoluteY())
993 && (mouse.getAbsoluteY() < getAbsoluteY() + height)
994 ) {
995 return true;
996 }
997 return false;
998 }
999
1000 /**
1001 * Convenience function to add a label to this container/window.
1002 *
1003 * @param text label
1004 * @param x column relative to parent
1005 * @param y row relative to parent
1006 * @return the new label
1007 */
1008 public final TLabel addLabel(final String text, final int x, final int y) {
1009 return addLabel(text, x, y, "tlabel");
1010 }
1011
1012 /**
1013 * Convenience function to add a label to this container/window.
1014 *
1015 * @param text label
1016 * @param x column relative to parent
1017 * @param y row relative to parent
1018 * @param colorKey ColorTheme key color to use for foreground text.
1019 * Default is "tlabel"
1020 * @return the new label
1021 */
1022 public final TLabel addLabel(final String text, final int x, final int y,
1023 final String colorKey) {
1024
1025 return new TLabel(this, text, x, y, colorKey);
1026 }
1027
1028 /**
1029 * Convenience function to add a button to this container/window.
1030 *
1031 * @param text label on the button
1032 * @param x column relative to parent
1033 * @param y row relative to parent
1034 * @param action to call when button is pressed
1035 * @return the new button
1036 */
1037 public final TButton addButton(final String text, final int x, final int y,
1038 final TAction action) {
1039
1040 return new TButton(this, text, x, y, action);
1041 }
1042
1043 /**
1044 * Convenience function to add a checkbox to this container/window.
1045 *
1046 * @param x column relative to parent
1047 * @param y row relative to parent
1048 * @param label label to display next to (right of) the checkbox
1049 * @param checked initial check state
1050 * @return the new checkbox
1051 */
1052 public final TCheckbox addCheckbox(final int x, final int y,
1053 final String label, final boolean checked) {
1054
1055 return new TCheckbox(this, x, y, label, checked);
1056 }
1057
1058 /**
1059 * Convenience function to add a progress bar to this container/window.
1060 *
1061 * @param x column relative to parent
1062 * @param y row relative to parent
1063 * @param width width of progress bar
1064 * @param value initial value of percent complete
1065 * @return the new progress bar
1066 */
1067 public final TProgressBar addProgressBar(final int x, final int y,
1068 final int width, final int value) {
1069
1070 return new TProgressBar(this, x, y, width, value);
1071 }
1072
1073 /**
1074 * Convenience function to add a radio button group to this
1075 * container/window.
1076 *
1077 * @param x column relative to parent
1078 * @param y row relative to parent
1079 * @param label label to display on the group box
1080 * @return the new radio button group
1081 */
1082 public final TRadioGroup addRadioGroup(final int x, final int y,
1083 final String label) {
1084
1085 return new TRadioGroup(this, x, y, label);
1086 }
1087
1088 /**
1089 * Convenience function to add a text field to this container/window.
1090 *
1091 * @param x column relative to parent
1092 * @param y row relative to parent
1093 * @param width visible text width
1094 * @param fixed if true, the text cannot exceed the display width
1095 * @return the new text field
1096 */
1097 public final TField addField(final int x, final int y,
1098 final int width, final boolean fixed) {
1099
1100 return new TField(this, x, y, width, fixed);
1101 }
1102
1103 /**
1104 * Convenience function to add a text field to this container/window.
1105 *
1106 * @param x column relative to parent
1107 * @param y row relative to parent
1108 * @param width visible text width
1109 * @param fixed if true, the text cannot exceed the display width
1110 * @param text initial text, default is empty string
1111 * @return the new text field
1112 */
1113 public final TField addField(final int x, final int y,
1114 final int width, final boolean fixed, final String text) {
1115
1116 return new TField(this, x, y, width, fixed, text);
1117 }
1118
1119 /**
1120 * Convenience function to add a text field to this container/window.
1121 *
1122 * @param x column relative to parent
1123 * @param y row relative to parent
1124 * @param width visible text width
1125 * @param fixed if true, the text cannot exceed the display width
1126 * @param text initial text, default is empty string
1127 * @param enterAction function to call when enter key is pressed
1128 * @param updateAction function to call when the text is updated
1129 * @return the new text field
1130 */
1131 public final TField addField(final int x, final int y,
1132 final int width, final boolean fixed, final String text,
1133 final TAction enterAction, final TAction updateAction) {
1134
1135 return new TField(this, x, y, width, fixed, text, enterAction,
1136 updateAction);
1137 }
1138
1139 /**
1140 * Convenience function to add a scrollable text box to this
1141 * container/window.
1142 *
1143 * @param text text on the screen
1144 * @param x column relative to parent
1145 * @param y row relative to parent
1146 * @param width width of text area
1147 * @param height height of text area
1148 * @param colorKey ColorTheme key color to use for foreground text
1149 * @return the new text box
1150 */
1151 public final TText addText(final String text, final int x,
1152 final int y, final int width, final int height, final String colorKey) {
1153
1154 return new TText(this, text, x, y, width, height, colorKey);
1155 }
1156
1157 /**
1158 * Convenience function to add a scrollable text box to this
1159 * container/window.
1160 *
1161 * @param text text on the screen
1162 * @param x column relative to parent
1163 * @param y row relative to parent
1164 * @param width width of text area
1165 * @param height height of text area
1166 * @return the new text box
1167 */
1168 public final TText addText(final String text, final int x, final int y,
1169 final int width, final int height) {
1170
1171 return new TText(this, text, x, y, width, height, "ttext");
1172 }
1173
1174 /**
1175 * Convenience function to spawn a message box.
1176 *
1177 * @param title window title, will be centered along the top border
1178 * @param caption message to display. Use embedded newlines to get a
1179 * multi-line box.
1180 * @return the new message box
1181 */
1182 public final TMessageBox messageBox(final String title,
1183 final String caption) {
1184
1185 return getApplication().messageBox(title, caption, TMessageBox.Type.OK);
1186 }
1187
1188 /**
1189 * Convenience function to spawn a message box.
1190 *
1191 * @param title window title, will be centered along the top border
1192 * @param caption message to display. Use embedded newlines to get a
1193 * multi-line box.
1194 * @param type one of the TMessageBox.Type constants. Default is
1195 * Type.OK.
1196 * @return the new message box
1197 */
1198 public final TMessageBox messageBox(final String title,
1199 final String caption, final TMessageBox.Type type) {
1200
1201 return getApplication().messageBox(title, caption, type);
1202 }
1203
1204 /**
1205 * Convenience function to spawn an input box.
1206 *
1207 * @param title window title, will be centered along the top border
1208 * @param caption message to display. Use embedded newlines to get a
1209 * multi-line box.
1210 * @return the new input box
1211 */
1212 public final TInputBox inputBox(final String title, final String caption) {
1213
1214 return getApplication().inputBox(title, caption);
1215 }
1216
1217 /**
1218 * Convenience function to spawn an input box.
1219 *
1220 * @param title window title, will be centered along the top border
1221 * @param caption message to display. Use embedded newlines to get a
1222 * multi-line box.
1223 * @param text initial text to seed the field with
1224 * @return the new input box
1225 */
1226 public final TInputBox inputBox(final String title, final String caption,
1227 final String text) {
1228
1229 return getApplication().inputBox(title, caption, text);
1230 }
1231
1232 /**
1233 * Convenience function to add a password text field to this
1234 * container/window.
1235 *
1236 * @param x column relative to parent
1237 * @param y row relative to parent
1238 * @param width visible text width
1239 * @param fixed if true, the text cannot exceed the display width
1240 * @return the new text field
1241 */
1242 public final TPasswordField addPasswordField(final int x, final int y,
1243 final int width, final boolean fixed) {
1244
1245 return new TPasswordField(this, x, y, width, fixed);
1246 }
1247
1248 /**
1249 * Convenience function to add a password text field to this
1250 * container/window.
1251 *
1252 * @param x column relative to parent
1253 * @param y row relative to parent
1254 * @param width visible text width
1255 * @param fixed if true, the text cannot exceed the display width
1256 * @param text initial text, default is empty string
1257 * @return the new text field
1258 */
1259 public final TPasswordField addPasswordField(final int x, final int y,
1260 final int width, final boolean fixed, final String text) {
1261
1262 return new TPasswordField(this, x, y, width, fixed, text);
1263 }
1264
1265 /**
1266 * Convenience function to add a password text field to this
1267 * container/window.
1268 *
1269 * @param x column relative to parent
1270 * @param y row relative to parent
1271 * @param width visible text width
1272 * @param fixed if true, the text cannot exceed the display width
1273 * @param text initial text, default is empty string
1274 * @param enterAction function to call when enter key is pressed
1275 * @param updateAction function to call when the text is updated
1276 * @return the new text field
1277 */
1278 public final TPasswordField addPasswordField(final int x, final int y,
1279 final int width, final boolean fixed, final String text,
1280 final TAction enterAction, final TAction updateAction) {
1281
1282 return new TPasswordField(this, x, y, width, fixed, text, enterAction,
1283 updateAction);
1284 }
1285
1286 /**
1287 * Convenience function to add a tree view to this container/window.
1288 *
1289 * @param x column relative to parent
1290 * @param y row relative to parent
1291 * @param width width of tree view
1292 * @param height height of tree view
1293 */
1294 public final TTreeView addTreeView(final int x, final int y,
1295 final int width, final int height) {
1296
1297 return new TTreeView(this, x, y, width, height);
1298 }
1299
1300 /**
1301 * Convenience function to add a tree view to this container/window.
1302 *
1303 * @param x column relative to parent
1304 * @param y row relative to parent
1305 * @param width width of tree view
1306 * @param height height of tree view
1307 * @param action action to perform when an item is selected
1308 */
1309 public final TTreeView addTreeView(final int x, final int y,
1310 final int width, final int height, final TAction action) {
1311
1312 return new TTreeView(this, x, y, width, height, action);
1313 }
1314
1315
1316 }