Merge branch 'lvonchanging'

This commit is contained in:
Bart 2024-01-06 12:07:09 +01:00
commit 76bbc6e2e5
3 changed files with 58 additions and 2 deletions

View File

@ -1289,6 +1289,8 @@ type
TLVChangeEvent = procedure(Sender: TObject; Item: TListItem;
Change: TItemChange) of object;
TLVChangingEvent = procedure (Sender: TObject; Item: TListItem;
Change: TItemChange; var AllowChange: Boolean) of object;
TLVDataFindEvent = procedure(Sender: TObject; AFind: TItemFind;
const AFindString: string; const AFindPosition: TPoint; AFindData: Pointer;
@ -1429,6 +1431,7 @@ type
// FLastVertScrollInfo: TScrollInfo;
FUpdateCount: integer;
FOnChange: TLVChangeEvent;
FOnChanging: TLVChangingEvent;
FOnColumnClick: TLVColumnClickEvent;
FOnCompare: TLVCompareEvent;
FOnData: TLVDataEvent;
@ -1522,6 +1525,7 @@ type
function CreateListItems: TListItems; virtual;
function CanEdit(Item: TListItem): Boolean; virtual;
procedure Change(AItem: TListItem; AChange: Integer); virtual;
function CanChange(AItem: TListItem; AChange: Integer): Boolean;
procedure ColClick(AColumn: TListColumn); virtual;
procedure Delete(AItem : TListItem); virtual;
@ -1593,6 +1597,7 @@ type
property ToolTips: Boolean index Ord(lvpToolTips) read GetProperty write SetProperty default True;
property ViewStyle: TViewStyle read FViewStyle write SetViewStyle default vsList;
property OnChange: TLVChangeEvent read FOnChange write FOnChange;
property OnChanging: TLVChangingEvent read FOnChanging write FOnChanging;
property OnColumnClick: TLVColumnClickEvent read FOnColumnClick write FOnColumnClick;
property OnCompare: TLVCompareEvent read FOnCompare write FOnCompare;
property OnCreateItemClass: TLVCreateItemClassEvent read FOnCreateItemClass write FOnCreateItemClass;
@ -1741,6 +1746,7 @@ type
property OnAdvancedCustomDrawItem;
property OnAdvancedCustomDrawSubItem;
property OnChange;
property OnChanging;
property OnClick;
property OnColumnClick;
property OnCompare;

View File

@ -205,6 +205,22 @@ begin
then FOnChange(Self, AItem, ItemChange);
end;
function TCustomListView.CanChange(AItem: TListItem; AChange: Integer): Boolean;
var
ItemChange: TItemChange;
begin
Result := True;
case AChange of
LVIF_TEXT: ItemChange := ctText;
LVIF_IMAGE: ItemChange := ctImage;
LVIF_STATE: ItemChange := ctState;
else
Exit;
end;
if Assigned(FOnChanging) then
FOnChanging(Self, AItem, ItemChange, Result);
end;
{------------------------------------------------------------------------------}
{ TCustomListView ColClick }
{------------------------------------------------------------------------------}
@ -261,6 +277,7 @@ var
Item: TListItem;
n: Integer;
oldSelected: TListItem;
AllowChange: Boolean;
begin
nm := PNMListView(AMessage.NMHdr);
// ignore any notifications while initializing items
@ -295,6 +312,7 @@ begin
// LVN_GETDISPINFO:
// LVN_ODCACHEHINT:
// LVN_ODFINDITEM: implemented in win32 widgetset
// LVN_ITEMCHANGING: implemented in win32 widgetset
// LVN_ODSTATECHANGED:
// LVN_BEGINLABELEDIT: implemented via TCustomListViewEditor
// LVN_ENDLABELEDIT: implemented via TCustomListViewEditor
@ -308,10 +326,14 @@ begin
// see delete
// besides... who's inserting items
end;
LVN_ITEMCHANGING: begin
//Check
//Currently implemented in win32 widgeset only
//in response CanChange should be called and the result of that function most likely
//should be handled at WS level (if at all possible for non-windows)
end;
LVN_ITEMCHANGED:
LVN_ITEMCHANGED:
begin
if nm^.iItem < 0 then
Item := nil

View File

@ -296,6 +296,32 @@ var
end;
end;
procedure HandleListViewChanging(ALV: TCustomListViewAccess);
var
nm: PNMListView;
Item: TListItem;
begin
//debugln(['HandleListChanging: HandleAllocated=',ALV.HandleAllocated,', (wcfInitializing in FWinControlFlags)=',Dbgs(wcfInitializing in ALV.FWinControlFlags)]);
if (wcfInitializing in ALV.FWinControlFlags) then Exit;
nm := PNMListView(NMHdr);
if nm^.iItem < 0 then
Item := nil
else
Item := ALV.Items[nm^.iItem];
if Assigned(Item) then
begin
//https://learn.microsoft.com/en-us/windows/win32/controls/lvn-itemchanging
//Returns TRUE to prevent the change, or FALSE to allow the change
//So the opposite of the result of CanChange function
if ALV.CanChange(Item, nm^.uChanged) then
MsgResult := 0 //Ord(BOOL(False))
else
MsgResult := 1; //Ord(BOOL(True))
end;
WinProcess := False;
end;
procedure HandleListViewOwnerDataHint(ALV: TCustomListViewAccess);
var
DataHintInfo: PNMLVCACHEHINT;
@ -323,6 +349,8 @@ begin
end;
LVN_ODFINDITEM:
HandleListViewFindItem(TCustomListViewAccess(AWinControl));
LVN_ITEMCHANGING:
HandleListViewChanging(TCustomListViewAccess(AWinControl));
LVN_ODCACHEHINT:
HandleListViewOwnerDataHint(TCustomListViewAccess(AWinControl));
end;