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