compon.inc 12 KB

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