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