/******************************************************************************* * Copyright (c) 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; /* * Detect drag in a custom control * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ * * @since 3.3 */ import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.events.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.*; public class Snippet259 { static class MyList extends Canvas { int selection; String [] items = new String [0]; static final int INSET_X = 2; static final int INSET_Y = 2; static int checkStyle (int style) { style &= ~(SWT.H_SCROLL | SWT.V_SCROLL); style |= SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE; return style; } public MyList (Composite parent, int style) { super (parent, checkStyle (style)); super.setDragDetect (false); addMouseListener (new MouseAdapter () { public void mouseDown (MouseEvent event) { GC gc = new GC (MyList.this); Rectangle client = getClientArea(); int index = 0, x = client.x + INSET_X, y = client.y + INSET_Y; while (index < items.length) { Point pt = gc.stringExtent(items [index]); Rectangle item = new Rectangle (x, y, pt.x, pt.y); if (item.contains (event.x, event.y)) break; y += pt.y; if (!client.contains (x, y)) return; index++; } gc.dispose (); if (index == items.length || !client.contains (x, y)) { return; } selection = index; redraw (); update (); dragDetect (event); } }); addPaintListener (new PaintListener () { public void paintControl (PaintEvent event) { GC gc = event.gc; Color foreground = event.display.getSystemColor (SWT.COLOR_LIST_FOREGROUND); Color background = event.display.getSystemColor (SWT.COLOR_LIST_BACKGROUND); Color selectForeground = event.display.getSystemColor (SWT.COLOR_LIST_SELECTION_TEXT); Color selectBackground = event.display.getSystemColor (SWT.COLOR_LIST_SELECTION); gc.setForeground (foreground); gc.setBackground (background); MyList.this.drawBackground (gc, event.x, event.y, event.width, event.height); int x = INSET_X, y = INSET_Y; for (int i=0; i