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