]> git.saurik.com Git - wxWidgets.git/blame - src/generic/datavgen.cpp
Explicitly include X11/Xlib.h to fix AIX build.
[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
3436 // is normally done by Space).
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 {
1c8e71c9 3453 const wxDataViewItem item = GetItemByRow(m_currentRow);
276227fc 3454
1c8e71c9
VS
3455 // Activate the current activatable column. If not column is focused (typically
3456 // because the user has full row selected), try to find the first activatable
3457 // column (this would typically be a checkbox and we don't want to force the user
3458 // to set focus on the checkbox column).
3459 wxDataViewColumn *activatableCol = FindColumnForEditing(item, wxDATAVIEW_CELL_ACTIVATABLE);
276227fc
VS
3460
3461 if ( activatableCol )
3462 {
3463 const unsigned colIdx = activatableCol->GetModelColumn();
3464 const wxRect cell_rect = GetOwner()->GetItemRect(item, activatableCol);
3465
3466 wxDataViewRenderer *cell = activatableCol->GetRenderer();
3467 cell->PrepareForItem(GetModel(), item, colIdx);
dc73d7f5 3468 cell->WXActivateCell(cell_rect, GetModel(), item, colIdx, NULL);
276227fc 3469 }
d37b709c
RR
3470 }
3471 break;
48ae48a9 3472
cab07038
RR
3473 case WXK_UP:
3474 if ( m_currentRow > 0 )
1dc779fc 3475 OnVerticalNavigation( m_currentRow - 1, event );
cab07038
RR
3476 break;
3477
3478 case WXK_DOWN:
4385e702 3479 if ( m_currentRow + 1 < GetRowCount() )
1dc779fc 3480 OnVerticalNavigation( m_currentRow + 1, event );
cab07038 3481 break;
bc4f1ff2 3482 // Add the process for tree expanding/collapsing
3b6280be 3483 case WXK_LEFT:
cfc21881 3484 OnLeftKey();
235d5f88 3485 break;
cfc21881 3486
235d5f88 3487 case WXK_RIGHT:
cfc21881 3488 OnRightKey();
235d5f88 3489 break;
cfc21881 3490
cab07038 3491 case WXK_END:
235d5f88 3492 {
cab07038 3493 if (!IsEmpty())
1dc779fc 3494 OnVerticalNavigation( GetRowCount() - 1, event );
cab07038 3495 break;
235d5f88 3496 }
cab07038
RR
3497 case WXK_HOME:
3498 if (!IsEmpty())
1dc779fc 3499 OnVerticalNavigation( 0, event );
cab07038
RR
3500 break;
3501
3502 case WXK_PAGEUP:
3503 {
3504 int steps = pageSize - 1;
3505 int index = m_currentRow - steps;
3506 if (index < 0)
3507 index = 0;
3508
1dc779fc 3509 OnVerticalNavigation( index, event );
cab07038
RR
3510 }
3511 break;
3512
3513 case WXK_PAGEDOWN:
3514 {
3515 int steps = pageSize - 1;
0a71f9e9
RR
3516 unsigned int index = m_currentRow + steps;
3517 unsigned int count = GetRowCount();
cab07038
RR
3518 if ( index >= count )
3519 index = count - 1;
3520
1dc779fc 3521 OnVerticalNavigation( index, event );
cab07038
RR
3522 }
3523 break;
3524
0a807957
RR
3525 case WXK_F2:
3526 {
a027a36f 3527 if( !m_selection.empty() )
0a807957 3528 {
a67e6b86
VS
3529 // Mimic Windows 7 behavior: edit the item that has focus
3530 // if it is selected and the first selected item if focus
3531 // is out of selection.
3532 int sel;
3533 if ( m_selection.Index(m_currentRow) != wxNOT_FOUND )
3534 sel = m_currentRow;
3535 else
3536 sel = m_selection[0];
3537
3538
3539 const wxDataViewItem item = GetItemByRow(sel);
1c8e71c9
VS
3540
3541 // Edit the current column. If not column is focused
3542 // (typically because the user has full row selected), try
3543 // to find the first editable column.
3544 wxDataViewColumn *editableCol = FindColumnForEditing(item, wxDATAVIEW_CELL_EDITABLE);
3545
3546 if ( editableCol )
3547 GetOwner()->StartEditor(item, GetOwner()->GetColumnIndex(editableCol));
0a807957
RR
3548 }
3549 }
3550 break;
3551
cab07038
RR
3552 default:
3553 event.Skip();
3554 }
3555}
3556
1dc779fc
VS
3557void wxDataViewMainWindow::OnVerticalNavigation(unsigned int newCurrent, const wxKeyEvent& event)
3558{
3559 wxCHECK_RET( newCurrent < GetRowCount(),
3560 wxT("invalid item index in OnVerticalNavigation()") );
3561
3562 // if there is no selection, we cannot move it anywhere
3563 if (!HasCurrentRow())
3564 return;
3565
3566 unsigned int oldCurrent = m_currentRow;
3567
3568 // in single selection we just ignore Shift as we can't select several
3569 // items anyhow
3570 if ( event.ShiftDown() && !IsSingleSel() )
3571 {
3572 RefreshRow( oldCurrent );
3573
3574 ChangeCurrentRow( newCurrent );
3575
3576 // select all the items between the old and the new one
3577 if ( oldCurrent > newCurrent )
3578 {
3579 newCurrent = oldCurrent;
3580 oldCurrent = m_currentRow;
3581 }
3582
3583 SelectRows( oldCurrent, newCurrent, true );
3584 if (oldCurrent!=newCurrent)
3585 SendSelectionChangedEvent(GetItemByRow(m_selection[0]));
3586 }
3587 else // !shift
3588 {
3589 RefreshRow( oldCurrent );
3590
3591 // all previously selected items are unselected unless ctrl is held
3592 if ( !event.ControlDown() )
3593 SelectAllRows(false);
3594
3595 ChangeCurrentRow( newCurrent );
3596
3597 if ( !event.ControlDown() )
3598 {
3599 SelectRow( m_currentRow, true );
3600 SendSelectionChangedEvent(GetItemByRow(m_currentRow));
3601 }
3602 else
3603 RefreshRow( m_currentRow );
3604 }
3605
3606 GetOwner()->EnsureVisible( m_currentRow, -1 );
3607}
3608
cfc21881
VS
3609void wxDataViewMainWindow::OnLeftKey()
3610{
1c8e71c9
VS
3611 if ( IsList() )
3612 {
3613 TryAdvanceCurrentColumn(NULL, /*forward=*/false);
3614 }
3615 else
3616 {
3617 wxDataViewTreeNode* node = GetTreeNodeByRow(m_currentRow);
cfc21881 3618
1c8e71c9
VS
3619 if ( TryAdvanceCurrentColumn(node, /*forward=*/false) )
3620 return;
cfc21881 3621
1c8e71c9
VS
3622 // Because TryAdvanceCurrentColumn() return false, we are at the first
3623 // column or using whole-row selection. In this situation, we can use
3624 // the standard TreeView handling of the left key.
3625 if (node->HasChildren() && node->IsOpen())
3626 {
3627 Collapse(m_currentRow);
3628 }
3629 else
3630 {
3631 // if the node is already closed, we move the selection to its parent
3632 wxDataViewTreeNode *parent_node = node->GetParent();
3633
3634 if (parent_node)
3635 {
3636 int parent = GetRowByItem( parent_node->GetItem() );
3637 if ( parent >= 0 )
3638 {
3639 unsigned int row = m_currentRow;
3640 SelectRow( row, false);
3641 SelectRow( parent, true );
3642 ChangeCurrentRow( parent );
3643 GetOwner()->EnsureVisible( parent, -1 );
3644 SendSelectionChangedEvent( parent_node->GetItem() );
3645 }
3646 }
3647 }
3648 }
3649}
3650
3651void wxDataViewMainWindow::OnRightKey()
3652{
3653 if ( IsList() )
cfc21881 3654 {
1c8e71c9 3655 TryAdvanceCurrentColumn(NULL, /*forward=*/true);
cfc21881 3656 }
1c8e71c9 3657 else
cfc21881 3658 {
1c8e71c9 3659 wxDataViewTreeNode* node = GetTreeNodeByRow(m_currentRow);
cfc21881 3660
1c8e71c9 3661 if ( node->HasChildren() )
cfc21881 3662 {
1c8e71c9 3663 if ( !node->IsOpen() )
cfc21881 3664 {
1c8e71c9
VS
3665 Expand( m_currentRow );
3666 }
3667 else
3668 {
3669 // if the node is already open, we move the selection to the first child
cfc21881 3670 unsigned int row = m_currentRow;
1c8e71c9
VS
3671 SelectRow( row, false );
3672 SelectRow( row + 1, true );
3673 ChangeCurrentRow( row + 1 );
3674 GetOwner()->EnsureVisible( row + 1, -1 );
3675 SendSelectionChangedEvent( GetItemByRow(row+1) );
cfc21881
VS
3676 }
3677 }
1c8e71c9
VS
3678 else
3679 {
3680 TryAdvanceCurrentColumn(node, /*forward=*/true);
3681 }
cfc21881
VS
3682 }
3683}
3684
1c8e71c9 3685bool wxDataViewMainWindow::TryAdvanceCurrentColumn(wxDataViewTreeNode *node, bool forward)
cfc21881 3686{
1c8e71c9
VS
3687 if ( GetOwner()->GetColumnCount() == 0 )
3688 return false;
3689
3690 if ( !m_useCellFocus )
3691 return false;
3692
3693 if ( node )
3694 {
3695 // navigation shouldn't work in branch nodes without other columns:
3696 if ( node->HasChildren() && !GetModel()->HasContainerColumns(node->GetItem()) )
3697 return false;
3698 }
3699
3700 if ( m_currentCol == NULL || !m_currentColSetByKeyboard )
3701 {
3702 if ( forward )
3703 {
3704 m_currentCol = GetOwner()->GetColumnAt(1);
3705 m_currentColSetByKeyboard = true;
3706 RefreshRow(m_currentRow);
3707 return true;
3708 }
3709 else
3710 return false;
3711 }
3712
3713 int idx = GetOwner()->GetColumnIndex(m_currentCol) + (forward ? +1 : -1);
3714
3715 if ( idx >= (int)GetOwner()->GetColumnCount() )
3716 return false;
3717
3718 if ( idx < 1 )
cfc21881 3719 {
1c8e71c9
VS
3720 // We are going to the left of the second column. Reset to whole-row
3721 // focus (which means first column would be edited).
3722 m_currentCol = NULL;
3723 RefreshRow(m_currentRow);
3724 return true;
cfc21881 3725 }
1c8e71c9
VS
3726
3727 m_currentCol = GetOwner()->GetColumnAt(idx);
3728 m_currentColSetByKeyboard = true;
3729 RefreshRow(m_currentRow);
3730 return true;
cfc21881
VS
3731}
3732
4ed7af08
RR
3733void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
3734{
e21f75bd
RR
3735 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
3736 {
3737 // let the base handle mouse wheel events.
3738 event.Skip();
3739 return;
3740 }
3741
3d9bff2f
RR
3742 // set the focus to ourself if any of the mouse buttons are pressed
3743 if(event.ButtonDown() && !HasFocus())
3744 SetFocus();
3745
0fdc2321
RR
3746 int x = event.GetX();
3747 int y = event.GetY();
3748 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
0fdc2321 3749 wxDataViewColumn *col = NULL;
b7fe2261 3750
0fdc2321 3751 int xpos = 0;
9861f022 3752 unsigned int cols = GetOwner()->GetColumnCount();
0a71f9e9 3753 unsigned int i;
0fdc2321
RR
3754 for (i = 0; i < cols; i++)
3755 {
702f5349 3756 wxDataViewColumn *c = GetOwner()->GetColumnAt( i );
9861f022
RR
3757 if (c->IsHidden())
3758 continue; // skip it!
3759
0fdc2321
RR
3760 if (x < xpos + c->GetWidth())
3761 {
3762 col = c;
3763 break;
3764 }
3765 xpos += c->GetWidth();
3766 }
7ed24cb6
VZ
3767
3768 wxDataViewModel* const model = GetModel();
3769
3770 const unsigned int current = GetLineAt( y );
3771 const wxDataViewItem item = GetItemByRow(current);
3772
3773 // Handle right clicking here, before everything else as context menu
3774 // events should be sent even when we click outside of any item, unlike all
3775 // the other ones.
3776 if (event.RightUp())
3777 {
3778 wxWindow *parent = GetParent();
3779 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, parent->GetId());
3780 le.SetEventObject(parent);
3781 le.SetModel(model);
3782
3783 if ( item.IsOk() && col )
3784 {
3785 le.SetItem( item );
3786 le.SetColumn( col->GetModelColumn() );
3787 le.SetDataViewColumn( col );
3788
3789 wxVariant value;
3790 model->GetValue( value, item, col->GetModelColumn() );
3791 le.SetValue(value);
3792 }
3793
3794 parent->ProcessWindowEvent(le);
3795 return;
3796 }
3797
f554a14b 3798 if (!col)
737883f2
VZ
3799 {
3800 event.Skip();
0fdc2321 3801 return;
737883f2 3802 }
f554a14b 3803
24c4a50f 3804 wxDataViewRenderer *cell = col->GetRenderer();
5179bc0b 3805 if ((current >= GetRowCount()) || (x > GetEndOfLastCol()))
e21f75bd
RR
3806 {
3807 // Unselect all if below the last row ?
737883f2 3808 event.Skip();
e21f75bd
RR
3809 return;
3810 }
f554a14b 3811
1841f079
VZ
3812 wxDataViewColumn* const
3813 expander = GetExpanderColumnOrFirstOne(GetOwner());
3814
902334c8
VZ
3815 // Test whether the mouse is hovering over the expander (a.k.a tree "+"
3816 // button) and also determine the offset of the real cell start, skipping
3817 // the indentation and the expander itself.
abdb8c18 3818 bool hoverOverExpander = false;
c46ecbe3 3819 int itemOffset = 0;
1841f079 3820 if ((!IsList()) && (expander == col))
24c4a50f
RR
3821 {
3822 wxDataViewTreeNode * node = GetTreeNodeByRow(current);
abdb8c18 3823
c46ecbe3
VZ
3824 int indent = node->GetIndentLevel();
3825 itemOffset = GetOwner()->GetIndent()*indent;
3826
3827 if ( node->HasChildren() )
3828 {
977a41ec
FM
3829 // we make the rectangle we are looking in a bit bigger than the actual
3830 // visual expander so the user can hit that little thing reliably
c46ecbe3 3831 wxRect rect(itemOffset,
977a41ec
FM
3832 GetLineStart( current ) + (GetLineHeight(current) - m_lineHeight)/2,
3833 m_lineHeight, m_lineHeight);
902334c8 3834
abdb8c18 3835 if( rect.Contains(x, y) )
24c4a50f 3836 {
bc4f1ff2 3837 // So the mouse is over the expander
abdb8c18 3838 hoverOverExpander = true;
24c4a50f
RR
3839 if (m_underMouse && m_underMouse != node)
3840 {
bc4f1ff2 3841 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
df2c23e7 3842 RefreshRow(GetRowByItem(m_underMouse->GetItem()));
24c4a50f
RR
3843 }
3844 if (m_underMouse != node)
3845 {
bc4f1ff2 3846 // wxLogMessage("Do the row: %d", current);
df2c23e7 3847 RefreshRow(current);
24c4a50f
RR
3848 }
3849 m_underMouse = node;
3850 }
3851 }
c46ecbe3
VZ
3852
3853 // Account for the expander as well, even if this item doesn't have it,
3854 // its parent does so it still counts for the offset.
3855 itemOffset += m_lineHeight;
24c4a50f 3856 }
abdb8c18 3857 if (!hoverOverExpander)
24c4a50f
RR
3858 {
3859 if (m_underMouse != NULL)
3860 {
bc4f1ff2 3861 // wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
df2c23e7 3862 RefreshRow(GetRowByItem(m_underMouse->GetItem()));
24c4a50f
RR
3863 m_underMouse = NULL;
3864 }
3865 }
3866
9adeb77a 3867#if wxUSE_DRAG_AND_DROP
e21f75bd
RR
3868 if (event.Dragging())
3869 {
3870 if (m_dragCount == 0)
3871 {
3872 // we have to report the raw, physical coords as we want to be
3873 // able to call HitTest(event.m_pointDrag) from the user code to
3874 // get the item being dragged
3875 m_dragStart = event.GetPosition();
3876 }
3877
3878 m_dragCount++;
3879
3880 if (m_dragCount != 3)
3881 return;
3882
3883 if (event.LeftIsDown())
3884 {
4cef3873 3885 m_owner->CalcUnscrolledPosition( m_dragStart.x, m_dragStart.y,
bc4f1ff2 3886 &m_dragStart.x, &m_dragStart.y );
821baf7d 3887 unsigned int drag_item_row = GetLineAt( m_dragStart.y );
7ed24cb6 3888 wxDataViewItem itemDragged = GetItemByRow( drag_item_row );
821baf7d 3889
e21f75bd 3890 // Notify cell about drag
821baf7d
RR
3891 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() );
3892 event.SetEventObject( m_owner );
7ed24cb6 3893 event.SetItem( itemDragged );
821baf7d
RR
3894 event.SetModel( model );
3895 if (!m_owner->HandleWindowEvent( event ))
3896 return;
9adeb77a 3897
821baf7d
RR
3898 if (!event.IsAllowed())
3899 return;
9adeb77a 3900
821baf7d
RR
3901 wxDataObject *obj = event.GetDataObject();
3902 if (!obj)
3903 return;
9adeb77a 3904
818d91a9 3905 wxDataViewDropSource drag( this, drag_item_row );
592883ed 3906 drag.SetData( *obj );
818d91a9 3907 /* wxDragResult res = */ drag.DoDragDrop();
592883ed 3908 delete obj;
e21f75bd
RR
3909 }
3910 return;
3911 }
3912 else
3913 {
3914 m_dragCount = 0;
3915 }
9adeb77a 3916#endif // wxUSE_DRAG_AND_DROP
e21f75bd 3917
abdb8c18 3918 bool simulateClick = false;
e21f75bd 3919
0fcce6b9
RR
3920 if (event.ButtonDClick())
3921 {
3922 m_renameTimer->Stop();
3923 m_lastOnSame = false;
3924 }
3925
438fb233 3926 bool ignore_other_columns =
1841f079 3927 ((expander != col) &&
977a41ec
FM
3928 (model->IsContainer(item)) &&
3929 (!model->HasContainerColumns(item)));
b5ec7dd6 3930
0fdc2321
RR
3931 if (event.LeftDClick())
3932 {
977a41ec
FM
3933 if(hoverOverExpander)
3934 {
3935 // a double click on the expander will be converted into a "simulated" normal click
3936 simulateClick = true;
3937 }
3938 else if ( current == m_lineLastClicked )
0fdc2321 3939 {
dc73d7f5
VS
3940 wxWindow *parent = GetParent();
3941 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, parent->GetId());
3942 le.SetItem( item );
3943 le.SetColumn( col->GetModelColumn() );
3944 le.SetDataViewColumn( col );
3945 le.SetEventObject(parent);
3946 le.SetModel(GetModel());
b7e9f8b1 3947
dc73d7f5 3948 parent->ProcessWindowEvent(le);
e21f75bd
RR
3949 return;
3950 }
3951 else
3952 {
3953 // The first click was on another item, so don't interpret this as
3954 // a double click, but as a simple click instead
abdb8c18 3955 simulateClick = true;
0fdc2321 3956 }
120b9b05 3957 }
f554a14b 3958
abdb8c18 3959 if (event.LeftUp() && !hoverOverExpander)
0fcce6b9 3960 {
0a71f9e9 3961 if (m_lineSelectSingleOnUp != (unsigned int)-1)
e21f75bd
RR
3962 {
3963 // select single line
3964 SelectAllRows( false );
3965 SelectRow( m_lineSelectSingleOnUp, true );
4a745e76 3966 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp) );
e21f75bd 3967 }
120b9b05 3968
4cef3873 3969 // If the user click the expander, we do not do editing even if the column
bc4f1ff2 3970 // with expander are editable
abdb8c18 3971 if (m_lastOnSame && !ignore_other_columns)
0fcce6b9 3972 {
a8461d31 3973 if ((col == m_currentCol) && (current == m_currentRow) &&
0bdfa388 3974 (cell->GetMode() & wxDATAVIEW_CELL_EDITABLE) )
0fcce6b9
RR
3975 {
3976 m_renameTimer->Start( 100, true );
3977 }
3978 }
3979
3980 m_lastOnSame = false;
0a71f9e9 3981 m_lineSelectSingleOnUp = (unsigned int)-1;
120b9b05 3982 }
abdb8c18 3983 else if(!event.LeftUp())
e21f75bd
RR
3984 {
3985 // This is necessary, because after a DnD operation in
3986 // from and to ourself, the up event is swallowed by the
3987 // DnD code. So on next non-up event (which means here and
3988 // now) m_lineSelectSingleOnUp should be reset.
0a71f9e9 3989 m_lineSelectSingleOnUp = (unsigned int)-1;
e21f75bd
RR
3990 }
3991
3992 if (event.RightDown())
3993 {
3994 m_lineBeforeLastClicked = m_lineLastClicked;
3995 m_lineLastClicked = current;
3996
3997 // If the item is already selected, do not update the selection.
3998 // Multi-selections should not be cleared if a selected item is clicked.
3999 if (!IsRowSelected(current))
4000 {
4001 SelectAllRows(false);
a09da78b 4002 const unsigned oldCurrent = m_currentRow;
e21f75bd
RR
4003 ChangeCurrentRow(current);
4004 SelectRow(m_currentRow,true);
a09da78b 4005 RefreshRow(oldCurrent);
526e19e2 4006 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
e21f75bd 4007 }
0a807957 4008 }
e21f75bd
RR
4009 else if (event.MiddleDown())
4010 {
e21f75bd 4011 }
abdb8c18 4012
977a41ec
FM
4013 if((event.LeftDown() || simulateClick) && hoverOverExpander)
4014 {
4015 wxDataViewTreeNode* node = GetTreeNodeByRow(current);
bc4f1ff2 4016
4cef3873 4017 // hoverOverExpander being true tells us that our node must be
bc4f1ff2 4018 // valid and have children.
977a41ec
FM
4019 // So we don't need any extra checks.
4020 if( node->IsOpen() )
235d5f88 4021 Collapse(current);
977a41ec 4022 else
235d5f88 4023 Expand(current);
977a41ec
FM
4024 }
4025 else if ((event.LeftDown() || simulateClick) && !hoverOverExpander)
0fcce6b9 4026 {
e21f75bd
RR
4027 m_lineBeforeLastClicked = m_lineLastClicked;
4028 m_lineLastClicked = current;
4029
0a71f9e9 4030 unsigned int oldCurrentRow = m_currentRow;
e21f75bd
RR
4031 bool oldWasSelected = IsRowSelected(m_currentRow);
4032
4033 bool cmdModifierDown = event.CmdDown();
4034 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
4035 {
4036 if ( IsSingleSel() || !IsRowSelected(current) )
4037 {
4038 SelectAllRows( false );
e21f75bd 4039 ChangeCurrentRow(current);
e21f75bd 4040 SelectRow(m_currentRow,true);
526e19e2 4041 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
e21f75bd
RR
4042 }
4043 else // multi sel & current is highlighted & no mod keys
4044 {
4045 m_lineSelectSingleOnUp = current;
4046 ChangeCurrentRow(current); // change focus
4047 }
4048 }
4049 else // multi sel & either ctrl or shift is down
4050 {
4051 if (cmdModifierDown)
4052 {
4053 ChangeCurrentRow(current);
e21f75bd 4054 ReverseRowSelection(m_currentRow);
9439bc69 4055 SendSelectionChangedEvent(GetItemByRow(m_currentRow));
e21f75bd
RR
4056 }
4057 else if (event.ShiftDown())
4058 {
4059 ChangeCurrentRow(current);
4060
0a71f9e9 4061 unsigned int lineFrom = oldCurrentRow,
977a41ec 4062 lineTo = current;
e21f75bd
RR
4063
4064 if ( lineTo < lineFrom )
4065 {
4066 lineTo = lineFrom;
4067 lineFrom = m_currentRow;
4068 }
4069
4070 SelectRows(lineFrom, lineTo, true);
526e19e2 4071 SendSelectionChangedEvent(GetItemByRow(m_selection[0]) );
e21f75bd
RR
4072 }
4073 else // !ctrl, !shift
4074 {
4075 // test in the enclosing if should make it impossible
9a83f860 4076 wxFAIL_MSG( wxT("how did we get here?") );
e21f75bd
RR
4077 }
4078 }
777f9cef 4079
72664514
RR
4080 if (m_currentRow != oldCurrentRow)
4081 RefreshRow( oldCurrentRow );
e21f75bd 4082
0fcce6b9 4083 wxDataViewColumn *oldCurrentCol = m_currentCol;
120b9b05 4084
0fcce6b9
RR
4085 // Update selection here...
4086 m_currentCol = col;
1c8e71c9 4087 m_currentColSetByKeyboard = false;
120b9b05 4088
abdb8c18 4089 m_lastOnSame = !simulateClick && ((col == oldCurrentCol) &&
87f0efe2 4090 (current == oldCurrentRow)) && oldWasSelected;
0bdfa388 4091
dc73d7f5 4092 // Call ActivateCell() after everything else as under GTK+
0bdfa388
RR
4093 if (cell->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE)
4094 {
dbc3aec1
VS
4095 // notify cell about click
4096 cell->PrepareForItem(model, item, col->GetModelColumn());
86755098 4097
c46ecbe3 4098 wxRect cell_rect( xpos + itemOffset,
902334c8 4099 GetLineStart( current ),
c46ecbe3 4100 col->GetWidth() - itemOffset,
902334c8 4101 GetLineHeight( current ) );
a3a8d81d 4102
dbc3aec1
VS
4103 // Report position relative to the cell's custom area, i.e.
4104 // no the entire space as given by the control but the one
4105 // used by the renderer after calculation of alignment etc.
a3a8d81d 4106
dbc3aec1
VS
4107 // adjust the rectangle ourselves to account for the alignment
4108 wxRect rectItem = cell_rect;
4109 const int align = cell->GetAlignment();
4110 if ( align != wxDVR_DEFAULT_ALIGNMENT )
4111 {
4112 const wxSize size = cell->GetSize();
a3a8d81d 4113
dbc3aec1
VS
4114 if ( size.x >= 0 && size.x < cell_rect.width )
4115 {
4116 if ( align & wxALIGN_CENTER_HORIZONTAL )
4117 rectItem.x += (cell_rect.width - size.x)/2;
4118 else if ( align & wxALIGN_RIGHT )
4119 rectItem.x += cell_rect.width - size.x;
4120 // else: wxALIGN_LEFT is the default
4121 }
a3a8d81d 4122
dbc3aec1
VS
4123 if ( size.y >= 0 && size.y < cell_rect.height )
4124 {
4125 if ( align & wxALIGN_CENTER_VERTICAL )
4126 rectItem.y += (cell_rect.height - size.y)/2;
4127 else if ( align & wxALIGN_BOTTOM )
4128 rectItem.y += cell_rect.height - size.y;
4129 // else: wxALIGN_TOP is the default
a3a8d81d 4130 }
dbc3aec1 4131 }
a3a8d81d 4132
dc73d7f5
VS
4133 wxMouseEvent event2(event);
4134 event2.m_x -= rectItem.x;
4135 event2.m_y -= rectItem.y;
4136 m_owner->CalcUnscrolledPosition(event2.m_x, event2.m_y, &event2.m_x, &event2.m_y);
a3a8d81d 4137
dc73d7f5
VS
4138 /* ignore ret */ cell->WXActivateCell
4139 (
4140 cell_rect,
4141 model,
4142 item,
4143 col->GetModelColumn(),
4144 &event2
4145 );
0bdfa388 4146 }
0fdc2321 4147 }
4ed7af08
RR
4148}
4149
4150void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event )
4151{
cab07038 4152 m_hasFocus = true;
120b9b05 4153
cab07038
RR
4154 if (HasCurrentRow())
4155 Refresh();
120b9b05 4156
cab07038
RR
4157 event.Skip();
4158}
4159
4160void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event )
4161{
4162 m_hasFocus = false;
120b9b05 4163
cab07038
RR
4164 if (HasCurrentRow())
4165 Refresh();
120b9b05 4166
4ed7af08
RR
4167 event.Skip();
4168}
4169
1c8e71c9
VS
4170void wxDataViewMainWindow::OnColumnsCountChanged()
4171{
4172 int editableCount = 0;
4173
4174 const unsigned cols = GetOwner()->GetColumnCount();
4175 for ( unsigned i = 0; i < cols; i++ )
4176 {
4177 wxDataViewColumn *c = GetOwner()->GetColumnAt(i);
4178 if ( c->IsHidden() )
4179 continue;
4180 if ( c->GetRenderer()->GetMode() != wxDATAVIEW_CELL_INERT )
4181 editableCount++;
4182 }
4183
4184 m_useCellFocus = (editableCount > 1);
4185
4186 UpdateDisplay();
4187}
4188
4ed7af08
RR
4189//-----------------------------------------------------------------------------
4190// wxDataViewCtrl
4191//-----------------------------------------------------------------------------
bc4f1ff2 4192
a76c2f37 4193WX_DEFINE_LIST(wxDataViewColumnList)
4ed7af08
RR
4194
4195IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
4b3feaa7
RR
4196BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase)
4197 EVT_SIZE(wxDataViewCtrl::OnSize)
4198END_EVENT_TABLE()
4199
4ed7af08
RR
4200wxDataViewCtrl::~wxDataViewCtrl()
4201{
4202 if (m_notifier)
4203 GetModel()->RemoveNotifier( m_notifier );
74123073 4204
b3a3c9d8 4205 m_cols.Clear();
d0154e3a 4206 m_colsBestWidths.clear();
4ed7af08
RR
4207}
4208
4209void wxDataViewCtrl::Init()
4210{
b3a3c9d8 4211 m_cols.DeleteContents(true);
4ed7af08 4212 m_notifier = NULL;
e822d1bd 4213
236a34ef 4214 // No sorting column at start
46234a03
VZ
4215 m_sortingColumnIdx = wxNOT_FOUND;
4216
236a34ef 4217 m_headerArea = NULL;
bed74e48
VS
4218
4219 m_colsDirty = false;
4ed7af08
RR
4220}
4221
62e9285a
VZ
4222bool wxDataViewCtrl::Create(wxWindow *parent,
4223 wxWindowID id,
4224 const wxPoint& pos,
4225 const wxSize& size,
4226 long style,
4227 const wxValidator& validator,
4228 const wxString& name)
4ed7af08 4229{
3fdf86f9
RR
4230// if ( (style & wxBORDER_MASK) == 0)
4231// style |= wxBORDER_SUNKEN;
777f9cef 4232
236a34ef
RR
4233 Init();
4234
c741d33f 4235 if (!wxControl::Create( parent, id, pos, size,
62e9285a 4236 style | wxScrolledWindowStyle, validator, name))
4b3feaa7
RR
4237 return false;
4238
b89cac3f 4239 SetInitialSize(size);
b5ec7dd6 4240
4ed7af08 4241#ifdef __WXMAC__
59e60167 4242 MacSetClipChildren( true );
4ed7af08
RR
4243#endif
4244
f554a14b 4245 m_clientArea = new wxDataViewMainWindow( this, wxID_ANY );
9861f022 4246
63ced01b
VZ
4247 // We use the cursor keys for moving the selection, not scrolling, so call
4248 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4249 // keyboard events forwarded to us from wxListMainWindow.
4250 DisableKeyboardScrolling();
4251
9861f022
RR
4252 if (HasFlag(wxDV_NO_HEADER))
4253 m_headerArea = NULL;
4254 else
56873923 4255 m_headerArea = new wxDataViewHeaderWindow(this);
f554a14b 4256
4ed7af08 4257 SetTargetWindow( m_clientArea );
f554a14b 4258
4ed7af08 4259 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
9861f022
RR
4260 if (m_headerArea)
4261 sizer->Add( m_headerArea, 0, wxGROW );
4ed7af08
RR
4262 sizer->Add( m_clientArea, 1, wxGROW );
4263 SetSizer( sizer );
b5ec7dd6 4264
4ed7af08
RR
4265 return true;
4266}
4267
3fdf86f9
RR
4268wxBorder wxDataViewCtrl::GetDefaultBorder() const
4269{
4270 return wxBORDER_THEME;
4271}
4272
4ed7af08
RR
4273#ifdef __WXMSW__
4274WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg,
bc4f1ff2
FM
4275 WXWPARAM wParam,
4276 WXLPARAM lParam)
4ed7af08 4277{
b910a8ad 4278 WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
4ed7af08
RR
4279
4280#ifndef __WXWINCE__
4281 // we need to process arrows ourselves for scrolling
4282 if ( nMsg == WM_GETDLGCODE )
4283 {
4284 rc |= DLGC_WANTARROWS;
4285 }
4286#endif
4287
4288 return rc;
4289}
4290#endif
4291
2571a33f
RR
4292wxSize wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize& size)
4293{
4294 wxSize newsize = size;
d7cda9b2 4295 if (!HasFlag(wxDV_NO_HEADER) && (m_headerArea))
977a41ec 4296 newsize.y -= m_headerArea->GetSize().y;
e822d1bd 4297
2571a33f
RR
4298 return newsize;
4299}
4300
f554a14b 4301void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
4ed7af08 4302{
4b3feaa7
RR
4303 // We need to override OnSize so that our scrolled
4304 // window a) does call Layout() to use sizers for
4305 // positioning the controls but b) does not query
4306 // the sizer for their size and use that for setting
4307 // the scrollable area as set that ourselves by
4308 // calling SetScrollbar() further down.
4309
4310 Layout();
4311
4312 AdjustScrollbars();
cd9b34ef
VZ
4313
4314 // We must redraw the headers if their height changed. Normally this
4315 // shouldn't happen as the control shouldn't let itself be resized beneath
4316 // its minimal height but avoid the display artefacts that appear if it
4317 // does happen, e.g. because there is really not enough vertical space.
4318 if ( !HasFlag(wxDV_NO_HEADER) && m_headerArea &&
4319 m_headerArea->GetSize().y <= m_headerArea->GetBestSize(). y )
4320 {
4321 m_headerArea->Refresh();
4322 }
4ed7af08
RR
4323}
4324
788432e3
RR
4325void wxDataViewCtrl::SetFocus()
4326{
4327 if (m_clientArea)
4328 m_clientArea->SetFocus();
4329}
4330
aba9bfd0 4331bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
4ed7af08
RR
4332{
4333 if (!wxDataViewCtrlBase::AssociateModel( model ))
4334 return false;
4335
aba9bfd0 4336 m_notifier = new wxGenericDataViewModelNotifier( m_clientArea );
4ed7af08 4337
f554a14b 4338 model->AddNotifier( m_notifier );
4ed7af08 4339
51bdecff 4340 m_clientArea->DestroyTree();
cfa42cb8 4341
aba9bfd0
RR
4342 m_clientArea->BuildTree(model);
4343
4b3feaa7 4344 m_clientArea->UpdateDisplay();
f554a14b 4345
4ed7af08
RR
4346 return true;
4347}
4348
9adeb77a
VZ
4349#if wxUSE_DRAG_AND_DROP
4350
821baf7d
RR
4351bool wxDataViewCtrl::EnableDragSource( const wxDataFormat &format )
4352{
4353 return m_clientArea->EnableDragSource( format );
4354}
4355
4356bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat &format )
4357{
4358 return m_clientArea->EnableDropTarget( format );
4359}
4360
9adeb77a
VZ
4361#endif // wxUSE_DRAG_AND_DROP
4362
4ed7af08
RR
4363bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
4364{
4365 if (!wxDataViewCtrlBase::AppendColumn(col))
4366 return false;
f554a14b 4367
afebb87b 4368 m_cols.Append( col );
d0154e3a 4369 m_colsBestWidths.push_back(0);
aef252d9 4370 OnColumnsCountChanged();
736fe67c
RR
4371 return true;
4372}
4373
4374bool wxDataViewCtrl::PrependColumn( wxDataViewColumn *col )
4375{
4376 if (!wxDataViewCtrlBase::PrependColumn(col))
4377 return false;
4378
4379 m_cols.Insert( col );
d0154e3a 4380 m_colsBestWidths.insert(m_colsBestWidths.begin(), 0);
aef252d9 4381 OnColumnsCountChanged();
4ed7af08
RR
4382 return true;
4383}
4384
19723525
RR
4385bool wxDataViewCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
4386{
4387 if (!wxDataViewCtrlBase::InsertColumn(pos,col))
4388 return false;
4389
4390 m_cols.Insert( pos, col );
d0154e3a 4391 m_colsBestWidths.insert(m_colsBestWidths.begin() + pos, 0);
aef252d9 4392 OnColumnsCountChanged();
19723525
RR
4393 return true;
4394}
4395
aef252d9
VZ
4396void wxDataViewCtrl::OnColumnChange(unsigned int idx)
4397{
4398 if ( m_headerArea )
4399 m_headerArea->UpdateColumn(idx);
4400
4401 m_clientArea->UpdateDisplay();
4402}
4403
4404void wxDataViewCtrl::OnColumnsCountChanged()
9861f022
RR
4405{
4406 if (m_headerArea)
e2bfe673 4407 m_headerArea->SetColumnCount(GetColumnCount());
9861f022 4408
1c8e71c9 4409 m_clientArea->OnColumnsCountChanged();
9861f022 4410}
3b6280be
RR
4411
4412void wxDataViewCtrl::DoSetExpanderColumn()
4413{
4414 m_clientArea->UpdateDisplay();
4415}
4416
4417void wxDataViewCtrl::DoSetIndent()
4418{
4419 m_clientArea->UpdateDisplay();
4420}
4421
afebb87b
RR
4422unsigned int wxDataViewCtrl::GetColumnCount() const
4423{
4424 return m_cols.GetCount();
4425}
4426
bbfd4548
VZ
4427bool wxDataViewCtrl::SetRowHeight( int lineHeight )
4428{
4429 if ( !m_clientArea )
4430 return false;
4431
4432 m_clientArea->SetRowHeight(lineHeight);
4433
4434 return true;
4435}
4436
aef252d9 4437wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int idx ) const
afebb87b 4438{
aef252d9 4439 return m_cols[idx];
afebb87b
RR
4440}
4441
702f5349 4442wxDataViewColumn *wxDataViewCtrl::GetColumnAt(unsigned int pos) const
fc8c1a15 4443{
702f5349
VZ
4444 // columns can't be reordered if there is no header window which allows
4445 // to do this
4446 const unsigned idx = m_headerArea ? m_headerArea->GetColumnsOrder()[pos]
977a41ec 4447 : pos;
59e60167 4448
702f5349
VZ
4449 return GetColumn(idx);
4450}
cfa42cb8 4451
702f5349
VZ
4452int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn *column) const
4453{
4454 const unsigned count = m_cols.size();
4455 for ( unsigned n = 0; n < count; n++ )
4456 {
4457 if ( m_cols[n] == column )
4458 return n;
4459 }
4460
4461 return wxNOT_FOUND;
4462}
4463
d0154e3a
VS
4464unsigned int wxDataViewCtrl::GetBestColumnWidth(int idx) const
4465{
4466 if ( m_colsBestWidths[idx] != 0 )
4467 return m_colsBestWidths[idx];
4468
0645bd38 4469 const int count = m_clientArea->GetRowCount();
d0154e3a
VS
4470 wxDataViewColumn *column = GetColumn(idx);
4471 wxDataViewRenderer *renderer =
4472 const_cast<wxDataViewRenderer*>(column->GetRenderer());
4473
0645bd38
VS
4474 class MaxWidthCalculator
4475 {
4476 public:
4477 MaxWidthCalculator(wxDataViewMainWindow *clientArea,
4478 wxDataViewRenderer *renderer,
4479 const wxDataViewModel *model,
4480 unsigned column)
4481 : m_width(0),
4482 m_clientArea(clientArea),
4483 m_renderer(renderer),
4484 m_model(model),
4485 m_column(column)
4486 {
4487 }
4488
4489 void UpdateWithWidth(int width)
4490 {
4491 m_width = wxMax(m_width, width);
4492 }
4493
4494 void UpdateWithRow(int row)
4495 {
4496 wxDataViewItem item = m_clientArea->GetItemByRow(row);
4497 m_renderer->PrepareForItem(m_model, item, m_column);
4498 m_width = wxMax(m_width, m_renderer->GetSize().x);
4499 }
4500
4501 int GetMaxWidth() const { return m_width; }
4502
4503 private:
4504 int m_width;
4505 wxDataViewMainWindow *m_clientArea;
4506 wxDataViewRenderer *m_renderer;
4507 const wxDataViewModel *m_model;
4508 unsigned m_column;
4509 };
4510
4511 MaxWidthCalculator calculator(m_clientArea, renderer,
4512 GetModel(), column->GetModelColumn());
d0154e3a
VS
4513
4514 if ( m_headerArea )
4515 {
0645bd38 4516 int header_width = m_headerArea->GetTextExtent(column->GetTitle()).x;
d0154e3a 4517 // Labels on native MSW header are indented on both sides
0645bd38
VS
4518 header_width +=
4519 wxRendererNative::Get().GetHeaderButtonMargin(m_headerArea);
4520 calculator.UpdateWithWidth(header_width);
4521 }
4522
4523 // The code below deserves some explanation. For very large controls, we
4524 // simply can't afford to calculate sizes for all items, it takes too
4525 // long. So the best we can do is to check the first and the last N/2
4526 // items in the control for some sufficiently large N and calculate best
4527 // sizes from that. That can result in the calculated best width being too
4528 // small for some outliers, but it's better to get slightly imperfect
4529 // result than to wait several seconds after every update. To avoid highly
4530 // visible miscalculations, we also include all currently visible items
4531 // no matter what. Finally, the value of N is determined dynamically by
4532 // measuring how much time we spent on the determining item widths so far.
4533
4534#if wxUSE_STOPWATCH
4535 int top_part_end = count;
4536 static const long CALC_TIMEOUT = 20/*ms*/;
4537 // don't call wxStopWatch::Time() too often
4538 static const unsigned CALC_CHECK_FREQ = 100;
4539 wxStopWatch timer;
4540#else
4541 // use some hard-coded limit, that's the best we can do without timer
4542 int top_part_end = wxMin(500, count);
4543#endif // wxUSE_STOPWATCH/!wxUSE_STOPWATCH
4544
4545 int row = 0;
4546
4547 for ( row = 0; row < top_part_end; row++ )
4548 {
4549#if wxUSE_STOPWATCH
4550 if ( row % CALC_CHECK_FREQ == CALC_CHECK_FREQ-1 &&
4551 timer.Time() > CALC_TIMEOUT )
4552 break;
4553#endif // wxUSE_STOPWATCH
4554 calculator.UpdateWithRow(row);
d0154e3a
VS
4555 }
4556
40fc5b2f 4557 // row is the first unmeasured item now; that's our value of N/2
0645bd38
VS
4558
4559 if ( row < count )
d0154e3a 4560 {
0645bd38 4561 top_part_end = row;
d0154e3a 4562
0645bd38
VS
4563 // add bottom N/2 items now:
4564 const int bottom_part_start = wxMax(row, count - row);
4565 for ( row = bottom_part_start; row < count; row++ )
4566 {
4567 calculator.UpdateWithRow(row);
4568 }
86755098 4569
0645bd38
VS
4570 // finally, include currently visible items in the calculation:
4571 const wxPoint origin = CalcUnscrolledPosition(wxPoint(0, 0));
4572 int first_visible = m_clientArea->GetLineAt(origin.y);
4573 int last_visible = m_clientArea->GetLineAt(origin.y + GetClientSize().y);
4574
4575 first_visible = wxMax(first_visible, top_part_end);
4576 last_visible = wxMin(bottom_part_start, last_visible);
4577
4578 for ( row = first_visible; row < last_visible; row++ )
4579 {
4580 calculator.UpdateWithRow(row);
4581 }
4582
4583 wxLogTrace("dataview",
4584 "determined best size from %d top, %d bottom plus %d more visible items out of %d total",
4585 top_part_end,
4586 count - bottom_part_start,
4587 wxMax(0, last_visible - first_visible),
4588 count);
d0154e3a
VS
4589 }
4590
0645bd38 4591 int max_width = calculator.GetMaxWidth();
d0154e3a
VS
4592 if ( max_width > 0 )
4593 max_width += 2 * PADDING_RIGHTLEFT;
4594
4595 const_cast<wxDataViewCtrl*>(this)->m_colsBestWidths[idx] = max_width;
4596 return max_width;
4597}
4598
702f5349 4599void wxDataViewCtrl::ColumnMoved(wxDataViewColumn * WXUNUSED(col),
977a41ec 4600 unsigned int WXUNUSED(new_pos))
702f5349
VZ
4601{
4602 // do _not_ reorder m_cols elements here, they should always be in the
4603 // order in which columns were added, we only display the columns in
4604 // different order
fc8c1a15
RR
4605 m_clientArea->UpdateDisplay();
4606}
4607
afebb87b
RR
4608bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
4609{
b3a3c9d8 4610 wxDataViewColumnList::compatibility_iterator ret = m_cols.Find( column );
4264606e 4611 if (!ret)
afebb87b
RR
4612 return false;
4613
d0154e3a 4614 m_colsBestWidths.erase(m_colsBestWidths.begin() + GetColumnIndex(column));
b7fe2261 4615 m_cols.Erase(ret);
aef252d9 4616 OnColumnsCountChanged();
afebb87b
RR
4617
4618 return true;
4619}
4620
4621bool wxDataViewCtrl::ClearColumns()
4622{
42cbde51 4623 SetExpanderColumn(NULL);
b3a3c9d8 4624 m_cols.Clear();
d0154e3a 4625 m_colsBestWidths.clear();
aef252d9 4626 OnColumnsCountChanged();
afebb87b
RR
4627 return true;
4628}
4629
bed74e48 4630void wxDataViewCtrl::InvalidateColBestWidth(int idx)
d0154e3a
VS
4631{
4632 m_colsBestWidths[idx] = 0;
bed74e48 4633 m_colsDirty = true;
d0154e3a
VS
4634}
4635
bed74e48 4636void wxDataViewCtrl::InvalidateColBestWidths()
d0154e3a
VS
4637{
4638 m_colsBestWidths.clear();
4639 m_colsBestWidths.resize(m_cols.size());
bed74e48
VS
4640 m_colsDirty = true;
4641}
d0154e3a 4642
bed74e48
VS
4643void wxDataViewCtrl::UpdateColWidths()
4644{
4645 if ( !m_headerArea )
4646 return;
4647
76b92fa5
VS
4648 const unsigned len = m_colsBestWidths.size();
4649 for ( unsigned i = 0; i < len; i++ )
bed74e48 4650 {
76b92fa5
VS
4651 if ( m_colsBestWidths[i] == 0 )
4652 m_headerArea->UpdateColumn(i);
bed74e48
VS
4653 }
4654}
4655
4656void wxDataViewCtrl::OnInternalIdle()
4657{
4658 wxDataViewCtrlBase::OnInternalIdle();
4659
4660 if ( m_colsDirty )
d0154e3a 4661 {
bed74e48
VS
4662 m_colsDirty = false;
4663 UpdateColWidths();
d0154e3a
VS
4664 }
4665}
4666
453091c2
RR
4667int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
4668{
5d9e1605
RR
4669#if 1
4670 unsigned int len = GetColumnCount();
4671 for ( unsigned int i = 0; i < len; i++ )
4672 {
4673 wxDataViewColumn * col = GetColumnAt(i);
4674 if (column==col)
4675 return i;
4676 }
ce00f59b 4677
5d9e1605
RR
4678 return wxNOT_FOUND;
4679#else
4680 // This returns the position in pixels which is not what we want.
702f5349
VZ
4681 int ret = 0,
4682 dummy = 0;
4683 unsigned int len = GetColumnCount();
4684 for ( unsigned int i = 0; i < len; i++ )
526e19e2 4685 {
702f5349 4686 wxDataViewColumn * col = GetColumnAt(i);
526e19e2
RR
4687 if (col->IsHidden())
4688 continue;
4689 ret += col->GetWidth();
4690 if (column==col)
4691 {
702f5349 4692 CalcScrolledPosition( ret, dummy, &ret, &dummy );
526e19e2
RR
4693 break;
4694 }
4695 }
4696 return ret;
5d9e1605 4697#endif
453091c2
RR
4698}
4699
21f47fb9
RR
4700wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
4701{
46234a03 4702 return m_sortingColumnIdx == wxNOT_FOUND ? NULL
977a41ec 4703 : GetColumn(m_sortingColumnIdx);
21f47fb9
RR
4704}
4705
80ce465c
VZ
4706wxDataViewItem wxDataViewCtrl::DoGetCurrentItem() const
4707{
4708 return GetItemByRow(m_clientArea->GetCurrentRow());
4709}
4710
4711void wxDataViewCtrl::DoSetCurrentItem(const wxDataViewItem& item)
4712{
4713 const int row = m_clientArea->GetRowByItem(item);
4714
4715 const unsigned oldCurrent = m_clientArea->GetCurrentRow();
4716 if ( static_cast<unsigned>(row) != oldCurrent )
4717 {
4718 m_clientArea->ChangeCurrentRow(row);
4719 m_clientArea->RefreshRow(oldCurrent);
4720 m_clientArea->RefreshRow(row);
4721 }
4722}
4723
fa93d732 4724int wxDataViewCtrl::GetSelectedItemsCount() const
66e09788 4725{
fa93d732 4726 return m_clientArea->GetSelections().size();
66e09788
RR
4727}
4728
b7e9f8b1
RR
4729int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
4730{
4731 sel.Empty();
f23b8c0d 4732 const wxDataViewSelection& selections = m_clientArea->GetSelections();
373a4816 4733
f23b8c0d 4734 const size_t len = selections.size();
eceb6af1 4735 for ( size_t i = 0; i < len; i++ )
b7e9f8b1 4736 {
f23b8c0d 4737 wxDataViewItem item = m_clientArea->GetItemByRow(selections[i]);
373a4816
VS
4738 if ( item.IsOk() )
4739 {
4740 sel.Add(item);
4741 }
4742 else
4743 {
4744 wxFAIL_MSG( "invalid item in selection - bad internal state" );
4745 }
b7e9f8b1 4746 }
373a4816
VS
4747
4748 return sel.size();
b7e9f8b1
RR
4749}
4750
4751void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
4752{
59e60167 4753 wxDataViewSelection selection(wxDataViewSelectionCmp);
4219d8b0
RR
4754
4755 wxDataViewItem last_parent;
4756
b7e9f8b1
RR
4757 int len = sel.GetCount();
4758 for( int i = 0; i < len; i ++ )
4759 {
4219d8b0
RR
4760 wxDataViewItem item = sel[i];
4761 wxDataViewItem parent = GetModel()->GetParent( item );
4762 if (parent)
4763 {
4764 if (parent != last_parent)
4765 ExpandAncestors(item);
4766 }
9adeb77a 4767
4219d8b0
RR
4768 last_parent = parent;
4769 int row = m_clientArea->GetRowByItem( item );
b7e9f8b1
RR
4770 if( row >= 0 )
4771 selection.Add( static_cast<unsigned int>(row) );
4772 }
9adeb77a 4773
b7e9f8b1
RR
4774 m_clientArea->SetSelections( selection );
4775}
4776
4777void wxDataViewCtrl::Select( const wxDataViewItem & item )
704c3490 4778{
4219d8b0 4779 ExpandAncestors( item );
9adeb77a 4780
b7e9f8b1
RR
4781 int row = m_clientArea->GetRowByItem( item );
4782 if( row >= 0 )
57f2a652 4783 {
bc4f1ff2 4784 // Unselect all rows before select another in the single select mode
57f2a652
RR
4785 if (m_clientArea->IsSingleSel())
4786 m_clientArea->SelectAllRows(false);
ce00f59b 4787
b7e9f8b1 4788 m_clientArea->SelectRow(row, true);
ce00f59b 4789
de67922e
RR
4790 // Also set focus to the selected item
4791 m_clientArea->ChangeCurrentRow( row );
57f2a652 4792 }
704c3490 4793}
3b6280be 4794
b7e9f8b1 4795void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
6ff7eee7 4796{
b7e9f8b1
RR
4797 int row = m_clientArea->GetRowByItem( item );
4798 if( row >= 0 )
4799 m_clientArea->SelectRow(row, false);
6ff7eee7
RR
4800}
4801
b7e9f8b1 4802bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
6ff7eee7 4803{
b7e9f8b1
RR
4804 int row = m_clientArea->GetRowByItem( item );
4805 if( row >= 0 )
4806 {
4807 return m_clientArea->IsRowSelected(row);
4808 }
4809 return false;
6ff7eee7
RR
4810}
4811
b7e9f8b1 4812void wxDataViewCtrl::SelectAll()
6ff7eee7 4813{
b7e9f8b1
RR
4814 m_clientArea->SelectAllRows(true);
4815}
df387169 4816
b7e9f8b1
RR
4817void wxDataViewCtrl::UnselectAll()
4818{
4819 m_clientArea->SelectAllRows(false);
6ff7eee7 4820}
b7e9f8b1 4821
fbda518c 4822void wxDataViewCtrl::EnsureVisible( int row, int column )
b7e9f8b1 4823{
fbda518c
RR
4824 if( row < 0 )
4825 row = 0;
67be459b 4826 if( row > (int) m_clientArea->GetRowCount() )
fbda518c
RR
4827 row = m_clientArea->GetRowCount();
4828
4829 int first = m_clientArea->GetFirstVisibleRow();
4830 int last = m_clientArea->GetLastVisibleRow();
4831 if( row < first )
4832 m_clientArea->ScrollTo( row, column );
4833 else if( row > last )
4834 m_clientArea->ScrollTo( row - last + first, column );
4835 else
4836 m_clientArea->ScrollTo( first, column );
b7e9f8b1
RR
4837}
4838
fbda518c 4839void wxDataViewCtrl::EnsureVisible( const wxDataViewItem & item, const wxDataViewColumn * column )
b7e9f8b1 4840{
4219d8b0 4841 ExpandAncestors( item );
9adeb77a 4842
13499080
RR
4843 m_clientArea->RecalculateDisplay();
4844
b7e9f8b1
RR
4845 int row = m_clientArea->GetRowByItem(item);
4846 if( row >= 0 )
fbda518c
RR
4847 {
4848 if( column == NULL )
9bcc8016 4849 EnsureVisible(row, -1);
fbda518c 4850 else
702f5349 4851 EnsureVisible( row, GetColumnIndex(column) );
fbda518c 4852 }
b7fe2261 4853
b7e9f8b1
RR
4854}
4855
4cef3873 4856void wxDataViewCtrl::HitTest( const wxPoint & point, wxDataViewItem & item,
bc4f1ff2 4857 wxDataViewColumn* &column ) const
66e09788
RR
4858{
4859 m_clientArea->HitTest(point, item, column);
4860}
4861
4cef3873 4862wxRect wxDataViewCtrl::GetItemRect( const wxDataViewItem & item,
bc4f1ff2 4863 const wxDataViewColumn* column ) const
66e09788
RR
4864{
4865 return m_clientArea->GetItemRect(item, column);
4866}
4867
b7e9f8b1
RR
4868wxDataViewItem wxDataViewCtrl::GetItemByRow( unsigned int row ) const
4869{
4870 return m_clientArea->GetItemByRow( row );
4871}
4872
4873int wxDataViewCtrl::GetRowByItem( const wxDataViewItem & item ) const
4874{
4875 return m_clientArea->GetRowByItem( item );
4876}
4877
afebb87b
RR
4878void wxDataViewCtrl::Expand( const wxDataViewItem & item )
4879{
e3d358bb
RR
4880 ExpandAncestors( item );
4881
afebb87b
RR
4882 int row = m_clientArea->GetRowByItem( item );
4883 if (row != -1)
4884 m_clientArea->Expand(row);
4885}
4886
4887void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
4888{
4889 int row = m_clientArea->GetRowByItem( item );
4890 if (row != -1)
b7fe2261 4891 m_clientArea->Collapse(row);
afebb87b
RR
4892}
4893
739a8399
RR
4894bool wxDataViewCtrl::IsExpanded( const wxDataViewItem & item ) const
4895{
4896 int row = m_clientArea->GetRowByItem( item );
4897 if (row != -1)
4898 return m_clientArea->IsExpanded(row);
4899 return false;
4900}
4901
c937344c
RR
4902void wxDataViewCtrl::StartEditor( const wxDataViewItem & item, unsigned int column )
4903{
4904 wxDataViewColumn* col = GetColumn( column );
4905 if (!col)
4906 return;
4cef3873 4907
c937344c 4908 wxDataViewRenderer* renderer = col->GetRenderer();
2c336e24
VS
4909 if (renderer->GetMode() != wxDATAVIEW_CELL_EDITABLE)
4910 return;
4911
4912 const wxRect itemRect = GetItemRect(item, col);
4913 renderer->StartEditing(item, itemRect);
c937344c
RR
4914}
4915
4cef3873 4916#endif // !wxUSE_GENERICDATAVIEWCTRL
4ed7af08 4917
4cef3873 4918#endif // wxUSE_DATAVIEWCTRL