Skip to content

Commit

Permalink
introduce static helper method GC#drawOn
Browse files Browse the repository at this point in the history
which takes care of creation and disposal of the GC object

Fixes: eclipse-platform#955
  • Loading branch information
tobias-melcher committed Feb 23, 2024
1 parent fc65397 commit b27a84d
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ public synchronized void start() {
final Runnable [] timer = new Runnable [1];
timer [0] = () -> {
if (!active) return;
GC gc = new GC(AnimatedProgress.this);
paintStripes(gc);
gc.dispose();
GC.drawOn(AnimatedProgress.this, gc -> paintStripes(gc));
display.timerExec (SLEEP, timer [0]);
};
display.timerExec (SLEEP, timer [0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,16 @@ private Point getTotalSize(Image image, String text) {
size.y += r.height;
}

GC gc = new GC(this);
if (text != null && text.length() > 0) {
Point e = gc.textExtent(text, DRAW_FLAGS);
size.x += e.x;
size.y = Math.max(size.y, e.y);
if (image != null) size.x += GAP;
} else {
size.y = Math.max(size.y, gc.getFontMetrics().getHeight());
}
gc.dispose();
GC.drawOn(this, gc -> {
if (text != null && text.length() > 0) {
Point e = gc.textExtent(text, DRAW_FLAGS);
size.x += e.x;
size.y = Math.max(size.y, e.y);
if (image != null) size.x += GAP;
} else {
size.y = Math.max(size.y, gc.getFontMetrics().getHeight());
}
});

return size;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3835,94 +3835,95 @@ boolean updateItems() {
}

boolean updateItems (int showIndex) {
GC gc = new GC(this);
if (!single && !mru && showIndex != -1) {
// make sure selected item will be showing
int firstIndex = showIndex;
if (priority[0] < showIndex) {
int maxWidth = getRightItemEdge(gc) - getLeftItemEdge(gc, CTabFolderRenderer.PART_BORDER);
int width = 0;
int[] widths = new int[items.length];
for (int i = priority[0]; i <= showIndex; i++) {
int state = CTabFolderRenderer.MINIMUM_SIZE;
if (i == selectedIndex) state |= SWT.SELECTED;
widths[i] = renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
width += widths[i];
if (width > maxWidth) break;
}
if (width > maxWidth) {
width = 0;
for (int i = showIndex; i >= 0; i--) {
int state = CTabFolderRenderer.MINIMUM_SIZE;
if (i == selectedIndex) state |= SWT.SELECTED;
if (widths[i] == 0) widths[i] = renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
width += widths[i];
if (width > maxWidth) break;
firstIndex = i;
}
} else {
firstIndex = priority[0];
for (int i = showIndex + 1; i < items.length; i++) {
final boolean changed[] = new boolean[] {false};
GC.drawOn(this, gc -> {
if (!single && !mru && showIndex != -1) {
// make sure selected item will be showing
int firstIndex = showIndex;
if (priority[0] < showIndex) {
int maxWidth = getRightItemEdge(gc) - getLeftItemEdge(gc, CTabFolderRenderer.PART_BORDER);
int width = 0;
int[] widths = new int[items.length];
for (int i = priority[0]; i <= showIndex; i++) {
int state = CTabFolderRenderer.MINIMUM_SIZE;
if (i == selectedIndex) state |= SWT.SELECTED;
widths[i] = renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
width += widths[i];
if (width >= maxWidth) break;
if (width > maxWidth) break;
}
if (width < maxWidth) {
for (int i = priority[0] - 1; i >= 0; i--) {
if (width > maxWidth) {
width = 0;
for (int i = showIndex; i >= 0; i--) {
int state = CTabFolderRenderer.MINIMUM_SIZE;
if (i == selectedIndex) state |= SWT.SELECTED;
if (widths[i] == 0) widths[i] = renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
width += widths[i];
if (width > maxWidth) break;
firstIndex = i;
}
} else {
firstIndex = priority[0];
for (int i = showIndex + 1; i < items.length; i++) {
int state = CTabFolderRenderer.MINIMUM_SIZE;
if (i == selectedIndex) state |= SWT.SELECTED;
widths[i] = renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
width += widths[i];
if (width >= maxWidth) break;
}
if (width < maxWidth) {
for (int i = priority[0] - 1; i >= 0; i--) {
int state = CTabFolderRenderer.MINIMUM_SIZE;
if (i == selectedIndex) state |= SWT.SELECTED;
if (widths[i] == 0) widths[i] = renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
width += widths[i];
if (width > maxWidth) break;
firstIndex = i;
}
}
}
}

}
if (firstIndex != priority[0]) {
int index = 0;
// enumerate tabs from first visible to the last existing one (sorted ascending)
for (int i = firstIndex; i < items.length; i++) {
priority[index++] = i;
}
// enumerate hidden tabs on the left hand from first visible one
// in the inverse order (sorted descending) so that the originally
// first opened tab is always at the end of the list
for (int i = firstIndex - 1; i >= 0; i--) {
priority[index++] = i;
if (firstIndex != priority[0]) {
int index = 0;
// enumerate tabs from first visible to the last existing one (sorted ascending)
for (int i = firstIndex; i < items.length; i++) {
priority[index++] = i;
}
// enumerate hidden tabs on the left hand from first visible one
// in the inverse order (sorted descending) so that the originally
// first opened tab is always at the end of the list
for (int i = firstIndex - 1; i >= 0; i--) {
priority[index++] = i;
}
}
}
}

boolean oldShowChevron = showChevron;
boolean changed = setItemSize(gc);
updateButtons();
boolean chevronChanged = showChevron != oldShowChevron;
if (chevronChanged) {
if (updateTabHeight(false)) {
// Tab height has changed. Item sizes have to be set again.
changed |= setItemSize(gc);
boolean oldShowChevron = showChevron;
changed[0] = setItemSize(gc);
updateButtons();
boolean chevronChanged = showChevron != oldShowChevron;
if (chevronChanged) {
if (updateTabHeight(false)) {
// Tab height has changed. Item sizes have to be set again.
changed[0] |= setItemSize(gc);
}
}
}
changed |= setItemLocation(gc);
setButtonBounds();
changed |= chevronChanged;
if (changed && getToolTipText() != null) {
Point pt = getDisplay().getCursorLocation();
pt = toControl(pt);
_setToolTipText(pt.x, pt.y);
}
gc.dispose();
return changed;
changed[0] |= setItemLocation(gc);
setButtonBounds();
changed[0] |= chevronChanged;
if (changed[0] && getToolTipText() != null) {
Point pt = getDisplay().getCursorLocation();
pt = toControl(pt);
_setToolTipText(pt.x, pt.y);
}
});
return changed[0];
}
boolean updateTabHeight(boolean force){
int oldHeight = tabHeight;
GC gc = new GC(this);
tabHeight = renderer.computeSize(CTabFolderRenderer.PART_HEADER, SWT.NONE, gc, SWT.DEFAULT, SWT.DEFAULT).y;
gc.dispose();
GC.drawOn(this, gc ->{
tabHeight = renderer.computeSize(CTabFolderRenderer.PART_HEADER, SWT.NONE, gc, SWT.DEFAULT, SWT.DEFAULT).y;
});
if (fixedTabHeight == SWT.DEFAULT && controls != null && controls.length > 0) {
for (int i = 0; i < controls.length; i++) {
if ((controlAlignments[i] & SWT.WRAP) == 0 && !controls[i].isDisposed() && controls[i].getVisible()) {
Expand Down Expand Up @@ -4008,9 +4009,7 @@ void updateBkImages(boolean colorChanged) {
bkImageBounds[i] = bounds;
if (controlBkImages[i] != null) controlBkImages[i].dispose();
controlBkImages[i] = new Image(control.getDisplay(), bounds);
GC gc = new GC(controlBkImages[i]);
renderer.draw(CTabFolderRenderer.PART_BACKGROUND, 0, bounds, gc);
gc.dispose();
GC.drawOn(controlBkImages[i], gc-> renderer.draw(CTabFolderRenderer.PART_BACKGROUND, 0, bounds, gc));
control.setBackground(null);
control.setBackgroundImage(controlBkImages[i]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,59 +29,59 @@ protected Point computeSize(Composite composite, int wHint, int hHint, boolean f
CTabItem[] items = folder.items;
CTabFolderRenderer renderer = folder.renderer;
// preferred width of tab area to show all tabs
int tabW = 0;
int selectedIndex = folder.selectedIndex;
if (selectedIndex == -1) selectedIndex = 0;
GC gc = new GC(folder);
for (int i = 0; i < items.length; i++) {
if (folder.single) {
tabW = Math.max(tabW, renderer.computeSize(i, SWT.SELECTED, gc, SWT.DEFAULT, SWT.DEFAULT).x);
} else {
int state = 0;
if (i == selectedIndex) state |= SWT.SELECTED;
tabW += renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
final int[] tabW = new int[] {0};
final int[] selectedIndex = new int[] {folder.selectedIndex};
if (selectedIndex[0] == -1) selectedIndex[0] = 0;
final int[] wrapHeight = new int[] {0};
GC.drawOn(folder, gc -> {
for (int i = 0; i < items.length; i++) {
if (folder.single) {
tabW[0] = Math.max(tabW[0], renderer.computeSize(i, SWT.SELECTED, gc, SWT.DEFAULT, SWT.DEFAULT).x);
} else {
int state = 0;
if (i == selectedIndex[0]) state |= SWT.SELECTED;
tabW[0] += renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
}
}
}

int width = 0, wrapHeight = 0;
boolean leftControl = false, rightControl = false;
if (wHint == SWT.DEFAULT) {
for (int i = 0; i < folder.controls.length; i++) {
Control control = folder.controls[i];
if (!control.isDisposed() && control.getVisible()) {
if ((folder.controlAlignments[i] & SWT.LEAD) != 0) {
leftControl = true;
} else {
rightControl = true;
int width = 0;
boolean leftControl = false, rightControl = false;
if (wHint == SWT.DEFAULT) {
for (int i = 0; i < folder.controls.length; i++) {
Control control = folder.controls[i];
if (!control.isDisposed() && control.getVisible()) {
if ((folder.controlAlignments[i] & SWT.LEAD) != 0) {
leftControl = true;
} else {
rightControl = true;
}
width += control.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
}
width += control.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
}
}
} else {
Point size = new Point (wHint, hHint);
boolean[][] positions = new boolean[1][];
Rectangle[] rects = folder.computeControlBounds(size, positions);
int minY = Integer.MAX_VALUE, maxY = 0;
for (int i = 0; i < rects.length; i++) {
if (positions[0][i]) {
minY = Math.min(minY, rects[i].y);
maxY = Math.max(maxY, rects[i].y + rects[i].height);
wrapHeight = maxY - minY;
} else {
if ((folder.controlAlignments[i] & SWT.LEAD) != 0) {
leftControl = true;
} else {
Point size = new Point (wHint, hHint);
boolean[][] positions = new boolean[1][];
Rectangle[] rects = folder.computeControlBounds(size, positions);
int minY = Integer.MAX_VALUE, maxY = 0;
for (int i = 0; i < rects.length; i++) {
if (positions[0][i]) {
minY = Math.min(minY, rects[i].y);
maxY = Math.max(maxY, rects[i].y + rects[i].height);
wrapHeight[0] = maxY - minY;
} else {
rightControl = true;
if ((folder.controlAlignments[i] & SWT.LEAD) != 0) {
leftControl = true;
} else {
rightControl = true;
}
width += rects[i].width;
}
width += rects[i].width;
}
}
}
if (leftControl) width += CTabFolder.SPACING * 2;
if (rightControl) width += CTabFolder.SPACING * 2;
tabW += width;

gc.dispose();
if (leftControl) width += CTabFolder.SPACING * 2;
if (rightControl) width += CTabFolder.SPACING * 2;
tabW[0] += width;
});

int controlW = 0;
int controlH = 0;
Expand All @@ -95,8 +95,8 @@ protected Point computeSize(Composite composite, int wHint, int hHint, boolean f
}
}

int minWidth = Math.max(tabW, controlW + folder.marginWidth);
int minHeight = (folder.minimized) ? 0 : controlH + wrapHeight;
int minWidth = Math.max(tabW[0], controlW + folder.marginWidth);
int minHeight = (folder.minimized) ? 0 : controlH + wrapHeight[0];
if (minWidth == 0) minWidth = CTabFolder.DEFAULT_WIDTH;
if (minHeight == 0) minHeight = CTabFolder.DEFAULT_HEIGHT;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,19 @@ public void dragOver(DropTargetEvent event) {
pt.y >= scrollY && pt.y <= (scrollY + SCROLL_TOLERANCE))) {
if (System.currentTimeMillis() >= scrollBeginTime) {
Rectangle area = text.getClientArea();
GC gc = new GC(text);
FontMetrics fm = gc.getFontMetrics();
gc.dispose();
double charWidth = fm.getAverageCharacterWidth();
int scrollAmount = (int) (10*charWidth);
if (pt.x < area.x + 3*charWidth) {
int leftPixel = text.getHorizontalPixel();
text.setHorizontalPixel(leftPixel - scrollAmount);
}
if (pt.x > area.width - 3*charWidth) {
int leftPixel = text.getHorizontalPixel();
text.setHorizontalPixel(leftPixel + scrollAmount);
}
GC.drawOn(text, gc->{
FontMetrics fm = gc.getFontMetrics();
double charWidth = fm.getAverageCharacterWidth();
int scrollAmount = (int) (10*charWidth);
if (pt.x < area.x + 3*charWidth) {
int leftPixel = text.getHorizontalPixel();
text.setHorizontalPixel(leftPixel - scrollAmount);
}
if (pt.x > area.width - 3*charWidth) {
int leftPixel = text.getHorizontalPixel();
text.setHorizontalPixel(leftPixel + scrollAmount);
}
});
int lineHeight = text.getLineHeight();
if (pt.y < area.y + lineHeight) {
int topPixel = text.getTopPixel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1500,10 +1500,10 @@ void setFont(Font font, int tabs) {
tabWidth = layout.getBounds().width;
layout.dispose();
if (styledText != null) {
GC gc = new GC(styledText);
averageCharWidth = (int) gc.getFontMetrics().getAverageCharacterWidth();
fixedPitch = gc.stringExtent("l").x == gc.stringExtent("W").x; //$NON-NLS-1$ //$NON-NLS-2$
gc.dispose();
GC.drawOn(styledText, gc->{
averageCharWidth = (int) gc.getFontMetrics().getAverageCharacterWidth();
fixedPitch = gc.stringExtent("l").x == gc.stringExtent("W").x; //$NON-NLS-1$ //$NON-NLS-2$
});
}
}
void setLineAlignment(int startLine, int count, int alignment) {
Expand Down
Loading

0 comments on commit b27a84d

Please sign in to comment.