dbf_reg.pas 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. unit dbf_reg;
  2. {===============================================================================
  3. || TDbf Component || http://tdbf.sf.net ||
  4. ===============================================================================}
  5. (*
  6. tDBF is supplied "AS IS". The author disclaims all warranties,
  7. expressed or implied, including, without limitation, the warranties of
  8. merchantability and or fitness for any purpose. The author assumes no
  9. liability for damages, direct or consequential, which may result from the
  10. use of TDBF.
  11. TDbf is licensed under the LGPL (lesser general public license).
  12. You are allowed to use this component in any project free of charge.
  13. You are
  14. - NOT allowed to claim that you have created this component. You are
  15. - NOT allowed to copy this component's code into your own component and
  16. claim that the code is your idea.
  17. *)
  18. interface
  19. {$I Dbf_Common.inc}
  20. procedure Register;
  21. implementation
  22. {$ifndef FPC}
  23. {$R Dbf.dcr}
  24. {$endif}
  25. uses
  26. SysUtils,
  27. Classes,
  28. {$ifdef KYLIX}
  29. QGraphics,
  30. QControls,
  31. QForms,
  32. QDialogs,
  33. {$else}
  34. Controls,
  35. Forms,
  36. Dialogs,
  37. {$endif}
  38. Dbf,
  39. Dbf_DbfFile,
  40. Dbf_IdxFile,
  41. Dbf_Fields,
  42. Dbf_Common,
  43. Dbf_Str
  44. {$ifndef FPC}
  45. ,ExptIntf
  46. {$endif}
  47. {$ifdef DELPHI_6}
  48. ,DesignIntf,DesignEditors
  49. {$else}
  50. {$ifndef FPC}
  51. ,DsgnIntf
  52. {$else}
  53. ,PropEdits
  54. ,LazarusPackageIntf
  55. ,LResources
  56. {,ComponentEditors}
  57. {$endif}
  58. {$endif}
  59. ;
  60. //==========================================================
  61. //============ DESIGNONLY ==================================
  62. //==========================================================
  63. (*
  64. //==========================================================
  65. //============ TFilePathProperty
  66. //==========================================================
  67. type
  68. TFilePathProperty = class(TStringProperty)
  69. public
  70. function GetValue: string; override;
  71. end;
  72. function TFilePathProperty.GetValue: string;
  73. begin
  74. Result := inherited GetValue;
  75. if Result = EmptyStr then
  76. begin
  77. SetValue(ExtractFilePath(ToolServices.GetProjectName));
  78. Result := inherited GetValue;
  79. end;
  80. end;
  81. *)
  82. //==========================================================
  83. //============ TTableNameProperty
  84. //==========================================================
  85. type
  86. TTableNameProperty = class(TStringProperty)
  87. public
  88. procedure Edit; override;
  89. function GetAttributes: TPropertyAttributes; override;
  90. end;
  91. procedure TTableNameProperty.Edit; {override;}
  92. var
  93. FileOpen: TOpenDialog;
  94. Dbf: TDbf;
  95. begin
  96. FileOpen := TOpenDialog.Create(Application);
  97. try
  98. with fileopen do begin
  99. Dbf := GetComponent(0) as TDbf;
  100. {$ifndef FPC}
  101. if Dbf.FilePath = EmptyStr then
  102. FileOpen.InitialDir := ExtractFilePath(ToolServices.GetProjectName)
  103. else
  104. {$endif}
  105. FileOpen.InitialDir := Dbf.AbsolutePath;
  106. Filename := GetValue;
  107. Filter := 'Dbf table|*.dbf';
  108. if Execute then begin
  109. SetValue(Filename);
  110. end;
  111. end;
  112. finally
  113. Fileopen.free;
  114. end;
  115. end;
  116. function TTableNameProperty.GetAttributes: TPropertyAttributes; {override;}
  117. begin
  118. Result := [paDialog, paRevertable];
  119. end;
  120. //==========================================================
  121. //============ TIndexFileNameProperty
  122. //==========================================================
  123. type
  124. TIndexFileNameProperty = class(TStringProperty)
  125. public
  126. procedure Edit; override;
  127. function GetAttributes: TPropertyAttributes; override;
  128. end;
  129. procedure TIndexFileNameProperty.Edit; {override;}
  130. var
  131. FileOpen: TOpenDialog;
  132. IndexDef: TDbfIndexDef;
  133. Indexes: TDbfIndexDefs;
  134. Dbf: TDbf;
  135. begin
  136. FileOpen := TOpenDialog.Create(Application);
  137. try
  138. with fileopen do begin
  139. IndexDef := GetComponent(0) as TDbfIndexDef;
  140. Indexes := TDbfIndexDefs(IndexDef.Collection);
  141. Dbf := TDbf(Indexes.FOwner);
  142. FileOpen.InitialDir := Dbf.AbsolutePath;
  143. Filename := GetValue;
  144. Filter := 'Simple index (ndx)|*.ndx'{|Multiple index (mdx)|*.mdx'};
  145. if Execute then begin
  146. SetValue(ExtractFileName(Filename));
  147. end;
  148. end;
  149. finally
  150. Fileopen.free;
  151. end;
  152. end;
  153. function TIndexFileNameProperty.GetAttributes: TPropertyAttributes; {override;}
  154. begin
  155. Result := [paDialog, paRevertable];
  156. end;
  157. //==========================================================
  158. //============ TSortFieldProperty
  159. //==========================================================
  160. type
  161. TSortFieldProperty = class(TStringProperty)
  162. public
  163. function GetAttributes: TPropertyAttributes; override;
  164. procedure GetValues(Proc: TGetStrProc); override;
  165. end;
  166. function TSortFieldProperty.GetAttributes: TPropertyAttributes; {override;}
  167. begin
  168. Result := [paValueList, paSortList, paRevertable];
  169. end;
  170. procedure TSortFieldProperty.GetValues(Proc: TGetStrProc);
  171. var
  172. IndexDef: TDbfIndexDef;
  173. Indexes: TDbfIndexDefs;
  174. Dbf: TDbf;
  175. I: integer;
  176. begin
  177. IndexDef := GetComponent(0) as TDbfIndexDef;
  178. Indexes := TDbfIndexDefs(IndexDef.Collection);
  179. Dbf := TDbf(Indexes.FOwner);
  180. for I := 0 to Dbf.FieldCount-1 do
  181. begin
  182. Proc(Dbf.Fields[i].FieldName);
  183. end;
  184. end;
  185. //==========================================================
  186. //============ TIndexNameProperty
  187. //==========================================================
  188. type
  189. TIndexNameProperty = class(TStringProperty)
  190. public
  191. function GetAttributes: TPropertyAttributes; override;
  192. procedure GetValues(Proc: TGetStrProc); override;
  193. procedure SetValue(const Value: string); override;
  194. function GetValue: string; override;
  195. end;
  196. function TIndexNameProperty.GetAttributes: TPropertyAttributes; {override;}
  197. begin
  198. Result := [paValueList, paRevertable];
  199. end;
  200. procedure TIndexNameProperty.GetValues(Proc: TGetStrProc);
  201. var
  202. Dbf: TDbf;
  203. I: Integer;
  204. begin
  205. Dbf := GetComponent(0) as TDbf;
  206. Dbf.UpdateIndexDefs;
  207. for I := 0 to Dbf.Indexes.Count - 1 do
  208. Proc(Dbf.Indexes[I].IndexFile);
  209. end;
  210. procedure TIndexNameProperty.SetValue(const Value: string); {override}
  211. var
  212. Dbf: TDbf;
  213. begin
  214. Dbf := GetComponent(0) as TDbf;
  215. Dbf.IndexName := Value;
  216. end;
  217. function TIndexNameProperty.GetValue: string; {override;}
  218. var
  219. Dbf: TDbf;
  220. begin
  221. Dbf := GetComponent(0) as TDbf;
  222. Result := Dbf.IndexName;
  223. end;
  224. //==========================================================
  225. //============ TVersionProperty
  226. //==========================================================
  227. type
  228. TVersionProperty = class(TStringProperty)
  229. public
  230. procedure Edit; override;
  231. function GetAttributes: TPropertyAttributes; override;
  232. end;
  233. procedure TVersionProperty.Edit; {override;}
  234. begin
  235. ShowMessage(
  236. Format(STRING_VERSION,[TDBF_MAJOR_VERSION, TDBF_MINOR_VERSION]) +
  237. ' : a dBase component'+#13+
  238. 'for Delphi and c++ builder with no BDE.'+#13+
  239. #13 +
  240. 'To get the latest version, please visit'+#13+
  241. 'the website: http://www.tdbf.net'+#13+
  242. 'or SourceForge: http://tdbf.sf.net');
  243. end;
  244. function TVersionProperty.GetAttributes: TPropertyAttributes; {override;}
  245. begin
  246. Result := [paDialog, paReadOnly, paRevertable];
  247. end;
  248. //==========================================================
  249. //============ TNativeFieldTypeProperty
  250. //==========================================================
  251. type
  252. TNativeFieldTypeProperty = class(TCharProperty)
  253. public
  254. function GetAttributes: TPropertyAttributes; override;
  255. procedure GetValues(Proc: TGetStrProc); override;
  256. procedure SetValue(const Value: string); override;
  257. end;
  258. procedure TNativeFieldTypeProperty.SetValue(const Value: string);
  259. var
  260. L: Longint;
  261. begin
  262. if Length(Value) = 0 then L := 0 else
  263. if Value[1] = '#' then L := StrToInt(Copy(Value, 2, Maxint))
  264. else L := Ord(Value[1]);
  265. SetOrdValue(L);
  266. end;
  267. function TNativeFieldTypeProperty.GetAttributes: TPropertyAttributes; {override;}
  268. begin
  269. result := [paRevertable,paValueList];
  270. end;
  271. procedure TNativeFieldTypeProperty.GetValues(Proc: TGetStrProc);
  272. begin
  273. Proc('C Character');
  274. Proc('N Numeric');
  275. Proc('D Date');
  276. Proc('L Logical');
  277. Proc('M Memo');
  278. Proc('B Blob');
  279. Proc('F Float');
  280. Proc('O Double');
  281. Proc('I Integer');
  282. Proc('G Graphic');
  283. Proc('+ AutoIncrement');
  284. Proc('@ DateTime');
  285. end;
  286. //==========================================================
  287. //============ initialization
  288. //==========================================================
  289. function IDE_DbfDefaultPath:string;
  290. begin
  291. {$ifndef FPC}
  292. if ToolServices<>nil then
  293. Result := ExtractFilePath(ToolServices.GetProjectName)
  294. else
  295. {$endif}
  296. Result := GetCurrentDir
  297. end;
  298. {$ifdef FPC}
  299. procedure RegisterUnitDbf;
  300. {$else}
  301. procedure Register;
  302. {$endif}
  303. begin
  304. Dbf.DbfBasePath := IDE_DbfDefaultPath;
  305. RegisterComponents('Data Access', [TDbf]);
  306. // RegisterPropertyEditor(TypeInfo(string), TDbf, 'FilePath', TFilePathProperty);
  307. RegisterPropertyEditor(TypeInfo(string), TDbf, 'TableName', TTableNameProperty);
  308. RegisterPropertyEditor(TypeInfo(string), TDbf, 'Version', TVersionProperty);
  309. RegisterPropertyEditor(TypeInfo(string), TDbf, 'IndexName', TIndexNameProperty);
  310. RegisterPropertyEditor(TypeInfo(string), TDbfIndexDef, 'IndexFile', TIndexFileNameProperty);
  311. RegisterPropertyEditor(TypeInfo(string), TDbfIndexDef, 'SortField', TSortFieldProperty);
  312. RegisterPropertyEditor(TypeInfo(char), TDbfFieldDef, 'NativeFieldType', TNativeFieldTypeProperty);
  313. end;
  314. {$ifdef FPC}
  315. procedure Register;
  316. begin
  317. RegisterUnit('Dbf', @RegisterUnitDbf);
  318. end;
  319. {$endif}
  320. {$ifdef FPC}
  321. initialization
  322. {$i tdbf.lrs}
  323. {$endif}
  324. end.