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