| 1 |
{ |
| 2 |
Copyright 2006-2018 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Manage a list of recently opened files, and show a menu in TCastleWindow. |
| 17 |
See TRecentFiles class. } |
| 18 |
unit CastleWindowRecentFiles; |
| 19 |
|
| 20 |
{$I castleconf.inc} |
| 21 |
|
| 22 |
interface |
| 23 |
|
| 24 |
uses Classes, CastleWindow, CastleRecentFiles; |
| 25 |
|
| 26 |
type |
| 27 |
{ Manage a list of recently opened files, and show a menu in TCastleWindow. } |
| 28 |
TWindowRecentFiles = class(TRecentFiles) |
| 29 |
private |
| 30 |
FNextMenuItem: TMenuEntry; |
| 31 |
FirstSeparator: TMenuEntry; |
| 32 |
procedure SetNextMenuItem(Value: TMenuEntry); |
| 33 |
protected |
| 34 |
procedure MenuCreate; override; |
| 35 |
|
| 36 |
{ Destroy the menu. Internal (do not call this directly, it's only called |
| 37 |
by parent class). |
| 38 |
Note that it doesn't read Urls list at all, it only |
| 39 |
depends on FirstSeparator value. } |
| 40 |
procedure MenuDestroy; override; |
| 41 |
public |
| 42 |
{ This determines the placement of "recent files" list inside your menu. |
| 43 |
|
| 44 |
If this is not @nil then each update to the recent files |
| 45 |
list (for example by @link(Add) method) will result in the menu |
| 46 |
items right before NextMenuItem to be updated. |
| 47 |
|
| 48 |
Also changing the value of this property will result in one-time |
| 49 |
update of the menu. Menu at the place of old NextMenuItem |
| 50 |
will be removed and menu at the place of new NextMenuItem will |
| 51 |
get created. |
| 52 |
|
| 53 |
Usually NextMenuItem should be the separator that divides |
| 54 |
the print and properties menu sections and the close menu sections. |
| 55 |
That's consistent with other GNOME programs, and this is what we usually |
| 56 |
try to follow. } |
| 57 |
property NextMenuItem: TMenuEntry read FNextMenuItem write SetNextMenuItem; |
| 58 |
end; |
| 59 |
|
| 60 |
implementation |
| 61 |
|
| 62 |
uses SysUtils, CastleClassUtils, CastleUriUtils; |
| 63 |
|
| 64 |
{ TMenuRecentItem ------------------------------------------------------------ } |
| 65 |
|
| 66 |
type |
| 67 |
TMenuRecentItem = class(TMenuItem) |
| 68 |
private |
| 69 |
FUrl: String; |
| 70 |
public |
| 71 |
{ Constructor. Number is 1-based number of this recent file entry. } |
| 72 |
constructor Create(const Number: Integer; const AUrl: String; |
| 73 |
AIntData: Integer); |
| 74 |
public |
| 75 |
OnOpenRecent: TOnOpenRecent; |
| 76 |
property Url: String read FUrl; |
| 77 |
function DoClick: boolean; override; |
| 78 |
end; |
| 79 |
|
| 80 |
constructor TMenuRecentItem.Create( |
| 81 |
const Number: Integer; const AUrl: String; AIntData: Integer); |
| 82 |
var |
| 83 |
S: string; |
| 84 |
begin |
| 85 |
FUrl := AUrl; |
| 86 |
|
| 87 |
if Number <= 9 then |
| 88 |
S := '_' + IntToStr(Number) else |
| 89 |
if Number = 10 then |
| 90 |
S := '1_0' else |
| 91 |
S := IntToStr(Number); |
| 92 |
S := S + '. ' + SQuoteMenuEntryCaption(UriCaption(Url)); |
| 93 |
|
| 94 |
inherited Create(S, AIntData); |
| 95 |
end; |
| 96 |
|
| 97 |
function TMenuRecentItem.DoClick: boolean; |
| 98 |
begin |
| 99 |
if Assigned(OnOpenRecent) then |
| 100 |
OnOpenRecent(Url); |
| 101 |
Result := true; |
| 102 |
end; |
| 103 |
|
| 104 |
{ TWindowRecentFiles -------------------------------------------------------------- } |
| 105 |
|
| 106 |
procedure TWindowRecentFiles.MenuCreate; |
| 107 |
var |
| 108 |
I: Integer; |
| 109 |
ParentMenu: TMenu; |
| 110 |
Position: Cardinal; |
| 111 |
MenuRecentOpen: TMenuRecentItem; |
| 112 |
begin |
| 113 |
inherited; |
| 114 |
|
| 115 |
{ Add recent files menu } |
| 116 |
if (NextMenuItem <> nil) and (NextMenuItem.ParentMenu <> nil) and |
| 117 |
{ When Urls.Count = 0 then we don't want to add anything, |
| 118 |
even FirstSeparator. } |
| 119 |
(Urls.Count > 0) then |
| 120 |
begin |
| 121 |
ParentMenu := NextMenuItem.ParentMenu; |
| 122 |
Position := NextMenuItem.ParentMenuPosition; |
| 123 |
FirstSeparator := TMenuSeparator.Create; |
| 124 |
ParentMenu.Insert(Position, FirstSeparator); |
| 125 |
for I := 0 to Urls.Count - 1 do |
| 126 |
begin |
| 127 |
MenuRecentOpen := TMenuRecentItem.Create(I+1, Urls[I], 0); |
| 128 |
MenuRecentOpen.OnOpenRecent := OnOpenRecent; |
| 129 |
ParentMenu.Insert(Position + Cardinal(I) + 1, MenuRecentOpen); |
| 130 |
end; |
| 131 |
end; |
| 132 |
end; |
| 133 |
|
| 134 |
procedure TWindowRecentFiles.MenuDestroy; |
| 135 |
var |
| 136 |
ParentMenu: TMenu; |
| 137 |
Position: Cardinal; |
| 138 |
begin |
| 139 |
{ Remove recent files menu from old NextMenuItem } |
| 140 |
if (NextMenuItem <> nil) and |
| 141 |
{ Checks for below are to safeguard against cases |
| 142 |
when you could assign NextMenuItem before it had |
| 143 |
any ParentMenu, or if you somehow rearranged your menu, |
| 144 |
or if Urls list was empty when you assigned NextMenuItem |
| 145 |
(in this case FirstSeparator is left nil, but later it should |
| 146 |
be created after the 1st add). } |
| 147 |
(NextMenuItem.ParentMenu <> nil) and |
| 148 |
(FirstSeparator <> nil) and |
| 149 |
(NextMenuItem.ParentMenu = FirstSeparator.ParentMenu) then |
| 150 |
begin |
| 151 |
ParentMenu := NextMenuItem.ParentMenu; |
| 152 |
Position := FirstSeparator.ParentMenuPosition; |
| 153 |
Assert(ParentMenu.Entries[Position] = FirstSeparator); |
| 154 |
FirstSeparator := nil; |
| 155 |
repeat |
| 156 |
ParentMenu.Delete(Position); |
| 157 |
until (ParentMenu.Entries[Position] = NextMenuItem); |
| 158 |
end; |
| 159 |
|
| 160 |
inherited; |
| 161 |
end; |
| 162 |
|
| 163 |
procedure TWindowRecentFiles.SetNextMenuItem(Value: TMenuEntry); |
| 164 |
begin |
| 165 |
if Value <> FNextMenuItem then |
| 166 |
begin |
| 167 |
MenuDestroy; |
| 168 |
FNextMenuItem := Value; |
| 169 |
MenuCreate; |
| 170 |
end; |
| 171 |
end; |
| 172 |
|
| 173 |
end. |