]>
Commit | Line | Data |
---|---|---|
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 | ||
702f5349 | 16 | #include "wx/dynarray.h" |
e2bfe673 VZ |
17 | #include "wx/vector.h" |
18 | ||
56873923 VZ |
19 | #include "wx/headercol.h" |
20 | ||
21 | // notice that the classes in this header are defined in the core library even | |
22 | // although currently they're only used by wxGrid which is in wxAdv because we | |
23 | // plan to use it in wxListCtrl which is in core too in the future | |
3bfaa5a7 | 24 | class WXDLLIMPEXP_FWD_CORE wxHeaderCtrlEvent; |
56873923 VZ |
25 | |
26 | // ---------------------------------------------------------------------------- | |
27 | // constants | |
28 | // ---------------------------------------------------------------------------- | |
29 | ||
30 | enum | |
31 | { | |
32 | // allow column drag and drop | |
613de0e8 VZ |
33 | wxHD_ALLOW_REORDER = 0x0001, |
34 | ||
35 | // allow hiding (and showing back) the columns using the menu shown by | |
36 | // right clicking the header | |
37 | wxHD_ALLOW_HIDE = 0x0002, | |
56873923 VZ |
38 | |
39 | // style used by default when creating the control | |
613de0e8 | 40 | wxHD_DEFAULT_STYLE = wxHD_ALLOW_REORDER |
56873923 VZ |
41 | }; |
42 | ||
43 | extern WXDLLIMPEXP_DATA_CORE(const char) wxHeaderCtrlNameStr[]; | |
44 | ||
56873923 VZ |
45 | // ---------------------------------------------------------------------------- |
46 | // wxHeaderCtrlBase defines the interface of a header control | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
49 | class WXDLLIMPEXP_CORE wxHeaderCtrlBase : public wxControl | |
50 | { | |
51 | public: | |
52 | /* | |
53 | Derived classes must provide default ctor as well as a ctor and | |
54 | Create() function with the following signatures: | |
55 | ||
56 | wxHeaderCtrl(wxWindow *parent, | |
57 | wxWindowID winid = wxID_ANY, | |
58 | const wxPoint& pos = wxDefaultPosition, | |
59 | const wxSize& size = wxDefaultSize, | |
e2bfe673 | 60 | long style = wxHD_DEFAULT_STYLE, |
56873923 VZ |
61 | const wxString& name = wxHeaderCtrlNameStr); |
62 | ||
63 | bool Create(wxWindow *parent, | |
64 | wxWindowID winid = wxID_ANY, | |
65 | const wxPoint& pos = wxDefaultPosition, | |
66 | const wxSize& size = wxDefaultSize, | |
e2bfe673 | 67 | long style = wxHD_DEFAULT_STYLE, |
56873923 VZ |
68 | const wxString& name = wxHeaderCtrlNameStr); |
69 | */ | |
70 | ||
e2bfe673 VZ |
71 | // column-related methods |
72 | // ---------------------- | |
73 | ||
74 | // set the number of columns in the control | |
75 | // | |
76 | // this also calls UpdateColumn() for all columns | |
4635abac | 77 | void SetColumnCount(unsigned int count); |
56873923 | 78 | |
e2bfe673 | 79 | // return the number of columns in the control as set by SetColumnCount() |
56873923 VZ |
80 | unsigned int GetColumnCount() const { return DoGetCount(); } |
81 | ||
82 | // return whether the control has any columns | |
e2bfe673 VZ |
83 | bool IsEmpty() const { return DoGetCount() == 0; } |
84 | ||
85 | // update the column with the given index | |
86 | void UpdateColumn(unsigned int idx) | |
87 | { | |
88 | wxCHECK_RET( idx < GetColumnCount(), "invalid column index" ); | |
89 | ||
90 | DoUpdate(idx); | |
91 | } | |
92 | ||
e8f25dbb VZ |
93 | |
94 | // columns order | |
95 | // ------------- | |
96 | ||
702f5349 VZ |
97 | // set the columns order: the array defines the column index which appears |
98 | // the given position, it must have GetColumnCount() elements and contain | |
99 | // all indices exactly once | |
100 | void SetColumnsOrder(const wxArrayInt& order); | |
101 | wxArrayInt GetColumnsOrder() const; | |
102 | ||
103 | // get the index of the column at the given display position | |
104 | unsigned int GetColumnAt(unsigned int pos) const; | |
105 | ||
106 | // get the position at which this column is currently displayed | |
107 | unsigned int GetColumnPos(unsigned int idx) const; | |
108 | ||
da5a641f VZ |
109 | // reset the columns order to the natural one |
110 | void ResetColumnsOrder(); | |
111 | ||
1bb74626 VZ |
112 | // helper function used by the generic version of this control and also |
113 | // wxGrid: reshuffles the array of column indices indexed by positions | |
114 | // (i.e. using the same convention as for SetColumnsOrder()) so that the | |
115 | // column with the given index is found at the specified position | |
116 | static void MoveColumnInOrderArray(wxArrayInt& order, | |
117 | unsigned int idx, | |
118 | unsigned int pos); | |
119 | ||
e2bfe673 | 120 | |
e8f25dbb VZ |
121 | // UI helpers |
122 | // ---------- | |
123 | ||
124 | // show the popup menu containing all columns with check marks for the ones | |
613de0e8 VZ |
125 | // which are currently shown and return true if something was done using it |
126 | // (in this case UpdateColumnVisibility() will have been called) or false | |
127 | // if the menu was cancelled | |
128 | // | |
129 | // this is called from the default right click handler for the controls | |
130 | // with wxHD_ALLOW_HIDE style | |
131 | bool ShowColumnsMenu(const wxPoint& pt, const wxString& title = wxString()); | |
132 | ||
133 | // show the columns customization dialog and return true if something was | |
134 | // changed using it (in which case UpdateColumnVisibility() and/or | |
135 | // UpdateColumnWidth() will have been called) | |
136 | // | |
137 | // this is called by the control itself from ShowColumnsMenu() (which in | |
138 | // turn is only called by the control if wxHD_ALLOW_HIDE style was | |
139 | // specified) and if the control has wxHD_ALLOW_REORDER style as well | |
140 | bool ShowCustomizeDialog(); | |
e8f25dbb VZ |
141 | |
142 | ||
e2bfe673 VZ |
143 | // implementation only from now on |
144 | // ------------------------------- | |
145 | ||
146 | // the user doesn't need to TAB to this control | |
147 | virtual bool AcceptsFocusFromKeyboard() const { return false; } | |
148 | ||
149 | // this method is only overridden in order to synchronize the control with | |
150 | // the main window when it is scrolled, the derived class must implement | |
151 | // DoScrollHorz() | |
152 | virtual void ScrollWindow(int dx, int dy, const wxRect *rect = NULL); | |
153 | ||
154 | protected: | |
155 | // this method must be implemented by the derived classes to return the | |
156 | // information for the given column | |
dcb6cbec | 157 | virtual wxHeaderColumn& GetColumn(unsigned int idx) = 0; |
e2bfe673 | 158 | |
3bfaa5a7 VZ |
159 | // this method is called from the default EVT_HEADER_SEPARATOR_DCLICK |
160 | // handler to update the fitting column width of the given column, it | |
161 | // should return true if the width was really updated | |
162 | virtual bool UpdateColumnWidthToFit(unsigned int WXUNUSED(idx), | |
163 | int WXUNUSED(widthTitle)) | |
164 | { | |
165 | return false; | |
166 | } | |
167 | ||
613de0e8 VZ |
168 | // this method is called from ShowColumnsMenu() and must be overridden to |
169 | // update the internal column visibility (there is no need to call | |
170 | // UpdateColumn() from here, this will be done internally) | |
171 | virtual void UpdateColumnVisibility(unsigned int WXUNUSED(idx), | |
172 | bool WXUNUSED(show)) | |
173 | { | |
174 | wxFAIL_MSG( "must be overridden if called" ); | |
175 | } | |
176 | ||
4635abac VZ |
177 | // this method can be overridden in the derived classes to do something |
178 | // (e.g. update/resize some internal data structures) before the number of | |
179 | // columns in the control changes | |
180 | virtual void OnColumnCountChanging(unsigned int WXUNUSED(count)) { } | |
181 | ||
f6655391 VZ |
182 | |
183 | // helper function for the derived classes: update the array of column | |
184 | // indices after the number of columns changed | |
185 | void DoResizeColumnIndices(wxArrayInt& colIndices, unsigned int count); | |
186 | ||
e2bfe673 VZ |
187 | private: |
188 | // methods implementing our public API and defined in platform-specific | |
189 | // implementations | |
190 | virtual void DoSetCount(unsigned int count) = 0; | |
191 | virtual unsigned int DoGetCount() const = 0; | |
192 | virtual void DoUpdate(unsigned int idx) = 0; | |
193 | ||
194 | virtual void DoScrollHorz(int dx) = 0; | |
195 | ||
702f5349 VZ |
196 | virtual void DoSetColumnsOrder(const wxArrayInt& order) = 0; |
197 | virtual wxArrayInt DoGetColumnsOrder() const = 0; | |
198 | ||
e2bfe673 VZ |
199 | // this window doesn't look nice with the border so don't use it by default |
200 | virtual wxBorder GetDefaultBorder() const { return wxBORDER_NONE; } | |
3bfaa5a7 VZ |
201 | |
202 | // event handlers | |
203 | void OnSeparatorDClick(wxHeaderCtrlEvent& event); | |
613de0e8 | 204 | void OnRClick(wxHeaderCtrlEvent& event); |
3bfaa5a7 VZ |
205 | |
206 | DECLARE_EVENT_TABLE() | |
e2bfe673 VZ |
207 | }; |
208 | ||
209 | // ---------------------------------------------------------------------------- | |
210 | // wxHeaderCtrl: port-specific header control implementation, notice that this | |
211 | // is still an ABC which is meant to be used as part of another | |
212 | // control, see wxHeaderCtrlSimple for a standalone version | |
213 | // ---------------------------------------------------------------------------- | |
214 | ||
70405f7e | 215 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
e2bfe673 VZ |
216 | #include "wx/msw/headerctrl.h" |
217 | #else | |
218 | #define wxHAS_GENERIC_HEADERCTRL | |
219 | #include "wx/generic/headerctrlg.h" | |
220 | #endif // platform | |
221 | ||
222 | // ---------------------------------------------------------------------------- | |
223 | // wxHeaderCtrlSimple: concrete header control which can be used standalone | |
224 | // ---------------------------------------------------------------------------- | |
225 | ||
226 | class WXDLLIMPEXP_CORE wxHeaderCtrlSimple : public wxHeaderCtrl | |
227 | { | |
228 | public: | |
229 | // control creation | |
230 | // ---------------- | |
231 | ||
232 | wxHeaderCtrlSimple() { Init(); } | |
233 | wxHeaderCtrlSimple(wxWindow *parent, | |
234 | wxWindowID winid = wxID_ANY, | |
235 | const wxPoint& pos = wxDefaultPosition, | |
236 | const wxSize& size = wxDefaultSize, | |
237 | long style = wxHD_DEFAULT_STYLE, | |
238 | const wxString& name = wxHeaderCtrlNameStr) | |
239 | { | |
240 | Init(); | |
241 | ||
242 | Create(parent, winid, pos, size, style, name); | |
243 | } | |
244 | ||
245 | // managing the columns | |
246 | // -------------------- | |
56873923 VZ |
247 | |
248 | // insert the column at the given position, using GetColumnCount() as | |
249 | // position appends it at the end | |
e2bfe673 | 250 | void InsertColumn(const wxHeaderColumnSimple& col, unsigned int idx) |
56873923 VZ |
251 | { |
252 | wxCHECK_RET( idx <= GetColumnCount(), "invalid column index" ); | |
253 | ||
254 | DoInsert(col, idx); | |
255 | } | |
256 | ||
257 | // append the column to the end of the control | |
e2bfe673 | 258 | void AppendColumn(const wxHeaderColumnSimple& col) |
56873923 VZ |
259 | { |
260 | DoInsert(col, GetColumnCount()); | |
261 | } | |
262 | ||
263 | // delete the column at the given index | |
264 | void DeleteColumn(unsigned int idx) | |
265 | { | |
266 | wxCHECK_RET( idx < GetColumnCount(), "invalid column index" ); | |
267 | ||
268 | DoDelete(idx); | |
269 | } | |
270 | ||
271 | // delete all the existing columns | |
272 | void DeleteAllColumns(); | |
273 | ||
274 | ||
275 | // modifying columns | |
276 | // ----------------- | |
277 | ||
a0009205 VZ |
278 | // show or hide the column, notice that even when a column is hidden we |
279 | // still account for it when using indices | |
280 | void ShowColumn(unsigned int idx, bool show = true) | |
281 | { | |
282 | wxCHECK_RET( idx < GetColumnCount(), "invalid column index" ); | |
283 | ||
284 | DoShowColumn(idx, show); | |
285 | } | |
286 | ||
287 | void HideColumn(unsigned int idx) | |
288 | { | |
289 | ShowColumn(idx, false); | |
290 | } | |
291 | ||
e2bfe673 VZ |
292 | // indicate that the column is used for sorting |
293 | void ShowSortIndicator(unsigned int idx, bool ascending = true) | |
56873923 | 294 | { |
a0009205 VZ |
295 | wxCHECK_RET( idx < GetColumnCount(), "invalid column index" ); |
296 | ||
e2bfe673 | 297 | DoShowSortIndicator(idx, ascending); |
56873923 VZ |
298 | } |
299 | ||
e2bfe673 VZ |
300 | // remove the sort indicator completely |
301 | void RemoveSortIndicator(); | |
56873923 | 302 | |
e2bfe673 | 303 | protected: |
e5a16353 | 304 | // implement/override base class methods |
dcb6cbec | 305 | virtual wxHeaderColumn& GetColumn(unsigned int idx); |
e5a16353 VZ |
306 | virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle); |
307 | ||
308 | // and define another one to be overridden in the derived classes: it | |
309 | // should return the best width for the given column contents or -1 if not | |
310 | // implemented, we use it to implement UpdateColumnWidthToFit() | |
311 | virtual int GetBestFittingWidth(unsigned int WXUNUSED(idx)) const | |
312 | { | |
313 | return -1; | |
314 | } | |
56873923 | 315 | |
e2bfe673 VZ |
316 | private: |
317 | // functions implementing our public API | |
318 | void DoInsert(const wxHeaderColumnSimple& col, unsigned int idx); | |
319 | void DoDelete(unsigned int idx); | |
320 | void DoShowColumn(unsigned int idx, bool show); | |
321 | void DoShowSortIndicator(unsigned int idx, bool ascending); | |
56873923 | 322 | |
e2bfe673 VZ |
323 | // common part of all ctors |
324 | void Init(); | |
56873923 | 325 | |
e2bfe673 VZ |
326 | // bring the column count in sync with the number of columns we store |
327 | void UpdateColumnCount() { SetColumnCount(m_cols.size()); } | |
d8fc3398 | 328 | |
a3e0efb6 | 329 | |
e2bfe673 VZ |
330 | // all our current columns |
331 | typedef wxVector<wxHeaderColumnSimple> Columns; | |
332 | Columns m_cols; | |
56873923 | 333 | |
e2bfe673 VZ |
334 | // the column currently used for sorting or -1 if none |
335 | unsigned int m_sortKey; | |
336 | ||
337 | ||
338 | DECLARE_NO_COPY_CLASS(wxHeaderCtrlSimple) | |
339 | }; | |
56873923 | 340 | |
fa3d4aaf VZ |
341 | // ---------------------------------------------------------------------------- |
342 | // wxHeaderCtrl events | |
343 | // ---------------------------------------------------------------------------- | |
344 | ||
345 | class WXDLLIMPEXP_CORE wxHeaderCtrlEvent : public wxNotifyEvent | |
346 | { | |
347 | public: | |
348 | wxHeaderCtrlEvent(wxEventType commandType = wxEVT_NULL, int winid = 0) | |
aef252d9 VZ |
349 | : wxNotifyEvent(commandType, winid), |
350 | m_col(-1), | |
351 | m_width(0), | |
565804f2 | 352 | m_order(static_cast<unsigned int>(-1)) |
fa3d4aaf VZ |
353 | { |
354 | } | |
355 | ||
356 | wxHeaderCtrlEvent(const wxHeaderCtrlEvent& event) | |
357 | : wxNotifyEvent(event), | |
aef252d9 VZ |
358 | m_col(event.m_col), |
359 | m_width(event.m_width), | |
565804f2 | 360 | m_order(event.m_order) |
fa3d4aaf VZ |
361 | { |
362 | } | |
363 | ||
aef252d9 | 364 | // the column which this event pertains to: valid for all header events |
fa3d4aaf VZ |
365 | int GetColumn() const { return m_col; } |
366 | void SetColumn(int col) { m_col = col; } | |
367 | ||
aef252d9 VZ |
368 | // the width of the column: valid for column resizing/dragging events only |
369 | int GetWidth() const { return m_width; } | |
370 | void SetWidth(int width) { m_width = width; } | |
371 | ||
702f5349 VZ |
372 | // the new position of the column: for end reorder events only |
373 | unsigned int GetNewOrder() const { return m_order; } | |
374 | void SetNewOrder(unsigned int order) { m_order = order; } | |
375 | ||
fa3d4aaf VZ |
376 | virtual wxEvent *Clone() const { return new wxHeaderCtrlEvent(*this); } |
377 | ||
378 | protected: | |
379 | // the column affected by the event | |
380 | int m_col; | |
381 | ||
aef252d9 VZ |
382 | // the current width for the dragging events |
383 | int m_width; | |
384 | ||
702f5349 VZ |
385 | // the new column position for end reorder event |
386 | unsigned int m_order; | |
387 | ||
fa3d4aaf VZ |
388 | private: |
389 | DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxHeaderCtrlEvent) | |
390 | }; | |
391 | ||
392 | ||
056d5a89 VZ |
393 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_CLICK; |
394 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_RIGHT_CLICK; | |
395 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_CLICK; | |
fa3d4aaf | 396 | |
056d5a89 VZ |
397 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_DCLICK; |
398 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_RIGHT_DCLICK; | |
399 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_DCLICK; | |
fa3d4aaf | 400 | |
3bfaa5a7 VZ |
401 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK; |
402 | ||
396825dc VZ |
403 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_BEGIN_RESIZE; |
404 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_RESIZING; | |
405 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_END_RESIZE; | |
aef252d9 | 406 | |
702f5349 VZ |
407 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_BEGIN_REORDER; |
408 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_END_REORDER; | |
409 | ||
565804f2 VZ |
410 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED; |
411 | ||
fa3d4aaf VZ |
412 | typedef void (wxEvtHandler::*wxHeaderCtrlEventFunction)(wxHeaderCtrlEvent&); |
413 | ||
414 | #define wxHeaderCtrlEventHandler(func) \ | |
415 | (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent( \ | |
416 | wxHeaderCtrlEventFunction, &func) | |
417 | ||
418 | #define wx__DECLARE_HEADER_EVT(evt, id, fn) \ | |
419 | wx__DECLARE_EVT1(wxEVT_COMMAND_HEADER_ ## evt, id, wxHeaderCtrlEventHandler(fn)) | |
420 | ||
421 | #define EVT_HEADER_CLICK(id, fn) wx__DECLARE_HEADER_EVT(CLICK, id, fn) | |
422 | #define EVT_HEADER_RIGHT_CLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_CLICK, id, fn) | |
423 | #define EVT_HEADER_MIDDLE_CLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_CLICK, id, fn) | |
424 | ||
425 | #define EVT_HEADER_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(DCLICK, id, fn) | |
426 | #define EVT_HEADER_RIGHT_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_DCLICK, id, fn) | |
427 | #define EVT_HEADER_MIDDLE_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_DCLICK, id, fn) | |
428 | ||
3bfaa5a7 VZ |
429 | #define EVT_HEADER_SEPARATOR_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(SEPARATOR_DCLICK, id, fn) |
430 | ||
396825dc VZ |
431 | #define EVT_HEADER_BEGIN_RESIZE(id, fn) wx__DECLARE_HEADER_EVT(BEGIN_RESIZE, id, fn) |
432 | #define EVT_HEADER_RESIZING(id, fn) wx__DECLARE_HEADER_EVT(RESIZING, id, fn) | |
433 | #define EVT_HEADER_END_RESIZE(id, fn) wx__DECLARE_HEADER_EVT(END_RESIZE, id, fn) | |
aef252d9 | 434 | |
702f5349 VZ |
435 | #define EVT_HEADER_BEGIN_REORDER(id, fn) wx__DECLARE_HEADER_EVT(BEGIN_REORDER, id, fn) |
436 | #define EVT_HEADER_END_REORDER(id, fn) wx__DECLARE_HEADER_EVT(END_REORDER, id, fn) | |
437 | ||
565804f2 VZ |
438 | #define EVT_HEADER_DRAGGING_CANCELLED(id, fn) wx__DECLARE_HEADER_EVT(DRAGGING_CANCELLED, id, fn) |
439 | ||
56873923 | 440 | #endif // _WX_HEADERCTRL_H_ |