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