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