Menu

[r2742]: / trunk / Src / FmFavouritesDlg.pas  Maximize  Restore  History

Download this file

576 lines (516 with data), 16.0 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a dialogue box that displays and manages the user's favourite
* snippets.
}
unit FmFavouritesDlg;
interface
uses
// Delphi
Classes, ActnList, StdCtrls, Controls, ExtCtrls, Forms, ComCtrls, Types,
Generics.Collections {must be listed after Classes},
// 3rd party
LVEx,
// Project
USnippetIDs,
FmGenericNonModalDlg, Favourites.UFavourites, IntfNotifier, UWindowSettings;
type
TFavouritesDlg = class(TGenericNonModalDlg)
alDlg: TActionList;
btnDisplay: TButton;
btnDelete: TButton;
btnDeleteAll: TButton;
actDelete: TAction;
actDeleteAll: TAction;
actDisplay: TAction;
chkNewTab: TCheckBox;
tbTransparency: TTrackBar;
lblTransparency: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure actDisplayUpdate(Sender: TObject);
procedure actDisplayExecute(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure actDeleteUpdate(Sender: TObject);
procedure actDeleteExecute(Sender: TObject);
procedure actDeleteAllUpdate(Sender: TObject);
procedure actDeleteAllExecute(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormDeactivate(Sender: TObject);
procedure tbTransparencyChange(Sender: TObject);
procedure tbTransparencyKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure tbTransparencyKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
strict private
type
TPersistentOptions = class(TObject)
strict private
var
fDisplayInNewTabs: Boolean;
fInactiveAlphaBlendValue: Byte;
public
constructor Create;
destructor Destroy; override;
property DisplayInNewTabs: Boolean
read fDisplayInNewTabs write fDisplayInNewTabs;
property InactiveAlphaBlendValue: Byte
read fInactiveAlphaBlendValue write fInactiveAlphaBlendValue;
end;
strict private
var
fLVFavs: TListViewEx;
fFavourites: TFavourites;
fNotifier: INotifier;
fOptions: TPersistentOptions;
fWindowSettings: TDlgWindowSettings;
fTrackBarKeyDown: Boolean;
fTrackBarMouseDown: Boolean;
class var
fInstance: TFavouritesDlg;
/// <summary>Specifies that a TFavouriteListItem is to be used to create
/// new list view items.</summary>
procedure LVFavouriteCreateItemClass(Sender: TCustomListView;
var ItemClass: TListItemClass);
/// <summary>Compares two list items, Item1 and Item2, using text contained
/// in row specified Data parameter, recording result of comparison in
/// Compare parameter.</summary>
procedure LVFavouritesCompare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
procedure LVDoubleClick(Sender: TObject);
procedure LVCustomDrawItem(Sender: TCustomListView; Item: TListItem;
State: TCustomDrawState; var DefaultDraw: Boolean);
procedure LVCustomDrawSubItem(Sender: TCustomListView; Item: TListItem;
SubItem: Integer; State: TCustomDrawState; var DefaultDraw: Boolean);
/// <summary>Returns text of list item LI in column specified by Idx.
/// </summary>
/// <remarks>Idx = 0 returns list item's caption while Idx > 0 returns text
/// of SubItem[Idx-1].</remarks>
procedure CreateLV;
procedure PopulateLV;
procedure ReSortLV;
procedure AddLVItem(const Favourite: TFavourite);
procedure RemoveLVItem(const Favourite: TFavourite);
procedure FavouritesListener(Sender: TObject; const EvtInfo: IInterface);
procedure FadeTo(const EndTransparency: Byte);
procedure FadeIn;
procedure FadeOut;
function FindListItem(const SnippetID: TSnippetID): TListItem;
procedure TBTransparencyMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure TBTransparencyMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
strict protected
procedure ConfigForm; override;
procedure ArrangeForm; override;
procedure InitForm; override;
public
class procedure Display(AOwner: TComponent; const Favourites: TFavourites;
Notifier: INotifier);
class procedure Close;
class function IsDisplayed: Boolean;
end;
implementation
uses
// Delphi
SysUtils, DateUtils, Windows, Graphics,
// Project
UCtrlArranger, UMessageBox, UPreferences, USettings, UStructs, UStrUtils;
{$R *.dfm}
type
/// <summary>Custom list item class that adds ability to store a TFavourite
/// record with list item.</summary>
TFavouriteListItem = class(TListItem)
strict private
var
/// <summary>Value of Warning property.</summary>
fFavourite: TFavourite;
public
/// <summary>Favourite associated with list item.</summary>
property Favourite: TFavourite read fFavourite write fFavourite;
end;
type
/// <summary>Hack to enable OnMouseUp and OnMouseDown events of TTrackBar to
/// be set.</summary>
/// <remarks>For some reason these events are protected in TTrackBar.
/// </remarks>
TTrackBarHack = class(TTrackBar);
{ TFavouritesDlg }
procedure TFavouritesDlg.actDeleteAllExecute(Sender: TObject);
resourcestring
sQuery = 'Do you really want to delete all your favourites?';
begin
if TMessageBox.Confirm(Self, sQuery) then
begin
fLVFavs.Items.BeginUpdate;
try
fFavourites.Clear;
finally
fLVFavs.Items.EndUpdate;
end;
end;
end;
procedure TFavouritesDlg.actDeleteAllUpdate(Sender: TObject);
begin
actDeleteAll.Enabled := not fFavourites.IsEmpty;
end;
procedure TFavouritesDlg.actDeleteExecute(Sender: TObject);
var
LI: TFavouriteListItem;
begin
LI := fLVFavs.Selected as TFavouriteListItem;
fFavourites.Remove(LI.Favourite.SnippetID);
end;
procedure TFavouritesDlg.actDeleteUpdate(Sender: TObject);
begin
actDelete.Enabled := Assigned(fLVFavs.Selected);
end;
procedure TFavouritesDlg.actDisplayExecute(Sender: TObject);
var
LI: TFavouriteListItem;
SelectedSnippet: TSnippetID;
begin
LI := fLVFavs.Selected as TFavouriteListItem;
SelectedSnippet := LI.Favourite.SnippetID;
fNotifier.DisplaySnippet(
SelectedSnippet.Name,
SelectedSnippet.UserDefined,
chkNewTab.Checked
);
fFavourites.Touch(SelectedSnippet);
fLVFavs.Selected := FindListItem(SelectedSnippet);
end;
procedure TFavouritesDlg.actDisplayUpdate(Sender: TObject);
begin
actDisplay.Enabled := Assigned(fLVFavs.Selected);
end;
procedure TFavouritesDlg.AddLVItem(const Favourite: TFavourite);
var
LI: TFavouriteListItem;
begin
LI := fLVFavs.Items.Add as TFavouriteListItem;
LI.Caption := Favourite.SnippetID.Name;
if IsToday(Favourite.LastAccessed) then
LI.SubItems.Add(TimeToStr(Favourite.LastAccessed))
else
LI.SubItems.Add(DateTimeToStr(Favourite.LastAccessed));
LI.Favourite := Favourite;
end;
procedure TFavouritesDlg.ArrangeForm;
begin
TCtrlArranger.AlignTops([fLVFavs, btnDisplay], 0);
TCtrlArranger.AlignLefts([fLVFavs, chkNewTab], 0);
TCtrlArranger.AlignLefts(
[btnDisplay, btnDelete, btnDeleteAll],
TCtrlArranger.RightOf(fLVFavs, 8)
);
TCtrlArranger.MoveBelow(fLVFavs, chkNewTab, 8);
pnlBody.ClientWidth := TCtrlArranger.TotalControlWidth(pnlBody) + 4;
pnlBody.ClientHeight := TCtrlArranger.TotalControlHeight(pnlBody);
inherited;
TCtrlArranger.AlignLefts([pnlBody, lblTransparency]);
TCtrlArranger.MoveToRightOf(lblTransparency, tbTransparency, 4);
TCtrlArranger.AlignTops([btnClose, lblTransparency, tbTransparency]);
end;
class procedure TFavouritesDlg.Close;
begin
if IsDisplayed then
FreeAndNil(fInstance);
end;
procedure TFavouritesDlg.ConfigForm;
begin
inherited;
fFavourites.AddListener(FavouritesListener);
TTrackBarHack(tbTransparency).OnMouseDown := TBTransparencyMouseDown;
TTrackBarHack(tbTransparency).OnMouseUp := TBTransparencyMouseUp;
end;
procedure TFavouritesDlg.CreateLV;
resourcestring
sSnippetName = 'Snippet';
sLastAccessed = 'Last used';
begin
fLVFavs := TListViewEx.Create(Self);
with fLVFavs do
begin
Parent := pnlBody;
Height := 240;
Width := 360;
HideSelection := False;
ReadOnly := True;
RowSelect := True;
TabOrder := 0;
TabStop := True;
ViewStyle := vsReport;
SortImmediately := False;
with Columns.Add do
begin
Caption := sSnippetName;
Width := 180;
end;
with Columns.Add do
begin
Caption := sLastAccessed;
Width := 140;
end;
OnDblClick := LVDoubleClick;
OnCompare := LVFavouritesCompare;
OnCreateItemClass := LVFavouriteCreateItemClass;
OnCustomDrawItem := LVCustomDrawItem;
OnCustomDrawSubItem := LVCustomDrawSubItem;
end;
end;
class procedure TFavouritesDlg.Display(AOwner: TComponent;
const Favourites: TFavourites; Notifier: INotifier);
begin
if not Assigned(fInstance) then
fInstance := Create(AOwner);
fInstance.fFavourites := Favourites;
fInstance.fNotifier := Notifier;
if not fInstance.Visible then
begin
fInstance.Show;
end;
fInstance.SetFocus;
end;
procedure TFavouritesDlg.FadeIn;
begin
FadeTo(High(Byte));
end;
procedure TFavouritesDlg.FadeOut;
begin
FadeTo(tbTransparency.Position);
end;
procedure TFavouritesDlg.FadeTo(const EndTransparency: Byte);
var
Step: Int8;
begin
Step := -1;
if AlphaBlendValue < EndTransparency then
Step := -Step;
while AlphaBlendValue <> EndTransparency do
begin
AlphaBlendValue := AlphaBlendValue + Step;
Sleep(2);
end;
end;
procedure TFavouritesDlg.FavouritesListener(Sender: TObject;
const EvtInfo: IInterface);
var
Evt: IFavouritesChangeEventInfo;
SelectedSnippet: TSnippetID;
HaveSelection: Boolean;
begin
Evt := EvtInfo as IFavouritesChangeEventInfo;
case Evt.Action of
cnAdded:
begin
HaveSelection := Assigned(fLVFavs.Selected);
if HaveSelection then
SelectedSnippet :=
(fLVFavs.Selected as TFavouriteListItem).Favourite.SnippetID;
AddLVItem(Evt.Favourite);
ReSortLV;
if HaveSelection then
fLVFavs.Selected := FindListItem(SelectedSnippet);
end;
cnRemoved:
begin
HaveSelection := Assigned(fLVFavs.Selected);
if HaveSelection then
SelectedSnippet :=
(fLVFavs.Selected as TFavouriteListItem).Favourite.SnippetID;
RemoveLVItem(Evt.Favourite);
ReSortLV;
if HaveSelection and (Evt.Favourite.SnippetID <> SelectedSnippet) then
fLVFavs.Selected := FindListItem(SelectedSnippet);
end;
end;
end;
function TFavouritesDlg.FindListItem(const SnippetID: TSnippetID): TListItem;
var
LI: TListItem;
begin
for LI in fLVFavs.Items do
begin
if (LI as TFavouriteListItem).Favourite.SnippetID = SnippetID then
Exit(LI);
end;
Result := nil;
end;
procedure TFavouritesDlg.FormActivate(Sender: TObject);
begin
FadeIn;
end;
procedure TFavouritesDlg.FormClose(Sender: TObject; var Action: TCloseAction);
begin
inherited;
fInstance := nil;
Action := caFree;
end;
procedure TFavouritesDlg.FormCreate(Sender: TObject);
begin
inherited;
CreateLV;
fOptions := TPersistentOptions.Create;
fWindowSettings := TDlgWindowSettings.CreateStandAlone(Self);
AlphaBlendValue := High(Byte);
end;
procedure TFavouritesDlg.FormDeactivate(Sender: TObject);
begin
FadeOut;
end;
procedure TFavouritesDlg.FormDestroy(Sender: TObject);
begin
fFavourites.RemoveListener(FavouritesListener);
fOptions.DisplayInNewTabs := chkNewTab.Checked;
fOptions.InactiveAlphaBlendValue := tbTransparency.Position;
fOptions.Free;
fWindowSettings.Save;
inherited;
end;
procedure TFavouritesDlg.InitForm;
begin
inherited;
fWindowSettings.Restore;
PopulateLV;
chkNewTab.Checked := fOptions.DisplayInNewTabs;
tbTransparency.OnChange := nil;
tbTransparency.Position := fOptions.InactiveAlphaBlendValue;
tbTransparency.OnChange := tbTransparencyChange;
end;
class function TFavouritesDlg.IsDisplayed: Boolean;
begin
Result := Assigned(fInstance) and fInstance.Visible;
end;
procedure TFavouritesDlg.LVCustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
UserDefined: Boolean;
begin
UserDefined := (Item as TFavouriteListItem).Favourite.SnippetID.UserDefined;
fLVFavs.Canvas.Font.Color := Preferences.DBHeadingColours[UserDefined];
end;
procedure TFavouritesDlg.LVCustomDrawSubItem(Sender: TCustomListView;
Item: TListItem; SubItem: Integer; State: TCustomDrawState;
var DefaultDraw: Boolean);
begin
fLVFavs.Canvas.Font.Color := clNone; // required to force later colour change
fLVFavs.Canvas.Font.Color := clWindowText;
end;
procedure TFavouritesDlg.LVDoubleClick(Sender: TObject);
begin
actDisplay.Execute;
end;
procedure TFavouritesDlg.LVFavouriteCreateItemClass(Sender: TCustomListView;
var ItemClass: TListItemClass);
begin
ItemClass := TFavouriteListItem;
end;
procedure TFavouritesDlg.LVFavouritesCompare(Sender: TObject; Item1,
Item2: TListItem; Data: Integer; var Compare: Integer);
var
Fav1, Fav2: TFavourite;
begin
Fav1 := (Item1 as TFavouriteListItem).Favourite;
Fav2 := (Item2 as TFavouriteListItem).Favourite;
case fLVFavs.SortColumn of
0:
Compare := Fav1.SnippetID.CompareTo(Fav2.SnippetID);
1:
Compare := CompareDateTime(Fav1.LastAccessed, Fav2.LastAccessed);
end;
if fLVFavs.SortOrder = soDown then
Compare := -Compare;
end;
procedure TFavouritesDlg.PopulateLV;
var
Fav: TFavourite;
begin
fLVFavs.Clear;
for Fav in fFavourites do
AddLVItem(Fav);
fLVFavs.Selected := nil;
ReSortLV;
end;
procedure TFavouritesDlg.RemoveLVItem(const Favourite: TFavourite);
var
LI: TListItem;
begin
for LI in fLVFavs.Items do
begin
if (LI as TFavouriteListItem).Favourite.SnippetID = Favourite.SnippetID then
begin
LI.Free;
Exit;
end;
end;
end;
procedure TFavouritesDlg.ReSortLV;
begin
if fLVFavs.SortColumn <> -1 then
fLVFavs.CustomSort(nil, fLVFavs.SortColumn);
end;
procedure TFavouritesDlg.tbTransparencyChange(Sender: TObject);
begin
if fTrackBarKeyDown or fTrackBarMouseDown then
AlphaBlendValue := tbTransparency.Position;
end;
procedure TFavouritesDlg.tbTransparencyKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
fTrackBarKeyDown := True;
FadeOut;
end;
procedure TFavouritesDlg.tbTransparencyKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
FadeIn;
fTrackBarKeyDown := False;
end;
procedure TFavouritesDlg.TBTransparencyMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
FadeOut;
fTrackBarMouseDown := True;
end;
procedure TFavouritesDlg.TBTransparencyMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
fTrackBarMouseDown := False;
FadeIn;
end;
{ TFavouritesDlg.TPersistentOptions }
constructor TFavouritesDlg.TPersistentOptions.Create;
var
Section: ISettingsSection;
begin
inherited Create;
Section := Settings.ReadSection(ssFavourites);
fDisplayInNewTabs := Boolean(
StrToIntDef(Section.ItemValues['DisplayInNewTabs'], Ord(False))
);
fInactiveAlphaBlendValue := StrToIntDef(
Section.ItemValues['InactiveAlphaBlendValue'], 160)
;
end;
destructor TFavouritesDlg.TPersistentOptions.Destroy;
var
Section: ISettingsSection;
begin
Section := Settings.EmptySection(ssFavourites);
Section.ItemValues['DisplayInNewTabs'] := IntToStr(Ord(fDisplayInNewTabs));
Section.ItemValues['InactiveAlphaBlendValue'] := IntToStr(
fInactiveAlphaBlendValue
);
Section.Save;
inherited;
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.