view dwtexamples/controlexample/ControlExample.d @ 140:c3880d67f906

more tabs
author Frank Benoit <benoit@tionex.de>
date Mon, 21 Jan 2008 23:52:45 +0100
parents 35300367f4d4
children 9a61fc56eb56
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;

pragma( msg, " === The ControlExample is work in progress ===" );


/+
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
+/

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;
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 ***/

import jive.stacktrace;

void main(){
    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() {
        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 (TracedException t) {
                Stdout.formatln( "ups {}", t );
            }
        }
        char[] error = (resourceBundle !is null) ?
            getResourceString("error.CouldNotLoadResources") :
            "Unable to load resources"; //$NON-NLS-1$
        freeResources();
        throw new TracedException(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));
    }
}