From 7a4abd677ec1932cecc34f9923f27cd170d1198b Mon Sep 17 00:00:00 2001 From: sekelsenmat Date: Mon, 6 Oct 2008 15:44:57 +0000 Subject: [PATCH] Improves pascocoa and texteditor example git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@586 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../examples/texteditor/controller.pas | 31 +++- .../pascocoa/examples/texteditor/model.pas | 6 + .../examples/texteditor/texteditor.lpi | 132 +++++++++--------- .../pascocoa/examples/texteditor/view.pas | 17 +++ 4 files changed, 116 insertions(+), 70 deletions(-) diff --git a/bindings/pascocoa/examples/texteditor/controller.pas b/bindings/pascocoa/examples/texteditor/controller.pas index 71b16e831..1b4d018eb 100644 --- a/bindings/pascocoa/examples/texteditor/controller.pas +++ b/bindings/pascocoa/examples/texteditor/controller.pas @@ -29,6 +29,7 @@ type class procedure doClose(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; {$ifndef VER2_2_2}static;{$endif} class procedure doOpenFile(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; {$ifndef VER2_2_2}static;{$endif} class procedure doSaveFile(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; {$ifndef VER2_2_2}static;{$endif} + class procedure doSaveAsFile(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; {$ifndef VER2_2_2}static;{$endif} class function applicationShouldTerminateAfterLastWindowClosed(_self: objc.id; _cmd: SEL; theApplication: objc.id): cbool; cdecl; {$ifndef VER2_2_2}static;{$endif} end; @@ -37,6 +38,7 @@ const Str_doClose = 'doClose:'; Str_doOpenFile = 'doOpenFile:'; Str_doSaveFile = 'doSaveFile:'; + Str_doSaveAsFile = 'doSaveAsFile:'; Str_applicationShouldTerminateAfterLastWindowClosed = 'applicationShouldTerminateAfterLastWindowClosed:'; var @@ -46,7 +48,7 @@ var implementation -uses view; +uses view, model; { TMyController } @@ -64,6 +66,7 @@ begin AddMethod(Str_doClose, 'v@:@', Pointer(doClose)); AddMethod(Str_doOpenFile, 'v@:@', Pointer(doOpenFile)); AddMethod(Str_doSaveFile, 'v@:@', Pointer(doSaveFile)); + AddMethod(Str_doSaveAsFile, 'v@:@', Pointer(doSaveAsFile)); AddMethod(Str_applicationShouldTerminateAfterLastWindowClosed, 'B@:@', Pointer(applicationShouldTerminateAfterLastWindowClosed)); end; @@ -97,9 +100,11 @@ begin if OpenPanel.runModal = NSFileHandlingPanelOKButton then begin + CFStringGetPascalString(OpenPanel.filename, @myModel.DocumentName, 255, kCFStringEncodingUTF8); + { Now move the contents of the edit control to the file } - FileContents := NSString.CreateWithHandle(objc.id(myView.TextField.StringValue)); + FileContents := NSString.initWithContentsOfFile(OpenPanel.filename); myView.TextField.setStringValue(CFStringRef(FileContents.Handle)); end; @@ -109,16 +114,34 @@ class procedure TMyController.doSaveFile(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; var FileContents: NSString; +begin + if myModel.DocumentName = Str_Untitled then doSaveAsFile(_self, _cmd, sender) + else + begin + { Now move the contents of the file to the edit control } + + FileContents := NSString.CreateWithHandle(objc.id(myView.TextField.StringValue)); + + FileContents.writeToFile_atomically(CFStringCreateWithPascalString(nil, myModel.DocumentName, kCFStringEncodingUTF8), False); + end; +end; + +class procedure TMyController.doSaveAsFile(_self: objc.id; _cmd: SEL; + sender: objc.id); cdecl; +var + FileContents: NSString; begin { Show dialog } if SavePanel.runModal = NSFileHandlingPanelOKButton then begin + CFStringGetPascalString(SavePanel.filename, @myModel.DocumentName, 255, kCFStringEncodingUTF8); + { Now move the contents of the file to the edit control } - FileContents := NSString.initWithContentsOfFile(OpenPanel.filename); + FileContents := NSString.CreateWithHandle(objc.id(myView.TextField.StringValue)); - FileContents.writeToFile_atomically(SavePanel.filename, False); + FileContents.writeToFile_atomically(CFStringCreateWithPascalString(nil, myModel.DocumentName, kCFStringEncodingUTF8), False); end; end; diff --git a/bindings/pascocoa/examples/texteditor/model.pas b/bindings/pascocoa/examples/texteditor/model.pas index 8f02f821e..ebca06a08 100644 --- a/bindings/pascocoa/examples/texteditor/model.pas +++ b/bindings/pascocoa/examples/texteditor/model.pas @@ -29,9 +29,13 @@ type { Routines and variables for program resources } imgOpen, imgSave, imgClose: NSImage; ResourcesDir: string; + DocumentName: shortstring; constructor Create; end; +const + Str_Untitled = 'Untitled /1'; + var myModel: TMyModel; @@ -74,6 +78,8 @@ begin ResourcesDir := GetResourcesDir; LoadImages; + + DocumentName := Str_Untitled; end; end. diff --git a/bindings/pascocoa/examples/texteditor/texteditor.lpi b/bindings/pascocoa/examples/texteditor/texteditor.lpi index bb02a37a8..b8297e33a 100644 --- a/bindings/pascocoa/examples/texteditor/texteditor.lpi +++ b/bindings/pascocoa/examples/texteditor/texteditor.lpi @@ -6,7 +6,7 @@ - + @@ -38,47 +38,47 @@ - + - - + + - + - - + + - + - - + + - + - - + + - + @@ -115,7 +115,7 @@ - + @@ -136,123 +136,123 @@ - + - + - + - - + + - - + + - - + + - - + + - - + + - - + + - + - + - - + + - - + + - - + + - + - - + + - - + + - - + + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + - - + + - - + + - + diff --git a/bindings/pascocoa/examples/texteditor/view.pas b/bindings/pascocoa/examples/texteditor/view.pas index 167f7d1b3..902583300 100644 --- a/bindings/pascocoa/examples/texteditor/view.pas +++ b/bindings/pascocoa/examples/texteditor/view.pas @@ -97,6 +97,7 @@ begin end; {@@ + Attaches event handlers for the menu } procedure TMyView.AttachEventHandlers(); begin @@ -106,11 +107,15 @@ begin 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 } function TMyView.CreateAppleMenu(): NSMenu; var @@ -130,6 +135,7 @@ begin end; {@@ + Creates the File submenu } function TMyView.CreateFileMenu(): NSMenu; var @@ -145,13 +151,21 @@ begin OpenItem := CreateMenuItem('Open'); Result.addItem(OpenItem.Handle); + SaveItem := CreateMenuItem('Save'); Result.addItem(SaveItem.Handle); + + SaveAsItem := CreateMenuItem('Save As'); + Result.addItem(SaveAsItem.Handle); + + Result.addItem(NSMenuItem.separatorItem.Handle); + ExitItem := CreateMenuItem('Exit'); Result.addItem(ExitItem.Handle); end; {@@ + Adds a submenu to the main menu } procedure TMyView.AddToMenubar(menu: NSMenu); var @@ -164,6 +178,7 @@ begin end; {@@ + Creates the toolbar object. Setting the items is done in the controller. } function TMyView.CreateToolbar(AOwnerView: NSView; AX, AY, AWidth, AHeight: Double): NSToolbar; @@ -174,6 +189,7 @@ begin end; {@@ + Creates the main menu } procedure TMyView.CreateMainMenu(); begin @@ -190,6 +206,7 @@ begin end; {@@ + Creates a new menu item from a title } function TMyView.CreateMenuItem(ATitle: shortstring): NSMenuItem; var