stubs for TFileOpenBox, cleanup putStringXY
[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.io.IOException;
34 import java.util.List;
35 import java.util.LinkedList;
36
37 import jexer.bits.ColorTheme;
38 import jexer.event.TCommandEvent;
39 import jexer.event.TInputEvent;
40 import jexer.event.TKeypressEvent;
41 import jexer.event.TMenuEvent;
42 import jexer.event.TMouseEvent;
43 import jexer.event.TResizeEvent;
44 import jexer.io.Screen;
45 import jexer.menu.TMenu;
46 import static jexer.TKeypress.*;
47
48 /**
49 * TWidget is the base class of all objects that can be drawn on screen or
50 * handle user input events.
51 */
52 public abstract class TWidget implements Comparable<TWidget> {
53
54 /**
55 * Every widget has a parent widget that it may be "contained" in. For
56 * example, a TWindow might contain several TTextFields, or a TComboBox
57 * may contain a TScrollBar.
58 */
59 private TWidget parent = null;
60
61 /**
62 * Get parent widget.
63 *
64 * @return parent widget
65 */
66 public final TWidget getParent() {
67 return parent;
68 }
69
70 /**
71 * Backdoor access for TWindow's constructor. ONLY TWindow USES THIS.
72 *
73 * @param window the top-level window
74 * @param x column relative to parent
75 * @param y row relative to parent
76 * @param width width of window
77 * @param height height of window
78 */
79 protected final void setupForTWindow(final TWindow window,
80 final int x, final int y, final int width, final int height) {
81
82 this.parent = window;
83 this.window = window;
84 this.x = x;
85 this.y = y;
86 this.width = width;
87 this.height = height;
88 }
89
90 /**
91 * Get this TWidget's parent TApplication.
92 *
93 * @return the parent TApplication
94 */
95 public TApplication getApplication() {
96 return window.getApplication();
97 }
98
99 /**
100 * Get the Screen.
101 *
102 * @return the Screen
103 */
104 public Screen getScreen() {
105 return window.getScreen();
106 }
107
108 /**
109 * Child widgets that this widget contains.
110 */
111 private List<TWidget> children;
112
113 /**
114 * Get the list of child widgets that this widget contains.
115 *
116 * @return the list of child widgets
117 */
118 public List<TWidget> getChildren() {
119 return children;
120 }
121
122 /**
123 * The currently active child widget that will receive keypress events.
124 */
125 private TWidget activeChild = null;
126
127 /**
128 * If true, this widget will receive events.
129 */
130 private boolean active = false;
131
132 /**
133 * Get active flag.
134 *
135 * @return if true, this widget will receive events
136 */
137 public final boolean isActive() {
138 return active;
139 }
140
141 /**
142 * Set active flag.
143 *
144 * @param active if true, this widget will receive events
145 */
146 public final void setActive(final boolean active) {
147 this.active = active;
148 }
149
150 /**
151 * The window that this widget draws to.
152 */
153 private TWindow window = null;
154
155 /**
156 * Get the window this widget is on.
157 *
158 * @return the window
159 */
160 public final TWindow getWindow() {
161 return window;
162 }
163
164 /**
165 * Absolute X position of the top-left corner.
166 */
167 private int x = 0;
168
169 /**
170 * Get X position.
171 *
172 * @return absolute X position of the top-left corner
173 */
174 public final int getX() {
175 return x;
176 }
177
178 /**
179 * Set X position.
180 *
181 * @param x absolute X position of the top-left corner
182 */
183 public final void setX(final int x) {
184 this.x = x;
185 }
186
187 /**
188 * Absolute Y position of the top-left corner.
189 */
190 private int y = 0;
191
192 /**
193 * Get Y position.
194 *
195 * @return absolute Y position of the top-left corner
196 */
197 public final int getY() {
198 return y;
199 }
200
201 /**
202 * Set Y position.
203 *
204 * @param y absolute Y position of the top-left corner
205 */
206 public final void setY(final int y) {
207 this.y = y;
208 }
209
210 /**
211 * Width.
212 */
213 private int width = 0;
214
215 /**
216 * Get the width.
217 *
218 * @return widget width
219 */
220 public final int getWidth() {
221 return this.width;
222 }
223
224 /**
225 * Change the width.
226 *
227 * @param width new widget width
228 */
229 public final void setWidth(final int width) {
230 this.width = width;
231 }
232
233 /**
234 * Height.
235 */
236 private int height = 0;
237
238 /**
239 * Get the height.
240 *
241 * @return widget height
242 */
243 public final int getHeight() {
244 return this.height;
245 }
246
247 /**
248 * Change the height.
249 *
250 * @param height new widget height
251 */
252 public final void setHeight(final int height) {
253 this.height = height;
254 }
255
256 /**
257 * My tab order inside a window or containing widget.
258 */
259 private int tabOrder = 0;
260
261 /**
262 * If true, this widget can be tabbed to or receive events.
263 */
264 private boolean enabled = true;
265
266 /**
267 * Get enabled flag.
268 *
269 * @return if true, this widget can be tabbed to or receive events
270 */
271 public final boolean isEnabled() {
272 return enabled;
273 }
274
275 /**
276 * Set enabled flag.
277 *
278 * @param enabled if true, this widget can be tabbed to or receive events
279 */
280 public final void setEnabled(final boolean enabled) {
281 this.enabled = enabled;
282 if (!enabled) {
283 active = false;
284 // See if there are any active siblings to switch to
285 boolean foundSibling = false;
286 if (parent != null) {
287 for (TWidget w: parent.children) {
288 if ((w.enabled)
289 && !(this instanceof THScroller)
290 && !(this instanceof TVScroller)
291 ) {
292 parent.activate(w);
293 foundSibling = true;
294 break;
295 }
296 }
297 if (!foundSibling) {
298 parent.activeChild = null;
299 }
300 }
301 }
302 }
303
304 /**
305 * If true, this widget has a cursor.
306 */
307 private boolean cursorVisible = false;
308
309 /**
310 * Set visible cursor flag.
311 *
312 * @param cursorVisible if true, this widget has a cursor
313 */
314 public final void setCursorVisible(final boolean cursorVisible) {
315 this.cursorVisible = cursorVisible;
316 }
317
318 /**
319 * See if this widget has a visible cursor.
320 *
321 * @return if true, this widget has a visible cursor
322 */
323 public final boolean isCursorVisible() {
324 return cursorVisible;
325 }
326
327 /**
328 * Cursor column position in relative coordinates.
329 */
330 private int cursorX = 0;
331
332 /**
333 * Get cursor X value.
334 *
335 * @return cursor column position in relative coordinates
336 */
337 public final int getCursorX() {
338 return cursorX;
339 }
340
341 /**
342 * Set cursor X value.
343 *
344 * @param cursorX column position in relative coordinates
345 */
346 public final void setCursorX(final int cursorX) {
347 this.cursorX = cursorX;
348 }
349
350 /**
351 * Cursor row position in relative coordinates.
352 */
353 private int cursorY = 0;
354
355 /**
356 * Get cursor Y value.
357 *
358 * @return cursor row position in relative coordinates
359 */
360 public final int getCursorY() {
361 return cursorY;
362 }
363
364 /**
365 * Set cursor Y value.
366 *
367 * @param cursorY row position in relative coordinates
368 */
369 public final void setCursorY(final int cursorY) {
370 this.cursorY = cursorY;
371 }
372
373 /**
374 * Comparison operator sorts on:
375 * <ul>
376 * <li>tabOrder for TWidgets</li>
377 * <li>z for TWindows</li>
378 * <li>text for TTreeItems</li>
379 * </ul>
380 *
381 * @param that another TWidget, TWindow, or TTreeItem instance
382 * @return difference between this.tabOrder and that.tabOrder, or
383 * difference between this.z and that.z, or String.compareTo(text)
384 */
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 * Convenience function to spawn a file open box.
1317 *
1318 * @param path path of selected file
1319 * @return the result of the new file open box
1320 */
1321 public final String fileOpenBox(final String path) throws IOException {
1322 return getApplication().fileOpenBox(path);
1323 }
1324
1325 /**
1326 * Convenience function to spawn a file open box.
1327 *
1328 * @param path path of selected file
1329 * @param type one of the Type constants
1330 * @return the result of the new file open box
1331 */
1332 public final String fileOpenBox(final String path,
1333 final TFileOpenBox.Type type) throws IOException {
1334
1335 return getApplication().fileOpenBox(path, type);
1336 }
1337 /**
1338 * Convenience function to add a directory list to this container/window.
1339 *
1340 * @param path directory path, must be a directory
1341 * @param x column relative to parent
1342 * @param y row relative to parent
1343 * @param width width of text area
1344 * @param height height of text area
1345 */
1346 public final TDirectoryList addDirectoryList(final String path, final int x,
1347 final int y, final int width, final int height) {
1348
1349 return new TDirectoryList(this, path, x, y, width, height, null);
1350 }
1351
1352 /**
1353 * Convenience function to add a directory list to this container/window.
1354 *
1355 * @param path directory path, must be a directory
1356 * @param x column relative to parent
1357 * @param y row relative to parent
1358 * @param width width of text area
1359 * @param height height of text area
1360 * @param action action to perform when an item is selected
1361 */
1362 public final TDirectoryList addDirectoryList(final String path, final int x,
1363 final int y, final int width, final int height, final TAction action) {
1364
1365 return new TDirectoryList(this, path, x, y, width, height, action);
1366 }
1367
1368 }