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