[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [platform-swt-dev] Cannot layout new widgets on a non rectangular shell

Hy Carolyn & Abhishek

@Carolyn: I know about the swt newsgroup and normally use it:) I will
post a reply here anyway in order that
Abhishek will stay on the same page. I can't find the topic regarding
having multiple event loops in the link
you provided. Could you please point me to the right location (you may
kindly also repy to the original request in
the swt newsgroup)?

@Abhishek: The code below works for me. Please note that I took quite a
big picture
to test. The scrolled composite is not transparent. I am not sure
whether you would need transparency for
the scrolled composite too and how to achieve it.

Cheers
/nick


import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;


public class NonRectangularShell { public static void main(String[] args) { final Display display = new Display ();

       InputStream inputStream =
Thread.currentThread().getContextClassLoader().
       getResourceAsStream("polygon.gif");

       if (inputStream == null) {
           throw new IllegalArgumentException("image not present?");
       }

       final Image image = new Image(display, inputStream);

       //final Image image =
display.getSystemImage(SWT.ICON_INFORMATION);

       final Shell shell = new Shell (display, SWT.NO_TRIM);
       Region region = new Region();
       final ImageData imageData = image.getImageData();
       if (imageData.alphaData != null) {
           Rectangle pixel = new Rectangle(0, 0, 1, 1);
           for (int y = 0; y < imageData.height; y++) {
               for (int x = 0; x < imageData.width; x++) {
                   if (imageData.getAlpha(x, y) == 255) {
                       pixel.x = imageData.x + x;
                       pixel.y = imageData.y + y;
                       region.add(pixel);
                   }
               }
           }
       } else {
           ImageData mask = imageData.getTransparencyMask();
           Rectangle pixel = new Rectangle(0, 0, 1, 1);
           for (int y = 0; y < mask.height; y++) {
               for (int x = 0; x < mask.width; x++) {
                   if (mask.getPixel(x, y) != 0) {
                       pixel.x = imageData.x + x;
                       pixel.y = imageData.y + y;
                       region.add(pixel);
                   }
               }
           }
       }
       shell.setRegion(region);

       // create a scrolled composite
       ScrolledComposite scrolledComposite = new
ScrolledComposite(shell, SWT.V_SCROLL);

       // set size and location
       scrolledComposite.setSize(150, 90);
       scrolledComposite.setLocation(100, 100);

       // create the composite
       Composite composite = new Composite(scrolledComposite, SWT.NONE);

       // put the composite into the scrolled composite
       scrolledComposite.setContent(composite);

       // add some components to the composite
       Button button1 = new Button(composite, SWT.NONE);
       button1.setLocation(10, 10);
       button1.setSize(60, 20);
       button1.setText("hello");

       Button button2 = new Button(composite, SWT.NONE);
       button2.setLocation(10, 100);
       button2.setSize(60, 20);
       button2.setText("hello 2");

       // pack the composite
       composite.pack();

       Listener l = new Listener() {
           int startX, startY;
           public void handleEvent(Event e)  {
               if (e.type == SWT.KeyDown && e.character == SWT.ESC) {
                   shell.dispose();
               }
               if (e.type == SWT.MouseDown && e.button == 1) {
                   startX = e.x;
                   startY = e.y;
               }
               if (e.type == SWT.MouseMove && (e.stateMask &
SWT.BUTTON1) != 0) {
                   Point p = shell.toDisplay(e.x, e.y);
                   p.x -= startX;
                   p.y -= startY;
                   shell.setLocation(p);
               }
               if (e.type == SWT.Paint) {
                   e.gc.drawImage(image, imageData.x, imageData.y);
               }
           }
       };
       shell.addListener(SWT.KeyDown, l);
       shell.addListener(SWT.MouseDown, l);
       shell.addListener(SWT.MouseMove, l);
       shell.addListener(SWT.Paint, l);

       shell.setSize(imageData.x + imageData.width, imageData.y +
imageData.height);
       shell.open();

       while (!shell.isDisposed ()) {
           if (!display.readAndDispatch ())
               display.sleep ();
       }
       region.dispose();
       image.dispose ();
       display.dispose ();
   }
}



Carolyn MacLeod wrote:


Nick,
The paragraph regarding multiple UI threads is completely incorrect and it was removed from the doc a long time ago.
You must be looking at doc from eclipse 2.1: http://help.eclipse.org/help21/topic/org.eclipse.platform.doc.isv/guide/swt_threading.htm


Check out some more recent doc, from eclipse 3.2: http://help.eclipse.org/help32/topic/org.eclipse.platform.doc.isv/guide/swt_threading.htm


Abhishek,
Make sure your composite has a layout, but your shell does not. Before opening the shell, pack() the composite, and position it on the shell using setLocation(x, y).
Make sure you put it in a place that is not being clipped out by the transparent region on the shell.


Both of you,
In future, please ask this type of user question on the swt newsgroup.

Thanks,
Carolyn



*nick <nick@xxxxxxxxxxxxxxxx>*
Sent by: platform-swt-dev-bounces@xxxxxxxxxxx

04/03/2007 02:54 AM
Please respond to
"Eclipse Platform SWT component developers list." <platform-swt-dev@xxxxxxxxxxx>




To
"Eclipse Platform SWT component developers list." <platform-swt-dev@xxxxxxxxxxx>
cc

Subject
Re: [platform-swt-dev] Cannot layout new widgets on a non rectangular shell










OK, I see. I am still confused about the vertical scrolling. You want a
vertical
scrollbar too right? I am not sure how to implement this in a non
rectangular window and
how such a scrollbar should look like ...

Does your code work appart from the vertical scrolling?

/nick

PS: Here the problem I have using multiple shells (posted this in March,
no suggestions so far):
------------------------------------------------------------------------------------------

I tried to write an application that has multiple UI-threads working
simultaneously.
According to the doc this should be possible if I understand correctly:


---------------- Note: The most common threading model for an SWT application is to run a single UI thread and perform long operations in computational threads. However, SWT does not restrict developers to this model. An application could run multiple UI-threads with a separate event loop in each thread.

----------------

The following application creates two windows as expected and all 3
thread seem to execute
simultaneously (tested on Windows XP). However, only one application
window is properly refreshed.

What is wrong?

-------------------------
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

class UserInterface extends Thread {

   public void run() {

       Display display = new Display();

Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
new Label(shell, SWT.SEPARATOR | SWT.VERTICAL);
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
System.out.println(Thread.currentThread().getName());
}
display.dispose();
}



}

public class SwtBackgroundThreadApplication {

   public SwtBackgroundThreadApplication() {
       // create a new thread and start it
       UserInterface userIf = new UserInterface();
       userIf.start();

       // create a new thread and start it
       UserInterface userIf2 = new UserInterface();
       userIf2.start();

       // do forever
       while (true) {
           System.out.println("main thread running");
           try {
               Thread.sleep(2000);
           } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
       }
   }

   public static void main(String[] args) {
       new SwtBackgroundThreadApplication();
   }
}

Abhishek Misra wrote:

> Hi Nick,
> No i just need a comp on a vertical scroll comp on a shell.
> This is a new pop up shell i am creating from my application. Now i
> needed multiple things, first is the shell which is non rectangular
> and has a image background which should propagate to the comp and any
> further widgets i lay on it, but so far it is not working. What kind
> of problems you had when you created multiple shells from your app.
>
> Thanks,
> abhi
>
>
> On 4/2/07, *nick* <nick@xxxxxxxxxxxxxxxx
> <mailto:nick@xxxxxxxxxxxxxxxx>> wrote:
>
> You can place a Composite into a ScrolledComposite, this works for me.
> Do you need
> to be able to resize the ScrolledComposite? But the shell (main
> window)
> itself is fixed since it is based on
> a transparent image correct (or do you create those images on the fly
> and need the window to
> reflect changes done to the image?)?
>
> Could you please explain in more detail?
>
> /nick
>
> PS: I had problems to use multiple shells within an application. You
> might want to avoid
> this.
>
> Abhishek Misra wrote:
>
> > Hi Carolyn,
> > I was trying to create a custom widget where the shell
> > could be created from a transparent image and i could at the very
> > minimum layout a Composite on a ScrolledComposite which will be
> on the
> > shell. I have so far been unable to accomplish the same probably
> > because of the fact that Scrolled composite is not fixed in
> size. Any
> > insights what is the best way to this?
> >
> > Thanks,
> > abhi
> >
> >
> > On 4/2/07, *Carolyn MacLeod* <Carolyn_MacLeod@xxxxxxxxxx
> <mailto:Carolyn_MacLeod@xxxxxxxxxx>
> > <mailto:Carolyn_MacLeod@xxxxxxxxxx
> <mailto:Carolyn_MacLeod@xxxxxxxxxx>>> wrote:
> >
> > Hi, Abhishek,
> > I think it would be easier in this case to just draw your label
> > text on
> > the shell instead of using a control.
> > For example, delete the Composite and CLabel code, and add
> > something like
> > the following to the Paint event, right after the drawImage:
> > e.gc.drawString("Hello there", imageData.x + 10,
> > imageData.y, true
> > );
> > Also, please note that CLabel does not support SWT.WRAP style.
> >
> > Nick,
> > Please use SWT.NONE instead of SWT.NO <http://SWT.NO>
> <http://SWT.NO <http://SWT.NO>> when
> > creating a Composite or Button
> > with none of their style bits set.
> >
> > Hope this helps,
> > Carolyn
> >
> >
> >
> >
> >
> >
> > nick < nick@xxxxxxxxxxxxxxxx <mailto:nick@xxxxxxxxxxxxxxxx>
> <mailto:nick@xxxxxxxxxxxxxxxx <mailto:nick@xxxxxxxxxxxxxxxx>>>
> > Sent by: platform-swt-dev-bounces@xxxxxxxxxxx
> <mailto:platform-swt-dev-bounces@xxxxxxxxxxx>
> > <mailto:platform-swt-dev-bounces@xxxxxxxxxxx
> <mailto:platform-swt-dev-bounces@xxxxxxxxxxx>>
> > 04/02/2007 08:40 PM
> > Please respond to
> > "Eclipse Platform SWT component developers list."
> > <platform-swt-dev@xxxxxxxxxxx
> <mailto:platform-swt-dev@xxxxxxxxxxx> <mailto:
> platform-swt-dev@xxxxxxxxxxx <mailto:platform-swt-dev@xxxxxxxxxxx>>>
> >
> >
> > To
> > "Eclipse Platform SWT component developers list."
> > <platform-swt-dev@xxxxxxxxxxx
> <mailto:platform-swt-dev@xxxxxxxxxxx>
> <mailto:platform-swt-dev@xxxxxxxxxxx
> <mailto:platform-swt-dev@xxxxxxxxxxx>>>
> > cc
> >
> > Subject
> > Re: [platform-swt-dev] Cannot layout new widgets on a non
> rectangular
> > shell
> >
> >
> >
> >
> >
> >
> > I don't think that this is related to the non-rectangular shape
> > of the
> > shell. Looks
> > more like a layout problem.
> >
> > replace
> >
> > -----------------------
> > final Composite comp = new Composite(shell, SWT.NONE);
> > comp.setBackgroundMode(SWT.INHERIT_FORCE);
> > GridLayout layout = new GridLayout(1, false);
> > .
> > .
> > -----------------------
> > with
> >
> > -----------------------
> > Composite composite = new Composite(shell, SWT.NO
> <http://SWT.NO> < http://SWT.NO>);
> > composite.setLayout(new FillLayout());
> > Button button1 = new Button(shell, SWT.NO
> <http://SWT.NO>
> > < http://SWT.NO>);
> > button1.setLocation(5, 5);
> > button1.setSize(150, 20);
> > button1.setText("hello");
> > -----------------------
> >
> > and the button will show up
> >
> > /nick
> >
> > Abhishek Misra wrote:
> >
> > > Created from snippet 219.
> > >
> > > http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet219.java?view=co
> > <
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet219.java?view=co>
> >
> > > <
> > > http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet219.java?view=co
> > <
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet219.java?view=co>
> > >
> > >
> > > I changed the image in the above snippet to a bigger
> 500*200 image
> > > which is transparent and use that.
> > >
> > > I am trying to create a new composite on the shell created
> in above
> > > step and put a label on it, but it doesn't work. All that
> shows
> > up is
> > > shell with the image.
> > >
> > > final Display display = new Display ();
> > > final Image image = ImageManager.getImage
> > ("info_window.png");
> > > final Shell shell = new Shell (display, SWT.NO_TRIM);
> > > Region region = new Region();
> > > final ImageData imageData = image.getImageData();
> > > if ( imageData.alphaData != null) {
> > > Rectangle pixel = new Rectangle(0, 0, 1, 1);
> > > for (int y = 0; y < imageData.height; y++) {
> > > for (int x = 0; x < imageData.width ; x++) {
> > > if (imageData.getAlpha(x, y) == 255) {
> > > pixel.x = imageData.x + x;
> > > pixel.y = imageData.y + y;
> > > region.add(pixel);
> > > }
> > > }
> > > }
> > > } else {
> > > ImageData mask = imageData.getTransparencyMask();
> > > Rectangle pixel = new Rectangle(0, 0, 1, 1);
> > > for (int y = 0; y < mask.height; y++) {
> > > for (int x = 0; x < mask.width; x++) {
> > > if (mask.getPixel(x, y) != 0) {
> > > pixel.x = imageData.x + x;
> > > pixel.y = imageData.y + y;
> > > region.add(pixel);
> > > }
> > > }
> > > }
> > > }
> > > shell.setRegion (region);
> > >
> > > final Composite comp = new Composite(shell, SWT.NONE);
> > > comp.setBackgroundMode(SWT.INHERIT_FORCE);
> > > GridLayout layout = new GridLayout(1, false);
> > > layout.marginRight = 10;
> > > comp.setLayout(layout);
> > > GridData data = new GridData(GridData.FILL_BOTH);
> > > comp.setLayoutData(data);
> > > CLabel label = new CLabel(comp, SWT.WRAP);
> > >
> > label.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
> > >
> > > final GridData textData = new
> > > GridData(GridData.FILL_HORIZONTAL |
> GridData.VERTICAL_ALIGN_FILL);
> > > textData.horizontalIndent = 0;
> > > textData.verticalIndent = 0;
> > > label.setLayoutData(textData);
> > > label.setBackgroundMode(SWT.INHERIT_DEFAULT);
> > >
> > > Listener l = new Listener() {
> > > int startX, startY;
> > > public void handleEvent(Event e) {
> > > if (e.type == SWT.KeyDown && e.character ==
> > SWT.ESC) {
> > > shell.dispose ();
> > > }
> > > if (e.type == SWT.MouseDown && e.button ==
> 1) {
> > > startX = e.x;
> > > startY = e.y;
> > > }
> > > if (e.type == SWT.MouseMove && (e.stateMask &
> > > SWT.BUTTON1) != 0) {
> > > Point p = shell.toDisplay (e.x, e.y);
> > > p.x -= startX;
> > > p.y -= startY;
> > > shell.setLocation(p);
> > > }
> > > if ( e.type == SWT.Paint) {
> > > e.gc.drawImage(image, imageData.x,
> imageData.y);
> > > }
> > > }
> > > };
> > > shell.addListener(SWT.KeyDown, l);
> > > shell.addListener(SWT.MouseDown, l);
> > > shell.addListener(SWT.MouseMove, l);
> > > shell.addListener(SWT.Paint, l);
> > >
> > > shell.setSize(imageData.x + imageData.width,
> imageData.y +
> > > imageData.height);
> > > shell.layout();
> > > shell.open ();
> > > while (!shell.isDisposed ()) {
> > > if (!display.readAndDispatch ())
> > > display.sleep ();
> > > }
> > > region.dispose();
> > > image.dispose ();
> > > display.dispose ();
> > > }
> > >
> > > Any idea what's wrong?
> > >
> > >
> > >
> > > Thanks,
> > > abhi
> > >
> > > >------------------------------------------------------------------------
> >
> > >
> > >_______________________________________________
> > >platform-swt-dev mailing list
> > >platform-swt-dev@xxxxxxxxxxx
> <mailto:platform-swt-dev@xxxxxxxxxxx>
> <mailto:platform-swt-dev@xxxxxxxxxxx
> <mailto:platform-swt-dev@xxxxxxxxxxx>>
> > > https://dev.eclipse.org/mailman/listinfo/platform-swt-dev
> > >
> > >
> >
> > _______________________________________________
> > platform-swt-dev mailing list
> > platform-swt-dev@xxxxxxxxxxx
> <mailto:platform-swt-dev@xxxxxxxxxxx>
> <mailto:platform-swt-dev@xxxxxxxxxxx
> <mailto:platform-swt-dev@xxxxxxxxxxx>>
> > https://dev.eclipse.org/mailman/listinfo/platform-swt-dev
> <https://dev.eclipse.org/mailman/listinfo/platform-swt-dev>
> >
> >
> > _______________________________________________
> > platform-swt-dev mailing list
> > platform-swt-dev@xxxxxxxxxxx
> <mailto:platform-swt-dev@xxxxxxxxxxx> <mailto:
> platform-swt-dev@xxxxxxxxxxx <mailto:platform-swt-dev@xxxxxxxxxxx>>
> > https://dev.eclipse.org/mailman/listinfo/platform-swt-dev
> >
> >
> >
> >------------------------------------------------------------------------
> >
> >_______________________________________________
> >platform-swt-dev mailing list
> > platform-swt-dev@xxxxxxxxxxx <mailto:platform-swt-dev@xxxxxxxxxxx>
> >https://dev.eclipse.org/mailman/listinfo/platform-swt-dev
> >
> >
>
>
> _______________________________________________
> platform-swt-dev mailing list
> platform-swt-dev@xxxxxxxxxxx <mailto:platform-swt-dev@xxxxxxxxxxx>
> https://dev.eclipse.org/mailman/listinfo/platform-swt-dev
> <https://dev.eclipse.org/mailman/listinfo/platform-swt-dev>
>
>
>
>------------------------------------------------------------------------
>
>_______________________________________________
>platform-swt-dev mailing list
>platform-swt-dev@xxxxxxxxxxx
>https://dev.eclipse.org/mailman/listinfo/platform-swt-dev
> >


_______________________________________________
platform-swt-dev mailing list
platform-swt-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/platform-swt-dev

------------------------------------------------------------------------

_______________________________________________
platform-swt-dev mailing list
platform-swt-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/platform-swt-dev




begin:vcard
fn:nicksoftware company limited
n:company limited;nicksoftware
org:;Software Development
adr:T. Suthep A. Mueang;;142/9 T. Suandok Soi 6;Chiangmai;;50200;Thailand
email;internet:nick@xxxxxxxxxxxxxxxx
tel;work:+66 53 281 844
tel;cell:+66 810 25 80 11
x-mozilla-html:FALSE
url:http://www.nicksoftware.co.th
version:2.1
end:vcard