Only allow keyboard resizing when RESIZABLE is set
[fanfix.git] / src / jexer / demos / DemoTextWindow.java
CommitLineData
e3dfbd23
KL
1/*
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
e3dfbd23 5 *
a2018e99 6 * Copyright (C) 2017 Kevin Lamonte
e3dfbd23 7 *
e16dda65
KL
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
e3dfbd23 14 *
e16dda65
KL
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
e3dfbd23 17 *
e16dda65
KL
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
e3dfbd23
KL
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer.demos;
30
31import jexer.*;
32import jexer.event.*;
efb7af1f 33import jexer.menu.*;
2ce6dab2
KL
34import static jexer.TCommand.*;
35import static jexer.TKeypress.*;
e3dfbd23
KL
36
37/**
38 * This window demonstates the TText, THScroller, and TVScroller widgets.
39 */
7668cb45 40public class DemoTextWindow extends TWindow {
e3dfbd23
KL
41
42 /**
43 * Hang onto my TText so I can resize it with the window.
44 */
45 private TText textField;
46
a043164f
KL
47 /**
48 * Public constructor makes a text window out of any string.
49 *
50 * @param parent the main application
51 * @param title the text string
52 * @param text the text string
53 */
54 public DemoTextWindow(final TApplication parent, final String title,
55 final String text) {
56
7657ad8c
KL
57 super(parent, title, 0, 0, 44, 22, RESIZABLE);
58 textField = addText(text, 1, 3, 40, 16);
59
60 addButton("&Left", 1, 1, new TAction() {
61 public void DO() {
62 textField.leftJustify();
63 }
64 });
65
66 addButton("&Center", 10, 1, new TAction() {
67 public void DO() {
68 textField.centerJustify();
69 }
70 });
71
72 addButton("&Right", 21, 1, new TAction() {
73 public void DO() {
74 textField.rightJustify();
75 }
76 });
77
78 addButton("&Full", 31, 1, new TAction() {
79 public void DO() {
80 textField.fullJustify();
81 }
82 });
2ce6dab2
KL
83
84 statusBar = newStatusBar("Reflowable text window");
85 statusBar.addShortcutKeypress(kbF1, cmHelp, "Help");
86 statusBar.addShortcutKeypress(kbF2, cmShell, "Shell");
87 statusBar.addShortcutKeypress(kbF3, cmOpen, "Open");
88 statusBar.addShortcutKeypress(kbF10, cmExit, "Exit");
a043164f 89 }
efb7af1f 90
e3dfbd23
KL
91 /**
92 * Public constructor.
93 *
94 * @param parent the main application
95 */
96 public DemoTextWindow(final TApplication parent) {
a043164f 97 this(parent, "Text Area",
e3dfbd23
KL
98"This is an example of a reflowable text field. Some example text follows.\n" +
99"\n" +
efb7af1f
KL
100"Notice that some menu items should be disabled when this window has focus.\n" +
101"\n" +
7657ad8c
KL
102"This library implements a text-based windowing system loosely " +
103"reminiscient of Borland's [Turbo " +
104"Vision](http://en.wikipedia.org/wiki/Turbo_Vision) library. For those " +
105"wishing to use the actual C++ Turbo Vision library, see [Sergio " +
106"Sigala's updated version](http://tvision.sourceforge.net/) that runs " +
e3dfbd23
KL
107"on many more platforms.\n" +
108"\n" +
7657ad8c 109"This library is licensed MIT. See the file LICENSE for the full license " +
e16dda65 110"for the details.\n");
a043164f 111
e3dfbd23
KL
112 }
113
114 /**
115 * Handle window/screen resize events.
116 *
117 * @param event resize event
118 */
119 @Override
120 public void onResize(final TResizeEvent event) {
121 if (event.getType() == TResizeEvent.Type.WIDGET) {
122 // Resize the text field
56661844
KL
123 TResizeEvent textSize = new TResizeEvent(TResizeEvent.Type.WIDGET,
124 event.getWidth() - 4, event.getHeight() - 6);
125 textField.onResize(textSize);
e3dfbd23
KL
126 return;
127 }
128
129 // Pass to children instead
130 for (TWidget widget: getChildren()) {
131 widget.onResize(event);
132 }
133 }
e3dfbd23 134
efb7af1f
KL
135 /**
136 * Play with menu items.
137 */
138 public void onFocus() {
139 getApplication().enableMenuItem(2001);
140 getApplication().disableMenuItem(TMenu.MID_SHELL);
141 getApplication().disableMenuItem(TMenu.MID_EXIT);
142 }
143
144 /**
145 * Called by application.switchWindow() when another window gets the
146 * focus.
147 */
148 public void onUnfocus() {
149 getApplication().disableMenuItem(2001);
150 getApplication().enableMenuItem(TMenu.MID_SHELL);
151 getApplication().enableMenuItem(TMenu.MID_EXIT);
152 }
153
154}