compon.inc 13 KB

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