compon.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. {* TComponent *}
  12. {****************************************************************************}
  13. Function TComponent.GetComponent(AIndex: Integer): TComponent;
  14. begin
  15. If assigned(FComponents) then
  16. Result:=TComponent(FComponents.Items[Aindex])
  17. else
  18. Result:=Nil;
  19. end;
  20. function TComponent.IsImplementorOf (const Intf:IInterface):boolean;
  21. var ref : IInterfaceComponentReference;
  22. begin
  23. result:=assigned(intf) and supports(intf,IInterfaceComponentReference,ref) and (ref.getcomponent=self);
  24. end;
  25. procedure TComponent.ReferenceInterface(const intf:IInterface;op:TOperation);
  26. var ref : IInterfaceComponentReference;
  27. begin
  28. if assigned(intf) and supports(intf,IInterfaceComponentReference,ref) then
  29. ref.getcomponent.notification(self,op);
  30. end;
  31. Function TComponent.GetComponentCount: Integer;
  32. begin
  33. If assigned(FComponents) then
  34. Result:=FComponents.Count
  35. else
  36. result:=0
  37. end;
  38. Function TComponent.GetComponentIndex: Integer;
  39. begin
  40. If Assigned(FOwner) and Assigned(FOwner.FComponents) then
  41. Result:=FOwner.FComponents.IndexOf(Self)
  42. else
  43. Result:=-1;
  44. end;
  45. Procedure TComponent.Insert(AComponent: TComponent);
  46. begin
  47. If not assigned(FComponents) then
  48. FComponents:=TList.Create;
  49. FComponents.Add(AComponent);
  50. AComponent.FOwner:=Self;
  51. end;
  52. Procedure TComponent.ReadLeft(Reader: TReader);
  53. begin
  54. LongRec(FDesignInfo).Lo:=Reader.ReadInteger;
  55. end;
  56. Procedure TComponent.ReadTop(Reader: TReader);
  57. begin
  58. LongRec(FDesignInfo).Hi:=Reader.ReadInteger;
  59. end;
  60. Procedure TComponent.Remove(AComponent: TComponent);
  61. begin
  62. AComponent.FOwner:=Nil;
  63. If assigned(FComponents) then
  64. begin
  65. FComponents.Remove(AComponent);
  66. IF FComponents.Count=0 then
  67. begin
  68. FComponents.Free;
  69. FComponents:=Nil;
  70. end;
  71. end;
  72. end;
  73. Procedure TComponent.RemoveNotification(AComponent: TComponent);
  74. begin
  75. if FFreeNotifies<>nil then
  76. begin
  77. FFreeNotifies.Remove(AComponent);
  78. if FFreeNotifies.Count=0 then
  79. begin
  80. FFreeNotifies.Free;
  81. FFreeNotifies:=nil;
  82. Exclude(FComponentState, csFreeNotification);
  83. end;
  84. end;
  85. end;
  86. Procedure TComponent.SetComponentIndex(Value: Integer);
  87. Var Temp,Count : longint;
  88. begin
  89. If Not assigned(FOwner) then exit;
  90. Temp:=getcomponentindex;
  91. If temp<0 then exit;
  92. If value<0 then value:=0;
  93. Count:=Fowner.FComponents.Count;
  94. If Value>=Count then value:=count-1;
  95. If Value<>Temp then
  96. begin
  97. FOWner.FComponents.Delete(Temp);
  98. FOwner.FComponents.Insert(Value,Self);
  99. end;
  100. end;
  101. Procedure TComponent.SetReference(Enable: Boolean);
  102. var
  103. Field: ^TComponent;
  104. begin
  105. if Assigned(Owner) then
  106. begin
  107. Field := Owner.FieldAddress(Name);
  108. if Assigned(Field) then
  109. if Enable then
  110. Field^ := Self
  111. else
  112. Field^ := nil;
  113. end;
  114. end;
  115. Procedure TComponent.WriteLeft(Writer: TWriter);
  116. begin
  117. Writer.WriteInteger(LongRec(FDesignInfo).Lo);
  118. end;
  119. Procedure TComponent.WriteTop(Writer: TWriter);
  120. begin
  121. Writer.WriteInteger(LongRec(FDesignInfo).Hi);
  122. end;
  123. Procedure TComponent.ChangeName(const NewName: TComponentName);
  124. begin
  125. FName:=NewName;
  126. end;
  127. Procedure TComponent.DefineProperties(Filer: TFiler);
  128. Var Ancestor : TComponent;
  129. Temp : longint;
  130. begin
  131. Temp:=0;
  132. Ancestor:=TComponent(Filer.Ancestor);
  133. If Assigned(Ancestor) then Temp:=Ancestor.FDesignInfo;
  134. Filer.Defineproperty('left',@readleft,@writeleft,
  135. (longrec(FDesignInfo).Lo<>Longrec(temp).Lo));
  136. Filer.Defineproperty('top',@readtop,@writetop,
  137. (longrec(FDesignInfo).Hi<>Longrec(temp).Hi));
  138. end;
  139. Procedure TComponent.GetChildren(Proc: TGetChildProc; Root: TComponent);
  140. begin
  141. // Does nothing.
  142. end;
  143. Function TComponent.GetChildOwner: TComponent;
  144. begin
  145. Result:=Nil;
  146. end;
  147. Function TComponent.GetChildParent: TComponent;
  148. begin
  149. Result:=Self;
  150. end;
  151. Function TComponent.GetNamePath: string;
  152. begin
  153. Result:=FName;
  154. end;
  155. Function TComponent.GetOwner: TPersistent;
  156. begin
  157. Result:=FOwner;
  158. end;
  159. Procedure TComponent.Loaded;
  160. begin
  161. Exclude(FComponentState, csLoading);
  162. end;
  163. Procedure TComponent.Loading;
  164. begin
  165. Include(FComponentState, csLoading);
  166. end;
  167. Procedure TComponent.Notification(AComponent: TComponent;
  168. Operation: TOperation);
  169. Var Runner : Longint;
  170. begin
  171. If (Operation=opRemove) and Assigned(FFreeNotifies) then
  172. begin
  173. FFreeNotifies.Remove(AComponent);
  174. If FFreeNotifies.Count=0 then
  175. begin
  176. FFreeNotifies.Free;
  177. FFreenotifies:=Nil;
  178. Exclude(FComponentState, csFreeNotification);
  179. end;
  180. end;
  181. If assigned(FComponents) then
  182. For Runner:=0 To FComponents.Count-1 do
  183. TComponent(FComponents.Items[Runner]).Notification(AComponent,Operation);
  184. end;
  185. procedure TComponent.PaletteCreated;
  186. begin
  187. end;
  188. Procedure TComponent.ReadState(Reader: TReader);
  189. begin
  190. Reader.ReadData(Self);
  191. end;
  192. Procedure TComponent.SetAncestor(Value: Boolean);
  193. Var Runner : Longint;
  194. begin
  195. If Value then
  196. Include(FComponentState, csAncestor)
  197. else
  198. Exclude(FComponentState, csAncestor);
  199. if Assigned(FComponents) then
  200. For Runner:=0 To FComponents.Count-1 do
  201. TComponent(FComponents.Items[Runner]).SetAncestor(Value);
  202. end;
  203. Procedure TComponent.SetDesigning(Value: Boolean; SetChildren : Boolean = True);
  204. Var Runner : Longint;
  205. begin
  206. If Value then
  207. Include(FComponentState, csDesigning)
  208. else
  209. Exclude(FComponentState, csDesigning);
  210. if Assigned(FComponents) and SetChildren then
  211. For Runner:=0 To FComponents.Count - 1 do
  212. TComponent(FComponents.items[Runner]).SetDesigning(Value);
  213. end;
  214. Procedure TComponent.SetDesignInstance(Value: Boolean);
  215. begin
  216. If Value then
  217. Include(FComponentState, csDesignInstance)
  218. else
  219. Exclude(FComponentState, csDesignInstance);
  220. end;
  221. Procedure TComponent.SetInline(Value: Boolean);
  222. begin
  223. If Value then
  224. Include(FComponentState, csInline)
  225. else
  226. Exclude(FComponentState, csInline);
  227. end;
  228. Procedure TComponent.SetName(const NewName: TComponentName);
  229. begin
  230. If FName=NewName then exit;
  231. If (NewName<>'') and not IsValidIdent(NewName) then
  232. Raise EComponentError.CreateFmt(SInvalidName,[NewName]);
  233. If Assigned(FOwner) Then
  234. FOwner.ValidateRename(Self,FName,NewName)
  235. else
  236. ValidateRename(Nil,FName,NewName);
  237. SetReference(False);
  238. ChangeName(NewName);
  239. Setreference(True);
  240. end;
  241. Procedure TComponent.SetChildOrder(Child: TComponent; Order: Integer);
  242. begin
  243. // does nothing
  244. end;
  245. Procedure TComponent.SetParentComponent(Value: TComponent);
  246. begin
  247. // Does nothing
  248. end;
  249. Procedure TComponent.Updating;
  250. begin
  251. Include(FComponentState, csUpdating);
  252. end;
  253. Procedure TComponent.Updated;
  254. begin
  255. Exclude(FComponentState, csUpdating);
  256. end;
  257. class Procedure TComponent.UpdateRegistry(Register: Boolean; const ClassID, ProgID: string);
  258. begin
  259. // For compatibility only.
  260. end;
  261. Procedure TComponent.ValidateRename(AComponent: TComponent;
  262. const CurName, NewName: string);
  263. begin
  264. //!! This contradicts the Delphi manual.
  265. If (AComponent<>Nil) and (CompareText(CurName,NewName)<>0) and (AComponent.Owner = Self) and
  266. (FindComponent(NewName)<>Nil) then
  267. raise EComponentError.Createfmt(SDuplicateName,[newname]);
  268. If (csDesigning in FComponentState) and (FOwner<>Nil) then
  269. FOwner.ValidateRename(AComponent,Curname,Newname);
  270. end;
  271. Procedure TComponent.ValidateContainer(AComponent: TComponent);
  272. begin
  273. AComponent.ValidateInsert(Self);
  274. end;
  275. Procedure TComponent.ValidateInsert(AComponent: TComponent);
  276. begin
  277. // Does nothing.
  278. end;
  279. Procedure TComponent.WriteState(Writer: TWriter);
  280. begin
  281. Writer.WriteComponentData(Self);
  282. end;
  283. Constructor TComponent.Create(AOwner: TComponent);
  284. begin
  285. FComponentStyle:=[csInheritable];
  286. If Assigned(AOwner) then AOwner.InsertComponent(Self);
  287. end;
  288. Procedure TComponent.BeforeDestruction;
  289. begin
  290. Destroying;
  291. inherited BeforeDestruction;
  292. end;
  293. Destructor TComponent.Destroy;
  294. Var
  295. I : Integer;
  296. L : TList;
  297. begin
  298. If Assigned(FFreeNotifies) then
  299. begin
  300. L := FFreeNotifies;
  301. FFreenotifies:=Nil;
  302. for I := L.Count - 1 downto 0 do
  303. TComponent(L.Items[I]).Notification(self,opRemove);
  304. L.Free;
  305. end;
  306. DestroyComponents;
  307. If FOwner<>Nil Then FOwner.RemoveComponent(Self);
  308. inherited destroy;
  309. end;
  310. Procedure TComponent.DestroyComponents;
  311. Var acomponent: TComponent;
  312. begin
  313. While assigned(FComponents) do
  314. begin
  315. aComponent:=TComponent(FComponents.Last);
  316. Remove(aComponent);
  317. Acomponent.Destroy;
  318. end;
  319. end;
  320. Procedure TComponent.Destroying;
  321. Var Runner : longint;
  322. begin
  323. If csDestroying in FComponentstate Then Exit;
  324. Include(FComponentState, csDestroying);
  325. If Assigned(FComponents) then
  326. for Runner:=0 to FComponents.Count-1 do
  327. TComponent(FComponents.Items[Runner]).Destroying;
  328. end;
  329. function TComponent.ExecuteAction(Action: TBasicAction): Boolean;
  330. begin
  331. if Action.HandlesTarget(Self) then
  332. begin
  333. Action.ExecuteTarget(Self);
  334. Result := True;
  335. end
  336. else
  337. Result := False;
  338. end;
  339. Function TComponent.FindComponent(const AName: string): TComponent;
  340. Var I : longint;
  341. begin
  342. Result:=Nil;
  343. If (AName='') or Not assigned(FComponents) then exit;
  344. For i:=0 to FComponents.Count-1 do
  345. if (CompareText(TComponent(FComponents[I]).Name,AName)=0) then
  346. begin
  347. Result:=TComponent(FComponents.Items[I]);
  348. exit;
  349. end;
  350. end;
  351. Procedure TComponent.FreeNotification(AComponent: TComponent);
  352. begin
  353. If (Owner<>Nil) and (AComponent=Owner) then exit;
  354. if csDestroying in ComponentState then
  355. AComponent.Notification(Self,opRemove)
  356. else
  357. begin
  358. If not Assigned(FFreeNotifies) then
  359. begin
  360. FFreeNotifies:=TList.Create;
  361. Include(FComponentState, csFreeNotification);
  362. end;
  363. If FFreeNotifies.IndexOf(AComponent)=-1 then
  364. begin
  365. FFreeNotifies.Add(AComponent);
  366. AComponent.FreeNotification (self);
  367. end;
  368. end;
  369. end;
  370. procedure TComponent.RemoveFreeNotification(AComponent: TComponent);
  371. begin
  372. RemoveNotification(AComponent);
  373. AComponent.RemoveNotification (self);
  374. end;
  375. Procedure TComponent.FreeOnRelease;
  376. begin
  377. // Delphi compatibility only at the moment.
  378. end;
  379. Function TComponent.GetParentComponent: TComponent;
  380. begin
  381. Result:=Nil;
  382. end;
  383. Function TComponent.HasParent: Boolean;
  384. begin
  385. Result:=False;
  386. end;
  387. Procedure TComponent.InsertComponent(AComponent: TComponent);
  388. begin
  389. AComponent.ValidateContainer(Self);
  390. ValidateRename(AComponent,'',AComponent.FName);
  391. Insert(AComponent);
  392. AComponent.SetReference(True);
  393. If csDesigning in FComponentState then
  394. AComponent.SetDesigning(true);
  395. Notification(AComponent,opInsert);
  396. end;
  397. Procedure TComponent.RemoveComponent(AComponent: TComponent);
  398. begin
  399. Notification(AComponent,opRemove);
  400. AComponent.SetReference(False);
  401. Remove(AComponent);
  402. Acomponent.Setdesigning(False);
  403. ValidateRename(AComponent,AComponent.FName,'');
  404. end;
  405. Function TComponent.SafeCallException(ExceptObject: TObject;
  406. ExceptAddr: Pointer): Integer;
  407. begin
  408. SafeCallException:=0;
  409. end;
  410. procedure TComponent.SetSubComponent(ASubComponent: Boolean);
  411. begin
  412. if ASubComponent then
  413. Include(FComponentStyle, csSubComponent)
  414. else
  415. Exclude(FComponentStyle, csSubComponent);
  416. end;
  417. function TComponent.UpdateAction(Action: TBasicAction): Boolean;
  418. begin
  419. if Action.HandlesTarget(Self) then
  420. begin
  421. Action.UpdateTarget(Self);
  422. Result := True;
  423. end
  424. else
  425. Result := False;
  426. end;
  427. function TComponent.QueryInterface(const IID: TGUID; out Obj): HResult;stdcall;
  428. begin
  429. if GetInterface(IID, Obj) then
  430. result:=S_OK
  431. else
  432. result:=E_NOINTERFACE;
  433. end;
  434. function TComponent._AddRef: Integer;stdcall;
  435. begin
  436. result:=-1;
  437. end;
  438. function TComponent._Release: Integer;stdcall;
  439. begin
  440. result:=-1;
  441. end;
  442. function TComponent.iicrGetComponent: TComponent;
  443. begin
  444. result:=self;
  445. end;