开发者

Redrawing control behind composite with SWT.NO_BACKGROUND

开发者 https://www.devze.com 2022-12-19 01:35 出处:网络
Original goal: I have a TreeMenu that i use to display my Menu. In this tree, a user can select different items.

Original goal:

I have a TreeMenu that i use to display my Menu. In this tree, a user can select different items.

I would like to disable the tree, so that a user cannot select a new item after choosing the first. The catch is, we cannot use setEnabled, because we are not allowed to use the greyed out look. The look/colors may not change.

Our proposed solution

Our first idea was to use a Composite with SWT.NO_BACKGROUND on top of the menu, to prevent any user interaction with the TreeMenu.

Code:

final Composite cover = new Composite(getPage().shell, SWT.NO_BACKGROUND);
cover.setLocation(getMenu().getLocation());
cover.setSize(getMenu().getSize());
cover.moveAbove(getMenu());

This has a problem with redrawing.

If the screen is covered by another screen and then brought back to front, the Cover Composite is filled with fragments of the previous overlapping window.

Our idea was to manually redraw the menu:

cover.moveBelow(getMenu());
getMenu().update();
cover.moveAbove(getMenu());

We placed the code inside the paintEventListener.

But this caused an infinite loop and did not solve the problem.

Questions

Does anyone have an 开发者_如何转开发idea how we can achive our orignial goal?

Does anyone know how we can make our proposed solution work?


Look at SWT-Snippet80. It shows how to prevent selections. A solution to your problem would be adding a listener like this to your tree:

  tree.addListener(SWT.Selection, new Listener() {

     TreeItem[] oldSelection = null;


     public void handleEvent( Event e ) {
        Tree tree = (Tree)(e.widget);
        TreeItem[] selection = tree.getSelection();
        if ( oldSelection != null )
           tree.setSelection(oldSelection);
        else
           oldSelection = selection;
     }
  });

I wouldn't recommend trying to implement your workaround. I believe that placing transparent controls on top of each other is unsupported in SWT - I think I read a comment from Steve Northover on this subject once. Even if you made it work for some OS, it probably won't work in another - it's too much of a hack.

A solution that is supported by SWT, is having transparent windows on top of each other. But that is also really hard to implement (resizing, moving, always on top, redraw artifacts) and probably as big a hack as the other workaround. Go for the listener.

0

精彩评论

暂无评论...
验证码 换一张
取 消