compon.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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 = Packed 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. Filer.Defineproperty('left',@readleft,@writeleft,
  129. (longrec(FDesignInfo).Lo<>Longrec(temp).Lo));
  130. Filer.Defineproperty('top',@readtop,@writetop,
  131. (longrec(FDesignInfo).Hi<>Longrec(temp).Hi));
  132. end;
  133. Procedure TComponent.GetChildren(Proc: TGetChildProc; Root: TComponent);
  134. begin
  135. // Does nothing.
  136. end;
  137. Function TComponent.GetChildOwner: TComponent;
  138. begin
  139. Result:=Nil;
  140. end;
  141. Function TComponent.GetChildParent: TComponent;
  142. begin
  143. Result:=Self;
  144. end;
  145. Function TComponent.GetNamePath: string;
  146. begin
  147. Result:=FName;
  148. end;
  149. Function TComponent.GetOwner: TPersistent;
  150. begin
  151. Result:=FOwner;
  152. end;
  153. Procedure TComponent.Loaded;
  154. begin
  155. Exclude(FComponentState,csLoading);
  156. end;
  157. Procedure TComponent.Notification(AComponent: TComponent;
  158. Operation: TOperation);
  159. Var Runner : Longint;
  160. begin
  161. If (Operation=opRemove) and Assigned(FFreeNotifies) then
  162. begin
  163. FFreeNotifies.Remove(AComponent);
  164. If FFreeNotifies.Count=0 then
  165. begin
  166. FFreeNotifies.Free;
  167. FFreenotifies:=Nil;
  168. end;
  169. end;
  170. If assigned(FComponents) then
  171. For Runner:=0 To FComponents.Count-1 do
  172. TComponent(FComponents.Items[Runner]).Notification(AComponent,Operation);
  173. end;
  174. procedure TComponent.PaletteCreated;
  175. begin
  176. end;
  177. Procedure TComponent.ReadState(Reader: TReader);
  178. begin
  179. Reader.ReadData(Self);
  180. end;
  181. Procedure TComponent.SetAncestor(Value: Boolean);
  182. Var Runner : Longint;
  183. begin
  184. If Value then
  185. Include(FComponentState,csAncestor)
  186. else
  187. Include(FCOmponentState,csAncestor);
  188. if Assigned(FComponents) then
  189. For Runner:=0 To FComponents.Count-1 do
  190. TComponent(FComponents.Items[Runner]).SetAncestor(Value);
  191. end;
  192. Procedure TComponent.SetDesigning(Value: Boolean);
  193. Var Runner : Longint;
  194. begin
  195. If Value then
  196. Include(FComponentSTate,csDesigning)
  197. else
  198. Exclude(FComponentSTate,csDesigning);
  199. if Assigned(FComponents) then
  200. For Runner:=0 To FComponents.Count - 1 do
  201. TComponent(FComponents.items[Runner]).SetDesigning(Value);
  202. end;
  203. Procedure TComponent.SetName(const NewName: TComponentName);
  204. begin
  205. If FName=NewName then exit;
  206. If not IsValidIdent(NewName) then
  207. Raise EComponentError.CreateFmt(SInvalidName,[NewName]);
  208. If Assigned(FOwner) Then
  209. FOwner.ValidateRename(Self,FName,NewName)
  210. else
  211. ValidateRename(Nil,FName,NewName);
  212. SetReference(False);
  213. ChangeName(NewName);
  214. Setreference(True);
  215. end;
  216. Procedure TComponent.SetChildOrder(Child: TComponent; Order: Integer);
  217. begin
  218. // does nothing
  219. end;
  220. Procedure TComponent.SetParentComponent(Value: TComponent);
  221. begin
  222. // Does nothing
  223. end;
  224. Procedure TComponent.Updating;
  225. begin
  226. Include (FComponentState,csUpdating);
  227. end;
  228. Procedure TComponent.Updated;
  229. begin
  230. Exclude(FComponentState,csUpdating);
  231. end;
  232. class Procedure TComponent.UpdateRegistry(Register: Boolean; const ClassID, ProgID: string);
  233. begin
  234. // For compatibility only.
  235. end;
  236. Procedure TComponent.ValidateRename(AComponent: TComponent;
  237. const CurName, NewName: string);
  238. begin
  239. //!! This contradicts the Delphi manual.
  240. If (AComponent<>Nil) and (CurName<>NewName) and (AComponent.Owner = Self) and
  241. (FindComponent(NewName)<>Nil) then
  242. raise EComponentError.Createfmt(SDuplicateName,[newname]);
  243. If (csDesigning in FComponentState) and (FOwner<>Nil) then
  244. FOwner.ValidateRename(AComponent,Curname,Newname);
  245. end;
  246. Procedure TComponent.ValidateContainer(AComponent: TComponent);
  247. begin
  248. end;
  249. Procedure TComponent.ValidateInsert(AComponent: TComponent);
  250. begin
  251. // Does nothing.
  252. end;
  253. Procedure TComponent.WriteState(Writer: TWriter);
  254. begin
  255. Writer.WriteComponentData(Self);
  256. end;
  257. Constructor TComponent.Create(AOwner: TComponent);
  258. begin
  259. FComponentStyle:=[csInheritable];
  260. If Assigned(AOwner) then AOwner.InsertComponent(Self);
  261. end;
  262. Destructor TComponent.Destroy;
  263. Var
  264. I : Integer;
  265. C : TComponent;
  266. begin
  267. Destroying;
  268. If Assigned(FFreeNotifies) then
  269. begin
  270. I:=FFreeNotifies.Count-1;
  271. While (I>=0) do
  272. begin
  273. C:=TComponent(FFreeNotifies.Items[I]);
  274. // Delete, so one component is not notified twice, if it is owned.
  275. FFreeNotifies.Delete(I);
  276. C.Notification (self,opRemove);
  277. If (FFreeNotifies=Nil) then
  278. I:=0
  279. else if (I>FFreeNotifies.Count) then
  280. I:=FFreeNotifies.Count;
  281. dec(i);
  282. end;
  283. FreeAndNil(FFreeNotifies);
  284. end;
  285. DestroyComponents;
  286. If FOwner<>Nil Then FOwner.RemoveComponent(Self);
  287. inherited destroy;
  288. end;
  289. Procedure TComponent.BeforeDestruction;
  290. begin
  291. if not(csDestroying in FComponentstate) then
  292. Destroying;
  293. end;
  294. Procedure TComponent.DestroyComponents;
  295. Var acomponent: TComponent;
  296. begin
  297. While assigned(FComponents) do
  298. begin
  299. aComponent:=TComponent(FComponents.Last);
  300. Remove(aComponent);
  301. Acomponent.Destroy;
  302. end;
  303. end;
  304. Procedure TComponent.Destroying;
  305. Var Runner : longint;
  306. begin
  307. If csDestroying in FComponentstate Then Exit;
  308. include (FComponentState,csDestroying);
  309. If Assigned(FComponents) then
  310. for Runner:=0 to FComponents.Count-1 do
  311. TComponent(FComponents.Items[Runner]).Destroying;
  312. end;
  313. function TComponent.ExecuteAction(Action: TBasicAction): Boolean;
  314. begin
  315. if Action.HandlesTarget(Self) then
  316. begin
  317. Action.ExecuteTarget(Self);
  318. Result := True;
  319. end
  320. else
  321. Result := False;
  322. end;
  323. Function TComponent.FindComponent(const AName: string): TComponent;
  324. Var I : longint;
  325. begin
  326. Result:=Nil;
  327. If (AName='') or Not assigned(FComponents) then exit;
  328. For i:=0 to FComponents.Count-1 do
  329. if TComponent(FComponents[I]).Name=AName then
  330. begin
  331. Result:=TComponent(FComponents.Items[I]);
  332. exit;
  333. end;
  334. end;
  335. Procedure TComponent.FreeNotification(AComponent: TComponent);
  336. begin
  337. If (Owner<>Nil) and (AComponent=Owner) then exit;
  338. if csDestroying in ComponentState then
  339. AComponent.Notification(Self,opRemove)
  340. else
  341. begin
  342. If not (Assigned(FFreeNotifies)) then
  343. FFreeNotifies:=TList.Create;
  344. If FFreeNotifies.IndexOf(AComponent)=-1 then
  345. begin
  346. FFreeNotifies.Add(AComponent);
  347. AComponent.FreeNotification (self);
  348. end;
  349. end;
  350. end;
  351. procedure TComponent.RemoveFreeNotification(AComponent: TComponent);
  352. begin
  353. RemoveNotification(AComponent);
  354. AComponent.RemoveNotification (self);
  355. end;
  356. Procedure TComponent.FreeOnRelease;
  357. begin
  358. // Delphi compatibility only at the moment.
  359. end;
  360. Function TComponent.GetParentComponent: TComponent;
  361. begin
  362. Result:=Nil;
  363. end;
  364. Function TComponent.HasParent: Boolean;
  365. begin
  366. Result:=False;
  367. end;
  368. Procedure TComponent.InsertComponent(AComponent: TComponent);
  369. begin
  370. AComponent.ValidateContainer(Self);
  371. ValidateRename(AComponent,'',AComponent.FName);
  372. Insert(AComponent);
  373. AComponent.SetReference(True);
  374. If csDesigning in FComponentState then
  375. AComponent.SetDesigning(true);
  376. Notification(AComponent,opInsert);
  377. end;
  378. Procedure TComponent.RemoveComponent(AComponent: TComponent);
  379. begin
  380. Notification(AComponent,opRemove);
  381. AComponent.SetReference(False);
  382. Remove(AComponent);
  383. Acomponent.Setdesigning(False);
  384. ValidateRename(AComponent,AComponent.FName,'');
  385. end;
  386. Function TComponent.SafeCallException(ExceptObject: TObject;
  387. ExceptAddr: Pointer): Integer;
  388. begin
  389. SafeCallException:=0;
  390. end;
  391. function TComponent.UpdateAction(Action: TBasicAction): Boolean;
  392. begin
  393. if Action.HandlesTarget(Self) then
  394. begin
  395. Action.UpdateTarget(Self);
  396. Result := True;
  397. end
  398. else
  399. Result := False;
  400. end;
  401. {$ifdef HASINTF}
  402. function TComponent.QueryInterface(const IID: TGUID; out Obj): HResult;stdcall;
  403. begin
  404. if GetInterface(IID, Obj) then
  405. result:=S_OK
  406. else
  407. result:=E_NOINTERFACE;
  408. end;
  409. function TComponent._AddRef: Integer;stdcall;
  410. begin
  411. result:=-1;
  412. end;
  413. function TComponent._Release: Integer;stdcall;
  414. begin
  415. result:=-1;
  416. end;
  417. {$endif HASINTF}
  418. {
  419. $Log$
  420. Revision 1.7 2005-02-02 19:56:11 florian
  421. + PaletteCreated added
  422. + TCollectionNotification added
  423. Revision 1.6 2005/01/31 19:41:39 peter
  424. * interface additions
  425. Revision 1.5 2004/09/22 19:57:45 michael
  426. + More optimal FreeNotification when destroying
  427. Revision 1.4 2004/09/22 18:48:31 michael
  428. + Fix in TComponent destroy FreeNotifications.
  429. Revision 1.3 2004/08/07 16:44:35 florian
  430. * tcomponent.destroying is now called in tcomponent.beforedestruction
  431. Revision 1.2 2004/01/12 17:44:22 peter
  432. * LongRec is a packed record
  433. Revision 1.1 2003/10/06 21:01:06 peter
  434. * moved classes unit to rtl
  435. Revision 1.9 2003/04/27 21:16:11 sg
  436. * Fixed TComponent.ValidateRename
  437. Revision 1.8 2002/10/15 20:06:19 michael
  438. + Fixed SetAncestor. Index was getting too big
  439. Revision 1.7 2002/09/07 15:15:24 peter
  440. * old logs removed and tabs fixed
  441. Revision 1.6 2002/01/09 10:40:24 michael
  442. + re-enabled Top/Left property writing
  443. Revision 1.5 2002/01/06 21:54:50 peter
  444. * action classes added
  445. }