cocoa: common approach for handling TAB key and preventing Cocoa to switch the focus

git-svn-id: trunk@58995 -
This commit is contained in:
dmitry 2018-09-14 19:23:54 +00:00
parent 7ff50b386f
commit 5576446ed2
4 changed files with 18 additions and 14 deletions

View File

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

View File

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

View File

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

View File

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