diff --git a/lcl/interfaces/cocoa/cocoaprivate.pp b/lcl/interfaces/cocoa/cocoaprivate.pp index 346dbc2785..80ee71f7d0 100644 --- a/lcl/interfaces/cocoa/cocoaprivate.pp +++ b/lcl/interfaces/cocoa/cocoaprivate.pp @@ -94,6 +94,12 @@ type function ResetCursorRects: Boolean; end; + { IButtonCallback } + + IButtonCallback = interface(ICommonCallback) + procedure ButtonClick; + end; + { IWindowCallback } IWindowCallback = interface(ICommonCallBack) @@ -124,7 +130,7 @@ type protected procedure actionButtonClick(sender: NSObject); message 'actionButtonClick:'; public - callback : ICommonCallback; + callback: IButtonCallback; function initWithFrame(frameRect: NSRect): id; override; function acceptsFirstResponder: Boolean; override; procedure mouseDown(event: NSEvent); override; @@ -139,7 +145,7 @@ type { TCocoaTextField } TCocoaTextField = objcclass(NSTextField) - callback : ICommonCallback; + callback: ICommonCallback; function acceptsFirstResponder: Boolean; override; procedure resetCursorRects; override; end; @@ -156,7 +162,7 @@ type { TCocoaTextView } TCocoaTextView = objcclass(NSTextView) - callback : ICommonCallback; + callback: ICommonCallback; function acceptsFirstResponder: Boolean; override; procedure resetCursorRects; override; end; @@ -171,7 +177,7 @@ type procedure windowDidResignKey(notification: NSNotification); message 'windowDidResignKey:'; procedure windowDidResize(notification: NSNotification); message 'windowDidResize:'; public - callback : IWindowCallback; + callback: IWindowCallback; function acceptsFirstResponder: Boolean; override; procedure mouseUp(event: NSEvent); override; procedure mouseDown(event: NSEvent); override; @@ -184,7 +190,7 @@ type { TCocoaCustomControl } TCocoaCustomControl = objcclass(NSControl) - callback : ICommonCallback; + callback: ICommonCallback; procedure drawRect(dirtyRect: NSRect); override; procedure resetCursorRects; override; end; @@ -192,7 +198,7 @@ type { TCocoaScrollView } TCocoaScrollView = objcclass(NSScrollView) - callback : ICommonCallback; + callback: ICommonCallback; procedure resetCursorRects; override; end; @@ -203,7 +209,7 @@ type TCocoaComboBoxList = class(TStringList) private - fOwner : TCocoaComboBox; + FOwner: TCocoaComboBox; protected procedure Changed; override; public @@ -221,9 +227,9 @@ type { TCocoaComboBox } TCocoaComboBox = objcclass(NSComboBox, NSComboBoxDataSourceProtocol, NSComboBoxDelegateProtocol) - callback : IComboboxCallBack; - list : TCocoaComboBoxList; - resultNS : NSString; //use to return values to combo + callback: IComboboxCallBack; + list: TCocoaComboBoxList; + resultNS: NSString; //use to return values to combo function comboBox_objectValueForItemAtIndex_(combo: TCocoaComboBox; row: NSInteger): id; message 'comboBox:objectValueForItemAtIndex:'; function numberOfItemsInComboBox(combo: TCocoaComboBox): NSInteger; @@ -239,7 +245,7 @@ type { TCocoaScrollBar } TCocoaScrollBar = objcclass(NSScroller) - callback : ICommonCallback; + callback: ICommonCallback; procedure resetCursorRects; override; end; @@ -251,16 +257,16 @@ type protected procedure Changed; override; public - Owner : TCocoaListView; + Owner: TCocoaListView; constructor Create(AOwner: TCocoaListView); end; { TCocoaListView } TCocoaListView = objcclass(NSTableView, NSTableViewDataSourceProtocol) - callback : ICommonCallback; - list : TCocoaStringList; - resultNS : NSString; //use to return values to combo + callback: ICommonCallback; + list: TCocoaStringList; + resultNS: NSString; //use to return values to combo function numberOfRowsInTableView(aTableView: NSTableView): NSInteger; message 'numberOfRowsInTableView:'; function tableView_objectValueForTableColumn_row(tableView: NSTableView; objectValueForTableColumn: NSTableColumn; row: NSInteger):id; @@ -272,7 +278,7 @@ type { TCocoaGroupBox } TCocoaGroupBox = objcclass(NSBox) - callback : ICommonCallback; + callback: ICommonCallback; procedure resetCursorRects; override; end; @@ -306,14 +312,15 @@ end; procedure TCocoaButton.actionButtonClick(sender: NSObject); begin - callback.MouseClick(1); - //todo: simulate MouseUp + // this is the action handler of button + callback.ButtonClick; end; function TCocoaButton.initWithFrame(frameRect: NSRect): id; begin - Result:=inherited initWithFrame(frameRect); - if Assigned(Result) then begin + Result := inherited initWithFrame(frameRect); + if Assigned(Result) then + begin setTarget(Self); setAction(objcselector('actionButtonClick:')); end; @@ -321,14 +328,14 @@ end; function TCocoaButton.acceptsFirstResponder: Boolean; begin - Result:=true; + Result := True; end; procedure TCocoaButton.mouseUp(event: NSEvent); var - mp : NSPoint; + mp: NSPoint; begin - mp:=event.locationInWindow; + mp := event.locationInWindow; callback.MouseUp(round(mp.x), round(mp.y)); inherited mouseUp(event); end; @@ -343,7 +350,7 @@ procedure TCocoaButton.mouseDown(event: NSEvent); var mp : NSPoint; begin - mp:=event.locationInWindow; + mp := event.locationInWindow; callback.MouseDown(round(mp.x), round(mp.y)); inherited mouseDown(event); end; diff --git a/lcl/interfaces/cocoa/cocoawsbuttons.pp b/lcl/interfaces/cocoa/cocoawsbuttons.pp index f74ae92604..5d1355060e 100644 --- a/lcl/interfaces/cocoa/cocoawsbuttons.pp +++ b/lcl/interfaces/cocoa/cocoawsbuttons.pp @@ -34,7 +34,7 @@ uses // widgetset WSButtons, WSLCLClasses, WSProc, // LCL Carbon - CocoaWSStdCtrls, CocoaUtils; + CocoaWSCommon, CocoaWSStdCtrls, CocoaUtils; type @@ -70,12 +70,12 @@ implementation class function TCocoaWSBitBtn.CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; var - btn : NSButton; + btn: NSButton; begin - btn:=AllocButton(AWinControl, AParams, NSRoundedBezelStyle, NSMomentaryPushInButton); + btn := AllocButton(AWinControl, TLCLButtonCallBack, AParams, NSRoundedBezelStyle, NSMomentaryPushInButton); if Assigned(btn) then AddViewToNSObject(btn, NSObject(AParams.WndParent), AParams.X, AParams.Y); - Result:=TLCLIntfHandle(btn); + Result := TLCLIntfHandle(btn); end; {------------------------------------------------------------------------------ diff --git a/lcl/interfaces/cocoa/cocoawscommon.pas b/lcl/interfaces/cocoa/cocoawscommon.pas index 21ad457537..8aa31ad053 100644 --- a/lcl/interfaces/cocoa/cocoawscommon.pas +++ b/lcl/interfaces/cocoa/cocoawscommon.pas @@ -38,6 +38,8 @@ type function ResetCursorRects: Boolean; virtual; end; + TLCLCommonCallBackClass = class of TLCLCommonCallBack; + { TCocoaWSWinControl } TCocoaWSWinControl=class(TWSWinControl) diff --git a/lcl/interfaces/cocoa/cocoawsstdctrls.pp b/lcl/interfaces/cocoa/cocoawsstdctrls.pp index cabe856b27..59ae1a5822 100644 --- a/lcl/interfaces/cocoa/cocoawsstdctrls.pp +++ b/lcl/interfaces/cocoa/cocoawsstdctrls.pp @@ -146,6 +146,14 @@ type class function GetText(const AWinControl: TWinControl; var AText: String): Boolean; override; end; + { TLCLButtonCallback } + + TLCLButtonCallback = class(TLCLCommonCallback, IButtonCallback) + public + procedure ButtonClick; virtual; + end; + TLCLButtonCallBackClass = class of TLCLButtonCallBack; + { TCocoaWSButton } TCocoaWSButton = class(TWSButton) @@ -154,12 +162,19 @@ type class procedure SetDefault(const AButton: TCustomButton; ADefault: Boolean); override; end; + { TLCLCheckBoxCallback } + + TLCLCheckBoxCallback = class(TLCLButtonCallBack) + public + procedure ButtonClick; override; + end; + { TCocoaWSCustomCheckBox } TCocoaWSCustomCheckBox = class(TWSCustomCheckBox) published - class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override; - class function RetrieveState(const ACustomCheckBox: TCustomCheckBox): TCheckBoxState; override; + class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override; + class function RetrieveState(const ACustomCheckBox: TCustomCheckBox): TCheckBoxState; override; class procedure SetState(const ACustomCheckBox: TCustomCheckBox; const NewState: TCheckBoxState); override; end; @@ -188,20 +203,20 @@ type end; function AllocTextView(ATarget: TWinControl; const AParams: TCreateParams; fieldEditor: Boolean): NSTextView; -function AllocButton(ATarget: TWinControl; const AParams: TCreateParams; btnBezel: NSBezelStyle; btnType: NSButtonType): NSButton; +function AllocButton(const ATarget: TWinControl; const ACallBackClass: TLCLButtonCallBackClass; const AParams: TCreateParams; btnBezel: NSBezelStyle; btnType: NSButtonType): NSButton; function AllocTextField(ATarget: TWinControl; const AParams: TCreateParams): TCocoaTextField; function AllocSecureTextField(ATarget: TWinControl; const AParams: TCreateParams): TCocoaSecureTextField; implementation -function AllocButton(ATarget: TWinControl; const AParams: TCreateParams; btnBezel: NSBezelStyle; btnType: NSButtonType): NSButton; +function AllocButton(const ATarget: TWinControl; const ACallBackClass: TLCLButtonCallBackClass; const AParams: TCreateParams; btnBezel: NSBezelStyle; btnType: NSButtonType): NSButton; var cap: NSString; begin - Result:=TCocoaButton.alloc.lclInitWithCreateParams(AParams); + Result := TCocoaButton.alloc.lclInitWithCreateParams(AParams); if Assigned(Result) then begin - TCocoaButton(Result).callback:=TLCLCommonCallback.Create(Result, ATarget); + TCocoaButton(Result).callback := ACallBackClass.Create(Result, ATarget); Result.initWithFrame(CreateParamsToNSRect(AParams)); cap := NSStringUTF8(AParams.Caption); Result.setTitle(cap); @@ -240,6 +255,22 @@ begin end; end; +{ TLCLButtonCallback } + +procedure TLCLButtonCallback.ButtonClick; +begin + SendSimpleMessage(Target, LM_CLICKED); +end; + +{ TLCLCheckBoxCallback } + +procedure TLCLCheckBoxCallback.ButtonClick; +begin + inherited; + SendSimpleMessage(Target, LM_CHANGED); + // todo: win32 has something about dbcheckbox handling here. so maybe we need to handle it special too +end; + { TLCLComboboxCallback } procedure TLCLComboboxCallback.ComboBoxWillPopUp; @@ -278,9 +309,9 @@ end; class function TCocoaWSButton.CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; var - btn : NSButton; + btn: NSButton; begin - btn:=AllocButton(AWinControl, AParams, NSRoundedBezelStyle, NSMomentaryPushInButton); + btn := AllocButton(AWinControl, TLCLButtonCallback, AParams, NSRoundedBezelStyle, NSMomentaryPushInButton); if Assigned(btn) then AddViewToNSObject(btn, NSObject(AParams.WndParent), AParams.X, AParams.Y); Result:=TLCLIntfHandle(btn); @@ -320,12 +351,12 @@ end; class function TCocoaWSCustomCheckBox.CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; var - btn : NSButton; + btn: NSButton; begin - btn:=AllocButton(AWinControl, AParams, 0, NSSwitchButton); + btn := AllocButton(AWinControl, TLCLCheckBoxCallBack, AParams, 0, NSSwitchButton); if Assigned(btn) then - AddViewToNSObject(btn, NSObject(AParams.WndParent), AParams.X, AParams.Y); - Result:=TLCLIntfHandle(btn); + AddViewToNSObject(btn, NSObject(AParams.WndParent), AParams.X, AParams.Y); + Result := TLCLIntfHandle(btn); end; {------------------------------------------------------------------------------ @@ -369,10 +400,10 @@ end; class function TCocoaWSRadioButton.CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; var - btn : NSButton; + btn: NSButton; begin - btn:=AllocButton(AWinControl, AParams, 0, NSRadioButton); - Result:=TLCLIntfHandle(btn); + btn := AllocButton(AWinControl, TLCLCheckBoxCallBack, AParams, 0, NSRadioButton); + Result := TLCLIntfHandle(btn); end; { TCocoaWSCustomEdit } @@ -709,13 +740,13 @@ end; class function TCocoaWSToggleBox.CreateHandle(const AWinControl:TWinControl; const AParams:TCreateParams):TLCLIntfHandle; var - btn : NSButton; - cl : NSButtonCell; + btn: NSButton; + cl: NSButtonCell; begin - btn:=AllocButton(AWinControl, AParams, NSTexturedRoundedBezelStyle, NSToggleButton); - cl:=NSButtonCell(NSButton(btn).cell); + btn := AllocButton(AWinControl, TLCLButtonCallBack, AParams, NSTexturedRoundedBezelStyle, NSToggleButton); + cl := NSButtonCell(NSButton(btn).cell); cl.setShowsStateBy(cl.showsStateBy or NSContentsCellMask); - Result:=TLCLIntfHandle(btn); + Result := TLCLIntfHandle(btn); end; { TCocoaWSScrollBar }