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