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