| 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 | if X < 5 then X := 5;
|
|---|
| 227 | Y := (CanvasSize.Y - TotalHeight) div 2;
|
|---|
| 228 | if Y < 5 then Y := 5;
|
|---|
| 229 |
|
|---|
| 230 | // Menu items
|
|---|
| 231 | Font.Style := [fsBold];
|
|---|
| 232 | Brush.Style := bsSolid;
|
|---|
| 233 | for I := 0 to Items.Count - 1 do begin
|
|---|
| 234 | Font.Size := Items[I].TextSize;
|
|---|
| 235 | if Items[I].Enabled then Font.Color := Items[I].TextColor
|
|---|
| 236 | else Font.Color := Items[I].TextDisabledColor;
|
|---|
| 237 | Items[I].Bounds.Left := X;
|
|---|
| 238 | Items[I].Bounds.Top := Y;
|
|---|
| 239 | Items[I].Canvas := Canvas;
|
|---|
| 240 | Items[I].Paint;
|
|---|
| 241 | LineHeight := Round(TextHeight('I') * 1.1);
|
|---|
| 242 | Inc(Y, LineHeight);
|
|---|
| 243 | end;
|
|---|
| 244 | end;
|
|---|
| 245 | end;
|
|---|
| 246 |
|
|---|
| 247 | constructor TMenu.Create;
|
|---|
| 248 | begin
|
|---|
| 249 | Items := TMenuItems.Create;
|
|---|
| 250 | end;
|
|---|
| 251 |
|
|---|
| 252 | destructor TMenu.Destroy;
|
|---|
| 253 | begin
|
|---|
| 254 | FreeAndNil(Items);
|
|---|
| 255 | inherited;
|
|---|
| 256 | end;
|
|---|
| 257 |
|
|---|
| 258 | { TMenuItems }
|
|---|
| 259 |
|
|---|
| 260 | function TMenuItems.AddButton(Text: string; OnClick: TNotifyEvent
|
|---|
| 261 | ): TMenuItemButton;
|
|---|
| 262 | begin
|
|---|
| 263 | Result := TMenuItemButton.Create;
|
|---|
| 264 | Result.Text := Text;
|
|---|
| 265 | Result.OnClick := OnClick;
|
|---|
| 266 | Add(Result);
|
|---|
| 267 | end;
|
|---|
| 268 |
|
|---|
| 269 | function TMenuItems.AddCheckBox(Text: string; OnChanged: TNotifyEvent): TMenuItemCheckBox;
|
|---|
| 270 | begin
|
|---|
| 271 | Result := TMenuItemCheckBox.Create;
|
|---|
| 272 | Result.Text := Text;
|
|---|
| 273 | Result.OnChanged := OnChanged;
|
|---|
| 274 | Add(Result);
|
|---|
| 275 | end;
|
|---|
| 276 |
|
|---|
| 277 | function TMenuItems.AddComboBox(Text: string; States: array of string
|
|---|
| 278 | ; OnChanged: TNotifyEvent): TMenuItemComboBox;
|
|---|
| 279 | var
|
|---|
| 280 | I: Integer;
|
|---|
| 281 | begin
|
|---|
| 282 | Result := TMenuItemComboBox.Create;
|
|---|
| 283 | Result.Text := Text;
|
|---|
| 284 | Result.OnChanged := OnChanged;
|
|---|
| 285 | for I := 0 to Length(States) - 1 do
|
|---|
| 286 | Result.States.Add(States[I]);
|
|---|
| 287 | Add(Result);
|
|---|
| 288 | end;
|
|---|
| 289 |
|
|---|
| 290 | end.
|
|---|
| 291 |
|
|---|