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