]> git.saurik.com Git - wxWidgets.git/blame - src/msw/headerctrl.cpp
use a slightly less ugly way to conditionally suppress unused parameter warnings
[wxWidgets.git] / src / msw / headerctrl.cpp
CommitLineData
56873923
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/msw/headerctrl.cpp
3// Purpose: implementation of wxHeaderCtrl for wxMSW
4// Author: Vadim Zeitlin
5// Created: 2008-12-01
6// RCS-ID: $Id$
7// Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8// Licence: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// for compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#ifndef WX_PRECOMP
9ef3e400 27 #include "wx/log.h"
56873923
VZ
28#endif // WX_PRECOMP
29
30#include "wx/headerctrl.h"
3bfaa5a7
VZ
31
32#ifndef wxHAS_GENERIC_HEADERCTRL
33
56873923
VZ
34#include "wx/imaglist.h"
35
36#include "wx/msw/wrapcctl.h"
9ef3e400 37#include "wx/msw/private.h"
56873923 38
fa3d4aaf
VZ
39// from src/msw/listctrl.cpp
40extern int WXDLLIMPEXP_CORE wxMSWGetColumnClicked(NMHDR *nmhdr, POINT *ptClick);
41
56873923
VZ
42// ============================================================================
43// wxHeaderCtrl implementation
44// ============================================================================
45
46// ----------------------------------------------------------------------------
47// wxHeaderCtrl construction/destruction
48// ----------------------------------------------------------------------------
49
50void wxHeaderCtrl::Init()
51{
52 m_imageList = NULL;
53}
54
55bool wxHeaderCtrl::Create(wxWindow *parent,
56 wxWindowID id,
57 const wxPoint& pos,
58 const wxSize& size,
59 long style,
60 const wxString& name)
61{
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
65
66 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
67 return false;
68
69 if ( !MSWCreateControl(WC_HEADER, _T(""), pos, size) )
70 return false;
71
72 return true;
73}
74
75WXDWORD wxHeaderCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
76{
77 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
78
79 if ( style & wxHD_DRAGDROP )
80 msStyle |= HDS_DRAGDROP;
81
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;
86
87 return msStyle;
88}
89
90wxHeaderCtrl::~wxHeaderCtrl()
91{
92 delete m_imageList;
93}
94
f24f6579
VZ
95// ----------------------------------------------------------------------------
96// wxHeaderCtrl scrolling
97// ----------------------------------------------------------------------------
98
d8fc3398 99void wxHeaderCtrl::DoScrollHorz(int dx)
f24f6579 100{
d8fc3398
VZ
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)
f24f6579
VZ
107 SetSize(GetPosition().x + dx, -1, GetSize().x - dx, -1, wxSIZE_USE_EXISTING);
108}
109
56873923
VZ
110// ----------------------------------------------------------------------------
111// wxHeaderCtrl geometry calculation
112// ----------------------------------------------------------------------------
113
114wxSize wxHeaderCtrl::DoGetBestSize() const
115{
116 RECT rc = wxGetClientRect(GetHwndOf(GetParent()));
117 WINDOWPOS wpos;
118 HDLAYOUT layout = { &rc, &wpos };
119 if ( !Header_Layout(GetHwnd(), &layout) )
120 {
121 wxLogLastError(_T("Header_Layout"));
122 return wxControl::DoGetBestSize();
123 }
124
125 return wxSize(wpos.cx, wpos.cy);
126}
127
128// ----------------------------------------------------------------------------
129// wxHeaderCtrl columns managements
130// ----------------------------------------------------------------------------
131
132unsigned int wxHeaderCtrl::DoGetCount() const
133{
134 return Header_GetItemCount(GetHwnd());
135}
136
e2bfe673 137void wxHeaderCtrl::DoSetCount(unsigned int count)
56873923 138{
e2bfe673
VZ
139 unsigned n;
140
141 // first delete all old columns
142 const unsigned countOld = DoGetCount();
143 for ( n = 0; n < countOld; n++ )
144 {
145 if ( !Header_DeleteItem(GetHwnd(), 0) )
146 {
147 wxLogLastError(_T("Header_DeleteItem"));
148 }
149 }
150
151 // and add the new ones
152 for ( n = 0; n < count; n++ )
153 {
4de60a27 154 DoInsertItem(n);
e2bfe673
VZ
155 }
156}
157
158void wxHeaderCtrl::DoUpdate(unsigned int idx)
159{
4de60a27
VZ
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);
166 DoInsertItem(idx);
e2bfe673
VZ
167}
168
4de60a27 169void wxHeaderCtrl::DoInsertItem(unsigned int idx)
e2bfe673
VZ
170{
171 const wxHeaderColumnBase& col = GetColumn(idx);
172
173 wxHDITEM hdi;
174
175 // notice that we need to store the string we use the pointer to until we
176 // pass it to the control
89c73d54
VZ
177 hdi.mask |= HDI_TEXT;
178 wxWxCharBuffer buf = col.GetTitle().wx_str();
179 hdi.pszText = buf.data();
180 hdi.cchTextMax = wxStrlen(buf);
56873923
VZ
181
182 const wxBitmap bmp = col.GetBitmap();
4de60a27 183 if ( bmp.IsOk() )
56873923 184 {
89c73d54 185 hdi.mask |= HDI_IMAGE;
56873923 186
89c73d54 187 if ( bmp.IsOk() )
56873923 188 {
89c73d54
VZ
189 const int bmpWidth = bmp.GetWidth(),
190 bmpHeight = bmp.GetHeight();
191
192 if ( !m_imageList )
193 {
194 m_imageList = new wxImageList(bmpWidth, bmpHeight);
195 Header_SetImageList(GetHwnd(), GetHimagelistOf(m_imageList));
196 }
197 else // already have an image list
198 {
199 // check that all bitmaps we use have the same size
200 int imageWidth,
201 imageHeight;
202 m_imageList->GetSize(0, imageWidth, imageHeight);
203
204 wxASSERT_MSG( imageWidth == bmpWidth && imageHeight == bmpHeight,
205 "all column bitmaps must have the same size" );
206 }
207
208 m_imageList->Add(bmp);
209 hdi.iImage = m_imageList->GetImageCount() - 1;
56873923 210 }
89c73d54 211 else // no bitmap but we still need to update the item
56873923 212 {
89c73d54 213 hdi.iImage = I_IMAGENONE;
56873923 214 }
56873923
VZ
215 }
216
4de60a27 217 if ( col.GetAlignment() != wxALIGN_NOT )
a0009205 218 {
e2bfe673
VZ
219 hdi.mask |= HDI_FORMAT;
220 switch ( col.GetAlignment() )
221 {
222 case wxALIGN_LEFT:
223 hdi.fmt |= HDF_LEFT;
224 break;
56873923 225
e2bfe673
VZ
226 case wxALIGN_CENTER:
227 case wxALIGN_CENTER_HORIZONTAL:
228 hdi.fmt |= HDF_CENTER;
229 break;
56873923 230
e2bfe673
VZ
231 case wxALIGN_RIGHT:
232 hdi.fmt |= HDF_RIGHT;
233 break;
56873923 234
e2bfe673
VZ
235 default:
236 wxFAIL_MSG( "invalid column header alignment" );
237 }
238 }
a0009205 239
e2bfe673 240 if ( col.IsSortKey() )
a0009205 241 {
e2bfe673
VZ
242 hdi.mask |= HDI_FORMAT;
243 hdi.fmt |= col.IsSortOrderAscending() ? HDF_SORTUP : HDF_SORTDOWN;
a0009205
VZ
244 }
245
e2bfe673 246 if ( col.GetWidth() != wxCOL_WIDTH_DEFAULT || col.IsHidden() )
a0009205 247 {
e2bfe673
VZ
248 hdi.mask |= HDI_WIDTH;
249 hdi.cxy = col.IsHidden() ? 0 : col.GetWidth();
a0009205 250 }
a0009205 251
4de60a27 252 if ( ::SendMessage(GetHwnd(), HDM_INSERTITEM, idx, (LPARAM)&hdi) == -1 )
56873923 253 {
4de60a27 254 wxLogLastError(_T("Header_InsertItem()"));
56873923
VZ
255 }
256}
257
fa3d4aaf
VZ
258// ----------------------------------------------------------------------------
259// wxHeaderCtrl events
260// ----------------------------------------------------------------------------
261
aef252d9 262wxEventType wxHeaderCtrl::GetClickEventType(bool dblclk, int button)
fa3d4aaf
VZ
263{
264 wxEventType evtType;
265 switch ( button )
266 {
267 case 0:
268 evtType = dblclk ? wxEVT_COMMAND_HEADER_DCLICK
269 : wxEVT_COMMAND_HEADER_CLICK;
270 break;
271
272 case 1:
273 evtType = dblclk ? wxEVT_COMMAND_HEADER_RIGHT_DCLICK
274 : wxEVT_COMMAND_HEADER_RIGHT_CLICK;
275 break;
276
277 case 2:
278 evtType = dblclk ? wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
279 : wxEVT_COMMAND_HEADER_MIDDLE_CLICK;
280 break;
281
282 default:
283 wxFAIL_MSG( wxS("unexpected event type") );
aef252d9 284 evtType = wxEVT_NULL;
fa3d4aaf
VZ
285 }
286
aef252d9 287 return evtType;
fa3d4aaf
VZ
288}
289
290bool wxHeaderCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
291{
292 NMHEADER * const nmhdr = (NMHEADER *)lParam;
293
aef252d9
VZ
294 wxEventType evtType = wxEVT_NULL;
295 int idx = nmhdr->iItem;
296 int width = 0;
a45caa71 297 bool cancelled = false;
0c9c5b43
VZ
298 const UINT code = nmhdr->hdr.code;
299 switch ( code )
fa3d4aaf 300 {
aef252d9
VZ
301 // click events
302 // ------------
303
fa3d4aaf
VZ
304 case HDN_ITEMCLICK:
305 case HDN_ITEMDBLCLICK:
aef252d9 306 evtType = GetClickEventType(code == HDN_ITEMDBLCLICK, nmhdr->iButton);
fa3d4aaf
VZ
307 break;
308
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
312 case NM_RCLICK:
313 case NM_RDBLCLK:
314 {
315 POINT pt;
aef252d9
VZ
316 idx = wxMSWGetColumnClicked(&nmhdr->hdr, &pt);
317 if ( idx != wxNOT_FOUND )
318 evtType = GetClickEventType(code == NM_RDBLCLK, 1);
fa3d4aaf
VZ
319 //else: ignore clicks outside any column
320 }
321 break;
3bfaa5a7
VZ
322
323 case HDN_DIVIDERDBLCLICK:
aef252d9
VZ
324 evtType = wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK;
325 break;
326
327
328 // column resizing events
329 // ----------------------
330
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:
0b2e1483
VZ
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() )
338 {
339 *result = TRUE;
340
341 return true;
342 }
343
396825dc 344 evtType = wxEVT_COMMAND_HEADER_BEGIN_RESIZE;
aef252d9
VZ
345 // fall through
346
347 case HDN_TRACKA:
348 case HDN_TRACKW:
349 if ( evtType == wxEVT_NULL )
396825dc 350 evtType = wxEVT_COMMAND_HEADER_RESIZING;
aef252d9
VZ
351 // fall through
352
353 case HDN_ENDTRACKA:
354 case HDN_ENDTRACKW:
a45caa71
VZ
355 width = nmhdr->pitem->cxy;
356
aef252d9 357 if ( evtType == wxEVT_NULL )
a45caa71 358 {
396825dc 359 evtType = wxEVT_COMMAND_HEADER_END_RESIZE;
aef252d9 360
a45caa71
VZ
361 // don't generate events with invalid width
362 const int minWidth = GetColumn(idx).GetMinWidth();
363 if ( width < minWidth )
364 width = minWidth;
365 }
366 break;
367
368 case HDN_ITEMCHANGING:
369 if ( nmhdr->pitem && (nmhdr->pitem->mask & HDI_WIDTH) )
370 {
371 // prevent the column from being shrunk beneath its min width
372 if ( nmhdr->pitem->cxy < GetColumn(idx).GetMinWidth() )
373 {
374 *result = TRUE;
375
376 return true;
377 }
378 }
379 break;
380
381 case NM_RELEASEDCAPTURE:
382 cancelled = true;
3bfaa5a7 383 break;
fa3d4aaf
VZ
384 }
385
aef252d9
VZ
386
387 // do generate the corresponding wx event
388 if ( evtType != wxEVT_NULL )
389 {
390 wxHeaderCtrlEvent event(evtType, GetId());
391 event.SetEventObject(this);
392 event.SetColumn(idx);
393 event.SetWidth(width);
a45caa71
VZ
394 if ( cancelled )
395 event.SetCancelled();
aef252d9
VZ
396
397 if ( GetEventHandler()->ProcessEvent(event) )
398 {
399 if ( !event.IsAllowed() )
400 {
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
404 *result = TRUE;
405 }
406
407 return true;
408 }
409 }
410
fa3d4aaf
VZ
411 return wxHeaderCtrlBase::MSWOnNotify(idCtrl, lParam, result);
412}
3bfaa5a7
VZ
413
414#endif // wxHAS_GENERIC_HEADERCTRL