home *** CD-ROM | disk | FTP | other *** search
- unit VIniFile;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- ComCtrls,Commctrl, menus, IniFiles, ImgList;
-
- const
- iiSelected = 0;
- iiRoot = 1;
- iiSection = 2;
- iiValue = 3;
- iiDelete = 4;
- iiRename = 5;
- iiExpand = 6;
- defSectionName = '<New Section>';
- defValueName = '<New Key>';
-
- type
- TEditKind = (ekNone, ekNewSection, ekNewValue, ekDelete, ekRename);
- TNodeDeleteEvent = procedure (Sender:TObject;
- Node:TTreeNode; var Ok:boolean) of object;
-
- TVIniFile = class(TTreeView)
- private
- { Private declarations }
- FIniFile:TIniFile;
- //FEditing:boolean;
- FEditAction:TEditKind;
- FFileName:string;
- FMnuRead:TMenuItem;
- FMnuSection:TMenuItem;
- FMnuKey:TMenuItem;
- FMnuRename:TMenuItem;
- FMnuDelete:TMenuItem;
- FMnuExpand:TMenuItem;
- FOnDeleteNode:TNodeDeleteEvent;
-
- procedure SetFileName(Val:TFileName);
- function GetFileName:TFileName;
- function GetValue:string;
- procedure SetValue(Val:string);
-
- protected
- { Protected declarations }
- procedure CreateIniTree;
- procedure DoDeleteNode(Sender:TObject);
- procedure DoAddSection(Sender:TObject);
- procedure DoAddNewKey(Sender:TObject);
- procedure DoRenameNode(Sender:TObject);
- procedure EditIniFile(Sender: TObject;Node: TTreeNode; var S: String);
- property IniFile:TIniFile read FIniFile;
- public
- { Public declarations }
- function PopupAddItem(Txt:string;ImIndex:integer;Action:TNotifyEvent):TMenuItem;
- procedure NewIniFile(aName:string);
- procedure SetEditMode(Sender:TObject);
- procedure SetExpandMode(Sender:TObject);
- procedure SetupPopup(Sender:TObject);
- procedure DoRenameSection(Node:TTreeNode;S:string);
- procedure DoRenameKey(Node:TTreeNode;S:string);
- property Value:string read GetValue write SetValue;
- procedure AddSection(aName:string);
- procedure AddNewKey(Node:TTreeNode;S:string);
- procedure RenameNode(Node:Ttreenode;S:string);
- procedure DeleteNode(Node:TTreeNode);
- constructor Create(AOwner:TComponent);override;
- destructor Destroy;override;
- published
- { Published declarations }
- property FileName:TFileName read GetFileName write SetFileName;
- property OnDeleteNode:TNodeDeleteEvent read FOnDeleteNode
- write FOnDeleteNode;
- end;
-
- procedure Register;
-
- implementation
-
- {$R *.DCR}
- procedure TVIniFile.NewIniFile(aName:string);
- var Fs:TFileStream;
- begin
- Fs:=TFileStream.Create(aName,fmCreate);
- try
- Fs.Write(#10#13,2);
- finally
- Fs.Free;
- end;
- FileName:=aName;
- end;
-
- procedure TVIniFile.EditIniFile(Sender: TObject;
- Node: TTreeNode; var S: String);
- begin
- case FEditAction of
- ekNewSection:AddSection(S);
- ekNewValue:AddNewKey(Node,S);
- ekDelete:DeleteNode(Node);
- ekRename:RenameNode(Node,S);
- end;
- FEditAction:=ekNone;
- end;
-
- procedure TVIniFile.DoRenameNode(Sender:TObject);
- begin
- if ReadOnly OR (Selected.Level=0) then Exit;
- FEditAction:=ekRename;
- Selected.EditText;
- end;
-
- procedure TVIniFile.DoRenameSection(Node:TTreeNode;S:string);
- var SL:TStringList;
- begin
- SL:=TStringList.Create;
- try
- with SL do
- begin
- LoadFromFile(FFileName);
- Strings[IndexOf('['+Node.Text+']')]:='['+S+']';
- SaveToFile(FFileName);
- end;
- finally
- SL.Free;
- end;
- end;
-
- procedure TVIniFile.DoRenameKey(Node:TTreeNode;S:string);
- var tv:string;
- begin
- Node.Selected:=True;
- tv:=Value;
- FIniFile:=TIniFile.Create(FFileName);
- try
- FIniFile.DeleteKey(Node.Parent.Text,Node.Text);
- FIniFile.WriteString(Node.Parent.Text,S,tv);
- finally
- FIniFile.Free;
- end;
- end;
-
-
-
- procedure TVIniFile.RenameNode(Node:Ttreenode; S:string);
- begin
- case Node.Level of
- 1:DoRenameSection(Node,S);
- 2:DoRenameKey(Node,S);
- end;
- end;
-
-
- procedure TVIniFile.DoAddNewKey(Sender:TObject);
- var Node:TTreeNode;
- begin
- if ReadOnly OR (Selected.Level<>1) then Exit;
- Node:=Items.AddChildFirst(Selected,defValueName);
- FEditAction:=ekNewValue;
- Selected.Expand(False);
- Node.EditText;
- end;
-
-
- procedure TVIniFile.AddNewKey(Node:TTreeNode;S:string);
- begin
- FIniFile:=TIniFile.Create(FFileName);
- try
- FIniFile.WriteString(Node.Parent.Text,S,'');
- finally
- FIniFile.Free;
- end;
- end;
-
-
- procedure TVIniFile.DoAddSection(Sender:TObject);
- var Node:TTreeNode;
- begin
- if ReadOnly then Exit;
- Node:=Items.AddChildFirst(Items[0],defSectionName);
- FEditAction:=ekNewSection;
- Items[0].expand(False);
- Node.EditText;
- end;
-
-
-
-
- procedure TVIniFile.AddSection(aName:string);
- var SL:TStringList;
- begin
- SL:=TStringList.Create;
- try
- with SL do
- begin
- LoadFromFile(FFileName);
- Insert(0,'['+aName+']');
- SaveToFile(FFileName);
- end;
- finally
- SL.Free;
- end;
- end;
-
- procedure TVIniFile.DoDeleteNode(Sender:TObject);
- var S:string;
- begin
- FEditAction:=ekDelete;
- EditIniFile(Sender,Selected,S);
- end;
-
- procedure TVIniFile.DeleteNode(Node:TTreeNode);
- var Ok:boolean;
- begin
- Ok:=not ReadOnly;
- FIniFile:=TIniFile.Create(FFileName);
- try
- if Assigned(FOnDeleteNode) then OnDeleteNode(Self,Node,Ok);
- if Ok then
- begin
- case Node.Level of
- 1:begin
- FIniFile.EraseSection(Node.Text);
- Items.Delete(Node);
- end;
- 2:begin
- FIniFile.DeleteKey(Node.Parent.Text,Node.Text);
- Items.Delete(Node);
- end;
- end;
- end;
- finally
- FIniFile.Free;
- end;
- end;
-
- function TVIniFile.GetValue:string;
- begin
- Result:='';
- if Selected.Level=2 then
- begin
- FIniFile:= TIniFile.Create(FFileName);
- try
- Result:=FIniFile.ReadString(Selected.Parent.Text,Selected.Text,'');
- finally
- FIniFile.Free;
- end;
- end;
- end;
-
- procedure TVIniFile.SetValue(Val:string);
- begin
- if Assigned(Selected) then
- with Selected do
- if (not ReadOnly) AND (Level=2) then
- begin
- FIniFile:= TIniFile.Create(FFileName);
- try
- FIniFile.WriteString(Parent.Text,Text, Val);
- finally
- FIniFile.Free;
- end;
- end;
- end;
-
- procedure TVIniFile.CreateIniTree;
- var S,K:TStringList;
- i,j:integer;
- N,Node,VNode:TTreeNode;
- begin
- Items.BeginUpdate;
- FIniFile:=TIniFile.Create(FFileName);
- N:=Items.Add(nil,ExtractFileName(FiniFile.FileName));
- N.ImageIndex:=iiRoot;
- N.SelectedIndex:=iiSelected;
- N.Expand(False);
- try
- S:=TStringList.Create;
- K:=TStringList.Create;
- try
- FIniFile.ReadSections(S);
- for i:=0 to S.Count-1 do
- begin
- Node:=Items.AddChild(N,S[i]);
- Node.ImageIndex:=iiSection;
- Node.SelectedIndex:=iiSelected;
- K.Clear;
- FIniFile.ReadSection(S[i],K);
- for j:=0 to K.Count-1 do
- begin
- VNode:=Items.AddChild(Node,K[j]);
- VNode.ImageIndex:=iiValue;
- VNode.SelectedIndex:=iiSelected;
- end;
- end;
- N.Expand(False);
- finally
- K.Free;
- S.Free;
- if Assigned(Items[0]) then Selected:=Items[0];
- end;
- finally
- FIniFile.Free;
- end;
- Items.EndUpdate;
- end;
-
- procedure TVIniFile.SetFileName(Val:TfileName);
- begin
- AutoExpand:=False;
- Items.Clear;
- FFileName:=Val;
- if FileExists(FFileName) then CreateIniTree;
- end;
-
- function TVIniFile.GetFileName:TFileName;
- begin
- Result:=FFileName;
- end;
-
- procedure TVIniFile.SetEditMode(Sender:TObject);
- begin
- ReadOnly:=not ReadOnly;
- FMnuRead.Checked:=ReadOnly;
- end;
-
- procedure TVIniFile.SetExpandMode(Sender:TObject);
- begin
- AutoExpand:=not AutoExpand;
- end;
-
- procedure TVIniFile.SetupPopup(Sender:TObject);
- begin
- FMnuRead.Checked:=ReadOnly;
- FMnuExpand.Checked:=AutoExpand;
- FMnuSection.Enabled:=False;
- FMnuKey.Enabled:=False;
- FMnuRename.Enabled:=False;
- FMnuDelete.Enabled:=False;
- if not ReadOnly then
- case Selected.Level of
- 0:begin
- FMnuSection.Enabled:=True;
- end;
- 1:begin
- FMnuKey.Enabled:=True;
- FMnuSection.Enabled:=True;
- FMnuRename.Enabled:=True;
- FMnuDelete.Enabled:=True;
- end;
- 2:begin
- FMnuSection.Enabled:=True;
- FMnuRename.Enabled:=True;
- FMnuDelete.Enabled:=True;
- end;
- end;
- end;
-
- function TVIniFile.PopupAddItem(Txt:string;ImIndex:integer;
- Action:TNotifyEvent):TMenuItem;
- begin
- Result:=TMenuItem.Create(PopupMenu);
- PopupMenu.Items.Add(Result);
- Result.Caption:=Txt;
- Result.OnClick:=Action;
- Result.ImageIndex:=ImIndex;
- end;
-
- constructor TVIniFile.Create(AOwner:TComponent);
- begin
- inherited Create(AOwner);
- PopupMenu:=TPopupMenu.Create(Self);
- PopupMenu.OnPopup:=SetupPopup;
- FMnuRead:=PopupAddItem('Read Only',-1,SetEditMode);
- FMnuRead.Checked:=True;
- FMnuExpand:=PopupAddItem('Expand Selection',iiExpand,SetExpandMode);
- FMnuExpand.Checked:=AutoExpand;
- PopupAddItem('-',-1,nil);
- FMnuSection:=PopupAddItem('New Section ',iiSection, DoAddSection);
- FMnuKey:=PopupAddItem('New Key ',iiValue, DoAddNewKey);
- PopupAddItem('-',-1,nil);
- FMnuRename:=PopupAddItem('Rename ',iiRename, DoRenameNode);
- FMnuDelete:=PopupAddItem('Delete',iiDelete, DoDeleteNode);
- Images:=TImageList.Create(Self);
- PopupMenu.Images:=Images;
- with Images do
- begin
- ResourceLoad(rtBitmap,'INISELECTED',clWhite);
- ResourceLoad(rtBitmap,'INIROOT',clWhite);
- ResourceLoad(rtBitmap,'INISECTION',clWhite);
- ResourceLoad(rtBitmap,'INIVALUE',clWhite);
- ResourceLoad(rtBitmap,'MNUDELETE',clBtnFace);
- ResourceLoad(rtBitmap,'MNURENAME',clBtnFace);
- ResourceLoad(rtBitmap,'MNUAUTOEXPAND',clBtnFace);
- end;
- OnEdited:=EditIniFile;
- end;
-
-
- destructor TVIniFile.Destroy;
- begin
- PopupMenu.Free;
- PopupMenu:=nil;
- Images.Free;
- Images:=nil;
- inherited Destroy;
- end;
-
-
-
- procedure Register;
- begin
- RegisterComponents('VisualRegistry', [TVIniFile]);
- end;
-
- end.
-