]> git.saurik.com Git - wxWidgets.git/blame - include/wx/headerctrl.h
don't access inexistent column in wxDataViewTreeCtrl::OnSize() (this bug also probabl...
[wxWidgets.git] / include / wx / headerctrl.h
CommitLineData
56873923
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: wx/headerctrl.h
3// Purpose: wxHeaderCtrlBase class: interface of wxHeaderCtrl
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#ifndef _WX_HEADERCTRL_H_
12#define _WX_HEADERCTRL_H_
13
14#include "wx/control.h"
15
e2bfe673
VZ
16#include "wx/vector.h"
17
56873923
VZ
18#include "wx/headercol.h"
19
20// notice that the classes in this header are defined in the core library even
21// although currently they're only used by wxGrid which is in wxAdv because we
22// plan to use it in wxListCtrl which is in core too in the future
3bfaa5a7 23class WXDLLIMPEXP_FWD_CORE wxHeaderCtrlEvent;
56873923
VZ
24
25// ----------------------------------------------------------------------------
26// constants
27// ----------------------------------------------------------------------------
28
29enum
30{
31 // allow column drag and drop
32 wxHD_DRAGDROP = 0x0001,
33
34 // style used by default when creating the control
35 wxHD_DEFAULT_STYLE = wxHD_DRAGDROP
36};
37
38extern WXDLLIMPEXP_DATA_CORE(const char) wxHeaderCtrlNameStr[];
39
56873923
VZ
40// ----------------------------------------------------------------------------
41// wxHeaderCtrlBase defines the interface of a header control
42// ----------------------------------------------------------------------------
43
44class WXDLLIMPEXP_CORE wxHeaderCtrlBase : public wxControl
45{
46public:
47 /*
48 Derived classes must provide default ctor as well as a ctor and
49 Create() function with the following signatures:
50
51 wxHeaderCtrl(wxWindow *parent,
52 wxWindowID winid = wxID_ANY,
53 const wxPoint& pos = wxDefaultPosition,
54 const wxSize& size = wxDefaultSize,
e2bfe673 55 long style = wxHD_DEFAULT_STYLE,
56873923
VZ
56 const wxString& name = wxHeaderCtrlNameStr);
57
58 bool Create(wxWindow *parent,
59 wxWindowID winid = wxID_ANY,
60 const wxPoint& pos = wxDefaultPosition,
61 const wxSize& size = wxDefaultSize,
e2bfe673 62 long style = wxHD_DEFAULT_STYLE,
56873923
VZ
63 const wxString& name = wxHeaderCtrlNameStr);
64 */
65
e2bfe673
VZ
66 // column-related methods
67 // ----------------------
68
69 // set the number of columns in the control
70 //
71 // this also calls UpdateColumn() for all columns
72 void SetColumnCount(unsigned int count) { DoSetCount(count); }
56873923 73
e2bfe673 74 // return the number of columns in the control as set by SetColumnCount()
56873923
VZ
75 unsigned int GetColumnCount() const { return DoGetCount(); }
76
77 // return whether the control has any columns
e2bfe673
VZ
78 bool IsEmpty() const { return DoGetCount() == 0; }
79
80 // update the column with the given index
81 void UpdateColumn(unsigned int idx)
82 {
83 wxCHECK_RET( idx < GetColumnCount(), "invalid column index" );
84
85 DoUpdate(idx);
86 }
87
88
89 // implementation only from now on
90 // -------------------------------
91
92 // the user doesn't need to TAB to this control
93 virtual bool AcceptsFocusFromKeyboard() const { return false; }
94
95 // this method is only overridden in order to synchronize the control with
96 // the main window when it is scrolled, the derived class must implement
97 // DoScrollHorz()
98 virtual void ScrollWindow(int dx, int dy, const wxRect *rect = NULL);
99
100protected:
101 // this method must be implemented by the derived classes to return the
102 // information for the given column
103 virtual wxHeaderColumnBase& GetColumn(unsigned int idx) = 0;
104
3bfaa5a7
VZ
105 // this method is called from the default EVT_HEADER_SEPARATOR_DCLICK
106 // handler to update the fitting column width of the given column, it
107 // should return true if the width was really updated
108 virtual bool UpdateColumnWidthToFit(unsigned int WXUNUSED(idx),
109 int WXUNUSED(widthTitle))
110 {
111 return false;
112 }
113
e2bfe673
VZ
114private:
115 // methods implementing our public API and defined in platform-specific
116 // implementations
117 virtual void DoSetCount(unsigned int count) = 0;
118 virtual unsigned int DoGetCount() const = 0;
119 virtual void DoUpdate(unsigned int idx) = 0;
120
121 virtual void DoScrollHorz(int dx) = 0;
122
123 // this window doesn't look nice with the border so don't use it by default
124 virtual wxBorder GetDefaultBorder() const { return wxBORDER_NONE; }
3bfaa5a7
VZ
125
126 // event handlers
127 void OnSeparatorDClick(wxHeaderCtrlEvent& event);
128
129 DECLARE_EVENT_TABLE()
e2bfe673
VZ
130};
131
132// ----------------------------------------------------------------------------
133// wxHeaderCtrl: port-specific header control implementation, notice that this
134// is still an ABC which is meant to be used as part of another
135// control, see wxHeaderCtrlSimple for a standalone version
136// ----------------------------------------------------------------------------
137
0c02fc77 138#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
e2bfe673
VZ
139 #include "wx/msw/headerctrl.h"
140#else
141 #define wxHAS_GENERIC_HEADERCTRL
142 #include "wx/generic/headerctrlg.h"
143#endif // platform
144
145// ----------------------------------------------------------------------------
146// wxHeaderCtrlSimple: concrete header control which can be used standalone
147// ----------------------------------------------------------------------------
148
149class WXDLLIMPEXP_CORE wxHeaderCtrlSimple : public wxHeaderCtrl
150{
151public:
152 // control creation
153 // ----------------
154
155 wxHeaderCtrlSimple() { Init(); }
156 wxHeaderCtrlSimple(wxWindow *parent,
157 wxWindowID winid = wxID_ANY,
158 const wxPoint& pos = wxDefaultPosition,
159 const wxSize& size = wxDefaultSize,
160 long style = wxHD_DEFAULT_STYLE,
161 const wxString& name = wxHeaderCtrlNameStr)
162 {
163 Init();
164
165 Create(parent, winid, pos, size, style, name);
166 }
167
168 // managing the columns
169 // --------------------
56873923
VZ
170
171 // insert the column at the given position, using GetColumnCount() as
172 // position appends it at the end
e2bfe673 173 void InsertColumn(const wxHeaderColumnSimple& col, unsigned int idx)
56873923
VZ
174 {
175 wxCHECK_RET( idx <= GetColumnCount(), "invalid column index" );
176
177 DoInsert(col, idx);
178 }
179
180 // append the column to the end of the control
e2bfe673 181 void AppendColumn(const wxHeaderColumnSimple& col)
56873923
VZ
182 {
183 DoInsert(col, GetColumnCount());
184 }
185
186 // delete the column at the given index
187 void DeleteColumn(unsigned int idx)
188 {
189 wxCHECK_RET( idx < GetColumnCount(), "invalid column index" );
190
191 DoDelete(idx);
192 }
193
194 // delete all the existing columns
195 void DeleteAllColumns();
196
197
198 // modifying columns
199 // -----------------
200
a0009205
VZ
201 // show or hide the column, notice that even when a column is hidden we
202 // still account for it when using indices
203 void ShowColumn(unsigned int idx, bool show = true)
204 {
205 wxCHECK_RET( idx < GetColumnCount(), "invalid column index" );
206
207 DoShowColumn(idx, show);
208 }
209
210 void HideColumn(unsigned int idx)
211 {
212 ShowColumn(idx, false);
213 }
214
e2bfe673
VZ
215 // indicate that the column is used for sorting
216 void ShowSortIndicator(unsigned int idx, bool ascending = true)
56873923 217 {
a0009205
VZ
218 wxCHECK_RET( idx < GetColumnCount(), "invalid column index" );
219
e2bfe673 220 DoShowSortIndicator(idx, ascending);
56873923
VZ
221 }
222
e2bfe673
VZ
223 // remove the sort indicator completely
224 void RemoveSortIndicator();
56873923 225
e2bfe673 226protected:
e5a16353 227 // implement/override base class methods
e2bfe673 228 virtual wxHeaderColumnBase& GetColumn(unsigned int idx);
e5a16353
VZ
229 virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle);
230
231 // and define another one to be overridden in the derived classes: it
232 // should return the best width for the given column contents or -1 if not
233 // implemented, we use it to implement UpdateColumnWidthToFit()
234 virtual int GetBestFittingWidth(unsigned int WXUNUSED(idx)) const
235 {
236 return -1;
237 }
56873923 238
e2bfe673
VZ
239private:
240 // functions implementing our public API
241 void DoInsert(const wxHeaderColumnSimple& col, unsigned int idx);
242 void DoDelete(unsigned int idx);
243 void DoShowColumn(unsigned int idx, bool show);
244 void DoShowSortIndicator(unsigned int idx, bool ascending);
56873923 245
e2bfe673
VZ
246 // common part of all ctors
247 void Init();
56873923 248
e2bfe673
VZ
249 // bring the column count in sync with the number of columns we store
250 void UpdateColumnCount() { SetColumnCount(m_cols.size()); }
d8fc3398 251
a3e0efb6 252
e2bfe673
VZ
253 // all our current columns
254 typedef wxVector<wxHeaderColumnSimple> Columns;
255 Columns m_cols;
56873923 256
e2bfe673
VZ
257 // the column currently used for sorting or -1 if none
258 unsigned int m_sortKey;
259
260
261 DECLARE_NO_COPY_CLASS(wxHeaderCtrlSimple)
262};
56873923 263
fa3d4aaf
VZ
264// ----------------------------------------------------------------------------
265// wxHeaderCtrl events
266// ----------------------------------------------------------------------------
267
268class WXDLLIMPEXP_CORE wxHeaderCtrlEvent : public wxNotifyEvent
269{
270public:
271 wxHeaderCtrlEvent(wxEventType commandType = wxEVT_NULL, int winid = 0)
272 : wxNotifyEvent(commandType, winid)
273 {
274 }
275
276 wxHeaderCtrlEvent(const wxHeaderCtrlEvent& event)
277 : wxNotifyEvent(event),
278 m_col(event.m_col)
279 {
280 }
281
282 int GetColumn() const { return m_col; }
283 void SetColumn(int col) { m_col = col; }
284
285 virtual wxEvent *Clone() const { return new wxHeaderCtrlEvent(*this); }
286
287protected:
288 // the column affected by the event
289 int m_col;
290
291private:
292 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxHeaderCtrlEvent)
293};
294
295
056d5a89
VZ
296extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_CLICK;
297extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_RIGHT_CLICK;
298extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_CLICK;
fa3d4aaf 299
056d5a89
VZ
300extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_DCLICK;
301extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_RIGHT_DCLICK;
302extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_DCLICK;
fa3d4aaf 303
3bfaa5a7
VZ
304extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK;
305
fa3d4aaf
VZ
306typedef void (wxEvtHandler::*wxHeaderCtrlEventFunction)(wxHeaderCtrlEvent&);
307
308#define wxHeaderCtrlEventHandler(func) \
309 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent( \
310 wxHeaderCtrlEventFunction, &func)
311
312#define wx__DECLARE_HEADER_EVT(evt, id, fn) \
313 wx__DECLARE_EVT1(wxEVT_COMMAND_HEADER_ ## evt, id, wxHeaderCtrlEventHandler(fn))
314
315#define EVT_HEADER_CLICK(id, fn) wx__DECLARE_HEADER_EVT(CLICK, id, fn)
316#define EVT_HEADER_RIGHT_CLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_CLICK, id, fn)
317#define EVT_HEADER_MIDDLE_CLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_CLICK, id, fn)
318
319#define EVT_HEADER_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(DCLICK, id, fn)
320#define EVT_HEADER_RIGHT_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_DCLICK, id, fn)
321#define EVT_HEADER_MIDDLE_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_DCLICK, id, fn)
322
3bfaa5a7
VZ
323#define EVT_HEADER_SEPARATOR_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(SEPARATOR_DCLICK, id, fn)
324
56873923 325#endif // _WX_HEADERCTRL_H_