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