expose ugly windows hack
[fanfix.git] / src / jexer / TText.java
CommitLineData
daa4106c 1/*
cc99cba8
KL
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
cc99cba8 5 *
a69ed767 6 * Copyright (C) 2019 Kevin Lamonte
cc99cba8 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:
cc99cba8 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.
cc99cba8 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.
cc99cba8
KL
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer;
30
a69ed767 31import java.util.Arrays;
cc99cba8
KL
32import java.util.LinkedList;
33import java.util.List;
34
35import jexer.bits.CellAttributes;
e820d5dd 36import jexer.bits.StringUtils;
cc99cba8
KL
37import jexer.event.TKeypressEvent;
38import jexer.event.TMouseEvent;
615a0d99
KL
39import static jexer.TKeypress.kbDown;
40import static jexer.TKeypress.kbEnd;
41import static jexer.TKeypress.kbHome;
42import static jexer.TKeypress.kbLeft;
43import static jexer.TKeypress.kbPgDn;
44import static jexer.TKeypress.kbPgUp;
45import static jexer.TKeypress.kbRight;
46import static jexer.TKeypress.kbUp;
cc99cba8
KL
47
48/**
7c870d89
KL
49 * TText implements a simple scrollable text area. It reflows automatically on
50 * resize.
cc99cba8 51 */
051e2913 52public class TText extends TScrollableWidget {
cc99cba8 53
615a0d99
KL
54 // ------------------------------------------------------------------------
55 // Constants --------------------------------------------------------------
56 // ------------------------------------------------------------------------
57
7657ad8c
KL
58 /**
59 * Available text justifications.
60 */
61 public enum Justification {
a69ed767
KL
62
63 /**
64 * Not justified at all, use spacing as provided by the client.
65 */
66 NONE,
67
7657ad8c
KL
68 /**
69 * Left-justified text.
70 */
71 LEFT,
72
73 /**
74 * Centered text.
75 */
76 CENTER,
77
78 /**
79 * Right-justified text.
80 */
81 RIGHT,
82
83 /**
84 * Fully-justified text.
85 */
86 FULL,
87 }
88
615a0d99
KL
89 // ------------------------------------------------------------------------
90 // Variables --------------------------------------------------------------
91 // ------------------------------------------------------------------------
92
7657ad8c
KL
93 /**
94 * How to justify the text.
95 */
96 private Justification justification = Justification.LEFT;
97
cc99cba8
KL
98 /**
99 * Text to display.
100 */
101 private String text;
102
103 /**
104 * Text converted to lines.
105 */
106 private List<String> lines;
107
108 /**
109 * Text color.
110 */
111 private String colorKey;
112
113 /**
56661844 114 * Maximum width of a single line.
cc99cba8 115 */
56661844 116 private int maxLineWidth;
cc99cba8
KL
117
118 /**
56661844 119 * Number of lines between each paragraph.
cc99cba8 120 */
56661844 121 private int lineSpacing = 1;
cc99cba8 122
615a0d99
KL
123 // ------------------------------------------------------------------------
124 // Constructors -----------------------------------------------------------
125 // ------------------------------------------------------------------------
cc99cba8
KL
126
127 /**
128 * Public constructor.
129 *
5dfd1c11
KL
130 * @param parent parent widget
131 * @param text text on the screen
132 * @param x column relative to parent
133 * @param y row relative to parent
134 * @param width width of text area
135 * @param height height of text area
cc99cba8
KL
136 */
137 public TText(final TWidget parent, final String text, final int x,
7c870d89 138 final int y, final int width, final int height) {
cc99cba8
KL
139
140 this(parent, text, x, y, width, height, "ttext");
141 }
142
143 /**
144 * Public constructor.
145 *
5dfd1c11
KL
146 * @param parent parent widget
147 * @param text text on the screen
148 * @param x column relative to parent
149 * @param y row relative to parent
150 * @param width width of text area
151 * @param height height of text area
152 * @param colorKey ColorTheme key color to use for foreground
153 * text. Default is "ttext".
cc99cba8
KL
154 */
155 public TText(final TWidget parent, final String text, final int x,
7c870d89
KL
156 final int y, final int width, final int height,
157 final String colorKey) {
cc99cba8
KL
158
159 // Set parent and window
a83fea2b 160 super(parent, x, y, width, height);
cc99cba8 161
cc99cba8
KL
162 this.text = text;
163 this.colorKey = colorKey;
164
165 lines = new LinkedList<String>();
166
56661844
KL
167 vScroller = new TVScroller(this, getWidth() - 1, 0, getHeight() - 1);
168 hScroller = new THScroller(this, 0, getHeight() - 1, getWidth() - 1);
169 reflowData();
cc99cba8
KL
170 }
171
615a0d99
KL
172 // ------------------------------------------------------------------------
173 // TScrollableWidget ------------------------------------------------------
174 // ------------------------------------------------------------------------
175
cc99cba8
KL
176 /**
177 * Draw the text box.
178 */
179 @Override
180 public void draw() {
181 // Setup my color
182 CellAttributes color = getTheme().getColor(colorKey);
183
184 int begin = vScroller.getValue();
185 int topY = 0;
186 for (int i = begin; i < lines.size(); i++) {
187 String line = lines.get(i);
e820d5dd 188 if (hScroller.getValue() < StringUtils.width(line)) {
cc99cba8
KL
189 line = line.substring(hScroller.getValue());
190 } else {
191 line = "";
192 }
193 String formatString = "%-" + Integer.toString(getWidth() - 1) + "s";
a69ed767 194 putStringXY(0, topY, String.format(formatString, line), color);
cc99cba8
KL
195 topY++;
196
7c870d89 197 if (topY >= (getHeight() - 1)) {
cc99cba8
KL
198 break;
199 }
200 }
201
202 // Pad the rest with blank lines
7c870d89 203 for (int i = topY; i < (getHeight() - 1); i++) {
a69ed767 204 hLineXY(0, i, getWidth() - 1, ' ', color);
cc99cba8
KL
205 }
206
207 }
208
209 /**
210 * Handle mouse press events.
211 *
5dfd1c11 212 * @param mouse mouse button press event
cc99cba8
KL
213 */
214 @Override
215 public void onMouseDown(final TMouseEvent mouse) {
7c870d89 216 if (mouse.isMouseWheelUp()) {
cc99cba8
KL
217 vScroller.decrement();
218 return;
219 }
7c870d89 220 if (mouse.isMouseWheelDown()) {
cc99cba8
KL
221 vScroller.increment();
222 return;
223 }
224
225 // Pass to children
226 super.onMouseDown(mouse);
227 }
228
229 /**
230 * Handle keystrokes.
231 *
5dfd1c11 232 * @param keypress keystroke event
cc99cba8
KL
233 */
234 @Override
235 public void onKeypress(final TKeypressEvent keypress) {
236 if (keypress.equals(kbLeft)) {
237 hScroller.decrement();
238 } else if (keypress.equals(kbRight)) {
239 hScroller.increment();
240 } else if (keypress.equals(kbUp)) {
241 vScroller.decrement();
242 } else if (keypress.equals(kbDown)) {
243 vScroller.increment();
244 } else if (keypress.equals(kbPgUp)) {
245 vScroller.bigDecrement();
246 } else if (keypress.equals(kbPgDn)) {
247 vScroller.bigIncrement();
248 } else if (keypress.equals(kbHome)) {
249 vScroller.toTop();
250 } else if (keypress.equals(kbEnd)) {
251 vScroller.toBottom();
252 } else {
253 // Pass other keys (tab etc.) on
254 super.onKeypress(keypress);
255 }
256 }
257
615a0d99
KL
258 /**
259 * Resize text and scrollbars for a new width/height.
260 */
261 @Override
262 public void reflowData() {
263 // Reset the lines
264 lines.clear();
265
266 // Break up text into paragraphs
267 String[] paragraphs = text.split("\n\n");
268 for (String p : paragraphs) {
269 switch (justification) {
a69ed767
KL
270 case NONE:
271 lines.addAll(Arrays.asList(p.split("\n")));
272 break;
615a0d99
KL
273 case LEFT:
274 lines.addAll(jexer.bits.StringUtils.left(p,
275 getWidth() - 1));
276 break;
277 case CENTER:
278 lines.addAll(jexer.bits.StringUtils.center(p,
279 getWidth() - 1));
280 break;
281 case RIGHT:
282 lines.addAll(jexer.bits.StringUtils.right(p,
283 getWidth() - 1));
284 break;
285 case FULL:
286 lines.addAll(jexer.bits.StringUtils.full(p,
287 getWidth() - 1));
288 break;
289 }
290
291 for (int i = 0; i < lineSpacing; i++) {
292 lines.add("");
293 }
294 }
295 computeBounds();
296 }
297
298 // ------------------------------------------------------------------------
299 // TText ------------------------------------------------------------------
300 // ------------------------------------------------------------------------
301
302 /**
303 * Set the text.
304 *
305 * @param text new text to display
306 */
307 public void setText(final String text) {
308 this.text = text;
309 reflowData();
310 }
311
312 /**
313 * Get the text.
314 *
315 * @return the text
316 */
317 public String getText() {
318 return text;
319 }
320
321 /**
322 * Convenience method used by TWindowLoggerOutput.
323 *
324 * @param line new line to add
325 */
326 public void addLine(final String line) {
e820d5dd 327 if (StringUtils.width(text) == 0) {
615a0d99
KL
328 text = line;
329 } else {
330 text += "\n\n";
331 text += line;
332 }
333 reflowData();
334 }
335
336 /**
337 * Recompute the bounds for the scrollbars.
338 */
339 private void computeBounds() {
340 maxLineWidth = 0;
341 for (String line : lines) {
e820d5dd
KL
342 if (StringUtils.width(line) > maxLineWidth) {
343 maxLineWidth = StringUtils.width(line);
615a0d99
KL
344 }
345 }
346
347 vScroller.setTopValue(0);
348 vScroller.setBottomValue((lines.size() - getHeight()) + 1);
349 if (vScroller.getBottomValue() < 0) {
350 vScroller.setBottomValue(0);
351 }
352 if (vScroller.getValue() > vScroller.getBottomValue()) {
353 vScroller.setValue(vScroller.getBottomValue());
354 }
355
356 hScroller.setLeftValue(0);
357 hScroller.setRightValue((maxLineWidth - getWidth()) + 1);
358 if (hScroller.getRightValue() < 0) {
359 hScroller.setRightValue(0);
360 }
361 if (hScroller.getValue() > hScroller.getRightValue()) {
362 hScroller.setValue(hScroller.getRightValue());
363 }
364 }
365
366 /**
367 * Set justification.
368 *
369 * @param justification LEFT, CENTER, RIGHT, or FULL
370 */
371 public void setJustification(final Justification justification) {
372 this.justification = justification;
373 reflowData();
374 }
375
376 /**
377 * Left-justify the text.
378 */
379 public void leftJustify() {
380 justification = Justification.LEFT;
381 reflowData();
382 }
383
384 /**
385 * Center-justify the text.
386 */
387 public void centerJustify() {
388 justification = Justification.CENTER;
389 reflowData();
390 }
391
392 /**
393 * Right-justify the text.
394 */
395 public void rightJustify() {
396 justification = Justification.RIGHT;
397 reflowData();
398 }
399
400 /**
401 * Fully-justify the text.
402 */
403 public void fullJustify() {
404 justification = Justification.FULL;
405 reflowData();
406 }
407
cc99cba8 408}