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