# HG changeset patch # User Jordan Miner # Date 1247710940 18000 # Node ID ccc108b25a0ae07b41fb4823c2cf4173f56ca96f # Parent 682fa50ab831af88e1b11905ad8155776fd38aff Convert to using a struct for events. Fix some comments too. diff -r 682fa50ab831 -r ccc108b25a0a dynamin/core/event.d --- a/dynamin/core/event.d Wed Jul 15 14:07:36 2009 -0500 +++ b/dynamin/core/event.d Wed Jul 15 21:22:20 2009 -0500 @@ -31,17 +31,17 @@ public import tango.core.Traits; /** - * A class used to notify handlers of an event. It is similar to .NET's events. + * A struct used to notify handlers of an event. It is similar to .NET's events. * Here is an example of its usage: * ----- * class Control { * public { - * Event!(PaintingEventArgs) painting; * protected void whenPainting(PaintingEventArgs e) { * /+ painting code goes here +/ * } + * Event!(whenPainting) painting; * this() { - * painting = new Event!(PaintingEventArgs)(&whenPainting); + * painting.mainHandler = &whenPainting; * } * } * } @@ -62,49 +62,41 @@ * When the event is fired, all the handlers are called first, followed * by the delegate passed into the constructor. */ -class Event(ArgsT = EventArgs) { -protected: - /// void delegate(ArgsT e) - public alias void delegate(ArgsT e) EventHandler; - /// void delegate(ArgsT e) - public alias void delegate(ArgsT e) EventDispatcher; - - EventHandler[] handlers; - EventHandler mainHandler; - EventDispatcher dispatcher; -public: +struct Event(alias mainHandler_) { + alias ParameterTupleOf!(mainHandler_)[0] ArgsType; + /// void delegate(ArgsType e) + public alias void delegate(ArgsType e) Handler; + /// void delegate(ArgsType e) + public alias void delegate(ArgsType e) Dispatcher; - /** - * Creates an event object. mainHandler is called after all - * the other handlers. - */ - this(EventHandler mainHandler) { - this.mainHandler = mainHandler; - dispatcher = &defaultDispatch; + // TODO: use a list-like struct? + Handler[] handlers; + /// + Handler mainHandler; + /// + Dispatcher dispatcher; + void defaultDispatch(ArgsType e) { + callHandlers(e); + callMainHandler(e); } - /** - * Creates an event object. mainHandler is called after all - * the other handlers. - */ - this(EventHandler mainHandler, EventDispatcher dispatcher) { - this.mainHandler = mainHandler; - if(!dispatcher.funcptr) throw new Exception("dispatcher cannot be null"); - this.dispatcher = dispatcher; - } + /** * Calls all the handlers added to this event, passing e to them. */ - void opCall(ArgsT e) { + void opCall(ArgsType e) { if(e is null) Stdout("Warning: EventArgs null").newline; - dispatcher(e); + if(!dispatcher.funcptr) + defaultDispatch(e); + else + dispatcher(e); } /** - * Adds handler to this event. handler will be called when the event is fired. + * Adds the specified handler to this event. The handler will be called + * when the event is fired. */ - void opAddAssign(EventHandler handler) { - if(!handler.funcptr) - throw new IllegalArgumentException("handler is null"); + void opAddAssign(Handler handler) { + if(!handler.funcptr) throw new Exception("handler cannot be null"); handlers.length = handlers.length + 1; handlers[length-1] = handler; // TODO: use a list? @@ -114,23 +106,23 @@ void opAddAssign(void delegate() handler) { struct Foo { void delegate() handler; - void wrapper(ArgsT e) { handler(); } + void wrapper(ArgsType e) { handler(); } } Foo* f = new Foo; f.handler = handler; - this += &f.wrapper; + *this += &f.wrapper; // I really wish D could do this: - //this += (ArgsT e) { handler(); }; + //this += (ArgsType e) { handler(); }; } /// TODO: implement this method - void opSubAssign(EventHandler handler) { - throw new Exception("Removing handlers not yet implemented"); + void opSubAssign(Handler handler) { + throw new Exception("removing handlers not yet implemented"); } /** * Calls the handlers (not including the main handler) added to this event. * Only use this method from a method that does custom dispatching. */ - void callHandlers(ArgsT e) { + void callHandlers(ArgsType e) { foreach(handler; handlers) handler(e); } @@ -139,19 +131,12 @@ * set to true. * Only use this method from a method that does custom dispatching. */ - void callMainHandler(ArgsT e) { + void callMainHandler(ArgsType e) { auto stopEventArgs = cast(StopEventArgs)e; - // if e is an instance of StopEventArgs, then check if it is canceled + // if e is an instance of StopEventArgs, then check if it is stopped if(stopEventArgs is null || !stopEventArgs.stopped) mainHandler(e); } - /** - * - */ - void defaultDispatch(ArgsT e) { - callHandlers(e); - callMainHandler(e); - } } // usage: mixin Event!(whenMoved) moved; @@ -237,11 +222,12 @@ // TODO: shorter name? class EventArgs { } +/// class StopEventArgs : EventArgs { /** - * If Stopped is set to true, then the Control will not respond to - * the event. Like if a key is typed while a text box is focused, - * but a handler sets Stopped to true, the text box will not + * If stopped is set to true, then the Control will not respond to + * the event. For instance, if a key is typed while a text box is focused, + * but a handler sets stopped to true, the text box will not * respond to the key. */ bool stopped = false; diff -r 682fa50ab831 -r ccc108b25a0a dynamin/gui/button.d --- a/dynamin/gui/button.d Wed Jul 15 14:07:36 2009 -0500 +++ b/dynamin/gui/button.d Wed Jul 15 21:22:20 2009 -0500 @@ -134,13 +134,13 @@ } public: - /// Override this method in a subclass to handle the Clicked event. + /// Override this method in a subclass to handle the clicked event. protected void whenClicked(EventArgs e) { } /// This event occurs after the button has been clicked. - Event!() clicked; + Event!(whenClicked) clicked; this() { - clicked = new Event!()(&whenClicked); + clicked.mainHandler = &whenClicked; _focusable = true; } this(string text) { diff -r 682fa50ab831 -r ccc108b25a0a dynamin/gui/check_box.d --- a/dynamin/gui/check_box.d Wed Jul 15 14:07:36 2009 -0500 +++ b/dynamin/gui/check_box.d Wed Jul 15 21:22:20 2009 -0500 @@ -58,12 +58,12 @@ } public: - /// Override this method in a subclass to handle the SelectedChanged event. + /// Override this method in a subclass to handle the checkedChanged event. protected void whenCheckedChanged(EventArgs e) { } /// This event occurs after . - Event!() checkedChanged; + Event!(whenCheckedChanged) checkedChanged; this() { - checkedChanged = new Event!()(&whenCheckedChanged); + checkedChanged.mainHandler = &whenCheckedChanged; _focusable = true; } this(string text) { diff -r 682fa50ab831 -r ccc108b25a0a dynamin/gui/container.d --- a/dynamin/gui/container.d Wed Jul 15 14:07:36 2009 -0500 +++ b/dynamin/gui/container.d Wed Jul 15 21:22:20 2009 -0500 @@ -48,16 +48,16 @@ /// Override this method in a subclass to handle the minSizeChanged event. protected void whenMinSizeChanged(EventArgs e) { } /// This event occurs after the control's minimum size has been changed. - Event!() minSizeChanged; + Event!(whenMinSizeChanged) minSizeChanged; /// Override this method in a subclass to handle the maxSizeChanged event. protected void whenMaxSizeChanged(EventArgs e) { } /// This event occurs after the control's maximum size has been changed. - Event!() maxSizeChanged; + Event!(whenMaxSizeChanged) maxSizeChanged; this() { - minSizeChanged = new Event!()(&whenMinSizeChanged); - maxSizeChanged = new Event!()(&whenMaxSizeChanged); + minSizeChanged.mainHandler = &whenMinSizeChanged; + maxSizeChanged.mainHandler = &whenMaxSizeChanged; _children = new ControlList(); elasticX = true; diff -r 682fa50ab831 -r ccc108b25a0a dynamin/gui/control.d --- a/dynamin/gui/control.d Wed Jul 15 14:07:36 2009 -0500 +++ b/dynamin/gui/control.d Wed Jul 15 21:22:20 2009 -0500 @@ -114,65 +114,65 @@ e.graphics.restore(); } - /// Override this method in a subclass to handle the Moved event. + /// Override this method in a subclass to handle the moved event. protected void whenMoved(EventArgs e) { } /// This event occurs after the control has been moved. - Event!() moved; + Event!(whenMoved) moved; - /// Override this method in a subclass to handle the Resized event. + /// Override this method in a subclass to handle the resized event. protected void whenResized(EventArgs e) { } /// This event occurs after the control has been resized. - Event!() resized; + Event!(whenResized) resized; - /// Override this method in a subclass to handle the MouseEntered event. + /// Override this method in a subclass to handle the mouseEntered event. protected void whenMouseEntered(EventArgs e) { } /// This event occurs after the mouse has entered the control. - Event!() mouseEntered; + Event!(whenMouseEntered) mouseEntered; - /// Override this method in a subclass to handle the MouseLeft event. + /// Override this method in a subclass to handle the mouseLeft event. protected void whenMouseLeft(EventArgs e) { } /// This event occurs after the mouse has left the control. - Event!() mouseLeft; + Event!(whenMouseLeft) mouseLeft; - /// Override this method in a subclass to handle the MouseDown event. + /// Override this method in a subclass to handle the mouseDown event. protected void whenMouseDown(MouseEventArgs e) { } /// This event occurs after a mouse button is pressed. - Event!(MouseEventArgs) mouseDown; + Event!(whenMouseDown) mouseDown; - /// Override this method in a subclass to handle the MouseUp event. + /// Override this method in a subclass to handle the mouseUp event. protected void whenMouseUp(MouseEventArgs e) { } /// This event occurs after a mouse button is released. - Event!(MouseEventArgs) mouseUp; + Event!(whenMouseUp) mouseUp; - /// Override this method in a subclass to handle the MouseMoved event. + /// Override this method in a subclass to handle the mouseMoved event. protected void whenMouseMoved(MouseEventArgs e) { } /// This event occurs after the mouse has been moved. - Event!(MouseEventArgs) mouseMoved; + Event!(whenMouseMoved) mouseMoved; - /// Override this method in a subclass to handle the MouseMoved event. + /// Override this method in a subclass to handle the mouseMoved event. protected void whenMouseDragged(MouseEventArgs e) { } /// This event occurs after the mouse has been moved. - Event!(MouseEventArgs) mouseDragged; + Event!(whenMouseDragged) mouseDragged; - /// Override this method in a subclass to handle the MouseTurned event. + /// Override this method in a subclass to handle the mouseTurned event. protected void whenMouseTurned(MouseTurnedEventArgs e) { } /// This event occurs after the mouse wheel has been turned. - Event!(MouseTurnedEventArgs) mouseTurned; + Event!(whenMouseTurned) mouseTurned; - /// Override this method in a subclass to handle the KeyDown event. + /// Override this method in a subclass to handle the keyDown event. protected void whenKeyDown(KeyEventArgs e) { } /// This event occurs after a key is pressed. - Event!(KeyEventArgs) keyDown; + Event!(whenKeyDown) keyDown; - /// Override this method in a subclass to handle the KeyTyped event. + /// Override this method in a subclass to handle the keyTyped event. protected void whenKeyTyped(KeyTypedEventArgs e) { } /// This event occurs after a character key is pressed. - Event!(KeyTypedEventArgs) keyTyped; + Event!(whenKeyTyped) keyTyped; - /// Override this method in a subclass to handle the KeyUp event. + /// Override this method in a subclass to handle the keyUp event. protected void whenKeyUp(KeyEventArgs e) { } /// This event occurs after a key is released. - Event!(KeyEventArgs) keyUp; + Event!(whenKeyUp) keyUp; /// Override this method in a subclass to handle the painting event. protected void whenPainting(PaintingEventArgs e) { @@ -180,22 +180,29 @@ e.graphics.paint(); } /// This event occurs when the control needs to be painted. - Event!(PaintingEventArgs) painting; + Event!(whenPainting) painting; this() { - moved = new Event!()(&whenMoved); - resized = new Event!()(&whenResized); - mouseEntered = new Event!()(&whenMouseEntered, &dispatchMouseEntered); - mouseLeft = new Event!()(&whenMouseLeft); - mouseDown = new Event!(MouseEventArgs)(&whenMouseDown, &dispatchMouseDown); - mouseUp = new Event!(MouseEventArgs)(&whenMouseUp, &dispatchMouseUp); - mouseMoved = new Event!(MouseEventArgs)(&whenMouseMoved, &dispatchMouseMoved); - mouseDragged = new Event!(MouseEventArgs)(&whenMouseDragged, &dispatchMouseDragged); - mouseTurned = new Event!(MouseTurnedEventArgs)(&whenMouseTurned, &dispatchMouseTurned); - keyDown = new Event!(KeyEventArgs)(&whenKeyDown); - keyTyped = new Event!(KeyTypedEventArgs)(&whenKeyTyped); - keyUp = new Event!(KeyEventArgs)(&whenKeyUp); - painting = new Event!(PaintingEventArgs)(&whenPainting, &dispatchPainting); + moved.mainHandler = &whenMoved; + resized.mainHandler = &whenResized; + mouseEntered.mainHandler = &whenMouseEntered; + mouseEntered.dispatcher = &dispatchMouseEntered; + mouseLeft.mainHandler = &whenMouseLeft; + mouseDown.mainHandler = &whenMouseDown; + mouseDown.dispatcher = &dispatchMouseDown; + mouseUp.mainHandler = &whenMouseUp; + mouseUp.dispatcher = &dispatchMouseUp; + mouseMoved.mainHandler = &whenMouseMoved; + mouseMoved.dispatcher = &dispatchMouseMoved; + mouseDragged.mainHandler = &whenMouseDragged; + mouseDragged.dispatcher = &dispatchMouseDragged; + mouseTurned.mainHandler = &whenMouseTurned; + mouseTurned.dispatcher = &dispatchMouseTurned; + keyDown.mainHandler = &whenKeyDown; + keyTyped.mainHandler = &whenKeyTyped; + keyUp.mainHandler = &whenKeyUp; + painting.mainHandler = &whenPainting; + painting.dispatcher = &dispatchPainting; _location = Point(0, 0); _size = Size(100, 100); diff -r 682fa50ab831 -r ccc108b25a0a dynamin/gui/list_box.d --- a/dynamin/gui/list_box.d Wed Jul 15 14:07:36 2009 -0500 +++ b/dynamin/gui/list_box.d Wed Jul 15 21:22:20 2009 -0500 @@ -78,10 +78,10 @@ } public: - /// Override this method in a subclass to handle the SelectionChanged event. + /// Override this method in a subclass to handle the selectionChanged event. protected void whenSelectionChanged(EventArgs e) { } /// This event occurs after the selection has changed. - Event!() selectionChanged; + Event!(whenSelectionChanged) selectionChanged; void listItemsChanged() { super.layout(); @@ -90,7 +90,7 @@ /// this() { - selectionChanged = new Event!()(&whenSelectionChanged); + selectionChanged.mainHandler = &whenSelectionChanged; _items = new List!(string)(&listItemsChanged); super(); diff -r 682fa50ab831 -r ccc108b25a0a dynamin/gui/notebook.d --- a/dynamin/gui/notebook.d Wed Jul 15 14:07:36 2009 -0500 +++ b/dynamin/gui/notebook.d Wed Jul 15 21:22:20 2009 -0500 @@ -99,7 +99,7 @@ layout(); } public: - /// Override this method in a subclass to handle the SelectionChanged event. + /// Override this method in a subclass to handle the selectionChanged event. protected void whenSelectionChanged(EventArgs e) { if(_content !is null) _children.remove(_content); @@ -111,10 +111,10 @@ layout(); } /// This event occurs after a different tab is selected. - Event!() selectionChanged; + Event!(whenSelectionChanged) selectionChanged; this() { - selectionChanged = new Event!()(&whenSelectionChanged); + selectionChanged.mainHandler = &whenSelectionChanged; _tabPages = new List!(TabPage)(&whenTabPagesChanged); _focusable = true; diff -r 682fa50ab831 -r ccc108b25a0a dynamin/gui/scroll_bar.d --- a/dynamin/gui/scroll_bar.d Wed Jul 15 14:07:36 2009 -0500 +++ b/dynamin/gui/scroll_bar.d Wed Jul 15 21:22:20 2009 -0500 @@ -69,7 +69,7 @@ // stores the location of the thumb as a percentage of the track real _thumbPos; this() { - valueChanged = new Event!()(&whenValueChanged); + valueChanged.mainHandler = &whenValueChanged; _track1 = new ScrollBarTrack; _track2 = new ScrollBarTrack; @@ -166,7 +166,7 @@ /// Override this method in a subclass to handle the ValueChanged event. protected void whenValueChanged(EventArgs e) { } /// This event occurs after Value has been changed. - Event!() valueChanged; + Event!(whenValueChanged) valueChanged; override Size bestSize() { if(cast(VScrollBar)this) diff -r 682fa50ab831 -r ccc108b25a0a dynamin/gui/text_box.d --- a/dynamin/gui/text_box.d Wed Jul 15 14:07:36 2009 -0500 +++ b/dynamin/gui/text_box.d Wed Jul 15 21:22:20 2009 -0500 @@ -107,13 +107,13 @@ } override int baseline() { return 14; } - /// Override this method in a subclass to handle the SelectionChanged event. + /// Override this method in a subclass to handle the selectionChanged event. protected void whenSelectionChanged(EventArgs e) { } /// This event occurs after the selection has changed. - Event!() SelectionChanged; + Event!(whenSelectionChanged) selectionChanged; this() { - SelectionChanged = new Event!()(&whenSelectionChanged); + selectionChanged.mainHandler = &whenSelectionChanged; super(); _focusable = true;