//--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "History.h" #include "Menus.hpp" #include "ComCtrls.hpp" //--------------------------------------------------------------------------- #pragma package(smart_init) TFileHistory *FileHistory; //--------------------------------------------------------------------------- //__fastcall TFileHistory::TFileHistory(String IniFileName, int theMaxHistoryItems, // TMenuItem *theFileMenu, TOpenFileFunc theOpenFileFunc) __fastcall TFileHistory::TFileHistory(String IniFileName, int theMaxHistoryItems, TMenuItem *theFileMenu, TOpenFileFunc theOpenFileFunc) { THistory *phistory; int b; //String Section; String 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" + String(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; */ int cnt = GetPrivateProfileInt(String("History").c_str(), String("Count").c_str(), 0, IniFileName.c_str()); if (cnt > 0) { Char tempStr[256]; for (int i = 0; i < cnt; i++) { phistory = new THistory; Section = String("History") + String(i); GetPrivateProfileString(Section.c_str(), String("DirectoryName").c_str(), String("").c_str(), tempStr, 256, IniFileName.c_str()); phistory->dn = String(tempStr); GetPrivateProfileString(Section.c_str(), String("FileName").c_str(), String("").c_str(), tempStr, 256, IniFileName.c_str()); phistory->fn = String(tempStr); phistory->Index = GetPrivateProfileInt(Section.c_str(), String("Index").c_str(), 0, IniFileName.c_str()); phistory->bpp = GetPrivateProfileInt(Section.c_str(), String("BytesPerPixel").c_str(), 0, IniFileName.c_str()); hstory->Add(phistory); } RebuildHistory(); } } //--------------------------------------------------------------------------- __fastcall TFileHistory::~TFileHistory() { THistory *phistory; //String Section; String 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" + String(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; */ WritePrivateProfileString(String("History").c_str(), String("Count").c_str(), String(hstory->Count).c_str(), FIniFileName.c_str()); if(hstory->Count > 0) { for (int i = 0; i < hstory->Count; i++) { phistory = (THistory *) hstory->Items[i]; Section = String("History") + String(i); WritePrivateProfileString(Section.c_str(), String("DirectoryName").c_str(), phistory->dn.c_str(), FIniFileName.c_str()); WritePrivateProfileString(Section.c_str(), String("FileName").c_str(), phistory->fn.c_str(), FIniFileName.c_str()); WritePrivateProfileString(Section.c_str(), String("Index").c_str(), String(phistory->Index).c_str(), FIniFileName.c_str()); WritePrivateProfileString(Section.c_str(), String("BytesPerPixel").c_str(), String(phistory->bpp).c_str(), FIniFileName.c_str()); } } while (hstory->Count >0) { phistory = (THistory *) hstory->Last(); hstory->Remove(phistory); delete phistory; } delete hstory; } //--------------------------------------------------------------------------- void TFileHistory::RebuildHistory(void) { //TMenuItem *MenuItem; TMenuItem *MenuItem; THistory *phistory; int Pos; 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); MenuItem = new TMenuItem(FileMenu); phistory = (THistory *) hstory->Items[Pos]; MenuItem->Caption = String("&") + String(Pos + 1) + String(" ") + phistory->dn + String("\\") + phistory->fn; MenuItem->OnClick = HistoryItemClick; FileMenu->Insert(Pos, MenuItem); } } } //--------------------------------------------------------------------------- void __fastcall TFileHistory::HistoryItemClick(TObject *Sender) { //int Pos = FileMenu->IndexOf((TMenuItem *) Sender); int Pos = FileMenu->IndexOf((TMenuItem *) Sender); THistory *phistory = (THistory *) hstory->Items[Pos]; //String path = phistory->dn + "\\" + phistory->fn; String path = phistory->dn + String("\\") + 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; TMenuItem *mi; if (FileMenu->Count > 0) { while (FileMenu->Count > 0) { //mi = (TMenuItem *) FileMenu->Items[0]; mi = (TMenuItem *) FileMenu->Items[0]; FileMenu->Delete(0); delete mi; } } } //--------------------------------------------------------------------------- //void __fastcall TFileHistory::InsertHistory(String dn, String fn, int Index, WORD bpp) void __fastcall TFileHistory::InsertHistory(String dn, String fn, int Index, WORD bpp) { int i; THistory *phistory; //String 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(); } //---------------------------------------------------------------------------