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