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