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