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