TooltipLabelListener
yidabu
yidabu.spam at gmail.com
Sat May 24 19:36:21 PDT 2008
// Written in the D programming language.
/*******************************************************************************
Copyright: Copyright (c) 2008 (yidabu g m a i l at com) All rights reserved
License: BSD style: $(LICENSE)
Version: Initial release: May 2008
Authors: yidabu ( D Programming Language China : http://www.d-programming-language-china.org/ )
based on jface.snippets.Snippet031TableViewerCustomTooltipsMultiSelection;
*******************************************************************************/
module TooltipTable;
import dwt.DWT;
import dwt.graphics.Point;
import dwt.graphics.Rectangle;
import dwt.layout.FillLayout;
import dwt.widgets.Display;
import dwt.widgets.Event;
import dwt.widgets.Label;
import dwt.widgets.Listener;
import dwt.widgets.Shell;
import dwt.widgets.Table;
import dwt.widgets.TableColumn;
import dwt.widgets.TableItem;
import dwt.dwthelper.System;
import dwt.widgets.Listener;
import Array = tango.core.Array;
TooltipLabelListener tooltipLabelListener;
static this()
{
tooltipLabelListener = new TooltipLabelListener();
}
/**
Example:
---
char[][] headers = ["headers1", "headers2"];
Listener tooltipTableListener = dgListener(&handleTooltipTable, table, headers);
table.addListener (DWT.Dispose, tooltipTableListener);
table.addListener (DWT.KeyDown, tooltipTableListener);
table.addListener (DWT.MouseMove, tooltipTableListener);
table.addListener (DWT.MouseHover, tooltipTableListener);
---
*/
void handleTooltipTable(Event event, Table table, char[][] headers = null)
{
Shell tooltip = null;
Label label = null;
/*
* (non-Javadoc)
* @see dwt.widgets.Listener#handleEvent(dwt.widgets.Event)
*/
switch (event.type) {
case DWT.KeyDown:
case DWT.Dispose:
case DWT.MouseMove: {
if (tooltip is null) break;
tooltip.dispose ();
tooltip = null;
label = null;
break;
}
case DWT.MouseHover: {
Point coords = new Point(event.x, event.y);
TableItem item = table.getItem(coords);
if (item !is null) {
int columnCount = table.getColumnCount();
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
if (item.getBounds(columnIndex).contains(coords))
{
auto columnText = table.getColumn(columnIndex).getText();
if( headers.length && Array.find(headers, columnText) == headers.length )
break;
/* Dispose of the old tooltip (if one exists */
if (tooltip !is null && !tooltip.isDisposed ()) tooltip.dispose ();
/* Create a new Tooltip */
tooltip = new Shell (table.getShell(), DWT.ON_TOP | DWT.NO_FOCUS | DWT.TOOL);
tooltip.setBackground (table.getDisplay().getSystemColor (DWT.COLOR_INFO_BACKGROUND));
FillLayout layout = new FillLayout ();
layout.marginWidth = 2;
tooltip.setLayout (layout);
label = new Label (tooltip, DWT.NONE);
label.setForeground (table.getDisplay().getSystemColor (DWT.COLOR_INFO_FOREGROUND));
label.setBackground (table.getDisplay().getSystemColor (DWT.COLOR_INFO_BACKGROUND));
/* Store the TableItem with the label so we can pass the mouse event later */
label.setData ("_TableItem_", item);
/* Set the tooltip text */
//label.setText("Tooltip: " ~ to!(char[])(item.getData()) ~ " : " ~ to!(char[])(columnIndex));
label.setText(item.getText(columnIndex));
/* Setup Listeners to remove the tooltip and transfer the received mouse events */
label.addListener (DWT.MouseExit, tooltipLabelListener);
label.addListener (DWT.MouseDown, tooltipLabelListener);
/* Set the size and position of the tooltip */
Point size = tooltip.computeSize (DWT.DEFAULT, DWT.DEFAULT);
Rectangle rect = item.getBounds (columnIndex);
Point pt = table.toDisplay (rect.x, rect.y);
tooltip.setBounds (pt.x, pt.y, size.x, size.y);
/* Show it */
tooltip.setVisible (true);
break;
}
}
}
}
}
}
/**
* This listener is added to the tooltip so that it can either dispose itself if the mouse
* exits the tooltip or so it can pass the selection event through to the table.
*/
final private class TooltipLabelListener : Listener
{
private bool isCTRLDown(Event e) {
return (e.stateMask & DWT.CTRL) != 0;
}
/*
* (non-Javadoc)
* @see dwt.widgets.Listener#handleEvent(dwt.widgets.Event)
*/
public void handleEvent (Event event) {
Label label = cast(Label)event.widget;
Shell shell = label.getShell ();
switch (event.type) {
case DWT.MouseDown: /* Handle a user Click */
/* Extract our Data */
Event e = new Event ();
e.item = cast(TableItem) label.getData ("_TableItem_");
Table table = (cast(TableItem) e.item).getParent();
/* Construct the new Selection[] to show */
TableItem [] newSelection = null;
if (isCTRLDown(event)) {
/* We have 2 scenario's.
* 1) We are selecting an already selected element - so remove it from the selected indices
* 2) We are selecting a non-selected element - so add it to the selected indices
*/
TableItem[] sel = table.getSelection();
for (int i = 0; i < sel.length; ++i) {
//if (e.item.equals(sel[i])) {
if (e.item is sel[i]) {
// We are de-selecting this element
newSelection = new TableItem[sel.length - 1];
System.arraycopy(sel, 0, newSelection, 0, i);
System.arraycopy(sel, i+1, newSelection, i, sel.length - i - 1);
break;
}
}
/*
* If we haven't created the newSelection[] yet, than we are adding the newly selected element
* into the list of selected indicies
*/
if (newSelection is null) {
newSelection = new TableItem[sel.length + 1];
System.arraycopy(sel, 0, newSelection, 0, sel.length);
newSelection[sel.length] = cast(TableItem) e.item;
}
} else {
/* CTRL is not down, so we simply select the single element */
newSelection = [ cast(TableItem) e.item ];
}
/* Set the new selection of the table and notify the listeners */
table.setSelection (newSelection);
table.notifyListeners (DWT.Selection, e);
/* Remove the Tooltip */
shell.dispose ();
table.setFocus();
break;
case DWT.MouseExit:
shell.dispose ();
break;
}
}
}
--
yidabu <yidabu.spam at gmail.com>
http://www.dsource.org/projects/dwin
D 语言-中文(D Chinese):
http://www.d-programming-language-china.org/
http://bbs.d-programming-language-china.org/
http://dwin.d-programming-language-china.org/
http://scite4d.d-programming-language-china.org/
More information about the Digitalmars-d-dwt
mailing list