compon.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. AComponent.ValidateInsert(Self);
  279. end;
  280. Procedure TComponent.ValidateInsert(AComponent: TComponent);
  281. begin
  282. // Does nothing.
  283. end;
  284. Procedure TComponent.WriteState(Writer: TWriter);
  285. begin
  286. Writer.WriteComponentData(Self);
  287. end;
  288. Constructor TComponent.Create(AOwner: TComponent);
  289. begin
  290. FComponentStyle:=[csInheritable];
  291. If Assigned(AOwner) then AOwner.InsertComponent(Self);
  292. end;
  293. Destructor TComponent.Destroy;
  294. Var
  295. I : Integer;
  296. C : TComponent;
  297. begin
  298. Destroying;
  299. If Assigned(FFreeNotifies) then
  300. begin
  301. I:=FFreeNotifies.Count-1;
  302. While (I>=0) do
  303. begin
  304. C:=TComponent(FFreeNotifies.Items[I]);
  305. // Delete, so one component is not notified twice, if it is owned.
  306. FFreeNotifies.Delete(I);
  307. C.Notification (self,opRemove);
  308. If (FFreeNotifies=Nil) then
  309. I:=0
  310. else if (I>FFreeNotifies.Count) then
  311. I:=FFreeNotifies.Count;
  312. dec(i);
  313. end;
  314. FreeAndNil(FFreeNotifies);
  315. end;
  316. DestroyComponents;
  317. If FOwner<>Nil Then FOwner.RemoveComponent(Self);
  318. inherited destroy;
  319. end;
  320. Procedure TComponent.BeforeDestruction;
  321. begin
  322. if not(csDestroying in FComponentstate) then
  323. Destroying;
  324. end;
  325. Procedure TComponent.DestroyComponents;
  326. Var acomponent: TComponent;
  327. begin
  328. While assigned(FComponents) do
  329. begin
  330. aComponent:=TComponent(FComponents.Last);
  331. Remove(aComponent);
  332. Acomponent.Destroy;
  333. end;
  334. end;
  335. Procedure TComponent.Destroying;
  336. Var Runner : longint;
  337. begin
  338. If csDestroying in FComponentstate Then Exit;
  339. include (FComponentState,csDestroying);
  340. If Assigned(FComponents) then
  341. for Runner:=0 to FComponents.Count-1 do
  342. TComponent(FComponents.Items[Runner]).Destroying;
  343. end;
  344. function TComponent.ExecuteAction(Action: TBasicAction): Boolean;
  345. begin
  346. if Action.HandlesTarget(Self) then
  347. begin
  348. Action.ExecuteTarget(Self);
  349. Result := True;
  350. end
  351. else
  352. Result := False;
  353. end;
  354. Function TComponent.FindComponent(const AName: string): TComponent;
  355. Var I : longint;
  356. begin
  357. Result:=Nil;
  358. If (AName='') or Not assigned(FComponents) then exit;
  359. For i:=0 to FComponents.Count-1 do
  360. if (CompareText(TComponent(FComponents[I]).Name,AName)=0) then
  361. begin
  362. Result:=TComponent(FComponents.Items[I]);
  363. exit;
  364. end;
  365. end;
  366. Procedure TComponent.FreeNotification(AComponent: TComponent);
  367. begin
  368. If (Owner<>Nil) and (AComponent=Owner) then exit;
  369. if csDestroying in ComponentState then
  370. AComponent.Notification(Self,opRemove)
  371. else
  372. begin
  373. If not (Assigned(FFreeNotifies)) then
  374. FFreeNotifies:=TList.Create;
  375. If FFreeNotifies.IndexOf(AComponent)=-1 then
  376. begin
  377. FFreeNotifies.Add(AComponent);
  378. AComponent.FreeNotification (self);
  379. end;
  380. end;
  381. end;
  382. procedure TComponent.RemoveFreeNotification(AComponent: TComponent);
  383. begin
  384. RemoveNotification(AComponent);
  385. AComponent.RemoveNotification (self);
  386. end;
  387. Procedure TComponent.FreeOnRelease;
  388. begin
  389. // Delphi compatibility only at the moment.
  390. end;
  391. Function TComponent.GetParentComponent: TComponent;
  392. begin
  393. Result:=Nil;
  394. end;
  395. Function TComponent.HasParent: Boolean;
  396. begin
  397. Result:=False;
  398. end;
  399. Procedure TComponent.InsertComponent(AComponent: TComponent);
  400. begin
  401. AComponent.ValidateContainer(Self);
  402. ValidateRename(AComponent,'',AComponent.FName);
  403. Insert(AComponent);
  404. AComponent.SetReference(True);
  405. If csDesigning in FComponentState then
  406. AComponent.SetDesigning(true);
  407. Notification(AComponent,opInsert);
  408. end;
  409. Procedure TComponent.RemoveComponent(AComponent: TComponent);
  410. begin
  411. Notification(AComponent,opRemove);
  412. AComponent.SetReference(False);
  413. Remove(AComponent);
  414. Acomponent.Setdesigning(False);
  415. ValidateRename(AComponent,AComponent.FName,'');
  416. end;
  417. Function TComponent.SafeCallException(ExceptObject: TObject;
  418. ExceptAddr: Pointer): Integer;
  419. begin
  420. SafeCallException:=0;
  421. end;
  422. procedure TComponent.SetSubComponent(ASubComponent: Boolean);
  423. begin
  424. if ASubComponent then
  425. Include(FComponentStyle, csSubComponent)
  426. else
  427. Exclude(FComponentStyle, csSubComponent);
  428. end;
  429. function TComponent.UpdateAction(Action: TBasicAction): Boolean;
  430. begin
  431. if Action.HandlesTarget(Self) then
  432. begin
  433. Action.UpdateTarget(Self);
  434. Result := True;
  435. end
  436. else
  437. Result := False;
  438. end;
  439. function TComponent.QueryInterface(const IID: TGUID; out Obj): HResult;stdcall;
  440. begin
  441. if GetInterface(IID, Obj) then
  442. result:=S_OK
  443. else
  444. result:=E_NOINTERFACE;
  445. end;
  446. function TComponent._AddRef: Integer;stdcall;
  447. begin
  448. result:=-1;
  449. end;
  450. function TComponent._Release: Integer;stdcall;
  451. begin
  452. result:=-1;
  453. end;
  454. function TComponent.iicrGetComponent: TComponent;
  455. begin
  456. result:=self;
  457. end;