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