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