static HTREEITEM TreeItemForPageNo(WindowInfo *win, HTREEITEM hItem, int pageNo) { HTREEITEM hCurrItem = nullptr; while (hItem) { TVITEM item; item.hItem = hItem; item.mask = TVIF_PARAM | TVIF_STATE; item.stateMask = TVIS_EXPANDED; TreeView_GetItem(win->hwndTocTree, &item); // return if this item is on the specified page (or on a latter page) if (item.lParam) { int page = ((DocTocItem *)item.lParam)->pageNo; if (1 <= page && page <= pageNo) hCurrItem = hItem; if (page >= pageNo) break; } // find any child item closer to the specified page HTREEITEM hSubItem = nullptr; if ((item.state & TVIS_EXPANDED)) hSubItem = TreeItemForPageNo(win, TreeView_GetChild(win->hwndTocTree, hItem), pageNo); if (hSubItem) hCurrItem = hSubItem; hItem = TreeView_GetNextSibling(win->hwndTocTree, hItem); } return hCurrItem; }
void UpdateTocSelection(WindowInfo *win, int currPageNo) { if (!win->tocLoaded || !win->tocVisible || win->tocKeepSelection) return; HTREEITEM hRoot = TreeView_GetRoot(win->hwndTocTree); if (!hRoot) return; // select the item closest to but not after the current page // (or the root item, if there's no such item) HTREEITEM hItem = TreeItemForPageNo(win, hRoot, currPageNo); TreeView_SelectItem(win->hwndTocTree, hItem); }