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