From ce8ee9bb1fc7394acfeb5a6bf3bf90ebce260af5 Mon Sep 17 00:00:00 2001 From: sekelsenmat Date: Mon, 6 Oct 2008 13:26:48 +0000 Subject: [PATCH] Improves pascocoa and texteditor example git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@584 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- bindings/objc/clean.sh | 3 + bindings/objc/objc.pas | 2 + bindings/pascocoa/appkit/NSToolbarItem.inc | 16 +- bindings/pascocoa/appkit/appkit.pas | 2 +- .../examples/texteditor/controller.pas | 26 +-- .../pascocoa/examples/texteditor/model.pas | 9 + .../examples/texteditor/mytoolbar.pas | 44 ++++- .../examples/texteditor/texteditor.lpi | 176 ++++++++++-------- .../examples/texteditor/texteditor.pas | 2 +- .../pascocoa/examples/texteditor/view.pas | 69 +++---- 10 files changed, 191 insertions(+), 158 deletions(-) create mode 100755 bindings/objc/clean.sh diff --git a/bindings/objc/clean.sh b/bindings/objc/clean.sh new file mode 100755 index 000000000..12c82fd3a --- /dev/null +++ b/bindings/objc/clean.sh @@ -0,0 +1,3 @@ +rm *.o +rm *.ppu +rm *.bak \ No newline at end of file diff --git a/bindings/objc/objc.pas b/bindings/objc/objc.pas index 06d453b71..9efb6a293 100644 --- a/bindings/objc/objc.pas +++ b/bindings/objc/objc.pas @@ -39,6 +39,8 @@ uses ctypes, unix; {$include malloc.inc} {.$include zone.inc} +{ Extra declarations } + implementation { Macros from error.h } diff --git a/bindings/pascocoa/appkit/NSToolbarItem.inc b/bindings/pascocoa/appkit/NSToolbarItem.inc index 7352f0985..f8843a2f6 100644 --- a/bindings/pascocoa/appkit/NSToolbarItem.inc +++ b/bindings/pascocoa/appkit/NSToolbarItem.inc @@ -14,13 +14,15 @@ {$include NSText.inc} {$include NSToolbar.inc} -//APPKIT_EXTERN NSString *NSToolbarSeparatorItemIdentifier; -//APPKIT_EXTERN NSString *NSToolbarSpaceItemIdentifier; -//APPKIT_EXTERN NSString *NSToolbarFlexibleSpaceItemIdentifier; -//APPKIT_EXTERN NSString *NSToolbarShowColorsItemIdentifier; // Shows the color panel. -//APPKIT_EXTERN NSString *NSToolbarShowFontsItemIdentifier; // Shows the font panel. -//APPKIT_EXTERN NSString *NSToolbarCustomizeToolbarItemIdentifier; // Puts the current toolbar into customize mode. -//APPKIT_EXTERN NSString *NSToolbarPrintItemIdentifier; // Sends printDocument: to firstResponder, but you can change this in toolbarWillAddItem: if you need to do so. +var + NSToolbarSeparatorItemIdentifier: CFStringRef; cvar; external; + NSToolbarSpaceItemIdentifier: CFStringRef; cvar; external; + NSToolbarFlexibleSpaceItemIdentifier: CFStringRef; cvar; external; + NSToolbarShowColorsItemIdentifier: CFStringRef; cvar; external; // Shows the color panel. + NSToolbarShowFontsItemIdentifier: CFStringRef; cvar; external; // Shows the font panel. + NSToolbarCustomizeToolbarItemIdentifier: CFStringRef; cvar; external; // Puts the current toolbar into customize mode. + NSToolbarPrintItemIdentifier: CFStringRef; cvar; external; // Sends printDocument: to firstResponder, but you can change this in toolbarWillAddItem: if you need to do so. + const NSToolbarItemVisibilityPriorityStandard = 0; NSToolbarItemVisibilityPriorityLow = 1000; diff --git a/bindings/pascocoa/appkit/appkit.pas b/bindings/pascocoa/appkit/appkit.pas index 95fa5777a..8e0fd9444 100644 --- a/bindings/pascocoa/appkit/appkit.pas +++ b/bindings/pascocoa/appkit/appkit.pas @@ -9,7 +9,7 @@ unit appkit; {$ifdef fpc} - {$mode delphi} + {$mode objfpc} {$packrecords c} {$endif} diff --git a/bindings/pascocoa/examples/texteditor/controller.pas b/bindings/pascocoa/examples/texteditor/controller.pas index 85f3db776..71b16e831 100644 --- a/bindings/pascocoa/examples/texteditor/controller.pas +++ b/bindings/pascocoa/examples/texteditor/controller.pas @@ -1,6 +1,8 @@ { controller.pas + Controller class for the texteditor example. + This example project is released under public domain AUTHORS: Felipe Monteiro de Carvalho @@ -24,11 +26,11 @@ type constructor Create; override; procedure AddMethods; override; { Objective-c Methods } - class procedure doClose(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; //static; - class procedure doOpenFile(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; //static; - class procedure doSaveFile(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl; //static; + 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 function applicationShouldTerminateAfterLastWindowClosed(_self: objc.id; - _cmd: SEL; theApplication: objc.id): cbool; cdecl; //static; + _cmd: SEL; theApplication: objc.id): cbool; cdecl; {$ifndef VER2_2_2}static;{$endif} end; const @@ -93,13 +95,13 @@ var begin { Show dialog } - if SavePanel.runModal = NSFileHandlingPanelOKButton then + if OpenPanel.runModal = NSFileHandlingPanelOKButton then begin - { Now move the contents of the file to the edit control } + { Now move the contents of the edit control to the file } - FileContents := NSString.initWithContentsOfFile(OpenPanel.filename); + FileContents := NSString.CreateWithHandle(objc.id(myView.TextField.StringValue)); - FileContents.writeToFile_atomically(SavePanel.filename, False); + myView.TextField.setStringValue(CFStringRef(FileContents.Handle)); end; end; @@ -110,13 +112,13 @@ var begin { Show dialog } - if OpenPanel.runModal = NSFileHandlingPanelOKButton then + if SavePanel.runModal = NSFileHandlingPanelOKButton then begin - { Now move the contents of the edit control to the file } + { Now move the contents of the file to the edit control } - FileContents := NSString.CreateWithHandle(objc.id(myView.TextField.StringValue)); + FileContents := NSString.initWithContentsOfFile(OpenPanel.filename); - myView.TextField.setStringValue(CFStringRef(FileContents.Handle)); + FileContents.writeToFile_atomically(SavePanel.filename, False); end; end; diff --git a/bindings/pascocoa/examples/texteditor/model.pas b/bindings/pascocoa/examples/texteditor/model.pas index 640c81b51..8f02f821e 100644 --- a/bindings/pascocoa/examples/texteditor/model.pas +++ b/bindings/pascocoa/examples/texteditor/model.pas @@ -1,3 +1,12 @@ +{ + model.pas + + Model class for the texteditor class. Holds and manages resource files and user data. + + This example project is released under public domain + + AUTHORS: Felipe Monteiro de Carvalho +} unit model; {$mode objfpc}{$H+} diff --git a/bindings/pascocoa/examples/texteditor/mytoolbar.pas b/bindings/pascocoa/examples/texteditor/mytoolbar.pas index b440ad305..267dfde4c 100644 --- a/bindings/pascocoa/examples/texteditor/mytoolbar.pas +++ b/bindings/pascocoa/examples/texteditor/mytoolbar.pas @@ -1,3 +1,12 @@ +{ + mytoolbar.pas + + Toolbar controller class. Creates and manages the toolbar. + + This example project is released under public domain + + AUTHORS: Felipe Monteiro de Carvalho +} unit mytoolbar; {$mode delphi}{$STATIC ON} @@ -17,14 +26,15 @@ type { Extra binding functions } constructor Create; override; procedure AddMethods; override; + { Public routines } procedure AttachEventHandlers; { Toolbar items } OpenToolbarItem, SaveToolbarItem, CloseToolbarItem: NSToolbarItem; { Objective-c Methods } class function toolbarAllowedItemIdentifiers(_self: objc.id; - _cmd: SEL; toolbar: objc.id {NSToolbar}): CFArrayRef; cdecl;// static; + _cmd: SEL; toolbar: objc.id {NSToolbar}): CFArrayRef; cdecl; {$ifndef VER2_2_2}static;{$endif} class function toolbarDefaultItemIdentifiers(_self: objc.id; - _cmd: SEL; toolbar: objc.id {NSToolbar}): CFArrayRef; cdecl;// static; + _cmd: SEL; toolbar: objc.id {NSToolbar}): CFArrayRef; cdecl; {$ifndef VER2_2_2}static;{$endif} end; const @@ -38,7 +48,7 @@ var function toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar (_self: objc.id; _cmd: SEL; toolbar: objc.id; - itemIdentifier: CFStringRef; flag: OBJC_BOOL): objc.id; cdecl;// static; + itemIdentifier: CFStringRef; flag: objc.BOOL): objc.id; cdecl;// static; implementation @@ -62,15 +72,15 @@ begin { Create toolbar items } OpenToolbarItem := NSToolbarItem.initWithItemIdentifier(OpenToolbarItemIdentifier); -// [toolbarItem setLabel:@"Save"]; -// [toolbarItem setPaletteLabel:[toolbarItem label]]; -// [toolbarItem setToolTip:@"Save Your Passwords"];} + OpenToolbarItem.setToolTip(CFStringCreateWithPascalString(nil, 'Open', kCFStringEncodingUTF8)); OpenToolbarItem.setImage(myModel.imgOpen.Handle); SaveToolbarItem := NSToolbarItem.initWithItemIdentifier(SaveToolbarItemIdentifier); + SaveToolbarItem.setToolTip(CFStringCreateWithPascalString(nil, 'Save', kCFStringEncodingUTF8)); SaveToolbarItem.setImage(myModel.imgSave.Handle); CloseToolbarItem := NSToolbarItem.initWithItemIdentifier(CloseToolbarItemIdentifier); + CloseToolbarItem.setToolTip(CFStringCreateWithPascalString(nil, 'Exit', kCFStringEncodingUTF8)); CloseToolbarItem.setImage(myModel.imgClose.Handle); end; @@ -94,15 +104,19 @@ begin 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. +} class function TMyToolbarController.toolbarAllowedItemIdentifiers(_self: objc.id; _cmd: SEL; toolbar: objc.id): CFArrayRef; cdecl; var toolbarItems: array[0..4] of CFStringRef; begin - WriteLn('OpenToolbarItemIdentifier: ', IntToHex(Cardinal(OpenToolbarItemIdentifier), 8)); +{ WriteLn('OpenToolbarItemIdentifier: ', IntToHex(Cardinal(OpenToolbarItemIdentifier), 8)); WriteLn('SaveToolbarItemIdentifier: ', IntToHex(Cardinal(SaveToolbarItemIdentifier), 8)); WriteLn('NSToolbarSpaceItemIdentifier: ', IntToHex(Cardinal(NSToolbarSpaceItemIdentifier), 8)); - WriteLn('CloseToolbarItemIdentifier: ', IntToHex(Cardinal(CloseToolbarItemIdentifier), 8)); + WriteLn('CloseToolbarItemIdentifier: ', IntToHex(Cardinal(CloseToolbarItemIdentifier), 8));} toolbarItems[0] := OpenToolbarItemIdentifier; toolbarItems[1] := SaveToolbarItemIdentifier; toolbarItems[2] := NSToolbarSpaceItemIdentifier; @@ -112,17 +126,27 @@ begin Result := CFArrayCreate(nil, @toolbarItems[0], 4, nil); end; +{@@ + Returns which items are available by default in the toolbar. + Other items exist in the toolbar but are optional. + We simply return the same items from toolbarAllowedItemIdentifiers. +} class function TMyToolbarController.toolbarDefaultItemIdentifiers(_self: objc.id; _cmd: SEL; toolbar: objc.id): CFArrayRef; cdecl; begin Result := toolbarAllowedItemIdentifiers(_self, _cmd, toolbar); end; +{@@ + Returns the NSToolbarItem for each item. + Note that we should return an item in our cache instead of creating a new one + everytime this routine is called. +} function toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar(_self: objc.id; _cmd: SEL; toolbar: objc.id {NSToolbar}; itemIdentifier: CFStringRef; - flag: OBJC_BOOL): objc.id {NSToolbarItem}; cdecl; + flag: objc.BOOL): objc.id {NSToolbarItem}; cdecl; begin - WriteLn('toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar: ', IntToHex(Cardinal(itemIdentifier), 8)); +// WriteLn('toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar: ', IntToHex(Cardinal(itemIdentifier), 8)); with myToolbarController do begin diff --git a/bindings/pascocoa/examples/texteditor/texteditor.lpi b/bindings/pascocoa/examples/texteditor/texteditor.lpi index a5b55f9ee..bb02a37a8 100644 --- a/bindings/pascocoa/examples/texteditor/texteditor.lpi +++ b/bindings/pascocoa/examples/texteditor/texteditor.lpi @@ -6,7 +6,7 @@ - + @@ -30,211 +30,229 @@ - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - - - + + + + - + - - - + - + - + - - - + + + + + + + + + + + + + + + + + + + + + + + - + - + - + - + - + - + - + - + - + - - + + - - + + - + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - + + + + + + + + + + + + diff --git a/bindings/pascocoa/examples/texteditor/texteditor.pas b/bindings/pascocoa/examples/texteditor/texteditor.pas index ff83f5a23..48cfe9edb 100644 --- a/bindings/pascocoa/examples/texteditor/texteditor.pas +++ b/bindings/pascocoa/examples/texteditor/texteditor.pas @@ -49,7 +49,7 @@ begin myController := TMyController.Create(); myView.AttachEventHandlers(); - myToolbarController.AttachEventHandlers(); + myToolbarController.AttachEventHandlers(); // Created in myView.CreateUserInterface { Enters main message loop } diff --git a/bindings/pascocoa/examples/texteditor/view.pas b/bindings/pascocoa/examples/texteditor/view.pas index 829f6efb7..167f7d1b3 100644 --- a/bindings/pascocoa/examples/texteditor/view.pas +++ b/bindings/pascocoa/examples/texteditor/view.pas @@ -1,3 +1,12 @@ +{ + view.pas + + View class of the texteditor example. Creates the user interface. + + This example project is released under public domain + + AUTHORS: Felipe Monteiro de Carvalho +} unit view; {$mode objfpc}{$H+} @@ -87,6 +96,8 @@ begin CreateMainMenu(); end; +{@@ +} procedure TMyView.AttachEventHandlers(); begin OpenItem.setTarget(myController.Handle); @@ -99,6 +110,8 @@ begin ExitItem.setAction(sel_registerName(PChar('doClose:'))); end; +{@@ +} function TMyView.CreateAppleMenu(): NSMenu; var AppleMenuTitle, ServicesMenuTitle: CFStringRef; @@ -114,56 +127,10 @@ begin ServicesMenu := NSMenu.initWithTitle(ServicesMenuTitle); NSApp.setServicesMenu(ServicesMenu.Handle); - -{ - NSMenuItem *menuitem; - // Create the application (Apple) menu. - NSMenu *menuApp = [[NSMenu alloc] initWithTitle: @""]; - - NSMenu *menuServices = [[NSMenu alloc] initWithTitle: @"Services"]; - [NSApp setServicesMenu:menuServices]; - - menuitem = [[NSMenuItem alloc] initWithTitle:@"Preferences..." action:nil keyEquivalent:@","]; - [menuApp addItem: menuitem]; - SetPreferencesMenuItem(menuitem); - [menuApp addItem: [NSMenuItem separatorItem]]; - menuitem = [[NSMenuItem alloc] initWithTitle: @"Services" - action:nil keyEquivalent:@""]; - [menuitem setSubmenu:menuServices]; - [menuApp addItem: menuitem]; - [menuitem release]; - - [menuApp addItem: [NSMenuItem separatorItem]]; - menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide" - action:@selector(hide:) keyEquivalent:@""]; - [menuitem setTarget: NSApp]; - [menuApp addItem: menuitem]; - [menuitem release]; - menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide Others" - action:@selector(hideOtherApplications:) keyEquivalent:@""]; - [menuitem setTarget: NSApp]; - [menuApp addItem: menuitem]; - [menuitem release]; - menuitem = [[NSMenuItem alloc] initWithTitle:@"Show All" - action:@selector(unhideAllApplications:) keyEquivalent:@""]; - [menuitem setTarget: NSApp]; - [menuApp addItem: menuitem]; - [menuitem release]; - [menuApp addItem: [NSMenuItem separatorItem]]; - menuitem = [[NSMenuItem alloc] initWithTitle:@"Quit" - action:@selector(terminate:) keyEquivalent:@"q"]; - [menuitem setTarget: NSApp]; - [menuApp addItem: menuitem]; - SetQuitMenuItem(menuitem); - [menuitem release]; - - [NSApp setAppleMenu:menuApp]; - AddToMenubar(menuApp); - [menuApp release]; - return PR_SUCCESS; -} end; +{@@ +} function TMyView.CreateFileMenu(): NSMenu; var MenuTitle: CFStringRef; @@ -184,6 +151,8 @@ begin Result.addItem(ExitItem.Handle); end; +{@@ +} procedure TMyView.AddToMenubar(menu: NSMenu); var dummyItem: NSMenuItem; @@ -234,6 +203,9 @@ begin Result := NSMenuItem.initWithTitle_action_keyEquivalent(ItemText, nil, KeyText); end; +{@@ + Creates an autosized NSTextField +} function TMyView.CreateTextField(): NSTextField; var ClientRect: NSRect; @@ -244,6 +216,7 @@ begin ClientRect.size.height := 500.0; Result := NSTextField.initWithFrame(ClientRect); + Result.setAutoresizingMask(NSViewWidthSizable or NSViewHeightSizable); MainWindowView.addSubview(Result.Handle); end;