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