1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/headerctrl.cpp
3 // Purpose: implementation of wxHeaderCtrl for wxMSW
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
32 #include "wx/headerctrl.h"
34 #ifndef wxHAS_GENERIC_HEADERCTRL
36 #include "wx/imaglist.h"
38 #include "wx/msw/wrapcctl.h"
39 #include "wx/msw/private.h"
41 // from src/msw/listctrl.cpp
42 extern int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
44 // ============================================================================
45 // wxHeaderCtrl implementation
46 // ============================================================================
48 // ----------------------------------------------------------------------------
49 // wxHeaderCtrl construction/destruction
50 // ----------------------------------------------------------------------------
52 void wxHeaderCtrl::Init()
59 bool wxHeaderCtrl::Create(wxWindow
*parent
,
66 // notice that we don't need InitCommonControlsEx(ICC_LISTVIEW_CLASSES)
67 // here as we already call InitCommonControls() in wxApp initialization
68 // code which covers this
70 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
73 if ( !MSWCreateControl(WC_HEADER
, _T(""), pos
, size
) )
79 WXDWORD
wxHeaderCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
81 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
83 if ( style
& wxHD_ALLOW_REORDER
)
84 msStyle
|= HDS_DRAGDROP
;
86 // the control looks nicer with these styles and there doesn't seem to be
87 // any reason to not use them so we always do (as for HDS_HORZ it is 0
88 // anyhow but include it for clarity)
89 msStyle
|= HDS_HORZ
| HDS_BUTTONS
| HDS_FLAT
| HDS_FULLDRAG
| HDS_HOTTRACK
;
94 wxHeaderCtrl::~wxHeaderCtrl()
99 // ----------------------------------------------------------------------------
100 // wxHeaderCtrl scrolling
101 // ----------------------------------------------------------------------------
103 void wxHeaderCtrl::DoSetSize(int x
, int y
,
107 wxHeaderCtrlBase::DoSetSize(x
+ m_scrollOffset
, y
, w
- m_scrollOffset
, h
,
111 void wxHeaderCtrl::DoScrollHorz(int dx
)
113 // as the native control doesn't support offsetting its contents, we use a
114 // hack here to make it appear correctly when the parent is scrolled:
115 // instead of scrolling or repainting we simply move the control window
116 // itself: to be precise, offset it by the scroll increment to the left and
117 // increment its width to still extend to the right boundary to compensate
118 // for it (notice that dx is negative when scrolling to the right)
119 m_scrollOffset
+= dx
;
121 wxHeaderCtrlBase::DoSetSize(GetPosition().x
+ dx
, -1,
122 GetSize().x
- dx
, -1,
123 wxSIZE_USE_EXISTING
);
126 // ----------------------------------------------------------------------------
127 // wxHeaderCtrl geometry calculation
128 // ----------------------------------------------------------------------------
130 wxSize
wxHeaderCtrl::DoGetBestSize() const
132 RECT rc
= wxGetClientRect(GetHwndOf(GetParent()));
134 HDLAYOUT layout
= { &rc
, &wpos
};
135 if ( !Header_Layout(GetHwnd(), &layout
) )
137 wxLogLastError(_T("Header_Layout"));
138 return wxControl::DoGetBestSize();
141 return wxSize(wpos
.cx
, wpos
.cy
);
144 // ----------------------------------------------------------------------------
145 // wxHeaderCtrl columns managements
146 // ----------------------------------------------------------------------------
148 unsigned int wxHeaderCtrl::DoGetCount() const
150 // we can't use Header_GetItemCount() here because it doesn't take the
151 // hidden columns into account and we can't find the hidden columns after
152 // the last shown one in MSWFromNativeIdx() without knowing where to stop
153 // so we have to store the columns count internally
157 int wxHeaderCtrl::GetShownColumnsCount() const
159 const int numItems
= Header_GetItemCount(GetHwnd());
161 wxASSERT_MSG( numItems
>= 0 && (unsigned)numItems
<= m_numColumns
,
162 "unexpected number of items in the native control" );
167 void wxHeaderCtrl::DoSetCount(unsigned int count
)
171 // first delete all old columns
172 const unsigned countOld
= GetShownColumnsCount();
173 for ( n
= 0; n
< countOld
; n
++ )
175 if ( !Header_DeleteItem(GetHwnd(), 0) )
177 wxLogLastError(_T("Header_DeleteItem"));
181 // update the column indices order array before changing m_numColumns
182 DoResizeColumnIndices(m_colIndices
, count
);
184 // and add the new ones
185 m_numColumns
= count
;
186 m_isHidden
.resize(m_numColumns
);
187 for ( n
= 0; n
< count
; n
++ )
189 const wxHeaderColumn
& col
= GetColumn(n
);
192 m_isHidden
[n
] = false;
194 DoInsertItem(col
, n
);
196 else // hidden initially
198 m_isHidden
[n
] = true;
203 void wxHeaderCtrl::DoUpdate(unsigned int idx
)
205 // the native control does provide Header_SetItem() but it's inconvenient
206 // to use it because it sends HDN_ITEMCHANGING messages and we'd have to
207 // arrange not to block setting the width from there and the logic would be
208 // more complicated as we'd have to reset the old values as well as setting
209 // the new ones -- so instead just recreate the column
211 const wxHeaderColumn
& col
= GetColumn(idx
);
212 if ( col
.IsHidden() )
214 // column is hidden now
215 if ( !m_isHidden
[idx
] )
217 // but it wasn't hidden before, so remove it
218 Header_DeleteItem(GetHwnd(), MSWToNativeIdx(idx
));
220 m_isHidden
[idx
] = true;
222 //else: nothing to do, updating hidden column doesn't have any effect
224 else // column is shown now
226 if ( m_isHidden
[idx
] )
228 m_isHidden
[idx
] = false;
230 else // and it was shown before as well
232 // we need to remove the old column
233 Header_DeleteItem(GetHwnd(), MSWToNativeIdx(idx
));
236 DoInsertItem(col
, idx
);
240 void wxHeaderCtrl::DoInsertItem(const wxHeaderColumn
& col
, unsigned int idx
)
242 wxASSERT_MSG( !col
.IsHidden(), "should only be called for shown columns" );
246 // notice that we need to store the string we use the pointer to until we
247 // pass it to the control
248 hdi
.mask
|= HDI_TEXT
;
249 wxWxCharBuffer buf
= col
.GetTitle().wx_str();
250 hdi
.pszText
= buf
.data();
251 hdi
.cchTextMax
= wxStrlen(buf
);
253 const wxBitmap bmp
= col
.GetBitmap();
256 hdi
.mask
|= HDI_IMAGE
;
260 const int bmpWidth
= bmp
.GetWidth(),
261 bmpHeight
= bmp
.GetHeight();
265 m_imageList
= new wxImageList(bmpWidth
, bmpHeight
);
266 Header_SetImageList(GetHwnd(), GetHimagelistOf(m_imageList
));
268 else // already have an image list
270 // check that all bitmaps we use have the same size
273 m_imageList
->GetSize(0, imageWidth
, imageHeight
);
275 wxASSERT_MSG( imageWidth
== bmpWidth
&& imageHeight
== bmpHeight
,
276 "all column bitmaps must have the same size" );
279 m_imageList
->Add(bmp
);
280 hdi
.iImage
= m_imageList
->GetImageCount() - 1;
282 else // no bitmap but we still need to update the item
284 hdi
.iImage
= I_IMAGENONE
;
288 if ( col
.GetAlignment() != wxALIGN_NOT
)
290 hdi
.mask
|= HDI_FORMAT
;
291 switch ( col
.GetAlignment() )
298 case wxALIGN_CENTER_HORIZONTAL
:
299 hdi
.fmt
|= HDF_CENTER
;
303 hdi
.fmt
|= HDF_RIGHT
;
307 wxFAIL_MSG( "invalid column header alignment" );
311 if ( col
.IsSortKey() )
313 hdi
.mask
|= HDI_FORMAT
;
314 hdi
.fmt
|= col
.IsSortOrderAscending() ? HDF_SORTUP
: HDF_SORTDOWN
;
317 if ( col
.GetWidth() != wxCOL_WIDTH_DEFAULT
)
319 hdi
.mask
|= HDI_WIDTH
;
320 hdi
.cxy
= col
.GetWidth();
323 hdi
.mask
|= HDI_ORDER
;
324 hdi
.iOrder
= MSWToNativeOrder(m_colIndices
.Index(idx
));
326 if ( ::SendMessage(GetHwnd(), HDM_INSERTITEM
,
327 MSWToNativeIdx(idx
), (LPARAM
)&hdi
) == -1 )
329 wxLogLastError(_T("Header_InsertItem()"));
333 void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt
& order
)
335 wxArrayInt orderShown
;
336 orderShown
.reserve(m_numColumns
);
338 for ( unsigned n
= 0; n
< m_numColumns
; n
++ )
340 const int idx
= order
[n
];
341 if ( GetColumn(idx
).IsShown() )
342 orderShown
.push_back(MSWToNativeIdx(idx
));
345 if ( !Header_SetOrderArray(GetHwnd(), orderShown
.size(), &orderShown
[0]) )
347 wxLogLastError(_T("Header_GetOrderArray"));
350 m_colIndices
= order
;
353 wxArrayInt
wxHeaderCtrl::DoGetColumnsOrder() const
355 // we don't use Header_GetOrderArray() here because it doesn't return
356 // information about the hidden columns, instead we just save the columns
357 // order array in DoSetColumnsOrder() and update it when they're reordered
361 // ----------------------------------------------------------------------------
362 // wxHeaderCtrl indexes and positions translation
363 // ----------------------------------------------------------------------------
365 int wxHeaderCtrl::MSWToNativeIdx(int idx
)
367 // don't check for GetColumn(idx).IsShown() as it could have just became
368 // false and we may be called from DoUpdate() to delete the old column
369 wxASSERT_MSG( !m_isHidden
[idx
],
370 "column must be visible to have an "
371 "index in the native control" );
374 for ( int i
= 0; i
< idx
; i
++ )
376 if ( GetColumn(i
).IsHidden() )
377 item
--; // one less column the native control knows about
380 wxASSERT_MSG( item
>= 0 && item
<= GetShownColumnsCount(), "logic error" );
385 int wxHeaderCtrl::MSWFromNativeIdx(int item
)
387 wxASSERT_MSG( item
>= 0 && item
< GetShownColumnsCount(),
388 "column index out of range" );
390 // reverse the above function
393 for ( unsigned n
= 0; n
< m_numColumns
; n
++ )
398 if ( GetColumn(n
).IsHidden() )
402 wxASSERT_MSG( MSWToNativeIdx(idx
) == item
, "logic error" );
407 int wxHeaderCtrl::MSWToNativeOrder(int pos
)
409 wxASSERT_MSG( pos
>= 0 && static_cast<unsigned>(pos
) < m_numColumns
,
410 "column position out of range" );
413 for ( int n
= 0; n
< pos
; n
++ )
415 if ( GetColumn(m_colIndices
[n
]).IsHidden() )
419 wxASSERT_MSG( order
>= 0 && order
<= GetShownColumnsCount(), "logic error" );
424 int wxHeaderCtrl::MSWFromNativeOrder(int order
)
426 wxASSERT_MSG( order
>= 0 && order
< GetShownColumnsCount(),
427 "native column position out of range" );
429 unsigned pos
= order
;
430 for ( unsigned n
= 0; n
< m_numColumns
; n
++ )
435 if ( GetColumn(m_colIndices
[n
]).IsHidden() )
439 wxASSERT_MSG( MSWToNativeOrder(pos
) == order
, "logic error" );
444 // ----------------------------------------------------------------------------
445 // wxHeaderCtrl events
446 // ----------------------------------------------------------------------------
448 wxEventType
wxHeaderCtrl::GetClickEventType(bool dblclk
, int button
)
454 evtType
= dblclk
? wxEVT_COMMAND_HEADER_DCLICK
455 : wxEVT_COMMAND_HEADER_CLICK
;
459 evtType
= dblclk
? wxEVT_COMMAND_HEADER_RIGHT_DCLICK
460 : wxEVT_COMMAND_HEADER_RIGHT_CLICK
;
464 evtType
= dblclk
? wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
465 : wxEVT_COMMAND_HEADER_MIDDLE_CLICK
;
469 wxFAIL_MSG( wxS("unexpected event type") );
470 evtType
= wxEVT_NULL
;
476 bool wxHeaderCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
478 NMHEADER
* const nmhdr
= (NMHEADER
*)lParam
;
480 wxEventType evtType
= wxEVT_NULL
;
484 const UINT code
= nmhdr
->hdr
.code
;
486 // we don't have the index for all events, e.g. not for NM_RELEASEDCAPTURE
487 // so only access for header control events (and yes, the direction of
488 // comparisons with FIRST/LAST is correct even if it seems inverted)
489 int idx
= code
<= HDN_FIRST
&& code
> HDN_LAST
? nmhdr
->iItem
: -1;
492 // we also get bogus HDN_BEGINDRAG with -1 index so don't call
493 // MSWFromNativeIdx() unconditionally for nmhdr->iItem
494 idx
= MSWFromNativeIdx(idx
);
503 case HDN_ITEMDBLCLICK
:
504 evtType
= GetClickEventType(code
== HDN_ITEMDBLCLICK
, nmhdr
->iButton
);
507 // although we should get the notifications about the right clicks
508 // via HDN_ITEM[DBL]CLICK too according to MSDN this simply doesn't
509 // happen in practice on any Windows system up to 2003
514 idx
= wxMSWGetColumnClicked(&nmhdr
->hdr
, &pt
);
515 if ( idx
!= wxNOT_FOUND
)
517 idx
= MSWFromNativeIdx(idx
);
518 evtType
= GetClickEventType(code
== NM_RDBLCLK
, 1);
520 //else: ignore clicks outside any column
524 case HDN_DIVIDERDBLCLICK
:
525 evtType
= wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
;
529 // column resizing events
530 // ----------------------
532 // see comments in wxListCtrl::MSWOnNotify() for why we catch both
533 // ASCII and Unicode versions of this message
534 case HDN_BEGINTRACKA
:
535 case HDN_BEGINTRACKW
:
536 // non-resizeable columns can't be resized no matter what, don't
537 // even generate any events for them
538 if ( !GetColumn(idx
).IsResizeable() )
544 evtType
= wxEVT_COMMAND_HEADER_BEGIN_RESIZE
;
549 width
= nmhdr
->pitem
->cxy
;
551 if ( evtType
== wxEVT_NULL
)
553 evtType
= wxEVT_COMMAND_HEADER_END_RESIZE
;
555 // don't generate events with invalid width
556 const int minWidth
= GetColumn(idx
).GetMinWidth();
557 if ( width
< minWidth
)
562 case HDN_ITEMCHANGING
:
563 if ( nmhdr
->pitem
&& (nmhdr
->pitem
->mask
& HDI_WIDTH
) )
565 // prevent the column from being shrunk beneath its min width
566 width
= nmhdr
->pitem
->cxy
;
567 if ( width
< GetColumn(idx
).GetMinWidth() )
569 // don't generate any events and prevent the change from
573 else // width is acceptable
575 // generate the resizing event from here as we don't seem
576 // to be getting HDN_TRACK events at all, at least with
578 evtType
= wxEVT_COMMAND_HEADER_RESIZING
;
584 // column reordering events
585 // ------------------------
588 // Windows sometimes sends us events with invalid column indices
589 if ( nmhdr
->iItem
== -1 )
592 // column must have the appropriate flag to be draggable
593 if ( !GetColumn(idx
).IsReorderable() )
599 evtType
= wxEVT_COMMAND_HEADER_BEGIN_REORDER
;
603 wxASSERT_MSG( nmhdr
->pitem
->mask
& HDI_ORDER
, "should have order" );
604 order
= nmhdr
->pitem
->iOrder
;
606 // we also get messages with invalid order when column reordering
607 // is cancelled (e.g. by pressing Esc)
611 order
= MSWFromNativeOrder(order
);
613 evtType
= wxEVT_COMMAND_HEADER_END_REORDER
;
616 case NM_RELEASEDCAPTURE
:
617 evtType
= wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
;
622 // do generate the corresponding wx event
623 if ( evtType
!= wxEVT_NULL
)
625 wxHeaderCtrlEvent
event(evtType
, GetId());
626 event
.SetEventObject(this);
627 event
.SetColumn(idx
);
628 event
.SetWidth(width
);
630 event
.SetNewOrder(order
);
632 if ( GetEventHandler()->ProcessEvent(event
) )
634 if ( event
.IsAllowed() )
635 return true; // skip default message handling below
637 // we need to veto the default handling of this message, don't
638 // return to execute the code in the "if veto" branch below
641 else // not processed
643 // special post-processing for HDN_ENDDRAG: we need to update the
644 // internal column indices array if this is allowed to go ahead as
645 // the native control is going to reorder its columns now
646 if ( evtType
== wxEVT_COMMAND_HEADER_END_REORDER
)
647 MoveColumnInOrderArray(m_colIndices
, idx
, order
);
653 // all of HDN_BEGIN{DRAG,TRACK}, HDN_TRACK and HDN_ITEMCHANGING
654 // interpret TRUE return value as meaning to stop the control
655 // default handling of the message
661 return wxHeaderCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
664 #endif // wxHAS_GENERIC_HEADERCTRL
666 #endif // wxUSE_HEADERCTRL