compon.inc 12 KB

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