[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: Canvas vs Composite Question....

Emil Crumhorn wrote:
You're on the right track but I think you need a push in the right direction. I've taken your code and created a test example that you can run. Compare it with your code to see some differences. Note the use of the layouts even on the widget you're drawing on to. Otherwise you'd never see the native widgets, just what you draw. Also note that you have to take into account how you're drawing a background when you put widgets on top, some layouts may simply position sub-composites on top of what you're drawing and you'd never see it (and sometimes you will probably want to write your own Layout class that deals with all that for you).

> Code (copy & paste into Eclipe and run the code formatter, CTRL+SHIFT+F)
> ---

The code had shell.open() commented out, so it never showed the window.
Try this slightly modified example, which also demonstrates a more real-world layout and corrects the Canvas layout's margin.


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.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class CustomDrawCompositeDemo {

	public static void main(String[] args) {
		// standard display and shell etc
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("Shell");
		shell.setSize(200, 200);
		shell.setLayout(new FillLayout());

// create your widget as it's the outer-most widget, it's a child of the shell
MyWidget mw = new MyWidget(shell, SWT.NONE);


		// this layout will lay out widgets inside the canvas where you are
		// drawing the border
		GridLayout gl = new GridLayout();
		gl.marginHeight = 10;
		gl.marginWidth = 10;
		mw.setLayout(gl);

		// create the composite that holds your widgets (labels etc) and put it
		// on the MyWidget
		Composite inner = new InnerComposite(mw, SWT.NONE);
		inner.setLayoutData(new GridData(GridData.FILL_BOTH));

		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
	// this class only deals with native components
	static class InnerComposite extends Composite {

		public InnerComposite(Composite parent, int style) {
			super(parent, style);

			setLayout(new GridLayout(2, false));

			Label one = new Label(this, SWT.None);
			one.setText("One:");
			Text text1 = new Text(this, SWT.BORDER);
			text1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			Label two = new Label(this, SWT.None);
			two.setText("Two:");
			Text text2 = new Text(this, SWT.BORDER);
			text2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		}
	}

	// this class only deals with drawing
	static class MyWidget extends Canvas {

		public MyWidget(Composite parent, int style) {
			super(parent, style);

			addPaintListener(new PaintListener() {
				public void paintControl(PaintEvent e) {
					drawControl(e);
				}
			});
		}

		// draws the border
		public void drawControl(PaintEvent e) {
			GC gc = e.gc;
			Rectangle rect = getClientArea();
			gc.setLineWidth(2);
			gc.setLineStyle(SWT.LINE_DASH);

// draw it a little inside of the total space that we take up
gc.drawRectangle(rect.x + 5, rect.y + 5, rect.width - 10, rect.height - 10);
}


	}

}