1 | unit UFormMain;
|
---|
2 |
|
---|
3 | {$mode delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
|
---|
9 | Menus, ActnList, ComCtrls, UPersistentForm, ULastOpenedList, UGrammer,
|
---|
10 | XMLConf;
|
---|
11 |
|
---|
12 | type
|
---|
13 |
|
---|
14 | { TFormMain }
|
---|
15 |
|
---|
16 | TFormMain = class(TForm)
|
---|
17 | ABuildCompiler: TAction;
|
---|
18 | ASettings: TAction;
|
---|
19 | ALookupTables: TAction;
|
---|
20 | AToolbarVisible: TAction;
|
---|
21 | AProjectOpenRecent: TAction;
|
---|
22 | AProjectClose: TAction;
|
---|
23 | AGrammerSettings: TAction;
|
---|
24 | AProjectNew: TAction;
|
---|
25 | AProjectSave: TAction;
|
---|
26 | AExit: TAction;
|
---|
27 | AProjectOpen: TAction;
|
---|
28 | AProjectSaveAs: TAction;
|
---|
29 | ARules: TAction;
|
---|
30 | ActionList1: TActionList;
|
---|
31 | CoolBar1: TCoolBar;
|
---|
32 | LastOpenedList1: TLastOpenedList;
|
---|
33 | MainMenu1: TMainMenu;
|
---|
34 | MemoEbnf: TMemo;
|
---|
35 | MemoBnf: TMemo;
|
---|
36 | MenuItem1: TMenuItem;
|
---|
37 | MenuItem10: TMenuItem;
|
---|
38 | MenuItem11: TMenuItem;
|
---|
39 | MenuItem12: TMenuItem;
|
---|
40 | MenuItem13: TMenuItem;
|
---|
41 | MenuItem14: TMenuItem;
|
---|
42 | MenuItem15: TMenuItem;
|
---|
43 | MenuItem2: TMenuItem;
|
---|
44 | MenuItem3: TMenuItem;
|
---|
45 | MenuItemRecent: TMenuItem;
|
---|
46 | MenuItemToolbar: TMenuItem;
|
---|
47 | MenuItem4: TMenuItem;
|
---|
48 | MenuItem5: TMenuItem;
|
---|
49 | MenuItem6: TMenuItem;
|
---|
50 | MenuItem7: TMenuItem;
|
---|
51 | MenuItem8: TMenuItem;
|
---|
52 | MenuItem9: TMenuItem;
|
---|
53 | OpenDialog1: TOpenDialog;
|
---|
54 | PageControl1: TPageControl;
|
---|
55 | PopupMenuOpenRecent: TPopupMenu;
|
---|
56 | SaveDialog1: TSaveDialog;
|
---|
57 | TabSheetRules: TTabSheet;
|
---|
58 | TabSheetBnf: TTabSheet;
|
---|
59 | TabSheetEbnf: TTabSheet;
|
---|
60 | ToolBar1: TToolBar;
|
---|
61 | ToolButton1: TToolButton;
|
---|
62 | ToolButton2: TToolButton;
|
---|
63 | ToolButton3: TToolButton;
|
---|
64 | ToolButton4: TToolButton;
|
---|
65 | ToolButton5: TToolButton;
|
---|
66 | ToolButton6: TToolButton;
|
---|
67 | ToolButton7: TToolButton;
|
---|
68 | procedure ABuildCompilerExecute(Sender: TObject);
|
---|
69 | procedure ALookupTablesExecute(Sender: TObject);
|
---|
70 | procedure ASettingsExecute(Sender: TObject);
|
---|
71 | procedure AToolbarVisibleExecute(Sender: TObject);
|
---|
72 | procedure AExitExecute(Sender: TObject);
|
---|
73 | procedure AGrammerSettingsExecute(Sender: TObject);
|
---|
74 | procedure ARulesExecute(Sender: TObject);
|
---|
75 | procedure AProjectNewExecute(Sender: TObject);
|
---|
76 | procedure AProjectOpenExecute(Sender: TObject);
|
---|
77 | procedure AProjectOpenRecentExecute(Sender: TObject);
|
---|
78 | procedure AProjectCloseExecute(Sender: TObject);
|
---|
79 | procedure AProjectSaveAsExecute(Sender: TObject);
|
---|
80 | procedure AProjectSaveExecute(Sender: TObject);
|
---|
81 | procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
---|
82 | procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
|
---|
83 | procedure FormShow(Sender: TObject);
|
---|
84 | procedure LastOpenedList1Change(Sender: TObject);
|
---|
85 | private
|
---|
86 | procedure GrammerChange(Sender: TObject);
|
---|
87 | procedure GrammerDestroy(Sender: TObject);
|
---|
88 | public
|
---|
89 | procedure ReloadMemo;
|
---|
90 | procedure UpdateInterface;
|
---|
91 | procedure ProjectOpen(FileName: string);
|
---|
92 | procedure ProjectNew;
|
---|
93 | procedure LoadConfig(XmlConfig: TXmlConfig);
|
---|
94 | procedure SaveConfig(XmlConfig: TXmlConfig);
|
---|
95 | end;
|
---|
96 |
|
---|
97 | var
|
---|
98 | FormMain: TFormMain;
|
---|
99 |
|
---|
100 |
|
---|
101 | implementation
|
---|
102 |
|
---|
103 | {$R *.lfm}
|
---|
104 |
|
---|
105 | uses
|
---|
106 | UFormRules, UCore, UFormGrammer, UBuilder, UFormLookupTables, UFormSettings;
|
---|
107 |
|
---|
108 | resourcestring
|
---|
109 | SUnsavedChanges = 'Unsaved changes';
|
---|
110 | SDoYouWantToSaveProject = 'There are unsaved changes in project. Do you want to save them?';
|
---|
111 | SNewGrammer = 'New grammer.grm';
|
---|
112 |
|
---|
113 | { TFormMain }
|
---|
114 |
|
---|
115 | procedure TFormMain.ARulesExecute(Sender: TObject);
|
---|
116 | var
|
---|
117 | FormRules: TFormRules;
|
---|
118 | begin
|
---|
119 | FormRules := TFormRules.Create(nil);
|
---|
120 | FormRules.Rules := Core.Grammer.Rules;
|
---|
121 | FormRules.ShowModal;
|
---|
122 | ReloadMemo;
|
---|
123 | UpdateInterface;
|
---|
124 | FreeAndNil(FormRules);
|
---|
125 | end;
|
---|
126 |
|
---|
127 | procedure TFormMain.AProjectSaveAsExecute(Sender: TObject);
|
---|
128 | begin
|
---|
129 | SaveDialog1.InitialDir := ExtractFileDir(Core.Grammer.FileName);
|
---|
130 | SaveDialog1.FileName := ExtractFileName(Core.Grammer.FileName);
|
---|
131 | if SaveDialog1.Execute then begin
|
---|
132 | LastOpenedList1.AddItem(SaveDialog1.FileName);
|
---|
133 | Core.Grammer.SaveToXmlFile(SaveDialog1.FileName);
|
---|
134 | UpdateInterface;
|
---|
135 | end;
|
---|
136 | end;
|
---|
137 |
|
---|
138 | procedure TFormMain.AProjectSaveExecute(Sender: TObject);
|
---|
139 | begin
|
---|
140 | Core.Grammer.SaveToXmlFile(Core.Grammer.FileName);
|
---|
141 | UpdateInterface;
|
---|
142 | end;
|
---|
143 |
|
---|
144 | procedure TFormMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
---|
145 | begin
|
---|
146 | Core.PersistentForm1.Save(Self);
|
---|
147 | Core.Finalize;
|
---|
148 | end;
|
---|
149 |
|
---|
150 | procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: boolean);
|
---|
151 | begin
|
---|
152 | AProjectClose.Execute;
|
---|
153 | if Assigned(Core.Grammer) then CanClose := False;
|
---|
154 | end;
|
---|
155 |
|
---|
156 | procedure TFormMain.FormShow(Sender: TObject);
|
---|
157 | begin
|
---|
158 | Core.Initialize;
|
---|
159 | Core.PersistentForm1.Load(Self);
|
---|
160 | PageControl1.TabIndex := 0;
|
---|
161 | ReloadMemo;
|
---|
162 | UpdateInterface;
|
---|
163 |
|
---|
164 | FormRules.Rules := Core.Grammer.Rules;
|
---|
165 | FormRules.ManualDock(TabSheetRules, nil, alClient);
|
---|
166 | FormRules.Align := alClient;
|
---|
167 | FormRules.Show;
|
---|
168 | end;
|
---|
169 |
|
---|
170 | procedure TFormMain.LastOpenedList1Change(Sender: TObject);
|
---|
171 | begin
|
---|
172 | LastOpenedList1.LoadToMenuItem(MenuItemRecent, AProjectOpenRecentExecute);
|
---|
173 | LastOpenedList1.LoadToMenuItem(PopupMenuOpenRecent.Items, AProjectOpenRecentExecute);
|
---|
174 | end;
|
---|
175 |
|
---|
176 | procedure TFormMain.ReloadMemo;
|
---|
177 | begin
|
---|
178 | if Assigned(Core.Grammer) then begin
|
---|
179 | MemoBnf.Lines.Text := Core.Grammer.ExportAsString(gnBnf);
|
---|
180 | MemoEbnf.Lines.Text := Core.Grammer.ExportAsString(gnEbnf);
|
---|
181 | end else begin
|
---|
182 | MemoBnf.Lines.Clear;
|
---|
183 | MemoEbnf.Lines.Clear;
|
---|
184 | end;
|
---|
185 | end;
|
---|
186 |
|
---|
187 | procedure TFormMain.UpdateInterface;
|
---|
188 | var
|
---|
189 | NewTitle: string;
|
---|
190 | begin
|
---|
191 | NewTitle := Application.Title;
|
---|
192 | if Assigned(Core.Grammer) then begin
|
---|
193 | NewTitle := ExtractFileNameWithoutExt(ExtractFileName(Core.Grammer.FileName)) + ' - ' + NewTitle;
|
---|
194 | if Core.Grammer.Modified then NewTitle := NewTitle + '*';
|
---|
195 | end;
|
---|
196 | Caption := NewTitle;
|
---|
197 | AProjectClose.Enabled := Assigned(Core.Grammer);
|
---|
198 | AProjectSave.Enabled := Assigned(Core.Grammer) and Core.Grammer.Modified;
|
---|
199 | AProjectSaveAs.Enabled := Assigned(Core.Grammer);
|
---|
200 | AGrammerSettings.Enabled := Assigned(Core.Grammer);
|
---|
201 | ABuildCompiler.Enabled := Assigned(Core.Grammer);
|
---|
202 | ARules.Enabled := Assigned(Core.Grammer);
|
---|
203 | CoolBar1.Visible := AToolbarVisible.Checked;
|
---|
204 | end;
|
---|
205 |
|
---|
206 | procedure TFormMain.GrammerChange(Sender: TObject);
|
---|
207 | begin
|
---|
208 | ReloadMemo;
|
---|
209 | UpdateInterface;
|
---|
210 | if Assigned(FormRules) then begin
|
---|
211 | FormRules.Rules := Core.Grammer.Rules;
|
---|
212 | FormRules.ReloadList;
|
---|
213 | FormRules.UpdateInterface;
|
---|
214 | end;
|
---|
215 | end;
|
---|
216 |
|
---|
217 | procedure TFormMain.GrammerDestroy(Sender: TObject);
|
---|
218 | begin
|
---|
219 | ReloadMemo;
|
---|
220 | UpdateInterface;
|
---|
221 | if Assigned(FormRules) then begin
|
---|
222 | FormRules.Rules := nil;
|
---|
223 | FormRules.ReloadList;
|
---|
224 | FormRules.UpdateInterface;
|
---|
225 | end;
|
---|
226 | end;
|
---|
227 |
|
---|
228 | procedure TFormMain.ProjectOpen(FileName: string);
|
---|
229 | begin
|
---|
230 | AProjectClose.Execute;
|
---|
231 | if not Assigned(Core.Grammer) then begin
|
---|
232 | LastOpenedList1.AddItem(FileName);
|
---|
233 | ProjectNew;
|
---|
234 | Core.Grammer := TGrammer.Create;
|
---|
235 | Core.Grammer.OnChange := GrammerChange;
|
---|
236 | Core.Grammer.OnDestroy := GrammerDestroy;
|
---|
237 | Core.Grammer.LoadFromXmlFile(FileName);
|
---|
238 | Core.Grammer.Change;
|
---|
239 | end;
|
---|
240 | end;
|
---|
241 |
|
---|
242 | procedure TFormMain.ProjectNew;
|
---|
243 | begin
|
---|
244 | if Assigned(Core.Grammer) then FreeAndNil(Core.Grammer);
|
---|
245 | Core.Grammer := TGrammer.Create;
|
---|
246 | Core.Grammer.OnChange := GrammerChange;
|
---|
247 | Core.Grammer.OnDestroy := GrammerDestroy;
|
---|
248 | Core.Grammer.FileName := SNewGrammer;
|
---|
249 | Core.Grammer.Change;
|
---|
250 | end;
|
---|
251 |
|
---|
252 | procedure TFormMain.LoadConfig(XmlConfig: TXmlConfig);
|
---|
253 | begin
|
---|
254 | AToolbarVisible.Checked := XmlConfig.GetValue('ToolbarVisible', True);
|
---|
255 | LastOpenedList1.LoadFromXMLConfig(XMLConfig, 'LastOpenedFiles');
|
---|
256 | end;
|
---|
257 |
|
---|
258 | procedure TFormMain.SaveConfig(XmlConfig: TXmlConfig);
|
---|
259 | begin
|
---|
260 | XmlConfig.SetValue('ToolbarVisible', AToolbarVisible.Checked);
|
---|
261 | LastOpenedList1.SaveToXMLConfig(XMLConfig, 'LastOpenedFiles');
|
---|
262 | end;
|
---|
263 |
|
---|
264 | procedure TFormMain.AExitExecute(Sender: TObject);
|
---|
265 | begin
|
---|
266 | Close;
|
---|
267 | end;
|
---|
268 |
|
---|
269 | procedure TFormMain.AGrammerSettingsExecute(Sender: TObject);
|
---|
270 | var
|
---|
271 | FormGrammer: TFormGrammer;
|
---|
272 | begin
|
---|
273 | FormGrammer := TFormGrammer.Create(nil);
|
---|
274 | FormGrammer.LoadData(Core.Grammer);
|
---|
275 | if FormGrammer.ShowModal = mrOk then begin
|
---|
276 | FormGrammer.SaveData(Core.Grammer);
|
---|
277 | UpdateInterface;
|
---|
278 | end;
|
---|
279 | FreeAndNil(FormGrammer);
|
---|
280 | end;
|
---|
281 |
|
---|
282 | procedure TFormMain.AProjectOpenRecentExecute(Sender: TObject);
|
---|
283 | begin
|
---|
284 | if Sender is TMenuItem then begin
|
---|
285 | AProjectClose.Execute;
|
---|
286 | if not Assigned(Core.Grammer) then begin
|
---|
287 | ProjectOpen(TMenuItem(Sender).Caption);
|
---|
288 | end;
|
---|
289 | end;
|
---|
290 | end;
|
---|
291 |
|
---|
292 | procedure TFormMain.AProjectNewExecute(Sender: TObject);
|
---|
293 | begin
|
---|
294 | AProjectClose.Execute;
|
---|
295 | if not Assigned(Core.Grammer) then begin
|
---|
296 | ProjectNew;
|
---|
297 | end;
|
---|
298 | end;
|
---|
299 |
|
---|
300 | procedure TFormMain.ABuildCompilerExecute(Sender: TObject);
|
---|
301 | var
|
---|
302 | Builder: TBuilder;
|
---|
303 | begin
|
---|
304 | Builder := TBuilder.Create;
|
---|
305 | try
|
---|
306 | Builder.Grammer := Core.Grammer;
|
---|
307 | Builder.BuildCompiler;
|
---|
308 | finally
|
---|
309 | FreeAndNil(Builder);
|
---|
310 | end;
|
---|
311 | end;
|
---|
312 |
|
---|
313 | procedure TFormMain.ALookupTablesExecute(Sender: TObject);
|
---|
314 | var
|
---|
315 | FormLookupTables: TFormLookupTables;
|
---|
316 | begin
|
---|
317 | FormLookupTables := TFormLookupTables.Create(nil);
|
---|
318 | try
|
---|
319 | FormLookupTables.LookupTables := Core.Grammer.LookupTables;
|
---|
320 | if FormLookupTables.ShowModal = mrOk then begin
|
---|
321 |
|
---|
322 | end;
|
---|
323 | finally
|
---|
324 | FreeAndNil(FormLookupTables);
|
---|
325 | end;
|
---|
326 | end;
|
---|
327 |
|
---|
328 | procedure TFormMain.ASettingsExecute(Sender: TObject);
|
---|
329 | begin
|
---|
330 | FormSettings := TFormSettings.Create(nil);
|
---|
331 | FormSettings.Load;
|
---|
332 | if FormSettings.ShowModal = mrOk then begin
|
---|
333 | FormSettings.Save;
|
---|
334 | end;
|
---|
335 | FreeAndNil(FormSettings);
|
---|
336 | end;
|
---|
337 |
|
---|
338 | procedure TFormMain.AToolbarVisibleExecute(Sender: TObject);
|
---|
339 | begin
|
---|
340 | UpdateInterface;
|
---|
341 | end;
|
---|
342 |
|
---|
343 | procedure TFormMain.AProjectOpenExecute(Sender: TObject);
|
---|
344 | begin
|
---|
345 | OpenDialog1.InitialDir := ExtractFileDir(LastOpenedList1.GetFirstFileName);
|
---|
346 | OpenDialog1.FileName := ExtractFileName(LastOpenedList1.GetFirstFileName);
|
---|
347 | if OpenDialog1.Execute then begin
|
---|
348 | ProjectOpen(OpenDialog1.FileName);
|
---|
349 | end;
|
---|
350 | end;
|
---|
351 |
|
---|
352 | procedure TFormMain.AProjectCloseExecute(Sender: TObject);
|
---|
353 | var
|
---|
354 | DialogResult: TModalResult;
|
---|
355 | begin
|
---|
356 | if not Assigned(Core.Grammer) then Exit;
|
---|
357 |
|
---|
358 | DialogResult := mrNo;
|
---|
359 | if Core.Grammer.Modified then begin
|
---|
360 | DialogResult := MessageDlg(SUnsavedChanges, SDoYouWantToSaveProject, mtConfirmation, [mbYes, mbNo, mbCancel], 0);
|
---|
361 | if DialogResult = mrYes then begin
|
---|
362 | AProjectSave.Execute;
|
---|
363 | end;
|
---|
364 | end;
|
---|
365 | if (DialogResult = mrNo) or (DialogResult = mrYes) then begin
|
---|
366 | FreeAndNil(Core.Grammer);
|
---|
367 | end;
|
---|
368 | end;
|
---|
369 |
|
---|
370 | end.
|
---|
371 |
|
---|