udo.killermann wrote:Board,
is there a way to create my own delegates (or btw: events) for my own classes? Or is this only possible if I use ObjC?
Regards
Udo
Hello Udo
Delegates and events are just functions. The difference is that they are not directly called by your code.
If events or delegates of Cocoa are used, there is always a wrapper function hidden to the objb developer created in your code. It is needed for type transformations and so on.
The example DeclareEvent shows how to access Cocoa events directly.
- Code: Select all
Declare Class "NSBezierPath"
Declare Sub "NSBezierPath" - (void)fill
Declare Sub "NSBezierPath" + (void)fillRect:(NSRect)aRect
Declare Function "NSBezierPath" + (NSBezierPath)bezierPathWithRect:(NSRect)aRect
Declare Sub "NSColor" - (void)set
Declare Event "Scroller" - (void)mouseDown:(NSEvent *)theEvent
Event mouseDown(theEvent As NSEvent)
Alert("mouseDown", Info(theEvent))
End Event
Event DrawRect(Argument As Rect)
' do drawing here
Dim r As Rect
Dim bp As NSBezierPath
Dim colour As NSColor = BlueColor
r = Rect(10.0, 10.0, 50.0, 60.0)
bp = NSBezierPath.bezierPathWithRect(r)
' colour = BlueColor
colour.set()
bp.fill()
End Event
ObjCDeclareDelegate is here as well:
- Code: Select all
' declare delegates are valid for the entire project
Declare Delegate "NSTableView" - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
Declare Delegate "NSTableView" - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
Declare Delegate "NSTableView" - (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
IBOutlet table As NSTableView
Dim a As Array("a", "b", "c")
Delegate tableView(aTableView As NSTableView, aTableColumn As NSTableColumn, rowIndex As Integer) As id
Return a.Object(rowIndex)
End Delegate
Delegate numberOfRowsInTableView(aTableView As NSTableView) As Integer
Return a.Count()
End Delegate
Delegate tableView(aTableView As NSTableView, anObject As id, aTableColumn As NSTableColumn, rowIndex As Integer)
a.Replace(anObject, rowIndex)
End Delegate
Applying those declare statements to your own classes is possible as well.