Menu

[r595]: / trunk / Src / UHistory.pas  Maximize  Restore  History

Download this file

294 lines (264 with data), 8.3 kB

  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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
{
* UHistory.pas
*
* Class that records and retrieves history of page accesses.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UHistory.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2005-2009 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UHistory;
interface
uses
// Project
ULists, UView;
type
{
THistory:
Class that records and retrieves history of page accesses. It provides
access to two lists: the "back" list of items that preceed the currently
selected item in the history and the "forward" list of items that follow the
currently selected item.
}
THistory = class(TObject)
strict private
var
fItems: TObjectListEx; // Stores items in history list
fCursor: Integer; // Index of current history item in list
const
cMaxHistoryItems = 50; // Max items stored in history
function GetCurrent: TViewItem;
{Gets the current view item in history.
@return Reference to current view item or nil if there is no current
item.
}
public
constructor Create;
{Class constructor. Sets up and initialises object.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
procedure Clear;
{Clears history list.
}
procedure NewItem(const ViewItem: TViewItem);
{Creates new history item for a view item.
@param ViewItem [in] View item to be added to history.
}
procedure SelectItem(const ViewItem: TViewItem);
{Selects a view item in history list.
@param ViewItem [in] View item to be selected.
@except EBug raised if ViewItem not in history.
}
function GoBack: TViewItem;
{Moves backward in history.
@return Current item after moving back in history or nil if we were at
start of list before method called.
}
procedure BackList(const List: TViewItemList);
{Builds a list of items in the "back" list.
@param List [in] Receives items in "back" list.
}
function BackListCount: Integer;
{Counts items in "back" list.
@return Number of items in list.
}
function GoForward: TViewItem;
{Moves forward in history.
@return Current item after moving forward in history or nil if we were
at end of list before method called.
}
procedure ForwardList(const List: TViewItemList);
{Builds a list of items in the "forward" list.
@param List [in] Receives items in "forward" list.
}
function ForwardListCount: Integer;
{Counts items in "forward" list.
@return Number of items in list.
}
property Current: TViewItem
read GetCurrent;
{Reference to current view item in history or nil if there is no current
item}
end;
implementation
uses
// Delphi
SysUtils,
// Project
UExceptions;
{ THistory }
procedure THistory.BackList(const List: TViewItemList);
{Builds a list of items in the "back" list.
@param List [in] Receives items in "back" list.
}
var
Idx: Integer; // loops thru "back" list
begin
List.Clear;
// Loop backward thru history list from just before current position, adding
// each item to given list
for Idx := Pred(fCursor) downto 0 do
List.Add(fItems[Idx] as TViewItem);
end;
function THistory.BackListCount: Integer;
{Counts items in "back" list.
@return Number of items in list.
}
begin
// We have entries in back list if current item index > 0
if fCursor > 0 then
Result := fCursor
else
Result := 0;
end;
procedure THistory.Clear;
{Clears history list.
}
begin
fItems.Clear; // frees objects in list
fCursor := -1; // there is no current item
end;
constructor THistory.Create;
{Class constructor. Sets up and initialises object.
}
begin
inherited;
fItems := TObjectListEx.Create(True);
Clear;
end;
destructor THistory.Destroy;
{Class destructor. Tears down object.
}
begin
FreeAndNil(fItems); // frees objects in list
inherited;
end;
procedure THistory.ForwardList(const List: TViewItemList);
{Builds a list of items in the "forward" list.
@param List [in] Receives items in "forward" list.
}
var
Idx: Integer; // loops thru forward list
begin
List.Clear;
// Loop forward thru history list from just after current position, adding
// each item to given list
for Idx := Succ(fCursor) to Pred(fItems.Count) do
List.Add(fItems[Idx] as TViewItem);
end;
function THistory.ForwardListCount: Integer;
{Counts items in "forward" list.
@return Number of items in list.
}
begin
// Number of items in list is count of those following cursor position
Result := Pred(fItems.Count) - fCursor;
if Result < 0 then
Result := 0;
end;
function THistory.GetCurrent: TViewItem;
{Gets the current view item in history.
@return Reference to current view item or nil if there is no current item.
}
begin
if (fCursor >= 0) and (fCursor < fItems.Count) then
Result := fItems[fCursor] as TViewItem
else
Result := nil;
end;
function THistory.GoBack: TViewItem;
{Moves backward in history.
@return Current item after moving back in history or nil if we were at start
of list before method called.
}
begin
if fCursor >= 0 then
Dec(fCursor);
Result := Current;
end;
function THistory.GoForward: TViewItem;
{Moves forward in history.
@return Current item after moving forward in history or nil if we were at
end of list before method called.
}
begin
if fCursor < fItems.Count then
Inc(fCursor);
Result := Current;
end;
procedure THistory.NewItem(const ViewItem: TViewItem);
{Creates new history item for a view item.
@param ViewItem [in] View item to be added to history.
}
var
I: Integer; // loops thru history list
ClonedItem: TViewItem; // cloned copy of view item
begin
Assert(fCursor <= fItems.Count, ClassName + '.NewItem: fCursor too large');
Assert(fCursor >= -1, ClassName + '.NewItem: fCursor too small');
// Create copy of given view item
ClonedItem := TViewItem.Create(ViewItem);
// Increment cursor if possible - it will reference new item
if fCursor < fItems.Count then
Inc(fCursor);
// Store new item in list at required position
if fCursor = fItems.Count then
// cursor beyond end of list so append item to list
fCursor := fItems.Add(ClonedItem)
else
begin
// cursor within list so store new item at current position ...
fItems[fCursor] := ClonedItem;
// ... and delete all items in list after new position
for I := Pred(fItems.Count) downto Succ(fCursor) do
fItems.Delete(I); // this frees associated view item
end;
// Delete first item in list if list is too large and adjust cursor
if fItems.Count > cMaxHistoryItems then
begin
fItems.Delete(0);
Dec(fCursor);
end;
end;
procedure THistory.SelectItem(const ViewItem: TViewItem);
{Selects a view item in history list.
@param ViewItem [in] View item to be selected.
@except EBug raised if ViewItem not in history.
}
var
Idx: Integer; // index of view item in history list
begin
Idx := fItems.IndexOf(ViewItem);
if Idx = -1 then
raise EBug.Create(
ClassName + '.SelectItem: Selected view item not in history list'
);
fCursor := Idx;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.