-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMenuPanelUnit.pas
More file actions
345 lines (308 loc) · 8.85 KB
/
MenuPanelUnit.pas
File metadata and controls
345 lines (308 loc) · 8.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
unit MenuPanelUnit;
interface
uses
System.SysUtils, System.Classes, Vcl.Controls, Vcl.ExtCtrls, Vcl.Graphics,
Vcl.Menus, Winapi.Windows, Winapi.Messages, System.Math;
type
TMenuPanel = class(TPanel)
private
FMainMenu: TMainMenu;
FHoverIndex: Integer;
FMenuX: Integer;
FMenuY: Integer;
// New properties for custom drawing and rounded corners.
FMenuBackgroundColor: TColor;
FMenuSelectionColor: TColor;
FMenuTextColor: TColor;
FBorderRadius: Integer;
// A helper method for the MainMenu property setter.
procedure SetMainMenu(const Value: TMainMenu);
procedure SetMenuX(const Value: Integer);
procedure SetMenuY(const Value: Integer);
procedure SetMenuBackgroundColor(const Value: TColor);
procedure SetMenuSelectionColor(const Value: TColor);
procedure SetMenuTextColor(const Value: TColor);
procedure SetBorderRadius(const Value: Integer);
// Event handlers for when the mouse leaves or clicks on the panel.
procedure DoMouseLeave(Sender: TObject);
procedure DoMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure DoPopup(Sender: TObject);
procedure PopulateSubmenu(ParentMenuItem: TMenuItem; TargetMenu: TPopupMenu);
protected
procedure Paint; override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
// MouseLeave is not an overridable method in TPanel, so we must use an event handler.
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property MainMenu: TMainMenu read FMainMenu write SetMainMenu;
property MenuX: Integer read FMenuX write SetMenuX default 8;
property MenuY: Integer read FMenuY write SetMenuY default 4;
property MenuBackgroundColor: TColor read FMenuBackgroundColor write SetMenuBackgroundColor default clWhite;
property MenuSelectionColor: TColor read FMenuSelectionColor write SetMenuSelectionColor default clHighlight;
property MenuTextColor: TColor read FMenuTextColor write SetMenuTextColor default clBlack;
property BorderRadius: Integer read FBorderRadius write SetBorderRadius default 0;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('FluidVCL', [TMenuPanel]);
end;
{ TMenuPanel }
constructor TMenuPanel.Create(AOwner: TComponent);
begin
inherited;
FHoverIndex := -1;
FMenuX := 8;
FMenuY := 4;
FMenuBackgroundColor := clWhite;
FMenuSelectionColor := clHighlight;
FMenuTextColor := clBlack;
FBorderRadius := 0;
OnMouseLeave := DoMouseLeave;
OnMouseDown := DoMouseDown;
end;
destructor TMenuPanel.Destroy;
begin
inherited;
end;
procedure TMenuPanel.SetMainMenu(const Value: TMainMenu);
begin
if FMainMenu <> Value then
begin
FMainMenu := Value;
Invalidate;
end;
end;
procedure TMenuPanel.SetMenuX(const Value: Integer);
begin
if FMenuX <> Value then
begin
FMenuX := Value;
Invalidate;
end;
end;
procedure TMenuPanel.SetMenuY(const Value: Integer);
begin
if FMenuY <> Value then
begin
FMenuY := Value;
Invalidate;
end;
end;
procedure TMenuPanel.SetMenuBackgroundColor(const Value: TColor);
begin
if FMenuBackgroundColor <> Value then
begin
FMenuBackgroundColor := Value;
Invalidate;
end;
end;
procedure TMenuPanel.SetMenuSelectionColor(const Value: TColor);
begin
if FMenuSelectionColor <> Value then
begin
FMenuSelectionColor := Value;
Invalidate;
end;
end;
procedure TMenuPanel.SetMenuTextColor(const Value: TColor);
begin
if FMenuTextColor <> Value then
begin
FMenuTextColor := Value;
Invalidate;
end;
end;
procedure TMenuPanel.SetBorderRadius(const Value: Integer);
begin
if FBorderRadius <> Value then
begin
FBorderRadius := Value;
Invalidate;
end;
end;
procedure TMenuPanel.Paint;
var
i: Integer;
X: Integer;
R: TRect;
Item: TMenuItem;
begin
inherited; // Call the inherited Paint method to draw the panel background.
if Assigned(FMainMenu) and (FMainMenu.Items.Count > 0) then
begin
with Canvas do
begin
Brush.Color := Self.Color;
FillRect(ClientRect);
X := FMenuX;
for i := 0 to FMainMenu.Items.Count - 1 do
begin
Item := FMainMenu.Items[i];
R := Rect(X, 0, X + TextWidth(Item.Caption) + 24, Self.Height);
if i = FHoverIndex then
begin
Brush.Color := FMenuSelectionColor;
Pen.Style := psClear;
RoundRect(R.Left, R.Top, R.Right, R.Bottom, FBorderRadius, FBorderRadius);
Font.Color := FMenuTextColor;
end
else
begin
Brush.Color := Self.Color;
FillRect(R);
Font.Color := Self.Font.Color;
end;
Font.Style := [];
Pen.Style := psSolid;
DrawText(Handle, PChar(Item.Caption), -1, R, DT_SINGLELINE or DT_VCENTER or DT_CENTER);
Inc(X, TextWidth(Item.Caption) + 24);
end;
end;
end;
end;
procedure TMenuPanel.MouseMove(Shift: TShiftState; X, Y: Integer);
var
i, PosX: Integer;
Item: TMenuItem;
NewHover: Integer;
begin
inherited; // Call the inherited MouseMove method first.
if Assigned(FMainMenu) and (FMainMenu.Items.Count > 0) then
begin
PosX := FMenuX;
NewHover := -1;
for i := 0 to FMainMenu.Items.Count - 1 do
begin
Item := FMainMenu.Items[i];
if (X >= PosX) and (X <= PosX + Canvas.TextWidth(Item.Caption) + 24) and
(Y >= 0) and (Y <= Self.Height) then
begin
NewHover := i;
Break; // Found the item, no need to continue the loop.
end;
Inc(PosX, Canvas.TextWidth(Item.Caption) + 24);
end;
if NewHover <> FHoverIndex then
begin
FHoverIndex := NewHover;
Invalidate;
end;
end;
end;
procedure TMenuPanel.DoMouseLeave(Sender: TObject);
begin
if FHoverIndex <> -1 then
begin
FHoverIndex := -1;
Invalidate; // Invalidate to redraw the panel without the hover effect.
end;
end;
procedure TMenuPanel.DoPopup(Sender: TObject);
var
Popup: TPopupMenu;
Rgn: HRGN;
i, MenuWidth, MenuHeight: Integer;
Item: TMenuItem;
ACanvas: TCanvas;
begin
Popup := TPopupMenu(Sender);
if (Assigned(Popup)) and (FBorderRadius > 0) then
begin
ACanvas := TCanvas.Create;
try
ACanvas.Handle := Popup.Handle;
ACanvas.Font.Assign(Self.Font); // Use the font from our panel for consistent measurement
MenuWidth := 0;
MenuHeight := 0;
for i := 0 to Popup.Items.Count - 1 do
begin
Item := Popup.Items[i];
if Item.Caption <> '-' then // Account for separators.
begin
MenuWidth := Max(MenuWidth, ACanvas.TextWidth(Item.Caption) + 40);
MenuHeight := MenuHeight + 24;
end
else
begin
MenuHeight := MenuHeight + 12;
end;
end;
Inc(MenuWidth, 1);
Rgn := CreateRoundRectRgn(0, 0, MenuWidth, MenuHeight, FBorderRadius, FBorderRadius);
SetWindowRgn(Popup.Handle, Rgn, True);
DeleteObject(Rgn);
finally
ACanvas.Free;
end;
end;
end;
procedure TMenuPanel.PopulateSubmenu(ParentMenuItem: TMenuItem; TargetMenu: TPopupMenu);
var
SubItem, NewSubItem: TMenuItem;
i: Integer;
begin
for i := 0 to ParentMenuItem.Count - 1 do
begin
SubItem := ParentMenuItem.Items[i];
NewSubItem := TMenuItem.Create(TargetMenu);
with NewSubItem do
begin
Caption := SubItem.Caption;
ShortCut := SubItem.ShortCut;
OnClick := SubItem.OnClick;
Hint := SubItem.Hint;
Checked := SubItem.Checked;
Enabled := SubItem.Enabled;
RadioItem := SubItem.RadioItem;
GroupIndex := SubItem.GroupIndex;
ImageIndex := SubItem.ImageIndex;
end;
TargetMenu.Items.Add(NewSubItem);
if SubItem.Count > 0 then
begin
//PopulateSubmenu(SubItem, NewSubItem.GetParentMenu as TPopupMenu);
end;
end;
end;
procedure TMenuPanel.DoMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
i, PosX: Integer;
Item: TMenuItem;
SubMenu: TPopupMenu;
Pt: TPoint;
begin
if Assigned(FMainMenu) and (FMainMenu.Items.Count > 0) and (Button = mbLeft) then
begin
try
PosX := FMenuX;
for i := 0 to FMainMenu.Items.Count - 1 do
begin
Item := FMainMenu.Items[i];
if (X >= PosX) and (X <= PosX + Canvas.TextWidth(Item.Caption) + 24) and
(Y >= 0) and (Y <= Self.Height) then
begin
if Item.Count > 0 then
begin
SubMenu := TPopupMenu.Create(Self);
SubMenu.AutoHotkeys := maManual;
if FBorderRadius > 0 then
SubMenu.OnPopup := DoPopup;
PopulateSubmenu(Item, SubMenu);
Pt := Self.ClientToScreen(Point(PosX, Self.Height));
SubMenu.Popup(Pt.X, Pt.Y);
end;
Break;
end;
Inc(PosX, Canvas.TextWidth(Item.Caption) + 24);
end;
finally
FHoverIndex := -1;
Invalidate;
end;
end;
end;
end.