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