update e621 (site changed + move to not deprecated BasicSupport
[fanfix.git] / src / jexer / TImageWindow.java
CommitLineData
a69ed767
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.image.BufferedImage;
32import java.io.File;
33import java.io.IOException;
9696a8f6 34import java.util.ResourceBundle;
a69ed767
KL
35import javax.imageio.ImageIO;
36
37import jexer.event.TKeypressEvent;
38import jexer.event.TMouseEvent;
39import jexer.event.TResizeEvent;
40import static jexer.TKeypress.*;
41
42/**
43 * TImageWindow shows an image with scrollbars.
44 */
45public class TImageWindow extends TScrollableWindow {
46
9696a8f6
KL
47 /**
48 * Translated strings.
49 */
50 private static final ResourceBundle i18n = ResourceBundle.getBundle(TImageWindow.class.getName());
51
a69ed767
KL
52 // ------------------------------------------------------------------------
53 // Constants --------------------------------------------------------------
54 // ------------------------------------------------------------------------
55
56 /**
57 * The number of lines to scroll on mouse wheel up/down.
58 */
59 private static final int wheelScrollSize = 3;
60
61 // ------------------------------------------------------------------------
62 // Variables --------------------------------------------------------------
63 // ------------------------------------------------------------------------
64
65 /**
66 * Hang onto the TImage so I can resize it with the window.
67 */
68 private TImage imageField;
69
70 // ------------------------------------------------------------------------
71 // Constructors -----------------------------------------------------------
72 // ------------------------------------------------------------------------
73
74 /**
75 * Public constructor opens a file.
76 *
77 * @param parent the main application
78 * @param file the file to open
79 * @throws IOException if a java.io operation throws
80 */
81 public TImageWindow(final TApplication parent,
82 final File file) throws IOException {
83
84 this(parent, file, 0, 0, parent.getScreen().getWidth(),
2bb26984 85 parent.getDesktopBottom() - parent.getDesktopTop());
a69ed767
KL
86 }
87
88 /**
89 * Public constructor opens a file.
90 *
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
98 */
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 {
102
103 super(parent, file.getName(), x, y, width, height, RESIZABLE);
104
105 BufferedImage image = ImageIO.read(file);
106
382bc294
KL
107 imageField = addImage(0, 0, getWidth() - 2, getHeight() - 2,
108 image, 0, 0);
a69ed767
KL
109 setTitle(file.getName());
110
111 setupAfterImage();
112 }
113
114 /**
115 * Setup other fields after the image is created.
116 */
117 private void setupAfterImage() {
118 if (imageField.getRows() < getHeight() - 2) {
119 imageField.setHeight(imageField.getRows());
120 setHeight(imageField.getRows() + 2);
121 }
122 if (imageField.getColumns() < getWidth() - 2) {
123 imageField.setWidth(imageField.getColumns());
124 setWidth(imageField.getColumns() + 2);
125 }
126
69a8c368
KL
127 hScroller = new THScroller(this,
128 Math.min(Math.max(0, getWidth() - 17), 17),
129 getHeight() - 2,
130 getWidth() - Math.min(Math.max(0, getWidth() - 17), 17) - 3);
a69ed767
KL
131 vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
132 setTopValue(0);
133 setBottomValue(imageField.getRows() - imageField.getHeight());
134 setLeftValue(0);
135 setRightValue(imageField.getColumns() - imageField.getWidth());
9696a8f6
KL
136
137 statusBar = newStatusBar(i18n.getString("statusBar"));
a69ed767
KL
138 }
139
140 // ------------------------------------------------------------------------
141 // Event handlers ---------------------------------------------------------
142 // ------------------------------------------------------------------------
143
144 /**
145 * Handle mouse press events.
146 *
147 * @param mouse mouse button press event
148 */
149 @Override
150 public void onMouseDown(final TMouseEvent mouse) {
151 // Use TWidget's code to pass the event to the children.
152 super.onMouseDown(mouse);
153
154 if (mouse.isMouseWheelUp()) {
155 imageField.setTop(imageField.getTop() - wheelScrollSize);
156 } else if (mouse.isMouseWheelDown()) {
157 imageField.setTop(imageField.getTop() + wheelScrollSize);
158 }
159 setVerticalValue(imageField.getTop());
160 }
161
162 /**
163 * Handle mouse release events.
164 *
165 * @param mouse mouse button release event
166 */
167 @Override
168 public void onMouseUp(final TMouseEvent mouse) {
169 // Use TWidget's code to pass the event to the children.
170 super.onMouseUp(mouse);
171
172 if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
173 // Clicked/dragged on vertical scrollbar
174 imageField.setTop(getVerticalValue());
175 }
176 if (mouse.isMouse1() && mouseOnHorizontalScroller(mouse)) {
177 // Clicked/dragged on horizontal scrollbar
178 imageField.setLeft(getHorizontalValue());
179 }
180 }
181
182 /**
183 * Method that subclasses can override to handle mouse movements.
184 *
185 * @param mouse mouse motion event
186 */
187 @Override
188 public void onMouseMotion(final TMouseEvent mouse) {
189 // Use TWidget's code to pass the event to the children.
190 super.onMouseMotion(mouse);
191
192 if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
193 // Clicked/dragged on vertical scrollbar
194 imageField.setTop(getVerticalValue());
195 }
196 if (mouse.isMouse1() && mouseOnHorizontalScroller(mouse)) {
197 // Clicked/dragged on horizontal scrollbar
198 imageField.setLeft(getHorizontalValue());
199 }
200 }
201
202 /**
203 * Handle window/screen resize events.
204 *
205 * @param event resize event
206 */
207 @Override
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);
214
215 // Have TScrollableWindow handle the scrollbars
216 super.onResize(event);
217 return;
218 }
219
220 // Pass to children instead
221 for (TWidget widget: getChildren()) {
222 widget.onResize(event);
223 }
224 }
225
226 /**
227 * Handle keystrokes.
228 *
229 * @param keypress keystroke event
230 */
231 @Override
232 public void onKeypress(final TKeypressEvent keypress) {
233 if (keypress.equals(kbUp)) {
234 verticalDecrement();
235 imageField.setTop(getVerticalValue());
236 return;
237 }
238 if (keypress.equals(kbDown)) {
239 verticalIncrement();
240 imageField.setTop(getVerticalValue());
241 return;
242 }
243 if (keypress.equals(kbPgUp)) {
244 bigVerticalDecrement();
245 imageField.setTop(getVerticalValue());
246 return;
247 }
248 if (keypress.equals(kbPgDn)) {
249 bigVerticalIncrement();
250 imageField.setTop(getVerticalValue());
251 return;
252 }
253 if (keypress.equals(kbRight)) {
254 horizontalIncrement();
255 imageField.setLeft(getHorizontalValue());
256 return;
257 }
258 if (keypress.equals(kbLeft)) {
259 horizontalDecrement();
260 imageField.setLeft(getHorizontalValue());
261 return;
262 }
263
264 // We did not take it, let the TImage instance see it.
265 super.onKeypress(keypress);
266
267 setVerticalValue(imageField.getTop());
268 setBottomValue(imageField.getRows() - imageField.getHeight());
269 setHorizontalValue(imageField.getLeft());
270 setRightValue(imageField.getColumns() - imageField.getWidth());
271 }
272
273 // ------------------------------------------------------------------------
274 // TWindow ----------------------------------------------------------------
275 // ------------------------------------------------------------------------
276
277 /**
278 * Draw the window.
279 */
280 @Override
281 public void draw() {
282 // Draw as normal.
283 super.draw();
284
285 // We have to get the scrollbar values after we have let the image
286 // try to draw.
287 setBottomValue(imageField.getRows() - imageField.getHeight());
288 setRightValue(imageField.getColumns() - imageField.getWidth());
289 }
290
291}