]> git.saurik.com Git - wxWidgets.git/blob - src/generic/datavgen.cpp
aa1a08a028dc99eb6f14f833752aa884d37ff61f
[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 wxDataViewColumn *expander = GetOwner()->GetExpanderColumn();
2305 if (!expander)
2306 {
2307 // TODO: last column for RTL support
2308 expander = GetOwner()->GetColumn( 0 );
2309 }
2310
2311 // redraw all cells for all rows which must be repainted and for all columns
2312 wxRect cell_rect;
2313 cell_rect.x = x_start;
2314 cell_rect.height = m_lineHeight; // -1 is for the horizontal rules
2315 for (unsigned int i = col_start; i < col_last; i++)
2316 {
2317 wxDataViewColumn *col = GetOwner()->GetColumn( i );
2318 wxDataViewRenderer *cell = col->GetRenderer();
2319 cell_rect.width = col->GetWidth();
2320
2321 if (col->IsHidden())
2322 continue; // skipt it!
2323
2324
2325 for (unsigned int item = item_start; item < item_last; item++)
2326 {
2327 // get the cell value and set it into the renderer
2328 wxVariant value;
2329 wxDataViewTreeNode * node = GetTreeNodeByRow(item);
2330 if( node == NULL )
2331 {
2332 continue;
2333 }
2334
2335 wxDataViewItem dataitem = node->GetItem();
2336 model->GetValue( value, dataitem, col->GetModelColumn());
2337 cell->SetValue( value );
2338
2339 // update the y offset
2340 cell_rect.y = item * m_lineHeight;
2341
2342 //Draw the expander here.
2343 int indent = node->GetIndentLevel();
2344 if( col == expander )
2345 {
2346 //Calculate the indent first
2347 indent = cell_rect.x + GetOwner()->GetIndent() * indent;
2348
2349 int expander_width = m_lineHeight - 2*EXPANDER_MARGIN;
2350 // change the cell_rect.x to the appropriate pos
2351 int expander_x = indent + EXPANDER_MARGIN , expander_y = cell_rect.y + EXPANDER_MARGIN ;
2352 indent = indent + m_lineHeight ; //try to use the m_lineHeight as the expander space
2353 dc.SetPen( m_penExpander );
2354 dc.SetBrush( wxNullBrush );
2355 if( node->HasChildren() )
2356 {
2357 //dc.DrawRoundedRectangle( expander_x,expander_y,expander_width,expander_width, 1.0);
2358 //dc.DrawLine( expander_x + 2 , expander_y + expander_width/2, expander_x + expander_width - 2, expander_y + expander_width/2 );
2359 wxRect rect( expander_x , expander_y, expander_width, expander_width);
2360 if( node->IsOpen() )
2361 wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, wxCONTROL_EXPANDED );
2362 else
2363 wxRendererNative::Get().DrawTreeItemButton( this, dc, rect );
2364 }
2365 else
2366 {
2367 // I am wandering whether we should draw dot lines between tree nodes
2368 delete node;
2369 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2370 }
2371
2372 //force the expander column to left-center align
2373 cell->SetAlignment( wxALIGN_CENTER_VERTICAL );
2374 }
2375
2376
2377 // cannot be bigger than allocated space
2378 wxSize size = cell->GetSize();
2379 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2380 size.x = wxMin( size.x + 2*PADDING_RIGHTLEFT, cell_rect.width - indent );
2381 size.y = wxMin( size.y + 2*PADDING_TOPBOTTOM, cell_rect.height );
2382
2383 wxRect item_rect(cell_rect.GetTopLeft(), size);
2384 int align = cell->GetAlignment();
2385
2386 // horizontal alignment:
2387 item_rect.x = cell_rect.x;
2388 if (align & wxALIGN_CENTER_HORIZONTAL)
2389 item_rect.x = cell_rect.x + (cell_rect.width / 2) - (size.x / 2);
2390 else if (align & wxALIGN_RIGHT)
2391 item_rect.x = cell_rect.x + cell_rect.width - size.x;
2392 //else: wxALIGN_LEFT is the default
2393
2394 // vertical alignment:
2395 item_rect.y = cell_rect.y;
2396 if (align & wxALIGN_CENTER_VERTICAL)
2397 item_rect.y = cell_rect.y + (cell_rect.height / 2) - (size.y / 2);
2398 else if (align & wxALIGN_BOTTOM)
2399 item_rect.y = cell_rect.y + cell_rect.height - size.y;
2400 //else: wxALIGN_TOP is the default
2401
2402 // add padding
2403 item_rect.x += PADDING_RIGHTLEFT;
2404 item_rect.y += PADDING_TOPBOTTOM;
2405 item_rect.width = size.x - 2 * PADDING_RIGHTLEFT;
2406 item_rect.height = size.y - 2 * PADDING_TOPBOTTOM;
2407
2408 //Here we add the tree indent
2409 item_rect.x += indent;
2410
2411 int state = 0;
2412 if (m_selection.Index(item) != wxNOT_FOUND)
2413 state |= wxDATAVIEW_CELL_SELECTED;
2414
2415 // TODO: it would be much more efficient to create a clipping
2416 // region for the entire column being rendered (in the OnPaint
2417 // of wxDataViewMainWindow) instead of a single clip region for
2418 // each cell. However it would mean that each renderer should
2419 // respect the given wxRect's top & bottom coords, eventually
2420 // violating only the left & right coords - however the user can
2421 // make its own renderer and thus we cannot be sure of that.
2422 dc.SetClippingRegion( item_rect );
2423 cell->Render( item_rect, &dc, state );
2424 dc.DestroyClippingRegion();
2425 }
2426
2427 cell_rect.x += cell_rect.width;
2428 }
2429 }
2430
2431 int wxDataViewMainWindow::GetCountPerPage() const
2432 {
2433 wxSize size = GetClientSize();
2434 return size.y / m_lineHeight;
2435 }
2436
2437 int wxDataViewMainWindow::GetEndOfLastCol() const
2438 {
2439 int width = 0;
2440 unsigned int i;
2441 for (i = 0; i < GetOwner()->GetColumnCount(); i++)
2442 {
2443 const wxDataViewColumn *c =
2444 wx_const_cast(wxDataViewCtrl*, GetOwner())->GetColumn( i );
2445
2446 if (!c->IsHidden())
2447 width += c->GetWidth();
2448 }
2449 return width;
2450 }
2451
2452 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2453 {
2454 int x = 0;
2455 int y = 0;
2456 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
2457
2458 return y / m_lineHeight;
2459 }
2460
2461 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2462 {
2463 wxSize client_size = GetClientSize();
2464 m_owner->CalcUnscrolledPosition( client_size.x, client_size.y,
2465 &client_size.x, &client_size.y );
2466
2467 //we should deal with the pixel here
2468 unsigned int row = (client_size.y)/m_lineHeight;
2469 if( client_size.y % m_lineHeight < m_lineHeight/2 )
2470 row -= 1;
2471
2472 return wxMin( GetRowCount()-1, row );
2473 }
2474
2475 unsigned int wxDataViewMainWindow::GetRowCount()
2476 {
2477 if ( m_count == -1 )
2478 {
2479 m_count = RecalculateCount();
2480 int width, height;
2481 GetVirtualSize( &width, &height );
2482 height = m_count * m_lineHeight;
2483
2484 SetVirtualSize( width, height );
2485 }
2486 return m_count;
2487 }
2488
2489 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row )
2490 {
2491 m_currentRow = row;
2492
2493 // send event
2494 }
2495
2496 void wxDataViewMainWindow::SelectAllRows( bool on )
2497 {
2498 if (IsEmpty())
2499 return;
2500
2501 if (on)
2502 {
2503 m_selection.Clear();
2504 for (unsigned int i = 0; i < GetRowCount(); i++)
2505 m_selection.Add( i );
2506 Refresh();
2507 }
2508 else
2509 {
2510 unsigned int first_visible = GetFirstVisibleRow();
2511 unsigned int last_visible = GetLastVisibleRow();
2512 unsigned int i;
2513 for (i = 0; i < m_selection.GetCount(); i++)
2514 {
2515 unsigned int row = m_selection[i];
2516 if ((row >= first_visible) && (row <= last_visible))
2517 RefreshRow( row );
2518 }
2519 m_selection.Clear();
2520 }
2521 }
2522
2523 void wxDataViewMainWindow::SelectRow( unsigned int row, bool on )
2524 {
2525 if (m_selection.Index( row ) == wxNOT_FOUND)
2526 {
2527 if (on)
2528 {
2529 m_selection.Add( row );
2530 RefreshRow( row );
2531 }
2532 }
2533 else
2534 {
2535 if (!on)
2536 {
2537 m_selection.Remove( row );
2538 RefreshRow( row );
2539 }
2540 }
2541 }
2542
2543 void wxDataViewMainWindow::SelectRows( unsigned int from, unsigned int to, bool on )
2544 {
2545 if (from > to)
2546 {
2547 unsigned int tmp = from;
2548 from = to;
2549 to = tmp;
2550 }
2551
2552 unsigned int i;
2553 for (i = from; i <= to; i++)
2554 {
2555 if (m_selection.Index( i ) == wxNOT_FOUND)
2556 {
2557 if (on)
2558 m_selection.Add( i );
2559 }
2560 else
2561 {
2562 if (!on)
2563 m_selection.Remove( i );
2564 }
2565 }
2566 RefreshRows( from, to );
2567 }
2568
2569 void wxDataViewMainWindow::Select( const wxArrayInt& aSelections )
2570 {
2571 for (size_t i=0; i < aSelections.GetCount(); i++)
2572 {
2573 int n = aSelections[i];
2574
2575 m_selection.Add( n );
2576 RefreshRow( n );
2577 }
2578 }
2579
2580 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row )
2581 {
2582 if (m_selection.Index( row ) == wxNOT_FOUND)
2583 m_selection.Add( row );
2584 else
2585 m_selection.Remove( row );
2586 RefreshRow( row );
2587 }
2588
2589 bool wxDataViewMainWindow::IsRowSelected( unsigned int row )
2590 {
2591 return (m_selection.Index( row ) != wxNOT_FOUND);
2592 }
2593
2594 void wxDataViewMainWindow::RefreshRow( unsigned int row )
2595 {
2596 wxRect rect( 0, row*m_lineHeight, GetEndOfLastCol(), m_lineHeight );
2597 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2598
2599 wxSize client_size = GetClientSize();
2600 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2601 wxRect intersect_rect = client_rect.Intersect( rect );
2602 if (intersect_rect.width > 0)
2603 Refresh( true, &intersect_rect );
2604 }
2605
2606 void wxDataViewMainWindow::RefreshRows( unsigned int from, unsigned int to )
2607 {
2608 if (from > to)
2609 {
2610 unsigned int tmp = to;
2611 to = from;
2612 from = tmp;
2613 }
2614
2615 wxRect rect( 0, from*m_lineHeight, GetEndOfLastCol(), (to-from+1) * m_lineHeight );
2616 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2617
2618 wxSize client_size = GetClientSize();
2619 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2620 wxRect intersect_rect = client_rect.Intersect( rect );
2621 if (intersect_rect.width > 0)
2622 Refresh( true, &intersect_rect );
2623 }
2624
2625 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow )
2626 {
2627 unsigned int count = GetRowCount();
2628 if (firstRow > count)
2629 return;
2630
2631 wxRect rect( 0, firstRow*m_lineHeight, GetEndOfLastCol(), count * m_lineHeight );
2632 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2633
2634 wxSize client_size = GetClientSize();
2635 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2636 wxRect intersect_rect = client_rect.Intersect( rect );
2637 if (intersect_rect.width > 0)
2638 Refresh( true, &intersect_rect );
2639 }
2640
2641 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event)
2642 {
2643 wxCHECK_RET( newCurrent < GetRowCount(),
2644 _T("invalid item index in OnArrowChar()") );
2645
2646 // if there is no selection, we cannot move it anywhere
2647 if (!HasCurrentRow())
2648 return;
2649
2650 unsigned int oldCurrent = m_currentRow;
2651
2652 // in single selection we just ignore Shift as we can't select several
2653 // items anyhow
2654 if ( event.ShiftDown() && !IsSingleSel() )
2655 {
2656 RefreshRow( oldCurrent );
2657
2658 ChangeCurrentRow( newCurrent );
2659
2660 // select all the items between the old and the new one
2661 if ( oldCurrent > newCurrent )
2662 {
2663 newCurrent = oldCurrent;
2664 oldCurrent = m_currentRow;
2665 }
2666
2667 SelectRows( oldCurrent, newCurrent, true );
2668 }
2669 else // !shift
2670 {
2671 RefreshRow( oldCurrent );
2672
2673 // all previously selected items are unselected unless ctrl is held
2674 if ( !event.ControlDown() )
2675 SelectAllRows(false);
2676
2677 ChangeCurrentRow( newCurrent );
2678
2679 if ( !event.ControlDown() )
2680 SelectRow( m_currentRow, true );
2681 else
2682 RefreshRow( m_currentRow );
2683 }
2684
2685 //EnsureVisible( m_currentRow );
2686 }
2687
2688 wxRect wxDataViewMainWindow::GetLineRect( unsigned int row ) const
2689 {
2690 wxRect rect;
2691 rect.x = 0;
2692 rect.y = m_lineHeight * row;
2693 rect.width = GetEndOfLastCol();
2694 rect.height = m_lineHeight;
2695
2696 return rect;
2697 }
2698
2699 class RowToItemJob: public DoJob
2700 {
2701 public:
2702 RowToItemJob( unsigned int row , int current ) { this->row = row; this->current = current ;}
2703 virtual ~RowToItemJob(){};
2704
2705 virtual int operator() ( wxDataViewTreeNode * node )
2706 {
2707 current ++;
2708 if( current == static_cast<int>(row))
2709 {
2710 ret = node->GetItem() ;
2711 return DoJob::OK;
2712 }
2713
2714 if( node->GetSubTreeCount() + current < static_cast<int>(row) )
2715 {
2716 current += node->GetSubTreeCount();
2717 return DoJob::IGR;
2718 }
2719 else
2720 {
2721 //If the current has no child node, we can find the desired item of the row number directly.
2722 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2723 if( node->GetNodes().GetCount() == 0)
2724 {
2725 int index = static_cast<int>(row) - current - 1;
2726 ret = node->GetChildren().Item( index );
2727 return DoJob::OK;
2728 }
2729 return DoJob::CONT;
2730 }
2731 }
2732
2733 virtual int operator() ( void * n )
2734 {
2735 current ++;
2736 if( current == static_cast<int>(row))
2737 {
2738 ret = wxDataViewItem( n ) ;
2739 return DoJob::OK;
2740 }
2741 return DoJob::CONT;
2742 }
2743 wxDataViewItem GetResult(){ return ret; }
2744 private:
2745 unsigned int row;
2746 int current ;
2747 wxDataViewItem ret;
2748 };
2749
2750 wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row) const
2751 {
2752 RowToItemJob job( row, -2 );
2753 Walker( m_root , job );
2754 return job.GetResult();
2755 }
2756
2757 class RowToTreeNodeJob: public DoJob
2758 {
2759 public:
2760 RowToTreeNodeJob( unsigned int row , int current, wxDataViewTreeNode * node )
2761 {
2762 this->row = row;
2763 this->current = current ;
2764 ret = NULL ;
2765 parent = node;
2766 }
2767 virtual ~RowToTreeNodeJob(){};
2768
2769 virtual int operator() ( wxDataViewTreeNode * node )
2770 {
2771 current ++;
2772 if( current == static_cast<int>(row))
2773 {
2774 ret = node ;
2775 return DoJob::OK;
2776 }
2777
2778 if( node->GetSubTreeCount() + current < static_cast<int>(row) )
2779 {
2780 current += node->GetSubTreeCount();
2781 return DoJob::IGR;
2782 }
2783 else
2784 {
2785 parent = node;
2786 //If the current has no child node, we can find the desired item of the row number directly.
2787 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2788 if( node->GetNodes().GetCount() == 0)
2789 {
2790 int index = static_cast<int>(row) - current - 1;
2791 void * n = node->GetChildren().Item( index );
2792 ret = new wxDataViewTreeNode( parent ) ;
2793 ret->SetItem( wxDataViewItem( n ));
2794 ret->SetHasChildren(false);
2795 return DoJob::OK;
2796 }
2797 return DoJob::CONT;
2798 }
2799
2800
2801 }
2802
2803 virtual int operator() ( void * n )
2804 {
2805 current ++;
2806 if( current == static_cast<int>(row))
2807 {
2808 ret = new wxDataViewTreeNode( parent ) ;
2809 ret->SetItem( wxDataViewItem( n ));
2810 ret->SetHasChildren(false);
2811 return DoJob::OK;
2812 }
2813
2814 return DoJob::CONT;
2815 }
2816 wxDataViewTreeNode * GetResult(){ return ret; }
2817 private:
2818 unsigned int row;
2819 int current ;
2820 wxDataViewTreeNode * ret;
2821 wxDataViewTreeNode * parent ;
2822 };
2823
2824
2825 wxDataViewTreeNode * wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row)
2826 {
2827 RowToTreeNodeJob job( row , -2, m_root );
2828 Walker( m_root , job );
2829 return job.GetResult();
2830 }
2831
2832 wxDataViewEvent wxDataViewMainWindow::SendExpanderEvent( wxEventType type, const wxDataViewItem & item )
2833 {
2834 wxWindow *parent = GetParent();
2835 wxDataViewEvent le(type, parent->GetId());
2836
2837 le.SetEventObject(parent);
2838 le.SetModel(GetOwner()->GetModel());
2839 le.SetItem( item );
2840
2841 parent->GetEventHandler()->ProcessEvent(le);
2842 return le;
2843 }
2844
2845 void wxDataViewMainWindow::OnExpanding( unsigned int row )
2846 {
2847 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
2848 if( node != NULL )
2849 {
2850 if( node->HasChildren())
2851 {
2852 if( !node->IsOpen())
2853 {
2854 wxDataViewEvent e = SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING,node->GetItem());
2855 //Check if the user prevent expanding
2856 if( e.GetSkipped() )
2857 return;
2858
2859 node->ToggleOpen();
2860 //Here I build the children of current node
2861 if( node->GetChildrenNumber() == 0 )
2862 BuildTreeHelper(GetOwner()->GetModel(), node->GetItem(), node);
2863 m_count = -1;
2864 UpdateDisplay();
2865 //Send the expanded event
2866 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED,node->GetItem());
2867 }
2868 else
2869 {
2870 SelectRow( row, false );
2871 SelectRow( row + 1, true );
2872 ChangeCurrentRow( row + 1 );
2873 }
2874 }
2875 else
2876 delete node;
2877 }
2878 }
2879
2880 void wxDataViewMainWindow::OnCollapsing(unsigned int row)
2881 {
2882 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
2883 if( node != NULL )
2884 {
2885 wxDataViewTreeNode * nd = node;
2886
2887 if( node->HasChildren() && node->IsOpen() )
2888 {
2889 wxDataViewEvent e = SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING,node->GetItem());
2890 if( e.GetSkipped() )
2891 return;
2892 node->ToggleOpen();
2893 m_count = -1;
2894 UpdateDisplay();
2895 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED,nd->GetItem());
2896 }
2897 else
2898 {
2899 node = node->GetParent();
2900 if( node != NULL )
2901 {
2902 int parent = GetRowByItem( node->GetItem() ) ;
2903 if( parent >= 0 )
2904 {
2905 SelectRow( row, false);
2906 SelectRow(parent , true );
2907 ChangeCurrentRow( parent );
2908 }
2909 }
2910 }
2911 if( !nd->HasChildren())
2912 delete nd;
2913 }
2914 }
2915
2916 wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item )
2917 {
2918 wxDataViewModel * model = GetOwner()->GetModel();
2919 if( model == NULL )
2920 return NULL;
2921
2922 //Compose the a parent-chain of the finding item
2923 ItemList list;
2924 list.DeleteContents( true );
2925 wxDataViewItem it( item );
2926 while( it.IsOk() )
2927 {
2928 wxDataViewItem * pItem = new wxDataViewItem( it );
2929 list.Insert( pItem );
2930 it = model->GetParent( it );
2931 }
2932
2933 //Find the item along the parent-chain.
2934 //This algorithm is designed to speed up the node-finding method
2935 wxDataViewTreeNode * node = m_root;
2936 for( ItemList::Node * n = list.GetFirst(); n; n = n->GetNext() )
2937 {
2938 if( node->HasChildren() )
2939 {
2940 if( node->GetChildrenNumber() == 0 )
2941 BuildTreeHelper(model, node->GetItem(), node);
2942
2943 wxDataViewTreeNodes nodes = node->GetNodes();
2944 //The wxSortedArray search a node in binary search, so using Item() is more efficient
2945 wxDataViewTreeNode temp;
2946 temp.SetItem(*(n->GetData()));
2947 int index = nodes.Index( &temp );
2948 if( index == wxNOT_FOUND )
2949 return NULL;
2950 node = nodes[index];
2951 }
2952 else
2953 return NULL;
2954 }
2955 return node;
2956 }
2957
2958 void wxDataViewMainWindow::HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column )
2959 {
2960 wxDataViewColumn *col = NULL;
2961 unsigned int cols = GetOwner()->GetColumnCount();
2962 unsigned int colnum = 0;
2963 unsigned int x_start = 0;
2964 int x, y;
2965 m_owner->CalcUnscrolledPosition( point.x, point.y, &x, &y );
2966 for (x_start = 0; colnum < cols; colnum++)
2967 {
2968 col = GetOwner()->GetColumn(colnum);
2969 if (col->IsHidden())
2970 continue; // skip it!
2971
2972 unsigned int w = col->GetWidth();
2973 if (x_start+w >= (unsigned int)x)
2974 break;
2975
2976 x_start += w;
2977 }
2978
2979 column = col;
2980 item = GetItemByRow( y/m_lineHeight );
2981 }
2982
2983 wxRect wxDataViewMainWindow::GetItemRect( const wxDataViewItem & item, const wxDataViewColumn* column )
2984 {
2985 int row = GetRowByItem(item);
2986 int y = row*m_lineHeight;
2987 int h = m_lineHeight;
2988 int x = 0;
2989 wxDataViewColumn *col = NULL;
2990 for( int i = 0, cols = GetOwner()->GetColumnCount(); i < cols; i ++ )
2991 {
2992 col = GetOwner()->GetColumn( i );
2993 x += col->GetWidth();
2994 if( GetOwner()->GetColumn(i+1) == column )
2995 break;
2996 }
2997 int w = col->GetWidth();
2998 m_owner->CalcScrolledPosition( x, y, &x, &y );
2999 return wxRect(x, y, w, h);
3000 }
3001
3002 int wxDataViewMainWindow::RecalculateCount()
3003 {
3004 return m_root->GetSubTreeCount();
3005 }
3006
3007 class ItemToRowJob : public DoJob
3008 {
3009 public:
3010 ItemToRowJob(const wxDataViewItem & item, ItemList::Node * node )
3011 { this->item = item ; ret = -1 ; nd = node ; }
3012 virtual ~ItemToRowJob(){};
3013
3014 //Maybe binary search will help to speed up this process
3015 virtual int operator() ( wxDataViewTreeNode * node)
3016 {
3017 ret ++;
3018 if( node->GetItem() == item )
3019 {
3020 return DoJob::OK;
3021 }
3022
3023 if( nd && node->GetItem() == *(nd->GetData()))
3024 {
3025 nd = nd->GetNext();
3026 return DoJob::CONT;
3027 }
3028 else
3029 {
3030 ret += node->GetSubTreeCount();
3031 return DoJob::IGR;
3032 }
3033
3034 }
3035
3036 virtual int operator() ( void * n )
3037 {
3038 ret ++;
3039 if( n == item.GetID() )
3040 return DoJob::OK;
3041 return DoJob::CONT;
3042 }
3043 //the row number is begin from zero
3044 int GetResult(){ return ret -1 ; }
3045 private:
3046 ItemList::Node * nd;
3047 wxDataViewItem item;
3048 int ret;
3049
3050 };
3051
3052 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item)
3053 {
3054 wxDataViewModel * model = GetOwner()->GetModel();
3055 if( model == NULL )
3056 return -1;
3057
3058 if( !item.IsOk() )
3059 return -1;
3060
3061 //Compose the a parent-chain of the finding item
3062 ItemList list;
3063 wxDataViewItem * pItem = NULL;
3064 list.DeleteContents( true );
3065 wxDataViewItem it( item );
3066 while( it.IsOk() )
3067 {
3068 pItem = new wxDataViewItem( it );
3069 list.Insert( pItem );
3070 it = model->GetParent( it );
3071 }
3072 pItem = new wxDataViewItem( );
3073 list.Insert( pItem );
3074
3075 ItemToRowJob job( item, list.GetFirst() );
3076 Walker(m_root , job );
3077 return job.GetResult();
3078 }
3079
3080 void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, wxDataViewTreeNode * node)
3081 {
3082 if( !model->IsContainer( item ) )
3083 return ;
3084
3085 wxDataViewItem i = model->GetFirstChild( item );
3086 int num = 0;
3087 while( i.IsOk() )
3088 {
3089 num ++;
3090 if( model->IsContainer( i ) )
3091 {
3092 wxDataViewTreeNode * n = new wxDataViewTreeNode( node );
3093 n->SetItem(i);
3094 n->SetHasChildren( true ) ;
3095 node->AddNode( n );
3096 }
3097 else
3098 {
3099 node->AddLeaf( i.GetID() );
3100 }
3101 i = model->GetNextSibling( i );
3102 }
3103 node->SetSubTreeCount( num );
3104 wxDataViewTreeNode * n = node->GetParent();
3105 if( n != NULL)
3106 n->ChangeSubTreeCount(num);
3107
3108 }
3109
3110 void wxDataViewMainWindow::BuildTree(wxDataViewModel * model)
3111 {
3112 //First we define a invalid item to fetch the top-level elements
3113 wxDataViewItem item;
3114 SortPrepare();
3115 BuildTreeHelper( model, item, m_root);
3116 m_count = -1 ;
3117 }
3118
3119 void DestroyTreeHelper( wxDataViewTreeNode * node )
3120 {
3121 if( node->GetNodeNumber() != 0 )
3122 {
3123 int len = node->GetNodeNumber();
3124 int i = 0 ;
3125 wxDataViewTreeNodes nodes = node->GetNodes();
3126 for( ; i < len; i ++ )
3127 {
3128 DestroyTreeHelper(nodes[i]);
3129 }
3130 }
3131 delete node;
3132 }
3133
3134 void wxDataViewMainWindow::DestroyTree()
3135 {
3136 DestroyTreeHelper(m_root);
3137 m_root->SetSubTreeCount(0);
3138 m_count = 0 ;
3139 }
3140
3141 void wxDataViewMainWindow::OnChar( wxKeyEvent &event )
3142 {
3143 if (event.GetKeyCode() == WXK_TAB)
3144 {
3145 wxNavigationKeyEvent nevent;
3146 nevent.SetWindowChange( event.ControlDown() );
3147 nevent.SetDirection( !event.ShiftDown() );
3148 nevent.SetEventObject( GetParent()->GetParent() );
3149 nevent.SetCurrentFocus( m_parent );
3150 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent ))
3151 return;
3152 }
3153
3154 // no item -> nothing to do
3155 if (!HasCurrentRow())
3156 {
3157 event.Skip();
3158 return;
3159 }
3160
3161 // don't use m_linesPerPage directly as it might not be computed yet
3162 const int pageSize = GetCountPerPage();
3163 wxCHECK_RET( pageSize, _T("should have non zero page size") );
3164
3165 switch ( event.GetKeyCode() )
3166 {
3167 case WXK_UP:
3168 if ( m_currentRow > 0 )
3169 OnArrowChar( m_currentRow - 1, event );
3170 break;
3171
3172 case WXK_DOWN:
3173 if ( m_currentRow < GetRowCount() - 1 )
3174 OnArrowChar( m_currentRow + 1, event );
3175 break;
3176 //Add the process for tree expanding/collapsing
3177 case WXK_LEFT:
3178 OnCollapsing(m_currentRow);
3179 break;
3180 case WXK_RIGHT:
3181 OnExpanding( m_currentRow);
3182 break;
3183 case WXK_END:
3184 if (!IsEmpty())
3185 OnArrowChar( GetRowCount() - 1, event );
3186 break;
3187
3188 case WXK_HOME:
3189 if (!IsEmpty())
3190 OnArrowChar( 0, event );
3191 break;
3192
3193 case WXK_PAGEUP:
3194 {
3195 int steps = pageSize - 1;
3196 int index = m_currentRow - steps;
3197 if (index < 0)
3198 index = 0;
3199
3200 OnArrowChar( index, event );
3201 }
3202 break;
3203
3204 case WXK_PAGEDOWN:
3205 {
3206 int steps = pageSize - 1;
3207 unsigned int index = m_currentRow + steps;
3208 unsigned int count = GetRowCount();
3209 if ( index >= count )
3210 index = count - 1;
3211
3212 OnArrowChar( index, event );
3213 }
3214 break;
3215
3216 default:
3217 event.Skip();
3218 }
3219 }
3220
3221 void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
3222 {
3223 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
3224 {
3225 // let the base handle mouse wheel events.
3226 event.Skip();
3227 return;
3228 }
3229
3230 int x = event.GetX();
3231 int y = event.GetY();
3232 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
3233
3234 wxDataViewColumn *col = NULL;
3235
3236 int xpos = 0;
3237 unsigned int cols = GetOwner()->GetColumnCount();
3238 unsigned int i;
3239 for (i = 0; i < cols; i++)
3240 {
3241 wxDataViewColumn *c = GetOwner()->GetColumn( i );
3242 if (c->IsHidden())
3243 continue; // skip it!
3244
3245 if (x < xpos + c->GetWidth())
3246 {
3247 col = c;
3248 break;
3249 }
3250 xpos += c->GetWidth();
3251 }
3252 if (!col)
3253 return;
3254 wxDataViewRenderer *cell = col->GetRenderer();
3255
3256 unsigned int current = y / m_lineHeight;
3257
3258 if ((current > GetRowCount()) || (x > GetEndOfLastCol()))
3259 {
3260 // Unselect all if below the last row ?
3261 return;
3262 }
3263
3264 wxDataViewModel *model = GetOwner()->GetModel();
3265
3266 if (event.Dragging())
3267 {
3268 if (m_dragCount == 0)
3269 {
3270 // we have to report the raw, physical coords as we want to be
3271 // able to call HitTest(event.m_pointDrag) from the user code to
3272 // get the item being dragged
3273 m_dragStart = event.GetPosition();
3274 }
3275
3276 m_dragCount++;
3277
3278 if (m_dragCount != 3)
3279 return;
3280
3281 if (event.LeftIsDown())
3282 {
3283 // Notify cell about drag
3284 }
3285 return;
3286 }
3287 else
3288 {
3289 m_dragCount = 0;
3290 }
3291
3292 bool forceClick = false;
3293
3294 if (event.ButtonDClick())
3295 {
3296 m_renameTimer->Stop();
3297 m_lastOnSame = false;
3298 }
3299
3300 if (event.LeftDClick())
3301 {
3302 if ( current == m_lineLastClicked )
3303 {
3304 if (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE)
3305 {
3306 wxDataViewItem item = GetItemByRow(current);
3307 wxVariant value;
3308 model->GetValue( value, item, col->GetModelColumn() );
3309 cell->SetValue( value );
3310 wxRect cell_rect( xpos, current * m_lineHeight,
3311 col->GetWidth(), m_lineHeight );
3312 cell->Activate( cell_rect, model, item, col->GetModelColumn() );
3313
3314 wxWindow *parent = GetParent();
3315 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, parent->GetId());
3316
3317 le.SetEventObject(parent);
3318 le.SetColumn(col->GetModelColumn());
3319 le.SetDataViewColumn(col);
3320 le.SetModel(GetOwner()->GetModel());
3321
3322 parent->GetEventHandler()->ProcessEvent(le);
3323 }
3324 return;
3325 }
3326 else
3327 {
3328 // The first click was on another item, so don't interpret this as
3329 // a double click, but as a simple click instead
3330 forceClick = true;
3331 }
3332 }
3333
3334 if (event.LeftUp())
3335 {
3336 if (m_lineSelectSingleOnUp != (unsigned int)-1)
3337 {
3338 // select single line
3339 SelectAllRows( false );
3340 SelectRow( m_lineSelectSingleOnUp, true );
3341 }
3342
3343 //Process the event of user clicking the expander
3344 bool expander = false;
3345 wxDataViewTreeNode * node = GetTreeNodeByRow(current);
3346 if( node!=NULL && node->HasChildren() )
3347 {
3348 int indent = node->GetIndentLevel();
3349 indent = GetOwner()->GetIndent()*indent;
3350 wxRect rect( xpos + indent + EXPANDER_MARGIN, current * m_lineHeight + EXPANDER_MARGIN, m_lineHeight-2*EXPANDER_MARGIN,m_lineHeight-2*EXPANDER_MARGIN);
3351 if( rect.Contains( x, y) )
3352 {
3353 expander = true;
3354 if( node->IsOpen() )
3355 OnCollapsing(current);
3356 else
3357 OnExpanding( current );
3358 }
3359 }
3360
3361 //If the user click the expander, we do not do editing even if the column with expander are editable
3362 if (m_lastOnSame && !expander )
3363 {
3364 if ((col == m_currentCol) && (current == m_currentRow) &&
3365 (cell->GetMode() == wxDATAVIEW_CELL_EDITABLE) )
3366 {
3367 m_renameTimer->Start( 100, true );
3368 }
3369 }
3370
3371 m_lastOnSame = false;
3372 m_lineSelectSingleOnUp = (unsigned int)-1;
3373 }
3374 else
3375 {
3376 // This is necessary, because after a DnD operation in
3377 // from and to ourself, the up event is swallowed by the
3378 // DnD code. So on next non-up event (which means here and
3379 // now) m_lineSelectSingleOnUp should be reset.
3380 m_lineSelectSingleOnUp = (unsigned int)-1;
3381 }
3382
3383 if (event.RightDown())
3384 {
3385 m_lineBeforeLastClicked = m_lineLastClicked;
3386 m_lineLastClicked = current;
3387
3388 // If the item is already selected, do not update the selection.
3389 // Multi-selections should not be cleared if a selected item is clicked.
3390 if (!IsRowSelected(current))
3391 {
3392 SelectAllRows(false);
3393 ChangeCurrentRow(current);
3394 SelectRow(m_currentRow,true);
3395 }
3396
3397 // notify cell about right click
3398 // cell->...
3399
3400 // Allow generation of context menu event
3401 event.Skip();
3402 }
3403 else if (event.MiddleDown())
3404 {
3405 // notify cell about middle click
3406 // cell->...
3407 }
3408 if (event.LeftDown() || forceClick)
3409 {
3410 #ifdef __WXMSW__
3411 SetFocus();
3412 #endif
3413
3414 m_lineBeforeLastClicked = m_lineLastClicked;
3415 m_lineLastClicked = current;
3416
3417 unsigned int oldCurrentRow = m_currentRow;
3418 bool oldWasSelected = IsRowSelected(m_currentRow);
3419
3420 bool cmdModifierDown = event.CmdDown();
3421 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
3422 {
3423 if ( IsSingleSel() || !IsRowSelected(current) )
3424 {
3425 SelectAllRows( false );
3426
3427 ChangeCurrentRow(current);
3428
3429 SelectRow(m_currentRow,true);
3430 }
3431 else // multi sel & current is highlighted & no mod keys
3432 {
3433 m_lineSelectSingleOnUp = current;
3434 ChangeCurrentRow(current); // change focus
3435 }
3436 }
3437 else // multi sel & either ctrl or shift is down
3438 {
3439 if (cmdModifierDown)
3440 {
3441 ChangeCurrentRow(current);
3442
3443 ReverseRowSelection(m_currentRow);
3444 }
3445 else if (event.ShiftDown())
3446 {
3447 ChangeCurrentRow(current);
3448
3449 unsigned int lineFrom = oldCurrentRow,
3450 lineTo = current;
3451
3452 if ( lineTo < lineFrom )
3453 {
3454 lineTo = lineFrom;
3455 lineFrom = m_currentRow;
3456 }
3457
3458 SelectRows(lineFrom, lineTo, true);
3459 }
3460 else // !ctrl, !shift
3461 {
3462 // test in the enclosing if should make it impossible
3463 wxFAIL_MSG( _T("how did we get here?") );
3464 }
3465 }
3466
3467 if (m_currentRow != oldCurrentRow)
3468 RefreshRow( oldCurrentRow );
3469
3470 wxDataViewColumn *oldCurrentCol = m_currentCol;
3471
3472 // Update selection here...
3473 m_currentCol = col;
3474
3475 m_lastOnSame = !forceClick && ((col == oldCurrentCol) &&
3476 (current == oldCurrentRow)) && oldWasSelected;
3477 }
3478 }
3479
3480 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event )
3481 {
3482 m_hasFocus = true;
3483
3484 if (HasCurrentRow())
3485 Refresh();
3486
3487 event.Skip();
3488 }
3489
3490 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event )
3491 {
3492 m_hasFocus = false;
3493
3494 if (HasCurrentRow())
3495 Refresh();
3496
3497 event.Skip();
3498 }
3499
3500 wxDataViewItem wxDataViewMainWindow::GetSelection() const
3501 {
3502 if( m_selection.GetCount() != 1 )
3503 return wxDataViewItem();
3504
3505 return GetItemByRow( m_selection.Item(0));
3506 }
3507
3508 //-----------------------------------------------------------------------------
3509 // wxDataViewCtrl
3510 //-----------------------------------------------------------------------------
3511 WX_DEFINE_LIST(wxDataViewColumnList);
3512
3513 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
3514
3515 BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase)
3516 EVT_SIZE(wxDataViewCtrl::OnSize)
3517 END_EVENT_TABLE()
3518
3519 wxDataViewCtrl::~wxDataViewCtrl()
3520 {
3521 if (m_notifier)
3522 GetModel()->RemoveNotifier( m_notifier );
3523 }
3524
3525 void wxDataViewCtrl::Init()
3526 {
3527 m_notifier = NULL;
3528 }
3529
3530 bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
3531 const wxPoint& pos, const wxSize& size,
3532 long style, const wxValidator& validator )
3533 {
3534 if (!wxControl::Create( parent, id, pos, size,
3535 style | wxScrolledWindowStyle|wxSUNKEN_BORDER, validator))
3536 return false;
3537
3538 Init();
3539
3540 #ifdef __WXMAC__
3541 MacSetClipChildren( true ) ;
3542 #endif
3543
3544 m_clientArea = new wxDataViewMainWindow( this, wxID_ANY );
3545
3546 if (HasFlag(wxDV_NO_HEADER))
3547 m_headerArea = NULL;
3548 else
3549 m_headerArea = new wxDataViewHeaderWindow( this, wxID_ANY );
3550
3551 SetTargetWindow( m_clientArea );
3552
3553 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
3554 if (m_headerArea)
3555 sizer->Add( m_headerArea, 0, wxGROW );
3556 sizer->Add( m_clientArea, 1, wxGROW );
3557 SetSizer( sizer );
3558
3559 return true;
3560 }
3561
3562 #ifdef __WXMSW__
3563 WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg,
3564 WXWPARAM wParam,
3565 WXLPARAM lParam)
3566 {
3567 WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
3568
3569 #ifndef __WXWINCE__
3570 // we need to process arrows ourselves for scrolling
3571 if ( nMsg == WM_GETDLGCODE )
3572 {
3573 rc |= DLGC_WANTARROWS;
3574 }
3575 #endif
3576
3577 return rc;
3578 }
3579 #endif
3580
3581 void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
3582 {
3583 // We need to override OnSize so that our scrolled
3584 // window a) does call Layout() to use sizers for
3585 // positioning the controls but b) does not query
3586 // the sizer for their size and use that for setting
3587 // the scrollable area as set that ourselves by
3588 // calling SetScrollbar() further down.
3589
3590 Layout();
3591
3592 AdjustScrollbars();
3593 }
3594
3595 bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
3596 {
3597 if (!wxDataViewCtrlBase::AssociateModel( model ))
3598 return false;
3599
3600 m_notifier = new wxGenericDataViewModelNotifier( m_clientArea );
3601
3602 model->AddNotifier( m_notifier );
3603
3604 m_clientArea->BuildTree(model);
3605
3606 m_clientArea->UpdateDisplay();
3607
3608 return true;
3609 }
3610
3611 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
3612 {
3613 if (!wxDataViewCtrlBase::AppendColumn(col))
3614 return false;
3615
3616 m_cols.Append( col );
3617 OnColumnChange();
3618 return true;
3619 }
3620
3621 void wxDataViewCtrl::OnColumnChange()
3622 {
3623 if (m_headerArea)
3624 m_headerArea->UpdateDisplay();
3625
3626 m_clientArea->UpdateDisplay();
3627 }
3628
3629 void wxDataViewCtrl::DoSetExpanderColumn()
3630 {
3631 m_clientArea->UpdateDisplay();
3632 }
3633
3634 void wxDataViewCtrl::DoSetIndent()
3635 {
3636 m_clientArea->UpdateDisplay();
3637 }
3638
3639 unsigned int wxDataViewCtrl::GetColumnCount() const
3640 {
3641 return m_cols.GetCount();
3642 }
3643
3644 wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const
3645 {
3646 wxDataViewColumnList::const_iterator iter;
3647 int i = 0;
3648 for (iter = m_cols.begin(); iter!=m_cols.end(); iter++)
3649 {
3650 if (i == pos)
3651 return *iter;
3652
3653 if ((*iter)->IsHidden())
3654 continue;
3655 i ++;
3656 }
3657 return NULL;
3658 }
3659
3660 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
3661 {
3662 wxDataViewColumnList::Node * ret = m_cols.Find( column );
3663 if (ret == NULL)
3664 return false;
3665
3666 m_cols.Erase(ret);
3667 delete column;
3668 OnColumnChange();
3669
3670 return true;
3671 }
3672
3673 bool wxDataViewCtrl::ClearColumns()
3674 {
3675 m_cols.clear();
3676 OnColumnChange();
3677 return true;
3678 }
3679
3680 //Selection code with wxDataViewItem as parameters
3681 wxDataViewItem wxDataViewCtrl::GetSelection() const
3682 {
3683 return m_clientArea->GetSelection();
3684 }
3685
3686 int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
3687 {
3688 sel.Empty();
3689 wxDataViewSelection selection = m_clientArea->GetSelections();
3690 int len = selection.GetCount();
3691 for( int i = 0; i < len; i ++)
3692 {
3693 unsigned int row = selection[i];
3694 sel.Add( m_clientArea->GetItemByRow( row ) );
3695 }
3696 return len;
3697 }
3698
3699 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
3700 {
3701 wxDataViewSelection selection(wxDataViewSelectionCmp) ;
3702 int len = sel.GetCount();
3703 for( int i = 0; i < len; i ++ )
3704 {
3705 int row = m_clientArea->GetRowByItem( sel[i] );
3706 if( row >= 0 )
3707 selection.Add( static_cast<unsigned int>(row) );
3708 }
3709 m_clientArea->SetSelections( selection );
3710 }
3711
3712 void wxDataViewCtrl::Select( const wxDataViewItem & item )
3713 {
3714 int row = m_clientArea->GetRowByItem( item );
3715 if( row >= 0 )
3716 m_clientArea->SelectRow(row, true);
3717 }
3718
3719 void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
3720 {
3721 int row = m_clientArea->GetRowByItem( item );
3722 if( row >= 0 )
3723 m_clientArea->SelectRow(row, false);
3724 }
3725
3726 bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
3727 {
3728 int row = m_clientArea->GetRowByItem( item );
3729 if( row >= 0 )
3730 {
3731 return m_clientArea->IsRowSelected(row);
3732 }
3733 return false;
3734 }
3735
3736 //Selection code with row number as parameter
3737 int wxDataViewCtrl::GetSelections( wxArrayInt & sel ) const
3738 {
3739 sel.Empty();
3740 wxDataViewSelection selection = m_clientArea->GetSelections();
3741 int len = selection.GetCount();
3742 for( int i = 0; i < len; i ++)
3743 {
3744 unsigned int row = selection[i];
3745 sel.Add( row );
3746 }
3747 return len;
3748 }
3749
3750 void wxDataViewCtrl::SetSelections( const wxArrayInt & sel )
3751 {
3752 wxDataViewSelection selection(wxDataViewSelectionCmp) ;
3753 int len = sel.GetCount();
3754 for( int i = 0; i < len; i ++ )
3755 {
3756 int row = sel[i];
3757 if( row >= 0 )
3758 selection.Add( static_cast<unsigned int>(row) );
3759 }
3760 m_clientArea->SetSelections( selection );
3761 }
3762
3763 void wxDataViewCtrl::Select( int row )
3764 {
3765 if( row >= 0 )
3766 m_clientArea->SelectRow( row, true );
3767 }
3768
3769 void wxDataViewCtrl::Unselect( int row )
3770 {
3771 if( row >= 0 )
3772 m_clientArea->SelectRow(row, false);
3773 }
3774
3775 bool wxDataViewCtrl::IsSelected( int row ) const
3776 {
3777 if( row >= 0 )
3778 return m_clientArea->IsRowSelected(row);
3779 return false;
3780 }
3781
3782 void wxDataViewCtrl::SelectRange( int from, int to )
3783 {
3784 wxArrayInt sel;
3785 for( int i = from; i < to; i ++ )
3786 sel.Add( i );
3787 m_clientArea->Select(sel);
3788 }
3789
3790 void wxDataViewCtrl::UnselectRange( int from, int to )
3791 {
3792 wxDataViewSelection sel = m_clientArea->GetSelections();
3793 for( int i = from; i < to; i ++ )
3794 if( sel.Index( i ) != wxNOT_FOUND )
3795 sel.Remove( i );
3796 m_clientArea->SetSelections(sel);
3797 }
3798
3799 void wxDataViewCtrl::SelectAll()
3800 {
3801 m_clientArea->SelectAllRows(true);
3802 }
3803
3804 void wxDataViewCtrl::UnselectAll()
3805 {
3806 m_clientArea->SelectAllRows(false);
3807 }
3808
3809 void wxDataViewCtrl::EnsureVisible( int row, int column )
3810 {
3811 if( row < 0 )
3812 row = 0;
3813 if( row > m_clientArea->GetRowCount() )
3814 row = m_clientArea->GetRowCount();
3815
3816 int first = m_clientArea->GetFirstVisibleRow();
3817 int last = m_clientArea->GetLastVisibleRow();
3818 if( row < first )
3819 m_clientArea->ScrollTo( row, column );
3820 else if( row > last )
3821 m_clientArea->ScrollTo( row - last + first, column );
3822 else
3823 m_clientArea->ScrollTo( first, column );
3824 }
3825
3826 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem & item, const wxDataViewColumn * column )
3827 {
3828 int row = m_clientArea->GetRowByItem(item);
3829 if( row >= 0 )
3830 {
3831 if( column == NULL )
3832 return EnsureVisible(row, -1);
3833 else
3834 {
3835 int col = 0;
3836 int len = GetColumnCount();
3837 for( int i = 0; i < len; i ++ )
3838 if( GetColumn(i) == column )
3839 {
3840 col = i;
3841 break;
3842 }
3843 EnsureVisible( row, col );
3844 }
3845 }
3846
3847 }
3848
3849 void wxDataViewCtrl::HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column ) const
3850 {
3851 m_clientArea->HitTest(point, item, column);
3852 }
3853
3854 wxRect wxDataViewCtrl::GetItemRect( const wxDataViewItem & item, const wxDataViewColumn* column ) const
3855 {
3856 return m_clientArea->GetItemRect(item, column);
3857 }
3858
3859 wxDataViewItem wxDataViewCtrl::GetItemByRow( unsigned int row ) const
3860 {
3861 return m_clientArea->GetItemByRow( row );
3862 }
3863
3864 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem & item ) const
3865 {
3866 return m_clientArea->GetRowByItem( item );
3867 }
3868
3869 void wxDataViewCtrl::Expand( const wxDataViewItem & item )
3870 {
3871 int row = m_clientArea->GetRowByItem( item );
3872 if (row != -1)
3873 m_clientArea->Expand(row);
3874 }
3875
3876 void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
3877 {
3878 int row = m_clientArea->GetRowByItem( item );
3879 if (row != -1)
3880 m_clientArea->Collapse(row);
3881 }
3882
3883 #endif
3884 // !wxUSE_GENERICDATAVIEWCTRL
3885
3886 #endif
3887 // wxUSE_DATAVIEWCTRL