view dwtx/jface/viewers/FocusCellOwnerDrawHighlighter.d @ 43:ea8ff534f622

Fix override and super aliases
author Frank Benoit <benoit@tionex.de>
date Fri, 11 Apr 2008 01:24:25 +0200
parents 644f1334b451
children 46a6e0e6ccd4
line wrap: on
line source

/*******************************************************************************
 * 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:
 *     Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
 *                                               - fix for bug 183850, 182652
 *     IBM Corporation - initial API and implementation
 * Port to the D programming language:
 *     Frank Benoit <benoit@tionex.de>
 ******************************************************************************/

module dwtx.jface.viewers.FocusCellOwnerDrawHighlighter;

import dwtx.jface.viewers.FocusCellHighlighter;
import dwtx.jface.viewers.ViewerCell;
import dwtx.jface.viewers.ViewerRow;
import dwtx.jface.viewers.ColumnViewer;

import dwt.DWT;
import dwt.graphics.Color;
import dwt.graphics.GC;
import dwt.graphics.Rectangle;
import dwt.widgets.Event;
import dwt.widgets.Listener;
import dwtx.core.runtime.Assert;

import dwt.dwthelper.utils;

/**
 * @since 3.3
 *
 */
public class FocusCellOwnerDrawHighlighter : FocusCellHighlighter {

    private ViewerCell oldCell;

    // Needed to work-around problem in bug 183850
    private static const bool WIN_32;

    static this(){
        WIN_32 = "win32".equals(DWT.getPlatform()); //$NON-NLS-1$
    }

    /**
     * @param viewer
     *            the viewer
     */
    public this(ColumnViewer viewer) {
        super(viewer);
        hookListener(viewer);
    }

    private void markFocusedCell(Event event, ViewerCell cell) {
        Color background = getSelectedCellBackgroundColor(cell);
        Color foreground = getSelectedCellForegroundColor(cell);

        if ( WIN_32 || foreground !is null || background !is null) {
            GC gc = event.gc;

            if (background is null) {
                background = cell.getItem().getDisplay().getSystemColor(
                        DWT.COLOR_LIST_SELECTION);
            }

            if (foreground is null) {
                foreground = cell.getItem().getDisplay().getSystemColor(
                        DWT.COLOR_LIST_SELECTION_TEXT);
            }

            gc.setBackground(background);
            gc.setForeground(foreground);
            gc.fillRectangle(event.getBounds());

            // This is a workaround for an DWT-Bug on WinXP bug 169517
            gc.drawText(" ", cell.getBounds().x, cell.getBounds().y, false); //$NON-NLS-1$
            event.detail &= ~DWT.SELECTED;
        }
    }

    private void removeSelectionInformation(Event event, ViewerCell cell) {
        GC gc = event.gc;
        gc.setBackground(cell.getViewerRow().getBackground(
                cell.getColumnIndex()));
        gc.setForeground(cell.getViewerRow().getForeground(
                cell.getColumnIndex()));
        gc.fillRectangle(cell.getBounds());
        // This is a workaround for an DWT-Bug on WinXP bug 169517
        gc.drawText(" ", cell.getBounds().x, cell.getBounds().y, false); //$NON-NLS-1$
        event.detail &= ~DWT.SELECTED;
    }

    private void hookListener(ColumnViewer viewer) {

        Listener listener = new class(viewer) Listener {
            ColumnViewer viewer_;
            this(ColumnViewer a){
                viewer_ = a;
            }
            public void handleEvent(Event event) {
                if ((event.detail & DWT.SELECTED) > 0) {
                    ViewerCell focusCell = getFocusCell();
                    ViewerRow row = viewer_.getViewerRowFromItem_package(event.item);

                    Assert.isNotNull(row,
                        "Internal structure invalid. Item without associated row is not possible."); //$NON-NLS-1$

                    ViewerCell cell = row.getCell(event.index);

                    if (focusCell is null || !cell.opEquals(focusCell)) {
                        removeSelectionInformation(event, cell);
                    } else {
                        markFocusedCell(event, cell);
                    }
                }
            }

        };
        viewer.getControl().addListener(DWT.EraseItem, listener);
    }

    /**
     * @param cell
     *            the cell which is colored
     * @return the color
     */
    protected Color getSelectedCellBackgroundColor(ViewerCell cell) {
        return null;
    }

    /**
     * @param cell
     *            the cell which is colored
     * @return the color
     */
    protected Color getSelectedCellForegroundColor(ViewerCell cell) {
        return null;
    }

    /*
     * (non-Javadoc)
     *
     * @see dwtx.jface.viewers.FocusCellHighlighter#focusCellChanged(dwtx.jface.viewers.ViewerCell)
     */
    protected override void focusCellChanged(ViewerCell cell) {
        super.focusCellChanged(cell);

        // Redraw new area
        if (cell !is null) {
            Rectangle rect = cell.getBounds();
            int x = cell.getColumnIndex() is 0 ? 0 : rect.x;
            int width = cell.getColumnIndex() is 0 ? rect.x + rect.width
                    : rect.width;
            // 1 is a fix for Linux-GTK
            cell.getControl().redraw(x, rect.y-1, width, rect.height+1, true);
        }

        if (oldCell !is null) {
            Rectangle rect = oldCell.getBounds();
            int x = oldCell.getColumnIndex() is 0 ? 0 : rect.x;
            int width = oldCell.getColumnIndex() is 0 ? rect.x + rect.width
                    : rect.width;
            // 1 is a fix for Linux-GTK
            oldCell.getControl().redraw(x, rect.y-1, width, rect.height+1, true);
        }

        this.oldCell = cell;
    }
}