compon.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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.Notification(AComponent: TComponent;
  170. Operation: TOperation);
  171. Var Runner : Longint;
  172. begin
  173. If (Operation=opRemove) and Assigned(FFreeNotifies) then
  174. begin
  175. FFreeNotifies.Remove(AComponent);
  176. If FFreeNotifies.Count=0 then
  177. begin
  178. FFreeNotifies.Free;
  179. FFreenotifies:=Nil;
  180. end;
  181. end;
  182. If assigned(FComponents) then
  183. For Runner:=0 To FComponents.Count-1 do
  184. TComponent(FComponents.Items[Runner]).Notification(AComponent,Operation);
  185. end;
  186. procedure TComponent.PaletteCreated;
  187. begin
  188. end;
  189. Procedure TComponent.ReadState(Reader: TReader);
  190. begin
  191. Reader.ReadData(Self);
  192. end;
  193. Procedure TComponent.SetAncestor(Value: Boolean);
  194. Var Runner : Longint;
  195. begin
  196. If Value then
  197. Include(FComponentState,csAncestor)
  198. else
  199. Include(FCOmponentState,csAncestor);
  200. if Assigned(FComponents) then
  201. For Runner:=0 To FComponents.Count-1 do
  202. TComponent(FComponents.Items[Runner]).SetAncestor(Value);
  203. end;
  204. Procedure TComponent.SetDesigning(Value: Boolean);
  205. Var Runner : Longint;
  206. begin
  207. If Value then
  208. Include(FComponentSTate,csDesigning)
  209. else
  210. Exclude(FComponentSTate,csDesigning);
  211. if Assigned(FComponents) then
  212. For Runner:=0 To FComponents.Count - 1 do
  213. TComponent(FComponents.items[Runner]).SetDesigning(Value);
  214. end;
  215. Procedure TComponent.SetName(const NewName: TComponentName);
  216. begin
  217. If FName=NewName then exit;
  218. If (NewName<>'') and not IsValidIdent(NewName) then
  219. Raise EComponentError.CreateFmt(SInvalidName,[NewName]);
  220. If Assigned(FOwner) Then
  221. FOwner.ValidateRename(Self,FName,NewName)
  222. else
  223. ValidateRename(Nil,FName,NewName);
  224. SetReference(False);
  225. ChangeName(NewName);
  226. Setreference(True);
  227. end;
  228. Procedure TComponent.SetChildOrder(Child: TComponent; Order: Integer);
  229. begin
  230. // does nothing
  231. end;
  232. Procedure TComponent.SetParentComponent(Value: TComponent);
  233. begin
  234. // Does nothing
  235. end;
  236. Procedure TComponent.Updating;
  237. begin
  238. Include (FComponentState,csUpdating);
  239. end;
  240. Procedure TComponent.Updated;
  241. begin
  242. Exclude(FComponentState,csUpdating);
  243. end;
  244. class Procedure TComponent.UpdateRegistry(Register: Boolean; const ClassID, ProgID: string);
  245. begin
  246. // For compatibility only.
  247. end;
  248. Procedure TComponent.ValidateRename(AComponent: TComponent;
  249. const CurName, NewName: string);
  250. begin
  251. //!! This contradicts the Delphi manual.
  252. If (AComponent<>Nil) and (CompareText(CurName,NewName)<>0) and (AComponent.Owner = Self) and
  253. (FindComponent(NewName)<>Nil) then
  254. raise EComponentError.Createfmt(SDuplicateName,[newname]);
  255. If (csDesigning in FComponentState) and (FOwner<>Nil) then
  256. FOwner.ValidateRename(AComponent,Curname,Newname);
  257. end;
  258. Procedure TComponent.ValidateContainer(AComponent: TComponent);
  259. begin
  260. end;
  261. Procedure TComponent.ValidateInsert(AComponent: TComponent);
  262. begin
  263. // Does nothing.
  264. end;
  265. Procedure TComponent.WriteState(Writer: TWriter);
  266. begin
  267. Writer.WriteComponentData(Self);
  268. end;
  269. Constructor TComponent.Create(AOwner: TComponent);
  270. begin
  271. FComponentStyle:=[csInheritable];
  272. If Assigned(AOwner) then AOwner.InsertComponent(Self);
  273. end;
  274. Destructor TComponent.Destroy;
  275. Var
  276. I : Integer;
  277. C : TComponent;
  278. begin
  279. Destroying;
  280. If Assigned(FFreeNotifies) then
  281. begin
  282. I:=FFreeNotifies.Count-1;
  283. While (I>=0) do
  284. begin
  285. C:=TComponent(FFreeNotifies.Items[I]);
  286. // Delete, so one component is not notified twice, if it is owned.
  287. FFreeNotifies.Delete(I);
  288. C.Notification (self,opRemove);
  289. If (FFreeNotifies=Nil) then
  290. I:=0
  291. else if (I>FFreeNotifies.Count) then
  292. I:=FFreeNotifies.Count;
  293. dec(i);
  294. end;
  295. FreeAndNil(FFreeNotifies);
  296. end;
  297. DestroyComponents;
  298. If FOwner<>Nil Then FOwner.RemoveComponent(Self);
  299. inherited destroy;
  300. end;
  301. Procedure TComponent.BeforeDestruction;
  302. begin
  303. if not(csDestroying in FComponentstate) then
  304. Destroying;
  305. end;
  306. Procedure TComponent.DestroyComponents;
  307. Var acomponent: TComponent;
  308. begin
  309. While assigned(FComponents) do
  310. begin
  311. aComponent:=TComponent(FComponents.Last);
  312. Remove(aComponent);
  313. Acomponent.Destroy;
  314. end;
  315. end;
  316. Procedure TComponent.Destroying;
  317. Var Runner : longint;
  318. begin
  319. If csDestroying in FComponentstate Then Exit;
  320. include (FComponentState,csDestroying);
  321. If Assigned(FComponents) then
  322. for Runner:=0 to FComponents.Count-1 do
  323. TComponent(FComponents.Items[Runner]).Destroying;
  324. end;
  325. function TComponent.ExecuteAction(Action: TBasicAction): Boolean;
  326. begin
  327. if Action.HandlesTarget(Self) then
  328. begin
  329. Action.ExecuteTarget(Self);
  330. Result := True;
  331. end
  332. else
  333. Result := False;
  334. end;
  335. Function TComponent.FindComponent(const AName: string): TComponent;
  336. Var I : longint;
  337. begin
  338. Result:=Nil;
  339. If (AName='') or Not assigned(FComponents) then exit;
  340. For i:=0 to FComponents.Count-1 do
  341. if (CompareText(TComponent(FComponents[I]).Name,AName)=0) then
  342. begin
  343. Result:=TComponent(FComponents.Items[I]);
  344. exit;
  345. end;
  346. end;
  347. Procedure TComponent.FreeNotification(AComponent: TComponent);
  348. begin
  349. If (Owner<>Nil) and (AComponent=Owner) then exit;
  350. if csDestroying in ComponentState then
  351. AComponent.Notification(Self,opRemove)
  352. else
  353. begin
  354. If not (Assigned(FFreeNotifies)) then
  355. FFreeNotifies:=TList.Create;
  356. If FFreeNotifies.IndexOf(AComponent)=-1 then
  357. begin
  358. FFreeNotifies.Add(AComponent);
  359. AComponent.FreeNotification (self);
  360. end;
  361. end;
  362. end;
  363. procedure TComponent.RemoveFreeNotification(AComponent: TComponent);
  364. begin
  365. RemoveNotification(AComponent);
  366. AComponent.RemoveNotification (self);
  367. end;
  368. Procedure TComponent.FreeOnRelease;
  369. begin
  370. // Delphi compatibility only at the moment.
  371. end;
  372. Function TComponent.GetParentComponent: TComponent;
  373. begin
  374. Result:=Nil;
  375. end;
  376. Function TComponent.HasParent: Boolean;
  377. begin
  378. Result:=False;
  379. end;
  380. Procedure TComponent.InsertComponent(AComponent: TComponent);
  381. begin
  382. AComponent.ValidateContainer(Self);
  383. ValidateRename(AComponent,'',AComponent.FName);
  384. Insert(AComponent);
  385. AComponent.SetReference(True);
  386. If csDesigning in FComponentState then
  387. AComponent.SetDesigning(true);
  388. Notification(AComponent,opInsert);
  389. end;
  390. Procedure TComponent.RemoveComponent(AComponent: TComponent);
  391. begin
  392. Notification(AComponent,opRemove);
  393. AComponent.SetReference(False);
  394. Remove(AComponent);
  395. Acomponent.Setdesigning(False);
  396. ValidateRename(AComponent,AComponent.FName,'');
  397. end;
  398. Function TComponent.SafeCallException(ExceptObject: TObject;
  399. ExceptAddr: Pointer): Integer;
  400. begin
  401. SafeCallException:=0;
  402. end;
  403. procedure TComponent.SetSubComponent(ASubComponent: Boolean);
  404. begin
  405. if ASubComponent then
  406. Include(FComponentStyle, csSubComponent)
  407. else
  408. Exclude(FComponentStyle, csSubComponent);
  409. end;
  410. function TComponent.UpdateAction(Action: TBasicAction): Boolean;
  411. begin
  412. if Action.HandlesTarget(Self) then
  413. begin
  414. Action.UpdateTarget(Self);
  415. Result := True;
  416. end
  417. else
  418. Result := False;
  419. end;
  420. function TComponent.QueryInterface(const IID: TGUID; out Obj): HResult;stdcall;
  421. begin
  422. if GetInterface(IID, Obj) then
  423. result:=S_OK
  424. else
  425. result:=E_NOINTERFACE;
  426. end;
  427. function TComponent._AddRef: Integer;stdcall;
  428. begin
  429. result:=-1;
  430. end;
  431. function TComponent._Release: Integer;stdcall;
  432. begin
  433. result:=-1;
  434. end;
  435. function TComponent.iicrGetComponent: TComponent;
  436. begin
  437. result:=self;
  438. end;