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