FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

D-fying the SWT port, like DWT is

 
Post new topic   Reply to topic     Forum Index -> Tioport
View previous topic :: View next topic  
Author Message
keinfarbton



Joined: 03 Dec 2005
Posts: 224
Location: Stuttgart - Germany

PostPosted: Thu May 03, 2007 5:28 pm    Post subject: D-fying the SWT port, like DWT is Reply with quote

I want to make the SWT port more usable for D.

First step was to generate the helper methods for all method having string and/or array args/retvals.

Second step is, to add hand written code, e.g. to implement the "handleEvent" methods like in DWT, were one can use D delegates.

I will now describe, how i want to integrate those handwritten code into the generated code. Hopefully this is a "stable" process.

There are two trees of files.
1. the gen tree with the generated output.
2. the mix tree with the code that shall be included into the gen tree.

The generator searches for files in the mix tree, that have the same name and package. For example, if it processes gen/a/B.d it looks if a file mix/a/B.d exists. If it exists, it will be read.

The mix file has separator line with this syntax
//TioPortMixin identifier
identifier is either "module", of a class name. The class name can be the main class in the module (e.g. "Control") or a nested class (e.g. Control.ListenerImpl).

Each separator starts a section of text that will be included into the generated code. the module section is place after the generated imports. The other section are placed immediatly at the beginning of a class body.

The makes it possible to add methods, global functions and imports.

To change the implementation of a method, the generator has the possibility to rename methods. The mix code can now reimplement the method, and/or call the original method.

The developement of such mixed code is pretty complex, because a full regeneration of the whole port is necessary to have the code mixed in.

To make it easier, i wrote a script, that make the way in the reverse direction.
Now modify the existing port sources and add the separators into the sources. The script extract the code, and writes the mix tree.

With this, it should be possible to have a stable process to apply changes on the generated code, that even works if the generated output will change the indention, comments, or even hopefulle, when a new SWT version is generated.

Now, why this whole story??

I want YOU to contribute. I want help from DWT users, that know the DWT, and know how to implement the handleEvent mechanism.

Please go into the generated SWT code and

Code:
.... generated code ...
import bla.bla.bla;

//TioPortMixin module
import the.new.module;
//TioPortMixinEnd

class Control : Widget {
    //TioPortMixin Control
    public void handleEvent( Event e ){
    }
    //TioPortMixinEnd
    .... generated code ...
}


Please feel free to contact me.
Back to top
View user's profile Send private message
bobef



Joined: 05 Jun 2005
Posts: 269

PostPosted: Fri May 04, 2007 12:45 am    Post subject: Reply with quote

I will try to implement the handEvent methods and will give you feedback later.
Back to top
View user's profile Send private message
bobef



Joined: 05 Jun 2005
Posts: 269

PostPosted: Fri May 04, 2007 2:44 am    Post subject: Reply with quote

widgets\widget.d inside the widget class:

Code:
//TioPortMixin handleEvent

   private class __handleEventListener : dejavu.lang.JObjectImpl.JObjectImpl, org.eclipse.swt.widgets.Listener.Listener
   {
      void handleEvent(Event event)
      {
         if(func)
         {
            event.cData=cData;
            func(event);
         }
      }

      void delegate(Event e) func;
      Object cData;
   }

   private __handleEventListener[void delegate(Event)][int] __handleEventListeners;

   public int handleEvent(Object customData, int eventType, void delegate(Event) func )
   {
      if(!func) return 0;
      auto listener=new __handleEventListener;
      listener.cData=customData;
      listener.func=func;
      __handleEventListeners[eventType][func]=listener;
      addListener(eventType,listener);
      return 1;
    }

   public int unhandleEvent(int eventType, void delegate(Event) func)
   {
      if(!func) return 0;
      auto a=eventType in __handleEventListeners;
      if(!a) return 0;
      auto b=func in *a;
      if(!b) return 0;
      removeListener(eventType,*b);
      (*a).remove(func);
      if(!a.length) __handleEventListeners.remove(eventType);
      return 1;
   }

   public int handleEvent(int eventType,void delegate(Event) func){return handleEvent(null,eventType,func);}

    //TioPortMixinEnd



widgets\event.d inside the event class:

Code:
//TioPortMixin cData_for_handleEvent
   public Object cData;
   //TioPortMixinEnd



I follow the readme, then copy the contents of "lib" in the same dir as asd.d, then use this command to build it:
rebuild asd SDD-org-eclipse-swt.lib

asd.d - used for testing:

Code:
module asd;

import org.eclipse.swt.All;
import dejavu.lang.String;

import org.eclipse.swt.StaticCtorsSwt;

import tango.io.Stdout;

pragma(lib, "swt-awt-win32-3235.lib");
pragma(lib, "swt-gdip-win32-3235.lib");
pragma(lib, "swt-wgl-win32-3235.lib");
pragma(lib, "swt-win32-3235.lib");

// (1) dummy function to access compiled-in resources (here we do not use this feature)
extern(C) ubyte[] resources_getDataById( char[] aId ){
    return null;
}

int msgBox(char[] text,char[] title="Information",int styles=SWT.ICON_INFORMATION|SWT.OK)
{
   MessageBox m=new MessageBox(Display.getCurrent().getActiveShell(),styles);
   m.dh_setText(title);
   m.dh_setMessage(text);
   return m.open();
}

void main(char[][] args) {
    // (2) initialize SWT
    org.eclipse.swt.StaticCtorsSwt.callAllStaticCtors();

    Display display = new Display();
    Shell shell = new Shell(display);

    shell.dh_setText("Hello, world!"); // (3)
   shell.setLayout(new FillLayout());
   shell.setSize(320,240);

   auto b1=new Button(shell,SWT.PUSH);
   b1.dh_setText("button1");
   void onb1(Event e)
   {
      auto bb=cast(Button)e.cData;
      if(bb)
      {
         Stdout.formatln( "Selected1 {} - removing listener", bb );
         bb.dh_setText("Selection1");
         bb.unhandleEvent(SWT.Selection,&onb1);
      }
   }
   void onb2(Event e)
   {
      auto bb=cast(Button)e.cData;
      if(bb)
      {
         Stdout.formatln( "Selected2 {}", bb );
         bb.dh_setText("Selection2");
      }
   }
   b1.handleEvent(b1,SWT.Selection,&onb1);
   b1.handleEvent(b1,SWT.Selection,&onb2);
   b1.handleEvent(SWT.MouseEnter,delegate(Event)
   {
      Stdout.formatln( "mouse in" );
   });
   b1.handleEvent(SWT.MouseExit,delegate(Event)
   {
      Stdout.formatln( "mouse out" );
   });
   shell.handleEvent(SWT.Close,delegate(Event)
   {
      msgBox("shell closing");
   });

    shell.open();

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }

    display.dispose();
}


And it works. Enjoy.

Put in public domain blah blah stuff goes here... hahaha
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Tioport All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group