Cocoa/ListView: fix and improve ImageList and IconSize related

This commit is contained in:
rich2014 2024-07-17 22:47:06 +08:00
parent 400b5fb630
commit d77896132e
4 changed files with 88 additions and 36 deletions

View File

@ -9,7 +9,7 @@ unit CocoaCollectionView;
interface
uses
Classes, SysUtils, Controls, ComCtrls,
Classes, SysUtils, Controls,
MacOSAll, CocoaAll, CocoaPrivate, Cocoa_Extra, CocoaUtils,
CocoaWSComCtrls, CocoaTextEdits;
@ -48,7 +48,7 @@ type
procedure backend_setCallback( cb: TLCLListViewCallback );
procedure backend_reloadData;
procedure backend_onInit( lclListView: TCustomListView );
procedure backend_onInit;
public
procedure updateItemValue( indexPath:NSIndexPath; cocoaItem:TCocoaCollectionItem );
message 'updateItemValue:cocoaItem:';
@ -242,27 +242,23 @@ begin
self.reloadData;
end;
procedure TCocoaCollectionView.backend_onInit( lclListView: TCustomListView );
procedure TCocoaCollectionView.backend_onInit;
begin
end;
procedure TCocoaCollectionView.updateItemValue(
indexPath:NSIndexPath; cocoaItem: TCocoaCollectionItem );
var
lclListView: TCustomListView;
lclItem: TListItem;
cocoaImage: NSImage;
lclImageIndex: Integer;
lclText: String;
isSelected: Boolean;
begin
lclListView:= TCustomListView( self.lclGetTarget );
if NOT Assigned(lclListView) then
Exit;
if NOT Assigned(self.callback) then
Exit;
lclItem:= lclListView.Items[indexPath.item];
cocoaImage:= self.callback.GetImageFromIndex( lclItem.ImageIndex );
self.callback.GetItemImageAt( indexPath.item, 0, lclImageIndex );
cocoaImage:= self.callback.GetImageFromIndex( lclImageIndex );
cocoaItem.imageView.setImage( cocoaImage );
self.callback.GetItemTextAt( indexPath.item, 0, lclText );

View File

@ -31,7 +31,7 @@ uses
MacOSAll, CocoaAll, CocoaUtils, CocoaGDIObjects,
cocoa_extra, CocoaPrivate, CocoaConst, CocoaConfig,
// LCL
LCLType;
LCLType, Controls;
type
@ -50,6 +50,7 @@ type
procedure ColumnClicked(ACol: Integer);
procedure DrawRow(rowidx: Integer; ctx: TCocoaContext; const r: TRect; state: TOwnerDrawState);
procedure GetRowHeight(rowidx: Integer; var height: Integer);
function GetBorderStyle: TBorderStyle;
end;
{ TCocoaStringList }
@ -237,7 +238,7 @@ const
implementation
uses
ComCtrls, CocoaWSComCtrls, CocoaWSCommon;
CocoaWSComCtrls, CocoaWSCommon;
type
{ TCellCocoaTableListView }
@ -250,7 +251,7 @@ type
public
procedure backend_setCallback( cb: TLCLListViewCallback );
procedure backend_reloadData;
procedure backend_onInit( lclListView: TCustomListView );
procedure backend_onInit;
public
function tableView_objectValueForTableColumn_row(tableView: NSTableView; tableColumn: NSTableColumn; row: NSInteger): id; message 'tableView:objectValueForTableColumn:row:';
procedure tableView_setObjectValue_forTableColumn_row(tableView: NSTableView; object_: id; tableColumn: NSTableColumn; row: NSInteger); message 'tableView:setObjectValue:forTableColumn:row:';
@ -1008,7 +1009,7 @@ begin
self.reloadData;
end;
procedure TCellCocoaTableListView.backend_onInit(lclListView: TCustomListView );
procedure TCellCocoaTableListView.backend_onInit;
var
sz: NSSize;
begin
@ -1017,7 +1018,7 @@ begin
self.setAllowsColumnReordering(False);
self.setAllowsColumnSelection(False);
UpdateFocusRing( self, lclListView.BorderStyle );
UpdateFocusRing( self, self.callback.getBorderStyle );
sz := self.intercellSpacing;
// Windows compatibility. on Windows there's no extra space between columns

View File

@ -188,6 +188,9 @@ type
procedure DrawRow(rowidx: Integer; ctx: TCocoaContext; const r: TRect;
state: TOwnerDrawState);
procedure GetRowHeight(rowidx: Integer; var h: Integer);
function GetBorderStyle: TBorderStyle;
function GetImageListType( out lvil: TListViewImageList ): Boolean;
procedure callTargetInitializeWnd;
end;
TLCLListViewCallBackClass = class of TLCLListViewCallback;
@ -195,8 +198,7 @@ type
TCocoaListViewBackendControlProtocol = objcprotocol
procedure backend_setCallback( cb: TLCLListViewCallback ); message 'backend_setCallback:';
procedure backend_reloadData; message 'backend_reloadData';
procedure backend_onInit( lclListView: TCustomListView );
message 'backend_onInit:';
procedure backend_onInit; message 'backend_onInit';
end;
{ TCocoaListView }
@ -207,7 +209,6 @@ type
_scrollView: TCocoaScrollView;
_backendControl: NSView; // NSTableView or NSCollectionView
_WSHandler: TCocoaWSListViewHandler;
_lclListView: TCustomListView;
_needsCallLclInit: Boolean;
_initializing: Boolean;
private
@ -222,7 +223,6 @@ type
public
procedure dealloc; override;
public
procedure setLclListView( lclListView: TCustomListView ); message 'setLclListView:';
procedure setViewStyle( viewStyle: TViewStyle ); message 'setViewStyle:';
function documentView: NSView; message 'documentView';
function scrollView: TCocoaScrollView; message 'scrollView';
@ -1299,9 +1299,18 @@ end;
procedure TCocoaWSListView_CollectionViewHandler.SetImageList(
const AList: TListViewImageList; const AValue: TCustomImageListResolution);
var
lclcb: TLCLListViewCallback;
lvil: TListViewImageList;
iconSize: NSSize;
begin
if AList=lvilState then
lclcb:= getCallback;
if NOT Assigned(lclcb) then
Exit;
if NOT lclcb.GetImageListType(lvil) then
Exit;
if AList <> lvil then
Exit;
iconSize.Width:= AValue.Width;
@ -2081,11 +2090,11 @@ begin
_scrollView.setAutoresizingMask( NSViewWidthSizable or NSViewHeightSizable );
_scrollView.callback:= self.callback;
self.setScrollView( _scrollView );
ScrollViewSetBorderStyle( _scrollView, _lclListView.BorderStyle);
ScrollViewSetBorderStyle( _scrollView, callback.getBorderStyle );
backendControlAccess:= TCocoaListViewBackendControlProtocol(_backendControl);
backendControlAccess.backend_onInit( _lclListView );
backendControlAccess.backend_setCallback( self.callback );
backendControlAccess.backend_onInit;
end;
procedure TCocoaListView.releaseControls;
@ -2109,7 +2118,7 @@ begin
_needsCallLclInit:= False;
if needsInit then begin
_initializing:= True;
TCustomListViewAccess(_lclListView).InitializeWnd;
callback.callTargetInitializeWnd;
_initializing:= False;
TCocoaListViewBackendControlProtocol(_backendControl).backend_reloadData;
end;
@ -2137,11 +2146,6 @@ begin
inherited dealloc;
end;
procedure TCocoaListView.setLclListView(lclListView: TCustomListView);
begin
_lclListView:= lclListView;
end;
{ TCocoaWSCustomListView }
class function TCocoaWSCustomListView.getWSHandler(
@ -2179,7 +2183,6 @@ var
lclcb: TLCLListViewCallback;
begin
cocoaListView:= TCocoaListView.alloc.lclInitWithCreateParams(AParams);
cocoaListView.setLclListView( lclListView );
cocoaListView.setAutoresizesSubviews( True );
lclcb := TLCLListViewCallback.Create( cocoaListView, lclListView, cocoaListView );
lclcb.listView := lclListView;
@ -2756,23 +2759,30 @@ end;
function TLCLListViewCallback.GetImageFromIndex(imgIdx: Integer): NSImage;
var
bmp : TBitmap;
lvil: TListViewImageList;
lst : TCustomImageList;
x,y : integer;
img : NSImage;
rep : NSBitmapImageRep;
cb : TCocoaBitmap;
begin
lst := TCustomListViewAccess(listView).LargeImages;
if NOT Assigned(lst) then
lst := TCustomListViewAccess(listView).SmallImages;
Result:= nil;
if imgIdx < 0 then
Exit;
if NOT self.GetImageListType( lvil ) then
Exit;
if lvil = lvilLarge then
lst:= TCustomListViewAccess(listView).LargeImages
else
lst:= TCustomListViewAccess(listView).SmallImages;
bmp := TBitmap.Create;
try
lst.GetBitmap(imgIdx, bmp);
if bmp.Handle = 0 then begin
Result := nil;
if bmp.Handle = 0 then
Exit;
end;
// Bitmap Handle should be nothing but TCocoaBitmap
cb := TCocoaBitmap(bmp.Handle);
@ -3003,6 +3013,45 @@ begin
end;
function TLCLListViewCallback.GetBorderStyle: TBorderStyle;
begin
Result:= TCustomListView(Target).BorderStyle;
end;
function TLCLListViewCallback.GetImageListType( out lvil: TListViewImageList ): Boolean;
const
preferredImages: array [TViewStyle] of TListViewImageList = (
lvilLarge, lvilSmall, lvilSmall, lvilSmall );
alternativeImages: array [TViewStyle] of TListViewImageList = (
lvilSmall, lvilLarge, lvilLarge, lvilLarge );
var
viewStyle: TViewStyle;
LVA: TCustomListViewAccess;
begin
Result:= True;
LVA:= TCustomListViewAccess(listView);
viewStyle:= LVA.ViewStyle;
lvil:= preferredImages[viewStyle];
if (lvil=lvilLarge) and Assigned(LVA.LargeImages) then
Exit;
if (lvil=lvilSmall) and Assigned(LVA.SmallImages) then
Exit;
lvil:= alternativeImages[viewStyle];
if (lvil=lvilLarge) and Assigned(LVA.LargeImages) then
Exit;
if (lvil=lvilSmall) and Assigned(LVA.SmallImages) then
Exit;
Result:= False;
end;
procedure TLCLListViewCallback.callTargetInitializeWnd;
begin
TCustomListViewAccess(Target).InitializeWnd;
end;
{ TCocoaWSTrackBar }
{------------------------------------------------------------------------------

View File

@ -279,6 +279,7 @@ type
procedure ColumnClicked(ACol: Integer); virtual;
procedure DrawRow(rowidx: Integer; ctx: TCocoaContext; const r: TRect; state: TOwnerDrawState); virtual;
procedure GetRowHeight(rowidx: integer; var h: Integer); virtual;
function GetBorderStyle: TBorderStyle;
end;
TLCLListBoxCallBackClass = class of TLCLListBoxCallBack;
@ -714,6 +715,11 @@ begin
TCustomListBox(Target).MeasureItem(rowidx, h);
end;
function TLCLListBoxCallback.GetBorderStyle: TBorderStyle;
begin
Result:= TCustomListBox(Target).BorderStyle;
end;
{ TLCLCheckBoxCallback }
procedure TLCLCheckBoxCallback.ButtonClick;