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