compon.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. {%MainUnit classes.pp}
  2. {
  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. {* TComponentEnumerator *}
  13. {****************************************************************************}
  14. constructor TComponentEnumerator.Create(AComponent: TComponent);
  15. begin
  16. inherited Create;
  17. FComponent := AComponent;
  18. FPosition := -1;
  19. end;
  20. function TComponentEnumerator.GetCurrent: TComponent;
  21. begin
  22. Result := FComponent.Components[FPosition];
  23. end;
  24. function TComponentEnumerator.MoveNext: Boolean;
  25. begin
  26. Inc(FPosition);
  27. Result := FPosition < FComponent.ComponentCount;
  28. end;
  29. {****************************************************************************}
  30. {* TComponent *}
  31. {****************************************************************************}
  32. function TComponent.GetComObject: IUnknown;
  33. begin
  34. { Check if VCLComObject is not assigned - we need to create it by }
  35. { the call to CreateVCLComObject routine. If in the end we are still }
  36. { have no valid VCLComObject pointer we need to raise an exception }
  37. if not Assigned(VCLComObject) then
  38. begin
  39. if Assigned(CreateVCLComObjectProc) then
  40. CreateVCLComObjectProc(Self);
  41. if not Assigned(VCLComObject) then
  42. raise EComponentError.CreateFmt(SNoComSupport,[Name]);
  43. end;
  44. { VCLComObject is IVCComObject but we need to return IUnknown }
  45. IVCLComObject(VCLComObject).QueryInterface(IUnknown, Result);
  46. end;
  47. Function TComponent.GetComponent(AIndex: Integer): TComponent;
  48. begin
  49. If not assigned(FComponents) then
  50. Result:=Nil
  51. else
  52. Result:=TComponent(FComponents.Items[Aindex]);
  53. end;
  54. function TComponent.IsImplementorOf (const Intf:IInterface):boolean;
  55. var ref : IInterfaceComponentReference;
  56. begin
  57. result:=assigned(intf) and supports(intf,IInterfaceComponentReference,ref);
  58. if result then
  59. result:=ref.getcomponent=self;
  60. end;
  61. procedure TComponent.ReferenceInterface(const intf:IInterface;op:TOperation);
  62. var ref : IInterfaceComponentReference;
  63. comp : TComponent;
  64. begin
  65. if assigned(intf) and supports(intf,IInterfaceComponentReference,ref) then
  66. begin
  67. comp:=ref.getcomponent;
  68. if op = opInsert then
  69. comp.FreeNotification(Self)
  70. else
  71. comp.RemoveFreeNotification(Self);
  72. end;
  73. end;
  74. Function TComponent.GetComponentCount: Integer;
  75. begin
  76. If not assigned(FComponents) then
  77. result:=0
  78. else
  79. Result:=FComponents.Count;
  80. end;
  81. Function TComponent.GetComponentIndex: Integer;
  82. begin
  83. If Assigned(FOwner) and Assigned(FOwner.FComponents) then
  84. Result:=FOWner.FComponents.IndexOf(Self)
  85. else
  86. Result:=-1;
  87. end;
  88. function TComponent.GetObservers: TObservers;
  89. begin
  90. if FDObservers=Nil then
  91. begin
  92. FDObservers:=TObservers.Create;
  93. FDObservers.OnCanObserve:=@CanObserve;
  94. FDObservers.OnObserverAdded:=@ObserverAdded;
  95. end;
  96. Result:=FDObservers;
  97. end;
  98. function TComponent.CanObserve(const ID: Integer): Boolean;
  99. begin
  100. Result:=False;
  101. end;
  102. procedure TComponent.ObserverAdded(const ID: Integer; const Observer: IObserver);
  103. begin
  104. // Do nothing, can be used in descendants
  105. end;
  106. Procedure TComponent.Insert(AComponent: TComponent);
  107. begin
  108. If not assigned(FComponents) then
  109. FComponents:=TFpList.Create;
  110. FComponents.Add(AComponent);
  111. AComponent.FOwner:=Self;
  112. end;
  113. Procedure TComponent.ReadLeft(Reader: TReader);
  114. begin
  115. LongRec(FDesignInfo).Lo:=Reader.ReadInteger;
  116. end;
  117. Procedure TComponent.ReadTop(Reader: TReader);
  118. begin
  119. LongRec(FDesignInfo).Hi:=Reader.ReadInteger;
  120. end;
  121. Procedure TComponent.Remove(AComponent: TComponent);
  122. begin
  123. AComponent.FOwner:=Nil;
  124. If assigned(FCOmponents) then
  125. begin
  126. FComponents.Remove(AComponent);
  127. IF FComponents.Count=0 then
  128. begin
  129. FComponents.Free;
  130. FComponents:=Nil;
  131. end;
  132. end;
  133. end;
  134. Procedure TComponent.RemoveNotification(AComponent: TComponent);
  135. begin
  136. if FFreeNotifies<>nil then
  137. begin
  138. FFreeNotifies.Remove(AComponent);
  139. if FFreeNotifies.Count=0 then
  140. begin
  141. FFreeNotifies.Free;
  142. FFreeNotifies:=nil;
  143. Exclude(FComponentState,csFreeNotification);
  144. end;
  145. end;
  146. end;
  147. Procedure TComponent.SetComponentIndex(Value: Integer);
  148. Var Temp,Count : longint;
  149. begin
  150. If Not assigned(Fowner) then exit;
  151. Temp:=getcomponentindex;
  152. If temp<0 then exit;
  153. If value<0 then value:=0;
  154. Count:=Fowner.FComponents.Count;
  155. If Value>=Count then value:=count-1;
  156. If Value<>Temp then
  157. begin
  158. FOWner.FComponents.Delete(Temp);
  159. FOwner.FComponents.Insert(Value,Self);
  160. end;
  161. end;
  162. Procedure TComponent.SetReference(Enable: Boolean);
  163. var
  164. Field: ^TComponent;
  165. begin
  166. if Assigned(Owner) then
  167. begin
  168. Field := Owner.FieldAddress(Name);
  169. if Assigned(Field) then
  170. if Enable then
  171. Field^ := Self
  172. else
  173. Field^ := nil;
  174. end;
  175. end;
  176. Procedure TComponent.WriteLeft(Writer: TWriter);
  177. begin
  178. Writer.WriteInteger(LongRec(FDesignInfo).Lo);
  179. end;
  180. Procedure TComponent.WriteTop(Writer: TWriter);
  181. begin
  182. Writer.WriteInteger(LongRec(FDesignInfo).Hi);
  183. end;
  184. Procedure TComponent.ChangeName(const NewName: TComponentName);
  185. begin
  186. FName:=NewName;
  187. end;
  188. Procedure TComponent.DefineProperties(Filer: TFiler);
  189. Var Ancestor : TComponent;
  190. Temp : longint;
  191. begin
  192. Temp:=0;
  193. Ancestor:=TComponent(Filer.Ancestor);
  194. If Assigned(Ancestor) then Temp:=Ancestor.FDesignInfo;
  195. Filer.Defineproperty('Left',@readleft,@writeleft,
  196. (longrec(FDesignInfo).Lo<>Longrec(temp).Lo));
  197. Filer.Defineproperty('Top',@readtop,@writetop,
  198. (longrec(FDesignInfo).Hi<>Longrec(temp).Hi));
  199. end;
  200. Procedure TComponent.GetChildren(Proc: TGetChildProc; Root: TComponent);
  201. begin
  202. // Does nothing.
  203. end;
  204. Function TComponent.GetChildOwner: TComponent;
  205. begin
  206. Result:=Nil;
  207. end;
  208. Function TComponent.GetChildParent: TComponent;
  209. begin
  210. Result:=Self;
  211. end;
  212. Function TComponent.GetEnumerator: TComponentEnumerator;
  213. begin
  214. Result:=TComponentEnumerator.Create(Self);
  215. end;
  216. Function TComponent.GetNamePath: string;
  217. begin
  218. Result:=FName;
  219. end;
  220. Function TComponent.GetOwner: TPersistent;
  221. begin
  222. Result:=FOwner;
  223. end;
  224. Procedure TComponent.Loaded;
  225. begin
  226. Exclude(FComponentState,csLoading);
  227. end;
  228. Procedure TComponent.Loading;
  229. begin
  230. Include(FComponentState,csLoading);
  231. end;
  232. Procedure TComponent.Notification(AComponent: TComponent;
  233. Operation: TOperation);
  234. Var
  235. C : Longint;
  236. begin
  237. If (Operation=opRemove) then
  238. RemoveFreeNotification(AComponent);
  239. If Not assigned(FComponents) then
  240. exit;
  241. C:=FComponents.Count-1;
  242. While (C>=0) do
  243. begin
  244. TComponent(FComponents.Items[C]).Notification(AComponent,Operation);
  245. Dec(C);
  246. if C>=FComponents.Count then
  247. C:=FComponents.Count-1;
  248. end;
  249. end;
  250. procedure TComponent.PaletteCreated;
  251. begin
  252. end;
  253. Procedure TComponent.ReadState(Reader: TReader);
  254. begin
  255. Reader.ReadData(Self);
  256. end;
  257. procedure TComponent.RemoveFreeNotifications;
  258. var
  259. I: Integer;
  260. C: TComponent;
  261. begin
  262. If Assigned(FFreeNotifies) then
  263. begin
  264. I := FFreeNotifies.Count - 1;
  265. while I >= 0 do
  266. begin
  267. C := TComponent(FFreeNotifies.Items[I]);
  268. // Delete, so one component is not notified twice, if it is owned.
  269. FFreeNotifies.Delete(I);
  270. C.Notification (Self, opRemove);
  271. If FFreeNotifies = nil then
  272. I := 0
  273. else
  274. if I > FFreeNotifies.Count then
  275. I := FFreeNotifies.Count;
  276. Dec(I);
  277. end;
  278. FreeAndNil(FFreeNotifies);
  279. end;
  280. end;
  281. Procedure TComponent.SetAncestor(Value: Boolean);
  282. Var Runner : Longint;
  283. begin
  284. If Value then
  285. Include(FComponentState,csAncestor)
  286. else
  287. Exclude(FCOmponentState,csAncestor);
  288. if Assigned(FComponents) then
  289. For Runner:=0 To FComponents.Count-1 do
  290. TComponent(FComponents.Items[Runner]).SetAncestor(Value);
  291. end;
  292. Procedure TComponent.SetDesigning(Value: Boolean; SetChildren : Boolean = True);
  293. Var Runner : Longint;
  294. begin
  295. If Value then
  296. Include(FComponentState,csDesigning)
  297. else
  298. Exclude(FComponentState,csDesigning);
  299. if Assigned(FComponents) and SetChildren then
  300. For Runner:=0 To FComponents.Count - 1 do
  301. TComponent(FComponents.items[Runner]).SetDesigning(Value);
  302. end;
  303. Procedure TComponent.SetDesignInstance(Value: Boolean);
  304. begin
  305. If Value then
  306. Include(FComponentState,csDesignInstance)
  307. else
  308. Exclude(FComponentState,csDesignInstance);
  309. end;
  310. Procedure TComponent.SetInline(Value: Boolean);
  311. begin
  312. If Value then
  313. Include(FComponentState,csInline)
  314. else
  315. Exclude(FComponentState,csInline);
  316. end;
  317. Procedure TComponent.SetName(const NewName: TComponentName);
  318. begin
  319. If FName=NewName then exit;
  320. If (NewName<>'') and not IsValidIdent(NewName) then
  321. Raise EComponentError.CreateFmt(SInvalidName,[NewName]);
  322. If Assigned(FOwner) Then
  323. FOwner.ValidateRename(Self,FName,NewName)
  324. else
  325. ValidateRename(Nil,FName,NewName);
  326. SetReference(False);
  327. ChangeName(NewName);
  328. Setreference(True);
  329. end;
  330. Procedure TComponent.SetChildOrder(Child: TComponent; Order: Integer);
  331. begin
  332. // does nothing
  333. end;
  334. Procedure TComponent.SetParentComponent(Value: TComponent);
  335. begin
  336. // Does nothing
  337. end;
  338. Procedure TComponent.Updating;
  339. begin
  340. Include (FComponentState,csUpdating);
  341. end;
  342. Procedure TComponent.Updated;
  343. begin
  344. Exclude(FComponentState,csUpdating);
  345. end;
  346. class Procedure TComponent.UpdateRegistry(Register: Boolean; const ClassID, ProgID: string);
  347. begin
  348. // For compatibility only.
  349. end;
  350. Procedure TComponent.ValidateRename(AComponent: TComponent;
  351. const CurName, NewName: string);
  352. begin
  353. //!! This contradicts the Delphi manual.
  354. If (AComponent<>Nil) and (CompareText(CurName,NewName)<>0) and (AComponent.Owner = Self) and
  355. (FindComponent(NewName)<>Nil) then
  356. raise EComponentError.Createfmt(SDuplicateName,[newname]);
  357. If (csDesigning in FComponentState) and (FOwner<>Nil) then
  358. FOwner.ValidateRename(AComponent,Curname,Newname);
  359. end;
  360. Procedure TComponent.ValidateContainer(AComponent: TComponent);
  361. begin
  362. AComponent.ValidateInsert(Self);
  363. end;
  364. Procedure TComponent.ValidateInsert(AComponent: TComponent);
  365. begin
  366. // Does nothing.
  367. end;
  368. Procedure TComponent.WriteState(Writer: TWriter);
  369. begin
  370. Writer.WriteComponentData(Self);
  371. end;
  372. Constructor TComponent.Create(AOwner: TComponent);
  373. begin
  374. FComponentStyle:=[csInheritable];
  375. If Assigned(AOwner) then AOwner.InsertComponent(Self);
  376. end;
  377. Destructor TComponent.Destroy;
  378. begin
  379. Destroying;
  380. FreeAndNil(FObservers);
  381. RemoveFreeNotifications;
  382. DestroyComponents;
  383. If FOwner<>Nil Then FOwner.RemoveComponent(Self);
  384. inherited destroy;
  385. end;
  386. Procedure TComponent.BeforeDestruction;
  387. begin
  388. if not(csDestroying in FComponentstate) then
  389. Destroying;
  390. end;
  391. Procedure TComponent.DestroyComponents;
  392. Var acomponent: TComponent;
  393. begin
  394. While assigned(FComponents) do
  395. begin
  396. aComponent:=TComponent(FComponents.Last);
  397. Remove(aComponent);
  398. Acomponent.Destroy;
  399. end;
  400. end;
  401. Procedure TComponent.Destroying;
  402. Var Runner : longint;
  403. begin
  404. If csDestroying in FComponentstate Then Exit;
  405. include (FComponentState,csDestroying);
  406. If Assigned(FComponents) then
  407. for Runner:=0 to FComponents.Count-1 do
  408. TComponent(FComponents.Items[Runner]).Destroying;
  409. end;
  410. function TComponent.ExecuteAction(Action: TBasicAction): Boolean;
  411. begin
  412. if Action.HandlesTarget(Self) then
  413. begin
  414. Action.ExecuteTarget(Self);
  415. Result := True;
  416. end
  417. else
  418. Result := False;
  419. end;
  420. Function TComponent.FindComponent(const AName: string): TComponent;
  421. Var I : longint;
  422. C : TComponent;
  423. begin
  424. Result:=Nil;
  425. If (AName='') or Not assigned(FComponents) then exit;
  426. For i:=0 to FComponents.Count-1 do
  427. Begin
  428. c:=TComponent(FComponents[I]);
  429. If (CompareText(C.Name,AName)=0) then
  430. Exit(C);
  431. End;
  432. end;
  433. Procedure TComponent.FreeNotification(AComponent: TComponent);
  434. begin
  435. If (Owner<>Nil) and (AComponent=Owner) then exit;
  436. If not (Assigned(FFreeNotifies)) then
  437. FFreeNotifies:=TFpList.Create;
  438. If FFreeNotifies.IndexOf(AComponent)=-1 then
  439. begin
  440. FFreeNotifies.Add(AComponent);
  441. AComponent.FreeNotification (self);
  442. end;
  443. end;
  444. procedure TComponent.RemoveFreeNotification(AComponent: TComponent);
  445. begin
  446. RemoveNotification(AComponent);
  447. AComponent.RemoveNotification (self);
  448. end;
  449. Procedure TComponent.FreeOnRelease;
  450. begin
  451. if Assigned(VCLComObject) then
  452. IVCLComObject(VCLComObject).FreeOnRelease;
  453. end;
  454. Function TComponent.GetParentComponent: TComponent;
  455. begin
  456. Result:=Nil;
  457. end;
  458. Function TComponent.HasParent: Boolean;
  459. begin
  460. Result:=False;
  461. end;
  462. Procedure TComponent.InsertComponent(AComponent: TComponent);
  463. begin
  464. AComponent.ValidateContainer(Self);
  465. ValidateRename(AComponent,'',AComponent.FName);
  466. If AComponent.FOwner<>Nil then
  467. AComponent.FOwner.Remove(AComponent);
  468. Insert(AComponent);
  469. AComponent.SetReference(True);
  470. If csDesigning in FComponentState then
  471. AComponent.SetDesigning(true);
  472. Notification(AComponent,opInsert);
  473. end;
  474. Procedure TComponent.RemoveComponent(AComponent: TComponent);
  475. begin
  476. Notification(AComponent,opRemove);
  477. AComponent.SetReference(False);
  478. Remove(AComponent);
  479. Acomponent.Setdesigning(False);
  480. ValidateRename(AComponent,AComponent.FName,'');
  481. end;
  482. Function TComponent.SafeCallException(ExceptObject: TObject;
  483. ExceptAddr: CodePointer): HResult;
  484. begin
  485. if Assigned(VCLComObject) then
  486. Result := IVCLComObject(VCLComObject).SafeCallException(ExceptObject, ExceptAddr)
  487. else
  488. Result := inherited SafeCallException(ExceptObject, ExceptAddr);
  489. end;
  490. procedure TComponent.SetSubComponent(ASubComponent: Boolean);
  491. begin
  492. if ASubComponent then
  493. Include(FComponentStyle, csSubComponent)
  494. else
  495. Exclude(FComponentStyle, csSubComponent);
  496. end;
  497. function TComponent.UpdateAction(Action: TBasicAction): Boolean;
  498. begin
  499. if Action.HandlesTarget(Self) then
  500. begin
  501. Action.UpdateTarget(Self);
  502. Result := True;
  503. end
  504. else
  505. Result := False;
  506. end;
  507. function TComponent.QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} IID: TGUID; out Obj): HResult;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  508. begin
  509. if Assigned(VCLComObject) then
  510. Result := IVCLComObject(VCLComObject).QueryInterface(IID, Obj)
  511. else
  512. if GetInterface(IID, Obj) then
  513. Result := S_OK
  514. else
  515. Result := E_NOINTERFACE;
  516. end;
  517. function TComponent._AddRef: Longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  518. begin
  519. if Assigned(VCLComObject) then
  520. Result := IVCLComObject(VCLComObject)._AddRef
  521. else
  522. Result := -1;
  523. end;
  524. function TComponent._Release: Longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  525. begin
  526. if Assigned(VCLComObject) then
  527. Result := IVCLComObject(VCLComObject)._Release
  528. else
  529. Result := -1;
  530. end;
  531. function TComponent.iicrGetComponent: TComponent;
  532. begin
  533. result:=self;
  534. end;
  535. function TComponent.GetTypeInfoCount(out Count: Integer): HResult; stdcall;
  536. begin
  537. if Assigned(VCLComObject) then
  538. Result := IVCLComObject(VCLComObject).GetTypeInfoCount(Count)
  539. else
  540. Result := E_NOTIMPL;
  541. end;
  542. function TComponent.GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
  543. begin
  544. if Assigned(VCLComObject) then
  545. Result := IVCLComObject(VCLComObject).GetTypeInfo(Index, LocaleID, TypeInfo)
  546. else
  547. Result := E_NOTIMPL;
  548. end;
  549. function TComponent.GetIDsOfNames(const IID: TGUID; Names: Pointer; NameCount,
  550. LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
  551. begin
  552. if Assigned(VCLComObject) then
  553. Result := IVCLComObject(VCLComObject).GetIDsOfNames(IID, Names, NameCount, LocaleID, DispIDs)
  554. else
  555. Result := E_NOTIMPL;
  556. end;
  557. function TComponent.Invoke(DispID: Integer; const IID: TGUID;
  558. LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo,
  559. ArgErr: Pointer): HResult; stdcall;
  560. begin
  561. if Assigned(VCLComObject) then
  562. Result := IVCLComObject(VCLComObject).Invoke(DispID, IID, LocaleID, Flags, Params,
  563. VarResult, ExcepInfo, ArgErr)
  564. else
  565. Result := E_NOTIMPL;
  566. end;
  567. { Delta stream support }
  568. procedure TComponent.GetDeltaStreams(aProc: TGetStreamProc);
  569. begin
  570. // To be implemented by descendants
  571. end;
  572. procedure TComponent.ReadDeltaStream(const S: TStream);
  573. begin
  574. S.ReadComponent(Self);
  575. end;
  576. procedure TComponent.ReadDeltaState;
  577. var
  578. Done : boolean;
  579. begin
  580. if (csDesigning in ComponentState) then
  581. exit;
  582. Done:=False;
  583. if Assigned(FOnGetDeltaStreams) then
  584. FOnGetDeltaStreams(Self,@ReadDeltaStream,Done);
  585. if not Done then
  586. GetDeltaStreams(@ReadDeltaStream);
  587. end;