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