www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.dwt - example3

From the same page
http://www.eclipse.org/articles/article.php?file=Article-CustomDrawingTableAndTreeItems/index.html


module example3;
/*******************************************************************************
 * Copyright (c) 2000, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;

/*
 * Table snippet: custom draw a Table's selection rectangle as a gradient
 * 
 * For a detailed explanation of this snippet see

 * 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 *  since 3.2
 */
import dwt.DWT;
import dwt.widgets.Display;
import dwt.widgets.Shell;
import dwt.widgets.Table;
import dwt.widgets.TableColumn;
import dwt.widgets.TableItem;
import dwt.widgets.Event;
import dwt.widgets.Listener;
import dwt.graphics.Color;
import dwt.graphics.GC;
import tango.io.Stdout;
import tango.util.Convert; 




static void main(char[][] args) {
  Display display = new Display(); 
  Shell shell = new Shell(display);
  final Color red = display.getSystemColor(DWT.COLOR_RED);
  final Color yellow = display.getSystemColor(DWT.COLOR_YELLOW);
  final Table table = new Table(shell, DWT.FULL_SELECTION);
  table.setHeaderVisible(true);
  TableColumn col0= new TableColumn(table, DWT.NONE);
    col0.setWidth(100);
  TableColumn col1= new TableColumn(table, DWT.NONE);
    col1.setWidth(100);
  TableColumn col2= new TableColumn(table, DWT.NONE);
    col2.setWidth(100);
  for (int i = 0; i < 5; i++) {
    TableItem item = new TableItem(table, DWT.NONE);
    item.setText(0, "item "~ to!(char[])(i)~ " col 0");
    item.setText(1, "item "~ to!(char[])(i)~" col 1");
    item.setText(2, "item "~to!(char[])(i)~" col 2");
  }
  table.pack();

  /*
   * NOTE: EraseItem is called repeatedly.  Therefore it is critical
   * for performance that this method be as efficient as possible.
   */
  table.addListener(DWT.EraseItem, new class Listener {
    public void handleEvent(Event event) {
      event.detail &= ~DWT.HOT;
      if ((event.detail & DWT.SELECTED) == 0) return; /* item not selected */
      int clientWidth = table.getClientArea().width;
      GC gc = event.gc;
      Color oldForeground = gc.getForeground();
      Color oldBackground = gc.getBackground();
      gc.setForeground(red);
      gc.setBackground(yellow);
      gc.fillGradientRectangle(0, event.y, clientWidth, event.height, false);
      gc.setForeground(oldForeground);
      gc.setBackground(oldBackground);
      event.detail &= ~DWT.SELECTED;
    }
  });

  shell.pack();
  shell.open();
  while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) display.sleep();
  }
  display.dispose();
}
Jun 07 2008