Fixes an uninitialized memory bug in PasCocoa

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@588 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
sekelsenmat 2008-10-08 11:28:15 +00:00
parent 630333ac37
commit 397ae7a036
5 changed files with 132 additions and 164 deletions

View File

@ -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.

View File

@ -35,10 +35,10 @@
<Filename Value="texteditor.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="texteditor"/>
<CursorPos X="80" Y="36"/>
<TopLine Value="26"/>
<CursorPos X="31" Y="7"/>
<TopLine Value="43"/>
<EditorIndex Value="0"/>
<UsageCount Value="40"/>
<UsageCount Value="62"/>
<Loaded Value="True"/>
</Unit0>
<Unit1>
@ -47,38 +47,38 @@
<UnitName Value="model"/>
<CursorPos X="24" Y="32"/>
<TopLine Value="27"/>
<EditorIndex Value="6"/>
<UsageCount Value="40"/>
<EditorIndex Value="5"/>
<UsageCount Value="62"/>
<Loaded Value="True"/>
</Unit1>
<Unit2>
<Filename Value="view.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="view"/>
<CursorPos X="49" Y="158"/>
<TopLine Value="146"/>
<EditorIndex Value="8"/>
<UsageCount Value="40"/>
<CursorPos X="49" Y="40"/>
<TopLine Value="72"/>
<EditorIndex Value="7"/>
<UsageCount Value="62"/>
<Loaded Value="True"/>
</Unit2>
<Unit3>
<Filename Value="controller.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="controller"/>
<CursorPos X="1" Y="145"/>
<TopLine Value="130"/>
<EditorIndex Value="7"/>
<UsageCount Value="40"/>
<CursorPos X="21" Y="30"/>
<TopLine Value="20"/>
<EditorIndex Value="6"/>
<UsageCount Value="62"/>
<Loaded Value="True"/>
</Unit3>
<Unit4>
<Filename Value="mytoolbar.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="mytoolbar"/>
<CursorPos X="99" Y="72"/>
<TopLine Value="51"/>
<CursorPos X="1" Y="77"/>
<TopLine Value="75"/>
<EditorIndex Value="2"/>
<UsageCount Value="35"/>
<UsageCount Value="57"/>
<Loaded Value="True"/>
</Unit4>
<Unit5>
@ -87,207 +87,203 @@
<CursorPos X="50" Y="3"/>
<TopLine Value="27"/>
<EditorIndex Value="3"/>
<UsageCount Value="16"/>
<UsageCount Value="27"/>
<Loaded Value="True"/>
</Unit5>
<Unit6>
<Filename Value="../../foundation/foundation.pas"/>
<UnitName Value="foundation"/>
<CursorPos X="30" Y="8"/>
<CursorPos X="30" Y="12"/>
<TopLine Value="1"/>
<UsageCount Value="11"/>
<EditorIndex Value="8"/>
<UsageCount Value="22"/>
<Loaded Value="True"/>
</Unit6>
<Unit7>
<Filename Value="../../appkit/NSToolbarItem.inc"/>
<CursorPos X="49" Y="5"/>
<TopLine Value="4"/>
<UsageCount Value="9"/>
<UsageCount Value="7"/>
</Unit7>
<Unit8>
<Filename Value="../../appkit/appkit.pas"/>
<UnitName Value="appkit"/>
<CursorPos X="28" Y="9"/>
<TopLine Value="1"/>
<UsageCount Value="11"/>
<UsageCount Value="9"/>
</Unit8>
<Unit9>
<Filename Value="../../../objc/objc.inc"/>
<CursorPos X="13" Y="60"/>
<TopLine Value="46"/>
<EditorIndex Value="5"/>
<UsageCount Value="13"/>
<EditorIndex Value="4"/>
<UsageCount Value="24"/>
<Loaded Value="True"/>
</Unit9>
<Unit10>
<Filename Value="../../appkit/NSApplication.inc"/>
<CursorPos X="31" Y="1047"/>
<TopLine Value="1041"/>
<UsageCount Value="10"/>
<UsageCount Value="8"/>
</Unit10>
<Unit11>
<Filename Value="../../appkit/NSAlert.inc"/>
<CursorPos X="16" Y="8"/>
<TopLine Value="1"/>
<UsageCount Value="12"/>
<UsageCount Value="10"/>
</Unit11>
<Unit12>
<Filename Value="../../appkit/AppKit.inc"/>
<CursorPos X="18" Y="61"/>
<CursorPos X="19" Y="51"/>
<TopLine Value="49"/>
<EditorIndex Value="1"/>
<UsageCount Value="11"/>
<Loaded Value="True"/>
<UsageCount Value="22"/>
</Unit12>
<Unit13>
<Filename Value="../../foundation/Foundation.inc"/>
<CursorPos X="48" Y="9"/>
<TopLine Value="5"/>
<UsageCount Value="11"/>
<UsageCount Value="9"/>
</Unit13>
<Unit14>
<Filename Value="../../appkit/NSBundle.inc"/>
<CursorPos X="34" Y="6"/>
<TopLine Value="1"/>
<UsageCount Value="10"/>
<UsageCount Value="8"/>
</Unit14>
<Unit15>
<Filename Value="../../foundation/NSBundle.inc"/>
<CursorPos X="8" Y="424"/>
<CursorPos X="35" Y="409"/>
<TopLine Value="403"/>
<EditorIndex Value="4"/>
<UsageCount Value="11"/>
<Loaded Value="True"/>
<UsageCount Value="22"/>
</Unit15>
<Unit16>
<Filename Value="../../appkit/NSNibLoading.inc"/>
<CursorPos X="41" Y="42"/>
<TopLine Value="33"/>
<UsageCount Value="10"/>
<UsageCount Value="8"/>
</Unit16>
<Unit17>
<Filename Value="../../foundation/NSObject.inc"/>
<CursorPos X="38" Y="203"/>
<TopLine Value="184"/>
<UsageCount Value="10"/>
<CursorPos X="9" Y="251"/>
<TopLine Value="240"/>
<EditorIndex Value="1"/>
<UsageCount Value="21"/>
<Loaded Value="True"/>
</Unit17>
</Units>
<JumpHistory Count="29" HistoryIndex="28">
<JumpHistory Count="28" HistoryIndex="27">
<Position1>
<Filename Value="view.pas"/>
<Caret Line="20" Column="22" TopLine="9"/>
<Filename Value="controller.pas"/>
<Caret Line="74" Column="15" TopLine="59"/>
</Position1>
<Position2>
<Filename Value="view.pas"/>
<Caret Line="178" Column="5" TopLine="155"/>
<Filename Value="../../../objc/objc.pas"/>
<Caret Line="27" Column="12" TopLine="19"/>
</Position2>
<Position3>
<Filename Value="view.pas"/>
<Caret Line="181" Column="5" TopLine="158"/>
<Filename Value="mytoolbar.pas"/>
<Caret Line="149" Column="9" TopLine="134"/>
</Position3>
<Position4>
<Filename Value="view.pas"/>
<Caret Line="23" Column="27" TopLine="13"/>
<Caret Line="218" Column="77" TopLine="200"/>
</Position4>
<Position5>
<Filename Value="view.pas"/>
<Caret Line="219" Column="1" TopLine="210"/>
<Filename Value="controller.pas"/>
<Caret Line="132" Column="5" TopLine="109"/>
</Position5>
<Position6>
<Filename Value="view.pas"/>
<Caret Line="23" Column="8" TopLine="14"/>
<Filename Value="controller.pas"/>
<Caret Line="108" Column="1" TopLine="94"/>
</Position6>
<Position7>
<Filename Value="view.pas"/>
<Caret Line="218" Column="1" TopLine="209"/>
<Filename Value="controller.pas"/>
<Caret Line="51" Column="17" TopLine="39"/>
</Position7>
<Position8>
<Filename Value="view.pas"/>
<Caret Line="131" Column="1" TopLine="114"/>
<Filename Value="controller.pas"/>
<Caret Line="123" Column="35" TopLine="118"/>
</Position8>
<Position9>
<Filename Value="view.pas"/>
<Caret Line="116" Column="44" TopLine="104"/>
<Filename Value="controller.pas"/>
<Caret Line="125" Column="61" TopLine="113"/>
</Position9>
<Position10>
<Filename Value="view.pas"/>
<Caret Line="192" Column="35" TopLine="180"/>
<Filename Value="controller.pas"/>
<Caret Line="103" Column="38" TopLine="91"/>
</Position10>
<Position11>
<Filename Value="controller.pas"/>
<Caret Line="74" Column="15" TopLine="59"/>
<Caret Line="126" Column="32" TopLine="113"/>
</Position11>
<Position12>
<Filename Value="../../../objc/objc.pas"/>
<Caret Line="27" Column="12" TopLine="19"/>
</Position12>
<Position13>
<Filename Value="mytoolbar.pas"/>
<Caret Line="149" Column="9" TopLine="134"/>
</Position13>
<Position14>
<Filename Value="view.pas"/>
<Caret Line="218" Column="77" TopLine="200"/>
</Position14>
<Position15>
<Filename Value="controller.pas"/>
<Caret Line="132" Column="5" TopLine="109"/>
</Position15>
<Position16>
<Filename Value="controller.pas"/>
<Caret Line="108" Column="1" TopLine="94"/>
</Position16>
<Position17>
<Filename Value="controller.pas"/>
<Caret Line="51" Column="17" TopLine="39"/>
</Position17>
<Position18>
<Filename Value="controller.pas"/>
<Caret Line="123" Column="35" TopLine="118"/>
</Position18>
<Position19>
<Filename Value="controller.pas"/>
<Caret Line="125" Column="61" TopLine="113"/>
</Position19>
<Position20>
<Filename Value="controller.pas"/>
<Caret Line="103" Column="38" TopLine="91"/>
</Position20>
<Position21>
<Filename Value="controller.pas"/>
<Caret Line="126" Column="32" TopLine="113"/>
</Position21>
<Position22>
<Filename Value="controller.pas"/>
<Caret Line="125" Column="1" TopLine="108"/>
</Position22>
<Position23>
</Position12>
<Position13>
<Filename Value="controller.pas"/>
<Caret Line="139" Column="1" TopLine="122"/>
</Position23>
<Position24>
</Position13>
<Position14>
<Filename Value="controller.pas"/>
<Caret Line="144" Column="44" TopLine="132"/>
</Position14>
<Position15>
<Filename Value="view.pas"/>
<Caret Line="158" Column="29" TopLine="138"/>
</Position15>
<Position16>
<Filename Value="view.pas"/>
<Caret Line="33" Column="60" TopLine="19"/>
</Position16>
<Position17>
<Filename Value="view.pas"/>
<Caret Line="207" Column="39" TopLine="199"/>
</Position17>
<Position18>
<Filename Value="view.pas"/>
<Caret Line="33" Column="17" TopLine="19"/>
</Position18>
<Position19>
<Filename Value="view.pas"/>
<Caret Line="211" Column="9" TopLine="197"/>
</Position19>
<Position20>
<Filename Value="view.pas"/>
<Caret Line="33" Column="78" TopLine="33"/>
</Position20>
<Position21>
<Filename Value="view.pas"/>
<Caret Line="208" Column="25" TopLine="197"/>
</Position21>
<Position22>
<Filename Value="view.pas"/>
<Caret Line="148" Column="1" TopLine="130"/>
</Position22>
<Position23>
<Filename Value="view.pas"/>
<Caret Line="133" Column="65" TopLine="121"/>
</Position23>
<Position24>
<Filename Value="view.pas"/>
<Caret Line="146" Column="69" TopLine="125"/>
</Position24>
<Position25>
<Filename Value="view.pas"/>
<Caret Line="158" Column="29" TopLine="138"/>
<Caret Line="194" Column="73" TopLine="192"/>
</Position25>
<Position26>
<Filename Value="../../foundation/NSBundle.inc"/>
<Caret Line="1" Column="1" TopLine="1"/>
<Filename Value="../../foundation/NSObject.inc"/>
<Caret Line="249" Column="1" TopLine="239"/>
</Position26>
<Position27>
<Filename Value="../../foundation/NSBundle.inc"/>
<Caret Line="56" Column="19" TopLine="49"/>
<Filename Value="../../foundation/NSObject.inc"/>
<Caret Line="263" Column="1" TopLine="250"/>
</Position27>
<Position28>
<Filename Value="../../appkit/AppKit.inc"/>
<Caret Line="24" Column="35" TopLine="21"/>
<Filename Value="../../foundation/NSObject.inc"/>
<Caret Line="270" Column="15" TopLine="260"/>
</Position28>
<Position29>
<Filename Value="../../appkit/AppKit.inc"/>
<Caret Line="61" Column="18" TopLine="49"/>
</Position29>
</JumpHistory>
</ProjectOptions>
<CompilerOptions>

View File

@ -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;

View File

@ -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;
{@@

View File

@ -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