summaryrefslogtreecommitdiff
path: root/src/gui/fpg_mru.pas
blob: 58106e12b2bf4669a64074740af104e6167b54c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2010 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    Description:
      A component implementing a 'Most Recently Used' feature normally
      inserted in the File menu.
}

unit fpg_mru;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fpg_menu;
  
type

  TMRUClickEvent = procedure(Sender: TObject; const FileName: String) of object;

  TfpgMRU = class(TComponent)
  private
    FItems: TStringList;
    FMaxItems: Integer;
    FShowFullPath: boolean;
    FParentMenuItem: TfpgPopupMenu;
//    FIniFilePath: string;
    FOnClick: TMRUClickEvent;
    procedure   SetMaxItems(const AValue: Integer);
//    procedure   SetIniFilePath(const AValue: string);
    procedure   SetParentMenuItem(const AValue: TfpgPopupMenu);
    procedure   SetShowFullPath(const AValue: boolean);
    procedure   SaveMRU;
    procedure   ItemsChange(Sender: TObject);
    procedure   ClearParentMenu;
  protected
    // this never gets called without a Form Streaming class, which fpGUI doesn't use
    procedure   Loaded; override;
    procedure   Notification(AComponent: TComponent; Operation: TOperation); override;
    procedure   DoClick(Sender: TObject);
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
    procedure   AddItem(const FileName: string);
    function    RemoveItem(const FileName : string) : boolean;
    procedure   LoadMRU;
  published
    property    MaxItems: Integer read FMaxItems write SetMaxItems default 4;
//    property    IniFilePath: string read FIniFilePath write SetIniFilePath;
    property    ShowFullPath: boolean read FShowFullPath write SetShowFullPath default True;
    property    ParentMenuItem: TfpgPopupMenu read FParentMenuItem write SetParentMenuItem;
    property    OnClick: TMRUClickEvent read FOnClick write FOnClick;
  end;


implementation

uses
  fpg_iniutils;

type
  //to be able to recognize MRU menu item when deleting
  TMRUMenuItem = class(TfpgMenuItem);


{ TfpgMRU }

procedure TfpgMRU.SetMaxItems(const AValue: Integer);
begin
  if AValue <> FMaxItems then
  begin
    if AValue < 1 then
      FMaxItems := 1
    else
    begin
      if AValue > High(Word) then // 65535 should be enough
        FMaxItems := High(Word)
      else
      begin
        FMaxItems := AValue;
        FItems.BeginUpdate;
        try
          while FItems.Count > MaxItems do
            FItems.Delete(FItems.Count - 1);
        finally
          FItems.EndUpdate;
        end;
      end;
    end;  { if/else }
  end;
end;

{
procedure TfpgMRU.SetIniFilePath(const AValue: string);
begin
  if FIniFilePath=AValue then exit;
  FIniFilePath:=AValue;
end;
}

procedure TfpgMRU.SetParentMenuItem(const AValue: TfpgPopupMenu);
begin
  if AValue = FParentMenuItem then
    Exit;
  FParentMenuItem := AValue;
end;

procedure TfpgMRU.SetShowFullPath(const AValue: boolean);
begin
  if FShowFullPath <> AValue then
  begin
    FShowFullPath := AValue;
    ItemsChange(Self);
  end;
end;

procedure TfpgMRU.LoadMRU;
var
  i: cardinal;
begin
  FItems.BeginUpdate;
  FItems.Clear;
  try
    for i := 1 to FMaxItems do
      if gINI.ValueExists('MRU', 'MRU'+IntToStr(i)) then
        FItems.Add(gINI.ReadString('MRU', 'MRU'+IntToStr(i), ''));
  finally
    FItems.EndUpdate;
  end;
end;

procedure TfpgMRU.SaveMRU;
var
  i: integer;
begin
  if FItems.Count = 0 then
    Exit;
    
  //delete old mru
  i := 1;
  while gINI.ValueExists('MRU', 'MRU'+IntToStr(i)) do
  begin
    gINI.DeleteKey('MRU', 'MRU'+IntToStr(i));
    Inc(i);
  end;

  //write new mru
  for i := 0 to FItems.Count-1 do
    gINI.WriteString('MRU', 'MRU'+IntToStr(i+1), FItems[i]);
end;

procedure TfpgMRU.ItemsChange(Sender: TObject);
var
  i: Integer;
  NewMenuItem: TfpgMenuItem;
  FileName: String;
begin
//  writeln('TfpgMRU.ItemsChange');
  if ParentMenuItem <> nil then
  begin
    ClearParentMenu;
    if FItems.Count = 0 then
      ParentMenuItem.AddMenuItem('-', '', nil); // add something if we have no previous MRU's
    for i := 0 to -1 + FItems.Count do
    begin
      if ShowFullPath then
        FileName := StringReplace(FItems[I], '&', '&&', [rfReplaceAll, rfIgnoreCase])
      else
        FileName := StringReplace(ExtractFileName(FItems[i]), '&', '&&', [rfReplaceAll, rfIgnoreCase]);

//      NewMenuItem := ParentMenuItem.AddMenuItem(Format('%s', [FileName]), '', @DoClick);
//      NewMenuItem.Tag := i;
      NewMenuItem := TMRUMenuItem.Create(ParentMenuItem);
      NewMenuItem.Text    := Format('%s', [FileName]);
      NewMenuItem.Tag     := i;
      NewMenuItem.OnClick := @DoClick;
    end;
  end;
end;

procedure TfpgMRU.ClearParentMenu;
//var
//  i:integer;
begin
  if Assigned(ParentMenuItem) then
    ParentMenuItem.DestroyComponents;
{
    for i := ParentMenuItem.ComponentCount-1 downto 0 do
      if ParentMenuItem.Components[i] is TMRUMenuItem then
        ParentMenuItem.Delete(i);
}
end;

procedure TfpgMRU.Loaded;
begin
  inherited Loaded;
  if not (csDesigning in ComponentState) then
//    if FIniFilePath <> '' then
      LoadMRU;
end;

procedure TfpgMRU.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if (Operation = opRemove) and (AComponent = FParentMenuItem) then
    FParentMenuItem := nil;
end;

procedure TfpgMRU.DoClick(Sender: TObject);
begin
  if Assigned(FOnClick) and (Sender is TMRUMenuItem) then
    FOnClick(Self, FItems[TMRUMenuItem(Sender).Tag]);
end;

constructor TfpgMRU.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FParentMenuItem := nil;
  FItems := TStringList.Create;
  FItems.OnChange := @ItemsChange;
  FMaxItems := 4;
  FShowFullPath := True;
//  Loaded;
end;

destructor TfpgMRU.Destroy;
begin
  if not (csDesigning in ComponentState) then
    SaveMRU;
  FItems.OnChange := nil;
  FItems.Free;
  inherited Destroy;
end;

procedure TfpgMRU.AddItem(const FileName: string);
begin
  if FileName <> '' then
  begin
    FItems.BeginUpdate;
    try
      if FItems.IndexOf(FileName) > -1 then
        FItems.Delete(FItems.IndexOf(FileName));
      FItems.Insert(0, FileName);

      while FItems.Count > MaxItems do
        FItems.Delete(MaxItems);
    finally
      FItems.EndUpdate;
    end;
  end;
end;

function TfpgMRU.RemoveItem(const FileName: string): boolean;
begin
  if FItems.IndexOf(FileName) > -1 then
  begin
    FItems.Delete(FItems.IndexOf(FileName));
    Result := True;
  end
  else
    Result := False;
end;

end.