Improves Cocoa widgetset

git-svn-id: trunk@15554 -
This commit is contained in:
sekelsenmat 2008-06-24 02:02:25 +00:00
parent b7dbfbdfe9
commit 98bf837c8d
2 changed files with 78 additions and 28 deletions

View File

@ -21,7 +21,7 @@
} }
unit CocoaPrivate; unit CocoaPrivate;
{$mode objfpc}{$H+} {$mode delphi}
interface interface
@ -39,6 +39,9 @@ uses
LMessages, LCLMessageGlue, LCLProc, LCLType, Graphics, Controls, Forms, LMessages, LCLMessageGlue, LCLProc, LCLType, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ComCtrls, ExtCtrls, Menus; Dialogs, StdCtrls, Buttons, ComCtrls, ExtCtrls, Menus;
const
Str_Button_OnClick = 'ButtonOnClick';
type type
{ TCocoaForm } { TCocoaForm }
@ -57,25 +60,30 @@ type
{ TCocoaControl } { TCocoaControl }
TCocoaControl = class(TObject) TCocoaControl = class(NSObject)
public public
{ classes } { classes }
ParentView: NSView; ParentView: NSView;
Control: NSControl;
LCLControl: TWinControl;
{ strings and sizes } { strings and sizes }
CFTitle: CFStringRef; CFTitle: CFStringRef;
ControlRect: NSRect; ControlRect: NSRect;
public public
constructor Create(const AWinControl: TWinControl; const AParams: TCreateParams); constructor Create(const AWinControl: TWinControl; const AParams: TCreateParams);
procedure InitializeFields;
procedure InitializeControl;
end; end;
{ TCocoaButton } { TCocoaButton }
TCocoaButton = class(TCocoaControl) TCocoaButton = class(TCocoaControl)
public
{ classes }
Handle: NSButton;
public public
constructor Create(const AWinControl: TWinControl; const AParams: TCreateParams); constructor Create(const AWinControl: TWinControl; const AParams: TCreateParams);
function Button: NSButton;
procedure AddMethods; override;
{ Objective-c Methods }
class procedure ButtonOnClick(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl;
end; end;
implementation implementation
@ -101,6 +109,44 @@ begin
MainWindow.setTitle(CFTitle); MainWindow.setTitle(CFTitle);
end; end;
{ TCocoaControl }
constructor TCocoaControl.Create(const AWinControl: TWinControl;
const AParams: TCreateParams);
begin
{ The class is registered on the Objective-C runtime before the NSObject constructor is called }
if not CreateClassDefinition(ClassName(), Str_NSObject) then Exception.Create('Failed to create objc class: ' + ClassName());
inherited Create;
// Initializes information fields
LCLControl := AWinControl;
InitializeFields();
end;
procedure TCocoaControl.InitializeFields;
begin
CFTitle := CFStringCreateWithPascalString(nil, LCLControl.Caption, kCFStringEncodingUTF8);
ControlRect.origin.x := LCLControl.Left;
ControlRect.origin.y := LCLControl.Top;
ControlRect.size.width := LCLControl.Width;
ControlRect.size.height := LCLControl.Height;
if LCLControl.Parent <> nil then
begin
if LCLControl.Parent is TCustomForm then
begin
ParentView := TCocoaForm(LCLControl.Parent.Handle).MainWindowView;
end;
end;
end;
procedure TCocoaControl.InitializeControl;
begin
Control.setTag(PtrInt(Self));
end;
{ TCocoaButton } { TCocoaButton }
constructor TCocoaButton.Create(const AWinControl: TWinControl; constructor TCocoaButton.Create(const AWinControl: TWinControl;
@ -108,33 +154,37 @@ constructor TCocoaButton.Create(const AWinControl: TWinControl;
begin begin
inherited Create(AWinControl, AParams); inherited Create(AWinControl, AParams);
Handle := NSButton.initWithFrame(ControlRect); Control := NSButton.initWithFrame(ControlRect);
Handle.setTitle(CFTitle); Button.setTitle(CFTitle);
Handle.setBezelStyle(NSRoundedBezelStyle); Button.setBezelStyle(NSRoundedBezelStyle);
// Handle.setAction(sel_registerName(PChar(ACallbackName))); Button.setAction(sel_registerName(PChar(Str_Button_OnClick)));
// Handle.setTarget(ACallbackClass.Handle); Button.setTarget(Handle);
if ParentView <> nil then ParentView.addSubview(Handle); if ParentView <> nil then ParentView.addSubview(Button);
end; end;
{ TCocoaControl } function TCocoaButton.Button: NSButton;
constructor TCocoaControl.Create(const AWinControl: TWinControl;
const AParams: TCreateParams);
begin begin
CFTitle := CFStringCreateWithPascalString(nil, AWinControl.Caption, kCFStringEncodingUTF8); Result := NSButton(Control);
end;
ControlRect.origin.x := AWinControl.Left; procedure TCocoaButton.AddMethods;
ControlRect.origin.y := AWinControl.Top; begin
ControlRect.size.width := AWinControl.Width; AddMethod(Str_Button_OnClick, 'v@:@', Pointer(ButtonOnClick));
ControlRect.size.height := AWinControl.Height; end;
if AWinControl.Parent <> nil then class procedure TCocoaButton.ButtonOnClick(_self: objc.id; _cmd: SEL; sender: objc.id); cdecl;
begin var
if AWinControl.Parent is TCustomForm then VSelf: TCocoaButton;
begin VNSControl: NSControl;
ParentView := TCocoaForm(AWinControl.Parent.Handle).MainWindowView; begin
end; VNSControl := NSControl.CreateWithHandle(sender);
try
VSelf := TCocoaButton(VNSControl.tag);
VSelf.LCLControl.OnClick(VSelf.LCLControl);
finally
VNSControl.Handle := nil;
VNSControl.Free;
end; end;
end; end;

View File

@ -328,7 +328,7 @@ var
CocoaButton: TCocoaButton; CocoaButton: TCocoaButton;
begin begin
CocoaButton := TCocoaButton.Create(AWinControl, AParams); CocoaButton := TCocoaButton.Create(AWinControl, AParams);
CocoaButton.Handle.setButtonType(NSSwitchButton); CocoaButton.Button.setButtonType(NSSwitchButton);
Result := TLCLIntfHandle(CocoaButton); Result := TLCLIntfHandle(CocoaButton);
end; end;