Changeset 243:84629474b5ec
- Timestamp:
- 05/17/08 17:22:11
(3 months ago)
- Author:
- Frank Benoit <benoit@tionex.de>
- branch:
- default
- Message:
Add dgListener template function for the Listener class
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r72 |
r243 |
|
| 14 | 14 | |
|---|
| 15 | 15 | import dwt.widgets.Event; |
|---|
| | 16 | |
|---|
| | 17 | import tango.core.Traits; |
|---|
| | 18 | import tango.core.Tuple; |
|---|
| 16 | 19 | |
|---|
| 17 | 20 | /** |
|---|
| … | … | |
| 54 | 57 | void handleEvent (Event event); |
|---|
| 55 | 58 | } |
|---|
| | 59 | |
|---|
| | 60 | /// Helper class for the dgListener template function |
|---|
| | 61 | private class _DgListenerT(Dg,T...) : Listener { |
|---|
| | 62 | |
|---|
| | 63 | alias ParameterTupleOf!(Dg) DgArgs; |
|---|
| | 64 | static assert( is(DgArgs == Tuple!(Event,T)), |
|---|
| | 65 | "Delegate args not correct" ); |
|---|
| | 66 | |
|---|
| | 67 | Dg dg; |
|---|
| | 68 | T t; |
|---|
| | 69 | |
|---|
| | 70 | private this( Dg dg, T t ){ |
|---|
| | 71 | this.dg = dg; |
|---|
| | 72 | static if( T.length > 0 ){ |
|---|
| | 73 | this.t = t; |
|---|
| | 74 | } |
|---|
| | 75 | } |
|---|
| | 76 | |
|---|
| | 77 | void handleEvent( Event e ){ |
|---|
| | 78 | dg(e,t); |
|---|
| | 79 | } |
|---|
| | 80 | } |
|---|
| | 81 | |
|---|
| | 82 | /++ |
|---|
| | 83 | + dgListener creates a class implementing the Listener interface and delegating the call to |
|---|
| | 84 | + handleEvent to the users delegate. This template function will store also additional parameters. |
|---|
| | 85 | + |
|---|
| | 86 | + Examle of usage: |
|---|
| | 87 | + --- |
|---|
| | 88 | + void handleTextEvent (Event e, int inset ) { |
|---|
| | 89 | + // ... |
|---|
| | 90 | + } |
|---|
| | 91 | + text.addListener (DWT.FocusOut, dgListener( &handleTextEvent, inset )); |
|---|
| | 92 | + --- |
|---|
| | 93 | +/ |
|---|
| | 94 | Listener dgListener( Dg, T... )( Dg dg, T args ){ |
|---|
| | 95 | return new _DgListenerT!( Dg, T )( dg, args ); |
|---|
| | 96 | } |
|---|
| | 97 | |
|---|
| | 98 | |
|---|
| | 99 | |
|---|
| | 100 | |
|---|
| | 101 | |
|---|
| | 102 | |
|---|
| | 103 | |
|---|