DWT event handling
BLS
nanali at nospam-wanadoo.fr
Sun May 18 22:17:03 PDT 2008
Bill Baxter schrieb:
> BLS wrote:
>> Frank Benoit schrieb:
>>> I added the template function Bill suggested once to the
>>> dwt.widgets.Listener module.
>>>
>>> http://www.dsource.org/projects/dwt-linux/browser/dwt/widgets/Listener.d?rev=243%3A84629474b5ec
>>>
>>>
>>> You can see that in action in this snippet
>>> http://www.dsource.org/projects/dwt-samples/browser/snippets/treeeditor/Snippet111.d?rev=85%3Afa286c85e7b8
>>>
>>>
>>> See lines: 102..106
>>>
>>> Thanks Bill for the snippets and for this cool template function
>>> suggestion.
>>>
>>> Frank
>>
>> Hi Frank, thanks for the update !
>>
>> I still try to figure out how this code could fit ...
>>
>> template MessageMap(Mappings ...) {
>> void addListener(uint uID) {
>> foreach(mapping; Mappings) {
>> if(mapping.matches(uID)) // DWTxxxx
>> mapping.executeAction();
>> }
>> }
>> }
>>
>> //maybe we need template specialisation here ???
>>
>>
>> struct OnClose(alias fn) {
>> alias fn executeAction;
>> static bool matches(uint uid) { return uid == 5; }
>> }
>>
>> struct OnRange(uint a, uint b, alias fn) {
>> alias fn executeAction;
>> static bool matches(uint uid) { return uid >= a && uid <= b; }
>> }
>>
>>
>> class Foo {
>> void foo() {
>> writefln("foo called");
>> }
>> void bar() {
>> writefln("bar called");
>> }
>>
>> mixin MessageMap!(
>> OnClose!(foo),
>> OnRange!(1, 3, bar)
>> );
>>
>> }
>>
>> void main() {
>> auto f = new Foo;
>> f.addListener(5);
>> f.addListener(2);
>> }
>>
>> At least it looks simpler (to me) Ideas ? Bjoern
>
> I don't get it. What's 5?
>
> --bb
Hi Bill,
Well it's just a short sample. The 5 represents a DWT.xxxx value f.e.
DWT.FocusOut
the class Foo expands to :
class Foo {
void foo() {
writefln("foo called");
}
void bar() {
writefln("bar called");
}
void addListener(uint uID) { //"BEGIN_MSG_MAP" <vbg>
foreach(mapping; Mappings) { //loops 2 times -> see Mixin
if(mapping.matches(uID)) //just like "if (uID == DWT.FocusOut)"
mapping.executeAction(); //call delegate respect. alias
}
}
}
The slightly modified MessageMap :
mixin MessageMap!(
OnFocusOut!(foo), /+ DWT.FocusOut calls foo() method +/
OnRange!(1, 3, bar)
);
HTH
Bjoern
More information about the Digitalmars-d-dwt
mailing list