www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.dwt - and snippet 273

I did not have the Parliament.jpg for this -suppose anything would work

module snippet273;
/*******************************************************************************
 * 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: modify the clipping of custom background paints
 * 
 * For a detailed explanation of this snippet see
 * http://www.eclipse.org/articles/Article-CustomDrawingTableAndTreeItems/customDraw.htm#_example4
 * 
 * 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 dwt.graphics.Rectangle;
import tango.io.Stdout;
import tango.util.Convert; 


static void main(char[][] args) {
  final char[][] MONTHS = [
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  ];
  final int[] HIGHS = [-7, -4, 1, 11, 18, 24, 26, 25, 20, 13, 5, -4];
  final int[] LOWS = [-15, -13, -7, 1, 7, 13, 15, 14, 10, 4, -2, -11];
  final int SCALE_MIN = -30; final int SCALE_MAX = 30;
  final int SCALE_RANGE = Math.abs(SCALE_MIN - SCALE_MAX);

  Display display = new Display();
  Shell shell = new Shell(display);
  shell.setBounds(10,10,400,350);
  shell.setText("Ottawa Average Daily Temperature Ranges");
  final Color blue = display.getSystemColor(DWT.COLOR_BLUE);
  final Color white = display.getSystemColor(DWT.COLOR_WHITE);
  final Color red = display.getSystemColor(DWT.COLOR_RED);
  // final Image parliamentImage = new Image(display, "./parliament.jpg");
  final Table table = new Table(shell, DWT.NONE);
  table.setBounds(10,10,350,300);
  // table.setBackgroundImage(parliamentImage);
  for (int i = 0; i < 12; i++) {
    TableItem item = new TableItem(table, DWT.NONE);
    item.setText(to!(char[])(MONTHS[i]) ~ " (" ~ to!(char[])(LOWS[i]) ~ "C..."
~ to!(char[])(HIGHS[i]) ~ "C)");
  }
  final int clientWidth = table.getClientArea().width;

  /*
   * NOTE: MeasureItem and EraseItem are called repeatedly. Therefore it is
   * critical for performance that these methods be as efficient as possible.
   */
  table.addListener(DWT.MeasureItem, new class Listener {
    public void handleEvent(Event event) {
      int itemIndex = table.indexOf(cast (TableItem)(event.item));
      int rightX = (HIGHS[itemIndex] - SCALE_MIN) * clientWidth / SCALE_RANGE;
      event.width = rightX;
    }
  });
  table.addListener(DWT.EraseItem, new class Listener {
    public void handleEvent(Event event) {
      int itemIndex = table.indexOf(cast (TableItem)(event.item));
      int leftX = (LOWS[itemIndex] - SCALE_MIN) * clientWidth / SCALE_RANGE;
      int rightX = (HIGHS[itemIndex] - SCALE_MIN) * clientWidth / SCALE_RANGE;
      GC gc = event.gc;
      Rectangle clipping = gc.getClipping();
      clipping.x = leftX;
      clipping.width = rightX - leftX;
      gc.setClipping(clipping);
      Color oldForeground = gc.getForeground();
      Color oldBackground = gc.getBackground();
      gc.setForeground(blue);
      gc.setBackground(white);
      gc.fillGradientRectangle(event.x, event.y, event.width / 2, event.height,
false);
      gc.setForeground(white);
      gc.setBackground(red);
      gc.fillGradientRectangle(
        event.x + event.width / 2, event.y, event.width / 2, event.height,
false);
      gc.setForeground(oldForeground);
      gc.setBackground(oldBackground);
      event.detail &= ~DWT.BACKGROUND;
      event.detail &= ~DWT.HOT;
    }
  });

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