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