source: branches/generator/Forms/UFormMain.pas

Last change on this file was 191, checked in by chronos, 6 years ago
  • Added: Interface language selection in Settings dialog.
File size: 9.8 KB
Line 
1unit UFormMain;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
9 Menus, ActnList, ComCtrls, UPersistentForm, ULastOpenedList, UGrammer,
10 XMLConf;
11
12type
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
97var
98 FormMain: TFormMain;
99
100
101implementation
102
103{$R *.lfm}
104
105uses
106 UFormRules, UCore, UFormGrammer, UBuilder, UFormLookupTables, UFormSettings;
107
108resourcestring
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
115procedure TFormMain.ARulesExecute(Sender: TObject);
116var
117 FormRules: TFormRules;
118begin
119 FormRules := TFormRules.Create(nil);
120 FormRules.Rules := Core.Grammer.Rules;
121 FormRules.ShowModal;
122 ReloadMemo;
123 UpdateInterface;
124 FreeAndNil(FormRules);
125end;
126
127procedure TFormMain.AProjectSaveAsExecute(Sender: TObject);
128begin
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;
136end;
137
138procedure TFormMain.AProjectSaveExecute(Sender: TObject);
139begin
140 Core.Grammer.SaveToXmlFile(Core.Grammer.FileName);
141 UpdateInterface;
142end;
143
144procedure TFormMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
145begin
146 Core.PersistentForm1.Save(Self);
147 Core.Finalize;
148end;
149
150procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: boolean);
151begin
152 AProjectClose.Execute;
153 if Assigned(Core.Grammer) then CanClose := False;
154end;
155
156procedure TFormMain.FormShow(Sender: TObject);
157begin
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;
168end;
169
170procedure TFormMain.LastOpenedList1Change(Sender: TObject);
171begin
172 LastOpenedList1.LoadToMenuItem(MenuItemRecent, AProjectOpenRecentExecute);
173 LastOpenedList1.LoadToMenuItem(PopupMenuOpenRecent.Items, AProjectOpenRecentExecute);
174end;
175
176procedure TFormMain.ReloadMemo;
177begin
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;
185end;
186
187procedure TFormMain.UpdateInterface;
188var
189 NewTitle: string;
190begin
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;
204end;
205
206procedure TFormMain.GrammerChange(Sender: TObject);
207begin
208 ReloadMemo;
209 UpdateInterface;
210 if Assigned(FormRules) then begin
211 FormRules.Rules := Core.Grammer.Rules;
212 FormRules.ReloadList;
213 FormRules.UpdateInterface;
214 end;
215end;
216
217procedure TFormMain.GrammerDestroy(Sender: TObject);
218begin
219 ReloadMemo;
220 UpdateInterface;
221 if Assigned(FormRules) then begin
222 FormRules.Rules := nil;
223 FormRules.ReloadList;
224 FormRules.UpdateInterface;
225 end;
226end;
227
228procedure TFormMain.ProjectOpen(FileName: string);
229begin
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;
240end;
241
242procedure TFormMain.ProjectNew;
243begin
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;
250end;
251
252procedure TFormMain.LoadConfig(XmlConfig: TXmlConfig);
253begin
254 AToolbarVisible.Checked := XmlConfig.GetValue('ToolbarVisible', True);
255 LastOpenedList1.LoadFromXMLConfig(XMLConfig, 'LastOpenedFiles');
256end;
257
258procedure TFormMain.SaveConfig(XmlConfig: TXmlConfig);
259begin
260 XmlConfig.SetValue('ToolbarVisible', AToolbarVisible.Checked);
261 LastOpenedList1.SaveToXMLConfig(XMLConfig, 'LastOpenedFiles');
262end;
263
264procedure TFormMain.AExitExecute(Sender: TObject);
265begin
266 Close;
267end;
268
269procedure TFormMain.AGrammerSettingsExecute(Sender: TObject);
270var
271 FormGrammer: TFormGrammer;
272begin
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);
280end;
281
282procedure TFormMain.AProjectOpenRecentExecute(Sender: TObject);
283begin
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;
290end;
291
292procedure TFormMain.AProjectNewExecute(Sender: TObject);
293begin
294 AProjectClose.Execute;
295 if not Assigned(Core.Grammer) then begin
296 ProjectNew;
297 end;
298end;
299
300procedure TFormMain.ABuildCompilerExecute(Sender: TObject);
301var
302 Builder: TBuilder;
303begin
304 Builder := TBuilder.Create;
305 try
306 Builder.Grammer := Core.Grammer;
307 Builder.BuildCompiler;
308 finally
309 FreeAndNil(Builder);
310 end;
311end;
312
313procedure TFormMain.ALookupTablesExecute(Sender: TObject);
314var
315 FormLookupTables: TFormLookupTables;
316begin
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;
326end;
327
328procedure TFormMain.ASettingsExecute(Sender: TObject);
329begin
330 FormSettings := TFormSettings.Create(nil);
331 FormSettings.Load;
332 if FormSettings.ShowModal = mrOk then begin
333 FormSettings.Save;
334 end;
335 FreeAndNil(FormSettings);
336end;
337
338procedure TFormMain.AToolbarVisibleExecute(Sender: TObject);
339begin
340 UpdateInterface;
341end;
342
343procedure TFormMain.AProjectOpenExecute(Sender: TObject);
344begin
345 OpenDialog1.InitialDir := ExtractFileDir(LastOpenedList1.GetFirstFileName);
346 OpenDialog1.FileName := ExtractFileName(LastOpenedList1.GetFirstFileName);
347 if OpenDialog1.Execute then begin
348 ProjectOpen(OpenDialog1.FileName);
349 end;
350end;
351
352procedure TFormMain.AProjectCloseExecute(Sender: TObject);
353var
354 DialogResult: TModalResult;
355begin
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;
368end;
369
370end.
371
Note: See TracBrowser for help on using the repository browser.