compon.inc 13 KB

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