Changeset 6 for trunk/UFormMain.pas


Ignore:
Timestamp:
Apr 21, 2016, 10:22:08 AM (9 years ago)
Author:
chronos
Message:
  • Added: Acronyms can be loaded from/saved to different files.
  • Added: Application remembers list of last used files.
  • Added: Show question dialog on exit if acronyms were modified.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UFormMain.pas

    r5 r6  
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
    99  ComCtrls, StdCtrls, ExtCtrls, ActnList, UAcronym, UPersistentForm, Contnrs,
    10   URegistry, Registry;
     10  URegistry, ULastOpenedList, Registry;
    1111
    1212type
     
    1919    AAcronymRemove: TAction;
    2020    AAcronymRemoveAll: TAction;
     21    AFileSaveAs: TAction;
     22    AFileClose: TAction;
     23    AFileSave: TAction;
     24    AFileNew: TAction;
     25    AFileOpen: TAction;
    2126    AImport: TAction;
    2227    AShow: TAction;
     
    2429    ActionList1: TActionList;
    2530    EditSearch: TEdit;
     31    LastOpenedList1: TLastOpenedList;
    2632    ListViewAcronyms: TListView;
     33    MainMenu1: TMainMenu;
    2734    MenuItem1: TMenuItem;
     35    MenuItem10: TMenuItem;
     36    MenuItem11: TMenuItem;
     37    MenuItem12: TMenuItem;
     38    MenuItem13: TMenuItem;
     39    MenuItem14: TMenuItem;
     40    MenuItem15: TMenuItem;
     41    MenuItemOpenRecent: TMenuItem;
    2842    MenuItem2: TMenuItem;
    2943    MenuItem3: TMenuItem;
     
    3246    MenuItem6: TMenuItem;
    3347    MenuItem7: TMenuItem;
     48    MenuItem8: TMenuItem;
     49    MenuItem9: TMenuItem;
     50    OpenDialog1: TOpenDialog;
    3451    PersistentForm1: TPersistentForm;
    3552    PopupMenuAcronym: TPopupMenu;
    3653    PopupMenuTryIcon: TPopupMenu;
     54    SaveDialog1: TSaveDialog;
    3755    TrayIcon1: TTrayIcon;
    3856    procedure AAcronymAddExecute(Sender: TObject);
     
    4159    procedure AAcronymRemoveExecute(Sender: TObject);
    4260    procedure AExitExecute(Sender: TObject);
     61    procedure AFileCloseExecute(Sender: TObject);
     62    procedure AFileNewExecute(Sender: TObject);
     63    procedure AFileOpenExecute(Sender: TObject);
     64    procedure AFileSaveAsExecute(Sender: TObject);
     65    procedure AFileSaveExecute(Sender: TObject);
    4366    procedure AImportExecute(Sender: TObject);
    4467    procedure AShowExecute(Sender: TObject);
     
    4972    procedure FormDestroy(Sender: TObject);
    5073    procedure FormShow(Sender: TObject);
     74    procedure LastOpenedList1Change(Sender: TObject);
    5175    procedure ListViewAcronymsData(Sender: TObject; Item: TListItem);
    5276    procedure ListViewAcronymsDblClick(Sender: TObject);
     
    5781  private
    5882    FoundAcronyms: TAcronymMeanings;
     83    RegistryContext: TRegistryContext;
     84    procedure OpenRecentClick(Sender: TObject);
    5985    procedure UpdateAcronymsList;
    6086    procedure UpdateInterface;
     87    procedure LoadConfig;
     88    procedure SaveConfig;
    6189  public
    6290    AcronymDb: TAcronymDb;
     
    74102
    75103const
    76   DefaultFileName = 'Acronyms.csv';
     104  ProjectExt = '.adp';
     105  DefaultFileName = 'Acronyms' + ProjectExt;
     106  DefaultRegKey = '\Software\Acronym Decoder';
    77107
    78108{ TFormMain }
     
    91121procedure TFormMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
    92122begin
    93   AcronymDB.SaveToFile(DefaultFileName);
     123  AFileClose.Execute;
    94124  PersistentForm1.Save(Self);
     125  SaveConfig;
    95126end;
    96127
    97128procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: boolean);
    98 begin
     129var
     130  ModalResult: TModalResult;
     131begin
     132  if Assigned(AcronymDb) and AcronymDb.Modified then begin
     133    ModalResult := MessageDlg('Application exit',
     134    'Acronyms were modified. Do you want to save them to file before exit?',
     135      mtConfirmation, [mbYes, mbNo, mbCancel], 0);
     136    if ModalResult = mrYes then AFileSave.Execute
     137    else if ModalResult = mrNo then CanClose := True
     138    else CanClose := False;
     139  end;
    99140end;
    100141
     
    102143begin
    103144  Close;
     145end;
     146
     147procedure TFormMain.AFileCloseExecute(Sender: TObject);
     148begin
     149  if Assigned(AcronymDb) then begin
     150    if AcronymDb.Modified then AFileSave.Execute;
     151    FreeAndNil(AcronymDb);
     152    UpdateAcronymsList;
     153    UpdateInterface;
     154  end;
     155end;
     156
     157procedure TFormMain.AFileNewExecute(Sender: TObject);
     158begin
     159  AFileClose.Execute;
     160  AcronymDb := TAcronymDb.Create;
     161  AcronymDb.FileName := DefaultFileName;
     162  AcronymDb.Acronyms.Clear;
     163  UpdateAcronymsList;
     164  UpdateInterface;
     165end;
     166
     167procedure TFormMain.AFileOpenExecute(Sender: TObject);
     168begin
     169  OpenDialog1.DefaultExt := ProjectExt;
     170  OpenDialog1.FileName := AcronymDb.FileName;
     171  if OpenDialog1.Execute then begin
     172    AcronymDb.LoadFromFile(OpenDialog1.FileName);
     173    LastOpenedList1.AddItem(OpenDialog1.FileName);
     174    UpdateAcronymsList;
     175    UpdateInterface;
     176  end;
     177end;
     178
     179procedure TFormMain.AFileSaveAsExecute(Sender: TObject);
     180begin
     181  SaveDialog1.DefaultExt := ProjectExt;
     182  SaveDialog1.FileName := AcronymDb.FileName;
     183  if SaveDialog1.Execute then begin
     184    AcronymDb.SaveToFile(SaveDialog1.FileName);
     185    LastOpenedList1.AddItem(SaveDialog1.FileName);
     186    UpdateInterface;
     187  end;
     188end;
     189
     190procedure TFormMain.AFileSaveExecute(Sender: TObject);
     191begin
     192  if FileExists(AcronymDb.FileName) then begin
     193    AcronymDb.SaveToFile(AcronymDb.FileName);
     194    LastOpenedList1.AddItem(AcronymDb.FileName);
     195    UpdateInterface;
     196  end else AFileSaveAs.Execute;
    104197end;
    105198
     
    133226  TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
    134227    AcronymDb.Acronyms.Clear;
     228    AcronymDb.Modified := True;
    135229    UpdateAcronymsList;
    136230    UpdateInterface;
     
    185279
    186280procedure TFormMain.FormShow(Sender: TObject);
    187 const
    188   DefaultRegKey = '\Software\Acronym Decoder';
    189 begin
    190   PersistentForm1.RegistryContext := RegContext(HKEY_CURRENT_USER, DefaultRegKey);
     281begin
     282  LoadConfig;
    191283  PersistentForm1.Load(Self);
    192   if FileExists(DefaultFileName) then
    193     AcronymDB.LoadFromFile(DefaultFileName);
    194   AcronymDb.FilterList(EditSearch.Text, FoundAcronyms);
     284
     285  if (LastOpenedList1.Items.Count > 0) and  FileExists(LastOpenedList1.Items[0]) then
     286    AcronymDB.LoadFromFile(LastOpenedList1.Items[0]);
    195287  UpdateAcronymsList;
    196288  UpdateInterface;
     289end;
     290
     291procedure TFormMain.LastOpenedList1Change(Sender: TObject);
     292begin
     293  LastOpenedList1.LoadToMenuItem(MenuItemOpenRecent, OpenRecentClick);
    197294end;
    198295
     
    235332end;
    236333
     334procedure TFormMain.OpenRecentClick(Sender: TObject);
     335begin
     336  AFileClose.Execute;
     337  AFileNew.Execute;
     338  AcronymDb.LoadFromFile(TMenuItem(Sender).Caption);
     339  LastOpenedList1.AddItem(TMenuItem(Sender).Caption);
     340  UpdateAcronymsList;
     341  UpdateInterface;
     342end;
     343
    237344procedure TFormMain.UpdateAcronymsList;
    238345begin
    239   AcronymDb.Acronyms.Sort(AcronymComparer);
    240   AcronymDb.FilterList(EditSearch.Text, FoundAcronyms);
     346  if Assigned(AcronymDb) then begin
     347    AcronymDb.Acronyms.Sort(AcronymComparer);
     348    AcronymDb.FilterList(EditSearch.Text, FoundAcronyms);
     349  end else FoundAcronyms.Clear;
    241350  ListViewAcronyms.Items.Count := FoundAcronyms.Count;
    242351  ListViewAcronyms.Refresh;
     
    244353
    245354procedure TFormMain.UpdateInterface;
    246 begin
    247   AAcronymRemove.Enabled := Assigned(ListViewAcronyms.Selected);
     355var
     356  Title: string;
     357begin
     358  ListViewAcronyms.Enabled := Assigned(AcronymDb);
     359  AAcronymRemove.Enabled := Assigned(AcronymDb) and Assigned(ListViewAcronyms.Selected);
     360  AAcronymModify.Enabled := Assigned(AcronymDb) and Assigned(ListViewAcronyms.Selected);
     361  AAcronymAdd.Enabled := Assigned(AcronymDb);
     362  AAcronymRemoveAll.Enabled := Assigned(AcronymDb) and (AcronymDb.Acronyms.Count > 0);
     363  AFileClose.Enabled := Assigned(AcronymDb);
     364  AFileSave.Enabled := Assigned(AcronymDb) and AcronymDb.Modified;
     365  AFileSaveAs.Enabled := Assigned(AcronymDb);
     366  EditSearch.Enabled := Assigned(AcronymDb);
     367
     368  Title := Application.Title;
     369  if Assigned(AcronymDb) then Title := Title + ' - ' + ExtractFileName(AcronymDb.FileName);
     370  if Assigned(AcronymDb) and AcronymDb.Modified then Title := Title + ' (modified)';
     371  Caption := Title;
     372end;
     373
     374procedure TFormMain.LoadConfig;
     375begin
     376  RegistryContext := RegContext(HKEY_CURRENT_USER, DefaultRegKey);
     377  PersistentForm1.RegistryContext := RegistryContext;
     378  LastOpenedList1.LoadFromRegistry(RegistryContext);
     379end;
     380
     381procedure TFormMain.SaveConfig;
     382begin
     383  LastOpenedList1.SaveToRegistry(RegistryContext);
    248384end;
    249385
Note: See TracChangeset for help on using the changeset viewer.