]> git.saurik.com Git - wxWidgets.git/blame - src/generic/datavgen.cpp
use fallback encoding in wxConvAuto when input is not in UTF-8
[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 }
301 void AddLeaf( void * leaf )
302 {
303 leaves.Add( leaf );
24c4a50f 304 if (g_column >= -1)
57f2a652 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 {
57f2a652
RR
367 nodes.Sort( &wxGenericTreeModelNodeCmp );
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()
66e09788
RR
426 {
427 SortPrepare();
428 m_root->Resort();
429 UpdateDisplay();
430 }
4ed7af08 431
66e09788
RR
432 void SortPrepare()
433 {
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();
66e09788
RR
447 g_asending = col->IsSortOrderAscending();
448 }
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
24c4a50f
RR
995// ---------------------------------------------------------
996// wxDataViewIconTextRenderer
997// ---------------------------------------------------------
998
999IMPLEMENT_CLASS(wxDataViewIconTextRenderer, wxDataViewCustomRenderer)
1000
1001wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
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}
1012
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}
1023
1024bool wxDataViewIconTextRenderer::Render( wxRect cell, wxDC *dc, int state )
1025{
1026 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
1027
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 }
1034
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
1665 // erase the line if it was drawn
c741d33f 1666 if (m_currentX < w)
87f0efe2
RR
1667 DrawCurrent();
1668
c741d33f 1669 if (event.ButtonUp())
87f0efe2
RR
1670 {
1671 m_isDragging = false;
c741d33f 1672 if (HasCapture())
87f0efe2
RR
1673 ReleaseMouse();
1674
1675 m_dirty = true;
1676
9861f022 1677 GetColumn(m_column)->SetWidth(m_currentX - m_minX);
87f0efe2
RR
1678
1679 Refresh();
1680 GetOwner()->Refresh();
1681 }
1682 else
1683 {
1684 m_currentX = wxMax(m_minX + 7, x);
1685
1686 // draw in the new location
1687 if (m_currentX < w) DrawCurrent();
1688 }
1689
1690 }
1691 else // not dragging
1692 {
1693 m_minX = 0;
1694 m_column = wxNOT_FOUND;
1695
1696 bool hit_border = false;
1697
1698 // end of the current column
1699 int xpos = 0;
1700
1701 // find the column where this event occured
9861f022 1702 int countCol = m_owner->GetColumnCount();
c741d33f 1703 for (int column = 0; column < countCol; column++)
87f0efe2
RR
1704 {
1705 wxDataViewColumn *p = GetColumn(column);
1706
c741d33f 1707 if (p->IsHidden())
87f0efe2
RR
1708 continue; // skip if not shown
1709
1710 xpos += p->GetWidth();
1711 m_column = column;
c741d33f 1712 if ((abs(x-xpos) < 3) && (y < 22))
87f0efe2
RR
1713 {
1714 hit_border = true;
1715 break;
1716 }
1717
c741d33f 1718 if (x < xpos)
87f0efe2
RR
1719 {
1720 // inside the column
1721 break;
1722 }
1723
1724 m_minX = xpos;
1725 }
1726
1727 if (m_column == wxNOT_FOUND)
1728 return;
1729
1730 bool resizeable = GetColumn(m_column)->IsResizeable();
1731 if (event.LeftDClick() && resizeable)
1732 {
9861f022 1733 GetColumn(m_column)->SetWidth(GetOwner()->GetBestColumnWidth(m_column));
87f0efe2
RR
1734 Refresh();
1735 }
1736 else if (event.LeftDown() || event.RightUp())
1737 {
c741d33f 1738 if (hit_border && event.LeftDown() && resizeable)
87f0efe2
RR
1739 {
1740 m_isDragging = true;
1741 CaptureMouse();
1742 m_currentX = x;
1743 DrawCurrent();
1744 }
1745 else // click on a column
1746 {
b7e9f8b1 1747 wxDataViewModel * model = GetOwner()->GetModel();
c741d33f 1748 wxEventType evt = event.LeftDown() ?
87f0efe2
RR
1749 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK :
1750 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK;
1751 SendEvent(evt, m_column);
b7e9f8b1
RR
1752
1753 //Left click the header
1754 if(event.LeftDown())
1755 {
1756 wxDataViewColumn *col = GetColumn(m_column);
1757 if(col->IsSortable())
1758 {
57f2a652
RR
1759 wxDataViewColumn* sortCol = m_owner->GetSortingColumn();
1760 if(model && sortCol == col)
b7e9f8b1
RR
1761 {
1762 bool order = col->IsSortOrderAscending();
1763 col->SetSortOrder(!order);
b7e9f8b1
RR
1764 }
1765 else if(model)
1766 {
57f2a652 1767 m_owner->SetSortingColumn(col);
b7e9f8b1
RR
1768 }
1769 }
1770 UpdateDisplay();
1771 if(model)
1772 model->Resort();
1773 //Send the column sorted event
1774 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, m_column);
1775 }
87f0efe2
RR
1776 }
1777 }
1778 else if (event.Moving())
1779 {
c741d33f 1780 if (hit_border && resizeable)
87f0efe2
RR
1781 m_currentCursor = m_resizeCursor;
1782 else
1783 m_currentCursor = wxSTANDARD_CURSOR;
1784
1785 SetCursor(*m_currentCursor);
1786 }
1787 }
1788}
1789
1790void wxGenericDataViewHeaderWindow::DrawCurrent()
1791{
1792 int x1 = m_currentX;
1793 int y1 = 0;
1794 ClientToScreen (&x1, &y1);
1795
1796 int x2 = m_currentX-1;
1797#ifdef __WXMSW__
1798 ++x2; // but why ????
1799#endif
1800 int y2 = 0;
1801 m_owner->GetClientSize( NULL, &y2 );
1802 m_owner->ClientToScreen( &x2, &y2 );
1803
1804 wxScreenDC dc;
9861f022
RR
1805 dc.SetLogicalFunction(wxINVERT);
1806 dc.SetPen(m_penCurrent);
1807 dc.SetBrush(*wxTRANSPARENT_BRUSH);
87f0efe2 1808 AdjustDC(dc);
9861f022 1809 dc.DrawLine(x1, y1, x2, y2);
87f0efe2
RR
1810}
1811
1812void wxGenericDataViewHeaderWindow::AdjustDC(wxDC& dc)
1813{
1814 int xpix, x;
1815
1816 m_owner->GetScrollPixelsPerUnit( &xpix, NULL );
1817 m_owner->GetViewStart( &x, NULL );
1818
1819 // shift the DC origin to match the position of the main window horizontal
1820 // scrollbar: this allows us to always use logical coords
1821 dc.SetDeviceOrigin( -x * xpix, 0 );
4ed7af08
RR
1822}
1823
87f0efe2
RR
1824#endif // defined(__WXMSW__)
1825
0fcce6b9
RR
1826//-----------------------------------------------------------------------------
1827// wxDataViewRenameTimer
1828//-----------------------------------------------------------------------------
1829
1830wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow *owner )
1831{
1832 m_owner = owner;
1833}
1834
1835void wxDataViewRenameTimer::Notify()
1836{
1837 m_owner->OnRenameTimer();
1838}
1839
4ed7af08
RR
1840//-----------------------------------------------------------------------------
1841// wxDataViewMainWindow
1842//-----------------------------------------------------------------------------
1843
704c3490
RR
1844//The tree building helper, declared firstly
1845void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, wxDataViewTreeNode * node);
1846
0a71f9e9 1847int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 )
cab07038
RR
1848{
1849 if (row1 > row2) return 1;
1850 if (row1 == row2) return 0;
1851 return -1;
1852}
1853
1854
45778c96 1855IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow, wxWindow)
4ed7af08
RR
1856
1857BEGIN_EVENT_TABLE(wxDataViewMainWindow,wxWindow)
1858 EVT_PAINT (wxDataViewMainWindow::OnPaint)
1859 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse)
1860 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus)
cab07038
RR
1861 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus)
1862 EVT_CHAR (wxDataViewMainWindow::OnChar)
4ed7af08
RR
1863END_EVENT_TABLE()
1864
1865wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID id,
1866 const wxPoint &pos, const wxSize &size, const wxString &name ) :
72664514 1867 wxWindow( parent, id, pos, size, wxWANTS_CHARS, name ),
cab07038 1868 m_selection( wxDataViewSelectionCmp )
120b9b05 1869
4ed7af08
RR
1870{
1871 SetOwner( parent );
f554a14b 1872
0fcce6b9
RR
1873 m_lastOnSame = false;
1874 m_renameTimer = new wxDataViewRenameTimer( this );
120b9b05 1875
0fcce6b9
RR
1876 // TODO: user better initial values/nothing selected
1877 m_currentCol = NULL;
1878 m_currentRow = 0;
1879
1880 // TODO: we need to calculate this smartly
c741d33f 1881 m_lineHeight =
87f0efe2
RR
1882#ifdef __WXMSW__
1883 17;
1884#else
1885 20;
1886#endif
9861f022 1887 wxASSERT(m_lineHeight > 2*PADDING_TOPBOTTOM);
e21f75bd
RR
1888
1889 m_dragCount = 0;
1890 m_dragStart = wxPoint(0,0);
0a71f9e9
RR
1891 m_lineLastClicked = (unsigned int) -1;
1892 m_lineBeforeLastClicked = (unsigned int) -1;
1893 m_lineSelectSingleOnUp = (unsigned int) -1;
120b9b05 1894
cab07038 1895 m_hasFocus = false;
f554a14b 1896
2e992e06 1897 SetBackgroundStyle( wxBG_STYLE_CUSTOM );
72664514
RR
1898 SetBackgroundColour( *wxWHITE );
1899
9861f022
RR
1900 m_penRule = wxPen(GetRuleColour(), 1, wxSOLID);
1901
3b6280be
RR
1902 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1903 m_penExpander = wxPen( wxColour(0,0,0), 1, wxSOLID );
aba9bfd0
RR
1904 //Some new added code to deal with the tree structure
1905 m_root = new wxDataViewTreeNode( NULL );
704c3490
RR
1906 m_root->SetHasChildren(true);
1907
3b6280be
RR
1908 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1909 m_count = -1 ;
24c4a50f 1910 m_underMouse = NULL;
4b3feaa7 1911 UpdateDisplay();
4ed7af08
RR
1912}
1913
1914wxDataViewMainWindow::~wxDataViewMainWindow()
1915{
aba9bfd0 1916 DestroyTree();
0fcce6b9
RR
1917 delete m_renameTimer;
1918}
1919
1920void wxDataViewMainWindow::OnRenameTimer()
1921{
1922 // We have to call this here because changes may just have
1923 // been made and no screen update taken place.
1924 if ( m_dirty )
1925 wxSafeYield();
1926
0fcce6b9 1927 int xpos = 0;
9861f022 1928 unsigned int cols = GetOwner()->GetColumnCount();
0a71f9e9 1929 unsigned int i;
0fcce6b9
RR
1930 for (i = 0; i < cols; i++)
1931 {
1932 wxDataViewColumn *c = GetOwner()->GetColumn( i );
9861f022
RR
1933 if (c->IsHidden())
1934 continue; // skip it!
1935
0fcce6b9
RR
1936 if (c == m_currentCol)
1937 break;
1938 xpos += c->GetWidth();
1939 }
c741d33f 1940 wxRect labelRect( xpos, m_currentRow * m_lineHeight,
87f0efe2 1941 m_currentCol->GetWidth(), m_lineHeight );
0fcce6b9 1942
1e510b1e
RR
1943 GetOwner()->CalcScrolledPosition( labelRect.x, labelRect.y,
1944 &labelRect.x, &labelRect.y);
1945
6cdcbce3
RR
1946 wxDataViewItem item = GetItemByRow( m_currentRow );
1947 m_currentCol->GetRenderer()->StartEditing( item, labelRect );
afebb87b 1948
4ed7af08
RR
1949}
1950
d5025dc0
RR
1951//------------------------------------------------------------------
1952// Helper class for do operation on the tree node
1953//------------------------------------------------------------------
aba9bfd0 1954class DoJob
a0f3af5f 1955{
aba9bfd0
RR
1956public:
1957 DoJob(){};
d5025dc0
RR
1958 virtual ~DoJob(){};
1959
1960 //The return value control how the tree-walker tranverse the tree
1961 // 0: Job done, stop tranverse and return
1962 // 1: Ignore the current node's subtree and continue
1963 // 2: Job not done, continue
1964 enum { OK = 0 , IGR = 1, CONT = 2 };
1965 virtual int operator() ( wxDataViewTreeNode * node ) = 0 ;
d47db7e0 1966 virtual int operator() ( void * n ) = 0;
aba9bfd0
RR
1967};
1968
aba9bfd0
RR
1969bool Walker( wxDataViewTreeNode * node, DoJob & func )
1970{
24c4a50f 1971 if( node==NULL )
aba9bfd0
RR
1972 return false;
1973
b7e9f8b1
RR
1974 switch( func( node ) )
1975 {
1976 case DoJob::OK :
1977 return true ;
1978 case DoJob::IGR:
1979 return false;
1980 case DoJob::CONT:
1981 default:
1982 ;
1983 }
1984
d47db7e0
RR
1985 wxDataViewTreeNodes nodes = node->GetNodes();
1986 wxDataViewTreeLeaves leaves = node->GetChildren();
1987
1988 int len_nodes = nodes.GetCount();
1989 int len = leaves.GetCount();
1990 int i = 0, nodes_i = 0;
b7e9f8b1 1991
aba9bfd0
RR
1992 for( ; i < len ; i ++ )
1993 {
d47db7e0
RR
1994 void * n = leaves[i];
1995 if( nodes_i < len_nodes && n == nodes[nodes_i]->GetItem().GetID() )
d5025dc0 1996 {
d47db7e0
RR
1997 wxDataViewTreeNode * nd = nodes[nodes_i];
1998 nodes_i++;
1999
d47db7e0
RR
2000 if( Walker( nd , func ) )
2001 return true;
2002
2003 }
2004 else
2005 switch( func( n ) )
2006 {
2007 case DoJob::OK :
2008 return true ;
2009 case DoJob::IGR:
2010 continue;
2011 case DoJob::CONT:
2012 default:
2013 ;
442c56e6 2014 }
aba9bfd0
RR
2015 }
2016 return false;
a0f3af5f
RR
2017}
2018
b7e9f8b1
RR
2019void wxDataViewMainWindow::SendModelEvent( wxEventType type, const wxDataViewItem & item )
2020{
2021 wxWindow *parent = GetParent();
2022 wxDataViewEvent le(type, parent->GetId());
2023
2024 le.SetEventObject(parent);
2025 le.SetModel(GetOwner()->GetModel());
2026 le.SetItem( item );
2027
2028 parent->GetEventHandler()->ProcessEvent(le);
2029}
2030
aba9bfd0 2031bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxDataViewItem & item)
a0f3af5f 2032{
66e09788 2033 SortPrepare();
d47db7e0 2034
351461fc
RR
2035 wxDataViewTreeNode * node;
2036 node = FindNode(parent);
b7e9f8b1 2037 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED, item );
351461fc
RR
2038
2039 if( node == NULL )
2040 return false;
2041
2042 node->SetHasChildren( true );
d47db7e0
RR
2043
2044 if( g_model->IsContainer( item ) )
2045 {
2046 wxDataViewTreeNode * newnode = new wxDataViewTreeNode( node );
2047 newnode->SetItem(item);
b7e9f8b1 2048 newnode->SetHasChildren( true );
d47db7e0
RR
2049 node->AddNode( newnode);
2050 }
2051 else
2052 node->AddLeaf( item.GetID() );
2053
2054 node->ChangeSubTreeCount(1);
2055
351461fc 2056 m_count = -1;
99d471a5 2057 UpdateDisplay();
fbda518c 2058
99d471a5 2059 return true;
a0f3af5f
RR
2060}
2061
d47db7e0 2062void DestroyTreeHelper( wxDataViewTreeNode * node);
704c3490 2063
32143117
VZ
2064bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent,
2065 const wxDataViewItem& item)
a0f3af5f 2066{
c59a09cf
RR
2067 wxDataViewTreeNode * node = FindNode(parent);
2068
2069 wxCHECK_MSG( node != NULL, false, "item not found" );
2070 wxCHECK_MSG( node->GetChildren().Index( item.GetID() ) != wxNOT_FOUND, false, "item not found" );
351461fc 2071
d47db7e0
RR
2072 int sub = -1;
2073 node->GetChildren().Remove( item.GetID() );
fbda518c
RR
2074 //Manuplate selection
2075 if( m_selection.GetCount() > 1 )
2076 {
fbda518c 2077 m_selection.Empty();
fbda518c 2078 }
24c4a50f
RR
2079 bool isContainer = false;
2080 wxDataViewTreeNodes nds = node->GetNodes();
2081 for (int i = 0; i < nds.GetCount(); i ++)
2082 {
2083 if (nds[i]->GetItem() == item)
2084 {
2085 isContainer = true;
2086 break;
2087 }
2088 }
2089 if( isContainer )
d47db7e0 2090 {
a8505db0 2091 wxDataViewTreeNode * n = NULL;
d47db7e0
RR
2092 wxDataViewTreeNodes nodes = node->GetNodes();
2093 int len = nodes.GetCount();
2094 for( int i = 0 ; i < len; i ++)
2095 {
2096 if( nodes[i]->GetItem() == item )
2097 {
2098 n = nodes[i];
2099 break;
2100 }
2101 }
a8505db0 2102
c59a09cf 2103 wxCHECK_MSG( n != NULL, false, "item not found" );
a8505db0 2104
d47db7e0
RR
2105 node->GetNodes().Remove( n );
2106 sub -= n->GetSubTreeCount();
2107 DestroyTreeHelper(n);
2108 }
d47db7e0 2109 //Make the row number invalid and get a new valid one when user call GetRowCount
442c56e6 2110 m_count = -1;
d47db7e0 2111 node->ChangeSubTreeCount(sub);
24c4a50f
RR
2112 if( node->GetChildrenNumber() == 0)
2113 {
2114 node->GetParent()->GetNodes().Remove( node );
2115 delete node;
2116 }
2117
d47db7e0
RR
2118 //Change the current row to the last row if the current exceed the max row number
2119 if( m_currentRow > GetRowCount() )
2120 m_currentRow = m_count - 1;
351461fc 2121
99d471a5 2122 UpdateDisplay();
c59a09cf
RR
2123
2124 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED, item);
2125
99d471a5 2126 return true;
a0f3af5f
RR
2127}
2128
aba9bfd0 2129bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem & item)
a0f3af5f 2130{
66e09788 2131 SortPrepare();
b7e9f8b1
RR
2132 g_model->Resort();
2133
2134 SendModelEvent(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED,item);
d47db7e0 2135
99d471a5 2136 return true;
a0f3af5f
RR
2137}
2138
b7e9f8b1 2139bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem & item, unsigned int col )
a0f3af5f 2140{
9861f022 2141 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
aba9bfd0 2142/*#define MAX_VIRTUAL_WIDTH 100000
9861f022
RR
2143
2144 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
0fdc2321
RR
2145 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2146 Refresh( true, &rect );
2147
2148 return true;
aba9bfd0 2149*/
66e09788 2150 SortPrepare();
b7e9f8b1
RR
2151 g_model->Resort();
2152
2153 //Send event
2154 wxWindow *parent = GetParent();
2155 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED, parent->GetId());
2156 le.SetEventObject(parent);
2157 le.SetModel(GetOwner()->GetModel());
2158 le.SetItem(item);
2159 le.SetColumn(col);
2160 le.SetDataViewColumn(GetOwner()->GetColumn(col));
2161 parent->GetEventHandler()->ProcessEvent(le);
d47db7e0 2162
0fcce6b9 2163 return true;
a0f3af5f
RR
2164}
2165
2166bool wxDataViewMainWindow::Cleared()
2167{
66e09788 2168 SortPrepare();
d47db7e0 2169
704c3490 2170 DestroyTree();
99d471a5 2171 UpdateDisplay();
b7e9f8b1
RR
2172
2173 wxWindow *parent = GetParent();
2174 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED, parent->GetId());
2175 le.SetEventObject(parent);
2176 le.SetModel(GetOwner()->GetModel());
2177 parent->GetEventHandler()->ProcessEvent(le);
2178
99d471a5 2179 return true;
a0f3af5f
RR
2180}
2181
4b3feaa7
RR
2182void wxDataViewMainWindow::UpdateDisplay()
2183{
2184 m_dirty = true;
2185}
2186
2187void wxDataViewMainWindow::OnInternalIdle()
2188{
2189 wxWindow::OnInternalIdle();
f554a14b 2190
4b3feaa7
RR
2191 if (m_dirty)
2192 {
2193 RecalculateDisplay();
2194 m_dirty = false;
2195 }
2196}
2197
2198void wxDataViewMainWindow::RecalculateDisplay()
2199{
aba9bfd0 2200 wxDataViewModel *model = GetOwner()->GetModel();
4b3feaa7
RR
2201 if (!model)
2202 {
2203 Refresh();
2204 return;
2205 }
f554a14b 2206
9861f022 2207 int width = GetEndOfLastCol();
aba9bfd0 2208 int height = GetRowCount() * m_lineHeight;
4b3feaa7
RR
2209
2210 SetVirtualSize( width, height );
2211 GetOwner()->SetScrollRate( 10, m_lineHeight );
f554a14b 2212
4b3feaa7
RR
2213 Refresh();
2214}
2215
2216void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
2217{
2218 wxWindow::ScrollWindow( dx, dy, rect );
9861f022
RR
2219
2220 if (GetOwner()->m_headerArea)
2221 GetOwner()->m_headerArea->ScrollWindow( dx, 0 );
4b3feaa7
RR
2222}
2223
fbda518c 2224void wxDataViewMainWindow::ScrollTo( int rows, int column )
b7e9f8b1
RR
2225{
2226 int x, y;
2227 m_owner->GetScrollPixelsPerUnit( &x, &y );
fbda518c
RR
2228 int sy = rows*m_lineHeight/y;
2229 int sx = 0;
2230 if( column != -1 )
2231 {
2232 wxRect rect = GetClientRect();
2233 unsigned int colnum = 0;
2234 unsigned int x_start = 0, x_end = 0, w = 0;
2235 int xx, yy, xe;
2236 m_owner->CalcUnscrolledPosition( rect.x, rect.y, &xx, &yy );
2237 for (x_start = 0; colnum < column; colnum++)
2238 {
2239 wxDataViewColumn *col = GetOwner()->GetColumn(colnum);
2240 if (col->IsHidden())
2241 continue; // skip it!
2242
2243 w = col->GetWidth();
2244 x_start += w;
2245 }
2246
2247 x_end = x_start + w;
2248 xe = xx + rect.width;
2249 if( x_end > xe )
2250 {
2251 sx = ( xx + x_end - xe )/x;
2252 }
2253 if( x_start < xx )
2254 {
2255 sx = x_start/x;
2256 }
2257 }
2258 m_owner->Scroll( sx, sy );
b7e9f8b1
RR
2259}
2260
f554a14b 2261void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
4ed7af08 2262{
aba9bfd0 2263 wxDataViewModel *model = GetOwner()->GetModel();
2e992e06
VZ
2264 wxAutoBufferedPaintDC dc( this );
2265
9861f022 2266 // prepare the DC
2e992e06
VZ
2267 dc.SetBackground(GetBackgroundColour());
2268 dc.Clear();
4b3feaa7 2269 GetOwner()->PrepareDC( dc );
4ed7af08 2270 dc.SetFont( GetFont() );
90675b95
RR
2271
2272 wxRect update = GetUpdateRegion().GetBox();
2273 m_owner->CalcUnscrolledPosition( update.x, update.y, &update.x, &update.y );
f554a14b 2274
9861f022 2275 // compute which items needs to be redrawn
0a71f9e9 2276 unsigned int item_start = wxMax( 0, (update.y / m_lineHeight) );
c741d33f 2277 unsigned int item_count =
87f0efe2 2278 wxMin( (int)(((update.y + update.height) / m_lineHeight) - item_start + 1),
3b6280be 2279 (int)(GetRowCount( )- item_start) );
9861f022
RR
2280 unsigned int item_last = item_start + item_count;
2281
2282 // compute which columns needs to be redrawn
2283 unsigned int cols = GetOwner()->GetColumnCount();
2284 unsigned int col_start = 0;
2285 unsigned int x_start = 0;
2286 for (x_start = 0; col_start < cols; col_start++)
2287 {
2288 wxDataViewColumn *col = GetOwner()->GetColumn(col_start);
2289 if (col->IsHidden())
2290 continue; // skip it!
2291
2292 unsigned int w = col->GetWidth();
2293 if (x_start+w >= (unsigned int)update.x)
2294 break;
2295
2296 x_start += w;
2297 }
90675b95 2298
9861f022
RR
2299 unsigned int col_last = col_start;
2300 unsigned int x_last = x_start;
2301 for (; col_last < cols; col_last++)
2302 {
2303 wxDataViewColumn *col = GetOwner()->GetColumn(col_last);
2304 if (col->IsHidden())
2305 continue; // skip it!
2306
2307 if (x_last > (unsigned int)update.GetRight())
2308 break;
2309
2310 x_last += col->GetWidth();
2311 }
2312
2313 // Draw horizontal rules if required
2314 if ( m_owner->HasFlag(wxDV_HORIZ_RULES) )
2315 {
2316 dc.SetPen(m_penRule);
2317 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2318
2319 for (unsigned int i = item_start; i <= item_last+1; i++)
2320 {
2321 int y = i * m_lineHeight;
2322 dc.DrawLine(x_start, y, x_last, y);
2323 }
2324 }
2325
2326 // Draw vertical rules if required
2327 if ( m_owner->HasFlag(wxDV_VERT_RULES) )
2328 {
2329 dc.SetPen(m_penRule);
2330 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2331
2332 int x = x_start;
2333 for (unsigned int i = col_start; i < col_last; i++)
2334 {
2335 wxDataViewColumn *col = GetOwner()->GetColumn(i);
2336 if (col->IsHidden())
2337 continue; // skip it
2338
2339 dc.DrawLine(x, item_start * m_lineHeight,
2340 x, item_last * m_lineHeight);
2341
2342 x += col->GetWidth();
2343 }
2344
2345 // Draw last vertical rule
c741d33f 2346 dc.DrawLine(x, item_start * m_lineHeight,
9861f022
RR
2347 x, item_last * m_lineHeight);
2348 }
2349
2350 // redraw the background for the items which are selected/current
2351 for (unsigned int item = item_start; item < item_last; item++)
cab07038 2352 {
87f0efe2
RR
2353 bool selected = m_selection.Index( item ) != wxNOT_FOUND;
2354 if (selected || item == m_currentRow)
cab07038 2355 {
5d3f234b 2356 int flags = selected ? (int)wxCONTROL_SELECTED : 0;
daebb44c
RR
2357 if (item == m_currentRow)
2358 flags |= wxCONTROL_CURRENT;
2359 if (m_hasFocus)
2360 flags |= wxCONTROL_FOCUSED;
9861f022
RR
2361
2362 wxRect rect( x_start, item*m_lineHeight, x_last, m_lineHeight );
daebb44c
RR
2363 wxRendererNative::Get().DrawItemSelectionRect
2364 (
2365 this,
2366 dc,
2367 rect,
2368 flags
2369 );
2370 }
cab07038 2371 }
120b9b05 2372
e887bad5
RR
2373 wxDataViewColumn *expander = GetOwner()->GetExpanderColumn();
2374 if (!expander)
2375 {
2376 // TODO: last column for RTL support
2377 expander = GetOwner()->GetColumn( 0 );
24c4a50f 2378 GetOwner()->SetExpanderColumn(expander);
e887bad5
RR
2379 }
2380
9861f022 2381 // redraw all cells for all rows which must be repainted and for all columns
90675b95 2382 wxRect cell_rect;
9861f022
RR
2383 cell_rect.x = x_start;
2384 cell_rect.height = m_lineHeight; // -1 is for the horizontal rules
2385 for (unsigned int i = col_start; i < col_last; i++)
90675b95
RR
2386 {
2387 wxDataViewColumn *col = GetOwner()->GetColumn( i );
baa9ebc4 2388 wxDataViewRenderer *cell = col->GetRenderer();
90675b95 2389 cell_rect.width = col->GetWidth();
f554a14b 2390
87f0efe2
RR
2391 if (col->IsHidden())
2392 continue; // skipt it!
2393
e887bad5 2394
9861f022 2395 for (unsigned int item = item_start; item < item_last; item++)
90675b95 2396 {
87f0efe2 2397 // get the cell value and set it into the renderer
90675b95 2398 wxVariant value;
b7e9f8b1
RR
2399 wxDataViewTreeNode * node = GetTreeNodeByRow(item);
2400 if( node == NULL )
2401 {
2402 continue;
2403 }
442c56e6 2404
3b6280be 2405 wxDataViewItem dataitem = node->GetItem();
1b27b2bd 2406 model->GetValue( value, dataitem, col->GetModelColumn());
90675b95 2407 cell->SetValue( value );
87f0efe2
RR
2408
2409 // update the y offset
9861f022 2410 cell_rect.y = item * m_lineHeight;
87f0efe2 2411
442c56e6 2412 //Draw the expander here.
3b6280be 2413 int indent = node->GetIndentLevel();
e887bad5 2414 if( col == expander )
3b6280be
RR
2415 {
2416 //Calculate the indent first
d5025dc0
RR
2417 indent = cell_rect.x + GetOwner()->GetIndent() * indent;
2418
3b6280be 2419 int expander_width = m_lineHeight - 2*EXPANDER_MARGIN;
d5025dc0
RR
2420 // change the cell_rect.x to the appropriate pos
2421 int expander_x = indent + EXPANDER_MARGIN , expander_y = cell_rect.y + EXPANDER_MARGIN ;
3b6280be
RR
2422 indent = indent + m_lineHeight ; //try to use the m_lineHeight as the expander space
2423 dc.SetPen( m_penExpander );
d5025dc0
RR
2424 dc.SetBrush( wxNullBrush );
2425 if( node->HasChildren() )
2426 {
d5025dc0 2427 wxRect rect( expander_x , expander_y, expander_width, expander_width);
24c4a50f
RR
2428 int flag = 0;
2429 if (m_underMouse == node)
2430 {
2431 flag |= wxCONTROL_CURRENT;
2432 }
d5025dc0 2433 if( node->IsOpen() )
24c4a50f 2434 wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, flag|wxCONTROL_EXPANDED );
d5025dc0 2435 else
24c4a50f 2436 wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, flag);
d5025dc0
RR
2437 }
2438 else
2439 {
d47db7e0
RR
2440 // I am wandering whether we should draw dot lines between tree nodes
2441 delete node;
2442 //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
2443 }
2444
2445 //force the expander column to left-center align
2446 cell->SetAlignment( wxALIGN_CENTER_VERTICAL );
3b6280be 2447 }
3b6280be 2448
442c56e6
VZ
2449
2450 // cannot be bigger than allocated space
87f0efe2 2451 wxSize size = cell->GetSize();
d5025dc0 2452 // Because of the tree structure indent, here we should minus the width of the cell for drawing
3b6280be 2453 size.x = wxMin( size.x + 2*PADDING_RIGHTLEFT, cell_rect.width - indent );
9861f022 2454 size.y = wxMin( size.y + 2*PADDING_TOPBOTTOM, cell_rect.height );
87f0efe2
RR
2455
2456 wxRect item_rect(cell_rect.GetTopLeft(), size);
9861f022 2457 int align = cell->GetAlignment();
87f0efe2
RR
2458
2459 // horizontal alignment:
9861f022
RR
2460 item_rect.x = cell_rect.x;
2461 if (align & wxALIGN_CENTER_HORIZONTAL)
2462 item_rect.x = cell_rect.x + (cell_rect.width / 2) - (size.x / 2);
2463 else if (align & wxALIGN_RIGHT)
87f0efe2
RR
2464 item_rect.x = cell_rect.x + cell_rect.width - size.x;
2465 //else: wxALIGN_LEFT is the default
2466
2467 // vertical alignment:
9861f022
RR
2468 item_rect.y = cell_rect.y;
2469 if (align & wxALIGN_CENTER_VERTICAL)
87f0efe2 2470 item_rect.y = cell_rect.y + (cell_rect.height / 2) - (size.y / 2);
9861f022 2471 else if (align & wxALIGN_BOTTOM)
87f0efe2
RR
2472 item_rect.y = cell_rect.y + cell_rect.height - size.y;
2473 //else: wxALIGN_TOP is the default
2474
9861f022
RR
2475 // add padding
2476 item_rect.x += PADDING_RIGHTLEFT;
2477 item_rect.y += PADDING_TOPBOTTOM;
2478 item_rect.width = size.x - 2 * PADDING_RIGHTLEFT;
2479 item_rect.height = size.y - 2 * PADDING_TOPBOTTOM;
64b3c262 2480
3b6280be
RR
2481 //Here we add the tree indent
2482 item_rect.x += indent;
442c56e6 2483
64b3c262 2484 int state = 0;
87f0efe2 2485 if (m_selection.Index(item) != wxNOT_FOUND)
64b3c262 2486 state |= wxDATAVIEW_CELL_SELECTED;
87f0efe2 2487
9861f022
RR
2488 // TODO: it would be much more efficient to create a clipping
2489 // region for the entire column being rendered (in the OnPaint
2490 // of wxDataViewMainWindow) instead of a single clip region for
2491 // each cell. However it would mean that each renderer should
2492 // respect the given wxRect's top & bottom coords, eventually
2493 // violating only the left & right coords - however the user can
2494 // make its own renderer and thus we cannot be sure of that.
2495 dc.SetClippingRegion( item_rect );
64b3c262 2496 cell->Render( item_rect, &dc, state );
9861f022 2497 dc.DestroyClippingRegion();
90675b95 2498 }
f554a14b 2499
90675b95
RR
2500 cell_rect.x += cell_rect.width;
2501 }
4ed7af08
RR
2502}
2503
9861f022 2504int wxDataViewMainWindow::GetCountPerPage() const
cab07038
RR
2505{
2506 wxSize size = GetClientSize();
2507 return size.y / m_lineHeight;
2508}
2509
9861f022 2510int wxDataViewMainWindow::GetEndOfLastCol() const
e21f75bd
RR
2511{
2512 int width = 0;
0a71f9e9 2513 unsigned int i;
9861f022 2514 for (i = 0; i < GetOwner()->GetColumnCount(); i++)
e21f75bd 2515 {
c741d33f 2516 const wxDataViewColumn *c =
9861f022
RR
2517 wx_const_cast(wxDataViewCtrl*, GetOwner())->GetColumn( i );
2518
2519 if (!c->IsHidden())
2520 width += c->GetWidth();
e21f75bd
RR
2521 }
2522 return width;
2523}
2524
9861f022 2525unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
72664514
RR
2526{
2527 int x = 0;
2528 int y = 0;
2529 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
120b9b05 2530
72664514
RR
2531 return y / m_lineHeight;
2532}
2533
442c56e6 2534unsigned int wxDataViewMainWindow::GetLastVisibleRow()
72664514
RR
2535{
2536 wxSize client_size = GetClientSize();
c741d33f 2537 m_owner->CalcUnscrolledPosition( client_size.x, client_size.y,
87f0efe2 2538 &client_size.x, &client_size.y );
72664514 2539
fbda518c
RR
2540 //we should deal with the pixel here
2541 unsigned int row = (client_size.y)/m_lineHeight;
2542 if( client_size.y % m_lineHeight < m_lineHeight/2 )
2543 row -= 1;
2544
2545 return wxMin( GetRowCount()-1, row );
72664514
RR
2546}
2547
442c56e6 2548unsigned int wxDataViewMainWindow::GetRowCount()
cab07038 2549{
3b6280be
RR
2550 if ( m_count == -1 )
2551 {
2552 m_count = RecalculateCount();
d5025dc0
RR
2553 int width, height;
2554 GetVirtualSize( &width, &height );
3b6280be
RR
2555 height = m_count * m_lineHeight;
2556
2557 SetVirtualSize( width, height );
2558 }
aba9bfd0 2559 return m_count;
cab07038
RR
2560}
2561
0a71f9e9 2562void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row )
e21f75bd
RR
2563{
2564 m_currentRow = row;
120b9b05 2565
e21f75bd
RR
2566 // send event
2567}
2568
cab07038
RR
2569void wxDataViewMainWindow::SelectAllRows( bool on )
2570{
4a851b11
VZ
2571 if (IsEmpty())
2572 return;
120b9b05 2573
cab07038
RR
2574 if (on)
2575 {
72664514 2576 m_selection.Clear();
0a71f9e9 2577 for (unsigned int i = 0; i < GetRowCount(); i++)
cab07038 2578 m_selection.Add( i );
72664514
RR
2579 Refresh();
2580 }
2581 else
2582 {
0a71f9e9
RR
2583 unsigned int first_visible = GetFirstVisibleRow();
2584 unsigned int last_visible = GetLastVisibleRow();
2585 unsigned int i;
72664514 2586 for (i = 0; i < m_selection.GetCount(); i++)
120b9b05 2587 {
0a71f9e9 2588 unsigned int row = m_selection[i];
72664514
RR
2589 if ((row >= first_visible) && (row <= last_visible))
2590 RefreshRow( row );
2591 }
2592 m_selection.Clear();
cab07038 2593 }
cab07038
RR
2594}
2595
0a71f9e9 2596void wxDataViewMainWindow::SelectRow( unsigned int row, bool on )
cab07038
RR
2597{
2598 if (m_selection.Index( row ) == wxNOT_FOUND)
2599 {
2600 if (on)
2601 {
2602 m_selection.Add( row );
2603 RefreshRow( row );
2604 }
2605 }
2606 else
2607 {
2608 if (!on)
2609 {
2610 m_selection.Remove( row );
2611 RefreshRow( row );
2612 }
2613 }
2614}
2615
0a71f9e9 2616void wxDataViewMainWindow::SelectRows( unsigned int from, unsigned int to, bool on )
cab07038
RR
2617{
2618 if (from > to)
2619 {
0a71f9e9 2620 unsigned int tmp = from;
cab07038
RR
2621 from = to;
2622 to = tmp;
2623 }
2624
0a71f9e9 2625 unsigned int i;
cab07038
RR
2626 for (i = from; i <= to; i++)
2627 {
2628 if (m_selection.Index( i ) == wxNOT_FOUND)
2629 {
2630 if (on)
2631 m_selection.Add( i );
2632 }
2633 else
2634 {
2635 if (!on)
2636 m_selection.Remove( i );
2637 }
2638 }
2639 RefreshRows( from, to );
2640}
2641
87f0efe2
RR
2642void wxDataViewMainWindow::Select( const wxArrayInt& aSelections )
2643{
2644 for (size_t i=0; i < aSelections.GetCount(); i++)
2645 {
2646 int n = aSelections[i];
2647
2648 m_selection.Add( n );
2649 RefreshRow( n );
2650 }
2651}
2652
0a71f9e9 2653void wxDataViewMainWindow::ReverseRowSelection( unsigned int row )
cab07038
RR
2654{
2655 if (m_selection.Index( row ) == wxNOT_FOUND)
2656 m_selection.Add( row );
2657 else
2658 m_selection.Remove( row );
120b9b05 2659 RefreshRow( row );
cab07038
RR
2660}
2661
0a71f9e9 2662bool wxDataViewMainWindow::IsRowSelected( unsigned int row )
cab07038
RR
2663{
2664 return (m_selection.Index( row ) != wxNOT_FOUND);
2665}
2666
0a71f9e9 2667void wxDataViewMainWindow::RefreshRow( unsigned int row )
cab07038 2668{
e21f75bd 2669 wxRect rect( 0, row*m_lineHeight, GetEndOfLastCol(), m_lineHeight );
cab07038 2670 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
120b9b05 2671
cab07038
RR
2672 wxSize client_size = GetClientSize();
2673 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2674 wxRect intersect_rect = client_rect.Intersect( rect );
2675 if (intersect_rect.width > 0)
2676 Refresh( true, &intersect_rect );
2677}
2678
0a71f9e9 2679void wxDataViewMainWindow::RefreshRows( unsigned int from, unsigned int to )
cab07038
RR
2680{
2681 if (from > to)
2682 {
0a71f9e9 2683 unsigned int tmp = to;
cab07038
RR
2684 to = from;
2685 from = tmp;
2686 }
2687
e21f75bd 2688 wxRect rect( 0, from*m_lineHeight, GetEndOfLastCol(), (to-from+1) * m_lineHeight );
cab07038 2689 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
120b9b05 2690
cab07038
RR
2691 wxSize client_size = GetClientSize();
2692 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2693 wxRect intersect_rect = client_rect.Intersect( rect );
2694 if (intersect_rect.width > 0)
2695 Refresh( true, &intersect_rect );
2696}
2697
0a71f9e9 2698void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow )
cab07038 2699{
0a71f9e9 2700 unsigned int count = GetRowCount();
4a851b11
VZ
2701 if (firstRow > count)
2702 return;
120b9b05 2703
e21f75bd 2704 wxRect rect( 0, firstRow*m_lineHeight, GetEndOfLastCol(), count * m_lineHeight );
cab07038 2705 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
120b9b05 2706
cab07038
RR
2707 wxSize client_size = GetClientSize();
2708 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2709 wxRect intersect_rect = client_rect.Intersect( rect );
2710 if (intersect_rect.width > 0)
2711 Refresh( true, &intersect_rect );
2712}
2713
0a71f9e9 2714void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event)
cab07038 2715{
4a851b11 2716 wxCHECK_RET( newCurrent < GetRowCount(),
cab07038
RR
2717 _T("invalid item index in OnArrowChar()") );
2718
2719 // if there is no selection, we cannot move it anywhere
2720 if (!HasCurrentRow())
2721 return;
2722
0a71f9e9 2723 unsigned int oldCurrent = m_currentRow;
cab07038
RR
2724
2725 // in single selection we just ignore Shift as we can't select several
2726 // items anyhow
e21f75bd 2727 if ( event.ShiftDown() && !IsSingleSel() )
cab07038
RR
2728 {
2729 RefreshRow( oldCurrent );
120b9b05 2730
e21f75bd 2731 ChangeCurrentRow( newCurrent );
cab07038
RR
2732
2733 // select all the items between the old and the new one
2734 if ( oldCurrent > newCurrent )
2735 {
2736 newCurrent = oldCurrent;
2737 oldCurrent = m_currentRow;
2738 }
2739
2740 SelectRows( oldCurrent, newCurrent, true );
2741 }
2742 else // !shift
2743 {
72664514 2744 RefreshRow( oldCurrent );
120b9b05 2745
cab07038
RR
2746 // all previously selected items are unselected unless ctrl is held
2747 if ( !event.ControlDown() )
2748 SelectAllRows(false);
2749
e21f75bd 2750 ChangeCurrentRow( newCurrent );
cab07038
RR
2751
2752 if ( !event.ControlDown() )
2753 SelectRow( m_currentRow, true );
72664514
RR
2754 else
2755 RefreshRow( m_currentRow );
cab07038
RR
2756 }
2757
9861f022
RR
2758 //EnsureVisible( m_currentRow );
2759}
2760
2761wxRect wxDataViewMainWindow::GetLineRect( unsigned int row ) const
2762{
2763 wxRect rect;
2764 rect.x = 0;
2765 rect.y = m_lineHeight * row;
2766 rect.width = GetEndOfLastCol();
2767 rect.height = m_lineHeight;
2768
2769 return rect;
cab07038
RR
2770}
2771
aba9bfd0
RR
2772class RowToItemJob: public DoJob
2773{
2774public:
2775 RowToItemJob( unsigned int row , int current ) { this->row = row; this->current = current ;}
2776 virtual ~RowToItemJob(){};
2777
d5025dc0 2778 virtual int operator() ( wxDataViewTreeNode * node )
d47db7e0
RR
2779 {
2780 current ++;
2781 if( current == static_cast<int>(row))
b7e9f8b1 2782 {
d47db7e0
RR
2783 ret = node->GetItem() ;
2784 return DoJob::OK;
2785 }
d5025dc0 2786
d47db7e0
RR
2787 if( node->GetSubTreeCount() + current < static_cast<int>(row) )
2788 {
2789 current += node->GetSubTreeCount();
2790 return DoJob::IGR;
2791 }
2792 else
b7e9f8b1
RR
2793 {
2794 //If the current has no child node, we can find the desired item of the row number directly.
2795 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2796 if( node->GetNodes().GetCount() == 0)
2797 {
2798 int index = static_cast<int>(row) - current - 1;
2799 ret = node->GetChildren().Item( index );
2800 return DoJob::OK;
2801 }
d47db7e0 2802 return DoJob::CONT;
b7e9f8b1 2803 }
d47db7e0
RR
2804 }
2805
2806 virtual int operator() ( void * n )
2807 {
2808 current ++;
2809 if( current == static_cast<int>(row))
b7e9f8b1 2810 {
d47db7e0
RR
2811 ret = wxDataViewItem( n ) ;
2812 return DoJob::OK;
2813 }
2814 return DoJob::CONT;
2815 }
d5025dc0 2816 wxDataViewItem GetResult(){ return ret; }
aba9bfd0
RR
2817private:
2818 unsigned int row;
2819 int current ;
2820 wxDataViewItem ret;
2821};
2822
fbda518c 2823wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row) const
aba9bfd0 2824{
b7e9f8b1 2825 RowToItemJob job( row, -2 );
aba9bfd0
RR
2826 Walker( m_root , job );
2827 return job.GetResult();
2828}
2829
3b6280be
RR
2830class RowToTreeNodeJob: public DoJob
2831{
2832public:
442c56e6
VZ
2833 RowToTreeNodeJob( unsigned int row , int current, wxDataViewTreeNode * node )
2834 {
2835 this->row = row;
2836 this->current = current ;
d47db7e0
RR
2837 ret = NULL ;
2838 parent = node;
2839 }
d5025dc0 2840 virtual ~RowToTreeNodeJob(){};
3b6280be
RR
2841
2842 virtual int operator() ( wxDataViewTreeNode * node )
d5025dc0 2843 {
d47db7e0 2844 current ++;
704c3490 2845 if( current == static_cast<int>(row))
b7e9f8b1 2846 {
d5025dc0
RR
2847 ret = node ;
2848 return DoJob::OK;
3b6280be 2849 }
d47db7e0
RR
2850
2851 if( node->GetSubTreeCount() + current < static_cast<int>(row) )
2852 {
2853 current += node->GetSubTreeCount();
2854 return DoJob::IGR;
2855 }
d5025dc0 2856 else
d47db7e0
RR
2857 {
2858 parent = node;
b7e9f8b1
RR
2859 //If the current has no child node, we can find the desired item of the row number directly.
2860 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2861 if( node->GetNodes().GetCount() == 0)
2862 {
2863 int index = static_cast<int>(row) - current - 1;
2864 void * n = node->GetChildren().Item( index );
2865 ret = new wxDataViewTreeNode( parent ) ;
2866 ret->SetItem( wxDataViewItem( n ));
2867 ret->SetHasChildren(false);
2868 return DoJob::OK;
2869 }
d47db7e0
RR
2870 return DoJob::CONT;
2871 }
2872
b7e9f8b1 2873
d5025dc0 2874 }
3b6280be 2875
d47db7e0
RR
2876 virtual int operator() ( void * n )
2877 {
2878 current ++;
2879 if( current == static_cast<int>(row))
b7e9f8b1 2880 {
d47db7e0
RR
2881 ret = new wxDataViewTreeNode( parent ) ;
2882 ret->SetItem( wxDataViewItem( n ));
2883 ret->SetHasChildren(false);
2884 return DoJob::OK;
2885 }
442c56e6 2886
d47db7e0
RR
2887 return DoJob::CONT;
2888 }
3b6280be
RR
2889 wxDataViewTreeNode * GetResult(){ return ret; }
2890private:
2891 unsigned int row;
2892 int current ;
2893 wxDataViewTreeNode * ret;
d47db7e0 2894 wxDataViewTreeNode * parent ;
3b6280be
RR
2895};
2896
2897
2898wxDataViewTreeNode * wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row)
2899{
b7e9f8b1 2900 RowToTreeNodeJob job( row , -2, m_root );
3b6280be
RR
2901 Walker( m_root , job );
2902 return job.GetResult();
2903}
2904
66e09788
RR
2905wxDataViewEvent wxDataViewMainWindow::SendExpanderEvent( wxEventType type, const wxDataViewItem & item )
2906{
2907 wxWindow *parent = GetParent();
2908 wxDataViewEvent le(type, parent->GetId());
2909
2910 le.SetEventObject(parent);
2911 le.SetModel(GetOwner()->GetModel());
2912 le.SetItem( item );
2913
2914 parent->GetEventHandler()->ProcessEvent(le);
2915 return le;
2916}
2917
3b6280be
RR
2918void wxDataViewMainWindow::OnExpanding( unsigned int row )
2919{
2920 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
2921 if( node != NULL )
2922 {
d5025dc0 2923 if( node->HasChildren())
66e09788 2924 {
d5025dc0 2925 if( !node->IsOpen())
3b6280be 2926 {
66e09788
RR
2927 wxDataViewEvent e = SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING,node->GetItem());
2928 //Check if the user prevent expanding
2929 if( e.GetSkipped() )
2930 return;
2931
d5025dc0 2932 node->ToggleOpen();
704c3490
RR
2933 //Here I build the children of current node
2934 if( node->GetChildrenNumber() == 0 )
24c4a50f
RR
2935 {
2936 SortPrepare();
704c3490 2937 BuildTreeHelper(GetOwner()->GetModel(), node->GetItem(), node);
24c4a50f 2938 }
d5025dc0 2939 m_count = -1;
351461fc 2940 UpdateDisplay();
66e09788
RR
2941 //Send the expanded event
2942 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED,node->GetItem());
351461fc
RR
2943 }
2944 else
2945 {
2946 SelectRow( row, false );
2947 SelectRow( row + 1, true );
2948 ChangeCurrentRow( row + 1 );
3b6280be 2949 }
66e09788 2950 }
d47db7e0
RR
2951 else
2952 delete node;
3b6280be
RR
2953 }
2954}
2955
2956void wxDataViewMainWindow::OnCollapsing(unsigned int row)
2957{
2958 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
d5025dc0 2959 if( node != NULL )
3b6280be 2960 {
d47db7e0
RR
2961 wxDataViewTreeNode * nd = node;
2962
3b6280be
RR
2963 if( node->HasChildren() && node->IsOpen() )
2964 {
66e09788
RR
2965 wxDataViewEvent e = SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING,node->GetItem());
2966 if( e.GetSkipped() )
2967 return;
3b6280be
RR
2968 node->ToggleOpen();
2969 m_count = -1;
351461fc 2970 UpdateDisplay();
66e09788
RR
2971 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED,nd->GetItem());
2972 }
2973 else
2974 {
2975 node = node->GetParent();
2976 if( node != NULL )
2977 {
2978 int parent = GetRowByItem( node->GetItem() ) ;
2979 if( parent >= 0 )
2980 {
2981 SelectRow( row, false);
2982 SelectRow(parent , true );
2983 ChangeCurrentRow( parent );
2984 }
2985 }
2986 }
2987 if( !nd->HasChildren())
2988 delete nd;
3b6280be
RR
2989 }
2990}
2991
351461fc
RR
2992wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item )
2993{
2994 wxDataViewModel * model = GetOwner()->GetModel();
2995 if( model == NULL )
2996 return NULL;
2997
2998 //Compose the a parent-chain of the finding item
2999 ItemList list;
3000 list.DeleteContents( true );
3001 wxDataViewItem it( item );
3002 while( it.IsOk() )
3003 {
3004 wxDataViewItem * pItem = new wxDataViewItem( it );
3005 list.Insert( pItem );
3006 it = model->GetParent( it );
3007 }
3008
442c56e6 3009 //Find the item along the parent-chain.
351461fc 3010 //This algorithm is designed to speed up the node-finding method
351461fc
RR
3011 wxDataViewTreeNode * node = m_root;
3012 for( ItemList::Node * n = list.GetFirst(); n; n = n->GetNext() )
3013 {
3014 if( node->HasChildren() )
3015 {
3016 if( node->GetChildrenNumber() == 0 )
24c4a50f
RR
3017 {
3018 SortPrepare();
351461fc 3019 BuildTreeHelper(model, node->GetItem(), node);
24c4a50f 3020 }
351461fc 3021
d47db7e0 3022 wxDataViewTreeNodes nodes = node->GetNodes();
d92cb015
RR
3023 int i = 0;
3024 for (; i < nodes.GetCount(); i ++)
3025 {
3026 if (nodes[i]->GetItem() == *(n->GetData()))
3027 {
3028 node = nodes[i];
3029 break;
3030 }
3031 }
3032 if (i == nodes.GetCount())
351461fc 3033 return NULL;
351461fc
RR
3034 }
3035 else
3036 return NULL;
3037 }
3038 return node;
3039}
3040
a87b466d 3041void wxDataViewMainWindow::HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column )
66e09788 3042{
fbda518c 3043 wxDataViewColumn *col = NULL;
66e09788
RR
3044 unsigned int cols = GetOwner()->GetColumnCount();
3045 unsigned int colnum = 0;
3046 unsigned int x_start = 0;
3047 int x, y;
3048 m_owner->CalcUnscrolledPosition( point.x, point.y, &x, &y );
3049 for (x_start = 0; colnum < cols; colnum++)
3050 {
fbda518c 3051 col = GetOwner()->GetColumn(colnum);
66e09788
RR
3052 if (col->IsHidden())
3053 continue; // skip it!
3054
3055 unsigned int w = col->GetWidth();
3056 if (x_start+w >= (unsigned int)x)
3057 break;
3058
3059 x_start += w;
3060 }
3061
fbda518c 3062 column = col;
66e09788
RR
3063 item = GetItemByRow( y/m_lineHeight );
3064}
3065
fbda518c 3066wxRect wxDataViewMainWindow::GetItemRect( const wxDataViewItem & item, const wxDataViewColumn* column )
66e09788
RR
3067{
3068 int row = GetRowByItem(item);
3069 int y = row*m_lineHeight;
3070 int h = m_lineHeight;
3071 int x = 0;
3072 wxDataViewColumn *col = NULL;
3073 for( int i = 0, cols = GetOwner()->GetColumnCount(); i < cols; i ++ )
3074 {
3075 col = GetOwner()->GetColumn( i );
3076 x += col->GetWidth();
fbda518c 3077 if( GetOwner()->GetColumn(i+1) == column )
66e09788
RR
3078 break;
3079 }
3080 int w = col->GetWidth();
3081 m_owner->CalcScrolledPosition( x, y, &x, &y );
3082 return wxRect(x, y, w, h);
3083}
3084
442c56e6 3085int wxDataViewMainWindow::RecalculateCount()
3b6280be 3086{
d47db7e0 3087 return m_root->GetSubTreeCount();
3b6280be
RR
3088}
3089
aba9bfd0
RR
3090class ItemToRowJob : public DoJob
3091{
3092public:
d47db7e0 3093 ItemToRowJob(const wxDataViewItem & item, ItemList::Node * node )
b7e9f8b1 3094 { this->item = item ; ret = -1 ; nd = node ; }
aba9bfd0
RR
3095 virtual ~ItemToRowJob(){};
3096
b7e9f8b1 3097 //Maybe binary search will help to speed up this process
d5025dc0
RR
3098 virtual int operator() ( wxDataViewTreeNode * node)
3099 {
3100 ret ++;
3101 if( node->GetItem() == item )
d47db7e0 3102 {
d5025dc0 3103 return DoJob::OK;
d47db7e0 3104 }
d5025dc0 3105
d47db7e0
RR
3106 if( nd && node->GetItem() == *(nd->GetData()))
3107 {
3108 nd = nd->GetNext();
d5025dc0 3109 return DoJob::CONT;
d47db7e0 3110 }
d5025dc0 3111 else
d47db7e0
RR
3112 {
3113 ret += node->GetSubTreeCount();
d5025dc0 3114 return DoJob::IGR;
d47db7e0 3115 }
442c56e6 3116
d5025dc0 3117 }
aba9bfd0 3118
d47db7e0
RR
3119 virtual int operator() ( void * n )
3120 {
3121 ret ++;
3122 if( n == item.GetID() )
3123 return DoJob::OK;
3124 return DoJob::CONT;
3125 }
3b6280be
RR
3126 //the row number is begin from zero
3127 int GetResult(){ return ret -1 ; }
aba9bfd0 3128private:
d47db7e0 3129 ItemList::Node * nd;
aba9bfd0
RR
3130 wxDataViewItem item;
3131 int ret;
442c56e6 3132
aba9bfd0
RR
3133};
3134
b7e9f8b1 3135int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item)
aba9bfd0 3136{
d47db7e0
RR
3137 wxDataViewModel * model = GetOwner()->GetModel();
3138 if( model == NULL )
fbda518c 3139 return -1;
d47db7e0 3140
b7e9f8b1
RR
3141 if( !item.IsOk() )
3142 return -1;
3143
d47db7e0
RR
3144 //Compose the a parent-chain of the finding item
3145 ItemList list;
b7e9f8b1 3146 wxDataViewItem * pItem = NULL;
d47db7e0
RR
3147 list.DeleteContents( true );
3148 wxDataViewItem it( item );
3149 while( it.IsOk() )
3150 {
b7e9f8b1 3151 pItem = new wxDataViewItem( it );
d47db7e0
RR
3152 list.Insert( pItem );
3153 it = model->GetParent( it );
3154 }
b7e9f8b1
RR
3155 pItem = new wxDataViewItem( );
3156 list.Insert( pItem );
d47db7e0
RR
3157
3158 ItemToRowJob job( item, list.GetFirst() );
aba9bfd0
RR
3159 Walker(m_root , job );
3160 return job.GetResult();
3161}
3162
704c3490 3163void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, wxDataViewTreeNode * node)
aba9bfd0 3164{
351461fc 3165 if( !model->IsContainer( item ) )
704c3490 3166 return ;
442c56e6 3167
aba9bfd0 3168 wxDataViewItem i = model->GetFirstChild( item );
d47db7e0 3169 int num = 0;
aba9bfd0
RR
3170 while( i.IsOk() )
3171 {
d47db7e0
RR
3172 num ++;
3173 if( model->IsContainer( i ) )
3174 {
3175 wxDataViewTreeNode * n = new wxDataViewTreeNode( node );
3176 n->SetItem(i);
3177 n->SetHasChildren( true ) ;
3178 node->AddNode( n );
3179 }
3180 else
3181 {
3182 node->AddLeaf( i.GetID() );
3183 }
3b6280be 3184 i = model->GetNextSibling( i );
aba9bfd0 3185 }
d47db7e0
RR
3186 node->SetSubTreeCount( num );
3187 wxDataViewTreeNode * n = node->GetParent();
3188 if( n != NULL)
3189 n->ChangeSubTreeCount(num);
3190
aba9bfd0
RR
3191}
3192
3193void wxDataViewMainWindow::BuildTree(wxDataViewModel * model)
3194{
3195 //First we define a invalid item to fetch the top-level elements
3196 wxDataViewItem item;
66e09788 3197 SortPrepare();
3b6280be
RR
3198 BuildTreeHelper( model, item, m_root);
3199 m_count = -1 ;
aba9bfd0
RR
3200}
3201
3202void DestroyTreeHelper( wxDataViewTreeNode * node )
3203{
d47db7e0 3204 if( node->GetNodeNumber() != 0 )
aba9bfd0 3205 {
d47db7e0 3206 int len = node->GetNodeNumber();
3b6280be 3207 int i = 0 ;
d47db7e0 3208 wxDataViewTreeNodes nodes = node->GetNodes();
3b6280be 3209 for( ; i < len; i ++ )
aba9bfd0
RR
3210 {
3211 DestroyTreeHelper(nodes[i]);
3212 }
3213 }
3214 delete node;
3215}
3216
3217void wxDataViewMainWindow::DestroyTree()
3218{
3219 DestroyTreeHelper(m_root);
d47db7e0 3220 m_root->SetSubTreeCount(0);
aba9bfd0
RR
3221 m_count = 0 ;
3222}
3223
cab07038
RR
3224void wxDataViewMainWindow::OnChar( wxKeyEvent &event )
3225{
3226 if (event.GetKeyCode() == WXK_TAB)
3227 {
3228 wxNavigationKeyEvent nevent;
3229 nevent.SetWindowChange( event.ControlDown() );
3230 nevent.SetDirection( !event.ShiftDown() );
3231 nevent.SetEventObject( GetParent()->GetParent() );
3232 nevent.SetCurrentFocus( m_parent );
3233 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent ))
3234 return;
3235 }
3236
3237 // no item -> nothing to do
3238 if (!HasCurrentRow())
3239 {
3240 event.Skip();
3241 return;
3242 }
120b9b05 3243
cab07038
RR
3244 // don't use m_linesPerPage directly as it might not be computed yet
3245 const int pageSize = GetCountPerPage();
3246 wxCHECK_RET( pageSize, _T("should have non zero page size") );
3247
3248 switch ( event.GetKeyCode() )
3249 {
3250 case WXK_UP:
3251 if ( m_currentRow > 0 )
3252 OnArrowChar( m_currentRow - 1, event );
3253 break;
3254
3255 case WXK_DOWN:
4a851b11 3256 if ( m_currentRow < GetRowCount() - 1 )
cab07038
RR
3257 OnArrowChar( m_currentRow + 1, event );
3258 break;
3b6280be
RR
3259 //Add the process for tree expanding/collapsing
3260 case WXK_LEFT:
b7e9f8b1
RR
3261 OnCollapsing(m_currentRow);
3262 break;
3263 case WXK_RIGHT:
3264 OnExpanding( m_currentRow);
3265 break;
cab07038
RR
3266 case WXK_END:
3267 if (!IsEmpty())
3268 OnArrowChar( GetRowCount() - 1, event );
3269 break;
3270
3271 case WXK_HOME:
3272 if (!IsEmpty())
3273 OnArrowChar( 0, event );
3274 break;
3275
3276 case WXK_PAGEUP:
3277 {
3278 int steps = pageSize - 1;
3279 int index = m_currentRow - steps;
3280 if (index < 0)
3281 index = 0;
3282
3283 OnArrowChar( index, event );
3284 }
3285 break;
3286
3287 case WXK_PAGEDOWN:
3288 {
3289 int steps = pageSize - 1;
0a71f9e9
RR
3290 unsigned int index = m_currentRow + steps;
3291 unsigned int count = GetRowCount();
cab07038
RR
3292 if ( index >= count )
3293 index = count - 1;
3294
3295 OnArrowChar( index, event );
3296 }
3297 break;
3298
3299 default:
3300 event.Skip();
3301 }
3302}
3303
4ed7af08
RR
3304void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
3305{
e21f75bd
RR
3306 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
3307 {
3308 // let the base handle mouse wheel events.
3309 event.Skip();
3310 return;
3311 }
3312
0fdc2321
RR
3313 int x = event.GetX();
3314 int y = event.GetY();
3315 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
0fdc2321 3316 wxDataViewColumn *col = NULL;
24c4a50f 3317
0fdc2321 3318 int xpos = 0;
9861f022 3319 unsigned int cols = GetOwner()->GetColumnCount();
0a71f9e9 3320 unsigned int i;
0fdc2321
RR
3321 for (i = 0; i < cols; i++)
3322 {
3323 wxDataViewColumn *c = GetOwner()->GetColumn( i );
9861f022
RR
3324 if (c->IsHidden())
3325 continue; // skip it!
3326
0fdc2321
RR
3327 if (x < xpos + c->GetWidth())
3328 {
3329 col = c;
3330 break;
3331 }
3332 xpos += c->GetWidth();
3333 }
f554a14b 3334 if (!col)
0fdc2321 3335 return;
f554a14b 3336
24c4a50f 3337 wxDataViewRenderer *cell = col->GetRenderer();
0a71f9e9 3338 unsigned int current = y / m_lineHeight;
e21f75bd
RR
3339 if ((current > GetRowCount()) || (x > GetEndOfLastCol()))
3340 {
3341 // Unselect all if below the last row ?
3342 return;
3343 }
f554a14b 3344
24c4a50f
RR
3345 //Test whether the mouse is hovered on the tree item button
3346 bool hover = false;
3347 if (GetOwner()->GetExpanderColumn() == col)
3348 {
3349 wxDataViewTreeNode * node = GetTreeNodeByRow(current);
3350 if( node!=NULL && node->HasChildren() )
3351 {
3352 int indent = node->GetIndentLevel();
3353 indent = GetOwner()->GetIndent()*indent;
3354 wxRect rect( xpos + indent + EXPANDER_MARGIN, current * m_lineHeight + EXPANDER_MARGIN, m_lineHeight-2*EXPANDER_MARGIN,m_lineHeight-2*EXPANDER_MARGIN);
3355 if( rect.Contains( x, y) )
3356 {
3357 //So the mouse is over the expander
3358 hover = true;
3359 if (m_underMouse && m_underMouse != node)
3360 {
3361 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3362 Refresh(GetRowByItem(m_underMouse->GetItem()));
3363 }
3364 if (m_underMouse != node)
3365 {
3366 //wxLogMessage("Do the row: %d", current);
3367 Refresh(current);
3368 }
3369 m_underMouse = node;
3370 }
3371 }
3372 if (node!=NULL && !node->HasChildren())
3373 delete node;
3374 }
3375 if (!hover)
3376 {
3377 if (m_underMouse != NULL)
3378 {
3379 wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3380 Refresh(GetRowByItem(m_underMouse->GetItem()));
3381 m_underMouse = NULL;
3382 }
3383 }
3384
aba9bfd0 3385 wxDataViewModel *model = GetOwner()->GetModel();
0fdc2321 3386
e21f75bd
RR
3387 if (event.Dragging())
3388 {
3389 if (m_dragCount == 0)
3390 {
3391 // we have to report the raw, physical coords as we want to be
3392 // able to call HitTest(event.m_pointDrag) from the user code to
3393 // get the item being dragged
3394 m_dragStart = event.GetPosition();
3395 }
3396
3397 m_dragCount++;
3398
3399 if (m_dragCount != 3)
3400 return;
3401
3402 if (event.LeftIsDown())
3403 {
3404 // Notify cell about drag
3405 }
3406 return;
3407 }
3408 else
3409 {
3410 m_dragCount = 0;
3411 }
3412
3413 bool forceClick = false;
3414
0fcce6b9
RR
3415 if (event.ButtonDClick())
3416 {
3417 m_renameTimer->Stop();
3418 m_lastOnSame = false;
3419 }
3420
0fdc2321
RR
3421 if (event.LeftDClick())
3422 {
e21f75bd 3423 if ( current == m_lineLastClicked )
0fdc2321 3424 {
e21f75bd
RR
3425 if (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE)
3426 {
6cdcbce3 3427 wxDataViewItem item = GetItemByRow(current);
e21f75bd 3428 wxVariant value;
6cdcbce3 3429 model->GetValue( value, item, col->GetModelColumn() );
e21f75bd 3430 cell->SetValue( value );
c741d33f 3431 wxRect cell_rect( xpos, current * m_lineHeight,
87f0efe2 3432 col->GetWidth(), m_lineHeight );
6cdcbce3 3433 cell->Activate( cell_rect, model, item, col->GetModelColumn() );
b7e9f8b1
RR
3434
3435 wxWindow *parent = GetParent();
3436 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, parent->GetId());
3437
3438 le.SetEventObject(parent);
3439 le.SetColumn(col->GetModelColumn());
3440 le.SetDataViewColumn(col);
3441 le.SetModel(GetOwner()->GetModel());
3442
3443 parent->GetEventHandler()->ProcessEvent(le);
e21f75bd
RR
3444 }
3445 return;
3446 }
3447 else
3448 {
3449 // The first click was on another item, so don't interpret this as
3450 // a double click, but as a simple click instead
3451 forceClick = true;
0fdc2321 3452 }
120b9b05 3453 }
f554a14b 3454
0fcce6b9
RR
3455 if (event.LeftUp())
3456 {
0a71f9e9 3457 if (m_lineSelectSingleOnUp != (unsigned int)-1)
e21f75bd
RR
3458 {
3459 // select single line
3460 SelectAllRows( false );
3461 SelectRow( m_lineSelectSingleOnUp, true );
3462 }
120b9b05 3463
3b6280be 3464 //Process the event of user clicking the expander
d5025dc0
RR
3465 bool expander = false;
3466 wxDataViewTreeNode * node = GetTreeNodeByRow(current);
3467 if( node!=NULL && node->HasChildren() )
3468 {
3469 int indent = node->GetIndentLevel();
3470 indent = GetOwner()->GetIndent()*indent;
3471 wxRect rect( xpos + indent + EXPANDER_MARGIN, current * m_lineHeight + EXPANDER_MARGIN, m_lineHeight-2*EXPANDER_MARGIN,m_lineHeight-2*EXPANDER_MARGIN);
3472 if( rect.Contains( x, y) )
3473 {
3474 expander = true;
704c3490
RR
3475 if( node->IsOpen() )
3476 OnCollapsing(current);
3477 else
3478 OnExpanding( current );
d5025dc0
RR
3479 }
3480 }
3b6280be
RR
3481
3482 //If the user click the expander, we do not do editing even if the column with expander are editable
3483 if (m_lastOnSame && !expander )
0fcce6b9 3484 {
a8461d31 3485 if ((col == m_currentCol) && (current == m_currentRow) &&
0fcce6b9
RR
3486 (cell->GetMode() == wxDATAVIEW_CELL_EDITABLE) )
3487 {
3488 m_renameTimer->Start( 100, true );
3489 }
3490 }
3491
3492 m_lastOnSame = false;
0a71f9e9 3493 m_lineSelectSingleOnUp = (unsigned int)-1;
120b9b05 3494 }
e21f75bd
RR
3495 else
3496 {
3497 // This is necessary, because after a DnD operation in
3498 // from and to ourself, the up event is swallowed by the
3499 // DnD code. So on next non-up event (which means here and
3500 // now) m_lineSelectSingleOnUp should be reset.
0a71f9e9 3501 m_lineSelectSingleOnUp = (unsigned int)-1;
e21f75bd
RR
3502 }
3503
3504 if (event.RightDown())
3505 {
3506 m_lineBeforeLastClicked = m_lineLastClicked;
3507 m_lineLastClicked = current;
3508
3509 // If the item is already selected, do not update the selection.
3510 // Multi-selections should not be cleared if a selected item is clicked.
3511 if (!IsRowSelected(current))
3512 {
3513 SelectAllRows(false);
3514 ChangeCurrentRow(current);
3515 SelectRow(m_currentRow,true);
3516 }
3517
3518 // notify cell about right click
3519 // cell->...
120b9b05 3520
e21f75bd
RR
3521 // Allow generation of context menu event
3522 event.Skip();
3523 }
3524 else if (event.MiddleDown())
3525 {
3526 // notify cell about middle click
3527 // cell->...
3528 }
3529 if (event.LeftDown() || forceClick)
0fcce6b9 3530 {
72664514 3531 SetFocus();
120b9b05 3532
e21f75bd
RR
3533 m_lineBeforeLastClicked = m_lineLastClicked;
3534 m_lineLastClicked = current;
3535
0a71f9e9 3536 unsigned int oldCurrentRow = m_currentRow;
e21f75bd
RR
3537 bool oldWasSelected = IsRowSelected(m_currentRow);
3538
3539 bool cmdModifierDown = event.CmdDown();
3540 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
3541 {
3542 if ( IsSingleSel() || !IsRowSelected(current) )
3543 {
3544 SelectAllRows( false );
3545
3546 ChangeCurrentRow(current);
3547
3548 SelectRow(m_currentRow,true);
3549 }
3550 else // multi sel & current is highlighted & no mod keys
3551 {
3552 m_lineSelectSingleOnUp = current;
3553 ChangeCurrentRow(current); // change focus
3554 }
3555 }
3556 else // multi sel & either ctrl or shift is down
3557 {
3558 if (cmdModifierDown)
3559 {
3560 ChangeCurrentRow(current);
3561
3562 ReverseRowSelection(m_currentRow);
3563 }
3564 else if (event.ShiftDown())
3565 {
3566 ChangeCurrentRow(current);
3567
0a71f9e9 3568 unsigned int lineFrom = oldCurrentRow,
e21f75bd
RR
3569 lineTo = current;
3570
3571 if ( lineTo < lineFrom )
3572 {
3573 lineTo = lineFrom;
3574 lineFrom = m_currentRow;
3575 }
3576
3577 SelectRows(lineFrom, lineTo, true);
3578 }
3579 else // !ctrl, !shift
3580 {
3581 // test in the enclosing if should make it impossible
3582 wxFAIL_MSG( _T("how did we get here?") );
3583 }
3584 }
3585
72664514
RR
3586 if (m_currentRow != oldCurrentRow)
3587 RefreshRow( oldCurrentRow );
e21f75bd 3588
0fcce6b9 3589 wxDataViewColumn *oldCurrentCol = m_currentCol;
120b9b05 3590
0fcce6b9
RR
3591 // Update selection here...
3592 m_currentCol = col;
120b9b05 3593
c741d33f 3594 m_lastOnSame = !forceClick && ((col == oldCurrentCol) &&
87f0efe2 3595 (current == oldCurrentRow)) && oldWasSelected;
0fdc2321 3596 }
4ed7af08
RR
3597}
3598
3599void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event )
3600{
cab07038 3601 m_hasFocus = true;
120b9b05 3602
cab07038
RR
3603 if (HasCurrentRow())
3604 Refresh();
120b9b05 3605
cab07038
RR
3606 event.Skip();
3607}
3608
3609void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event )
3610{
3611 m_hasFocus = false;
120b9b05 3612
cab07038
RR
3613 if (HasCurrentRow())
3614 Refresh();
120b9b05 3615
4ed7af08
RR
3616 event.Skip();
3617}
3618
fbda518c 3619wxDataViewItem wxDataViewMainWindow::GetSelection() const
704c3490
RR
3620{
3621 if( m_selection.GetCount() != 1 )
3622 return wxDataViewItem();
d47db7e0
RR
3623
3624 return GetItemByRow( m_selection.Item(0));
704c3490
RR
3625}
3626
4ed7af08
RR
3627//-----------------------------------------------------------------------------
3628// wxDataViewCtrl
3629//-----------------------------------------------------------------------------
afebb87b 3630WX_DEFINE_LIST(wxDataViewColumnList);
4ed7af08
RR
3631
3632IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
3633
4b3feaa7
RR
3634BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase)
3635 EVT_SIZE(wxDataViewCtrl::OnSize)
3636END_EVENT_TABLE()
3637
4ed7af08
RR
3638wxDataViewCtrl::~wxDataViewCtrl()
3639{
3640 if (m_notifier)
3641 GetModel()->RemoveNotifier( m_notifier );
3642}
3643
3644void wxDataViewCtrl::Init()
3645{
3646 m_notifier = NULL;
3647}
3648
3649bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
f554a14b 3650 const wxPoint& pos, const wxSize& size,
4ed7af08
RR
3651 long style, const wxValidator& validator )
3652{
c741d33f 3653 if (!wxControl::Create( parent, id, pos, size,
87f0efe2 3654 style | wxScrolledWindowStyle|wxSUNKEN_BORDER, validator))
4b3feaa7
RR
3655 return false;
3656
4ed7af08 3657 Init();
f554a14b 3658
4ed7af08
RR
3659#ifdef __WXMAC__
3660 MacSetClipChildren( true ) ;
3661#endif
3662
f554a14b 3663 m_clientArea = new wxDataViewMainWindow( this, wxID_ANY );
9861f022
RR
3664
3665 if (HasFlag(wxDV_NO_HEADER))
3666 m_headerArea = NULL;
3667 else
3668 m_headerArea = new wxDataViewHeaderWindow( this, wxID_ANY );
f554a14b 3669
4ed7af08 3670 SetTargetWindow( m_clientArea );
f554a14b 3671
4ed7af08 3672 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
9861f022
RR
3673 if (m_headerArea)
3674 sizer->Add( m_headerArea, 0, wxGROW );
4ed7af08
RR
3675 sizer->Add( m_clientArea, 1, wxGROW );
3676 SetSizer( sizer );
3677
3678 return true;
3679}
3680
3681#ifdef __WXMSW__
3682WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg,
3683 WXWPARAM wParam,
3684 WXLPARAM lParam)
3685{
b910a8ad 3686 WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
4ed7af08
RR
3687
3688#ifndef __WXWINCE__
3689 // we need to process arrows ourselves for scrolling
3690 if ( nMsg == WM_GETDLGCODE )
3691 {
3692 rc |= DLGC_WANTARROWS;
3693 }
3694#endif
3695
3696 return rc;
3697}
3698#endif
3699
f554a14b 3700void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
4ed7af08 3701{
4b3feaa7
RR
3702 // We need to override OnSize so that our scrolled
3703 // window a) does call Layout() to use sizers for
3704 // positioning the controls but b) does not query
3705 // the sizer for their size and use that for setting
3706 // the scrollable area as set that ourselves by
3707 // calling SetScrollbar() further down.
3708
3709 Layout();
3710
3711 AdjustScrollbars();
4ed7af08
RR
3712}
3713
aba9bfd0 3714bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
4ed7af08
RR
3715{
3716 if (!wxDataViewCtrlBase::AssociateModel( model ))
3717 return false;
3718
aba9bfd0 3719 m_notifier = new wxGenericDataViewModelNotifier( m_clientArea );
4ed7af08 3720
f554a14b 3721 model->AddNotifier( m_notifier );
4ed7af08 3722
aba9bfd0
RR
3723 m_clientArea->BuildTree(model);
3724
4b3feaa7 3725 m_clientArea->UpdateDisplay();
f554a14b 3726
4ed7af08
RR
3727 return true;
3728}
3729
3730bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
3731{
3732 if (!wxDataViewCtrlBase::AppendColumn(col))
3733 return false;
f554a14b 3734
afebb87b 3735 m_cols.Append( col );
9861f022 3736 OnColumnChange();
4ed7af08
RR
3737 return true;
3738}
3739
9861f022
RR
3740void wxDataViewCtrl::OnColumnChange()
3741{
3742 if (m_headerArea)
3743 m_headerArea->UpdateDisplay();
3744
3745 m_clientArea->UpdateDisplay();
3746}
3b6280be
RR
3747
3748void wxDataViewCtrl::DoSetExpanderColumn()
3749{
3750 m_clientArea->UpdateDisplay();
3751}
3752
3753void wxDataViewCtrl::DoSetIndent()
3754{
3755 m_clientArea->UpdateDisplay();
3756}
3757
afebb87b
RR
3758unsigned int wxDataViewCtrl::GetColumnCount() const
3759{
3760 return m_cols.GetCount();
3761}
3762
3763wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const
3764{
3765 wxDataViewColumnList::const_iterator iter;
3766 int i = 0;
3767 for (iter = m_cols.begin(); iter!=m_cols.end(); iter++)
3768 {
3769 if (i == pos)
3770 return *iter;
3771
3772 if ((*iter)->IsHidden())
3773 continue;
3774 i ++;
3775 }
3776 return NULL;
3777}
3778
3779bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
3780{
3781 wxDataViewColumnList::Node * ret = m_cols.Find( column );
3782 if (ret == NULL)
3783 return false;
3784
3785 m_cols.Erase(ret);
3786 delete column;
3787 OnColumnChange();
3788
3789 return true;
3790}
3791
3792bool wxDataViewCtrl::ClearColumns()
3793{
3794 m_cols.clear();
3795 OnColumnChange();
3796 return true;
3797}
3798
b7e9f8b1 3799//Selection code with wxDataViewItem as parameters
fbda518c 3800wxDataViewItem wxDataViewCtrl::GetSelection() const
66e09788
RR
3801{
3802 return m_clientArea->GetSelection();
3803}
3804
b7e9f8b1
RR
3805int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
3806{
3807 sel.Empty();
3808 wxDataViewSelection selection = m_clientArea->GetSelections();
3809 int len = selection.GetCount();
3810 for( int i = 0; i < len; i ++)
3811 {
3812 unsigned int row = selection[i];
3813 sel.Add( m_clientArea->GetItemByRow( row ) );
3814 }
3815 return len;
3816}
3817
3818void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
3819{
3820 wxDataViewSelection selection(wxDataViewSelectionCmp) ;
3821 int len = sel.GetCount();
3822 for( int i = 0; i < len; i ++ )
3823 {
3824 int row = m_clientArea->GetRowByItem( sel[i] );
3825 if( row >= 0 )
3826 selection.Add( static_cast<unsigned int>(row) );
3827 }
3828 m_clientArea->SetSelections( selection );
3829}
3830
3831void wxDataViewCtrl::Select( const wxDataViewItem & item )
704c3490 3832{
b7e9f8b1
RR
3833 int row = m_clientArea->GetRowByItem( item );
3834 if( row >= 0 )
57f2a652
RR
3835 {
3836 //Unselect all rows before select another in the single select mode
3837 if (m_clientArea->IsSingleSel())
3838 m_clientArea->SelectAllRows(false);
b7e9f8b1 3839 m_clientArea->SelectRow(row, true);
57f2a652 3840 }
704c3490 3841}
3b6280be 3842
b7e9f8b1 3843void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
6ff7eee7 3844{
b7e9f8b1
RR
3845 int row = m_clientArea->GetRowByItem( item );
3846 if( row >= 0 )
3847 m_clientArea->SelectRow(row, false);
6ff7eee7
RR
3848}
3849
b7e9f8b1 3850bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
6ff7eee7 3851{
b7e9f8b1
RR
3852 int row = m_clientArea->GetRowByItem( item );
3853 if( row >= 0 )
3854 {
3855 return m_clientArea->IsRowSelected(row);
3856 }
3857 return false;
6ff7eee7
RR
3858}
3859
b7e9f8b1
RR
3860//Selection code with row number as parameter
3861int wxDataViewCtrl::GetSelections( wxArrayInt & sel ) const
6ff7eee7 3862{
b7e9f8b1
RR
3863 sel.Empty();
3864 wxDataViewSelection selection = m_clientArea->GetSelections();
3865 int len = selection.GetCount();
3866 for( int i = 0; i < len; i ++)
3867 {
3868 unsigned int row = selection[i];
3869 sel.Add( row );
3870 }
3871 return len;
6ff7eee7 3872}
df387169 3873
b7e9f8b1 3874void wxDataViewCtrl::SetSelections( const wxArrayInt & sel )
fc211fe5 3875{
b7e9f8b1
RR
3876 wxDataViewSelection selection(wxDataViewSelectionCmp) ;
3877 int len = sel.GetCount();
3878 for( int i = 0; i < len; i ++ )
3879 {
3880 int row = sel[i];
3881 if( row >= 0 )
3882 selection.Add( static_cast<unsigned int>(row) );
3883 }
3884 m_clientArea->SetSelections( selection );
fc211fe5
RR
3885}
3886
b7e9f8b1 3887void wxDataViewCtrl::Select( int row )
6ff7eee7 3888{
b7e9f8b1 3889 if( row >= 0 )
57f2a652
RR
3890 {
3891 if (m_clientArea->IsSingleSel())
3892 m_clientArea->SelectAllRows(false);
b7e9f8b1 3893 m_clientArea->SelectRow( row, true );
57f2a652 3894 }
b7e9f8b1 3895}
df387169 3896
b7e9f8b1
RR
3897void wxDataViewCtrl::Unselect( int row )
3898{
3899 if( row >= 0 )
3900 m_clientArea->SelectRow(row, false);
3901}
3902
3903bool wxDataViewCtrl::IsSelected( int row ) const
3904{
3905 if( row >= 0 )
3906 return m_clientArea->IsRowSelected(row);
6ff7eee7
RR
3907 return false;
3908}
3909
b7e9f8b1 3910void wxDataViewCtrl::SelectRange( int from, int to )
6ff7eee7 3911{
b7e9f8b1
RR
3912 wxArrayInt sel;
3913 for( int i = from; i < to; i ++ )
3914 sel.Add( i );
3915 m_clientArea->Select(sel);
3916}
df387169 3917
b7e9f8b1
RR
3918void wxDataViewCtrl::UnselectRange( int from, int to )
3919{
3920 wxDataViewSelection sel = m_clientArea->GetSelections();
3921 for( int i = from; i < to; i ++ )
3922 if( sel.Index( i ) != wxNOT_FOUND )
3923 sel.Remove( i );
3924 m_clientArea->SetSelections(sel);
6ff7eee7
RR
3925}
3926
b7e9f8b1 3927void wxDataViewCtrl::SelectAll()
6ff7eee7 3928{
b7e9f8b1
RR
3929 m_clientArea->SelectAllRows(true);
3930}
df387169 3931
b7e9f8b1
RR
3932void wxDataViewCtrl::UnselectAll()
3933{
3934 m_clientArea->SelectAllRows(false);
6ff7eee7 3935}
b7e9f8b1 3936
fbda518c 3937void wxDataViewCtrl::EnsureVisible( int row, int column )
b7e9f8b1 3938{
fbda518c
RR
3939 if( row < 0 )
3940 row = 0;
3941 if( row > m_clientArea->GetRowCount() )
3942 row = m_clientArea->GetRowCount();
3943
3944 int first = m_clientArea->GetFirstVisibleRow();
3945 int last = m_clientArea->GetLastVisibleRow();
3946 if( row < first )
3947 m_clientArea->ScrollTo( row, column );
3948 else if( row > last )
3949 m_clientArea->ScrollTo( row - last + first, column );
3950 else
3951 m_clientArea->ScrollTo( first, column );
b7e9f8b1
RR
3952}
3953
fbda518c 3954void wxDataViewCtrl::EnsureVisible( const wxDataViewItem & item, const wxDataViewColumn * column )
b7e9f8b1
RR
3955{
3956 int row = m_clientArea->GetRowByItem(item);
3957 if( row >= 0 )
fbda518c
RR
3958 {
3959 if( column == NULL )
3960 return EnsureVisible(row, -1);
3961 else
3962 {
3963 int col = 0;
3964 int len = GetColumnCount();
3965 for( int i = 0; i < len; i ++ )
3966 if( GetColumn(i) == column )
3967 {
3968 col = i;
3969 break;
3970 }
3971 EnsureVisible( row, col );
3972 }
3973 }
3974
b7e9f8b1
RR
3975}
3976
a87b466d 3977void wxDataViewCtrl::HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column ) const
66e09788
RR
3978{
3979 m_clientArea->HitTest(point, item, column);
3980}
3981
fbda518c 3982wxRect wxDataViewCtrl::GetItemRect( const wxDataViewItem & item, const wxDataViewColumn* column ) const
66e09788
RR
3983{
3984 return m_clientArea->GetItemRect(item, column);
3985}
3986
b7e9f8b1
RR
3987wxDataViewItem wxDataViewCtrl::GetItemByRow( unsigned int row ) const
3988{
3989 return m_clientArea->GetItemByRow( row );
3990}
3991
3992int wxDataViewCtrl::GetRowByItem( const wxDataViewItem & item ) const
3993{
3994 return m_clientArea->GetRowByItem( item );
3995}
3996
afebb87b
RR
3997void wxDataViewCtrl::Expand( const wxDataViewItem & item )
3998{
3999 int row = m_clientArea->GetRowByItem( item );
4000 if (row != -1)
4001 m_clientArea->Expand(row);
4002}
4003
4004void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
4005{
4006 int row = m_clientArea->GetRowByItem( item );
4007 if (row != -1)
4008 m_clientArea->Collapse(row);
4009}
4010
b7e9f8b1 4011 #endif
4ed7af08
RR
4012 // !wxUSE_GENERICDATAVIEWCTRL
4013
f554a14b 4014#endif
4ed7af08 4015 // wxUSE_DATAVIEWCTRL