view dwtexamples/controlexample/ControlExample.d @ 4:8d49c4eb4800

Added user examples
author Frank Benoit <benoit@tionex.de>
date Sun, 10 Feb 2008 04:28:56 +0100
parents 6e0b2c96d1fd
children 1de00968e454
line wrap: on
line source

/*******************************************************************************
 * 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
 * Port to the D programming language:
 *     Frank Benoit <benoit@tionex.de>
*******************************************************************************/
module dwtexamples.controlexample.ControlExample;

import dwt.DWT;
import dwt.graphics.Image;
import dwt.graphics.ImageData;
import dwt.graphics.Point;
import dwt.graphics.Rectangle;
import dwt.layout.FillLayout;
import dwt.widgets.Composite;
import dwt.widgets.Display;
import dwt.widgets.Shell;
import dwt.widgets.TabFolder;
import dwt.widgets.TabItem;
import dwt.dwthelper.ResourceBundle;
import dwt.dwthelper.ByteArrayInputStream;

import dwtexamples.controlexample.Tab;
import dwtexamples.controlexample.ButtonTab;
import dwtexamples.controlexample.CanvasTab;
import dwtexamples.controlexample.ComboTab;
import dwtexamples.controlexample.CoolBarTab;
//import dwtexamples.controlexample.DateTimeTab;
version(linux){
    import dwtexamples.controlexample.DialogTab;
}
import dwtexamples.controlexample.ExpandBarTab;
import dwtexamples.controlexample.GroupTab;
import dwtexamples.controlexample.LabelTab;
import dwtexamples.controlexample.LinkTab;
import dwtexamples.controlexample.ListTab;
import dwtexamples.controlexample.MenuTab;
import dwtexamples.controlexample.ProgressBarTab;
import dwtexamples.controlexample.SashTab;
import dwtexamples.controlexample.ScaleTab;
import dwtexamples.controlexample.ShellTab;
import dwtexamples.controlexample.SliderTab;
import dwtexamples.controlexample.SpinnerTab;
import dwtexamples.controlexample.TabFolderTab;
import dwtexamples.controlexample.TableTab;
import dwtexamples.controlexample.TextTab;
import dwtexamples.controlexample.ToolBarTab;
import dwtexamples.controlexample.ToolTipTab;
import dwtexamples.controlexample.TreeTab;


import tango.core.Exception;
import tango.text.convert.Format;
import tango.io.Stdout;
import Math = tango.math.Math;


/*** Linker workaround start ***/
import tango.io.Stdout;
import tango.math.Math;
import tango.text.convert.Format;
import tango.util.Convert;
import tango.util.PathUtil;
/*** Linker workaround end ***/

version(JIVE){
    import jive.stacktrace;
}

version( CONTROL_EXAMPLE_MAIN ){
    void main(){
        Stdout.formatln( "The ControlExample: still work left" );
        Stdout.formatln( "todo: search for //PORTING_LEFT" );
        Stdout.formatln( "todo: Implement Get/Set API reflection" );
        Stdout.formatln( "todo: DateTimeTab not implemented" );
        Stdout.formatln( "todo: ExpandBarTab looks strange" );
        Stdout.formatln( "" );
        Stdout.formatln( "On linux GTK:" );
        Stdout.formatln( "bug:  ProgressBarTab crash on vertical" );
        Stdout.formatln( "        in java it behaves the same" );
        Stdout.formatln( "bug:  SliderTab horizontal arrow buttons are too high." );
        Stdout.formatln( "        in java it behaves the same" );
        Stdout.formatln( "        Known bug:" );
        Stdout.formatln( "        https://bugs.eclipse.org/bugs/show_bug.cgi?id=197402" );
        Stdout.formatln( "        http://bugzilla.gnome.org/show_bug.cgi?id=475909" );
        Stdout.formatln( "" );
        Stdout.formatln( "please report problems" );
        ControlExample.main( null );
    }
}

public class ControlExample {
    private static ResourceBundle resourceBundle;
    private static const char[] resourceData = import( "controlexample.properties" );

    private ShellTab shellTab;
    private TabFolder tabFolder;
    private Tab [] tabs;
    Image images[];

    static const int ciClosedFolder = 0, ciOpenFolder = 1, ciTarget = 2, ciBackground = 3, ciParentBackground = 4;

    static const byte[][] imageData = [
        cast(byte[]) import( "closedFolder.gif" ),
        cast(byte[]) import( "openFolder.gif" ),
        cast(byte[]) import( "target.gif" ),
        cast(byte[]) import( "backgroundImage.png" ),
        cast(byte[]) import( "parentBackgroundImage.png" )
    ];
    static const int[] imageTypes = [
        DWT.ICON,
        DWT.ICON,
        DWT.ICON,
        DWT.BITMAP,
        DWT.BITMAP];

    bool startup = true;

    static this(){
        resourceBundle = ResourceBundle.getBundleFromData( resourceData ); //$NON-NLS-1$
    }

    /**
     * Creates an instance of a ControlExample embedded inside
     * the supplied parent Composite.
     *
     * @param parent the container of the example
     */
    public this(Composite parent) {
        initResources();
        tabFolder = new TabFolder (parent, DWT.NONE);
        tabs = createTabs();
        for (int i=0; i<tabs.length; i++) {
            TabItem item = new TabItem (tabFolder, DWT.NONE);
            item.setText (tabs [i].getTabText ());
            item.setControl (tabs [i].createTabFolderPage (tabFolder));
            item.setData (tabs [i]);
        }
        startup = false;
    }

    /**
     * Answers the set of example Tabs
     */
    Tab[] createTabs() {
        version(Windows){
        return [ cast(Tab)
            new ButtonTab (this),
            new CanvasTab (this),
            new ComboTab (this),
            new CoolBarTab (this),
            //new DateTimeTab (this), // DateTime Control not implemented
            //new DialogTab (this),   // Dialog not implemented
            new ExpandBarTab (this),
            new GroupTab (this),
            new LabelTab (this),
            new LinkTab (this),
            new ListTab (this),
            new MenuTab (this),
            //new ProgressBarTab (this), // crash on start
            new SashTab (this),
            //new ScaleTab (this),       // crash on start
            shellTab = new ShellTab(this),
            //new SliderTab (this),      // crash on start
            //new SpinnerTab (this),     // crash on start
            new TabFolderTab (this),
            new TableTab (this),
            new TextTab (this),
            new ToolBarTab (this),
            new ToolTipTab (this),
            new TreeTab (this)
        ];
        } else { // linux
        return [ cast(Tab)
            new ButtonTab (this),
            new CanvasTab (this),
            new ComboTab (this),
            new CoolBarTab (this),
            //new DateTimeTab (this),
            new DialogTab (this),
            new ExpandBarTab (this),
            new GroupTab (this),
            new LabelTab (this),
            new LinkTab (this),
            new ListTab (this),
            new MenuTab (this),
            new ProgressBarTab (this),
            new SashTab (this),
            new ScaleTab (this),
            shellTab = new ShellTab(this),
            new SliderTab (this),
            new SpinnerTab (this),
            new TabFolderTab (this),
            new TableTab (this),
            new TextTab (this),
            new ToolBarTab (this),
            new ToolTipTab (this),
            new TreeTab (this)
        ];
        }
    }

    /**
     * Disposes of all resources associated with a particular
     * instance of the ControlExample.
     */
    public void dispose() {
        /*
         * Destroy any shells that may have been created
         * by the Shells tab.  When a shell is disposed,
         * all child shells are also disposed.  Therefore
         * it is necessary to check for disposed shells
         * in the shells list to avoid disposing a shell
         * twice.
         */
        if (shellTab !is null) shellTab.closeAllShells ();
        shellTab = null;
        tabFolder = null;
        freeResources();
    }

    /**
     * Frees the resources
     */
    void freeResources() {
        if (images !is null) {
            for (int i = 0; i < images.length; ++i) {
                final Image image = images[i];
                if (image !is null) image.dispose();
            }
            images = null;
        }
    }

    /**
     * Gets a string from the resource bundle.
     * We don't want to crash because of a missing String.
     * Returns the key if not found.
     */
    static char[] getResourceString(char[] key) {
        char[] res;
        try {
            res = resourceBundle.getString(key);
        } catch (NoSuchElementException e) {
            return key;
        }
        if( res is null ){
            return "!" ~ key ~ "!"; //$NON-NLS-1$ //$NON-NLS-2$
        }
        return res;
    }

//     /**
//      * Gets a string from the resource bundle and binds it
//      * with the given arguments. If the key is not found,
//      * return the key.
//      */
//     static char[] getResourceString(char[] key, Object[] args) {
//         char[] res;
//         try {
//             res = Format(getResourceString(key), args);
//         } catch (NoSuchElementException e) {
//             return key;
//         }
//         if( res is null ){
//             return "!" ~ key ~ "!"; //$NON-NLS-1$ //$NON-NLS-2$
//         }
//         return res;
//     }

    /**
     * Loads the resources
     */
    void initResources() {
        //final Class clazz = ControlExample.class;
        if (resourceBundle !is null) {
            try {
                if (images is null) {
                    images = new Image[imageData.length];

                    for (int i = 0; i < imageData.length; ++i) {
                        InputStream sourceStream = new ByteArrayInputStream( imageData[i] );
                        ImageData source = new ImageData(sourceStream);
                        if (imageTypes[i] is DWT.ICON) {
                            ImageData mask = source.getTransparencyMask();
                            images[i] = new Image(null, source, mask);
                        } else {
                            images[i] = new Image(null, source);
                        }
                        try {
                            sourceStream.close();
                        } catch (IOException e) {
                            Stderr.formatln( "Stacktrace: {}", e.toString );
                        }
                    }
                }
                return;
            } catch (Exception t) {
                Stdout.formatln( "ups {}", t );
            }
        }
        char[] error = (resourceBundle !is null) ?
            getResourceString("error.CouldNotLoadResources") :
            "Unable to load resources"; //$NON-NLS-1$
        freeResources();
        throw new Exception(error);
    }

    /**
     * Invokes as a standalone program.
     */
    public static void main(char[][] args) {
        Display display = new Display();
        Shell shell = new Shell(display, DWT.SHELL_TRIM);
        shell.setLayout(new FillLayout());
        ControlExample instance = new ControlExample(shell);
        shell.setText(getResourceString("window.title"));
        setShellSize(instance, shell);
        shell.open();
        while (! shell.isDisposed()) {
            if (! display.readAndDispatch()) display.sleep();
        }
        instance.dispose();
        display.dispose();
    }

    /**
     * Grabs input focus.
     */
    public void setFocus() {
        tabFolder.setFocus();
    }

    /**
     * Sets the size of the shell to it's "packed" size,
     * unless that makes it larger than the monitor it is being displayed on,
     * in which case just set the shell size to be slightly smaller than the monitor.
     */
    static void setShellSize(ControlExample instance, Shell shell) {
        Point size = shell.computeSize(DWT.DEFAULT, DWT.DEFAULT);
        Rectangle monitorArea = shell.getMonitor().getClientArea();
        /* Workaround: if the tab folder is wider than the screen,
         * carbon clips instead of somehow scrolling the tab items.
         * We try to recover some width by using shorter tab names. */
        if (size.x > monitorArea.width && DWT.getPlatform()=="carbon") {
            TabItem [] tabItems = instance.tabFolder.getItems();
            for (int i=0; i<tabItems.length; i++) {
                tabItems[i].setText (instance.tabs [i].getShortTabText ());
            }
            size = shell.computeSize(DWT.DEFAULT, DWT.DEFAULT);
        }
        shell.setSize(Math.min(size.x, monitorArea.width), Math.min(size.y, monitorArea.height));
    }
}