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