source: trunk/Menu.pas

Last change on this file was 143, checked in by chronos, 11 months ago

Removed U prefix from all units.

File size: 7.2 KB
Line 
1unit Menu;
2
3interface
4
5uses
6 Classes, SysUtils, Graphics, Controls, Generics.Collections, BasicControls;
7
8type
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
83implementation
84
85{ TMenuItemButton }
86
87procedure TMenuItemButton.Paint;
88var
89 P: TPoint;
90begin
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);
98end;
99
100{ TMenuItemCheckBox }
101
102procedure TMenuItemCheckBox.Paint;
103var
104 OutputText: string;
105 P: TPoint;
106begin
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);
116end;
117
118{ TMenuItem }
119
120function TMenuItem.GetOutputText: string;
121begin
122 Result := Text;
123end;
124
125constructor TMenuItem.Create;
126begin
127 Enabled := True;
128 TextSize := 40;
129 TextColor := clWhite;
130 TextDisabledColor := clSilver;
131end;
132
133{ TMenuItemComboBox }
134
135constructor TMenuItemComboBox.Create;
136begin
137 inherited;
138 States := TStringList.Create;
139end;
140
141destructor TMenuItemComboBox.Destroy;
142begin
143 FreeAndNil(States);
144 inherited;
145end;
146
147procedure TMenuItemComboBox.Paint;
148var
149 P: TPoint;
150begin
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);
158end;
159
160function TMenuItemComboBox.GetOutputText: string;
161begin
162 Result := Text + ': ' + States[Index];
163end;
164
165{ TMenu }
166
167procedure TMenu.MouseMove(Position: TPoint);
168var
169 I: Integer;
170begin
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;
175end;
176
177procedure TMenu.MouseUp(Button: TMouseButton; Position: TPoint);
178var
179 I: Integer;
180begin
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;
199end;
200
201procedure TMenu.Paint(Canvas: TCanvas; CanvasSize: TPoint);
202var
203 I: Integer;
204 X: Integer;
205 Y: Integer;
206 LineHeight: Integer;
207 TotalWidth: Integer;
208 TotalHeight: Integer;
209begin
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;
243end;
244
245constructor TMenu.Create;
246begin
247 Items := TMenuItems.Create;
248end;
249
250destructor TMenu.Destroy;
251begin
252 FreeAndNil(Items);
253 inherited;
254end;
255
256{ TMenuItems }
257
258function TMenuItems.AddButton(Text: string; OnClick: TNotifyEvent
259 ): TMenuItemButton;
260begin
261 Result := TMenuItemButton.Create;
262 Result.Text := Text;
263 Result.OnClick := OnClick;
264 Add(Result);
265end;
266
267function TMenuItems.AddCheckBox(Text: string; OnChanged: TNotifyEvent): TMenuItemCheckBox;
268begin
269 Result := TMenuItemCheckBox.Create;
270 Result.Text := Text;
271 Result.OnChanged := OnChanged;
272 Add(Result);
273end;
274
275function TMenuItems.AddComboBox(Text: string; States: array of string
276 ; OnChanged: TNotifyEvent): TMenuItemComboBox;
277var
278 I: Integer;
279begin
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);
286end;
287
288end.
289
Note: See TracBrowser for help on using the repository browser.