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