]> git.saurik.com Git - wxWidgets.git/blame - src/generic/datavgen.cpp
Fix VC++ compile error
[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
43fd7dbd
VZ
1992 // Notice that it is possible that the item being deleted is not in the
1993 // tree at all, for example we could be deleting a never shown (because
1994 // collapsed) item in a tree model. So it's not an error if we don't know
1995 // about this item, just return without doing anything then.
1996 if ( !node || node->GetChildren().Index(item.GetID()) == wxNOT_FOUND )
1997 return false;
351461fc 1998
d47db7e0
RR
1999 int sub = -1;
2000 node->GetChildren().Remove( item.GetID() );
bc4f1ff2 2001 // Manipolate selection
fbda518c
RR
2002 if( m_selection.GetCount() > 1 )
2003 {
fbda518c 2004 m_selection.Empty();
fbda518c 2005 }
24c4a50f
RR
2006 bool isContainer = false;
2007 wxDataViewTreeNodes nds = node->GetNodes();
b7fe2261
VZ
2008 for (size_t i = 0; i < nds.GetCount(); i ++)
2009 {
24c4a50f
RR
2010 if (nds[i]->GetItem() == item)
2011 {
2012 isContainer = true;
2013 break;
2014 }
2015 }
2016 if( isContainer )
d47db7e0 2017 {
a8505db0 2018 wxDataViewTreeNode * n = NULL;
d47db7e0
RR
2019 wxDataViewTreeNodes nodes = node->GetNodes();
2020 int len = nodes.GetCount();
59e60167 2021 for( int i = 0; i < len; i ++)
d47db7e0
RR
2022 {
2023 if( nodes[i]->GetItem() == item )
2024 {
2025 n = nodes[i];
2026 break;
2027 }
2028 }
a8505db0 2029
c59a09cf 2030 wxCHECK_MSG( n != NULL, false, "item not found" );
a8505db0 2031
d47db7e0
RR
2032 node->GetNodes().Remove( n );
2033 sub -= n->GetSubTreeCount();
74123073 2034 ::DestroyTreeHelper(n);
d47db7e0 2035 }
bc4f1ff2 2036 // Make the row number invalid and get a new valid one when user call GetRowCount
442c56e6 2037 m_count = -1;
d47db7e0 2038 node->ChangeSubTreeCount(sub);
24c4a50f 2039
bc4f1ff2 2040 // Change the current row to the last row if the current exceed the max row number
d47db7e0
RR
2041 if( m_currentRow > GetRowCount() )
2042 m_currentRow = m_count - 1;
351461fc 2043
99d471a5 2044 UpdateDisplay();
b7fe2261 2045
99d471a5 2046 return true;
a0f3af5f
RR
2047}
2048
aba9bfd0 2049bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem & item)
a0f3af5f 2050{
66e09788 2051 SortPrepare();
b7e9f8b1
RR
2052 g_model->Resort();
2053
bc4f1ff2 2054 // Send event
6608fdab
RR
2055 wxWindow *parent = GetParent();
2056 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId());
2057 le.SetEventObject(parent);
2058 le.SetModel(GetOwner()->GetModel());
2059 le.SetItem(item);
2060 parent->GetEventHandler()->ProcessEvent(le);
b5ec7dd6 2061
99d471a5 2062 return true;
a0f3af5f
RR
2063}
2064
b7e9f8b1 2065bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem & item, unsigned int col )
a0f3af5f 2066{
9861f022 2067 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
aba9bfd0 2068/*#define MAX_VIRTUAL_WIDTH 100000
9861f022
RR
2069
2070 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
0fdc2321
RR
2071 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2072 Refresh( true, &rect );
2073
2074 return true;
aba9bfd0 2075*/
66e09788 2076 SortPrepare();
b7e9f8b1
RR
2077 g_model->Resort();
2078
bc4f1ff2 2079 // Send event
b7e9f8b1 2080 wxWindow *parent = GetParent();
6608fdab 2081 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId());
b7e9f8b1
RR
2082 le.SetEventObject(parent);
2083 le.SetModel(GetOwner()->GetModel());
2084 le.SetItem(item);
2085 le.SetColumn(col);
2086 le.SetDataViewColumn(GetOwner()->GetColumn(col));
2087 parent->GetEventHandler()->ProcessEvent(le);
d47db7e0 2088
0fcce6b9 2089 return true;
a0f3af5f
RR
2090}
2091
2092bool wxDataViewMainWindow::Cleared()
2093{
704c3490 2094 DestroyTree();
cfa42cb8 2095
33ba5a05
RR
2096 SortPrepare();
2097 BuildTree( GetOwner()->GetModel() );
cfa42cb8 2098
99d471a5 2099 UpdateDisplay();
b7e9f8b1 2100
99d471a5 2101 return true;
a0f3af5f
RR
2102}
2103
4b3feaa7
RR
2104void wxDataViewMainWindow::UpdateDisplay()
2105{
2106 m_dirty = true;
7d5d2f23 2107 m_underMouse = NULL;
4b3feaa7
RR
2108}
2109
2110void wxDataViewMainWindow::OnInternalIdle()
2111{
2112 wxWindow::OnInternalIdle();
f554a14b 2113
4b3feaa7
RR
2114 if (m_dirty)
2115 {
2116 RecalculateDisplay();
2117 m_dirty = false;
2118 }
2119}
2120
2121void wxDataViewMainWindow::RecalculateDisplay()
2122{
aba9bfd0 2123 wxDataViewModel *model = GetOwner()->GetModel();
4b3feaa7
RR
2124 if (!model)
2125 {
2126 Refresh();
2127 return;
2128 }
f554a14b 2129
9861f022 2130 int width = GetEndOfLastCol();
344ed1f3 2131 int height = GetLineStart( GetRowCount() );
4b3feaa7
RR
2132
2133 SetVirtualSize( width, height );
2134 GetOwner()->SetScrollRate( 10, m_lineHeight );
f554a14b 2135
4b3feaa7
RR
2136 Refresh();
2137}
2138
2139void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
2140{
2141 wxWindow::ScrollWindow( dx, dy, rect );
9861f022
RR
2142
2143 if (GetOwner()->m_headerArea)
2144 GetOwner()->m_headerArea->ScrollWindow( dx, 0 );
4b3feaa7
RR
2145}
2146
fbda518c 2147void wxDataViewMainWindow::ScrollTo( int rows, int column )
b7e9f8b1
RR
2148{
2149 int x, y;
2150 m_owner->GetScrollPixelsPerUnit( &x, &y );
344ed1f3 2151 int sy = GetLineStart( rows )/y;
fbda518c
RR
2152 int sx = 0;
2153 if( column != -1 )
2154 {
2155 wxRect rect = GetClientRect();
67be459b 2156 int colnum = 0;
dd639a4f 2157 int x_start, w = 0;
fbda518c
RR
2158 int xx, yy, xe;
2159 m_owner->CalcUnscrolledPosition( rect.x, rect.y, &xx, &yy );
2160 for (x_start = 0; colnum < column; colnum++)
2161 {
702f5349 2162 wxDataViewColumn *col = GetOwner()->GetColumnAt(colnum);
fbda518c
RR
2163 if (col->IsHidden())
2164 continue; // skip it!
2165
2166 w = col->GetWidth();
2167 x_start += w;
2168 }
2169
e822d1bd 2170 int x_end = x_start + w;
fbda518c
RR
2171 xe = xx + rect.width;
2172 if( x_end > xe )
2173 {
2174 sx = ( xx + x_end - xe )/x;
2175 }
2176 if( x_start < xx )
2177 {
b7fe2261 2178 sx = x_start/x;
fbda518c
RR
2179 }
2180 }
2181 m_owner->Scroll( sx, sy );
b7e9f8b1
RR
2182}
2183
9861f022 2184int wxDataViewMainWindow::GetCountPerPage() const
cab07038
RR
2185{
2186 wxSize size = GetClientSize();
2187 return size.y / m_lineHeight;
2188}
2189
9861f022 2190int wxDataViewMainWindow::GetEndOfLastCol() const
e21f75bd
RR
2191{
2192 int width = 0;
0a71f9e9 2193 unsigned int i;
9861f022 2194 for (i = 0; i < GetOwner()->GetColumnCount(); i++)
e21f75bd 2195 {
c741d33f 2196 const wxDataViewColumn *c =
702f5349 2197 const_cast<wxDataViewCtrl*>(GetOwner())->GetColumnAt( i );
9861f022
RR
2198
2199 if (!c->IsHidden())
2200 width += c->GetWidth();
e21f75bd
RR
2201 }
2202 return width;
2203}
2204
9861f022 2205unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
72664514
RR
2206{
2207 int x = 0;
2208 int y = 0;
2209 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
120b9b05 2210
344ed1f3 2211 return GetLineAt( y );
72664514
RR
2212}
2213
442c56e6 2214unsigned int wxDataViewMainWindow::GetLastVisibleRow()
72664514
RR
2215{
2216 wxSize client_size = GetClientSize();
c741d33f 2217 m_owner->CalcUnscrolledPosition( client_size.x, client_size.y,
977a41ec 2218 &client_size.x, &client_size.y );
72664514 2219
bc4f1ff2 2220 // we should deal with the pixel here
344ed1f3 2221 unsigned int row = GetLineAt(client_size.y) - 1;
b7fe2261 2222
fbda518c 2223 return wxMin( GetRowCount()-1, row );
72664514
RR
2224}
2225
442c56e6 2226unsigned int wxDataViewMainWindow::GetRowCount()
cab07038 2227{
3b6280be
RR
2228 if ( m_count == -1 )
2229 {
2230 m_count = RecalculateCount();
344ed1f3 2231 UpdateDisplay();
3b6280be 2232 }
aba9bfd0 2233 return m_count;
cab07038
RR
2234}
2235
0a71f9e9 2236void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row )
e21f75bd
RR
2237{
2238 m_currentRow = row;
120b9b05 2239
e21f75bd
RR
2240 // send event
2241}
2242
cab07038
RR
2243void wxDataViewMainWindow::SelectAllRows( bool on )
2244{
4a851b11
VZ
2245 if (IsEmpty())
2246 return;
120b9b05 2247
cab07038
RR
2248 if (on)
2249 {
72664514 2250 m_selection.Clear();
0a71f9e9 2251 for (unsigned int i = 0; i < GetRowCount(); i++)
cab07038 2252 m_selection.Add( i );
72664514
RR
2253 Refresh();
2254 }
2255 else
2256 {
0a71f9e9
RR
2257 unsigned int first_visible = GetFirstVisibleRow();
2258 unsigned int last_visible = GetLastVisibleRow();
2259 unsigned int i;
72664514 2260 for (i = 0; i < m_selection.GetCount(); i++)
120b9b05 2261 {
0a71f9e9 2262 unsigned int row = m_selection[i];
72664514
RR
2263 if ((row >= first_visible) && (row <= last_visible))
2264 RefreshRow( row );
2265 }
2266 m_selection.Clear();
cab07038 2267 }
cab07038
RR
2268}
2269
0a71f9e9 2270void wxDataViewMainWindow::SelectRow( unsigned int row, bool on )
cab07038
RR
2271{
2272 if (m_selection.Index( row ) == wxNOT_FOUND)
2273 {
2274 if (on)
2275 {
2276 m_selection.Add( row );
2277 RefreshRow( row );
2278 }
2279 }
2280 else
2281 {
2282 if (!on)
2283 {
2284 m_selection.Remove( row );
2285 RefreshRow( row );
2286 }
2287 }
2288}
2289
0a71f9e9 2290void wxDataViewMainWindow::SelectRows( unsigned int from, unsigned int to, bool on )
cab07038
RR
2291{
2292 if (from > to)
2293 {
0a71f9e9 2294 unsigned int tmp = from;
cab07038
RR
2295 from = to;
2296 to = tmp;
2297 }
2298
0a71f9e9 2299 unsigned int i;
cab07038
RR
2300 for (i = from; i <= to; i++)
2301 {
2302 if (m_selection.Index( i ) == wxNOT_FOUND)
2303 {
2304 if (on)
2305 m_selection.Add( i );
2306 }
2307 else
2308 {
2309 if (!on)
2310 m_selection.Remove( i );
2311 }
2312 }
2313 RefreshRows( from, to );
2314}
2315
87f0efe2
RR
2316void wxDataViewMainWindow::Select( const wxArrayInt& aSelections )
2317{
2318 for (size_t i=0; i < aSelections.GetCount(); i++)
2319 {
2320 int n = aSelections[i];
2321
2322 m_selection.Add( n );
2323 RefreshRow( n );
2324 }
2325}
2326
0a71f9e9 2327void wxDataViewMainWindow::ReverseRowSelection( unsigned int row )
cab07038
RR
2328{
2329 if (m_selection.Index( row ) == wxNOT_FOUND)
2330 m_selection.Add( row );
2331 else
2332 m_selection.Remove( row );
120b9b05 2333 RefreshRow( row );
cab07038
RR
2334}
2335
0a71f9e9 2336bool wxDataViewMainWindow::IsRowSelected( unsigned int row )
cab07038
RR
2337{
2338 return (m_selection.Index( row ) != wxNOT_FOUND);
2339}
2340
526e19e2
RR
2341void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem& item)
2342{
2343 wxWindow *parent = GetParent();
2344 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, parent->GetId());
2345
2346 le.SetEventObject(parent);
2347 le.SetModel(GetOwner()->GetModel());
2348 le.SetItem( item );
2349
2350 parent->GetEventHandler()->ProcessEvent(le);
2351}
2352
0a71f9e9 2353void wxDataViewMainWindow::RefreshRow( unsigned int row )
cab07038 2354{
344ed1f3 2355 wxRect rect( 0, GetLineStart( row ), GetEndOfLastCol(), GetLineHeight( row ) );
cab07038 2356 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
120b9b05 2357
cab07038
RR
2358 wxSize client_size = GetClientSize();
2359 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2360 wxRect intersect_rect = client_rect.Intersect( rect );
2361 if (intersect_rect.width > 0)
2362 Refresh( true, &intersect_rect );
2363}
2364
0a71f9e9 2365void wxDataViewMainWindow::RefreshRows( unsigned int from, unsigned int to )
cab07038
RR
2366{
2367 if (from > to)
2368 {
0a71f9e9 2369 unsigned int tmp = to;
cab07038
RR
2370 to = from;
2371 from = tmp;
2372 }
2373
344ed1f3 2374 wxRect rect( 0, GetLineStart( from ), GetEndOfLastCol(), GetLineStart( (to-from+1) ) );
cab07038 2375 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
120b9b05 2376
cab07038
RR
2377 wxSize client_size = GetClientSize();
2378 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2379 wxRect intersect_rect = client_rect.Intersect( rect );
2380 if (intersect_rect.width > 0)
2381 Refresh( true, &intersect_rect );
2382}
2383
0a71f9e9 2384void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow )
cab07038 2385{
cab07038 2386 wxSize client_size = GetClientSize();
344ed1f3
RR
2387 int start = GetLineStart( firstRow );
2388 m_owner->CalcScrolledPosition( start, 0, &start, NULL );
2389 if (start > client_size.y) return;
2390
2391 wxRect rect( 0, start, client_size.x, client_size.y - start );
777f9cef 2392
344ed1f3 2393 Refresh( true, &rect );
cab07038
RR
2394}
2395
0a71f9e9 2396void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event)
cab07038 2397{
4a851b11 2398 wxCHECK_RET( newCurrent < GetRowCount(),
9a83f860 2399 wxT("invalid item index in OnArrowChar()") );
cab07038
RR
2400
2401 // if there is no selection, we cannot move it anywhere
2402 if (!HasCurrentRow())
2403 return;
2404
0a71f9e9 2405 unsigned int oldCurrent = m_currentRow;
cab07038
RR
2406
2407 // in single selection we just ignore Shift as we can't select several
2408 // items anyhow
e21f75bd 2409 if ( event.ShiftDown() && !IsSingleSel() )
cab07038
RR
2410 {
2411 RefreshRow( oldCurrent );
120b9b05 2412
e21f75bd 2413 ChangeCurrentRow( newCurrent );
cab07038
RR
2414
2415 // select all the items between the old and the new one
2416 if ( oldCurrent > newCurrent )
2417 {
2418 newCurrent = oldCurrent;
2419 oldCurrent = m_currentRow;
2420 }
2421
2422 SelectRows( oldCurrent, newCurrent, true );
526e19e2
RR
2423 if (oldCurrent!=newCurrent)
2424 SendSelectionChangedEvent(GetItemByRow(m_selection[0]));
cab07038
RR
2425 }
2426 else // !shift
2427 {
72664514 2428 RefreshRow( oldCurrent );
120b9b05 2429
cab07038
RR
2430 // all previously selected items are unselected unless ctrl is held
2431 if ( !event.ControlDown() )
2432 SelectAllRows(false);
2433
e21f75bd 2434 ChangeCurrentRow( newCurrent );
cab07038
RR
2435
2436 if ( !event.ControlDown() )
526e19e2 2437 {
cab07038 2438 SelectRow( m_currentRow, true );
526e19e2
RR
2439 SendSelectionChangedEvent(GetItemByRow(m_currentRow));
2440 }
72664514
RR
2441 else
2442 RefreshRow( m_currentRow );
cab07038
RR
2443 }
2444
67be459b 2445 GetOwner()->EnsureVisible( m_currentRow, -1 );
9861f022
RR
2446}
2447
2448wxRect wxDataViewMainWindow::GetLineRect( unsigned int row ) const
2449{
2450 wxRect rect;
2451 rect.x = 0;
344ed1f3 2452 rect.y = GetLineStart( row );
9861f022 2453 rect.width = GetEndOfLastCol();
344ed1f3 2454 rect.height = GetLineHeight( row );
9861f022
RR
2455
2456 return rect;
cab07038
RR
2457}
2458
344ed1f3
RR
2459int wxDataViewMainWindow::GetLineStart( unsigned int row ) const
2460{
ba5f54e6 2461 const wxDataViewModel *model = GetOwner()->GetModel();
777f9cef 2462
344ed1f3
RR
2463 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT)
2464 {
2465 // TODO make more efficient
777f9cef 2466
344ed1f3 2467 int start = 0;
777f9cef 2468
344ed1f3
RR
2469 unsigned int r;
2470 for (r = 0; r < row; r++)
2471 {
e170469f
RR
2472 const wxDataViewTreeNode* node = GetTreeNodeByRow(r);
2473 if (!node) return start;
777f9cef 2474
e170469f 2475 wxDataViewItem item = node->GetItem();
777f9cef 2476
e170469f
RR
2477 if (node && !node->HasChildren())
2478 {
2479 // Yes, if the node does not have any child, it must be a leaf which
2480 // mean that it is a temporarily created by GetTreeNodeByRow
59e60167 2481 wxDELETE(node);
e170469f 2482 }
777f9cef 2483
e170469f
RR
2484 unsigned int cols = GetOwner()->GetColumnCount();
2485 unsigned int col;
2486 int height = m_lineHeight;
2487 for (col = 0; col < cols; col++)
2488 {
344ed1f3
RR
2489 const wxDataViewColumn *column = GetOwner()->GetColumn(col);
2490 if (column->IsHidden())
2491 continue; // skip it!
777f9cef 2492
4cef3873
VZ
2493 if ((col != 0) &&
2494 model->IsContainer(item) &&
ce468dc2 2495 !model->HasContainerColumns(item))
ba5f54e6 2496 continue; // skip it!
777f9cef 2497
ba5f54e6
RR
2498 wxVariant value;
2499 model->GetValue( value, item, column->GetModelColumn() );
ce468dc2 2500
4cef3873 2501 wxDataViewRenderer *renderer =
ce468dc2
FM
2502 const_cast<wxDataViewRenderer*>(column->GetRenderer());
2503 renderer->SetValue( value );
344ed1f3 2504 height = wxMax( height, renderer->GetSize().y );
e170469f 2505 }
777f9cef 2506
e170469f 2507 start += height;
344ed1f3 2508 }
777f9cef 2509
344ed1f3
RR
2510 return start;
2511 }
2512 else
2513 {
2514 return row * m_lineHeight;
2515 }
2516}
2517
2518int wxDataViewMainWindow::GetLineAt( unsigned int y ) const
2519{
ba5f54e6
RR
2520 const wxDataViewModel *model = GetOwner()->GetModel();
2521
777f9cef
VZ
2522 // check for the easy case first
2523 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT) )
2524 return y / m_lineHeight;
2525
2526 // TODO make more efficient
2527 unsigned int row = 0;
2528 unsigned int yy = 0;
2529 for (;;)
344ed1f3 2530 {
ce468dc2
FM
2531 const wxDataViewTreeNode* node = GetTreeNodeByRow(row);
2532 if (!node)
2533 {
2534 // not really correct...
2535 return row + ((y-yy) / m_lineHeight);
2536 }
777f9cef 2537
ce468dc2 2538 wxDataViewItem item = node->GetItem();
777f9cef
VZ
2539
2540 if (node && !node->HasChildren())
344ed1f3 2541 {
777f9cef
VZ
2542 // Yes, if the node does not have any child, it must be a leaf which
2543 // mean that it is a temporarily created by GetTreeNodeByRow
59e60167 2544 wxDELETE(node);
344ed1f3 2545 }
777f9cef 2546
ce468dc2
FM
2547 unsigned int cols = GetOwner()->GetColumnCount();
2548 unsigned int col;
2549 int height = m_lineHeight;
2550 for (col = 0; col < cols; col++)
2551 {
777f9cef
VZ
2552 const wxDataViewColumn *column = GetOwner()->GetColumn(col);
2553 if (column->IsHidden())
2554 continue; // skip it!
2555
4cef3873
VZ
2556 if ((col != 0) &&
2557 model->IsContainer(item) &&
ce468dc2 2558 !model->HasContainerColumns(item))
777f9cef
VZ
2559 continue; // skip it!
2560
777f9cef
VZ
2561 wxVariant value;
2562 model->GetValue( value, item, column->GetModelColumn() );
ce468dc2 2563
4cef3873 2564 wxDataViewRenderer *renderer =
ce468dc2
FM
2565 const_cast<wxDataViewRenderer*>(column->GetRenderer());
2566 renderer->SetValue( value );
777f9cef 2567 height = wxMax( height, renderer->GetSize().y );
ce468dc2 2568 }
777f9cef 2569
ce468dc2
FM
2570 yy += height;
2571 if (y < yy)
2572 return row;
777f9cef 2573
ce468dc2 2574 row++;
344ed1f3
RR
2575 }
2576}
2577
2578int wxDataViewMainWindow::GetLineHeight( unsigned int row ) const
2579{
ba5f54e6 2580 const wxDataViewModel *model = GetOwner()->GetModel();
777f9cef 2581
344ed1f3
RR
2582 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT)
2583 {
2584 wxASSERT( !IsVirtualList() );
777f9cef 2585
344ed1f3
RR
2586 const wxDataViewTreeNode* node = GetTreeNodeByRow(row);
2587 // wxASSERT( node );
2588 if (!node) return m_lineHeight;
2589
ba5f54e6 2590 wxDataViewItem item = node->GetItem();
777f9cef 2591
e170469f
RR
2592 if (node && !node->HasChildren())
2593 {
2594 // Yes, if the node does not have any child, it must be a leaf which
2595 // mean that it is a temporarily created by GetTreeNodeByRow
977a41ec 2596 wxDELETE(node);
e170469f 2597 }
777f9cef 2598
981cd83e 2599 int height = m_lineHeight;
777f9cef 2600
344ed1f3
RR
2601 unsigned int cols = GetOwner()->GetColumnCount();
2602 unsigned int col;
2603 for (col = 0; col < cols; col++)
2604 {
2605 const wxDataViewColumn *column = GetOwner()->GetColumn(col);
2606 if (column->IsHidden())
2607 continue; // skip it!
ba5f54e6 2608
4cef3873
VZ
2609 if ((col != 0) &&
2610 model->IsContainer(item) &&
ce468dc2 2611 !model->HasContainerColumns(item))
ba5f54e6 2612 continue; // skip it!
777f9cef 2613
ba5f54e6
RR
2614 wxVariant value;
2615 model->GetValue( value, item, column->GetModelColumn() );
ce468dc2 2616
4cef3873 2617 wxDataViewRenderer *renderer =
ce468dc2
FM
2618 const_cast<wxDataViewRenderer*>(column->GetRenderer());
2619 renderer->SetValue( value );
344ed1f3
RR
2620 height = wxMax( height, renderer->GetSize().y );
2621 }
2622
2623 return height;
2624 }
2625 else
2626 {
2627 return m_lineHeight;
2628 }
2629}
2630
aba9bfd0
RR
2631class RowToItemJob: public DoJob
2632{
2633public:
4cef3873 2634 RowToItemJob( unsigned int row , int current )
bc4f1ff2
FM
2635 { this->row = row; this->current = current; }
2636 virtual ~RowToItemJob() {}
aba9bfd0 2637
d5025dc0 2638 virtual int operator() ( wxDataViewTreeNode * node )
d47db7e0
RR
2639 {
2640 current ++;
2641 if( current == static_cast<int>(row))
977a41ec 2642 {
59e60167 2643 ret = node->GetItem();
d47db7e0
RR
2644 return DoJob::OK;
2645 }
d5025dc0 2646
d47db7e0
RR
2647 if( node->GetSubTreeCount() + current < static_cast<int>(row) )
2648 {
2649 current += node->GetSubTreeCount();
2650 return DoJob::IGR;
2651 }
2652 else
b7e9f8b1 2653 {
4cef3873 2654 // If the current has no child node, we can find the desired item of the row
bc4f1ff2 2655 // number directly.
4cef3873 2656 // This if can speed up finding in some case, and will has a very good effect
bc4f1ff2 2657 // when it comes to list view
b7e9f8b1
RR
2658 if( node->GetNodes().GetCount() == 0)
2659 {
2660 int index = static_cast<int>(row) - current - 1;
2661 ret = node->GetChildren().Item( index );
2662 return DoJob::OK;
2663 }
d47db7e0 2664 return DoJob::CONT;
b7e9f8b1 2665 }
d47db7e0
RR
2666 }
2667
2668 virtual int operator() ( void * n )
2669 {
2670 current ++;
2671 if( current == static_cast<int>(row))
977a41ec 2672 {
59e60167 2673 ret = wxDataViewItem( n );
d47db7e0
RR
2674 return DoJob::OK;
2675 }
2676 return DoJob::CONT;
2677 }
bc4f1ff2
FM
2678
2679 wxDataViewItem GetResult() const
2680 { return ret; }
2681
aba9bfd0
RR
2682private:
2683 unsigned int row;
59e60167 2684 int current;
aba9bfd0
RR
2685 wxDataViewItem ret;
2686};
2687
fbda518c 2688wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row) const
aba9bfd0 2689{
86ba79ed 2690 if (IsVirtualList())
a3cc79d9 2691 {
86ba79ed 2692 return wxDataViewItem( wxUIntToPtr(row+1) );
a3cc79d9
RR
2693 }
2694 else
2695 {
2696 RowToItemJob job( row, -2 );
2697 Walker( m_root , job );
49d2b75d 2698 return job.GetResult();
a3cc79d9 2699 }
aba9bfd0
RR
2700}
2701
3b6280be
RR
2702class RowToTreeNodeJob: public DoJob
2703{
2704public:
442c56e6
VZ
2705 RowToTreeNodeJob( unsigned int row , int current, wxDataViewTreeNode * node )
2706 {
2707 this->row = row;
59e60167
VZ
2708 this->current = current;
2709 ret = NULL;
d47db7e0
RR
2710 parent = node;
2711 }
59e60167 2712 virtual ~RowToTreeNodeJob(){ }
3b6280be
RR
2713
2714 virtual int operator() ( wxDataViewTreeNode * node )
d5025dc0 2715 {
d47db7e0 2716 current ++;
704c3490 2717 if( current == static_cast<int>(row))
977a41ec 2718 {
59e60167 2719 ret = node;
d5025dc0 2720 return DoJob::OK;
3b6280be 2721 }
d47db7e0
RR
2722
2723 if( node->GetSubTreeCount() + current < static_cast<int>(row) )
2724 {
2725 current += node->GetSubTreeCount();
2726 return DoJob::IGR;
2727 }
d5025dc0 2728 else
d47db7e0
RR
2729 {
2730 parent = node;
bc4f1ff2 2731
4cef3873 2732 // If the current node has no children, we can find the desired item of the
bc4f1ff2 2733 // row number directly.
4cef3873 2734 // This if can speed up finding in some case, and will have a very good
bc4f1ff2 2735 // effect for list views.
b7e9f8b1
RR
2736 if( node->GetNodes().GetCount() == 0)
2737 {
2738 int index = static_cast<int>(row) - current - 1;
2739 void * n = node->GetChildren().Item( index );
59e60167 2740 ret = new wxDataViewTreeNode( parent );
b7e9f8b1
RR
2741 ret->SetItem( wxDataViewItem( n ));
2742 ret->SetHasChildren(false);
2743 return DoJob::OK;
2744 }
d47db7e0
RR
2745 return DoJob::CONT;
2746 }
d5025dc0 2747 }
3b6280be 2748
d47db7e0
RR
2749 virtual int operator() ( void * n )
2750 {
2751 current ++;
2752 if( current == static_cast<int>(row))
977a41ec 2753 {
59e60167 2754 ret = new wxDataViewTreeNode( parent );
d47db7e0
RR
2755 ret->SetItem( wxDataViewItem( n ));
2756 ret->SetHasChildren(false);
2757 return DoJob::OK;
2758 }
442c56e6 2759
d47db7e0
RR
2760 return DoJob::CONT;
2761 }
bc4f1ff2
FM
2762
2763 wxDataViewTreeNode * GetResult() const
2764 { return ret; }
2765
3b6280be
RR
2766private:
2767 unsigned int row;
59e60167 2768 int current;
3b6280be 2769 wxDataViewTreeNode * ret;
59e60167 2770 wxDataViewTreeNode * parent;
3b6280be
RR
2771};
2772
344ed1f3 2773wxDataViewTreeNode * wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row) const
3b6280be 2774{
344ed1f3 2775 wxASSERT( !IsVirtualList() );
777f9cef 2776
344ed1f3
RR
2777 RowToTreeNodeJob job( row , -2, m_root );
2778 Walker( m_root , job );
2779 return job.GetResult();
3b6280be
RR
2780}
2781
4cef3873 2782wxDataViewEvent wxDataViewMainWindow::SendExpanderEvent( wxEventType type,
bc4f1ff2 2783 const wxDataViewItem & item )
66e09788
RR
2784{
2785 wxWindow *parent = GetParent();
2786 wxDataViewEvent le(type, parent->GetId());
2787
2788 le.SetEventObject(parent);
2789 le.SetModel(GetOwner()->GetModel());
2790 le.SetItem( item );
2791
2792 parent->GetEventHandler()->ProcessEvent(le);
2793 return le;
2794}
2795
739a8399
RR
2796bool wxDataViewMainWindow::IsExpanded( unsigned int row ) const
2797{
2798 if (IsVirtualList())
bc4f1ff2 2799 return false;
9adeb77a 2800
739a8399
RR
2801 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
2802 if (!node)
bc4f1ff2 2803 return false;
9adeb77a 2804
739a8399 2805 if (!node->HasChildren())
bf9ea288 2806 {
bc4f1ff2
FM
2807 delete node;
2808 return false;
bf9ea288 2809 }
9adeb77a 2810
739a8399
RR
2811 return node->IsOpen();
2812}
2813
235d5f88
RR
2814bool wxDataViewMainWindow::HasChildren( unsigned int row ) const
2815{
2816 if (IsVirtualList())
2817 return false;
2818
2819 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
2820 if (!node)
2821 return false;
739a8399 2822
235d5f88
RR
2823 if (!node->HasChildren())
2824 {
2825 delete node;
2826 return false;
2827 }
2828
2829 return true;
2830}
2831
2832void wxDataViewMainWindow::Expand( unsigned int row )
3b6280be 2833{
9657c197 2834 if (IsVirtualList())
bc4f1ff2 2835 return;
9657c197 2836
3b6280be 2837 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
235d5f88
RR
2838 if (!node)
2839 return;
4cef3873 2840
235d5f88 2841 if (!node->HasChildren())
3b6280be 2842 {
235d5f88
RR
2843 delete node;
2844 return;
2845 }
4cef3873 2846
235d5f88 2847 if (!node->IsOpen())
3b6280be 2848 {
4cef3873 2849 wxDataViewEvent e =
bc4f1ff2
FM
2850 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, node->GetItem());
2851
2852 // Check if the user prevent expanding
2853 if( e.GetSkipped() )
66e09788 2854 return;
b7fe2261 2855
bc4f1ff2 2856 node->ToggleOpen();
977a41ec 2857
bc4f1ff2
FM
2858 // build the children of current node
2859 if( node->GetChildrenNumber() == 0 )
2860 {
2861 SortPrepare();
2862 ::BuildTreeHelper(GetOwner()->GetModel(), node->GetItem(), node);
2863 }
977a41ec 2864
bc4f1ff2
FM
2865 // By expanding the node all row indices that are currently in the selection list
2866 // and are greater than our node have become invalid. So we have to correct that now.
2867 const unsigned rowAdjustment = node->GetSubTreeCount();
2868 for(unsigned i=0; i<m_selection.size(); ++i)
2869 {
2870 const unsigned testRow = m_selection[i];
2871 // all rows above us are not affected, so skip them
2872 if(testRow <= row)
2873 continue;
2874
2875 m_selection[i] += rowAdjustment;
2876 }
977a41ec 2877
bc4f1ff2
FM
2878 if(m_currentRow > row)
2879 ChangeCurrentRow(m_currentRow + rowAdjustment);
977a41ec 2880
bc4f1ff2
FM
2881 m_count = -1;
2882 UpdateDisplay();
2883 // Send the expanded event
2884 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED,node->GetItem());
351461fc 2885 }
3b6280be
RR
2886}
2887
235d5f88 2888void wxDataViewMainWindow::Collapse(unsigned int row)
3b6280be 2889{
9657c197 2890 if (IsVirtualList())
bc4f1ff2 2891 return;
e822d1bd 2892
7d835958
RR
2893 wxDataViewTreeNode *node = GetTreeNodeByRow(row);
2894 if (!node)
2895 return;
4cef3873 2896
235d5f88 2897 if (!node->HasChildren())
3b6280be 2898 {
7d835958
RR
2899 delete node;
2900 return;
2901 }
d47db7e0 2902
235d5f88 2903 if (node->IsOpen())
3b6280be 2904 {
4cef3873 2905 wxDataViewEvent e =
bc4f1ff2 2906 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING,node->GetItem());
66e09788
RR
2907 if( e.GetSkipped() )
2908 return;
abdb8c18 2909
977a41ec
FM
2910 // Find out if there are selected items below the current node.
2911 bool selectCollapsingRow = false;
2912 const unsigned rowAdjustment = node->GetSubTreeCount();
2913 unsigned maxRowToBeTested = row + rowAdjustment;
2914 for(unsigned i=0; i<m_selection.size(); ++i)
2915 {
2916 const unsigned testRow = m_selection[i];
2917 if(testRow > row && testRow <= maxRowToBeTested)
2918 {
2919 selectCollapsingRow = true;
2920 // get out as soon as we have found a node that is selected
2921 break;
2922 }
2923 }
2924
2925 node->ToggleOpen();
2926
2927 // If the node to be closed has selected items the user won't see those any longer.
2928 // We select the collapsing node in this case.
2929 if(selectCollapsingRow)
2930 {
2931 SelectAllRows(false);
2932 ChangeCurrentRow(row);
2933 SelectRow(row, true);
2934 SendSelectionChangedEvent(GetItemByRow(row));
2935 }
2936 else
2937 {
2938 // if there were no selected items below our node we still need to "fix" the
2939 // selection list to adjust for the changing of the row indices.
235d5f88 2940 // We actually do the opposite of what we are doing in Expand().
977a41ec
FM
2941 for(unsigned i=0; i<m_selection.size(); ++i)
2942 {
2943 const unsigned testRow = m_selection[i];
2944 // all rows above us are not affected, so skip them
2945 if(testRow <= row)
2946 continue;
2947
2948 m_selection[i] -= rowAdjustment;
2949 }
2950
2951 // if the "current row" is being collapsed away we change it to the current row ;-)
2952 if(m_currentRow > row && m_currentRow <= maxRowToBeTested)
2953 ChangeCurrentRow(row);
2954 else if(m_currentRow > row)
2955 ChangeCurrentRow(m_currentRow - rowAdjustment);
2956 }
abdb8c18 2957
3b6280be 2958 m_count = -1;
351461fc 2959 UpdateDisplay();
7d835958 2960 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED,node->GetItem());
66e09788 2961 }
3b6280be
RR
2962}
2963
351461fc
RR
2964wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item )
2965{
2966 wxDataViewModel * model = GetOwner()->GetModel();
2967 if( model == NULL )
2968 return NULL;
9adeb77a 2969
ce2fe798
RR
2970 if (!item.IsOk())
2971 return m_root;
351461fc 2972
bc4f1ff2 2973 // Compose the a parent-chain of the finding item
351461fc
RR
2974 ItemList list;
2975 list.DeleteContents( true );
2976 wxDataViewItem it( item );
2977 while( it.IsOk() )
2978 {
2979 wxDataViewItem * pItem = new wxDataViewItem( it );
2980 list.Insert( pItem );
2981 it = model->GetParent( it );
2982 }
2983
bc4f1ff2
FM
2984 // Find the item along the parent-chain.
2985 // This algorithm is designed to speed up the node-finding method
351461fc 2986 wxDataViewTreeNode * node = m_root;
59e60167 2987 for( ItemList::const_iterator iter = list.begin(); iter !=list.end(); iter++ )
351461fc
RR
2988 {
2989 if( node->HasChildren() )
2990 {
2991 if( node->GetChildrenNumber() == 0 )
24c4a50f
RR
2992 {
2993 SortPrepare();
74123073 2994 ::BuildTreeHelper(model, node->GetItem(), node);
24c4a50f 2995 }
351461fc 2996
d47db7e0 2997 wxDataViewTreeNodes nodes = node->GetNodes();
d2c1ee8a
RR
2998 unsigned int i;
2999 bool found = false;
777f9cef 3000
d2c1ee8a 3001 for (i = 0; i < nodes.GetCount(); i ++)
d92cb015 3002 {
c899416d 3003 if (nodes[i]->GetItem() == (**iter))
b7fe2261 3004 {
d2c1ee8a 3005 if (nodes[i]->GetItem() == item)
977a41ec 3006 return nodes[i];
777f9cef 3007
d92cb015 3008 node = nodes[i];
d2c1ee8a 3009 found = true;
d92cb015
RR
3010 break;
3011 }
3012 }
d2c1ee8a 3013 if (!found)
351461fc 3014 return NULL;
351461fc
RR
3015 }
3016 else
3017 return NULL;
3018 }
d2c1ee8a 3019 return NULL;
351461fc
RR
3020}
3021
4cef3873 3022void wxDataViewMainWindow::HitTest( const wxPoint & point, wxDataViewItem & item,
bc4f1ff2 3023 wxDataViewColumn* &column )
66e09788 3024{
fbda518c 3025 wxDataViewColumn *col = NULL;
66e09788
RR
3026 unsigned int cols = GetOwner()->GetColumnCount();
3027 unsigned int colnum = 0;
66e09788
RR
3028 int x, y;
3029 m_owner->CalcUnscrolledPosition( point.x, point.y, &x, &y );
e822d1bd 3030 for (unsigned x_start = 0; colnum < cols; colnum++)
66e09788 3031 {
702f5349 3032 col = GetOwner()->GetColumnAt(colnum);
66e09788
RR
3033 if (col->IsHidden())
3034 continue; // skip it!
3035
3036 unsigned int w = col->GetWidth();
3037 if (x_start+w >= (unsigned int)x)
3038 break;
3039
3040 x_start += w;
3041 }
3042
fbda518c 3043 column = col;
344ed1f3 3044 item = GetItemByRow( GetLineAt( y ) );
66e09788
RR
3045}
3046
4cef3873 3047wxRect wxDataViewMainWindow::GetItemRect( const wxDataViewItem & item,
bc4f1ff2 3048 const wxDataViewColumn* column )
66e09788 3049{
c937344c
RR
3050 int xpos = 0;
3051 int width = 0;
4cef3873 3052
c937344c
RR
3053 unsigned int cols = GetOwner()->GetColumnCount();
3054 // If column is null the loop will compute the combined width of all columns.
3055 // Otherwise, it will compute the x position of the column we are looking for.
3056 for (unsigned int i = 0; i < cols; i++)
3057 {
3058 wxDataViewColumn* col = GetOwner()->GetColumnAt( i );
3059
3060 if (col == column)
3061 break;
3062
3063 if (col->IsHidden())
3064 continue; // skip it!
3065
3066 xpos += col->GetWidth();
3067 width += col->GetWidth();
3068 }
3069
3070 if(column != 0)
3071 {
3072 // If we have a column, we need can get its width directly.
3073 if(column->IsHidden())
3074 width = 0;
3075 else
3076 width = column->GetWidth();
3077
3078 }
3079 else
3080 {
3081 // If we have no column, we reset the x position back to zero.
3082 xpos = 0;
3083 }
3084
3085 // we have to take an expander column into account and compute its indentation
3086 // to get the correct x position where the actual text is
3087 int indent = 0;
66e09788 3088 int row = GetRowByItem(item);
c937344c 3089 if (!IsVirtualList() && (column == 0 || GetOwner()->GetExpanderColumn() == column) )
66e09788 3090 {
c937344c
RR
3091 wxDataViewTreeNode* node = GetTreeNodeByRow(row);
3092 indent = GetOwner()->GetIndent() * node->GetIndentLevel();
03647350 3093 indent = indent + m_lineHeight; // use m_lineHeight as the width of the expander
c937344c
RR
3094
3095 if(!node->HasChildren())
3096 delete node;
66e09788 3097 }
c937344c
RR
3098
3099 wxRect itemRect( xpos + indent,
3100 GetLineStart( row ),
3101 width - indent,
3102 GetLineHeight( row ) );
3103
3104 GetOwner()->CalcScrolledPosition( itemRect.x, itemRect.y,
3105 &itemRect.x, &itemRect.y );
3106
3107 return itemRect;
66e09788
RR
3108}
3109
442c56e6 3110int wxDataViewMainWindow::RecalculateCount()
3b6280be 3111{
86ba79ed 3112 if (IsVirtualList())
a3cc79d9 3113 {
9330d5af
RR
3114 wxDataViewVirtualListModel *list_model =
3115 (wxDataViewVirtualListModel*) GetOwner()->GetModel();
03647350 3116
9330d5af 3117 return list_model->GetCount();
a3cc79d9
RR
3118 }
3119 else
3120 {
3121 return m_root->GetSubTreeCount();
3122 }
3b6280be
RR
3123}
3124
aba9bfd0
RR
3125class ItemToRowJob : public DoJob
3126{
3127public:
59e60167
VZ
3128 ItemToRowJob(const wxDataViewItem& item_, ItemList::const_iterator iter)
3129 : m_iter(iter),
977a41ec 3130 item(item_)
59e60167
VZ
3131 {
3132 ret = -1;
3133 }
aba9bfd0 3134
bc4f1ff2 3135 // Maybe binary search will help to speed up this process
d5025dc0
RR
3136 virtual int operator() ( wxDataViewTreeNode * node)
3137 {
977a41ec
FM
3138 ret ++;
3139 if( node->GetItem() == item )
3140 {
3141 return DoJob::OK;
3142 }
d5025dc0 3143
977a41ec
FM
3144 if( node->GetItem() == **m_iter )
3145 {
3146 m_iter++;
3147 return DoJob::CONT;
3148 }
3149 else
3150 {
3151 ret += node->GetSubTreeCount();
3152 return DoJob::IGR;
3153 }
442c56e6 3154
d5025dc0 3155 }
aba9bfd0 3156
d47db7e0
RR
3157 virtual int operator() ( void * n )
3158 {
3159 ret ++;
3160 if( n == item.GetID() )
3161 return DoJob::OK;
3162 return DoJob::CONT;
3163 }
bc4f1ff2
FM
3164
3165 // the row number is begin from zero
3166 int GetResult() const
3167 { return ret -1; }
59e60167 3168
aba9bfd0 3169private:
c899416d 3170 ItemList::const_iterator m_iter;
aba9bfd0
RR
3171 wxDataViewItem item;
3172 int ret;
442c56e6 3173
aba9bfd0
RR
3174};
3175
344ed1f3 3176int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item) const
aba9bfd0 3177{
344ed1f3 3178 const wxDataViewModel * model = GetOwner()->GetModel();
d47db7e0 3179 if( model == NULL )
fbda518c 3180 return -1;
d47db7e0 3181
86ba79ed 3182 if (IsVirtualList())
d47db7e0 3183 {
86ba79ed 3184 return wxPtrToUInt( item.GetID() ) -1;
d47db7e0 3185 }
a3cc79d9
RR
3186 else
3187 {
3188 if( !item.IsOk() )
3189 return -1;
3190
bc4f1ff2 3191 // Compose the a parent-chain of the finding item
a3cc79d9 3192 ItemList list;
e822d1bd 3193 wxDataViewItem * pItem;
a3cc79d9
RR
3194 list.DeleteContents( true );
3195 wxDataViewItem it( item );
3196 while( it.IsOk() )
3197 {
3198 pItem = new wxDataViewItem( it );
3199 list.Insert( pItem );
3200 it = model->GetParent( it );
3201 }
3202 pItem = new wxDataViewItem( );
3203 list.Insert( pItem );
d47db7e0 3204
a3cc79d9
RR
3205 ItemToRowJob job( item, list.begin() );
3206 Walker(m_root , job );
3207 return job.GetResult();
3208 }
aba9bfd0
RR
3209}
3210
4cef3873 3211static void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item,
bc4f1ff2 3212 wxDataViewTreeNode * node)
aba9bfd0 3213{
351461fc 3214 if( !model->IsContainer( item ) )
59e60167 3215 return;
442c56e6 3216
c899416d
RR
3217 wxDataViewItemArray children;
3218 unsigned int num = model->GetChildren( item, children);
9adeb77a 3219
67be459b 3220 unsigned int index = 0;
c899416d 3221 while( index < num )
aba9bfd0 3222 {
c899416d 3223 if( model->IsContainer( children[index] ) )
d47db7e0
RR
3224 {
3225 wxDataViewTreeNode * n = new wxDataViewTreeNode( node );
c899416d 3226 n->SetItem(children[index]);
59e60167 3227 n->SetHasChildren( true );
d47db7e0
RR
3228 node->AddNode( n );
3229 }
3230 else
3231 {
c899416d 3232 node->AddLeaf( children[index].GetID() );
d47db7e0 3233 }
c899416d 3234 index ++;
aba9bfd0 3235 }
d47db7e0
RR
3236 node->SetSubTreeCount( num );
3237 wxDataViewTreeNode * n = node->GetParent();
3238 if( n != NULL)
3239 n->ChangeSubTreeCount(num);
3240
aba9bfd0
RR
3241}
3242
3243void wxDataViewMainWindow::BuildTree(wxDataViewModel * model)
3244{
51bdecff
RR
3245 DestroyTree();
3246
e39de702 3247 if (GetOwner()->GetModel()->IsVirtualListModel())
a3cc79d9 3248 {
59e60167 3249 m_count = -1;
a3cc79d9
RR
3250 return;
3251 }
3252
51bdecff
RR
3253 m_root = new wxDataViewTreeNode( NULL );
3254 m_root->SetHasChildren(true);
3255
bc4f1ff2 3256 // First we define a invalid item to fetch the top-level elements
aba9bfd0 3257 wxDataViewItem item;
66e09788 3258 SortPrepare();
3b6280be 3259 BuildTreeHelper( model, item, m_root);
59e60167 3260 m_count = -1;
aba9bfd0
RR
3261}
3262
74123073 3263static void DestroyTreeHelper( wxDataViewTreeNode * node )
aba9bfd0 3264{
d47db7e0 3265 if( node->GetNodeNumber() != 0 )
aba9bfd0 3266 {
d47db7e0 3267 int len = node->GetNodeNumber();
74123073 3268 wxDataViewTreeNodes& nodes = node->GetNodes();
bc4f1ff2 3269 for (int i = 0; i < len; i++)
aba9bfd0 3270 DestroyTreeHelper(nodes[i]);
aba9bfd0
RR
3271 }
3272 delete node;
3273}
3274
3275void wxDataViewMainWindow::DestroyTree()
3276{
344ed1f3 3277 if (!IsVirtualList())
51bdecff 3278 {
bc4f1ff2
FM
3279 ::DestroyTreeHelper(m_root);
3280 m_count = 0;
3281 m_root = NULL;
51bdecff 3282 }
aba9bfd0
RR
3283}
3284
cab07038
RR
3285void wxDataViewMainWindow::OnChar( wxKeyEvent &event )
3286{
63ced01b
VZ
3287 wxWindow * const parent = GetParent();
3288
3289 // propagate the char event upwards
3290 wxKeyEvent eventForParent(event);
3291 eventForParent.SetEventObject(parent);
3292 if ( parent->ProcessWindowEvent(eventForParent) )
3293 return;
3294
3295 if ( parent->HandleAsNavigationKey(event) )
f029f1d1 3296 return;
cab07038
RR
3297
3298 // no item -> nothing to do
3299 if (!HasCurrentRow())
3300 {
3301 event.Skip();
3302 return;
3303 }
120b9b05 3304
cab07038
RR
3305 // don't use m_linesPerPage directly as it might not be computed yet
3306 const int pageSize = GetCountPerPage();
9a83f860 3307 wxCHECK_RET( pageSize, wxT("should have non zero page size") );
cab07038
RR
3308
3309 switch ( event.GetKeyCode() )
3310 {
d37b709c 3311 case WXK_RETURN:
d37b709c 3312 {
48ae48a9
VZ
3313 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED,
3314 parent->GetId());
d37b709c
RR
3315 le.SetItem( GetItemByRow(m_currentRow) );
3316 le.SetEventObject(parent);
3317 le.SetModel(GetOwner()->GetModel());
3318
3319 parent->GetEventHandler()->ProcessEvent(le);
3320 }
3321 break;
48ae48a9 3322
cab07038
RR
3323 case WXK_UP:
3324 if ( m_currentRow > 0 )
3325 OnArrowChar( m_currentRow - 1, event );
3326 break;
3327
3328 case WXK_DOWN:
4a851b11 3329 if ( m_currentRow < GetRowCount() - 1 )
cab07038
RR
3330 OnArrowChar( m_currentRow + 1, event );
3331 break;
bc4f1ff2 3332 // Add the process for tree expanding/collapsing
3b6280be 3333 case WXK_LEFT:
235d5f88
RR
3334 {
3335 if (IsVirtualList())
3336 break;
3337
3338 wxDataViewTreeNode* node = GetTreeNodeByRow(m_currentRow);
3339 if (!node)
3340 break;
3341
0a807957 3342 if (node->HasChildren() && node->IsOpen())
235d5f88
RR
3343 {
3344 Collapse(m_currentRow);
3345 }
0a807957 3346 else // if the node is already closed we move the selection to its parent
235d5f88
RR
3347 {
3348 wxDataViewTreeNode *parent_node = node->GetParent();
0a807957
RR
3349
3350 if(!node->HasChildren())
3351 delete node;
3352
235d5f88
RR
3353 if (parent_node)
3354 {
3355 int parent = GetRowByItem( parent_node->GetItem() );
3356 if ( parent >= 0 )
3357 {
3358 unsigned int row = m_currentRow;
3359 SelectRow( row, false);
3360 SelectRow( parent, true );
3361 ChangeCurrentRow( parent );
0a807957 3362 GetOwner()->EnsureVisible( parent, -1 );
235d5f88
RR
3363 SendSelectionChangedEvent( parent_node->GetItem() );
3364 }
3365 }
3366 }
3367 break;
3368 }
3369 case WXK_RIGHT:
3370 {
3371 if (!IsExpanded( m_currentRow ))
3372 Expand( m_currentRow );
3373 else
3374 {
3375 unsigned int row = m_currentRow;
3376 SelectRow( row, false );
3377 SelectRow( row + 1, true );
3378 ChangeCurrentRow( row + 1 );
0a807957 3379 GetOwner()->EnsureVisible( row + 1, -1 );
235d5f88
RR
3380 SendSelectionChangedEvent( GetItemByRow(row+1) );
3381 }
3382 break;
3383 }
cab07038 3384 case WXK_END:
235d5f88 3385 {
cab07038
RR
3386 if (!IsEmpty())
3387 OnArrowChar( GetRowCount() - 1, event );
3388 break;
235d5f88 3389 }
cab07038
RR
3390 case WXK_HOME:
3391 if (!IsEmpty())
3392 OnArrowChar( 0, event );
3393 break;
3394
3395 case WXK_PAGEUP:
3396 {
3397 int steps = pageSize - 1;
3398 int index = m_currentRow - steps;
3399 if (index < 0)
3400 index = 0;
3401
3402 OnArrowChar( index, event );
3403 }
3404 break;
3405
3406 case WXK_PAGEDOWN:
3407 {
3408 int steps = pageSize - 1;
0a71f9e9
RR
3409 unsigned int index = m_currentRow + steps;
3410 unsigned int count = GetRowCount();
cab07038
RR
3411 if ( index >= count )
3412 index = count - 1;
3413
3414 OnArrowChar( index, event );
3415 }
3416 break;
3417
0a807957
RR
3418 case WXK_F2:
3419 {
3420 if(m_selection.size() == 1)
3421 {
3422 // TODO: we need to revise that when we have a concept for a 'current column'
3423 GetOwner()->StartEditor(GetItemByRow(m_selection[0]), 0);
3424 }
3425 }
3426 break;
3427
cab07038
RR
3428 default:
3429 event.Skip();
3430 }
3431}
3432
4ed7af08
RR
3433void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
3434{
e21f75bd
RR
3435 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
3436 {
3437 // let the base handle mouse wheel events.
3438 event.Skip();
3439 return;
3440 }
3441
0fdc2321
RR
3442 int x = event.GetX();
3443 int y = event.GetY();
3444 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
0fdc2321 3445 wxDataViewColumn *col = NULL;
b7fe2261 3446
0fdc2321 3447 int xpos = 0;
9861f022 3448 unsigned int cols = GetOwner()->GetColumnCount();
0a71f9e9 3449 unsigned int i;
0fdc2321
RR
3450 for (i = 0; i < cols; i++)
3451 {
702f5349 3452 wxDataViewColumn *c = GetOwner()->GetColumnAt( i );
9861f022
RR
3453 if (c->IsHidden())
3454 continue; // skip it!
3455
0fdc2321
RR
3456 if (x < xpos + c->GetWidth())
3457 {
3458 col = c;
3459 break;
3460 }
3461 xpos += c->GetWidth();
3462 }
f554a14b 3463 if (!col)
0fdc2321 3464 return;
f554a14b 3465
24c4a50f 3466 wxDataViewRenderer *cell = col->GetRenderer();
344ed1f3 3467 unsigned int current = GetLineAt( y );
5179bc0b 3468 if ((current >= GetRowCount()) || (x > GetEndOfLastCol()))
e21f75bd
RR
3469 {
3470 // Unselect all if below the last row ?
3471 return;
3472 }
f554a14b 3473
bc4f1ff2 3474 // Test whether the mouse is hovered on the tree item button
abdb8c18 3475 bool hoverOverExpander = false;
344ed1f3 3476 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col))
24c4a50f
RR
3477 {
3478 wxDataViewTreeNode * node = GetTreeNodeByRow(current);
3479 if( node!=NULL && node->HasChildren() )
3480 {
3481 int indent = node->GetIndentLevel();
3482 indent = GetOwner()->GetIndent()*indent;
abdb8c18 3483
977a41ec
FM
3484 // we make the rectangle we are looking in a bit bigger than the actual
3485 // visual expander so the user can hit that little thing reliably
abdb8c18 3486 wxRect rect( xpos + indent,
977a41ec
FM
3487 GetLineStart( current ) + (GetLineHeight(current) - m_lineHeight)/2,
3488 m_lineHeight, m_lineHeight);
abdb8c18 3489 if( rect.Contains(x, y) )
24c4a50f 3490 {
bc4f1ff2 3491 // So the mouse is over the expander
abdb8c18 3492 hoverOverExpander = true;
24c4a50f
RR
3493 if (m_underMouse && m_underMouse != node)
3494 {
bc4f1ff2 3495 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
df2c23e7 3496 RefreshRow(GetRowByItem(m_underMouse->GetItem()));
24c4a50f
RR
3497 }
3498 if (m_underMouse != node)
3499 {
bc4f1ff2 3500 // wxLogMessage("Do the row: %d", current);
df2c23e7 3501 RefreshRow(current);
24c4a50f
RR
3502 }
3503 m_underMouse = node;
3504 }
3505 }
438fb233 3506 if (node!=NULL && !node->HasChildren())
24c4a50f
RR
3507 delete node;
3508 }
abdb8c18 3509 if (!hoverOverExpander)
24c4a50f
RR
3510 {
3511 if (m_underMouse != NULL)
3512 {
bc4f1ff2 3513 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
df2c23e7 3514 RefreshRow(GetRowByItem(m_underMouse->GetItem()));
24c4a50f
RR
3515 m_underMouse = NULL;
3516 }
3517 }
3518
aba9bfd0 3519 wxDataViewModel *model = GetOwner()->GetModel();
0fdc2321 3520
9adeb77a 3521#if wxUSE_DRAG_AND_DROP
e21f75bd
RR
3522 if (event.Dragging())
3523 {
3524 if (m_dragCount == 0)
3525 {
3526 // we have to report the raw, physical coords as we want to be
3527 // able to call HitTest(event.m_pointDrag) from the user code to
3528 // get the item being dragged
3529 m_dragStart = event.GetPosition();
3530 }
3531
3532 m_dragCount++;
3533
3534 if (m_dragCount != 3)
3535 return;
3536
3537 if (event.LeftIsDown())
3538 {
4cef3873 3539 m_owner->CalcUnscrolledPosition( m_dragStart.x, m_dragStart.y,
bc4f1ff2 3540 &m_dragStart.x, &m_dragStart.y );
821baf7d
RR
3541 unsigned int drag_item_row = GetLineAt( m_dragStart.y );
3542 wxDataViewItem item = GetItemByRow( drag_item_row );
3543
e21f75bd 3544 // Notify cell about drag
821baf7d
RR
3545 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() );
3546 event.SetEventObject( m_owner );
3547 event.SetItem( item );
3548 event.SetModel( model );
3549 if (!m_owner->HandleWindowEvent( event ))
3550 return;
9adeb77a 3551
821baf7d
RR
3552 if (!event.IsAllowed())
3553 return;
9adeb77a 3554
821baf7d
RR
3555 wxDataObject *obj = event.GetDataObject();
3556 if (!obj)
3557 return;
9adeb77a 3558
818d91a9 3559 wxDataViewDropSource drag( this, drag_item_row );
592883ed 3560 drag.SetData( *obj );
818d91a9 3561 /* wxDragResult res = */ drag.DoDragDrop();
592883ed 3562 delete obj;
e21f75bd
RR
3563 }
3564 return;
3565 }
3566 else
3567 {
3568 m_dragCount = 0;
3569 }
9adeb77a 3570#endif // wxUSE_DRAG_AND_DROP
e21f75bd 3571
abdb8c18 3572 bool simulateClick = false;
e21f75bd 3573
0fcce6b9
RR
3574 if (event.ButtonDClick())
3575 {
3576 m_renameTimer->Stop();
3577 m_lastOnSame = false;
3578 }
3579
438fb233
RR
3580 wxDataViewItem item = GetItemByRow(current);
3581 bool ignore_other_columns =
3582 ((GetOwner()->GetExpanderColumn() != col) &&
977a41ec
FM
3583 (model->IsContainer(item)) &&
3584 (!model->HasContainerColumns(item)));
b5ec7dd6 3585
0fdc2321
RR
3586 if (event.LeftDClick())
3587 {
977a41ec
FM
3588 if(hoverOverExpander)
3589 {
3590 // a double click on the expander will be converted into a "simulated" normal click
3591 simulateClick = true;
3592 }
3593 else if ( current == m_lineLastClicked )
0fdc2321 3594 {
438fb233 3595 if ((!ignore_other_columns) && (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE))
e21f75bd 3596 {
62186006
VZ
3597 const unsigned colIdx = col->GetModelColumn();
3598
3599 wxVariant value;
3600 model->GetValue( value, item, colIdx );
3601
3602 cell->WXOnActivate(model, value, item, colIdx);
3603
6eec70b9
VZ
3604 if ( wxDataViewCustomRenderer *custom = cell->WXGetAsCustom() )
3605 {
62186006
VZ
3606 cell->SetValue( value );
3607
6eec70b9
VZ
3608 wxRect cell_rect( xpos, GetLineStart( current ),
3609 col->GetWidth(), GetLineHeight( current ) );
62186006 3610 custom->Activate( cell_rect, model, item, colIdx );
6eec70b9 3611 }
2c3f6edd
RR
3612 }
3613 else
3614 {
b7e9f8b1
RR
3615 wxWindow *parent = GetParent();
3616 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, parent->GetId());
45c156e0 3617 le.SetItem( item );
b7e9f8b1 3618 le.SetEventObject(parent);
b7e9f8b1
RR
3619 le.SetModel(GetOwner()->GetModel());
3620
3621 parent->GetEventHandler()->ProcessEvent(le);
e21f75bd
RR
3622 }
3623 return;
3624 }
3625 else
3626 {
3627 // The first click was on another item, so don't interpret this as
3628 // a double click, but as a simple click instead
abdb8c18 3629 simulateClick = true;
0fdc2321 3630 }
120b9b05 3631 }
f554a14b 3632
abdb8c18 3633 if (event.LeftUp() && !hoverOverExpander)
0fcce6b9 3634 {
0a71f9e9 3635 if (m_lineSelectSingleOnUp != (unsigned int)-1)
e21f75bd
RR
3636 {
3637 // select single line
3638 SelectAllRows( false );
3639 SelectRow( m_lineSelectSingleOnUp, true );
4a745e76 3640 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp) );
e21f75bd 3641 }
120b9b05 3642
4cef3873 3643 // If the user click the expander, we do not do editing even if the column
bc4f1ff2 3644 // with expander are editable
abdb8c18 3645 if (m_lastOnSame && !ignore_other_columns)
0fcce6b9 3646 {
a8461d31 3647 if ((col == m_currentCol) && (current == m_currentRow) &&
0bdfa388 3648 (cell->GetMode() & wxDATAVIEW_CELL_EDITABLE) )
0fcce6b9
RR
3649 {
3650 m_renameTimer->Start( 100, true );
3651 }
3652 }
3653
3654 m_lastOnSame = false;
0a71f9e9 3655 m_lineSelectSingleOnUp = (unsigned int)-1;
120b9b05 3656 }
abdb8c18 3657 else if(!event.LeftUp())
e21f75bd
RR
3658 {
3659 // This is necessary, because after a DnD operation in
3660 // from and to ourself, the up event is swallowed by the
3661 // DnD code. So on next non-up event (which means here and
3662 // now) m_lineSelectSingleOnUp should be reset.
0a71f9e9 3663 m_lineSelectSingleOnUp = (unsigned int)-1;
e21f75bd
RR
3664 }
3665
3666 if (event.RightDown())
3667 {
3668 m_lineBeforeLastClicked = m_lineLastClicked;
3669 m_lineLastClicked = current;
3670
3671 // If the item is already selected, do not update the selection.
3672 // Multi-selections should not be cleared if a selected item is clicked.
3673 if (!IsRowSelected(current))
3674 {
3675 SelectAllRows(false);
3676 ChangeCurrentRow(current);
3677 SelectRow(m_currentRow,true);
526e19e2 3678 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
e21f75bd 3679 }
0a807957
RR
3680 }
3681 else if (event.RightUp())
3682 {
f210b1b5
RR
3683 wxVariant value;
3684 model->GetValue( value, item, col->GetModelColumn() );
0bdfa388
RR
3685 wxWindow *parent = GetParent();
3686 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, parent->GetId());
3687 le.SetItem( item );
3688 le.SetEventObject(parent);
3689 le.SetModel(GetOwner()->GetModel());
3690 le.SetValue(value);
3691 parent->GetEventHandler()->ProcessEvent(le);
e21f75bd
RR
3692 }
3693 else if (event.MiddleDown())
3694 {
e21f75bd 3695 }
abdb8c18 3696
977a41ec
FM
3697 if((event.LeftDown() || simulateClick) && hoverOverExpander)
3698 {
3699 wxDataViewTreeNode* node = GetTreeNodeByRow(current);
bc4f1ff2 3700
4cef3873 3701 // hoverOverExpander being true tells us that our node must be
bc4f1ff2 3702 // valid and have children.
977a41ec
FM
3703 // So we don't need any extra checks.
3704 if( node->IsOpen() )
235d5f88 3705 Collapse(current);
977a41ec 3706 else
235d5f88 3707 Expand(current);
977a41ec
FM
3708 }
3709 else if ((event.LeftDown() || simulateClick) && !hoverOverExpander)
0fcce6b9 3710 {
72664514 3711 SetFocus();
120b9b05 3712
e21f75bd
RR
3713 m_lineBeforeLastClicked = m_lineLastClicked;
3714 m_lineLastClicked = current;
3715
0a71f9e9 3716 unsigned int oldCurrentRow = m_currentRow;
e21f75bd
RR
3717 bool oldWasSelected = IsRowSelected(m_currentRow);
3718
3719 bool cmdModifierDown = event.CmdDown();
3720 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
3721 {
3722 if ( IsSingleSel() || !IsRowSelected(current) )
3723 {
3724 SelectAllRows( false );
e21f75bd 3725 ChangeCurrentRow(current);
e21f75bd 3726 SelectRow(m_currentRow,true);
526e19e2 3727 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
e21f75bd
RR
3728 }
3729 else // multi sel & current is highlighted & no mod keys
3730 {
3731 m_lineSelectSingleOnUp = current;
3732 ChangeCurrentRow(current); // change focus
3733 }
3734 }
3735 else // multi sel & either ctrl or shift is down
3736 {
3737 if (cmdModifierDown)
3738 {
3739 ChangeCurrentRow(current);
e21f75bd 3740 ReverseRowSelection(m_currentRow);
9439bc69 3741 SendSelectionChangedEvent(GetItemByRow(m_currentRow));
e21f75bd
RR
3742 }
3743 else if (event.ShiftDown())
3744 {
3745 ChangeCurrentRow(current);
3746
0a71f9e9 3747 unsigned int lineFrom = oldCurrentRow,
977a41ec 3748 lineTo = current;
e21f75bd
RR
3749
3750 if ( lineTo < lineFrom )
3751 {
3752 lineTo = lineFrom;
3753 lineFrom = m_currentRow;
3754 }
3755
3756 SelectRows(lineFrom, lineTo, true);
526e19e2 3757 SendSelectionChangedEvent(GetItemByRow(m_selection[0]) );
e21f75bd
RR
3758 }
3759 else // !ctrl, !shift
3760 {
3761 // test in the enclosing if should make it impossible
9a83f860 3762 wxFAIL_MSG( wxT("how did we get here?") );
e21f75bd
RR
3763 }
3764 }
777f9cef 3765
72664514
RR
3766 if (m_currentRow != oldCurrentRow)
3767 RefreshRow( oldCurrentRow );
e21f75bd 3768
0fcce6b9 3769 wxDataViewColumn *oldCurrentCol = m_currentCol;
120b9b05 3770
0fcce6b9
RR
3771 // Update selection here...
3772 m_currentCol = col;
120b9b05 3773
abdb8c18 3774 m_lastOnSame = !simulateClick && ((col == oldCurrentCol) &&
87f0efe2 3775 (current == oldCurrentRow)) && oldWasSelected;
0bdfa388
RR
3776
3777 // Call LeftClick after everything else as under GTK+
3778 if (cell->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE)
3779 {
6eec70b9
VZ
3780 if ( wxDataViewCustomRenderer *custom = cell->WXGetAsCustom() )
3781 {
3782 // notify cell about click
3783 wxVariant value;
3784 model->GetValue( value, item, col->GetModelColumn() );
3785 custom->SetValue( value );
3786 wxRect cell_rect( xpos, GetLineStart( current ),
3787 col->GetWidth(), GetLineHeight( current ) );
3788 /* ignore ret */ custom->LeftClick( event.GetPosition(), cell_rect,
3789 model, item, col->GetModelColumn());
3790 }
0bdfa388 3791 }
0fdc2321 3792 }
4ed7af08
RR
3793}
3794
3795void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event )
3796{
cab07038 3797 m_hasFocus = true;
120b9b05 3798
cab07038
RR
3799 if (HasCurrentRow())
3800 Refresh();
120b9b05 3801
cab07038
RR
3802 event.Skip();
3803}
3804
3805void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event )
3806{
3807 m_hasFocus = false;
120b9b05 3808
cab07038
RR
3809 if (HasCurrentRow())
3810 Refresh();
120b9b05 3811
4ed7af08
RR
3812 event.Skip();
3813}
3814
fbda518c 3815wxDataViewItem wxDataViewMainWindow::GetSelection() const
704c3490
RR
3816{
3817 if( m_selection.GetCount() != 1 )
3818 return wxDataViewItem();
d47db7e0
RR
3819
3820 return GetItemByRow( m_selection.Item(0));
704c3490
RR
3821}
3822
4ed7af08
RR
3823//-----------------------------------------------------------------------------
3824// wxDataViewCtrl
3825//-----------------------------------------------------------------------------
bc4f1ff2 3826
a76c2f37 3827WX_DEFINE_LIST(wxDataViewColumnList)
4ed7af08
RR
3828
3829IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
4b3feaa7
RR
3830BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase)
3831 EVT_SIZE(wxDataViewCtrl::OnSize)
3832END_EVENT_TABLE()
3833
4ed7af08
RR
3834wxDataViewCtrl::~wxDataViewCtrl()
3835{
3836 if (m_notifier)
3837 GetModel()->RemoveNotifier( m_notifier );
74123073 3838
b3a3c9d8 3839 m_cols.Clear();
4ed7af08
RR
3840}
3841
3842void wxDataViewCtrl::Init()
3843{
b3a3c9d8 3844 m_cols.DeleteContents(true);
4ed7af08 3845 m_notifier = NULL;
e822d1bd 3846
236a34ef 3847 // No sorting column at start
46234a03
VZ
3848 m_sortingColumnIdx = wxNOT_FOUND;
3849
236a34ef 3850 m_headerArea = NULL;
4ed7af08
RR
3851}
3852
3853bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
bc4f1ff2
FM
3854 const wxPoint& pos, const wxSize& size,
3855 long style, const wxValidator& validator )
4ed7af08 3856{
3fdf86f9
RR
3857// if ( (style & wxBORDER_MASK) == 0)
3858// style |= wxBORDER_SUNKEN;
777f9cef 3859
236a34ef
RR
3860 Init();
3861
c741d33f 3862 if (!wxControl::Create( parent, id, pos, size,
008e999e 3863 style | wxScrolledWindowStyle, validator))
4b3feaa7
RR
3864 return false;
3865
b89cac3f 3866 SetInitialSize(size);
b5ec7dd6 3867
4ed7af08 3868#ifdef __WXMAC__
59e60167 3869 MacSetClipChildren( true );
4ed7af08
RR
3870#endif
3871
f554a14b 3872 m_clientArea = new wxDataViewMainWindow( this, wxID_ANY );
9861f022 3873
63ced01b
VZ
3874 // We use the cursor keys for moving the selection, not scrolling, so call
3875 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
3876 // keyboard events forwarded to us from wxListMainWindow.
3877 DisableKeyboardScrolling();
3878
9861f022
RR
3879 if (HasFlag(wxDV_NO_HEADER))
3880 m_headerArea = NULL;
3881 else
56873923 3882 m_headerArea = new wxDataViewHeaderWindow(this);
f554a14b 3883
4ed7af08 3884 SetTargetWindow( m_clientArea );
f554a14b 3885
4ed7af08 3886 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
9861f022
RR
3887 if (m_headerArea)
3888 sizer->Add( m_headerArea, 0, wxGROW );
4ed7af08
RR
3889 sizer->Add( m_clientArea, 1, wxGROW );
3890 SetSizer( sizer );
b5ec7dd6 3891
4ed7af08
RR
3892 return true;
3893}
3894
3fdf86f9
RR
3895wxBorder wxDataViewCtrl::GetDefaultBorder() const
3896{
3897 return wxBORDER_THEME;
3898}
3899
4ed7af08
RR
3900#ifdef __WXMSW__
3901WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg,
bc4f1ff2
FM
3902 WXWPARAM wParam,
3903 WXLPARAM lParam)
4ed7af08 3904{
b910a8ad 3905 WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
4ed7af08
RR
3906
3907#ifndef __WXWINCE__
3908 // we need to process arrows ourselves for scrolling
3909 if ( nMsg == WM_GETDLGCODE )
3910 {
3911 rc |= DLGC_WANTARROWS;
3912 }
3913#endif
3914
3915 return rc;
3916}
3917#endif
3918
2571a33f
RR
3919wxSize wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize& size)
3920{
3921 wxSize newsize = size;
d7cda9b2 3922 if (!HasFlag(wxDV_NO_HEADER) && (m_headerArea))
977a41ec 3923 newsize.y -= m_headerArea->GetSize().y;
e822d1bd 3924
2571a33f
RR
3925 return newsize;
3926}
3927
f554a14b 3928void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
4ed7af08 3929{
4b3feaa7
RR
3930 // We need to override OnSize so that our scrolled
3931 // window a) does call Layout() to use sizers for
3932 // positioning the controls but b) does not query
3933 // the sizer for their size and use that for setting
3934 // the scrollable area as set that ourselves by
3935 // calling SetScrollbar() further down.
3936
3937 Layout();
3938
3939 AdjustScrollbars();
4ed7af08
RR
3940}
3941
788432e3
RR
3942void wxDataViewCtrl::SetFocus()
3943{
3944 if (m_clientArea)
3945 m_clientArea->SetFocus();
3946}
3947
aba9bfd0 3948bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
4ed7af08
RR
3949{
3950 if (!wxDataViewCtrlBase::AssociateModel( model ))
3951 return false;
3952
aba9bfd0 3953 m_notifier = new wxGenericDataViewModelNotifier( m_clientArea );
4ed7af08 3954
f554a14b 3955 model->AddNotifier( m_notifier );
4ed7af08 3956
51bdecff 3957 m_clientArea->DestroyTree();
cfa42cb8 3958
aba9bfd0
RR
3959 m_clientArea->BuildTree(model);
3960
4b3feaa7 3961 m_clientArea->UpdateDisplay();
f554a14b 3962
4ed7af08
RR
3963 return true;
3964}
3965
9adeb77a
VZ
3966#if wxUSE_DRAG_AND_DROP
3967
821baf7d
RR
3968bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format )
3969{
3970 return m_clientArea->EnableDragSource( format );
3971}
3972
3973bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format )
3974{
3975 return m_clientArea->EnableDropTarget( format );
3976}
3977
9adeb77a
VZ
3978#endif // wxUSE_DRAG_AND_DROP
3979
4ed7af08
RR
3980bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
3981{
3982 if (!wxDataViewCtrlBase::AppendColumn(col))
3983 return false;
f554a14b 3984
afebb87b 3985 m_cols.Append( col );
aef252d9 3986 OnColumnsCountChanged();
736fe67c
RR
3987 return true;
3988}
3989
3990bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
3991{
3992 if (!wxDataViewCtrlBase::PrependColumn(col))
3993 return false;
3994
3995 m_cols.Insert( col );
aef252d9 3996 OnColumnsCountChanged();
4ed7af08
RR
3997 return true;
3998}
3999
19723525
RR
4000bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
4001{
4002 if (!wxDataViewCtrlBase::InsertColumn(pos,col))
4003 return false;
4004
4005 m_cols.Insert( pos, col );
aef252d9 4006 OnColumnsCountChanged();
19723525
RR
4007 return true;
4008}
4009
aef252d9
VZ
4010void wxDataViewCtrl::OnColumnChange(unsigned int idx)
4011{
4012 if ( m_headerArea )
4013 m_headerArea->UpdateColumn(idx);
4014
4015 m_clientArea->UpdateDisplay();
4016}
4017
4018void wxDataViewCtrl::OnColumnsCountChanged()
9861f022
RR
4019{
4020 if (m_headerArea)
e2bfe673 4021 m_headerArea->SetColumnCount(GetColumnCount());
9861f022
RR
4022
4023 m_clientArea->UpdateDisplay();
4024}
3b6280be
RR
4025
4026void wxDataViewCtrl::DoSetExpanderColumn()
4027{
4028 m_clientArea->UpdateDisplay();
4029}
4030
4031void wxDataViewCtrl::DoSetIndent()
4032{
4033 m_clientArea->UpdateDisplay();
4034}
4035
afebb87b
RR
4036unsigned int wxDataViewCtrl::GetColumnCount() const
4037{
4038 return m_cols.GetCount();
4039}
4040
aef252d9 4041wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int idx ) const
afebb87b 4042{
aef252d9 4043 return m_cols[idx];
afebb87b
RR
4044}
4045
702f5349 4046wxDataViewColumn *wxDataViewCtrl::GetColumnAt(unsigned int pos) const
fc8c1a15 4047{
702f5349
VZ
4048 // columns can't be reordered if there is no header window which allows
4049 // to do this
4050 const unsigned idx = m_headerArea ? m_headerArea->GetColumnsOrder()[pos]
977a41ec 4051 : pos;
59e60167 4052
702f5349
VZ
4053 return GetColumn(idx);
4054}
cfa42cb8 4055
702f5349
VZ
4056int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn *column) const
4057{
4058 const unsigned count = m_cols.size();
4059 for ( unsigned n = 0; n < count; n++ )
4060 {
4061 if ( m_cols[n] == column )
4062 return n;
4063 }
4064
4065 return wxNOT_FOUND;
4066}
4067
4068void wxDataViewCtrl::ColumnMoved(wxDataViewColumn * WXUNUSED(col),
977a41ec 4069 unsigned int WXUNUSED(new_pos))
702f5349
VZ
4070{
4071 // do _not_ reorder m_cols elements here, they should always be in the
4072 // order in which columns were added, we only display the columns in
4073 // different order
fc8c1a15
RR
4074 m_clientArea->UpdateDisplay();
4075}
4076
afebb87b
RR
4077bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
4078{
b3a3c9d8 4079 wxDataViewColumnList::compatibility_iterator ret = m_cols.Find( column );
4264606e 4080 if (!ret)
afebb87b
RR
4081 return false;
4082
b7fe2261 4083 m_cols.Erase(ret);
aef252d9 4084 OnColumnsCountChanged();
afebb87b
RR
4085
4086 return true;
4087}
4088
4089bool wxDataViewCtrl::ClearColumns()
4090{
b3a3c9d8 4091 m_cols.Clear();
aef252d9 4092 OnColumnsCountChanged();
afebb87b
RR
4093 return true;
4094}
4095
453091c2
RR
4096int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
4097{
5d9e1605
RR
4098#if 1
4099 unsigned int len = GetColumnCount();
4100 for ( unsigned int i = 0; i < len; i++ )
4101 {
4102 wxDataViewColumn * col = GetColumnAt(i);
4103 if (column==col)
4104 return i;
4105 }
4106
4107 return wxNOT_FOUND;
4108#else
4109 // This returns the position in pixels which is not what we want.
702f5349
VZ
4110 int ret = 0,
4111 dummy = 0;
4112 unsigned int len = GetColumnCount();
4113 for ( unsigned int i = 0; i < len; i++ )
526e19e2 4114 {
702f5349 4115 wxDataViewColumn * col = GetColumnAt(i);
526e19e2
RR
4116 if (col->IsHidden())
4117 continue;
4118 ret += col->GetWidth();
4119 if (column==col)
4120 {
702f5349 4121 CalcScrolledPosition( ret, dummy, &ret, &dummy );
526e19e2
RR
4122 break;
4123 }
4124 }
4125 return ret;
5d9e1605 4126#endif
453091c2
RR
4127}
4128
21f47fb9
RR
4129wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
4130{
46234a03 4131 return m_sortingColumnIdx == wxNOT_FOUND ? NULL
977a41ec 4132 : GetColumn(m_sortingColumnIdx);
21f47fb9
RR
4133}
4134
bc4f1ff2 4135// Selection code with wxDataViewItem as parameters
fbda518c 4136wxDataViewItem wxDataViewCtrl::GetSelection() const
66e09788
RR
4137{
4138 return m_clientArea->GetSelection();
4139}
4140
b7e9f8b1
RR
4141int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
4142{
4143 sel.Empty();
4144 wxDataViewSelection selection = m_clientArea->GetSelections();
4145 int len = selection.GetCount();
4146 for( int i = 0; i < len; i ++)
4147 {
4148 unsigned int row = selection[i];
4149 sel.Add( m_clientArea->GetItemByRow( row ) );
4150 }
4151 return len;
4152}
4153
4154void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
4155{
59e60167 4156 wxDataViewSelection selection(wxDataViewSelectionCmp);
4219d8b0
RR
4157
4158 wxDataViewItem last_parent;
4159
b7e9f8b1
RR
4160 int len = sel.GetCount();
4161 for( int i = 0; i < len; i ++ )
4162 {
4219d8b0
RR
4163 wxDataViewItem item = sel[i];
4164 wxDataViewItem parent = GetModel()->GetParent( item );
4165 if (parent)
4166 {
4167 if (parent != last_parent)
4168 ExpandAncestors(item);
4169 }
9adeb77a 4170
4219d8b0
RR
4171 last_parent = parent;
4172 int row = m_clientArea->GetRowByItem( item );
b7e9f8b1
RR
4173 if( row >= 0 )
4174 selection.Add( static_cast<unsigned int>(row) );
4175 }
9adeb77a 4176
b7e9f8b1
RR
4177 m_clientArea->SetSelections( selection );
4178}
4179
4180void wxDataViewCtrl::Select( const wxDataViewItem & item )
704c3490 4181{
4219d8b0 4182 ExpandAncestors( item );
9adeb77a 4183
b7e9f8b1
RR
4184 int row = m_clientArea->GetRowByItem( item );
4185 if( row >= 0 )
57f2a652 4186 {
bc4f1ff2 4187 // Unselect all rows before select another in the single select mode
57f2a652
RR
4188 if (m_clientArea->IsSingleSel())
4189 m_clientArea->SelectAllRows(false);
bc6ca1a9 4190
b7e9f8b1 4191 m_clientArea->SelectRow(row, true);
bc6ca1a9 4192
de67922e
RR
4193 // Also set focus to the selected item
4194 m_clientArea->ChangeCurrentRow( row );
57f2a652 4195 }
704c3490 4196}
3b6280be 4197
b7e9f8b1 4198void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
6ff7eee7 4199{
b7e9f8b1
RR
4200 int row = m_clientArea->GetRowByItem( item );
4201 if( row >= 0 )
4202 m_clientArea->SelectRow(row, false);
6ff7eee7
RR
4203}
4204
b7e9f8b1 4205bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
6ff7eee7 4206{
b7e9f8b1
RR
4207 int row = m_clientArea->GetRowByItem( item );
4208 if( row >= 0 )
4209 {
4210 return m_clientArea->IsRowSelected(row);
4211 }
4212 return false;
6ff7eee7
RR
4213}
4214
bc4f1ff2 4215// Selection code with row number as parameter
b7e9f8b1 4216int wxDataViewCtrl::GetSelections( wxArrayInt & sel ) const
6ff7eee7 4217{
b7e9f8b1
RR
4218 sel.Empty();
4219 wxDataViewSelection selection = m_clientArea->GetSelections();
4220 int len = selection.GetCount();
4221 for( int i = 0; i < len; i ++)
4222 {
4223 unsigned int row = selection[i];
4224 sel.Add( row );
4225 }
4226 return len;
6ff7eee7 4227}
df387169 4228
b7e9f8b1 4229void wxDataViewCtrl::SetSelections( const wxArrayInt & sel )
fc211fe5 4230{
59e60167 4231 wxDataViewSelection selection(wxDataViewSelectionCmp);
b7e9f8b1
RR
4232 int len = sel.GetCount();
4233 for( int i = 0; i < len; i ++ )
4234 {
4235 int row = sel[i];
4236 if( row >= 0 )
4237 selection.Add( static_cast<unsigned int>(row) );
4238 }
4239 m_clientArea->SetSelections( selection );
fc211fe5
RR
4240}
4241
b7e9f8b1 4242void wxDataViewCtrl::Select( int row )
6ff7eee7 4243{
b7e9f8b1 4244 if( row >= 0 )
57f2a652
RR
4245 {
4246 if (m_clientArea->IsSingleSel())
4247 m_clientArea->SelectAllRows(false);
b7e9f8b1 4248 m_clientArea->SelectRow( row, true );
57f2a652 4249 }
b7e9f8b1 4250}
df387169 4251
b7e9f8b1
RR
4252void wxDataViewCtrl::Unselect( int row )
4253{
4254 if( row >= 0 )
4255 m_clientArea->SelectRow(row, false);
4256}
4257
4258bool wxDataViewCtrl::IsSelected( int row ) const
4259{
4260 if( row >= 0 )
4261 return m_clientArea->IsRowSelected(row);
6ff7eee7
RR
4262 return false;
4263}
4264
b7e9f8b1 4265void wxDataViewCtrl::SelectRange( int from, int to )
6ff7eee7 4266{
b7fe2261 4267 wxArrayInt sel;
b7e9f8b1
RR
4268 for( int i = from; i < to; i ++ )
4269 sel.Add( i );
4270 m_clientArea->Select(sel);
4271}
df387169 4272
b7e9f8b1
RR
4273void wxDataViewCtrl::UnselectRange( int from, int to )
4274{
4275 wxDataViewSelection sel = m_clientArea->GetSelections();
4276 for( int i = from; i < to; i ++ )
4277 if( sel.Index( i ) != wxNOT_FOUND )
4278 sel.Remove( i );
4279 m_clientArea->SetSelections(sel);
6ff7eee7
RR
4280}
4281
b7e9f8b1 4282void wxDataViewCtrl::SelectAll()
6ff7eee7 4283{
b7e9f8b1
RR
4284 m_clientArea->SelectAllRows(true);
4285}
df387169 4286
b7e9f8b1
RR
4287void wxDataViewCtrl::UnselectAll()
4288{
4289 m_clientArea->SelectAllRows(false);
6ff7eee7 4290}
b7e9f8b1 4291
fbda518c 4292void wxDataViewCtrl::EnsureVisible( int row, int column )
b7e9f8b1 4293{
fbda518c
RR
4294 if( row < 0 )
4295 row = 0;
67be459b 4296 if( row > (int) m_clientArea->GetRowCount() )
fbda518c
RR
4297 row = m_clientArea->GetRowCount();
4298
4299 int first = m_clientArea->GetFirstVisibleRow();
4300 int last = m_clientArea->GetLastVisibleRow();
4301 if( row < first )
4302 m_clientArea->ScrollTo( row, column );
4303 else if( row > last )
4304 m_clientArea->ScrollTo( row - last + first, column );
4305 else
4306 m_clientArea->ScrollTo( first, column );
b7e9f8b1
RR
4307}
4308
fbda518c 4309void wxDataViewCtrl::EnsureVisible( const wxDataViewItem & item, const wxDataViewColumn * column )
b7e9f8b1 4310{
4219d8b0 4311 ExpandAncestors( item );
9adeb77a 4312
13499080
RR
4313 m_clientArea->RecalculateDisplay();
4314
b7e9f8b1
RR
4315 int row = m_clientArea->GetRowByItem(item);
4316 if( row >= 0 )
fbda518c
RR
4317 {
4318 if( column == NULL )
9bcc8016 4319 EnsureVisible(row, -1);
fbda518c 4320 else
702f5349 4321 EnsureVisible( row, GetColumnIndex(column) );
fbda518c 4322 }
b7fe2261 4323
b7e9f8b1
RR
4324}
4325
4cef3873 4326void wxDataViewCtrl::HitTest( const wxPoint & point, wxDataViewItem & item,
bc4f1ff2 4327 wxDataViewColumn* &column ) const
66e09788
RR
4328{
4329 m_clientArea->HitTest(point, item, column);
4330}
4331
4cef3873 4332wxRect wxDataViewCtrl::GetItemRect( const wxDataViewItem & item,
bc4f1ff2 4333 const wxDataViewColumn* column ) const
66e09788
RR
4334{
4335 return m_clientArea->GetItemRect(item, column);
4336}
4337
b7e9f8b1
RR
4338wxDataViewItem wxDataViewCtrl::GetItemByRow( unsigned int row ) const
4339{
4340 return m_clientArea->GetItemByRow( row );
4341}
4342
4343int wxDataViewCtrl::GetRowByItem( const wxDataViewItem & item ) const
4344{
4345 return m_clientArea->GetRowByItem( item );
4346}
4347
afebb87b
RR
4348void wxDataViewCtrl::Expand( const wxDataViewItem & item )
4349{
4350 int row = m_clientArea->GetRowByItem( item );
4351 if (row != -1)
4352 m_clientArea->Expand(row);
4353}
4354
4355void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
4356{
4357 int row = m_clientArea->GetRowByItem( item );
4358 if (row != -1)
b7fe2261 4359 m_clientArea->Collapse(row);
afebb87b
RR
4360}
4361
739a8399
RR
4362bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const
4363{
4364 int row = m_clientArea->GetRowByItem( item );
4365 if (row != -1)
4366 return m_clientArea->IsExpanded(row);
4367 return false;
4368}
4369
c937344c
RR
4370void wxDataViewCtrl::StartEditor( const wxDataViewItem & item, unsigned int column )
4371{
4372 wxDataViewColumn* col = GetColumn( column );
4373 if (!col)
4374 return;
4cef3873 4375
c937344c
RR
4376 wxRect itemRect = GetItemRect(item, col);
4377 wxDataViewRenderer* renderer = col->GetRenderer();
4378 renderer->StartEditing(item, itemRect);
4379}
4380
4cef3873 4381#endif // !wxUSE_GENERICDATAVIEWCTRL
4ed7af08 4382
4cef3873 4383#endif // wxUSE_DATAVIEWCTRL