Drawing Area example of DUI code

DUI version 00.07

Welcome
What is DUI
Screenshots
Contacts
DUI
Downloads
Windows pack
nightly
Tutorial
Diffs to GTK
Events
Class view
Class Index
Class List
OpenGL
To do
roadmap
To do
Known Problems
Links
Site pages
Links
Acknowledgments

DrawingArea.png

drawing area

/*
 * This file is part of dui.
 * 
 * dui is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 * 
 * dui is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with dui; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

module test.TestDrawingArea;

import dui.All;

private import std.math;


/**
 * This tests the DUI drawing area widget
 */
class TestDrawingArea : VBox
{

        debug(status)
    {
        int complete(){return 100;}
        char[] gtkName(){return "";}
        char[] description(){return "";}
        char[] author(){return "Antonio";}
    }

    this()
    {
        
        //debug(1)
        //{
        //  printf("instantiating TestDrawing\n");
        //}

        super(false,7);

        TestDrawing drawingArea = new TestDrawing();
        
        OptionMenu gcOptions = new OptionMenu();
        Menu menu = new Menu();
        menu.append(new MenuItem(drawingArea,"GC COPY","GCFuncCOPY"));
        menu.append(new MenuItem(drawingArea,"GC INVERT","GCFuncINVERT"));
        menu.append(new MenuItem(drawingArea,"GC XOR","GCFuncXOR"));
        menu.append(new MenuItem(drawingArea,"GC CLEAR","GCFuncCLEAR"));
        menu.append(new MenuItem(drawingArea,"GC AND","GCFuncAND"));
        menu.append(new MenuItem(drawingArea,"GC AND_REVERSE","GCFuncAND_REVERSE"));
        menu.append(new MenuItem(drawingArea,"GC AND_INVERT","GCFuncAND_INVERT"));
        menu.append(new MenuItem(drawingArea,"GC NOOP","GCFuncNOOP"));
        menu.append(new MenuItem(drawingArea,"GC OR","GCFuncOR"));
        menu.append(new MenuItem(drawingArea,"GC EQUIV","GCFuncEQUIV"));
        menu.append(new MenuItem(drawingArea,"GC OR_REVERSE","GCFuncOR_REVERSE"));
        menu.append(new MenuItem(drawingArea,"GC COPY_INVERT","GCFuncCOPY_INVERT"));
        menu.append(new MenuItem(drawingArea,"GC OR_INVERT","GCFuncOR_INVERT"));
        menu.append(new MenuItem(drawingArea,"GC NAND","GCFuncNAND"));
        menu.append(new MenuItem(drawingArea,"GC NOR","GCFuncNOR"));
        menu.append(new MenuItem(drawingArea,"GC SET","GCFuncSET"));
        gcOptions.setMenu(menu);

        packStart(drawingArea,true,true,0);
        packStart(gcOptions,false,false,0);
    }
    
    class TestDrawing : DrawingArea , 
                        ExposeListener , 
                        MapListener ,
                        MouseMotionListener , 
                        MouseButtonListener , 
                        MenuItemListener
    {

        int gcFunction = 0;
        Color paintColor;
        Color black;
    
        this()
        {
            setSizeRequest(333,333);

            paintColor = new Color(0,0,0);
            black = new Color(0,0,0);

            Dispatcher dispatcher = Dispatcher.getDispatcher();
            dispatcher.addMouseButtonListener((MouseButtonListener)this,(Widget)this);
            dispatcher.addMouseMotionListener(this,this);
            dispatcher.addExposeListener(this,this);
            dispatcher.addMapListener(this,this);
        }

        public bit mapCallback(Drawable d)
        {
            //printf("MAP CALLBACK\n");
            return false;
        }
        
        public bit unmapCallback(Drawable d)
        {
            //printf("UN-MAP CALLBACK\n");
            return false;
        }


        /**
         * This will be called from the expose event call back.
         * \bug this is called on get or loose focus - review
         */
        public bit exposeCallback(Widget widget)
        {
            //printf("testWindow.exposed ----------------------------- \n");
            drawPoins(widget.getDrawable());
            return true;
        }
        public bit noExposeCallback(Widget widget)
        {
            //printf("testWindow.noExposed ----------------------------- \n");
            return true;
        }


        public bit motionNotifyCallback(Widget widget, EventMotion event)
        {
            //printf("testWindow.mouseMoved ----------------------------- \n");
            if ( event.testState(ModifierType.BUTTON1_MASK) )
            {
                Drawable d = getDrawable();
                GC gc = d.getGC();
                gc.setForeground(paintColor);
                gc.setFunction(gcFunction);
                //d.drawPoint(event.getX(),event.getY());
                d.drawArc(true,event.getX()-2,event.getY()-2,4,4,0,64*360);
                //d.drawRectangle(true,event.getX()-2,event.getY()-2,4,4);
                gc.setForeground(black);
                gc.setFunction(GC.COPY);
            }
            return true;
        }

        public bit mouseButtonPressCallback(Widget widget, EventButton event)
        {
            //printf("testWindow.buttonPressed ----------------------------- \n");
            if ( event.button() == 1 )
            {
                Drawable d = getDrawable();
                GC gc = d.getGC();
                gc.setForeground(new Color(0,0,0));
                gc.setFunction(gcFunction);
                d.drawArc(true,event.getX()-2,event.getY()-2,4,4,0,64*360);
                //d.drawRectangle(true,event.getX()-2,event.getY()-2,4,4);
                gc.setForeground(black);
                gc.setFunction(GC.COPY);
            }
            return true;
        }

        public bit mouseButtonReleaseCallback(Widget widget, EventButton event)
        {
            //printf("testWindow.buttonReleased ----------------------------- \n");
            return true;
        }

        private void drawPoins(Drawable d)
        {
            int totalcount = 0;
            int count = 0;
            Color color = new Color();
            GC gc = d.getGC();
            int width;
            int height;
            int x =0;
            int y =0;
            width = getWidth();
            height = getHeight();
            float dx = 256.0 / width;
            float dy = 256.0 / height ;
            float xx;
            float yy;
            while ( x < width || y <height )
            {
                xx = x * dx;
                yy = y * dy;
                color.set(xx,yy,sqrt((float)(xx*xx)+(float)(yy*yy)) );
                gc.setForeground(color);
                d.drawPoint(x,y);
                x +=1;
                if  ( x > width)
                {
                    if ( y>height)
                    {
                        //y=0;
                    }
                    else
                    {
                        x = 0;
                        y+=1;
                    }
                }
                ++totalcount;
            }
            gc.setForeground(black);
            delete color;
        }

        void activateItemCallback(MenuItem menuItem, char [] action)
        {
        }
        void activateCallback(MenuItem menuItem, char [] action)
        {
            switch(action)
            {
                case "GCFuncCOPY":              gcFunction = GC.COPY;                   break;
                case "GCFuncINVERT":                gcFunction = GC.INVERT;             break;
                case "GCFuncXOR":                   gcFunction = GC.XOR;                    break;
                case "GCFuncCLEAR":             gcFunction = GC.CLEAR;              break;
                case "GCFuncAND":                   gcFunction = GC.AND;                    break;
                case "GCFuncAND_REVERSE":   gcFunction = GC.AND_REVERSE;    break;
                case "GCFuncAND_INVERT":    gcFunction = GC.AND_INVERT;     break;
                case "GCFuncNOOP":              gcFunction = GC.NOOP;                   break;
                case "GCFuncOR":                    gcFunction = GC.OR;                     break;
                case "GCFuncEQUIV":             gcFunction = GC.EQUIV;              break;
                case "GCFuncOR_REVERSE":    gcFunction = GC.OR_REVERSE;     break;
                case "GCFuncCOPY_INVERT":   gcFunction = GC.COPY_INVERT;    break;
                case "GCFuncOR_INVERT":     gcFunction = GC.OR_INVERT;      break;
                case "GCFuncNAND":              gcFunction = GC.NAND;               break;
                case "GCFuncNOR":                   gcFunction = GC.NOR;                    break;
                case "GCFuncSET":                   gcFunction = GC.SET;                    break;
                    
            }
        }

    }

}

Generated on Sun Dec 21 02:47:16 2003 for DUI by doxygen 1.3.4