/** Saves a tree control's state. **/ void TemplateManager::SaveTreeState(CTreeCtrlEx& tree) { // Save the tree state. m_treeStateFile.SetLength(0); // Grab all the template file stuff and save it, so we can later save this // information to the registry. WORD wCount = GetCount(); m_treeStateFile.Write(&wCount, sizeof(WORD)); for (int i = 0; i < (int)wCount; i++) { // Get the template file pointer. WWhizTemplateGroup* file = GetTemplateGroup(i); // Write out the template file name. WORD len = file->GetFilename().GetLength(); m_treeStateFile.Write(&len, sizeof(WORD)); m_treeStateFile.Write((LPCTSTR)file->GetFilename(), len); // Write out the time stamp. unsigned __int64 time = file->GetTimeStamp().GetTime(); m_treeStateFile.Write(&time, sizeof(unsigned __int64)); } // Write the tree item count. DWORD dwCount = tree.GetCount(); m_treeStateFile.Write(&dwCount, sizeof(DWORD)); // Now save all the tree branch stuff. CTreeCursor cursor = tree.GetRootItem(); while (cursor) { UINT state = cursor.GetState(0xFF); m_treeStateFile.Write(&state, sizeof(UINT)); cursor = cursor.GetNext(); } }
/** Restores a tree control's state. **/ bool TemplateManager::RestoreTreeState(CTreeCtrlEx& tree) { // Check the file length. if (m_treeStateFile.GetLength() == 0) return false; // Unchanged. // Restore the tree state. m_treeStateFile.Seek(0, CFile::begin); // Read the template file count. WORD wCount; m_treeStateFile.Read(&wCount, sizeof(WORD)); if ((int)wCount != GetCount()) return true; // Changed. // Run each template file. for (int i = 0; i < (int)wCount; i++) { CString filename; // Read in the template file name. WORD len; m_treeStateFile.Read(&len, sizeof(WORD)); LPTSTR buf = filename.GetBufferSetLength(len); m_treeStateFile.Read(buf, len); filename.ReleaseBuffer(len); // Write out the time stamp. unsigned __int64 timeIn; m_treeStateFile.Read(&timeIn, sizeof(unsigned __int64)); time_t time = timeIn; // Now, find it. int j; for (j = 0; j < GetCount(); j++) { WWhizTemplateGroup* file = GetTemplateGroup(j); // If we found a match, continue in the outer loop. if (file->GetFilename() == filename && file->GetTimeStamp() == time) break; } // If we reached the end, then the entry wasn't found. if (j == GetCount()) return true; // Changed. } // Read the tree item count. DWORD dwCount; m_treeStateFile.Read(&dwCount, sizeof(DWORD)); // One more sanity check. if (dwCount != tree.GetCount()) return true; // Changed. // Read the tree info. CTreeCursor selectedCursor; CTreeCursor cursor = tree.GetRootItem(); while (cursor) { UINT state; state = cursor.GetState(0xFF); m_treeStateFile.Read(&state, sizeof(UINT)); if (state & TVIS_EXPANDED) cursor.Expand(); else cursor.Expand(TVE_COLLAPSE); if (state & TVIS_SELECTED) { cursor.Select(TVGN_CARET | TVGN_FIRSTVISIBLE); cursor.Select(); selectedCursor = cursor; } cursor = cursor.GetNext(); } selectedCursor.EnsureVisible(); return false; }