Cocoa/ListView: Change the control that CaptionEditor is attached to from ListView (layer 1) to backendControl (layer 3) so that it can follow the scrolling

This commit is contained in:
rich2014 2024-08-19 23:18:55 +08:00
parent 152ed000a7
commit fd6b0b516b
5 changed files with 33 additions and 49 deletions

View File

@ -731,12 +731,15 @@ begin
end;
procedure TCocoaCollectionView.addSubview(aView: NSView);
var
textField: TCocoaTextField Absolute aView;
begin
inherited;
if NOT aView.isKindOfClass(TCocoaTextField) then
Exit;
if NOT Assigned(self.callback) then
Exit;
if self.callback.onAddSubview(aView) then
Exit;
inherited addSubview(aView);
TCocoaListView(self.callback.Owner).setCaptionEditor( textField );
end;
procedure TCocoaCollectionView.updateItemValue(
@ -1073,7 +1076,6 @@ function TCocoaWSListView_CollectionViewHandler.ItemDisplayRect(const AIndex,
var
item: NSCollectionViewItem;
frame: NSRect;
rect: TRect;
begin
Result:= Bounds(0,0,0,0);
item:= _collectionView.itemAtIndex( AIndex );
@ -1089,11 +1091,7 @@ begin
_listView.getLCLControlCanvas.Font.Height:= Round(item.textField.font.pointSize);
_collectionView.styleHandler.onAdjustTextEditor( _listView );
frame:= item.textField.frame;
NSToLCLRect( frame, item.view.frame.size.height, rect );
item.view.lclLocalToScreen( rect.left, rect.top );
_listView.lclScreenToLocal( rect.left, rect.top );
frame.origin.x:= rect.left;
frame.origin.y:= rect.top;
frame:= item.view.convertRect_toView( frame, _collectionView );
end;
drIcon:
begin

View File

@ -41,7 +41,6 @@ type
function getItemStableSelection(ARow: Integer): Boolean;
procedure ColumnClicked(ACol: Integer);
function onAddSubview( aView:NSView ): Boolean;
function drawItem( row: Integer; ctx: TCocoaContext; const r: TRect; state: TOwnerDrawState ): Boolean;
function customDraw( row: Integer; col: Integer; ctx: TCocoaContext; state: TCustomDrawState ): Boolean;
@ -75,7 +74,6 @@ type
function shouldSelectionChange(NewSel: Integer): Boolean; virtual; abstract;
procedure ColumnClicked(ACol: Integer); virtual; abstract;
function onAddSubview( aView:NSView ): Boolean; virtual; abstract;
function drawItem( row: Integer; ctx: TCocoaContext; const r: TRect; state: TOwnerDrawState ): Boolean; virtual; abstract;
function customDraw( row: Integer; col: Integer; ctx: TCocoaContext; state: TCustomDrawState ): Boolean; virtual; abstract;

View File

@ -106,7 +106,6 @@ type
function isCustomDrawSupported: Boolean; override;
procedure GetRowHeight(rowidx: Integer; var h: Integer); override;
function GetBorderStyle: TBorderStyle; override;
function onAddSubview(aView: NSView): Boolean; override;
procedure selectOne(ARow: Integer; isSelected:Boolean );
procedure callTargetInitializeWnd;
@ -180,7 +179,7 @@ type
_WSHandler: TCocoaWSListViewHandler;
_needsCallLclInit: Boolean;
_initializing: Boolean;
_captionEditor: NSTextField;
_captionEditor: TCocoaTextField;
_captionFont: NSFont;
_captionAlignment: NSTextAlignment;
private
@ -204,7 +203,7 @@ type
function initializing: Boolean; message 'isinitializing';
function getLCLControlCanvas: TCanvas; message 'getLCLControlCanvas';
procedure setCaptionEditor( captionEditor: NSTextField ); message 'setCaptionEditor:';
procedure setCaptionEditor( captionEditor: TCocoaTextField ); message 'setCaptionEditor:';
procedure setCaptionFont( captionFont: NSFont ); message 'setCaptionFont:';
procedure setCaptionAlignment( alignment: NSTextAlignment ); message 'setCaptionAlignment:';
end;
@ -240,12 +239,19 @@ begin
Result:= TCustomListView(self.callback.Target).Canvas;
end;
procedure TCocoaListView.setCaptionEditor(captionEditor: NSTextField);
procedure TCocoaListView.setCaptionEditor(captionEditor: TCocoaTextField);
begin
_captionEditor:= captionEditor;
if Assigned(_captionEditor) then
_captionEditor.release;
_captionEditor:= captionEditor.retain;
_captionEditor.setBezeled( False );
_captionEditor.setFocusRingType( NSFocusRingTypeExterior );
_captionEditor.setAlignment( _captionAlignment );
_captionEditor.fixedInitSetting:= True;
if Assigned(_captionFont) then
_captionEditor.setFont( _captionFont );
_captionEditor.setAlignment( _captionAlignment );
end;
procedure TCocoaListView.setCaptionFont(captionFont: NSFont);
@ -253,7 +259,11 @@ begin
if Assigned(_captionFont) then
_captionFont.release;
_captionFont:= captionFont.retain;
_captionEditor.setFont( _captionFont );
if Assigned(_captionEditor) then begin
_captionEditor.removeFromSuperview;
_backendControl.addSubview_positioned_relativeTo( _captionEditor, NSWindowAbove, nil );
_captionEditor.setFont( _captionFont );
end;
end;
procedure TCocoaListView.setCaptionAlignment( alignment: NSTextAlignment );
@ -350,6 +360,7 @@ end;
procedure TCocoaListView.dealloc;
begin
self.releaseControls;
_captionEditor.release;
_captionFont.release;
inherited dealloc;
end;
@ -591,23 +602,6 @@ begin
Result:= TCustomListView(Target).BorderStyle;
end;
function TLCLListViewCallback.onAddSubview(aView: NSView): Boolean;
var
field: TCocoaTextField;
begin
Result:= False;
if NOT aView.isKindOfClass(TCocoaTextField) then
Exit;
field:= TCocoaTextField( aView );
field.setBezeled( False );
field.setFocusRingType( NSFocusRingTypeExterior );
field.fixedInitSetting:= True;
TCocoaListView(self.Owner).addSubview( field ); // add to TCocoaListView
TCocoaListView(self.Owner).setCaptionEditor( field ); // add to TCocoaListView
Result:= True;
end;
function TLCLListViewCallback.GetImageListType( out lvil: TListViewImageList ): Boolean;
const
preferredImages: array [TViewStyle] of TListViewImageList = (

View File

@ -452,12 +452,17 @@ begin
end;
procedure TCocoaTableListView.addSubview(aView: NSView);
var
textField: TCocoaTextField Absolute aView;
begin
inherited;
if NOT aView.isKindOfClass(TCocoaTextField) then
Exit;
if NOT Assigned(self.callback) then
Exit;
if self.callback.onAddSubview(aView) then
if NOT (self.callback.Owner.isKindOfClass(TCocoaListView)) then
Exit;
inherited addSubview(aView);
TCocoaListView(self.callback.Owner).setCaptionEditor( textField );
end;
function TCocoaTableListView.lclGetCanvas: TCanvas;
@ -1671,7 +1676,6 @@ function TCocoaWSListView_TableViewHandler.ItemDisplayRect(const AIndex,
var
item: TCocoaTableListItem;
frame: NSRect;
rect: TRect;
begin
Result:= Bounds(0,0,0,0);
item:= _tableView.viewAtColumn_row_makeIfNecessary( ASubItem, AIndex, True );
@ -1687,11 +1691,7 @@ begin
// to do: completely restore TFont
_listView.getLCLControlCanvas.Font.Height:= Round(item.textField.font.pointSize);
frame:= item.textField.frame;
NSToLCLRect( frame, item.frame.size.height, rect );
item.lclLocalToScreen( rect.left, rect.top );
_listView.lclScreenToLocal( rect.left, rect.top );
frame.origin.x:= rect.left;
frame.origin.y:= rect.top;
frame:= item.convertRect_toView( frame, _tableView );
end;
drIcon:
begin

View File

@ -292,7 +292,6 @@ type
function isCustomDrawSupported: Boolean; override;
procedure GetRowHeight(rowidx: integer; var h: Integer); override;
function GetBorderStyle: TBorderStyle; override;
function onAddSubview(aView: NSView): Boolean; override;
end;
TLCLListBoxCallBackClass = class of TLCLListBoxCallBack;
@ -717,11 +716,6 @@ begin
Result:= TCustomListBox(Target).BorderStyle;
end;
function TLCLListBoxCallback.onAddSubview(aView: NSView): Boolean;
begin
Result:= False;
end;
{ TCocoaListBoxStringList }
procedure TCocoaListBoxStringList.InsertItem(Index: Integer; const S: string;