Changeset 6 for trunk/UFormMain.pas
- Timestamp:
- Apr 21, 2016, 10:22:08 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UFormMain.pas
r5 r6 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus, 9 9 ComCtrls, StdCtrls, ExtCtrls, ActnList, UAcronym, UPersistentForm, Contnrs, 10 URegistry, Registry;10 URegistry, ULastOpenedList, Registry; 11 11 12 12 type … … 19 19 AAcronymRemove: TAction; 20 20 AAcronymRemoveAll: TAction; 21 AFileSaveAs: TAction; 22 AFileClose: TAction; 23 AFileSave: TAction; 24 AFileNew: TAction; 25 AFileOpen: TAction; 21 26 AImport: TAction; 22 27 AShow: TAction; … … 24 29 ActionList1: TActionList; 25 30 EditSearch: TEdit; 31 LastOpenedList1: TLastOpenedList; 26 32 ListViewAcronyms: TListView; 33 MainMenu1: TMainMenu; 27 34 MenuItem1: TMenuItem; 35 MenuItem10: TMenuItem; 36 MenuItem11: TMenuItem; 37 MenuItem12: TMenuItem; 38 MenuItem13: TMenuItem; 39 MenuItem14: TMenuItem; 40 MenuItem15: TMenuItem; 41 MenuItemOpenRecent: TMenuItem; 28 42 MenuItem2: TMenuItem; 29 43 MenuItem3: TMenuItem; … … 32 46 MenuItem6: TMenuItem; 33 47 MenuItem7: TMenuItem; 48 MenuItem8: TMenuItem; 49 MenuItem9: TMenuItem; 50 OpenDialog1: TOpenDialog; 34 51 PersistentForm1: TPersistentForm; 35 52 PopupMenuAcronym: TPopupMenu; 36 53 PopupMenuTryIcon: TPopupMenu; 54 SaveDialog1: TSaveDialog; 37 55 TrayIcon1: TTrayIcon; 38 56 procedure AAcronymAddExecute(Sender: TObject); … … 41 59 procedure AAcronymRemoveExecute(Sender: TObject); 42 60 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); 43 66 procedure AImportExecute(Sender: TObject); 44 67 procedure AShowExecute(Sender: TObject); … … 49 72 procedure FormDestroy(Sender: TObject); 50 73 procedure FormShow(Sender: TObject); 74 procedure LastOpenedList1Change(Sender: TObject); 51 75 procedure ListViewAcronymsData(Sender: TObject; Item: TListItem); 52 76 procedure ListViewAcronymsDblClick(Sender: TObject); … … 57 81 private 58 82 FoundAcronyms: TAcronymMeanings; 83 RegistryContext: TRegistryContext; 84 procedure OpenRecentClick(Sender: TObject); 59 85 procedure UpdateAcronymsList; 60 86 procedure UpdateInterface; 87 procedure LoadConfig; 88 procedure SaveConfig; 61 89 public 62 90 AcronymDb: TAcronymDb; … … 74 102 75 103 const 76 DefaultFileName = 'Acronyms.csv'; 104 ProjectExt = '.adp'; 105 DefaultFileName = 'Acronyms' + ProjectExt; 106 DefaultRegKey = '\Software\Acronym Decoder'; 77 107 78 108 { TFormMain } … … 91 121 procedure TFormMain.FormClose(Sender: TObject; var CloseAction: TCloseAction); 92 122 begin 93 A cronymDB.SaveToFile(DefaultFileName);123 AFileClose.Execute; 94 124 PersistentForm1.Save(Self); 125 SaveConfig; 95 126 end; 96 127 97 128 procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: boolean); 98 begin 129 var 130 ModalResult: TModalResult; 131 begin 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; 99 140 end; 100 141 … … 102 143 begin 103 144 Close; 145 end; 146 147 procedure TFormMain.AFileCloseExecute(Sender: TObject); 148 begin 149 if Assigned(AcronymDb) then begin 150 if AcronymDb.Modified then AFileSave.Execute; 151 FreeAndNil(AcronymDb); 152 UpdateAcronymsList; 153 UpdateInterface; 154 end; 155 end; 156 157 procedure TFormMain.AFileNewExecute(Sender: TObject); 158 begin 159 AFileClose.Execute; 160 AcronymDb := TAcronymDb.Create; 161 AcronymDb.FileName := DefaultFileName; 162 AcronymDb.Acronyms.Clear; 163 UpdateAcronymsList; 164 UpdateInterface; 165 end; 166 167 procedure TFormMain.AFileOpenExecute(Sender: TObject); 168 begin 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; 177 end; 178 179 procedure TFormMain.AFileSaveAsExecute(Sender: TObject); 180 begin 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; 188 end; 189 190 procedure TFormMain.AFileSaveExecute(Sender: TObject); 191 begin 192 if FileExists(AcronymDb.FileName) then begin 193 AcronymDb.SaveToFile(AcronymDb.FileName); 194 LastOpenedList1.AddItem(AcronymDb.FileName); 195 UpdateInterface; 196 end else AFileSaveAs.Execute; 104 197 end; 105 198 … … 133 226 TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin 134 227 AcronymDb.Acronyms.Clear; 228 AcronymDb.Modified := True; 135 229 UpdateAcronymsList; 136 230 UpdateInterface; … … 185 279 186 280 procedure TFormMain.FormShow(Sender: TObject); 187 const 188 DefaultRegKey = '\Software\Acronym Decoder'; 189 begin 190 PersistentForm1.RegistryContext := RegContext(HKEY_CURRENT_USER, DefaultRegKey); 281 begin 282 LoadConfig; 191 283 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]); 195 287 UpdateAcronymsList; 196 288 UpdateInterface; 289 end; 290 291 procedure TFormMain.LastOpenedList1Change(Sender: TObject); 292 begin 293 LastOpenedList1.LoadToMenuItem(MenuItemOpenRecent, OpenRecentClick); 197 294 end; 198 295 … … 235 332 end; 236 333 334 procedure TFormMain.OpenRecentClick(Sender: TObject); 335 begin 336 AFileClose.Execute; 337 AFileNew.Execute; 338 AcronymDb.LoadFromFile(TMenuItem(Sender).Caption); 339 LastOpenedList1.AddItem(TMenuItem(Sender).Caption); 340 UpdateAcronymsList; 341 UpdateInterface; 342 end; 343 237 344 procedure TFormMain.UpdateAcronymsList; 238 345 begin 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; 241 350 ListViewAcronyms.Items.Count := FoundAcronyms.Count; 242 351 ListViewAcronyms.Refresh; … … 244 353 245 354 procedure TFormMain.UpdateInterface; 246 begin 247 AAcronymRemove.Enabled := Assigned(ListViewAcronyms.Selected); 355 var 356 Title: string; 357 begin 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; 372 end; 373 374 procedure TFormMain.LoadConfig; 375 begin 376 RegistryContext := RegContext(HKEY_CURRENT_USER, DefaultRegKey); 377 PersistentForm1.RegistryContext := RegistryContext; 378 LastOpenedList1.LoadFromRegistry(RegistryContext); 379 end; 380 381 procedure TFormMain.SaveConfig; 382 begin 383 LastOpenedList1.SaveToRegistry(RegistryContext); 248 384 end; 249 385
Note:
See TracChangeset
for help on using the changeset viewer.