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