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