Menu

[r4627]: / branches / parsnip / Src / FmFavouritesDlg.pas  Maximize  Restore  History

Download this file

820 lines (734 with data), 24.6 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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
{
* 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
ExtCtrls,
Controls,
StdCtrls,
Classes,
ActnList,
ComCtrls,
Forms,
Types,
Generics.Defaults,
// 3rd party
Collections.Base,
Collections.Lists,
// Project
CS.Database.Types,
FmGenericNonModalDlg,
IntfNotifier,
UBox,
UWindowSettings;
type
/// <summary>Implements a non-modal dialogue box that displays and manages
/// the user's favourite snippets.</summary>
/// <remarks>By default the dialogue box fades to semi-transparency when it
/// is not active and returns to full opacity when activated.</remarks>
TFavouritesDlg = class(TGenericNonModalDlg)
actDelete: TAction;
actDeleteAll: TAction;
actDisplay: TAction;
alDlg: TActionList;
btnDelete: TButton;
btnDeleteAll: TButton;
btnDisplay: TButton;
chkNewTab: TCheckBox;
lblTransparency: TLabel;
tbTransparency: TTrackBar;
lbFavs: TListBox;
/// <summary>Clears all favourites.</summary>
/// <remarks>Prompts user for permission before proceeding.</remarks>
procedure actDeleteAllExecute(Sender: TObject);
/// <summary>Disables DeleteAll action iff there are no favourites.
/// </summary>
procedure actDeleteAllUpdate(Sender: TObject);
/// <summary>Removes favourite selected in list view from list.</summary>
procedure actDeleteExecute(Sender: TObject);
/// <summary>Enables Delete action iff a favourite is selected in list
/// view.</summary>
procedure actDeleteUpdate(Sender: TObject);
/// <summary>Displays favourite selected in list view.</summary>
procedure actDisplayExecute(Sender: TObject);
/// <summary>Enables Display action iff a favourite is selected in list
/// view.</summary>
procedure actDisplayUpdate(Sender: TObject);
/// <summary>Fades form to full opacity when dialogue box is activated.
/// </summary>
procedure FormActivate(Sender: TObject);
/// <summary>Causes form's object instance to be automatically freed when
/// closed.</summary>
procedure FormClose(Sender: TObject; var Action: TCloseAction);
/// <summary>Sets default property values when form is created.</summary>
procedure FormCreate(Sender: TObject);
/// <summary>Fades form to specified transparency when dialogue box is
/// de-activated.</summary>
procedure FormDeactivate(Sender: TObject);
/// <summary>Saves form's window state before form is destroyed.</summary>
procedure FormDestroy(Sender: TObject);
/// <summary>Adjusts form transparency according to new trackbar position
/// whenever it changes.</summary>
procedure tbTransparencyChange(Sender: TObject);
/// <summary>Records that a key has been pressed over the transparency
/// trackbar and fades the form down to the transparency specified by the
/// trackbar.</summary>
procedure tbTransparencyKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
/// <summary>Records that the currently depressed key has been released
/// over the trackbar and fades the form back up to full opacity.</summary>
procedure tbTransparencyKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure lbFavsDblClick(Sender: TObject);
strict private
type
/// <summary>Reads and stores persistent options in settings.</summary>
TPersistentOptions = class(TObject)
strict private
var
/// <summary>Value of DisplayInNewTabs property.</summary>
fDisplayInNewTabs: Boolean;
/// <summary>Value of InactiveAlphaBlendValue property.</summary>
fInactiveAlphaBlendValue: Byte;
const
/// <summary>Name of settings key where DisplayInNewTabs property is
/// stored.</summary>
DisplayInNewTabsKey = 'DisplayInNewTabs';
/// <summary>Name of settings key where InactiveAlphaBlendValue
/// property is stored.</summary>
InactiveAlphaBlendValueKey = 'InactiveAlphaBlendValue';
public
/// <summary>Constructs object and reads options from settings.
/// </summary>
constructor Create;
/// <summary>Writes options to settings then destroys object.</summary>
destructor Destroy; override;
/// <summary>Specifies whether or not selected favourites are to be
/// displayed in new tabs in main window's detail pane.</summary>
property DisplayInNewTabs: Boolean
read fDisplayInNewTabs write fDisplayInNewTabs;
/// <summary>Stores alpha blend value that determines transparency of
/// deactivated form.</summary>
property InactiveAlphaBlendValue: Byte
read fInactiveAlphaBlendValue write fInactiveAlphaBlendValue;
end;
TListBoxMgr = class(TObject)
strict private
var
fSnippetList: TSortedList<ISnippet>;
fLB: TListBox;
fSortFn: TComparison<ISnippet>;
function IndexOfSnippet(const SnippetID: TSnippetID): Integer;
function GetSnippetAt(const Idx: Integer): ISnippet;
procedure SetSortFn(const SortFn: TComparison<ISnippet>);
procedure InternalAddSnippet(Snippet: ISnippet);
procedure InternalDeleteSnippetAt(const Idx: Integer);
procedure LBDrawItem(Control: TWinControl; Index: Integer; Rect: TRect;
State: TOwnerDrawState);
procedure LBData(Control: TWinControl; Index: Integer;
var Data: string);
public
constructor Create(LB: TListBox);
destructor Destroy; override;
procedure Populate(SnippetIDs: ISnippetIDList);
procedure Clear;
procedure Delete(const SnippetID: TSnippetID);
procedure Add(Snippet: ISnippet);
procedure Update(Snippet: ISnippet);
function HasSelection: Boolean;
function GetSelected: TSnippetID;
procedure ReSort(const SortFn: TComparison<ISnippet>);
end;
strict private
var
/// <summary>Object that manages display and painting of list box that
/// displays favourite snippets.</summary>
fFavsLBMgr: TListBoxMgr;
/// <summary>Object encapsulating favourites.</summary>
fFavourites: ISnippetIDList;
/// <summary>Notifier object used to request display of snippets in main
/// form.</summary>
fNotifier: INotifier;
/// <summary>Form's persistent options.</summary>
fOptions: TPersistentOptions;
/// <summary>Object used to read / write window position in settings.
/// </summary>
fWindowSettings: TDlgWindowSettings;
/// <summary>Flag indicating if a key is currently pressed when the
/// transparency trackbar has keyboard focus.</summary>
fTrackBarKeyDown: Boolean;
/// <summary>Flag indicating if the mouse is currently pressed over the
/// transparency trackbar.</summary>
fTrackBarMouseDown: Boolean;
class var
/// <summary>Reference to dialogue box form instance.</summary>
/// <remarks>Set to nil if there is no instance of the form.</remarks>
fInstance: TFavouritesDlg;
strict private
procedure LoadFavourites;
/// <summary>Changes the transparency of the form from its current level to
/// the given new transparency level.</summary>
/// <remarks>The change in transparency is performed in small steps to make
/// it appear animated.</remarks>
procedure FadeTo(const NewTransparency: Byte);
/// <summary>Fades the form in from its current transparent state to full
/// opacity.</summary>
procedure FadeIn;
/// <summary>Fades the form from its current transparency to the level
/// indicated by the transparency trackbar.</summary>
procedure FadeOut;
/// <summary>Records that a mouse button has been pressed over the
/// transparency trackbar and fades the form down to the transparency
/// specified by the trackbar.</summary>
procedure TBTransparencyMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
/// <summary>Records that the currently depressed mouse button has been
/// released over the trackbar and fades the form back up to full
/// opacity.</summary>
procedure TBTransparencyMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
/// <summary>Handles database change events by updating the favorites list
/// as necessary.</summary>
/// <param name="Sender">TObject [in] Object that triggered event. Not
/// used.</param>
/// <param name="EvtInfo">IInterface [in] Object that carries information
/// about the database change event.</param>
procedure DBChangeEventHandler(Sender: TObject; const EvtInfo: IInterface);
strict protected
/// <summary>Configures some of the form's objects.</summary>
procedure ConfigForm; override;
/// <summary>Arranges and aligns the form's controls.</summary>
procedure ArrangeForm; override;
/// <summary>Initialises form's controls and restores last window position.
/// </summary>
procedure InitForm; override;
public
/// <summary>Displays and intialises the dialogue box non-modally, or
/// re-activates it if it is already displayed.</summary>
/// <param name="AOwner">TComponent [in] Reference to any owning component.
/// May be nil.</param>
/// <param name="Notifier">INotifier [in] Reference to notifier object used
/// to cause a favourite snippet to be displayed in main window.</param>
class procedure Display(AOwner: TComponent; Notifier: INotifier);
/// <summary>Closes the dialogue box, freeing any form instance.</summary>
class procedure Close;
/// <summary>Checks if the form is currently displayed.</summary>
class function IsDisplayed: Boolean;
end;
implementation
uses
// Delphi
SysUtils,
Windows,
Graphics,
// Project
CS.Database.Snippets,
DB.UMain,
IntfCommon,
UComparers,
UCtrlArranger,
UMessageBox,
UPreferences,
USettings,
UStrUtils;
{$R *.dfm}
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?';
var
SnippetID: TSnippetID;
begin
if not TMessageBox.Confirm(Self, sQuery) then
Exit;
// To avoid lots of flicker as items are deleted one by one, we disable
// handling of database change events: we know what the outcome of clearing
// all favourites is, so can put display into correct state.
Database.RemoveChangeEventHandler(DBChangeEventHandler);
try
fFavsLBMgr.Clear;
// NOTE: if this method is re-implemented with the database change handler
// still in place, then the following iteration will need to be done on a
// copy of fFavourites because DBChangeEventHandler will update fFavourites,
// which is not allowed while it is being enumerated.
for SnippetID in fFavourites do
fNotifier.ChangeSnippetStar(SnippetID, False);
fFavourites.Clear;
finally
Database.AddChangeEventHandler(DBChangeEventHandler);
end;
end;
procedure TFavouritesDlg.actDeleteAllUpdate(Sender: TObject);
begin
actDeleteAll.Enabled := not fFavourites.IsEmpty;
end;
procedure TFavouritesDlg.actDeleteExecute(Sender: TObject);
begin
fNotifier.ChangeSnippetStar(fFavsLBMgr.GetSelected, False);
end;
procedure TFavouritesDlg.actDeleteUpdate(Sender: TObject);
begin
actDelete.Enabled := fFavsLBMgr.HasSelection;
end;
procedure TFavouritesDlg.actDisplayExecute(Sender: TObject);
begin
fNotifier.DisplaySnippet(fFavsLBMgr.GetSelected, chkNewTab.Checked);
end;
procedure TFavouritesDlg.actDisplayUpdate(Sender: TObject);
begin
actDisplay.Enabled := fFavsLBMgr.HasSelection;
end;
procedure TFavouritesDlg.ArrangeForm;
begin
TCtrlArranger.AlignTops([lbFavs, btnDisplay], 0);
TCtrlArranger.AlignLefts([lbFavs, chkNewTab], 0);
TCtrlArranger.AlignLefts(
[btnDisplay, btnDelete, btnDeleteAll],
TCtrlArranger.RightOf(lbFavs, 8)
);
TCtrlArranger.MoveBelow(lbFavs, 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); // FreeAndNil is necessary here
end;
procedure TFavouritesDlg.ConfigForm;
begin
inherited;
LoadFavourites;
TTrackBarHack(tbTransparency).OnMouseDown := TBTransparencyMouseDown;
TTrackBarHack(tbTransparency).OnMouseUp := TBTransparencyMouseUp;
Database.AddChangeEventHandler(DBChangeEventHandler);
end;
procedure TFavouritesDlg.DBChangeEventHandler(Sender: TObject;
const EvtInfo: IInterface);
var
EventInfo: IDatabaseChangeEventInfo; // information about the event
Snippet: ISnippet; // snippet referenced in event
/// <summary>Extracts snippet ID from EvtInfo object.</summary>
function EvtInfoToSnippetID: TSnippetID;
begin
Result := (EventInfo.Info as TBox<TSnippetID>).Value;
end;
begin
EventInfo := EvtInfo as IDatabaseChangeEventInfo;
case EventInfo.Kind of
evSnippetAdded:
begin
Snippet := Database.LookupSnippet(EvtInfoToSnippetID);
if Snippet.Starred then
begin
fFavourites.Add(Snippet.ID);
fFavsLBMgr.Add(Snippet);
end;
end;
evBeforeSnippetDelete:
begin
if fFavourites.Contains(EvtInfoToSnippetID) then
begin
// Starred snippet was deleted: remove from list
fFavsLBMgr.Delete(EvtInfoToSnippetID);
fFavourites.Remove(EvtInfoToSnippetID);
end;
end;
evSnippetChanged:
begin
Snippet := Database.LookupSnippet(EvtInfoToSnippetID);
if fFavourites.Contains(Snippet.ID) then
begin
if not Snippet.Starred then
begin
// Snippet that is in favourites list is no longer starred, so remove
// it.
fFavsLBMgr.Delete(Snippet.ID);
fFavourites.Remove(Snippet.ID);
end
else
begin
// Snippet in list is still starred, but has changed in some other
// way, so update list in case sort order affected.
fFavsLBMgr.Update(Snippet);
end;
end
else
begin
if Snippet.Starred then
begin
// Snippet that wasn't in snippet list has been starred, so add it to
// list.
fFavourites.Add(Snippet.ID);
fFavsLBMgr.Add(Snippet);
end;
end;
end;
end;
end;
class procedure TFavouritesDlg.Display(AOwner: TComponent; Notifier: INotifier);
begin
if not Assigned(fInstance) then
fInstance := Create(AOwner);
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 NewTransparency: Byte);
var
Step: Int8;
begin
Step := -1;
if AlphaBlendValue < NewTransparency then
Step := -Step;
while AlphaBlendValue <> NewTransparency do
begin
AlphaBlendValue := AlphaBlendValue + Step;
Sleep(2);
end;
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;
fFavsLBMgr := TListBoxMgr.Create(lbFavs);
fOptions := TPersistentOptions.Create;
fWindowSettings := TDlgWindowSettings.CreateStandAlone(Self);
AlphaBlendValue := High(Byte);
fFavourites := TSnippetIDList.Create;
end;
procedure TFavouritesDlg.FormDeactivate(Sender: TObject);
begin
FadeOut;
end;
procedure TFavouritesDlg.FormDestroy(Sender: TObject);
begin
Database.RemoveChangeEventHandler(DBChangeEventHandler);
fFavsLBMgr.Free;
fOptions.DisplayInNewTabs := chkNewTab.Checked;
fOptions.InactiveAlphaBlendValue := tbTransparency.Position;
fOptions.Free;
fWindowSettings.Save;
inherited;
end;
procedure TFavouritesDlg.InitForm;
begin
inherited;
fWindowSettings.Restore;
fFavsLBMgr.Populate(fFavourites);
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.lbFavsDblClick(Sender: TObject);
begin
actDisplay.Execute;
end;
procedure TFavouritesDlg.LoadFavourites;
begin
fFavourites := Database.SelectSnippets(
function (Snippet: ISnippet): Boolean
begin
Result := Snippet.Starred;
end
);
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 := Section.GetBoolean(DisplayInNewTabsKey, False);
fInactiveAlphaBlendValue := Section.GetInteger(
InactiveAlphaBlendValueKey, 160
);
end;
destructor TFavouritesDlg.TPersistentOptions.Destroy;
var
Section: ISettingsSection;
begin
Section := Settings.EmptySection(ssFavourites);
Section.SetBoolean(DisplayInNewTabsKey, fDisplayInNewTabs);
Section.SetInteger(InactiveAlphaBlendValueKey, fInactiveAlphaBlendValue);
Section.Save;
inherited;
end;
{ TFavouritesDlg.TListBoxMgr }
procedure TFavouritesDlg.TListBoxMgr.Add(Snippet: ISnippet);
var
SelSnippet: ISnippet;
begin
fLB.Items.BeginUpdate;
try
if HasSelection then
SelSnippet := GetSnippetAt(fLB.ItemIndex)
else
SelSnippet := nil;
InternalAddSnippet(Snippet);
if Assigned(SelSnippet) then
fLB.ItemIndex := fSnippetList.IndexOf(SelSnippet);
finally
fLB.Items.EndUpdate;
end;
end;
procedure TFavouritesDlg.TListBoxMgr.Clear;
begin
fLB.Items.BeginUpdate;
try
fLB.ItemIndex := -1;
fLB.Count := 0;
fSnippetList.Clear;
finally
fLB.Items.EndUpdate;
end;
end;
constructor TFavouritesDlg.TListBoxMgr.Create(LB: TListBox);
begin
Assert(Assigned(LB), ClassName + '.Create: LB is nil');
inherited Create;
fLB := LB;
SetSortFn(nil); // this creates fSnippetList so don't need to do it here
fLB.Style := lbVirtualOwnerDraw;
fLB.ItemHeight := 19;
fLB.OnDrawItem := LBDrawItem;
fLB.OnData := LBData;
end;
procedure TFavouritesDlg.TListBoxMgr.Delete(const SnippetID: TSnippetID);
var
SnippetIdx: Integer;
begin
fLB.Items.BeginUpdate;
try
SnippetIdx := IndexOfSnippet(SnippetID);
if SnippetIdx = -1 then
Exit;
InternalDeleteSnippetAt(SnippetIdx);
if SnippetIdx >= fLB.Count then
SnippetIdx := Pred(fLB.Count);
fLB.ItemIndex := SnippetIdx;
finally
fLB.Items.EndUpdate;
end;
end;
destructor TFavouritesDlg.TListBoxMgr.Destroy;
begin
Clear;
fSnippetList.Free;
inherited;
end;
function TFavouritesDlg.TListBoxMgr.GetSelected: TSnippetID;
begin
if not HasSelection then
Exit(TSnippetID.CreateNull);
Result := GetSnippetAt(fLB.ItemIndex).ID;
end;
function TFavouritesDlg.TListBoxMgr.GetSnippetAt(const Idx: Integer): ISnippet;
begin
Result := fSnippetList[Idx];
end;
function TFavouritesDlg.TListBoxMgr.HasSelection: Boolean;
begin
Result := fLB.ItemIndex >= 0;
end;
function TFavouritesDlg.TListBoxMgr.IndexOfSnippet(const SnippetID: TSnippetID):
Integer;
var
Idx: Integer;
begin
for Idx := 0 to Pred(fSnippetList.Count) do
if SnippetID = fSnippetList[Idx].ID then
Exit(Idx);
Result := -1;
end;
procedure TFavouritesDlg.TListBoxMgr.InternalAddSnippet(Snippet: ISnippet);
begin
fSnippetList.Add(Snippet);
fLB.Count := fSnippetList.Count;
end;
procedure TFavouritesDlg.TListBoxMgr.InternalDeleteSnippetAt(
const Idx: Integer);
begin
fLB.Count := fSnippetList.Count - 1;
fSnippetList.RemoveAt(Idx);
end;
procedure TFavouritesDlg.TListBoxMgr.LBData(Control: TWinControl;
Index: Integer; var Data: string);
begin
Data := fSnippetList[Index].Title;
end;
procedure TFavouritesDlg.TListBoxMgr.LBDrawItem(Control: TWinControl;
Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
Canvas: TCanvas;
DisplayText: string;
{ TODO -cSynchSpaces: Revisit this method in the light of synch spaces. }
function IsUserDefinedItem: Boolean;
begin
Result := True;
end;
begin
Assert(Control = fLB,
ClassName + '.LBDrawItem: Event handler called for wrong list box');
Canvas := fLB.Canvas;
DisplayText := GetSnippetAt(Index).Title;
if not (odSelected in State) then
{ TODO -cSynchSpaces: Revisit colour selection in the light of synch spaces.
}
Canvas.Font.Color := Preferences.DBHeadingColours[IsUserDefinedItem];
Canvas.TextRect(
Rect,
Rect.Left + 2,
(Rect.Top + Rect.Bottom - Canvas.TextHeight(DisplayText)) div 2,
DisplayText
);
end;
procedure TFavouritesDlg.TListBoxMgr.Populate(SnippetIDs: ISnippetIDList);
var
SnippetID: TSnippetID;
begin
fLB.Items.BeginUpdate;
try
for SnippetID in SnippetIDs do
InternalAddSnippet(Database.LookupSnippet(SnippetID));
finally
fLB.Items.EndUpdate
end;
end;
procedure TFavouritesDlg.TListBoxMgr.ReSort(
const SortFn: TComparison<ISnippet>);
var
SelSnippet: ISnippet;
begin
if HasSelection then
SelSnippet := GetSnippetAt(fLB.ItemIndex)
else
SelSnippet := nil;
SetSortFn(SortFn);
fLB.Invalidate;
if Assigned(SelSnippet) then
fLB.ItemIndex := fSnippetList.IndexOf(SelSnippet);
end;
procedure TFavouritesDlg.TListBoxMgr.SetSortFn(
const SortFn: TComparison<ISnippet>);
var
NewList: TSortedList<ISnippet>;
OldList: TSortedList<ISnippet>;
Rules: TRules<ISnippet>;
begin
if not Assigned(SortFn) then
fSortFn := function(const Left, Right: ISnippet): Integer
begin
Result := StrCompareText(Left.Title, Right.Title);
end
else
fSortFn := SortFn;
Rules := TRulesFactory<ISnippet>.Construct(
fSortFn,
function (const Value: ISnippet): Integer
begin
Result := Value.ID.Hash;
end
);
if Assigned(fSnippetList) then
begin
OldList := fSnippetList;
NewList := TSortedList<ISnippet>.Create(Rules, OldList);
fSnippetList := NewList;
OldList.Free;
end
else
fSnippetList := TSortedList<ISnippet>.Create(Rules);
end;
procedure TFavouritesDlg.TListBoxMgr.Update(Snippet: ISnippet);
var
Idx: Integer;
begin
fLB.Items.BeginUpdate;
try
Idx := IndexOfSnippet(Snippet.ID);
InternalDeleteSnippetAt(Idx);
InternalAddSnippet(Snippet);
fLB.ItemIndex := IndexOfSnippet(Snippet.ID); // index may have changed
finally
fLB.Items.EndUpdate;
end;
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.