diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/ToolBar.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/ToolBar.java index febc6e278a..ab92d4c7b2 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/ToolBar.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/ToolBar.java @@ -18,7 +18,6 @@ import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; -import org.eclipse.swt.internal.*; import org.eclipse.swt.widgets.toolbar.*; /** @@ -55,35 +54,6 @@ */ public class ToolBar extends Composite { - /** - * Renderer interface for the {@link ToolBar} widget. All renderers have to - * implement this to work with the ToolBar. - */ - public interface IToolBarRenderer { - - /** - * Renders the handle. - * - * @param gc GC to render with. - * @param bounds Bounds of the rendering. x and y are always 0. - */ - void render(GC gc, Rectangle bounds); - - /** - * Computes the size of the rendered ToolBar. - * - * @return The size as {@link Point} - */ - Point computeSize(Point size); - - /** - * Returns the row count of the rendered widget. - * - * @return The row count. - */ - int rowCount(); - } - /** The {@link ToolItem}s contained in the {@link ToolBar} */ private final java.util.List items = new ArrayList<>(); @@ -91,7 +61,7 @@ public interface IToolBarRenderer { public int itemCount; /** The renderer used to render to {@link ToolBar}. */ - private final IToolBarRenderer renderer; + private final ToolBarRenderer renderer; private final boolean flat; private final boolean wrap; @@ -148,7 +118,7 @@ public ToolBar(Composite parent, int style, boolean internal) { super(parent, checkStyle(style)); this.style |= SWT.DOUBLE_BUFFERED; - renderer = new ToolBarRenderer(this); + renderer = new DefaultToolBarRenderer(this); Listener listener = event -> { if (isDisposed()) { @@ -301,12 +271,12 @@ private void onPaint(Event event) { return; } - Rectangle bounds = getBounds(); - if (bounds.width == 0 && bounds.height == 0) { + final Point size = getSize(); + if (size.x == 0 || size.y == 0) { return; } - Drawing.drawWithGC(this, event.gc, gc -> renderer.render(gc, bounds)); + Drawing.drawWithGC(this, event.gc, gc -> renderer.paint(gc, size.x, size.y)); } /** @@ -351,18 +321,7 @@ private void onPaint(Event event) { @Override public Point computeSize(int wHint, int hHint, boolean changed) { checkWidget(); - Point sizeHint = new Point(wHint, hHint); - Point size = renderer.computeSize(sizeHint); - - return size; - } - - Point computeSizeInPixels(int wHint, int hHint, boolean changed) { - checkWidget(); - Point sizeHint = new Point(wHint, hHint); - Point size = renderer.computeSize(sizeHint); - - return DPIUtil.autoScaleUp(size); + return renderer.computeSize(wHint, hHint); } void createItem(ToolItem item, int index) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/ToolBarRenderer.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/ToolBarRenderer.java new file mode 100644 index 0000000000..d501485949 --- /dev/null +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/ToolBarRenderer.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 2000, 2021 IBM Corporation and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.widgets; + +import org.eclipse.swt.graphics.*; + +/** + * Renderer interface for the {@link ToolBar} widget. All renderers have to + * implement this to work with the ToolBar. + */ +public abstract class ToolBarRenderer extends ControlRenderer { + + /** + * Computes the size of the rendered ToolBar. + * + * @return The size as {@link Point} + */ + public abstract Point computeSize(int widthHint, int heightHint); + + /** + * Returns the row count of the rendered widget. + * + * @return The row count. + */ + public abstract int rowCount(); + + protected ToolBarRenderer(ToolBar control) { + super(control); + } +} diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/toolbar/ToolBarRenderer.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/toolbar/DefaultToolBarRenderer.java similarity index 77% rename from bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/toolbar/ToolBarRenderer.java rename to bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/toolbar/DefaultToolBarRenderer.java index a77d00c78a..e84920ab90 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/toolbar/ToolBarRenderer.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/toolbar/DefaultToolBarRenderer.java @@ -19,25 +19,25 @@ import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.widgets.*; -import org.eclipse.swt.widgets.ToolBar.*; import org.eclipse.swt.widgets.toolbar.ToolBarLayout.*; /** * Default renderer for the ToolBar. */ -public class ToolBarRenderer implements IToolBarRenderer { +public class DefaultToolBarRenderer extends ToolBarRenderer { public static final Color COLOR_SEPARATOR = new Color(Display.getDefault(), 160, 160, 160); private final ToolBar bar; private int rowCount = SWT.DEFAULT; - public ToolBarRenderer(ToolBar toolbar) { + public DefaultToolBarRenderer(ToolBar toolbar) { + super(toolbar); this.bar = toolbar; } @Override - public void render(GC gc, Rectangle bounds) { - Point size = new Point(bounds.width, bounds.height); + public void paint(GC gc, int width, int height) { + Point size = new Point(width, height); ToolBarLayout layout = computeLayout(size); rowCount = layout.rows().size(); @@ -50,10 +50,10 @@ private void render(GC gc, Point size, List rows) { gc.drawLine(0, 0, size.x, 0); } - for (int i = 0; i < rows.size(); i++) { - Row row = rows.get(i); + for (Row row : rows) { for (ItemRecord itemRecord : row.items) { - bar.getItem(itemRecord.index()).render(gc, itemRecord.bounds()); + final ToolItem item = bar.getItem(itemRecord.index()); + item.render(gc, itemRecord.bounds()); } if (row.hasRowSeparator) { drawHorizontalSeparator(gc, row); @@ -72,8 +72,8 @@ private ToolBarLayout computeLayout(Point size) { List itemRecords = new ArrayList<>(); for (int i = 0; i < bar.getItemCount(); i++) { ToolItem item = bar.getItem(i); - Point preferedSize = item.getSize(); - Rectangle initialBounds = new Rectangle(0, 0, preferedSize.x, preferedSize.y); + Point preferredSize = item.getSize(); + Rectangle initialBounds = new Rectangle(0, 0, preferredSize.x, preferredSize.y); ItemRecord itemRecord = new ItemRecord(i, initialBounds, item.isSeparator()); itemRecords.add(itemRecord); } @@ -86,8 +86,8 @@ private ToolBarLayout computeLayout(Point size) { @Override - public Point computeSize(Point size) { - ToolBarLayout layout = computeLayout(size); + public Point computeSize(int widthHint, int heightHint) { + ToolBarLayout layout = computeLayout(new Point(widthHint, heightHint)); Point computedSize = layout.size(); if (bar.isBorder()) { @@ -96,12 +96,12 @@ public Point computeSize(Point size) { } Point finalSize = new Point(computedSize.x, computedSize.y); - if (size.x > 0) { - finalSize.x = size.x; + if (widthHint > 0) { + finalSize.x = widthHint; } - if (size.y > 0) { - finalSize.y = size.y; + if (heightHint > 0) { + finalSize.y = heightHint; } return finalSize; @@ -111,8 +111,7 @@ public Point computeSize(Point size) { @Override public int rowCount() { if (rowCount == SWT.DEFAULT) { - Rectangle bounds = bar.getBounds(); - Point size = new Point(bounds.x, bounds.y); + Point size = getSize(); rowCount = computeLayout(size).rows().size(); } return rowCount; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/toolbar/ToolBarLayout.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/toolbar/ToolBarLayout.java index 781a646d13..f4c5161227 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/toolbar/ToolBarLayout.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/toolbar/ToolBarLayout.java @@ -135,7 +135,7 @@ void setX(int x) { /** * Mirrors the layout vertically. */ - void mirrow() { + void mirrorX() { for (ItemRecord itemRecord : items) { itemRecord.bounds.x = availableSpace.x - itemRecord.bounds.x - itemRecord.bounds.width; } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/toolbar/ToolBarLayoutGenerator.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/toolbar/ToolBarLayoutGenerator.java index 75a706f364..fa03e85254 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/toolbar/ToolBarLayoutGenerator.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/toolbar/ToolBarLayoutGenerator.java @@ -71,7 +71,7 @@ private ToolBarLayout computeSingleHorizontalRow(Point offset) { row.setY(offset.y); if (rtl) { - row.mirrow(); + row.mirrorX(); } int width = offset.x + row.usedSpace.x; @@ -114,7 +114,7 @@ private ToolBarLayout computeMultipleHorizontalRows(Point offset) { } if (rtl) { - row.mirrow(); + row.mirrorX(); } }