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"
30 #include "wx/headerctrl.h"
32 #ifndef wxHAS_GENERIC_HEADERCTRL
34 #include "wx/imaglist.h"
36 #include "wx/msw/wrapcctl.h"
37 #include "wx/msw/private.h"
39 // from src/msw/listctrl.cpp
40 extern int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
42 // ============================================================================
43 // wxHeaderCtrl implementation
44 // ============================================================================
46 // ----------------------------------------------------------------------------
47 // wxHeaderCtrl construction/destruction
48 // ----------------------------------------------------------------------------
50 void wxHeaderCtrl::Init()
55 bool wxHeaderCtrl::Create(wxWindow
*parent
,
62 // notice that we don't need InitCommonControlsEx(ICC_LISTVIEW_CLASSES)
63 // here as we already call InitCommonControls() in wxApp initialization
64 // code which covers this
66 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
69 if ( !MSWCreateControl(WC_HEADER
, _T(""), pos
, size
) )
75 WXDWORD
wxHeaderCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
77 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
79 if ( style
& wxHD_DRAGDROP
)
80 msStyle
|= HDS_DRAGDROP
;
82 // the control looks nicer with these styles and there doesn't seem to be
83 // any reason to not use them so we always do (as for HDS_HORZ it is 0
84 // anyhow but include it for clarity)
85 msStyle
|= HDS_HORZ
| HDS_BUTTONS
| HDS_FLAT
| HDS_FULLDRAG
| HDS_HOTTRACK
;
90 wxHeaderCtrl::~wxHeaderCtrl()
95 // ----------------------------------------------------------------------------
96 // wxHeaderCtrl scrolling
97 // ----------------------------------------------------------------------------
99 void wxHeaderCtrl::DoScrollHorz(int dx
)
101 // as the native control doesn't support offsetting its contents, we use a
102 // hack here to make it appear correctly when the parent is scrolled:
103 // instead of scrolling or repainting we simply move the control window
104 // itself: to be precise, offset it by the scroll increment to the left and
105 // increment its width to still extend to the right boundary to compensate
106 // for it (notice that dx is negative when scrolling to the right)
107 SetSize(GetPosition().x
+ dx
, -1, GetSize().x
- dx
, -1, wxSIZE_USE_EXISTING
);
110 // ----------------------------------------------------------------------------
111 // wxHeaderCtrl geometry calculation
112 // ----------------------------------------------------------------------------
114 wxSize
wxHeaderCtrl::DoGetBestSize() const
116 RECT rc
= wxGetClientRect(GetHwndOf(GetParent()));
118 HDLAYOUT layout
= { &rc
, &wpos
};
119 if ( !Header_Layout(GetHwnd(), &layout
) )
121 wxLogLastError(_T("Header_Layout"));
122 return wxControl::DoGetBestSize();
125 return wxSize(wpos
.cx
, wpos
.cy
);
128 // ----------------------------------------------------------------------------
129 // wxHeaderCtrl columns managements
130 // ----------------------------------------------------------------------------
132 unsigned int wxHeaderCtrl::DoGetCount() const
134 return Header_GetItemCount(GetHwnd());
137 void wxHeaderCtrl::DoSetCount(unsigned int count
)
141 // first delete all old columns
142 const unsigned countOld
= DoGetCount();
143 for ( n
= 0; n
< countOld
; n
++ )
145 if ( !Header_DeleteItem(GetHwnd(), 0) )
147 wxLogLastError(_T("Header_DeleteItem"));
151 // and add the new ones
152 for ( n
= 0; n
< count
; n
++ )
158 void wxHeaderCtrl::DoUpdate(unsigned int idx
)
160 // the native control does provide Header_SetItem() but it's inconvenient
161 // to use it because it sends HDN_ITEMCHANGING messages and we'd have to
162 // arrange not to block setting the width from there and the logic would be
163 // more complicated as we'd have to reset the old values as well as setting
164 // the new ones -- so instead just recreate the column
165 Header_DeleteItem(GetHwnd(), idx
);
169 void wxHeaderCtrl::DoInsertItem(unsigned int idx
)
171 const wxHeaderColumnBase
& col
= GetColumn(idx
);
175 // notice that we need to store the string we use the pointer to until we
176 // pass it to the control
177 hdi
.mask
|= HDI_TEXT
;
178 wxWxCharBuffer buf
= col
.GetTitle().wx_str();
179 hdi
.pszText
= buf
.data();
180 hdi
.cchTextMax
= wxStrlen(buf
);
182 const wxBitmap bmp
= col
.GetBitmap();
185 hdi
.mask
|= HDI_IMAGE
;
189 const int bmpWidth
= bmp
.GetWidth(),
190 bmpHeight
= bmp
.GetHeight();
194 m_imageList
= new wxImageList(bmpWidth
, bmpHeight
);
195 Header_SetImageList(GetHwnd(), GetHimagelistOf(m_imageList
));
197 else // already have an image list
199 // check that all bitmaps we use have the same size
202 m_imageList
->GetSize(0, imageWidth
, imageHeight
);
204 wxASSERT_MSG( imageWidth
== bmpWidth
&& imageHeight
== bmpHeight
,
205 "all column bitmaps must have the same size" );
208 m_imageList
->Add(bmp
);
209 hdi
.iImage
= m_imageList
->GetImageCount() - 1;
211 else // no bitmap but we still need to update the item
213 hdi
.iImage
= I_IMAGENONE
;
217 if ( col
.GetAlignment() != wxALIGN_NOT
)
219 hdi
.mask
|= HDI_FORMAT
;
220 switch ( col
.GetAlignment() )
227 case wxALIGN_CENTER_HORIZONTAL
:
228 hdi
.fmt
|= HDF_CENTER
;
232 hdi
.fmt
|= HDF_RIGHT
;
236 wxFAIL_MSG( "invalid column header alignment" );
240 if ( col
.IsSortKey() )
242 hdi
.mask
|= HDI_FORMAT
;
243 hdi
.fmt
|= col
.IsSortOrderAscending() ? HDF_SORTUP
: HDF_SORTDOWN
;
246 if ( col
.GetWidth() != wxCOL_WIDTH_DEFAULT
|| col
.IsHidden() )
248 hdi
.mask
|= HDI_WIDTH
;
249 hdi
.cxy
= col
.IsHidden() ? 0 : col
.GetWidth();
252 if ( ::SendMessage(GetHwnd(), HDM_INSERTITEM
, idx
, (LPARAM
)&hdi
) == -1 )
254 wxLogLastError(_T("Header_InsertItem()"));
258 // ----------------------------------------------------------------------------
259 // wxHeaderCtrl events
260 // ----------------------------------------------------------------------------
262 wxEventType
wxHeaderCtrl::GetClickEventType(bool dblclk
, int button
)
268 evtType
= dblclk
? wxEVT_COMMAND_HEADER_DCLICK
269 : wxEVT_COMMAND_HEADER_CLICK
;
273 evtType
= dblclk
? wxEVT_COMMAND_HEADER_RIGHT_DCLICK
274 : wxEVT_COMMAND_HEADER_RIGHT_CLICK
;
278 evtType
= dblclk
? wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
279 : wxEVT_COMMAND_HEADER_MIDDLE_CLICK
;
283 wxFAIL_MSG( wxS("unexpected event type") );
284 evtType
= wxEVT_NULL
;
290 bool wxHeaderCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
292 NMHEADER
* const nmhdr
= (NMHEADER
*)lParam
;
294 wxEventType evtType
= wxEVT_NULL
;
295 int idx
= nmhdr
->iItem
;
297 bool cancelled
= false;
298 const UINT code
= nmhdr
->hdr
.code
;
305 case HDN_ITEMDBLCLICK
:
306 evtType
= GetClickEventType(code
== HDN_ITEMDBLCLICK
, nmhdr
->iButton
);
309 // although we should get the notifications about the right clicks
310 // via HDN_ITEM[DBL]CLICK too according to MSDN this simply doesn't
311 // happen in practice on any Windows system up to 2003
316 idx
= wxMSWGetColumnClicked(&nmhdr
->hdr
, &pt
);
317 if ( idx
!= wxNOT_FOUND
)
318 evtType
= GetClickEventType(code
== NM_RDBLCLK
, 1);
319 //else: ignore clicks outside any column
323 case HDN_DIVIDERDBLCLICK
:
324 evtType
= wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
;
328 // column resizing events
329 // ----------------------
331 // see comments in wxListCtrl::MSWOnNotify() for why we catch both
332 // ASCII and Unicode versions of this message
333 case HDN_BEGINTRACKA
:
334 case HDN_BEGINTRACKW
:
335 // non-resizeable columns can't be resized no matter what, don't
336 // even generate any events for them
337 if ( !GetColumn(idx
).IsResizeable() )
344 evtType
= wxEVT_COMMAND_HEADER_BEGIN_RESIZE
;
349 if ( evtType
== wxEVT_NULL
)
350 evtType
= wxEVT_COMMAND_HEADER_RESIZING
;
355 width
= nmhdr
->pitem
->cxy
;
357 if ( evtType
== wxEVT_NULL
)
359 evtType
= wxEVT_COMMAND_HEADER_END_RESIZE
;
361 // don't generate events with invalid width
362 const int minWidth
= GetColumn(idx
).GetMinWidth();
363 if ( width
< minWidth
)
368 case HDN_ITEMCHANGING
:
369 if ( nmhdr
->pitem
&& (nmhdr
->pitem
->mask
& HDI_WIDTH
) )
371 // prevent the column from being shrunk beneath its min width
372 if ( nmhdr
->pitem
->cxy
< GetColumn(idx
).GetMinWidth() )
381 case NM_RELEASEDCAPTURE
:
387 // do generate the corresponding wx event
388 if ( evtType
!= wxEVT_NULL
)
390 wxHeaderCtrlEvent
event(evtType
, GetId());
391 event
.SetEventObject(this);
392 event
.SetColumn(idx
);
393 event
.SetWidth(width
);
395 event
.SetCancelled();
397 if ( GetEventHandler()->ProcessEvent(event
) )
399 if ( !event
.IsAllowed() )
401 // all of HDN_BEGIN{DRAG,TRACK}, HDN_TRACK and HDN_ITEMCHANGING
402 // interpret TRUE return value as meaning to stop the control
403 // default handling of the message
411 return wxHeaderCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
414 #endif // wxHAS_GENERIC_HEADERCTRL