compon.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. {
  2. $Id$
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {****************************************************************************}
  12. {* TComponent *}
  13. {****************************************************************************}
  14. Type
  15. Longrec = Record
  16. Hi,lo : word;
  17. end;
  18. Function TComponent.GetComponent(AIndex: Integer): TComponent;
  19. begin
  20. If not assigned(FComponents) then
  21. Result:=Nil
  22. else
  23. Result:=TComponent(FComponents.Items[Aindex]);
  24. end;
  25. Function TComponent.GetComponentCount: Integer;
  26. begin
  27. If not assigned(FComponents) then
  28. result:=0
  29. else
  30. Result:=FComponents.Count;
  31. end;
  32. Function TComponent.GetComponentIndex: Integer;
  33. begin
  34. If Assigned(FOwner) and Assigned(FOwner.FComponents) then
  35. Result:=FOWner.FComponents.IndexOf(Self)
  36. else
  37. Result:=-1;
  38. end;
  39. Procedure TComponent.Insert(AComponent: TComponent);
  40. begin
  41. If not assigned(FComponents) then
  42. FComponents:=TList.Create;
  43. FComponents.Add(AComponent);
  44. AComponent.FOwner:=Self;
  45. end;
  46. Procedure TComponent.ReadLeft(Reader: TReader);
  47. begin
  48. // LongRec(FDesignInfo).Lo:=Reader.ReadInteger;
  49. end;
  50. Procedure TComponent.ReadTop(Reader: TReader);
  51. begin
  52. // LongRec(FDesignInfo).Hi:=Reader.ReadInteger;
  53. end;
  54. Procedure TComponent.Remove(AComponent: TComponent);
  55. begin
  56. AComponent.FOwner:=Nil;
  57. If assigned(FCOmponents) then
  58. begin
  59. FComponents.Remove(AComponent);
  60. IF FComponents.Count=0 then
  61. begin
  62. FComponents.Free;
  63. FComponents:=Nil;
  64. end;
  65. end;
  66. end;
  67. Procedure TComponent.RemoveNotification(AComponent: TComponent);
  68. begin
  69. if FFreeNotifies<>nil then
  70. begin
  71. FFreeNotifies.Remove(AComponent);
  72. if FFreeNotifies.Count=0 then
  73. begin
  74. FFreeNotifies.Free;
  75. FFreeNotifies:=nil;
  76. Exclude(FComponentState,csFreeNotification);
  77. end;
  78. end;
  79. end;
  80. Procedure TComponent.SetComponentIndex(Value: Integer);
  81. Var Temp,Count : longint;
  82. begin
  83. If Not assigned(Fowner) then exit;
  84. Temp:=getcomponentindex;
  85. If temp<0 then exit;
  86. If value<0 then value:=0;
  87. Count:=Fowner.FComponents.Count;
  88. If Value>=Count then value:=count-1;
  89. If Value<>Temp then
  90. begin
  91. FOWner.FComponents.Delete(Temp);
  92. FOwner.FComponents.Insert(Value,Self);
  93. end;
  94. end;
  95. Procedure TComponent.SetReference(Enable: Boolean);
  96. var
  97. Field: ^TComponent;
  98. begin
  99. if Assigned(Owner) then
  100. begin
  101. Field := Owner.FieldAddress(Name);
  102. if Assigned(Field) then
  103. if Enable then
  104. Field^ := Self
  105. else
  106. Field^ := nil;
  107. end;
  108. end;
  109. Procedure TComponent.WriteLeft(Writer: TWriter);
  110. begin
  111. Writer.WriteInteger(LongRec(FDesignInfo).Lo);
  112. end;
  113. Procedure TComponent.WriteTop(Writer: TWriter);
  114. begin
  115. Writer.WriteInteger(LongRec(FDesignInfo).Hi);
  116. end;
  117. Procedure TComponent.ChangeName(const NewName: TComponentName);
  118. begin
  119. FName:=NewName;
  120. end;
  121. Procedure TComponent.DefineProperties(Filer: TFiler);
  122. Var Ancestor : TComponent;
  123. Temp : longint;
  124. begin
  125. Temp:=0;
  126. Ancestor:=TComponent(Filer.Ancestor);
  127. If Assigned(Ancestor) then Temp:=Ancestor.FDesignInfo;
  128. {
  129. Filer.Defineproperty('left',readleft,writeleft,
  130. (longrec(FDesignInfo).Lo<>Longrec(temp).Lo));
  131. Filer.Defineproperty('top',readtop,writetop,
  132. (longrec(FDesignInfo).Hi<>Longrec(temp).Hi));
  133. }
  134. end;
  135. Procedure TComponent.GetChildren(Proc: TGetChildProc; Root: TComponent);
  136. begin
  137. // Does nothing.
  138. end;
  139. Function TComponent.GetChildOwner: TComponent;
  140. begin
  141. Result:=Nil;
  142. end;
  143. Function TComponent.GetChildParent: TComponent;
  144. begin
  145. Result:=Self;
  146. end;
  147. Function TComponent.GetNamePath: string;
  148. begin
  149. Result:=FName;
  150. end;
  151. Function TComponent.GetOwner: TPersistent;
  152. begin
  153. Result:=FOwner;
  154. end;
  155. Procedure TComponent.Loaded;
  156. begin
  157. Exclude(FComponentState,csLoading);
  158. end;
  159. Procedure TComponent.Notification(AComponent: TComponent;
  160. Operation: TOperation);
  161. Var Runner : Longint;
  162. begin
  163. If (Operation=opRemove) and Assigned(FFreeNotifies) then
  164. begin
  165. FFreeNotifies.Remove(AComponent);
  166. If FFreeNotifies.Count=0 then
  167. begin
  168. FFreeNotifies.Free;
  169. FFreenotifies:=Nil;
  170. end;
  171. end;
  172. If assigned(FComponents) then
  173. For Runner:=0 To FComponents.Count-1 do
  174. TComponent(FComponents.Items[Runner]).Notification(AComponent,Operation);
  175. end;
  176. Procedure TComponent.ReadState(Reader: TReader);
  177. begin
  178. Reader.ReadData(Self);
  179. end;
  180. Procedure TComponent.SetAncestor(Value: Boolean);
  181. Var Runner : Longint;
  182. begin
  183. If Value then
  184. Include(FComponentState,csAncestor)
  185. else
  186. Include(FCOmponentState,csAncestor);
  187. if Assigned(FComponents) then
  188. For Runner:=0 To FComponents.Count do
  189. TComponent(FComponents.Items[Runner]).SetAncestor(Value);
  190. end;
  191. Procedure TComponent.SetDesigning(Value: Boolean);
  192. Var Runner : Longint;
  193. begin
  194. If Value then
  195. Include(FComponentSTate,csDesigning)
  196. else
  197. Exclude(FComponentSTate,csDesigning);
  198. if Assigned(FComponents) then
  199. For Runner:=0 To FComponents.Count - 1 do
  200. TComponent(FComponents.items[Runner]).SetDesigning(Value);
  201. end;
  202. Procedure TComponent.SetName(const NewName: TComponentName);
  203. begin
  204. If FName=NewName then exit;
  205. If not IsValidIdent(NewName) then
  206. Raise EComponentError.CreateFmt(SInvalidName,[NewName]);
  207. If Assigned(FOwner) Then
  208. FOwner.ValidateRename(Self,FName,NewName)
  209. else
  210. ValidateRename(Nil,FName,NewName);
  211. SetReference(False);
  212. ChangeName(NewName);
  213. Setreference(True);
  214. end;
  215. Procedure TComponent.SetChildOrder(Child: TComponent; Order: Integer);
  216. begin
  217. // does nothing
  218. end;
  219. Procedure TComponent.SetParentComponent(Value: TComponent);
  220. begin
  221. // Does nothing
  222. end;
  223. Procedure TComponent.Updating;
  224. begin
  225. Include (FComponentState,csUpdating);
  226. end;
  227. Procedure TComponent.Updated;
  228. begin
  229. Exclude(FComponentState,csUpdating);
  230. end;
  231. class Procedure TComponent.UpdateRegistry(Register: Boolean; const ClassID, ProgID: string);
  232. begin
  233. // For compatibility only.
  234. end;
  235. Procedure TComponent.ValidateRename(AComponent: TComponent;
  236. const CurName, NewName: string);
  237. begin
  238. //!! This contradicts the Delphi manual.
  239. If (AComponent<>Nil) and (CurName<>NewName) and
  240. (FindComponent(NewName)<>Nil) then
  241. raise EComponentError.Createfmt(SDuplicateName,[newname]);
  242. If (csDesigning in FComponentState) and (FOwner<>Nil) then
  243. FOwner.ValidateRename(AComponent,Curname,Newname);
  244. end;
  245. Procedure TComponent.ValidateContainer(AComponent: TComponent);
  246. begin
  247. end;
  248. Procedure TComponent.ValidateInsert(AComponent: TComponent);
  249. begin
  250. // Does nothing.
  251. end;
  252. Procedure TComponent.WriteState(Writer: TWriter);
  253. begin
  254. Writer.WriteComponentData(Self);
  255. end;
  256. Constructor TComponent.Create(AOwner: TComponent);
  257. begin
  258. FComponentStyle:=[csInheritable];
  259. If Assigned(AOwner) then AOwner.InsertComponent(Self);
  260. end;
  261. Destructor TComponent.Destroy;
  262. Var Runner : Longint;
  263. begin
  264. If Assigned(FFreeNotifies) then
  265. begin
  266. For Runner:=0 To FFreeNotifies.Count-1 do
  267. TComponent(FFreeNotifies.Items[Runner]).Notification (self,opRemove);
  268. FFreeNotifies.Free;
  269. FFreeNotifies:=Nil;
  270. end;
  271. Destroying;
  272. DestroyComponents;
  273. If FOwner<>Nil Then FOwner.RemoveComponent(Self);
  274. inherited destroy;
  275. end;
  276. Procedure TComponent.DestroyComponents;
  277. Var acomponent: TComponent;
  278. begin
  279. While assigned(FComponents) do
  280. begin
  281. aComponent:=TComponent(FComponents.Last);
  282. Remove(aComponent);
  283. Acomponent.Destroy;
  284. end;
  285. end;
  286. Procedure TComponent.Destroying;
  287. Var Runner : longint;
  288. begin
  289. If csDestroying in FComponentstate Then Exit;
  290. include (FComponentState,csDestroying);
  291. If Assigned(FComponents) then
  292. for Runner:=0 to FComponents.Count-1 do
  293. TComponent(FComponents.Items[Runner]).Destroying;
  294. end;
  295. Function TComponent.FindComponent(const AName: string): TComponent;
  296. Var I : longint;
  297. begin
  298. Result:=Nil;
  299. If (AName='') or Not assigned(FComponents) then exit;
  300. For i:=0 to FComponents.Count-1 do
  301. if TComponent(FComponents[I]).Name=AName then
  302. begin
  303. Result:=TComponent(FComponents.Items[I]);
  304. exit;
  305. end;
  306. end;
  307. Procedure TComponent.FreeNotification(AComponent: TComponent);
  308. begin
  309. If (Owner<>Nil) and (AComponent=Owner) then exit;
  310. If not (Assigned(FFreeNotifies)) then
  311. FFreeNotifies:=TList.Create;
  312. If FFreeNotifies.IndexOf(AComponent)=-1 then
  313. begin
  314. FFreeNotifies.Add(AComponent);
  315. AComponent.FreeNotification (self);
  316. end;
  317. end;
  318. procedure TComponent.RemoveFreeNotification(AComponent: TComponent);
  319. begin
  320. RemoveNotification(AComponent);
  321. AComponent.RemoveNotification (self);
  322. end;
  323. Procedure TComponent.FreeOnRelease;
  324. begin
  325. // Delphi compatibility only at the moment.
  326. end;
  327. Function TComponent.GetParentComponent: TComponent;
  328. begin
  329. Result:=Nil;
  330. end;
  331. Function TComponent.HasParent: Boolean;
  332. begin
  333. Result:=False;
  334. end;
  335. Procedure TComponent.InsertComponent(AComponent: TComponent);
  336. begin
  337. AComponent.ValidateContainer(Self);
  338. ValidateRename(AComponent,'',AComponent.FName);
  339. Insert(AComponent);
  340. AComponent.SetReference(True);
  341. If csDesigning in FComponentState then
  342. AComponent.SetDesigning(true);
  343. Notification(AComponent,opInsert);
  344. end;
  345. Procedure TComponent.RemoveComponent(AComponent: TComponent);
  346. begin
  347. Notification(AComponent,opRemove);
  348. AComponent.SetReference(False);
  349. Remove(AComponent);
  350. Acomponent.Setdesigning(False);
  351. ValidateRename(AComponent,AComponent.FName,'');
  352. end;
  353. Function TComponent.SafeCallException(ExceptObject: TObject;
  354. ExceptAddr: Pointer): Integer;
  355. begin
  356. SafeCallException:=0;
  357. end;
  358. {
  359. $Log$
  360. Revision 1.4 2001-12-03 21:39:58 peter
  361. * seek(int64) overload only for 1.1 compiler
  362. Revision 1.3 2001/01/08 18:36:01 sg
  363. * Applied bugfix for bug #1330 (merged)
  364. Revision 1.2 2000/07/13 11:32:59 michael
  365. + removed logs
  366. }