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