
Introduction
When I was working on application I realized that it needed a static splitter
with the ability to hide/show several rows or columns simultaneously.
Surprisingly I couldn't find such component. The closest solution was proposed
by Oleg Galkin
http://www.codeguru.com/splitter/show_hide_static_panes.shtml. His splitter
can hide/show one of the splitter rows (columns), but only one. A static
splitter with ability to hide/show multiple columns/rows is proposed in this
article.
Problems with algorithm extension
MFC CSplitterWnd accesses its panes by ID and ID defines the position of a pane
in the splitter. Assume that column n is to be hidden. In Oleg Galkin's
algorithm, column n gets the last column ID. The columns following n are shifted
to the left by 1, i.e. ID of n+1 control is assigned to control n, n+2 ID is
assigned to n+1, etc. Attempts to extend this approach for multiple column
hiding led to an overcomplicated algorithm. Every time you hide column, the IDs
of columns hidden in previous operations are changed again. So if you hide three
columns, the IDs of some controls are changed three times. It is a non-trivial
problem to trace all of these changes.
New approach
CExtSplitter class uses absolute and relative addresses to
operate with splitter panes. The absolute column address is an initial column
number and the relative address is current column number in splitter.
HideColumn and ShowColumn public functions work with
absolute addresses. Relative addresses are used internally. CExtSplitter
class saves pointers to all controls in an internal array. The array initialized
once when the splitter is created and doesn't change during the splitter
existence. Rows and columns of this array are used to access splitter panes by
absolute ID. If the splitter was initialized with m rows and n columns then
hiding column k (0 < k < n), means hide the column that was column k initially.
Note that after several hide operations, column k can appear in the splitter at
any position less than k.
Implementation details
CExtSplitter class has list of shown and hidden columns. The
value of a list member is an absolute column address and position is and
relative column address.
class CExtSplitter : public CSplitterWnd
{
public:
typedef std::list < int > LIST_INT;
CExtSplitter();
virtual ~CExtSplitter();
BOOL CreateStatic(CWnd* pParentWnd,
int nRows, int nCols,
DWORD dwStyle = WS_CHILD | WS_VISIBLE,
UINT nID = AFX_IDW_PANE_FIRST);
virtual BOOL CreateView( int row, int col,
CRuntimeClass* pViewClass, SIZE sizeInit,
CCreateContext* pContext );
void HideColumn(int colHide);
void ShowColumn(int colShow);
void HideRow(int colRow);
void ShowRow(int row);
public:
LIST_INT m_shown_cols; LIST_INT m_hid_cols; LIST_INT m_shown_rows; LIST_INT m_hid_rows;
protected:
C2DArray m_pane_ptr_array;
};
Function HideColumn moves column from list of shown columns to
list of hidden columns and re-numerate the splitter panes. C++ code for
re-numeration rule is shown below
void CExtSplitter::RenumeratePanes()
{
int i,j,id;
for(i=0; i < m_nMaxRows; i++)
{
for(j=0; j < m_nMaxCols; j++)
{
CPoint pos = RelToAbsPosition(i,j);
CWnd* pPane =
(CWnd*) m_pane_ptr_array(pos.x, pos.y);
ASSERT(pPane != NULL);
id=AFX_IDW_PANE_FIRST + i * 16 + j;
int r=pPane->SetDlgCtrlID(id);
ASSERT(r);
if(IsPaneVisible(pos.x,pos.y))
pPane->ShowWindow(SW_SHOW);
else
pPane->ShowWindow(SW_HIDE);
}
}
}
where RelToAbsPosition function transform the relative pane
position to the absolute pane position.
Demo project
In the demo project you can call the hide and show functions through the
"View" submenu. CExtSplitter class depends on C2DArray,
which is included to the project.