]> git.saurik.com Git - wxWidgets.git/blob - src/msw/headerctrl.cpp
use a slightly less ugly way to conditionally suppress unused parameter warnings
[wxWidgets.git] / src / msw / headerctrl.cpp
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
27 #include "wx/log.h"
28 #endif // WX_PRECOMP
29
30 #include "wx/headerctrl.h"
31
32 #ifndef wxHAS_GENERIC_HEADERCTRL
33
34 #include "wx/imaglist.h"
35
36 #include "wx/msw/wrapcctl.h"
37 #include "wx/msw/private.h"
38
39 // from src/msw/listctrl.cpp
40 extern int WXDLLIMPEXP_CORE wxMSWGetColumnClicked(NMHDR *nmhdr, POINT *ptClick);
41
42 // ============================================================================
43 // wxHeaderCtrl implementation
44 // ============================================================================
45
46 // ----------------------------------------------------------------------------
47 // wxHeaderCtrl construction/destruction
48 // ----------------------------------------------------------------------------
49
50 void wxHeaderCtrl::Init()
51 {
52 m_imageList = NULL;
53 }
54
55 bool 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
75 WXDWORD 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
90 wxHeaderCtrl::~wxHeaderCtrl()
91 {
92 delete m_imageList;
93 }
94
95 // ----------------------------------------------------------------------------
96 // wxHeaderCtrl scrolling
97 // ----------------------------------------------------------------------------
98
99 void wxHeaderCtrl::DoScrollHorz(int dx)
100 {
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);
108 }
109
110 // ----------------------------------------------------------------------------
111 // wxHeaderCtrl geometry calculation
112 // ----------------------------------------------------------------------------
113
114 wxSize 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
132 unsigned int wxHeaderCtrl::DoGetCount() const
133 {
134 return Header_GetItemCount(GetHwnd());
135 }
136
137 void wxHeaderCtrl::DoSetCount(unsigned int count)
138 {
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 {
154 DoInsertItem(n);
155 }
156 }
157
158 void wxHeaderCtrl::DoUpdate(unsigned int idx)
159 {
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);
167 }
168
169 void wxHeaderCtrl::DoInsertItem(unsigned int idx)
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
177 hdi.mask |= HDI_TEXT;
178 wxWxCharBuffer buf = col.GetTitle().wx_str();
179 hdi.pszText = buf.data();
180 hdi.cchTextMax = wxStrlen(buf);
181
182 const wxBitmap bmp = col.GetBitmap();
183 if ( bmp.IsOk() )
184 {
185 hdi.mask |= HDI_IMAGE;
186
187 if ( bmp.IsOk() )
188 {
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;
210 }
211 else // no bitmap but we still need to update the item
212 {
213 hdi.iImage = I_IMAGENONE;
214 }
215 }
216
217 if ( col.GetAlignment() != wxALIGN_NOT )
218 {
219 hdi.mask |= HDI_FORMAT;
220 switch ( col.GetAlignment() )
221 {
222 case wxALIGN_LEFT:
223 hdi.fmt |= HDF_LEFT;
224 break;
225
226 case wxALIGN_CENTER:
227 case wxALIGN_CENTER_HORIZONTAL:
228 hdi.fmt |= HDF_CENTER;
229 break;
230
231 case wxALIGN_RIGHT:
232 hdi.fmt |= HDF_RIGHT;
233 break;
234
235 default:
236 wxFAIL_MSG( "invalid column header alignment" );
237 }
238 }
239
240 if ( col.IsSortKey() )
241 {
242 hdi.mask |= HDI_FORMAT;
243 hdi.fmt |= col.IsSortOrderAscending() ? HDF_SORTUP : HDF_SORTDOWN;
244 }
245
246 if ( col.GetWidth() != wxCOL_WIDTH_DEFAULT || col.IsHidden() )
247 {
248 hdi.mask |= HDI_WIDTH;
249 hdi.cxy = col.IsHidden() ? 0 : col.GetWidth();
250 }
251
252 if ( ::SendMessage(GetHwnd(), HDM_INSERTITEM, idx, (LPARAM)&hdi) == -1 )
253 {
254 wxLogLastError(_T("Header_InsertItem()"));
255 }
256 }
257
258 // ----------------------------------------------------------------------------
259 // wxHeaderCtrl events
260 // ----------------------------------------------------------------------------
261
262 wxEventType wxHeaderCtrl::GetClickEventType(bool dblclk, int button)
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") );
284 evtType = wxEVT_NULL;
285 }
286
287 return evtType;
288 }
289
290 bool wxHeaderCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
291 {
292 NMHEADER * const nmhdr = (NMHEADER *)lParam;
293
294 wxEventType evtType = wxEVT_NULL;
295 int idx = nmhdr->iItem;
296 int width = 0;
297 bool cancelled = false;
298 const UINT code = nmhdr->hdr.code;
299 switch ( code )
300 {
301 // click events
302 // ------------
303
304 case HDN_ITEMCLICK:
305 case HDN_ITEMDBLCLICK:
306 evtType = GetClickEventType(code == HDN_ITEMDBLCLICK, nmhdr->iButton);
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;
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
320 }
321 break;
322
323 case HDN_DIVIDERDBLCLICK:
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:
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
344 evtType = wxEVT_COMMAND_HEADER_BEGIN_RESIZE;
345 // fall through
346
347 case HDN_TRACKA:
348 case HDN_TRACKW:
349 if ( evtType == wxEVT_NULL )
350 evtType = wxEVT_COMMAND_HEADER_RESIZING;
351 // fall through
352
353 case HDN_ENDTRACKA:
354 case HDN_ENDTRACKW:
355 width = nmhdr->pitem->cxy;
356
357 if ( evtType == wxEVT_NULL )
358 {
359 evtType = wxEVT_COMMAND_HEADER_END_RESIZE;
360
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;
383 break;
384 }
385
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);
394 if ( cancelled )
395 event.SetCancelled();
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
411 return wxHeaderCtrlBase::MSWOnNotify(idCtrl, lParam, result);
412 }
413
414 #endif // wxHAS_GENERIC_HEADERCTRL