]> git.saurik.com Git - wxWidgets.git/blob - src/msw/headerctrl.cpp
9390925fec71d49f1f855758a68fd41a97a02dd1
[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 #include "wx/imaglist.h"
32
33 #include "wx/msw/wrapcctl.h"
34 #include "wx/msw/private.h"
35
36 // ============================================================================
37 // wxHeaderCtrl implementation
38 // ============================================================================
39
40 // ----------------------------------------------------------------------------
41 // wxHeaderCtrl construction/destruction
42 // ----------------------------------------------------------------------------
43
44 void wxHeaderCtrl::Init()
45 {
46 m_imageList = NULL;
47 }
48
49 bool wxHeaderCtrl::Create(wxWindow *parent,
50 wxWindowID id,
51 const wxPoint& pos,
52 const wxSize& size,
53 long style,
54 const wxString& name)
55 {
56 // notice that we don't need InitCommonControlsEx(ICC_LISTVIEW_CLASSES)
57 // here as we already call InitCommonControls() in wxApp initialization
58 // code which covers this
59
60 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
61 return false;
62
63 if ( !MSWCreateControl(WC_HEADER, _T(""), pos, size) )
64 return false;
65
66 return true;
67 }
68
69 WXDWORD wxHeaderCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
70 {
71 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
72
73 if ( style & wxHD_DRAGDROP )
74 msStyle |= HDS_DRAGDROP;
75
76 // the control looks nicer with these styles and there doesn't seem to be
77 // any reason to not use them so we always do (as for HDS_HORZ it is 0
78 // anyhow but include it for clarity)
79 msStyle |= HDS_HORZ | HDS_BUTTONS | HDS_FLAT | HDS_FULLDRAG | HDS_HOTTRACK;
80
81 return msStyle;
82 }
83
84 wxHeaderCtrl::~wxHeaderCtrl()
85 {
86 delete m_imageList;
87 }
88
89 // ----------------------------------------------------------------------------
90 // wxHeaderCtrl geometry calculation
91 // ----------------------------------------------------------------------------
92
93 wxSize wxHeaderCtrl::DoGetBestSize() const
94 {
95 RECT rc = wxGetClientRect(GetHwndOf(GetParent()));
96 WINDOWPOS wpos;
97 HDLAYOUT layout = { &rc, &wpos };
98 if ( !Header_Layout(GetHwnd(), &layout) )
99 {
100 wxLogLastError(_T("Header_Layout"));
101 return wxControl::DoGetBestSize();
102 }
103
104 return wxSize(wpos.cx, wpos.cy);
105 }
106
107 // ----------------------------------------------------------------------------
108 // wxHeaderCtrl columns managements
109 // ----------------------------------------------------------------------------
110
111 unsigned int wxHeaderCtrl::DoGetCount() const
112 {
113 return Header_GetItemCount(GetHwnd());
114 }
115
116 void wxHeaderCtrl::DoInsert(const wxHeaderColumn& col, unsigned int idx)
117 {
118 // copy the HDITEM because we may modify it below
119 HDITEM hdi = col.GetHDI();
120
121 const wxBitmap bmp = col.GetBitmap();
122 if ( bmp.IsOk() )
123 {
124 const int bmpWidth = bmp.GetWidth(),
125 bmpHeight = bmp.GetHeight();
126
127 if ( !m_imageList )
128 {
129 m_imageList = new wxImageList(bmpWidth, bmpHeight);
130 Header_SetImageList(GetHwnd(), GetHimagelistOf(m_imageList));
131 }
132 else // already have an image list
133 {
134 // check that all bitmaps we use have the same size
135 int imageWidth,
136 imageHeight;
137 m_imageList->GetSize(0, imageWidth, imageHeight);
138
139 wxASSERT_MSG( imageWidth == bmpWidth && imageHeight == bmpHeight,
140 "all column bitmaps must have the same size" );
141 }
142
143 m_imageList->Add(bmp);
144 hdi.mask |= HDI_IMAGE;
145 hdi.iImage = m_imageList->GetImageCount() - 1;
146 }
147
148 if ( col.IsHidden() )
149 {
150 hdi.mask |= HDI_WIDTH;
151 hdi.cxy = 0;
152 }
153
154 if ( Header_InsertItem(GetHwnd(), idx, &hdi) == -1 )
155 {
156 wxLogLastError(_T("Header_InsertItem"));
157 }
158 }
159
160 void wxHeaderCtrl::DoDelete(unsigned int idx)
161 {
162 if ( !Header_DeleteItem(GetHwnd(), idx) )
163 {
164 wxLogLastError(_T("Header_DeleteItem"));
165 }
166 }
167
168 // ----------------------------------------------------------------------------
169 // wxHeaderCtrl columns attributes
170 // ----------------------------------------------------------------------------
171
172 void wxHeaderCtrl::DoShowColumn(unsigned int idx, bool show)
173 {
174 wxHDITEM hdi;
175 hdi.mask = HDI_WIDTH;
176
177 if ( !Header_GetItem(GetHwnd(), idx, &hdi) )
178 {
179 wxLogLastError(_T("Header_GetItem(HDI_WIDTH)"));
180 return;
181 }
182
183 if ( show )
184 hdi.cxy = 80; // FIXME: we don't have the column width here any more
185 else
186 hdi.cxy = 0;
187
188 if ( !Header_SetItem(GetHwnd(), idx, &hdi) )
189 {
190 wxLogLastError(_T("Header_SetItem(HDI_WIDTH)"));
191 return;
192 }
193 }
194
195 void wxHeaderCtrl::DoShowSortIndicator(unsigned int idx, int sortOrder)
196 {
197 wxHDITEM hdi;
198 hdi.mask = HDI_FORMAT;
199
200 if ( !Header_GetItem(GetHwnd(), idx, &hdi) )
201 {
202 wxLogLastError(_T("Header_GetItem(HDI_FORMAT)"));
203 return;
204 }
205
206 if ( sortOrder == -1 )
207 hdi.fmt &= ~(HDF_SORTDOWN | HDF_SORTUP);
208 else
209 hdi.fmt |= sortOrder ? HDF_SORTUP : HDF_SORTDOWN;
210
211 if ( !Header_SetItem(GetHwnd(), idx, &hdi) )
212 {
213 wxLogLastError(_T("Header_SetItem(HDI_FORMAT)"));
214 }
215 }
216