Skip to content

Commit 6e05a9f

Browse files
committed
Update VTHeaderPopup
1 parent d07bd57 commit 6e05a9f

File tree

1 file changed

+46
-38
lines changed

1 file changed

+46
-38
lines changed

Source/VTHeaderPopup.pas

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
unit VTHeaderPopup;
22

33
//----------------------------------------------------------------------------------------------------------------------
4+
//
5+
// Version 4.7.0
6+
//
47
// The contents of this file are subject to the Mozilla Public License
58
// Version 1.1 (the "License"); you may not use this file except in
69
// compliance with the License. You may obtain a copy of the License at
@@ -43,7 +46,7 @@
4346
//
4447
// Modified 20 Oct 2002 by Borut Maricic <[email protected]>.
4548
// - Added the possibility to use Troy Wolbrink's Unicode aware popup menu.
46-
// Define the compiler symbol TNT to enable it. You can get Troy's Unicode
49+
// Define the compiler symbol TNT to enable it. You can get Troy's Unicode
4750
// controls collection from http://home.ccci.org/wolbrink/tnt/delphi_unicode_controls.htm.
4851
//
4952
// Modified 24 Feb 2002 by Ralf Junker <[email protected]>.
@@ -71,7 +74,8 @@ interface
7174
type
7275
TVTHeaderPopupOption = (
7376
poOriginalOrder, // Show menu items in original column order as they were added to the tree.
74-
poAllowHideAll // Allows to hide all columns, including the last one.
77+
poAllowHideAll, // Allows to hide all columns, including the last one.
78+
poResizeToFitItem // Adds an item which, if clicks, resizes all columns to fit by callung TVTHeader.AutoFitColumns
7579
);
7680
TVTHeaderPopupOptions = set of TVTHeaderPopupOption;
7781

@@ -85,12 +89,15 @@ interface
8589
var Cmd: TAddPopupItemType) of object;
8690
TColumnChangeEvent = procedure(const Sender: TBaseVirtualTree; const Column: TColumnIndex; Visible: Boolean) of object;
8791

92+
TVTMenuItem = TMenuItem;
93+
8894
TVTHeaderPopupMenu = class(TPopupMenu)
89-
private
95+
strict private
96+
FOptions: TVTHeaderPopupOptions;
97+
9098
FOnAddHeaderPopupItem: TAddHeaderPopupItemEvent;
9199
FOnColumnChange: TColumnChangeEvent;
92-
FOptions: TVTHeaderPopupOptions;
93-
protected
100+
strict protected
94101
procedure DoAddHeaderPopupItem(const Column: TColumnIndex; out Cmd: TAddPopupItemType); virtual;
95102
procedure DoColumnChange(Column: TColumnIndex; Visible: Boolean); virtual;
96103
procedure OnMenuItemClick(Sender: TObject);
@@ -110,10 +117,10 @@ implementation
110117
uses
111118
Classes;
112119

120+
const
121+
cResizeToFitMenuItemName = 'VT_ResizeToFitMenuItem';
113122
type
114123
TVirtualTreeCast = class(TBaseVirtualTree); // Necessary to make the header accessible.
115-
TVTMenuItem = class(TMenuItem)
116-
end;
117124

118125
//----------------- TVTHeaderPopupMenu ---------------------------------------------------------------------------------
119126

@@ -139,22 +146,28 @@ procedure TVTHeaderPopupMenu.DoColumnChange(Column: TColumnIndex; Visible: Boole
139146
procedure TVTHeaderPopupMenu.OnMenuItemClick(Sender: TObject);
140147

141148
begin
142-
if PopupComponent is TBaseVirtualTree then
143-
begin
144-
with TVTMenuItem(Sender),
145-
TVirtualTreeCast(PopupComponent).Header.Columns.Items[Tag] do
146-
begin
147-
if Checked then
148-
Options := Options - [coVisible]
149-
else
150-
Options := Options + [coVisible];
149+
if Assigned(PopupComponent) and (PopupComponent is TBaseVirtualTree) then begin
150+
if TVTMenuItem(Sender).Name = cResizeToFitMenuItemName then begin
151+
TVirtualTreeCast(PopupComponent).Header.AutoFitColumns();
152+
end
153+
else begin
154+
with TVTMenuItem(Sender),
155+
TVirtualTreeCast(PopupComponent).Header.Columns.Items[Tag] do
156+
begin
157+
if Checked then
158+
Options := Options - [coVisible]
159+
else
160+
Options := Options + [coVisible];
151161

152-
DoColumnChange(TVTMenuItem(Sender).Tag, not Checked);
153-
end;
162+
DoColumnChange(TVTMenuItem(Sender).Tag, not Checked);
163+
end;
164+
end;//else
154165
end;
155166
end;
156167

157168
//----------------------------------------------------------------------------------------------------------------------
169+
resourcestring
170+
sResizeToFit = '&Resize All Columns To Fit';
158171

159172
procedure TVTHeaderPopupMenu.Popup(x, y: Integer);
160173

@@ -168,35 +181,29 @@ procedure TVTHeaderPopupMenu.Popup(x, y: Integer);
168181
VisibleCounter: Cardinal;
169182
VisibleItem: TVTMenuItem;
170183

171-
CurrentMenuItem: TMenuItem;
172-
173184
begin
174-
if PopupComponent is TBaseVirtualTree then
185+
if Assigned(PopupComponent) and (PopupComponent is TBaseVirtualTree) then
175186
begin
176-
// Delete existing VT menu items. Keep normal ones
187+
// Delete existing menu items.
177188
while Items.Count > 0 do
178-
begin
179-
CurrentMenuItem := Items[Items.Count - 1];
180-
if CurrentMenuItem is TVTMenuItem then
181-
CurrentMenuItem.Free
182-
else
183-
break;
184-
end;
189+
Items[0].Free;
190+
191+
if poResizeToFitItem in Self.Options then begin
192+
NewMenuItem := NewItem(sResizeToFit, 0, False, True, OnMenuItemClick, 0, cResizeToFitMenuItemName);
193+
Items.Add(NewMenuItem);
194+
Items.Add(NewLine());
195+
end;//poResizeToFitItem
185196

186197
// Add column menu items.
187198
with TVirtualTreeCast(PopupComponent).Header do
188199
begin
189-
if Columns.Count = 0 then
190-
Exit;
200+
if hoShowImages in Options then
201+
Self.Images := Images
202+
else
203+
// Remove a possible reference to image list of another tree previously assigned.
204+
Self.Images := nil;
191205
VisibleItem := nil;
192206
VisibleCounter := 0;
193-
//add separator if necessary
194-
if Items.Count > 0 then
195-
begin
196-
NewMenuItem := TVTMenuItem.Create(Self);
197-
NewMenuItem.Caption := cLineCaption;
198-
Items.Add(NewMenuItem);
199-
end;
200207
for ColPos := 0 to Columns.Count - 1 do
201208
begin
202209
if poOriginalOrder in FOptions then
@@ -215,6 +222,7 @@ procedure TVTHeaderPopupMenu.Popup(x, y: Integer);
215222
NewMenuItem.Tag := ColIdx;
216223
NewMenuItem.Caption := Text;
217224
NewMenuItem.Hint := Hint;
225+
NewMenuItem.ImageIndex := ImageIndex;
218226
NewMenuItem.Checked := coVisible in Options;
219227
NewMenuItem.OnClick := OnMenuItemClick;
220228
if Cmd = apDisabled then

0 commit comments

Comments
 (0)