1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
unit inieditor;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls,
Graphics, Dialogs, StdCtrls, EditBtn, ExtCtrls,
IniFiles, lazutf8, SynEdit,SynMemo,{SynEditTypes,}SynHighlighterIni, SynEditTypes;
type
{ TFormIniEditor }
TFormIniEditor = class(TForm)
GUITimer: TIdleTimer;
Label1: TLabel;
OKButton: TButton;
CancelButton: TButton;
ProfileSelect: TComboBox;
FileNameEdit: TFileNameEdit;
INIFileLabel: TLabel;
ProfileLabel: TLabel;
SynIniHighlighter: TSynIniSyn;
SynMemo: TSynMemo;
procedure CancelButtonClick(Sender: TObject);
procedure FileNameEditAcceptFileName(Sender: TObject; var Value: String);
procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormDropFiles(Sender: TObject; const FileNames: array of String);
procedure FormShow(Sender: TObject);
procedure GUITimerTimer(Sender: TObject);
procedure ProfileSelectSelect(Sender: TObject);
procedure SynMemoStatusChange(Sender: TObject; Changes: TSynStatusChanges);
private
FIniFilename: string;
FIniStream: TMemoryStream;
FMustReloadProfileSelect: boolean;
FSaveChanges: boolean; //If off, don't save any changes (e.g. when canceling form)
FProfileSelectKey: string;
FProfileSelectSection: string;
FProfileSelectValue: string;
procedure LoadGUI(KeepCursorPosition: boolean; LoadSynmemo: boolean; LoadProfileselect: boolean); //Loads GUI elements from backing stream
procedure SetIniFilename(AValue: string); //Loads file into stream
procedure SetProfileSelectKey(AValue: string);
procedure SetProfileSelectSection(AValue: string);
// Saves synmemo contents to memory stream and optionally to file, too
procedure SynMemoSave(AlsoToFile: boolean);
{ private declarations }
public
// File to be edited/viewed:
property INIFile: string read FIniFilename write SetIniFilename;
// Section name where profile selection is to be stored. If nothing, the root/top level will be used
property ProfileSelectSection: string read FProfileSelectSection write SetProfileSelectSection;
// Key to be used for storing selected profile
// If empty, don't use the select profile combobox
property ProfileSelectKey: string read FProfileSelectKey write SetProfileSelectKey;
// The value the user selected
property ProfileSelectValue: string read FProfileSelectValue;
end;
var
Form1: TFormIniEditor;
implementation
{$R *.lfm}
{ TFormIniEditor }
procedure TFormIniEditor.FormDropFiles(Sender: TObject;
const FileNames: array of String);
begin
// Go through property for reload file+reload GUI code
IniFile:=FileNames[0];
end;
procedure TFormIniEditor.FormShow(Sender: TObject);
begin
if FileNameEdit.FileName='' then
FileNameEdit.FileName:=ExpandFileNameUTF8(FIniFilename);
end;
procedure TFormIniEditor.GUITimerTimer(Sender: TObject);
begin
if FMustReloadProfileSelect then
LoadGUI(true,false,true);
end;
procedure TFormIniEditor.ProfileSelectSelect(Sender: TObject);
// Save selected profile
var
MyIniFile: TIniFile;
begin
FProfileSelectValue:=ProfileSelect.Text;
if FProfileSelectValue<>'' then
begin
// Save any changes in synmemo
SynMemoSave(false);
FIniStream.Position:=0; //Load from beginning
MyIniFile := TIniFile.Create(FIniStream, True);
FIniStream.Position:=0; //Needed to save again
try
MyIniFile.WriteString(FProfileSelectSection,FProfileSelectKey,FProfileSelectValue);
finally
MyIniFile.Free;
end;
LoadGUI(true,true,true);
end;
end;
procedure TFormIniEditor.SynMemoStatusChange(Sender: TObject;
Changes: TSynStatusChanges);
begin
// If user edits the profile name directly or edits/deletes/adds
// one of the section names, the combobox will need to be refreshed.
// Pass this on to a timer so it gets done without disturbing the updates
if (FMustReloadProfileSelect=false) and (scModified in Changes) then
begin
//todo: test for section line or key=value
FMustReloadProfileSelect:=true;
end;
end;
procedure TFormIniEditor.LoadGUI(KeepCursorPosition: boolean; LoadSynmemo: boolean; LoadProfileselect: boolean);
var
MyIniFile : TIniFile;
OldPos: TPoint;
ProfIndex: integer;
Sections: TStringList;
begin
SynMemo.BeginUpdate(false);
if LoadSynMemo then
begin
OldPos:=SynMemo.CaretXY;
FIniStream.Position:=0; //Load from beginning
SynMemo.Lines.LoadFromStream(FIniStream);
end;
FIniStream.Position:=0; //Load from beginning
MyIniFile := TIniFile.Create(FIniStream, True);
FIniStream.Position:=0; //In case we want to save
Sections:=TStringList.Create;
try
if LoadProfileselect then
begin
Sections.Sorted:=true;
MyIniFile.ReadSections(Sections);
// Now take out the one where the profile is stored as it will confuse users
if Sections.Find(FProfileSelectSection, ProfIndex) then
Sections.Delete(ProfIndex);
ProfileSelect.Clear;
ProfileSelect.Items.AddStrings(Sections);
FProfileSelectValue:=MyIniFile.ReadString(FProfileSelectSection,FProfileSelectKey,'');
ProfileSelect.Text:=FProfileSelectValue;
FMustReloadProfileSelect:=false;
end;
// restore cursor/caret position
if LoadSynMemo and KeepCursorPosition then
SynMemo.CaretXY:=OldPos;
finally
MyIniFile.Free;
Sections.Free;
SynMemo.EndUpdate;
end;
end;
procedure TFormIniEditor.SetIniFilename(AValue: string);
begin
if FIniFilename=AValue then Exit;
FIniFilename:=AValue;
if FIniFileName<>'' then
begin
try
FIniStream.Clear;
FIniStream.LoadFromFile(FIniFilename);
except
on E: Exception do
ShowMessage('Error loading file '+FIniFilename+': '+E.Message);
end;
LoadGUI(false,true,true);
end;
end;
procedure TFormIniEditor.SetProfileSelectKey(AValue: string);
begin
if FProfileSelectKey=AValue then Exit;
FProfileSelectKey:=AValue;
if (FProfileSelectKey<>'') and (FProfileSelectSection<>'') then
LoadGUI(false,true,true);
end;
procedure TFormIniEditor.SetProfileSelectSection(AValue: string);
begin
if FProfileSelectSection=AValue then Exit;
FProfileSelectSection:=AValue;
if (FProfileSelectKey<>'') and (FProfileSelectSection<>'') then
LoadGUI(false,true,true);
end;
procedure TFormIniEditor.SynMemoSave(AlsoToFile: boolean);
begin
if FSaveChanges then
begin
FIniStream.Clear;
FIniStream.Position:=0; //Save from beginning
SynMemo.Lines.SaveToStream(FiniStream);
FIniStream.Position:=0; //Save from beginning
if AlsoToFile and (FIniFileName<>'') then
FIniStream.SaveToFile(FIniFilename);
end;
end;
procedure TFormIniEditor.CancelButtonClick(Sender: TObject);
begin
FSaveChanges:=false;
end;
procedure TFormIniEditor.FileNameEditAcceptFileName(Sender: TObject;
var Value: String);
begin
// go through the property to load new file
INIFile:=Value;
end;
procedure TFormIniEditor.FormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
// Save any uncommitted changes
//todo: only if dirty synmemo changestamp?; also check if this is best event
SynMemoSave(true);
end;
procedure TFormIniEditor.FormCreate(Sender: TObject);
begin
FIniStream:=TMemoryStream.Create;
FSaveChanges:=true; //allow editing
end;
procedure TFormIniEditor.FormDestroy(Sender: TObject);
begin
FIniStream.Free;
end;
end.
|