compon.inc 11 KB

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