Ignore:
Timestamp:
Nov 30, 2017, 12:02:32 AM (7 years ago)
Author:
chronos
Message:
  • Added: New project Close action.
  • Added: Track project Modified flag and offer project save if modified project is closed.
  • Added: Remember last opened project file.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/generator/Forms/UFormMain.pas

    r124 r126  
    1515  TFormMain = class(TForm)
    1616    ABuildCompiler: TAction;
     17    AProjectClose: TAction;
    1718    AGrammerSettings: TAction;
    18     ANew: TAction;
    19     ASave: TAction;
     19    AProjectNew: TAction;
     20    AProjectSave: TAction;
    2021    AExit: TAction;
    21     AOpen: TAction;
    22     ASaveAs: TAction;
     22    AProjectOpen: TAction;
     23    AProjectSaveAs: TAction;
    2324    ARules: TAction;
    2425    ActionList1: TActionList;
     
    3031    MenuItem11: TMenuItem;
    3132    MenuItem12: TMenuItem;
     33    MenuItem13: TMenuItem;
    3234    MenuItem2: TMenuItem;
    3335    MenuItem3: TMenuItem;
     
    4648    procedure AExitExecute(Sender: TObject);
    4749    procedure AGrammerSettingsExecute(Sender: TObject);
    48     procedure ANewExecute(Sender: TObject);
    49     procedure AOpenExecute(Sender: TObject);
    5050    procedure ARulesExecute(Sender: TObject);
    51     procedure ASaveAsExecute(Sender: TObject);
    52     procedure ASaveExecute(Sender: TObject);
     51    procedure AProjectNewExecute(Sender: TObject);
     52    procedure AProjectOpenExecute(Sender: TObject);
     53    procedure AProjectCloseExecute(Sender: TObject);
     54    procedure AProjectSaveAsExecute(Sender: TObject);
     55    procedure AProjectSaveExecute(Sender: TObject);
    5356    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
     57    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
    5458    procedure FormShow(Sender: TObject);
    5559  private
     
    7175  UFormRules, UCore, UFormGrammer;
    7276
    73 { TFormMain }
     77resourcestring
     78  SUnsavedChanges = 'Unsaved changes';
     79  SDoYouWantToSaveProject = 'There are unsaved changes in project. Do you want to save them?';
     80  SNewGrammer = 'New grammer.xtg';
     81
     82  { TFormMain }
    7483
    7584procedure TFormMain.ARulesExecute(Sender: TObject);
     
    8190  FormRules.ShowModal;
    8291  ReloadMemo;
     92  UpdateInterface;
    8393  FreeAndNil(FormRules);
    8494end;
    8595
    86 procedure TFormMain.ASaveAsExecute(Sender: TObject);
     96procedure TFormMain.AProjectSaveAsExecute(Sender: TObject);
    8797begin
    8898  SaveDialog1.FileName := Core.Grammer.FileName;
    8999  if SaveDialog1.Execute then begin
     100    Core.LastOpenedFile := SaveDialog1.FileName;
    90101    Core.Grammer.SaveToXmlFile(SaveDialog1.FileName);
    91102    UpdateInterface;
     
    93104end;
    94105
    95 procedure TFormMain.ASaveExecute(Sender: TObject);
     106procedure TFormMain.AProjectSaveExecute(Sender: TObject);
    96107begin
    97108  Core.Grammer.SaveToXmlFile(Core.Grammer.FileName);
     
    104115end;
    105116
     117procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: boolean);
     118begin
     119  AProjectClose.Execute;
     120  if Assigned(Core.Grammer) then CanClose := False;
     121end;
     122
    106123procedure TFormMain.FormShow(Sender: TObject);
    107124begin
    108125  Core.PersistentForm1.Load(Self, True);
     126  Core.Initialize;
    109127  ReloadMemo;
    110128  UpdateInterface;
     
    113131procedure TFormMain.ReloadMemo;
    114132begin
    115   MemoBNF.Lines.Text := Core.Grammer.GetString;
     133  if Assigned(Core.Grammer) then
     134    MemoBNF.Lines.Text := Core.Grammer.GetString
     135    else MemoBNF.Lines.Clear;
    116136end;
    117137
    118138procedure TFormMain.UpdateInterface;
    119 begin
    120   Caption := Core.Grammer.FileName + ' - ' + Application.Title;
     139var
     140  NewTitle: string;
     141begin
     142  NewTitle := Application.Title;
     143  if Assigned(Core.Grammer) then begin
     144    NewTitle := ExtractFileNameWithoutExt(ExtractFileName(Core.Grammer.FileName)) + ' - ' + NewTitle;
     145    if Core.Grammer.Modified then NewTitle := NewTitle + '*';
     146  end;
     147  Caption := NewTitle;
     148  AProjectClose.Enabled := Assigned(Core.Grammer);
     149  AProjectSave.Enabled := Assigned(Core.Grammer) and Core.Grammer.Modified;
     150  AProjectSaveAs.Enabled := Assigned(Core.Grammer);
     151  AGrammerSettings.Enabled := Assigned(Core.Grammer);
     152  ABuildCompiler.Enabled := Assigned(Core.Grammer);
     153  ARules.Enabled := Assigned(Core.Grammer);
    121154end;
    122155
     
    134167  if FormGrammer.ShowModal = mrOk then begin
    135168    FormGrammer.SaveData(Core.Grammer);
     169    UpdateInterface;
    136170  end;
    137171  FreeAndNil(FormGrammer);
    138172end;
    139173
    140 procedure TFormMain.ANewExecute(Sender: TObject);
    141 begin
    142   Core.Grammer.Rules.Clear;
    143   ReloadMemo;
    144   UpdateInterface;
     174procedure TFormMain.AProjectNewExecute(Sender: TObject);
     175begin
     176  AProjectClose.Execute;
     177  if not Assigned(Core.Grammer) then begin
     178    Core.Grammer := TGrammer.Create;
     179    Core.Grammer.FileName := SNewGrammer;
     180    ReloadMemo;
     181    UpdateInterface;
     182  end;
    145183end;
    146184
     
    150188end;
    151189
    152 procedure TFormMain.AOpenExecute(Sender: TObject);
    153 begin
    154   OpenDialog1.FileName := Core.Grammer.FileName;
    155   if OpenDialog1.Execute then begin
    156     Core.Grammer.LoadFromXmlFile(OpenDialog1.FileName);
     190procedure TFormMain.AProjectOpenExecute(Sender: TObject);
     191begin
     192  AProjectClose.Execute;
     193  if not Assigned(Core.Grammer) then begin
     194    OpenDialog1.FileName := Core.LastOpenedFile;
     195    if OpenDialog1.Execute then begin
     196      Core.LastOpenedFile := OpenDialog1.FileName;
     197      Core.Grammer := TGrammer.Create;
     198      Core.Grammer.LoadFromXmlFile(OpenDialog1.FileName);
     199      ReloadMemo;
     200      UpdateInterface;
     201    end;
     202  end;
     203end;
     204
     205procedure TFormMain.AProjectCloseExecute(Sender: TObject);
     206var
     207  DialogResult: TModalResult;
     208begin
     209  if not Assigned(Core.Grammer) then Exit;
     210
     211  DialogResult := mrNo;
     212  if Core.Grammer.Modified then begin
     213    DialogResult := MessageDlg(SUnsavedChanges, SDoYouWantToSaveProject, mtConfirmation, [mbYes, mbNo, mbCancel], 0);
     214    if DialogResult = mrYes then begin
     215      AProjectSave.Execute;
     216    end;
     217  end;
     218  if (DialogResult = mrNo) or (DialogResult = mrYes) then begin
     219    FreeAndNil(Core.Grammer);
    157220    ReloadMemo;
    158221    UpdateInterface;
Note: See TracChangeset for help on using the changeset viewer.