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