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

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)
---

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.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;

public class Tester {

 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.marginTop = 10;
  gl.marginLeft = 10;
  mw.setLayout(gl);

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

  // 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(1, true));

   Label one = new Label(this, SWT.None);
   one.setText("One");
   Label two = new Label(this, SWT.None);
   two.setText("Two");
  }
 }

 // 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);
  }

 }

}

--

Regards,
Emil


"Kris Lyon" <kris.lyon@xxxxxxxxxx> wrote in message 
news:4b07d4579fe2c7fc88d8237ca01f2eb2$1@xxxxxxxxxxxxxxxxxx
> Alrighty... with a little more perseverance, i've been able to get both 
> the widgets and the border drawing at the same time on the canvas.  The 
> issue I was having seems to be tied to the use of a Layout.  If I set the 
> location/bounds of the widgets manually, then they draw as expected.  But 
> if I use a layout to manage them, then I only get the widgets. (No custom 
> border)  The paintControl method is still called, but no border is drawn.
>
> As for using the Group box border, im just toying around with custom 
> controls trying to get a feel for what i can do, and how.  I wanted to 
> know how to surround something with a custom frame or border.
>
> So i seem to have a workaround, but it's not ideal (could be improved by 
> placing the widgets on their on composite, then i'd only have to place a 
> single control) Anyone know why the setting of a layout causes this 
> behavior?
>
>
> The exact code i'm playing with here is:
>
> public class MyWidget extends Canvas {
>
> public MyWidget( Composite parent, int style ){
> super( parent, style );
>
> Label one = new Label( this, SWT.None );
> one.setText("One");
> // one.setLocation(10,10);
> // one.setBounds(10, 10, 100, 50);
> Label two = new Label( this, SWT.None );
> two.setText("Two");
>
> //setLayout( new FillLayout(SWT.VERTICAL) );
>
> addPaintListener( new PaintListener(){
> public void paintControl( PaintEvent e ){
> MyWidget.this.paintControl(e);
> }
> });
>
> }
>
> public void paintControl( PaintEvent e ){
> GC gc = e.gc;
> Point pt = this.getSize();
> gc.setLineWidth(2);
> gc.setLineStyle(SWT.LINE_DASH);
>                gc.drawRectangle(1, 1, pt.x, pt.y);
> }
>
> }
>