compon.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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.SetComponentIndex(Value: Integer);
  68. Var Temp,Count : longint;
  69. begin
  70. If Not assigned(Fowner) then exit;
  71. Temp:=getcomponentindex;
  72. If temp<0 then exit;
  73. If value<0 then value:=0;
  74. Count:=Fowner.FComponents.Count;
  75. If Value>=Count then value:=count-1;
  76. If Value<>Temp then
  77. begin
  78. FOWner.FComponents.Delete(Temp);
  79. FOwner.FComponents.Insert(Value,Self);
  80. end;
  81. end;
  82. Procedure TComponent.SetReference(Enable: Boolean);
  83. var
  84. Field: ^TComponent;
  85. begin
  86. if Assigned(Owner) then
  87. begin
  88. Field := Owner.FieldAddress(Name);
  89. if Assigned(Field) then
  90. if Enable then
  91. Field^ := Self
  92. else
  93. Field^ := nil;
  94. end;
  95. end;
  96. Procedure TComponent.WriteLeft(Writer: TWriter);
  97. begin
  98. Writer.WriteInteger(LongRec(FDesignInfo).Lo);
  99. end;
  100. Procedure TComponent.WriteTop(Writer: TWriter);
  101. begin
  102. Writer.WriteInteger(LongRec(FDesignInfo).Hi);
  103. end;
  104. Procedure TComponent.ChangeName(const NewName: TComponentName);
  105. begin
  106. FName:=NewName;
  107. end;
  108. Procedure TComponent.DefineProperties(Filer: TFiler);
  109. Var Ancestor : TComponent;
  110. Temp : longint;
  111. begin
  112. Temp:=0;
  113. Ancestor:=TComponent(Filer.Ancestor);
  114. If Assigned(Ancestor) then Temp:=Ancestor.FDesignInfo;
  115. {
  116. Filer.Defineproperty('left',readleft,writeleft,
  117. (longrec(FDesignInfo).Lo<>Longrec(temp).Lo));
  118. Filer.Defineproperty('top',readtop,writetop,
  119. (longrec(FDesignInfo).Hi<>Longrec(temp).Hi));
  120. }
  121. end;
  122. Procedure TComponent.GetChildren(Proc: TGetChildProc; Root: TComponent);
  123. begin
  124. // Does nothing.
  125. end;
  126. Function TComponent.GetChildOwner: TComponent;
  127. begin
  128. Result:=Nil;
  129. end;
  130. Function TComponent.GetChildParent: TComponent;
  131. begin
  132. Result:=Self;
  133. end;
  134. Function TComponent.GetNamePath: string;
  135. begin
  136. Result:=FName;
  137. end;
  138. Function TComponent.GetOwner: TPersistent;
  139. begin
  140. Result:=FOwner;
  141. end;
  142. Procedure TComponent.Loaded;
  143. begin
  144. Exclude(FComponentState,csLoading);
  145. end;
  146. Procedure TComponent.Notification(AComponent: TComponent;
  147. Operation: TOperation);
  148. Var Runner : Longint;
  149. begin
  150. If (Operation=opRemove) and Assigned(FFreeNotifies) then
  151. begin
  152. FFreeNotifies.Remove(AComponent);
  153. If FFreeNotifies.Count=0 then
  154. begin
  155. FFreeNotifies.Free;
  156. FFreenotifies:=Nil;
  157. end;
  158. end;
  159. If assigned(FComponents) then
  160. For Runner:=0 To FComponents.Count-1 do
  161. TComponent(FComponents.Items[Runner]).Notification(AComponent,Operation);
  162. end;
  163. Procedure TComponent.ReadState(Reader: TReader);
  164. begin
  165. Reader.ReadData(Self);
  166. end;
  167. Procedure TComponent.SetAncestor(Value: Boolean);
  168. Var Runner : Longint;
  169. begin
  170. If Value then
  171. Include(FComponentState,csAncestor)
  172. else
  173. Include(FCOmponentState,csAncestor);
  174. if Assigned(FComponents) then
  175. For Runner:=0 To FComponents.Count do
  176. TComponent(FComponents.Items[Runner]).SetAncestor(Value);
  177. end;
  178. Procedure TComponent.SetDesigning(Value: Boolean);
  179. Var Runner : Longint;
  180. begin
  181. If Value then
  182. Include(FComponentSTate,csDesigning)
  183. else
  184. Exclude(FComponentSTate,csDesigning);
  185. if Assigned(FComponents) then
  186. For Runner:=0 To FComponents.Count - 1 do
  187. TComponent(FComponents.items[Runner]).SetDesigning(Value);
  188. end;
  189. Procedure TComponent.SetName(const NewName: TComponentName);
  190. begin
  191. If FName=NewName then exit;
  192. If not IsValidIdent(NewName) then
  193. Raise EComponentError.CreateFmt(SInvalidName,[NewName]);
  194. If Assigned(FOwner) Then
  195. FOwner.ValidateRename(Self,FName,NewName)
  196. else
  197. ValidateRename(Nil,FName,NewName);
  198. SetReference(False);
  199. ChangeName(NewName);
  200. Setreference(True);
  201. end;
  202. Procedure TComponent.SetChildOrder(Child: TComponent; Order: Integer);
  203. begin
  204. // does nothing
  205. end;
  206. Procedure TComponent.SetParentComponent(Value: TComponent);
  207. begin
  208. // Does nothing
  209. end;
  210. Procedure TComponent.Updating;
  211. begin
  212. Include (FComponentState,csUpdating);
  213. end;
  214. Procedure TComponent.Updated;
  215. begin
  216. Exclude(FComponentState,csUpdating);
  217. end;
  218. class Procedure TComponent.UpdateRegistry(Register: Boolean; const ClassID, ProgID: string);
  219. begin
  220. // For compatibility only.
  221. end;
  222. Procedure TComponent.ValidateRename(AComponent: TComponent;
  223. const CurName, NewName: string);
  224. begin
  225. //!! This contradicts the Delphi manual.
  226. If (AComponent<>Nil) and (CurName<>NewName) and
  227. (FindComponent(NewName)<>Nil) then
  228. raise EComponentError.Createfmt(SDuplicateName,[newname]);
  229. If (csDesigning in FComponentState) and (FOwner<>Nil) then
  230. FOwner.ValidateRename(AComponent,Curname,Newname);
  231. end;
  232. Procedure TComponent.ValidateContainer(AComponent: TComponent);
  233. begin
  234. end;
  235. Procedure TComponent.ValidateInsert(AComponent: TComponent);
  236. begin
  237. // Does nothing.
  238. end;
  239. Procedure TComponent.WriteState(Writer: TWriter);
  240. begin
  241. Writer.WriteComponentData(Self);
  242. end;
  243. Constructor TComponent.Create(AOwner: TComponent);
  244. begin
  245. FComponentStyle:=[csInheritable];
  246. If Assigned(AOwner) then AOwner.InsertComponent(Self);
  247. end;
  248. Destructor TComponent.Destroy;
  249. Var Runner : Longint;
  250. begin
  251. If Assigned(FFreeNotifies) then
  252. begin
  253. For Runner:=0 To FFreeNotifies.Count-1 do
  254. TComponent(FFreeNotifies.Items[Runner]).Notification (self,opRemove);
  255. FFreeNotifies.Free;
  256. FFreeNotifies:=Nil;
  257. end;
  258. Destroying;
  259. DestroyComponents;
  260. If FOwner<>Nil Then FOwner.RemoveComponent(Self);
  261. inherited destroy;
  262. end;
  263. Procedure TComponent.DestroyComponents;
  264. Var acomponent: TComponent;
  265. begin
  266. While assigned(FComponents) do
  267. begin
  268. aComponent:=TComponent(FComponents.Last);
  269. Remove(aComponent);
  270. Acomponent.Destroy;
  271. end;
  272. end;
  273. Procedure TComponent.Destroying;
  274. Var Runner : longint;
  275. begin
  276. If csDestroying in FComponentstate Then Exit;
  277. include (FComponentState,csDestroying);
  278. If Assigned(FComponents) then
  279. for Runner:=0 to FComponents.Count-1 do
  280. TComponent(FComponents.Items[Runner]).Destroying;
  281. end;
  282. Function TComponent.FindComponent(const AName: string): TComponent;
  283. Var I : longint;
  284. begin
  285. Result:=Nil;
  286. If (AName='') or Not assigned(FComponents) then exit;
  287. For i:=0 to FComponents.Count-1 do
  288. if TComponent(FComponents[I]).Name=AName then
  289. begin
  290. Result:=TComponent(FComponents.Items[I]);
  291. exit;
  292. end;
  293. end;
  294. Procedure TComponent.FreeNotification(AComponent: TComponent);
  295. begin
  296. If (Owner<>Nil) and (AComponent=Owner) then exit;
  297. If not (Assigned(FFreeNotifies)) then
  298. FFreeNotifies:=TList.Create;
  299. If FFreeNotifies.IndexOf(AComponent)=-1 then
  300. begin
  301. FFreeNotifies.Add(AComponent);
  302. AComponent.FreeNotification (self);
  303. end;
  304. end;
  305. Procedure TComponent.FreeOnRelease;
  306. begin
  307. // Delphi compatibility only at the moment.
  308. end;
  309. Function TComponent.GetParentComponent: TComponent;
  310. begin
  311. Result:=Nil;
  312. end;
  313. Function TComponent.HasParent: Boolean;
  314. begin
  315. Result:=False;
  316. end;
  317. Procedure TComponent.InsertComponent(AComponent: TComponent);
  318. begin
  319. AComponent.ValidateContainer(Self);
  320. ValidateRename(AComponent,'',AComponent.FName);
  321. Insert(AComponent);
  322. AComponent.SetReference(True);
  323. If csDesigning in FComponentState then
  324. AComponent.SetDesigning(true);
  325. Notification(AComponent,opInsert);
  326. end;
  327. Procedure TComponent.RemoveComponent(AComponent: TComponent);
  328. begin
  329. Notification(AComponent,opRemove);
  330. AComponent.SetReference(False);
  331. Remove(AComponent);
  332. Acomponent.Setdesigning(False);
  333. ValidateRename(AComponent,AComponent.FName,'');
  334. end;
  335. Function TComponent.SafeCallException(ExceptObject: TObject;
  336. ExceptAddr: Pointer): Integer;
  337. begin
  338. SafeCallException:=0;
  339. end;
  340. {
  341. $Log$
  342. Revision 1.3 2001-01-08 18:36:01 sg
  343. * Applied bugfix for bug #1330 (merged)
  344. Revision 1.2 2000/07/13 11:32:59 michael
  345. + removed logs
  346. }