Commit | Line | Data |
---|---|---|
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 | */ | |
29 | package jexer; | |
30 | ||
31 | import java.awt.image.BufferedImage; | |
32 | import java.io.File; | |
33 | import java.io.IOException; | |
9696a8f6 | 34 | import java.util.ResourceBundle; |
a69ed767 KL |
35 | import javax.imageio.ImageIO; |
36 | ||
37 | import jexer.event.TKeypressEvent; | |
38 | import jexer.event.TMouseEvent; | |
39 | import jexer.event.TResizeEvent; | |
40 | import static jexer.TKeypress.*; | |
41 | ||
42 | /** | |
43 | * TImageWindow shows an image with scrollbars. | |
44 | */ | |
45 | public 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(), | |
85 | parent.getScreen().getHeight() - 2); | |
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 | ||
107 | imageField = new TImage(this, 0, 0, getWidth() - 2, getHeight() - 2, | |
108 | image, 0, 0, null); | |
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 | ||
127 | hScroller = new THScroller(this, 17, getHeight() - 2, getWidth() - 20); | |
128 | vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2); | |
129 | setTopValue(0); | |
130 | setBottomValue(imageField.getRows() - imageField.getHeight()); | |
131 | setLeftValue(0); | |
132 | setRightValue(imageField.getColumns() - imageField.getWidth()); | |
9696a8f6 KL |
133 | |
134 | statusBar = newStatusBar(i18n.getString("statusBar")); | |
a69ed767 KL |
135 | } |
136 | ||
137 | // ------------------------------------------------------------------------ | |
138 | // Event handlers --------------------------------------------------------- | |
139 | // ------------------------------------------------------------------------ | |
140 | ||
141 | /** | |
142 | * Handle mouse press events. | |
143 | * | |
144 | * @param mouse mouse button press event | |
145 | */ | |
146 | @Override | |
147 | public void onMouseDown(final TMouseEvent mouse) { | |
148 | // Use TWidget's code to pass the event to the children. | |
149 | super.onMouseDown(mouse); | |
150 | ||
151 | if (mouse.isMouseWheelUp()) { | |
152 | imageField.setTop(imageField.getTop() - wheelScrollSize); | |
153 | } else if (mouse.isMouseWheelDown()) { | |
154 | imageField.setTop(imageField.getTop() + wheelScrollSize); | |
155 | } | |
156 | setVerticalValue(imageField.getTop()); | |
157 | } | |
158 | ||
159 | /** | |
160 | * Handle mouse release events. | |
161 | * | |
162 | * @param mouse mouse button release event | |
163 | */ | |
164 | @Override | |
165 | public void onMouseUp(final TMouseEvent mouse) { | |
166 | // Use TWidget's code to pass the event to the children. | |
167 | super.onMouseUp(mouse); | |
168 | ||
169 | if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) { | |
170 | // Clicked/dragged on vertical scrollbar | |
171 | imageField.setTop(getVerticalValue()); | |
172 | } | |
173 | if (mouse.isMouse1() && mouseOnHorizontalScroller(mouse)) { | |
174 | // Clicked/dragged on horizontal scrollbar | |
175 | imageField.setLeft(getHorizontalValue()); | |
176 | } | |
177 | } | |
178 | ||
179 | /** | |
180 | * Method that subclasses can override to handle mouse movements. | |
181 | * | |
182 | * @param mouse mouse motion event | |
183 | */ | |
184 | @Override | |
185 | public void onMouseMotion(final TMouseEvent mouse) { | |
186 | // Use TWidget's code to pass the event to the children. | |
187 | super.onMouseMotion(mouse); | |
188 | ||
189 | if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) { | |
190 | // Clicked/dragged on vertical scrollbar | |
191 | imageField.setTop(getVerticalValue()); | |
192 | } | |
193 | if (mouse.isMouse1() && mouseOnHorizontalScroller(mouse)) { | |
194 | // Clicked/dragged on horizontal scrollbar | |
195 | imageField.setLeft(getHorizontalValue()); | |
196 | } | |
197 | } | |
198 | ||
199 | /** | |
200 | * Handle window/screen resize events. | |
201 | * | |
202 | * @param event resize event | |
203 | */ | |
204 | @Override | |
205 | public void onResize(final TResizeEvent event) { | |
206 | if (event.getType() == TResizeEvent.Type.WIDGET) { | |
207 | // Resize the image field | |
208 | TResizeEvent imageSize = new TResizeEvent(TResizeEvent.Type.WIDGET, | |
209 | event.getWidth() - 2, event.getHeight() - 2); | |
210 | imageField.onResize(imageSize); | |
211 | ||
212 | // Have TScrollableWindow handle the scrollbars | |
213 | super.onResize(event); | |
214 | return; | |
215 | } | |
216 | ||
217 | // Pass to children instead | |
218 | for (TWidget widget: getChildren()) { | |
219 | widget.onResize(event); | |
220 | } | |
221 | } | |
222 | ||
223 | /** | |
224 | * Handle keystrokes. | |
225 | * | |
226 | * @param keypress keystroke event | |
227 | */ | |
228 | @Override | |
229 | public void onKeypress(final TKeypressEvent keypress) { | |
230 | if (keypress.equals(kbUp)) { | |
231 | verticalDecrement(); | |
232 | imageField.setTop(getVerticalValue()); | |
233 | return; | |
234 | } | |
235 | if (keypress.equals(kbDown)) { | |
236 | verticalIncrement(); | |
237 | imageField.setTop(getVerticalValue()); | |
238 | return; | |
239 | } | |
240 | if (keypress.equals(kbPgUp)) { | |
241 | bigVerticalDecrement(); | |
242 | imageField.setTop(getVerticalValue()); | |
243 | return; | |
244 | } | |
245 | if (keypress.equals(kbPgDn)) { | |
246 | bigVerticalIncrement(); | |
247 | imageField.setTop(getVerticalValue()); | |
248 | return; | |
249 | } | |
250 | if (keypress.equals(kbRight)) { | |
251 | horizontalIncrement(); | |
252 | imageField.setLeft(getHorizontalValue()); | |
253 | return; | |
254 | } | |
255 | if (keypress.equals(kbLeft)) { | |
256 | horizontalDecrement(); | |
257 | imageField.setLeft(getHorizontalValue()); | |
258 | return; | |
259 | } | |
260 | ||
261 | // We did not take it, let the TImage instance see it. | |
262 | super.onKeypress(keypress); | |
263 | ||
264 | setVerticalValue(imageField.getTop()); | |
265 | setBottomValue(imageField.getRows() - imageField.getHeight()); | |
266 | setHorizontalValue(imageField.getLeft()); | |
267 | setRightValue(imageField.getColumns() - imageField.getWidth()); | |
268 | } | |
269 | ||
270 | // ------------------------------------------------------------------------ | |
271 | // TWindow ---------------------------------------------------------------- | |
272 | // ------------------------------------------------------------------------ | |
273 | ||
274 | /** | |
275 | * Draw the window. | |
276 | */ | |
277 | @Override | |
278 | public void draw() { | |
279 | // Draw as normal. | |
280 | super.draw(); | |
281 | ||
282 | // We have to get the scrollbar values after we have let the image | |
283 | // try to draw. | |
284 | setBottomValue(imageField.getRows() - imageField.getHeight()); | |
285 | setRightValue(imageField.getColumns() - imageField.getWidth()); | |
286 | } | |
287 | ||
288 | } |