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