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