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