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