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