Merge branch 'subtree'
[fanfix.git] / src / jexer / TFontChooserWindow.java
CommitLineData
e23ea538
KL
1/*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer;
30
31import java.awt.Font;
32import java.awt.GraphicsEnvironment;
33import java.util.ArrayList;
34import java.util.Arrays;
35import java.util.List;
36import java.util.ResourceBundle;
37
a75902fa 38import jexer.backend.ECMA48Terminal;
e23ea538
KL
39import jexer.backend.SwingTerminal;
40import jexer.bits.CellAttributes;
41import jexer.bits.GraphicsChars;
42import jexer.event.TKeypressEvent;
43import static jexer.TKeypress.*;
44
45/**
46 * TFontChooserWindow provides an easy UI for users to alter the running
47 * font.
48 *
49 */
50public class TFontChooserWindow extends TWindow {
51
52 /**
53 * Translated strings.
54 */
55 private static final ResourceBundle i18n = ResourceBundle.getBundle(TFontChooserWindow.class.getName());
56
57 // ------------------------------------------------------------------------
58 // Variables --------------------------------------------------------------
59 // ------------------------------------------------------------------------
60
61 /**
62 * The Swing screen.
63 */
64 private SwingTerminal terminal = null;
65
a75902fa
KL
66 /**
67 * The ECMA48 screen.
68 */
69 private ECMA48Terminal ecmaTerminal = null;
70
e23ea538
KL
71 /**
72 * The font name.
73 */
74 private TComboBox fontName;
75
76 /**
77 * The font size.
78 */
79 private TField fontSize;
80
81 /**
82 * The X text adjustment.
83 */
84 private TField textAdjustX;
85
86 /**
87 * The Y text adjustment.
88 */
89 private TField textAdjustY;
90
91 /**
92 * The height text adjustment.
93 */
94 private TField textAdjustHeight;
95
96 /**
97 * The width text adjustment.
98 */
99 private TField textAdjustWidth;
100
a75902fa
KL
101 /**
102 * The sixel palette size.
103 */
104 private TComboBox sixelPaletteSize;
105
e23ea538
KL
106 /**
107 * The original font size.
108 */
109 private int oldFontSize = 20;
110
111 /**
112 * The original font.
113 */
114 private Font oldFont = null;
115
116 /**
117 * The original text adjust X value.
118 */
119 private int oldTextAdjustX = 0;
120
121 /**
122 * The original text adjust Y value.
123 */
124 private int oldTextAdjustY = 0;
125
126 /**
127 * The original text adjust height value.
128 */
129 private int oldTextAdjustHeight = 0;
130
131 /**
132 * The original text adjust width value.
133 */
134 private int oldTextAdjustWidth = 0;
135
a75902fa
KL
136 /**
137 * The original sixel palette (number of colors) value.
138 */
139 private int oldSixelPaletteSize = 1024;
140
e23ea538
KL
141 // ------------------------------------------------------------------------
142 // Constructors -----------------------------------------------------------
143 // ------------------------------------------------------------------------
144
145 /**
146 * Public constructor. The window will be centered on screen.
147 *
148 * @param application the TApplication that manages this window
149 */
150 public TFontChooserWindow(final TApplication application) {
151
152 // Register with the TApplication
a75902fa 153 super(application, i18n.getString("windowTitle"), 0, 0, 60, 21, MODAL);
e23ea538
KL
154
155 // Add shortcut text
156 newStatusBar(i18n.getString("statusBar"));
157
158 if (getScreen() instanceof SwingTerminal) {
159 terminal = (SwingTerminal) getScreen();
160 }
a75902fa
KL
161 if (getScreen() instanceof ECMA48Terminal) {
162 ecmaTerminal = (ECMA48Terminal) getScreen();
163 }
e23ea538
KL
164
165 addLabel(i18n.getString("fontName"), 1, 1, "ttext", false);
166 addLabel(i18n.getString("fontSize"), 1, 2, "ttext", false);
167 addLabel(i18n.getString("textAdjustX"), 1, 4, "ttext", false);
168 addLabel(i18n.getString("textAdjustY"), 1, 5, "ttext", false);
169 addLabel(i18n.getString("textAdjustHeight"), 1, 6, "ttext", false);
170 addLabel(i18n.getString("textAdjustWidth"), 1, 7, "ttext", false);
a75902fa 171 addLabel(i18n.getString("sixelPaletteSize"), 1, 9, "ttext", false);
e23ea538 172
a75902fa 173 int col = 21;
e23ea538
KL
174 if (terminal == null) {
175 // Non-Swing case: we can't change anything
176 addLabel(i18n.getString("unavailable"), col, 1);
177 addLabel(i18n.getString("unavailable"), col, 2);
178 addLabel(i18n.getString("unavailable"), col, 4);
179 addLabel(i18n.getString("unavailable"), col, 5);
180 addLabel(i18n.getString("unavailable"), col, 6);
181 addLabel(i18n.getString("unavailable"), col, 7);
a75902fa
KL
182 }
183 if (ecmaTerminal == null) {
184 addLabel(i18n.getString("unavailable"), col, 9);
185 }
186 if (ecmaTerminal != null) {
187 oldSixelPaletteSize = ecmaTerminal.getSixelPaletteSize();
188
189 String [] sixelSizes = { "2", "256", "512", "1024", "2048" };
190 List<String> sizes = new ArrayList<String>();
191 sizes.addAll(Arrays.asList(sixelSizes));
192 sixelPaletteSize = addComboBox(col, 9, 10, sizes, 0, 6,
193 new TAction() {
194 public void DO() {
195 try {
196 ecmaTerminal.setSixelPaletteSize(Integer.parseInt(
197 sixelPaletteSize.getText()));
198 } catch (NumberFormatException e) {
199 // SQUASH
200 }
201 }
202 }
203 );
204 sixelPaletteSize.setText(Integer.toString(oldSixelPaletteSize));
205 }
206
207 if (terminal != null) {
e23ea538
KL
208 oldFont = terminal.getFont();
209 oldFontSize = terminal.getFontSize();
210 oldTextAdjustX = terminal.getTextAdjustX();
211 oldTextAdjustY = terminal.getTextAdjustY();
212 oldTextAdjustHeight = terminal.getTextAdjustHeight();
213 oldTextAdjustWidth = terminal.getTextAdjustWidth();
214
215 String [] fontNames = GraphicsEnvironment.
216 getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
217 List<String> fonts = new ArrayList<String>();
218 fonts.add(0, i18n.getString("builtInTerminus"));
219 fonts.addAll(Arrays.asList(fontNames));
220 fontName = addComboBox(col, 1, 25, fonts, 0, 10,
221 new TAction() {
222 public void DO() {
223 if (fontName.getText().equals(i18n.
224 getString("builtInTerminus"))) {
225
226 terminal.setDefaultFont();
227 } else {
228 terminal.setFont(new Font(fontName.getText(),
229 Font.PLAIN, terminal.getFontSize()));
230 fontSize.setText(Integer.toString(
231 terminal.getFontSize()));
232 textAdjustX.setText(Integer.toString(
233 terminal.getTextAdjustX()));
234 textAdjustY.setText(Integer.toString(
235 terminal.getTextAdjustY()));
236 textAdjustHeight.setText(Integer.toString(
237 terminal.getTextAdjustHeight()));
238 textAdjustWidth.setText(Integer.toString(
239 terminal.getTextAdjustWidth()));
240 }
241 }
242 }
243 );
244
245 // Font size
246 fontSize = addField(col, 2, 3, true,
247 Integer.toString(terminal.getFontSize()),
248 new TAction() {
249 public void DO() {
250 int currentSize = terminal.getFontSize();
251 int newSize = currentSize;
252 try {
253 newSize = Integer.parseInt(fontSize.getText());
254 } catch (NumberFormatException e) {
255 fontSize.setText(Integer.toString(currentSize));
256 }
257 if (newSize != currentSize) {
258 terminal.setFontSize(newSize);
259 textAdjustX.setText(Integer.toString(
260 terminal.getTextAdjustX()));
261 textAdjustY.setText(Integer.toString(
262 terminal.getTextAdjustY()));
263 textAdjustHeight.setText(Integer.toString(
264 terminal.getTextAdjustHeight()));
265 textAdjustWidth.setText(Integer.toString(
266 terminal.getTextAdjustWidth()));
267 }
268 }
269 },
270 null);
271
272 addSpinner(col + 3, 2,
273 new TAction() {
274 public void DO() {
275 int currentSize = terminal.getFontSize();
276 int newSize = currentSize;
277 try {
278 newSize = Integer.parseInt(fontSize.getText());
279 newSize++;
280 } catch (NumberFormatException e) {
281 fontSize.setText(Integer.toString(currentSize));
282 }
283 fontSize.setText(Integer.toString(newSize));
284 if (newSize != currentSize) {
285 terminal.setFontSize(newSize);
286 textAdjustX.setText(Integer.toString(
287 terminal.getTextAdjustX()));
288 textAdjustY.setText(Integer.toString(
289 terminal.getTextAdjustY()));
290 textAdjustHeight.setText(Integer.toString(
291 terminal.getTextAdjustHeight()));
292 textAdjustWidth.setText(Integer.toString(
293 terminal.getTextAdjustWidth()));
294 }
295 }
296 },
297 new TAction() {
298 public void DO() {
299 int currentSize = terminal.getFontSize();
300 int newSize = currentSize;
301 try {
302 newSize = Integer.parseInt(fontSize.getText());
303 newSize--;
304 } catch (NumberFormatException e) {
305 fontSize.setText(Integer.toString(currentSize));
306 }
307 fontSize.setText(Integer.toString(newSize));
308 if (newSize != currentSize) {
309 terminal.setFontSize(newSize);
310 textAdjustX.setText(Integer.toString(
311 terminal.getTextAdjustX()));
312 textAdjustY.setText(Integer.toString(
313 terminal.getTextAdjustY()));
314 textAdjustHeight.setText(Integer.toString(
315 terminal.getTextAdjustHeight()));
316 textAdjustWidth.setText(Integer.toString(
317 terminal.getTextAdjustWidth()));
318 }
319 }
320 }
321 );
322
323 // textAdjustX
324 textAdjustX = addField(col, 4, 3, true,
325 Integer.toString(terminal.getTextAdjustX()),
326 new TAction() {
327 public void DO() {
328 int currentAdjust = terminal.getTextAdjustX();
329 int newAdjust = currentAdjust;
330 try {
331 newAdjust = Integer.parseInt(textAdjustX.getText());
332 } catch (NumberFormatException e) {
333 textAdjustX.setText(Integer.toString(currentAdjust));
334 }
335 if (newAdjust != currentAdjust) {
336 terminal.setTextAdjustX(newAdjust);
337 }
338 }
339 },
340 null);
341
342 addSpinner(col + 3, 4,
343 new TAction() {
344 public void DO() {
345 int currentAdjust = terminal.getTextAdjustX();
346 int newAdjust = currentAdjust;
347 try {
348 newAdjust = Integer.parseInt(textAdjustX.getText());
349 newAdjust++;
350 } catch (NumberFormatException e) {
351 textAdjustX.setText(Integer.toString(currentAdjust));
352 }
353 textAdjustX.setText(Integer.toString(newAdjust));
354 if (newAdjust != currentAdjust) {
355 terminal.setTextAdjustX(newAdjust);
356 }
357 }
358 },
359 new TAction() {
360 public void DO() {
361 int currentAdjust = terminal.getTextAdjustX();
362 int newAdjust = currentAdjust;
363 try {
364 newAdjust = Integer.parseInt(textAdjustX.getText());
365 newAdjust--;
366 } catch (NumberFormatException e) {
367 textAdjustX.setText(Integer.toString(currentAdjust));
368 }
369 textAdjustX.setText(Integer.toString(newAdjust));
370 if (newAdjust != currentAdjust) {
371 terminal.setTextAdjustX(newAdjust);
372 }
373 }
374 }
375 );
376
377 // textAdjustY
378 textAdjustY = addField(col, 5, 3, true,
379 Integer.toString(terminal.getTextAdjustY()),
380 new TAction() {
381 public void DO() {
382 int currentAdjust = terminal.getTextAdjustY();
383 int newAdjust = currentAdjust;
384 try {
385 newAdjust = Integer.parseInt(textAdjustY.getText());
386 } catch (NumberFormatException e) {
387 textAdjustY.setText(Integer.toString(currentAdjust));
388 }
389 if (newAdjust != currentAdjust) {
390 terminal.setTextAdjustY(newAdjust);
391 }
392 }
393 },
394 null);
395
396 addSpinner(col + 3, 5,
397 new TAction() {
398 public void DO() {
399 int currentAdjust = terminal.getTextAdjustY();
400 int newAdjust = currentAdjust;
401 try {
402 newAdjust = Integer.parseInt(textAdjustY.getText());
403 newAdjust++;
404 } catch (NumberFormatException e) {
405 textAdjustY.setText(Integer.toString(currentAdjust));
406 }
407 textAdjustY.setText(Integer.toString(newAdjust));
408 if (newAdjust != currentAdjust) {
409 terminal.setTextAdjustY(newAdjust);
410 }
411 }
412 },
413 new TAction() {
414 public void DO() {
415 int currentAdjust = terminal.getTextAdjustY();
416 int newAdjust = currentAdjust;
417 try {
418 newAdjust = Integer.parseInt(textAdjustY.getText());
419 newAdjust--;
420 } catch (NumberFormatException e) {
421 textAdjustY.setText(Integer.toString(currentAdjust));
422 }
423 textAdjustY.setText(Integer.toString(newAdjust));
424 if (newAdjust != currentAdjust) {
425 terminal.setTextAdjustY(newAdjust);
426 }
427 }
428 }
429 );
430
431 // textAdjustHeight
432 textAdjustHeight = addField(col, 6, 3, true,
433 Integer.toString(terminal.getTextAdjustHeight()),
434 new TAction() {
435 public void DO() {
436 int currentAdjust = terminal.getTextAdjustHeight();
437 int newAdjust = currentAdjust;
438 try {
439 newAdjust = Integer.parseInt(textAdjustHeight.getText());
440 } catch (NumberFormatException e) {
441 textAdjustHeight.setText(Integer.toString(currentAdjust));
442 }
443 if (newAdjust != currentAdjust) {
444 terminal.setTextAdjustHeight(newAdjust);
445 }
446 }
447 },
448 null);
449
450 addSpinner(col + 3, 6,
451 new TAction() {
452 public void DO() {
453 int currentAdjust = terminal.getTextAdjustHeight();
454 int newAdjust = currentAdjust;
455 try {
456 newAdjust = Integer.parseInt(textAdjustHeight.getText());
457 newAdjust++;
458 } catch (NumberFormatException e) {
459 textAdjustHeight.setText(Integer.toString(currentAdjust));
460 }
461 textAdjustHeight.setText(Integer.toString(newAdjust));
462 if (newAdjust != currentAdjust) {
463 terminal.setTextAdjustHeight(newAdjust);
464 }
465 }
466 },
467 new TAction() {
468 public void DO() {
469 int currentAdjust = terminal.getTextAdjustHeight();
470 int newAdjust = currentAdjust;
471 try {
472 newAdjust = Integer.parseInt(textAdjustHeight.getText());
473 newAdjust--;
474 } catch (NumberFormatException e) {
475 textAdjustHeight.setText(Integer.toString(currentAdjust));
476 }
477 textAdjustHeight.setText(Integer.toString(newAdjust));
478 if (newAdjust != currentAdjust) {
479 terminal.setTextAdjustHeight(newAdjust);
480 }
481 }
482 }
483 );
484
485 // textAdjustWidth
486 textAdjustWidth = addField(col, 7, 3, true,
487 Integer.toString(terminal.getTextAdjustWidth()),
488 new TAction() {
489 public void DO() {
490 int currentAdjust = terminal.getTextAdjustWidth();
491 int newAdjust = currentAdjust;
492 try {
493 newAdjust = Integer.parseInt(textAdjustWidth.getText());
494 } catch (NumberFormatException e) {
495 textAdjustWidth.setText(Integer.toString(currentAdjust));
496 }
497 if (newAdjust != currentAdjust) {
498 terminal.setTextAdjustWidth(newAdjust);
499 }
500 }
501 },
502 null);
503
504 addSpinner(col + 3, 7,
505 new TAction() {
506 public void DO() {
507 int currentAdjust = terminal.getTextAdjustWidth();
508 int newAdjust = currentAdjust;
509 try {
510 newAdjust = Integer.parseInt(textAdjustWidth.getText());
511 newAdjust++;
512 } catch (NumberFormatException e) {
513 textAdjustWidth.setText(Integer.toString(currentAdjust));
514 }
515 textAdjustWidth.setText(Integer.toString(newAdjust));
516 if (newAdjust != currentAdjust) {
517 terminal.setTextAdjustWidth(newAdjust);
518 }
519 }
520 },
521 new TAction() {
522 public void DO() {
523 int currentAdjust = terminal.getTextAdjustWidth();
524 int newAdjust = currentAdjust;
525 try {
526 newAdjust = Integer.parseInt(textAdjustWidth.getText());
527 newAdjust--;
528 } catch (NumberFormatException e) {
529 textAdjustWidth.setText(Integer.toString(currentAdjust));
530 }
531 textAdjustWidth.setText(Integer.toString(newAdjust));
532 if (newAdjust != currentAdjust) {
533 terminal.setTextAdjustWidth(newAdjust);
534 }
535 }
536 }
537 );
538
539 }
540
541 addButton(i18n.getString("okButton"), 18, getHeight() - 4,
542 new TAction() {
543 public void DO() {
544 // Close window.
545 TFontChooserWindow.this.close();
546 }
547 });
548
549 TButton cancelButton = addButton(i18n.getString("cancelButton"),
550 30, getHeight() - 4,
551 new TAction() {
552 public void DO() {
553 // Restore old values, then close the window.
554 if (terminal != null) {
555 terminal.setFont(oldFont);
556 terminal.setFontSize(oldFontSize);
557 terminal.setTextAdjustX(oldTextAdjustX);
558 terminal.setTextAdjustY(oldTextAdjustY);
559 terminal.setTextAdjustHeight(oldTextAdjustHeight);
560 terminal.setTextAdjustWidth(oldTextAdjustWidth);
561 }
a75902fa
KL
562 if (ecmaTerminal != null) {
563 ecmaTerminal.setSixelPaletteSize(oldSixelPaletteSize);
564 }
e23ea538
KL
565 TFontChooserWindow.this.close();
566 }
567 });
568
569 // Save this for last: make the cancel button default action.
570 activate(cancelButton);
571
572 }
573
574 // ------------------------------------------------------------------------
575 // Event handlers ---------------------------------------------------------
576 // ------------------------------------------------------------------------
577
578 /**
579 * Handle keystrokes.
580 *
581 * @param keypress keystroke event
582 */
583 @Override
584 public void onKeypress(final TKeypressEvent keypress) {
585 // Escape - behave like cancel
586 if (keypress.equals(kbEsc)) {
587 // Restore old values, then close the window.
588 if (terminal != null) {
589 terminal.setFont(oldFont);
590 terminal.setFontSize(oldFontSize);
591 }
a75902fa
KL
592 if (ecmaTerminal != null) {
593 ecmaTerminal.setSixelPaletteSize(oldSixelPaletteSize);
594 }
e23ea538
KL
595 getApplication().closeWindow(this);
596 return;
597 }
598
599 // Pass to my parent
600 super.onKeypress(keypress);
601 }
602
603 // ------------------------------------------------------------------------
604 // TWindow ----------------------------------------------------------------
605 // ------------------------------------------------------------------------
606
607 /**
608 * Draw me on screen.
609 */
610 @Override
611 public void draw() {
612 super.draw();
613
614 int left = 34;
615 CellAttributes color = getTheme().getColor("ttext");
616 drawBox(left, 6, left + 24, 14, color, color, 3, false);
617 putStringXY(left + 2, 6, i18n.getString("sample"), color);
618 for (int i = 7; i < 13; i++) {
619 hLineXY(left + 1, i, 22, GraphicsChars.HATCH, color);
620 }
621
622 }
623
624 // ------------------------------------------------------------------------
625 // TFontChooserWindow -----------------------------------------------------
626 // ------------------------------------------------------------------------
627
628}