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