compon.inc 12 KB

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