1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/headerctrlg.cpp
3 // Purpose: generic wxHeaderCtrl implementation
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
29 #include "wx/headerctrl.h"
31 #ifdef wxHAS_GENERIC_HEADERCTRL
33 #include "wx/dcbuffer.h"
34 #include "wx/renderer.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
43 const unsigned NO_SORT
= (unsigned)-1;
45 const unsigned COL_NONE
= (unsigned)-1;
47 } // anonymous namespace
49 // ============================================================================
50 // wxHeaderCtrl implementation
51 // ============================================================================
53 // ----------------------------------------------------------------------------
54 // wxHeaderCtrl creation
55 // ----------------------------------------------------------------------------
57 void wxHeaderCtrl
::Init()
63 bool wxHeaderCtrl
::Create(wxWindow
*parent
,
70 if ( !wxHeaderCtrlBase
::Create(parent
, id
, pos
, size
,
71 style
, wxDefaultValidator
, name
) )
74 // tell the system to not paint the background at all to avoid flicker as
75 // we paint the entire window area in our OnPaint()
76 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
81 wxHeaderCtrl
::~wxHeaderCtrl()
85 // ----------------------------------------------------------------------------
86 // wxHeaderCtrl columns manipulation
87 // ----------------------------------------------------------------------------
89 unsigned int wxHeaderCtrl
::DoGetCount() const
94 // ----------------------------------------------------------------------------
95 // wxHeaderCtrl scrolling
96 // ----------------------------------------------------------------------------
98 void wxHeaderCtrl
::DoScrollHorz(int dx
)
100 m_scrollOffset
+= dx
;
102 // don't call our own version which calls this function!
103 wxControl
::ScrollWindow(dx
, 0);
106 // ----------------------------------------------------------------------------
107 // wxHeaderCtrl geometry
108 // ----------------------------------------------------------------------------
110 wxSize wxHeaderCtrl
::DoGetBestSize() const
112 // the vertical size is rather arbitrary but it looks better if we leave
113 // some space around the text
114 return wxSize(GetColStart(GetColumnCount()), (7*GetCharHeight())/4);
117 int wxHeaderCtrl
::GetColStart(unsigned int idx
) const
120 for ( unsigned n
= 0; n
< idx
; n
++ )
122 const wxHeaderColumn
& col
= m_cols
[n
];
124 pos
+= col
.GetWidth();
130 // ----------------------------------------------------------------------------
131 // wxHeaderCtrl repainting
132 // ----------------------------------------------------------------------------
134 void wxHeaderCtrl
::RefreshCol(unsigned int idx
)
136 wxRect rect
= GetClientRect();
137 rect
.x
+= GetColStart(idx
);
138 rect
.width
= m_cols
[idx
].GetWidth();
143 void wxHeaderCtrl
::RefreshColsAfter(unsigned int idx
)
145 wxRect rect
= GetClientRect();
146 const int ofs
= GetColStart(idx
);
153 // ----------------------------------------------------------------------------
154 // wxHeaderCtrl event handlers
155 // ----------------------------------------------------------------------------
157 BEGIN_EVENT_TABLE(wxHeaderCtrl
, wxControl
)
158 EVT_PAINT(wxHeaderCtrl
::OnPaint
)
160 EVT_MOUSE_EVENTS(wxHeaderCtrl
::OnMouse
)
163 void wxHeaderCtrl
::OnPaint(wxPaintEvent
& WXUNUSED(event
))
166 GetClientSize(&w
, &h
);
168 wxAutoBufferedPaintDC
dc(this);
170 dc
.SetBackground(GetBackgroundColour());
173 // account for the horizontal scrollbar offset in the parent window
174 dc
.SetDeviceOrigin(m_scrollOffset
, 0);
176 const unsigned int count
= m_cols
.size();
178 for ( unsigned int i
= 0; i
< count
; i
++ )
180 const wxHeaderColumn
& col
= m_cols
[i
];
181 if ( col
.IsHidden() )
184 const int colWidth
= col
.GetWidth();
186 wxHeaderSortIconType sortArrow
;
187 switch ( m_sortOrders
[i
] )
190 wxFAIL_MSG( "wrong sort order value" );
194 sortArrow
= wxHDR_SORT_ICON_NONE
;
198 sortArrow
= wxHDR_SORT_ICON_DOWN
;
202 sortArrow
= wxHDR_SORT_ICON_UP
;
210 state
= wxCONTROL_CURRENT
;
214 state
= wxCONTROL_DISABLED
;
217 wxHeaderButtonParams params
;
218 params
.m_labelText
= col
.GetTitle();
219 params
.m_labelBitmap
= col
.GetBitmap();
220 params
.m_labelAlignment
= col
.GetAlignment();
222 wxRendererNative
::Get().DrawHeaderButton
226 wxRect(xpos
, 0, colWidth
, h
),
236 void wxHeaderCtrl
::OnMouse(wxMouseEvent
& event
)
241 #endif // wxHAS_GENERIC_HEADERCTRL