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