Menu

[r2000]: / trunk / Src / USnippetsChkListMgr.pas  Maximize  Restore  History

Download this file

283 lines (253 with data), 8.5 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
{
* USnippetsChkListMgr.pas
*
* Implements class that manages and draws check list box controls that display
* lists of snippets.
*
* $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 USnippetsChkListMgr.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2009-2011 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit USnippetsChkListMgr;
interface
uses
// Delphi
Controls, CheckLst, Windows,
// Project
DB.USnippet, USnippetIDs;
type
{
TSnippetsChkListMgr:
Manages and draws check list box controls that display lists of snippets.
Builds and clears list, sets check marks for specified snippets and
maintains and restores snapshots of checked items.
}
TSnippetsChkListMgr = class(TObject)
strict private
fCLB: TCheckListBox; // Reference to check list box being managed
fSaveList: TSnippetList; // Internal snaphot of checked snippets
procedure CheckSnippet(const Snippet: TSnippet);
{Checks entry corresponding to a snippet in check list box. Snippets not
in check list box are ignored.
@param Snippet [in] Snippet to be checked.
}
procedure ClearChecks;
{Clears all checks from items in check list box.
}
procedure DrawItem(Control: TWinControl;
Index: Integer; Rect: TRect; State: TOwnerDrawState);
{OnDrawItem event handler for associated check list box. Draws user
defined snippet names in a different colour to main database snippets.
@param Control [in] Check list box that triggered the event. Must be the
managed control.
@param Index [in] Index if item being drawn.
@param Rect [in] Rectangle in check list box's canvas where item is to
be drawn.
@param State [in] State of list item.
}
public
constructor Create(const CLB: TCheckListBox);
{Class constructor. Sets up object to manage a specified check list box.
@param CLB [in] Check list box to be managed.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
procedure Clear;
{Clears the check box list.
}
procedure Save;
{Takes an internal snapshot of the currently checked snippets in check
list box.
}
procedure Restore;
{Checks all snippets from any current snapshot in check list box. All
other entries in check list box a cleared. Any snippets in snapshot that
are not in the check list box are ignored.
}
procedure AddSnippet(const Snippet: TSnippet);
{Adds a snippet to the check box list, unchecked.
@param Snippet [in] Snippet to be added to list.
}
procedure CheckSnippets(const SnipList: TSnippetList);
{Checks entries in list corresponding to each snippet in a list.
@param SnipList [in] List of snippets to check. Snippets not in check
list box are ignored.
}
procedure GetCheckedSnippets(const SnipList: TSnippetList); overload;
{Gets all checked snippets in check list box.
@param SnipList [in] List that receives checked snippets objects.
}
procedure GetCheckedSnippets(const SnipList: ISnippetIDList); overload;
{Gets all checked snippets in check list box.
@param SnipList [in] List that receives ids of checked snippets.
}
end;
implementation
uses
// Delphi
Graphics, StdCtrls,
// Project
UColours, UGraphicUtils;
{ TSnippetsChkListMgr }
procedure TSnippetsChkListMgr.AddSnippet(const Snippet: TSnippet);
{Adds a snippet to the check box list, unchecked.
@param Snippet [in] Snippet to be added to list.
}
begin
fCLB.Items.AddObject(Snippet.Name, Snippet);
end;
procedure TSnippetsChkListMgr.CheckSnippet(const Snippet: TSnippet);
{Checks entry corresponding to a snippet in check list box. Snippets not in
check list box are ignored.
@param Snippet [in] Snippet to be checked.
}
var
Idx: Integer; // index of snippet in check list box
begin
Idx := fCLB.Items.IndexOfObject(Snippet);
if Idx >= 0 then
fCLB.Checked[Idx] := True;
end;
procedure TSnippetsChkListMgr.CheckSnippets(const SnipList: TSnippetList);
{Checks entries in list corresponding to each snippet in a list.
@param SnipList [in] List of snippets to check. Snippets not in check list
box are ignored.
}
var
Snippet: TSnippet; // each snippet in list
begin
for Snippet in SnipList do
CheckSnippet(Snippet);
end;
procedure TSnippetsChkListMgr.Clear;
{Clears the check box list.
}
begin
fCLB.Clear;
end;
procedure TSnippetsChkListMgr.ClearChecks;
{Clears all checks from items in check list box.
}
var
Idx: Integer; // loops through all items in check list box
begin
for Idx := 0 to Pred(fCLB.Count) do
fCLB.Checked[Idx] := False;
end;
constructor TSnippetsChkListMgr.Create(const CLB: TCheckListBox);
{Class constructor. Sets up object to manage a specified check list box.
@param CLB [in] Check list box to be managed.
}
begin
Assert(Assigned(CLB), ClassName + '.Create: CLB is nil');
inherited Create;
fCLB := CLB;
// make list box owner drawn and set correct height to allow for check boxes
fCLB.OnDrawItem := DrawItem;
fCLB.Style := lbOwnerDrawFixed;
fCLB.ItemHeight := StringExtent('Xy', fCLB.Font).cy;
fSaveList := TSnippetList.Create;
end;
destructor TSnippetsChkListMgr.Destroy;
{Class destructor. Tears down object.
}
begin
fCLB.OnDrawItem := nil;
fSaveList.Free;
inherited;
end;
procedure TSnippetsChkListMgr.DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
{OnDrawItem event handler for associated check list box. Draws user defined
snippet names in a different colour to main database snippets.
@param Control [in] Check list box that triggered the event. Must be the
managed control.
@param Index [in] Index if item being drawn.
@param Rect [in] Rectangle in check list box's canvas where item is to be
drawn.
@param State [in] State of list item.
}
var
Canvas: TCanvas; // check list box's canvas
begin
inherited;
Assert(fCLB = Control, ClassName + '.DrawItem: Control <> fCLB');
Canvas := fCLB.Canvas;
if not (odSelected in State)
and (fCLB.Items.Objects[Index] as TSnippet).UserDefined then
Canvas.Font.Color := clUserSnippet;
Canvas.TextRect(
Rect,
Rect.Left + 2,
(Rect.Top + Rect.Bottom - Canvas.TextHeight(fCLB.Items[Index])) div 2,
fCLB.Items[Index]
);
end;
procedure TSnippetsChkListMgr.GetCheckedSnippets(
const SnipList: ISnippetIDList);
{Gets all checked snippets in check list box.
@param SnipList [in] List that receives ids of checked snippets.
}
var
Idx: Integer; // loops through all items in list box
begin
SnipList.Clear;
for Idx := 0 to Pred(fCLB.Count) do
if fCLB.Checked[Idx] then
SnipList.Add((fCLB.Items.Objects[Idx] as TSnippet).ID);
end;
procedure TSnippetsChkListMgr.GetCheckedSnippets(
const SnipList: TSnippetList);
{Gets all checked snippets in check list box.
@param SnipList [in] List that receives checked snippets objects.
}
var
Idx: Integer; // loops through all items in list box
begin
SnipList.Clear;
for Idx := 0 to Pred(fCLB.Count) do
if fCLB.Checked[Idx] then
SnipList.Add(fCLB.Items.Objects[Idx] as TSnippet);
end;
procedure TSnippetsChkListMgr.Restore;
{Checks all snippets from any current snapshot in check list box. All other
entries in check list box a cleared. Any snippets in snapshot that are not
in the check list box are ignored.
}
begin
ClearChecks;
CheckSnippets(fSaveList);
end;
procedure TSnippetsChkListMgr.Save;
{Takes an internal snapshot of the currently checked snippets in check list
box.
}
begin
GetCheckedSnippets(fSaveList);
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.