|
1 | 1 | package us.deathmarine.luyten;
|
2 | 2 |
|
| 3 | +import java.awt.Component; |
3 | 4 | import java.awt.Cursor;
|
4 | 5 | import java.awt.Font;
|
5 | 6 | import java.awt.Panel;
|
6 |
| -import java.awt.PopupMenu; |
7 | 7 | import java.awt.Rectangle;
|
8 | 8 | import java.awt.event.ActionEvent;
|
9 | 9 | import java.awt.event.ActionListener;
|
|
15 | 15 | import java.awt.event.MouseWheelEvent;
|
16 | 16 | import java.awt.event.MouseWheelListener;
|
17 | 17 | import java.io.StringWriter;
|
18 |
| -import java.lang.reflect.Field; |
19 | 18 | import java.util.Arrays;
|
20 | 19 | import java.util.HashSet;
|
21 | 20 | import java.util.Map;
|
22 | 21 | import java.util.Set;
|
23 | 22 | import java.util.TreeMap;
|
24 | 23 | import java.util.concurrent.ConcurrentHashMap;
|
25 | 24 |
|
26 |
| -import javax.swing.JDialog; |
27 | 25 | import javax.swing.JLabel;
|
28 | 26 | import javax.swing.JMenuItem;
|
29 | 27 | import javax.swing.JPopupMenu;
|
30 | 28 | import javax.swing.JScrollBar;
|
31 |
| -import javax.swing.JScrollPane; |
| 29 | +import javax.swing.JViewport; |
32 | 30 | import javax.swing.ScrollPaneConstants;
|
| 31 | +import javax.swing.Scrollable; |
| 32 | +import javax.swing.SwingConstants; |
33 | 33 | import javax.swing.SwingUtilities;
|
34 | 34 | import javax.swing.event.HyperlinkEvent;
|
35 | 35 | import org.fife.ui.rsyntaxtextarea.LinkGenerator;
|
@@ -154,6 +154,7 @@ else if (name.toLowerCase().endsWith(".py"))
|
154 | 154 | public void actionPerformed(ActionEvent e) {
|
155 | 155 | JFontChooser fontChooser = new JFontChooser();
|
156 | 156 | fontChooser.setSelectedFont(textArea.getFont());
|
| 157 | + fontChooser.setSelectedFontSize(textArea.getFont().getSize()); |
157 | 158 | int result = fontChooser.showDialog(mainWindow);
|
158 | 159 | if (result == JFontChooser.OK_OPTION)
|
159 | 160 | textArea.setFont(fontChooser.getSelectedFont());
|
@@ -211,19 +212,195 @@ public int getSourceOffset() {
|
211 | 212 | }
|
212 | 213 | });
|
213 | 214 |
|
214 |
| - //Add Ctrl+Wheel Zoom for Text Size |
| 215 | + /* |
| 216 | + * Add Ctrl+Wheel Zoom for Text Size |
| 217 | + * Removes all standard listeners and writes new listeners for wheelscroll movement. |
| 218 | + */ |
| 219 | + for(MouseWheelListener listeners :scrollPane.getMouseWheelListeners()){ |
| 220 | + scrollPane.removeMouseWheelListener(listeners); |
| 221 | + }; |
215 | 222 | scrollPane.addMouseWheelListener(new MouseWheelListener(){
|
216 | 223 | @Override
|
217 | 224 | public void mouseWheelMoved(MouseWheelEvent e) {
|
| 225 | + |
218 | 226 | if ((e.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) != 0){
|
219 | 227 | Font font = textArea.getFont();
|
220 | 228 | int size = font.getSize();
|
221 |
| - if(e.getWheelRotation() > 0){ //Down |
222 |
| - textArea.setFont(new Font(font.getName(), font.getStyle(), ++size)); |
223 |
| - }else{ |
224 |
| - textArea.setFont(new Font(font.getName(), font.getStyle(), --size >= 2 ? --size : 2)); |
| 229 | + if(e.getWheelRotation() > 0){ |
| 230 | + textArea.setFont(new Font(font.getName(), font.getStyle(), --size >= 8 ? --size : 8)); |
| 231 | + }else{ |
| 232 | + textArea.setFont(new Font(font.getName(), font.getStyle(), ++size)); |
225 | 233 | }
|
226 |
| - } |
| 234 | + }else{ |
| 235 | + if (scrollPane.isWheelScrollingEnabled() && |
| 236 | + e.getWheelRotation() != 0) { |
| 237 | + JScrollBar toScroll = scrollPane.getVerticalScrollBar(); |
| 238 | + int direction = e.getWheelRotation() < 0 ? -1 : 1; |
| 239 | + int orientation = SwingConstants.VERTICAL; |
| 240 | + if (toScroll == null || !toScroll.isVisible()) { |
| 241 | + toScroll = scrollPane.getHorizontalScrollBar(); |
| 242 | + if (toScroll == null || !toScroll.isVisible()) { |
| 243 | + return; |
| 244 | + } |
| 245 | + orientation = SwingConstants.HORIZONTAL; |
| 246 | + } |
| 247 | + e.consume(); |
| 248 | + |
| 249 | + if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) { |
| 250 | + JViewport vp = scrollPane.getViewport(); |
| 251 | + if (vp == null) { |
| 252 | + return; |
| 253 | + } |
| 254 | + Component comp = vp.getView(); |
| 255 | + int units = Math.abs(e.getUnitsToScroll()); |
| 256 | + boolean limitScroll = Math.abs(e.getWheelRotation()) == 1; |
| 257 | + Object fastWheelScroll = toScroll.getClientProperty( |
| 258 | + "JScrollBar.fastWheelScrolling"); |
| 259 | + if (Boolean.TRUE == fastWheelScroll && |
| 260 | + comp instanceof Scrollable) { |
| 261 | + Scrollable scrollComp = (Scrollable) comp; |
| 262 | + Rectangle viewRect = vp.getViewRect(); |
| 263 | + int startingX = viewRect.x; |
| 264 | + boolean leftToRight = |
| 265 | + comp.getComponentOrientation().isLeftToRight(); |
| 266 | + int scrollMin = toScroll.getMinimum(); |
| 267 | + int scrollMax = toScroll.getMaximum() - |
| 268 | + toScroll.getModel().getExtent(); |
| 269 | + |
| 270 | + if (limitScroll) { |
| 271 | + int blockIncr = |
| 272 | + scrollComp.getScrollableBlockIncrement(viewRect, |
| 273 | + orientation, |
| 274 | + direction); |
| 275 | + if (direction < 0) { |
| 276 | + scrollMin = Math.max(scrollMin, |
| 277 | + toScroll.getValue() - blockIncr); |
| 278 | + } |
| 279 | + else { |
| 280 | + scrollMax = Math.min(scrollMax, |
| 281 | + toScroll.getValue() + blockIncr); |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + for (int i = 0; i < units; i++) { |
| 286 | + int unitIncr = |
| 287 | + scrollComp.getScrollableUnitIncrement(viewRect, |
| 288 | + orientation, direction); |
| 289 | + if (orientation == SwingConstants.VERTICAL) { |
| 290 | + if (direction < 0) { |
| 291 | + viewRect.y -= unitIncr; |
| 292 | + if (viewRect.y <= scrollMin) { |
| 293 | + viewRect.y = scrollMin; |
| 294 | + break; |
| 295 | + } |
| 296 | + } |
| 297 | + else { // (direction > 0 |
| 298 | + viewRect.y += unitIncr; |
| 299 | + if (viewRect.y >= scrollMax) { |
| 300 | + viewRect.y = scrollMax; |
| 301 | + break; |
| 302 | + } |
| 303 | + } |
| 304 | + } |
| 305 | + else { |
| 306 | + if ((leftToRight && direction < 0) || |
| 307 | + (!leftToRight && direction > 0)) { |
| 308 | + viewRect.x -= unitIncr; |
| 309 | + if (leftToRight) { |
| 310 | + if (viewRect.x < scrollMin) { |
| 311 | + viewRect.x = scrollMin; |
| 312 | + break; |
| 313 | + } |
| 314 | + } |
| 315 | + } |
| 316 | + else if ((leftToRight && direction > 0) || |
| 317 | + (!leftToRight && direction < 0)) { |
| 318 | + viewRect.x += unitIncr; |
| 319 | + if (leftToRight) { |
| 320 | + if (viewRect.x > scrollMax) { |
| 321 | + viewRect.x = scrollMax; |
| 322 | + break; |
| 323 | + } |
| 324 | + } |
| 325 | + } |
| 326 | + else { |
| 327 | + assert false : "Non-sensical ComponentOrientation / scroll direction"; |
| 328 | + } |
| 329 | + } |
| 330 | + } |
| 331 | + if (orientation == SwingConstants.VERTICAL) { |
| 332 | + toScroll.setValue(viewRect.y); |
| 333 | + } |
| 334 | + else { |
| 335 | + if (leftToRight) { |
| 336 | + toScroll.setValue(viewRect.x); |
| 337 | + } |
| 338 | + else { |
| 339 | + int newPos = toScroll.getValue() - |
| 340 | + (viewRect.x - startingX); |
| 341 | + if (newPos < scrollMin) { |
| 342 | + newPos = scrollMin; |
| 343 | + } |
| 344 | + else if (newPos > scrollMax) { |
| 345 | + newPos = scrollMax; |
| 346 | + } |
| 347 | + toScroll.setValue(newPos); |
| 348 | + } |
| 349 | + } |
| 350 | + } else { |
| 351 | + int delta; |
| 352 | + int limit = -1; |
| 353 | + |
| 354 | + if (limitScroll) { |
| 355 | + if (direction < 0) { |
| 356 | + limit = toScroll.getValue() - toScroll.getBlockIncrement(direction); |
| 357 | + } else { |
| 358 | + limit = toScroll.getValue() + toScroll.getBlockIncrement(direction); |
| 359 | + } |
| 360 | + } |
| 361 | + |
| 362 | + for (int i = 0; i < units; i++) { |
| 363 | + if (direction > 0) { |
| 364 | + delta = toScroll.getUnitIncrement(direction); |
| 365 | + } else { |
| 366 | + delta = -toScroll.getUnitIncrement(direction); |
| 367 | + } |
| 368 | + int oldValue = toScroll.getValue(); |
| 369 | + int newValue = oldValue + delta; |
| 370 | + if (delta > 0 && newValue < oldValue) { |
| 371 | + newValue = toScroll.getMaximum(); |
| 372 | + } else if (delta < 0 && newValue > oldValue) { |
| 373 | + newValue = toScroll.getMinimum(); |
| 374 | + } |
| 375 | + if (oldValue == newValue) { |
| 376 | + break; |
| 377 | + } |
| 378 | + if (limitScroll && i > 0) { |
| 379 | + assert limit != -1; |
| 380 | + if ((direction < 0 && newValue < limit) |
| 381 | + || (direction > 0 && newValue > limit)) { |
| 382 | + break; |
| 383 | + } |
| 384 | + } |
| 385 | + toScroll.setValue(newValue); |
| 386 | + } |
| 387 | + |
| 388 | + } |
| 389 | + } else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) { |
| 390 | + int oldValue = toScroll.getValue(); |
| 391 | + int blockIncrement = toScroll.getBlockIncrement(direction); |
| 392 | + int delta = blockIncrement * ((direction > 0) ? +1 : -1); |
| 393 | + int newValue = oldValue + delta; |
| 394 | + if (delta > 0 && newValue < oldValue) { |
| 395 | + newValue = toScroll.getMaximum(); |
| 396 | + } |
| 397 | + else if (delta < 0 && newValue > oldValue) { |
| 398 | + newValue = toScroll.getMinimum(); |
| 399 | + } |
| 400 | + toScroll.setValue(newValue); |
| 401 | + } |
| 402 | + } |
| 403 | + } |
227 | 404 | e.consume();
|
228 | 405 | }
|
229 | 406 | });
|
|
0 commit comments