//--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "History.h" //--------------------------------------------------------------------------- #pragma package(smart_init) TFileHistory *FileHistory; //--------------------------------------------------------------------------- __fastcall TFileHistory::TFileHistory(AnsiString IniFileName, int theMaxHistoryItems, TMenuItem *theFileMenu, TOpenFileFunc theOpenFileFunc) { THistory *phistory; int b; AnsiString Section; FIniFileName = IniFileName; MaxHistoryItems = theMaxHistoryItems; FileMenu = theFileMenu; OpenFileFunc = theOpenFileFunc; hstory = new TList; TIniFile *HistoryIni = new TIniFile(FIniFileName); int cnt = HistoryIni->ReadInteger("History", "Count", 0); if (cnt > 0) { for (int i = 0; i < cnt; i++) { phistory = new THistory; Section = "History" + AnsiString(i); phistory->dn = HistoryIni->ReadString(Section, "DirectoryName", ""); phistory->fn = HistoryIni->ReadString(Section, "FileName", ""); phistory->Index = HistoryIni->ReadInteger(Section, "Index", 0); b = HistoryIni->ReadInteger(Section, "BytesPerPixel", 0); phistory->bpp = b; hstory->Add(phistory); } RebuildHistory(); } delete HistoryIni; } //--------------------------------------------------------------------------- __fastcall TFileHistory::~TFileHistory() { THistory *phistory; AnsiString Section; MenuClear(); TIniFile *HistoryIni = new TIniFile(FIniFileName); HistoryIni->WriteInteger("History", "Count", hstory->Count); if (hstory->Count > 0) { for (int i = 0; i < hstory->Count; i++) { phistory = (THistory *) hstory->Items[i]; Section = "History" + AnsiString(i); HistoryIni->WriteString(Section, "DirectoryName", phistory->dn); HistoryIni->WriteString(Section, "FileName", phistory->fn); HistoryIni->WriteInteger(Section, "Index", phistory->Index); HistoryIni->WriteInteger(Section, "BytesPerPixel", phistory->bpp); } } delete HistoryIni; while (hstory->Count >0) { phistory = (THistory *) hstory->Last(); hstory->Remove(phistory); delete phistory; } delete hstory; } //--------------------------------------------------------------------------- void TFileHistory::RebuildHistory(void) { TMenuItem *MenuItem; THistory *phistory; int Pos; AnsiString fn; MenuClear(); if (hstory->Count > MaxHistoryItems) { for (Pos = MaxHistoryItems; Pos < hstory->Count; Pos++) { phistory = (THistory *) hstory->Items[Pos]; hstory->Remove(phistory); delete phistory; } } if (hstory->Count > 0) { for (Pos = 0; Pos < hstory->Count; Pos++) { MenuItem = new TMenuItem(FileMenu); phistory = (THistory *) hstory->Items[Pos]; MenuItem->Caption = "&" + AnsiString(Pos + 1) + " " + phistory->dn + "\\" + phistory->fn; MenuItem->OnClick = HistoryItemClick; FileMenu->Insert(Pos, MenuItem); } } } //--------------------------------------------------------------------------- void __fastcall TFileHistory::HistoryItemClick(TObject *Sender) { int Pos = FileMenu->IndexOf((TMenuItem *) Sender); THistory *phistory = (THistory *) hstory->Items[Pos]; AnsiString path = phistory->dn + "\\" + phistory->fn; if (FileExists(path/*phistory->fn*/)) { OpenFileFunc(phistory->dn, phistory->fn, phistory->Index, phistory->bpp); } else { MessageDlg("This File has been deleted by user", mtError, TMsgDlgButtons()<Remove(phistory); delete phistory; RebuildHistory(); } //--------------------------------------------------------------------------- void __fastcall TFileHistory::MenuClear() { TMenuItem *mi; AnsiString str; if (FileMenu->Count > 0) { while (FileMenu->Count > 0) { mi = (TMenuItem *) FileMenu->Items[0]; str = mi->Caption; FileMenu->Delete(0); delete mi; } } } //--------------------------------------------------------------------------- void __fastcall TFileHistory::InsertHistory(AnsiString dn, AnsiString fn, int Index, WORD bpp) { int i; THistory *phistory; AnsiString path = dn + "\\" + fn; for (i = 0; i < hstory->Count; i++) { phistory = (THistory *) hstory->Items[i]; if (phistory->fn == fn){//path) { hstory->Remove(phistory); delete phistory; } } phistory = new THistory; phistory->dn = dn; phistory->fn = fn;//path; phistory->Index = Index; phistory->bpp = bpp; hstory->Insert(0, phistory); RebuildHistory(); } //---------------------------------------------------------------------------