Skip to content

Instantly share code, notes, and snippets.

@NetzwergX
Created November 17, 2014 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NetzwergX/c3214b935314a8f38485 to your computer and use it in GitHub Desktop.
Save NetzwergX/c3214b935314a8f38485 to your computer and use it in GitHub Desktop.
Portable SWT ImageButton
/*
* Copyright (c) 2014 Sebastian Teumert (<http://teumert.net>)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.teumert.swt;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
/**
* <p>A simple push button with text below or above the image.</p>
*
* <p>Valid values for text alignment are SWT.LEFT, SWT.CENTER or SWT.RIGHT,
* valid values for text position are SWT.TOP or SWT.BOTTOM.</p>
*
* @author Sebastian Teumert
*
*/
public class ImageButton extends Button {
private String text = "";
private Image image;
private Point textSize, textPosition, imagePosition;
private int style;
private int padding = 8;
/**
* @param arg0
*/
public ImageButton (Composite arg0) {
this(arg0, SWT.BOTTOM | SWT.CENTER);
}
/**
* @param arg0
*/
public ImageButton (Composite arg0, int style) {
super(arg0, SWT.NONE);
this.style = style;
addPaintListener(new PaintListener() {
/* (non-Javadoc)
* @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
*/
public void paintControl (PaintEvent arg0) {
GC gc = arg0.gc;
//gc.setFont(getFont());
// TODO: Add clipping if text or image is wider then actual width
int textX = 8;
if ((ImageButton.this.style & SWT.CENTER) == SWT.CENTER) textX = getSize().x / 2 - textSize.x / 2;
else if ((ImageButton.this.style & SWT.LEFT) == SWT.LEFT) textX = padding;
else if ((ImageButton.this.style & SWT.RIGHT) == SWT.RIGHT) textX = getSize().x - textSize.x - padding;
if ((ImageButton.this.style & SWT.BOTTOM) == SWT.BOTTOM) {
imagePosition = new Point((getSize().x - image.getImageData().width)/2, padding);
textPosition = new Point(textX, getSize().y - textSize.y - padding);
} else if ((ImageButton.this.style & SWT.TOP) == SWT.TOP) {
imagePosition = new Point((getSize().x - image.getImageData().width)/2, padding + textSize.y + padding);
textPosition = new Point(textX, padding);
}
gc.drawImage(image, imagePosition.x, imagePosition.y);
gc.drawText(text, textPosition.x, textPosition.y, true);
}
});
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Button#setText(java.lang.String)
*/
@Override
public void setText (String arg0) {
this.text = arg0;
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Button#getText()
*/
@Override
public String getText () {
return text;
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Button#setImage(org.eclipse.swt.graphics.Image)
*/
@Override
public void setImage (Image arg0) {
this.image = arg0;
computeSize(0, 0, true);
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Button#getImage()
*/
@Override
public Image getImage () {
return image;
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Widget#checkSubclass()
*/
@Override
protected void checkSubclass () {}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#computeSize(int, int)
*/
@Override
public Point computeSize (int arg0, int arg1) {
return new Point(Math.max(textSize.x, getImage().getImageData().width) + (2 * padding), getImage().getImageData().height + textSize.y + (3 * padding));
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Button#computeSize(int, int, boolean)
*/
@Override
public Point computeSize (int arg0, int arg1, boolean arg2) {
if (arg2 || textSize == null) {
GC gc = new GC(this);
//gc.setFont(getFont());
textSize = gc.textExtent(getText());
gc.dispose();
}
return computeSize(arg0, arg1);
}
/**
* @param padding of the button.
*
* <p>The padding is the distance of the
* image and text to the border of the button. The padding is also used
* for the vertical margin between the image and text.</p>
*/
public void setPadding (int padding) {
this.padding = padding;
}
/**
* @return Returns the padding of the button. The padding is the distance of
* the image and text to the border of the button. The padding is also used
* for the vertical margin between the image and text.
*/
public int getPadding () {
return padding;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment