]> git.saurik.com Git - wxWidgets.git/blame - src/generic/datavgen.cpp
fixing screen coordinate transformation
[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())
bf9ea288
RR
2426 {
2427 delete node;
739a8399 2428 return false;
bf9ea288 2429 }
739a8399
RR
2430
2431 return node->IsOpen();
2432}
2433
2434
3b6280be
RR
2435void wxDataViewMainWindow::OnExpanding( unsigned int row )
2436{
9657c197
RR
2437 if (IsVirtualList())
2438 return;
2439
3b6280be
RR
2440 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
2441 if( node != NULL )
2442 {
d5025dc0 2443 if( node->HasChildren())
66e09788 2444 {
d5025dc0 2445 if( !node->IsOpen())
3b6280be 2446 {
66e09788
RR
2447 wxDataViewEvent e = SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING,node->GetItem());
2448 //Check if the user prevent expanding
2449 if( e.GetSkipped() )
2450 return;
b7fe2261 2451
d5025dc0 2452 node->ToggleOpen();
704c3490
RR
2453 //Here I build the children of current node
2454 if( node->GetChildrenNumber() == 0 )
24c4a50f
RR
2455 {
2456 SortPrepare();
74123073 2457 ::BuildTreeHelper(GetOwner()->GetModel(), node->GetItem(), node);
24c4a50f 2458 }
d5025dc0 2459 m_count = -1;
351461fc 2460 UpdateDisplay();
66e09788
RR
2461 //Send the expanded event
2462 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED,node->GetItem());
351461fc
RR
2463 }
2464 else
2465 {
2466 SelectRow( row, false );
2467 SelectRow( row + 1, true );
2468 ChangeCurrentRow( row + 1 );
526e19e2 2469 SendSelectionChangedEvent( GetItemByRow(row+1));
3b6280be 2470 }
66e09788 2471 }
d47db7e0
RR
2472 else
2473 delete node;
3b6280be
RR
2474 }
2475}
2476
2477void wxDataViewMainWindow::OnCollapsing(unsigned int row)
2478{
9657c197
RR
2479 if (IsVirtualList())
2480 return;
e822d1bd 2481
3b6280be 2482 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
d5025dc0 2483 if( node != NULL )
3b6280be 2484 {
d47db7e0
RR
2485 wxDataViewTreeNode * nd = node;
2486
3b6280be
RR
2487 if( node->HasChildren() && node->IsOpen() )
2488 {
66e09788
RR
2489 wxDataViewEvent e = SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING,node->GetItem());
2490 if( e.GetSkipped() )
2491 return;
3b6280be
RR
2492 node->ToggleOpen();
2493 m_count = -1;
351461fc 2494 UpdateDisplay();
66e09788
RR
2495 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED,nd->GetItem());
2496 }
2497 else
2498 {
2499 node = node->GetParent();
2500 if( node != NULL )
2501 {
59e60167 2502 int parent = GetRowByItem( node->GetItem() );
66e09788
RR
2503 if( parent >= 0 )
2504 {
2505 SelectRow( row, false);
2506 SelectRow(parent , true );
2507 ChangeCurrentRow( parent );
526e19e2 2508 SendSelectionChangedEvent( node->GetItem() );
66e09788
RR
2509 }
2510 }
2511 }
2512 if( !nd->HasChildren())
2513 delete nd;
3b6280be
RR
2514 }
2515}
2516
351461fc
RR
2517wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item )
2518{
2519 wxDataViewModel * model = GetOwner()->GetModel();
2520 if( model == NULL )
2521 return NULL;
2522
2523 //Compose the a parent-chain of the finding item
2524 ItemList list;
2525 list.DeleteContents( true );
2526 wxDataViewItem it( item );
2527 while( it.IsOk() )
2528 {
2529 wxDataViewItem * pItem = new wxDataViewItem( it );
2530 list.Insert( pItem );
2531 it = model->GetParent( it );
2532 }
2533
442c56e6 2534 //Find the item along the parent-chain.
351461fc 2535 //This algorithm is designed to speed up the node-finding method
351461fc 2536 wxDataViewTreeNode * node = m_root;
59e60167 2537 for( ItemList::const_iterator iter = list.begin(); iter !=list.end(); iter++ )
351461fc
RR
2538 {
2539 if( node->HasChildren() )
2540 {
2541 if( node->GetChildrenNumber() == 0 )
24c4a50f
RR
2542 {
2543 SortPrepare();
74123073 2544 ::BuildTreeHelper(model, node->GetItem(), node);
24c4a50f 2545 }
351461fc 2546
d47db7e0 2547 wxDataViewTreeNodes nodes = node->GetNodes();
d2c1ee8a
RR
2548 unsigned int i;
2549 bool found = false;
777f9cef 2550
d2c1ee8a 2551 for (i = 0; i < nodes.GetCount(); i ++)
d92cb015 2552 {
c899416d 2553 if (nodes[i]->GetItem() == (**iter))
b7fe2261 2554 {
d2c1ee8a
RR
2555 if (nodes[i]->GetItem() == item)
2556 return nodes[i];
777f9cef 2557
d92cb015 2558 node = nodes[i];
d2c1ee8a 2559 found = true;
d92cb015
RR
2560 break;
2561 }
2562 }
d2c1ee8a 2563 if (!found)
351461fc 2564 return NULL;
351461fc
RR
2565 }
2566 else
2567 return NULL;
2568 }
d2c1ee8a 2569 return NULL;
351461fc
RR
2570}
2571
a87b466d 2572void wxDataViewMainWindow::HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column )
66e09788 2573{
fbda518c 2574 wxDataViewColumn *col = NULL;
66e09788
RR
2575 unsigned int cols = GetOwner()->GetColumnCount();
2576 unsigned int colnum = 0;
66e09788
RR
2577 int x, y;
2578 m_owner->CalcUnscrolledPosition( point.x, point.y, &x, &y );
e822d1bd 2579 for (unsigned x_start = 0; colnum < cols; colnum++)
66e09788 2580 {
702f5349 2581 col = GetOwner()->GetColumnAt(colnum);
66e09788
RR
2582 if (col->IsHidden())
2583 continue; // skip it!
2584
2585 unsigned int w = col->GetWidth();
2586 if (x_start+w >= (unsigned int)x)
2587 break;
2588
2589 x_start += w;
2590 }
2591
fbda518c 2592 column = col;
344ed1f3 2593 item = GetItemByRow( GetLineAt( y ) );
66e09788
RR
2594}
2595
fbda518c 2596wxRect wxDataViewMainWindow::GetItemRect( const wxDataViewItem & item, const wxDataViewColumn* column )
66e09788
RR
2597{
2598 int row = GetRowByItem(item);
344ed1f3
RR
2599 int y = GetLineStart( row );
2600 int h = GetLineHeight( m_lineHeight );
66e09788
RR
2601 int x = 0;
2602 wxDataViewColumn *col = NULL;
2603 for( int i = 0, cols = GetOwner()->GetColumnCount(); i < cols; i ++ )
2604 {
702f5349 2605 col = GetOwner()->GetColumnAt( i );
66e09788 2606 x += col->GetWidth();
702f5349 2607 if( GetOwner()->GetColumnAt(i+1) == column )
66e09788
RR
2608 break;
2609 }
2610 int w = col->GetWidth();
2611 m_owner->CalcScrolledPosition( x, y, &x, &y );
2612 return wxRect(x, y, w, h);
2613}
2614
442c56e6 2615int wxDataViewMainWindow::RecalculateCount()
3b6280be 2616{
a3cc79d9
RR
2617 if (!m_root)
2618 {
2619 wxDataViewIndexListModel *list_model = (wxDataViewIndexListModel*) GetOwner()->GetModel();
f52984b8
RR
2620#ifndef __WXMAC__
2621 return list_model->GetLastIndex() + 1;
2622#else
2623 return list_model->GetLastIndex() - 1;
2624#endif
a3cc79d9
RR
2625 }
2626 else
2627 {
2628 return m_root->GetSubTreeCount();
2629 }
3b6280be
RR
2630}
2631
aba9bfd0
RR
2632class ItemToRowJob : public DoJob
2633{
2634public:
59e60167
VZ
2635 ItemToRowJob(const wxDataViewItem& item_, ItemList::const_iterator iter)
2636 : m_iter(iter),
2637 item(item_)
2638 {
2639 ret = -1;
2640 }
aba9bfd0 2641
b7e9f8b1 2642 //Maybe binary search will help to speed up this process
d5025dc0
RR
2643 virtual int operator() ( wxDataViewTreeNode * node)
2644 {
2645 ret ++;
2646 if( node->GetItem() == item )
d47db7e0 2647 {
d5025dc0 2648 return DoJob::OK;
d47db7e0 2649 }
d5025dc0 2650
c899416d 2651 if( node->GetItem() == **m_iter )
d47db7e0 2652 {
59e60167 2653 m_iter++;
d5025dc0 2654 return DoJob::CONT;
d47db7e0 2655 }
d5025dc0 2656 else
d47db7e0
RR
2657 {
2658 ret += node->GetSubTreeCount();
d5025dc0 2659 return DoJob::IGR;
d47db7e0 2660 }
442c56e6 2661
d5025dc0 2662 }
aba9bfd0 2663
d47db7e0
RR
2664 virtual int operator() ( void * n )
2665 {
2666 ret ++;
2667 if( n == item.GetID() )
2668 return DoJob::OK;
2669 return DoJob::CONT;
2670 }
3b6280be 2671 //the row number is begin from zero
59e60167
VZ
2672 int GetResult() { return ret -1; }
2673
aba9bfd0 2674private:
c899416d 2675 ItemList::const_iterator m_iter;
aba9bfd0
RR
2676 wxDataViewItem item;
2677 int ret;
442c56e6 2678
aba9bfd0
RR
2679};
2680
344ed1f3 2681int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item) const
aba9bfd0 2682{
344ed1f3 2683 const wxDataViewModel * model = GetOwner()->GetModel();
d47db7e0 2684 if( model == NULL )
fbda518c 2685 return -1;
d47db7e0 2686
a3cc79d9 2687 if (!m_root)
d47db7e0 2688 {
a3cc79d9 2689 return wxPtrToUInt( item.GetID() );
d47db7e0 2690 }
a3cc79d9
RR
2691 else
2692 {
2693 if( !item.IsOk() )
2694 return -1;
2695
2696 //Compose the a parent-chain of the finding item
2697 ItemList list;
e822d1bd 2698 wxDataViewItem * pItem;
a3cc79d9
RR
2699 list.DeleteContents( true );
2700 wxDataViewItem it( item );
2701 while( it.IsOk() )
2702 {
2703 pItem = new wxDataViewItem( it );
2704 list.Insert( pItem );
2705 it = model->GetParent( it );
2706 }
2707 pItem = new wxDataViewItem( );
2708 list.Insert( pItem );
d47db7e0 2709
a3cc79d9
RR
2710 ItemToRowJob job( item, list.begin() );
2711 Walker(m_root , job );
2712 return job.GetResult();
2713 }
aba9bfd0
RR
2714}
2715
74123073 2716static void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, wxDataViewTreeNode * node)
aba9bfd0 2717{
351461fc 2718 if( !model->IsContainer( item ) )
59e60167 2719 return;
442c56e6 2720
c899416d
RR
2721 wxDataViewItemArray children;
2722 unsigned int num = model->GetChildren( item, children);
67be459b 2723 unsigned int index = 0;
c899416d 2724 while( index < num )
aba9bfd0 2725 {
c899416d 2726 if( model->IsContainer( children[index] ) )
d47db7e0
RR
2727 {
2728 wxDataViewTreeNode * n = new wxDataViewTreeNode( node );
c899416d 2729 n->SetItem(children[index]);
59e60167 2730 n->SetHasChildren( true );
d47db7e0
RR
2731 node->AddNode( n );
2732 }
2733 else
2734 {
c899416d 2735 node->AddLeaf( children[index].GetID() );
d47db7e0 2736 }
c899416d 2737 index ++;
aba9bfd0 2738 }
d47db7e0
RR
2739 node->SetSubTreeCount( num );
2740 wxDataViewTreeNode * n = node->GetParent();
2741 if( n != NULL)
2742 n->ChangeSubTreeCount(num);
2743
aba9bfd0
RR
2744}
2745
2746void wxDataViewMainWindow::BuildTree(wxDataViewModel * model)
2747{
51bdecff
RR
2748 DestroyTree();
2749
e39de702 2750 if (GetOwner()->GetModel()->IsVirtualListModel())
a3cc79d9 2751 {
59e60167 2752 m_count = -1;
a3cc79d9
RR
2753 return;
2754 }
2755
51bdecff
RR
2756 m_root = new wxDataViewTreeNode( NULL );
2757 m_root->SetHasChildren(true);
2758
aba9bfd0
RR
2759 //First we define a invalid item to fetch the top-level elements
2760 wxDataViewItem item;
66e09788 2761 SortPrepare();
3b6280be 2762 BuildTreeHelper( model, item, m_root);
59e60167 2763 m_count = -1;
aba9bfd0
RR
2764}
2765
74123073 2766static void DestroyTreeHelper( wxDataViewTreeNode * node )
aba9bfd0 2767{
d47db7e0 2768 if( node->GetNodeNumber() != 0 )
aba9bfd0 2769 {
d47db7e0 2770 int len = node->GetNodeNumber();
59e60167 2771 int i = 0;
74123073 2772 wxDataViewTreeNodes& nodes = node->GetNodes();
59e60167 2773 for(; i < len; i ++ )
aba9bfd0
RR
2774 {
2775 DestroyTreeHelper(nodes[i]);
2776 }
2777 }
2778 delete node;
2779}
2780
2781void wxDataViewMainWindow::DestroyTree()
2782{
344ed1f3 2783 if (!IsVirtualList())
51bdecff 2784 {
74123073 2785 ::DestroyTreeHelper(m_root);
51bdecff
RR
2786 m_count = 0;
2787 m_root = NULL;
2788 }
aba9bfd0
RR
2789}
2790
cab07038
RR
2791void wxDataViewMainWindow::OnChar( wxKeyEvent &event )
2792{
788432e3 2793 if ( GetParent()->HandleAsNavigationKey(event) )
f029f1d1 2794 return;
cab07038
RR
2795
2796 // no item -> nothing to do
2797 if (!HasCurrentRow())
2798 {
2799 event.Skip();
2800 return;
2801 }
120b9b05 2802
cab07038
RR
2803 // don't use m_linesPerPage directly as it might not be computed yet
2804 const int pageSize = GetCountPerPage();
2805 wxCHECK_RET( pageSize, _T("should have non zero page size") );
2806
2807 switch ( event.GetKeyCode() )
2808 {
d37b709c
RR
2809 case WXK_RETURN:
2810 {
74a98b99 2811 if (m_currentRow >= 0)
d37b709c
RR
2812 {
2813 wxWindow *parent = GetParent();
2814 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, parent->GetId());
2815 le.SetItem( GetItemByRow(m_currentRow) );
2816 le.SetEventObject(parent);
2817 le.SetModel(GetOwner()->GetModel());
2818
2819 parent->GetEventHandler()->ProcessEvent(le);
2820 }
2821 break;
2822 }
cab07038
RR
2823 case WXK_UP:
2824 if ( m_currentRow > 0 )
2825 OnArrowChar( m_currentRow - 1, event );
2826 break;
2827
2828 case WXK_DOWN:
4a851b11 2829 if ( m_currentRow < GetRowCount() - 1 )
cab07038
RR
2830 OnArrowChar( m_currentRow + 1, event );
2831 break;
3b6280be
RR
2832 //Add the process for tree expanding/collapsing
2833 case WXK_LEFT:
9bcc8016
JS
2834 OnCollapsing(m_currentRow);
2835 break;
2836 case WXK_RIGHT:
2837 OnExpanding( m_currentRow);
2838 break;
cab07038
RR
2839 case WXK_END:
2840 if (!IsEmpty())
2841 OnArrowChar( GetRowCount() - 1, event );
2842 break;
2843
2844 case WXK_HOME:
2845 if (!IsEmpty())
2846 OnArrowChar( 0, event );
2847 break;
2848
2849 case WXK_PAGEUP:
2850 {
2851 int steps = pageSize - 1;
2852 int index = m_currentRow - steps;
2853 if (index < 0)
2854 index = 0;
2855
2856 OnArrowChar( index, event );
2857 }
2858 break;
2859
2860 case WXK_PAGEDOWN:
2861 {
2862 int steps = pageSize - 1;
0a71f9e9
RR
2863 unsigned int index = m_currentRow + steps;
2864 unsigned int count = GetRowCount();
cab07038
RR
2865 if ( index >= count )
2866 index = count - 1;
2867
2868 OnArrowChar( index, event );
2869 }
2870 break;
2871
2872 default:
2873 event.Skip();
2874 }
2875}
2876
4ed7af08
RR
2877void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
2878{
e21f75bd
RR
2879 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
2880 {
2881 // let the base handle mouse wheel events.
2882 event.Skip();
2883 return;
2884 }
2885
0fdc2321
RR
2886 int x = event.GetX();
2887 int y = event.GetY();
2888 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
0fdc2321 2889 wxDataViewColumn *col = NULL;
b7fe2261 2890
0fdc2321 2891 int xpos = 0;
9861f022 2892 unsigned int cols = GetOwner()->GetColumnCount();
0a71f9e9 2893 unsigned int i;
0fdc2321
RR
2894 for (i = 0; i < cols; i++)
2895 {
702f5349 2896 wxDataViewColumn *c = GetOwner()->GetColumnAt( i );
9861f022
RR
2897 if (c->IsHidden())
2898 continue; // skip it!
2899
0fdc2321
RR
2900 if (x < xpos + c->GetWidth())
2901 {
2902 col = c;
2903 break;
2904 }
2905 xpos += c->GetWidth();
2906 }
f554a14b 2907 if (!col)
0fdc2321 2908 return;
f554a14b 2909
24c4a50f 2910 wxDataViewRenderer *cell = col->GetRenderer();
344ed1f3 2911 unsigned int current = GetLineAt( y );
5179bc0b 2912 if ((current >= GetRowCount()) || (x > GetEndOfLastCol()))
e21f75bd
RR
2913 {
2914 // Unselect all if below the last row ?
2915 return;
2916 }
f554a14b 2917
24c4a50f
RR
2918 //Test whether the mouse is hovered on the tree item button
2919 bool hover = false;
344ed1f3 2920 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col))
24c4a50f
RR
2921 {
2922 wxDataViewTreeNode * node = GetTreeNodeByRow(current);
2923 if( node!=NULL && node->HasChildren() )
2924 {
2925 int indent = node->GetIndentLevel();
2926 indent = GetOwner()->GetIndent()*indent;
777f9cef 2927 wxRect rect( xpos + indent + EXPANDER_MARGIN,
344ed1f3
RR
2928 GetLineStart( current ) + EXPANDER_MARGIN + (GetLineHeight(current)/2) - (m_lineHeight/2) - EXPANDER_OFFSET,
2929 m_lineHeight-2*EXPANDER_MARGIN,
981cd83e 2930 m_lineHeight-2*EXPANDER_MARGIN + EXPANDER_OFFSET);
24c4a50f
RR
2931 if( rect.Contains( x, y) )
2932 {
2933 //So the mouse is over the expander
2934 hover = true;
2935 if (m_underMouse && m_underMouse != node)
2936 {
2937 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
df2c23e7 2938 RefreshRow(GetRowByItem(m_underMouse->GetItem()));
24c4a50f
RR
2939 }
2940 if (m_underMouse != node)
2941 {
2942 //wxLogMessage("Do the row: %d", current);
df2c23e7 2943 RefreshRow(current);
24c4a50f
RR
2944 }
2945 m_underMouse = node;
2946 }
2947 }
438fb233 2948 if (node!=NULL && !node->HasChildren())
24c4a50f
RR
2949 delete node;
2950 }
2951 if (!hover)
2952 {
2953 if (m_underMouse != NULL)
2954 {
526e19e2 2955 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
df2c23e7 2956 RefreshRow(GetRowByItem(m_underMouse->GetItem()));
24c4a50f
RR
2957 m_underMouse = NULL;
2958 }
2959 }
2960
aba9bfd0 2961 wxDataViewModel *model = GetOwner()->GetModel();
0fdc2321 2962
e21f75bd
RR
2963 if (event.Dragging())
2964 {
2965 if (m_dragCount == 0)
2966 {
2967 // we have to report the raw, physical coords as we want to be
2968 // able to call HitTest(event.m_pointDrag) from the user code to
2969 // get the item being dragged
2970 m_dragStart = event.GetPosition();
2971 }
2972
2973 m_dragCount++;
2974
2975 if (m_dragCount != 3)
2976 return;
2977
2978 if (event.LeftIsDown())
2979 {
2980 // Notify cell about drag
2981 }
2982 return;
2983 }
2984 else
2985 {
2986 m_dragCount = 0;
2987 }
2988
2989 bool forceClick = false;
2990
0fcce6b9
RR
2991 if (event.ButtonDClick())
2992 {
2993 m_renameTimer->Stop();
2994 m_lastOnSame = false;
2995 }
2996
438fb233
RR
2997 wxDataViewItem item = GetItemByRow(current);
2998 bool ignore_other_columns =
2999 ((GetOwner()->GetExpanderColumn() != col) &&
3000 (model->IsContainer(item)) &&
3001 (!model->HasContainerColumns(item)));
b5ec7dd6 3002
0fdc2321
RR
3003 if (event.LeftDClick())
3004 {
e21f75bd 3005 if ( current == m_lineLastClicked )
0fdc2321 3006 {
438fb233 3007 if ((!ignore_other_columns) && (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE))
e21f75bd
RR
3008 {
3009 wxVariant value;
6cdcbce3 3010 model->GetValue( value, item, col->GetModelColumn() );
e21f75bd 3011 cell->SetValue( value );
344ed1f3
RR
3012 wxRect cell_rect( xpos, GetLineStart( current ),
3013 col->GetWidth(), GetLineHeight( current ) );
6cdcbce3 3014 cell->Activate( cell_rect, model, item, col->GetModelColumn() );
b7e9f8b1 3015
2c3f6edd
RR
3016 }
3017 else
3018 {
b7e9f8b1
RR
3019 wxWindow *parent = GetParent();
3020 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, parent->GetId());
45c156e0 3021 le.SetItem( item );
b7e9f8b1 3022 le.SetEventObject(parent);
b7e9f8b1
RR
3023 le.SetModel(GetOwner()->GetModel());
3024
3025 parent->GetEventHandler()->ProcessEvent(le);
e21f75bd
RR
3026 }
3027 return;
3028 }
3029 else
3030 {
3031 // The first click was on another item, so don't interpret this as
3032 // a double click, but as a simple click instead
3033 forceClick = true;
0fdc2321 3034 }
120b9b05 3035 }
f554a14b 3036
0fcce6b9
RR
3037 if (event.LeftUp())
3038 {
0a71f9e9 3039 if (m_lineSelectSingleOnUp != (unsigned int)-1)
e21f75bd
RR
3040 {
3041 // select single line
3042 SelectAllRows( false );
3043 SelectRow( m_lineSelectSingleOnUp, true );
4a745e76 3044 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp) );
e21f75bd 3045 }
120b9b05 3046
3b6280be 3047 //Process the event of user clicking the expander
d5025dc0 3048 bool expander = false;
344ed1f3 3049 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col))
d5025dc0 3050 {
9bcc8016
JS
3051 wxDataViewTreeNode * node = GetTreeNodeByRow(current);
3052 if( node!=NULL && node->HasChildren() )
3053 {
3054 int indent = node->GetIndentLevel();
3055 indent = GetOwner()->GetIndent()*indent;
777f9cef 3056 wxRect rect( xpos + indent + EXPANDER_MARGIN,
344ed1f3
RR
3057 GetLineStart( current ) + EXPANDER_MARGIN + (GetLineHeight(current)/2) - (m_lineHeight/2) - EXPANDER_OFFSET,
3058 m_lineHeight-2*EXPANDER_MARGIN,
981cd83e 3059 m_lineHeight-2*EXPANDER_MARGIN + EXPANDER_OFFSET);
777f9cef 3060
9bcc8016
JS
3061 if( rect.Contains( x, y) )
3062 {
3063 expander = true;
3064 if( node->IsOpen() )
3065 OnCollapsing(current);
3066 else
3067 OnExpanding( current );
3068 }
3069 }
74123073
RR
3070 if (node && !node->HasChildren())
3071 delete node;
d5025dc0 3072 }
3b6280be 3073 //If the user click the expander, we do not do editing even if the column with expander are editable
438fb233 3074 if (m_lastOnSame && !expander && !ignore_other_columns)
0fcce6b9 3075 {
a8461d31 3076 if ((col == m_currentCol) && (current == m_currentRow) &&
0bdfa388 3077 (cell->GetMode() & wxDATAVIEW_CELL_EDITABLE) )
0fcce6b9
RR
3078 {
3079 m_renameTimer->Start( 100, true );
3080 }
3081 }
3082
3083 m_lastOnSame = false;
0a71f9e9 3084 m_lineSelectSingleOnUp = (unsigned int)-1;
120b9b05 3085 }
e21f75bd
RR
3086 else
3087 {
3088 // This is necessary, because after a DnD operation in
3089 // from and to ourself, the up event is swallowed by the
3090 // DnD code. So on next non-up event (which means here and
3091 // now) m_lineSelectSingleOnUp should be reset.
0a71f9e9 3092 m_lineSelectSingleOnUp = (unsigned int)-1;
e21f75bd
RR
3093 }
3094
3095 if (event.RightDown())
3096 {
3097 m_lineBeforeLastClicked = m_lineLastClicked;
3098 m_lineLastClicked = current;
3099
3100 // If the item is already selected, do not update the selection.
3101 // Multi-selections should not be cleared if a selected item is clicked.
3102 if (!IsRowSelected(current))
3103 {
3104 SelectAllRows(false);
3105 ChangeCurrentRow(current);
3106 SelectRow(m_currentRow,true);
526e19e2 3107 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
e21f75bd
RR
3108 }
3109
f210b1b5
RR
3110 wxVariant value;
3111 model->GetValue( value, item, col->GetModelColumn() );
0bdfa388
RR
3112 wxWindow *parent = GetParent();
3113 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, parent->GetId());
3114 le.SetItem( item );
3115 le.SetEventObject(parent);
3116 le.SetModel(GetOwner()->GetModel());
3117 le.SetValue(value);
3118 parent->GetEventHandler()->ProcessEvent(le);
e21f75bd
RR
3119 }
3120 else if (event.MiddleDown())
3121 {
e21f75bd
RR
3122 }
3123 if (event.LeftDown() || forceClick)
0fcce6b9 3124 {
72664514 3125 SetFocus();
120b9b05 3126
e21f75bd
RR
3127 m_lineBeforeLastClicked = m_lineLastClicked;
3128 m_lineLastClicked = current;
3129
0a71f9e9 3130 unsigned int oldCurrentRow = m_currentRow;
e21f75bd
RR
3131 bool oldWasSelected = IsRowSelected(m_currentRow);
3132
3133 bool cmdModifierDown = event.CmdDown();
3134 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
3135 {
3136 if ( IsSingleSel() || !IsRowSelected(current) )
3137 {
3138 SelectAllRows( false );
e21f75bd 3139 ChangeCurrentRow(current);
e21f75bd 3140 SelectRow(m_currentRow,true);
526e19e2 3141 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
e21f75bd
RR
3142 }
3143 else // multi sel & current is highlighted & no mod keys
3144 {
3145 m_lineSelectSingleOnUp = current;
3146 ChangeCurrentRow(current); // change focus
3147 }
3148 }
3149 else // multi sel & either ctrl or shift is down
3150 {
3151 if (cmdModifierDown)
3152 {
3153 ChangeCurrentRow(current);
e21f75bd 3154 ReverseRowSelection(m_currentRow);
526e19e2 3155 SendSelectionChangedEvent(GetItemByRow(m_selection[0]) );
e21f75bd
RR
3156 }
3157 else if (event.ShiftDown())
3158 {
3159 ChangeCurrentRow(current);
3160
0a71f9e9 3161 unsigned int lineFrom = oldCurrentRow,
e21f75bd
RR
3162 lineTo = current;
3163
3164 if ( lineTo < lineFrom )
3165 {
3166 lineTo = lineFrom;
3167 lineFrom = m_currentRow;
3168 }
3169
3170 SelectRows(lineFrom, lineTo, true);
526e19e2 3171 SendSelectionChangedEvent(GetItemByRow(m_selection[0]) );
e21f75bd
RR
3172 }
3173 else // !ctrl, !shift
3174 {
3175 // test in the enclosing if should make it impossible
3176 wxFAIL_MSG( _T("how did we get here?") );
3177 }
3178 }
777f9cef 3179
72664514
RR
3180 if (m_currentRow != oldCurrentRow)
3181 RefreshRow( oldCurrentRow );
e21f75bd 3182
0fcce6b9 3183 wxDataViewColumn *oldCurrentCol = m_currentCol;
120b9b05 3184
0fcce6b9
RR
3185 // Update selection here...
3186 m_currentCol = col;
120b9b05 3187
c741d33f 3188 m_lastOnSame = !forceClick && ((col == oldCurrentCol) &&
87f0efe2 3189 (current == oldCurrentRow)) && oldWasSelected;
0bdfa388
RR
3190
3191 // Call LeftClick after everything else as under GTK+
3192 if (cell->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE)
3193 {
3194 // notify cell about right click
3195 wxVariant value;
3196 model->GetValue( value, item, col->GetModelColumn() );
3197 cell->SetValue( value );
344ed1f3
RR
3198 wxRect cell_rect( xpos, GetLineStart( current ),
3199 col->GetWidth(), GetLineHeight( current ) );
0bdfa388
RR
3200 /* ignore ret */ cell->LeftClick( event.GetPosition(), cell_rect, model, item, col->GetModelColumn());
3201 }
0fdc2321 3202 }
4ed7af08
RR
3203}
3204
3205void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event )
3206{
cab07038 3207 m_hasFocus = true;
120b9b05 3208
cab07038
RR
3209 if (HasCurrentRow())
3210 Refresh();
120b9b05 3211
cab07038
RR
3212 event.Skip();
3213}
3214
3215void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event )
3216{
3217 m_hasFocus = false;
120b9b05 3218
cab07038
RR
3219 if (HasCurrentRow())
3220 Refresh();
120b9b05 3221
4ed7af08
RR
3222 event.Skip();
3223}
3224
fbda518c 3225wxDataViewItem wxDataViewMainWindow::GetSelection() const
704c3490
RR
3226{
3227 if( m_selection.GetCount() != 1 )
3228 return wxDataViewItem();
d47db7e0
RR
3229
3230 return GetItemByRow( m_selection.Item(0));
704c3490
RR
3231}
3232
4ed7af08
RR
3233//-----------------------------------------------------------------------------
3234// wxDataViewCtrl
3235//-----------------------------------------------------------------------------
a76c2f37 3236WX_DEFINE_LIST(wxDataViewColumnList)
4ed7af08
RR
3237
3238IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
3239
4b3feaa7
RR
3240BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase)
3241 EVT_SIZE(wxDataViewCtrl::OnSize)
3242END_EVENT_TABLE()
3243
4ed7af08
RR
3244wxDataViewCtrl::~wxDataViewCtrl()
3245{
3246 if (m_notifier)
3247 GetModel()->RemoveNotifier( m_notifier );
74123073 3248
b3a3c9d8 3249 m_cols.Clear();
4ed7af08
RR
3250}
3251
3252void wxDataViewCtrl::Init()
3253{
b3a3c9d8 3254 m_cols.DeleteContents(true);
4ed7af08 3255 m_notifier = NULL;
e822d1bd 3256
236a34ef 3257 // No sorting column at start
46234a03
VZ
3258 m_sortingColumnIdx = wxNOT_FOUND;
3259
236a34ef 3260 m_headerArea = NULL;
4ed7af08
RR
3261}
3262
3263bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
f554a14b 3264 const wxPoint& pos, const wxSize& size,
4ed7af08
RR
3265 long style, const wxValidator& validator )
3266{
008e999e
RD
3267 if ( (style & wxBORDER_MASK) == 0)
3268 style |= wxBORDER_SUNKEN;
777f9cef 3269
236a34ef
RR
3270 Init();
3271
c741d33f 3272 if (!wxControl::Create( parent, id, pos, size,
008e999e 3273 style | wxScrolledWindowStyle, validator))
4b3feaa7
RR
3274 return false;
3275
b89cac3f 3276 SetInitialSize(size);
b5ec7dd6 3277
4ed7af08 3278#ifdef __WXMAC__
59e60167 3279 MacSetClipChildren( true );
4ed7af08
RR
3280#endif
3281
f554a14b 3282 m_clientArea = new wxDataViewMainWindow( this, wxID_ANY );
9861f022
RR
3283
3284 if (HasFlag(wxDV_NO_HEADER))
3285 m_headerArea = NULL;
3286 else
56873923 3287 m_headerArea = new wxDataViewHeaderWindow(this);
f554a14b 3288
4ed7af08 3289 SetTargetWindow( m_clientArea );
f554a14b 3290
4ed7af08 3291 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
9861f022
RR
3292 if (m_headerArea)
3293 sizer->Add( m_headerArea, 0, wxGROW );
4ed7af08
RR
3294 sizer->Add( m_clientArea, 1, wxGROW );
3295 SetSizer( sizer );
b5ec7dd6 3296
4ed7af08
RR
3297 return true;
3298}
3299
3300#ifdef __WXMSW__
3301WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg,
3302 WXWPARAM wParam,
3303 WXLPARAM lParam)
3304{
b910a8ad 3305 WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
4ed7af08
RR
3306
3307#ifndef __WXWINCE__
3308 // we need to process arrows ourselves for scrolling
3309 if ( nMsg == WM_GETDLGCODE )
3310 {
3311 rc |= DLGC_WANTARROWS;
3312 }
3313#endif
3314
3315 return rc;
3316}
3317#endif
3318
2571a33f
RR
3319wxSize wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize& size)
3320{
3321 wxSize newsize = size;
d7cda9b2 3322 if (!HasFlag(wxDV_NO_HEADER) && (m_headerArea))
2571a33f 3323 newsize.y -= m_headerArea->GetSize().y;
e822d1bd 3324
2571a33f
RR
3325 return newsize;
3326}
3327
f554a14b 3328void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
4ed7af08 3329{
4b3feaa7
RR
3330 // We need to override OnSize so that our scrolled
3331 // window a) does call Layout() to use sizers for
3332 // positioning the controls but b) does not query
3333 // the sizer for their size and use that for setting
3334 // the scrollable area as set that ourselves by
3335 // calling SetScrollbar() further down.
3336
3337 Layout();
3338
3339 AdjustScrollbars();
4ed7af08
RR
3340}
3341
788432e3
RR
3342void wxDataViewCtrl::SetFocus()
3343{
3344 if (m_clientArea)
3345 m_clientArea->SetFocus();
3346}
3347
aba9bfd0 3348bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
4ed7af08
RR
3349{
3350 if (!wxDataViewCtrlBase::AssociateModel( model ))
3351 return false;
3352
aba9bfd0 3353 m_notifier = new wxGenericDataViewModelNotifier( m_clientArea );
4ed7af08 3354
f554a14b 3355 model->AddNotifier( m_notifier );
4ed7af08 3356
51bdecff 3357 m_clientArea->DestroyTree();
cfa42cb8 3358
aba9bfd0
RR
3359 m_clientArea->BuildTree(model);
3360
4b3feaa7 3361 m_clientArea->UpdateDisplay();
f554a14b 3362
4ed7af08
RR
3363 return true;
3364}
3365
3366bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
3367{
3368 if (!wxDataViewCtrlBase::AppendColumn(col))
3369 return false;
f554a14b 3370
afebb87b 3371 m_cols.Append( col );
aef252d9 3372 OnColumnsCountChanged();
736fe67c
RR
3373 return true;
3374}
3375
3376bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
3377{
3378 if (!wxDataViewCtrlBase::PrependColumn(col))
3379 return false;
3380
3381 m_cols.Insert( col );
aef252d9 3382 OnColumnsCountChanged();
4ed7af08
RR
3383 return true;
3384}
3385
19723525
RR
3386bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
3387{
3388 if (!wxDataViewCtrlBase::InsertColumn(pos,col))
3389 return false;
3390
3391 m_cols.Insert( pos, col );
aef252d9 3392 OnColumnsCountChanged();
19723525
RR
3393 return true;
3394}
3395
aef252d9
VZ
3396void wxDataViewCtrl::OnColumnChange(unsigned int idx)
3397{
3398 if ( m_headerArea )
3399 m_headerArea->UpdateColumn(idx);
3400
3401 m_clientArea->UpdateDisplay();
3402}
3403
3404void wxDataViewCtrl::OnColumnsCountChanged()
9861f022
RR
3405{
3406 if (m_headerArea)
e2bfe673 3407 m_headerArea->SetColumnCount(GetColumnCount());
9861f022
RR
3408
3409 m_clientArea->UpdateDisplay();
3410}
3b6280be
RR
3411
3412void wxDataViewCtrl::DoSetExpanderColumn()
3413{
3414 m_clientArea->UpdateDisplay();
3415}
3416
3417void wxDataViewCtrl::DoSetIndent()
3418{
3419 m_clientArea->UpdateDisplay();
3420}
3421
afebb87b
RR
3422unsigned int wxDataViewCtrl::GetColumnCount() const
3423{
3424 return m_cols.GetCount();
3425}
3426
aef252d9 3427wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int idx ) const
afebb87b 3428{
aef252d9 3429 return m_cols[idx];
afebb87b
RR
3430}
3431
702f5349 3432wxDataViewColumn *wxDataViewCtrl::GetColumnAt(unsigned int pos) const
fc8c1a15 3433{
702f5349
VZ
3434 // columns can't be reordered if there is no header window which allows
3435 // to do this
3436 const unsigned idx = m_headerArea ? m_headerArea->GetColumnsOrder()[pos]
3437 : pos;
59e60167 3438
702f5349
VZ
3439 return GetColumn(idx);
3440}
cfa42cb8 3441
702f5349
VZ
3442int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn *column) const
3443{
3444 const unsigned count = m_cols.size();
3445 for ( unsigned n = 0; n < count; n++ )
3446 {
3447 if ( m_cols[n] == column )
3448 return n;
3449 }
3450
3451 return wxNOT_FOUND;
3452}
3453
3454void wxDataViewCtrl::ColumnMoved(wxDataViewColumn * WXUNUSED(col),
3455 unsigned int WXUNUSED(new_pos))
3456{
3457 // do _not_ reorder m_cols elements here, they should always be in the
3458 // order in which columns were added, we only display the columns in
3459 // different order
fc8c1a15
RR
3460 m_clientArea->UpdateDisplay();
3461}
3462
afebb87b
RR
3463bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
3464{
b3a3c9d8 3465 wxDataViewColumnList::compatibility_iterator ret = m_cols.Find( column );
4264606e 3466 if (!ret)
afebb87b
RR
3467 return false;
3468
b7fe2261 3469 m_cols.Erase(ret);
aef252d9 3470 OnColumnsCountChanged();
afebb87b
RR
3471
3472 return true;
3473}
3474
3475bool wxDataViewCtrl::ClearColumns()
3476{
b3a3c9d8 3477 m_cols.Clear();
aef252d9 3478 OnColumnsCountChanged();
afebb87b
RR
3479 return true;
3480}
3481
453091c2
RR
3482int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
3483{
702f5349
VZ
3484 int ret = 0,
3485 dummy = 0;
3486 unsigned int len = GetColumnCount();
3487 for ( unsigned int i = 0; i < len; i++ )
526e19e2 3488 {
702f5349 3489 wxDataViewColumn * col = GetColumnAt(i);
526e19e2
RR
3490 if (col->IsHidden())
3491 continue;
3492 ret += col->GetWidth();
3493 if (column==col)
3494 {
702f5349 3495 CalcScrolledPosition( ret, dummy, &ret, &dummy );
526e19e2
RR
3496 break;
3497 }
3498 }
3499 return ret;
453091c2
RR
3500}
3501
21f47fb9
RR
3502wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
3503{
46234a03
VZ
3504 return m_sortingColumnIdx == wxNOT_FOUND ? NULL
3505 : GetColumn(m_sortingColumnIdx);
21f47fb9
RR
3506}
3507
b7e9f8b1 3508//Selection code with wxDataViewItem as parameters
fbda518c 3509wxDataViewItem wxDataViewCtrl::GetSelection() const
66e09788
RR
3510{
3511 return m_clientArea->GetSelection();
3512}
3513
b7e9f8b1
RR
3514int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
3515{
3516 sel.Empty();
3517 wxDataViewSelection selection = m_clientArea->GetSelections();
3518 int len = selection.GetCount();
3519 for( int i = 0; i < len; i ++)
3520 {
3521 unsigned int row = selection[i];
3522 sel.Add( m_clientArea->GetItemByRow( row ) );
3523 }
3524 return len;
3525}
3526
3527void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
3528{
59e60167 3529 wxDataViewSelection selection(wxDataViewSelectionCmp);
4219d8b0
RR
3530
3531 wxDataViewItem last_parent;
3532
b7e9f8b1
RR
3533 int len = sel.GetCount();
3534 for( int i = 0; i < len; i ++ )
3535 {
4219d8b0
RR
3536 wxDataViewItem item = sel[i];
3537 wxDataViewItem parent = GetModel()->GetParent( item );
3538 if (parent)
3539 {
3540 if (parent != last_parent)
3541 ExpandAncestors(item);
3542 }
3543
3544 last_parent = parent;
3545 int row = m_clientArea->GetRowByItem( item );
b7e9f8b1
RR
3546 if( row >= 0 )
3547 selection.Add( static_cast<unsigned int>(row) );
3548 }
4219d8b0 3549
b7e9f8b1
RR
3550 m_clientArea->SetSelections( selection );
3551}
3552
3553void wxDataViewCtrl::Select( const wxDataViewItem & item )
704c3490 3554{
4219d8b0
RR
3555 ExpandAncestors( item );
3556
b7e9f8b1
RR
3557 int row = m_clientArea->GetRowByItem( item );
3558 if( row >= 0 )
57f2a652
RR
3559 {
3560 //Unselect all rows before select another in the single select mode
3561 if (m_clientArea->IsSingleSel())
3562 m_clientArea->SelectAllRows(false);
b7e9f8b1 3563 m_clientArea->SelectRow(row, true);
57f2a652 3564 }
704c3490 3565}
3b6280be 3566
b7e9f8b1 3567void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
6ff7eee7 3568{
b7e9f8b1
RR
3569 int row = m_clientArea->GetRowByItem( item );
3570 if( row >= 0 )
3571 m_clientArea->SelectRow(row, false);
6ff7eee7
RR
3572}
3573
b7e9f8b1 3574bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
6ff7eee7 3575{
b7e9f8b1
RR
3576 int row = m_clientArea->GetRowByItem( item );
3577 if( row >= 0 )
3578 {
3579 return m_clientArea->IsRowSelected(row);
3580 }
3581 return false;
6ff7eee7
RR
3582}
3583
b7e9f8b1
RR
3584//Selection code with row number as parameter
3585int wxDataViewCtrl::GetSelections( wxArrayInt & sel ) const
6ff7eee7 3586{
b7e9f8b1
RR
3587 sel.Empty();
3588 wxDataViewSelection selection = m_clientArea->GetSelections();
3589 int len = selection.GetCount();
3590 for( int i = 0; i < len; i ++)
3591 {
3592 unsigned int row = selection[i];
3593 sel.Add( row );
3594 }
3595 return len;
6ff7eee7 3596}
df387169 3597
b7e9f8b1 3598void wxDataViewCtrl::SetSelections( const wxArrayInt & sel )
fc211fe5 3599{
59e60167 3600 wxDataViewSelection selection(wxDataViewSelectionCmp);
b7e9f8b1
RR
3601 int len = sel.GetCount();
3602 for( int i = 0; i < len; i ++ )
3603 {
3604 int row = sel[i];
3605 if( row >= 0 )
3606 selection.Add( static_cast<unsigned int>(row) );
3607 }
3608 m_clientArea->SetSelections( selection );
fc211fe5
RR
3609}
3610
b7e9f8b1 3611void wxDataViewCtrl::Select( int row )
6ff7eee7 3612{
b7e9f8b1 3613 if( row >= 0 )
57f2a652
RR
3614 {
3615 if (m_clientArea->IsSingleSel())
3616 m_clientArea->SelectAllRows(false);
b7e9f8b1 3617 m_clientArea->SelectRow( row, true );
57f2a652 3618 }
b7e9f8b1 3619}
df387169 3620
b7e9f8b1
RR
3621void wxDataViewCtrl::Unselect( int row )
3622{
3623 if( row >= 0 )
3624 m_clientArea->SelectRow(row, false);
3625}
3626
3627bool wxDataViewCtrl::IsSelected( int row ) const
3628{
3629 if( row >= 0 )
3630 return m_clientArea->IsRowSelected(row);
6ff7eee7
RR
3631 return false;
3632}
3633
b7e9f8b1 3634void wxDataViewCtrl::SelectRange( int from, int to )
6ff7eee7 3635{
b7fe2261 3636 wxArrayInt sel;
b7e9f8b1
RR
3637 for( int i = from; i < to; i ++ )
3638 sel.Add( i );
3639 m_clientArea->Select(sel);
3640}
df387169 3641
b7e9f8b1
RR
3642void wxDataViewCtrl::UnselectRange( int from, int to )
3643{
3644 wxDataViewSelection sel = m_clientArea->GetSelections();
3645 for( int i = from; i < to; i ++ )
3646 if( sel.Index( i ) != wxNOT_FOUND )
3647 sel.Remove( i );
3648 m_clientArea->SetSelections(sel);
6ff7eee7
RR
3649}
3650
b7e9f8b1 3651void wxDataViewCtrl::SelectAll()
6ff7eee7 3652{
b7e9f8b1
RR
3653 m_clientArea->SelectAllRows(true);
3654}
df387169 3655
b7e9f8b1
RR
3656void wxDataViewCtrl::UnselectAll()
3657{
3658 m_clientArea->SelectAllRows(false);
6ff7eee7 3659}
b7e9f8b1 3660
fbda518c 3661void wxDataViewCtrl::EnsureVisible( int row, int column )
b7e9f8b1 3662{
fbda518c
RR
3663 if( row < 0 )
3664 row = 0;
67be459b 3665 if( row > (int) m_clientArea->GetRowCount() )
fbda518c
RR
3666 row = m_clientArea->GetRowCount();
3667
3668 int first = m_clientArea->GetFirstVisibleRow();
3669 int last = m_clientArea->GetLastVisibleRow();
3670 if( row < first )
3671 m_clientArea->ScrollTo( row, column );
3672 else if( row > last )
3673 m_clientArea->ScrollTo( row - last + first, column );
3674 else
3675 m_clientArea->ScrollTo( first, column );
b7e9f8b1
RR
3676}
3677
fbda518c 3678void wxDataViewCtrl::EnsureVisible( const wxDataViewItem & item, const wxDataViewColumn * column )
b7e9f8b1 3679{
4219d8b0 3680 ExpandAncestors( item );
a881f34e 3681
13499080
RR
3682 m_clientArea->RecalculateDisplay();
3683
b7e9f8b1
RR
3684 int row = m_clientArea->GetRowByItem(item);
3685 if( row >= 0 )
fbda518c
RR
3686 {
3687 if( column == NULL )
9bcc8016 3688 EnsureVisible(row, -1);
fbda518c 3689 else
702f5349 3690 EnsureVisible( row, GetColumnIndex(column) );
fbda518c 3691 }
b7fe2261 3692
b7e9f8b1
RR
3693}
3694
a87b466d 3695void wxDataViewCtrl::HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column ) const
66e09788
RR
3696{
3697 m_clientArea->HitTest(point, item, column);
3698}
3699
fbda518c 3700wxRect wxDataViewCtrl::GetItemRect( const wxDataViewItem & item, const wxDataViewColumn* column ) const
66e09788
RR
3701{
3702 return m_clientArea->GetItemRect(item, column);
3703}
3704
b7e9f8b1
RR
3705wxDataViewItem wxDataViewCtrl::GetItemByRow( unsigned int row ) const
3706{
3707 return m_clientArea->GetItemByRow( row );
3708}
3709
3710int wxDataViewCtrl::GetRowByItem( const wxDataViewItem & item ) const
3711{
3712 return m_clientArea->GetRowByItem( item );
3713}
3714
afebb87b
RR
3715void wxDataViewCtrl::Expand( const wxDataViewItem & item )
3716{
3717 int row = m_clientArea->GetRowByItem( item );
3718 if (row != -1)
3719 m_clientArea->Expand(row);
3720}
3721
3722void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
3723{
3724 int row = m_clientArea->GetRowByItem( item );
3725 if (row != -1)
b7fe2261 3726 m_clientArea->Collapse(row);
afebb87b
RR
3727}
3728
739a8399
RR
3729bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const
3730{
3731 int row = m_clientArea->GetRowByItem( item );
3732 if (row != -1)
3733 return m_clientArea->IsExpanded(row);
3734 return false;
3735}
3736
3737
b7e9f8b1 3738 #endif
4ed7af08
RR
3739 // !wxUSE_GENERICDATAVIEWCTRL
3740
f554a14b 3741#endif
4ed7af08 3742 // wxUSE_DATAVIEWCTRL