Saturday, March 22, 2008

Transparent bordered shell in SWT

I've wanted to get a transparent bordered shell in SWT ever since I saw the transparent bordered video shell in Leopard video quickview.

A shell with alpha support is coming in 3.4. However, I have not looked at the 3.4 material as yet and this sample was built on 3.3.

The shell can be used in the same way that the org.eclipse.swt.custom.PopupList class can be used except that you can place any controls you want and not just a List. Lets see how to do this -

In SWT, a Canvas control can have an alpha value. Thus, the base needs to be a canvas. First, create a control that extends the Canvas. Set the layout for this canvas to a FillLayout. This canvas will contain other controls. It is important to set the margin of the layout. This margin will be the width of your transparent border. Now, set an alpha value for the canvas. The alpha determines how transparent the border will be.

Image -
Now create or load a gradiented (is there such a word?) image. In the paintListener for the Canvas, draw this gradiented image. Because of the alpha on the canvas, the image will be semi-transparent. Now add any controls (typically a composite with controls within it) to the canvas and these will be drawn with a transparent border around them.

To get the real transparency, this canvas needs to be placed inside a shell created with the style SWT.NO_TRIM and SWT.ON_TOP. Also, the canvas needs to be created with the style SWT.NO_BACKGROUND. However, due to these styles, the shell cannot be really moved around. Thus, its useful where no movement is necessary - a transparent menu, infopopups, ...

Thus, to summarize,
1. A shell with styles SWT.NO_TRIM and SWT.ON_TOP contains
2. a canvas with style NO_BACKGROUND, a gradiented image, an alpha value, a margin (border width) contains
3. a control (composite) that is displayed within the canvas.

  DB Reading BASE: An Acid Alternative: In partitioned databases, trading some consistency for availability can lead to dramatic improvement...