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