1 | unit Menu;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Graphics, Controls, Generics.Collections, BasicControls;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TMenuItemKind = (ikButton, ikComboBox, ikCheckBox);
|
---|
10 |
|
---|
11 | { TMenuItem }
|
---|
12 |
|
---|
13 | TMenuItem = class(TControl)
|
---|
14 | BackgroundColor: TColor;
|
---|
15 | BackgroundSelectedColor: TColor;
|
---|
16 | TextColor: TColor;
|
---|
17 | TextDisabledColor: TColor;
|
---|
18 | Kind: TMenuItemKind;
|
---|
19 | Text: string;
|
---|
20 | Selected: Boolean;
|
---|
21 | TextSize: Integer;
|
---|
22 | function GetOutputText: string; virtual;
|
---|
23 | constructor Create; override;
|
---|
24 | end;
|
---|
25 |
|
---|
26 | { TMenuItemCheckBox }
|
---|
27 |
|
---|
28 | TMenuItemCheckBox = class(TMenuItem)
|
---|
29 | private
|
---|
30 | FOnChanged: TNotifyEvent;
|
---|
31 | public
|
---|
32 | Checked: Boolean;
|
---|
33 | procedure Paint; override;
|
---|
34 | property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
|
---|
35 | end;
|
---|
36 |
|
---|
37 | { TMenuItemComboBox }
|
---|
38 |
|
---|
39 | TMenuItemComboBox = class(TMenuItem)
|
---|
40 | private
|
---|
41 | FOnChanged: TNotifyEvent;
|
---|
42 | public
|
---|
43 | Index: Integer;
|
---|
44 | States: TStringList;
|
---|
45 | constructor Create; override;
|
---|
46 | destructor Destroy; override;
|
---|
47 | procedure Paint; override;
|
---|
48 | function GetOutputText: string; override;
|
---|
49 | property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
|
---|
50 | end;
|
---|
51 |
|
---|
52 | { TMenuItemButton }
|
---|
53 |
|
---|
54 | TMenuItemButton = class(TMenuItem)
|
---|
55 | public
|
---|
56 | procedure Paint; override;
|
---|
57 | end;
|
---|
58 |
|
---|
59 | { TMenuItems }
|
---|
60 |
|
---|
61 | TMenuItems = class(TObjectList<TMenuItem>)
|
---|
62 | function AddButton(Text: string; OnClick: TNotifyEvent): TMenuItemButton;
|
---|
63 | function AddCheckBox(Text: string; OnChanged: TNotifyEvent): TMenuItemCheckBox;
|
---|
64 | function AddComboBox(Text: string; States: array of string; OnChanged:
|
---|
65 | TNotifyEvent): TMenuItemComboBox;
|
---|
66 | end;
|
---|
67 |
|
---|
68 | TMenu = class
|
---|
69 | private
|
---|
70 | FOnExit: TNotifyEvent;
|
---|
71 | public
|
---|
72 | Items: TMenuItems;
|
---|
73 | Parent: TMenu;
|
---|
74 | procedure MouseMove(Position: TPoint);
|
---|
75 | procedure MouseUp(Button: TMouseButton; Position: TPoint);
|
---|
76 | procedure Paint(Canvas: TCanvas; CanvasSize: TPoint);
|
---|
77 | constructor Create; virtual;
|
---|
78 | destructor Destroy; override;
|
---|
79 | property OnExit: TNotifyEvent read FOnExit write FOnExit;
|
---|
80 | end;
|
---|
81 |
|
---|
82 |
|
---|
83 | implementation
|
---|
84 |
|
---|
85 | { TMenuItemButton }
|
---|
86 |
|
---|
87 | procedure TMenuItemButton.Paint;
|
---|
88 | var
|
---|
89 | P: TPoint;
|
---|
90 | begin
|
---|
91 | P := Point(Bounds.Left, Bounds.Top);
|
---|
92 | if Selected then Canvas.Brush.Color := BackgroundSelectedColor
|
---|
93 | else Canvas.Brush.Color := BackgroundColor;
|
---|
94 | if Canvas.Brush.Color = clNone then Canvas.Brush.Style := bsClear
|
---|
95 | else Canvas.Brush.Style := bsSolid;
|
---|
96 | Bounds := Classes.Bounds(P.X, P.Y, Canvas.TextWidth(Text), Canvas.TextHeight(Text));
|
---|
97 | Canvas.TextOut(P.X, P.Y, Text);
|
---|
98 | end;
|
---|
99 |
|
---|
100 | { TMenuItemCheckBox }
|
---|
101 |
|
---|
102 | procedure TMenuItemCheckBox.Paint;
|
---|
103 | var
|
---|
104 | OutputText: string;
|
---|
105 | P: TPoint;
|
---|
106 | begin
|
---|
107 | P := Point(Bounds.Left, Bounds.Top);
|
---|
108 | OutputText := Text;
|
---|
109 | if Checked then OutputText := '✓' + OutputText;
|
---|
110 | if Selected then Canvas.Brush.Color := BackgroundSelectedColor
|
---|
111 | else Canvas.Brush.Color := BackgroundColor;
|
---|
112 | if Canvas.Brush.Color = clNone then Canvas.Brush.Style := bsClear
|
---|
113 | else Canvas.Brush.Style := bsSolid;
|
---|
114 | Bounds := Classes.Bounds(P.X, P.Y, Canvas.TextWidth(OutputText), Canvas.TextHeight(OutputText));
|
---|
115 | Canvas.TextOut(P.X, P.Y, OutputText);
|
---|
116 | end;
|
---|
117 |
|
---|
118 | { TMenuItem }
|
---|
119 |
|
---|
120 | function TMenuItem.GetOutputText: string;
|
---|
121 | begin
|
---|
122 | Result := Text;
|
---|
123 | end;
|
---|
124 |
|
---|
125 | constructor TMenuItem.Create;
|
---|
126 | begin
|
---|
127 | Enabled := True;
|
---|
128 | TextSize := 40;
|
---|
129 | TextColor := clWhite;
|
---|
130 | TextDisabledColor := clSilver;
|
---|
131 | end;
|
---|
132 |
|
---|
133 | { TMenuItemComboBox }
|
---|
134 |
|
---|
135 | constructor TMenuItemComboBox.Create;
|
---|
136 | begin
|
---|
137 | inherited;
|
---|
138 | States := TStringList.Create;
|
---|
139 | end;
|
---|
140 |
|
---|
141 | destructor TMenuItemComboBox.Destroy;
|
---|
142 | begin
|
---|
143 | FreeAndNil(States);
|
---|
144 | inherited;
|
---|
145 | end;
|
---|
146 |
|
---|
147 | procedure TMenuItemComboBox.Paint;
|
---|
148 | var
|
---|
149 | P: TPoint;
|
---|
150 | begin
|
---|
151 | P := Point(Bounds.Left, Bounds.Top);
|
---|
152 | if Selected then Canvas.Brush.Color := BackgroundSelectedColor
|
---|
153 | else Canvas.Brush.Color := BackgroundColor;
|
---|
154 | if Canvas.Brush.Color = clNone then Canvas.Brush.Style := bsClear
|
---|
155 | else Canvas.Brush.Style := bsSolid;
|
---|
156 | Bounds := Classes.Bounds(P.X, P.Y, Canvas.TextWidth(GetOutputText), Canvas.TextHeight(GetOutputText));
|
---|
157 | Canvas.TextOut(P.X, P.Y, GetOutputText);
|
---|
158 | end;
|
---|
159 |
|
---|
160 | function TMenuItemComboBox.GetOutputText: string;
|
---|
161 | begin
|
---|
162 | Result := Text + ': ' + States[Index];
|
---|
163 | end;
|
---|
164 |
|
---|
165 | { TMenu }
|
---|
166 |
|
---|
167 | procedure TMenu.MouseMove(Position: TPoint);
|
---|
168 | var
|
---|
169 | I: Integer;
|
---|
170 | begin
|
---|
171 | for I := 0 to Items.Count - 1 do begin
|
---|
172 | if Items[I].Enabled then
|
---|
173 | Items[I].Selected := Items[I].Bounds.Contains(Position);
|
---|
174 | end;
|
---|
175 | end;
|
---|
176 |
|
---|
177 | procedure TMenu.MouseUp(Button: TMouseButton; Position: TPoint);
|
---|
178 | var
|
---|
179 | I: Integer;
|
---|
180 | begin
|
---|
181 | for I := 0 to Items.Count - 1 do begin
|
---|
182 | if Items[I].Bounds.Contains(Position) then begin
|
---|
183 | if (Items[I] is TMenuItemButton) then begin
|
---|
184 | TMenuItemButton(Items[I]).MouseUp(Position);
|
---|
185 | end else
|
---|
186 | if (Items[I] is TMenuItemCheckBox) then begin
|
---|
187 | (Items[I] as TMenuItemCheckBox).Checked := not (Items[I] as TMenuItemCheckBox).Checked;
|
---|
188 | if Assigned((Items[I] as TMenuItemCheckBox).FOnChanged) then
|
---|
189 | (Items[I] as TMenuItemCheckBox).FOnChanged(Items[I]);
|
---|
190 | end else
|
---|
191 | if (Items[I] is TMenuItemComboBox) then begin
|
---|
192 | (Items[I] as TMenuItemComboBox).Index := ((Items[I] as TMenuItemComboBox).Index + 1) mod
|
---|
193 | (Items[I] as TMenuItemComboBox).States.Count;
|
---|
194 | if Assigned((Items[I] as TMenuItemComboBox).FOnChanged) then
|
---|
195 | (Items[I] as TMenuItemComboBox).FOnChanged(Items[I]);
|
---|
196 | end;
|
---|
197 | end;
|
---|
198 | end;
|
---|
199 | end;
|
---|
200 |
|
---|
201 | procedure TMenu.Paint(Canvas: TCanvas; CanvasSize: TPoint);
|
---|
202 | var
|
---|
203 | I: Integer;
|
---|
204 | X: Integer;
|
---|
205 | Y: Integer;
|
---|
206 | LineHeight: Integer;
|
---|
207 | TotalWidth: Integer;
|
---|
208 | TotalHeight: Integer;
|
---|
209 | begin
|
---|
210 | with Canvas do begin
|
---|
211 | TotalWidth := 0;
|
---|
212 | TotalHeight := 0;
|
---|
213 |
|
---|
214 | // Calculate total dimensions for centering
|
---|
215 | Font.Style := [fsBold];
|
---|
216 | Brush.Style := bsSolid;
|
---|
217 | for I := 0 to Items.Count - 1 do begin
|
---|
218 | Font.Size := Items[I].TextSize;
|
---|
219 | if TotalWidth < TextWidth(Items[I].GetOutputText) then
|
---|
220 | TotalWidth := TextWidth(Items[I].GetOutputText);
|
---|
221 | LineHeight := Round(TextHeight('I') * 1.1);
|
---|
222 | TotalHeight := TotalHeight + LineHeight;
|
---|
223 | end;
|
---|
224 |
|
---|
225 | X := (CanvasSize.X - TotalWidth) div 2;
|
---|
226 | Y := (CanvasSize.Y - TotalHeight) div 2;
|
---|
227 |
|
---|
228 | // Menu items
|
---|
229 | Font.Style := [fsBold];
|
---|
230 | Brush.Style := bsSolid;
|
---|
231 | for I := 0 to Items.Count - 1 do begin
|
---|
232 | Font.Size := Items[I].TextSize;
|
---|
233 | if Items[I].Enabled then Font.Color := Items[I].TextColor
|
---|
234 | else Font.Color := Items[I].TextDisabledColor;
|
---|
235 | Items[I].Bounds.Left := X;
|
---|
236 | Items[I].Bounds.Top := Y;
|
---|
237 | Items[I].Canvas := Canvas;
|
---|
238 | Items[I].Paint;
|
---|
239 | LineHeight := Round(TextHeight('I') * 1.1);
|
---|
240 | Inc(Y, LineHeight);
|
---|
241 | end;
|
---|
242 | end;
|
---|
243 | end;
|
---|
244 |
|
---|
245 | constructor TMenu.Create;
|
---|
246 | begin
|
---|
247 | Items := TMenuItems.Create;
|
---|
248 | end;
|
---|
249 |
|
---|
250 | destructor TMenu.Destroy;
|
---|
251 | begin
|
---|
252 | FreeAndNil(Items);
|
---|
253 | inherited;
|
---|
254 | end;
|
---|
255 |
|
---|
256 | { TMenuItems }
|
---|
257 |
|
---|
258 | function TMenuItems.AddButton(Text: string; OnClick: TNotifyEvent
|
---|
259 | ): TMenuItemButton;
|
---|
260 | begin
|
---|
261 | Result := TMenuItemButton.Create;
|
---|
262 | Result.Text := Text;
|
---|
263 | Result.OnClick := OnClick;
|
---|
264 | Add(Result);
|
---|
265 | end;
|
---|
266 |
|
---|
267 | function TMenuItems.AddCheckBox(Text: string; OnChanged: TNotifyEvent): TMenuItemCheckBox;
|
---|
268 | begin
|
---|
269 | Result := TMenuItemCheckBox.Create;
|
---|
270 | Result.Text := Text;
|
---|
271 | Result.OnChanged := OnChanged;
|
---|
272 | Add(Result);
|
---|
273 | end;
|
---|
274 |
|
---|
275 | function TMenuItems.AddComboBox(Text: string; States: array of string
|
---|
276 | ; OnChanged: TNotifyEvent): TMenuItemComboBox;
|
---|
277 | var
|
---|
278 | I: Integer;
|
---|
279 | begin
|
---|
280 | Result := TMenuItemComboBox.Create;
|
---|
281 | Result.Text := Text;
|
---|
282 | Result.OnChanged := OnChanged;
|
---|
283 | for I := 0 to Length(States) - 1 do
|
---|
284 | Result.States.Add(States[I]);
|
---|
285 | Add(Result);
|
---|
286 | end;
|
---|
287 |
|
---|
288 | end.
|
---|
289 |
|
---|