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"
33 #include "wx/headerctrl.h"
35 #ifndef wxHAS_GENERIC_HEADERCTRL
37 #include "wx/imaglist.h"
39 #include "wx/msw/wrapcctl.h"
40 #include "wx/msw/private.h"
42 #ifndef HDM_SETBITMAPMARGIN
43 #define HDM_SETBITMAPMARGIN 0x1234
46 #ifndef Header_SetBitmapMargin
47 #define Header_SetBitmapMargin(hwnd, margin) \
48 ::SendMessage((hwnd), HDM_SETBITMAPMARGIN, (WPARAM)(margin), 0)
51 // from src/msw/listctrl.cpp
52 extern int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
54 // ============================================================================
55 // wxHeaderCtrl implementation
56 // ============================================================================
58 // ----------------------------------------------------------------------------
59 // wxHeaderCtrl construction/destruction
60 // ----------------------------------------------------------------------------
62 void wxHeaderCtrl::Init()
67 m_colBeingDragged
= -1;
70 bool wxHeaderCtrl::Create(wxWindow
*parent
,
77 // notice that we don't need InitCommonControlsEx(ICC_LISTVIEW_CLASSES)
78 // here as we already call InitCommonControls() in wxApp initialization
79 // code which covers this
81 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
84 if ( !MSWCreateControl(WC_HEADER
, wxT(""), pos
, size
) )
87 // special hack for margins when using comctl32.dll v6 or later: the
88 // default margin is too big and results in label truncation when the
89 // column width is just about right to show it together with the sort
90 // indicator, so reduce it to a smaller value (in principle we could even
91 // use 0 here but this starts to look ugly)
92 if ( wxApp::GetComCtl32Version() >= 600 )
94 Header_SetBitmapMargin(GetHwnd(), ::GetSystemMetrics(SM_CXEDGE
));
100 WXDWORD
wxHeaderCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
102 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
104 if ( style
& wxHD_ALLOW_REORDER
)
105 msStyle
|= HDS_DRAGDROP
;
107 // the control looks nicer with these styles and there doesn't seem to be
108 // any reason to not use them so we always do (as for HDS_HORZ it is 0
109 // anyhow but include it for clarity)
110 // NOTE: don't use however HDS_FLAT because it makes the control look
111 // non-native when running WinXP in classic mode
112 msStyle
|= HDS_HORZ
| HDS_BUTTONS
| HDS_FULLDRAG
| HDS_HOTTRACK
;
117 wxHeaderCtrl::~wxHeaderCtrl()
122 // ----------------------------------------------------------------------------
123 // wxHeaderCtrl scrolling
124 // ----------------------------------------------------------------------------
126 void wxHeaderCtrl::DoSetSize(int x
, int y
,
130 wxHeaderCtrlBase::DoSetSize(x
+ m_scrollOffset
, y
, w
- m_scrollOffset
, h
,
134 void wxHeaderCtrl::DoScrollHorz(int dx
)
136 // as the native control doesn't support offsetting its contents, we use a
137 // hack here to make it appear correctly when the parent is scrolled:
138 // instead of scrolling or repainting we simply move the control window
139 // itself: to be precise, offset it by the scroll increment to the left and
140 // increment its width to still extend to the right boundary to compensate
141 // for it (notice that dx is negative when scrolling to the right)
142 m_scrollOffset
+= dx
;
144 wxHeaderCtrlBase::DoSetSize(GetPosition().x
+ dx
, -1,
145 GetSize().x
- dx
, -1,
146 wxSIZE_USE_EXISTING
);
149 // ----------------------------------------------------------------------------
150 // wxHeaderCtrl geometry calculation
151 // ----------------------------------------------------------------------------
153 wxSize
wxHeaderCtrl::DoGetBestSize() const
155 RECT rc
= wxGetClientRect(GetHwndOf(GetParent()));
157 HDLAYOUT layout
= { &rc
, &wpos
};
158 if ( !Header_Layout(GetHwnd(), &layout
) )
160 wxLogLastError(wxT("Header_Layout"));
161 return wxControl::DoGetBestSize();
164 return wxSize(wpos
.cx
, wpos
.cy
);
167 // ----------------------------------------------------------------------------
168 // wxHeaderCtrl columns managements
169 // ----------------------------------------------------------------------------
171 unsigned int wxHeaderCtrl::DoGetCount() const
173 // we can't use Header_GetItemCount() here because it doesn't take the
174 // hidden columns into account and we can't find the hidden columns after
175 // the last shown one in MSWFromNativeIdx() without knowing where to stop
176 // so we have to store the columns count internally
180 int wxHeaderCtrl::GetShownColumnsCount() const
182 const int numItems
= Header_GetItemCount(GetHwnd());
184 wxASSERT_MSG( numItems
>= 0 && (unsigned)numItems
<= m_numColumns
,
185 "unexpected number of items in the native control" );
190 void wxHeaderCtrl::DoSetCount(unsigned int count
)
194 // first delete all old columns
195 const unsigned countOld
= GetShownColumnsCount();
196 for ( n
= 0; n
< countOld
; n
++ )
198 if ( !Header_DeleteItem(GetHwnd(), 0) )
200 wxLogLastError(wxT("Header_DeleteItem"));
204 // update the column indices order array before changing m_numColumns
205 DoResizeColumnIndices(m_colIndices
, count
);
207 // and add the new ones
208 m_numColumns
= count
;
209 m_isHidden
.resize(m_numColumns
);
210 for ( n
= 0; n
< count
; n
++ )
212 const wxHeaderColumn
& col
= GetColumn(n
);
215 m_isHidden
[n
] = false;
217 DoInsertItem(col
, n
);
219 else // hidden initially
221 m_isHidden
[n
] = true;
226 void wxHeaderCtrl::DoUpdate(unsigned int idx
)
228 // the native control does provide Header_SetItem() but it's inconvenient
229 // to use it because it sends HDN_ITEMCHANGING messages and we'd have to
230 // arrange not to block setting the width from there and the logic would be
231 // more complicated as we'd have to reset the old values as well as setting
232 // the new ones -- so instead just recreate the column
234 const wxHeaderColumn
& col
= GetColumn(idx
);
235 if ( col
.IsHidden() )
237 // column is hidden now
238 if ( !m_isHidden
[idx
] )
240 // but it wasn't hidden before, so remove it
241 Header_DeleteItem(GetHwnd(), MSWToNativeIdx(idx
));
243 m_isHidden
[idx
] = true;
245 //else: nothing to do, updating hidden column doesn't have any effect
247 else // column is shown now
249 if ( m_isHidden
[idx
] )
251 m_isHidden
[idx
] = false;
253 else // and it was shown before as well
255 // we need to remove the old column
256 Header_DeleteItem(GetHwnd(), MSWToNativeIdx(idx
));
259 DoInsertItem(col
, idx
);
263 void wxHeaderCtrl::DoInsertItem(const wxHeaderColumn
& col
, unsigned int idx
)
265 wxASSERT_MSG( !col
.IsHidden(), "should only be called for shown columns" );
269 // notice that we need to store the string we use the pointer to until we
270 // pass it to the control
271 hdi
.mask
|= HDI_TEXT
;
272 wxWxCharBuffer buf
= col
.GetTitle().t_str();
273 hdi
.pszText
= buf
.data();
274 hdi
.cchTextMax
= wxStrlen(buf
);
276 const wxBitmap bmp
= col
.GetBitmap();
279 hdi
.mask
|= HDI_IMAGE
;
283 const int bmpWidth
= bmp
.GetWidth(),
284 bmpHeight
= bmp
.GetHeight();
288 m_imageList
= new wxImageList(bmpWidth
, bmpHeight
);
289 (void) // suppress mingw32 warning about unused computed value
290 Header_SetImageList(GetHwnd(), GetHimagelistOf(m_imageList
));
292 else // already have an image list
294 // check that all bitmaps we use have the same size
297 m_imageList
->GetSize(0, imageWidth
, imageHeight
);
299 wxASSERT_MSG( imageWidth
== bmpWidth
&& imageHeight
== bmpHeight
,
300 "all column bitmaps must have the same size" );
303 m_imageList
->Add(bmp
);
304 hdi
.iImage
= m_imageList
->GetImageCount() - 1;
306 else // no bitmap but we still need to update the item
308 hdi
.iImage
= I_IMAGENONE
;
312 if ( col
.GetAlignment() != wxALIGN_NOT
)
314 hdi
.mask
|= HDI_FORMAT
| HDF_LEFT
;
315 switch ( col
.GetAlignment() )
322 case wxALIGN_CENTER_HORIZONTAL
:
323 hdi
.fmt
|= HDF_CENTER
;
327 hdi
.fmt
|= HDF_RIGHT
;
331 wxFAIL_MSG( "invalid column header alignment" );
335 if ( col
.IsSortKey() )
337 hdi
.mask
|= HDI_FORMAT
;
338 hdi
.fmt
|= col
.IsSortOrderAscending() ? HDF_SORTUP
: HDF_SORTDOWN
;
341 if ( col
.GetWidth() != wxCOL_WIDTH_DEFAULT
)
343 hdi
.mask
|= HDI_WIDTH
;
344 hdi
.cxy
= col
.GetWidth();
347 hdi
.mask
|= HDI_ORDER
;
348 hdi
.iOrder
= MSWToNativeOrder(m_colIndices
.Index(idx
));
350 if ( ::SendMessage(GetHwnd(), HDM_INSERTITEM
,
351 MSWToNativeIdx(idx
), (LPARAM
)&hdi
) == -1 )
353 wxLogLastError(wxT("Header_InsertItem()"));
357 void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt
& order
)
359 wxArrayInt orderShown
;
360 orderShown
.reserve(m_numColumns
);
362 for ( unsigned n
= 0; n
< m_numColumns
; n
++ )
364 const int idx
= order
[n
];
365 if ( GetColumn(idx
).IsShown() )
366 orderShown
.push_back(MSWToNativeIdx(idx
));
369 if ( !Header_SetOrderArray(GetHwnd(), orderShown
.size(), &orderShown
[0]) )
371 wxLogLastError(wxT("Header_GetOrderArray"));
374 m_colIndices
= order
;
377 wxArrayInt
wxHeaderCtrl::DoGetColumnsOrder() const
379 // we don't use Header_GetOrderArray() here because it doesn't return
380 // information about the hidden columns, instead we just save the columns
381 // order array in DoSetColumnsOrder() and update it when they're reordered
385 // ----------------------------------------------------------------------------
386 // wxHeaderCtrl indexes and positions translation
387 // ----------------------------------------------------------------------------
389 int wxHeaderCtrl::MSWToNativeIdx(int idx
)
391 // don't check for GetColumn(idx).IsShown() as it could have just became
392 // false and we may be called from DoUpdate() to delete the old column
393 wxASSERT_MSG( !m_isHidden
[idx
],
394 "column must be visible to have an "
395 "index in the native control" );
398 for ( int i
= 0; i
< idx
; i
++ )
400 if ( GetColumn(i
).IsHidden() )
401 item
--; // one less column the native control knows about
404 wxASSERT_MSG( item
>= 0 && item
<= GetShownColumnsCount(), "logic error" );
409 int wxHeaderCtrl::MSWFromNativeIdx(int item
)
411 wxASSERT_MSG( item
>= 0 && item
< GetShownColumnsCount(),
412 "column index out of range" );
414 // reverse the above function
417 for ( unsigned n
= 0; n
< m_numColumns
; n
++ )
422 if ( GetColumn(n
).IsHidden() )
426 wxASSERT_MSG( MSWToNativeIdx(idx
) == item
, "logic error" );
431 int wxHeaderCtrl::MSWToNativeOrder(int pos
)
433 wxASSERT_MSG( pos
>= 0 && static_cast<unsigned>(pos
) < m_numColumns
,
434 "column position out of range" );
437 for ( int n
= 0; n
< pos
; n
++ )
439 if ( GetColumn(m_colIndices
[n
]).IsHidden() )
443 wxASSERT_MSG( order
>= 0 && order
<= GetShownColumnsCount(), "logic error" );
448 int wxHeaderCtrl::MSWFromNativeOrder(int order
)
450 wxASSERT_MSG( order
>= 0 && order
< GetShownColumnsCount(),
451 "native column position out of range" );
453 unsigned pos
= order
;
454 for ( unsigned n
= 0; n
< m_numColumns
; n
++ )
459 if ( GetColumn(m_colIndices
[n
]).IsHidden() )
463 wxASSERT_MSG( MSWToNativeOrder(pos
) == order
, "logic error" );
468 // ----------------------------------------------------------------------------
469 // wxHeaderCtrl events
470 // ----------------------------------------------------------------------------
472 wxEventType
wxHeaderCtrl::GetClickEventType(bool dblclk
, int button
)
478 evtType
= dblclk
? wxEVT_COMMAND_HEADER_DCLICK
479 : wxEVT_COMMAND_HEADER_CLICK
;
483 evtType
= dblclk
? wxEVT_COMMAND_HEADER_RIGHT_DCLICK
484 : wxEVT_COMMAND_HEADER_RIGHT_CLICK
;
488 evtType
= dblclk
? wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
489 : wxEVT_COMMAND_HEADER_MIDDLE_CLICK
;
493 wxFAIL_MSG( wxS("unexpected event type") );
494 evtType
= wxEVT_NULL
;
500 bool wxHeaderCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
502 NMHEADER
* const nmhdr
= (NMHEADER
*)lParam
;
504 wxEventType evtType
= wxEVT_NULL
;
508 const UINT code
= nmhdr
->hdr
.code
;
510 // we don't have the index for all events, e.g. not for NM_RELEASEDCAPTURE
511 // so only access for header control events (and yes, the direction of
512 // comparisons with FIRST/LAST is correct even if it seems inverted)
513 int idx
= code
<= HDN_FIRST
&& code
> HDN_LAST
? nmhdr
->iItem
: -1;
516 // we also get bogus HDN_BEGINDRAG with -1 index so don't call
517 // MSWFromNativeIdx() unconditionally for nmhdr->iItem
518 idx
= MSWFromNativeIdx(idx
);
527 case HDN_ITEMDBLCLICK
:
528 evtType
= GetClickEventType(code
== HDN_ITEMDBLCLICK
, nmhdr
->iButton
);
530 // We're not dragging any more.
531 m_colBeingDragged
= -1;
534 // although we should get the notifications about the right clicks
535 // via HDN_ITEM[DBL]CLICK too according to MSDN this simply doesn't
536 // happen in practice on any Windows system up to 2003
541 idx
= wxMSWGetColumnClicked(&nmhdr
->hdr
, &pt
);
542 if ( idx
!= wxNOT_FOUND
)
544 idx
= MSWFromNativeIdx(idx
);
546 // due to a bug in mingw32 headers NM_RDBLCLK is signed
547 // there so we need a cast to avoid warnings about signed/
548 // unsigned comparison
549 evtType
= GetClickEventType(
550 code
== static_cast<UINT
>(NM_RDBLCLK
), 1);
552 //else: ignore clicks outside any column
556 case HDN_DIVIDERDBLCLICK
:
557 evtType
= wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
;
561 // column resizing events
562 // ----------------------
564 // see comments in wxListCtrl::MSWOnNotify() for why we catch both
565 // ASCII and Unicode versions of this message
566 case HDN_BEGINTRACKA
:
567 case HDN_BEGINTRACKW
:
568 // non-resizable columns can't be resized no matter what, don't
569 // even generate any events for them
570 if ( !GetColumn(idx
).IsResizeable() )
576 evtType
= wxEVT_COMMAND_HEADER_BEGIN_RESIZE
;
581 width
= nmhdr
->pitem
->cxy
;
583 if ( evtType
== wxEVT_NULL
)
585 evtType
= wxEVT_COMMAND_HEADER_END_RESIZE
;
587 // don't generate events with invalid width
588 const int minWidth
= GetColumn(idx
).GetMinWidth();
589 if ( width
< minWidth
)
594 // The control is not supposed to send HDN_TRACK when using
595 // HDS_FULLDRAG (which we do use) but apparently some versions of
596 // comctl32.dll still do it, see #13506, so catch both messages
597 // just in case we are dealing with one of these buggy versions.
599 case HDN_ITEMCHANGING
:
600 if ( nmhdr
->pitem
&& (nmhdr
->pitem
->mask
& HDI_WIDTH
) )
602 // prevent the column from being shrunk beneath its min width
603 width
= nmhdr
->pitem
->cxy
;
604 if ( width
< GetColumn(idx
).GetMinWidth() )
606 // don't generate any events and prevent the change from
610 else // width is acceptable
612 // generate the resizing event from here as we don't seem
613 // to be getting HDN_TRACK events at all, at least with
615 evtType
= wxEVT_COMMAND_HEADER_RESIZING
;
621 // column reordering events
622 // ------------------------
625 // Windows sometimes sends us events with invalid column indices
626 if ( nmhdr
->iItem
== -1 )
629 // If we are dragging a column that is not draggable and the mouse
630 // is moved over a different column then we get the column number from
631 // the column under the mouse. This results in an unexpected behaviour
632 // if this column is draggable. To prevent this remember the column we
633 // are dragging for the complete drag and drop cycle.
634 if ( m_colBeingDragged
== -1 )
636 m_colBeingDragged
= idx
;
639 // column must have the appropriate flag to be draggable
640 if ( !GetColumn(m_colBeingDragged
).IsReorderable() )
646 evtType
= wxEVT_COMMAND_HEADER_BEGIN_REORDER
;
650 wxASSERT_MSG( nmhdr
->pitem
->mask
& HDI_ORDER
, "should have order" );
651 order
= nmhdr
->pitem
->iOrder
;
653 // we also get messages with invalid order when column reordering
654 // is cancelled (e.g. by pressing Esc)
658 order
= MSWFromNativeOrder(order
);
660 evtType
= wxEVT_COMMAND_HEADER_END_REORDER
;
662 // We (successfully) ended dragging the column.
663 m_colBeingDragged
= -1;
666 case NM_RELEASEDCAPTURE
:
667 evtType
= wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
;
669 // Dragging the column was cancelled.
670 m_colBeingDragged
= -1;
675 // do generate the corresponding wx event
676 if ( evtType
!= wxEVT_NULL
)
678 wxHeaderCtrlEvent
event(evtType
, GetId());
679 event
.SetEventObject(this);
680 event
.SetColumn(idx
);
681 event
.SetWidth(width
);
683 event
.SetNewOrder(order
);
685 const bool processed
= GetEventHandler()->ProcessEvent(event
);
687 if ( processed
&& !event
.IsAllowed() )
692 // special post-processing for HDN_ENDDRAG: we need to update the
693 // internal column indices array if this is allowed to go ahead as
694 // the native control is going to reorder its columns now
695 if ( evtType
== wxEVT_COMMAND_HEADER_END_REORDER
)
696 MoveColumnInOrderArray(m_colIndices
, idx
, order
);
700 // skip default processing below
708 // all of HDN_BEGIN{DRAG,TRACK}, HDN_TRACK and HDN_ITEMCHANGING
709 // interpret TRUE return value as meaning to stop the control
710 // default handling of the message
716 return wxHeaderCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
719 #endif // wxHAS_GENERIC_HEADERCTRL
721 #endif // wxUSE_HEADERCTRL