diff --git a/bindings/pascocoa/examples/texteditor/mytoolbar.pas b/bindings/pascocoa/examples/texteditor/mytoolbar.pas index 267dfde4c..570a1864b 100644 --- a/bindings/pascocoa/examples/texteditor/mytoolbar.pas +++ b/bindings/pascocoa/examples/texteditor/mytoolbar.pas @@ -26,8 +26,6 @@ type { Extra binding functions } constructor Create; override; procedure AddMethods; override; - { Public routines } - procedure AttachEventHandlers; { Toolbar items } OpenToolbarItem, SaveToolbarItem, CloseToolbarItem: NSToolbarItem; { Objective-c Methods } @@ -74,14 +72,20 @@ begin OpenToolbarItem := NSToolbarItem.initWithItemIdentifier(OpenToolbarItemIdentifier); OpenToolbarItem.setToolTip(CFStringCreateWithPascalString(nil, 'Open', kCFStringEncodingUTF8)); OpenToolbarItem.setImage(myModel.imgOpen.Handle); + OpenToolbarItem.setTarget(myController.Handle); + OpenToolbarItem.setAction(sel_registerName(PChar('doOpenFile:'))); SaveToolbarItem := NSToolbarItem.initWithItemIdentifier(SaveToolbarItemIdentifier); SaveToolbarItem.setToolTip(CFStringCreateWithPascalString(nil, 'Save', kCFStringEncodingUTF8)); SaveToolbarItem.setImage(myModel.imgSave.Handle); + SaveToolbarItem.setTarget(myController.Handle); + SaveToolbarItem.setAction(sel_registerName(PChar('doSaveFile:'))); CloseToolbarItem := NSToolbarItem.initWithItemIdentifier(CloseToolbarItemIdentifier); CloseToolbarItem.setToolTip(CFStringCreateWithPascalString(nil, 'Exit', kCFStringEncodingUTF8)); CloseToolbarItem.setImage(myModel.imgClose.Handle); + CloseToolbarItem.setTarget(myController.Handle); + CloseToolbarItem.setAction(sel_registerName(PChar('doClose:'))); end; procedure TMyToolbarController.AddMethods; @@ -92,18 +96,6 @@ begin '@@:@@c', @toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar); end; -procedure TMyToolbarController.AttachEventHandlers; -begin - OpenToolbarItem.setTarget(myController.Handle); - OpenToolbarItem.setAction(sel_registerName(PChar('doOpenFile:'))); - - SaveToolbarItem.setTarget(myController.Handle); - SaveToolbarItem.setAction(sel_registerName(PChar('doSaveFile:'))); - - CloseToolbarItem.setTarget(myController.Handle); - CloseToolbarItem.setAction(sel_registerName(PChar('doClose:'))); -end; - {@@ Lists all of the items in the toolbar by their identifiers. This method is necessary to implement a toolbar. diff --git a/bindings/pascocoa/examples/texteditor/texteditor.lpi b/bindings/pascocoa/examples/texteditor/texteditor.lpi index 2110de66b..4874edefc 100644 --- a/bindings/pascocoa/examples/texteditor/texteditor.lpi +++ b/bindings/pascocoa/examples/texteditor/texteditor.lpi @@ -35,10 +35,10 @@ - - + + - + @@ -47,38 +47,38 @@ - - + + - - - - + + + + - - - - + + + + - - + + - + @@ -87,207 +87,203 @@ - + - + - + + + - + - + - - + + - + - + - + - - - + - + - + - + - - - + - + - - - + + + + + - + - - + + - - + + - - + + - + - - + + - - + + - - + + - - + + - - + + - - + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - + + - - + + - - + + - - - - diff --git a/bindings/pascocoa/examples/texteditor/texteditor.pas b/bindings/pascocoa/examples/texteditor/texteditor.pas index e722452b0..13b7b3eb9 100644 --- a/bindings/pascocoa/examples/texteditor/texteditor.pas +++ b/bindings/pascocoa/examples/texteditor/texteditor.pas @@ -35,9 +35,10 @@ begin // NSBundleNib.loadNibNamed_owner(CFStringCreateWithPascalString(nil, 'applemenu', kCFStringEncodingUTF8), NSApp.Handle); - { Initializes the model object, - must be after the view so that the resources are loaded } + { Initializes the controller and the model. Must be before the view to + attach the events (controller) and guarantee that resources are loaded (model) } + myController := TMyController.Create(); myModel := TMyModel.Create(); { Initializes the view object } @@ -45,14 +46,6 @@ begin myView := TMyView.Create; myView.CreateUserInterface; - { The controller needs to be the last to be creates for an unknown reason, - and we can only attach the events after the controller is created } - - myController := TMyController.Create(); - - myView.AttachEventHandlers(); - myToolbarController.AttachEventHandlers(); // Created in myView.CreateUserInterface - { Enters main message loop } myView.MainWindow.orderFrontRegardless; diff --git a/bindings/pascocoa/examples/texteditor/view.pas b/bindings/pascocoa/examples/texteditor/view.pas index 902583300..4d7f94a0c 100644 --- a/bindings/pascocoa/examples/texteditor/view.pas +++ b/bindings/pascocoa/examples/texteditor/view.pas @@ -30,7 +30,7 @@ type function CreateAppleMenu(): NSMenu; function CreateFileMenu(): NSMenu; procedure CreateMainMenu(); - function CreateMenuItem(ATitle: shortstring): NSMenuItem; + function CreateMenuItem(ATitle: shortstring; ACallback: string; ATarget: objc.id): NSMenuItem; function CreateToolbar(AOwnerView: NSView; AX, AY, AWidth, AHeight: Double): NSToolbar; function CreateTextField(): NSTextField; @@ -47,7 +47,6 @@ type MainWindowRect: NSRect; { methods } procedure CreateUserInterface(); - procedure AttachEventHandlers(); end; var @@ -96,24 +95,6 @@ begin CreateMainMenu(); end; -{@@ - Attaches event handlers for the menu -} -procedure TMyView.AttachEventHandlers(); -begin - OpenItem.setTarget(myController.Handle); - OpenItem.setAction(sel_registerName(PChar('doOpenFile:'))); - - SaveItem.setTarget(myController.Handle); - SaveItem.setAction(sel_registerName(PChar('doSaveFile:'))); - - SaveAsItem.setTarget(myController.Handle); - SaveAsItem.setAction(sel_registerName(PChar('doSaveAsFile:'))); - - ExitItem.setTarget(myController.Handle); - ExitItem.setAction(sel_registerName(PChar('doClose:'))); -end; - {@@ Creates the Apple submenu } @@ -149,18 +130,20 @@ begin { Adds items to it } - OpenItem := CreateMenuItem('Open'); + OpenItem := CreateMenuItem('Open', 'doOpenFile:', myController.Handle); Result.addItem(OpenItem.Handle); - SaveItem := CreateMenuItem('Save'); + + SaveItem := CreateMenuItem('Save', 'doSaveFile:', myController.Handle); Result.addItem(SaveItem.Handle); - SaveAsItem := CreateMenuItem('Save As'); + + SaveAsItem := CreateMenuItem('Save As', 'doSaveAsFile:', myController.Handle); Result.addItem(SaveAsItem.Handle); Result.addItem(NSMenuItem.separatorItem.Handle); - ExitItem := CreateMenuItem('Exit'); + ExitItem := CreateMenuItem('Exit', 'doClose:', myController.Handle); Result.addItem(ExitItem.Handle); end; @@ -208,7 +191,7 @@ end; {@@ Creates a new menu item from a title } -function TMyView.CreateMenuItem(ATitle: shortstring): NSMenuItem; +function TMyView.CreateMenuItem(ATitle: shortstring; ACallback: string; ATarget: objc.id): NSMenuItem; var ItemText: CFStringRef; KeyText: CFStringRef; @@ -218,6 +201,9 @@ begin WriteLn(' ItemText: ', IntToHex(Int64(ItemText), 8), ' ATitle: ', ATitle); Result := NSMenuItem.initWithTitle_action_keyEquivalent(ItemText, nil, KeyText); + + Result.setTarget(ATarget); + Result.setAction(sel_registerName(PChar(ACallback))); end; {@@ diff --git a/bindings/pascocoa/foundation/NSObject.inc b/bindings/pascocoa/foundation/NSObject.inc index 2e62a5779..51aaba1ef 100644 --- a/bindings/pascocoa/foundation/NSObject.inc +++ b/bindings/pascocoa/foundation/NSObject.inc @@ -246,6 +246,7 @@ begin // Allocate space for the class and its metaclass // new_class := CFAllocatorAllocate(kCFAllocatorMalloc, 2 * SizeOf(objc_class), 0); + FillChar(new_class^, 2 * SizeOf(objc_class), $0); meta_class := @new_class[1]; // setup class