]> git.saurik.com Git - wxWidgets.git/blob - src/msw/headerctrl.cpp
84736a1e1f90d78f19b211590d1986cd36f3739f
[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 DoSetOrInsertItem(Insert, n);
155 }
156 }
157
158 void wxHeaderCtrl::DoUpdate(unsigned int idx)
159 {
160 DoSetOrInsertItem(Set, idx);
161 }
162
163 void wxHeaderCtrl::DoSetOrInsertItem(Operation oper, unsigned int idx)
164 {
165 const wxHeaderColumnBase& col = GetColumn(idx);
166
167 wxHDITEM hdi;
168
169 // notice that we need to store the string we use the pointer to until we
170 // pass it to the control
171 hdi.mask |= HDI_TEXT;
172 wxWxCharBuffer buf = col.GetTitle().wx_str();
173 hdi.pszText = buf.data();
174 hdi.cchTextMax = wxStrlen(buf);
175
176 const wxBitmap bmp = col.GetBitmap();
177 if ( bmp.IsOk() || oper == Set )
178 {
179 hdi.mask |= HDI_IMAGE;
180
181 if ( bmp.IsOk() )
182 {
183 const int bmpWidth = bmp.GetWidth(),
184 bmpHeight = bmp.GetHeight();
185
186 if ( !m_imageList )
187 {
188 m_imageList = new wxImageList(bmpWidth, bmpHeight);
189 Header_SetImageList(GetHwnd(), GetHimagelistOf(m_imageList));
190 }
191 else // already have an image list
192 {
193 // check that all bitmaps we use have the same size
194 int imageWidth,
195 imageHeight;
196 m_imageList->GetSize(0, imageWidth, imageHeight);
197
198 wxASSERT_MSG( imageWidth == bmpWidth && imageHeight == bmpHeight,
199 "all column bitmaps must have the same size" );
200 }
201
202 m_imageList->Add(bmp);
203 hdi.iImage = m_imageList->GetImageCount() - 1;
204 }
205 else // no bitmap but we still need to update the item
206 {
207 hdi.iImage = I_IMAGENONE;
208 }
209 }
210
211 if ( col.GetAlignment() != wxALIGN_NOT || oper == Set )
212 {
213 hdi.mask |= HDI_FORMAT;
214 switch ( col.GetAlignment() )
215 {
216 case wxALIGN_LEFT:
217 hdi.fmt |= HDF_LEFT;
218 break;
219
220 case wxALIGN_CENTER:
221 case wxALIGN_CENTER_HORIZONTAL:
222 hdi.fmt |= HDF_CENTER;
223 break;
224
225 case wxALIGN_RIGHT:
226 hdi.fmt |= HDF_RIGHT;
227 break;
228
229 default:
230 wxFAIL_MSG( "invalid column header alignment" );
231 }
232 }
233
234 if ( col.IsSortKey() )
235 {
236 hdi.mask |= HDI_FORMAT;
237 hdi.fmt |= col.IsSortOrderAscending() ? HDF_SORTUP : HDF_SORTDOWN;
238 }
239
240 if ( col.GetWidth() != wxCOL_WIDTH_DEFAULT || col.IsHidden() )
241 {
242 hdi.mask |= HDI_WIDTH;
243 hdi.cxy = col.IsHidden() ? 0 : col.GetWidth();
244 }
245
246 const LRESULT rc = ::SendMessage(GetHwnd(),
247 oper == Set ? HDM_SETITEM : HDM_INSERTITEM,
248 idx,
249 (LPARAM)&hdi);
250 if ( oper == Set )
251 {
252 if ( !rc )
253 wxLogLastError(_T("Header_SetItem()"));
254 }
255 else // Insert
256 {
257 if ( rc == -1 )
258 wxLogLastError(_T("Header_InsertItem()"));
259 }
260 }
261
262 // ----------------------------------------------------------------------------
263 // wxHeaderCtrl events
264 // ----------------------------------------------------------------------------
265
266 wxEventType wxHeaderCtrl::GetClickEventType(bool dblclk, int button)
267 {
268 wxEventType evtType;
269 switch ( button )
270 {
271 case 0:
272 evtType = dblclk ? wxEVT_COMMAND_HEADER_DCLICK
273 : wxEVT_COMMAND_HEADER_CLICK;
274 break;
275
276 case 1:
277 evtType = dblclk ? wxEVT_COMMAND_HEADER_RIGHT_DCLICK
278 : wxEVT_COMMAND_HEADER_RIGHT_CLICK;
279 break;
280
281 case 2:
282 evtType = dblclk ? wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
283 : wxEVT_COMMAND_HEADER_MIDDLE_CLICK;
284 break;
285
286 default:
287 wxFAIL_MSG( wxS("unexpected event type") );
288 evtType = wxEVT_NULL;
289 }
290
291 return evtType;
292 }
293
294 bool wxHeaderCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
295 {
296 NMHEADER * const nmhdr = (NMHEADER *)lParam;
297
298 wxEventType evtType = wxEVT_NULL;
299 int idx = nmhdr->iItem;
300 int width = 0;
301 bool cancelled = false;
302 const UINT code = nmhdr->hdr.code;
303 switch ( code )
304 {
305 // click events
306 // ------------
307
308 case HDN_ITEMCLICK:
309 case HDN_ITEMDBLCLICK:
310 evtType = GetClickEventType(code == HDN_ITEMDBLCLICK, nmhdr->iButton);
311 break;
312
313 // although we should get the notifications about the right clicks
314 // via HDN_ITEM[DBL]CLICK too according to MSDN this simply doesn't
315 // happen in practice on any Windows system up to 2003
316 case NM_RCLICK:
317 case NM_RDBLCLK:
318 {
319 POINT pt;
320 idx = wxMSWGetColumnClicked(&nmhdr->hdr, &pt);
321 if ( idx != wxNOT_FOUND )
322 evtType = GetClickEventType(code == NM_RDBLCLK, 1);
323 //else: ignore clicks outside any column
324 }
325 break;
326
327 case HDN_DIVIDERDBLCLICK:
328 evtType = wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK;
329 break;
330
331
332 // column resizing events
333 // ----------------------
334
335 // see comments in wxListCtrl::MSWOnNotify() for why we catch both
336 // ASCII and Unicode versions of this message
337 case HDN_BEGINTRACKA:
338 case HDN_BEGINTRACKW:
339 // non-resizeable columns can't be resized no matter what, don't
340 // even generate any events for them
341 if ( !GetColumn(idx).IsResizeable() )
342 {
343 *result = TRUE;
344
345 return true;
346 }
347
348 evtType = wxEVT_COMMAND_HEADER_BEGIN_RESIZE;
349 // fall through
350
351 case HDN_TRACKA:
352 case HDN_TRACKW:
353 if ( evtType == wxEVT_NULL )
354 evtType = wxEVT_COMMAND_HEADER_RESIZING;
355 // fall through
356
357 case HDN_ENDTRACKA:
358 case HDN_ENDTRACKW:
359 width = nmhdr->pitem->cxy;
360
361 if ( evtType == wxEVT_NULL )
362 {
363 evtType = wxEVT_COMMAND_HEADER_END_RESIZE;
364
365 // don't generate events with invalid width
366 const int minWidth = GetColumn(idx).GetMinWidth();
367 if ( width < minWidth )
368 width = minWidth;
369 }
370 break;
371
372 case HDN_ITEMCHANGING:
373 if ( nmhdr->pitem && (nmhdr->pitem->mask & HDI_WIDTH) )
374 {
375 // prevent the column from being shrunk beneath its min width
376 if ( nmhdr->pitem->cxy < GetColumn(idx).GetMinWidth() )
377 {
378 *result = TRUE;
379
380 return true;
381 }
382 }
383 break;
384
385 case NM_RELEASEDCAPTURE:
386 cancelled = true;
387 break;
388 }
389
390
391 // do generate the corresponding wx event
392 if ( evtType != wxEVT_NULL )
393 {
394 wxHeaderCtrlEvent event(evtType, GetId());
395 event.SetEventObject(this);
396 event.SetColumn(idx);
397 event.SetWidth(width);
398 if ( cancelled )
399 event.SetCancelled();
400
401 if ( GetEventHandler()->ProcessEvent(event) )
402 {
403 if ( !event.IsAllowed() )
404 {
405 // all of HDN_BEGIN{DRAG,TRACK}, HDN_TRACK and HDN_ITEMCHANGING
406 // interpret TRUE return value as meaning to stop the control
407 // default handling of the message
408 *result = TRUE;
409 }
410
411 return true;
412 }
413 }
414
415 return wxHeaderCtrlBase::MSWOnNotify(idCtrl, lParam, result);
416 }
417
418 #endif // wxHAS_GENERIC_HEADERCTRL