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