compon.inc 11 KB

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