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