view dwt/dnd/DropTargetEffect.d @ 156:969e7de37c3d default tip

Fixes to get dwt to work with dmd and ldc
author Jacob Carlborg <doob@me.com>
date Wed, 08 Jul 2009 21:56:44 +0200
parents 43123e716bda
children
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2007, 2008 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
 *     
 * Port to the D programming language:
 *     Jacob Carlborg <doob@me.com>
 *******************************************************************************/
module dwt.dnd.DropTargetEffect;

import dwt.dwthelper.utils;

import dwt.DWT;
import dwt.graphics.Point;
import dwt.graphics.Rectangle;
import dwt.widgets.Control;
import dwt.widgets.Table;
import dwt.widgets.TableItem;
import dwt.widgets.Tree;
import dwt.widgets.TreeItem;
import dwt.widgets.Widget;

import dwt.dnd.DropTargetAdapter;


/**
 * This class provides a default drag under effect during a drag and drop. 
 * The current implementation does not provide any visual feedback.
 * 
 * <p>The drop target effect has the same API as the 
 * <code>DropTargetAdapter</code> so that it can provide custom visual 
 * feedback when a <code>DropTargetEvent</code> occurs. 
 * </p>
 * 
 * <p>Classes that wish to provide their own drag under effect
 * can extend the <code>DropTargetEffect</code> and override any applicable methods 
 * in <code>DropTargetAdapter</code> to display their own drag under effect.</p>
 *
 * <p>The feedback value is either one of the FEEDBACK constants defined in 
 * class <code>DND</code> which is applicable to instances of this class, 
 * or it must be built by <em>bitwise OR</em>'ing together 
 * (that is, using the <code>int</code> "|" operator) two or more
 * of those <code>DND</code> effect constants. 
 * </p>
 * <p>
 * <dl>
 * <dt><b>Feedback:</b></dt>
 * <dd>FEEDBACK_EXPAND, FEEDBACK_INSERT_AFTER, FEEDBACK_INSERT_BEFORE, 
 * FEEDBACK_NONE, FEEDBACK_SELECT, FEEDBACK_SCROLL</dd>
 * </dl>
 * </p>
 * 
 * @see DropTargetAdapter
 * @see DropTargetEvent
 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
 * 
 * @since 3.3
 */
public class DropTargetEffect : DropTargetAdapter {
    Control control;

    /**
     * Creates a new <code>DropTargetEffect</code> to handle the drag under effect on the specified 
     * <code>Control</code>.
     * 
     * @param control the <code>Control</code> over which the user positions the cursor to drop the data
     * 
     * @exception IllegalArgumentException <ul>
     *    <li>ERROR_NULL_ARGUMENT - if the control is null</li>
     * </ul>
     */
    public this(Control control) {
        if (control is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
        this.control = control;
    }

    /**
     * Returns the Control which is registered for this DropTargetEffect.  This is the control over which the 
     * user positions the cursor to drop the data.
     *
     * @return the Control which is registered for this DropTargetEffect
     */
    public Control getControl() {
        return control;
    }
    
    /**
     * Returns the item at the given x-y coordinate in the receiver
     * or null if no such item exists. The x-y coordinate is in the
     * display relative coordinates.
     *
     * @param x the x coordinate used to locate the item
     * @param y the y coordinate used to locate the item
     * @return the item at the given x-y coordinate, or null if the coordinate is not in a selectable item
     */
    public Widget getItem(int x, int y) {
        if (cast(Table) control) {
            return getItem(cast(Table) control, x, y);
        }
        if (cast(Tree) control) {
            return getItem(cast(Tree) control, x, y);
        }           
        return null;
    }
    
    Widget getItem(Table table, int x, int y) {
        Point coordinates = new Point(x, y);
        coordinates = table.toControl(coordinates);
        TableItem item = table.getItem(coordinates);
        if (item !is null) return item;
        Rectangle area = table.getClientArea();
        int tableBottom = area.y + area.height;
        int itemCount = table.getItemCount();
        for (int i=table.getTopIndex(); i<itemCount; i++) {
            item = table.getItem(i);
            Rectangle rect = item.getBounds();
            rect.x = area.x;
            rect.width = area.width;
            if (rect.contains(coordinates)) return item;
            if (rect.y > tableBottom) break;
        }
        return null;
    }
    
    Widget getItem(Tree tree, int x, int y) {
        Point point = new Point(x, y);
        point = tree.toControl(point);
        TreeItem item = tree.getItem(point);
        if (item is null) {
            Rectangle area = tree.getClientArea();
            if (area.contains(point)) {
                int treeBottom = area.y + area.height;
                item = tree.getTopItem();
                while (item !is null) {
                    Rectangle rect = item.getBounds();
                    int itemBottom = rect.y + rect.height;
                    if (rect.y <= point.y && point.y < itemBottom) return item;
                    if (itemBottom > treeBottom) break;
                    item = nextItem(tree, item);
                }
                return null;
            }
        }
        return item;
    }
    
    TreeItem nextItem(Tree tree, TreeItem item) {
        if (item is null) return null;
        if (item.getExpanded() && item.getItemCount() > 0) return item.getItem(0);
        TreeItem childItem = item;
        TreeItem parentItem = childItem.getParentItem();
        int index = parentItem is null ? tree.indexOf(childItem) : parentItem.indexOf(childItem);
        int count = parentItem is null ? tree.getItemCount() : parentItem.getItemCount();
        while (true) {
            if (index + 1 < count) return parentItem is null ? tree.getItem(index + 1) : parentItem.getItem(index + 1);
            if (parentItem is null) return null;
            childItem = parentItem;
            parentItem = childItem.getParentItem();
            index = parentItem is null ? tree.indexOf(childItem) : parentItem.indexOf(childItem);
            count = parentItem is null ? tree.getItemCount() : parentItem.getItemCount();
        }
    }
    
    TreeItem previousItem(Tree tree, TreeItem item) {
        if (item is null) return null;
        TreeItem childItem = item;
        TreeItem parentItem = childItem.getParentItem();
        int index = parentItem is null ? tree.indexOf(childItem) : parentItem.indexOf(childItem);
        if (index is 0) return parentItem;
        TreeItem nextItem_ = parentItem is null ? tree.getItem(index-1) : parentItem.getItem(index-1);
        int count = nextItem_.getItemCount();
        while (count > 0 && nextItem_.getExpanded()) {
            nextItem_ = nextItem_.getItem(count - 1);
            count = nextItem_.getItemCount();
        }
        return nextItem_;
    }
}