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