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