]> git.saurik.com Git - wxWidgets.git/blame - src/generic/datavgen.cpp
Disable wxUSE_ENH_METAFILE for wxGTK builds.
[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 41#include "wx/popupwin.h"
4ed7af08 42#include "wx/renderer.h"
2e992e06 43#include "wx/dcbuffer.h"
2586d4a1 44#include "wx/icon.h"
351461fc
RR
45#include "wx/list.h"
46#include "wx/listimpl.cpp"
0c8ed3eb 47#include "wx/imaglist.h"
e2bfe673 48#include "wx/headerctrl.h"
592883ed 49#include "wx/dnd.h"
0645bd38 50#include "wx/stopwatch.h"
64ac3db8 51#include "wx/weakref.h"
4ed7af08 52
4ed7af08
RR
53//-----------------------------------------------------------------------------
54// classes
55//-----------------------------------------------------------------------------
56
5d9e1605
RR
57class wxDataViewColumn;
58class wxDataViewHeaderWindow;
4ed7af08
RR
59class wxDataViewCtrl;
60
5d9e1605
RR
61//-----------------------------------------------------------------------------
62// classes
63//-----------------------------------------------------------------------------
64
9861f022
RR
65static const int SCROLL_UNIT_X = 15;
66
67// the cell padding on the left/right
68static const int PADDING_RIGHTLEFT = 3;
69
3b6280be
RR
70// the expander space margin
71static const int EXPANDER_MARGIN = 4;
9861f022 72
344ed1f3
RR
73#ifdef __WXMSW__
74static const int EXPANDER_OFFSET = 4;
75#else
76static const int EXPANDER_OFFSET = 1;
77#endif
78
bc4f1ff2 79// Below is the compare stuff.
4cef3873 80// For the generic implementation, both the leaf nodes and the nodes are sorted for
bc4f1ff2
FM
81// fast search when needed
82static wxDataViewModel* g_model;
c4864c04
VZ
83
84// The column is either the index of the column to be used for sorting or one
85// of the special values in this enum:
86enum
87{
0c5a056a
VZ
88 // Sort when we're thawed later.
89 SortColumn_OnThaw = -3,
90
c4864c04
VZ
91 // Don't sort at all.
92 SortColumn_None = -2,
93
94 // Sort using the model default sort order.
95 SortColumn_Default = -1
96};
97
98static int g_column = SortColumn_None;
24c4a50f 99static bool g_asending = true;
57f2a652 100
1841f079
VZ
101// ----------------------------------------------------------------------------
102// helper functions
103// ----------------------------------------------------------------------------
104
105namespace
106{
107
108// Return the expander column or, if it is not set, the first column and also
109// set it as the expander one for the future.
110wxDataViewColumn* GetExpanderColumnOrFirstOne(wxDataViewCtrl* dataview)
111{
112 wxDataViewColumn* expander = dataview->GetExpanderColumn();
113 if (!expander)
114 {
115 // TODO-RTL: last column for RTL support
116 expander = dataview->GetColumnAt( 0 );
117 dataview->SetExpanderColumn(expander);
118 }
119
120 return expander;
121}
122
123} // anonymous namespace
124
5d9e1605
RR
125//-----------------------------------------------------------------------------
126// wxDataViewColumn
127//-----------------------------------------------------------------------------
128
129void wxDataViewColumn::Init(int width, wxAlignment align, int flags)
130{
d0154e3a 131 m_width = width;
5d9e1605
RR
132 m_minWidth = 0;
133 m_align = align;
134 m_flags = flags;
135 m_sort = false;
136 m_sortAscending = true;
137}
ce00f59b 138
d0154e3a
VS
139int wxDataViewColumn::GetWidth() const
140{
141 switch ( m_width )
142 {
143 case wxCOL_WIDTH_DEFAULT:
144 return wxDVC_DEFAULT_WIDTH;
145
146 case wxCOL_WIDTH_AUTOSIZE:
147 wxCHECK_MSG( m_owner, wxDVC_DEFAULT_WIDTH, "no owner control" );
148 return m_owner->GetBestColumnWidth(m_owner->GetColumnIndex(this));
149
150 default:
151 return m_width;
152 }
153}
154
5d9e1605
RR
155void wxDataViewColumn::UpdateDisplay()
156{
157 if (m_owner)
158 {
159 int idx = m_owner->GetColumnIndex( this );
160 m_owner->OnColumnChange( idx );
161 }
162}
163
732a176c
VZ
164void wxDataViewColumn::UnsetAsSortKey()
165{
166 m_sort = false;
167
168 if ( m_owner )
169 m_owner->SetSortingColumnIndex(wxNOT_FOUND);
170
171 UpdateDisplay();
172}
173
15b8afdc
VZ
174void wxDataViewColumn::SetSortOrder(bool ascending)
175{
176 if ( !m_owner )
177 return;
178
179 // First unset the old sort column if any.
180 int oldSortKey = m_owner->GetSortingColumnIndex();
181 if ( oldSortKey != wxNOT_FOUND )
182 {
183 m_owner->GetColumn(oldSortKey)->UnsetAsSortKey();
184 }
185
186 // Now set this one as the new sort column.
187 const int idx = m_owner->GetColumnIndex(this);
188 m_owner->SetSortingColumnIndex(idx);
189
190 m_sort = true;
191 m_sortAscending = ascending;
192
193 // Call this directly instead of using UpdateDisplay() as we already have
194 // the column index, no need to look it up again.
195 m_owner->OnColumnChange(idx);
196}
197
fa3d4aaf
VZ
198//-----------------------------------------------------------------------------
199// wxDataViewHeaderWindow
200//-----------------------------------------------------------------------------
201
e2bfe673 202class wxDataViewHeaderWindow : public wxHeaderCtrl
87f0efe2
RR
203{
204public:
e2bfe673
VZ
205 wxDataViewHeaderWindow(wxDataViewCtrl *parent)
206 : wxHeaderCtrl(parent)
87f0efe2 207 {
87f0efe2
RR
208 }
209
e2bfe673
VZ
210 wxDataViewCtrl *GetOwner() const
211 { return static_cast<wxDataViewCtrl *>(GetParent()); }
87f0efe2 212
3bfaa5a7
VZ
213protected:
214 // implement/override wxHeaderCtrl functions by forwarding them to the main
215 // control
482d06f8 216 virtual const wxHeaderColumn& GetColumn(unsigned int idx) const
87f0efe2 217 {
e2bfe673 218 return *(GetOwner()->GetColumn(idx));
87f0efe2
RR
219 }
220
3bfaa5a7
VZ
221 virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle)
222 {
223 wxDataViewCtrl * const owner = GetOwner();
224
225 int widthContents = owner->GetBestColumnWidth(idx);
226 owner->GetColumn(idx)->SetWidth(wxMax(widthTitle, widthContents));
aef252d9 227 owner->OnColumnChange(idx);
3bfaa5a7
VZ
228
229 return true;
230 }
231
232private:
fa3d4aaf
VZ
233 bool SendEvent(wxEventType type, unsigned int n)
234 {
235 wxDataViewCtrl * const owner = GetOwner();
236 wxDataViewEvent event(type, owner->GetId());
237
238 event.SetEventObject(owner);
239 event.SetColumn(n);
240 event.SetDataViewColumn(owner->GetColumn(n));
241 event.SetModel(owner->GetModel());
242
243 // for events created by wxDataViewHeaderWindow the
244 // row / value fields are not valid
857f05e1 245 return owner->ProcessWindowEvent(event);
fa3d4aaf
VZ
246 }
247
248 void OnClick(wxHeaderCtrlEvent& event)
249 {
46234a03
VZ
250 const unsigned idx = event.GetColumn();
251
ce7fe42e 252 if ( SendEvent(wxEVT_DATAVIEW_COLUMN_HEADER_CLICK, idx) )
46234a03
VZ
253 return;
254
255 // default handling for the column click is to sort by this column or
256 // toggle its sort order
257 wxDataViewCtrl * const owner = GetOwner();
258 wxDataViewColumn * const col = owner->GetColumn(idx);
259 if ( !col->IsSortable() )
260 {
261 // no default handling for non-sortable columns
fa3d4aaf 262 event.Skip();
46234a03
VZ
263 return;
264 }
265
266 if ( col->IsSortKey() )
267 {
268 // already using this column for sorting, just change the order
269 col->ToggleSortOrder();
270 }
271 else // not using this column for sorting yet
272 {
aadbdd16 273 col->SetSortOrder(true);
46234a03
VZ
274 }
275
276 wxDataViewModel * const model = owner->GetModel();
277 if ( model )
278 model->Resort();
279
280 owner->OnColumnChange(idx);
ff7bc95e 281
ce7fe42e 282 SendEvent(wxEVT_DATAVIEW_COLUMN_SORTED, idx);
fa3d4aaf
VZ
283 }
284
285 void OnRClick(wxHeaderCtrlEvent& event)
286 {
3bc1418b 287 if ( !SendEvent(wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK,
fa3d4aaf
VZ
288 event.GetColumn()) )
289 event.Skip();
290 }
291
bc4f1ff2 292 void OnResize(wxHeaderCtrlEvent& event)
aef252d9 293 {
dcb6cbec
VZ
294 wxDataViewCtrl * const owner = GetOwner();
295
565804f2 296 const unsigned col = event.GetColumn();
dcb6cbec 297 owner->GetColumn(col)->SetWidth(event.GetWidth());
565804f2 298 GetOwner()->OnColumnChange(col);
aef252d9
VZ
299 }
300
702f5349
VZ
301 void OnEndReorder(wxHeaderCtrlEvent& event)
302 {
303 wxDataViewCtrl * const owner = GetOwner();
304 owner->ColumnMoved(owner->GetColumn(event.GetColumn()),
977a41ec 305 event.GetNewOrder());
702f5349
VZ
306 }
307
fa3d4aaf 308 DECLARE_EVENT_TABLE()
c0c133e1 309 wxDECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow);
a0f3af5f 310};
4ed7af08 311
fa3d4aaf
VZ
312BEGIN_EVENT_TABLE(wxDataViewHeaderWindow, wxHeaderCtrl)
313 EVT_HEADER_CLICK(wxID_ANY, wxDataViewHeaderWindow::OnClick)
314 EVT_HEADER_RIGHT_CLICK(wxID_ANY, wxDataViewHeaderWindow::OnRClick)
aef252d9 315
bc4f1ff2
FM
316 EVT_HEADER_RESIZING(wxID_ANY, wxDataViewHeaderWindow::OnResize)
317 EVT_HEADER_END_RESIZE(wxID_ANY, wxDataViewHeaderWindow::OnResize)
702f5349
VZ
318
319 EVT_HEADER_END_REORDER(wxID_ANY, wxDataViewHeaderWindow::OnEndReorder)
fa3d4aaf
VZ
320END_EVENT_TABLE()
321
0fcce6b9
RR
322//-----------------------------------------------------------------------------
323// wxDataViewRenameTimer
324//-----------------------------------------------------------------------------
325
326class wxDataViewRenameTimer: public wxTimer
327{
328private:
329 wxDataViewMainWindow *m_owner;
330
331public:
332 wxDataViewRenameTimer( wxDataViewMainWindow *owner );
333 void Notify();
334};
335
aba9bfd0
RR
336//-----------------------------------------------------------------------------
337// wxDataViewTreeNode
338//-----------------------------------------------------------------------------
bc4f1ff2 339
442c56e6 340class wxDataViewTreeNode;
57f2a652 341WX_DEFINE_ARRAY( wxDataViewTreeNode *, wxDataViewTreeNodes );
d47db7e0 342
4cef3873 343int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode ** node1,
bc4f1ff2 344 wxDataViewTreeNode ** node2);
aba9bfd0
RR
345
346class wxDataViewTreeNode
347{
348public:
422aa8ec 349 wxDataViewTreeNode(wxDataViewTreeNode *parent, const wxDataViewItem& item)
fb5f677b
VZ
350 : m_parent(parent),
351 m_item(item),
d0cfefc4 352 m_branchData(NULL)
b5ec7dd6 353 {
438fb233 354 }
b5ec7dd6 355
d0cfefc4
VS
356 ~wxDataViewTreeNode()
357 {
a2d3c415
VS
358 if ( m_branchData )
359 {
360 wxDataViewTreeNodes& nodes = m_branchData->children;
361 for ( wxDataViewTreeNodes::iterator i = nodes.begin();
362 i != nodes.end();
363 ++i )
364 {
365 delete *i;
366 }
367
368 delete m_branchData;
369 }
d0cfefc4
VS
370 }
371
422aa8ec 372 static wxDataViewTreeNode* CreateRootNode()
d47db7e0 373 {
422aa8ec 374 wxDataViewTreeNode *n = new wxDataViewTreeNode(NULL, wxDataViewItem());
ddcc73a7 375 n->m_branchData = new BranchNodeData;
d0cfefc4 376 n->m_branchData->open = true;
422aa8ec 377 return n;
d47db7e0 378 }
aba9bfd0 379
344ed1f3 380 wxDataViewTreeNode * GetParent() const { return m_parent; }
422aa8ec 381
a2d3c415 382 const wxDataViewTreeNodes& GetChildNodes() const
d0cfefc4
VS
383 {
384 wxASSERT( m_branchData != NULL );
ff3c5ad3 385 return m_branchData->children;
d0cfefc4 386 }
442c56e6 387
35219832 388 void InsertChild(wxDataViewTreeNode *node, unsigned index)
d47db7e0 389 {
d0cfefc4
VS
390 if ( !m_branchData )
391 m_branchData = new BranchNodeData;
392
35219832
VS
393 m_branchData->children.Insert(node, index);
394
422aa8ec 395 // TODO: insert into sorted array directly in O(log n) instead of resorting in O(n log n)
24c4a50f 396 if (g_column >= -1)
ff3c5ad3 397 m_branchData->children.Sort( &wxGenericTreeModelNodeCmp );
57f2a652 398 }
aba9bfd0 399
a2d3c415
VS
400 void RemoveChild(wxDataViewTreeNode *node)
401 {
402 wxCHECK_RET( m_branchData != NULL, "leaf node doesn't have children" );
403 m_branchData->children.Remove(node);
404 }
405
5bb82ad4
VS
406 // returns position of child node for given item in children list or wxNOT_FOUND
407 int FindChildByItem(const wxDataViewItem& item) const
408 {
409 if ( !m_branchData )
410 return wxNOT_FOUND;
411
412 const wxDataViewTreeNodes& nodes = m_branchData->children;
413 const int len = nodes.size();
414 for ( int i = 0; i < len; i++ )
415 {
416 if ( nodes[i]->m_item == item )
417 return i;
418 }
419 return wxNOT_FOUND;
420 }
421
344ed1f3 422 const wxDataViewItem & GetItem() const { return m_item; }
438fb233 423 void SetItem( const wxDataViewItem & item ) { m_item = item; }
aba9bfd0 424
344ed1f3 425 int GetIndentLevel() const
3b6280be 426 {
59e60167 427 int ret = 0;
344ed1f3 428 const wxDataViewTreeNode * node = this;
438fb233
RR
429 while( node->GetParent()->GetParent() != NULL )
430 {
431 node = node->GetParent();
432 ret ++;
433 }
434 return ret;
3b6280be 435 }
aba9bfd0 436
344ed1f3 437 bool IsOpen() const
442c56e6 438 {
d0cfefc4 439 return m_branchData && m_branchData->open;
442c56e6 440 }
d47db7e0
RR
441
442 void ToggleOpen()
442c56e6 443 {
ddcc73a7
VZ
444 // We do not allow the (invisible) root node to be collapsed because
445 // there is no way to expand it again.
446 if ( !m_parent )
447 return;
448
d0cfefc4
VS
449 wxCHECK_RET( m_branchData != NULL, "can't open leaf node" );
450
d47db7e0 451 int sum = 0;
422aa8ec 452
ff3c5ad3 453 const wxDataViewTreeNodes& nodes = m_branchData->children;
d0cfefc4 454 const int len = nodes.GetCount();
59e60167 455 for ( int i = 0;i < len; i ++)
d0cfefc4 456 sum += 1 + nodes[i]->GetSubTreeCount();
d47db7e0 457
d0cfefc4 458 if (m_branchData->open)
d47db7e0
RR
459 {
460 ChangeSubTreeCount(-sum);
d0cfefc4 461 m_branchData->open = !m_branchData->open;
d47db7e0
RR
462 }
463 else
464 {
d0cfefc4 465 m_branchData->open = !m_branchData->open;
422aa8ec 466 ChangeSubTreeCount(+sum);
d47db7e0
RR
467 }
468 }
422aa8ec
VS
469
470 // "HasChildren" property corresponds to model's IsContainer(). Note that it may be true
ff3c5ad3 471 // even if GetChildNodes() is empty; see below.
d0cfefc4
VS
472 bool HasChildren() const
473 {
474 return m_branchData != NULL;
475 }
476
477 void SetHasChildren(bool has)
478 {
ddcc73a7
VZ
479 // The invisible root item always has children, so ignore any attempts
480 // to change this.
481 if ( !m_parent )
482 return;
483
d0cfefc4
VS
484 if ( !has )
485 {
486 wxDELETE(m_branchData);
487 }
488 else if ( m_branchData == NULL )
489 {
490 m_branchData = new BranchNodeData;
491 }
492 }
493
494 int GetSubTreeCount() const
495 {
496 return m_branchData ? m_branchData->subTreeCount : 0;
497 }
d47db7e0 498
442c56e6 499 void ChangeSubTreeCount( int num )
d47db7e0 500 {
d0cfefc4
VS
501 wxASSERT( m_branchData != NULL );
502
503 if( !m_branchData->open )
59e60167 504 return;
422aa8ec 505
d0cfefc4
VS
506 m_branchData->subTreeCount += num;
507 wxASSERT( m_branchData->subTreeCount >= 0 );
422aa8ec 508
438fb233
RR
509 if( m_parent )
510 m_parent->ChangeSubTreeCount(num);
d47db7e0
RR
511 }
512
d3f00f59
VZ
513 void Resort()
514 {
d0cfefc4
VS
515 if ( !m_branchData )
516 return;
517
24c4a50f 518 if (g_column >= -1)
d3f00f59 519 {
ff3c5ad3 520 wxDataViewTreeNodes& nodes = m_branchData->children;
d0cfefc4
VS
521
522 nodes.Sort( &wxGenericTreeModelNodeCmp );
523 int len = nodes.GetCount();
57f2a652 524 for (int i = 0; i < len; i ++)
422aa8ec 525 {
d0cfefc4
VS
526 if ( nodes[i]->HasChildren() )
527 nodes[i]->Resort();
422aa8ec 528 }
57ab4546 529 }
57ab4546
VS
530 }
531
532
aba9bfd0 533private:
d0cfefc4
VS
534 wxDataViewTreeNode *m_parent;
535
422aa8ec
VS
536 // Corresponding model item.
537 wxDataViewItem m_item;
538
d0cfefc4
VS
539 // Data specific to non-leaf (branch, inner) nodes. They are kept in a
540 // separate struct in order to conserve memory.
541 struct BranchNodeData
542 {
543 BranchNodeData()
544 : open(false),
545 subTreeCount(0)
546 {
547 }
422aa8ec 548
d0cfefc4
VS
549 // Child nodes. Note that this may be empty even if m_hasChildren in
550 // case this branch of the tree wasn't expanded and realized yet.
ff3c5ad3 551 wxDataViewTreeNodes children;
422aa8ec 552
d0cfefc4
VS
553 // Is the branch node currently open (expanded)?
554 bool open;
422aa8ec 555
d0cfefc4
VS
556 // Total count of expanded (i.e. visible with the help of some
557 // scrolling) items in the subtree, but excluding this node. I.e. it is
558 // 0 for leaves and is the number of rows the subtree occupies for
559 // branch nodes.
560 int subTreeCount;
561 };
422aa8ec 562
d0cfefc4 563 BranchNodeData *m_branchData;
aba9bfd0
RR
564};
565
422aa8ec 566
4cef3873 567int LINKAGEMODE wxGenericTreeModelNodeCmp( wxDataViewTreeNode ** node1,
bc4f1ff2 568 wxDataViewTreeNode ** node2)
d47db7e0 569{
57f2a652 570 return g_model->Compare( (*node1)->GetItem(), (*node2)->GetItem(), g_column, g_asending );
d47db7e0
RR
571}
572
d47db7e0 573
a0f3af5f
RR
574//-----------------------------------------------------------------------------
575// wxDataViewMainWindow
576//-----------------------------------------------------------------------------
4ed7af08 577
c3b0247d 578WX_DEFINE_SORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection);
cab07038 579
a0f3af5f 580class wxDataViewMainWindow: public wxWindow
4ed7af08 581{
a0f3af5f
RR
582public:
583 wxDataViewMainWindow( wxDataViewCtrl *parent,
584 wxWindowID id,
585 const wxPoint &pos = wxDefaultPosition,
586 const wxSize &size = wxDefaultSize,
587 const wxString &name = wxT("wxdataviewctrlmainwindow") );
d3c7fc99 588 virtual ~wxDataViewMainWindow();
777f9cef 589
c2c89730 590 bool IsList() const { return GetModel()->IsListModel(); }
344ed1f3 591 bool IsVirtualList() const { return m_root == NULL; }
4ed7af08 592
aba9bfd0
RR
593 // notifications from wxDataViewModel
594 bool ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item );
32143117 595 bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item );
aba9bfd0 596 bool ItemChanged( const wxDataViewItem &item );
d93cc830 597 bool ValueChanged( const wxDataViewItem &item, unsigned int model_column );
a0f3af5f 598 bool Cleared();
d3f00f59 599 void Resort()
b7fe2261 600 {
344ed1f3 601 if (!IsVirtualList())
a3cc79d9
RR
602 {
603 SortPrepare();
604 m_root->Resort();
605 }
b7fe2261 606 UpdateDisplay();
66e09788 607 }
4ed7af08 608
0c5a056a
VZ
609 // Override the base class method to resort if needed, i.e. if
610 // SortPrepare() was called -- and ignored -- while we were frozen.
611 virtual void DoThaw()
612 {
613 if ( g_column == SortColumn_OnThaw )
614 {
615 Resort();
616 g_column = SortColumn_None;
617 }
618
619 wxWindow::DoThaw();
620 }
621
66e09788
RR
622 void SortPrepare()
623 {
c2c89730 624 g_model = GetModel();
0c5a056a 625
57f2a652 626 wxDataViewColumn* col = GetOwner()->GetSortingColumn();
66e09788
RR
627 if( !col )
628 {
24c4a50f 629 if (g_model->HasDefaultCompare())
732a176c
VZ
630 {
631 // See below for the explanation of IsFrozen() test.
632 if ( IsFrozen() )
633 g_column = SortColumn_OnThaw;
634 else
635 g_column = SortColumn_Default;
636 }
24c4a50f 637 else
c4864c04 638 g_column = SortColumn_None;
24c4a50f 639
66e09788
RR
640 g_asending = true;
641 return;
642 }
732a176c
VZ
643
644 // Avoid sorting while the window is frozen, this allows to quickly add
645 // many items without resorting after each addition and only resort
646 // them all at once when the window is finally thawed, see above.
647 if ( IsFrozen() )
648 {
649 g_column = SortColumn_OnThaw;
650 return;
651 }
652
57f2a652 653 g_column = col->GetModelColumn();
b7fe2261 654 g_asending = col->IsSortOrderAscending();
66e09788 655 }
b7fe2261 656
a0f3af5f
RR
657 void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; }
658 wxDataViewCtrl *GetOwner() { return m_owner; }
9861f022 659 const wxDataViewCtrl *GetOwner() const { return m_owner; }
4ed7af08 660
c2c89730
VS
661 wxDataViewModel* GetModel() { return GetOwner()->GetModel(); }
662 const wxDataViewModel* GetModel() const { return GetOwner()->GetModel(); }
663
9adeb77a 664#if wxUSE_DRAG_AND_DROP
818d91a9 665 wxBitmap CreateItemBitmap( unsigned int row, int &indent );
9adeb77a 666#endif // wxUSE_DRAG_AND_DROP
a0f3af5f 667 void OnPaint( wxPaintEvent &event );
64ac3db8 668 void OnCharHook( wxKeyEvent &event );
cab07038 669 void OnChar( wxKeyEvent &event );
ed3aece5 670 void OnVerticalNavigation(int delta, const wxKeyEvent& event);
cfc21881
VS
671 void OnLeftKey();
672 void OnRightKey();
a0f3af5f
RR
673 void OnMouse( wxMouseEvent &event );
674 void OnSetFocus( wxFocusEvent &event );
cab07038 675 void OnKillFocus( wxFocusEvent &event );
f554a14b 676
a0f3af5f
RR
677 void UpdateDisplay();
678 void RecalculateDisplay();
679 void OnInternalIdle();
f554a14b 680
0fcce6b9 681 void OnRenameTimer();
0fcce6b9 682
9861f022 683 void ScrollWindow( int dx, int dy, const wxRect *rect = NULL );
fbda518c 684 void ScrollTo( int rows, int column );
120b9b05 685
80ce465c 686 unsigned GetCurrentRow() const { return m_currentRow; }
0a71f9e9
RR
687 bool HasCurrentRow() { return m_currentRow != (unsigned int)-1; }
688 void ChangeCurrentRow( unsigned int row );
1c8e71c9 689 bool TryAdvanceCurrentColumn(wxDataViewTreeNode *node, bool forward);
120b9b05 690
f52f3795
VS
691 wxDataViewColumn *GetCurrentColumn() const { return m_currentCol; }
692 void ClearCurrentColumn() { m_currentCol = NULL; }
693
47b378bd 694 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE); }
cab07038 695 bool IsEmpty() { return GetRowCount() == 0; }
120b9b05 696
9861f022
RR
697 int GetCountPerPage() const;
698 int GetEndOfLastCol() const;
699 unsigned int GetFirstVisibleRow() const;
bc4f1ff2 700
4cef3873
VZ
701 // I change this method to un const because in the tree view,
702 // the displaying number of the tree are changing along with the
bc4f1ff2 703 // expanding/collapsing of the tree nodes
3b6280be 704 unsigned int GetLastVisibleRow();
f2bf2d71 705 unsigned int GetRowCount() const;
120b9b05 706
f23b8c0d 707 const wxDataViewSelection& GetSelections() const { return m_selection; }
4cef3873 708 void SetSelections( const wxDataViewSelection & sel )
bc4f1ff2 709 { m_selection = sel; UpdateDisplay(); }
87f0efe2 710 void Select( const wxArrayInt& aSelections );
cab07038 711 void SelectAllRows( bool on );
0a71f9e9
RR
712 void SelectRow( unsigned int row, bool on );
713 void SelectRows( unsigned int from, unsigned int to, bool on );
714 void ReverseRowSelection( unsigned int row );
715 bool IsRowSelected( unsigned int row );
526e19e2 716 void SendSelectionChangedEvent( const wxDataViewItem& item);
120b9b05 717
0a71f9e9
RR
718 void RefreshRow( unsigned int row );
719 void RefreshRows( unsigned int from, unsigned int to );
720 void RefreshRowsAfter( unsigned int firstRow );
120b9b05 721
9861f022
RR
722 // returns the colour to be used for drawing the rules
723 wxColour GetRuleColour() const
724 {
725 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT);
726 }
727
9861f022 728 wxRect GetLineRect( unsigned int row ) const;
777f9cef 729
344ed1f3
RR
730 int GetLineStart( unsigned int row ) const; // row * m_lineHeight in fixed mode
731 int GetLineHeight( unsigned int row ) const; // m_lineHeight in fixed mode
732 int GetLineAt( unsigned int y ) const; // y / m_lineHeight in fixed mode
9861f022 733
bbfd4548 734 void SetRowHeight( int lineHeight ) { m_lineHeight = lineHeight; }
0fdeaabe 735 int GetRowHeight() const { return m_lineHeight; }
1af67319 736 int GetDefaultRowHeight() const;
bbfd4548 737
bc4f1ff2 738 // Some useful functions for row and item mapping
fbda518c 739 wxDataViewItem GetItemByRow( unsigned int row ) const;
344ed1f3 740 int GetRowByItem( const wxDataViewItem & item ) const;
777f9cef 741
0fdeaabe
VS
742 wxDataViewTreeNode * GetTreeNodeByRow( unsigned int row ) const;
743 // We did not need this temporarily
744 // wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
745
bc4f1ff2 746 // Methods for building the mapping tree
aba9bfd0
RR
747 void BuildTree( wxDataViewModel * model );
748 void DestroyTree();
a87b466d 749 void HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column );
fbda518c 750 wxRect GetItemRect( const wxDataViewItem & item, const wxDataViewColumn* column );
afebb87b 751
235d5f88
RR
752 void Expand( unsigned int row );
753 void Collapse( unsigned int row );
739a8399 754 bool IsExpanded( unsigned int row ) const;
235d5f88 755 bool HasChildren( unsigned int row ) const;
821baf7d 756
9adeb77a 757#if wxUSE_DRAG_AND_DROP
a653c966
RR
758 bool EnableDragSource( const wxDataFormat &format );
759 bool EnableDropTarget( const wxDataFormat &format );
760
9deec111 761 void RemoveDropHint();
a653c966
RR
762 wxDragResult OnDragOver( wxDataFormat format, wxCoord x, wxCoord y, wxDragResult def );
763 bool OnDrop( wxDataFormat format, wxCoord x, wxCoord y );
764 wxDragResult OnData( wxDataFormat format, wxCoord x, wxCoord y, wxDragResult def );
765 void OnLeave();
9adeb77a 766#endif // wxUSE_DRAG_AND_DROP
821baf7d 767
1c8e71c9
VS
768 void OnColumnsCountChanged();
769
64ac3db8
VZ
770 // Called by wxDataViewCtrl and our own OnRenameTimer() to start edit the
771 // specified item in the given column.
772 void StartEditing(const wxDataViewItem& item, const wxDataViewColumn* col);
773
3b6280be 774private:
f2bf2d71 775 int RecalculateCount() const;
3b6280be 776
fd48fe89
VZ
777 // Return false only if the event was vetoed by its handler.
778 bool SendExpanderEvent(wxEventType type, const wxDataViewItem& item);
aba9bfd0 779
442c56e6 780 wxDataViewTreeNode * FindNode( const wxDataViewItem & item );
351461fc 781
1c8e71c9
VS
782 wxDataViewColumn *FindColumnForEditing(const wxDataViewItem& item, wxDataViewCellMode mode);
783
cf5d4c76
VS
784 bool IsCellEditableInMode(const wxDataViewItem& item, const wxDataViewColumn *col, wxDataViewCellMode mode) const;
785
1c959a62
VZ
786 void DrawCellBackground( wxDataViewRenderer* cell, wxDC& dc, const wxRect& rect );
787
a0f3af5f 788private:
0fcce6b9
RR
789 wxDataViewCtrl *m_owner;
790 int m_lineHeight;
791 bool m_dirty;
120b9b05 792
0fcce6b9 793 wxDataViewColumn *m_currentCol;
99d471a5 794 unsigned int m_currentRow;
cab07038 795 wxDataViewSelection m_selection;
120b9b05 796
0fcce6b9 797 wxDataViewRenameTimer *m_renameTimer;
0fcce6b9 798 bool m_lastOnSame;
f554a14b 799
cab07038 800 bool m_hasFocus;
1c8e71c9
VS
801 bool m_useCellFocus;
802 bool m_currentColSetByKeyboard;
cab07038 803
9adeb77a 804#if wxUSE_DRAG_AND_DROP
e21f75bd
RR
805 int m_dragCount;
806 wxPoint m_dragStart;
9adeb77a 807
821baf7d
RR
808 bool m_dragEnabled;
809 wxDataFormat m_dragFormat;
9adeb77a 810
821baf7d
RR
811 bool m_dropEnabled;
812 wxDataFormat m_dropFormat;
9deec111
RR
813 bool m_dropHint;
814 unsigned int m_dropHintLine;
9adeb77a 815#endif // wxUSE_DRAG_AND_DROP
e21f75bd
RR
816
817 // for double click logic
0a71f9e9 818 unsigned int m_lineLastClicked,
977a41ec
FM
819 m_lineBeforeLastClicked,
820 m_lineSelectSingleOnUp;
cab07038 821
9861f022
RR
822 // the pen used to draw horiz/vertical rules
823 wxPen m_penRule;
824
3b6280be
RR
825 // the pen used to draw the expander and the lines
826 wxPen m_penExpander;
827
bc4f1ff2 828 // This is the tree structure of the model
442c56e6 829 wxDataViewTreeNode * m_root;
3b6280be 830 int m_count;
bc4f1ff2
FM
831
832 // This is the tree node under the cursor
24c4a50f 833 wxDataViewTreeNode * m_underMouse;
bc4f1ff2 834
64ac3db8
VZ
835 // The control used for editing or NULL.
836 wxWeakRef<wxWindow> m_editorCtrl;
837
838 // Id m_editorCtrl is non-NULL, pointer to the associated renderer.
839 wxDataViewRenderer* m_editorRenderer;
840
a0f3af5f
RR
841private:
842 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow)
843 DECLARE_EVENT_TABLE()
844};
4ed7af08 845
f554a14b 846// ---------------------------------------------------------
aba9bfd0 847// wxGenericDataViewModelNotifier
f554a14b 848// ---------------------------------------------------------
a0f3af5f 849
aba9bfd0 850class wxGenericDataViewModelNotifier: public wxDataViewModelNotifier
4ed7af08 851{
a0f3af5f 852public:
aba9bfd0 853 wxGenericDataViewModelNotifier( wxDataViewMainWindow *mainWindow )
a0f3af5f 854 { m_mainWindow = mainWindow; }
f554a14b 855
aba9bfd0
RR
856 virtual bool ItemAdded( const wxDataViewItem & parent, const wxDataViewItem & item )
857 { return m_mainWindow->ItemAdded( parent , item ); }
442c56e6 858 virtual bool ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
071691a0 859 { return m_mainWindow->ItemDeleted( parent, item ); }
aba9bfd0
RR
860 virtual bool ItemChanged( const wxDataViewItem & item )
861 { return m_mainWindow->ItemChanged(item); }
862 virtual bool ValueChanged( const wxDataViewItem & item , unsigned int col )
863 { return m_mainWindow->ValueChanged( item, col ); }
a0f3af5f
RR
864 virtual bool Cleared()
865 { return m_mainWindow->Cleared(); }
d3f00f59 866 virtual void Resort()
977a41ec 867 { m_mainWindow->Resort(); }
f554a14b 868
a0f3af5f
RR
869 wxDataViewMainWindow *m_mainWindow;
870};
4ed7af08 871
f554a14b 872// ---------------------------------------------------------
baa9ebc4 873// wxDataViewRenderer
f554a14b 874// ---------------------------------------------------------
4ed7af08 875
baa9ebc4 876IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase)
4ed7af08 877
c741d33f 878wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype,
9861f022
RR
879 wxDataViewCellMode mode,
880 int align) :
6eec70b9 881 wxDataViewCustomRendererBase( varianttype, mode, align )
4ed7af08 882{
9861f022
RR
883 m_align = align;
884 m_mode = mode;
c937bcac 885 m_ellipsizeMode = wxELLIPSIZE_MIDDLE;
6eec70b9 886 m_dc = NULL;
4ed7af08
RR
887}
888
baa9ebc4 889wxDataViewRenderer::~wxDataViewRenderer()
3d9d7cc4 890{
6eec70b9
VZ
891 delete m_dc;
892}
893
894wxDC *wxDataViewRenderer::GetDC()
895{
896 if (m_dc == NULL)
897 {
898 if (GetOwner() == NULL)
899 return NULL;
900 if (GetOwner()->GetOwner() == NULL)
901 return NULL;
902 m_dc = new wxClientDC( GetOwner()->GetOwner() );
903 }
904
905 return m_dc;
906}
907
908void wxDataViewRenderer::SetAlignment( int align )
909{
910 m_align=align;
911}
912
913int wxDataViewRenderer::GetAlignment() const
914{
915 return m_align;
916}
917
6eec70b9
VZ
918// ---------------------------------------------------------
919// wxDataViewCustomRenderer
920// ---------------------------------------------------------
921
922IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
923
924wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
925 wxDataViewCellMode mode, int align ) :
926 wxDataViewRenderer( varianttype, mode, align )
927{
928}
929
f554a14b 930// ---------------------------------------------------------
baa9ebc4 931// wxDataViewTextRenderer
f554a14b 932// ---------------------------------------------------------
4ed7af08 933
6eec70b9 934IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer)
4ed7af08 935
c741d33f 936wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype,
9861f022 937 wxDataViewCellMode mode, int align ) :
6eec70b9 938 wxDataViewRenderer( varianttype, mode, align )
4ed7af08
RR
939{
940}
941
baa9ebc4 942bool wxDataViewTextRenderer::SetValue( const wxVariant &value )
4ed7af08 943{
90675b95 944 m_text = value.GetString();
f554a14b 945
90675b95 946 return true;
4ed7af08
RR
947}
948
9861f022 949bool wxDataViewTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const
4ed7af08
RR
950{
951 return false;
952}
953
bc4f1ff2 954bool wxDataViewTextRenderer::HasEditorCtrl() const
442c56e6 955{
99d471a5
RR
956 return true;
957}
958
64c70359 959wxWindow* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow *parent,
99d471a5
RR
960 wxRect labelRect, const wxVariant &value )
961{
0a807957
RR
962 wxTextCtrl* ctrl = new wxTextCtrl( parent, wxID_ANY, value,
963 wxPoint(labelRect.x,labelRect.y),
dd90475f
VS
964 wxSize(labelRect.width,labelRect.height),
965 wxTE_PROCESS_ENTER );
0a807957
RR
966
967 // select the text in the control an place the cursor at the end
968 ctrl->SetInsertionPointEnd();
969 ctrl->SelectAll();
970
971 return ctrl;
99d471a5
RR
972}
973
64c70359 974bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxWindow *editor, wxVariant &value )
99d471a5
RR
975{
976 wxTextCtrl *text = (wxTextCtrl*) editor;
977 value = text->GetValue();
978 return true;
979}
980
62265c2c 981bool wxDataViewTextRenderer::Render(wxRect rect, wxDC *dc, int state)
3d9d7cc4 982{
62265c2c 983 RenderText(m_text, 0, rect, dc, state);
90675b95 984 return true;
3d9d7cc4
RR
985}
986
9861f022 987wxSize wxDataViewTextRenderer::GetSize() const
3d9d7cc4 988{
87f0efe2 989 if (!m_text.empty())
86755098
VS
990 return GetTextExtent(m_text);
991 else
992 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE);
3d9d7cc4
RR
993}
994
2586d4a1 995// ---------------------------------------------------------
baa9ebc4 996// wxDataViewBitmapRenderer
2586d4a1
RR
997// ---------------------------------------------------------
998
6eec70b9 999IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer)
2586d4a1 1000
c741d33f 1001wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype,
9861f022 1002 wxDataViewCellMode mode, int align ) :
6eec70b9 1003 wxDataViewRenderer( varianttype, mode, align )
2586d4a1
RR
1004{
1005}
1006
baa9ebc4 1007bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
2586d4a1
RR
1008{
1009 if (value.GetType() == wxT("wxBitmap"))
1010 m_bitmap << value;
1011 if (value.GetType() == wxT("wxIcon"))
1012 m_icon << value;
1013
1014 return true;
1015}
1016
9861f022 1017bool wxDataViewBitmapRenderer::GetValue( wxVariant& WXUNUSED(value) ) const
2586d4a1
RR
1018{
1019 return false;
1020}
1021
baa9ebc4 1022bool wxDataViewBitmapRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
2586d4a1 1023{
a1b806b9 1024 if (m_bitmap.IsOk())
2586d4a1 1025 dc->DrawBitmap( m_bitmap, cell.x, cell.y );
a1b806b9 1026 else if (m_icon.IsOk())
2586d4a1
RR
1027 dc->DrawIcon( m_icon, cell.x, cell.y );
1028
1029 return true;
1030}
1031
9861f022 1032wxSize wxDataViewBitmapRenderer::GetSize() const
2586d4a1 1033{
a1b806b9 1034 if (m_bitmap.IsOk())
2586d4a1 1035 return wxSize( m_bitmap.GetWidth(), m_bitmap.GetHeight() );
a1b806b9 1036 else if (m_icon.IsOk())
2586d4a1
RR
1037 return wxSize( m_icon.GetWidth(), m_icon.GetHeight() );
1038
ce468dc2 1039 return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE);
2586d4a1
RR
1040}
1041
f554a14b 1042// ---------------------------------------------------------
baa9ebc4 1043// wxDataViewToggleRenderer
f554a14b 1044// ---------------------------------------------------------
4ed7af08 1045
6eec70b9 1046IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer)
4ed7af08 1047
baa9ebc4 1048wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
9861f022 1049 wxDataViewCellMode mode, int align ) :
6eec70b9 1050 wxDataViewRenderer( varianttype, mode, align )
4ed7af08 1051{
90675b95 1052 m_toggle = false;
4ed7af08
RR
1053}
1054
baa9ebc4 1055bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
4ed7af08 1056{
90675b95 1057 m_toggle = value.GetBool();
f554a14b 1058
a8461d31 1059 return true;
4ed7af08
RR
1060}
1061
9861f022 1062bool wxDataViewToggleRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
4ed7af08
RR
1063{
1064 return false;
1065}
f554a14b 1066
baa9ebc4 1067bool wxDataViewToggleRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
4ed7af08 1068{
862d8041 1069 int flags = 0;
90675b95 1070 if (m_toggle)
862d8041 1071 flags |= wxCONTROL_CHECKED;
98f8e666
VZ
1072 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE ||
1073 GetEnabled() == false)
862d8041
RR
1074 flags |= wxCONTROL_DISABLED;
1075
45a04dd3
VZ
1076 // Ensure that the check boxes always have at least the minimal required
1077 // size, otherwise DrawCheckBox() doesn't really work well. If this size is
1078 // greater than the cell size, the checkbox will be truncated but this is a
1079 // lesser evil.
1080 wxSize size = cell.GetSize();
1081 size.IncTo(GetSize());
1082 cell.SetSize(size);
68d7680d 1083
90b903c2 1084 wxRendererNative::Get().DrawCheckBox(
862d8041
RR
1085 GetOwner()->GetOwner(),
1086 *dc,
de4bf0b3 1087 cell,
862d8041 1088 flags );
f554a14b 1089
90675b95 1090 return true;
4ed7af08
RR
1091}
1092
4e231c29 1093bool wxDataViewToggleRenderer::WXActivateCell(const wxRect& WXUNUSED(cellRect),
dc73d7f5
VS
1094 wxDataViewModel *model,
1095 const wxDataViewItem& item,
1096 unsigned int col,
1097 const wxMouseEvent *mouseEvent)
0fdc2321 1098{
dc73d7f5 1099 if ( mouseEvent )
98f8e666 1100 {
9483e1ac
VZ
1101 // Only react to clicks directly on the checkbox, not elsewhere in the
1102 // same cell.
4e231c29 1103 if ( !wxRect(GetSize()).Contains(mouseEvent->GetPosition()) )
dc73d7f5 1104 return false;
98f8e666 1105 }
dbc3aec1 1106
dc73d7f5
VS
1107 model->ChangeValue(!m_toggle, item, col);
1108 return true;
0fdc2321
RR
1109}
1110
9861f022 1111wxSize wxDataViewToggleRenderer::GetSize() const
4ed7af08 1112{
de4bf0b3
FM
1113 // the window parameter is not used by GetCheckBoxSize() so it's
1114 // safe to pass NULL
1115 return wxRendererNative::Get().GetCheckBoxSize(NULL);
4ed7af08
RR
1116}
1117
f554a14b 1118// ---------------------------------------------------------
baa9ebc4 1119// wxDataViewProgressRenderer
f554a14b 1120// ---------------------------------------------------------
4ed7af08 1121
6eec70b9 1122IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer, wxDataViewRenderer)
4ed7af08 1123
baa9ebc4 1124wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
9861f022 1125 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
6eec70b9 1126 wxDataViewRenderer( varianttype, mode, align )
4ed7af08
RR
1127{
1128 m_label = label;
1129 m_value = 0;
1130}
1131
baa9ebc4 1132bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
4ed7af08
RR
1133{
1134 m_value = (long) value;
f554a14b 1135
4ed7af08
RR
1136 if (m_value < 0) m_value = 0;
1137 if (m_value > 100) m_value = 100;
f554a14b 1138
4ed7af08
RR
1139 return true;
1140}
f554a14b 1141
9861f022
RR
1142bool wxDataViewProgressRenderer::GetValue( wxVariant &value ) const
1143{
1144 value = (long) m_value;
1145 return true;
1146}
1147
62265c2c
VZ
1148bool
1149wxDataViewProgressRenderer::Render(wxRect rect, wxDC *dc, int WXUNUSED(state))
4ed7af08 1150{
62265c2c 1151 // deflate the rect to leave a small border between bars in adjacent rows
3e60a3c1
VZ
1152 wxRect bar = rect.Deflate(0, 1);
1153
62265c2c
VZ
1154 dc->SetBrush( *wxTRANSPARENT_BRUSH );
1155 dc->SetPen( *wxBLACK_PEN );
1156 dc->DrawRectangle( bar );
4ed7af08 1157
3e60a3c1 1158 bar.width = (int)(bar.width * m_value / 100.);
62265c2c
VZ
1159 dc->SetPen( *wxTRANSPARENT_PEN );
1160
1161 const wxDataViewItemAttr& attr = GetAttr();
1162 dc->SetBrush( attr.HasColour() ? wxBrush(attr.GetColour())
1163 : *wxBLUE_BRUSH );
1164 dc->DrawRectangle( bar );
f554a14b 1165
4ed7af08
RR
1166 return true;
1167}
1168
9861f022 1169wxSize wxDataViewProgressRenderer::GetSize() const
4ed7af08
RR
1170{
1171 return wxSize(40,12);
1172}
f554a14b 1173
b7fe2261 1174// ---------------------------------------------------------
24c4a50f 1175// wxDataViewIconTextRenderer
b7fe2261 1176// ---------------------------------------------------------
24c4a50f 1177
6eec70b9 1178IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewRenderer)
24c4a50f 1179
b7fe2261 1180wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
977a41ec 1181const wxString &varianttype, wxDataViewCellMode mode, int align ) :
6eec70b9 1182 wxDataViewRenderer( varianttype, mode, align )
24c4a50f
RR
1183{
1184 SetMode(mode);
1185 SetAlignment(align);
1186}
1187
24c4a50f
RR
1188bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value )
1189{
1190 m_value << value;
1191 return true;
1192}
1193
b5ec7dd6 1194bool wxDataViewIconTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const
24c4a50f
RR
1195{
1196 return false;
1197}
b7fe2261 1198
62265c2c 1199bool wxDataViewIconTextRenderer::Render(wxRect rect, wxDC *dc, int state)
24c4a50f 1200{
52e750fc 1201 int xoffset = 0;
38c34918
VZ
1202
1203 const wxIcon& icon = m_value.GetIcon();
1204 if ( icon.IsOk() )
24c4a50f 1205 {
62265c2c 1206 dc->DrawIcon(icon, rect.x, rect.y + (rect.height - icon.GetHeight())/2);
38c34918 1207 xoffset = icon.GetWidth()+4;
24c4a50f 1208 }
b5ec7dd6 1209
62265c2c 1210 RenderText(m_value.GetText(), xoffset, rect, dc, state);
24c4a50f
RR
1211
1212 return true;
1213}
1214
1215wxSize wxDataViewIconTextRenderer::GetSize() const
1216{
e44ac7bc
RR
1217 if (!m_value.GetText().empty())
1218 {
86755098 1219 wxSize size = GetTextExtent(m_value.GetText());
b5ec7dd6 1220
e44ac7bc 1221 if (m_value.GetIcon().IsOk())
86755098
VS
1222 size.x += m_value.GetIcon().GetWidth() + 4;
1223 return size;
e44ac7bc
RR
1224 }
1225 return wxSize(80,20);
24c4a50f
RR
1226}
1227
64c70359 1228wxWindow* wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow *parent, wxRect labelRect, const wxVariant& value)
24c4a50f 1229{
c937344c
RR
1230 wxDataViewIconText iconText;
1231 iconText << value;
1232
1233 wxString text = iconText.GetText();
1234
1235 // adjust the label rect to take the width of the icon into account
1236 if (iconText.GetIcon().IsOk())
1237 {
1238 int w = iconText.GetIcon().GetWidth() + 4;
1239 labelRect.x += w;
1240 labelRect.width -= w;
1241 }
1242
0a807957
RR
1243 wxTextCtrl* ctrl = new wxTextCtrl( parent, wxID_ANY, text,
1244 wxPoint(labelRect.x,labelRect.y),
dd90475f
VS
1245 wxSize(labelRect.width,labelRect.height),
1246 wxTE_PROCESS_ENTER );
0a807957
RR
1247
1248 // select the text in the control an place the cursor at the end
1249 ctrl->SetInsertionPointEnd();
1250 ctrl->SelectAll();
1251
1252 return ctrl;
24c4a50f
RR
1253}
1254
64c70359 1255bool wxDataViewIconTextRenderer::GetValueFromEditorCtrl( wxWindow *editor, wxVariant& value )
24c4a50f 1256{
c937344c
RR
1257 wxTextCtrl *text = (wxTextCtrl*) editor;
1258
cdacccae
VZ
1259 // The icon can't be edited so get its old value and reuse it.
1260 wxVariant valueOld;
1261 wxDataViewColumn* const col = GetOwner();
1262 GetView()->GetModel()->GetValue(valueOld, m_item, col->GetModelColumn());
1263
1264 wxDataViewIconText iconText;
1265 iconText << valueOld;
1266
1267 // But replace the text with the value entered by user.
1268 iconText.SetText(text->GetValue());
1269
c937344c
RR
1270 value << iconText;
1271 return true;
24c4a50f
RR
1272}
1273
a653c966
RR
1274//-----------------------------------------------------------------------------
1275// wxDataViewDropTarget
1276//-----------------------------------------------------------------------------
1277
9adeb77a
VZ
1278#if wxUSE_DRAG_AND_DROP
1279
818d91a9
RR
1280class wxBitmapCanvas: public wxWindow
1281{
1282public:
1283 wxBitmapCanvas( wxWindow *parent, const wxBitmap &bitmap, const wxSize &size ) :
977a41ec 1284 wxWindow( parent, wxID_ANY, wxPoint(0,0), size )
818d91a9
RR
1285 {
1286 m_bitmap = bitmap;
1287 Connect( wxEVT_PAINT, wxPaintEventHandler(wxBitmapCanvas::OnPaint) );
1288 }
9adeb77a 1289
818d91a9
RR
1290 void OnPaint( wxPaintEvent &WXUNUSED(event) )
1291 {
1292 wxPaintDC dc(this);
1293 dc.DrawBitmap( m_bitmap, 0, 0);
1294 }
9adeb77a 1295
818d91a9
RR
1296 wxBitmap m_bitmap;
1297};
1298
1299class wxDataViewDropSource: public wxDropSource
1300{
1301public:
1302 wxDataViewDropSource( wxDataViewMainWindow *win, unsigned int row ) :
977a41ec 1303 wxDropSource( win )
818d91a9
RR
1304 {
1305 m_win = win;
1306 m_row = row;
1307 m_hint = NULL;
1308 }
9adeb77a 1309
818d91a9
RR
1310 ~wxDataViewDropSource()
1311 {
1312 delete m_hint;
1313 }
9adeb77a 1314
818d91a9
RR
1315 virtual bool GiveFeedback( wxDragResult WXUNUSED(effect) )
1316 {
1317 wxPoint pos = wxGetMousePosition();
9adeb77a 1318
818d91a9
RR
1319 if (!m_hint)
1320 {
1321 int liney = m_win->GetLineStart( m_row );
1322 int linex = 0;
1323 m_win->GetOwner()->CalcUnscrolledPosition( 0, liney, NULL, &liney );
1324 m_win->ClientToScreen( &linex, &liney );
1325 m_dist_x = pos.x - linex;
1326 m_dist_y = pos.y - liney;
9adeb77a 1327
818d91a9
RR
1328 int indent = 0;
1329 wxBitmap ib = m_win->CreateItemBitmap( m_row, indent );
1330 m_dist_x -= indent;
9adeb77a 1331 m_hint = new wxFrame( m_win->GetParent(), wxID_ANY, wxEmptyString,
977a41ec
FM
1332 wxPoint(pos.x - m_dist_x, pos.y + 5 ),
1333 ib.GetSize(),
1334 wxFRAME_TOOL_WINDOW |
1335 wxFRAME_FLOAT_ON_PARENT |
1336 wxFRAME_NO_TASKBAR |
1337 wxNO_BORDER );
818d91a9
RR
1338 new wxBitmapCanvas( m_hint, ib, ib.GetSize() );
1339 m_hint->Show();
1340 }
1341 else
1342 {
1343 m_hint->Move( pos.x - m_dist_x, pos.y + 5 );
1344 m_hint->SetTransparent( 128 );
1345 }
9adeb77a 1346
818d91a9
RR
1347 return false;
1348 }
9adeb77a 1349
818d91a9
RR
1350 wxDataViewMainWindow *m_win;
1351 unsigned int m_row;
1352 wxFrame *m_hint;
1353 int m_dist_x,m_dist_y;
1354};
1355
1356
a653c966
RR
1357class wxDataViewDropTarget: public wxDropTarget
1358{
1359public:
1360 wxDataViewDropTarget( wxDataObject *obj, wxDataViewMainWindow *win ) :
1361 wxDropTarget( obj )
1362 {
1363 m_win = win;
1364 }
1365
1366 virtual wxDragResult OnDragOver( wxCoord x, wxCoord y, wxDragResult def )
ce468dc2 1367 {
977a41ec
FM
1368 wxDataFormat format = GetMatchingPair();
1369 if (format == wxDF_INVALID)
1370 return wxDragNone;
1371 return m_win->OnDragOver( format, x, y, def);
ce468dc2 1372 }
9adeb77a 1373
a653c966 1374 virtual bool OnDrop( wxCoord x, wxCoord y )
ce468dc2 1375 {
977a41ec
FM
1376 wxDataFormat format = GetMatchingPair();
1377 if (format == wxDF_INVALID)
1378 return false;
1379 return m_win->OnDrop( format, x, y );
ce468dc2 1380 }
9adeb77a 1381
a653c966 1382 virtual wxDragResult OnData( wxCoord x, wxCoord y, wxDragResult def )
ce468dc2 1383 {
977a41ec
FM
1384 wxDataFormat format = GetMatchingPair();
1385 if (format == wxDF_INVALID)
1386 return wxDragNone;
1387 if (!GetData())
1388 return wxDragNone;
1389 return m_win->OnData( format, x, y, def );
ce468dc2 1390 }
9adeb77a 1391
a653c966
RR
1392 virtual void OnLeave()
1393 { m_win->OnLeave(); }
1394
1395 wxDataViewMainWindow *m_win;
1396};
1397
9adeb77a
VZ
1398#endif // wxUSE_DRAG_AND_DROP
1399
0fcce6b9
RR
1400//-----------------------------------------------------------------------------
1401// wxDataViewRenameTimer
1402//-----------------------------------------------------------------------------
1403
1404wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow *owner )
1405{
1406 m_owner = owner;
1407}
1408
1409void wxDataViewRenameTimer::Notify()
1410{
1411 m_owner->OnRenameTimer();
1412}
1413
4ed7af08
RR
1414//-----------------------------------------------------------------------------
1415// wxDataViewMainWindow
1416//-----------------------------------------------------------------------------
1417
bc4f1ff2 1418// The tree building helper, declared firstly
10875c13 1419static void BuildTreeHelper( const wxDataViewModel * model, const wxDataViewItem & item,
bc4f1ff2 1420 wxDataViewTreeNode * node);
704c3490 1421
0a71f9e9 1422int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 )
cab07038
RR
1423{
1424 if (row1 > row2) return 1;
1425 if (row1 == row2) return 0;
1426 return -1;
1427}
1428
45778c96 1429IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow, wxWindow)
4ed7af08
RR
1430
1431BEGIN_EVENT_TABLE(wxDataViewMainWindow,wxWindow)
1432 EVT_PAINT (wxDataViewMainWindow::OnPaint)
1433 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse)
1434 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus)
cab07038 1435 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus)
64ac3db8 1436 EVT_CHAR_HOOK (wxDataViewMainWindow::OnCharHook)
cab07038 1437 EVT_CHAR (wxDataViewMainWindow::OnChar)
4ed7af08
RR
1438END_EVENT_TABLE()
1439
1440wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID id,
1441 const wxPoint &pos, const wxSize &size, const wxString &name ) :
d81ad1f0 1442 wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE, name ),
cab07038 1443 m_selection( wxDataViewSelectionCmp )
120b9b05 1444
4ed7af08
RR
1445{
1446 SetOwner( parent );
f554a14b 1447
64ac3db8
VZ
1448 m_editorRenderer = NULL;
1449
0fcce6b9
RR
1450 m_lastOnSame = false;
1451 m_renameTimer = new wxDataViewRenameTimer( this );
120b9b05 1452
0fcce6b9
RR
1453 // TODO: user better initial values/nothing selected
1454 m_currentCol = NULL;
1c8e71c9
VS
1455 m_currentColSetByKeyboard = false;
1456 m_useCellFocus = false;
ed3aece5 1457 m_currentRow = (unsigned)-1;
1af67319 1458 m_lineHeight = GetDefaultRowHeight();
e21f75bd 1459
9adeb77a 1460#if wxUSE_DRAG_AND_DROP
e21f75bd
RR
1461 m_dragCount = 0;
1462 m_dragStart = wxPoint(0,0);
120b9b05 1463
821baf7d
RR
1464 m_dragEnabled = false;
1465 m_dropEnabled = false;
9deec111
RR
1466 m_dropHint = false;
1467 m_dropHintLine = (unsigned int) -1;
9adeb77a
VZ
1468#endif // wxUSE_DRAG_AND_DROP
1469
1470 m_lineLastClicked = (unsigned int) -1;
1471 m_lineBeforeLastClicked = (unsigned int) -1;
1472 m_lineSelectSingleOnUp = (unsigned int) -1;
821baf7d 1473
cab07038 1474 m_hasFocus = false;
f554a14b 1475
72664514
RR
1476 SetBackgroundColour( *wxWHITE );
1477
cfa42cb8
JS
1478 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
1479
069b2e59 1480 m_penRule = wxPen(GetRuleColour());
9861f022 1481
bc4f1ff2
FM
1482 // compose a pen whichcan draw black lines
1483 // TODO: maybe there is something system colour to use
069b2e59 1484 m_penExpander = wxPen(wxColour(0,0,0));
bc4f1ff2 1485
422aa8ec 1486 m_root = wxDataViewTreeNode::CreateRootNode();
704c3490 1487
bc4f1ff2 1488 // Make m_count = -1 will cause the class recaculate the real displaying number of rows.
59e60167 1489 m_count = -1;
24c4a50f 1490 m_underMouse = NULL;
4b3feaa7 1491 UpdateDisplay();
4ed7af08
RR
1492}
1493
1494wxDataViewMainWindow::~wxDataViewMainWindow()
1495{
aba9bfd0 1496 DestroyTree();
0fcce6b9
RR
1497 delete m_renameTimer;
1498}
1499
9adeb77a 1500
1af67319
VZ
1501int wxDataViewMainWindow::GetDefaultRowHeight() const
1502{
1503#ifdef __WXMSW__
1504 // We would like to use the same line height that Explorer uses. This is
1505 // different from standard ListView control since Vista.
1506 if ( wxGetWinVersion() >= wxWinVersion_Vista )
1507 return wxMax(16, GetCharHeight()) + 6; // 16 = mini icon height
1508 else
1509#endif // __WXMSW__
1510 return wxMax(16, GetCharHeight()) + 1; // 16 = mini icon height
1511}
1512
1513
1514
9adeb77a 1515#if wxUSE_DRAG_AND_DROP
a653c966
RR
1516bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat &format )
1517{
1518 m_dragFormat = format;
1519 m_dragEnabled = format != wxDF_INVALID;
9adeb77a 1520
a653c966
RR
1521 return true;
1522}
9adeb77a 1523
a653c966
RR
1524bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat &format )
1525{
1526 m_dropFormat = format;
1527 m_dropEnabled = format != wxDF_INVALID;
9adeb77a 1528
a653c966
RR
1529 if (m_dropEnabled)
1530 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format ), this ) );
9adeb77a 1531
a653c966
RR
1532 return true;
1533}
1534
9deec111
RR
1535void wxDataViewMainWindow::RemoveDropHint()
1536{
1537 if (m_dropHint)
1538 {
1539 m_dropHint = false;
1540 RefreshRow( m_dropHintLine );
1541 m_dropHintLine = (unsigned int) -1;
1542 }
1543}
1544
4cef3873 1545wxDragResult wxDataViewMainWindow::OnDragOver( wxDataFormat format, wxCoord x,
bc4f1ff2 1546 wxCoord y, wxDragResult def )
a653c966
RR
1547{
1548 int xx = x;
1549 int yy = y;
1550 m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy );
1551 unsigned int row = GetLineAt( yy );
1552
aecf930c 1553 if ((row >= GetRowCount()) || (xx > GetEndOfLastCol()))
9adeb77a 1554 {
9deec111 1555 RemoveDropHint();
a653c966 1556 return wxDragNone;
9deec111 1557 }
a653c966
RR
1558
1559 wxDataViewItem item = GetItemByRow( row );
9adeb77a 1560
c2c89730 1561 wxDataViewModel *model = GetModel();
9adeb77a 1562
ce7fe42e 1563 wxDataViewEvent event( wxEVT_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() );
a653c966
RR
1564 event.SetEventObject( m_owner );
1565 event.SetItem( item );
1566 event.SetModel( model );
1567 event.SetDataFormat( format );
c04be1a2 1568 event.SetDropEffect( def );
a653c966 1569 if (!m_owner->HandleWindowEvent( event ))
9deec111
RR
1570 {
1571 RemoveDropHint();
a653c966 1572 return wxDragNone;
9deec111 1573 }
a653c966
RR
1574
1575 if (!event.IsAllowed())
9deec111
RR
1576 {
1577 RemoveDropHint();
a653c966 1578 return wxDragNone;
9deec111
RR
1579 }
1580
9adeb77a 1581
9deec111
RR
1582 if (m_dropHint && (row != m_dropHintLine))
1583 RefreshRow( m_dropHintLine );
1584 m_dropHint = true;
1585 m_dropHintLine = row;
1586 RefreshRow( row );
9adeb77a 1587
a653c966
RR
1588 return def;
1589}
1590
1591bool wxDataViewMainWindow::OnDrop( wxDataFormat format, wxCoord x, wxCoord y )
1592{
9deec111
RR
1593 RemoveDropHint();
1594
a653c966
RR
1595 int xx = x;
1596 int yy = y;
1597 m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy );
1598 unsigned int row = GetLineAt( yy );
1599
aecf930c 1600 if ((row >= GetRowCount()) || (xx > GetEndOfLastCol()))
a653c966
RR
1601 return false;
1602
1603 wxDataViewItem item = GetItemByRow( row );
9adeb77a 1604
c2c89730 1605 wxDataViewModel *model = GetModel();
9adeb77a 1606
ce7fe42e 1607 wxDataViewEvent event( wxEVT_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() );
a653c966
RR
1608 event.SetEventObject( m_owner );
1609 event.SetItem( item );
1610 event.SetModel( model );
1611 event.SetDataFormat( format );
1612 if (!m_owner->HandleWindowEvent( event ))
1613 return false;
1614
1615 if (!event.IsAllowed())
1616 return false;
9deec111 1617
a653c966
RR
1618 return true;
1619}
1620
4cef3873 1621wxDragResult wxDataViewMainWindow::OnData( wxDataFormat format, wxCoord x, wxCoord y,
bc4f1ff2 1622 wxDragResult def )
a653c966
RR
1623{
1624 int xx = x;
1625 int yy = y;
1626 m_owner->CalcUnscrolledPosition( xx, yy, &xx, &yy );
1627 unsigned int row = GetLineAt( yy );
1628
aecf930c 1629 if ((row >= GetRowCount()) || (xx > GetEndOfLastCol()))
a653c966
RR
1630 return wxDragNone;
1631
1632 wxDataViewItem item = GetItemByRow( row );
9adeb77a 1633
c2c89730 1634 wxDataViewModel *model = GetModel();
9adeb77a 1635
a653c966
RR
1636 wxCustomDataObject *obj = (wxCustomDataObject *) GetDropTarget()->GetDataObject();
1637
ce7fe42e 1638 wxDataViewEvent event( wxEVT_DATAVIEW_ITEM_DROP, m_owner->GetId() );
a653c966
RR
1639 event.SetEventObject( m_owner );
1640 event.SetItem( item );
1641 event.SetModel( model );
1642 event.SetDataFormat( format );
1643 event.SetDataSize( obj->GetSize() );
1644 event.SetDataBuffer( obj->GetData() );
c04be1a2 1645 event.SetDropEffect( def );
a653c966
RR
1646 if (!m_owner->HandleWindowEvent( event ))
1647 return wxDragNone;
1648
1649 if (!event.IsAllowed())
1650 return wxDragNone;
1651
1652 return def;
1653}
1654
1655void wxDataViewMainWindow::OnLeave()
1656{
9deec111 1657 RemoveDropHint();
a653c966
RR
1658}
1659
818d91a9
RR
1660wxBitmap wxDataViewMainWindow::CreateItemBitmap( unsigned int row, int &indent )
1661{
1662 int height = GetLineHeight( row );
1663 int width = 0;
1664 unsigned int cols = GetOwner()->GetColumnCount();
1665 unsigned int col;
1666 for (col = 0; col < cols; col++)
1667 {
1668 wxDataViewColumn *column = GetOwner()->GetColumnAt(col);
1669 if (column->IsHidden())
1670 continue; // skip it!
1671 width += column->GetWidth();
1672 }
1673
1674 indent = 0;
ce5abbf9 1675 if (!IsList())
818d91a9
RR
1676 {
1677 wxDataViewTreeNode *node = GetTreeNodeByRow(row);
1678 indent = GetOwner()->GetIndent() * node->GetIndentLevel();
4cef3873 1679 indent = indent + m_lineHeight;
bc4f1ff2 1680 // try to use the m_lineHeight as the expander space
818d91a9
RR
1681 }
1682 width -= indent;
1683
1684 wxBitmap bitmap( width, height );
1685 wxMemoryDC dc( bitmap );
1686 dc.SetFont( GetFont() );
1687 dc.SetPen( *wxBLACK_PEN );
1688 dc.SetBrush( *wxWHITE_BRUSH );
1689 dc.DrawRectangle( 0,0,width,height );
9adeb77a 1690
818d91a9 1691 wxDataViewModel *model = m_owner->GetModel();
9adeb77a 1692
1841f079
VZ
1693 wxDataViewColumn * const
1694 expander = GetExpanderColumnOrFirstOne(GetOwner());
9adeb77a 1695
818d91a9
RR
1696 int x = 0;
1697 for (col = 0; col < cols; col++)
1698 {
1699 wxDataViewColumn *column = GetOwner()->GetColumnAt( col );
1700 wxDataViewRenderer *cell = column->GetRenderer();
1701
1702 if (column->IsHidden())
1703 continue; // skip it!
1704
1705 width = column->GetWidth();
9adeb77a 1706
818d91a9
RR
1707 if (column == expander)
1708 width -= indent;
9adeb77a 1709
818d91a9 1710 wxDataViewItem item = GetItemByRow( row );
f0ccd2cb 1711 cell->PrepareForItem(model, item, column->GetModelColumn());
62265c2c 1712
a6f1201f
VZ
1713 wxRect item_rect(x, 0, width, height);
1714 item_rect.Deflate(PADDING_RIGHTLEFT, 0);
818d91a9 1715
bc4f1ff2 1716 // dc.SetClippingRegion( item_rect );
62265c2c 1717 cell->WXCallRender(item_rect, &dc, 0);
bc4f1ff2 1718 // dc.DestroyClippingRegion();
9adeb77a 1719
818d91a9
RR
1720 x += width;
1721 }
9adeb77a 1722
818d91a9
RR
1723 return bitmap;
1724}
1725
9adeb77a
VZ
1726#endif // wxUSE_DRAG_AND_DROP
1727
1728
1c8e71c9
VS
1729// Draw focus rect for individual cell. Unlike native focus rect, we render
1730// this in foreground text color (typically white) to enhance contrast and
1731// make it visible.
1732static void DrawSelectedCellFocusRect(wxDC& dc, const wxRect& rect)
1733{
1734 // (This code is based on wxRendererGeneric::DrawFocusRect and modified.)
1735
1736 // draw the pixels manually because the "dots" in wxPen with wxDOT style
1737 // may be short traits and not really dots
1738 //
1739 // note that to behave in the same manner as DrawRect(), we must exclude
1740 // the bottom and right borders from the rectangle
1741 wxCoord x1 = rect.GetLeft(),
1742 y1 = rect.GetTop(),
1743 x2 = rect.GetRight(),
1744 y2 = rect.GetBottom();
1745
1746 wxDCPenChanger pen(dc, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
1747
1748 wxCoord z;
1749 for ( z = x1 + 1; z < x2; z += 2 )
1750 dc.DrawPoint(z, rect.GetTop());
1751
1752 wxCoord shift = z == x2 ? 0 : 1;
1753 for ( z = y1 + shift; z < y2; z += 2 )
1754 dc.DrawPoint(x2, z);
1755
1756 shift = z == y2 ? 0 : 1;
1757 for ( z = x2 - shift; z > x1; z -= 2 )
1758 dc.DrawPoint(z, y2);
1759
1760 shift = z == x1 ? 0 : 1;
1761 for ( z = y2 - shift; z > y1; z -= 2 )
1762 dc.DrawPoint(x1, z);
1763}
1764
1765
1117d56f 1766void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
0fcce6b9 1767{
c2c89730 1768 wxDataViewModel *model = GetModel();
1117d56f 1769 wxAutoBufferedPaintDC dc( this );
0fcce6b9 1770
bb58fa37 1771 dc.SetBrush(GetOwner()->GetBackgroundColour());
1117d56f 1772 dc.SetPen( *wxTRANSPARENT_PEN );
bb58fa37 1773 dc.DrawRectangle(GetClientSize());
1117d56f 1774
da87ce5a
VZ
1775 if ( IsEmpty() )
1776 {
1777 // No items to draw.
1778 return;
1779 }
1780
1117d56f
RR
1781 // prepare the DC
1782 GetOwner()->PrepareDC( dc );
1783 dc.SetFont( GetFont() );
1784
1785 wxRect update = GetUpdateRegion().GetBox();
1786 m_owner->CalcUnscrolledPosition( update.x, update.y, &update.x, &update.y );
1787
1788 // compute which items needs to be redrawn
344ed1f3 1789 unsigned int item_start = GetLineAt( wxMax(0,update.y) );
1117d56f 1790 unsigned int item_count =
344ed1f3 1791 wxMin( (int)( GetLineAt( wxMax(0,update.y+update.height) ) - item_start + 1),
977a41ec 1792 (int)(GetRowCount( ) - item_start));
1117d56f 1793 unsigned int item_last = item_start + item_count;
99f44d97
VZ
1794
1795 // Send the event to wxDataViewCtrl itself.
1796 wxWindow * const parent = GetParent();
ce7fe42e 1797 wxDataViewEvent cache_event(wxEVT_DATAVIEW_CACHE_HINT, parent->GetId());
99f44d97 1798 cache_event.SetEventObject(parent);
47a8b1e1
VZ
1799 cache_event.SetCache(item_start, item_last - 1);
1800 parent->ProcessWindowEvent(cache_event);
1117d56f
RR
1801
1802 // compute which columns needs to be redrawn
9861f022 1803 unsigned int cols = GetOwner()->GetColumnCount();
f03c22f9
VZ
1804 if ( !cols )
1805 {
1806 // we assume that we have at least one column below and painting an
1807 // empty control is unnecessary anyhow
1808 return;
1809 }
1810
1117d56f 1811 unsigned int col_start = 0;
e822d1bd 1812 unsigned int x_start;
1117d56f 1813 for (x_start = 0; col_start < cols; col_start++)
0fcce6b9 1814 {
702f5349 1815 wxDataViewColumn *col = GetOwner()->GetColumnAt(col_start);
1117d56f 1816 if (col->IsHidden())
9861f022
RR
1817 continue; // skip it!
1818
1117d56f
RR
1819 unsigned int w = col->GetWidth();
1820 if (x_start+w >= (unsigned int)update.x)
0fcce6b9 1821 break;
0fcce6b9 1822
1117d56f
RR
1823 x_start += w;
1824 }
1e510b1e 1825
1117d56f
RR
1826 unsigned int col_last = col_start;
1827 unsigned int x_last = x_start;
1828 for (; col_last < cols; col_last++)
1829 {
702f5349 1830 wxDataViewColumn *col = GetOwner()->GetColumnAt(col_last);
1117d56f
RR
1831 if (col->IsHidden())
1832 continue; // skip it!
afebb87b 1833
1117d56f
RR
1834 if (x_last > (unsigned int)update.GetRight())
1835 break;
4ed7af08 1836
1117d56f
RR
1837 x_last += col->GetWidth();
1838 }
d5025dc0 1839
4bdc891f
VZ
1840 // Draw background of alternate rows specially if required
1841 if ( m_owner->HasFlag(wxDV_ROW_LINES) )
1842 {
1843 wxColour altRowColour = m_owner->m_alternateRowColour;
1844 if ( !altRowColour.IsOk() )
1845 {
1846 // Determine the alternate rows colour automatically from the
1847 // background colour.
1848 const wxColour bgColour = m_owner->GetBackgroundColour();
1849
1850 // Depending on the background, alternate row color
1851 // will be 3% more dark or 50% brighter.
1852 int alpha = bgColour.GetRGB() > 0x808080 ? 97 : 150;
1853 altRowColour = bgColour.ChangeLightness(alpha);
1854 }
1855
1856 dc.SetPen(*wxTRANSPARENT_PEN);
1857 dc.SetBrush(wxBrush(altRowColour));
1858
1859 for (unsigned int item = item_start; item < item_last; item++)
1860 {
1861 if ( item % 2 )
1862 {
1863 dc.DrawRectangle(x_start,
1864 GetLineStart(item),
1865 GetClientSize().GetWidth(),
1866 GetLineHeight(item));
1867 }
1868 }
1869 }
1870
1117d56f
RR
1871 // Draw horizontal rules if required
1872 if ( m_owner->HasFlag(wxDV_HORIZ_RULES) )
1873 {
1874 dc.SetPen(m_penRule);
1875 dc.SetBrush(*wxTRANSPARENT_BRUSH);
aba9bfd0 1876
de4bf0b3 1877 for (unsigned int i = item_start; i <= item_last; i++)
1117d56f 1878 {
344ed1f3 1879 int y = GetLineStart( i );
1117d56f
RR
1880 dc.DrawLine(x_start, y, x_last, y);
1881 }
1882 }
aba9bfd0 1883
1117d56f
RR
1884 // Draw vertical rules if required
1885 if ( m_owner->HasFlag(wxDV_VERT_RULES) )
b7e9f8b1 1886 {
1117d56f
RR
1887 dc.SetPen(m_penRule);
1888 dc.SetBrush(*wxTRANSPARENT_BRUSH);
b7e9f8b1 1889
3999336c
VS
1890 // NB: Vertical rules are drawn in the last pixel of a column so that
1891 // they align perfectly with native MSW wxHeaderCtrl as well as for
1892 // consistency with MSW native list control. There's no vertical
1893 // rule at the most-left side of the control.
1894
1895 int x = x_start - 1;
1117d56f
RR
1896 for (unsigned int i = col_start; i < col_last; i++)
1897 {
702f5349 1898 wxDataViewColumn *col = GetOwner()->GetColumnAt(i);
1117d56f
RR
1899 if (col->IsHidden())
1900 continue; // skip it
d47db7e0 1901
3999336c
VS
1902 x += col->GetWidth();
1903
344ed1f3
RR
1904 dc.DrawLine(x, GetLineStart( item_start ),
1905 x, GetLineStart( item_last ) );
1117d56f 1906 }
1117d56f
RR
1907 }
1908
1909 // redraw the background for the items which are selected/current
1910 for (unsigned int item = item_start; item < item_last; item++)
aba9bfd0 1911 {
1117d56f 1912 bool selected = m_selection.Index( item ) != wxNOT_FOUND;
1c8e71c9 1913
1117d56f 1914 if (selected || item == m_currentRow)
d5025dc0 1915 {
e3dbeaaf
VZ
1916 wxRect rect( x_start, GetLineStart( item ),
1917 x_last - x_start, GetLineHeight( item ) );
1c8e71c9
VS
1918
1919 // draw selection and whole-item focus:
1920 if ( selected )
1921 {
1922 int flags = wxCONTROL_SELECTED;
1923 if (m_hasFocus)
1924 flags |= wxCONTROL_FOCUSED;
1925
1926 wxRendererNative::Get().DrawItemSelectionRect
1927 (
1928 this,
1929 dc,
1930 rect,
1931 flags
1932 );
1933 }
1934
1935 // draw keyboard focus rect if applicable
1936 if ( item == m_currentRow && m_hasFocus )
1937 {
1938 bool renderColumnFocus = false;
1939
1940 if ( m_useCellFocus && m_currentCol && m_currentColSetByKeyboard )
1941 {
1942 renderColumnFocus = true;
1943
1944 // If this is container node without columns, render full-row focus:
1945 if ( !IsList() )
1946 {
1947 wxDataViewTreeNode *node = GetTreeNodeByRow(item);
1948 if ( node->HasChildren() && !model->HasContainerColumns(node->GetItem()) )
1949 renderColumnFocus = false;
1950 }
1951 }
1952
1953 if ( renderColumnFocus )
1954 {
1955 for ( unsigned int i = col_start; i < col_last; i++ )
1956 {
1957 wxDataViewColumn *col = GetOwner()->GetColumnAt(i);
1958 if ( col->IsHidden() )
1959 continue;
1960
1961 rect.width = col->GetWidth();
1962
1963 if ( col == m_currentCol )
1964 {
1965 // make the rect more visible by adding a small
1966 // margin around it:
1967 rect.Deflate(1, 1);
1968
1969 if ( selected )
1970 {
1971 // DrawFocusRect() uses XOR and is all but
1972 // invisible against dark-blue background. Use
1973 // the same color used for selected text.
1974 DrawSelectedCellFocusRect(dc, rect);
1975 }
1976 else
1977 {
1978 wxRendererNative::Get().DrawFocusRect
1979 (
1980 this,
1981 dc,
1982 rect,
1983 0
1984 );
1985 }
1986 break;
1987 }
1988
1989 rect.x += rect.width;
1990 }
1991 }
1992 else
1993 {
1994 // render focus rectangle for the whole row
1995 wxRendererNative::Get().DrawFocusRect
1996 (
1997 this,
1998 dc,
1999 rect,
2000 selected ? (int)wxCONTROL_SELECTED : 0
2001 );
2002 }
2003 }
d47db7e0 2004 }
aba9bfd0 2005 }
9adeb77a
VZ
2006
2007#if wxUSE_DRAG_AND_DROP
9deec111 2008 if (m_dropHint)
9adeb77a
VZ
2009 {
2010 wxRect rect( x_start, GetLineStart( m_dropHintLine ),
e3dbeaaf 2011 x_last - x_start, GetLineHeight( m_dropHintLine ) );
9deec111
RR
2012 dc.SetPen( *wxBLACK_PEN );
2013 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2014 dc.DrawRectangle( rect );
2015 }
9adeb77a 2016#endif // wxUSE_DRAG_AND_DROP
a0f3af5f 2017
1841f079
VZ
2018 wxDataViewColumn * const
2019 expander = GetExpanderColumnOrFirstOne(GetOwner());
d47db7e0 2020
344ed1f3 2021 // redraw all cells for all rows which must be repainted and all columns
1117d56f
RR
2022 wxRect cell_rect;
2023 cell_rect.x = x_start;
1117d56f 2024 for (unsigned int i = col_start; i < col_last; i++)
d47db7e0 2025 {
702f5349 2026 wxDataViewColumn *col = GetOwner()->GetColumnAt( i );
1117d56f
RR
2027 wxDataViewRenderer *cell = col->GetRenderer();
2028 cell_rect.width = col->GetWidth();
d47db7e0 2029
b10c4089 2030 if ( col->IsHidden() || cell_rect.width <= 0 )
344ed1f3 2031 continue; // skip it!
fbda518c 2032
1117d56f
RR
2033 for (unsigned int item = item_start; item < item_last; item++)
2034 {
2035 // get the cell value and set it into the renderer
1117d56f
RR
2036 wxDataViewTreeNode *node = NULL;
2037 wxDataViewItem dataitem;
cfa42cb8 2038
344ed1f3 2039 if (!IsVirtualList())
1117d56f
RR
2040 {
2041 node = GetTreeNodeByRow(item);
2042 if( node == NULL )
2043 continue;
a0f3af5f 2044
1117d56f 2045 dataitem = node->GetItem();
704c3490 2046
311c4a7a
VZ
2047 // Skip all columns of "container" rows except the expander
2048 // column itself unless HasContainerColumns() overrides this.
1841f079 2049 if ( col != expander &&
311c4a7a
VZ
2050 model->IsContainer(dataitem) &&
2051 !model->HasContainerColumns(dataitem) )
1117d56f
RR
2052 continue;
2053 }
2054 else
2055 {
86ba79ed 2056 dataitem = wxDataViewItem( wxUIntToPtr(item+1) );
1117d56f 2057 }
8b6cf9bf 2058
f0ccd2cb 2059 cell->PrepareForItem(model, dataitem, col->GetModelColumn());
62265c2c 2060
c9287446 2061 // update cell_rect
344ed1f3 2062 cell_rect.y = GetLineStart( item );
bc4f1ff2 2063 cell_rect.height = GetLineHeight( item );
8b6cf9bf 2064
1c959a62
VZ
2065 // draw the background
2066 bool selected = m_selection.Index( item ) != wxNOT_FOUND;
2067 if ( !selected )
2068 DrawCellBackground( cell, dc, cell_rect );
2069
b10c4089 2070 // deal with the expander
1117d56f 2071 int indent = 0;
ce5abbf9 2072 if ((!IsList()) && (col == expander))
1117d56f 2073 {
bc4f1ff2 2074 // Calculate the indent first
b10c4089 2075 indent = GetOwner()->GetIndent() * node->GetIndentLevel();
bc4f1ff2 2076
b10c4089
VZ
2077 // we reserve m_lineHeight of horizontal space for the expander
2078 // but leave EXPANDER_MARGIN around the expander itself
2079 int exp_x = cell_rect.x + indent + EXPANDER_MARGIN;
bc4f1ff2 2080
b10c4089 2081 indent += m_lineHeight;
bc4f1ff2 2082
b10c4089
VZ
2083 // draw expander if needed and visible
2084 if ( node->HasChildren() && exp_x < cell_rect.GetRight() )
1117d56f 2085 {
b10c4089
VZ
2086 dc.SetPen( m_penExpander );
2087 dc.SetBrush( wxNullBrush );
2088
2089 int exp_size = m_lineHeight - 2*EXPANDER_MARGIN;
2090 int exp_y = cell_rect.y + (cell_rect.height - exp_size)/2
2091 + EXPANDER_MARGIN - EXPANDER_OFFSET;
2092
2093 const wxRect rect(exp_x, exp_y, exp_size, exp_size);
2094
1117d56f 2095 int flag = 0;
b10c4089 2096 if ( m_underMouse == node )
1117d56f 2097 flag |= wxCONTROL_CURRENT;
b10c4089
VZ
2098 if ( node->IsOpen() )
2099 flag |= wxCONTROL_EXPANDED;
2100
2101 // ensure that we don't overflow the cell (which might
2102 // happen if the column is very narrow)
2103 wxDCClipper clip(dc, cell_rect);
2104
2105 wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, flag);
1117d56f 2106 }
bc4f1ff2
FM
2107
2108 // force the expander column to left-center align
977a41ec 2109 cell->SetAlignment( wxALIGN_CENTER_VERTICAL );
1117d56f 2110 }
1117d56f 2111
a6f1201f
VZ
2112 wxRect item_rect = cell_rect;
2113 item_rect.Deflate(PADDING_RIGHTLEFT, 0);
2114
2115 // account for the tree indent (harmless if we're not indented)
1117d56f 2116 item_rect.x += indent;
a6f1201f 2117 item_rect.width -= indent;
1117d56f 2118
b10c4089
VZ
2119 if ( item_rect.width <= 0 )
2120 continue;
2121
1117d56f 2122 int state = 0;
1c959a62 2123 if (m_hasFocus && selected)
1117d56f
RR
2124 state |= wxDATAVIEW_CELL_SELECTED;
2125
2126 // TODO: it would be much more efficient to create a clipping
2127 // region for the entire column being rendered (in the OnPaint
2128 // of wxDataViewMainWindow) instead of a single clip region for
2129 // each cell. However it would mean that each renderer should
2130 // respect the given wxRect's top & bottom coords, eventually
2131 // violating only the left & right coords - however the user can
2132 // make its own renderer and thus we cannot be sure of that.
a6f1201f 2133 wxDCClipper clip(dc, item_rect);
2d0d7813 2134
62265c2c 2135 cell->WXCallRender(item_rect, &dc, state);
1117d56f
RR
2136 }
2137
2138 cell_rect.x += cell_rect.width;
2139 }
2140}
2141
1c959a62
VZ
2142
2143void wxDataViewMainWindow::DrawCellBackground( wxDataViewRenderer* cell, wxDC& dc, const wxRect& rect )
2144{
2145 wxRect rectBg( rect );
2146
2147 // don't overlap the horizontal rules
2148 if ( m_owner->HasFlag(wxDV_HORIZ_RULES) )
2149 {
2150 rectBg.x++;
2151 rectBg.width--;
2152 }
2153
2154 // don't overlap the vertical rules
2155 if ( m_owner->HasFlag(wxDV_VERT_RULES) )
2156 {
2157 rectBg.y++;
2158 rectBg.height--;
2159 }
2160
2161 cell->RenderBackground(&dc, rectBg);
2162}
2163
1117d56f
RR
2164void wxDataViewMainWindow::OnRenameTimer()
2165{
2166 // We have to call this here because changes may just have
2167 // been made and no screen update taken place.
2168 if ( m_dirty )
977a41ec
FM
2169 {
2170 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2171 // (needs to be tested!)
1117d56f 2172 wxSafeYield();
977a41ec 2173 }
1117d56f 2174
0a807957 2175 wxDataViewItem item = GetItemByRow( m_currentRow );
1117d56f 2176
64ac3db8
VZ
2177 StartEditing( item, m_currentCol );
2178}
1117d56f 2179
64ac3db8
VZ
2180void
2181wxDataViewMainWindow::StartEditing(const wxDataViewItem& item,
2182 const wxDataViewColumn* col)
2183{
2184 wxDataViewRenderer* renderer = col->GetRenderer();
cf5d4c76 2185 if ( !IsCellEditableInMode(item, col, wxDATAVIEW_CELL_EDITABLE) )
64ac3db8
VZ
2186 return;
2187
2188 const wxRect itemRect = GetItemRect(item, col);
2189 if ( renderer->StartEditing(item, itemRect) )
2190 {
2191 // Save the renderer to be able to finish/cancel editing it later and
2192 // save the control to be able to detect if we're still editing it.
2193 m_editorRenderer = renderer;
2194 m_editorCtrl = renderer->GetEditorCtrl();
2195 }
1117d56f
RR
2196}
2197
bc4f1ff2 2198//-----------------------------------------------------------------------------
1117d56f 2199// Helper class for do operation on the tree node
bc4f1ff2 2200//-----------------------------------------------------------------------------
1117d56f
RR
2201class DoJob
2202{
2203public:
59e60167
VZ
2204 DoJob() { }
2205 virtual ~DoJob() { }
1117d56f 2206
bc4f1ff2 2207 // The return value control how the tree-walker tranverse the tree
d54f0605
VS
2208 enum
2209 {
2210 DONE, // Job done, stop traversing and return
2211 SKIP_SUBTREE, // Ignore the current node's subtree and continue
2212 CONTINUE // Job not done, continue
2213 };
2214
59e60167 2215 virtual int operator() ( wxDataViewTreeNode * node ) = 0;
1117d56f
RR
2216};
2217
2218bool Walker( wxDataViewTreeNode * node, DoJob & func )
2219{
422aa8ec 2220 wxCHECK_MSG( node, false, "can't walk NULL node" );
1117d56f
RR
2221
2222 switch( func( node ) )
2223 {
d54f0605 2224 case DoJob::DONE:
59e60167 2225 return true;
d54f0605 2226 case DoJob::SKIP_SUBTREE:
1117d56f 2227 return false;
d54f0605 2228 case DoJob::CONTINUE:
422aa8ec 2229 break;
1117d56f
RR
2230 }
2231
d0cfefc4 2232 if ( node->HasChildren() )
1117d56f 2233 {
ff3c5ad3 2234 const wxDataViewTreeNodes& nodes = node->GetChildNodes();
d0cfefc4
VS
2235
2236 for ( wxDataViewTreeNodes::const_iterator i = nodes.begin();
2237 i != nodes.end();
2238 ++i )
2239 {
2240 if ( Walker(*i, func) )
2241 return true;
2242 }
1117d56f 2243 }
422aa8ec 2244
1117d56f
RR
2245 return false;
2246}
2247
2248bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxDataViewItem & item)
2249{
86ba79ed 2250 if (IsVirtualList())
1117d56f 2251 {
49d2b75d 2252 wxDataViewVirtualListModel *list_model =
c2c89730 2253 (wxDataViewVirtualListModel*) GetModel();
49d2b75d 2254 m_count = list_model->GetCount();
1117d56f 2255 }
0b93babd
VS
2256 else
2257 {
2258 SortPrepare();
cfa42cb8 2259
422aa8ec 2260 wxDataViewTreeNode *parentNode = FindNode(parent);
1117d56f 2261
422aa8ec 2262 if ( !parentNode )
0b93babd 2263 return false;
1117d56f 2264
5bb82ad4
VS
2265 wxDataViewItemArray modelSiblings;
2266 GetModel()->GetChildren(parent, modelSiblings);
2267 const int modelSiblingsSize = modelSiblings.size();
2268
2269 int posInModel = modelSiblings.Index(item, /*fromEnd=*/true);
2270 wxCHECK_MSG( posInModel != wxNOT_FOUND, false, "adding non-existent item?" );
35219832 2271
422aa8ec 2272 wxDataViewTreeNode *itemNode = new wxDataViewTreeNode(parentNode, item);
c2c89730 2273 itemNode->SetHasChildren(GetModel()->IsContainer(item));
1117d56f 2274
422aa8ec 2275 parentNode->SetHasChildren(true);
5bb82ad4
VS
2276
2277 const wxDataViewTreeNodes& nodeSiblings = parentNode->GetChildNodes();
2278 const int nodeSiblingsSize = nodeSiblings.size();
2279
2280 int nodePos = 0;
2281
2282 if ( posInModel == modelSiblingsSize - 1 )
2283 {
2284 nodePos = nodeSiblingsSize;
2285 }
2286 else if ( modelSiblingsSize == nodeSiblingsSize + 1 )
2287 {
2288 // This is the simple case when our node tree already matches the
2289 // model and only this one item is missing.
2290 nodePos = posInModel;
2291 }
2292 else
2293 {
2294 // It's possible that a larger discrepancy between the model and
2295 // our realization exists. This can happen e.g. when adding a bunch
2296 // of items to the model and then calling ItemsAdded() just once
2297 // afterwards. In this case, we must find the right position by
2298 // looking at sibling items.
2299
2300 // append to the end if we won't find a better position:
2301 nodePos = nodeSiblingsSize;
2302
2303 for ( int nextItemPos = posInModel + 1;
2304 nextItemPos < modelSiblingsSize;
2305 nextItemPos++ )
2306 {
2307 int nextNodePos = parentNode->FindChildByItem(modelSiblings[nextItemPos]);
2308 if ( nextNodePos != wxNOT_FOUND )
2309 {
2310 nodePos = nextNodePos;
2311 break;
2312 }
2313 }
2314 }
2315
422aa8ec 2316 parentNode->ChangeSubTreeCount(+1);
5bb82ad4 2317 parentNode->InsertChild(itemNode, nodePos);
1117d56f 2318
0b93babd 2319 m_count = -1;
1117d56f 2320 }
1117d56f 2321
bed74e48 2322 GetOwner()->InvalidateColBestWidths();
1117d56f
RR
2323 UpdateDisplay();
2324
2325 return true;
2326}
2327
1117d56f 2328bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent,
86ba79ed 2329 const wxDataViewItem& item)
1117d56f 2330{
86ba79ed 2331 if (IsVirtualList())
1117d56f 2332 {
49d2b75d 2333 wxDataViewVirtualListModel *list_model =
c2c89730 2334 (wxDataViewVirtualListModel*) GetModel();
49d2b75d 2335 m_count = list_model->GetCount();
03647350 2336
57ab4546
VS
2337 if ( !m_selection.empty() )
2338 {
2339 const int row = GetRowByItem(item);
2340
f6410588
VS
2341 int rowIndexInSelection = wxNOT_FOUND;
2342
57ab4546
VS
2343 const size_t selCount = m_selection.size();
2344 for ( size_t i = 0; i < selCount; i++ )
2345 {
f6410588
VS
2346 if ( m_selection[i] == (unsigned)row )
2347 rowIndexInSelection = i;
2348 else if ( m_selection[i] > (unsigned)row )
57ab4546
VS
2349 m_selection[i]--;
2350 }
2351
f6410588
VS
2352 if ( rowIndexInSelection != wxNOT_FOUND )
2353 m_selection.RemoveAt(rowIndexInSelection);
57ab4546
VS
2354 }
2355
1117d56f 2356 }
b6252949
VS
2357 else // general case
2358 {
422aa8ec 2359 wxDataViewTreeNode *parentNode = FindNode(parent);
1117d56f 2360
b6252949
VS
2361 // Notice that it is possible that the item being deleted is not in the
2362 // tree at all, for example we could be deleting a never shown (because
2363 // collapsed) item in a tree model. So it's not an error if we don't know
2364 // about this item, just return without doing anything then.
b632efe0 2365 if ( !parentNode )
28fefd45 2366 return true;
b23de238 2367
d0cfefc4 2368 wxCHECK_MSG( parentNode->HasChildren(), false, "parent node doesn't have children?" );
ff3c5ad3 2369 const wxDataViewTreeNodes& parentsChildren = parentNode->GetChildNodes();
351461fc 2370
422aa8ec
VS
2371 // We can't use FindNode() to find 'item', because it was already
2372 // removed from the model by the time ItemDeleted() is called, so we
2373 // have to do it manually. We keep track of its position as well for
2374 // later use.
2375 int itemPosInNode = 0;
57ab4546 2376 wxDataViewTreeNode *itemNode = NULL;
422aa8ec
VS
2377 for ( wxDataViewTreeNodes::const_iterator i = parentsChildren.begin();
2378 i != parentsChildren.end();
2379 ++i, ++itemPosInNode )
d47db7e0 2380 {
422aa8ec 2381 if( (*i)->GetItem() == item )
d47db7e0 2382 {
422aa8ec 2383 itemNode = *i;
d47db7e0
RR
2384 break;
2385 }
2386 }
57ab4546 2387
422aa8ec
VS
2388 // If the parent wasn't expanded, it's possible that we didn't have a
2389 // node corresponding to 'item' and so there's nothing left to do.
2390 if ( !itemNode )
b6252949 2391 {
422aa8ec
VS
2392 // If this was the last child to be removed, it's possible the parent
2393 // node became a leaf. Let's ask the model about it.
ff3c5ad3 2394 if ( parentNode->GetChildNodes().empty() )
c2c89730 2395 parentNode->SetHasChildren(GetModel()->IsContainer(parent));
a8505db0 2396
28fefd45 2397 return true;
b6252949 2398 }
57ab4546 2399
422aa8ec
VS
2400 // Delete the item from wxDataViewTreeNode representation:
2401 const int itemsDeleted = 1 + itemNode->GetSubTreeCount();
2402
a2d3c415
VS
2403 parentNode->RemoveChild(itemNode);
2404 delete itemNode;
422aa8ec
VS
2405 parentNode->ChangeSubTreeCount(-itemsDeleted);
2406
b6252949
VS
2407 // Make the row number invalid and get a new valid one when user call GetRowCount
2408 m_count = -1;
422aa8ec
VS
2409
2410 // If this was the last child to be removed, it's possible the parent
2411 // node became a leaf. Let's ask the model about it.
ff3c5ad3 2412 if ( parentNode->GetChildNodes().empty() )
c3b504ad
VZ
2413 {
2414 bool isContainer = GetModel()->IsContainer(parent);
2415 parentNode->SetHasChildren(isContainer);
2416 if ( isContainer )
2417 {
2418 // If it's still a container, make sure we show "+" icon for it
2419 // and not "-" one as there is nothing to collapse any more.
2420 if ( parentNode->IsOpen() )
2421 parentNode->ToggleOpen();
2422 }
2423 }
57ab4546
VS
2424
2425 // Update selection by removing 'item' and its entire children tree from the selection.
2426 if ( !m_selection.empty() )
2427 {
2428 // we can't call GetRowByItem() on 'item', as it's already deleted, so compute it from
b632efe0 2429 // the parent ('parentNode') and position in its list of children
57ab4546
VS
2430 int itemRow;
2431 if ( itemPosInNode == 0 )
2432 {
b632efe0
VS
2433 // 1st child, row number is that of the parent parentNode + 1
2434 itemRow = GetRowByItem(parentNode->GetItem()) + 1;
57ab4546
VS
2435 }
2436 else
2437 {
2438 // row number is that of the sibling above 'item' + its subtree if any + 1
ff3c5ad3 2439 const wxDataViewTreeNode *siblingNode = parentNode->GetChildNodes()[itemPosInNode - 1];
57ab4546 2440
422aa8ec
VS
2441 itemRow = GetRowByItem(siblingNode->GetItem()) +
2442 siblingNode->GetSubTreeCount() +
2443 1;
57ab4546
VS
2444 }
2445
2446 wxDataViewSelection newsel(wxDataViewSelectionCmp);
2447
3823a15e
VZ
2448 const size_t numSelections = m_selection.size();
2449 for ( size_t i = 0; i < numSelections; ++i )
57ab4546 2450 {
3823a15e 2451 const int s = m_selection[i];
57ab4546
VS
2452 if ( s < itemRow )
2453 newsel.push_back(s);
2454 else if ( s >= itemRow + itemsDeleted )
2455 newsel.push_back(s - itemsDeleted);
2456 // else: deleted item, remove from selection
2457 }
2458
2459 m_selection = newsel;
2460 }
d47db7e0 2461 }
24c4a50f 2462
bc4f1ff2 2463 // Change the current row to the last row if the current exceed the max row number
ed3aece5 2464 if ( m_currentRow >= GetRowCount() )
8c7b8711 2465 ChangeCurrentRow(m_count - 1);
351461fc 2466
bed74e48 2467 GetOwner()->InvalidateColBestWidths();
99d471a5 2468 UpdateDisplay();
b7fe2261 2469
99d471a5 2470 return true;
a0f3af5f
RR
2471}
2472
aba9bfd0 2473bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem & item)
a0f3af5f 2474{
66e09788 2475 SortPrepare();
b7e9f8b1
RR
2476 g_model->Resort();
2477
bed74e48 2478 GetOwner()->InvalidateColBestWidths();
ac098108 2479
bc4f1ff2 2480 // Send event
6608fdab 2481 wxWindow *parent = GetParent();
ce7fe42e 2482 wxDataViewEvent le(wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId());
6608fdab 2483 le.SetEventObject(parent);
c2c89730 2484 le.SetModel(GetModel());
6608fdab 2485 le.SetItem(item);
857f05e1 2486 parent->ProcessWindowEvent(le);
b5ec7dd6 2487
99d471a5 2488 return true;
a0f3af5f
RR
2489}
2490
d93cc830 2491bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem & item, unsigned int model_column )
a0f3af5f 2492{
d93cc830
RR
2493 int view_column = -1;
2494 unsigned int n_col = m_owner->GetColumnCount();
2495 for (unsigned i = 0; i < n_col; i++)
2496 {
2497 wxDataViewColumn *column = m_owner->GetColumn( i );
2498 if (column->GetModelColumn() == model_column)
2499 {
2500 view_column = (int) i;
2501 break;
2502 }
2503 }
2504 if (view_column == -1)
2505 return false;
2506
9861f022 2507 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
aba9bfd0 2508/*#define MAX_VIRTUAL_WIDTH 100000
9861f022
RR
2509
2510 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
0fdc2321
RR
2511 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2512 Refresh( true, &rect );
2513
2514 return true;
aba9bfd0 2515*/
66e09788 2516 SortPrepare();
b7e9f8b1
RR
2517 g_model->Resort();
2518
bed74e48 2519 GetOwner()->InvalidateColBestWidth(view_column);
ac098108 2520
bc4f1ff2 2521 // Send event
b7e9f8b1 2522 wxWindow *parent = GetParent();
ce7fe42e 2523 wxDataViewEvent le(wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId());
b7e9f8b1 2524 le.SetEventObject(parent);
c2c89730 2525 le.SetModel(GetModel());
b7e9f8b1 2526 le.SetItem(item);
d93cc830
RR
2527 le.SetColumn(view_column);
2528 le.SetDataViewColumn(GetOwner()->GetColumn(view_column));
857f05e1 2529 parent->ProcessWindowEvent(le);
d47db7e0 2530
0fcce6b9 2531 return true;
a0f3af5f
RR
2532}
2533
2534bool wxDataViewMainWindow::Cleared()
2535{
704c3490 2536 DestroyTree();
97e5b064 2537 m_selection.Clear();
ed3aece5 2538 m_currentRow = (unsigned)-1;
cfa42cb8 2539
be416221
VZ
2540 if (GetModel())
2541 {
2542 SortPrepare();
2543 BuildTree( GetModel() );
2544 }
2545 else
2546 {
2547 m_count = 0;
2548 }
cfa42cb8 2549
bed74e48 2550 GetOwner()->InvalidateColBestWidths();
99d471a5 2551 UpdateDisplay();
b7e9f8b1 2552
99d471a5 2553 return true;
a0f3af5f
RR
2554}
2555
4b3feaa7
RR
2556void wxDataViewMainWindow::UpdateDisplay()
2557{
2558 m_dirty = true;
7d5d2f23 2559 m_underMouse = NULL;
4b3feaa7
RR
2560}
2561
2562void wxDataViewMainWindow::OnInternalIdle()
2563{
2564 wxWindow::OnInternalIdle();
f554a14b 2565
4b3feaa7
RR
2566 if (m_dirty)
2567 {
2568 RecalculateDisplay();
2569 m_dirty = false;
2570 }
2571}
2572
2573void wxDataViewMainWindow::RecalculateDisplay()
2574{
c2c89730 2575 wxDataViewModel *model = GetModel();
4b3feaa7
RR
2576 if (!model)
2577 {
2578 Refresh();
2579 return;
2580 }
f554a14b 2581
9861f022 2582 int width = GetEndOfLastCol();
344ed1f3 2583 int height = GetLineStart( GetRowCount() );
4b3feaa7
RR
2584
2585 SetVirtualSize( width, height );
2586 GetOwner()->SetScrollRate( 10, m_lineHeight );
f554a14b 2587
4b3feaa7
RR
2588 Refresh();
2589}
2590
2591void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
2592{
d93cc830
RR
2593 m_underMouse = NULL;
2594
4b3feaa7 2595 wxWindow::ScrollWindow( dx, dy, rect );
9861f022
RR
2596
2597 if (GetOwner()->m_headerArea)
2598 GetOwner()->m_headerArea->ScrollWindow( dx, 0 );
4b3feaa7
RR
2599}
2600
fbda518c 2601void wxDataViewMainWindow::ScrollTo( int rows, int column )
b7e9f8b1 2602{
d93cc830
RR
2603 m_underMouse = NULL;
2604
b7e9f8b1
RR
2605 int x, y;
2606 m_owner->GetScrollPixelsPerUnit( &x, &y );
344ed1f3 2607 int sy = GetLineStart( rows )/y;
eff1c3e8 2608 int sx = -1;
fbda518c
RR
2609 if( column != -1 )
2610 {
2611 wxRect rect = GetClientRect();
67be459b 2612 int colnum = 0;
dd639a4f 2613 int x_start, w = 0;
fbda518c
RR
2614 int xx, yy, xe;
2615 m_owner->CalcUnscrolledPosition( rect.x, rect.y, &xx, &yy );
2616 for (x_start = 0; colnum < column; colnum++)
2617 {
702f5349 2618 wxDataViewColumn *col = GetOwner()->GetColumnAt(colnum);
fbda518c
RR
2619 if (col->IsHidden())
2620 continue; // skip it!
2621
2622 w = col->GetWidth();
2623 x_start += w;
2624 }
2625
e822d1bd 2626 int x_end = x_start + w;
fbda518c
RR
2627 xe = xx + rect.width;
2628 if( x_end > xe )
2629 {
2630 sx = ( xx + x_end - xe )/x;
2631 }
2632 if( x_start < xx )
2633 {
b7fe2261 2634 sx = x_start/x;
fbda518c
RR
2635 }
2636 }
2637 m_owner->Scroll( sx, sy );
b7e9f8b1
RR
2638}
2639
9861f022 2640int wxDataViewMainWindow::GetCountPerPage() const
cab07038
RR
2641{
2642 wxSize size = GetClientSize();
2643 return size.y / m_lineHeight;
2644}
2645
9861f022 2646int wxDataViewMainWindow::GetEndOfLastCol() const
e21f75bd
RR
2647{
2648 int width = 0;
0a71f9e9 2649 unsigned int i;
9861f022 2650 for (i = 0; i < GetOwner()->GetColumnCount(); i++)
e21f75bd 2651 {
c741d33f 2652 const wxDataViewColumn *c =
702f5349 2653 const_cast<wxDataViewCtrl*>(GetOwner())->GetColumnAt( i );
9861f022
RR
2654
2655 if (!c->IsHidden())
2656 width += c->GetWidth();
e21f75bd
RR
2657 }
2658 return width;
2659}
2660
9861f022 2661unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
72664514
RR
2662{
2663 int x = 0;
2664 int y = 0;
2665 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
120b9b05 2666
344ed1f3 2667 return GetLineAt( y );
72664514
RR
2668}
2669
442c56e6 2670unsigned int wxDataViewMainWindow::GetLastVisibleRow()
72664514
RR
2671{
2672 wxSize client_size = GetClientSize();
c741d33f 2673 m_owner->CalcUnscrolledPosition( client_size.x, client_size.y,
977a41ec 2674 &client_size.x, &client_size.y );
72664514 2675
bc4f1ff2 2676 // we should deal with the pixel here
344ed1f3 2677 unsigned int row = GetLineAt(client_size.y) - 1;
b7fe2261 2678
fbda518c 2679 return wxMin( GetRowCount()-1, row );
72664514
RR
2680}
2681
f2bf2d71 2682unsigned int wxDataViewMainWindow::GetRowCount() const
cab07038 2683{
3b6280be
RR
2684 if ( m_count == -1 )
2685 {
f2bf2d71
VZ
2686 wxDataViewMainWindow* const
2687 self = const_cast<wxDataViewMainWindow*>(this);
2688 self->m_count = RecalculateCount();
2689 self->UpdateDisplay();
3b6280be 2690 }
aba9bfd0 2691 return m_count;
cab07038
RR
2692}
2693
0a71f9e9 2694void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row )
e21f75bd
RR
2695{
2696 m_currentRow = row;
120b9b05 2697
e21f75bd
RR
2698 // send event
2699}
2700
cab07038
RR
2701void wxDataViewMainWindow::SelectAllRows( bool on )
2702{
4a851b11
VZ
2703 if (IsEmpty())
2704 return;
120b9b05 2705
cab07038
RR
2706 if (on)
2707 {
72664514 2708 m_selection.Clear();
0a71f9e9 2709 for (unsigned int i = 0; i < GetRowCount(); i++)
cab07038 2710 m_selection.Add( i );
72664514
RR
2711 Refresh();
2712 }
2713 else
2714 {
0a71f9e9
RR
2715 unsigned int first_visible = GetFirstVisibleRow();
2716 unsigned int last_visible = GetLastVisibleRow();
2717 unsigned int i;
72664514 2718 for (i = 0; i < m_selection.GetCount(); i++)
120b9b05 2719 {
0a71f9e9 2720 unsigned int row = m_selection[i];
72664514
RR
2721 if ((row >= first_visible) && (row <= last_visible))
2722 RefreshRow( row );
2723 }
2724 m_selection.Clear();
cab07038 2725 }
cab07038
RR
2726}
2727
0a71f9e9 2728void wxDataViewMainWindow::SelectRow( unsigned int row, bool on )
cab07038
RR
2729{
2730 if (m_selection.Index( row ) == wxNOT_FOUND)
2731 {
2732 if (on)
2733 {
2734 m_selection.Add( row );
2735 RefreshRow( row );
2736 }
2737 }
2738 else
2739 {
2740 if (!on)
2741 {
2742 m_selection.Remove( row );
2743 RefreshRow( row );
2744 }
2745 }
2746}
2747
0a71f9e9 2748void wxDataViewMainWindow::SelectRows( unsigned int from, unsigned int to, bool on )
cab07038
RR
2749{
2750 if (from > to)
2751 {
0a71f9e9 2752 unsigned int tmp = from;
cab07038
RR
2753 from = to;
2754 to = tmp;
2755 }
2756
0a71f9e9 2757 unsigned int i;
cab07038
RR
2758 for (i = from; i <= to; i++)
2759 {
2760 if (m_selection.Index( i ) == wxNOT_FOUND)
2761 {
2762 if (on)
2763 m_selection.Add( i );
2764 }
2765 else
2766 {
2767 if (!on)
2768 m_selection.Remove( i );
2769 }
2770 }
2771 RefreshRows( from, to );
2772}
2773
87f0efe2
RR
2774void wxDataViewMainWindow::Select( const wxArrayInt& aSelections )
2775{
2776 for (size_t i=0; i < aSelections.GetCount(); i++)
2777 {
2778 int n = aSelections[i];
2779
2780 m_selection.Add( n );
2781 RefreshRow( n );
2782 }
2783}
2784
0a71f9e9 2785void wxDataViewMainWindow::ReverseRowSelection( unsigned int row )
cab07038
RR
2786{
2787 if (m_selection.Index( row ) == wxNOT_FOUND)
2788 m_selection.Add( row );
2789 else
2790 m_selection.Remove( row );
120b9b05 2791 RefreshRow( row );
cab07038
RR
2792}
2793
0a71f9e9 2794bool wxDataViewMainWindow::IsRowSelected( unsigned int row )
cab07038
RR
2795{
2796 return (m_selection.Index( row ) != wxNOT_FOUND);
2797}
2798
526e19e2
RR
2799void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem& item)
2800{
2801 wxWindow *parent = GetParent();
ce7fe42e 2802 wxDataViewEvent le(wxEVT_DATAVIEW_SELECTION_CHANGED, parent->GetId());
526e19e2
RR
2803
2804 le.SetEventObject(parent);
c2c89730 2805 le.SetModel(GetModel());
526e19e2
RR
2806 le.SetItem( item );
2807
857f05e1 2808 parent->ProcessWindowEvent(le);
526e19e2
RR
2809}
2810
0a71f9e9 2811void wxDataViewMainWindow::RefreshRow( unsigned int row )
cab07038 2812{
344ed1f3 2813 wxRect rect( 0, GetLineStart( row ), GetEndOfLastCol(), GetLineHeight( row ) );
cab07038 2814 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
120b9b05 2815
cab07038
RR
2816 wxSize client_size = GetClientSize();
2817 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2818 wxRect intersect_rect = client_rect.Intersect( rect );
2819 if (intersect_rect.width > 0)
2820 Refresh( true, &intersect_rect );
2821}
2822
0a71f9e9 2823void wxDataViewMainWindow::RefreshRows( unsigned int from, unsigned int to )
cab07038
RR
2824{
2825 if (from > to)
2826 {
0a71f9e9 2827 unsigned int tmp = to;
cab07038
RR
2828 to = from;
2829 from = tmp;
2830 }
2831
344ed1f3 2832 wxRect rect( 0, GetLineStart( from ), GetEndOfLastCol(), GetLineStart( (to-from+1) ) );
cab07038 2833 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
120b9b05 2834
cab07038
RR
2835 wxSize client_size = GetClientSize();
2836 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2837 wxRect intersect_rect = client_rect.Intersect( rect );
2838 if (intersect_rect.width > 0)
2839 Refresh( true, &intersect_rect );
2840}
2841
0a71f9e9 2842void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow )
cab07038 2843{
cab07038 2844 wxSize client_size = GetClientSize();
344ed1f3
RR
2845 int start = GetLineStart( firstRow );
2846 m_owner->CalcScrolledPosition( start, 0, &start, NULL );
2847 if (start > client_size.y) return;
2848
2849 wxRect rect( 0, start, client_size.x, client_size.y - start );
777f9cef 2850
344ed1f3 2851 Refresh( true, &rect );
cab07038
RR
2852}
2853
9861f022
RR
2854wxRect wxDataViewMainWindow::GetLineRect( unsigned int row ) const
2855{
2856 wxRect rect;
2857 rect.x = 0;
344ed1f3 2858 rect.y = GetLineStart( row );
9861f022 2859 rect.width = GetEndOfLastCol();
344ed1f3 2860 rect.height = GetLineHeight( row );
9861f022
RR
2861
2862 return rect;
cab07038
RR
2863}
2864
344ed1f3
RR
2865int wxDataViewMainWindow::GetLineStart( unsigned int row ) const
2866{
c2c89730 2867 const wxDataViewModel *model = GetModel();
777f9cef 2868
344ed1f3
RR
2869 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT)
2870 {
2871 // TODO make more efficient
777f9cef 2872
344ed1f3 2873 int start = 0;
777f9cef 2874
344ed1f3
RR
2875 unsigned int r;
2876 for (r = 0; r < row; r++)
2877 {
e170469f
RR
2878 const wxDataViewTreeNode* node = GetTreeNodeByRow(r);
2879 if (!node) return start;
777f9cef 2880
e170469f 2881 wxDataViewItem item = node->GetItem();
777f9cef 2882
e170469f
RR
2883 unsigned int cols = GetOwner()->GetColumnCount();
2884 unsigned int col;
2885 int height = m_lineHeight;
2886 for (col = 0; col < cols; col++)
2887 {
344ed1f3
RR
2888 const wxDataViewColumn *column = GetOwner()->GetColumn(col);
2889 if (column->IsHidden())
2890 continue; // skip it!
777f9cef 2891
4cef3873
VZ
2892 if ((col != 0) &&
2893 model->IsContainer(item) &&
ce468dc2 2894 !model->HasContainerColumns(item))
ba5f54e6 2895 continue; // skip it!
777f9cef 2896
4cef3873 2897 wxDataViewRenderer *renderer =
ce468dc2 2898 const_cast<wxDataViewRenderer*>(column->GetRenderer());
f0ccd2cb 2899 renderer->PrepareForItem(model, item, column->GetModelColumn());
86755098 2900
344ed1f3 2901 height = wxMax( height, renderer->GetSize().y );
e170469f 2902 }
777f9cef 2903
e170469f 2904 start += height;
344ed1f3 2905 }
777f9cef 2906
344ed1f3
RR
2907 return start;
2908 }
2909 else
2910 {
2911 return row * m_lineHeight;
2912 }
2913}
2914
2915int wxDataViewMainWindow::GetLineAt( unsigned int y ) const
2916{
c2c89730 2917 const wxDataViewModel *model = GetModel();
ba5f54e6 2918
777f9cef
VZ
2919 // check for the easy case first
2920 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT) )
2921 return y / m_lineHeight;
2922
2923 // TODO make more efficient
2924 unsigned int row = 0;
2925 unsigned int yy = 0;
2926 for (;;)
344ed1f3 2927 {
ce468dc2
FM
2928 const wxDataViewTreeNode* node = GetTreeNodeByRow(row);
2929 if (!node)
2930 {
2931 // not really correct...
2932 return row + ((y-yy) / m_lineHeight);
2933 }
777f9cef 2934
ce468dc2 2935 wxDataViewItem item = node->GetItem();
777f9cef 2936
ce468dc2
FM
2937 unsigned int cols = GetOwner()->GetColumnCount();
2938 unsigned int col;
2939 int height = m_lineHeight;
2940 for (col = 0; col < cols; col++)
2941 {
777f9cef
VZ
2942 const wxDataViewColumn *column = GetOwner()->GetColumn(col);
2943 if (column->IsHidden())
2944 continue; // skip it!
2945
4cef3873
VZ
2946 if ((col != 0) &&
2947 model->IsContainer(item) &&
ce468dc2 2948 !model->HasContainerColumns(item))
777f9cef
VZ
2949 continue; // skip it!
2950
4cef3873 2951 wxDataViewRenderer *renderer =
ce468dc2 2952 const_cast<wxDataViewRenderer*>(column->GetRenderer());
f0ccd2cb 2953 renderer->PrepareForItem(model, item, column->GetModelColumn());
86755098 2954
777f9cef 2955 height = wxMax( height, renderer->GetSize().y );
ce468dc2 2956 }
777f9cef 2957
ce468dc2
FM
2958 yy += height;
2959 if (y < yy)
2960 return row;
777f9cef 2961
ce468dc2 2962 row++;
344ed1f3
RR
2963 }
2964}
2965
2966int wxDataViewMainWindow::GetLineHeight( unsigned int row ) const
2967{
c2c89730 2968 const wxDataViewModel *model = GetModel();
777f9cef 2969
344ed1f3
RR
2970 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT)
2971 {
2972 wxASSERT( !IsVirtualList() );
777f9cef 2973
344ed1f3
RR
2974 const wxDataViewTreeNode* node = GetTreeNodeByRow(row);
2975 // wxASSERT( node );
2976 if (!node) return m_lineHeight;
2977
ba5f54e6 2978 wxDataViewItem item = node->GetItem();
777f9cef 2979
981cd83e 2980 int height = m_lineHeight;
777f9cef 2981
344ed1f3
RR
2982 unsigned int cols = GetOwner()->GetColumnCount();
2983 unsigned int col;
2984 for (col = 0; col < cols; col++)
2985 {
2986 const wxDataViewColumn *column = GetOwner()->GetColumn(col);
2987 if (column->IsHidden())
2988 continue; // skip it!
ba5f54e6 2989
4cef3873
VZ
2990 if ((col != 0) &&
2991 model->IsContainer(item) &&
ce468dc2 2992 !model->HasContainerColumns(item))
ba5f54e6 2993 continue; // skip it!
777f9cef 2994
4cef3873 2995 wxDataViewRenderer *renderer =
ce468dc2 2996 const_cast<wxDataViewRenderer*>(column->GetRenderer());
f0ccd2cb 2997 renderer->PrepareForItem(model, item, column->GetModelColumn());
86755098 2998
344ed1f3
RR
2999 height = wxMax( height, renderer->GetSize().y );
3000 }
3001
3002 return height;
3003 }
3004 else
3005 {
3006 return m_lineHeight;
3007 }
3008}
3009
aba9bfd0 3010
3b6280be
RR
3011class RowToTreeNodeJob: public DoJob
3012{
3013public:
442c56e6
VZ
3014 RowToTreeNodeJob( unsigned int row , int current, wxDataViewTreeNode * node )
3015 {
3016 this->row = row;
59e60167
VZ
3017 this->current = current;
3018 ret = NULL;
d47db7e0
RR
3019 parent = node;
3020 }
3b6280be
RR
3021
3022 virtual int operator() ( wxDataViewTreeNode * node )
d5025dc0 3023 {
d47db7e0 3024 current ++;
704c3490 3025 if( current == static_cast<int>(row))
977a41ec 3026 {
59e60167 3027 ret = node;
d54f0605 3028 return DoJob::DONE;
3b6280be 3029 }
d47db7e0
RR
3030
3031 if( node->GetSubTreeCount() + current < static_cast<int>(row) )
3032 {
3033 current += node->GetSubTreeCount();
d54f0605 3034 return DoJob::SKIP_SUBTREE;
d47db7e0 3035 }
d5025dc0 3036 else
d47db7e0
RR
3037 {
3038 parent = node;
bc4f1ff2 3039
422aa8ec
VS
3040 // If the current node has only leaf children, we can find the
3041 // desired node directly. This can speed up finding the node
3042 // in some cases, and will have a very good effect for list views.
d0cfefc4 3043 if ( node->HasChildren() &&
ff3c5ad3 3044 (int)node->GetChildNodes().size() == node->GetSubTreeCount() )
b7e9f8b1 3045 {
422aa8ec 3046 const int index = static_cast<int>(row) - current - 1;
ff3c5ad3 3047 ret = node->GetChildNodes()[index];
d54f0605 3048 return DoJob::DONE;
b7e9f8b1 3049 }
d0cfefc4 3050
d54f0605 3051 return DoJob::CONTINUE;
d47db7e0 3052 }
d5025dc0 3053 }
3b6280be 3054
bc4f1ff2
FM
3055 wxDataViewTreeNode * GetResult() const
3056 { return ret; }
3057
3b6280be
RR
3058private:
3059 unsigned int row;
59e60167 3060 int current;
3b6280be 3061 wxDataViewTreeNode * ret;
59e60167 3062 wxDataViewTreeNode * parent;
3b6280be
RR
3063};
3064
344ed1f3 3065wxDataViewTreeNode * wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row) const
3b6280be 3066{
344ed1f3 3067 wxASSERT( !IsVirtualList() );
777f9cef 3068
ed3aece5
VZ
3069 if ( row == (unsigned)-1 )
3070 return NULL;
3071
344ed1f3
RR
3072 RowToTreeNodeJob job( row , -2, m_root );
3073 Walker( m_root , job );
3074 return job.GetResult();
3b6280be
RR
3075}
3076
422aa8ec
VS
3077wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row) const
3078{
b0272c60 3079 wxDataViewItem item;
422aa8ec
VS
3080 if (IsVirtualList())
3081 {
b0272c60
VZ
3082 if ( row < GetRowCount() )
3083 item = wxDataViewItem(wxUIntToPtr(row+1));
422aa8ec
VS
3084 }
3085 else
3086 {
3087 wxDataViewTreeNode *node = GetTreeNodeByRow(row);
b0272c60
VZ
3088 if ( node )
3089 item = node->GetItem();
422aa8ec 3090 }
b0272c60
VZ
3091
3092 return item;
422aa8ec
VS
3093}
3094
fd48fe89
VZ
3095bool
3096wxDataViewMainWindow::SendExpanderEvent(wxEventType type,
3097 const wxDataViewItem& item)
66e09788
RR
3098{
3099 wxWindow *parent = GetParent();
3100 wxDataViewEvent le(type, parent->GetId());
3101
3102 le.SetEventObject(parent);
c2c89730 3103 le.SetModel(GetModel());
66e09788
RR
3104 le.SetItem( item );
3105
fd48fe89 3106 return !parent->ProcessWindowEvent(le) || le.IsAllowed();
66e09788
RR
3107}
3108
739a8399
RR
3109bool wxDataViewMainWindow::IsExpanded( unsigned int row ) const
3110{
ce5abbf9 3111 if (IsList())
bc4f1ff2 3112 return false;
9adeb77a 3113
739a8399
RR
3114 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
3115 if (!node)
bc4f1ff2 3116 return false;
9adeb77a 3117
739a8399 3118 if (!node->HasChildren())
bc4f1ff2 3119 return false;
9adeb77a 3120
739a8399
RR
3121 return node->IsOpen();
3122}
3123
235d5f88
RR
3124bool wxDataViewMainWindow::HasChildren( unsigned int row ) const
3125{
ce5abbf9 3126 if (IsList())
235d5f88
RR
3127 return false;
3128
3129 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
3130 if (!node)
3131 return false;
739a8399 3132
235d5f88 3133 if (!node->HasChildren())
235d5f88 3134 return false;
235d5f88
RR
3135
3136 return true;
3137}
3138
3139void wxDataViewMainWindow::Expand( unsigned int row )
3b6280be 3140{
ce5abbf9 3141 if (IsList())
bc4f1ff2 3142 return;
9657c197 3143
3b6280be 3144 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
235d5f88
RR
3145 if (!node)
3146 return;
4cef3873 3147
235d5f88 3148 if (!node->HasChildren())
235d5f88 3149 return;
4cef3873 3150
5d2ebe49
VZ
3151 if (!node->IsOpen())
3152 {
ce7fe42e 3153 if ( !SendExpanderEvent(wxEVT_DATAVIEW_ITEM_EXPANDING, node->GetItem()) )
5d2ebe49
VZ
3154 {
3155 // Vetoed by the event handler.
3156 return;
3157 }
b7fe2261 3158
5d2ebe49 3159 node->ToggleOpen();
977a41ec 3160
5d2ebe49
VZ
3161 // build the children of current node
3162 if( node->GetChildNodes().empty() )
3163 {
3164 SortPrepare();
3165 ::BuildTreeHelper(GetModel(), node->GetItem(), node);
3166 }
977a41ec 3167
5d2ebe49
VZ
3168 // By expanding the node all row indices that are currently in the selection list
3169 // and are greater than our node have become invalid. So we have to correct that now.
3170 const unsigned rowAdjustment = node->GetSubTreeCount();
3171 for(unsigned i=0; i<m_selection.size(); ++i)
3172 {
3173 const unsigned testRow = m_selection[i];
3174 // all rows above us are not affected, so skip them
3175 if(testRow <= row)
3176 continue;
bc4f1ff2 3177
5d2ebe49
VZ
3178 m_selection[i] += rowAdjustment;
3179 }
977a41ec 3180
5d2ebe49
VZ
3181 if(m_currentRow > row)
3182 ChangeCurrentRow(m_currentRow + rowAdjustment);
977a41ec 3183
5d2ebe49
VZ
3184 m_count = -1;
3185 UpdateDisplay();
3186 // Send the expanded event
ce7fe42e 3187 SendExpanderEvent(wxEVT_DATAVIEW_ITEM_EXPANDED,node->GetItem());
5d2ebe49 3188 }
3b6280be
RR
3189}
3190
235d5f88 3191void wxDataViewMainWindow::Collapse(unsigned int row)
3b6280be 3192{
ce5abbf9 3193 if (IsList())
bc4f1ff2 3194 return;
e822d1bd 3195
7d835958
RR
3196 wxDataViewTreeNode *node = GetTreeNodeByRow(row);
3197 if (!node)
3198 return;
4cef3873 3199
235d5f88 3200 if (!node->HasChildren())
7d835958 3201 return;
d47db7e0 3202
235d5f88 3203 if (node->IsOpen())
3b6280be 3204 {
ce7fe42e 3205 if ( !SendExpanderEvent(wxEVT_DATAVIEW_ITEM_COLLAPSING,node->GetItem()) )
fd48fe89
VZ
3206 {
3207 // Vetoed by the event handler.
66e09788 3208 return;
fd48fe89 3209 }
abdb8c18 3210
977a41ec
FM
3211 // Find out if there are selected items below the current node.
3212 bool selectCollapsingRow = false;
3213 const unsigned rowAdjustment = node->GetSubTreeCount();
3214 unsigned maxRowToBeTested = row + rowAdjustment;
3215 for(unsigned i=0; i<m_selection.size(); ++i)
3216 {
3217 const unsigned testRow = m_selection[i];
3218 if(testRow > row && testRow <= maxRowToBeTested)
3219 {
3220 selectCollapsingRow = true;
3221 // get out as soon as we have found a node that is selected
3222 break;
3223 }
3224 }
3225
3226 node->ToggleOpen();
3227
3228 // If the node to be closed has selected items the user won't see those any longer.
3229 // We select the collapsing node in this case.
3230 if(selectCollapsingRow)
3231 {
3232 SelectAllRows(false);
3233 ChangeCurrentRow(row);
3234 SelectRow(row, true);
3235 SendSelectionChangedEvent(GetItemByRow(row));
3236 }
3237 else
3238 {
3239 // if there were no selected items below our node we still need to "fix" the
3240 // selection list to adjust for the changing of the row indices.
235d5f88 3241 // We actually do the opposite of what we are doing in Expand().
977a41ec
FM
3242 for(unsigned i=0; i<m_selection.size(); ++i)
3243 {
3244 const unsigned testRow = m_selection[i];
3245 // all rows above us are not affected, so skip them
3246 if(testRow <= row)
3247 continue;
3248
3249 m_selection[i] -= rowAdjustment;
3250 }
3251
3252 // if the "current row" is being collapsed away we change it to the current row ;-)
3253 if(m_currentRow > row && m_currentRow <= maxRowToBeTested)
3254 ChangeCurrentRow(row);
3255 else if(m_currentRow > row)
3256 ChangeCurrentRow(m_currentRow - rowAdjustment);
3257 }
abdb8c18 3258
3b6280be 3259 m_count = -1;
351461fc 3260 UpdateDisplay();
ce7fe42e 3261 SendExpanderEvent(wxEVT_DATAVIEW_ITEM_COLLAPSED,node->GetItem());
66e09788 3262 }
3b6280be
RR
3263}
3264
351461fc
RR
3265wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item )
3266{
c2c89730 3267 const wxDataViewModel * model = GetModel();
351461fc
RR
3268 if( model == NULL )
3269 return NULL;
9adeb77a 3270
ce2fe798
RR
3271 if (!item.IsOk())
3272 return m_root;
351461fc 3273
10875c13
VZ
3274 // Compose the parent-chain for the item we are looking for
3275 wxVector<wxDataViewItem> parentChain;
351461fc
RR
3276 wxDataViewItem it( item );
3277 while( it.IsOk() )
3278 {
10875c13
VZ
3279 parentChain.push_back(it);
3280 it = model->GetParent(it);
351461fc
RR
3281 }
3282
bc4f1ff2
FM
3283 // Find the item along the parent-chain.
3284 // This algorithm is designed to speed up the node-finding method
10875c13 3285 wxDataViewTreeNode* node = m_root;
99ef4372 3286 for( unsigned iter = parentChain.size()-1; ; --iter )
351461fc
RR
3287 {
3288 if( node->HasChildren() )
3289 {
ff3c5ad3 3290 if( node->GetChildNodes().empty() )
24c4a50f 3291 {
422aa8ec
VS
3292 // Even though the item is a container, it doesn't have any
3293 // child nodes in the control's representation yet. We have
3294 // to realize its subtree now.
24c4a50f 3295 SortPrepare();
74123073 3296 ::BuildTreeHelper(model, node->GetItem(), node);
24c4a50f 3297 }
351461fc 3298
ff3c5ad3 3299 const wxDataViewTreeNodes& nodes = node->GetChildNodes();
d2c1ee8a 3300 bool found = false;
777f9cef 3301
10875c13 3302 for (unsigned i = 0; i < nodes.GetCount(); ++i)
d92cb015 3303 {
10875c13
VZ
3304 wxDataViewTreeNode* currentNode = nodes[i];
3305 if (currentNode->GetItem() == parentChain[iter])
b7fe2261 3306 {
10875c13
VZ
3307 if (currentNode->GetItem() == item)
3308 return currentNode;
777f9cef 3309
10875c13 3310 node = currentNode;
d2c1ee8a 3311 found = true;
d92cb015
RR
3312 break;
3313 }
3314 }
d2c1ee8a 3315 if (!found)
351461fc 3316 return NULL;
351461fc
RR
3317 }
3318 else
3319 return NULL;
99ef4372
VZ
3320
3321 if ( !iter )
3322 break;
351461fc 3323 }
d2c1ee8a 3324 return NULL;
351461fc
RR
3325}
3326
4cef3873 3327void wxDataViewMainWindow::HitTest( const wxPoint & point, wxDataViewItem & item,
bc4f1ff2 3328 wxDataViewColumn* &column )
66e09788 3329{
fbda518c 3330 wxDataViewColumn *col = NULL;
66e09788
RR
3331 unsigned int cols = GetOwner()->GetColumnCount();
3332 unsigned int colnum = 0;
66e09788
RR
3333 int x, y;
3334 m_owner->CalcUnscrolledPosition( point.x, point.y, &x, &y );
e822d1bd 3335 for (unsigned x_start = 0; colnum < cols; colnum++)
66e09788 3336 {
702f5349 3337 col = GetOwner()->GetColumnAt(colnum);
66e09788
RR
3338 if (col->IsHidden())
3339 continue; // skip it!
3340
3341 unsigned int w = col->GetWidth();
3342 if (x_start+w >= (unsigned int)x)
3343 break;
3344
3345 x_start += w;
3346 }
3347
fbda518c 3348 column = col;
344ed1f3 3349 item = GetItemByRow( GetLineAt( y ) );
66e09788
RR
3350}
3351
4cef3873 3352wxRect wxDataViewMainWindow::GetItemRect( const wxDataViewItem & item,
bc4f1ff2 3353 const wxDataViewColumn* column )
66e09788 3354{
c937344c
RR
3355 int xpos = 0;
3356 int width = 0;
4cef3873 3357
c937344c
RR
3358 unsigned int cols = GetOwner()->GetColumnCount();
3359 // If column is null the loop will compute the combined width of all columns.
3360 // Otherwise, it will compute the x position of the column we are looking for.
3361 for (unsigned int i = 0; i < cols; i++)
3362 {
3363 wxDataViewColumn* col = GetOwner()->GetColumnAt( i );
3364
3365 if (col == column)
3366 break;
3367
3368 if (col->IsHidden())
3369 continue; // skip it!
3370
3371 xpos += col->GetWidth();
3372 width += col->GetWidth();
3373 }
3374
3375 if(column != 0)
3376 {
3377 // If we have a column, we need can get its width directly.
3378 if(column->IsHidden())
3379 width = 0;
3380 else
3381 width = column->GetWidth();
3382
3383 }
3384 else
3385 {
3386 // If we have no column, we reset the x position back to zero.
3387 xpos = 0;
3388 }
3389
3390 // we have to take an expander column into account and compute its indentation
3391 // to get the correct x position where the actual text is
3392 int indent = 0;
66e09788 3393 int row = GetRowByItem(item);
1841f079
VZ
3394 if (!IsList() &&
3395 (column == 0 || GetExpanderColumnOrFirstOne(GetOwner()) == column) )
66e09788 3396 {
c937344c
RR
3397 wxDataViewTreeNode* node = GetTreeNodeByRow(row);
3398 indent = GetOwner()->GetIndent() * node->GetIndentLevel();
03647350 3399 indent = indent + m_lineHeight; // use m_lineHeight as the width of the expander
66e09788 3400 }
c937344c
RR
3401
3402 wxRect itemRect( xpos + indent,
3403 GetLineStart( row ),
3404 width - indent,
3405 GetLineHeight( row ) );
3406
3407 GetOwner()->CalcScrolledPosition( itemRect.x, itemRect.y,
3408 &itemRect.x, &itemRect.y );
3409
3410 return itemRect;
66e09788
RR
3411}
3412
f2bf2d71 3413int wxDataViewMainWindow::RecalculateCount() const
3b6280be 3414{
86ba79ed 3415 if (IsVirtualList())
a3cc79d9 3416 {
9330d5af 3417 wxDataViewVirtualListModel *list_model =
c2c89730 3418 (wxDataViewVirtualListModel*) GetModel();
03647350 3419
9330d5af 3420 return list_model->GetCount();
a3cc79d9
RR
3421 }
3422 else
3423 {
3424 return m_root->GetSubTreeCount();
3425 }
3b6280be
RR
3426}
3427
aba9bfd0
RR
3428class ItemToRowJob : public DoJob
3429{
3430public:
10875c13 3431 ItemToRowJob(const wxDataViewItem& item_, wxVector<wxDataViewItem>::reverse_iterator iter)
59e60167 3432 : m_iter(iter),
977a41ec 3433 item(item_)
59e60167
VZ
3434 {
3435 ret = -1;
3436 }
aba9bfd0 3437
bc4f1ff2 3438 // Maybe binary search will help to speed up this process
d5025dc0
RR
3439 virtual int operator() ( wxDataViewTreeNode * node)
3440 {
977a41ec
FM
3441 ret ++;
3442 if( node->GetItem() == item )
3443 {
d54f0605 3444 return DoJob::DONE;
977a41ec 3445 }
d5025dc0 3446
10875c13 3447 if( node->GetItem() == *m_iter )
977a41ec
FM
3448 {
3449 m_iter++;
d54f0605 3450 return DoJob::CONTINUE;
977a41ec
FM
3451 }
3452 else
3453 {
3454 ret += node->GetSubTreeCount();
d54f0605 3455 return DoJob::SKIP_SUBTREE;
977a41ec 3456 }
442c56e6 3457
d5025dc0 3458 }
aba9bfd0 3459
bc4f1ff2
FM
3460 // the row number is begin from zero
3461 int GetResult() const
3462 { return ret -1; }
59e60167 3463
aba9bfd0 3464private:
10875c13 3465 wxVector<wxDataViewItem>::reverse_iterator m_iter;
aba9bfd0
RR
3466 wxDataViewItem item;
3467 int ret;
442c56e6 3468
aba9bfd0
RR
3469};
3470
344ed1f3 3471int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item) const
aba9bfd0 3472{
c2c89730 3473 const wxDataViewModel * model = GetModel();
d47db7e0 3474 if( model == NULL )
fbda518c 3475 return -1;
d47db7e0 3476
86ba79ed 3477 if (IsVirtualList())
d47db7e0 3478 {
86ba79ed 3479 return wxPtrToUInt( item.GetID() ) -1;
d47db7e0 3480 }
a3cc79d9
RR
3481 else
3482 {
3483 if( !item.IsOk() )
3484 return -1;
3485
10875c13
VZ
3486 // Compose the parent-chain of the item we are looking for
3487 wxVector<wxDataViewItem> parentChain;
a3cc79d9
RR
3488 wxDataViewItem it( item );
3489 while( it.IsOk() )
3490 {
10875c13
VZ
3491 parentChain.push_back(it);
3492 it = model->GetParent(it);
a3cc79d9 3493 }
d47db7e0 3494
10875c13
VZ
3495 // add an 'invalid' item to represent our 'invisible' root node
3496 parentChain.push_back(wxDataViewItem());
3497
3498 // the parent chain was created by adding the deepest parent first.
3499 // so if we want to start at the root node, we have to iterate backwards through the vector
3500 ItemToRowJob job( item, parentChain.rbegin() );
3501 Walker( m_root, job );
a3cc79d9
RR
3502 return job.GetResult();
3503 }
aba9bfd0
RR
3504}
3505
10875c13 3506static void BuildTreeHelper( const wxDataViewModel * model, const wxDataViewItem & item,
bc4f1ff2 3507 wxDataViewTreeNode * node)
aba9bfd0 3508{
351461fc 3509 if( !model->IsContainer( item ) )
59e60167 3510 return;
442c56e6 3511
c899416d
RR
3512 wxDataViewItemArray children;
3513 unsigned int num = model->GetChildren( item, children);
9adeb77a 3514
422aa8ec 3515 for ( unsigned int index = 0; index < num; index++ )
aba9bfd0 3516 {
422aa8ec
VS
3517 wxDataViewTreeNode *n = new wxDataViewTreeNode(node, children[index]);
3518
3519 if( model->IsContainer(children[index]) )
59e60167 3520 n->SetHasChildren( true );
422aa8ec 3521
35219832 3522 node->InsertChild(n, index);
aba9bfd0 3523 }
d47db7e0 3524
422aa8ec
VS
3525 wxASSERT( node->IsOpen() );
3526 node->ChangeSubTreeCount(+num);
aba9bfd0
RR
3527}
3528
3529void wxDataViewMainWindow::BuildTree(wxDataViewModel * model)
3530{
51bdecff
RR
3531 DestroyTree();
3532
c2c89730 3533 if (GetModel()->IsVirtualListModel())
a3cc79d9 3534 {
59e60167 3535 m_count = -1;
a3cc79d9
RR
3536 return;
3537 }
3538
422aa8ec 3539 m_root = wxDataViewTreeNode::CreateRootNode();
51bdecff 3540
bc4f1ff2 3541 // First we define a invalid item to fetch the top-level elements
aba9bfd0 3542 wxDataViewItem item;
66e09788 3543 SortPrepare();
3b6280be 3544 BuildTreeHelper( model, item, m_root);
59e60167 3545 m_count = -1;
aba9bfd0
RR
3546}
3547
aba9bfd0
RR
3548void wxDataViewMainWindow::DestroyTree()
3549{
344ed1f3 3550 if (!IsVirtualList())
51bdecff 3551 {
a2d3c415
VS
3552 wxDELETE(m_root);
3553 m_count = 0;
51bdecff 3554 }
aba9bfd0
RR
3555}
3556
1c8e71c9
VS
3557wxDataViewColumn*
3558wxDataViewMainWindow::FindColumnForEditing(const wxDataViewItem& item, wxDataViewCellMode mode)
3559{
3560 // Edit the current column editable in 'mode'. If no column is focused
3561 // (typically because the user has full row selected), try to find the
3562 // first editable column (this would typically be a checkbox for
3563 // wxDATAVIEW_CELL_ACTIVATABLE and we don't want to force the user to set
3564 // focus on the checkbox column; or on the only editable text column).
3565
3566 wxDataViewColumn *candidate = m_currentCol;
3567
3568 if ( candidate &&
cf5d4c76 3569 !IsCellEditableInMode(item, candidate, mode) &&
1c8e71c9
VS
3570 !m_currentColSetByKeyboard )
3571 {
3572 // If current column was set by mouse to something not editable (in
3573 // 'mode') and the user pressed Space/F2 to edit it, treat the
3574 // situation as if there was whole-row focus, because that's what is
3575 // visually indicated and the mouse click could very well be targeted
3576 // on the row rather than on an individual cell.
3577 //
3578 // But if it was done by keyboard, respect that even if the column
3579 // isn't editable, because focus is visually on that column and editing
3580 // something else would be surprising.
3581 candidate = NULL;
3582 }
3583
3584 if ( !candidate )
3585 {
3586 const unsigned cols = GetOwner()->GetColumnCount();
3587 for ( unsigned i = 0; i < cols; i++ )
3588 {
3589 wxDataViewColumn *c = GetOwner()->GetColumnAt(i);
3590 if ( c->IsHidden() )
3591 continue;
3592
cf5d4c76 3593 if ( IsCellEditableInMode(item, c, mode) )
1c8e71c9
VS
3594 {
3595 candidate = c;
3596 break;
3597 }
3598 }
3599 }
3600
3601 // If on container item without columns, only the expander column
3602 // may be directly editable:
3603 if ( candidate &&
3604 GetOwner()->GetExpanderColumn() != candidate &&
3605 GetModel()->IsContainer(item) &&
3606 !GetModel()->HasContainerColumns(item) )
3607 {
3608 candidate = GetOwner()->GetExpanderColumn();
3609 }
3610
3611 if ( !candidate )
3612 return NULL;
3613
cf5d4c76 3614 if ( !IsCellEditableInMode(item, candidate, mode) )
1c8e71c9
VS
3615 return NULL;
3616
3617 return candidate;
3618}
3619
cf5d4c76
VS
3620bool wxDataViewMainWindow::IsCellEditableInMode(const wxDataViewItem& item,
3621 const wxDataViewColumn *col,
3622 wxDataViewCellMode mode) const
3623{
3624 if ( col->GetRenderer()->GetMode() != mode )
3625 return false;
3626
3627 if ( !GetModel()->IsEnabled(item, col->GetModelColumn()) )
3628 return false;
3629
3630 return true;
3631}
3632
64ac3db8
VZ
3633void wxDataViewMainWindow::OnCharHook(wxKeyEvent& event)
3634{
3635 if ( m_editorCtrl )
3636 {
3637 // Handle any keys special for the in-place editor and return without
3638 // calling Skip() below.
3639 switch ( event.GetKeyCode() )
3640 {
3641 case WXK_ESCAPE:
3642 m_editorRenderer->CancelEditing();
3643 return;
3644
3645 case WXK_RETURN:
3646 m_editorRenderer->FinishEditing();
3647 return;
3648 }
3649 }
3650
3651 event.Skip();
3652}
3653
cab07038
RR
3654void wxDataViewMainWindow::OnChar( wxKeyEvent &event )
3655{
63ced01b
VZ
3656 wxWindow * const parent = GetParent();
3657
3658 // propagate the char event upwards
3659 wxKeyEvent eventForParent(event);
3660 eventForParent.SetEventObject(parent);
3661 if ( parent->ProcessWindowEvent(eventForParent) )
3662 return;
3663
3664 if ( parent->HandleAsNavigationKey(event) )
f029f1d1 3665 return;
cab07038
RR
3666
3667 // no item -> nothing to do
3668 if (!HasCurrentRow())
3669 {
3670 event.Skip();
3671 return;
3672 }
120b9b05 3673
cab07038
RR
3674 // don't use m_linesPerPage directly as it might not be computed yet
3675 const int pageSize = GetCountPerPage();
9a83f860 3676 wxCHECK_RET( pageSize, wxT("should have non zero page size") );
cab07038
RR
3677
3678 switch ( event.GetKeyCode() )
3679 {
d37b709c 3680 case WXK_RETURN:
a739b8b2
VS
3681 if ( event.HasModifiers() )
3682 {
3683 event.Skip();
3684 break;
3685 }
3686 else
d37b709c 3687 {
ce7fe42e 3688 // Enter activates the item, i.e. sends wxEVT_DATAVIEW_ITEM_ACTIVATED to
c2efa4b4 3689 // it. Only if that event is not handled do we activate column renderer (which
0fff3dfc 3690 // is normally done by Space) or even inline editing.
c2efa4b4 3691
276227fc 3692 const wxDataViewItem item = GetItemByRow(m_currentRow);
d37b709c 3693
ce7fe42e 3694 wxDataViewEvent le(wxEVT_DATAVIEW_ITEM_ACTIVATED,
c2efa4b4
VS
3695 parent->GetId());
3696 le.SetItem(item);
3697 le.SetEventObject(parent);
3698 le.SetModel(GetModel());
3699
857f05e1 3700 if ( parent->ProcessWindowEvent(le) )
c2efa4b4
VS
3701 break;
3702 // else: fall through to WXK_SPACE handling
3703 }
3704
3705 case WXK_SPACE:
a739b8b2
VS
3706 if ( event.HasModifiers() )
3707 {
3708 event.Skip();
3709 break;
3710 }
3711 else
c2efa4b4 3712 {
0fff3dfc
VS
3713 // Space toggles activatable items or -- if not activatable --
3714 // starts inline editing (this is normally done using F2 on
3715 // Windows, but Space is common everywhere else, so use it too
3716 // for greater cross-platform compatibility).
3717
1c8e71c9 3718 const wxDataViewItem item = GetItemByRow(m_currentRow);
276227fc 3719
1c8e71c9
VS
3720 // Activate the current activatable column. If not column is focused (typically
3721 // because the user has full row selected), try to find the first activatable
3722 // column (this would typically be a checkbox and we don't want to force the user
3723 // to set focus on the checkbox column).
3724 wxDataViewColumn *activatableCol = FindColumnForEditing(item, wxDATAVIEW_CELL_ACTIVATABLE);
276227fc
VS
3725
3726 if ( activatableCol )
3727 {
3728 const unsigned colIdx = activatableCol->GetModelColumn();
3729 const wxRect cell_rect = GetOwner()->GetItemRect(item, activatableCol);
3730
3731 wxDataViewRenderer *cell = activatableCol->GetRenderer();
3732 cell->PrepareForItem(GetModel(), item, colIdx);
dc73d7f5 3733 cell->WXActivateCell(cell_rect, GetModel(), item, colIdx, NULL);
0fff3dfc
VS
3734
3735 break;
3736 }
3737 // else: fall through to WXK_F2 handling
3738 }
3739
3740 case WXK_F2:
a739b8b2
VS
3741 if ( event.HasModifiers() )
3742 {
3743 event.Skip();
3744 break;
3745 }
3746 else
0fff3dfc
VS
3747 {
3748 if( !m_selection.empty() )
3749 {
3750 // Mimic Windows 7 behavior: edit the item that has focus
3751 // if it is selected and the first selected item if focus
3752 // is out of selection.
3753 int sel;
3754 if ( m_selection.Index(m_currentRow) != wxNOT_FOUND )
3755 sel = m_currentRow;
3756 else
3757 sel = m_selection[0];
3758
3759
3760 const wxDataViewItem item = GetItemByRow(sel);
3761
3762 // Edit the current column. If no column is focused
3763 // (typically because the user has full row selected), try
3764 // to find the first editable column.
3765 wxDataViewColumn *editableCol = FindColumnForEditing(item, wxDATAVIEW_CELL_EDITABLE);
3766
3767 if ( editableCol )
907f09f4 3768 GetOwner()->EditItem(item, editableCol);
276227fc 3769 }
d37b709c
RR
3770 }
3771 break;
48ae48a9 3772
cab07038 3773 case WXK_UP:
ed3aece5 3774 OnVerticalNavigation( -1, event );
cab07038
RR
3775 break;
3776
3777 case WXK_DOWN:
ed3aece5 3778 OnVerticalNavigation( +1, event );
cab07038 3779 break;
bc4f1ff2 3780 // Add the process for tree expanding/collapsing
3b6280be 3781 case WXK_LEFT:
cfc21881 3782 OnLeftKey();
235d5f88 3783 break;
cfc21881 3784
235d5f88 3785 case WXK_RIGHT:
cfc21881 3786 OnRightKey();
235d5f88 3787 break;
cfc21881 3788
cab07038 3789 case WXK_END:
ed3aece5 3790 OnVerticalNavigation( +(int)GetRowCount(), event );
cab07038 3791 break;
ed3aece5 3792
cab07038 3793 case WXK_HOME:
ed3aece5 3794 OnVerticalNavigation( -(int)GetRowCount(), event );
cab07038
RR
3795 break;
3796
3797 case WXK_PAGEUP:
ed3aece5 3798 OnVerticalNavigation( -(pageSize - 1), event );
cab07038
RR
3799 break;
3800
3801 case WXK_PAGEDOWN:
ed3aece5 3802 OnVerticalNavigation( +(pageSize - 1), event );
cab07038
RR
3803 break;
3804
3805 default:
3806 event.Skip();
3807 }
3808}
3809
ed3aece5 3810void wxDataViewMainWindow::OnVerticalNavigation(int delta, const wxKeyEvent& event)
1dc779fc 3811{
1dc779fc 3812 // if there is no selection, we cannot move it anywhere
ed3aece5 3813 if (!HasCurrentRow() || IsEmpty())
1dc779fc
VS
3814 return;
3815
ed3aece5
VZ
3816 int newRow = (int)m_currentRow + delta;
3817
3818 // let's keep the new row inside the allowed range
3819 if ( newRow < 0 )
3820 newRow = 0;
3821
3822 const int rowCount = (int)GetRowCount();
3823 if ( newRow >= rowCount )
3824 newRow = rowCount - 1;
3825
1dc779fc 3826 unsigned int oldCurrent = m_currentRow;
ed3aece5 3827 unsigned int newCurrent = (unsigned int)newRow;
1dc779fc
VS
3828
3829 // in single selection we just ignore Shift as we can't select several
3830 // items anyhow
3831 if ( event.ShiftDown() && !IsSingleSel() )
3832 {
3833 RefreshRow( oldCurrent );
3834
3835 ChangeCurrentRow( newCurrent );
3836
3837 // select all the items between the old and the new one
3838 if ( oldCurrent > newCurrent )
3839 {
3840 newCurrent = oldCurrent;
3841 oldCurrent = m_currentRow;
3842 }
3843
3844 SelectRows( oldCurrent, newCurrent, true );
3845 if (oldCurrent!=newCurrent)
3846 SendSelectionChangedEvent(GetItemByRow(m_selection[0]));
3847 }
3848 else // !shift
3849 {
3850 RefreshRow( oldCurrent );
3851
3852 // all previously selected items are unselected unless ctrl is held
3853 if ( !event.ControlDown() )
3854 SelectAllRows(false);
3855
3856 ChangeCurrentRow( newCurrent );
3857
3858 if ( !event.ControlDown() )
3859 {
3860 SelectRow( m_currentRow, true );
3861 SendSelectionChangedEvent(GetItemByRow(m_currentRow));
3862 }
3863 else
3864 RefreshRow( m_currentRow );
3865 }
3866
3867 GetOwner()->EnsureVisible( m_currentRow, -1 );
3868}
3869
cfc21881
VS
3870void wxDataViewMainWindow::OnLeftKey()
3871{
1c8e71c9
VS
3872 if ( IsList() )
3873 {
3874 TryAdvanceCurrentColumn(NULL, /*forward=*/false);
3875 }
3876 else
3877 {
3878 wxDataViewTreeNode* node = GetTreeNodeByRow(m_currentRow);
ed3aece5
VZ
3879 if ( !node )
3880 return;
cfc21881 3881
1c8e71c9
VS
3882 if ( TryAdvanceCurrentColumn(node, /*forward=*/false) )
3883 return;
cfc21881 3884
1c8e71c9
VS
3885 // Because TryAdvanceCurrentColumn() return false, we are at the first
3886 // column or using whole-row selection. In this situation, we can use
3887 // the standard TreeView handling of the left key.
3888 if (node->HasChildren() && node->IsOpen())
3889 {
3890 Collapse(m_currentRow);
3891 }
3892 else
3893 {
3894 // if the node is already closed, we move the selection to its parent
3895 wxDataViewTreeNode *parent_node = node->GetParent();
3896
3897 if (parent_node)
3898 {
3899 int parent = GetRowByItem( parent_node->GetItem() );
3900 if ( parent >= 0 )
3901 {
3902 unsigned int row = m_currentRow;
3903 SelectRow( row, false);
3904 SelectRow( parent, true );
3905 ChangeCurrentRow( parent );
3906 GetOwner()->EnsureVisible( parent, -1 );
3907 SendSelectionChangedEvent( parent_node->GetItem() );
3908 }
3909 }
3910 }
3911 }
3912}
3913
3914void wxDataViewMainWindow::OnRightKey()
3915{
3916 if ( IsList() )
cfc21881 3917 {
1c8e71c9 3918 TryAdvanceCurrentColumn(NULL, /*forward=*/true);
cfc21881 3919 }
1c8e71c9 3920 else
cfc21881 3921 {
1c8e71c9 3922 wxDataViewTreeNode* node = GetTreeNodeByRow(m_currentRow);
ed3aece5
VZ
3923 if ( !node )
3924 return;
cfc21881 3925
1c8e71c9 3926 if ( node->HasChildren() )
cfc21881 3927 {
1c8e71c9 3928 if ( !node->IsOpen() )
cfc21881 3929 {
1c8e71c9
VS
3930 Expand( m_currentRow );
3931 }
3932 else
3933 {
3934 // if the node is already open, we move the selection to the first child
cfc21881 3935 unsigned int row = m_currentRow;
1c8e71c9
VS
3936 SelectRow( row, false );
3937 SelectRow( row + 1, true );
3938 ChangeCurrentRow( row + 1 );
3939 GetOwner()->EnsureVisible( row + 1, -1 );
3940 SendSelectionChangedEvent( GetItemByRow(row+1) );
cfc21881
VS
3941 }
3942 }
1c8e71c9
VS
3943 else
3944 {
3945 TryAdvanceCurrentColumn(node, /*forward=*/true);
3946 }
cfc21881
VS
3947 }
3948}
3949
1c8e71c9 3950bool wxDataViewMainWindow::TryAdvanceCurrentColumn(wxDataViewTreeNode *node, bool forward)
cfc21881 3951{
1c8e71c9
VS
3952 if ( GetOwner()->GetColumnCount() == 0 )
3953 return false;
3954
3955 if ( !m_useCellFocus )
3956 return false;
3957
3958 if ( node )
3959 {
3960 // navigation shouldn't work in branch nodes without other columns:
3961 if ( node->HasChildren() && !GetModel()->HasContainerColumns(node->GetItem()) )
3962 return false;
3963 }
3964
3965 if ( m_currentCol == NULL || !m_currentColSetByKeyboard )
3966 {
3967 if ( forward )
3968 {
3969 m_currentCol = GetOwner()->GetColumnAt(1);
3970 m_currentColSetByKeyboard = true;
3971 RefreshRow(m_currentRow);
3972 return true;
3973 }
3974 else
3975 return false;
3976 }
3977
3978 int idx = GetOwner()->GetColumnIndex(m_currentCol) + (forward ? +1 : -1);
3979
3980 if ( idx >= (int)GetOwner()->GetColumnCount() )
3981 return false;
3982
78e18e8d
VS
3983 GetOwner()->EnsureVisible(m_currentRow, idx);
3984
1c8e71c9 3985 if ( idx < 1 )
cfc21881 3986 {
1c8e71c9
VS
3987 // We are going to the left of the second column. Reset to whole-row
3988 // focus (which means first column would be edited).
3989 m_currentCol = NULL;
3990 RefreshRow(m_currentRow);
3991 return true;
cfc21881 3992 }
1c8e71c9
VS
3993
3994 m_currentCol = GetOwner()->GetColumnAt(idx);
3995 m_currentColSetByKeyboard = true;
3996 RefreshRow(m_currentRow);
3997 return true;
cfc21881
VS
3998}
3999
4ed7af08
RR
4000void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
4001{
e21f75bd
RR
4002 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
4003 {
4004 // let the base handle mouse wheel events.
4005 event.Skip();
4006 return;
4007 }
4008
a386b9c6 4009 if(event.ButtonDown())
6ceda4f5 4010 {
a386b9c6
VZ
4011 // Not skipping button down events would prevent the system from
4012 // setting focus to this window as most (all?) of them do by default,
4013 // so skip it to enable default handling.
6ceda4f5
VZ
4014 event.Skip();
4015 }
3d9bff2f 4016
0fdc2321
RR
4017 int x = event.GetX();
4018 int y = event.GetY();
4019 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
0fdc2321 4020 wxDataViewColumn *col = NULL;
b7fe2261 4021
0fdc2321 4022 int xpos = 0;
9861f022 4023 unsigned int cols = GetOwner()->GetColumnCount();
0a71f9e9 4024 unsigned int i;
0fdc2321
RR
4025 for (i = 0; i < cols; i++)
4026 {
702f5349 4027 wxDataViewColumn *c = GetOwner()->GetColumnAt( i );
9861f022
RR
4028 if (c->IsHidden())
4029 continue; // skip it!
4030
0fdc2321
RR
4031 if (x < xpos + c->GetWidth())
4032 {
4033 col = c;
4034 break;
4035 }
4036 xpos += c->GetWidth();
4037 }
7ed24cb6
VZ
4038
4039 wxDataViewModel* const model = GetModel();
4040
4041 const unsigned int current = GetLineAt( y );
4042 const wxDataViewItem item = GetItemByRow(current);
4043
4044 // Handle right clicking here, before everything else as context menu
4045 // events should be sent even when we click outside of any item, unlike all
4046 // the other ones.
4047 if (event.RightUp())
4048 {
4049 wxWindow *parent = GetParent();
ce7fe42e 4050 wxDataViewEvent le(wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, parent->GetId());
7ed24cb6
VZ
4051 le.SetEventObject(parent);
4052 le.SetModel(model);
4053
4054 if ( item.IsOk() && col )
4055 {
4056 le.SetItem( item );
4057 le.SetColumn( col->GetModelColumn() );
4058 le.SetDataViewColumn( col );
7ed24cb6
VZ
4059 }
4060
4061 parent->ProcessWindowEvent(le);
4062 return;
4063 }
4064
9e8125a1
VZ
4065#if wxUSE_DRAG_AND_DROP
4066 if (event.Dragging() || ((m_dragCount > 0) && event.Leaving()))
4067 {
4068 if (m_dragCount == 0)
4069 {
4070 // we have to report the raw, physical coords as we want to be
4071 // able to call HitTest(event.m_pointDrag) from the user code to
4072 // get the item being dragged
4073 m_dragStart = event.GetPosition();
4074 }
4075
4076 m_dragCount++;
4077 if ((m_dragCount < 3) && (event.Leaving()))
4078 m_dragCount = 3;
4079 else if (m_dragCount != 3)
4080 return;
4081
4082 if (event.LeftIsDown())
4083 {
4084 m_owner->CalcUnscrolledPosition( m_dragStart.x, m_dragStart.y,
4085 &m_dragStart.x, &m_dragStart.y );
4086 unsigned int drag_item_row = GetLineAt( m_dragStart.y );
4087 wxDataViewItem itemDragged = GetItemByRow( drag_item_row );
4088
4089 // Notify cell about drag
4090 wxDataViewEvent event( wxEVT_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() );
4091 event.SetEventObject( m_owner );
4092 event.SetItem( itemDragged );
4093 event.SetModel( model );
4094 if (!m_owner->HandleWindowEvent( event ))
4095 return;
4096
4097 if (!event.IsAllowed())
4098 return;
4099
4100 wxDataObject *obj = event.GetDataObject();
4101 if (!obj)
4102 return;
4103
4104 wxDataViewDropSource drag( this, drag_item_row );
4105 drag.SetData( *obj );
4106 /* wxDragResult res = */ drag.DoDragDrop(event.GetDragFlags());
4107 delete obj;
4108 }
4109 return;
4110 }
4111 else
4112 {
4113 m_dragCount = 0;
4114 }
4115#endif // wxUSE_DRAG_AND_DROP
4116
11e3c6ef
VZ
4117 // Check if we clicked outside the item area.
4118 if ((current >= GetRowCount()) || !col)
737883f2 4119 {
11e3c6ef
VZ
4120 // Follow Windows convention here: clicking either left or right (but
4121 // not middle) button clears the existing selection.
4122 if (m_owner && (event.LeftDown() || event.RightDown()))
4123 {
4124 if (!GetSelections().empty())
4125 {
4126 m_owner->UnselectAll();
4127 SendSelectionChangedEvent(wxDataViewItem());
4128 }
4129 }
737883f2 4130 event.Skip();
0fdc2321 4131 return;
737883f2 4132 }
f554a14b 4133
24c4a50f 4134 wxDataViewRenderer *cell = col->GetRenderer();
1841f079
VZ
4135 wxDataViewColumn* const
4136 expander = GetExpanderColumnOrFirstOne(GetOwner());
4137
902334c8
VZ
4138 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
4139 // button) and also determine the offset of the real cell start, skipping
4140 // the indentation and the expander itself.
abdb8c18 4141 bool hoverOverExpander = false;
c46ecbe3 4142 int itemOffset = 0;
1841f079 4143 if ((!IsList()) && (expander == col))
24c4a50f
RR
4144 {
4145 wxDataViewTreeNode * node = GetTreeNodeByRow(current);
abdb8c18 4146
c46ecbe3
VZ
4147 int indent = node->GetIndentLevel();
4148 itemOffset = GetOwner()->GetIndent()*indent;
4149
4150 if ( node->HasChildren() )
4151 {
977a41ec
FM
4152 // we make the rectangle we are looking in a bit bigger than the actual
4153 // visual expander so the user can hit that little thing reliably
c46ecbe3 4154 wxRect rect(itemOffset,
977a41ec
FM
4155 GetLineStart( current ) + (GetLineHeight(current) - m_lineHeight)/2,
4156 m_lineHeight, m_lineHeight);
902334c8 4157
abdb8c18 4158 if( rect.Contains(x, y) )
24c4a50f 4159 {
bc4f1ff2 4160 // So the mouse is over the expander
abdb8c18 4161 hoverOverExpander = true;
24c4a50f
RR
4162 if (m_underMouse && m_underMouse != node)
4163 {
bc4f1ff2 4164 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
df2c23e7 4165 RefreshRow(GetRowByItem(m_underMouse->GetItem()));
24c4a50f
RR
4166 }
4167 if (m_underMouse != node)
4168 {
bc4f1ff2 4169 // wxLogMessage("Do the row: %d", current);
df2c23e7 4170 RefreshRow(current);
24c4a50f
RR
4171 }
4172 m_underMouse = node;
4173 }
4174 }
c46ecbe3
VZ
4175
4176 // Account for the expander as well, even if this item doesn't have it,
4177 // its parent does so it still counts for the offset.
4178 itemOffset += m_lineHeight;
24c4a50f 4179 }
abdb8c18 4180 if (!hoverOverExpander)
24c4a50f
RR
4181 {
4182 if (m_underMouse != NULL)
4183 {
bc4f1ff2 4184 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
df2c23e7 4185 RefreshRow(GetRowByItem(m_underMouse->GetItem()));
24c4a50f
RR
4186 m_underMouse = NULL;
4187 }
4188 }
4189
abdb8c18 4190 bool simulateClick = false;
e21f75bd 4191
0fcce6b9
RR
4192 if (event.ButtonDClick())
4193 {
4194 m_renameTimer->Stop();
4195 m_lastOnSame = false;
4196 }
4197
438fb233 4198 bool ignore_other_columns =
1841f079 4199 ((expander != col) &&
977a41ec
FM
4200 (model->IsContainer(item)) &&
4201 (!model->HasContainerColumns(item)));
b5ec7dd6 4202
0fdc2321
RR
4203 if (event.LeftDClick())
4204 {
977a41ec
FM
4205 if(hoverOverExpander)
4206 {
4207 // a double click on the expander will be converted into a "simulated" normal click
4208 simulateClick = true;
4209 }
4210 else if ( current == m_lineLastClicked )
0fdc2321 4211 {
dc73d7f5 4212 wxWindow *parent = GetParent();
ce7fe42e 4213 wxDataViewEvent le(wxEVT_DATAVIEW_ITEM_ACTIVATED, parent->GetId());
dc73d7f5
VS
4214 le.SetItem( item );
4215 le.SetColumn( col->GetModelColumn() );
4216 le.SetDataViewColumn( col );
4217 le.SetEventObject(parent);
4218 le.SetModel(GetModel());
b7e9f8b1 4219
dc73d7f5 4220 parent->ProcessWindowEvent(le);
e21f75bd
RR
4221 return;
4222 }
4223 else
4224 {
4225 // The first click was on another item, so don't interpret this as
4226 // a double click, but as a simple click instead
abdb8c18 4227 simulateClick = true;
0fdc2321 4228 }
120b9b05 4229 }
f554a14b 4230
abdb8c18 4231 if (event.LeftUp() && !hoverOverExpander)
0fcce6b9 4232 {
0a71f9e9 4233 if (m_lineSelectSingleOnUp != (unsigned int)-1)
e21f75bd
RR
4234 {
4235 // select single line
4236 SelectAllRows( false );
4237 SelectRow( m_lineSelectSingleOnUp, true );
4a745e76 4238 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp) );
e21f75bd 4239 }
120b9b05 4240
4cef3873 4241 // If the user click the expander, we do not do editing even if the column
bc4f1ff2 4242 // with expander are editable
abdb8c18 4243 if (m_lastOnSame && !ignore_other_columns)
0fcce6b9 4244 {
a8461d31 4245 if ((col == m_currentCol) && (current == m_currentRow) &&
cf5d4c76 4246 IsCellEditableInMode(item, col, wxDATAVIEW_CELL_EDITABLE) )
0fcce6b9
RR
4247 {
4248 m_renameTimer->Start( 100, true );
4249 }
4250 }
4251
4252 m_lastOnSame = false;
0a71f9e9 4253 m_lineSelectSingleOnUp = (unsigned int)-1;
120b9b05 4254 }
abdb8c18 4255 else if(!event.LeftUp())
e21f75bd
RR
4256 {
4257 // This is necessary, because after a DnD operation in
4258 // from and to ourself, the up event is swallowed by the
4259 // DnD code. So on next non-up event (which means here and
4260 // now) m_lineSelectSingleOnUp should be reset.
0a71f9e9 4261 m_lineSelectSingleOnUp = (unsigned int)-1;
e21f75bd
RR
4262 }
4263
4264 if (event.RightDown())
4265 {
4266 m_lineBeforeLastClicked = m_lineLastClicked;
4267 m_lineLastClicked = current;
4268
4269 // If the item is already selected, do not update the selection.
4270 // Multi-selections should not be cleared if a selected item is clicked.
4271 if (!IsRowSelected(current))
4272 {
4273 SelectAllRows(false);
a09da78b 4274 const unsigned oldCurrent = m_currentRow;
e21f75bd
RR
4275 ChangeCurrentRow(current);
4276 SelectRow(m_currentRow,true);
a09da78b 4277 RefreshRow(oldCurrent);
526e19e2 4278 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
e21f75bd 4279 }
0a807957 4280 }
e21f75bd
RR
4281 else if (event.MiddleDown())
4282 {
e21f75bd 4283 }
abdb8c18 4284
977a41ec
FM
4285 if((event.LeftDown() || simulateClick) && hoverOverExpander)
4286 {
4287 wxDataViewTreeNode* node = GetTreeNodeByRow(current);
bc4f1ff2 4288
4cef3873 4289 // hoverOverExpander being true tells us that our node must be
bc4f1ff2 4290 // valid and have children.
977a41ec
FM
4291 // So we don't need any extra checks.
4292 if( node->IsOpen() )
235d5f88 4293 Collapse(current);
977a41ec 4294 else
235d5f88 4295 Expand(current);
977a41ec
FM
4296 }
4297 else if ((event.LeftDown() || simulateClick) && !hoverOverExpander)
0fcce6b9 4298 {
e21f75bd
RR
4299 m_lineBeforeLastClicked = m_lineLastClicked;
4300 m_lineLastClicked = current;
4301
0a71f9e9 4302 unsigned int oldCurrentRow = m_currentRow;
e21f75bd
RR
4303 bool oldWasSelected = IsRowSelected(m_currentRow);
4304
4305 bool cmdModifierDown = event.CmdDown();
4306 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
4307 {
4308 if ( IsSingleSel() || !IsRowSelected(current) )
4309 {
4310 SelectAllRows( false );
e21f75bd 4311 ChangeCurrentRow(current);
e21f75bd 4312 SelectRow(m_currentRow,true);
526e19e2 4313 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
e21f75bd
RR
4314 }
4315 else // multi sel & current is highlighted & no mod keys
4316 {
4317 m_lineSelectSingleOnUp = current;
4318 ChangeCurrentRow(current); // change focus
4319 }
4320 }
4321 else // multi sel & either ctrl or shift is down
4322 {
4323 if (cmdModifierDown)
4324 {
4325 ChangeCurrentRow(current);
e21f75bd 4326 ReverseRowSelection(m_currentRow);
9439bc69 4327 SendSelectionChangedEvent(GetItemByRow(m_currentRow));
e21f75bd
RR
4328 }
4329 else if (event.ShiftDown())
4330 {
4331 ChangeCurrentRow(current);
4332
0a71f9e9 4333 unsigned int lineFrom = oldCurrentRow,
977a41ec 4334 lineTo = current;
e21f75bd
RR
4335
4336 if ( lineTo < lineFrom )
4337 {
4338 lineTo = lineFrom;
4339 lineFrom = m_currentRow;
4340 }
4341
4342 SelectRows(lineFrom, lineTo, true);
526e19e2 4343 SendSelectionChangedEvent(GetItemByRow(m_selection[0]) );
e21f75bd
RR
4344 }
4345 else // !ctrl, !shift
4346 {
4347 // test in the enclosing if should make it impossible
9a83f860 4348 wxFAIL_MSG( wxT("how did we get here?") );
e21f75bd
RR
4349 }
4350 }
777f9cef 4351
72664514
RR
4352 if (m_currentRow != oldCurrentRow)
4353 RefreshRow( oldCurrentRow );
e21f75bd 4354
0fcce6b9 4355 wxDataViewColumn *oldCurrentCol = m_currentCol;
120b9b05 4356
0fcce6b9
RR
4357 // Update selection here...
4358 m_currentCol = col;
1c8e71c9 4359 m_currentColSetByKeyboard = false;
120b9b05 4360
15ec266a
VZ
4361 // This flag is used to decide whether we should start editing the item
4362 // label. We do it if the user clicks twice (but not double clicks,
4363 // i.e. simulateClick is false) on the same item but not if the click
4364 // was used for something else already, e.g. selecting the item (so it
4365 // must have been already selected) or giving the focus to the control
4366 // (so it must have had focus already).
abdb8c18 4367 m_lastOnSame = !simulateClick && ((col == oldCurrentCol) &&
15ec266a
VZ
4368 (current == oldCurrentRow)) && oldWasSelected &&
4369 HasFocus();
0bdfa388 4370
dc73d7f5 4371 // Call ActivateCell() after everything else as under GTK+
cf5d4c76 4372 if ( IsCellEditableInMode(item, col, wxDATAVIEW_CELL_ACTIVATABLE) )
0bdfa388 4373 {
dbc3aec1
VS
4374 // notify cell about click
4375 cell->PrepareForItem(model, item, col->GetModelColumn());
86755098 4376
c46ecbe3 4377 wxRect cell_rect( xpos + itemOffset,
902334c8 4378 GetLineStart( current ),
c46ecbe3 4379 col->GetWidth() - itemOffset,
902334c8 4380 GetLineHeight( current ) );
a3a8d81d 4381
dbc3aec1 4382 // Report position relative to the cell's custom area, i.e.
4e231c29 4383 // not the entire space as given by the control but the one
dbc3aec1 4384 // used by the renderer after calculation of alignment etc.
4e231c29
VZ
4385 //
4386 // Notice that this results in negative coordinates when clicking
4387 // in the upper left corner of a centre-aligned cell which doesn't
4388 // fill its column entirely so this is somewhat surprising, but we
4389 // do it like this for compatibility with the native GTK+ version,
4390 // see #12270.
a3a8d81d 4391
dbc3aec1 4392 // adjust the rectangle ourselves to account for the alignment
4e231c29
VZ
4393 int align = cell->GetAlignment();
4394 if ( align == wxDVR_DEFAULT_ALIGNMENT )
4395 align = wxALIGN_CENTRE;
4396
dbc3aec1 4397 wxRect rectItem = cell_rect;
4e231c29
VZ
4398 const wxSize size = cell->GetSize();
4399 if ( size.x >= 0 && size.x < cell_rect.width )
dbc3aec1 4400 {
4e231c29
VZ
4401 if ( align & wxALIGN_CENTER_HORIZONTAL )
4402 rectItem.x += (cell_rect.width - size.x)/2;
4403 else if ( align & wxALIGN_RIGHT )
4404 rectItem.x += cell_rect.width - size.x;
4405 // else: wxALIGN_LEFT is the default
4406 }
a3a8d81d 4407
4e231c29
VZ
4408 if ( size.y >= 0 && size.y < cell_rect.height )
4409 {
4410 if ( align & wxALIGN_CENTER_VERTICAL )
4411 rectItem.y += (cell_rect.height - size.y)/2;
4412 else if ( align & wxALIGN_BOTTOM )
4413 rectItem.y += cell_rect.height - size.y;
4414 // else: wxALIGN_TOP is the default
dbc3aec1 4415 }
a3a8d81d 4416
dc73d7f5
VS
4417 wxMouseEvent event2(event);
4418 event2.m_x -= rectItem.x;
4419 event2.m_y -= rectItem.y;
4420 m_owner->CalcUnscrolledPosition(event2.m_x, event2.m_y, &event2.m_x, &event2.m_y);
a3a8d81d 4421
dc73d7f5
VS
4422 /* ignore ret */ cell->WXActivateCell
4423 (
4424 cell_rect,
4425 model,
4426 item,
4427 col->GetModelColumn(),
4428 &event2
4429 );
0bdfa388 4430 }
0fdc2321 4431 }
4ed7af08
RR
4432}
4433
4434void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event )
4435{
cab07038 4436 m_hasFocus = true;
120b9b05 4437
cab07038
RR
4438 if (HasCurrentRow())
4439 Refresh();
120b9b05 4440
cab07038
RR
4441 event.Skip();
4442}
4443
4444void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event )
4445{
4446 m_hasFocus = false;
120b9b05 4447
cab07038
RR
4448 if (HasCurrentRow())
4449 Refresh();
120b9b05 4450
4ed7af08
RR
4451 event.Skip();
4452}
4453
1c8e71c9
VS
4454void wxDataViewMainWindow::OnColumnsCountChanged()
4455{
4456 int editableCount = 0;
4457
4458 const unsigned cols = GetOwner()->GetColumnCount();
4459 for ( unsigned i = 0; i < cols; i++ )
4460 {
4461 wxDataViewColumn *c = GetOwner()->GetColumnAt(i);
4462 if ( c->IsHidden() )
4463 continue;
4464 if ( c->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT )
4465 editableCount++;
4466 }
4467
4468 m_useCellFocus = (editableCount > 1);
4469
4470 UpdateDisplay();
4471}
4472
4ed7af08
RR
4473//-----------------------------------------------------------------------------
4474// wxDataViewCtrl
4475//-----------------------------------------------------------------------------
bc4f1ff2 4476
a76c2f37 4477WX_DEFINE_LIST(wxDataViewColumnList)
4ed7af08
RR
4478
4479IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
4b3feaa7
RR
4480BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase)
4481 EVT_SIZE(wxDataViewCtrl::OnSize)
4482END_EVENT_TABLE()
4483
4ed7af08
RR
4484wxDataViewCtrl::~wxDataViewCtrl()
4485{
4486 if (m_notifier)
4487 GetModel()->RemoveNotifier( m_notifier );
74123073 4488
b3a3c9d8 4489 m_cols.Clear();
d0154e3a 4490 m_colsBestWidths.clear();
4ed7af08
RR
4491}
4492
4493void wxDataViewCtrl::Init()
4494{
b3a3c9d8 4495 m_cols.DeleteContents(true);
4ed7af08 4496 m_notifier = NULL;
e822d1bd 4497
236a34ef 4498 // No sorting column at start
46234a03
VZ
4499 m_sortingColumnIdx = wxNOT_FOUND;
4500
236a34ef 4501 m_headerArea = NULL;
09f84bc7 4502 m_clientArea = NULL;
bed74e48
VS
4503
4504 m_colsDirty = false;
4ed7af08
RR
4505}
4506
62e9285a
VZ
4507bool wxDataViewCtrl::Create(wxWindow *parent,
4508 wxWindowID id,
4509 const wxPoint& pos,
4510 const wxSize& size,
4511 long style,
4512 const wxValidator& validator,
4513 const wxString& name)
4ed7af08 4514{
3fdf86f9
RR
4515// if ( (style & wxBORDER_MASK) == 0)
4516// style |= wxBORDER_SUNKEN;
777f9cef 4517
236a34ef
RR
4518 Init();
4519
c741d33f 4520 if (!wxControl::Create( parent, id, pos, size,
62e9285a 4521 style | wxScrolledWindowStyle, validator, name))
4b3feaa7
RR
4522 return false;
4523
b89cac3f 4524 SetInitialSize(size);
b5ec7dd6 4525
4ed7af08 4526#ifdef __WXMAC__
59e60167 4527 MacSetClipChildren( true );
4ed7af08
RR
4528#endif
4529
f554a14b 4530 m_clientArea = new wxDataViewMainWindow( this, wxID_ANY );
9861f022 4531
63ced01b
VZ
4532 // We use the cursor keys for moving the selection, not scrolling, so call
4533 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4534 // keyboard events forwarded to us from wxListMainWindow.
4535 DisableKeyboardScrolling();
4536
9861f022
RR
4537 if (HasFlag(wxDV_NO_HEADER))
4538 m_headerArea = NULL;
4539 else
56873923 4540 m_headerArea = new wxDataViewHeaderWindow(this);
f554a14b 4541
4ed7af08 4542 SetTargetWindow( m_clientArea );
f554a14b 4543
4ed7af08 4544 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
9861f022
RR
4545 if (m_headerArea)
4546 sizer->Add( m_headerArea, 0, wxGROW );
4ed7af08
RR
4547 sizer->Add( m_clientArea, 1, wxGROW );
4548 SetSizer( sizer );
b5ec7dd6 4549
4ed7af08
RR
4550 return true;
4551}
4552
3fdf86f9
RR
4553wxBorder wxDataViewCtrl::GetDefaultBorder() const
4554{
4555 return wxBORDER_THEME;
4556}
4557
4ed7af08
RR
4558#ifdef __WXMSW__
4559WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg,
bc4f1ff2
FM
4560 WXWPARAM wParam,
4561 WXLPARAM lParam)
4ed7af08 4562{
b910a8ad 4563 WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
4ed7af08
RR
4564
4565#ifndef __WXWINCE__
4566 // we need to process arrows ourselves for scrolling
4567 if ( nMsg == WM_GETDLGCODE )
4568 {
4569 rc |= DLGC_WANTARROWS;
4570 }
4571#endif
4572
4573 return rc;
4574}
4575#endif
4576
2571a33f
RR
4577wxSize wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize& size)
4578{
4579 wxSize newsize = size;
d7cda9b2 4580 if (!HasFlag(wxDV_NO_HEADER) && (m_headerArea))
977a41ec 4581 newsize.y -= m_headerArea->GetSize().y;
e822d1bd 4582
2571a33f
RR
4583 return newsize;
4584}
4585
f554a14b 4586void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
4ed7af08 4587{
4b3feaa7
RR
4588 // We need to override OnSize so that our scrolled
4589 // window a) does call Layout() to use sizers for
4590 // positioning the controls but b) does not query
4591 // the sizer for their size and use that for setting
4592 // the scrollable area as set that ourselves by
4593 // calling SetScrollbar() further down.
4594
4595 Layout();
4596
4597 AdjustScrollbars();
cd9b34ef
VZ
4598
4599 // We must redraw the headers if their height changed. Normally this
4600 // shouldn't happen as the control shouldn't let itself be resized beneath
4601 // its minimal height but avoid the display artefacts that appear if it
4602 // does happen, e.g. because there is really not enough vertical space.
4603 if ( !HasFlag(wxDV_NO_HEADER) && m_headerArea &&
4604 m_headerArea->GetSize().y <= m_headerArea->GetBestSize(). y )
4605 {
4606 m_headerArea->Refresh();
4607 }
4ed7af08
RR
4608}
4609
788432e3
RR
4610void wxDataViewCtrl::SetFocus()
4611{
4612 if (m_clientArea)
4613 m_clientArea->SetFocus();
4614}
4615
1af67319
VZ
4616bool wxDataViewCtrl::SetFont(const wxFont & font)
4617{
4618 if (!wxControl::SetFont(font))
4619 return false;
4620
4621 if (m_headerArea)
4622 m_headerArea->SetFont(font);
4623
4624 if (m_clientArea)
4625 {
4626 m_clientArea->SetFont(font);
4627 m_clientArea->SetRowHeight(m_clientArea->GetDefaultRowHeight());
4628 }
4629
4630 if (m_headerArea || m_clientArea)
4631 {
4632 InvalidateColBestWidths();
4633 Layout();
4634 }
4635
4636 return true;
4637}
4638
4639
4640
aba9bfd0 4641bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
4ed7af08
RR
4642{
4643 if (!wxDataViewCtrlBase::AssociateModel( model ))
4644 return false;
4645
be416221
VZ
4646 if (model)
4647 {
4648 m_notifier = new wxGenericDataViewModelNotifier( m_clientArea );
4649 model->AddNotifier( m_notifier );
4650 }
4651 else if (m_notifier)
4652 {
4653 m_notifier->Cleared();
4654 m_notifier = NULL;
4655 }
4ed7af08 4656
51bdecff 4657 m_clientArea->DestroyTree();
cfa42cb8 4658
be416221
VZ
4659 if (model)
4660 {
4661 m_clientArea->BuildTree(model);
4662 }
aba9bfd0 4663
4b3feaa7 4664 m_clientArea->UpdateDisplay();
f554a14b 4665
4ed7af08
RR
4666 return true;
4667}
4668
9adeb77a
VZ
4669#if wxUSE_DRAG_AND_DROP
4670
821baf7d
RR
4671bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format )
4672{
4673 return m_clientArea->EnableDragSource( format );
4674}
4675
4676bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format )
4677{
4678 return m_clientArea->EnableDropTarget( format );
4679}
4680
9adeb77a
VZ
4681#endif // wxUSE_DRAG_AND_DROP
4682
4ed7af08
RR
4683bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
4684{
4685 if (!wxDataViewCtrlBase::AppendColumn(col))
4686 return false;
f554a14b 4687
afebb87b 4688 m_cols.Append( col );
840fc4d1 4689 m_colsBestWidths.push_back(CachedColWidthInfo());
aef252d9 4690 OnColumnsCountChanged();
736fe67c
RR
4691 return true;
4692}
4693
4694bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
4695{
4696 if (!wxDataViewCtrlBase::PrependColumn(col))
4697 return false;
4698
4699 m_cols.Insert( col );
840fc4d1 4700 m_colsBestWidths.insert(m_colsBestWidths.begin(), CachedColWidthInfo());
aef252d9 4701 OnColumnsCountChanged();
4ed7af08
RR
4702 return true;
4703}
4704
19723525
RR
4705bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
4706{
4707 if (!wxDataViewCtrlBase::InsertColumn(pos,col))
4708 return false;
4709
4710 m_cols.Insert( pos, col );
840fc4d1 4711 m_colsBestWidths.insert(m_colsBestWidths.begin() + pos, CachedColWidthInfo());
aef252d9 4712 OnColumnsCountChanged();
19723525
RR
4713 return true;
4714}
4715
aef252d9
VZ
4716void wxDataViewCtrl::OnColumnChange(unsigned int idx)
4717{
4718 if ( m_headerArea )
4719 m_headerArea->UpdateColumn(idx);
4720
4721 m_clientArea->UpdateDisplay();
4722}
4723
4724void wxDataViewCtrl::OnColumnsCountChanged()
9861f022
RR
4725{
4726 if (m_headerArea)
e2bfe673 4727 m_headerArea->SetColumnCount(GetColumnCount());
9861f022 4728
1c8e71c9 4729 m_clientArea->OnColumnsCountChanged();
9861f022 4730}
3b6280be
RR
4731
4732void wxDataViewCtrl::DoSetExpanderColumn()
4733{
4734 m_clientArea->UpdateDisplay();
4735}
4736
4737void wxDataViewCtrl::DoSetIndent()
4738{
4739 m_clientArea->UpdateDisplay();
4740}
4741
afebb87b
RR
4742unsigned int wxDataViewCtrl::GetColumnCount() const
4743{
4744 return m_cols.GetCount();
4745}
4746
bbfd4548
VZ
4747bool wxDataViewCtrl::SetRowHeight( int lineHeight )
4748{
4749 if ( !m_clientArea )
4750 return false;
4751
4752 m_clientArea->SetRowHeight(lineHeight);
4753
4754 return true;
4755}
4756
aef252d9 4757wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int idx ) const
afebb87b 4758{
aef252d9 4759 return m_cols[idx];
afebb87b
RR
4760}
4761
702f5349 4762wxDataViewColumn *wxDataViewCtrl::GetColumnAt(unsigned int pos) const
fc8c1a15 4763{
702f5349
VZ
4764 // columns can't be reordered if there is no header window which allows
4765 // to do this
4766 const unsigned idx = m_headerArea ? m_headerArea->GetColumnsOrder()[pos]
977a41ec 4767 : pos;
59e60167 4768
702f5349
VZ
4769 return GetColumn(idx);
4770}
cfa42cb8 4771
702f5349
VZ
4772int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn *column) const
4773{
4774 const unsigned count = m_cols.size();
4775 for ( unsigned n = 0; n < count; n++ )
4776 {
4777 if ( m_cols[n] == column )
4778 return n;
4779 }
4780
4781 return wxNOT_FOUND;
4782}
4783
d0154e3a
VS
4784unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx) const
4785{
840fc4d1
VS
4786 if ( m_colsBestWidths[idx].width != 0 )
4787 return m_colsBestWidths[idx].width;
d0154e3a 4788
0645bd38 4789 const int count = m_clientArea->GetRowCount();
d0154e3a
VS
4790 wxDataViewColumn *column = GetColumn(idx);
4791 wxDataViewRenderer *renderer =
4792 const_cast<wxDataViewRenderer*>(column->GetRenderer());
4793
0645bd38
VS
4794 class MaxWidthCalculator
4795 {
4796 public:
0fdeaabe
VS
4797 MaxWidthCalculator(const wxDataViewCtrl *dvc,
4798 wxDataViewMainWindow *clientArea,
0645bd38
VS
4799 wxDataViewRenderer *renderer,
4800 const wxDataViewModel *model,
0fdeaabe
VS
4801 unsigned column,
4802 int expanderSize)
0645bd38 4803 : m_width(0),
0fdeaabe 4804 m_dvc(dvc),
0645bd38
VS
4805 m_clientArea(clientArea),
4806 m_renderer(renderer),
4807 m_model(model),
0fdeaabe
VS
4808 m_column(column),
4809 m_expanderSize(expanderSize)
4810
0645bd38 4811 {
0fdeaabe
VS
4812 m_isExpanderCol =
4813 !clientArea->IsList() &&
4814 (column == 0 ||
4815 GetExpanderColumnOrFirstOne(const_cast<wxDataViewCtrl*>(dvc)) == dvc->GetColumnAt(column));
0645bd38
VS
4816 }
4817
4818 void UpdateWithWidth(int width)
4819 {
4820 m_width = wxMax(m_width, width);
4821 }
4822
4823 void UpdateWithRow(int row)
4824 {
0fdeaabe
VS
4825 int indent = 0;
4826 wxDataViewItem item;
4827
4828 if ( m_isExpanderCol )
4829 {
4830 wxDataViewTreeNode *node = m_clientArea->GetTreeNodeByRow(row);
4831 item = node->GetItem();
4832 indent = m_dvc->GetIndent() * node->GetIndentLevel() + m_expanderSize;
4833 }
4834 else
4835 {
4836 item = m_clientArea->GetItemByRow(row);
4837 }
4838
0645bd38 4839 m_renderer->PrepareForItem(m_model, item, m_column);
0fdeaabe 4840 m_width = wxMax(m_width, m_renderer->GetSize().x + indent);
0645bd38
VS
4841 }
4842
4843 int GetMaxWidth() const { return m_width; }
4844
4845 private:
4846 int m_width;
0fdeaabe 4847 const wxDataViewCtrl *m_dvc;
0645bd38
VS
4848 wxDataViewMainWindow *m_clientArea;
4849 wxDataViewRenderer *m_renderer;
4850 const wxDataViewModel *m_model;
4851 unsigned m_column;
0fdeaabe
VS
4852 bool m_isExpanderCol;
4853 int m_expanderSize;
0645bd38
VS
4854 };
4855
0fdeaabe
VS
4856 MaxWidthCalculator calculator(this, m_clientArea, renderer,
4857 GetModel(), column->GetModelColumn(),
4858 m_clientArea->GetRowHeight());
d0154e3a 4859
f0630187
VS
4860 calculator.UpdateWithWidth(column->GetMinWidth());
4861
d0154e3a 4862 if ( m_headerArea )
53137278 4863 calculator.UpdateWithWidth(m_headerArea->GetColumnTitleWidth(*column));
0645bd38
VS
4864
4865 // The code below deserves some explanation. For very large controls, we
4866 // simply can't afford to calculate sizes for all items, it takes too
4867 // long. So the best we can do is to check the first and the last N/2
4868 // items in the control for some sufficiently large N and calculate best
4869 // sizes from that. That can result in the calculated best width being too
4870 // small for some outliers, but it's better to get slightly imperfect
4871 // result than to wait several seconds after every update. To avoid highly
4872 // visible miscalculations, we also include all currently visible items
4873 // no matter what. Finally, the value of N is determined dynamically by
4874 // measuring how much time we spent on the determining item widths so far.
4875
4876#if wxUSE_STOPWATCH
4877 int top_part_end = count;
4878 static const long CALC_TIMEOUT = 20/*ms*/;
4879 // don't call wxStopWatch::Time() too often
4880 static const unsigned CALC_CHECK_FREQ = 100;
4881 wxStopWatch timer;
4882#else
4883 // use some hard-coded limit, that's the best we can do without timer
4884 int top_part_end = wxMin(500, count);
4885#endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4886
4887 int row = 0;
4888
4889 for ( row = 0; row < top_part_end; row++ )
4890 {
4891#if wxUSE_STOPWATCH
4892 if ( row % CALC_CHECK_FREQ == CALC_CHECK_FREQ-1 &&
4893 timer.Time() > CALC_TIMEOUT )
4894 break;
4895#endif // wxUSE_STOPWATCH
4896 calculator.UpdateWithRow(row);
d0154e3a
VS
4897 }
4898
40fc5b2f 4899 // row is the first unmeasured item now; that's our value of N/2
0645bd38
VS
4900
4901 if ( row < count )
d0154e3a 4902 {
0645bd38 4903 top_part_end = row;
d0154e3a 4904
0645bd38
VS
4905 // add bottom N/2 items now:
4906 const int bottom_part_start = wxMax(row, count - row);
4907 for ( row = bottom_part_start; row < count; row++ )
4908 {
4909 calculator.UpdateWithRow(row);
4910 }
86755098 4911
0645bd38
VS
4912 // finally, include currently visible items in the calculation:
4913 const wxPoint origin = CalcUnscrolledPosition(wxPoint(0, 0));
4914 int first_visible = m_clientArea->GetLineAt(origin.y);
4915 int last_visible = m_clientArea->GetLineAt(origin.y + GetClientSize().y);
4916
4917 first_visible = wxMax(first_visible, top_part_end);
4918 last_visible = wxMin(bottom_part_start, last_visible);
4919
4920 for ( row = first_visible; row < last_visible; row++ )
4921 {
4922 calculator.UpdateWithRow(row);
4923 }
4924
4925 wxLogTrace("dataview",
4926 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4927 top_part_end,
4928 count - bottom_part_start,
4929 wxMax(0, last_visible - first_visible),
4930 count);
d0154e3a
VS
4931 }
4932
0645bd38 4933 int max_width = calculator.GetMaxWidth();
d0154e3a
VS
4934 if ( max_width > 0 )
4935 max_width += 2 * PADDING_RIGHTLEFT;
4936
840fc4d1 4937 const_cast<wxDataViewCtrl*>(this)->m_colsBestWidths[idx].width = max_width;
d0154e3a
VS
4938 return max_width;
4939}
4940
702f5349 4941void wxDataViewCtrl::ColumnMoved(wxDataViewColumn * WXUNUSED(col),
977a41ec 4942 unsigned int WXUNUSED(new_pos))
702f5349
VZ
4943{
4944 // do _not_ reorder m_cols elements here, they should always be in the
4945 // order in which columns were added, we only display the columns in
4946 // different order
fc8c1a15
RR
4947 m_clientArea->UpdateDisplay();
4948}
4949
afebb87b
RR
4950bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
4951{
b3a3c9d8 4952 wxDataViewColumnList::compatibility_iterator ret = m_cols.Find( column );
4264606e 4953 if (!ret)
afebb87b
RR
4954 return false;
4955
d0154e3a 4956 m_colsBestWidths.erase(m_colsBestWidths.begin() + GetColumnIndex(column));
b7fe2261 4957 m_cols.Erase(ret);
f52f3795
VS
4958
4959 if ( m_clientArea->GetCurrentColumn() == column )
4960 m_clientArea->ClearCurrentColumn();
4961
aef252d9 4962 OnColumnsCountChanged();
afebb87b
RR
4963
4964 return true;
4965}
4966
4967bool wxDataViewCtrl::ClearColumns()
4968{
42cbde51 4969 SetExpanderColumn(NULL);
b3a3c9d8 4970 m_cols.Clear();
d0154e3a 4971 m_colsBestWidths.clear();
f52f3795
VS
4972
4973 m_clientArea->ClearCurrentColumn();
4974
aef252d9 4975 OnColumnsCountChanged();
f52f3795 4976
afebb87b
RR
4977 return true;
4978}
4979
bed74e48 4980void wxDataViewCtrl::InvalidateColBestWidth(int idx)
d0154e3a 4981{
840fc4d1
VS
4982 m_colsBestWidths[idx].width = 0;
4983 m_colsBestWidths[idx].dirty = true;
bed74e48 4984 m_colsDirty = true;
d0154e3a
VS
4985}
4986
bed74e48 4987void wxDataViewCtrl::InvalidateColBestWidths()
d0154e3a 4988{
840fc4d1 4989 // mark all columns as dirty:
d0154e3a
VS
4990 m_colsBestWidths.clear();
4991 m_colsBestWidths.resize(m_cols.size());
bed74e48
VS
4992 m_colsDirty = true;
4993}
d0154e3a 4994
bed74e48
VS
4995void wxDataViewCtrl::UpdateColWidths()
4996{
840fc4d1
VS
4997 m_colsDirty = false;
4998
bed74e48
VS
4999 if ( !m_headerArea )
5000 return;
5001
76b92fa5
VS
5002 const unsigned len = m_colsBestWidths.size();
5003 for ( unsigned i = 0; i < len; i++ )
bed74e48 5004 {
840fc4d1
VS
5005 // Note that we have to have an explicit 'dirty' flag here instead of
5006 // checking if the width==0, as is done in GetBestColumnWidth().
5007 //
5008 // Testing width==0 wouldn't work correctly if some code called
5009 // GetWidth() after col. width invalidation but before
5010 // wxDataViewCtrl::UpdateColWidths() was called at idle time. This
5011 // would result in the header's column width getting out of sync with
5012 // the control itself.
5013 if ( m_colsBestWidths[i].dirty )
5014 {
76b92fa5 5015 m_headerArea->UpdateColumn(i);
840fc4d1
VS
5016 m_colsBestWidths[i].dirty = false;
5017 }
bed74e48
VS
5018 }
5019}
5020
5021void wxDataViewCtrl::OnInternalIdle()
5022{
5023 wxDataViewCtrlBase::OnInternalIdle();
5024
5025 if ( m_colsDirty )
bed74e48 5026 UpdateColWidths();
d0154e3a
VS
5027}
5028
453091c2
RR
5029int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
5030{
5d9e1605
RR
5031 unsigned int len = GetColumnCount();
5032 for ( unsigned int i = 0; i < len; i++ )
5033 {
5034 wxDataViewColumn * col = GetColumnAt(i);
5035 if (column==col)
5036 return i;
5037 }
ce00f59b 5038
5d9e1605 5039 return wxNOT_FOUND;
453091c2
RR
5040}
5041
21f47fb9
RR
5042wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
5043{
46234a03 5044 return m_sortingColumnIdx == wxNOT_FOUND ? NULL
977a41ec 5045 : GetColumn(m_sortingColumnIdx);
21f47fb9
RR
5046}
5047
80ce465c
VZ
5048wxDataViewItem wxDataViewCtrl::DoGetCurrentItem() const
5049{
5050 return GetItemByRow(m_clientArea->GetCurrentRow());
5051}
5052
5053void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem& item)
5054{
5055 const int row = m_clientArea->GetRowByItem(item);
5056
5057 const unsigned oldCurrent = m_clientArea->GetCurrentRow();
5058 if ( static_cast<unsigned>(row) != oldCurrent )
5059 {
5060 m_clientArea->ChangeCurrentRow(row);
5061 m_clientArea->RefreshRow(oldCurrent);
5062 m_clientArea->RefreshRow(row);
5063 }
5064}
5065
ee1377e1
VS
5066wxDataViewColumn *wxDataViewCtrl::GetCurrentColumn() const
5067{
5068 return m_clientArea->GetCurrentColumn();
5069}
5070
fa93d732 5071int wxDataViewCtrl::GetSelectedItemsCount() const
66e09788 5072{
fa93d732 5073 return m_clientArea->GetSelections().size();
66e09788
RR
5074}
5075
b7e9f8b1
RR
5076int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
5077{
5078 sel.Empty();
f23b8c0d 5079 const wxDataViewSelection& selections = m_clientArea->GetSelections();
373a4816 5080
f23b8c0d 5081 const size_t len = selections.size();
eceb6af1 5082 for ( size_t i = 0; i < len; i++ )
b7e9f8b1 5083 {
f23b8c0d 5084 wxDataViewItem item = m_clientArea->GetItemByRow(selections[i]);
373a4816
VS
5085 if ( item.IsOk() )
5086 {
5087 sel.Add(item);
5088 }
5089 else
5090 {
5091 wxFAIL_MSG( "invalid item in selection - bad internal state" );
5092 }
b7e9f8b1 5093 }
373a4816
VS
5094
5095 return sel.size();
b7e9f8b1
RR
5096}
5097
5098void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
5099{
59e60167 5100 wxDataViewSelection selection(wxDataViewSelectionCmp);
4219d8b0
RR
5101
5102 wxDataViewItem last_parent;
5103
b7e9f8b1
RR
5104 int len = sel.GetCount();
5105 for( int i = 0; i < len; i ++ )
5106 {
4219d8b0
RR
5107 wxDataViewItem item = sel[i];
5108 wxDataViewItem parent = GetModel()->GetParent( item );
5109 if (parent)
5110 {
5111 if (parent != last_parent)
5112 ExpandAncestors(item);
5113 }
9adeb77a 5114
4219d8b0
RR
5115 last_parent = parent;
5116 int row = m_clientArea->GetRowByItem( item );
b7e9f8b1
RR
5117 if( row >= 0 )
5118 selection.Add( static_cast<unsigned int>(row) );
5119 }
9adeb77a 5120
b7e9f8b1
RR
5121 m_clientArea->SetSelections( selection );
5122}
5123
5124void wxDataViewCtrl::Select( const wxDataViewItem & item )
704c3490 5125{
4219d8b0 5126 ExpandAncestors( item );
9adeb77a 5127
b7e9f8b1
RR
5128 int row = m_clientArea->GetRowByItem( item );
5129 if( row >= 0 )
57f2a652 5130 {
bc4f1ff2 5131 // Unselect all rows before select another in the single select mode
57f2a652
RR
5132 if (m_clientArea->IsSingleSel())
5133 m_clientArea->SelectAllRows(false);
ce00f59b 5134
b7e9f8b1 5135 m_clientArea->SelectRow(row, true);
ce00f59b 5136
de67922e
RR
5137 // Also set focus to the selected item
5138 m_clientArea->ChangeCurrentRow( row );
57f2a652 5139 }
704c3490 5140}
3b6280be 5141
b7e9f8b1 5142void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
6ff7eee7 5143{
b7e9f8b1
RR
5144 int row = m_clientArea->GetRowByItem( item );
5145 if( row >= 0 )
5146 m_clientArea->SelectRow(row, false);
6ff7eee7
RR
5147}
5148
b7e9f8b1 5149bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
6ff7eee7 5150{
b7e9f8b1
RR
5151 int row = m_clientArea->GetRowByItem( item );
5152 if( row >= 0 )
5153 {
5154 return m_clientArea->IsRowSelected(row);
5155 }
5156 return false;
6ff7eee7
RR
5157}
5158
4bdc891f
VZ
5159void wxDataViewCtrl::SetAlternateRowColour(const wxColour& colour)
5160{
5161 m_alternateRowColour = colour;
5162}
5163
b7e9f8b1 5164void wxDataViewCtrl::SelectAll()
6ff7eee7 5165{
b7e9f8b1
RR
5166 m_clientArea->SelectAllRows(true);
5167}
df387169 5168
b7e9f8b1
RR
5169void wxDataViewCtrl::UnselectAll()
5170{
5171 m_clientArea->SelectAllRows(false);
6ff7eee7 5172}
b7e9f8b1 5173
fbda518c 5174void wxDataViewCtrl::EnsureVisible( int row, int column )
b7e9f8b1 5175{
fbda518c
RR
5176 if( row < 0 )
5177 row = 0;
67be459b 5178 if( row > (int) m_clientArea->GetRowCount() )
fbda518c
RR
5179 row = m_clientArea->GetRowCount();
5180
5181 int first = m_clientArea->GetFirstVisibleRow();
5182 int last = m_clientArea->GetLastVisibleRow();
5183 if( row < first )
5184 m_clientArea->ScrollTo( row, column );
5185 else if( row > last )
5186 m_clientArea->ScrollTo( row - last + first, column );
5187 else
5188 m_clientArea->ScrollTo( first, column );
b7e9f8b1
RR
5189}
5190
fbda518c 5191void wxDataViewCtrl::EnsureVisible( const wxDataViewItem & item, const wxDataViewColumn * column )
b7e9f8b1 5192{
4219d8b0 5193 ExpandAncestors( item );
9adeb77a 5194
13499080
RR
5195 m_clientArea->RecalculateDisplay();
5196
b7e9f8b1
RR
5197 int row = m_clientArea->GetRowByItem(item);
5198 if( row >= 0 )
fbda518c
RR
5199 {
5200 if( column == NULL )
9bcc8016 5201 EnsureVisible(row, -1);
fbda518c 5202 else
702f5349 5203 EnsureVisible( row, GetColumnIndex(column) );
fbda518c 5204 }
b7fe2261 5205
b7e9f8b1
RR
5206}
5207
4cef3873 5208void wxDataViewCtrl::HitTest( const wxPoint & point, wxDataViewItem & item,
bc4f1ff2 5209 wxDataViewColumn* &column ) const
66e09788
RR
5210{
5211 m_clientArea->HitTest(point, item, column);
5212}
5213
4cef3873 5214wxRect wxDataViewCtrl::GetItemRect( const wxDataViewItem & item,
bc4f1ff2 5215 const wxDataViewColumn* column ) const
66e09788
RR
5216{
5217 return m_clientArea->GetItemRect(item, column);
5218}
5219
b7e9f8b1
RR
5220wxDataViewItem wxDataViewCtrl::GetItemByRow( unsigned int row ) const
5221{
5222 return m_clientArea->GetItemByRow( row );
5223}
5224
5225int wxDataViewCtrl::GetRowByItem( const wxDataViewItem & item ) const
5226{
5227 return m_clientArea->GetRowByItem( item );
5228}
5229
afebb87b
RR
5230void wxDataViewCtrl::Expand( const wxDataViewItem & item )
5231{
e3d358bb
RR
5232 ExpandAncestors( item );
5233
afebb87b
RR
5234 int row = m_clientArea->GetRowByItem( item );
5235 if (row != -1)
b6b17152 5236 {
afebb87b 5237 m_clientArea->Expand(row);
b6b17152
VS
5238 InvalidateColBestWidths();
5239 }
afebb87b
RR
5240}
5241
5242void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
5243{
5244 int row = m_clientArea->GetRowByItem( item );
5245 if (row != -1)
b6b17152 5246 {
b7fe2261 5247 m_clientArea->Collapse(row);
b6b17152
VS
5248 InvalidateColBestWidths();
5249 }
afebb87b
RR
5250}
5251
739a8399
RR
5252bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const
5253{
5254 int row = m_clientArea->GetRowByItem( item );
5255 if (row != -1)
5256 return m_clientArea->IsExpanded(row);
5257 return false;
5258}
5259
907f09f4 5260void wxDataViewCtrl::EditItem(const wxDataViewItem& item, const wxDataViewColumn *column)
c937344c 5261{
907f09f4
VS
5262 wxCHECK_RET( item.IsOk(), "invalid item" );
5263 wxCHECK_RET( column, "no column provided" );
4cef3873 5264
907f09f4 5265 m_clientArea->StartEditing(item, column);
c937344c
RR
5266}
5267
4cef3873 5268#endif // !wxUSE_GENERICDATAVIEWCTRL
4ed7af08 5269
4cef3873 5270#endif // wxUSE_DATAVIEWCTRL