From 5576446ed2a8b1a61a4310d6668bcc419ff35659 Mon Sep 17 00:00:00 2001 From: dmitry Date: Fri, 14 Sep 2018 19:23:54 +0000 Subject: [PATCH] cocoa: common approach for handling TAB key and preventing Cocoa to switch the focus git-svn-id: trunk@58995 - --- lcl/interfaces/cocoa/cocoaprivate.pas | 1 + lcl/interfaces/cocoa/cocoatextedits.pas | 10 ---------- lcl/interfaces/cocoa/cocoawscommon.pas | 17 +++++++++++++++-- lcl/interfaces/cocoa/cocoawsstdctrls.pas | 4 ++-- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lcl/interfaces/cocoa/cocoaprivate.pas b/lcl/interfaces/cocoa/cocoaprivate.pas index 8149534247..76909b28c6 100644 --- a/lcl/interfaces/cocoa/cocoaprivate.pas +++ b/lcl/interfaces/cocoa/cocoaprivate.pas @@ -69,6 +69,7 @@ type procedure KeyEvPrepare(Event: NSEvent; AForceAsKeyDown: Boolean = False); procedure KeyEvBefore(out AllowCocoaHandle: boolean); procedure KeyEvAfter; + procedure SetTabSuppress(ASuppress: Boolean); function scrollWheel(Event: NSEvent): Boolean; // size, pos events diff --git a/lcl/interfaces/cocoa/cocoatextedits.pas b/lcl/interfaces/cocoa/cocoatextedits.pas index a8cc8f203a..489f6945af 100644 --- a/lcl/interfaces/cocoa/cocoatextedits.pas +++ b/lcl/interfaces/cocoa/cocoatextedits.pas @@ -114,7 +114,6 @@ type callback: ICommonCallback; FEnabled: Boolean; - allowTabs : Boolean; supressTextChangeEvent: Integer; // if above zero, then don't send text change event function acceptsFirstResponder: Boolean; override; @@ -538,12 +537,7 @@ begin end; cb.KeyEvPrepare(event); - res := true; cb.KeyEvBefore(res); - - //todo: this exceptional code for Tab should NOT be needed! - // callback should take care all of that - res := res and (event.keyCode <> NSKeyCodeTab); if res then inherited keyDown(event); cb.KeyEvAfter; end; @@ -810,10 +804,6 @@ begin begin callback.KeyEvPrepare(event); callback.KeyEvBefore(res); - res := res and ( - // memo can get "tab" if it's allowed - ((event.keyCode <> NSKeyCodeTab) or (allowTabs)) - ); if res then inherited keyDown(event); callback.KeyEvAfter; end else diff --git a/lcl/interfaces/cocoa/cocoawscommon.pas b/lcl/interfaces/cocoa/cocoawscommon.pas index c5c1c53387..4caaedb468 100644 --- a/lcl/interfaces/cocoa/cocoawscommon.pas +++ b/lcl/interfaces/cocoa/cocoawscommon.pas @@ -52,6 +52,7 @@ type Owner: NSObject; Frame: NSObject; BlockCocoaUpDown: Boolean; + SuppressTabDown: Boolean; // all tabs should be suppressed, so Cocoa would not switch focus class constructor Create; constructor Create(AOwner: NSObject; ATarget: TWinControl); virtual; @@ -71,6 +72,7 @@ type procedure KeyEvPrepare(Event: NSEvent; AForceAsKeyDown: Boolean = False); procedure KeyEvBefore(out AllowCocoaHandle: boolean); procedure KeyEvAfter; + procedure SetTabSuppress(ASuppress: Boolean); procedure MouseClick; virtual; function MouseMove(Event: NSEvent): Boolean; virtual; @@ -301,6 +303,8 @@ begin FBoundsReportedToChildren:=false; FIsOpaque:=false; FIsEventRouting:=false; + SuppressTabDown := true; // by default all Tabs would not be allowed for Cocoa. + // it should be enabled, i.e. for TMemo with WantTabs=true end; destructor TLCLCommonCallback.Destroy; @@ -1102,8 +1106,12 @@ end; procedure TLCLCommonCallback.KeyEvBefore(out AllowCocoaHandle: boolean); begin AllowCocoaHandle := true; - if _IsKeyDown then KeyEvBeforeDown(AllowCocoaHandle) - else KeyEvBeforeUp(AllowCocoaHandle); + if _IsKeyDown then begin + KeyEvBeforeDown(AllowCocoaHandle); + if AllowCocoaHandle and SuppressTabDown and (_KeyMsg.CharCode = VK_TAB) then + AllowCocoaHandle := false; + end else + KeyEvBeforeUp(AllowCocoaHandle); end; procedure TLCLCommonCallback.KeyEvAfter; @@ -1112,6 +1120,11 @@ begin else KeyEvAfterUp; end; +procedure TLCLCommonCallback.SetTabSuppress(ASuppress: Boolean); +begin + SuppressTabDown := ASuppress; +end; + procedure TLCLCommonCallback.MouseClick; begin LCLSendClickedMsg(Target); diff --git a/lcl/interfaces/cocoa/cocoawsstdctrls.pas b/lcl/interfaces/cocoa/cocoawsstdctrls.pas index e2bf8c9cd2..ea9425968f 100644 --- a/lcl/interfaces/cocoa/cocoawsstdctrls.pas +++ b/lcl/interfaces/cocoa/cocoawsstdctrls.pas @@ -1294,7 +1294,7 @@ begin TextViewSetWordWrap(txt, scr, TCustomMemo(AWinControl).WordWrap); TextViewSetAllignment(txt, TCustomMemo(AWinControl).Alignment); - txt.allowTabs := TCustomMemo(AWinControl).WantTabs; + txt.callback.SetTabSuppress(not TCustomMemo(AWinControl).WantTabs); Result := TLCLIntfHandle(scr); end; @@ -1457,7 +1457,7 @@ var begin txt := GetTextView(ACustomMemo); if (not Assigned(txt)) then Exit; - txt.allowTabs := NewWantTabs; + txt.callback.SetTabSuppress(not NewWantTabs); end; class procedure TCocoaWSCustomMemo.SetWordWrap(const ACustomMemo: TCustomMemo; const NewWordWrap: boolean);