|
12 | 12 | import org.eclipse.jface.resource.ColorRegistry; |
13 | 13 | import org.eclipse.jface.resource.JFaceResources; |
14 | 14 | import org.eclipse.jface.text.ITextViewer; |
| 15 | +import org.eclipse.swt.SWT; |
| 16 | +import org.eclipse.swt.custom.ScrolledComposite; |
15 | 17 | import org.eclipse.swt.custom.StyledText; |
16 | 18 | import org.eclipse.swt.dnd.Clipboard; |
17 | 19 | import org.eclipse.swt.dnd.TextTransfer; |
18 | 20 | import org.eclipse.swt.dnd.Transfer; |
| 21 | +import org.eclipse.swt.events.ControlAdapter; |
| 22 | +import org.eclipse.swt.events.ControlEvent; |
19 | 23 | import org.eclipse.swt.graphics.Color; |
| 24 | +import org.eclipse.swt.graphics.Point; |
20 | 25 | import org.eclipse.swt.graphics.RGB; |
| 26 | +import org.eclipse.swt.widgets.Composite; |
21 | 27 | import org.eclipse.swt.widgets.Control; |
22 | 28 | import org.eclipse.swt.widgets.Display; |
| 29 | +import org.eclipse.swt.widgets.ScrollBar; |
| 30 | +import org.eclipse.swt.widgets.Scrollable; |
23 | 31 | import org.eclipse.swt.widgets.Shell; |
| 32 | +import org.eclipse.swt.widgets.Table; |
| 33 | +import org.eclipse.swt.widgets.TableColumn; |
24 | 34 | import org.eclipse.ui.IEditorPart; |
25 | 35 | import org.eclipse.ui.IWorkbench; |
26 | 36 | import org.eclipse.ui.IWorkbenchPage; |
@@ -272,6 +282,88 @@ public static Color getDefaultGhostTextColor(Display display) { |
272 | 282 | return new Color(display, new RGB(DEFAULT_GHOST_TEXT_SCALE, DEFAULT_GHOST_TEXT_SCALE, DEFAULT_GHOST_TEXT_SCALE)); |
273 | 283 | } |
274 | 284 |
|
| 285 | + /** |
| 286 | + * Forwards vertical mouse wheel scrolling from a nested scrollable to its nearest parent scroller when the nested |
| 287 | + * control is already at the scroll boundary. |
| 288 | + */ |
| 289 | + public static void forwardVerticalMouseWheelToParentScrollerAtBoundary(Scrollable scrollable) { |
| 290 | + scrollable.addListener(SWT.MouseWheel, event -> { |
| 291 | + if (event.count == 0 || scrollable.isDisposed() |
| 292 | + || canScrollVertically(scrollable.getVerticalBar(), event.count)) { |
| 293 | + return; |
| 294 | + } |
| 295 | + |
| 296 | + ScrolledComposite parentScroller = findParentScroller(scrollable); |
| 297 | + if (parentScroller != null |
| 298 | + && canScrollVertically(parentScroller.getVerticalBar(), event.count)) { |
| 299 | + event.doit = false; |
| 300 | + scrollParentVertically(parentScroller, event.count); |
| 301 | + } |
| 302 | + }); |
| 303 | + } |
| 304 | + |
| 305 | + private static ScrolledComposite findParentScroller(Scrollable scrollable) { |
| 306 | + Composite parent = scrollable.getParent(); |
| 307 | + while (parent != null) { |
| 308 | + if (parent instanceof ScrolledComposite scrolledComposite) { |
| 309 | + return scrolledComposite; |
| 310 | + } |
| 311 | + parent = parent.getParent(); |
| 312 | + } |
| 313 | + return null; |
| 314 | + } |
| 315 | + |
| 316 | + private static void scrollParentVertically(ScrolledComposite scrolledComposite, int wheelCount) { |
| 317 | + ScrollBar verticalBar = scrolledComposite.getVerticalBar(); |
| 318 | + if (verticalBar == null || verticalBar.isDisposed()) { |
| 319 | + return; |
| 320 | + } |
| 321 | + |
| 322 | + Point origin = scrolledComposite.getOrigin(); |
| 323 | + int minimum = verticalBar.getMinimum(); |
| 324 | + int maximum = Math.max(minimum, |
| 325 | + verticalBar.getMaximum() - verticalBar.getThumb()); |
| 326 | + int delta = -wheelCount * Math.max(1, verticalBar.getIncrement()); |
| 327 | + int nextY = Math.max(minimum, Math.min(maximum, origin.y + delta)); |
| 328 | + scrolledComposite.setOrigin(origin.x, nextY); |
| 329 | + } |
| 330 | + |
| 331 | + private static boolean canScrollVertically(ScrollBar verticalBar, int wheelCount) { |
| 332 | + if (verticalBar == null || verticalBar.isDisposed() |
| 333 | + || !verticalBar.getEnabled()) { |
| 334 | + return false; |
| 335 | + } |
| 336 | + |
| 337 | + int minimum = verticalBar.getMinimum(); |
| 338 | + int maximum = Math.max(minimum, |
| 339 | + verticalBar.getMaximum() - verticalBar.getThumb()); |
| 340 | + int selection = Math.max(minimum, |
| 341 | + Math.min(maximum, verticalBar.getSelection())); |
| 342 | + if (wheelCount > 0) { |
| 343 | + return selection > minimum; |
| 344 | + } |
| 345 | + return selection < maximum; |
| 346 | + } |
| 347 | + |
| 348 | + /** |
| 349 | + * Resizes a table column to fill the table client area not occupied by the fixed-width columns. |
| 350 | + */ |
| 351 | + public static void resizeColumnToFillTable(Table table, TableColumn fillColumn, |
| 352 | + int minWidth, TableColumn... fixedColumns) { |
| 353 | + table.addControlListener(new ControlAdapter() { |
| 354 | + @Override |
| 355 | + public void controlResized(ControlEvent e) { |
| 356 | + int remainingWidth = table.getClientArea().width; |
| 357 | + for (TableColumn fixedColumn : fixedColumns) { |
| 358 | + remainingWidth -= fixedColumn.getWidth(); |
| 359 | + } |
| 360 | + if (remainingWidth > minWidth) { |
| 361 | + fillColumn.setWidth(remainingWidth); |
| 362 | + } |
| 363 | + } |
| 364 | + }); |
| 365 | + } |
| 366 | + |
275 | 367 | /** |
276 | 368 | * Copy the given text to the clipboard. |
277 | 369 | */ |
|
0 commit comments