2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import java
.awt
.image
.BufferedImage
;
33 import java
.io
.IOException
;
34 import java
.util
.ResourceBundle
;
35 import javax
.imageio
.ImageIO
;
37 import jexer
.event
.TKeypressEvent
;
38 import jexer
.event
.TMouseEvent
;
39 import jexer
.event
.TResizeEvent
;
40 import static jexer
.TKeypress
.*;
43 * TImageWindow shows an image with scrollbars.
45 public class TImageWindow
extends TScrollableWindow
{
50 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TImageWindow
.class.getName());
52 // ------------------------------------------------------------------------
53 // Constants --------------------------------------------------------------
54 // ------------------------------------------------------------------------
57 * The number of lines to scroll on mouse wheel up/down.
59 private static final int wheelScrollSize
= 3;
61 // ------------------------------------------------------------------------
62 // Variables --------------------------------------------------------------
63 // ------------------------------------------------------------------------
66 * Hang onto the TImage so I can resize it with the window.
68 private TImage imageField
;
70 // ------------------------------------------------------------------------
71 // Constructors -----------------------------------------------------------
72 // ------------------------------------------------------------------------
75 * Public constructor opens a file.
77 * @param parent the main application
78 * @param file the file to open
79 * @throws IOException if a java.io operation throws
81 public TImageWindow(final TApplication parent
,
82 final File file
) throws IOException
{
84 this(parent
, file
, 0, 0, parent
.getScreen().getWidth(),
85 parent
.getDesktopBottom() - parent
.getDesktopTop());
89 * Public constructor opens a file.
91 * @param parent the main application
92 * @param file the file to open
93 * @param x column relative to parent
94 * @param y row relative to parent
95 * @param width width of window
96 * @param height height of window
97 * @throws IOException if a java.io operation throws
99 public TImageWindow(final TApplication parent
, final File file
,
100 final int x
, final int y
, final int width
,
101 final int height
) throws IOException
{
103 super(parent
, file
.getName(), x
, y
, width
, height
, RESIZABLE
);
105 BufferedImage image
= ImageIO
.read(file
);
107 imageField
= addImage(0, 0, getWidth() - 2, getHeight() - 2,
109 setTitle(file
.getName());
115 * Setup other fields after the image is created.
117 private void setupAfterImage() {
118 if (imageField
.getRows() < getHeight() - 2) {
119 imageField
.setHeight(imageField
.getRows());
120 setHeight(imageField
.getRows() + 2);
122 if (imageField
.getColumns() < getWidth() - 2) {
123 imageField
.setWidth(imageField
.getColumns());
124 setWidth(imageField
.getColumns() + 2);
127 hScroller
= new THScroller(this,
128 Math
.min(Math
.max(0, getWidth() - 17), 17),
130 getWidth() - Math
.min(Math
.max(0, getWidth() - 17), 17) - 3);
131 vScroller
= new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
133 setBottomValue(imageField
.getRows() - imageField
.getHeight());
135 setRightValue(imageField
.getColumns() - imageField
.getWidth());
137 statusBar
= newStatusBar(i18n
.getString("statusBar"));
140 // ------------------------------------------------------------------------
141 // Event handlers ---------------------------------------------------------
142 // ------------------------------------------------------------------------
145 * Handle mouse press events.
147 * @param mouse mouse button press event
150 public void onMouseDown(final TMouseEvent mouse
) {
151 // Use TWidget's code to pass the event to the children.
152 super.onMouseDown(mouse
);
154 if (mouse
.isMouseWheelUp()) {
155 imageField
.setTop(imageField
.getTop() - wheelScrollSize
);
156 } else if (mouse
.isMouseWheelDown()) {
157 imageField
.setTop(imageField
.getTop() + wheelScrollSize
);
159 setVerticalValue(imageField
.getTop());
163 * Handle mouse release events.
165 * @param mouse mouse button release event
168 public void onMouseUp(final TMouseEvent mouse
) {
169 // Use TWidget's code to pass the event to the children.
170 super.onMouseUp(mouse
);
172 if (mouse
.isMouse1() && mouseOnVerticalScroller(mouse
)) {
173 // Clicked/dragged on vertical scrollbar
174 imageField
.setTop(getVerticalValue());
176 if (mouse
.isMouse1() && mouseOnHorizontalScroller(mouse
)) {
177 // Clicked/dragged on horizontal scrollbar
178 imageField
.setLeft(getHorizontalValue());
183 * Method that subclasses can override to handle mouse movements.
185 * @param mouse mouse motion event
188 public void onMouseMotion(final TMouseEvent mouse
) {
189 // Use TWidget's code to pass the event to the children.
190 super.onMouseMotion(mouse
);
192 if (mouse
.isMouse1() && mouseOnVerticalScroller(mouse
)) {
193 // Clicked/dragged on vertical scrollbar
194 imageField
.setTop(getVerticalValue());
196 if (mouse
.isMouse1() && mouseOnHorizontalScroller(mouse
)) {
197 // Clicked/dragged on horizontal scrollbar
198 imageField
.setLeft(getHorizontalValue());
203 * Handle window/screen resize events.
205 * @param event resize event
208 public void onResize(final TResizeEvent event
) {
209 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
210 // Resize the image field
211 TResizeEvent imageSize
= new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
212 event
.getWidth() - 2, event
.getHeight() - 2);
213 imageField
.onResize(imageSize
);
215 // Have TScrollableWindow handle the scrollbars
216 super.onResize(event
);
220 // Pass to children instead
221 for (TWidget widget
: getChildren()) {
222 widget
.onResize(event
);
229 * @param keypress keystroke event
232 public void onKeypress(final TKeypressEvent keypress
) {
233 if (keypress
.equals(kbUp
)) {
235 imageField
.setTop(getVerticalValue());
238 if (keypress
.equals(kbDown
)) {
240 imageField
.setTop(getVerticalValue());
243 if (keypress
.equals(kbPgUp
)) {
244 bigVerticalDecrement();
245 imageField
.setTop(getVerticalValue());
248 if (keypress
.equals(kbPgDn
)) {
249 bigVerticalIncrement();
250 imageField
.setTop(getVerticalValue());
253 if (keypress
.equals(kbRight
)) {
254 horizontalIncrement();
255 imageField
.setLeft(getHorizontalValue());
258 if (keypress
.equals(kbLeft
)) {
259 horizontalDecrement();
260 imageField
.setLeft(getHorizontalValue());
264 // We did not take it, let the TImage instance see it.
265 super.onKeypress(keypress
);
267 setVerticalValue(imageField
.getTop());
268 setBottomValue(imageField
.getRows() - imageField
.getHeight());
269 setHorizontalValue(imageField
.getLeft());
270 setRightValue(imageField
.getColumns() - imageField
.getWidth());
273 // ------------------------------------------------------------------------
274 // TWindow ----------------------------------------------------------------
275 // ------------------------------------------------------------------------
285 // We have to get the scrollbar values after we have let the image
287 setBottomValue(imageField
.getRows() - imageField
.getHeight());
288 setRightValue(imageField
.getColumns() - imageField
.getWidth());