/[projects]/dao/DelphiScanner/Components/tpsystools_4.04/examples/CBuilder/ExCollU.cpp
ViewVC logotype

Contents of /dao/DelphiScanner/Components/tpsystools_4.04/examples/CBuilder/ExCollU.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2671 - (show annotations) (download)
Tue Aug 25 18:15:15 2015 UTC (8 years, 8 months ago) by torben
File size: 11521 byte(s)
Added tpsystools component
1 // ***** BEGIN LICENSE BLOCK *****
2 // * Version: MPL 1.1
3 // *
4 // * The contents of this file are subject to the Mozilla Public License Version
5 // * 1.1 (the "License"); you may not use this file except in compliance with
6 // * the License. You may obtain a copy of the License at
7 // * http://www.mozilla.org/MPL/
8 // *
9 // * Software distributed under the License is distributed on an "AS IS" basis,
10 // * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 // * for the specific language governing rights and limitations under the
12 // * License.
13 // *
14 // * The Original Code is TurboPower SysTools
15 // *
16 // * The Initial Developer of the Original Code is
17 // * TurboPower Software
18 // *
19 // * Portions created by the Initial Developer are Copyright (C) 1996-2002
20 // * the Initial Developer. All Rights Reserved.
21 // *
22 // * Contributor(s):
23 // *
24 // * ***** END LICENSE BLOCK *****
25 //---------------------------------------------------------------------------
26 #include <vcl\vcl.h>
27 #pragma hdrstop
28 #include <stdlib.h>
29 #include "ExCollU.h"
30 //---------------------------------------------------------------------------
31 #pragma package(smart_init)
32 #pragma link "StColl"
33
34 #pragma resource "*.dfm"
35 TForm1 *Form1;
36 //---------------------------------------------------------------------------
37 __fastcall TForm1::TForm1(TComponent* Owner)
38 : TForm(Owner)
39 {
40 }
41 //---------------------------------------------------------------------------
42 void __fastcall MyDelNodeData(void * Data)
43 {
44 delete (ARecord*)Data;
45 }
46 //---------------------------------------------------------------------------
47 bool __fastcall MatchCollString(TStContainer* Container, void * Data, void * OtherData)
48 {
49 ARecord R1 = *(ARecord*) Data;
50 ARecord R2 = *(ARecord*) OtherData;
51
52 return
53 (R1.First != R2.First) || (R1.Last != R2.Last);
54 }
55 //---------------------------------------------------------------------------
56 bool __fastcall CollWalker(TStContainer* Container, void * Data, void * OtherData)
57 //this function makes no comparison and always returns true
58 //so it will visit all nodes in the collection
59 {
60 ARecord R1 = *(ARecord*) Data;
61
62 Form1->LB1->Items->Add(R1.First + ' ' + R1.Last + ', ' + IntToStr(R1.Age));
63 return true;
64 }
65 //---------------------------------------------------------------------------
66 void __fastcall MyStoreData(Classes::TWriter* Writer, void * Data)
67 {
68 ARecord R1 = *(ARecord*) Data;
69 Writer->WriteString(R1.First);
70 Writer->WriteString(R1.Last);
71 Writer->WriteInteger(R1.Age);
72 }
73 //---------------------------------------------------------------------------
74 void * __fastcall MyLoadData(Classes::TReader* Reader)
75 {
76 ARecord* Res = new ARecord;
77 Res->First = Reader->ReadString();
78 Res->Last = Reader->ReadString();
79 Res->Age = Reader->ReadInteger();
80 return Res;
81 }
82 //---------------------------------------------------------------------------
83 void TForm1::UpdateButtons(bool COK)
84 {
85 ClearBtn->Enabled = COK;
86 PackBtn->Enabled = COK;
87 AtBtn->Enabled = COK;
88 AtInsBtn->Enabled = COK;
89 AtPutBtn->Enabled = COK;
90 DelBtn->Enabled = COK;
91 AtDelBtn->Enabled = COK;
92 InsBtn->Enabled = COK;
93 EffBtn->Enabled = COK;
94 SaveBtn->Enabled = COK;
95 }
96 //---------------------------------------------------------------------------
97 void TForm1::SetBusy(bool B)
98 {
99 if (B)
100 Screen->Cursor = crHourGlass;
101 else
102 Screen->Cursor = crDefault;
103 }
104 //---------------------------------------------------------------------------
105 Boolean TForm1::CheckControls(ARecord &AR)
106 {
107 if ((Edit3->Text == "") || (Edit4->Text == "") || (Edit5->Text == ""))
108 return false;
109
110 AR.First = Edit3->Text;
111 AR.Last = Edit4->Text;
112
113 AR.Age = StrToInt(Edit5->Text);
114 return true;
115 }
116 //---------------------------------------------------------------------------
117 void TForm1::FillControls(ARecord AR)
118 {
119 Edit3->Text = AR.First;
120 Edit4->Text = AR.Last;
121 Edit5->Text = IntToStr(AR.Age);
122 }
123 //---------------------------------------------------------------------------
124 void TForm1::FillListBox(void)
125 {
126 LB1->Clear();
127 LB1->Perform(WM_SETREDRAW,0,0);
128
129 SetBusy(true);
130
131 MyCollection->Iterate(CollWalker, true, 0);
132
133 LB1->Perform(WM_SETREDRAW,1,0);
134 LB1->Update();
135 LB1->ItemIndex = 0;
136 Edit2->Text = "0";
137
138 SetBusy(false);
139 }
140 //---------------------------------------------------------------------------
141 void __fastcall TForm1::FormCreate(TObject *Sender)
142 {
143 RegisterClassAlias(__classid(TStCollection), "TStCollection");
144 UpdateButtons(false);
145
146 FirstA[0] = "Fred";
147 FirstA[1] = "Robert";
148 FirstA[2] = "Barney";
149 FirstA[3] = "Horatio";
150 FirstA[4] = "Kent";
151 FirstA[5] = "Arthur";
152 FirstA[6] = "Lee";
153 FirstA[7] = "John Q. ";
154
155 LastA[0] = "Flintstone";
156 LastA[1] = "Java";
157 LastA[2] = "Rubble";
158 LastA[3] = "Hornblower";
159 LastA[4] = "C++Builder";
160 LastA[5] = "Miller";
161 LastA[6] = "Delphi";
162 LastA[7] = "Public";
163 }
164 //---------------------------------------------------------------------------
165 void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
166 {
167 delete MyCollection;
168 }
169 //---------------------------------------------------------------------------
170 void __fastcall TForm1::CreateBtnClick(TObject *Sender)
171 {
172 int I;
173 ARecord* AR;
174
175 if (MyCollection)
176 delete MyCollection;
177
178 UpdateButtons(false);
179 MyCollection = new TStCollection(100);
180
181 MyCollection->DisposeData = MyDelNodeData;
182 MyCollection->LoadData = MyLoadData;
183 MyCollection->StoreData = MyStoreData;
184
185 randomize;
186 LB1->Clear();
187 LB1->Perform(WM_SETREDRAW,0,0);
188 SetBusy(true);
189
190 for (I = 0; I < MaxElem; I++) {
191 AR = new ARecord;
192 AR->First = FirstA[random(8)];
193 AR->Last = LastA[random(8)];
194 AR->Age = random(100);
195
196 MyCollection->Insert(AR);
197 LB1->Items->Add(AR->First + " " + AR->Last + ", " + IntToStr(AR->Age));
198 }
199 LB1->Perform(WM_SETREDRAW,1,0);
200 LB1->Update();
201
202 MyCollection->Pack();
203 Edit1->Text = IntToStr(MyCollection->Efficiency);
204 UpdateButtons(true);
205 SetBusy(false);
206 }
207 //---------------------------------------------------------------------------
208 void __fastcall TForm1::ClearBtnClick(TObject *Sender)
209 {
210 MyCollection->Clear();
211 LB1->Clear();
212 Edit1->Text = IntToStr(MyCollection->Efficiency);
213 }
214 //---------------------------------------------------------------------------
215 void __fastcall TForm1::PackBtnClick(TObject *Sender)
216 {
217 MyCollection->Pack();
218 Edit1->Text = IntToStr(MyCollection->Efficiency);
219 }
220 //---------------------------------------------------------------------------
221 void __fastcall TForm1::AtBtnClick(TObject *Sender)
222 {
223 void * Data;
224 long E;
225
226 if (Edit2->Text == "")
227 Edit2->Text = "0";
228 E = StrToInt(Edit2->Text);
229 if ((E > MyCollection->Count-1) || (E < 0)) {
230 ShowMessage("Element value out of range (0.." + IntToStr(MyCollection->Count) + ")");
231 Edit2->Text = "0";
232 return;
233 }
234
235 Data = MyCollection->At(E);
236 ARecord R1 = *(ARecord*) Data;
237 FillControls(R1);;
238 }
239 //---------------------------------------------------------------------------
240 void __fastcall TForm1::AtInsBtnClick(TObject *Sender)
241 {
242 long E;
243 ARecord* PAR;
244
245 PAR = new ARecord;
246 if (!CheckControls(*PAR)) {
247 ShowMessage("One or more data controls invalid");
248 delete PAR;
249 return;
250 }
251
252 if (Edit2->Text == "")
253 Edit2->Text = "0";
254 E = StrToInt(Edit2->Text);
255 if ((E > MyCollection->Count-1) || (E < 0)) {
256 ShowMessage("Element value out of range (0.." + IntToStr(MyCollection->Count) + ")");
257 Edit2->Text = "0";
258 return;
259 }
260
261 MyCollection->AtInsert(E, PAR);
262 FillListBox();
263 }
264 //---------------------------------------------------------------------------
265 void __fastcall TForm1::AtPutBtnClick(TObject *Sender)
266 {
267 long E;
268 void * Data;
269 ARecord AR;
270
271 if (!CheckControls(AR)) {
272 ShowMessage("One or more data controls invalid");
273 return;
274 }
275
276 if (Edit2->Text == "")
277 Edit2->Text = "0";
278 E = StrToInt(Edit2->Text);
279 if ((E > MyCollection->Count-1) || (E < 0)) {
280 ShowMessage("Element value out of range (0.." + IntToStr(MyCollection->Count) + ")");
281 Edit2->Text = "0";
282 return;
283 }
284
285 Data = MyCollection->At(E);
286 if (Data) {
287 *(ARecord*) Data = AR;
288 MyCollection->AtPut(E, Data);
289 FillListBox();
290 }
291 }
292 //---------------------------------------------------------------------------
293 void __fastcall TForm1::DelBtnClick(TObject *Sender)
294 {
295 ARecord AR;
296 void * PN;
297
298 if (!CheckControls(AR)) {
299 ShowMessage("One or more data entry fields invalid");
300 return;
301 }
302
303 PN = MyCollection->Iterate(MatchCollString, true, &AR);
304 if (!PN) {
305 MyCollection->Delete(PN);
306 FillListBox();
307 } else
308 ShowMessage("Data not found");
309 }
310 //---------------------------------------------------------------------------
311 void __fastcall TForm1::AtDelBtnClick(TObject *Sender)
312 {
313 long E;
314
315 if (Edit2->Text == "")
316 E = 0;
317 else
318 E = StrToInt(Edit2->Text);
319 if ((E > MyCollection->Count-1) || (E < 0)) {
320 ShowMessage("Element value out of range (0.." + IntToStr(MyCollection->Count) + ")");
321 Edit2->Text = "0";
322 return;
323 }
324 MyCollection->AtDelete(E);
325 FillListBox();
326 }
327 //---------------------------------------------------------------------------
328 void __fastcall TForm1::InsBtnClick(TObject *Sender)
329 {
330 int E;
331 ARecord* AR;
332
333 if (Edit2->Text == "")
334 E = 0;
335 else
336 E = StrToInt(Edit2->Text);
337
338 if ((E > MyCollection->Count-1) || (E < 0)) {
339 ShowMessage("Element value out of range (0.." + IntToStr(MyCollection->Count) + ")");
340 Edit2->Text = "0";
341 return;
342 }
343
344 AR = new ARecord;
345 if (!CheckControls(*AR)) {
346 ShowMessage("One or more data entry fields invalid");
347 delete AR;
348 return;
349 }
350
351 MyCollection->Insert(AR);
352 FillListBox();
353 }
354 //---------------------------------------------------------------------------
355 void __fastcall TForm1::EffBtnClick(TObject *Sender)
356 {
357 Edit1->Text = IntToStr(MyCollection->Efficiency);
358 }
359 //---------------------------------------------------------------------------
360 void __fastcall TForm1::LoadBtnClick(TObject *Sender)
361 {
362 if (OD1->Execute()) {
363 if (!MyCollection) {
364 UpdateButtons(false);
365 MyCollection = new TStCollection(100);
366 MyCollection->DisposeData = MyDelNodeData;
367 MyCollection->LoadData = MyLoadData;
368 MyCollection->StoreData = MyStoreData;
369 }
370
371 LB1->Clear();
372 MyCollection->Clear();
373
374 SetBusy(true);
375 MyCollection->LoadFromFile(OD1->FileName);
376 MyCollection->Pack();
377 SetBusy(false);
378
379 FillListBox();
380 UpdateButtons(true);
381 }
382 }
383 //---------------------------------------------------------------------------
384 void __fastcall TForm1::SaveBtnClick(TObject *Sender)
385 {
386 if (SD1->Execute()) {
387 SetBusy(true);
388 MyCollection->StoreToFile(SD1->FileName);
389 SetBusy(false);
390 }
391 }
392 //---------------------------------------------------------------------------
393 void __fastcall TForm1::LB1Click(TObject *Sender)
394 {
395 Edit2->Text = IntToStr(LB1->ItemIndex);
396 }
397 //---------------------------------------------------------------------------
398 void __fastcall TForm1::LB1DblClick(TObject *Sender)
399 {
400 MyCollection->AtDelete(LB1->ItemIndex);
401 FillListBox();
402 Edit2->Text = '0';
403 }
404 //---------------------------------------------------------------------------

  ViewVC Help
Powered by ViewVC 1.1.20