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