cocoa: implemented: form resize event, WinAPI.GetWindowRect

git-svn-id: trunk@22870 -
This commit is contained in:
dmitry 2009-11-30 09:30:19 +00:00
parent 3a5dfa739f
commit 77b4e22f3c
6 changed files with 204 additions and 31 deletions

View File

@ -55,6 +55,7 @@ type
procedure Deactivate; virtual; abstract;
procedure CloseQuery(var CanClose: Boolean); virtual; abstract;
procedure Close; virtual; abstract;
procedure Resize; virtual; abstract;
end;
{ TCocoaWindowContentView }
@ -64,6 +65,12 @@ type
procedure drawRect(r: NSRect); override;
end;
{todo: consider using common protocol for all TCocoa* derived objcclasses }
{TCocoaTopLeftRect = objcprotocol
procedure getTopLeftFrame(var r: TRect); message 'getTopLeftFrame:';
procedure setTopLeftFrame(const r: TRect); message 'setTopLeftFrame:';
end;}
{ TCocoaButton }
TCocoaButton = objcclass(NSButton)
@ -105,6 +112,7 @@ type
procedure windowWillClose(notification: NSNotification); message 'windowWillClose:';
procedure windowDidBecomeKey(notification: NSNotification); message 'windowDidBecomeKey:';
procedure windowDidResignKey(notification: NSNotification); message 'windowDidResignKey:';
procedure windowDidResize(notification: NSNotification); message 'windowDidResize:';
public
callback : TCommonCallback;
wincallback : TWindowCallback;
@ -227,6 +235,11 @@ begin
wincallback.Deactivate;
end;
procedure TCocoaWindow.windowDidResize(notification: NSNotification);
begin
wincallback.Resize;
end;
function TCocoaWindow.acceptsFirstResponder: Boolean;
begin
Result:=true;

View File

@ -9,8 +9,11 @@ uses
MacOSAll, CocoaAll,
Types, LCLType;
function GetNSPoint(x,y: single): NSPoint; inline;
function GetNSRect(x, y, width, height: Integer): NSRect; inline;
function RectToNSRect(const r: TRect): NSRect;
procedure NSRectToRect(const ns: NSRect; var r: TRect);
function CreateParamsToNSRect(const params: TCreateParams): NSRect;
function NSStringUtf8(s: PChar): NSString;
@ -21,10 +24,9 @@ function GetNSObjectView(obj: NSObject): NSView;
procedure AddViewToNSObject(ctrl: NSView; obj: NSObject);
procedure AddViewToNSObject(ctrl: NSView; obj: NSObject; X,Y: integer);
function GetCocoaRect(parent: NSView; const ChildBounds: TRect): NSRect;
function GetCocoaRect(parent: NSView; ChildX, ChildY, ChildWidth, ChildHeight: Integer): NSRect;
procedure SetViewBoundsPos(view: NSView; X,Y: integer; invalidateView: Boolean=false);
procedure SetViewFramePos(view: NSView; X,Y: Single; invalidateView: Boolean=false);
procedure SetViewFrame(view: NSView; X,Y,W,H: Single; invalidateView: Boolean=false);
procedure GetViewFrame(view: NSView; var FrameRect: TRect);
procedure SetNSText(text: NSText; const s: String); inline;
function GetNSText(text: NSText): string; inline;
@ -32,6 +34,12 @@ function GetNSText(text: NSText): string; inline;
procedure SetNSControlValue(c: NSControl; const S: String); inline;
function GetNSControlValue(c: NSControl): String; inline;
procedure LCLToCocoaRect(const lcl, parent: NSRect; var cocoa: NSRect); inline;
procedure CocoaToLCLRect(const cocoa, parent: NSRect; var lcl: NSRect); inline;
function GetNSWindowFrame(win: NSWindow; var lcl: TRect): Boolean; inline;
function GetNSViewFrame(view: NSView; var lcl: TRect): boolean; inline;
implementation
const
@ -70,7 +78,17 @@ begin
end;
{X,Y - coordinates relative to Left-Top (LCL-style)}
procedure SetViewBoundsPos(view: NSView; X,Y: integer; invalidateView: Boolean);
procedure SetViewFramePos(view: NSView; X,Y: Single; invalidateView: Boolean);
var
f : NSRect;
begin
f:=view.frame;
SetViewFrame(view,x,y,f.size.width, f.size.height, invalidateView);
end;
{X,Y - coordinates relative to Left-Top (LCL-style)}
{W,H - width and height of the NSView}
procedure SetViewFrame(view: NSView; X,Y,W,H: single; invalidateView: Boolean=false); overload;
var
parent : NSView;
pb, b : NSRect;
@ -80,36 +98,27 @@ begin
pb:=parent.bounds;
b:=view.frame;
b.origin.x:=X;
b.origin.y:=pb.size.height-y-b.size.height;
b.origin.y:=pb.size.height-y-h;
b.size.width:=w;
b.size.height:=h;
view.setFrame(b);
if invalidateView then view.setNeedsDisplay_(true);
end;
function GetCocoaRect(parent: NSView; ChildX, ChildY, ChildWidth, ChildHeight: Integer): NSRect;
procedure GetViewFrame(view: NSView; var FrameRect: TRect);
var
b : NSRect;
parent : NSView;
pb, f : NSRect;
begin
if not Assigned(parent) then begin
Result.origin.x:=ChildX;
Result.origin.y:=ChildY;
Result.size.width:=ChildWidth;
Result.size.height:=ChildHeight;
end else begin
b:=parent.bounds;
Result.origin.x:=ChildX;
Result.origin.y:=b.size.height - ChildY;
Result.size.width:=ChildWidth;
Result.size.height:=ChildHeight;
f:=view.frame;
parent:=view.superView;
if Assigned(parent) then begin
pb:=parent.bounds;
f.origin.y:=pb.size.height-f.origin.y-f.size.height;
end;
NSRectToRect(f, FrameRect);
end;
function GetCocoaRect(parent: NSView; const ChildBounds: TRect): NSRect;
begin
with ChildBounds do
Result:=GetCocoaRect(parent, Left, Top, Right-Left, Bottom-Top);
end;
function GetNSObjectView(obj: NSObject): NSView;
begin
Result:=nil;
@ -130,7 +139,13 @@ end;
procedure AddViewToNSObject(ctrl: NSView; obj: NSObject; X,Y: integer);
begin
AddViewToNSObject(ctrl, obj);
SetViewBoundsPos(ctrl, x,y);
SetViewFramePos(ctrl, x,y);
end;
function GetNSPoint(x, y: single): NSPoint;
begin
Result.x:=x;
Result.y:=y;
end;
function GetNSRect(x, y, width, height: Integer): NSRect;
@ -146,6 +161,14 @@ begin
Result:=GetNSRect(r.Left,r.Top,r.Right-r.Left,r.Bottom-r.Top);
end;
procedure NSRectToRect(const ns: NSRect; var r: TRect);
begin
r.Left:=round(ns.origin.x);
r.Top:=round(ns.origin.y);
r.Right:=round(ns.origin.x+ns.size.width);
r.Bottom:=round(ns.origin.y+ns.size.height);
end;
function CreateParamsToNSRect(const params: TCreateParams): NSRect;
begin
with params do Result:=GetNSRect(X,Y,Width,Height);
@ -203,6 +226,44 @@ begin
end;
procedure LCLToCocoaRect(const lcl, parent: NSRect; var cocoa: NSRect);
begin
cocoa.origin.x:=lcl.origin.x;
cocoa.origin.y:=parent.size.height - lcl.size.height - lcl.origin.y;
cocoa.size:=lcl.size;
end;
procedure CocoaToLCLRect(const cocoa, parent: NSRect; var lcl: NSRect);
begin
lcl.origin.x:=cocoa.origin.x;
lcl.origin.y:=parent.size.height-cocoa.size.height-cocoa.origin.y;
lcl.size:=cocoa.size;
end;
function GetNSWindowFrame(win: NSWindow; var lcl: TRect): Boolean;
var
f : NSRect;
begin
if Assigned(win.screen) then begin
CocoaToLCLRect( win.frame, win.screen.frame, f);
NSRectToRect(f, lcl);
end else
NSRectToRect(win.frame, lcl);
Result:=true;
end;
function GetNSViewFrame(view: NSView; var lcl: TRect): boolean; inline;
var
f : NSRect;
begin
if Assigned(view.superview) then begin
CocoaToLCLRect( view.frame, view.superview.frame, f);
NSRectToRect(f, lcl);
end else
NSRectToRect(view.frame, lcl);
Result:=true;
end;
initialization

View File

@ -53,4 +53,24 @@ begin
Result:=true;
end;
{------------------------------------------------------------------------------
Method: GetWindowRect
Params: Handle - Handle of window
Rect - Record for window coordinates
Returns: if the function succeeds, the return value is nonzero; if the
function fails, the return value is zero
Retrieves the screen bounding rectangle of the specified window
------------------------------------------------------------------------------}
function TCocoaWidgetSet.GetWindowRect(Handle: hwnd; var ARect: TRect): Integer;
begin
Result:=0;
if Handle=0 then Exit;
if NSObject(Handle).isKindOfClass_(NSWindow) then
GetNSWindowFrame(NSWindow(Handle), ARect)
else
{todo: GetNSViewFrame(NSView(Handle), ARect)};
end;
//##apiwiz##eps## // Do not remove, no wizard declaration after this line

View File

@ -120,9 +120,9 @@ function GetTextColor(DC: HDC) : TColorRef; Override;
function GetTextExtentPoint(DC: HDC; Str: PChar; Count: Integer; var Size: TSize): Boolean; override;
function GetTextMetrics(DC: HDC; var TM: TTextMetric): Boolean; override;
function GetWindowLong(Handle : hwnd; int: Integer): PtrInt; override;
function GetWindowOrgEx(dc : hdc; P : PPoint): Integer; override;
function GetWindowOrgEx(dc : hdc; P : PPoint): Integer; override;}
function GetWindowRect(Handle: hwnd; var ARect: TRect): Integer; override;
function GetWindowRelativePosition(Handle: hwnd; var Left, Top: Integer): boolean; override;
{function GetWindowRelativePosition(Handle: hwnd; var Left, Top: Integer): boolean; override;
function GetWindowSize(Handle: hwnd; var Width, Height: Integer): boolean; override;
function GradientFill(DC: HDC; Vertices: PTriVertex; NumVertices : Longint;
Meshes: Pointer; NumMeshes : Longint; Mode : Longint): Boolean; override;

View File

@ -7,6 +7,7 @@ interface
uses
MacOSAll, CocoaAll,
Classes,
Controls, {todo: remove controls?}
WSControls,
CocoaPrivate, CocoaUtils, LCLMessageGlue;
@ -32,6 +33,10 @@ type
class procedure SetText(const AWinControl: TWinControl; const AText: String); override;
class function GetText(const AWinControl: TWinControl; var AText: String): Boolean; override;
class function GetTextLen(const AWinControl: TWinControl; var ALength: Integer): Boolean; override;
class function GetClientBounds(const AWincontrol: TWinControl; var ARect: TRect): Boolean; override;
class function GetClientRect(const AWincontrol: TWinControl; var ARect: TRect): Boolean; override;
class procedure SetBounds(const AWinControl: TWinControl; const ALeft, ATop, AWidth, AHeight: Integer); override;
end;
@ -110,5 +115,26 @@ begin
else ALength:=0
end;
class function TCocoaWSWinControl.GetClientBounds(const AWincontrol: TWinControl; var ARect: TRect): Boolean;
begin
Result:=(AWinControl.Handle<>0);
if not Result then Exit;
GetViewFrame(NSView(AWinControl.Handle), ARect);
end;
class function TCocoaWSWinControl.GetClientRect(const AWincontrol: TWinControl; var ARect: TRect): Boolean;
begin
Result:=(AWinControl.Handle<>0);
if not Result then Exit;
NSRectToRect(NSView(AWinControl.Handle).bounds, ARect);
end;
class procedure TCocoaWSWinControl.SetBounds(const AWinControl: TWinControl;
const ALeft, ATop, AWidth, AHeight: Integer);
begin
if (AWinControl.Handle=0) then Exit;
SetViewFrame(NSView(AWinControl.Handle), ALeft, ATop, AWidth, AHeight);
end;
end.

View File

@ -35,7 +35,7 @@ uses
// Widgetset
WSForms, WSLCLClasses, WSProc, LCLMessageGlue,
// LCL Cocoa
CocoaPrivate, CocoaUtils, CocoaWSCommon;
CocoaPrivate, CocoaUtils, CocoaWSCommon, CocoaWSStdCtrls ;
type
{ TLCLWindowCallback }
@ -48,6 +48,7 @@ type
procedure Deactivate; override;
procedure CloseQuery(var CanClose: Boolean); override;
procedure Close; override;
procedure Resize; override;
end;
@ -102,6 +103,11 @@ type
// class procedure SetBorderIcons(const AForm: TCustomForm; const ABorderIcons: TBorderIcons); override;
// class procedure SetFormBorderStyle(const AForm: TCustomForm; const AFormBorderStyle: TFormBorderStyle); override;
{need to override these }
class function GetClientBounds(const AWincontrol: TWinControl; var ARect: TRect): Boolean; override;
class function GetClientRect(const AWincontrol: TWinControl; var ARect: TRect): Boolean; override;
class procedure SetBounds(const AWinControl: TWinControl; const ALeft, ATop, AWidth, AHeight: Integer); override;
end;
{ TCocoaWSForm }
@ -169,6 +175,17 @@ begin
LCLSendCloseUpMsg(Target);
end;
procedure TLCLWindowCallback.Resize;
var
sz : NSSize;
r : TRect;
begin
sz := Owner.frame.size;
TCocoaWSCustomForm.GetClientBounds(TWinControl(Target), r);
if Assigned(Target) then
LCLSendSizeMsg(Target, Round(sz.width), Round(sz.height), SIZENORMAL);
end;
{ TCocoaWSCustomForm }
@ -193,9 +210,9 @@ begin
Result:=0;
Exit;
end;
win:=TCocoaWindow(win.initWithContentRect_styleMask_backing_defer(CreateParamsToNSRect(AParams), WinMask, NSBackingStoreBuffered, False));
TCocoaWindow(win).callback:=TLCLCommonCallback.Create(win, AWinControl);
TCocoaWindow(win).wincallback:=TLCLWindowCallback.Create(win, AWinControl);
win.initWithContentRect_styleMask_backing_defer(CreateParamsToNSRect(AParams), WinMask, NSBackingStoreBuffered, False);
win.setDelegate(win);
win.setTitle(NSStringUtf8(AWinControl.Caption));
@ -245,4 +262,40 @@ begin
win.setTitle(NSStringUtf8(AText));
end;
class function TCocoaWSCustomForm.GetClientBounds(const AWinControl: TWinControl; var ARect: TRect): Boolean;
begin
Result:=AWinControl.Handle<>0;
if not Result then Exit;
Result:=GetNSWindowFrame(NSWindow(AWinControl.Handle), ARect);
end;
class function TCocoaWSCustomForm.GetClientRect(const AWinControl: TWinControl;
var ARect: TRect): Boolean;
begin
Result:=AWinControl.Handle<>0;
if not Result then Exit;
{todo:}
GetViewFrame( NSWindow(AWinControl.Handle).contentView, ARect );
writeln('GetClientRect ... ', ARect.Left, ' ',ARect.Top);
end;
class procedure TCocoaWSCustomForm.SetBounds(const AWinControl: TWinControl;
const ALeft, ATop, AWidth, AHeight: Integer);
var
sf, wf : NSRect;
begin
if AWinControl.Handle=0 then Exit;
{todo: setFrame_display(, true)? }
sf:=NSScreen.mainScreen.frame;
writeln('SetBounds: ', ALeft, ' ',ATop, ' ',AWidth,' ',AHeight);
LCLToCocoaRect( GetNSRect(ALeft,ATop,AWidth,AHeight), sf, wf);
NSWindow(AWinControl.Handle).setFrame_display(wf, false);
//NSWindow(AWinControl.Handle).setFrame_display( GetNSRect(ALeft,ATop, AWidth, AHeight), false);
//NSWindow(AWinControl.Handle).setFrameTopLeftPoint( GetNSPoint(ALeft, ATop));
end;
end.