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