1
1
unit VTHeaderPopup;
2
2
3
3
// ----------------------------------------------------------------------------------------------------------------------
4
+ //
5
+ // Version 4.7.0
6
+ //
4
7
// The contents of this file are subject to the Mozilla Public License
5
8
// Version 1.1 (the "License"); you may not use this file except in
6
9
// compliance with the License. You may obtain a copy of the License at
43
46
//
44
47
// Modified 20 Oct 2002 by Borut Maricic <[email protected] >.
45
48
// - 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
47
50
// controls collection from http://home.ccci.org/wolbrink/tnt/delphi_unicode_controls.htm.
48
51
//
49
52
// Modified 24 Feb 2002 by Ralf Junker <[email protected] >.
@@ -71,7 +74,8 @@ interface
71
74
type
72
75
TVTHeaderPopupOption = (
73
76
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
75
79
);
76
80
TVTHeaderPopupOptions = set of TVTHeaderPopupOption;
77
81
@@ -85,12 +89,15 @@ interface
85
89
var Cmd: TAddPopupItemType) of object ;
86
90
TColumnChangeEvent = procedure(const Sender: TBaseVirtualTree; const Column: TColumnIndex; Visible: Boolean) of object ;
87
91
92
+ TVTMenuItem = TMenuItem;
93
+
88
94
TVTHeaderPopupMenu = class (TPopupMenu)
89
- private
95
+ strict private
96
+ FOptions: TVTHeaderPopupOptions;
97
+
90
98
FOnAddHeaderPopupItem: TAddHeaderPopupItemEvent;
91
99
FOnColumnChange: TColumnChangeEvent;
92
- FOptions: TVTHeaderPopupOptions;
93
- protected
100
+ strict protected
94
101
procedure DoAddHeaderPopupItem (const Column: TColumnIndex; out Cmd: TAddPopupItemType); virtual ;
95
102
procedure DoColumnChange (Column: TColumnIndex; Visible: Boolean); virtual ;
96
103
procedure OnMenuItemClick (Sender: TObject);
@@ -110,10 +117,10 @@ implementation
110
117
uses
111
118
Classes;
112
119
120
+ const
121
+ cResizeToFitMenuItemName = ' VT_ResizeToFitMenuItem' ;
113
122
type
114
123
TVirtualTreeCast = class (TBaseVirtualTree); // Necessary to make the header accessible.
115
- TVTMenuItem = class (TMenuItem)
116
- end ;
117
124
118
125
// ----------------- TVTHeaderPopupMenu ---------------------------------------------------------------------------------
119
126
@@ -139,22 +146,28 @@ procedure TVTHeaderPopupMenu.DoColumnChange(Column: TColumnIndex; Visible: Boole
139
146
procedure TVTHeaderPopupMenu.OnMenuItemClick (Sender: TObject);
140
147
141
148
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];
151
161
152
- DoColumnChange(TVTMenuItem(Sender).Tag, not Checked);
153
- end ;
162
+ DoColumnChange(TVTMenuItem(Sender).Tag, not Checked);
163
+ end ;
164
+ end ;// else
154
165
end ;
155
166
end ;
156
167
157
168
// ----------------------------------------------------------------------------------------------------------------------
169
+ resourcestring
170
+ sResizeToFit = ' &Resize All Columns To Fit' ;
158
171
159
172
procedure TVTHeaderPopupMenu.Popup (x, y: Integer);
160
173
@@ -168,35 +181,29 @@ procedure TVTHeaderPopupMenu.Popup(x, y: Integer);
168
181
VisibleCounter: Cardinal;
169
182
VisibleItem: TVTMenuItem;
170
183
171
- CurrentMenuItem: TMenuItem;
172
-
173
184
begin
174
- if PopupComponent is TBaseVirtualTree then
185
+ if Assigned( PopupComponent) and (PopupComponent is TBaseVirtualTree) then
175
186
begin
176
- // Delete existing VT menu items. Keep normal ones
187
+ // Delete existing menu items.
177
188
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
185
196
186
197
// Add column menu items.
187
198
with TVirtualTreeCast(PopupComponent).Header do
188
199
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 ;
191
205
VisibleItem := nil ;
192
206
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 ;
200
207
for ColPos := 0 to Columns.Count - 1 do
201
208
begin
202
209
if poOriginalOrder in FOptions then
@@ -215,6 +222,7 @@ procedure TVTHeaderPopupMenu.Popup(x, y: Integer);
215
222
NewMenuItem.Tag := ColIdx;
216
223
NewMenuItem.Caption := Text;
217
224
NewMenuItem.Hint := Hint;
225
+ NewMenuItem.ImageIndex := ImageIndex;
218
226
NewMenuItem.Checked := coVisible in Options;
219
227
NewMenuItem.OnClick := OnMenuItemClick;
220
228
if Cmd = apDisabled then
0 commit comments