collect.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {****************************************************************************}
  11. {* TCollectionItem *}
  12. {****************************************************************************}
  13. function TCollectionItem.GetIndex: Integer;
  14. begin
  15. if FCollection<>nil then
  16. Result:=FCollection.FItems.IndexOf(Pointer(Self))
  17. else
  18. Result:=-1;
  19. end;
  20. procedure TCollectionItem.SetCollection(Value: TCollection);
  21. begin
  22. IF Value<>FCollection then
  23. begin
  24. If FCollection<>Nil then FCollection.RemoveItem(Self);
  25. if Value<>Nil then Value.InsertItem(Self);
  26. FCollection:=Value;
  27. end;
  28. end;
  29. procedure TCollectionItem.Changed(AllItems: Boolean);
  30. begin
  31. If (FCollection<>Nil) and (FCollection.UpdateCount=0) then
  32. begin
  33. If AllItems then
  34. FCollection.Update(Nil)
  35. else
  36. FCollection.Update(Self);
  37. end;
  38. end;
  39. function TCollectionItem.GetNamePath: string;
  40. begin
  41. If FCollection<>Nil then
  42. Result:=FCollection.GetNamePath+'['+IntToStr(Index)+']'
  43. else
  44. Result:=ClassName;
  45. end;
  46. function TCollectionItem.GetOwner: TPersistent;
  47. begin
  48. Result:=FCollection;
  49. end;
  50. function TCollectionItem.GetDisplayName: string;
  51. begin
  52. Result:=ClassName;
  53. end;
  54. procedure TCollectionItem.SetIndex(Value: Integer);
  55. Var Temp : Longint;
  56. begin
  57. Temp:=GetIndex;
  58. If (Temp>-1) and (Temp<>Value) then
  59. begin
  60. FCollection.FItems.Move(Temp,Value);
  61. Changed(True);
  62. end;
  63. end;
  64. procedure TCollectionItem.SetDisplayName(const Value: string);
  65. begin
  66. Changed(False);
  67. end;
  68. constructor TCollectionItem.Create(ACollection: TCollection);
  69. begin
  70. Inherited Create;
  71. SetCollection(ACollection);
  72. end;
  73. destructor TCollectionItem.Destroy;
  74. begin
  75. SetCollection(Nil);
  76. Inherited Destroy;
  77. end;
  78. {****************************************************************************}
  79. {* TCollectionEnumerator *}
  80. {****************************************************************************}
  81. constructor TCollectionEnumerator.Create(ACollection: TCollection);
  82. begin
  83. inherited Create;
  84. FCollection := ACollection;
  85. FPosition := -1;
  86. end;
  87. function TCollectionEnumerator.GetCurrent: TCollectionItem;
  88. begin
  89. Result := FCollection.Items[FPosition];
  90. end;
  91. function TCollectionEnumerator.MoveNext: Boolean;
  92. begin
  93. Inc(FPosition);
  94. Result := FPosition < FCollection.Count;
  95. end;
  96. {****************************************************************************}
  97. {* TCollection *}
  98. {****************************************************************************}
  99. function TCollection.Owner: TPersistent;
  100. begin
  101. result:=getowner;
  102. end;
  103. function TCollection.GetCount: Integer;
  104. begin
  105. If Assigned(FItems) Then
  106. Result:=FItems.Count
  107. else
  108. Result:=0;
  109. end;
  110. Procedure TCollection.SetPropName;
  111. Var
  112. TheOwner : TPersistent;
  113. PropList : PPropList;
  114. I, PropCount : Integer;
  115. begin
  116. FPropName:='';
  117. TheOwner:=GetOwner;
  118. if (TheOwner=Nil) Or (TheOwner.Classinfo=Nil) Then Exit;
  119. // get information from the owner RTTI
  120. PropCount:=GetPropList(TheOwner, PropList);
  121. Try
  122. For I:=0 To PropCount-1 Do
  123. If (PropList^[i]^.PropType^.Kind=tkClass) And
  124. (GetObjectProp(TheOwner, PropList^[i], ClassType)=Self) Then
  125. Begin
  126. FPropName:=PropList^[i]^.Name;
  127. Exit;
  128. End;
  129. Finally
  130. FreeMem(PropList);
  131. End;
  132. end;
  133. function TCollection.GetPropName: string;
  134. Var
  135. TheOwner : TPersistent;
  136. begin
  137. Result:=FPropNAme;
  138. TheOwner:=GetOwner;
  139. If (Result<>'') or (TheOwner=Nil) Or (TheOwner.Classinfo=Nil) then exit;
  140. SetPropName;
  141. Result:=FPropName;
  142. end;
  143. procedure TCollection.InsertItem(Item: TCollectionItem);
  144. begin
  145. If Not(Item Is FitemClass) then
  146. exit;
  147. FItems.add(Pointer(Item));
  148. Item.FID:=FNextID;
  149. inc(FNextID);
  150. SetItemName(Item);
  151. Notify(Item,cnAdded);
  152. Changed;
  153. end;
  154. procedure TCollection.RemoveItem(Item: TCollectionItem);
  155. begin
  156. Notify(Item,cnExtracting);
  157. FItems.Remove(Pointer(Item));
  158. Item.FCollection:=Nil;
  159. Changed;
  160. end;
  161. function TCollection.GetAttrCount: Integer;
  162. begin
  163. Result:=0;
  164. end;
  165. function TCollection.GetAttr(Index: Integer): string;
  166. begin
  167. Result:='';
  168. end;
  169. function TCollection.GetItemAttr(Index, ItemIndex: Integer): string;
  170. begin
  171. Result:=TCollectionItem(FItems.Items[ItemIndex]).DisplayName;
  172. end;
  173. function TCollection.GetEnumerator: TCollectionEnumerator;
  174. begin
  175. Result := TCollectionEnumerator.Create(Self);
  176. end;
  177. function TCollection.GetNamePath: string;
  178. var o : TObject;
  179. begin
  180. o:=getowner;
  181. if assigned(o) and (propname<>'') and (o IS TPersistent) then
  182. result:=TPersistent(o).getnamepath+'.'+propname
  183. else
  184. result:=classname;
  185. end;
  186. procedure TCollection.Changed;
  187. begin
  188. if FUpdateCount=0 then
  189. Update(Nil);
  190. end;
  191. function TCollection.GetItem(Index: Integer): TCollectionItem;
  192. begin
  193. Result:=TCollectionItem(FItems.Items[Index]);
  194. end;
  195. procedure TCollection.SetItem(Index: Integer; Value: TCollectionItem);
  196. begin
  197. TCollectionItem(FItems.items[Index]).Assign(Value);
  198. end;
  199. procedure TCollection.SetItemName(Item: TCollectionItem);
  200. begin
  201. end;
  202. procedure TCollection.Update(Item: TCollectionItem);
  203. begin
  204. end;
  205. constructor TCollection.Create(AItemClass: TCollectionItemClass);
  206. begin
  207. inherited create;
  208. FItemClass:=AItemClass;
  209. FItems:=TFpList.Create;
  210. end;
  211. destructor TCollection.Destroy;
  212. begin
  213. If Assigned(FItems) Then Clear;
  214. FItems.Free;
  215. Inherited Destroy;
  216. end;
  217. function TCollection.Add: TCollectionItem;
  218. begin
  219. Result:=FItemClass.Create(Self);
  220. end;
  221. procedure TCollection.Assign(Source: TPersistent);
  222. Var I : Longint;
  223. begin
  224. If Source is TCollection then
  225. begin
  226. Clear;
  227. For I:=0 To TCollection(Source).Count-1 do
  228. Add.Assign(TCollection(Source).Items[I]);
  229. exit;
  230. end
  231. else
  232. Inherited Assign(Source);
  233. end;
  234. procedure TCollection.BeginUpdate;
  235. begin
  236. inc(FUpdateCount);
  237. end;
  238. procedure TCollection.Clear;
  239. begin
  240. If Assigned(FItems) then
  241. While FItems.Count>0 do TCollectionItem(FItems.Last).Free;
  242. end;
  243. procedure TCollection.EndUpdate;
  244. begin
  245. dec(FUpdateCount);
  246. if FUpdateCount=0 then
  247. Changed;
  248. end;
  249. function TCollection.FindItemID(ID: Integer): TCollectionItem;
  250. Var
  251. I : Longint;
  252. begin
  253. For I:=0 to Fitems.Count-1 do
  254. begin
  255. Result:=TCollectionItem(FItems.items[I]);
  256. If Result.Id=Id then
  257. exit;
  258. end;
  259. Result:=Nil;
  260. end;
  261. procedure TCollection.Delete(Index: Integer);
  262. begin
  263. Notify(TCollectionItem(FItems[Index]),cnDeleting);
  264. TCollectionItem(FItems[Index]).Free;
  265. end;
  266. function TCollection.Insert(Index: Integer): TCollectionItem;
  267. begin
  268. Result:=Add;
  269. Result.Index:=Index;
  270. end;
  271. procedure TCollection.Notify(Item: TCollectionItem;Action: TCollectionNotification);
  272. begin
  273. end;
  274. procedure TCollection.Sort(Const Compare : TCollectionSortCompare);
  275. begin
  276. BeginUpdate;
  277. try
  278. FItems.Sort(TListSortCompare(Compare));
  279. Finally
  280. EndUpdate;
  281. end;
  282. end;
  283. procedure TCollection.Exchange(Const Index1, index2: integer);
  284. begin
  285. FItems.Exchange(Index1,Index2);
  286. end;
  287. {****************************************************************************}
  288. {* TOwnedCollection *}
  289. {****************************************************************************}
  290. Constructor TOwnedCollection.Create(AOwner: TPersistent; AItemClass: TCollectionItemClass);
  291. Begin
  292. FOwner := AOwner;
  293. inherited Create(AItemClass);
  294. end;
  295. Function TOwnedCollection.GetOwner: TPersistent;
  296. begin
  297. Result:=FOwner;
  298. end;