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