static void AddTreeItem(CTreeCursor cursor, UINT id, CString name) { int curPos = 0; int nextPos; // Autoexpand. bool autoExpand = false; if (name[0] == '+') { curPos++; autoExpand = true; } while (true) { // See if there is a sub-tree. nextPos = name.Find('|', curPos); if (nextPos == -1) break; CString branchName = name.Mid(curPos, nextPos - curPos); curPos = nextPos + 1; cursor = AddTreeBranch(cursor, branchName); if (autoExpand) cursor.GetParent().Expand(); cursor.SortChildren(); } CTreeCursor lastBranchCursor; lastBranchCursor = cursor.AddTail(name.Mid(curPos)); lastBranchCursor.SetData(id); if (autoExpand) { lastBranchCursor.Expand(); lastBranchCursor.GetParent().Expand(); } cursor.SortChildren(); }
/** 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; }