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