Skip to content

Commit

Permalink
Added image/icon handling options for tabs and improved text padding if
Browse files Browse the repository at this point in the history
tab icon is not visible
1. Added showSelectedImage option to CTabFolder
2. Added showSelectedTabImage and showUnselectedTabImage methods to
renderer
  • Loading branch information
shubhamWaghmare-sap authored and sratz committed Dec 14, 2023
1 parent 27feab2 commit dacba96
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public class CTabFolder extends Composite {
int[] gradientPercents;
boolean gradientVertical;
boolean showUnselectedImage = true;
boolean showSelectedImage = true;

// close, min/max and chevron buttons
boolean showClose = false;
Expand Down Expand Up @@ -1366,6 +1367,18 @@ public boolean getUnselectedImageVisible() {
checkWidget();
return showUnselectedImage;
}
/**
* Returns <code>true</code> if an image appears
* in selected tabs.
*
* @return <code>true</code> if an image appears in selected tabs
*
* @since 3.125
*/
public boolean getSelectedImageVisible() {
checkWidget();
return showSelectedImage;
}
/**
* Return the index of the specified tab or -1 if the tab is not
* in the receiver.
Expand Down Expand Up @@ -3693,6 +3706,25 @@ public void setUnselectedImageVisible(boolean visible) {
showUnselectedImage = visible;
updateFolder(REDRAW);
}
/**
* Specify whether the image appears on selected tabs.
*
* @param visible <code>true</code> makes the image appear
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @since 3.125
*/
public void setSelectedImageVisible(boolean visible) {
checkWidget();
if (showSelectedImage == visible) return;
// display image on selected items
showSelectedImage = visible;
updateFolder(REDRAW);
}
/**
* Shows the item. If the item is already showing in the receiver,
* this method simply returns. Otherwise, the items are scrolled until
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public class CTabFolderRenderer {
static final int ITEM_LEFT_MARGIN = 4;
static final int ITEM_RIGHT_MARGIN = 4;
static final int INTERNAL_SPACING = 4;
static final int TABS_WITHOUT_ICONS_PADDING = 14;
static final int FLAGS = SWT.DRAW_TRANSPARENT | SWT.DRAW_MNEMONIC | SWT.DRAW_DELIMITER;
static final String ELLIPSIS = "..."; //$NON-NLS-1$
private static final String CHEVRON_ELLIPSIS = "99+"; //$NON-NLS-1$
Expand Down Expand Up @@ -318,10 +319,11 @@ protected Point computeSize (int part, int state, GC gc, int wHint, int hHint) {
Image image = item.getImage();
if (image != null && !image.isDisposed()) {
Rectangle bounds = image.getBounds();
if ((state & SWT.SELECTED) != 0 || parent.showUnselectedImage) {
if (((state & SWT.SELECTED) != 0 && parent.showSelectedImage)
|| ((state & SWT.SELECTED) == 0 && parent.showUnselectedImage)) {
width += bounds.width;
}
height = bounds.height;
height = bounds.height;
}
String text = null;
if ((state & MINIMUM_SIZE) != 0) {
Expand Down Expand Up @@ -355,9 +357,17 @@ protected Point computeSize (int part, int state, GC gc, int wHint, int hHint) {
gc.setFont(gcFont);
}
}

width += getTextPadding(item, state) * 2;

if (parent.showClose || item.showClose) {
if ((state & SWT.SELECTED) != 0 || parent.showUnselectedClose) {
if (width > 0) width += INTERNAL_SPACING;
if (((state & SWT.SELECTED) != 0 && parent.showSelectedImage)
|| ((state & SWT.SELECTED) == 0 && parent.showUnselectedImage)) {
if (width > 0) width += INTERNAL_SPACING;
} else {
if (width > 0) width -= INTERNAL_SPACING;
}
width += computeSize(PART_CLOSE_BUTTON, SWT.NONE, gc, SWT.DEFAULT, SWT.DEFAULT).x;
}
}
Expand All @@ -370,6 +380,27 @@ protected Point computeSize (int part, int state, GC gc, int wHint, int hHint) {
return new Point(width, height);
}

/**
* Returns padding for the text of a tab when image is not available or is hidden.
*
* @param item CTabItem
* @param state current state
*
*/
private int getTextPadding(CTabItem item, int state) {
CTabFolder parent = item.getParent();
Image image = item.getImage();
String text = item.getText();

if (text != null && parent.getMinimumCharacters() != 0) {
if (image == null || image.isDisposed() || ((state & SWT.SELECTED) != 0 && !parent.showSelectedImage)
|| ((state & SWT.SELECTED) == 0 && !parent.showUnselectedImage))
return TABS_WITHOUT_ICONS_PADDING;
}

return 0;
}

/**
* Given a desired <em>client area</em> for the part
* (as described by the arguments), returns the bounding
Expand Down Expand Up @@ -1385,7 +1416,7 @@ void drawSelected(int itemIndex, GC gc, Rectangle bounds, int state ) {
int xDraw = x - trim.x;
if (parent.single && (parent.showClose || item.showClose)) xDraw += item.closeRect.width;
Image image = item.getImage();
if (image != null && !image.isDisposed()) {
if (image != null && !image.isDisposed() && parent.showSelectedImage) {
Rectangle imageBounds = image.getBounds();
// only draw image if it won't overlap with close button
int maxImageWidth = rightEdge - xDraw - (trim.width + trim.x);
Expand All @@ -1400,6 +1431,7 @@ void drawSelected(int itemIndex, GC gc, Rectangle bounds, int state ) {
}

// draw Text
xDraw += getTextPadding(item, state);
int textWidth = rightEdge - xDraw - (trim.width + trim.x);
if (!parent.single && item.closeRect.width > 0) textWidth -= item.closeRect.width + INTERNAL_SPACING;
if (textWidth > 0) {
Expand Down Expand Up @@ -1613,6 +1645,7 @@ void drawUnselected(int index, GC gc, Rectangle bounds, int state) {
}
}
// draw Text
xDraw += getTextPadding(item, state);
int textWidth = x + width - xDraw - (trim.width + trim.x);
if (parent.showUnselectedClose && (parent.showClose || item.showClose)) {
textWidth -= item.closeRect.width + INTERNAL_SPACING;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,18 @@ public void test_iconWrappedOnNextLine() {
}
}

@Test
public void test_selectedImageVisible() {
createTabFolder(null);

ctabFolder.setSelectedImageVisible(true);
assertTrue(ctabFolder.getSelectedImageVisible());

ctabFolder.setSelectedImageVisible(false);
assertFalse(ctabFolder.getSelectedImageVisible());

}

private void processEvents() {
Display display = shell.getDisplay();

Expand Down

0 comments on commit dacba96

Please sign in to comment.