home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / visedit / vinifile.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-05-12  |  9.6 KB  |  417 lines

  1. unit VIniFile;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   ComCtrls,Commctrl, menus, IniFiles, ImgList;
  8.  
  9. const
  10.    iiSelected = 0;
  11.    iiRoot     = 1;
  12.    iiSection  = 2;
  13.    iiValue    = 3;
  14.    iiDelete   = 4;
  15.    iiRename   = 5;
  16.    iiExpand     = 6;
  17.    defSectionName = '<New Section>';
  18.    defValueName     = '<New Key>';
  19.  
  20. type
  21. TEditKind = (ekNone, ekNewSection, ekNewValue, ekDelete, ekRename);
  22. TNodeDeleteEvent = procedure (Sender:TObject;
  23.                               Node:TTreeNode; var Ok:boolean) of object;
  24.  
  25.   TVIniFile = class(TTreeView)
  26.   private
  27.     { Private declarations }
  28.     FIniFile:TIniFile;
  29.     //FEditing:boolean;
  30.     FEditAction:TEditKind;
  31.     FFileName:string;
  32.     FMnuRead:TMenuItem;
  33.     FMnuSection:TMenuItem;
  34.     FMnuKey:TMenuItem;
  35.     FMnuRename:TMenuItem;
  36.     FMnuDelete:TMenuItem;
  37.     FMnuExpand:TMenuItem;
  38.     FOnDeleteNode:TNodeDeleteEvent;
  39.  
  40.     procedure SetFileName(Val:TFileName);
  41.     function GetFileName:TFileName;
  42.     function GetValue:string;
  43.     procedure SetValue(Val:string);
  44.  
  45.   protected
  46.     { Protected declarations }
  47.     procedure CreateIniTree;
  48.     procedure DoDeleteNode(Sender:TObject);
  49.     procedure DoAddSection(Sender:TObject);
  50.     procedure DoAddNewKey(Sender:TObject);
  51.     procedure DoRenameNode(Sender:TObject);
  52.     procedure EditIniFile(Sender: TObject;Node: TTreeNode; var S: String);
  53.     property IniFile:TIniFile read FIniFile;
  54.   public
  55.     { Public declarations }
  56.     function PopupAddItem(Txt:string;ImIndex:integer;Action:TNotifyEvent):TMenuItem;
  57.     procedure NewIniFile(aName:string);
  58.     procedure SetEditMode(Sender:TObject);
  59.     procedure SetExpandMode(Sender:TObject);
  60.     procedure SetupPopup(Sender:TObject);
  61.     procedure DoRenameSection(Node:TTreeNode;S:string);
  62.     procedure DoRenameKey(Node:TTreeNode;S:string);
  63.     property Value:string read GetValue write SetValue;
  64.     procedure AddSection(aName:string);
  65.     procedure AddNewKey(Node:TTreeNode;S:string);
  66.     procedure RenameNode(Node:Ttreenode;S:string);
  67.     procedure DeleteNode(Node:TTreeNode);
  68.     constructor Create(AOwner:TComponent);override;
  69.     destructor Destroy;override;
  70.   published
  71.     { Published declarations }
  72.     property FileName:TFileName read GetFileName write SetFileName;
  73.     property OnDeleteNode:TNodeDeleteEvent read FOnDeleteNode
  74.                                            write FOnDeleteNode;
  75.   end;
  76.  
  77. procedure Register;
  78.  
  79. implementation
  80.  
  81. {$R *.DCR}
  82. procedure TVIniFile.NewIniFile(aName:string);
  83. var Fs:TFileStream;
  84. begin
  85. Fs:=TFileStream.Create(aName,fmCreate);
  86. try
  87. Fs.Write(#10#13,2);
  88. finally
  89. Fs.Free;
  90. end;
  91. FileName:=aName;
  92. end;
  93.  
  94. procedure TVIniFile.EditIniFile(Sender: TObject;
  95.                       Node: TTreeNode; var S: String);
  96. begin
  97. case FEditAction of
  98.    ekNewSection:AddSection(S);
  99.    ekNewValue:AddNewKey(Node,S);
  100.    ekDelete:DeleteNode(Node);
  101.    ekRename:RenameNode(Node,S);
  102.    end;
  103. FEditAction:=ekNone;
  104. end;
  105.  
  106. procedure TVIniFile.DoRenameNode(Sender:TObject);
  107. begin
  108. if ReadOnly OR (Selected.Level=0) then Exit;
  109. FEditAction:=ekRename;
  110. Selected.EditText;
  111. end;
  112.  
  113. procedure TVIniFile.DoRenameSection(Node:TTreeNode;S:string);
  114. var SL:TStringList;
  115. begin
  116. SL:=TStringList.Create;
  117. try
  118. with SL do
  119.   begin
  120.   LoadFromFile(FFileName);
  121.   Strings[IndexOf('['+Node.Text+']')]:='['+S+']';
  122.   SaveToFile(FFileName);
  123.   end;
  124. finally
  125. SL.Free;
  126. end;
  127. end;
  128.  
  129. procedure TVIniFile.DoRenameKey(Node:TTreeNode;S:string);
  130. var tv:string;
  131. begin
  132. Node.Selected:=True;
  133. tv:=Value;
  134. FIniFile:=TIniFile.Create(FFileName);
  135. try
  136.  FIniFile.DeleteKey(Node.Parent.Text,Node.Text);
  137.  FIniFile.WriteString(Node.Parent.Text,S,tv);
  138. finally
  139. FIniFile.Free;
  140. end;
  141. end;
  142.  
  143.  
  144.  
  145. procedure TVIniFile.RenameNode(Node:Ttreenode; S:string);
  146. begin
  147. case Node.Level of
  148.   1:DoRenameSection(Node,S);
  149.   2:DoRenameKey(Node,S);
  150.   end;
  151. end;
  152.  
  153.  
  154. procedure TVIniFile.DoAddNewKey(Sender:TObject);
  155. var Node:TTreeNode;
  156. begin
  157. if ReadOnly OR (Selected.Level<>1) then Exit;
  158. Node:=Items.AddChildFirst(Selected,defValueName);
  159. FEditAction:=ekNewValue;
  160. Selected.Expand(False);
  161. Node.EditText;
  162. end;
  163.  
  164.  
  165. procedure TVIniFile.AddNewKey(Node:TTreeNode;S:string);
  166. begin
  167. FIniFile:=TIniFile.Create(FFileName);
  168. try
  169.  FIniFile.WriteString(Node.Parent.Text,S,'');
  170. finally
  171. FIniFile.Free;
  172. end;
  173. end;
  174.  
  175.  
  176. procedure TVIniFile.DoAddSection(Sender:TObject);
  177. var Node:TTreeNode;
  178. begin
  179. if ReadOnly then Exit;
  180. Node:=Items.AddChildFirst(Items[0],defSectionName);
  181. FEditAction:=ekNewSection;
  182. Items[0].expand(False);
  183. Node.EditText;
  184. end;
  185.  
  186.  
  187.  
  188.  
  189. procedure TVIniFile.AddSection(aName:string);
  190. var  SL:TStringList;
  191. begin
  192. SL:=TStringList.Create;
  193. try
  194. with SL do
  195.   begin
  196.    LoadFromFile(FFileName);
  197.    Insert(0,'['+aName+']');
  198.    SaveToFile(FFileName);
  199.   end;
  200. finally
  201. SL.Free;
  202. end;
  203. end;
  204.  
  205. procedure TVIniFile.DoDeleteNode(Sender:TObject);
  206. var S:string;
  207. begin
  208. FEditAction:=ekDelete;
  209. EditIniFile(Sender,Selected,S);
  210. end;
  211.  
  212. procedure TVIniFile.DeleteNode(Node:TTreeNode);
  213. var Ok:boolean;
  214. begin
  215. Ok:=not ReadOnly;
  216. FIniFile:=TIniFile.Create(FFileName);
  217. try
  218. if Assigned(FOnDeleteNode) then OnDeleteNode(Self,Node,Ok);
  219. if Ok then
  220.   begin
  221.   case Node.Level of
  222.      1:begin
  223.         FIniFile.EraseSection(Node.Text);
  224.         Items.Delete(Node);
  225.         end;
  226.      2:begin
  227.         FIniFile.DeleteKey(Node.Parent.Text,Node.Text);
  228.         Items.Delete(Node);
  229.         end;
  230.      end;
  231.   end;
  232. finally
  233. FIniFile.Free;
  234. end;
  235. end;
  236.  
  237. function TVIniFile.GetValue:string;
  238. begin
  239. Result:='';
  240. if Selected.Level=2 then
  241.   begin
  242.   FIniFile:= TIniFile.Create(FFileName);
  243.   try
  244.   Result:=FIniFile.ReadString(Selected.Parent.Text,Selected.Text,'');
  245.   finally
  246.   FIniFile.Free;
  247.   end;
  248.   end;
  249. end;
  250.  
  251. procedure TVIniFile.SetValue(Val:string);
  252. begin
  253. if Assigned(Selected) then
  254.   with Selected do
  255.     if (not ReadOnly) AND (Level=2) then
  256.       begin
  257.        FIniFile:= TIniFile.Create(FFileName);
  258.        try
  259.        FIniFile.WriteString(Parent.Text,Text, Val);
  260.        finally
  261.         FIniFile.Free;
  262.        end;
  263.       end;
  264. end;
  265.  
  266. procedure TVIniFile.CreateIniTree;
  267. var S,K:TStringList;
  268.     i,j:integer;
  269.     N,Node,VNode:TTreeNode;
  270. begin
  271. Items.BeginUpdate;
  272. FIniFile:=TIniFile.Create(FFileName);
  273. N:=Items.Add(nil,ExtractFileName(FiniFile.FileName));
  274. N.ImageIndex:=iiRoot;
  275. N.SelectedIndex:=iiSelected;
  276. N.Expand(False);
  277. try
  278.     S:=TStringList.Create;
  279.     K:=TStringList.Create;
  280.     try
  281.     FIniFile.ReadSections(S);
  282.     for i:=0 to S.Count-1 do
  283.       begin
  284.       Node:=Items.AddChild(N,S[i]);
  285.       Node.ImageIndex:=iiSection;
  286.       Node.SelectedIndex:=iiSelected;
  287.       K.Clear;
  288.       FIniFile.ReadSection(S[i],K);
  289.       for j:=0 to K.Count-1 do
  290.         begin
  291.          VNode:=Items.AddChild(Node,K[j]);
  292.          VNode.ImageIndex:=iiValue;
  293.          VNode.SelectedIndex:=iiSelected;
  294.         end;
  295.       end;
  296.     N.Expand(False);
  297.     finally
  298.       K.Free;
  299.       S.Free;
  300.       if Assigned(Items[0]) then Selected:=Items[0];
  301.     end;
  302.   finally
  303.    FIniFile.Free;
  304.   end;
  305. Items.EndUpdate;
  306. end;
  307.  
  308. procedure TVIniFile.SetFileName(Val:TfileName);
  309. begin
  310. AutoExpand:=False;
  311. Items.Clear;
  312. FFileName:=Val;
  313. if FileExists(FFileName) then CreateIniTree;
  314. end;
  315.  
  316. function TVIniFile.GetFileName:TFileName;
  317. begin
  318. Result:=FFileName;
  319. end;
  320.  
  321. procedure TVIniFile.SetEditMode(Sender:TObject);
  322. begin
  323. ReadOnly:=not ReadOnly;
  324. FMnuRead.Checked:=ReadOnly;
  325. end;
  326.  
  327. procedure TVIniFile.SetExpandMode(Sender:TObject);
  328. begin
  329. AutoExpand:=not AutoExpand;
  330. end;
  331.  
  332. procedure TVIniFile.SetupPopup(Sender:TObject);
  333. begin
  334. FMnuRead.Checked:=ReadOnly;
  335. FMnuExpand.Checked:=AutoExpand;
  336. FMnuSection.Enabled:=False;
  337. FMnuKey.Enabled:=False;
  338. FMnuRename.Enabled:=False;
  339. FMnuDelete.Enabled:=False;
  340. if not ReadOnly then
  341. case Selected.Level of
  342.   0:begin
  343.     FMnuSection.Enabled:=True;
  344.     end;
  345.   1:begin
  346.     FMnuKey.Enabled:=True;
  347.     FMnuSection.Enabled:=True;
  348.     FMnuRename.Enabled:=True;
  349.     FMnuDelete.Enabled:=True;
  350.     end;
  351.   2:begin
  352.     FMnuSection.Enabled:=True;
  353.     FMnuRename.Enabled:=True;
  354.     FMnuDelete.Enabled:=True;
  355.     end;
  356.   end;
  357. end;
  358.  
  359. function TVIniFile.PopupAddItem(Txt:string;ImIndex:integer;
  360.                                 Action:TNotifyEvent):TMenuItem;
  361. begin
  362. Result:=TMenuItem.Create(PopupMenu);
  363. PopupMenu.Items.Add(Result);
  364. Result.Caption:=Txt;
  365. Result.OnClick:=Action;
  366. Result.ImageIndex:=ImIndex;
  367. end;
  368.  
  369. constructor TVIniFile.Create(AOwner:TComponent);
  370. begin
  371. inherited Create(AOwner);
  372. PopupMenu:=TPopupMenu.Create(Self);
  373. PopupMenu.OnPopup:=SetupPopup;
  374. FMnuRead:=PopupAddItem('Read Only',-1,SetEditMode);
  375. FMnuRead.Checked:=True;
  376. FMnuExpand:=PopupAddItem('Expand Selection',iiExpand,SetExpandMode);
  377. FMnuExpand.Checked:=AutoExpand;
  378. PopupAddItem('-',-1,nil);
  379. FMnuSection:=PopupAddItem('New Section ',iiSection, DoAddSection);
  380. FMnuKey:=PopupAddItem('New Key ',iiValue, DoAddNewKey);
  381. PopupAddItem('-',-1,nil);
  382. FMnuRename:=PopupAddItem('Rename ',iiRename, DoRenameNode);
  383. FMnuDelete:=PopupAddItem('Delete',iiDelete, DoDeleteNode);
  384. Images:=TImageList.Create(Self);
  385. PopupMenu.Images:=Images;
  386.   with Images do
  387.     begin
  388.       ResourceLoad(rtBitmap,'INISELECTED',clWhite);
  389.       ResourceLoad(rtBitmap,'INIROOT',clWhite);
  390.       ResourceLoad(rtBitmap,'INISECTION',clWhite);
  391.       ResourceLoad(rtBitmap,'INIVALUE',clWhite);
  392.       ResourceLoad(rtBitmap,'MNUDELETE',clBtnFace);
  393.       ResourceLoad(rtBitmap,'MNURENAME',clBtnFace);
  394.       ResourceLoad(rtBitmap,'MNUAUTOEXPAND',clBtnFace);
  395.     end;
  396. OnEdited:=EditIniFile;
  397. end;
  398.  
  399.  
  400. destructor TVIniFile.Destroy;
  401. begin
  402. PopupMenu.Free;
  403. PopupMenu:=nil;
  404. Images.Free;
  405. Images:=nil;
  406. inherited Destroy;
  407. end;
  408.  
  409.  
  410.  
  411. procedure Register;
  412. begin
  413.   RegisterComponents('VisualRegistry', [TVIniFile]);
  414. end;
  415.  
  416. end.
  417.