}
int y = onBottom ? size.y - borderBottom - height : borderTop;
drawBackground(gc, shape, x, y, width, height, defaultBackground, image, colors, percents, vertical);
}
void drawBackground(GC gc, int[] shape, int x, int y, int width, int height, Color defaultBackground, Image image, Color[] colors, int[] percents, boolean vertical) {
Region clipping = new Region();
gc.getClipping(clipping);
Region region = new Region();
region.add(shape);
region.intersect(clipping);
gc.setClipping(region);
if (image != null) {
// draw the background image in shape
gc.setBackground(defaultBackground);
gc.fillRectangle(x, y, width, height);
Rectangle imageRect = image.getBounds();
gc.drawImage(image, imageRect.x, imageRect.y, imageRect.width, imageRect.height, x, y, width, height);
} else if (colors != null) {
// draw gradient
if (colors.length == 1) {
Color background = colors[0] != null ? colors[0] : defaultBackground;
gc.setBackground(background);
gc.fillRectangle(x, y, width, height);
} else {
if (vertical) {
if (onBottom) {
int pos = 0;
if (percents[percents.length - 1] < 100) {
pos = percents[percents.length - 1] * height / 100;
gc.setBackground(defaultBackground);
gc.fillRectangle(x, y, width, pos);
}
Color lastColor = colors[colors.length-1];
if (lastColor == null) lastColor = defaultBackground;
for (int i = percents.length-1; i >= 0; i--) {
gc.setForeground(lastColor);
lastColor = colors[i];
if (lastColor == null) lastColor = defaultBackground;
gc.setBackground(lastColor);
int gradientHeight = percents[i] * height / 100;
gc.fillGradientRectangle(x, y+pos, width, gradientHeight, true);
pos += gradientHeight;
}
} else {
Color lastColor = colors[0];
if (lastColor == null) lastColor = defaultBackground;
int pos = 0;
for (int i = 0; i < percents.length; i++) {
gc.setForeground(lastColor);
lastColor = colors[i + 1];
if (lastColor == null) lastColor = defaultBackground;
gc.setBackground(lastColor);
int gradientHeight = percents[i] * height / 100;
gc.fillGradientRectangle(x, y+pos, width, gradientHeight, true);
pos += gradientHeight;
}
if (pos < height) {
gc.setBackground(defaultBackground);
gc.fillRectangle(x, pos, width, height-pos+1);
}
}
} else { //horizontal gradient
y = 0;
height = getSize().y;
Color lastColor = colors[0];
if (lastColor == null) lastColor = defaultBackground;
int pos = 0;
for (int i = 0; i < percents.length; ++i) {
gc.setForeground(lastColor);
lastColor = colors[i + 1];
if (lastColor == null) lastColor = defaultBackground;
gc.setBackground(lastColor);
int gradientWidth = (percents[i] * width / 100) - pos;
gc.fillGradientRectangle(x+pos, y, gradientWidth, height, false);
pos += gradientWidth;
}
if (pos < width) {
gc.setBackground(defaultBackground);
gc.fillRectangle(x+pos, y, width-pos, height);
}
}
}
} else {
// draw a solid background using default background in shape
if ((getStyle() & SWT.NO_BACKGROUND) != 0 || !defaultBackground.equals(getBackground())) {
gc.setBackground(defaultBackground);
gc.fillRectangle(x, y, width, height);
}
}
gc.setClipping(clipping);
clipping.dispose();
region.dispose();
}