Menu

[r147]: / trunk / Src / FrOverview.pas  Maximize  Restore  History

Download this file

786 lines (736 with data), 28.7 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
{
* FrOverview.pas
*
* Titled frame that displays lists of snippets, arranged in different ways and
* manages user interaction with the displayed items.
*
* v0.1 of 30 Jan 2005 - Original version.
* v0.2 of 18 Feb 2005 - Fixed possible out of bounds error in accessing
* fActions dynamic array before actions stored in it.
* v0.3 of 18 Feb 2005 - Deleted unused units from uses clauses.
* v0.4 of 19 Feb 2005 - Simplified and clarified code in
* TOverviewFrame.Display method.
* - Changed frame to implement the renamed IFrameActionMgr
* and IOverviewActionMgr interfaces.
* v0.5 of 22 Feb 2005 - Changed way that user input is notified to program:
* frame no longer directly records and triggers actions
* but now calls methods of an externally provided
* notifier object.
* - Removed support for IFrameActionMgr interfaces.
* - Added support for ISetNotifier interface.
* - Reworked UI response code to use INotifier object.
* v1.0 of 25 May 2006 - Improved and corrected comments.
* - Made various method parameters constant.
* v1.1 of 11 Feb 2007 - Implemented new IsActive, NextTab and PreviousTab
* methods of ITabFrameDisplayMgr.
* v1.2 of 16 Feb 2007 - Changed so that when database is empty Uncategorised
* heading is not displayed.
* v1.3 of 16 Feb 2007 - Revised to work with redefined and new display
* interfaces from IntfFrameMgrs.
* - Removed redundant fActions field.
* v1.4 of 26 Aug 2008 - Added OnCustomDrawItem event handler to tree view to
* display items from user database in a different colour
* and to display category and other header items in
* bold.
* v1.5 of 13 Jan 2009 - Replaced tree view's draw item event with new TTVDraw
* private class that descends from TSnippetsTVDraw.
* - Made some visbility specifiers strict.
* - Made assert statement use ClassName method.
* v2.0 of 06 Jun 2009 - Removed uncategorised tab and replaced with two new
* tabs: alphabetical (snippets grouped by initial
* letter) and snippet kind (snippets grouped by type).
* - Enabled expanding and collapsing of sections in
* treeview and provided keyboard and local toolbar
* support. State persists when tabs are changed.
* - Clicking to right of tree node now selects it.
* - TTVDraw revised re changes in TSnippetsTVDraw base
* class.
* - Now use a separate set of classes in another unit to
* populate tree view in the various different styles.
* - Added toolbar and popup menu and support for
* ICommandBarConfig using contained TCommandBarMgr
* instance.
* - Renamed some identifiers and components.
*
*
* ***** 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 FrOverview.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.
*
* ***** END LICENSE BLOCK *****
}
unit FrOverview;
interface
uses
// Delphi
ComCtrls, Controls, Classes, Windows, ExtCtrls, StdCtrls, ToolWin, Menus,
// Project
FrTitled, IntfFrameMgrs, IntfNotifier, UCommandBars, UOverviewTreeState,
USnippets, USnippetsTVDraw, UView, UViewItemTreeNode;
type
{
TOverviewFrame:
Titled frame that displays lists of snippets grouped in various ways and
manages user interaction with the displayed items. The frame implements
display manager interfaces, command bar interfaces and notifies application
of user-initiated events via a notifier object.
}
TOverviewFrame = class(TTitledFrame,
ITabbedDisplayMgr, // uses tabs to select different views
IOverviewDisplayMgr, // for loading and selecting snippets
IPaneInfo, // provides information about pane
ISetNotifier, // sets notifier object
ICommandBarConfig // configuration of "command bars"
)
tcDisplayStyle: TTabControl;
tvSnippets: TTreeView;
tbarOverview: TToolBar;
mnuOverview: TPopupMenu;
procedure tcDisplayStyleChange(Sender: TObject);
procedure tcDisplayStyleChanging(Sender: TObject; var AllowChange: Boolean);
procedure tvSnippetsChanging(Sender: TObject; Node: TTreeNode;
var AllowChange: Boolean);
procedure tvSnippetsCreateNodeClass(Sender: TCustomTreeView;
var NodeClass: TTreeNodeClass);
procedure tvSnippetsDeletion(Sender: TObject; Node: TTreeNode);
procedure tvSnippetsKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure tvSnippetsKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure tvSnippetsMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
strict private
type
{
TTVDraw:
Object used to draw snippets tree view nodes.
}
TTVDraw = class(TSnippetsTVDraw)
strict protected
function IsUserDefinedNode(const Node: TTreeNode): Boolean; override;
{Checks if a node represents a user defined snippets object.
@param Node [in] Node to be checked.
@return True if node represents user defined object, False if not.
}
function IsSectionHeadNode(const Node: TTreeNode): Boolean;
override;
{Checks if a node represents a section header.
@param Node [in] Node to be checked.
@return True if node is a section header, False if not.
}
end;
var
fTVDraw: TTVDraw; // object that renders tree view nodes
fNotifier: INotifier; // notifies app of user initiated events
fCanChange: Boolean; // whether selected node allowed to change
fSelectedItem: TViewItem; // current selected view item in tree view
fPrevSelectedItem: TViewItem; // previous selected view item in tree view
fRoutineList: TRoutineList; // list of currently displayed snippets
fTreeStates: array of TOverviewTreeState;
// array of tree state objects: one per tab
fCommandBars: TCommandBarMgr; // configures popup menu and toolbar
const
cPermittedKeys = [VK_UP, VK_DOWN, VK_PRIOR, VK_NEXT, VK_LEFT, VK_RIGHT];
{Keypresses allowed on treeview}
procedure SelectionChange(const Item: TViewItem);
{Records new selected view item and, if item has changed, triggers action
to notify program of selection change.
@param Item [in] Selected item.
}
procedure Redisplay;
{Redisplays all snippets within current snippet list in required style.
}
procedure InternalSelectItem(const Item: TViewItem);
{Selects the tree node associated with a view item in the tree view if
view item is in the tree view, otherwise deselects current item.
@param Item [in] View item to be selected.
}
function FindItemNode(const Item: TViewItem): TViewItemTreeNode;
{Finds node representing a view item in the tree view.
@param Item [in] View item to be found (may be nil).
@return Tree node associated with view item or nil if no match or if
Item is nil.
}
function GetSectionNode(const Node: TTreeNode): TViewItemTreeNode;
{Gets reference to the top level parent (section) node for any tree node.
@param Node [in] Node whose section node is required.
@return Required section node. Maybe Node if Node is a section node.
}
protected // interface implementations - do not make strict
{ ITabbedDisplayMgr }
procedure SelectTab(const TabIdx: Integer);
{Selects tab with specified index.
@param TabIdx [in] Index of tab to be selected.
}
function SelectedTab: Integer;
{Returns index of currently selected tab.
@return Required tab index.
}
procedure NextTab;
{Switches to next tab, or return to first tab if current tab is last.
}
procedure PreviousTab;
{Switches to previous tab, or return to last tab if current tab is first.
}
{ IOverviewDisplayMgr }
procedure Display(const RoutineList: TRoutineList);
{Displays the snippets in the current overview tab.
@param RoutineList [in] List of snippets to be displayed or nil if
nothing to be displayed.
}
procedure Clear;
{Clears the display.
}
procedure SelectItem(const ViewItem: TViewItem);
{Selects a view item in the overview pane.
@param ViewItem [in] Item to be selected.
}
procedure UpdateTreeState(const State: TTreeNodeAction);
{Updates expand / collapse state of treevew.
@param State [in] Required expand / collapse state.
}
function CanUpdateTreeState(const State: TTreeNodeAction): Boolean;
{Checks if a specified expand / collapse tree view action can be
performed.
@param State [in] Expand / collapse action being queried.
@return True if action can be performed, False if not.
}
{ IPaneInfo }
function IsInteractive: Boolean;
{Checks if the pane is currently interactive with user.
@return True if pane is interactive, False if not.
}
{ ISetNotifier }
procedure SetNotifier(const Notifier: INotifier);
{Sets the object's notifier object to be called in response to user input.
@param Notifier [in] Required notifier object.
}
{ ICommandBarConfig }
property CommandBars: TCommandBarMgr
read fCommandBars implements ICommandBarConfig;
{References contained object implementing ICommandBarConfig interfaces}
public
constructor Create(AOwner: TComponent); override;
{Class constructor. Sets up object.
@param AOwner [in] Component that owns frame.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
end;
implementation
uses
// Delphi
SysUtils,
// Project
UOverviewTreeBuilder;
{$R *.dfm}
{ TOverviewFrame }
function TOverviewFrame.CanUpdateTreeState(
const State: TTreeNodeAction): Boolean;
{Checks if a specified expand / collapse tree view action can be performed.
@param State [in] Expand / collapse action being queried.
@return True if action can be performed, False if not.
}
begin
case State of
taExpandNode:
Result := Assigned(tvSnippets.Selected) and
not GetSectionNode(tvSnippets.Selected).Expanded;
taExpandAll:
Result := True;
taCollapseNode:
Result := Assigned(tvSnippets.Selected) and
GetSectionNode(tvSnippets.Selected).Expanded;
taCollapseAll:
Result := True;
else
Result := False;
end;
end;
procedure TOverviewFrame.Clear;
{Clears the display.
}
begin
SelectItem(nil);
Display(nil);
end;
constructor TOverviewFrame.Create(AOwner: TComponent);
{Class constructor. Sets up object.
@param AOwner [in] Component that owns frame.
}
var
TabIdx: Integer; // loops through tabs
begin
inherited;
// Create delegated (contained) command bar manager for toolbar and popup menu
fCommandBars := TCommandBarMgr.Create(Self);
fCommandBars.AddCommandBar(
cOverviewToolBar, TToolBarWrapper.Create(tbarOverview)
);
fCommandBars.AddCommandBar(
cOverviewPopupMenu, TPopupMenuWrapper.Create(mnuOverview)
);
// Create new empty objects to store current and previous selected view items
fSelectedItem := TViewItem.Create;
fPrevSelectedItem := TViewItem.Create;
// Create treeview draw object
fTVDraw := TTVDraw.Create;
tvSnippets.OnCustomDrawItem := fTVDraw.CustomDrawItem;
// Create list to store displayed snippets
fRoutineList := TRoutineList.Create;
SetLength(fTreeStates, tcDisplayStyle.Tabs.Count);
for TabIdx := 0 to Pred(tcDisplayStyle.Tabs.Count) do
fTreeStates[TabIdx] := TOverviewTreeState.Create(tvSnippets);
end;
destructor TOverviewFrame.Destroy;
{Class destructor. Tears down object.
}
var
TabIdx: Integer; // loops through tabs
begin
for TabIdx := Pred(tcDisplayStyle.Tabs.Count) downto 0 do
FreeAndNil(fTreeStates[TabIdx]);
FreeAndNil(fTVDraw);
FreeAndNil(fPrevSelectedItem);
FreeAndNil(fSelectedItem);
FreeAndNil(fRoutineList); // does not free referenced snippets
inherited;
end;
procedure TOverviewFrame.Display(const RoutineList: TRoutineList);
{Displays the snippets in the current overview tab.
@param RoutineList [in] List of snippets to be displayed or nil if nothing
to be displayed.
}
begin
// Only do update if new snippet list is different to current one
if not fRoutineList.IsEqual(RoutineList) then
begin
// Take copy of new list
fRoutineList.Assign(RoutineList);
Redisplay;
end;
end;
function TOverviewFrame.FindItemNode(const Item: TViewItem): TViewItemTreeNode;
{Finds node representing a view item in the tree view.
@param Item [in] View item to be found (may be nil).
@return Tree node associated with view item or nil if no match or if Item is
nil.
}
var
Idx: Integer; // loops thru all tree nodes
Node: TViewItemTreeNode; // reference to a tree node in tree view
begin
// Assume we can't find node
Result := nil;
if Assigned(Item) then
begin
// Search thru all nodes in tree view
for Idx := 0 to Pred(tvSnippets.Items.Count) do
begin
// Check if current node is required one ...
Node := tvSnippets.Items[Idx] as TViewItemTreeNode;
if Node.ViewItem.IsEqual(Item) then
begin
// ... and get out if so
Result := Node;
Break;
end;
end;
end;
end;
function TOverviewFrame.GetSectionNode(
const Node: TTreeNode): TViewItemTreeNode;
{Gets reference to the top level parent (section) node for any tree node.
@param Node [in] Node whose section node is required.
@return Required section node. Maybe Node if Node is a section node.
}
var
CurrentNode: TTreeNode; // iterates through all parent nodes
begin
CurrentNode := Node;
while (CurrentNode.Level > 0) do
CurrentNode := CurrentNode.Parent;
Result := CurrentNode as TViewItemTreeNode;
end;
procedure TOverviewFrame.InternalSelectItem(const Item: TViewItem);
{Selects the tree node associated with a view item in the tree view if view
item is in the tree view, otherwise deselects current item.
@param Item [in] View item to be selected.
}
var
Node: TViewItemTreeNode; // tree node associated with view item
begin
// Get node associated with given view item
Node := FindItemNode(Item);
// Allow tree view selection to change
fCanChange := True;
if Assigned(Node) then
begin
// We found node: select it and make sure visible
tvSnippets.Selected := Node;
Node.MakeVisible;
end
else
begin
// Can't find item: show top of tree
tvSnippets.Selected := nil;
tvSnippets.TopItem := tvSnippets.Items.GetFirstNode;
end;
// Prevent selection changes
fCanChange := False;
end;
function TOverviewFrame.IsInteractive: Boolean;
{Checks if the pane is currently interactive with user.
@return True if pane is interactive, False if not.
}
var
Idx: Integer; // loops thru frame's owned components
begin
// This pane is interactive if one of owned controls has focus
Result := False;
for Idx := 0 to Pred(ComponentCount) do
if (Components[Idx] is TWinControl)
and (Components[Idx] as TWinControl).Focused then
begin
Result := True;
Break;
end;
end;
procedure TOverviewFrame.NextTab;
{Switches to next tab, or return to first tab if current tab is last.
}
begin
if SelectedTab = Pred(tcDisplayStyle.Tabs.Count) then
SelectTab(0)
else
SelectTab(Succ(SelectedTab));
end;
procedure TOverviewFrame.PreviousTab;
{Switches to previous tab, or return to last tab if current tab is first.
}
begin
if SelectedTab = 0 then
SelectTab(Pred(tcDisplayStyle.Tabs.Count))
else
SelectTab(Pred(SelectedTab));
end;
procedure TOverviewFrame.Redisplay;
{Redisplays all snippets within current snippet list in required style.
}
// ---------------------------------------------------------------------------
function AddViewItemNode(const ParentNode: TViewItemTreeNode;
const ViewItem: TViewItem): TViewItemTreeNode;
{Adds a new node to the tree view that represents a view item.
@param ParentNode [in] Node that is parent of new node.
@param ViewItem [in] View item for which we are adding node.
@return New tree node.
}
begin
Result := tvSnippets.Items.AddChild(ParentNode, ViewItem.Description)
as TViewItemTreeNode;
Result.ViewItem := ViewItem;
end;
// ---------------------------------------------------------------------------
var
Builder: TOverviewTreeBuilder;
BuilderClasses: array of TOverviewTreeBuilderClass;
begin
SetLength(BuilderClasses, tcDisplayStyle.Tabs.Count);
BuilderClasses[cCategorisedTab] := TOverviewCategorisedTreeBuilder;
BuilderClasses[cAlphabeticTab] := TOverviewAlphabeticTreeBuilder;
BuilderClasses[cKindTab] := TOverviewSnipKindTreeBuilder;
Builder := nil;
tvSnippets.Items.BeginUpdate;
try
fCanChange := False;
tvSnippets.Items.Clear;
if fRoutineList.Count = 0 then
Exit;
Builder := BuilderClasses[tcDisplayStyle.TabIndex].Create(
tvSnippets, fRoutineList
);
Builder.Build;
tvSnippets.FullExpand;
fTreeStates[tcDisplayStyle.TabIndex].RestoreState;
finally
FreeAndNil(Builder);
fCanChange := True;
tvSnippets.Items.EndUpdate;
end;
// Reselect current view item if possible
InternalSelectItem(fSelectedItem);
end;
function TOverviewFrame.SelectedTab: Integer;
{Returns index of currently selected tab.
@return Required tab index.
}
begin
Result := tcDisplayStyle.TabIndex;
end;
procedure TOverviewFrame.SelectionChange(const Item: TViewItem);
{Records new selected view item and, if item has changed, triggers action to
notify program of selection change.
@param Item [in] Selected item.
}
begin
// Record new selected item
fSelectedItem.Assign(Item);
if not fSelectedItem.IsEqual(fPrevSelectedItem) then
begin
// Item has actually changed: store as previously selected item
fPrevSelectedItem.Assign(fSelectedItem);
// Notify application of change
if Assigned(fNotifier) then
fNotifier.ShowViewItem(fSelectedItem);
end;
end;
procedure TOverviewFrame.SelectItem(const ViewItem: TViewItem);
{Selects a view item in the overview pane.
@param ViewItem [in] Item to be selected.
}
begin
// Select in tree view
InternalSelectItem(ViewItem);
// Record view item as selected one
fSelectedItem.Assign(ViewItem);
fPrevSelectedItem.Assign(fSelectedItem);
end;
procedure TOverviewFrame.SelectTab(const TabIdx: Integer);
{Selects tab with specified index.
@param TabIdx [in] Tab to be selected.
}
begin
Assert((TabIdx >= 0) and (TabIdx < tcDisplayStyle.Tabs.Count),
ClassName + '.SelectTab: TabIdx out range');
tcDisplayStyle.TabIndex := TabIdx;
Redisplay;
end;
procedure TOverviewFrame.SetNotifier(const Notifier: INotifier);
{Sets the object's notifier object to be called in response to user input.
@param Notifier [in] Required notifier object.
}
begin
fNotifier := Notifier;
end;
procedure TOverviewFrame.tcDisplayStyleChange(Sender: TObject);
{Handles tab set's tab change event. Notifies application of display style
change via notifier object.
@param Sender [in] Not used.
}
begin
fNotifier.ChangeOverviewStyle(tcDisplayStyle.TabIndex);
end;
procedure TOverviewFrame.tcDisplayStyleChanging(Sender: TObject;
var AllowChange: Boolean);
{Handles tab sets tab changing event, called just before tab changes. Stores
current state of tree in tab that is closing.
@param Sender [in] Not used.
@param AllowChanges [in/out] Not used or modified. Permits tab change.
}
begin
fTreeStates[tcDisplayStyle.TabIndex].SaveState;
end;
procedure TOverviewFrame.tvSnippetsChanging(Sender: TObject;
Node: TTreeNode; var AllowChange: Boolean);
{Handles event triggered when snippets tree view selection is about to change.
We allow or prevent this according to state of fCanChange flag.
@param Sender [in] Not used.
@param Node [in] Not used.
@param AllowChange [in/out] Set true to permit selection change or false to
prevent.
}
begin
AllowChange := fCanChange;
end;
procedure TOverviewFrame.tvSnippetsCreateNodeClass(Sender: TCustomTreeView;
var NodeClass: TTreeNodeClass);
{Event handler called when a tree node is being created that provdes class
type to be used for new node. We notify that node is to be TViewItemTreeNode.
@param Sender [in] Not used.
@param NodeClass [in/out] Set to TViewItemTreeNode.
}
begin
NodeClass := TViewItemTreeNode;
end;
procedure TOverviewFrame.tvSnippetsDeletion(Sender: TObject;
Node: TTreeNode);
{Handles event triggered when a tree node is to be deleted. We free associated
view item.
@param Sender [in] Not used.
@param Node [in] Reference to node being freed (of type TViewItemTreeNode).
}
begin
if Assigned(Node) then
(Node as TViewItemTreeNode).ViewItem.Free;
end;
procedure TOverviewFrame.tvSnippetsKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
{Handles a key down event on tree view. We manage movement thru tree view in
response to Up, Down, PageUp and PageDown. We permit selection to change in
when one of these keys is pressed.
@param Sender [in] Not used.
@param Key [in/out] Key being pressed. Not altered.
@param Shift [in] Modifier keys.
}
begin
if Key in cPermittedKeys then
fCanChange := True;
end;
procedure TOverviewFrame.tvSnippetsKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
{Handles a key up event on tree view. We manage movement thru tree view in
response to Up, Down, PageUp and PageDown keys. Release of one these keys
follows a selection change and we trigger a notification of this event from
here.
@param Sender [in] Not used.
@param Key [in/out] Key pressed. Not altered.
@param Shift [in] Modifier keys.
}
var
Node: TTreeNode; // selected tree node
begin
if Key in cPermittedKeys then
begin
// One of keys triggering selection change was released. We get reference to `
// selected node and trigger notification via SelectionChange method
Node := tvSnippets.Selected;
if Assigned(Node) and (Node is TViewItemTreeNode) then
SelectionChange((Node as TViewItemTreeNode).ViewItem);
end;
// Prevent further changes
fCanChange := False;
end;
procedure TOverviewFrame.tvSnippetsMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
{Handles mouse down events on the snippets tree view. If the mouse click is
on a tree node we allow the node to be selected.
@param Sender [in] Not used.
@param Button [in] Not used.
@param Shift [in] Not used.
@param X X ]in] position of mouse.
@param Y Y [in] position of mouse.
}
var
Node: TTreeNode; // tree node clicked
begin
// Check if mouse click on a tree node
if [htOnItem, htOnRight] * tvSnippets.GetHitTestInfoAt(X, Y) <> [] then
begin
// Get node clicked
Node := tvSnippets.GetNodeAt(X, Y);
if Assigned(Node) and (Node is TViewItemTreeNode) then
begin
// Select the node
fCanChange := True;
tvSnippets.Selected := Node;
fCanChange := False;
// Notify selection
SelectionChange((Node as TViewItemTreeNode).ViewItem);
end;
end;
end;
procedure TOverviewFrame.UpdateTreeState(const State: TTreeNodeAction);
{Updates expand / collapse state of treevew.
@param State [in] Required expand / collapse state.
}
var
Node: TViewItemTreeNode; // currently selected node
SectionNode: TViewItemTreeNode; // section node to which Node belongs
begin
// Get current node (if any) and corresponding section node (if any)
Node := FindItemNode(fSelectedItem);
if Assigned(Node) then
SectionNode := GetSectionNode(Node)
else
SectionNode := nil;
// Perform expand or collapse
tvSnippets.Items.BeginUpdate;
try
case State of
taExpandNode:
begin
// expand section to which selected node belongs maintaining &
// re-selecting current selection
if Assigned(SectionNode) then
SectionNode.Expanded := True;
InternalSelectItem(fSelectedItem);
end;
taExpandAll:
begin
// expand all tree, maintaining & re-selecting current selection
tvSnippets.FullExpand;
InternalSelectItem(fSelectedItem);
end;
taCollapseNode:
begin
// collapse section to which selected node belongs
// select the section node
fCanChange := True;
tvSnippets.Selected := SectionNode;
fCanChange := False;
// collapse section and notify change of selection
if Assigned(SectionNode) then
begin
SectionNode.Expanded := False;
SelectionChange((SectionNode as TViewItemTreeNode).ViewItem);
end;
end;
taCollapseAll:
begin
// collapse whole tree
// select the node
fCanChange := True;
tvSnippets.Selected := SectionNode;
fCanChange := False;
// collapse whole tree and notify change of selection
tvSnippets.FullCollapse;
if Assigned(SectionNode) then
SelectionChange((SectionNode as TViewItemTreeNode).ViewItem);
end;
end;
finally
tvSnippets.Items.EndUpdate;
end;
end;
{ TOverviewFrame.TTVDraw }
function TOverviewFrame.TTVDraw.IsSectionHeadNode(
const Node: TTreeNode): Boolean;
{Checks if a node represents a section header.
@param Node [in] Node to be checked.
@return True if node is a section header, False if not.
}
begin
Result := (Node as TViewItemTreeNode).ViewItem.Kind
in [vkCategory, vkAlphabet, vkSnipKind];
end;
function TOverviewFrame.TTVDraw.IsUserDefinedNode(
const Node: TTreeNode): Boolean;
{Checks if a node represents a user defined snippets object.
@param Node [in] Node to be checked.
@return True if node represents user defined object, False if not.
}
var
ViewItem: TViewItem; // view item represented by node
begin
ViewItem := (Node as TViewItemTreeNode).ViewItem;
Result := ((ViewItem.Kind = vkRoutine) and ViewItem.Routine.UserDefined)
or ((ViewItem.Kind = vkCategory) and ViewItem.Category.UserDefined);
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.