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