Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Custom control and GridLayout
Custom control and GridLayout [message #445692] Tue, 09 November 2004 16:25 Go to next message
Peter Sommerfeld is currently offline Peter SommerfeldFriend
Messages: 12
Registered: July 2009
Junior Member
Hi,

I'm investigating the creation of custom widgets and
follow roughly the pattern described in the article
"Creating Your Own Widgets using SWT" by Steve
Northover & Carolyn MacLeod. But instead of using
my own layout manager I use Grid Layout. Surpisingly
the Basic Widget (class TextButton) does not show up
properly within the custom composite (class Statusbar).
If I change the TextButton with a Label (commented out
in the code below) everything is OK. Do I miss any
constraint, a silly bug, or something else ? Platform
Win XP, Eclipse 3.1M3.

tia, Peter

/* Code: */

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/*
* T e x t B u t t o n
*/
class TextButton extends Canvas {

String
text;
int
width = 0,
height = 0
;

public TextButton(Composite parent) {
super(parent, SWT.FLAT);

Display display = getDisplay();

setBackground(getDisplay().getSystemColor(SWT.COLOR_GRAY));

Listener listener = new Listener() {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.MouseEnter:
onMouseEnter(); break;
case SWT.MouseDown:
onMouseDown (); break;
case SWT.MouseExit:
onMouseExit(); break;
case SWT.MouseUp:
onMouseUp(); break;
case SWT.Paint:
onPaint(e.gc); break;
}
}
};
int[] events = new int[] {
SWT.MouseEnter, SWT.MouseDown,
SWT.MouseUp, SWT.MouseExit, SWT.Paint};
for (int i = 0; i < events.length; i++)
addListener(events[i], listener);
}

void onMouseDown (){}
void onMouseEnter(){}
void onMouseExit(){}
void onMouseUp(){}

void onPaint(GC gc){
// center string omitted
gc.drawString(text,1,1);
}

public void setText(String s){

if(s == null)
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
text = s;

GC gc = new GC(this);
Point extent = gc.stringExtent(s);
gc.dispose();
height = extent.y + 2;
width = extent.x + 2;
redraw();
}

public Point computeSize(int w, int h, int flush){
return new Point(width,height);
}
}

/*
* St a t u s b a r
*/

class Statusbar extends Composite {

final Image // an arbitary image
infoImage = new Image(getDisplay(),"info.gif")
;
final Label
statusImage = new Label(this,SWT.NONE ),
statusText = new Label(this,SWT.NONE),
separator1 = new Label(this,SWT.SEPARATOR)
// ,
// buttonLabel = new Label(this,SWT.NONE)
;
final TextButton
textButton = new TextButton(this)
;
final int cols = 4;

public Statusbar(Composite parent) {
super(parent, SWT.FLAT);

Color color = getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
infoImage.setBackground(color);

// layout

GridLayout layout = new GridLayout(cols,false);

layout.horizontalSpacing = 5;
layout.marginWidth = 5;
layout.marginHeight = 2;
setLayout(layout);

GridData data = new GridData(SWT.LEFT,SWT.CENTER,false,false);
statusImage.setLayoutData(data);

data = new GridData(SWT.LEFT,SWT.CENTER,true,false);
statusText.setLayoutData(data);

data = new GridData(5,17);
data.horizontalAlignment= SWT.CENTER;
data.verticalAlignment = SWT.TOP;
separator1.setLayoutData(data);


data = new GridData(SWT.LEFT,SWT.CENTER,false,false);
textButton.setLayoutData(data);

// data = new GridData(SWT.LEFT,SWT.CENTER,false,false);
// buttonLabel.setLayoutData(data);

// items

statusImage.setImage(infoImage);
statusText.setText("Welcome !");
textButton.setText("Button X");
// buttonLabel.setText("Button X");

layout();

}

public Point computeSize(int width, int height, boolean changed) {
return new Point(width,22);
}
}

/*
* M a i n
*/

public class Main {

public static void main(String[] args) {

Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(display.getPrimaryMonitor().getClientArea()) ;

GridLayout layout = new GridLayout();
layout.marginWidth = 0;
layout.marginHeight = 0;
layout.verticalSpacing = 0;
shell.setLayout(layout);

Statusbar statusbar = new Statusbar(shell);
GridData data = new GridData(SWT.FILL,SWT.FILL,true,false);
statusbar.setLayoutData(data);

shell.layout();
shell.open();

while (!display.isDisposed() && display.getShells().length > 0) {
if (!display.readAndDispatch ())
display.sleep ();
}
}
}

/* End code */
Re: Custom control and GridLayout [message #445756 is a reply to message #445692] Wed, 10 November 2004 15:34 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
Statusbar.computeSize is bad. Because Statusbar is using a GridLayout to
manage the size and position of its children, you should not override
computeSize in Statusbar. The default behaviour is to defer to GridLayout
to provide the computed size and this is the correct behaviour. If you were
not using GridLayout then there is still a problem with the implementation
of Statusbar.computeSize - the parameter"width" is actually "wHint" which is
just a hint and can have the value SWT.DEFAULT (-1). You are returning a
width of -1 in this case which is not good.


"Peter Sommerfeld" <p.sommerfeld@utanet.at> wrote in message
news:cmqr11$3ii$1@eclipse.org...
> Hi,
>
> I'm investigating the creation of custom widgets and
> follow roughly the pattern described in the article
> "Creating Your Own Widgets using SWT" by Steve
> Northover & Carolyn MacLeod. But instead of using
> my own layout manager I use Grid Layout. Surpisingly
> the Basic Widget (class TextButton) does not show up
> properly within the custom composite (class Statusbar).
> If I change the TextButton with a Label (commented out
> in the code below) everything is OK. Do I miss any
> constraint, a silly bug, or something else ? Platform
> Win XP, Eclipse 3.1M3.
>
> tia, Peter
>
> /* Code: */
>
> import org.eclipse.swt.*;
> import org.eclipse.swt.graphics.*;
> import org.eclipse.swt.layout.*;
> import org.eclipse.swt.widgets.*;
>
> /*
> * T e x t B u t t o n
> */
> class TextButton extends Canvas {
>
> String
> text;
> int
> width = 0,
> height = 0
> ;
>
> public TextButton(Composite parent) {
> super(parent, SWT.FLAT);
>
> Display display = getDisplay();
>
> setBackground(getDisplay().getSystemColor(SWT.COLOR_GRAY));
>
> Listener listener = new Listener() {
> public void handleEvent(Event e) {
> switch (e.type) {
> case SWT.MouseEnter:
> onMouseEnter(); break;
> case SWT.MouseDown:
> onMouseDown (); break;
> case SWT.MouseExit:
> onMouseExit(); break;
> case SWT.MouseUp:
> onMouseUp(); break;
> case SWT.Paint:
> onPaint(e.gc); break;
> }
> }
> };
> int[] events = new int[] {
> SWT.MouseEnter, SWT.MouseDown,
> SWT.MouseUp, SWT.MouseExit, SWT.Paint};
> for (int i = 0; i < events.length; i++)
> addListener(events[i], listener);
> }
>
> void onMouseDown (){}
> void onMouseEnter(){}
> void onMouseExit(){}
> void onMouseUp(){}
>
> void onPaint(GC gc){
> // center string omitted
> gc.drawString(text,1,1);
> }
>
> public void setText(String s){
>
> if(s == null)
> SWT.error(SWT.ERROR_INVALID_ARGUMENT);
> text = s;
>
> GC gc = new GC(this);
> Point extent = gc.stringExtent(s);
> gc.dispose();
> height = extent.y + 2;
> width = extent.x + 2;
> redraw();
> }
>
> public Point computeSize(int w, int h, int flush){
> return new Point(width,height);
> }
> }
>
> /*
> * St a t u s b a r
> */
>
> class Statusbar extends Composite {
>
> final Image // an arbitary image
> infoImage = new Image(getDisplay(),"info.gif")
> ;
> final Label
> statusImage = new Label(this,SWT.NONE ),
> statusText = new Label(this,SWT.NONE),
> separator1 = new Label(this,SWT.SEPARATOR)
> // ,
> // buttonLabel = new Label(this,SWT.NONE)
> ;
> final TextButton
> textButton = new TextButton(this)
> ;
> final int cols = 4;
>
> public Statusbar(Composite parent) {
> super(parent, SWT.FLAT);
>
> Color color = getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
> infoImage.setBackground(color);
>
> // layout
>
> GridLayout layout = new GridLayout(cols,false);
>
> layout.horizontalSpacing = 5;
> layout.marginWidth = 5;
> layout.marginHeight = 2;
> setLayout(layout);
>
> GridData data = new GridData(SWT.LEFT,SWT.CENTER,false,false);
> statusImage.setLayoutData(data);
>
> data = new GridData(SWT.LEFT,SWT.CENTER,true,false);
> statusText.setLayoutData(data);
>
> data = new GridData(5,17);
> data.horizontalAlignment= SWT.CENTER;
> data.verticalAlignment = SWT.TOP;
> separator1.setLayoutData(data);
>
>
> data = new GridData(SWT.LEFT,SWT.CENTER,false,false);
> textButton.setLayoutData(data);
>
> // data = new GridData(SWT.LEFT,SWT.CENTER,false,false);
> // buttonLabel.setLayoutData(data);
>
> // items
>
> statusImage.setImage(infoImage);
> statusText.setText("Welcome !");
> textButton.setText("Button X");
> // buttonLabel.setText("Button X");
>
> layout();
>
> }
>
> public Point computeSize(int width, int height, boolean changed) {
> return new Point(width,22);
> }
> }
>
> /*
> * M a i n
> */
>
> public class Main {
>
> public static void main(String[] args) {
>
> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setBounds(display.getPrimaryMonitor().getClientArea()) ;
>
> GridLayout layout = new GridLayout();
> layout.marginWidth = 0;
> layout.marginHeight = 0;
> layout.verticalSpacing = 0;
> shell.setLayout(layout);
>
> Statusbar statusbar = new Statusbar(shell);
> GridData data = new GridData(SWT.FILL,SWT.FILL,true,false);
> statusbar.setLayoutData(data);
>
> shell.layout();
> shell.open();
>
> while (!display.isDisposed() && display.getShells().length > 0) {
> if (!display.readAndDispatch ())
> display.sleep ();
> }
> }
> }
>
> /* End code */
>
>
>
>
>
>
Re: Custom control and GridLayout [message #445773 is a reply to message #445756] Wed, 10 November 2004 21:17 Go to previous message
Peter Sommerfeld is currently offline Peter SommerfeldFriend
Messages: 12
Registered: July 2009
Junior Member
"Veronika Irvine" schrieb:
> Statusbar.computeSize is bad [...]

Veronika, thanks for advice !

Peter
Previous Topic:Information about "Display.mouseHoverProc"
Next Topic:Displaying the Selection Hilighting When doing a programatic selection in a TableViewer
Goto Forum:
  


Current Time: Tue Oct 27 02:07:45 GMT 2020

Powered by FUDForum. Page generated in 0.01655 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top