开发者

SWT Tree with columns: Remove alternating background coloring for rows on Linux/Mac

开发者 https://www.devze.com 2023-03-08 17:30 出处:网络
I\'m coloring some of the columns of a tree with columns (which looks like 开发者_开发百科a Table). On Windows systems it looks good but on Linux/Mac it doesn\'t because they use alternating backgrou

I'm coloring some of the columns of a tree with columns (which looks like 开发者_开发百科a Table). On Windows systems it looks good but on Linux/Mac it doesn't because they use alternating background coloring (white, blue, white, blue,...).

I would like to remove the alternating background coloring for the tree. Any ideas if it's possible? I tried to forbid tree.setLinesVisible(true) on these systems, but it also won't draw vertical lines, which I need.


I think you can do this using owner draw. Use this snippet as starting point, and exchange the EraseItem event handler with this:

public void handleEvent( Event event ) {
   event.detail &= ~SWT.HOT;
   GC gc = event.gc;
   Rectangle area = table.getClientArea();
   /*
    * If you wish to paint the selection beyond the end of
    * last column, you must change the clipping region.
    */
   int columnCount = table.getColumnCount();
   if ( event.index == columnCount - 1 || columnCount == 0 ) {
      int width = area.x + area.width - event.x;
      if ( width > 0 ) {
         Region region = new Region();
         gc.getClipping(region);
         region.add(event.x, event.y, width, event.height);
         gc.setClipping(region);
         region.dispose();
      }
   }
   gc.setAdvanced(true);
   if ( gc.getAdvanced() ) gc.setAlpha(127);
   Rectangle rect = event.getBounds();
   Color background = gc.getBackground();
   gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
   gc.fillRectangle(0, rect.y, 500, rect.height);
   // restore colors for subsequent drawing
   gc.setBackground(background);
}

Should work on GTK (which is the linux window manager SWT uses) and with Trees, haven't tried it though.

Since owner draw is expensive I strongly suggest to add such a event handler for GTK:

if(SWT.getPlatform().equals("gtk")){ ... }

But the whole point of SWT is having native drawing. This improves the user acceptance of your software, because users feel "at home". Maybe you can design a way, where you don't have to take away the alternating background for GTK users.

0

精彩评论

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