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