]>
Commit | Line | Data |
---|---|---|
4ed7af08 | 1 | ///////////////////////////////////////////////////////////////////////////// |
f554a14b | 2 | // Name: src/generic/datavgen.cpp |
4ed7af08 RR |
3 | // Purpose: wxDataViewCtrl generic implementation |
4 | // Author: Robert Roebling | |
87f0efe2 | 5 | // Modified by: Francesco Montorsi, Guru Kathiresan, Otto Wyss |
4ed7af08 RR |
6 | // Id: $Id$ |
7 | // Copyright: (c) 1998 Robert Roebling | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // For compilers that support precompilation, includes "wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
ed4b0fdc WS |
14 | #ifdef __BORLANDC__ |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
4ed7af08 RR |
18 | #if wxUSE_DATAVIEWCTRL |
19 | ||
20 | #include "wx/dataview.h" | |
21 | ||
22 | #ifdef wxUSE_GENERICDATAVIEWCTRL | |
23 | ||
f554a14b | 24 | #ifndef WX_PRECOMP |
57bd4c60 | 25 | #ifdef __WXMSW__ |
87f0efe2 | 26 | #include "wx/msw/private.h" |
57bd4c60 | 27 | #include "wx/msw/wrapwin.h" |
87f0efe2 | 28 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" |
57bd4c60 | 29 | #endif |
f554a14b WS |
30 | #include "wx/sizer.h" |
31 | #include "wx/log.h" | |
ed4b0fdc | 32 | #include "wx/dcclient.h" |
c0badb70 | 33 | #include "wx/timer.h" |
9eddec69 | 34 | #include "wx/settings.h" |
8d0ca292 | 35 | #include "wx/msgdlg.h" |
99d471a5 | 36 | #include "wx/dcscreen.h" |
f554a14b WS |
37 | #endif |
38 | ||
4ed7af08 | 39 | #include "wx/stockitem.h" |
4ed7af08 RR |
40 | #include "wx/calctrl.h" |
41 | #include "wx/popupwin.h" | |
4ed7af08 | 42 | #include "wx/renderer.h" |
2e992e06 | 43 | #include "wx/dcbuffer.h" |
2586d4a1 | 44 | #include "wx/icon.h" |
351461fc RR |
45 | #include "wx/list.h" |
46 | #include "wx/listimpl.cpp" | |
4ed7af08 | 47 | |
4ed7af08 RR |
48 | //----------------------------------------------------------------------------- |
49 | // classes | |
50 | //----------------------------------------------------------------------------- | |
51 | ||
52 | class wxDataViewCtrl; | |
53 | ||
9861f022 RR |
54 | static const int SCROLL_UNIT_X = 15; |
55 | ||
56 | // the cell padding on the left/right | |
57 | static const int PADDING_RIGHTLEFT = 3; | |
58 | ||
59 | // the cell padding on the top/bottom | |
60 | static const int PADDING_TOPBOTTOM = 1; | |
61 | ||
3b6280be RR |
62 | // the expander space margin |
63 | static const int EXPANDER_MARGIN = 4; | |
9861f022 | 64 | |
a0f3af5f RR |
65 | //----------------------------------------------------------------------------- |
66 | // wxDataViewHeaderWindow | |
67 | //----------------------------------------------------------------------------- | |
4ed7af08 | 68 | |
87f0efe2 RR |
69 | #define USE_NATIVE_HEADER_WINDOW 1 |
70 | ||
c741d33f VZ |
71 | // NB: for some reason, this class must be dllexport'ed or we get warnings from |
72 | // MSVC in DLL build | |
73 | class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase : public wxControl | |
87f0efe2 RR |
74 | { |
75 | public: | |
76 | wxDataViewHeaderWindowBase() | |
77 | { m_owner = NULL; } | |
78 | ||
79 | bool Create(wxDataViewCtrl *parent, wxWindowID id, | |
c741d33f | 80 | const wxPoint &pos, const wxSize &size, |
87f0efe2 RR |
81 | const wxString &name) |
82 | { | |
83 | return wxWindow::Create(parent, id, pos, size, wxNO_BORDER, name); | |
84 | } | |
85 | ||
86 | void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; } | |
87 | wxDataViewCtrl *GetOwner() { return m_owner; } | |
88 | ||
89 | // called on column addition/removal | |
90 | virtual void UpdateDisplay() { /* by default, do nothing */ } | |
91 | ||
87f0efe2 | 92 | // returns the n-th column |
9861f022 | 93 | virtual wxDataViewColumn *GetColumn(unsigned int n) |
87f0efe2 RR |
94 | { |
95 | wxASSERT(m_owner); | |
c741d33f | 96 | wxDataViewColumn *ret = m_owner->GetColumn(n); |
87f0efe2 RR |
97 | wxASSERT(ret); |
98 | ||
c741d33f | 99 | return ret; |
87f0efe2 RR |
100 | } |
101 | ||
102 | protected: | |
103 | wxDataViewCtrl *m_owner; | |
104 | ||
105 | // sends an event generated from the n-th wxDataViewColumn | |
106 | void SendEvent(wxEventType type, unsigned int n); | |
107 | }; | |
108 | ||
109 | // on wxMSW the header window (only that part however) can be made native! | |
110 | #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW | |
111 | ||
9861f022 | 112 | #define COLUMN_WIDTH_OFFSET 2 |
87f0efe2 RR |
113 | #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow |
114 | ||
115 | class wxDataViewHeaderWindowMSW : public wxDataViewHeaderWindowBase | |
4ed7af08 RR |
116 | { |
117 | public: | |
87f0efe2 RR |
118 | |
119 | wxDataViewHeaderWindowMSW( wxDataViewCtrl *parent, | |
9861f022 RR |
120 | wxWindowID id, |
121 | const wxPoint &pos = wxDefaultPosition, | |
122 | const wxSize &size = wxDefaultSize, | |
123 | const wxString &name = wxT("wxdataviewctrlheaderwindow") ) | |
c741d33f | 124 | { |
87f0efe2 RR |
125 | Create(parent, id, pos, size, name); |
126 | } | |
4ed7af08 | 127 | |
87f0efe2 | 128 | bool Create(wxDataViewCtrl *parent, wxWindowID id, |
c741d33f | 129 | const wxPoint &pos, const wxSize &size, |
87f0efe2 RR |
130 | const wxString &name); |
131 | ||
132 | ~wxDataViewHeaderWindowMSW(); | |
133 | ||
9861f022 RR |
134 | // called when any column setting is changed and/or changed |
135 | // the column count | |
87f0efe2 RR |
136 | virtual void UpdateDisplay(); |
137 | ||
138 | // called when the main window gets scrolled | |
139 | virtual void ScrollWindow(int dx, int dy, const wxRect *rect = NULL); | |
140 | ||
141 | protected: | |
142 | virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result); | |
9861f022 RR |
143 | virtual void DoSetSize(int x, int y, int width, int height, int sizeFlags); |
144 | ||
145 | unsigned int GetColumnIdxFromHeader(NMHEADER *nmHDR); | |
146 | ||
147 | wxDataViewColumn *GetColumnFromHeader(NMHEADER *nmHDR) | |
148 | { return GetColumn(GetColumnIdxFromHeader(nmHDR)); } | |
149 | ||
87f0efe2 RR |
150 | private: |
151 | DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW) | |
152 | }; | |
153 | ||
154 | #else // !defined(__WXMSW__) | |
155 | ||
156 | #define HEADER_WINDOW_HEIGHT 25 | |
9861f022 RR |
157 | #define HEADER_HORIZ_BORDER 5 |
158 | #define HEADER_VERT_BORDER 3 | |
87f0efe2 RR |
159 | #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow |
160 | ||
161 | class wxGenericDataViewHeaderWindow : public wxDataViewHeaderWindowBase | |
162 | { | |
163 | public: | |
164 | wxGenericDataViewHeaderWindow( wxDataViewCtrl *parent, | |
165 | wxWindowID id, | |
166 | const wxPoint &pos = wxDefaultPosition, | |
167 | const wxSize &size = wxDefaultSize, | |
168 | const wxString &name = wxT("wxdataviewctrlheaderwindow") ) | |
c741d33f | 169 | { |
87f0efe2 RR |
170 | Init(); |
171 | Create(parent, id, pos, size, name); | |
172 | } | |
173 | ||
174 | bool Create(wxDataViewCtrl *parent, wxWindowID id, | |
c741d33f | 175 | const wxPoint &pos, const wxSize &size, |
87f0efe2 RR |
176 | const wxString &name); |
177 | ||
178 | ~wxGenericDataViewHeaderWindow() | |
179 | { | |
180 | delete m_resizeCursor; | |
181 | } | |
182 | ||
c3112d56 | 183 | virtual void UpdateDisplay() { Refresh(); } |
442c56e6 | 184 | |
87f0efe2 | 185 | // event handlers: |
4ed7af08 | 186 | |
a0f3af5f RR |
187 | void OnPaint( wxPaintEvent &event ); |
188 | void OnMouse( wxMouseEvent &event ); | |
189 | void OnSetFocus( wxFocusEvent &event ); | |
f554a14b | 190 | |
87f0efe2 RR |
191 | |
192 | protected: | |
193 | ||
194 | // vars used for column resizing: | |
195 | ||
a0f3af5f | 196 | wxCursor *m_resizeCursor; |
87f0efe2 RR |
197 | const wxCursor *m_currentCursor; |
198 | bool m_isDragging; | |
199 | ||
200 | bool m_dirty; // needs refresh? | |
201 | int m_column; // index of the column being resized | |
202 | int m_currentX; // divider line position in logical (unscrolled) coords | |
c741d33f | 203 | int m_minX; // minimal position beyond which the divider line |
87f0efe2 RR |
204 | // can't be dragged in logical coords |
205 | ||
9861f022 RR |
206 | // the pen used to draw the current column width drag line |
207 | // when resizing the columsn | |
208 | wxPen m_penCurrent; | |
209 | ||
210 | ||
87f0efe2 RR |
211 | // internal utilities: |
212 | ||
213 | void Init() | |
214 | { | |
215 | m_currentCursor = (wxCursor *) NULL; | |
216 | m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE ); | |
217 | ||
218 | m_isDragging = false; | |
219 | m_dirty = false; | |
220 | ||
221 | m_column = wxNOT_FOUND; | |
222 | m_currentX = 0; | |
223 | m_minX = 0; | |
9861f022 RR |
224 | |
225 | wxColour col = wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT); | |
226 | m_penCurrent = wxPen(col, 1, wxSOLID); | |
87f0efe2 RR |
227 | } |
228 | ||
229 | void DrawCurrent(); | |
230 | void AdjustDC(wxDC& dc); | |
f554a14b | 231 | |
a0f3af5f | 232 | private: |
87f0efe2 | 233 | DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow) |
a0f3af5f RR |
234 | DECLARE_EVENT_TABLE() |
235 | }; | |
4ed7af08 | 236 | |
87f0efe2 RR |
237 | #endif // defined(__WXMSW__) |
238 | ||
0fcce6b9 RR |
239 | //----------------------------------------------------------------------------- |
240 | // wxDataViewRenameTimer | |
241 | //----------------------------------------------------------------------------- | |
242 | ||
243 | class wxDataViewRenameTimer: public wxTimer | |
244 | { | |
245 | private: | |
246 | wxDataViewMainWindow *m_owner; | |
247 | ||
248 | public: | |
249 | wxDataViewRenameTimer( wxDataViewMainWindow *owner ); | |
250 | void Notify(); | |
251 | }; | |
252 | ||
aba9bfd0 RR |
253 | //----------------------------------------------------------------------------- |
254 | // wxDataViewTreeNode | |
255 | //----------------------------------------------------------------------------- | |
442c56e6 | 256 | class wxDataViewTreeNode; |
d47db7e0 RR |
257 | WX_DEFINE_SORTED_ARRAY( wxDataViewTreeNode *, wxDataViewTreeNodes ); |
258 | WX_DEFINE_SORTED_ARRAY( void* , wxDataViewTreeLeaves); | |
259 | ||
260 | int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode * node1, wxDataViewTreeNode * node2); | |
261 | int LINKAGEMODE wxGenericTreeModelItemCmp( void * id1, void * id2); | |
aba9bfd0 RR |
262 | |
263 | class wxDataViewTreeNode | |
264 | { | |
265 | public: | |
266 | wxDataViewTreeNode( wxDataViewTreeNode * parent ) | |
d47db7e0 RR |
267 | :leaves( wxGenericTreeModelItemCmp ), |
268 | nodes(wxGenericTreeModelNodeCmp) | |
442c56e6 | 269 | { this->parent = parent; |
aba9bfd0 RR |
270 | if( parent == NULL ) |
271 | open = true; | |
272 | else | |
273 | open = false; | |
704c3490 | 274 | hasChildren = false; |
d47db7e0 | 275 | subTreeCount = 0; |
aba9bfd0 RR |
276 | } |
277 | //I don't know what I need to do in the destructure | |
278 | ~wxDataViewTreeNode() | |
d47db7e0 | 279 | { |
442c56e6 | 280 | |
d47db7e0 | 281 | } |
aba9bfd0 RR |
282 | |
283 | wxDataViewTreeNode * GetParent() { return parent; } | |
284 | void SetParent( wxDataViewTreeNode * parent ) { this->parent = parent; } | |
d47db7e0 RR |
285 | wxDataViewTreeNodes & GetNodes() { return nodes; } |
286 | wxDataViewTreeLeaves & GetChildren() { return leaves; } | |
442c56e6 VZ |
287 | |
288 | void AddNode( wxDataViewTreeNode * node ) | |
d47db7e0 RR |
289 | { |
290 | nodes.Add( node ); | |
291 | leaves.Add( node->GetItem().GetID() ); | |
292 | } | |
293 | void AddLeaf( void * leaf ) { leaves.Add( leaf ); } | |
aba9bfd0 RR |
294 | |
295 | wxDataViewItem & GetItem() { return item; } | |
351461fc | 296 | void SetItem( const wxDataViewItem & item ) { this->item = item; } |
aba9bfd0 | 297 | |
d47db7e0 RR |
298 | unsigned int GetChildrenNumber() { return leaves.GetCount(); } |
299 | unsigned int GetNodeNumber() { return nodes.GetCount(); } | |
3b6280be RR |
300 | int GetIndentLevel() |
301 | { | |
302 | int ret = 0 ; | |
303 | wxDataViewTreeNode * node = this; | |
304 | while( node->GetParent()->GetParent() != NULL ) | |
305 | { | |
306 | node = node->GetParent(); | |
307 | ret ++; | |
308 | } | |
309 | return ret; | |
310 | } | |
aba9bfd0 | 311 | |
442c56e6 VZ |
312 | bool IsOpen() |
313 | { | |
314 | return open ; | |
315 | } | |
d47db7e0 RR |
316 | |
317 | void ToggleOpen() | |
442c56e6 | 318 | { |
d47db7e0 RR |
319 | int len = nodes.GetCount(); |
320 | int sum = 0; | |
321 | for ( int i = 0 ;i < len ; i ++) | |
322 | sum += nodes[i]->GetSubTreeCount(); | |
323 | ||
324 | sum += leaves.GetCount(); | |
325 | if( open ) | |
326 | { | |
327 | ChangeSubTreeCount(-sum); | |
328 | open = !open; | |
329 | } | |
330 | else | |
331 | { | |
332 | open = !open; | |
333 | ChangeSubTreeCount(sum); | |
334 | } | |
335 | } | |
704c3490 RR |
336 | bool HasChildren() { return hasChildren; } |
337 | void SetHasChildren( bool has ){ hasChildren = has; } | |
d47db7e0 RR |
338 | |
339 | void SetSubTreeCount( int num ) { subTreeCount = num; } | |
340 | int GetSubTreeCount() { return subTreeCount; } | |
442c56e6 | 341 | void ChangeSubTreeCount( int num ) |
d47db7e0 RR |
342 | { |
343 | if( !open ) | |
344 | return ; | |
345 | subTreeCount += num; | |
346 | if( parent ) | |
347 | parent->ChangeSubTreeCount(num); | |
348 | } | |
349 | ||
aba9bfd0 RR |
350 | private: |
351 | wxDataViewTreeNode * parent; | |
d47db7e0 RR |
352 | wxDataViewTreeNodes nodes; |
353 | wxDataViewTreeLeaves leaves; | |
442c56e6 | 354 | wxDataViewItem item; |
aba9bfd0 | 355 | bool open; |
704c3490 | 356 | bool hasChildren; |
d47db7e0 | 357 | int subTreeCount; |
aba9bfd0 RR |
358 | }; |
359 | ||
d47db7e0 RR |
360 | //Below is the compare stuff |
361 | //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed | |
362 | static wxDataViewModel * g_model; | |
363 | ||
364 | int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode * node1, wxDataViewTreeNode * node2) | |
365 | { | |
366 | return g_model->Compare( node1->GetItem(), node2->GetItem() ); | |
367 | } | |
368 | ||
369 | int LINKAGEMODE wxGenericTreeModelItemCmp( void * id1, void * id2) | |
370 | { | |
371 | return g_model->Compare( id1, id2 ); | |
372 | } | |
373 | ||
374 | ||
375 | ||
a0f3af5f RR |
376 | //----------------------------------------------------------------------------- |
377 | // wxDataViewMainWindow | |
378 | //----------------------------------------------------------------------------- | |
4ed7af08 | 379 | |
c741d33f | 380 | WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection, |
87f0efe2 | 381 | WXDLLIMPEXP_ADV); |
351461fc RR |
382 | WX_DECLARE_LIST(wxDataViewItem, ItemList); |
383 | WX_DEFINE_LIST(ItemList); | |
cab07038 | 384 | |
a0f3af5f | 385 | class wxDataViewMainWindow: public wxWindow |
4ed7af08 | 386 | { |
a0f3af5f RR |
387 | public: |
388 | wxDataViewMainWindow( wxDataViewCtrl *parent, | |
389 | wxWindowID id, | |
390 | const wxPoint &pos = wxDefaultPosition, | |
391 | const wxSize &size = wxDefaultSize, | |
392 | const wxString &name = wxT("wxdataviewctrlmainwindow") ); | |
d3c7fc99 | 393 | virtual ~wxDataViewMainWindow(); |
4ed7af08 | 394 | |
aba9bfd0 RR |
395 | // notifications from wxDataViewModel |
396 | bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item ); | |
32143117 | 397 | bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ); |
aba9bfd0 RR |
398 | bool ItemChanged( const wxDataViewItem &item ); |
399 | bool ValueChanged( const wxDataViewItem &item, unsigned int col ); | |
a0f3af5f | 400 | bool Cleared(); |
4ed7af08 | 401 | |
a0f3af5f RR |
402 | void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; } |
403 | wxDataViewCtrl *GetOwner() { return m_owner; } | |
9861f022 | 404 | const wxDataViewCtrl *GetOwner() const { return m_owner; } |
4ed7af08 | 405 | |
a0f3af5f | 406 | void OnPaint( wxPaintEvent &event ); |
0a71f9e9 | 407 | void OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event); |
cab07038 | 408 | void OnChar( wxKeyEvent &event ); |
a0f3af5f RR |
409 | void OnMouse( wxMouseEvent &event ); |
410 | void OnSetFocus( wxFocusEvent &event ); | |
cab07038 | 411 | void OnKillFocus( wxFocusEvent &event ); |
f554a14b | 412 | |
a0f3af5f RR |
413 | void UpdateDisplay(); |
414 | void RecalculateDisplay(); | |
415 | void OnInternalIdle(); | |
f554a14b | 416 | |
0fcce6b9 | 417 | void OnRenameTimer(); |
0fcce6b9 | 418 | |
9861f022 | 419 | void ScrollWindow( int dx, int dy, const wxRect *rect = NULL ); |
120b9b05 | 420 | |
0a71f9e9 RR |
421 | bool HasCurrentRow() { return m_currentRow != (unsigned int)-1; } |
422 | void ChangeCurrentRow( unsigned int row ); | |
120b9b05 | 423 | |
47b378bd | 424 | bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE); } |
cab07038 | 425 | bool IsEmpty() { return GetRowCount() == 0; } |
120b9b05 | 426 | |
9861f022 RR |
427 | int GetCountPerPage() const; |
428 | int GetEndOfLastCol() const; | |
429 | unsigned int GetFirstVisibleRow() const; | |
3b6280be RR |
430 | //I change this method to un const because in the tree view, the displaying number of the tree are changing along with the expanding/collapsing of the tree nodes |
431 | unsigned int GetLastVisibleRow(); | |
432 | unsigned int GetRowCount() ; | |
120b9b05 | 433 | |
704c3490 RR |
434 | wxDataViewItem GetSelection(); |
435 | ||
87f0efe2 | 436 | void Select( const wxArrayInt& aSelections ); |
cab07038 | 437 | void SelectAllRows( bool on ); |
0a71f9e9 RR |
438 | void SelectRow( unsigned int row, bool on ); |
439 | void SelectRows( unsigned int from, unsigned int to, bool on ); | |
440 | void ReverseRowSelection( unsigned int row ); | |
441 | bool IsRowSelected( unsigned int row ); | |
120b9b05 | 442 | |
0a71f9e9 RR |
443 | void RefreshRow( unsigned int row ); |
444 | void RefreshRows( unsigned int from, unsigned int to ); | |
445 | void RefreshRowsAfter( unsigned int firstRow ); | |
120b9b05 | 446 | |
9861f022 RR |
447 | // returns the colour to be used for drawing the rules |
448 | wxColour GetRuleColour() const | |
449 | { | |
450 | return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT); | |
451 | } | |
452 | ||
453 | //void EnsureVisible( unsigned int row ); | |
454 | wxRect GetLineRect( unsigned int row ) const; | |
455 | ||
aba9bfd0 RR |
456 | //Some useful functions for row and item mapping |
457 | wxDataViewItem GetItemByRow( unsigned int row ); | |
458 | unsigned int GetRowByItem( const wxDataViewItem & item ); | |
459 | ||
460 | //Methods for building the mapping tree | |
461 | void BuildTree( wxDataViewModel * model ); | |
462 | void DestroyTree(); | |
3b6280be RR |
463 | private: |
464 | wxDataViewTreeNode * GetTreeNodeByRow( unsigned int row ); | |
704c3490 RR |
465 | //We did not need this temporarily |
466 | //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item ); | |
3b6280be RR |
467 | |
468 | int RecalculateCount() ; | |
469 | ||
470 | void OnExpanding( unsigned int row ); | |
471 | void OnCollapsing( unsigned int row ); | |
aba9bfd0 | 472 | |
442c56e6 | 473 | wxDataViewTreeNode * FindNode( const wxDataViewItem & item ); |
351461fc | 474 | |
a0f3af5f | 475 | private: |
0fcce6b9 RR |
476 | wxDataViewCtrl *m_owner; |
477 | int m_lineHeight; | |
478 | bool m_dirty; | |
120b9b05 | 479 | |
0fcce6b9 | 480 | wxDataViewColumn *m_currentCol; |
99d471a5 | 481 | unsigned int m_currentRow; |
cab07038 | 482 | wxDataViewSelection m_selection; |
120b9b05 | 483 | |
0fcce6b9 | 484 | wxDataViewRenameTimer *m_renameTimer; |
0fcce6b9 | 485 | bool m_lastOnSame; |
f554a14b | 486 | |
cab07038 RR |
487 | bool m_hasFocus; |
488 | ||
e21f75bd RR |
489 | int m_dragCount; |
490 | wxPoint m_dragStart; | |
491 | ||
492 | // for double click logic | |
0a71f9e9 | 493 | unsigned int m_lineLastClicked, |
e21f75bd RR |
494 | m_lineBeforeLastClicked, |
495 | m_lineSelectSingleOnUp; | |
cab07038 | 496 | |
9861f022 RR |
497 | // the pen used to draw horiz/vertical rules |
498 | wxPen m_penRule; | |
499 | ||
3b6280be RR |
500 | // the pen used to draw the expander and the lines |
501 | wxPen m_penExpander; | |
502 | ||
aba9bfd0 | 503 | //This is the tree structure of the model |
442c56e6 | 504 | wxDataViewTreeNode * m_root; |
3b6280be | 505 | int m_count; |
a0f3af5f RR |
506 | private: |
507 | DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow) | |
508 | DECLARE_EVENT_TABLE() | |
509 | }; | |
4ed7af08 | 510 | |
f554a14b | 511 | // --------------------------------------------------------- |
aba9bfd0 | 512 | // wxGenericDataViewModelNotifier |
f554a14b | 513 | // --------------------------------------------------------- |
a0f3af5f | 514 | |
aba9bfd0 | 515 | class wxGenericDataViewModelNotifier: public wxDataViewModelNotifier |
4ed7af08 | 516 | { |
a0f3af5f | 517 | public: |
aba9bfd0 | 518 | wxGenericDataViewModelNotifier( wxDataViewMainWindow *mainWindow ) |
a0f3af5f | 519 | { m_mainWindow = mainWindow; } |
f554a14b | 520 | |
aba9bfd0 RR |
521 | virtual bool ItemAdded( const wxDataViewItem & parent, const wxDataViewItem & item ) |
522 | { return m_mainWindow->ItemAdded( parent , item ); } | |
442c56e6 | 523 | virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ) |
071691a0 | 524 | { return m_mainWindow->ItemDeleted( parent, item ); } |
aba9bfd0 RR |
525 | virtual bool ItemChanged( const wxDataViewItem & item ) |
526 | { return m_mainWindow->ItemChanged(item); } | |
527 | virtual bool ValueChanged( const wxDataViewItem & item , unsigned int col ) | |
528 | { return m_mainWindow->ValueChanged( item, col ); } | |
a0f3af5f RR |
529 | virtual bool Cleared() |
530 | { return m_mainWindow->Cleared(); } | |
f554a14b | 531 | |
a0f3af5f RR |
532 | wxDataViewMainWindow *m_mainWindow; |
533 | }; | |
4ed7af08 | 534 | |
f554a14b | 535 | // --------------------------------------------------------- |
baa9ebc4 | 536 | // wxDataViewRenderer |
f554a14b | 537 | // --------------------------------------------------------- |
4ed7af08 | 538 | |
baa9ebc4 | 539 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase) |
4ed7af08 | 540 | |
c741d33f | 541 | wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, |
9861f022 RR |
542 | wxDataViewCellMode mode, |
543 | int align) : | |
544 | wxDataViewRendererBase( varianttype, mode, align ) | |
4ed7af08 | 545 | { |
3d9d7cc4 | 546 | m_dc = NULL; |
9861f022 RR |
547 | m_align = align; |
548 | m_mode = mode; | |
4ed7af08 RR |
549 | } |
550 | ||
baa9ebc4 | 551 | wxDataViewRenderer::~wxDataViewRenderer() |
3d9d7cc4 RR |
552 | { |
553 | if (m_dc) | |
554 | delete m_dc; | |
555 | } | |
556 | ||
baa9ebc4 | 557 | wxDC *wxDataViewRenderer::GetDC() |
3d9d7cc4 RR |
558 | { |
559 | if (m_dc == NULL) | |
560 | { | |
561 | if (GetOwner() == NULL) | |
562 | return NULL; | |
563 | if (GetOwner()->GetOwner() == NULL) | |
564 | return NULL; | |
565 | m_dc = new wxClientDC( GetOwner()->GetOwner() ); | |
566 | } | |
f554a14b | 567 | |
3d9d7cc4 RR |
568 | return m_dc; |
569 | } | |
570 | ||
f554a14b | 571 | // --------------------------------------------------------- |
baa9ebc4 | 572 | // wxDataViewCustomRenderer |
f554a14b | 573 | // --------------------------------------------------------- |
3d9d7cc4 | 574 | |
baa9ebc4 | 575 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer) |
3d9d7cc4 | 576 | |
baa9ebc4 | 577 | wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype, |
9861f022 RR |
578 | wxDataViewCellMode mode, int align ) : |
579 | wxDataViewRenderer( varianttype, mode, align ) | |
3d9d7cc4 RR |
580 | { |
581 | } | |
582 | ||
f554a14b | 583 | // --------------------------------------------------------- |
baa9ebc4 | 584 | // wxDataViewTextRenderer |
f554a14b | 585 | // --------------------------------------------------------- |
4ed7af08 | 586 | |
baa9ebc4 | 587 | IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewCustomRenderer) |
4ed7af08 | 588 | |
c741d33f | 589 | wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, |
9861f022 RR |
590 | wxDataViewCellMode mode, int align ) : |
591 | wxDataViewCustomRenderer( varianttype, mode, align ) | |
4ed7af08 RR |
592 | { |
593 | } | |
594 | ||
baa9ebc4 | 595 | bool wxDataViewTextRenderer::SetValue( const wxVariant &value ) |
4ed7af08 | 596 | { |
90675b95 | 597 | m_text = value.GetString(); |
f554a14b | 598 | |
90675b95 | 599 | return true; |
4ed7af08 RR |
600 | } |
601 | ||
9861f022 | 602 | bool wxDataViewTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const |
4ed7af08 RR |
603 | { |
604 | return false; | |
605 | } | |
606 | ||
99d471a5 | 607 | bool wxDataViewTextRenderer::HasEditorCtrl() |
442c56e6 | 608 | { |
99d471a5 RR |
609 | return true; |
610 | } | |
611 | ||
612 | wxControl* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow *parent, | |
613 | wxRect labelRect, const wxVariant &value ) | |
614 | { | |
442c56e6 | 615 | return new wxTextCtrl( parent, wxID_ANY, value, |
99d471a5 RR |
616 | wxPoint(labelRect.x,labelRect.y), |
617 | wxSize(labelRect.width,labelRect.height) ); | |
618 | } | |
619 | ||
620 | bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl *editor, wxVariant &value ) | |
621 | { | |
622 | wxTextCtrl *text = (wxTextCtrl*) editor; | |
623 | value = text->GetValue(); | |
624 | return true; | |
625 | } | |
626 | ||
87f0efe2 | 627 | bool wxDataViewTextRenderer::Render( wxRect cell, wxDC *dc, int state ) |
3d9d7cc4 | 628 | { |
87f0efe2 RR |
629 | wxDataViewCtrl *view = GetOwner()->GetOwner(); |
630 | wxColour col = (state & wxDATAVIEW_CELL_SELECTED) ? | |
631 | wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) : | |
632 | view->GetForegroundColour(); | |
633 | ||
634 | dc->SetTextForeground(col); | |
90675b95 RR |
635 | dc->DrawText( m_text, cell.x, cell.y ); |
636 | ||
637 | return true; | |
3d9d7cc4 RR |
638 | } |
639 | ||
9861f022 | 640 | wxSize wxDataViewTextRenderer::GetSize() const |
3d9d7cc4 | 641 | { |
9861f022 | 642 | const wxDataViewCtrl *view = GetView(); |
87f0efe2 RR |
643 | if (!m_text.empty()) |
644 | { | |
645 | int x,y; | |
646 | view->GetTextExtent( m_text, &x, &y ); | |
647 | return wxSize( x, y ); | |
648 | } | |
3d9d7cc4 RR |
649 | return wxSize(80,20); |
650 | } | |
651 | ||
2586d4a1 | 652 | // --------------------------------------------------------- |
baa9ebc4 | 653 | // wxDataViewBitmapRenderer |
2586d4a1 RR |
654 | // --------------------------------------------------------- |
655 | ||
baa9ebc4 | 656 | IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewCustomRenderer) |
2586d4a1 | 657 | |
c741d33f | 658 | wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, |
9861f022 RR |
659 | wxDataViewCellMode mode, int align ) : |
660 | wxDataViewCustomRenderer( varianttype, mode, align ) | |
2586d4a1 RR |
661 | { |
662 | } | |
663 | ||
baa9ebc4 | 664 | bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value ) |
2586d4a1 RR |
665 | { |
666 | if (value.GetType() == wxT("wxBitmap")) | |
667 | m_bitmap << value; | |
668 | if (value.GetType() == wxT("wxIcon")) | |
669 | m_icon << value; | |
670 | ||
671 | return true; | |
672 | } | |
673 | ||
9861f022 | 674 | bool wxDataViewBitmapRenderer::GetValue( wxVariant& WXUNUSED(value) ) const |
2586d4a1 RR |
675 | { |
676 | return false; | |
677 | } | |
678 | ||
baa9ebc4 | 679 | bool wxDataViewBitmapRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) ) |
2586d4a1 RR |
680 | { |
681 | if (m_bitmap.Ok()) | |
682 | dc->DrawBitmap( m_bitmap, cell.x, cell.y ); | |
683 | else if (m_icon.Ok()) | |
684 | dc->DrawIcon( m_icon, cell.x, cell.y ); | |
685 | ||
686 | return true; | |
687 | } | |
688 | ||
9861f022 | 689 | wxSize wxDataViewBitmapRenderer::GetSize() const |
2586d4a1 RR |
690 | { |
691 | if (m_bitmap.Ok()) | |
692 | return wxSize( m_bitmap.GetWidth(), m_bitmap.GetHeight() ); | |
693 | else if (m_icon.Ok()) | |
694 | return wxSize( m_icon.GetWidth(), m_icon.GetHeight() ); | |
695 | ||
696 | return wxSize(16,16); | |
697 | } | |
698 | ||
f554a14b | 699 | // --------------------------------------------------------- |
baa9ebc4 | 700 | // wxDataViewToggleRenderer |
f554a14b | 701 | // --------------------------------------------------------- |
4ed7af08 | 702 | |
baa9ebc4 | 703 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer, wxDataViewCustomRenderer) |
4ed7af08 | 704 | |
baa9ebc4 | 705 | wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype, |
9861f022 RR |
706 | wxDataViewCellMode mode, int align ) : |
707 | wxDataViewCustomRenderer( varianttype, mode, align ) | |
4ed7af08 | 708 | { |
90675b95 | 709 | m_toggle = false; |
4ed7af08 RR |
710 | } |
711 | ||
baa9ebc4 | 712 | bool wxDataViewToggleRenderer::SetValue( const wxVariant &value ) |
4ed7af08 | 713 | { |
90675b95 | 714 | m_toggle = value.GetBool(); |
f554a14b | 715 | |
a8461d31 | 716 | return true; |
4ed7af08 RR |
717 | } |
718 | ||
9861f022 | 719 | bool wxDataViewToggleRenderer::GetValue( wxVariant &WXUNUSED(value) ) const |
4ed7af08 RR |
720 | { |
721 | return false; | |
722 | } | |
f554a14b | 723 | |
baa9ebc4 | 724 | bool wxDataViewToggleRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) ) |
4ed7af08 | 725 | { |
90675b95 | 726 | // User wxRenderer here |
f554a14b | 727 | |
90675b95 RR |
728 | wxRect rect; |
729 | rect.x = cell.x + cell.width/2 - 10; | |
730 | rect.width = 20; | |
731 | rect.y = cell.y + cell.height/2 - 10; | |
732 | rect.height = 20; | |
120b9b05 | 733 | |
862d8041 | 734 | int flags = 0; |
90675b95 | 735 | if (m_toggle) |
862d8041 RR |
736 | flags |= wxCONTROL_CHECKED; |
737 | if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE) | |
738 | flags |= wxCONTROL_DISABLED; | |
739 | ||
90b903c2 | 740 | wxRendererNative::Get().DrawCheckBox( |
862d8041 RR |
741 | GetOwner()->GetOwner(), |
742 | *dc, | |
743 | rect, | |
744 | flags ); | |
f554a14b | 745 | |
90675b95 | 746 | return true; |
4ed7af08 RR |
747 | } |
748 | ||
c741d33f | 749 | bool wxDataViewToggleRenderer::Activate( wxRect WXUNUSED(cell), |
aba9bfd0 RR |
750 | wxDataViewModel *model, |
751 | const wxDataViewItem & item, unsigned int col) | |
0fdc2321 RR |
752 | { |
753 | bool value = !m_toggle; | |
754 | wxVariant variant = value; | |
aba9bfd0 RR |
755 | model->SetValue( variant, item, col); |
756 | model->ValueChanged( item, col ); | |
0fdc2321 RR |
757 | return true; |
758 | } | |
759 | ||
9861f022 | 760 | wxSize wxDataViewToggleRenderer::GetSize() const |
4ed7af08 | 761 | { |
3d9d7cc4 | 762 | return wxSize(20,20); |
4ed7af08 RR |
763 | } |
764 | ||
f554a14b | 765 | // --------------------------------------------------------- |
baa9ebc4 | 766 | // wxDataViewProgressRenderer |
f554a14b | 767 | // --------------------------------------------------------- |
4ed7af08 | 768 | |
baa9ebc4 | 769 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer) |
4ed7af08 | 770 | |
baa9ebc4 | 771 | wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label, |
9861f022 RR |
772 | const wxString &varianttype, wxDataViewCellMode mode, int align ) : |
773 | wxDataViewCustomRenderer( varianttype, mode, align ) | |
4ed7af08 RR |
774 | { |
775 | m_label = label; | |
776 | m_value = 0; | |
777 | } | |
778 | ||
baa9ebc4 | 779 | wxDataViewProgressRenderer::~wxDataViewProgressRenderer() |
4ed7af08 RR |
780 | { |
781 | } | |
782 | ||
baa9ebc4 | 783 | bool wxDataViewProgressRenderer::SetValue( const wxVariant &value ) |
4ed7af08 RR |
784 | { |
785 | m_value = (long) value; | |
f554a14b | 786 | |
4ed7af08 RR |
787 | if (m_value < 0) m_value = 0; |
788 | if (m_value > 100) m_value = 100; | |
f554a14b | 789 | |
4ed7af08 RR |
790 | return true; |
791 | } | |
f554a14b | 792 | |
9861f022 RR |
793 | bool wxDataViewProgressRenderer::GetValue( wxVariant &value ) const |
794 | { | |
795 | value = (long) m_value; | |
796 | return true; | |
797 | } | |
798 | ||
baa9ebc4 | 799 | bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) ) |
4ed7af08 RR |
800 | { |
801 | double pct = (double)m_value / 100.0; | |
802 | wxRect bar = cell; | |
803 | bar.width = (int)(cell.width * pct); | |
804 | dc->SetPen( *wxTRANSPARENT_PEN ); | |
805 | dc->SetBrush( *wxBLUE_BRUSH ); | |
806 | dc->DrawRectangle( bar ); | |
807 | ||
808 | dc->SetBrush( *wxTRANSPARENT_BRUSH ); | |
809 | dc->SetPen( *wxBLACK_PEN ); | |
810 | dc->DrawRectangle( cell ); | |
f554a14b | 811 | |
4ed7af08 RR |
812 | return true; |
813 | } | |
814 | ||
9861f022 | 815 | wxSize wxDataViewProgressRenderer::GetSize() const |
4ed7af08 RR |
816 | { |
817 | return wxSize(40,12); | |
818 | } | |
f554a14b WS |
819 | |
820 | // --------------------------------------------------------- | |
baa9ebc4 | 821 | // wxDataViewDateRenderer |
f554a14b | 822 | // --------------------------------------------------------- |
4ed7af08 | 823 | |
21ead767 VZ |
824 | #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN) |
825 | ||
826 | #if wxUSE_DATE_RENDERER_POPUP | |
8d0ca292 | 827 | |
baa9ebc4 | 828 | class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow |
4ed7af08 | 829 | { |
f554a14b | 830 | public: |
baa9ebc4 | 831 | wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value, |
aba9bfd0 RR |
832 | wxDataViewModel *model, const wxDataViewItem & item, unsigned int col) : |
833 | wxPopupTransientWindow( parent, wxBORDER_SIMPLE ), | |
834 | m_item( item ) | |
4ed7af08 RR |
835 | { |
836 | m_model = model; | |
837 | m_col = col; | |
f554a14b | 838 | m_cal = new wxCalendarCtrl( this, wxID_ANY, *value ); |
4ed7af08 RR |
839 | wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL ); |
840 | sizer->Add( m_cal, 1, wxGROW ); | |
841 | SetSizer( sizer ); | |
842 | sizer->Fit( this ); | |
843 | } | |
f554a14b | 844 | |
4ed7af08 | 845 | void OnCalendar( wxCalendarEvent &event ); |
f554a14b | 846 | |
4ed7af08 | 847 | wxCalendarCtrl *m_cal; |
aba9bfd0 | 848 | wxDataViewModel *m_model; |
0a71f9e9 | 849 | unsigned int m_col; |
aba9bfd0 | 850 | const wxDataViewItem & m_item; |
f554a14b | 851 | |
a8461d31 PC |
852 | protected: |
853 | virtual void OnDismiss() | |
854 | { | |
855 | } | |
856 | ||
4ed7af08 RR |
857 | private: |
858 | DECLARE_EVENT_TABLE() | |
859 | }; | |
860 | ||
baa9ebc4 RR |
861 | BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow) |
862 | EVT_CALENDAR( wxID_ANY, wxDataViewDateRendererPopupTransient::OnCalendar ) | |
4ed7af08 RR |
863 | END_EVENT_TABLE() |
864 | ||
baa9ebc4 | 865 | void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event ) |
4ed7af08 RR |
866 | { |
867 | wxDateTime date = event.GetDate(); | |
868 | wxVariant value = date; | |
aba9bfd0 RR |
869 | m_model->SetValue( value, m_item, m_col ); |
870 | m_model->ValueChanged( m_item, m_col ); | |
4ed7af08 RR |
871 | DismissAndNotify(); |
872 | } | |
873 | ||
21ead767 | 874 | #endif // wxUSE_DATE_RENDERER_POPUP |
8d0ca292 | 875 | |
baa9ebc4 | 876 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer) |
4ed7af08 | 877 | |
baa9ebc4 | 878 | wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype, |
9861f022 RR |
879 | wxDataViewCellMode mode, int align ) : |
880 | wxDataViewCustomRenderer( varianttype, mode, align ) | |
4ed7af08 RR |
881 | { |
882 | } | |
f554a14b | 883 | |
baa9ebc4 | 884 | bool wxDataViewDateRenderer::SetValue( const wxVariant &value ) |
4ed7af08 RR |
885 | { |
886 | m_date = value.GetDateTime(); | |
f554a14b | 887 | |
4ed7af08 RR |
888 | return true; |
889 | } | |
890 | ||
9861f022 RR |
891 | bool wxDataViewDateRenderer::GetValue( wxVariant &value ) const |
892 | { | |
893 | value = m_date; | |
894 | return true; | |
895 | } | |
896 | ||
baa9ebc4 | 897 | bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) ) |
4ed7af08 RR |
898 | { |
899 | dc->SetFont( GetOwner()->GetOwner()->GetFont() ); | |
900 | wxString tmp = m_date.FormatDate(); | |
901 | dc->DrawText( tmp, cell.x, cell.y ); | |
902 | ||
903 | return true; | |
904 | } | |
905 | ||
9861f022 | 906 | wxSize wxDataViewDateRenderer::GetSize() const |
4ed7af08 | 907 | { |
9861f022 | 908 | const wxDataViewCtrl* view = GetView(); |
4ed7af08 RR |
909 | wxString tmp = m_date.FormatDate(); |
910 | wxCoord x,y,d; | |
911 | view->GetTextExtent( tmp, &x, &y, &d ); | |
912 | return wxSize(x,y+d); | |
913 | } | |
914 | ||
aba9bfd0 RR |
915 | bool wxDataViewDateRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewModel *model, |
916 | const wxDataViewItem & item, unsigned int col ) | |
4ed7af08 RR |
917 | { |
918 | wxVariant variant; | |
aba9bfd0 | 919 | model->GetValue( variant, item, col ); |
4ed7af08 RR |
920 | wxDateTime value = variant.GetDateTime(); |
921 | ||
21ead767 | 922 | #if wxUSE_DATE_RENDERER_POPUP |
baa9ebc4 | 923 | wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient( |
aba9bfd0 | 924 | GetOwner()->GetOwner()->GetParent(), &value, model, item, col); |
4ed7af08 RR |
925 | wxPoint pos = wxGetMousePosition(); |
926 | popup->Move( pos ); | |
927 | popup->Layout(); | |
928 | popup->Popup( popup->m_cal ); | |
21ead767 | 929 | #else // !wxUSE_DATE_RENDERER_POPUP |
8d0ca292 | 930 | wxMessageBox(value.Format()); |
21ead767 | 931 | #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP |
4ed7af08 RR |
932 | return true; |
933 | } | |
934 | ||
f554a14b | 935 | // --------------------------------------------------------- |
4ed7af08 | 936 | // wxDataViewColumn |
f554a14b | 937 | // --------------------------------------------------------- |
4ed7af08 RR |
938 | |
939 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn, wxDataViewColumnBase) | |
940 | ||
c741d33f | 941 | wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell, |
87f0efe2 RR |
942 | unsigned int model_column, |
943 | int width, wxAlignment align, int flags ) : | |
944 | wxDataViewColumnBase( title, cell, model_column, width, align, flags ) | |
4ed7af08 | 945 | { |
9861f022 RR |
946 | SetAlignment(align); |
947 | SetTitle(title); | |
948 | SetFlags(flags); | |
949 | ||
950 | Init(width < 0 ? wxDVC_DEFAULT_WIDTH : width); | |
4ed7af08 RR |
951 | } |
952 | ||
c741d33f | 953 | wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell, |
87f0efe2 RR |
954 | unsigned int model_column, |
955 | int width, wxAlignment align, int flags ) : | |
956 | wxDataViewColumnBase( bitmap, cell, model_column, width, align, flags ) | |
07a84e7b | 957 | { |
9861f022 RR |
958 | SetAlignment(align); |
959 | SetFlags(flags); | |
960 | ||
961 | Init(width < 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH : width); | |
07a84e7b RR |
962 | } |
963 | ||
9861f022 RR |
964 | wxDataViewColumn::~wxDataViewColumn() |
965 | { | |
966 | } | |
967 | ||
968 | void wxDataViewColumn::Init( int width ) | |
47cef10f | 969 | { |
87f0efe2 | 970 | m_width = width; |
9861f022 | 971 | m_minWidth = wxDVC_DEFAULT_MINWIDTH; |
c3112d56 | 972 | m_ascending = true; |
47cef10f RR |
973 | } |
974 | ||
9861f022 | 975 | void wxDataViewColumn::SetResizeable( bool resizeable ) |
31fb32e1 | 976 | { |
9861f022 RR |
977 | if (resizeable) |
978 | m_flags |= wxDATAVIEW_COL_RESIZABLE; | |
979 | else | |
980 | m_flags &= ~wxDATAVIEW_COL_RESIZABLE; | |
981 | } | |
982 | ||
983 | void wxDataViewColumn::SetHidden( bool hidden ) | |
984 | { | |
985 | if (hidden) | |
986 | m_flags |= wxDATAVIEW_COL_HIDDEN; | |
987 | else | |
988 | m_flags &= ~wxDATAVIEW_COL_HIDDEN; | |
989 | ||
990 | // tell our owner to e.g. update its scrollbars: | |
991 | if (GetOwner()) | |
992 | GetOwner()->OnColumnChange(); | |
993 | } | |
994 | ||
995 | void wxDataViewColumn::SetSortable( bool sortable ) | |
996 | { | |
997 | if (sortable) | |
998 | m_flags |= wxDATAVIEW_COL_SORTABLE; | |
999 | else | |
1000 | m_flags &= ~wxDATAVIEW_COL_SORTABLE; | |
442c56e6 | 1001 | |
c3112d56 RR |
1002 | // Update header button |
1003 | if (GetOwner()) | |
1004 | GetOwner()->OnColumnChange(); | |
31fb32e1 RR |
1005 | } |
1006 | ||
c3112d56 | 1007 | void wxDataViewColumn::SetSortOrder( bool ascending ) |
47cef10f | 1008 | { |
c3112d56 | 1009 | m_ascending = ascending; |
442c56e6 | 1010 | |
c3112d56 RR |
1011 | // Update header button |
1012 | if (GetOwner()) | |
1013 | GetOwner()->OnColumnChange(); | |
47cef10f RR |
1014 | } |
1015 | ||
87f0efe2 | 1016 | bool wxDataViewColumn::IsSortOrderAscending() const |
31fb32e1 | 1017 | { |
c3112d56 | 1018 | return m_ascending; |
31fb32e1 RR |
1019 | } |
1020 | ||
9861f022 | 1021 | void wxDataViewColumn::SetInternalWidth( int width ) |
4ed7af08 | 1022 | { |
9861f022 | 1023 | m_width = width; |
f554a14b | 1024 | |
9861f022 RR |
1025 | // the scrollbars of the wxDataViewCtrl needs to be recalculated! |
1026 | if (m_owner && m_owner->m_clientArea) | |
1027 | m_owner->m_clientArea->RecalculateDisplay(); | |
4ed7af08 RR |
1028 | } |
1029 | ||
9861f022 | 1030 | void wxDataViewColumn::SetWidth( int width ) |
47cef10f | 1031 | { |
9861f022 | 1032 | m_owner->m_headerArea->UpdateDisplay(); |
47cef10f | 1033 | |
9861f022 | 1034 | SetInternalWidth(width); |
47cef10f RR |
1035 | } |
1036 | ||
533544f2 | 1037 | |
4ed7af08 | 1038 | //----------------------------------------------------------------------------- |
87f0efe2 | 1039 | // wxDataViewHeaderWindowBase |
4ed7af08 RR |
1040 | //----------------------------------------------------------------------------- |
1041 | ||
87f0efe2 RR |
1042 | void wxDataViewHeaderWindowBase::SendEvent(wxEventType type, unsigned int n) |
1043 | { | |
1044 | wxWindow *parent = GetParent(); | |
1045 | wxDataViewEvent le(type, parent->GetId()); | |
1046 | ||
1047 | le.SetEventObject(parent); | |
1048 | le.SetColumn(n); | |
1049 | le.SetDataViewColumn(GetColumn(n)); | |
1050 | le.SetModel(GetOwner()->GetModel()); | |
1051 | ||
1052 | // for events created by wxDataViewHeaderWindow the | |
1053 | // row / value fields are not valid | |
1054 | ||
1055 | parent->GetEventHandler()->ProcessEvent(le); | |
1056 | } | |
1057 | ||
1058 | #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW | |
1059 | ||
87f0efe2 | 1060 | // implemented in msw/listctrl.cpp: |
a68962fc | 1061 | int WXDLLIMPEXP_CORE wxMSWGetColumnClicked(NMHDR *nmhdr, POINT *ptClick); |
87f0efe2 | 1062 | |
5d3f234b | 1063 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW, wxWindow) |
87f0efe2 RR |
1064 | |
1065 | bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl *parent, wxWindowID id, | |
c741d33f | 1066 | const wxPoint &pos, const wxSize &size, |
87f0efe2 RR |
1067 | const wxString &name ) |
1068 | { | |
1069 | m_owner = parent; | |
1070 | ||
1071 | if ( !CreateControl(parent, id, pos, size, 0, wxDefaultValidator, name) ) | |
1072 | return false; | |
1073 | ||
1074 | int x = pos.x == wxDefaultCoord ? 0 : pos.x, | |
1075 | y = pos.y == wxDefaultCoord ? 0 : pos.y, | |
1076 | w = size.x == wxDefaultCoord ? 1 : size.x, | |
1077 | h = size.y == wxDefaultCoord ? 22 : size.y; | |
1078 | ||
1079 | // create the native WC_HEADER window: | |
1080 | WXHWND hwndParent = (HWND)parent->GetHandle(); | |
1081 | WXDWORD msStyle = WS_CHILD | HDS_BUTTONS | HDS_HORZ | HDS_HOTTRACK | HDS_FULLDRAG; | |
c741d33f VZ |
1082 | m_hWnd = CreateWindowEx(0, |
1083 | WC_HEADER, | |
1084 | (LPCTSTR) NULL, | |
87f0efe2 RR |
1085 | msStyle, |
1086 | x, y, w, h, | |
c741d33f VZ |
1087 | (HWND)hwndParent, |
1088 | (HMENU)-1, | |
1089 | wxGetInstance(), | |
87f0efe2 | 1090 | (LPVOID) NULL); |
c741d33f | 1091 | if (m_hWnd == NULL) |
87f0efe2 RR |
1092 | { |
1093 | wxLogLastError(_T("CreateWindowEx")); | |
1094 | return false; | |
1095 | } | |
1096 | ||
5d3f234b | 1097 | // we need to subclass the m_hWnd to force wxWindow::HandleNotify |
87f0efe2 | 1098 | // to call wxDataViewHeaderWindow::MSWOnNotify |
5d3f234b | 1099 | SubclassWin(m_hWnd); |
87f0efe2 | 1100 | |
c741d33f | 1101 | // the following is required to get the default win's font for |
9861f022 RR |
1102 | // header windows and must be done befor sending the HDM_LAYOUT msg |
1103 | SetFont(GetFont()); | |
87f0efe2 | 1104 | |
9861f022 RR |
1105 | RECT rcParent; |
1106 | HDLAYOUT hdl; | |
1107 | WINDOWPOS wp; | |
87f0efe2 | 1108 | |
c741d33f VZ |
1109 | // Retrieve the bounding rectangle of the parent window's |
1110 | // client area, and then request size and position values | |
1111 | // from the header control. | |
1112 | ::GetClientRect((HWND)hwndParent, &rcParent); | |
1113 | ||
1114 | hdl.prc = &rcParent; | |
1115 | hdl.pwpos = ℘ | |
1116 | if (!SendMessage((HWND)m_hWnd, HDM_LAYOUT, 0, (LPARAM) &hdl)) | |
87f0efe2 RR |
1117 | { |
1118 | wxLogLastError(_T("SendMessage")); | |
1119 | return false; | |
1120 | } | |
c741d33f VZ |
1121 | |
1122 | // Set the size, position, and visibility of the header control. | |
1123 | SetWindowPos((HWND)m_hWnd, | |
1124 | wp.hwndInsertAfter, | |
1125 | wp.x, wp.y, | |
1126 | wp.cx, wp.cy, | |
87f0efe2 RR |
1127 | wp.flags | SWP_SHOWWINDOW); |
1128 | ||
1129 | // set our size hints: wxDataViewCtrl will put this wxWindow inside | |
1130 | // a wxBoxSizer and in order to avoid super-big header windows, | |
1131 | // we need to set our height as fixed | |
1132 | SetMinSize(wxSize(-1, wp.cy)); | |
1133 | SetMaxSize(wxSize(-1, wp.cy)); | |
1134 | ||
87f0efe2 RR |
1135 | return true; |
1136 | } | |
1137 | ||
1138 | wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow() | |
1139 | { | |
5d3f234b | 1140 | UnsubclassWin(); |
87f0efe2 RR |
1141 | } |
1142 | ||
1143 | void wxDataViewHeaderWindowMSW::UpdateDisplay() | |
1144 | { | |
1145 | // remove old columns | |
914e6945 | 1146 | for (int j=0, max=Header_GetItemCount((HWND)m_hWnd); j < max; j++) |
87f0efe2 | 1147 | Header_DeleteItem((HWND)m_hWnd, 0); |
c741d33f | 1148 | |
87f0efe2 | 1149 | // add the updated array of columns to the header control |
9861f022 RR |
1150 | unsigned int cols = GetOwner()->GetColumnCount(); |
1151 | unsigned int added = 0; | |
87f0efe2 RR |
1152 | for (unsigned int i = 0; i < cols; i++) |
1153 | { | |
1154 | wxDataViewColumn *col = GetColumn( i ); | |
1155 | if (col->IsHidden()) | |
1156 | continue; // don't add it! | |
1157 | ||
1158 | HDITEM hdi; | |
1159 | hdi.mask = HDI_TEXT | HDI_FORMAT | HDI_WIDTH; | |
fab3f50e | 1160 | hdi.pszText = (wxChar *) col->GetTitle().wx_str(); |
87f0efe2 RR |
1161 | hdi.cxy = col->GetWidth(); |
1162 | hdi.cchTextMax = sizeof(hdi.pszText)/sizeof(hdi.pszText[0]); | |
1163 | hdi.fmt = HDF_LEFT | HDF_STRING; | |
9861f022 RR |
1164 | |
1165 | // lParam is reserved for application's use: | |
1166 | // we store there the column index to use it later in MSWOnNotify | |
1167 | // (since columns may have been hidden) | |
1168 | hdi.lParam = (LPARAM)i; | |
1169 | ||
1170 | // the native wxMSW implementation of the header window | |
c741d33f | 1171 | // draws the column separator COLUMN_WIDTH_OFFSET pixels |
9861f022 RR |
1172 | // on the right: to correct this effect we make the column |
1173 | // exactly COLUMN_WIDTH_OFFSET wider (for the first column): | |
1174 | if (i == 0) | |
1175 | hdi.cxy += COLUMN_WIDTH_OFFSET; | |
1176 | ||
1177 | switch (col->GetAlignment()) | |
1178 | { | |
1179 | case wxALIGN_LEFT: | |
1180 | hdi.fmt |= HDF_LEFT; | |
1181 | break; | |
1182 | case wxALIGN_CENTER: | |
1183 | case wxALIGN_CENTER_HORIZONTAL: | |
1184 | hdi.fmt |= HDF_CENTER; | |
1185 | break; | |
1186 | case wxALIGN_RIGHT: | |
1187 | hdi.fmt |= HDF_RIGHT; | |
1188 | break; | |
5d3f234b RR |
1189 | |
1190 | default: | |
1191 | // such alignment is not allowed for the column header! | |
1192 | wxFAIL; | |
9861f022 | 1193 | } |
c741d33f VZ |
1194 | |
1195 | SendMessage((HWND)m_hWnd, HDM_INSERTITEM, | |
1196 | (WPARAM)added, (LPARAM)&hdi); | |
9861f022 | 1197 | added++; |
87f0efe2 RR |
1198 | } |
1199 | } | |
1200 | ||
9861f022 RR |
1201 | unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER *nmHDR) |
1202 | { | |
1203 | unsigned int idx; | |
1204 | ||
1205 | // NOTE: we don't just return nmHDR->iItem because when there are | |
1206 | // hidden columns, nmHDR->iItem may be different from | |
1207 | // nmHDR->pitem->lParam | |
1208 | ||
1209 | if (nmHDR->pitem && nmHDR->pitem->mask & HDI_LPARAM) | |
1210 | { | |
1211 | idx = (unsigned int)nmHDR->pitem->lParam; | |
1212 | return idx; | |
1213 | } | |
1214 | ||
1215 | HDITEM item; | |
1216 | item.mask = HDI_LPARAM; | |
1217 | Header_GetItem((HWND)m_hWnd, nmHDR->iItem, &item); | |
1218 | ||
1219 | return (unsigned int)item.lParam; | |
1220 | } | |
1221 | ||
87f0efe2 RR |
1222 | bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) |
1223 | { | |
1224 | NMHDR *nmhdr = (NMHDR *)lParam; | |
1225 | ||
1226 | // is it a message from the header? | |
1227 | if ( nmhdr->hwndFrom != (HWND)m_hWnd ) | |
1228 | return wxWindow::MSWOnNotify(idCtrl, lParam, result); | |
1229 | ||
1230 | NMHEADER *nmHDR = (NMHEADER *)nmhdr; | |
1231 | switch ( nmhdr->code ) | |
1232 | { | |
1233 | case HDN_BEGINTRACK: | |
1234 | // user has started to resize a column: | |
1235 | // do we need to veto it? | |
1236 | if (!GetColumn(nmHDR->iItem)->IsResizeable()) | |
1237 | { | |
1238 | // veto it! | |
1239 | *result = TRUE; | |
1240 | } | |
1241 | break; | |
1242 | ||
1243 | case HDN_BEGINDRAG: | |
1244 | // user has started to reorder a column | |
1245 | break; | |
1246 | ||
9861f022 | 1247 | case HDN_ITEMCHANGING: |
c741d33f | 1248 | if (nmHDR->pitem != NULL && |
9861f022 RR |
1249 | (nmHDR->pitem->mask & HDI_WIDTH) != 0) |
1250 | { | |
1251 | int minWidth = GetColumnFromHeader(nmHDR)->GetMinWidth(); | |
1252 | if (nmHDR->pitem->cxy < minWidth) | |
1253 | { | |
c741d33f | 1254 | // do not allow the user to resize this column under |
9861f022 RR |
1255 | // its minimal width: |
1256 | *result = TRUE; | |
1257 | } | |
1258 | } | |
1259 | break; | |
1260 | ||
87f0efe2 RR |
1261 | case HDN_ITEMCHANGED: // user is resizing a column |
1262 | case HDN_ENDTRACK: // user has finished resizing a column | |
1263 | case HDN_ENDDRAG: // user has finished reordering a column | |
1264 | ||
1265 | // update the width of the modified column: | |
c741d33f | 1266 | if (nmHDR->pitem != NULL && |
9861f022 RR |
1267 | (nmHDR->pitem->mask & HDI_WIDTH) != 0) |
1268 | { | |
1269 | unsigned int idx = GetColumnIdxFromHeader(nmHDR); | |
1270 | unsigned int w = nmHDR->pitem->cxy; | |
1271 | wxDataViewColumn *col = GetColumn(idx); | |
1272 | ||
1273 | // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET | |
1274 | if (idx == 0 && w > COLUMN_WIDTH_OFFSET) | |
1275 | w -= COLUMN_WIDTH_OFFSET; | |
1276 | ||
1277 | if (w >= (unsigned)col->GetMinWidth()) | |
1278 | col->SetInternalWidth(w); | |
1279 | } | |
87f0efe2 RR |
1280 | break; |
1281 | ||
1282 | case HDN_ITEMCLICK: | |
1283 | { | |
9861f022 | 1284 | unsigned int idx = GetColumnIdxFromHeader(nmHDR); |
c741d33f | 1285 | wxEventType evt = nmHDR->iButton == 0 ? |
87f0efe2 RR |
1286 | wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK : |
1287 | wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK; | |
9861f022 | 1288 | SendEvent(evt, idx); |
87f0efe2 RR |
1289 | } |
1290 | break; | |
1291 | ||
1292 | case NM_RCLICK: | |
1293 | { | |
1294 | // NOTE: for some reason (i.e. for a bug in Windows) | |
1295 | // the HDN_ITEMCLICK notification is not sent on | |
1296 | // right clicks, so we need to handle NM_RCLICK | |
1297 | ||
1298 | POINT ptClick; | |
9861f022 | 1299 | int column = wxMSWGetColumnClicked(nmhdr, &ptClick); |
87f0efe2 | 1300 | if (column != wxNOT_FOUND) |
9861f022 RR |
1301 | { |
1302 | HDITEM item; | |
1303 | item.mask = HDI_LPARAM; | |
1304 | Header_GetItem((HWND)m_hWnd, column, &item); | |
1305 | ||
1306 | // 'idx' may be different from 'column' if there are | |
1307 | // hidden columns... | |
1308 | unsigned int idx = (unsigned int)item.lParam; | |
87f0efe2 | 1309 | SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, |
9861f022 RR |
1310 | idx); |
1311 | } | |
87f0efe2 RR |
1312 | } |
1313 | break; | |
1314 | ||
1315 | case HDN_GETDISPINFOW: | |
1316 | // see wxListCtrl::MSWOnNotify for more info! | |
1317 | break; | |
1318 | ||
1319 | case HDN_ITEMDBLCLICK: | |
1320 | { | |
9861f022 | 1321 | unsigned int idx = GetColumnIdxFromHeader(nmHDR); |
87f0efe2 RR |
1322 | int w = GetOwner()->GetBestColumnWidth(idx); |
1323 | ||
1324 | // update the native control: | |
1325 | HDITEM hd; | |
1326 | ZeroMemory(&hd, sizeof(hd)); | |
1327 | hd.mask = HDI_WIDTH; | |
1328 | hd.cxy = w; | |
c741d33f | 1329 | Header_SetItem(GetHwnd(), |
9861f022 RR |
1330 | nmHDR->iItem, // NOTE: we don't want 'idx' here! |
1331 | &hd); | |
87f0efe2 RR |
1332 | |
1333 | // update the wxDataViewColumn class: | |
9861f022 | 1334 | GetColumn(idx)->SetInternalWidth(w); |
87f0efe2 RR |
1335 | } |
1336 | break; | |
1337 | ||
1338 | default: | |
1339 | return wxWindow::MSWOnNotify(idCtrl, lParam, result); | |
1340 | } | |
1341 | ||
1342 | return true; | |
1343 | } | |
1344 | ||
c741d33f | 1345 | void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx), int WXUNUSED(dy), |
87f0efe2 RR |
1346 | const wxRect *WXUNUSED(rect)) |
1347 | { | |
1348 | wxSize ourSz = GetClientSize(); | |
1349 | wxSize ownerSz = m_owner->GetClientSize(); | |
1350 | ||
1351 | // where should the (logical) origin of this window be placed? | |
1352 | int x1 = 0, y1 = 0; | |
1353 | m_owner->CalcUnscrolledPosition(0, 0, &x1, &y1); | |
4ed7af08 | 1354 | |
c741d33f VZ |
1355 | // put this window on top of our parent and |
1356 | SetWindowPos((HWND)m_hWnd, HWND_TOP, -x1, 0, | |
1357 | ownerSz.GetWidth() + x1, ourSz.GetHeight(), | |
87f0efe2 RR |
1358 | SWP_SHOWWINDOW); |
1359 | } | |
1360 | ||
c741d33f VZ |
1361 | void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x), int WXUNUSED(y), |
1362 | int WXUNUSED(w), int WXUNUSED(h), | |
9861f022 RR |
1363 | int WXUNUSED(f)) |
1364 | { | |
1365 | // the wxDataViewCtrl's internal wxBoxSizer will call this function when | |
1366 | // the wxDataViewCtrl window gets resized: the following dummy call | |
1367 | // to ScrollWindow() is required in order to get this header window | |
1368 | // correctly repainted when it's (horizontally) scrolled: | |
1369 | ||
1370 | ScrollWindow(0, 0); | |
1371 | } | |
1372 | ||
87f0efe2 RR |
1373 | #else // !defined(__WXMSW__) |
1374 | ||
1375 | IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow, wxWindow) | |
1376 | BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow, wxWindow) | |
1377 | EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint) | |
1378 | EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse) | |
1379 | EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus) | |
4ed7af08 RR |
1380 | END_EVENT_TABLE() |
1381 | ||
87f0efe2 | 1382 | bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl *parent, wxWindowID id, |
c741d33f VZ |
1383 | const wxPoint &pos, const wxSize &size, |
1384 | const wxString &name ) | |
4ed7af08 | 1385 | { |
87f0efe2 | 1386 | m_owner = parent; |
4b3feaa7 | 1387 | |
87f0efe2 RR |
1388 | if (!wxDataViewHeaderWindowBase::Create(parent, id, pos, size, name)) |
1389 | return false; | |
f554a14b | 1390 | |
4ed7af08 | 1391 | wxVisualAttributes attr = wxPanel::GetClassDefaultAttributes(); |
2e992e06 | 1392 | SetBackgroundStyle( wxBG_STYLE_CUSTOM ); |
4ed7af08 RR |
1393 | SetOwnForegroundColour( attr.colFg ); |
1394 | SetOwnBackgroundColour( attr.colBg ); | |
1395 | if (!m_hasFont) | |
1396 | SetOwnFont( attr.font ); | |
4ed7af08 | 1397 | |
87f0efe2 RR |
1398 | // set our size hints: wxDataViewCtrl will put this wxWindow inside |
1399 | // a wxBoxSizer and in order to avoid super-big header windows, | |
1400 | // we need to set our height as fixed | |
1401 | SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT)); | |
1402 | SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT)); | |
1403 | ||
1404 | return true; | |
4ed7af08 RR |
1405 | } |
1406 | ||
87f0efe2 | 1407 | void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) |
4ed7af08 | 1408 | { |
4b3feaa7 RR |
1409 | int w, h; |
1410 | GetClientSize( &w, &h ); | |
1411 | ||
2e992e06 VZ |
1412 | wxAutoBufferedPaintDC dc( this ); |
1413 | ||
1414 | dc.SetBackground(GetBackgroundColour()); | |
1415 | dc.Clear(); | |
f554a14b | 1416 | |
4ed7af08 RR |
1417 | int xpix; |
1418 | m_owner->GetScrollPixelsPerUnit( &xpix, NULL ); | |
1419 | ||
1420 | int x; | |
1421 | m_owner->GetViewStart( &x, NULL ); | |
1422 | ||
1423 | // account for the horz scrollbar offset | |
1424 | dc.SetDeviceOrigin( -x * xpix, 0 ); | |
f554a14b | 1425 | |
4ed7af08 | 1426 | dc.SetFont( GetFont() ); |
f554a14b | 1427 | |
9861f022 | 1428 | unsigned int cols = GetOwner()->GetColumnCount(); |
0a71f9e9 | 1429 | unsigned int i; |
4b3feaa7 RR |
1430 | int xpos = 0; |
1431 | for (i = 0; i < cols; i++) | |
1432 | { | |
87f0efe2 RR |
1433 | wxDataViewColumn *col = GetColumn( i ); |
1434 | if (col->IsHidden()) | |
9861f022 | 1435 | continue; // skip it! |
f554a14b | 1436 | |
87f0efe2 | 1437 | int cw = col->GetWidth(); |
4b3feaa7 | 1438 | int ch = h; |
4b3feaa7 | 1439 | |
c3112d56 RR |
1440 | wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE; |
1441 | if (col->IsSortable()) | |
1442 | { | |
1443 | if (col->IsSortOrderAscending()) | |
1444 | sortArrow = wxHDR_SORT_ICON_UP; | |
1445 | else | |
1446 | sortArrow = wxHDR_SORT_ICON_DOWN; | |
1447 | } | |
1448 | ||
4b3feaa7 RR |
1449 | wxRendererNative::Get().DrawHeaderButton |
1450 | ( | |
1451 | this, | |
1452 | dc, | |
72664514 | 1453 | wxRect(xpos, 0, cw, ch-1), |
4b3feaa7 | 1454 | m_parent->IsEnabled() ? 0 |
c3112d56 RR |
1455 | : (int)wxCONTROL_DISABLED, |
1456 | sortArrow | |
4b3feaa7 RR |
1457 | ); |
1458 | ||
9861f022 RR |
1459 | // align as required the column title: |
1460 | int x = xpos; | |
1461 | wxSize titleSz = dc.GetTextExtent(col->GetTitle()); | |
1462 | switch (col->GetAlignment()) | |
1463 | { | |
1464 | case wxALIGN_LEFT: | |
1465 | x += HEADER_HORIZ_BORDER; | |
1466 | break; | |
1467 | case wxALIGN_CENTER: | |
1468 | case wxALIGN_CENTER_HORIZONTAL: | |
1469 | x += (cw - titleSz.GetWidth() - 2 * HEADER_HORIZ_BORDER)/2; | |
1470 | break; | |
1471 | case wxALIGN_RIGHT: | |
1472 | x += cw - titleSz.GetWidth() - HEADER_HORIZ_BORDER; | |
1473 | break; | |
1474 | } | |
1475 | ||
1476 | // always center the title vertically: | |
1477 | int y = wxMax((ch - titleSz.GetHeight()) / 2, HEADER_VERT_BORDER); | |
1478 | ||
c741d33f | 1479 | dc.SetClippingRegion( xpos+HEADER_HORIZ_BORDER, |
9861f022 RR |
1480 | HEADER_VERT_BORDER, |
1481 | wxMax(cw - 2 * HEADER_HORIZ_BORDER, 1), // width | |
1482 | wxMax(ch - 2 * HEADER_VERT_BORDER, 1)); // height | |
1483 | dc.DrawText( col->GetTitle(), x, y ); | |
1484 | dc.DestroyClippingRegion(); | |
f554a14b | 1485 | |
87f0efe2 | 1486 | xpos += cw; |
4b3feaa7 | 1487 | } |
4ed7af08 RR |
1488 | } |
1489 | ||
87f0efe2 | 1490 | void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent &event ) |
4ed7af08 | 1491 | { |
87f0efe2 RR |
1492 | GetParent()->SetFocus(); |
1493 | event.Skip(); | |
4ed7af08 RR |
1494 | } |
1495 | ||
87f0efe2 | 1496 | void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent &event ) |
4ed7af08 | 1497 | { |
87f0efe2 RR |
1498 | // we want to work with logical coords |
1499 | int x; | |
1500 | m_owner->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL); | |
1501 | int y = event.GetY(); | |
1502 | ||
1503 | if (m_isDragging) | |
1504 | { | |
c741d33f | 1505 | // we don't draw the line beyond our window, |
87f0efe2 RR |
1506 | // but we allow dragging it there |
1507 | int w = 0; | |
1508 | GetClientSize( &w, NULL ); | |
1509 | m_owner->CalcUnscrolledPosition(w, 0, &w, NULL); | |
1510 | w -= 6; | |
1511 | ||
1512 | // erase the line if it was drawn | |
c741d33f | 1513 | if (m_currentX < w) |
87f0efe2 RR |
1514 | DrawCurrent(); |
1515 | ||
c741d33f | 1516 | if (event.ButtonUp()) |
87f0efe2 RR |
1517 | { |
1518 | m_isDragging = false; | |
c741d33f | 1519 | if (HasCapture()) |
87f0efe2 RR |
1520 | ReleaseMouse(); |
1521 | ||
1522 | m_dirty = true; | |
1523 | ||
9861f022 | 1524 | GetColumn(m_column)->SetWidth(m_currentX - m_minX); |
87f0efe2 RR |
1525 | |
1526 | Refresh(); | |
1527 | GetOwner()->Refresh(); | |
1528 | } | |
1529 | else | |
1530 | { | |
1531 | m_currentX = wxMax(m_minX + 7, x); | |
1532 | ||
1533 | // draw in the new location | |
1534 | if (m_currentX < w) DrawCurrent(); | |
1535 | } | |
1536 | ||
1537 | } | |
1538 | else // not dragging | |
1539 | { | |
1540 | m_minX = 0; | |
1541 | m_column = wxNOT_FOUND; | |
1542 | ||
1543 | bool hit_border = false; | |
1544 | ||
1545 | // end of the current column | |
1546 | int xpos = 0; | |
1547 | ||
1548 | // find the column where this event occured | |
9861f022 | 1549 | int countCol = m_owner->GetColumnCount(); |
c741d33f | 1550 | for (int column = 0; column < countCol; column++) |
87f0efe2 RR |
1551 | { |
1552 | wxDataViewColumn *p = GetColumn(column); | |
1553 | ||
c741d33f | 1554 | if (p->IsHidden()) |
87f0efe2 RR |
1555 | continue; // skip if not shown |
1556 | ||
1557 | xpos += p->GetWidth(); | |
1558 | m_column = column; | |
c741d33f | 1559 | if ((abs(x-xpos) < 3) && (y < 22)) |
87f0efe2 RR |
1560 | { |
1561 | hit_border = true; | |
1562 | break; | |
1563 | } | |
1564 | ||
c741d33f | 1565 | if (x < xpos) |
87f0efe2 RR |
1566 | { |
1567 | // inside the column | |
1568 | break; | |
1569 | } | |
1570 | ||
1571 | m_minX = xpos; | |
1572 | } | |
1573 | ||
1574 | if (m_column == wxNOT_FOUND) | |
1575 | return; | |
1576 | ||
1577 | bool resizeable = GetColumn(m_column)->IsResizeable(); | |
1578 | if (event.LeftDClick() && resizeable) | |
1579 | { | |
9861f022 | 1580 | GetColumn(m_column)->SetWidth(GetOwner()->GetBestColumnWidth(m_column)); |
87f0efe2 RR |
1581 | Refresh(); |
1582 | } | |
1583 | else if (event.LeftDown() || event.RightUp()) | |
1584 | { | |
c741d33f | 1585 | if (hit_border && event.LeftDown() && resizeable) |
87f0efe2 RR |
1586 | { |
1587 | m_isDragging = true; | |
1588 | CaptureMouse(); | |
1589 | m_currentX = x; | |
1590 | DrawCurrent(); | |
1591 | } | |
1592 | else // click on a column | |
1593 | { | |
c741d33f | 1594 | wxEventType evt = event.LeftDown() ? |
87f0efe2 RR |
1595 | wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK : |
1596 | wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK; | |
1597 | SendEvent(evt, m_column); | |
1598 | } | |
1599 | } | |
1600 | else if (event.Moving()) | |
1601 | { | |
c741d33f | 1602 | if (hit_border && resizeable) |
87f0efe2 RR |
1603 | m_currentCursor = m_resizeCursor; |
1604 | else | |
1605 | m_currentCursor = wxSTANDARD_CURSOR; | |
1606 | ||
1607 | SetCursor(*m_currentCursor); | |
1608 | } | |
1609 | } | |
1610 | } | |
1611 | ||
1612 | void wxGenericDataViewHeaderWindow::DrawCurrent() | |
1613 | { | |
1614 | int x1 = m_currentX; | |
1615 | int y1 = 0; | |
1616 | ClientToScreen (&x1, &y1); | |
1617 | ||
1618 | int x2 = m_currentX-1; | |
1619 | #ifdef __WXMSW__ | |
1620 | ++x2; // but why ???? | |
1621 | #endif | |
1622 | int y2 = 0; | |
1623 | m_owner->GetClientSize( NULL, &y2 ); | |
1624 | m_owner->ClientToScreen( &x2, &y2 ); | |
1625 | ||
1626 | wxScreenDC dc; | |
9861f022 RR |
1627 | dc.SetLogicalFunction(wxINVERT); |
1628 | dc.SetPen(m_penCurrent); | |
1629 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
87f0efe2 | 1630 | AdjustDC(dc); |
9861f022 | 1631 | dc.DrawLine(x1, y1, x2, y2); |
87f0efe2 RR |
1632 | } |
1633 | ||
1634 | void wxGenericDataViewHeaderWindow::AdjustDC(wxDC& dc) | |
1635 | { | |
1636 | int xpix, x; | |
1637 | ||
1638 | m_owner->GetScrollPixelsPerUnit( &xpix, NULL ); | |
1639 | m_owner->GetViewStart( &x, NULL ); | |
1640 | ||
1641 | // shift the DC origin to match the position of the main window horizontal | |
1642 | // scrollbar: this allows us to always use logical coords | |
1643 | dc.SetDeviceOrigin( -x * xpix, 0 ); | |
4ed7af08 RR |
1644 | } |
1645 | ||
87f0efe2 RR |
1646 | #endif // defined(__WXMSW__) |
1647 | ||
0fcce6b9 RR |
1648 | //----------------------------------------------------------------------------- |
1649 | // wxDataViewRenameTimer | |
1650 | //----------------------------------------------------------------------------- | |
1651 | ||
1652 | wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow *owner ) | |
1653 | { | |
1654 | m_owner = owner; | |
1655 | } | |
1656 | ||
1657 | void wxDataViewRenameTimer::Notify() | |
1658 | { | |
1659 | m_owner->OnRenameTimer(); | |
1660 | } | |
1661 | ||
4ed7af08 RR |
1662 | //----------------------------------------------------------------------------- |
1663 | // wxDataViewMainWindow | |
1664 | //----------------------------------------------------------------------------- | |
1665 | ||
704c3490 RR |
1666 | //The tree building helper, declared firstly |
1667 | void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, wxDataViewTreeNode * node); | |
1668 | ||
0a71f9e9 | 1669 | int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 ) |
cab07038 RR |
1670 | { |
1671 | if (row1 > row2) return 1; | |
1672 | if (row1 == row2) return 0; | |
1673 | return -1; | |
1674 | } | |
1675 | ||
1676 | ||
45778c96 | 1677 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow, wxWindow) |
4ed7af08 RR |
1678 | |
1679 | BEGIN_EVENT_TABLE(wxDataViewMainWindow,wxWindow) | |
1680 | EVT_PAINT (wxDataViewMainWindow::OnPaint) | |
1681 | EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse) | |
1682 | EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus) | |
cab07038 RR |
1683 | EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus) |
1684 | EVT_CHAR (wxDataViewMainWindow::OnChar) | |
4ed7af08 RR |
1685 | END_EVENT_TABLE() |
1686 | ||
1687 | wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID id, | |
1688 | const wxPoint &pos, const wxSize &size, const wxString &name ) : | |
72664514 | 1689 | wxWindow( parent, id, pos, size, wxWANTS_CHARS, name ), |
cab07038 | 1690 | m_selection( wxDataViewSelectionCmp ) |
120b9b05 | 1691 | |
4ed7af08 RR |
1692 | { |
1693 | SetOwner( parent ); | |
f554a14b | 1694 | |
0fcce6b9 RR |
1695 | m_lastOnSame = false; |
1696 | m_renameTimer = new wxDataViewRenameTimer( this ); | |
120b9b05 | 1697 | |
0fcce6b9 RR |
1698 | // TODO: user better initial values/nothing selected |
1699 | m_currentCol = NULL; | |
1700 | m_currentRow = 0; | |
1701 | ||
1702 | // TODO: we need to calculate this smartly | |
c741d33f | 1703 | m_lineHeight = |
87f0efe2 RR |
1704 | #ifdef __WXMSW__ |
1705 | 17; | |
1706 | #else | |
1707 | 20; | |
1708 | #endif | |
9861f022 | 1709 | wxASSERT(m_lineHeight > 2*PADDING_TOPBOTTOM); |
e21f75bd RR |
1710 | |
1711 | m_dragCount = 0; | |
1712 | m_dragStart = wxPoint(0,0); | |
0a71f9e9 RR |
1713 | m_lineLastClicked = (unsigned int) -1; |
1714 | m_lineBeforeLastClicked = (unsigned int) -1; | |
1715 | m_lineSelectSingleOnUp = (unsigned int) -1; | |
120b9b05 | 1716 | |
cab07038 | 1717 | m_hasFocus = false; |
f554a14b | 1718 | |
2e992e06 | 1719 | SetBackgroundStyle( wxBG_STYLE_CUSTOM ); |
72664514 RR |
1720 | SetBackgroundColour( *wxWHITE ); |
1721 | ||
9861f022 RR |
1722 | m_penRule = wxPen(GetRuleColour(), 1, wxSOLID); |
1723 | ||
3b6280be RR |
1724 | //Here I compose a pen can draw black lines, maybe there are something system colour to use |
1725 | m_penExpander = wxPen( wxColour(0,0,0), 1, wxSOLID ); | |
aba9bfd0 RR |
1726 | //Some new added code to deal with the tree structure |
1727 | m_root = new wxDataViewTreeNode( NULL ); | |
704c3490 RR |
1728 | m_root->SetHasChildren(true); |
1729 | ||
3b6280be RR |
1730 | //Make m_count = -1 will cause the class recaculate the real displaying number of rows. |
1731 | m_count = -1 ; | |
4b3feaa7 | 1732 | UpdateDisplay(); |
4ed7af08 RR |
1733 | } |
1734 | ||
1735 | wxDataViewMainWindow::~wxDataViewMainWindow() | |
1736 | { | |
aba9bfd0 | 1737 | DestroyTree(); |
0fcce6b9 RR |
1738 | delete m_renameTimer; |
1739 | } | |
1740 | ||
1741 | void wxDataViewMainWindow::OnRenameTimer() | |
1742 | { | |
1743 | // We have to call this here because changes may just have | |
1744 | // been made and no screen update taken place. | |
1745 | if ( m_dirty ) | |
1746 | wxSafeYield(); | |
1747 | ||
0fcce6b9 | 1748 | int xpos = 0; |
9861f022 | 1749 | unsigned int cols = GetOwner()->GetColumnCount(); |
0a71f9e9 | 1750 | unsigned int i; |
0fcce6b9 RR |
1751 | for (i = 0; i < cols; i++) |
1752 | { | |
1753 | wxDataViewColumn *c = GetOwner()->GetColumn( i ); | |
9861f022 RR |
1754 | if (c->IsHidden()) |
1755 | continue; // skip it! | |
1756 | ||
0fcce6b9 RR |
1757 | if (c == m_currentCol) |
1758 | break; | |
1759 | xpos += c->GetWidth(); | |
1760 | } | |
c741d33f | 1761 | wxRect labelRect( xpos, m_currentRow * m_lineHeight, |
87f0efe2 | 1762 | m_currentCol->GetWidth(), m_lineHeight ); |
0fcce6b9 | 1763 | |
1e510b1e RR |
1764 | GetOwner()->CalcScrolledPosition( labelRect.x, labelRect.y, |
1765 | &labelRect.x, &labelRect.y); | |
1766 | ||
6cdcbce3 RR |
1767 | wxDataViewItem item = GetItemByRow( m_currentRow ); |
1768 | m_currentCol->GetRenderer()->StartEditing( item, labelRect ); | |
4ed7af08 RR |
1769 | } |
1770 | ||
d5025dc0 RR |
1771 | //------------------------------------------------------------------ |
1772 | // Helper class for do operation on the tree node | |
1773 | //------------------------------------------------------------------ | |
aba9bfd0 | 1774 | class DoJob |
a0f3af5f | 1775 | { |
aba9bfd0 RR |
1776 | public: |
1777 | DoJob(){}; | |
d5025dc0 RR |
1778 | virtual ~DoJob(){}; |
1779 | ||
1780 | //The return value control how the tree-walker tranverse the tree | |
1781 | // 0: Job done, stop tranverse and return | |
1782 | // 1: Ignore the current node's subtree and continue | |
1783 | // 2: Job not done, continue | |
1784 | enum { OK = 0 , IGR = 1, CONT = 2 }; | |
1785 | virtual int operator() ( wxDataViewTreeNode * node ) = 0 ; | |
d47db7e0 | 1786 | virtual int operator() ( void * n ) = 0; |
aba9bfd0 RR |
1787 | }; |
1788 | ||
d47db7e0 | 1789 | #if 0 |
aba9bfd0 | 1790 | class ItemAddJob: public DoJob |
a0f3af5f | 1791 | { |
aba9bfd0 | 1792 | public: |
704c3490 RR |
1793 | ItemAddJob( const wxDataViewItem & parent, const wxDataViewItem & item, int * count ) |
1794 | { this->parent = parent ; this->item = item ; m_count = count; } | |
d5025dc0 | 1795 | virtual ~ItemAddJob(){}; |
aba9bfd0 | 1796 | |
d5025dc0 | 1797 | virtual int operator() ( wxDataViewTreeNode * node ) |
aba9bfd0 RR |
1798 | { |
1799 | if( node->GetItem() == parent ) | |
1800 | { | |
704c3490 | 1801 | node->SetHasChildren( true ); |
aba9bfd0 | 1802 | wxDataViewTreeNode * newnode = new wxDataViewTreeNode( node ); |
3b6280be RR |
1803 | newnode->SetItem(item); |
1804 | node->AppendChild( newnode); | |
704c3490 | 1805 | *m_count = -1; |
d5025dc0 | 1806 | return OK; |
aba9bfd0 | 1807 | } |
d5025dc0 | 1808 | return CONT; |
aba9bfd0 | 1809 | } |
704c3490 | 1810 | |
aba9bfd0 | 1811 | private: |
704c3490 | 1812 | int * m_count; |
aba9bfd0 RR |
1813 | wxDataViewItem parent, item; |
1814 | }; | |
d47db7e0 | 1815 | #endif |
aba9bfd0 RR |
1816 | |
1817 | bool Walker( wxDataViewTreeNode * node, DoJob & func ) | |
1818 | { | |
3b6280be | 1819 | if( node==NULL || !node->HasChildren()) |
aba9bfd0 RR |
1820 | return false; |
1821 | ||
d47db7e0 RR |
1822 | wxDataViewTreeNodes nodes = node->GetNodes(); |
1823 | wxDataViewTreeLeaves leaves = node->GetChildren(); | |
1824 | ||
1825 | int len_nodes = nodes.GetCount(); | |
1826 | int len = leaves.GetCount(); | |
1827 | int i = 0, nodes_i = 0; | |
aba9bfd0 RR |
1828 | for( ; i < len ; i ++ ) |
1829 | { | |
d47db7e0 RR |
1830 | void * n = leaves[i]; |
1831 | if( nodes_i < len_nodes && n == nodes[nodes_i]->GetItem().GetID() ) | |
d5025dc0 | 1832 | { |
d47db7e0 RR |
1833 | wxDataViewTreeNode * nd = nodes[nodes_i]; |
1834 | nodes_i++; | |
1835 | ||
1836 | switch( func( nd ) ) | |
1837 | { | |
1838 | case DoJob::OK : | |
1839 | return true ; | |
1840 | case DoJob::IGR: | |
1841 | continue; | |
1842 | case DoJob::CONT: | |
1843 | default: | |
1844 | ; | |
1845 | } | |
442c56e6 | 1846 | |
d47db7e0 RR |
1847 | if( Walker( nd , func ) ) |
1848 | return true; | |
1849 | ||
1850 | } | |
1851 | else | |
1852 | switch( func( n ) ) | |
1853 | { | |
1854 | case DoJob::OK : | |
1855 | return true ; | |
1856 | case DoJob::IGR: | |
1857 | continue; | |
1858 | case DoJob::CONT: | |
1859 | default: | |
1860 | ; | |
442c56e6 | 1861 | } |
aba9bfd0 RR |
1862 | } |
1863 | return false; | |
a0f3af5f RR |
1864 | } |
1865 | ||
aba9bfd0 | 1866 | bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxDataViewItem & item) |
a0f3af5f | 1867 | { |
d47db7e0 RR |
1868 | g_model = GetOwner()->GetModel(); |
1869 | ||
351461fc RR |
1870 | wxDataViewTreeNode * node; |
1871 | node = FindNode(parent); | |
1872 | ||
1873 | if( node == NULL ) | |
1874 | return false; | |
1875 | ||
1876 | node->SetHasChildren( true ); | |
d47db7e0 RR |
1877 | |
1878 | if( g_model->IsContainer( item ) ) | |
1879 | { | |
1880 | wxDataViewTreeNode * newnode = new wxDataViewTreeNode( node ); | |
1881 | newnode->SetItem(item); | |
1882 | node->AddNode( newnode); | |
1883 | } | |
1884 | else | |
1885 | node->AddLeaf( item.GetID() ); | |
1886 | ||
1887 | node->ChangeSubTreeCount(1); | |
1888 | ||
351461fc | 1889 | m_count = -1; |
99d471a5 RR |
1890 | UpdateDisplay(); |
1891 | return true; | |
a0f3af5f RR |
1892 | } |
1893 | ||
d47db7e0 | 1894 | #if 0 |
704c3490 RR |
1895 | class ItemDeleteJob: public DoJob |
1896 | { | |
1897 | public: | |
1898 | ItemDeleteJob( const wxDataViewItem & item, int * count ) { m_item = item; m_count = count; } | |
1899 | virtual ~ItemDeleteJob(){} | |
1900 | virtual int operator() ( wxDataViewTreeNode * node ) | |
1901 | { | |
1902 | if( node->GetItem() == m_item ) | |
1903 | { | |
1904 | node->GetParent()->GetChildren().Remove( node ); | |
1905 | delete node; | |
1906 | *m_count = -1; | |
1907 | return DoJob::OK; | |
1908 | } | |
1909 | return DoJob::CONT; | |
1910 | } | |
1911 | ||
1912 | private: | |
1913 | int * m_count; | |
1914 | wxDataViewItem m_item; | |
1915 | }; | |
d47db7e0 RR |
1916 | #endif |
1917 | ||
1918 | void DestroyTreeHelper( wxDataViewTreeNode * node); | |
704c3490 | 1919 | |
32143117 VZ |
1920 | bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent, |
1921 | const wxDataViewItem& item) | |
a0f3af5f | 1922 | { |
d47db7e0 RR |
1923 | g_model = GetOwner()->GetModel(); |
1924 | ||
351461fc | 1925 | wxDataViewTreeNode * node; |
d47db7e0 RR |
1926 | wxDataViewItem parent_item = g_model->GetParent( item ); |
1927 | node = FindNode(parent_item); | |
351461fc | 1928 | |
d47db7e0 | 1929 | if( node == NULL || node->GetChildren().Index( item.GetID() ) == wxNOT_FOUND ) |
351461fc RR |
1930 | { |
1931 | return false; | |
1932 | } | |
1933 | ||
d47db7e0 RR |
1934 | int sub = -1; |
1935 | node->GetChildren().Remove( item.GetID() ); | |
1936 | if( GetOwner()->GetModel()->IsContainer( item ) ) | |
1937 | { | |
1938 | wxDataViewTreeNode * n ; | |
1939 | wxDataViewTreeNodes nodes = node->GetNodes(); | |
1940 | int len = nodes.GetCount(); | |
1941 | for( int i = 0 ; i < len; i ++) | |
1942 | { | |
1943 | if( nodes[i]->GetItem() == item ) | |
1944 | { | |
1945 | n = nodes[i]; | |
1946 | break; | |
1947 | } | |
1948 | } | |
1949 | node->GetNodes().Remove( n ); | |
1950 | sub -= n->GetSubTreeCount(); | |
1951 | DestroyTreeHelper(n); | |
1952 | } | |
1953 | ||
1954 | if( node->GetChildrenNumber() == 0) | |
1955 | node->SetHasChildren( false ); | |
1956 | ||
1957 | //Make the row number invalid and get a new valid one when user call GetRowCount | |
442c56e6 | 1958 | m_count = -1; |
d47db7e0 RR |
1959 | node->ChangeSubTreeCount(sub); |
1960 | //Change the current row to the last row if the current exceed the max row number | |
1961 | if( m_currentRow > GetRowCount() ) | |
1962 | m_currentRow = m_count - 1; | |
351461fc | 1963 | |
99d471a5 RR |
1964 | UpdateDisplay(); |
1965 | return true; | |
a0f3af5f RR |
1966 | } |
1967 | ||
aba9bfd0 | 1968 | bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem & item) |
a0f3af5f | 1969 | { |
d47db7e0 RR |
1970 | g_model = GetOwner()->GetModel(); |
1971 | ||
704c3490 RR |
1972 | unsigned int row = GetRowByItem(item); |
1973 | RefreshRow( row ); | |
99d471a5 | 1974 | return true; |
a0f3af5f RR |
1975 | } |
1976 | ||
aba9bfd0 | 1977 | bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem & item, unsigned int WXUNUSED(col) ) |
a0f3af5f | 1978 | { |
9861f022 | 1979 | // NOTE: to be valid, we cannot use e.g. INT_MAX - 1 |
aba9bfd0 | 1980 | /*#define MAX_VIRTUAL_WIDTH 100000 |
9861f022 RR |
1981 | |
1982 | wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight ); | |
0fdc2321 RR |
1983 | m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); |
1984 | Refresh( true, &rect ); | |
1985 | ||
1986 | return true; | |
aba9bfd0 | 1987 | */ |
d47db7e0 RR |
1988 | g_model = GetOwner()->GetModel(); |
1989 | ||
704c3490 RR |
1990 | unsigned int row = GetRowByItem(item); |
1991 | RefreshRow( row ); | |
0fcce6b9 | 1992 | return true; |
a0f3af5f RR |
1993 | } |
1994 | ||
1995 | bool wxDataViewMainWindow::Cleared() | |
1996 | { | |
d47db7e0 RR |
1997 | g_model = GetOwner()->GetModel(); |
1998 | ||
704c3490 | 1999 | DestroyTree(); |
99d471a5 RR |
2000 | UpdateDisplay(); |
2001 | return true; | |
a0f3af5f RR |
2002 | } |
2003 | ||
4b3feaa7 RR |
2004 | void wxDataViewMainWindow::UpdateDisplay() |
2005 | { | |
2006 | m_dirty = true; | |
2007 | } | |
2008 | ||
2009 | void wxDataViewMainWindow::OnInternalIdle() | |
2010 | { | |
2011 | wxWindow::OnInternalIdle(); | |
f554a14b | 2012 | |
4b3feaa7 RR |
2013 | if (m_dirty) |
2014 | { | |
2015 | RecalculateDisplay(); | |
2016 | m_dirty = false; | |
2017 | } | |
2018 | } | |
2019 | ||
2020 | void wxDataViewMainWindow::RecalculateDisplay() | |
2021 | { | |
aba9bfd0 | 2022 | wxDataViewModel *model = GetOwner()->GetModel(); |
4b3feaa7 RR |
2023 | if (!model) |
2024 | { | |
2025 | Refresh(); | |
2026 | return; | |
2027 | } | |
f554a14b | 2028 | |
9861f022 | 2029 | int width = GetEndOfLastCol(); |
aba9bfd0 | 2030 | int height = GetRowCount() * m_lineHeight; |
4b3feaa7 RR |
2031 | |
2032 | SetVirtualSize( width, height ); | |
2033 | GetOwner()->SetScrollRate( 10, m_lineHeight ); | |
f554a14b | 2034 | |
4b3feaa7 RR |
2035 | Refresh(); |
2036 | } | |
2037 | ||
2038 | void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) | |
2039 | { | |
2040 | wxWindow::ScrollWindow( dx, dy, rect ); | |
9861f022 RR |
2041 | |
2042 | if (GetOwner()->m_headerArea) | |
2043 | GetOwner()->m_headerArea->ScrollWindow( dx, 0 ); | |
4b3feaa7 RR |
2044 | } |
2045 | ||
f554a14b | 2046 | void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) |
4ed7af08 | 2047 | { |
aba9bfd0 | 2048 | wxDataViewModel *model = GetOwner()->GetModel(); |
2e992e06 VZ |
2049 | wxAutoBufferedPaintDC dc( this ); |
2050 | ||
9861f022 | 2051 | // prepare the DC |
2e992e06 VZ |
2052 | dc.SetBackground(GetBackgroundColour()); |
2053 | dc.Clear(); | |
4b3feaa7 | 2054 | GetOwner()->PrepareDC( dc ); |
4ed7af08 | 2055 | dc.SetFont( GetFont() ); |
90675b95 RR |
2056 | |
2057 | wxRect update = GetUpdateRegion().GetBox(); | |
2058 | m_owner->CalcUnscrolledPosition( update.x, update.y, &update.x, &update.y ); | |
f554a14b | 2059 | |
9861f022 | 2060 | // compute which items needs to be redrawn |
0a71f9e9 | 2061 | unsigned int item_start = wxMax( 0, (update.y / m_lineHeight) ); |
c741d33f | 2062 | unsigned int item_count = |
87f0efe2 | 2063 | wxMin( (int)(((update.y + update.height) / m_lineHeight) - item_start + 1), |
3b6280be | 2064 | (int)(GetRowCount( )- item_start) ); |
9861f022 RR |
2065 | unsigned int item_last = item_start + item_count; |
2066 | ||
2067 | // compute which columns needs to be redrawn | |
2068 | unsigned int cols = GetOwner()->GetColumnCount(); | |
2069 | unsigned int col_start = 0; | |
2070 | unsigned int x_start = 0; | |
2071 | for (x_start = 0; col_start < cols; col_start++) | |
2072 | { | |
2073 | wxDataViewColumn *col = GetOwner()->GetColumn(col_start); | |
2074 | if (col->IsHidden()) | |
2075 | continue; // skip it! | |
2076 | ||
2077 | unsigned int w = col->GetWidth(); | |
2078 | if (x_start+w >= (unsigned int)update.x) | |
2079 | break; | |
2080 | ||
2081 | x_start += w; | |
2082 | } | |
90675b95 | 2083 | |
9861f022 RR |
2084 | unsigned int col_last = col_start; |
2085 | unsigned int x_last = x_start; | |
2086 | for (; col_last < cols; col_last++) | |
2087 | { | |
2088 | wxDataViewColumn *col = GetOwner()->GetColumn(col_last); | |
2089 | if (col->IsHidden()) | |
2090 | continue; // skip it! | |
2091 | ||
2092 | if (x_last > (unsigned int)update.GetRight()) | |
2093 | break; | |
2094 | ||
2095 | x_last += col->GetWidth(); | |
2096 | } | |
2097 | ||
2098 | // Draw horizontal rules if required | |
2099 | if ( m_owner->HasFlag(wxDV_HORIZ_RULES) ) | |
2100 | { | |
2101 | dc.SetPen(m_penRule); | |
2102 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
2103 | ||
2104 | for (unsigned int i = item_start; i <= item_last+1; i++) | |
2105 | { | |
2106 | int y = i * m_lineHeight; | |
2107 | dc.DrawLine(x_start, y, x_last, y); | |
2108 | } | |
2109 | } | |
2110 | ||
2111 | // Draw vertical rules if required | |
2112 | if ( m_owner->HasFlag(wxDV_VERT_RULES) ) | |
2113 | { | |
2114 | dc.SetPen(m_penRule); | |
2115 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
2116 | ||
2117 | int x = x_start; | |
2118 | for (unsigned int i = col_start; i < col_last; i++) | |
2119 | { | |
2120 | wxDataViewColumn *col = GetOwner()->GetColumn(i); | |
2121 | if (col->IsHidden()) | |
2122 | continue; // skip it | |
2123 | ||
2124 | dc.DrawLine(x, item_start * m_lineHeight, | |
2125 | x, item_last * m_lineHeight); | |
2126 | ||
2127 | x += col->GetWidth(); | |
2128 | } | |
2129 | ||
2130 | // Draw last vertical rule | |
c741d33f | 2131 | dc.DrawLine(x, item_start * m_lineHeight, |
9861f022 RR |
2132 | x, item_last * m_lineHeight); |
2133 | } | |
2134 | ||
2135 | // redraw the background for the items which are selected/current | |
2136 | for (unsigned int item = item_start; item < item_last; item++) | |
cab07038 | 2137 | { |
87f0efe2 RR |
2138 | bool selected = m_selection.Index( item ) != wxNOT_FOUND; |
2139 | if (selected || item == m_currentRow) | |
cab07038 | 2140 | { |
5d3f234b | 2141 | int flags = selected ? (int)wxCONTROL_SELECTED : 0; |
daebb44c RR |
2142 | if (item == m_currentRow) |
2143 | flags |= wxCONTROL_CURRENT; | |
2144 | if (m_hasFocus) | |
2145 | flags |= wxCONTROL_FOCUSED; | |
9861f022 RR |
2146 | |
2147 | wxRect rect( x_start, item*m_lineHeight, x_last, m_lineHeight ); | |
daebb44c RR |
2148 | wxRendererNative::Get().DrawItemSelectionRect |
2149 | ( | |
2150 | this, | |
2151 | dc, | |
2152 | rect, | |
2153 | flags | |
2154 | ); | |
2155 | } | |
cab07038 | 2156 | } |
120b9b05 | 2157 | |
9861f022 | 2158 | // redraw all cells for all rows which must be repainted and for all columns |
90675b95 | 2159 | wxRect cell_rect; |
9861f022 RR |
2160 | cell_rect.x = x_start; |
2161 | cell_rect.height = m_lineHeight; // -1 is for the horizontal rules | |
2162 | for (unsigned int i = col_start; i < col_last; i++) | |
90675b95 RR |
2163 | { |
2164 | wxDataViewColumn *col = GetOwner()->GetColumn( i ); | |
baa9ebc4 | 2165 | wxDataViewRenderer *cell = col->GetRenderer(); |
90675b95 | 2166 | cell_rect.width = col->GetWidth(); |
f554a14b | 2167 | |
87f0efe2 RR |
2168 | if (col->IsHidden()) |
2169 | continue; // skipt it! | |
2170 | ||
9861f022 | 2171 | for (unsigned int item = item_start; item < item_last; item++) |
90675b95 | 2172 | { |
87f0efe2 | 2173 | // get the cell value and set it into the renderer |
90675b95 | 2174 | wxVariant value; |
3b6280be RR |
2175 | wxDataViewTreeNode * node = GetTreeNodeByRow(item); |
2176 | if( node == NULL ) | |
d47db7e0 | 2177 | { |
3b6280be | 2178 | continue; |
d47db7e0 | 2179 | } |
442c56e6 | 2180 | |
3b6280be RR |
2181 | wxDataViewItem dataitem = node->GetItem(); |
2182 | model->GetValue( value, dataitem, col->GetModelColumn()); | |
90675b95 | 2183 | cell->SetValue( value ); |
87f0efe2 RR |
2184 | |
2185 | // update the y offset | |
9861f022 | 2186 | cell_rect.y = item * m_lineHeight; |
87f0efe2 | 2187 | |
442c56e6 | 2188 | //Draw the expander here. |
3b6280be RR |
2189 | int indent = node->GetIndentLevel(); |
2190 | if( col->GetModelColumn() == GetOwner()->GetExpanderColumn() ) | |
2191 | { | |
2192 | //Calculate the indent first | |
d5025dc0 RR |
2193 | indent = cell_rect.x + GetOwner()->GetIndent() * indent; |
2194 | ||
3b6280be | 2195 | int expander_width = m_lineHeight - 2*EXPANDER_MARGIN; |
d5025dc0 RR |
2196 | // change the cell_rect.x to the appropriate pos |
2197 | int expander_x = indent + EXPANDER_MARGIN , expander_y = cell_rect.y + EXPANDER_MARGIN ; | |
3b6280be RR |
2198 | indent = indent + m_lineHeight ; //try to use the m_lineHeight as the expander space |
2199 | dc.SetPen( m_penExpander ); | |
d5025dc0 RR |
2200 | dc.SetBrush( wxNullBrush ); |
2201 | if( node->HasChildren() ) | |
2202 | { | |
2203 | //dc.DrawRoundedRectangle( expander_x,expander_y,expander_width,expander_width, 1.0); | |
2204 | //dc.DrawLine( expander_x + 2 , expander_y + expander_width/2, expander_x + expander_width - 2, expander_y + expander_width/2 ); | |
2205 | wxRect rect( expander_x , expander_y, expander_width, expander_width); | |
2206 | if( node->IsOpen() ) | |
2207 | wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, wxCONTROL_EXPANDED ); | |
2208 | else | |
2209 | wxRendererNative::Get().DrawTreeItemButton( this, dc, rect ); | |
2210 | } | |
2211 | else | |
2212 | { | |
d47db7e0 RR |
2213 | // I am wandering whether we should draw dot lines between tree nodes |
2214 | delete node; | |
2215 | //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow | |
d5025dc0 RR |
2216 | } |
2217 | ||
2218 | //force the expander column to left-center align | |
2219 | cell->SetAlignment( wxALIGN_CENTER_VERTICAL ); | |
3b6280be | 2220 | } |
3b6280be | 2221 | |
442c56e6 VZ |
2222 | |
2223 | // cannot be bigger than allocated space | |
87f0efe2 | 2224 | wxSize size = cell->GetSize(); |
d5025dc0 | 2225 | // Because of the tree structure indent, here we should minus the width of the cell for drawing |
3b6280be | 2226 | size.x = wxMin( size.x + 2*PADDING_RIGHTLEFT, cell_rect.width - indent ); |
9861f022 | 2227 | size.y = wxMin( size.y + 2*PADDING_TOPBOTTOM, cell_rect.height ); |
87f0efe2 RR |
2228 | |
2229 | wxRect item_rect(cell_rect.GetTopLeft(), size); | |
9861f022 | 2230 | int align = cell->GetAlignment(); |
87f0efe2 RR |
2231 | |
2232 | // horizontal alignment: | |
9861f022 RR |
2233 | item_rect.x = cell_rect.x; |
2234 | if (align & wxALIGN_CENTER_HORIZONTAL) | |
2235 | item_rect.x = cell_rect.x + (cell_rect.width / 2) - (size.x / 2); | |
2236 | else if (align & wxALIGN_RIGHT) | |
87f0efe2 RR |
2237 | item_rect.x = cell_rect.x + cell_rect.width - size.x; |
2238 | //else: wxALIGN_LEFT is the default | |
2239 | ||
2240 | // vertical alignment: | |
9861f022 RR |
2241 | item_rect.y = cell_rect.y; |
2242 | if (align & wxALIGN_CENTER_VERTICAL) | |
87f0efe2 | 2243 | item_rect.y = cell_rect.y + (cell_rect.height / 2) - (size.y / 2); |
9861f022 | 2244 | else if (align & wxALIGN_BOTTOM) |
87f0efe2 RR |
2245 | item_rect.y = cell_rect.y + cell_rect.height - size.y; |
2246 | //else: wxALIGN_TOP is the default | |
2247 | ||
9861f022 RR |
2248 | // add padding |
2249 | item_rect.x += PADDING_RIGHTLEFT; | |
2250 | item_rect.y += PADDING_TOPBOTTOM; | |
2251 | item_rect.width = size.x - 2 * PADDING_RIGHTLEFT; | |
2252 | item_rect.height = size.y - 2 * PADDING_TOPBOTTOM; | |
64b3c262 | 2253 | |
3b6280be RR |
2254 | //Here we add the tree indent |
2255 | item_rect.x += indent; | |
442c56e6 | 2256 | |
64b3c262 | 2257 | int state = 0; |
87f0efe2 | 2258 | if (m_selection.Index(item) != wxNOT_FOUND) |
64b3c262 | 2259 | state |= wxDATAVIEW_CELL_SELECTED; |
87f0efe2 | 2260 | |
9861f022 RR |
2261 | // TODO: it would be much more efficient to create a clipping |
2262 | // region for the entire column being rendered (in the OnPaint | |
2263 | // of wxDataViewMainWindow) instead of a single clip region for | |
2264 | // each cell. However it would mean that each renderer should | |
2265 | // respect the given wxRect's top & bottom coords, eventually | |
2266 | // violating only the left & right coords - however the user can | |
2267 | // make its own renderer and thus we cannot be sure of that. | |
2268 | dc.SetClippingRegion( item_rect ); | |
64b3c262 | 2269 | cell->Render( item_rect, &dc, state ); |
9861f022 | 2270 | dc.DestroyClippingRegion(); |
90675b95 | 2271 | } |
f554a14b | 2272 | |
90675b95 RR |
2273 | cell_rect.x += cell_rect.width; |
2274 | } | |
4ed7af08 RR |
2275 | } |
2276 | ||
9861f022 | 2277 | int wxDataViewMainWindow::GetCountPerPage() const |
cab07038 RR |
2278 | { |
2279 | wxSize size = GetClientSize(); | |
2280 | return size.y / m_lineHeight; | |
2281 | } | |
2282 | ||
9861f022 | 2283 | int wxDataViewMainWindow::GetEndOfLastCol() const |
e21f75bd RR |
2284 | { |
2285 | int width = 0; | |
0a71f9e9 | 2286 | unsigned int i; |
9861f022 | 2287 | for (i = 0; i < GetOwner()->GetColumnCount(); i++) |
e21f75bd | 2288 | { |
c741d33f | 2289 | const wxDataViewColumn *c = |
9861f022 RR |
2290 | wx_const_cast(wxDataViewCtrl*, GetOwner())->GetColumn( i ); |
2291 | ||
2292 | if (!c->IsHidden()) | |
2293 | width += c->GetWidth(); | |
e21f75bd RR |
2294 | } |
2295 | return width; | |
2296 | } | |
2297 | ||
9861f022 | 2298 | unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const |
72664514 RR |
2299 | { |
2300 | int x = 0; | |
2301 | int y = 0; | |
2302 | m_owner->CalcUnscrolledPosition( x, y, &x, &y ); | |
120b9b05 | 2303 | |
72664514 RR |
2304 | return y / m_lineHeight; |
2305 | } | |
2306 | ||
442c56e6 | 2307 | unsigned int wxDataViewMainWindow::GetLastVisibleRow() |
72664514 RR |
2308 | { |
2309 | wxSize client_size = GetClientSize(); | |
c741d33f | 2310 | m_owner->CalcUnscrolledPosition( client_size.x, client_size.y, |
87f0efe2 | 2311 | &client_size.x, &client_size.y ); |
72664514 | 2312 | |
5637e131 | 2313 | return wxMin( GetRowCount()-1, ((unsigned)client_size.y/m_lineHeight)+1 ); |
72664514 RR |
2314 | } |
2315 | ||
442c56e6 | 2316 | unsigned int wxDataViewMainWindow::GetRowCount() |
cab07038 | 2317 | { |
3b6280be RR |
2318 | if ( m_count == -1 ) |
2319 | { | |
2320 | m_count = RecalculateCount(); | |
d5025dc0 RR |
2321 | int width, height; |
2322 | GetVirtualSize( &width, &height ); | |
3b6280be RR |
2323 | height = m_count * m_lineHeight; |
2324 | ||
2325 | SetVirtualSize( width, height ); | |
2326 | } | |
aba9bfd0 | 2327 | return m_count; |
cab07038 RR |
2328 | } |
2329 | ||
0a71f9e9 | 2330 | void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row ) |
e21f75bd RR |
2331 | { |
2332 | m_currentRow = row; | |
120b9b05 | 2333 | |
e21f75bd RR |
2334 | // send event |
2335 | } | |
2336 | ||
cab07038 RR |
2337 | void wxDataViewMainWindow::SelectAllRows( bool on ) |
2338 | { | |
4a851b11 VZ |
2339 | if (IsEmpty()) |
2340 | return; | |
120b9b05 | 2341 | |
cab07038 RR |
2342 | if (on) |
2343 | { | |
72664514 | 2344 | m_selection.Clear(); |
0a71f9e9 | 2345 | for (unsigned int i = 0; i < GetRowCount(); i++) |
cab07038 | 2346 | m_selection.Add( i ); |
72664514 RR |
2347 | Refresh(); |
2348 | } | |
2349 | else | |
2350 | { | |
0a71f9e9 RR |
2351 | unsigned int first_visible = GetFirstVisibleRow(); |
2352 | unsigned int last_visible = GetLastVisibleRow(); | |
2353 | unsigned int i; | |
72664514 | 2354 | for (i = 0; i < m_selection.GetCount(); i++) |
120b9b05 | 2355 | { |
0a71f9e9 | 2356 | unsigned int row = m_selection[i]; |
72664514 RR |
2357 | if ((row >= first_visible) && (row <= last_visible)) |
2358 | RefreshRow( row ); | |
2359 | } | |
2360 | m_selection.Clear(); | |
cab07038 | 2361 | } |
cab07038 RR |
2362 | } |
2363 | ||
0a71f9e9 | 2364 | void wxDataViewMainWindow::SelectRow( unsigned int row, bool on ) |
cab07038 RR |
2365 | { |
2366 | if (m_selection.Index( row ) == wxNOT_FOUND) | |
2367 | { | |
2368 | if (on) | |
2369 | { | |
2370 | m_selection.Add( row ); | |
2371 | RefreshRow( row ); | |
2372 | } | |
2373 | } | |
2374 | else | |
2375 | { | |
2376 | if (!on) | |
2377 | { | |
2378 | m_selection.Remove( row ); | |
2379 | RefreshRow( row ); | |
2380 | } | |
2381 | } | |
2382 | } | |
2383 | ||
0a71f9e9 | 2384 | void wxDataViewMainWindow::SelectRows( unsigned int from, unsigned int to, bool on ) |
cab07038 RR |
2385 | { |
2386 | if (from > to) | |
2387 | { | |
0a71f9e9 | 2388 | unsigned int tmp = from; |
cab07038 RR |
2389 | from = to; |
2390 | to = tmp; | |
2391 | } | |
2392 | ||
0a71f9e9 | 2393 | unsigned int i; |
cab07038 RR |
2394 | for (i = from; i <= to; i++) |
2395 | { | |
2396 | if (m_selection.Index( i ) == wxNOT_FOUND) | |
2397 | { | |
2398 | if (on) | |
2399 | m_selection.Add( i ); | |
2400 | } | |
2401 | else | |
2402 | { | |
2403 | if (!on) | |
2404 | m_selection.Remove( i ); | |
2405 | } | |
2406 | } | |
2407 | RefreshRows( from, to ); | |
2408 | } | |
2409 | ||
87f0efe2 RR |
2410 | void wxDataViewMainWindow::Select( const wxArrayInt& aSelections ) |
2411 | { | |
2412 | for (size_t i=0; i < aSelections.GetCount(); i++) | |
2413 | { | |
2414 | int n = aSelections[i]; | |
2415 | ||
2416 | m_selection.Add( n ); | |
2417 | RefreshRow( n ); | |
2418 | } | |
2419 | } | |
2420 | ||
0a71f9e9 | 2421 | void wxDataViewMainWindow::ReverseRowSelection( unsigned int row ) |
cab07038 RR |
2422 | { |
2423 | if (m_selection.Index( row ) == wxNOT_FOUND) | |
2424 | m_selection.Add( row ); | |
2425 | else | |
2426 | m_selection.Remove( row ); | |
120b9b05 | 2427 | RefreshRow( row ); |
cab07038 RR |
2428 | } |
2429 | ||
0a71f9e9 | 2430 | bool wxDataViewMainWindow::IsRowSelected( unsigned int row ) |
cab07038 RR |
2431 | { |
2432 | return (m_selection.Index( row ) != wxNOT_FOUND); | |
2433 | } | |
2434 | ||
0a71f9e9 | 2435 | void wxDataViewMainWindow::RefreshRow( unsigned int row ) |
cab07038 | 2436 | { |
e21f75bd | 2437 | wxRect rect( 0, row*m_lineHeight, GetEndOfLastCol(), m_lineHeight ); |
cab07038 | 2438 | m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); |
120b9b05 | 2439 | |
cab07038 RR |
2440 | wxSize client_size = GetClientSize(); |
2441 | wxRect client_rect( 0, 0, client_size.x, client_size.y ); | |
2442 | wxRect intersect_rect = client_rect.Intersect( rect ); | |
2443 | if (intersect_rect.width > 0) | |
2444 | Refresh( true, &intersect_rect ); | |
2445 | } | |
2446 | ||
0a71f9e9 | 2447 | void wxDataViewMainWindow::RefreshRows( unsigned int from, unsigned int to ) |
cab07038 RR |
2448 | { |
2449 | if (from > to) | |
2450 | { | |
0a71f9e9 | 2451 | unsigned int tmp = to; |
cab07038 RR |
2452 | to = from; |
2453 | from = tmp; | |
2454 | } | |
2455 | ||
e21f75bd | 2456 | wxRect rect( 0, from*m_lineHeight, GetEndOfLastCol(), (to-from+1) * m_lineHeight ); |
cab07038 | 2457 | m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); |
120b9b05 | 2458 | |
cab07038 RR |
2459 | wxSize client_size = GetClientSize(); |
2460 | wxRect client_rect( 0, 0, client_size.x, client_size.y ); | |
2461 | wxRect intersect_rect = client_rect.Intersect( rect ); | |
2462 | if (intersect_rect.width > 0) | |
2463 | Refresh( true, &intersect_rect ); | |
2464 | } | |
2465 | ||
0a71f9e9 | 2466 | void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow ) |
cab07038 | 2467 | { |
0a71f9e9 | 2468 | unsigned int count = GetRowCount(); |
4a851b11 VZ |
2469 | if (firstRow > count) |
2470 | return; | |
120b9b05 | 2471 | |
e21f75bd | 2472 | wxRect rect( 0, firstRow*m_lineHeight, GetEndOfLastCol(), count * m_lineHeight ); |
cab07038 | 2473 | m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); |
120b9b05 | 2474 | |
cab07038 RR |
2475 | wxSize client_size = GetClientSize(); |
2476 | wxRect client_rect( 0, 0, client_size.x, client_size.y ); | |
2477 | wxRect intersect_rect = client_rect.Intersect( rect ); | |
2478 | if (intersect_rect.width > 0) | |
2479 | Refresh( true, &intersect_rect ); | |
2480 | } | |
2481 | ||
0a71f9e9 | 2482 | void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event) |
cab07038 | 2483 | { |
4a851b11 | 2484 | wxCHECK_RET( newCurrent < GetRowCount(), |
cab07038 RR |
2485 | _T("invalid item index in OnArrowChar()") ); |
2486 | ||
2487 | // if there is no selection, we cannot move it anywhere | |
2488 | if (!HasCurrentRow()) | |
2489 | return; | |
2490 | ||
0a71f9e9 | 2491 | unsigned int oldCurrent = m_currentRow; |
cab07038 RR |
2492 | |
2493 | // in single selection we just ignore Shift as we can't select several | |
2494 | // items anyhow | |
e21f75bd | 2495 | if ( event.ShiftDown() && !IsSingleSel() ) |
cab07038 RR |
2496 | { |
2497 | RefreshRow( oldCurrent ); | |
120b9b05 | 2498 | |
e21f75bd | 2499 | ChangeCurrentRow( newCurrent ); |
cab07038 RR |
2500 | |
2501 | // select all the items between the old and the new one | |
2502 | if ( oldCurrent > newCurrent ) | |
2503 | { | |
2504 | newCurrent = oldCurrent; | |
2505 | oldCurrent = m_currentRow; | |
2506 | } | |
2507 | ||
2508 | SelectRows( oldCurrent, newCurrent, true ); | |
2509 | } | |
2510 | else // !shift | |
2511 | { | |
72664514 | 2512 | RefreshRow( oldCurrent ); |
120b9b05 | 2513 | |
cab07038 RR |
2514 | // all previously selected items are unselected unless ctrl is held |
2515 | if ( !event.ControlDown() ) | |
2516 | SelectAllRows(false); | |
2517 | ||
e21f75bd | 2518 | ChangeCurrentRow( newCurrent ); |
cab07038 RR |
2519 | |
2520 | if ( !event.ControlDown() ) | |
2521 | SelectRow( m_currentRow, true ); | |
72664514 RR |
2522 | else |
2523 | RefreshRow( m_currentRow ); | |
cab07038 RR |
2524 | } |
2525 | ||
9861f022 RR |
2526 | //EnsureVisible( m_currentRow ); |
2527 | } | |
2528 | ||
2529 | wxRect wxDataViewMainWindow::GetLineRect( unsigned int row ) const | |
2530 | { | |
2531 | wxRect rect; | |
2532 | rect.x = 0; | |
2533 | rect.y = m_lineHeight * row; | |
2534 | rect.width = GetEndOfLastCol(); | |
2535 | rect.height = m_lineHeight; | |
2536 | ||
2537 | return rect; | |
cab07038 RR |
2538 | } |
2539 | ||
aba9bfd0 RR |
2540 | class RowToItemJob: public DoJob |
2541 | { | |
2542 | public: | |
2543 | RowToItemJob( unsigned int row , int current ) { this->row = row; this->current = current ;} | |
2544 | virtual ~RowToItemJob(){}; | |
2545 | ||
d5025dc0 | 2546 | virtual int operator() ( wxDataViewTreeNode * node ) |
d47db7e0 RR |
2547 | { |
2548 | current ++; | |
2549 | if( current == static_cast<int>(row)) | |
2550 | { | |
2551 | ret = node->GetItem() ; | |
2552 | return DoJob::OK; | |
2553 | } | |
d5025dc0 | 2554 | |
d47db7e0 RR |
2555 | if( node->GetSubTreeCount() + current < static_cast<int>(row) ) |
2556 | { | |
2557 | current += node->GetSubTreeCount(); | |
2558 | return DoJob::IGR; | |
2559 | } | |
2560 | else | |
2561 | return DoJob::CONT; | |
2562 | } | |
2563 | ||
2564 | virtual int operator() ( void * n ) | |
2565 | { | |
2566 | current ++; | |
2567 | if( current == static_cast<int>(row)) | |
2568 | { | |
2569 | ret = wxDataViewItem( n ) ; | |
2570 | return DoJob::OK; | |
2571 | } | |
2572 | return DoJob::CONT; | |
2573 | } | |
d5025dc0 | 2574 | wxDataViewItem GetResult(){ return ret; } |
aba9bfd0 RR |
2575 | private: |
2576 | unsigned int row; | |
2577 | int current ; | |
2578 | wxDataViewItem ret; | |
2579 | }; | |
2580 | ||
2581 | wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row) | |
2582 | { | |
d47db7e0 | 2583 | RowToItemJob job( row, -1 ); |
aba9bfd0 RR |
2584 | Walker( m_root , job ); |
2585 | return job.GetResult(); | |
2586 | } | |
2587 | ||
3b6280be RR |
2588 | class RowToTreeNodeJob: public DoJob |
2589 | { | |
2590 | public: | |
442c56e6 VZ |
2591 | RowToTreeNodeJob( unsigned int row , int current, wxDataViewTreeNode * node ) |
2592 | { | |
2593 | this->row = row; | |
2594 | this->current = current ; | |
d47db7e0 RR |
2595 | ret = NULL ; |
2596 | parent = node; | |
2597 | } | |
d5025dc0 | 2598 | virtual ~RowToTreeNodeJob(){}; |
3b6280be RR |
2599 | |
2600 | virtual int operator() ( wxDataViewTreeNode * node ) | |
d5025dc0 | 2601 | { |
d47db7e0 | 2602 | current ++; |
704c3490 | 2603 | if( current == static_cast<int>(row)) |
d5025dc0 RR |
2604 | { |
2605 | ret = node ; | |
2606 | return DoJob::OK; | |
3b6280be | 2607 | } |
d47db7e0 RR |
2608 | |
2609 | if( node->GetSubTreeCount() + current < static_cast<int>(row) ) | |
2610 | { | |
2611 | current += node->GetSubTreeCount(); | |
2612 | return DoJob::IGR; | |
2613 | } | |
d5025dc0 | 2614 | else |
d47db7e0 RR |
2615 | { |
2616 | parent = node; | |
2617 | return DoJob::CONT; | |
2618 | } | |
2619 | ||
d5025dc0 | 2620 | } |
3b6280be | 2621 | |
d47db7e0 RR |
2622 | virtual int operator() ( void * n ) |
2623 | { | |
2624 | current ++; | |
2625 | if( current == static_cast<int>(row)) | |
2626 | { | |
2627 | ret = new wxDataViewTreeNode( parent ) ; | |
2628 | ret->SetItem( wxDataViewItem( n )); | |
2629 | ret->SetHasChildren(false); | |
2630 | return DoJob::OK; | |
2631 | } | |
442c56e6 | 2632 | |
d47db7e0 RR |
2633 | return DoJob::CONT; |
2634 | } | |
3b6280be RR |
2635 | wxDataViewTreeNode * GetResult(){ return ret; } |
2636 | private: | |
2637 | unsigned int row; | |
2638 | int current ; | |
2639 | wxDataViewTreeNode * ret; | |
d47db7e0 | 2640 | wxDataViewTreeNode * parent ; |
3b6280be RR |
2641 | }; |
2642 | ||
2643 | ||
2644 | wxDataViewTreeNode * wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row) | |
2645 | { | |
d47db7e0 | 2646 | RowToTreeNodeJob job( row , -1, m_root ); |
3b6280be RR |
2647 | Walker( m_root , job ); |
2648 | return job.GetResult(); | |
2649 | } | |
2650 | ||
d47db7e0 | 2651 | #if 0 |
3b6280be RR |
2652 | class CountJob : public DoJob |
2653 | { | |
2654 | public: | |
2655 | CountJob(){ count = 0 ; } | |
d5025dc0 | 2656 | virtual ~CountJob(){}; |
442c56e6 | 2657 | |
3b6280be RR |
2658 | virtual int operator () ( wxDataViewTreeNode * node ) |
2659 | { | |
d5025dc0 RR |
2660 | count ++; |
2661 | if ( node->IsOpen()) | |
2662 | return DoJob::CONT; | |
2663 | else | |
2664 | return DoJob::IGR; | |
3b6280be RR |
2665 | } |
2666 | ||
2667 | unsigned int GetResult() | |
2668 | { | |
2669 | return count ; | |
2670 | } | |
2671 | private: | |
2672 | unsigned int count; | |
2673 | }; | |
d47db7e0 | 2674 | #endif |
3b6280be RR |
2675 | |
2676 | void wxDataViewMainWindow::OnExpanding( unsigned int row ) | |
2677 | { | |
2678 | wxDataViewTreeNode * node = GetTreeNodeByRow(row); | |
2679 | if( node != NULL ) | |
2680 | { | |
d5025dc0 RR |
2681 | if( node->HasChildren()) |
2682 | if( !node->IsOpen()) | |
3b6280be | 2683 | { |
d5025dc0 | 2684 | node->ToggleOpen(); |
704c3490 RR |
2685 | //Here I build the children of current node |
2686 | if( node->GetChildrenNumber() == 0 ) | |
2687 | BuildTreeHelper(GetOwner()->GetModel(), node->GetItem(), node); | |
d5025dc0 | 2688 | m_count = -1; |
351461fc RR |
2689 | UpdateDisplay(); |
2690 | } | |
2691 | else | |
2692 | { | |
2693 | SelectRow( row, false ); | |
2694 | SelectRow( row + 1, true ); | |
2695 | ChangeCurrentRow( row + 1 ); | |
3b6280be | 2696 | } |
d47db7e0 RR |
2697 | else |
2698 | delete node; | |
3b6280be RR |
2699 | } |
2700 | } | |
2701 | ||
2702 | void wxDataViewMainWindow::OnCollapsing(unsigned int row) | |
2703 | { | |
2704 | wxDataViewTreeNode * node = GetTreeNodeByRow(row); | |
d5025dc0 | 2705 | if( node != NULL ) |
3b6280be | 2706 | { |
d47db7e0 RR |
2707 | wxDataViewTreeNode * nd = node; |
2708 | ||
3b6280be RR |
2709 | if( node->HasChildren() && node->IsOpen() ) |
2710 | { | |
2711 | node->ToggleOpen(); | |
2712 | m_count = -1; | |
351461fc | 2713 | UpdateDisplay(); |
3b6280be | 2714 | //RefreshRows(row,GetLastVisibleRow()); |
d5025dc0 RR |
2715 | } |
2716 | else | |
2717 | { | |
2718 | node = node->GetParent(); | |
2719 | if( node != NULL ) | |
2720 | { | |
2721 | int parent = GetRowByItem( node->GetItem()) ; | |
2722 | SelectRow( row, false); | |
2723 | SelectRow(parent , true ); | |
2724 | ChangeCurrentRow( parent ); | |
2725 | } | |
2726 | } | |
d47db7e0 RR |
2727 | if( !nd->HasChildren()) |
2728 | delete nd; | |
3b6280be RR |
2729 | } |
2730 | } | |
2731 | ||
351461fc RR |
2732 | wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item ) |
2733 | { | |
2734 | wxDataViewModel * model = GetOwner()->GetModel(); | |
2735 | if( model == NULL ) | |
2736 | return NULL; | |
2737 | ||
2738 | //Compose the a parent-chain of the finding item | |
2739 | ItemList list; | |
2740 | list.DeleteContents( true ); | |
2741 | wxDataViewItem it( item ); | |
2742 | while( it.IsOk() ) | |
2743 | { | |
2744 | wxDataViewItem * pItem = new wxDataViewItem( it ); | |
2745 | list.Insert( pItem ); | |
2746 | it = model->GetParent( it ); | |
2747 | } | |
2748 | ||
442c56e6 | 2749 | //Find the item along the parent-chain. |
351461fc RR |
2750 | //This algorithm is designed to speed up the node-finding method |
2751 | bool found = true; | |
2752 | wxDataViewTreeNode * node = m_root; | |
2753 | for( ItemList::Node * n = list.GetFirst(); n; n = n->GetNext() ) | |
2754 | { | |
2755 | if( node->HasChildren() ) | |
2756 | { | |
2757 | if( node->GetChildrenNumber() == 0 ) | |
2758 | BuildTreeHelper(model, node->GetItem(), node); | |
2759 | ||
d47db7e0 RR |
2760 | int len = node->GetNodeNumber(); |
2761 | wxDataViewTreeNodes nodes = node->GetNodes(); | |
351461fc RR |
2762 | int j = 0; |
2763 | for( ; j < len; j ++) | |
2764 | { | |
2765 | if( nodes[j]->GetItem() == *(n->GetData())) | |
2766 | { | |
2767 | node = nodes[j]; | |
2768 | break; | |
442c56e6 | 2769 | } |
351461fc RR |
2770 | } |
2771 | // Whenever we can't find the node in any level, return NULL to indicate the item can't be found | |
2772 | if( j == len ) | |
2773 | { | |
2774 | found = false; | |
2775 | return NULL; | |
2776 | } | |
2777 | } | |
2778 | else | |
2779 | return NULL; | |
2780 | } | |
2781 | return node; | |
2782 | } | |
2783 | ||
442c56e6 | 2784 | int wxDataViewMainWindow::RecalculateCount() |
3b6280be | 2785 | { |
d47db7e0 | 2786 | return m_root->GetSubTreeCount(); |
3b6280be RR |
2787 | } |
2788 | ||
aba9bfd0 RR |
2789 | class ItemToRowJob : public DoJob |
2790 | { | |
2791 | public: | |
d47db7e0 RR |
2792 | ItemToRowJob(const wxDataViewItem & item, ItemList::Node * node ) |
2793 | { this->item = item ; ret = 0 ; nd = node ; } | |
aba9bfd0 RR |
2794 | virtual ~ItemToRowJob(){}; |
2795 | ||
d5025dc0 RR |
2796 | virtual int operator() ( wxDataViewTreeNode * node) |
2797 | { | |
2798 | ret ++; | |
2799 | if( node->GetItem() == item ) | |
d47db7e0 | 2800 | { |
d5025dc0 | 2801 | return DoJob::OK; |
d47db7e0 | 2802 | } |
d5025dc0 | 2803 | |
d47db7e0 RR |
2804 | if( nd && node->GetItem() == *(nd->GetData())) |
2805 | { | |
2806 | nd = nd->GetNext(); | |
d5025dc0 | 2807 | return DoJob::CONT; |
d47db7e0 | 2808 | } |
d5025dc0 | 2809 | else |
d47db7e0 RR |
2810 | { |
2811 | ret += node->GetSubTreeCount(); | |
d5025dc0 | 2812 | return DoJob::IGR; |
d47db7e0 | 2813 | } |
442c56e6 | 2814 | |
d5025dc0 | 2815 | } |
aba9bfd0 | 2816 | |
d47db7e0 RR |
2817 | virtual int operator() ( void * n ) |
2818 | { | |
2819 | ret ++; | |
2820 | if( n == item.GetID() ) | |
2821 | return DoJob::OK; | |
2822 | return DoJob::CONT; | |
2823 | } | |
3b6280be RR |
2824 | //the row number is begin from zero |
2825 | int GetResult(){ return ret -1 ; } | |
aba9bfd0 | 2826 | private: |
d47db7e0 | 2827 | ItemList::Node * nd; |
aba9bfd0 RR |
2828 | wxDataViewItem item; |
2829 | int ret; | |
442c56e6 | 2830 | |
aba9bfd0 RR |
2831 | }; |
2832 | ||
2833 | unsigned int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item) | |
2834 | { | |
d47db7e0 RR |
2835 | wxDataViewModel * model = GetOwner()->GetModel(); |
2836 | if( model == NULL ) | |
2837 | return 0; | |
2838 | ||
2839 | //Compose the a parent-chain of the finding item | |
2840 | ItemList list; | |
2841 | list.DeleteContents( true ); | |
2842 | wxDataViewItem it( item ); | |
2843 | while( it.IsOk() ) | |
2844 | { | |
2845 | wxDataViewItem * pItem = new wxDataViewItem( it ); | |
2846 | list.Insert( pItem ); | |
2847 | it = model->GetParent( it ); | |
2848 | } | |
2849 | ||
2850 | ItemToRowJob job( item, list.GetFirst() ); | |
aba9bfd0 RR |
2851 | Walker(m_root , job ); |
2852 | return job.GetResult(); | |
2853 | } | |
2854 | ||
704c3490 | 2855 | void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, wxDataViewTreeNode * node) |
aba9bfd0 | 2856 | { |
351461fc | 2857 | if( !model->IsContainer( item ) ) |
704c3490 | 2858 | return ; |
442c56e6 | 2859 | |
aba9bfd0 | 2860 | wxDataViewItem i = model->GetFirstChild( item ); |
d47db7e0 | 2861 | int num = 0; |
aba9bfd0 RR |
2862 | while( i.IsOk() ) |
2863 | { | |
d47db7e0 RR |
2864 | num ++; |
2865 | if( model->IsContainer( i ) ) | |
2866 | { | |
2867 | wxDataViewTreeNode * n = new wxDataViewTreeNode( node ); | |
2868 | n->SetItem(i); | |
2869 | n->SetHasChildren( true ) ; | |
2870 | node->AddNode( n ); | |
2871 | } | |
2872 | else | |
2873 | { | |
2874 | node->AddLeaf( i.GetID() ); | |
2875 | } | |
3b6280be | 2876 | i = model->GetNextSibling( i ); |
aba9bfd0 | 2877 | } |
d47db7e0 RR |
2878 | node->SetSubTreeCount( num ); |
2879 | wxDataViewTreeNode * n = node->GetParent(); | |
2880 | if( n != NULL) | |
2881 | n->ChangeSubTreeCount(num); | |
2882 | ||
aba9bfd0 RR |
2883 | } |
2884 | ||
2885 | void wxDataViewMainWindow::BuildTree(wxDataViewModel * model) | |
2886 | { | |
2887 | //First we define a invalid item to fetch the top-level elements | |
2888 | wxDataViewItem item; | |
d47db7e0 | 2889 | g_model = GetOwner()->GetModel(); |
3b6280be RR |
2890 | BuildTreeHelper( model, item, m_root); |
2891 | m_count = -1 ; | |
aba9bfd0 RR |
2892 | } |
2893 | ||
2894 | void DestroyTreeHelper( wxDataViewTreeNode * node ) | |
2895 | { | |
d47db7e0 | 2896 | if( node->GetNodeNumber() != 0 ) |
aba9bfd0 | 2897 | { |
d47db7e0 | 2898 | int len = node->GetNodeNumber(); |
3b6280be | 2899 | int i = 0 ; |
d47db7e0 | 2900 | wxDataViewTreeNodes nodes = node->GetNodes(); |
3b6280be | 2901 | for( ; i < len; i ++ ) |
aba9bfd0 RR |
2902 | { |
2903 | DestroyTreeHelper(nodes[i]); | |
2904 | } | |
2905 | } | |
2906 | delete node; | |
2907 | } | |
2908 | ||
2909 | void wxDataViewMainWindow::DestroyTree() | |
2910 | { | |
2911 | DestroyTreeHelper(m_root); | |
d47db7e0 | 2912 | m_root->SetSubTreeCount(0); |
aba9bfd0 RR |
2913 | m_count = 0 ; |
2914 | } | |
2915 | ||
cab07038 RR |
2916 | void wxDataViewMainWindow::OnChar( wxKeyEvent &event ) |
2917 | { | |
2918 | if (event.GetKeyCode() == WXK_TAB) | |
2919 | { | |
2920 | wxNavigationKeyEvent nevent; | |
2921 | nevent.SetWindowChange( event.ControlDown() ); | |
2922 | nevent.SetDirection( !event.ShiftDown() ); | |
2923 | nevent.SetEventObject( GetParent()->GetParent() ); | |
2924 | nevent.SetCurrentFocus( m_parent ); | |
2925 | if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent )) | |
2926 | return; | |
2927 | } | |
2928 | ||
2929 | // no item -> nothing to do | |
2930 | if (!HasCurrentRow()) | |
2931 | { | |
2932 | event.Skip(); | |
2933 | return; | |
2934 | } | |
120b9b05 | 2935 | |
cab07038 RR |
2936 | // don't use m_linesPerPage directly as it might not be computed yet |
2937 | const int pageSize = GetCountPerPage(); | |
2938 | wxCHECK_RET( pageSize, _T("should have non zero page size") ); | |
2939 | ||
2940 | switch ( event.GetKeyCode() ) | |
2941 | { | |
2942 | case WXK_UP: | |
2943 | if ( m_currentRow > 0 ) | |
2944 | OnArrowChar( m_currentRow - 1, event ); | |
2945 | break; | |
2946 | ||
2947 | case WXK_DOWN: | |
4a851b11 | 2948 | if ( m_currentRow < GetRowCount() - 1 ) |
cab07038 RR |
2949 | OnArrowChar( m_currentRow + 1, event ); |
2950 | break; | |
3b6280be RR |
2951 | //Add the process for tree expanding/collapsing |
2952 | case WXK_LEFT: | |
2953 | OnCollapsing(m_currentRow); | |
2954 | break; | |
2955 | case WXK_RIGHT: | |
2956 | OnExpanding( m_currentRow); | |
2957 | break; | |
cab07038 RR |
2958 | case WXK_END: |
2959 | if (!IsEmpty()) | |
2960 | OnArrowChar( GetRowCount() - 1, event ); | |
2961 | break; | |
2962 | ||
2963 | case WXK_HOME: | |
2964 | if (!IsEmpty()) | |
2965 | OnArrowChar( 0, event ); | |
2966 | break; | |
2967 | ||
2968 | case WXK_PAGEUP: | |
2969 | { | |
2970 | int steps = pageSize - 1; | |
2971 | int index = m_currentRow - steps; | |
2972 | if (index < 0) | |
2973 | index = 0; | |
2974 | ||
2975 | OnArrowChar( index, event ); | |
2976 | } | |
2977 | break; | |
2978 | ||
2979 | case WXK_PAGEDOWN: | |
2980 | { | |
2981 | int steps = pageSize - 1; | |
0a71f9e9 RR |
2982 | unsigned int index = m_currentRow + steps; |
2983 | unsigned int count = GetRowCount(); | |
cab07038 RR |
2984 | if ( index >= count ) |
2985 | index = count - 1; | |
2986 | ||
2987 | OnArrowChar( index, event ); | |
2988 | } | |
2989 | break; | |
2990 | ||
2991 | default: | |
2992 | event.Skip(); | |
2993 | } | |
2994 | } | |
2995 | ||
4ed7af08 RR |
2996 | void wxDataViewMainWindow::OnMouse( wxMouseEvent &event ) |
2997 | { | |
e21f75bd RR |
2998 | if (event.GetEventType() == wxEVT_MOUSEWHEEL) |
2999 | { | |
3000 | // let the base handle mouse wheel events. | |
3001 | event.Skip(); | |
3002 | return; | |
3003 | } | |
3004 | ||
0fdc2321 RR |
3005 | int x = event.GetX(); |
3006 | int y = event.GetY(); | |
3007 | m_owner->CalcUnscrolledPosition( x, y, &x, &y ); | |
3008 | ||
3009 | wxDataViewColumn *col = NULL; | |
3010 | ||
3011 | int xpos = 0; | |
9861f022 | 3012 | unsigned int cols = GetOwner()->GetColumnCount(); |
0a71f9e9 | 3013 | unsigned int i; |
0fdc2321 RR |
3014 | for (i = 0; i < cols; i++) |
3015 | { | |
3016 | wxDataViewColumn *c = GetOwner()->GetColumn( i ); | |
9861f022 RR |
3017 | if (c->IsHidden()) |
3018 | continue; // skip it! | |
3019 | ||
0fdc2321 RR |
3020 | if (x < xpos + c->GetWidth()) |
3021 | { | |
3022 | col = c; | |
3023 | break; | |
3024 | } | |
3025 | xpos += c->GetWidth(); | |
3026 | } | |
f554a14b | 3027 | if (!col) |
0fdc2321 | 3028 | return; |
baa9ebc4 | 3029 | wxDataViewRenderer *cell = col->GetRenderer(); |
f554a14b | 3030 | |
0a71f9e9 | 3031 | unsigned int current = y / m_lineHeight; |
120b9b05 | 3032 | |
e21f75bd RR |
3033 | if ((current > GetRowCount()) || (x > GetEndOfLastCol())) |
3034 | { | |
3035 | // Unselect all if below the last row ? | |
3036 | return; | |
3037 | } | |
f554a14b | 3038 | |
aba9bfd0 | 3039 | wxDataViewModel *model = GetOwner()->GetModel(); |
0fdc2321 | 3040 | |
e21f75bd RR |
3041 | if (event.Dragging()) |
3042 | { | |
3043 | if (m_dragCount == 0) | |
3044 | { | |
3045 | // we have to report the raw, physical coords as we want to be | |
3046 | // able to call HitTest(event.m_pointDrag) from the user code to | |
3047 | // get the item being dragged | |
3048 | m_dragStart = event.GetPosition(); | |
3049 | } | |
3050 | ||
3051 | m_dragCount++; | |
3052 | ||
3053 | if (m_dragCount != 3) | |
3054 | return; | |
3055 | ||
3056 | if (event.LeftIsDown()) | |
3057 | { | |
3058 | // Notify cell about drag | |
3059 | } | |
3060 | return; | |
3061 | } | |
3062 | else | |
3063 | { | |
3064 | m_dragCount = 0; | |
3065 | } | |
3066 | ||
3067 | bool forceClick = false; | |
3068 | ||
0fcce6b9 RR |
3069 | if (event.ButtonDClick()) |
3070 | { | |
3071 | m_renameTimer->Stop(); | |
3072 | m_lastOnSame = false; | |
3073 | } | |
3074 | ||
0fdc2321 RR |
3075 | if (event.LeftDClick()) |
3076 | { | |
e21f75bd | 3077 | if ( current == m_lineLastClicked ) |
0fdc2321 | 3078 | { |
e21f75bd RR |
3079 | if (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE) |
3080 | { | |
6cdcbce3 | 3081 | wxDataViewItem item = GetItemByRow(current); |
e21f75bd | 3082 | wxVariant value; |
6cdcbce3 | 3083 | model->GetValue( value, item, col->GetModelColumn() ); |
e21f75bd | 3084 | cell->SetValue( value ); |
c741d33f | 3085 | wxRect cell_rect( xpos, current * m_lineHeight, |
87f0efe2 | 3086 | col->GetWidth(), m_lineHeight ); |
6cdcbce3 | 3087 | cell->Activate( cell_rect, model, item, col->GetModelColumn() ); |
e21f75bd RR |
3088 | } |
3089 | return; | |
3090 | } | |
3091 | else | |
3092 | { | |
3093 | // The first click was on another item, so don't interpret this as | |
3094 | // a double click, but as a simple click instead | |
3095 | forceClick = true; | |
0fdc2321 | 3096 | } |
120b9b05 | 3097 | } |
f554a14b | 3098 | |
0fcce6b9 RR |
3099 | if (event.LeftUp()) |
3100 | { | |
0a71f9e9 | 3101 | if (m_lineSelectSingleOnUp != (unsigned int)-1) |
e21f75bd RR |
3102 | { |
3103 | // select single line | |
3104 | SelectAllRows( false ); | |
3105 | SelectRow( m_lineSelectSingleOnUp, true ); | |
3106 | } | |
120b9b05 | 3107 | |
3b6280be | 3108 | //Process the event of user clicking the expander |
d5025dc0 RR |
3109 | bool expander = false; |
3110 | wxDataViewTreeNode * node = GetTreeNodeByRow(current); | |
3111 | if( node!=NULL && node->HasChildren() ) | |
3112 | { | |
3113 | int indent = node->GetIndentLevel(); | |
3114 | indent = GetOwner()->GetIndent()*indent; | |
3115 | wxRect rect( xpos + indent + EXPANDER_MARGIN, current * m_lineHeight + EXPANDER_MARGIN, m_lineHeight-2*EXPANDER_MARGIN,m_lineHeight-2*EXPANDER_MARGIN); | |
3116 | if( rect.Contains( x, y) ) | |
3117 | { | |
3118 | expander = true; | |
704c3490 RR |
3119 | if( node->IsOpen() ) |
3120 | OnCollapsing(current); | |
3121 | else | |
3122 | OnExpanding( current ); | |
d5025dc0 RR |
3123 | } |
3124 | } | |
3b6280be RR |
3125 | |
3126 | //If the user click the expander, we do not do editing even if the column with expander are editable | |
3127 | if (m_lastOnSame && !expander ) | |
0fcce6b9 | 3128 | { |
a8461d31 | 3129 | if ((col == m_currentCol) && (current == m_currentRow) && |
0fcce6b9 RR |
3130 | (cell->GetMode() == wxDATAVIEW_CELL_EDITABLE) ) |
3131 | { | |
3132 | m_renameTimer->Start( 100, true ); | |
3133 | } | |
3134 | } | |
3135 | ||
3136 | m_lastOnSame = false; | |
0a71f9e9 | 3137 | m_lineSelectSingleOnUp = (unsigned int)-1; |
120b9b05 | 3138 | } |
e21f75bd RR |
3139 | else |
3140 | { | |
3141 | // This is necessary, because after a DnD operation in | |
3142 | // from and to ourself, the up event is swallowed by the | |
3143 | // DnD code. So on next non-up event (which means here and | |
3144 | // now) m_lineSelectSingleOnUp should be reset. | |
0a71f9e9 | 3145 | m_lineSelectSingleOnUp = (unsigned int)-1; |
e21f75bd RR |
3146 | } |
3147 | ||
3148 | if (event.RightDown()) | |
3149 | { | |
3150 | m_lineBeforeLastClicked = m_lineLastClicked; | |
3151 | m_lineLastClicked = current; | |
3152 | ||
3153 | // If the item is already selected, do not update the selection. | |
3154 | // Multi-selections should not be cleared if a selected item is clicked. | |
3155 | if (!IsRowSelected(current)) | |
3156 | { | |
3157 | SelectAllRows(false); | |
3158 | ChangeCurrentRow(current); | |
3159 | SelectRow(m_currentRow,true); | |
3160 | } | |
3161 | ||
3162 | // notify cell about right click | |
3163 | // cell->... | |
120b9b05 | 3164 | |
e21f75bd RR |
3165 | // Allow generation of context menu event |
3166 | event.Skip(); | |
3167 | } | |
3168 | else if (event.MiddleDown()) | |
3169 | { | |
3170 | // notify cell about middle click | |
3171 | // cell->... | |
3172 | } | |
3173 | if (event.LeftDown() || forceClick) | |
0fcce6b9 | 3174 | { |
72664514 RR |
3175 | #ifdef __WXMSW__ |
3176 | SetFocus(); | |
3177 | #endif | |
120b9b05 | 3178 | |
e21f75bd RR |
3179 | m_lineBeforeLastClicked = m_lineLastClicked; |
3180 | m_lineLastClicked = current; | |
3181 | ||
0a71f9e9 | 3182 | unsigned int oldCurrentRow = m_currentRow; |
e21f75bd RR |
3183 | bool oldWasSelected = IsRowSelected(m_currentRow); |
3184 | ||
3185 | bool cmdModifierDown = event.CmdDown(); | |
3186 | if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) ) | |
3187 | { | |
3188 | if ( IsSingleSel() || !IsRowSelected(current) ) | |
3189 | { | |
3190 | SelectAllRows( false ); | |
3191 | ||
3192 | ChangeCurrentRow(current); | |
3193 | ||
3194 | SelectRow(m_currentRow,true); | |
3195 | } | |
3196 | else // multi sel & current is highlighted & no mod keys | |
3197 | { | |
3198 | m_lineSelectSingleOnUp = current; | |
3199 | ChangeCurrentRow(current); // change focus | |
3200 | } | |
3201 | } | |
3202 | else // multi sel & either ctrl or shift is down | |
3203 | { | |
3204 | if (cmdModifierDown) | |
3205 | { | |
3206 | ChangeCurrentRow(current); | |
3207 | ||
3208 | ReverseRowSelection(m_currentRow); | |
3209 | } | |
3210 | else if (event.ShiftDown()) | |
3211 | { | |
3212 | ChangeCurrentRow(current); | |
3213 | ||
0a71f9e9 | 3214 | unsigned int lineFrom = oldCurrentRow, |
e21f75bd RR |
3215 | lineTo = current; |
3216 | ||
3217 | if ( lineTo < lineFrom ) | |
3218 | { | |
3219 | lineTo = lineFrom; | |
3220 | lineFrom = m_currentRow; | |
3221 | } | |
3222 | ||
3223 | SelectRows(lineFrom, lineTo, true); | |
3224 | } | |
3225 | else // !ctrl, !shift | |
3226 | { | |
3227 | // test in the enclosing if should make it impossible | |
3228 | wxFAIL_MSG( _T("how did we get here?") ); | |
3229 | } | |
3230 | } | |
3231 | ||
72664514 RR |
3232 | if (m_currentRow != oldCurrentRow) |
3233 | RefreshRow( oldCurrentRow ); | |
e21f75bd | 3234 | |
0fcce6b9 | 3235 | wxDataViewColumn *oldCurrentCol = m_currentCol; |
120b9b05 | 3236 | |
0fcce6b9 RR |
3237 | // Update selection here... |
3238 | m_currentCol = col; | |
120b9b05 | 3239 | |
c741d33f | 3240 | m_lastOnSame = !forceClick && ((col == oldCurrentCol) && |
87f0efe2 | 3241 | (current == oldCurrentRow)) && oldWasSelected; |
0fdc2321 | 3242 | } |
4ed7af08 RR |
3243 | } |
3244 | ||
3245 | void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event ) | |
3246 | { | |
cab07038 | 3247 | m_hasFocus = true; |
120b9b05 | 3248 | |
cab07038 RR |
3249 | if (HasCurrentRow()) |
3250 | Refresh(); | |
120b9b05 | 3251 | |
cab07038 RR |
3252 | event.Skip(); |
3253 | } | |
3254 | ||
3255 | void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event ) | |
3256 | { | |
3257 | m_hasFocus = false; | |
120b9b05 | 3258 | |
cab07038 RR |
3259 | if (HasCurrentRow()) |
3260 | Refresh(); | |
120b9b05 | 3261 | |
4ed7af08 RR |
3262 | event.Skip(); |
3263 | } | |
3264 | ||
704c3490 RR |
3265 | wxDataViewItem wxDataViewMainWindow::GetSelection() |
3266 | { | |
3267 | if( m_selection.GetCount() != 1 ) | |
3268 | return wxDataViewItem(); | |
d47db7e0 RR |
3269 | |
3270 | return GetItemByRow( m_selection.Item(0)); | |
704c3490 RR |
3271 | } |
3272 | ||
4ed7af08 RR |
3273 | //----------------------------------------------------------------------------- |
3274 | // wxDataViewCtrl | |
3275 | //----------------------------------------------------------------------------- | |
3276 | ||
3277 | IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase) | |
3278 | ||
4b3feaa7 RR |
3279 | BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase) |
3280 | EVT_SIZE(wxDataViewCtrl::OnSize) | |
3281 | END_EVENT_TABLE() | |
3282 | ||
4ed7af08 RR |
3283 | wxDataViewCtrl::~wxDataViewCtrl() |
3284 | { | |
3285 | if (m_notifier) | |
3286 | GetModel()->RemoveNotifier( m_notifier ); | |
3287 | } | |
3288 | ||
3289 | void wxDataViewCtrl::Init() | |
3290 | { | |
3291 | m_notifier = NULL; | |
3292 | } | |
3293 | ||
3294 | bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id, | |
f554a14b | 3295 | const wxPoint& pos, const wxSize& size, |
4ed7af08 RR |
3296 | long style, const wxValidator& validator ) |
3297 | { | |
c741d33f | 3298 | if (!wxControl::Create( parent, id, pos, size, |
87f0efe2 | 3299 | style | wxScrolledWindowStyle|wxSUNKEN_BORDER, validator)) |
4b3feaa7 RR |
3300 | return false; |
3301 | ||
4ed7af08 | 3302 | Init(); |
f554a14b | 3303 | |
4ed7af08 RR |
3304 | #ifdef __WXMAC__ |
3305 | MacSetClipChildren( true ) ; | |
3306 | #endif | |
3307 | ||
f554a14b | 3308 | m_clientArea = new wxDataViewMainWindow( this, wxID_ANY ); |
9861f022 RR |
3309 | |
3310 | if (HasFlag(wxDV_NO_HEADER)) | |
3311 | m_headerArea = NULL; | |
3312 | else | |
3313 | m_headerArea = new wxDataViewHeaderWindow( this, wxID_ANY ); | |
f554a14b | 3314 | |
4ed7af08 | 3315 | SetTargetWindow( m_clientArea ); |
f554a14b | 3316 | |
4ed7af08 | 3317 | wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL ); |
9861f022 RR |
3318 | if (m_headerArea) |
3319 | sizer->Add( m_headerArea, 0, wxGROW ); | |
4ed7af08 RR |
3320 | sizer->Add( m_clientArea, 1, wxGROW ); |
3321 | SetSizer( sizer ); | |
3322 | ||
3323 | return true; | |
3324 | } | |
3325 | ||
3326 | #ifdef __WXMSW__ | |
3327 | WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg, | |
3328 | WXWPARAM wParam, | |
3329 | WXLPARAM lParam) | |
3330 | { | |
b910a8ad | 3331 | WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam); |
4ed7af08 RR |
3332 | |
3333 | #ifndef __WXWINCE__ | |
3334 | // we need to process arrows ourselves for scrolling | |
3335 | if ( nMsg == WM_GETDLGCODE ) | |
3336 | { | |
3337 | rc |= DLGC_WANTARROWS; | |
3338 | } | |
3339 | #endif | |
3340 | ||
3341 | return rc; | |
3342 | } | |
3343 | #endif | |
3344 | ||
f554a14b | 3345 | void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) ) |
4ed7af08 | 3346 | { |
4b3feaa7 RR |
3347 | // We need to override OnSize so that our scrolled |
3348 | // window a) does call Layout() to use sizers for | |
3349 | // positioning the controls but b) does not query | |
3350 | // the sizer for their size and use that for setting | |
3351 | // the scrollable area as set that ourselves by | |
3352 | // calling SetScrollbar() further down. | |
3353 | ||
3354 | Layout(); | |
3355 | ||
3356 | AdjustScrollbars(); | |
4ed7af08 RR |
3357 | } |
3358 | ||
aba9bfd0 | 3359 | bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model ) |
4ed7af08 RR |
3360 | { |
3361 | if (!wxDataViewCtrlBase::AssociateModel( model )) | |
3362 | return false; | |
3363 | ||
aba9bfd0 | 3364 | m_notifier = new wxGenericDataViewModelNotifier( m_clientArea ); |
4ed7af08 | 3365 | |
f554a14b | 3366 | model->AddNotifier( m_notifier ); |
4ed7af08 | 3367 | |
aba9bfd0 RR |
3368 | m_clientArea->BuildTree(model); |
3369 | ||
4b3feaa7 | 3370 | m_clientArea->UpdateDisplay(); |
f554a14b | 3371 | |
4ed7af08 RR |
3372 | return true; |
3373 | } | |
3374 | ||
3375 | bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col ) | |
3376 | { | |
3377 | if (!wxDataViewCtrlBase::AppendColumn(col)) | |
3378 | return false; | |
f554a14b | 3379 | |
9861f022 | 3380 | OnColumnChange(); |
4ed7af08 RR |
3381 | return true; |
3382 | } | |
3383 | ||
9861f022 RR |
3384 | void wxDataViewCtrl::OnColumnChange() |
3385 | { | |
3386 | if (m_headerArea) | |
3387 | m_headerArea->UpdateDisplay(); | |
3388 | ||
3389 | m_clientArea->UpdateDisplay(); | |
3390 | } | |
3b6280be RR |
3391 | |
3392 | void wxDataViewCtrl::DoSetExpanderColumn() | |
3393 | { | |
3394 | m_clientArea->UpdateDisplay(); | |
3395 | } | |
3396 | ||
3397 | void wxDataViewCtrl::DoSetIndent() | |
3398 | { | |
3399 | m_clientArea->UpdateDisplay(); | |
3400 | } | |
3401 | ||
442c56e6 | 3402 | wxDataViewItem wxDataViewCtrl::GetSelection() |
704c3490 RR |
3403 | { |
3404 | return m_clientArea->GetSelection(); | |
3405 | } | |
3b6280be | 3406 | |
aba9bfd0 | 3407 | /******************************************************************** |
87f0efe2 | 3408 | void wxDataViewCtrl::SetSelection( int row ) |
6ff7eee7 | 3409 | { |
87f0efe2 | 3410 | m_clientArea->SelectRow(row, true); |
6ff7eee7 RR |
3411 | } |
3412 | ||
87f0efe2 | 3413 | void wxDataViewCtrl::SetSelectionRange( unsigned int from, unsigned int to ) |
6ff7eee7 | 3414 | { |
87f0efe2 | 3415 | m_clientArea->SelectRows(from, to, true); |
6ff7eee7 RR |
3416 | } |
3417 | ||
87f0efe2 | 3418 | void wxDataViewCtrl::SetSelections( const wxArrayInt& aSelections ) |
6ff7eee7 | 3419 | { |
87f0efe2 | 3420 | m_clientArea->Select(aSelections); |
6ff7eee7 | 3421 | } |
df387169 WS |
3422 | |
3423 | void wxDataViewCtrl::Unselect( unsigned int WXUNUSED(row) ) | |
fc211fe5 | 3424 | { |
df387169 | 3425 | // FIXME - TODO |
fc211fe5 RR |
3426 | } |
3427 | ||
df387169 | 3428 | bool wxDataViewCtrl::IsSelected( unsigned int WXUNUSED(row) ) const |
6ff7eee7 | 3429 | { |
df387169 WS |
3430 | // FIXME - TODO |
3431 | ||
6ff7eee7 RR |
3432 | return false; |
3433 | } | |
3434 | ||
3435 | int wxDataViewCtrl::GetSelection() const | |
3436 | { | |
df387169 WS |
3437 | // FIXME - TODO |
3438 | ||
6ff7eee7 RR |
3439 | return -1; |
3440 | } | |
3441 | ||
df387169 | 3442 | int wxDataViewCtrl::GetSelections(wxArrayInt& WXUNUSED(aSelections) ) const |
6ff7eee7 | 3443 | { |
df387169 WS |
3444 | // FIXME - TODO |
3445 | ||
6ff7eee7 RR |
3446 | return 0; |
3447 | } | |
aba9bfd0 | 3448 | *********************************************************************/ |
f554a14b | 3449 | #endif |
4ed7af08 RR |
3450 | // !wxUSE_GENERICDATAVIEWCTRL |
3451 | ||
f554a14b | 3452 | #endif |
4ed7af08 | 3453 | // wxUSE_DATAVIEWCTRL |