| 1 | unit Dpi.Menus;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, LCLType, Menus;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 9 | { TMenuItem }
|
|---|
| 10 |
|
|---|
| 11 | TMenuItem = class(TComponent)
|
|---|
| 12 | private
|
|---|
| 13 | FItems: TList;
|
|---|
| 14 | FParent: TMenuItem;
|
|---|
| 15 | FOnClick: TNotifyEvent;
|
|---|
| 16 | function GetCaption: TTranslateString;
|
|---|
| 17 | function GetChecked: Boolean;
|
|---|
| 18 | function GetCount: Integer;
|
|---|
| 19 | function GetEnabled: Boolean;
|
|---|
| 20 | function GetGroupIndex: Byte;
|
|---|
| 21 | function GetItem(Index: Integer): TMenuItem;
|
|---|
| 22 | function GetOnClick: TNotifyEvent;
|
|---|
| 23 | function GetRadioItem: Boolean;
|
|---|
| 24 | function GetShortCut: TShortCut;
|
|---|
| 25 | function GetVisible: Boolean;
|
|---|
| 26 | function IsCaptionStored: Boolean;
|
|---|
| 27 | function IsCheckedStored: Boolean;
|
|---|
| 28 | function IsEnabledStored: Boolean;
|
|---|
| 29 | function IsShortCutStored: Boolean;
|
|---|
| 30 | function IsVisibleStored: Boolean;
|
|---|
| 31 | procedure SetCaption(AValue: TTranslateString);
|
|---|
| 32 | procedure SetChecked(AValue: Boolean);
|
|---|
| 33 | procedure SetEnabled(AValue: Boolean);
|
|---|
| 34 | procedure SetGroupIndex(AValue: Byte);
|
|---|
| 35 | procedure SetOnClick(AValue: TNotifyEvent);
|
|---|
| 36 | procedure SetRadioItem(AValue: Boolean);
|
|---|
| 37 | procedure SetShortCut(AValue: TShortCut);
|
|---|
| 38 | procedure SetVisible(AValue: Boolean);
|
|---|
| 39 | procedure OnClickHandler(Sender: TObject);
|
|---|
| 40 | protected
|
|---|
| 41 | function GetNativeMenuItem: Menus.TMenuItem; virtual;
|
|---|
| 42 | procedure SetParentComponent(AValue: TComponent); override;
|
|---|
| 43 | public
|
|---|
| 44 | NativeMenuItem: Menus.TMenuItem;
|
|---|
| 45 | constructor Create(AOwner: TComponent); override;
|
|---|
| 46 | destructor Destroy; override;
|
|---|
| 47 | procedure Delete(Index: Integer);
|
|---|
| 48 | procedure Add(Item: TMenuItem);
|
|---|
| 49 | procedure Insert(Index: Integer; Item: TMenuItem);
|
|---|
| 50 | function IndexOf(Item: TMenuItem): Integer;
|
|---|
| 51 | procedure Remove(Item: TMenuItem);
|
|---|
| 52 | property Items[Index: Integer]: TMenuItem read GetItem; default;
|
|---|
| 53 | property Count: Integer read GetCount;
|
|---|
| 54 | procedure Clear;
|
|---|
| 55 | procedure Click; virtual;
|
|---|
| 56 | published
|
|---|
| 57 | property RadioItem: Boolean read GetRadioItem write SetRadioItem default False;
|
|---|
| 58 | property ShortCut: TShortCut read GetShortCut write SetShortCut
|
|---|
| 59 | stored IsShortCutStored default 0;
|
|---|
| 60 | property Enabled: Boolean read GetEnabled write SetEnabled
|
|---|
| 61 | stored IsEnabledStored default True;
|
|---|
| 62 | property Visible: Boolean read GetVisible write SetVisible
|
|---|
| 63 | stored IsVisibleStored default True;
|
|---|
| 64 | property Checked: Boolean read GetChecked write SetChecked
|
|---|
| 65 | stored IsCheckedStored default False;
|
|---|
| 66 | property Caption: TTranslateString read GetCaption write SetCaption
|
|---|
| 67 | stored IsCaptionStored;
|
|---|
| 68 | property OnClick: TNotifyEvent read GetOnClick write SetOnClick;
|
|---|
| 69 | property GroupIndex: Byte read GetGroupIndex write SetGroupIndex default 0;
|
|---|
| 70 | end;
|
|---|
| 71 |
|
|---|
| 72 | TMenu = class(TComponent)
|
|---|
| 73 | private
|
|---|
| 74 | FItems: TMenuItem;
|
|---|
| 75 | protected
|
|---|
| 76 | function GetNativeMenu: Menus.TMenu; virtual;
|
|---|
| 77 | public
|
|---|
| 78 | property Items: TMenuItem read FItems;
|
|---|
| 79 | constructor Create(AOwner: TComponent); override;
|
|---|
| 80 | destructor Destroy; override;
|
|---|
| 81 | end;
|
|---|
| 82 |
|
|---|
| 83 | { TPopupMenu }
|
|---|
| 84 |
|
|---|
| 85 | TPopupMenu = class(TMenu)
|
|---|
| 86 | private
|
|---|
| 87 | function GetAutoPopup: Boolean;
|
|---|
| 88 | procedure SetAutoPopup(AValue: Boolean);
|
|---|
| 89 | protected
|
|---|
| 90 | function GetNativeMenu: Menus.TMenu; override;
|
|---|
| 91 | function GetNativePopupMenu: Menus.TPopupMenu; virtual;
|
|---|
| 92 | public
|
|---|
| 93 | NativePopupMenu: Menus.TPopupMenu;
|
|---|
| 94 | procedure PopUp; overload;
|
|---|
| 95 | procedure PopUp(X, Y: Integer); virtual; overload;
|
|---|
| 96 | constructor Create(AOwner: TComponent); override;
|
|---|
| 97 | destructor Destroy; override;
|
|---|
| 98 | published
|
|---|
| 99 | property AutoPopup: Boolean read GetAutoPopup write SetAutoPopup default True;
|
|---|
| 100 | end;
|
|---|
| 101 |
|
|---|
| 102 | function ShortCut(const Key: Word; const Shift: TShiftState): TShortCut;
|
|---|
| 103 | procedure Register;
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 | implementation
|
|---|
| 107 |
|
|---|
| 108 | uses
|
|---|
| 109 | Dpi.Common, Dpi.Controls, LCLStrConsts;
|
|---|
| 110 |
|
|---|
| 111 | function ShortCut(const Key: Word; const Shift: TShiftState): TShortCut;
|
|---|
| 112 | begin
|
|---|
| 113 | Result := Menus.ShortCut(Key, Shift);
|
|---|
| 114 | end;
|
|---|
| 115 |
|
|---|
| 116 | procedure Register;
|
|---|
| 117 | begin
|
|---|
| 118 | RegisterComponents(DpiControlsComponentPaletteName, [TPopupMenu]);
|
|---|
| 119 | end;
|
|---|
| 120 |
|
|---|
| 121 | { TMenu }
|
|---|
| 122 |
|
|---|
| 123 | function TMenu.GetNativeMenu: Menus.TMenu;
|
|---|
| 124 | begin
|
|---|
| 125 | Result := nil;
|
|---|
| 126 | end;
|
|---|
| 127 |
|
|---|
| 128 | constructor TMenu.Create(AOwner: TComponent);
|
|---|
| 129 | begin
|
|---|
| 130 | inherited;
|
|---|
| 131 | FItems := TMenuItem.Create(Self);
|
|---|
| 132 | end;
|
|---|
| 133 |
|
|---|
| 134 | destructor TMenu.Destroy;
|
|---|
| 135 | begin
|
|---|
| 136 | FreeAndNil(FItems);
|
|---|
| 137 | inherited;
|
|---|
| 138 | end;
|
|---|
| 139 |
|
|---|
| 140 | { TMenuItem }
|
|---|
| 141 |
|
|---|
| 142 | function TMenuItem.GetCaption: TTranslateString;
|
|---|
| 143 | begin
|
|---|
| 144 | Result := GetNativeMenuItem.Caption;
|
|---|
| 145 | end;
|
|---|
| 146 |
|
|---|
| 147 | function TMenuItem.GetChecked: Boolean;
|
|---|
| 148 | begin
|
|---|
| 149 | Result := GetNativeMenuItem.Checked;
|
|---|
| 150 | end;
|
|---|
| 151 |
|
|---|
| 152 | function TMenuItem.GetCount: Integer;
|
|---|
| 153 | begin
|
|---|
| 154 | Result := FItems.Count;
|
|---|
| 155 | end;
|
|---|
| 156 |
|
|---|
| 157 | function TMenuItem.GetEnabled: Boolean;
|
|---|
| 158 | begin
|
|---|
| 159 | Result := GetNativeMenuItem.Enabled;
|
|---|
| 160 | end;
|
|---|
| 161 |
|
|---|
| 162 | function TMenuItem.GetGroupIndex: Byte;
|
|---|
| 163 | begin
|
|---|
| 164 | Result := GetNativeMenuItem.GroupIndex;
|
|---|
| 165 | end;
|
|---|
| 166 |
|
|---|
| 167 | function TMenuItem.GetItem(Index: Integer): TMenuItem;
|
|---|
| 168 | begin
|
|---|
| 169 | Result := TMenuItem(FItems[Index]);
|
|---|
| 170 | end;
|
|---|
| 171 |
|
|---|
| 172 | function TMenuItem.GetOnClick: TNotifyEvent;
|
|---|
| 173 | begin
|
|---|
| 174 | Result := FOnClick;
|
|---|
| 175 | end;
|
|---|
| 176 |
|
|---|
| 177 | function TMenuItem.GetRadioItem: Boolean;
|
|---|
| 178 | begin
|
|---|
| 179 | Result := GetNativeMenuItem.RadioItem;
|
|---|
| 180 | end;
|
|---|
| 181 |
|
|---|
| 182 | function TMenuItem.GetShortCut: TShortCut;
|
|---|
| 183 | begin
|
|---|
| 184 | Result := GetNativeMenuItem.ShortCut;
|
|---|
| 185 | end;
|
|---|
| 186 |
|
|---|
| 187 | function TMenuItem.GetVisible: Boolean;
|
|---|
| 188 | begin
|
|---|
| 189 | Result := GetNativeMenuItem.Visible;
|
|---|
| 190 | end;
|
|---|
| 191 |
|
|---|
| 192 | function TMenuItem.IsCaptionStored: Boolean;
|
|---|
| 193 | begin
|
|---|
| 194 | Result := False;
|
|---|
| 195 | end;
|
|---|
| 196 |
|
|---|
| 197 | function TMenuItem.IsCheckedStored: Boolean;
|
|---|
| 198 | begin
|
|---|
| 199 | Result := False;
|
|---|
| 200 | end;
|
|---|
| 201 |
|
|---|
| 202 | function TMenuItem.IsEnabledStored: Boolean;
|
|---|
| 203 | begin
|
|---|
| 204 | Result := False;
|
|---|
| 205 | end;
|
|---|
| 206 |
|
|---|
| 207 | function TMenuItem.IsShortCutStored: Boolean;
|
|---|
| 208 | begin
|
|---|
| 209 | Result := False;
|
|---|
| 210 | end;
|
|---|
| 211 |
|
|---|
| 212 | function TMenuItem.IsVisibleStored: Boolean;
|
|---|
| 213 | begin
|
|---|
| 214 | Result := False;
|
|---|
| 215 | end;
|
|---|
| 216 |
|
|---|
| 217 | procedure TMenuItem.SetCaption(AValue: TTranslateString);
|
|---|
| 218 | begin
|
|---|
| 219 | GetNativeMenuItem.Caption := AValue;
|
|---|
| 220 | end;
|
|---|
| 221 |
|
|---|
| 222 | procedure TMenuItem.SetChecked(AValue: Boolean);
|
|---|
| 223 | begin
|
|---|
| 224 | GetNativeMenuItem.Checked := AValue;
|
|---|
| 225 | end;
|
|---|
| 226 |
|
|---|
| 227 | procedure TMenuItem.SetEnabled(AValue: Boolean);
|
|---|
| 228 | begin
|
|---|
| 229 | GetNativeMenuItem.Enabled := AValue;
|
|---|
| 230 | end;
|
|---|
| 231 |
|
|---|
| 232 | procedure TMenuItem.SetGroupIndex(AValue: Byte);
|
|---|
| 233 | begin
|
|---|
| 234 | GetNativeMenuItem.GroupIndex := AValue;
|
|---|
| 235 | end;
|
|---|
| 236 |
|
|---|
| 237 | procedure TMenuItem.SetOnClick(AValue: TNotifyEvent);
|
|---|
| 238 | begin
|
|---|
| 239 | FOnClick := AValue;
|
|---|
| 240 | end;
|
|---|
| 241 |
|
|---|
| 242 | procedure TMenuItem.SetRadioItem(AValue: Boolean);
|
|---|
| 243 | begin
|
|---|
| 244 | GetNativeMenuItem.RadioItem := AValue;
|
|---|
| 245 | end;
|
|---|
| 246 |
|
|---|
| 247 | procedure TMenuItem.SetShortCut(AValue: TShortCut);
|
|---|
| 248 | begin
|
|---|
| 249 | GetNativeMenuItem.ShortCut := AValue;
|
|---|
| 250 | end;
|
|---|
| 251 |
|
|---|
| 252 | procedure TMenuItem.SetVisible(AValue: Boolean);
|
|---|
| 253 | begin
|
|---|
| 254 | GetNativeMenuItem.Visible := AValue;
|
|---|
| 255 | end;
|
|---|
| 256 |
|
|---|
| 257 | procedure TMenuItem.OnClickHandler(Sender: TObject);
|
|---|
| 258 | begin
|
|---|
| 259 | if Assigned(FOnClick) then
|
|---|
| 260 | FOnClick(Self);
|
|---|
| 261 | end;
|
|---|
| 262 |
|
|---|
| 263 | procedure TMenuItem.Delete(Index: Integer);
|
|---|
| 264 | begin
|
|---|
| 265 | FItems.Delete(Index);
|
|---|
| 266 | GetNativeMenuItem.Delete(Index);
|
|---|
| 267 | end;
|
|---|
| 268 |
|
|---|
| 269 | procedure TMenuItem.Add(Item: TMenuItem);
|
|---|
| 270 | begin
|
|---|
| 271 | Insert(GetCount, Item);
|
|---|
| 272 | end;
|
|---|
| 273 |
|
|---|
| 274 | procedure TMenuItem.Insert(Index: Integer; Item: TMenuItem);
|
|---|
| 275 | begin
|
|---|
| 276 | FItems.Insert(Index, Item);
|
|---|
| 277 | GetNativeMenuItem.Insert(Index, Item.GetNativeMenuItem);
|
|---|
| 278 | end;
|
|---|
| 279 |
|
|---|
| 280 | function TMenuItem.IndexOf(Item: TMenuItem): Integer;
|
|---|
| 281 | begin
|
|---|
| 282 | if FItems = nil then
|
|---|
| 283 | Result := -1
|
|---|
| 284 | else
|
|---|
| 285 | Result := FItems.IndexOf(Item);
|
|---|
| 286 | end;
|
|---|
| 287 |
|
|---|
| 288 | procedure TMenuItem.Remove(Item: TMenuItem);
|
|---|
| 289 | var
|
|---|
| 290 | I: Integer;
|
|---|
| 291 | begin
|
|---|
| 292 | I := IndexOf(Item);
|
|---|
| 293 | if I < 0 then
|
|---|
| 294 | raise EMenuError.Create(SMenuNotFound);
|
|---|
| 295 | Delete(I);
|
|---|
| 296 | end;
|
|---|
| 297 |
|
|---|
| 298 | procedure TMenuItem.Clear;
|
|---|
| 299 | begin
|
|---|
| 300 | GetNativeMenuItem.Clear;
|
|---|
| 301 | FItems.Clear;
|
|---|
| 302 | end;
|
|---|
| 303 |
|
|---|
| 304 | procedure TMenuItem.Click;
|
|---|
| 305 | begin
|
|---|
| 306 | GetNativeMenuItem.Click;
|
|---|
| 307 | end;
|
|---|
| 308 |
|
|---|
| 309 | function TMenuItem.GetNativeMenuItem: Menus.TMenuItem;
|
|---|
| 310 | begin
|
|---|
| 311 | if not Assigned(NativeMenuItem) then begin
|
|---|
| 312 | NativeMenuItem := Menus.TMenuItem.Create(nil);
|
|---|
| 313 | NativeMenuItem.Name := 'Native' + Name;
|
|---|
| 314 | NativeMenuItem.OnClick := OnClickHandler;
|
|---|
| 315 | end;
|
|---|
| 316 | Result := NativeMenuItem;
|
|---|
| 317 | end;
|
|---|
| 318 |
|
|---|
| 319 | procedure TMenuItem.SetParentComponent(AValue: TComponent);
|
|---|
| 320 | begin
|
|---|
| 321 | if (FParent = AValue) then Exit;
|
|---|
| 322 | if Assigned(FParent) then FParent.Remove(Self);
|
|---|
| 323 | if Assigned(AValue) then
|
|---|
| 324 | begin
|
|---|
| 325 | if (AValue is TMenu)
|
|---|
| 326 | then TMenu(AValue).Items.Add(Self)
|
|---|
| 327 | else if (AValue is TMenuItem)
|
|---|
| 328 | then TMenuItem(AValue).Add(Self)
|
|---|
| 329 | else
|
|---|
| 330 | raise Exception.Create('TDpiMenuItem.SetParentComponent: suggested parent not of type TDpiMenu or TDpiMenuItem');
|
|---|
| 331 | end;
|
|---|
| 332 | end;
|
|---|
| 333 |
|
|---|
| 334 | constructor TMenuItem.Create(AOwner: TComponent);
|
|---|
| 335 | begin
|
|---|
| 336 | inherited;
|
|---|
| 337 | FItems := TList.Create;
|
|---|
| 338 | end;
|
|---|
| 339 |
|
|---|
| 340 | destructor TMenuItem.Destroy;
|
|---|
| 341 | begin
|
|---|
| 342 | FreeAndNil(FItems);
|
|---|
| 343 | // TODO: Release menu items
|
|---|
| 344 | //FreeAndNil(NativeMenuItem);
|
|---|
| 345 | inherited;
|
|---|
| 346 | end;
|
|---|
| 347 |
|
|---|
| 348 | { TPopupMenu }
|
|---|
| 349 |
|
|---|
| 350 | procedure TPopupMenu.PopUp;
|
|---|
| 351 | var
|
|---|
| 352 | Pos: TPoint;
|
|---|
| 353 | begin
|
|---|
| 354 | Pos := Mouse.CursorPos;
|
|---|
| 355 | Popup(Pos.X, Pos.Y);
|
|---|
| 356 | end;
|
|---|
| 357 |
|
|---|
| 358 | procedure TPopupMenu.PopUp(X, Y: Integer);
|
|---|
| 359 | begin
|
|---|
| 360 | GetNativePopupMenu.PopUp(ScaleToNative(X), ScaleToNative(Y));
|
|---|
| 361 | end;
|
|---|
| 362 |
|
|---|
| 363 | constructor TPopupMenu.Create(AOwner: TComponent);
|
|---|
| 364 | begin
|
|---|
| 365 | inherited;
|
|---|
| 366 | GetNativePopupMenu;
|
|---|
| 367 | end;
|
|---|
| 368 |
|
|---|
| 369 | function TPopupMenu.GetAutoPopup: Boolean;
|
|---|
| 370 | begin
|
|---|
| 371 | Result := GetNativePopupMenu.AutoPopup;
|
|---|
| 372 | end;
|
|---|
| 373 |
|
|---|
| 374 | procedure TPopupMenu.SetAutoPopup(AValue: Boolean);
|
|---|
| 375 | begin
|
|---|
| 376 | GetNativePopupMenu.AutoPopup := AValue;
|
|---|
| 377 | end;
|
|---|
| 378 |
|
|---|
| 379 | function TPopupMenu.GetNativeMenu: Menus.TMenu;
|
|---|
| 380 | begin
|
|---|
| 381 | Result := GetNativePopupMenu;
|
|---|
| 382 | end;
|
|---|
| 383 |
|
|---|
| 384 | function TPopupMenu.GetNativePopupMenu: Menus.TPopupMenu;
|
|---|
| 385 | begin
|
|---|
| 386 | if not Assigned(NativePopupMenu) then begin
|
|---|
| 387 | NativePopupMenu := Menus.TPopupMenu.Create(nil);
|
|---|
| 388 | if Assigned(Items.NativeMenuItem) then Items.NativeMenuItem.Free;
|
|---|
| 389 | Items.NativeMenuItem := NativePopupMenu.Items;
|
|---|
| 390 | end;
|
|---|
| 391 | Result := NativePopupMenu;
|
|---|
| 392 | end;
|
|---|
| 393 |
|
|---|
| 394 | destructor TPopupMenu.Destroy;
|
|---|
| 395 | begin
|
|---|
| 396 | if Assigned(NativePopupMenu) then FreeAndNil(NativePopupMenu);
|
|---|
| 397 | inherited;
|
|---|
| 398 | end;
|
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 | end.
|
|---|
| 402 |
|
|---|