compon.inc 12 KB

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