]> git.saurik.com Git - wxWidgets.git/blob - src/generic/datavgen.cpp
Correct drawing placement (16px + 1 px border below)
[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 (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_CENTER:
1641 case wxALIGN_CENTER_HORIZONTAL:
1642 x += (cw - titleSz.GetWidth() - 2 * HEADER_HORIZ_BORDER)/2;
1643 break;
1644 case wxALIGN_RIGHT:
1645 x += cw - titleSz.GetWidth() - HEADER_HORIZ_BORDER;
1646 break;
1647 }
1648
1649 // always center the title vertically:
1650 int y = wxMax((ch - titleSz.GetHeight()) / 2, HEADER_VERT_BORDER);
1651
1652 dc.SetClippingRegion( xpos+HEADER_HORIZ_BORDER,
1653 HEADER_VERT_BORDER,
1654 wxMax(cw - 2 * HEADER_HORIZ_BORDER, 1), // width
1655 wxMax(ch - 2 * HEADER_VERT_BORDER, 1)); // height
1656 dc.DrawText( col->GetTitle(), x, y );
1657 dc.DestroyClippingRegion();
1658
1659 xpos += cw;
1660 }
1661 }
1662
1663 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent &event )
1664 {
1665 GetParent()->SetFocus();
1666 event.Skip();
1667 }
1668
1669 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent &event )
1670 {
1671 // we want to work with logical coords
1672 int x;
1673 m_owner->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
1674 int y = event.GetY();
1675
1676 if (m_isDragging)
1677 {
1678 // we don't draw the line beyond our window,
1679 // but we allow dragging it there
1680 int w = 0;
1681 GetClientSize( &w, NULL );
1682 m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
1683 w -= 6;
1684
1685 if (event.ButtonUp())
1686 {
1687 m_isDragging = false;
1688 if (HasCapture())
1689 ReleaseMouse();
1690
1691 m_dirty = true;
1692 }
1693 m_currentX = wxMax(m_minX + 7, x);
1694
1695 if (m_currentX < w)
1696 {
1697 GetColumn(m_column)->SetWidth(m_currentX - m_minX);
1698 Refresh();
1699 GetOwner()->Refresh();
1700 }
1701
1702 }
1703 else // not dragging
1704 {
1705 m_minX = 0;
1706 m_column = wxNOT_FOUND;
1707
1708 bool hit_border = false;
1709
1710 // end of the current column
1711 int xpos = 0;
1712
1713 // find the column where this event occured
1714 int countCol = m_owner->GetColumnCount();
1715 for (int column = 0; column < countCol; column++)
1716 {
1717 wxDataViewColumn *p = GetColumn(column);
1718
1719 if (p->IsHidden())
1720 continue; // skip if not shown
1721
1722 xpos += p->GetWidth();
1723 m_column = column;
1724 if ((abs(x-xpos) < 3) && (y < 22))
1725 {
1726 hit_border = true;
1727 break;
1728 }
1729
1730 if (x < xpos)
1731 {
1732 // inside the column
1733 break;
1734 }
1735
1736 m_minX = xpos;
1737 }
1738
1739 int old_hover = m_hover;
1740 m_hover = m_column;
1741 if (event.Leaving())
1742 m_hover = wxNOT_FOUND;
1743 if (old_hover != m_hover)
1744 Refresh();
1745
1746 if (m_column == wxNOT_FOUND)
1747 return;
1748
1749 bool resizeable = GetColumn(m_column)->IsResizeable();
1750 if (event.LeftDClick() && resizeable)
1751 {
1752 GetColumn(m_column)->SetWidth(GetOwner()->GetBestColumnWidth(m_column));
1753 Refresh();
1754 }
1755 else if (event.LeftDown() || event.RightUp())
1756 {
1757 if (hit_border && event.LeftDown() && resizeable)
1758 {
1759 m_isDragging = true;
1760 CaptureMouse();
1761 m_currentX = x;
1762 }
1763 else // click on a column
1764 {
1765 wxDataViewModel * model = GetOwner()->GetModel();
1766 wxEventType evt = event.LeftDown() ?
1767 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK :
1768 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK;
1769 SendEvent(evt, m_column);
1770
1771 //Left click the header
1772 if(event.LeftDown())
1773 {
1774 wxDataViewColumn *col = GetColumn(m_column);
1775 if(col->IsSortable())
1776 {
1777 wxDataViewColumn* sortCol = m_owner->GetSortingColumn();
1778 if(model && sortCol == col)
1779 {
1780 bool order = col->IsSortOrderAscending();
1781 col->SetSortOrder(!order);
1782 }
1783 else if(model)
1784 {
1785 m_owner->SetSortingColumn(col);
1786 }
1787 }
1788 UpdateDisplay();
1789 if(model)
1790 model->Resort();
1791 //Send the column sorted event
1792 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, m_column);
1793 }
1794 }
1795 }
1796 else if (event.Moving())
1797 {
1798 if (hit_border && resizeable)
1799 m_currentCursor = m_resizeCursor;
1800 else
1801 m_currentCursor = wxSTANDARD_CURSOR;
1802
1803 SetCursor(*m_currentCursor);
1804 }
1805 }
1806 }
1807
1808 //I must say that this function is deprecated, but I think it is useful to keep it for a time
1809 void wxGenericDataViewHeaderWindow::DrawCurrent()
1810 {
1811 #if 1
1812 GetColumn(m_column)->SetWidth(m_currentX - m_minX);
1813 #else
1814 int x1 = m_currentX;
1815 int y1 = 0;
1816 ClientToScreen (&x1, &y1);
1817
1818 int x2 = m_currentX-1;
1819 #ifdef __WXMSW__
1820 ++x2; // but why ????
1821 #endif
1822 int y2 = 0;
1823 m_owner->GetClientSize( NULL, &y2 );
1824 m_owner->ClientToScreen( &x2, &y2 );
1825
1826 wxScreenDC dc;
1827 dc.SetLogicalFunction(wxINVERT);
1828 dc.SetPen(m_penCurrent);
1829 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1830 AdjustDC(dc);
1831 dc.DrawLine(x1, y1, x2, y2 );
1832 #endif
1833 }
1834
1835 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC& dc)
1836 {
1837 int xpix, x;
1838
1839 m_owner->GetScrollPixelsPerUnit( &xpix, NULL );
1840 m_owner->GetViewStart( &x, NULL );
1841
1842 // shift the DC origin to match the position of the main window horizontal
1843 // scrollbar: this allows us to always use logical coords
1844 dc.SetDeviceOrigin( -x * xpix, 0 );
1845 }
1846
1847 #endif // defined(__WXMSW__)
1848
1849 //-----------------------------------------------------------------------------
1850 // wxDataViewRenameTimer
1851 //-----------------------------------------------------------------------------
1852
1853 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow *owner )
1854 {
1855 m_owner = owner;
1856 }
1857
1858 void wxDataViewRenameTimer::Notify()
1859 {
1860 m_owner->OnRenameTimer();
1861 }
1862
1863 //-----------------------------------------------------------------------------
1864 // wxDataViewMainWindow
1865 //-----------------------------------------------------------------------------
1866
1867 //The tree building helper, declared firstly
1868 void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, wxDataViewTreeNode * node);
1869
1870 int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 )
1871 {
1872 if (row1 > row2) return 1;
1873 if (row1 == row2) return 0;
1874 return -1;
1875 }
1876
1877
1878 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow, wxWindow)
1879
1880 BEGIN_EVENT_TABLE(wxDataViewMainWindow,wxWindow)
1881 EVT_PAINT (wxDataViewMainWindow::OnPaint)
1882 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse)
1883 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus)
1884 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus)
1885 EVT_CHAR (wxDataViewMainWindow::OnChar)
1886 END_EVENT_TABLE()
1887
1888 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID id,
1889 const wxPoint &pos, const wxSize &size, const wxString &name ) :
1890 wxWindow( parent, id, pos, size, wxWANTS_CHARS, name ),
1891 m_selection( wxDataViewSelectionCmp )
1892
1893 {
1894 SetOwner( parent );
1895
1896 m_lastOnSame = false;
1897 m_renameTimer = new wxDataViewRenameTimer( this );
1898
1899 // TODO: user better initial values/nothing selected
1900 m_currentCol = NULL;
1901 m_currentRow = 0;
1902
1903 // TODO: we need to calculate this smartly
1904 m_lineHeight =
1905 #ifdef __WXMSW__
1906 17;
1907 #else
1908 20;
1909 #endif
1910 wxASSERT(m_lineHeight > 2*PADDING_TOPBOTTOM);
1911
1912 m_dragCount = 0;
1913 m_dragStart = wxPoint(0,0);
1914 m_lineLastClicked = (unsigned int) -1;
1915 m_lineBeforeLastClicked = (unsigned int) -1;
1916 m_lineSelectSingleOnUp = (unsigned int) -1;
1917
1918 m_hasFocus = false;
1919
1920 SetBackgroundStyle( wxBG_STYLE_CUSTOM );
1921 SetBackgroundColour( *wxWHITE );
1922
1923 m_penRule = wxPen(GetRuleColour(), 1, wxSOLID);
1924
1925 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1926 m_penExpander = wxPen( wxColour(0,0,0), 1, wxSOLID );
1927 //Some new added code to deal with the tree structure
1928 m_root = new wxDataViewTreeNode( NULL );
1929 m_root->SetHasChildren(true);
1930
1931 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1932 m_count = -1 ;
1933 m_underMouse = NULL;
1934 UpdateDisplay();
1935 }
1936
1937 wxDataViewMainWindow::~wxDataViewMainWindow()
1938 {
1939 DestroyTree();
1940 delete m_renameTimer;
1941 }
1942
1943 void wxDataViewMainWindow::OnRenameTimer()
1944 {
1945 // We have to call this here because changes may just have
1946 // been made and no screen update taken place.
1947 if ( m_dirty )
1948 wxSafeYield();
1949
1950 int xpos = 0;
1951 unsigned int cols = GetOwner()->GetColumnCount();
1952 unsigned int i;
1953 for (i = 0; i < cols; i++)
1954 {
1955 wxDataViewColumn *c = GetOwner()->GetColumn( i );
1956 if (c->IsHidden())
1957 continue; // skip it!
1958
1959 if (c == m_currentCol)
1960 break;
1961 xpos += c->GetWidth();
1962 }
1963 wxRect labelRect( xpos, m_currentRow * m_lineHeight,
1964 m_currentCol->GetWidth(), m_lineHeight );
1965
1966 GetOwner()->CalcScrolledPosition( labelRect.x, labelRect.y,
1967 &labelRect.x, &labelRect.y);
1968
1969 wxDataViewItem item = GetItemByRow( m_currentRow );
1970 m_currentCol->GetRenderer()->StartEditing( item, labelRect );
1971
1972 }
1973
1974 //------------------------------------------------------------------
1975 // Helper class for do operation on the tree node
1976 //------------------------------------------------------------------
1977 class DoJob
1978 {
1979 public:
1980 DoJob(){};
1981 virtual ~DoJob(){};
1982
1983 //The return value control how the tree-walker tranverse the tree
1984 // 0: Job done, stop tranverse and return
1985 // 1: Ignore the current node's subtree and continue
1986 // 2: Job not done, continue
1987 enum { OK = 0 , IGR = 1, CONT = 2 };
1988 virtual int operator() ( wxDataViewTreeNode * node ) = 0 ;
1989 virtual int operator() ( void * n ) = 0;
1990 };
1991
1992 bool Walker( wxDataViewTreeNode * node, DoJob & func )
1993 {
1994 if( node==NULL )
1995 return false;
1996
1997 switch( func( node ) )
1998 {
1999 case DoJob::OK :
2000 return true ;
2001 case DoJob::IGR:
2002 return false;
2003 case DoJob::CONT:
2004 default:
2005 ;
2006 }
2007
2008 wxDataViewTreeNodes nodes = node->GetNodes();
2009 wxDataViewTreeLeaves leaves = node->GetChildren();
2010
2011 int len_nodes = nodes.GetCount();
2012 int len = leaves.GetCount();
2013 int i = 0, nodes_i = 0;
2014
2015 for( ; i < len ; i ++ )
2016 {
2017 void * n = leaves[i];
2018 if( nodes_i < len_nodes && n == nodes[nodes_i]->GetItem().GetID() )
2019 {
2020 wxDataViewTreeNode * nd = nodes[nodes_i];
2021 nodes_i++;
2022
2023 if( Walker( nd , func ) )
2024 return true;
2025
2026 }
2027 else
2028 switch( func( n ) )
2029 {
2030 case DoJob::OK :
2031 return true ;
2032 case DoJob::IGR:
2033 continue;
2034 case DoJob::CONT:
2035 default:
2036 ;
2037 }
2038 }
2039 return false;
2040 }
2041
2042 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxDataViewItem & item)
2043 {
2044 SortPrepare();
2045
2046 wxDataViewTreeNode * node;
2047 node = FindNode(parent);
2048
2049 if( node == NULL )
2050 return false;
2051
2052 node->SetHasChildren( true );
2053
2054 if( g_model->IsContainer( item ) )
2055 {
2056 wxDataViewTreeNode * newnode = new wxDataViewTreeNode( node );
2057 newnode->SetItem(item);
2058 newnode->SetHasChildren( true );
2059 node->AddNode( newnode);
2060 }
2061 else
2062 node->AddLeaf( item.GetID() );
2063
2064 node->ChangeSubTreeCount(1);
2065
2066 m_count = -1;
2067 UpdateDisplay();
2068
2069 return true;
2070 }
2071
2072 void DestroyTreeHelper( wxDataViewTreeNode * node);
2073
2074 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent,
2075 const wxDataViewItem& item)
2076 {
2077 wxDataViewTreeNode * node = FindNode(parent);
2078
2079 wxCHECK_MSG( node != NULL, false, "item not found" );
2080 wxCHECK_MSG( node->GetChildren().Index( item.GetID() ) != wxNOT_FOUND, false, "item not found" );
2081
2082 int sub = -1;
2083 node->GetChildren().Remove( item.GetID() );
2084 //Manuplate selection
2085 if( m_selection.GetCount() > 1 )
2086 {
2087 m_selection.Empty();
2088 }
2089 bool isContainer = false;
2090 wxDataViewTreeNodes nds = node->GetNodes();
2091 for (size_t i = 0; i < nds.GetCount(); i ++)
2092 {
2093 if (nds[i]->GetItem() == item)
2094 {
2095 isContainer = true;
2096 break;
2097 }
2098 }
2099 if( isContainer )
2100 {
2101 wxDataViewTreeNode * n = NULL;
2102 wxDataViewTreeNodes nodes = node->GetNodes();
2103 int len = nodes.GetCount();
2104 for( int i = 0 ; i < len; i ++)
2105 {
2106 if( nodes[i]->GetItem() == item )
2107 {
2108 n = nodes[i];
2109 break;
2110 }
2111 }
2112
2113 wxCHECK_MSG( n != NULL, false, "item not found" );
2114
2115 node->GetNodes().Remove( n );
2116 sub -= n->GetSubTreeCount();
2117 DestroyTreeHelper(n);
2118 }
2119 //Make the row number invalid and get a new valid one when user call GetRowCount
2120 m_count = -1;
2121 node->ChangeSubTreeCount(sub);
2122 if( node->GetChildrenNumber() == 0)
2123 {
2124 node->GetParent()->GetNodes().Remove( node );
2125 delete node;
2126 }
2127
2128 //Change the current row to the last row if the current exceed the max row number
2129 if( m_currentRow > GetRowCount() )
2130 m_currentRow = m_count - 1;
2131
2132 UpdateDisplay();
2133
2134 return true;
2135 }
2136
2137 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem & item)
2138 {
2139 SortPrepare();
2140 g_model->Resort();
2141
2142 //Send event
2143 wxWindow *parent = GetParent();
2144 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId());
2145 le.SetEventObject(parent);
2146 le.SetModel(GetOwner()->GetModel());
2147 le.SetItem(item);
2148 parent->GetEventHandler()->ProcessEvent(le);
2149
2150 return true;
2151 }
2152
2153 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem & item, unsigned int col )
2154 {
2155 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2156 /*#define MAX_VIRTUAL_WIDTH 100000
2157
2158 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2159 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2160 Refresh( true, &rect );
2161
2162 return true;
2163 */
2164 SortPrepare();
2165 g_model->Resort();
2166
2167 //Send event
2168 wxWindow *parent = GetParent();
2169 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, parent->GetId());
2170 le.SetEventObject(parent);
2171 le.SetModel(GetOwner()->GetModel());
2172 le.SetItem(item);
2173 le.SetColumn(col);
2174 le.SetDataViewColumn(GetOwner()->GetColumn(col));
2175 parent->GetEventHandler()->ProcessEvent(le);
2176
2177 return true;
2178 }
2179
2180 bool wxDataViewMainWindow::Cleared()
2181 {
2182 SortPrepare();
2183
2184 DestroyTree();
2185 UpdateDisplay();
2186
2187 return true;
2188 }
2189
2190 void wxDataViewMainWindow::UpdateDisplay()
2191 {
2192 m_dirty = true;
2193 }
2194
2195 void wxDataViewMainWindow::OnInternalIdle()
2196 {
2197 wxWindow::OnInternalIdle();
2198
2199 if (m_dirty)
2200 {
2201 RecalculateDisplay();
2202 m_dirty = false;
2203 }
2204 }
2205
2206 void wxDataViewMainWindow::RecalculateDisplay()
2207 {
2208 wxDataViewModel *model = GetOwner()->GetModel();
2209 if (!model)
2210 {
2211 Refresh();
2212 return;
2213 }
2214
2215 int width = GetEndOfLastCol();
2216 int height = GetRowCount() * m_lineHeight;
2217
2218 SetVirtualSize( width, height );
2219 GetOwner()->SetScrollRate( 10, m_lineHeight );
2220
2221 Refresh();
2222 }
2223
2224 void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
2225 {
2226 wxWindow::ScrollWindow( dx, dy, rect );
2227
2228 if (GetOwner()->m_headerArea)
2229 GetOwner()->m_headerArea->ScrollWindow( dx, 0 );
2230 }
2231
2232 void wxDataViewMainWindow::ScrollTo( int rows, int column )
2233 {
2234 int x, y;
2235 m_owner->GetScrollPixelsPerUnit( &x, &y );
2236 int sy = rows*m_lineHeight/y;
2237 int sx = 0;
2238 if( column != -1 )
2239 {
2240 wxRect rect = GetClientRect();
2241 unsigned int colnum = 0;
2242 unsigned int x_start = 0, x_end = 0, w = 0;
2243 int xx, yy, xe;
2244 m_owner->CalcUnscrolledPosition( rect.x, rect.y, &xx, &yy );
2245 for (x_start = 0; colnum < column; colnum++)
2246 {
2247 wxDataViewColumn *col = GetOwner()->GetColumn(colnum);
2248 if (col->IsHidden())
2249 continue; // skip it!
2250
2251 w = col->GetWidth();
2252 x_start += w;
2253 }
2254
2255 x_end = x_start + w;
2256 xe = xx + rect.width;
2257 if( x_end > xe )
2258 {
2259 sx = ( xx + x_end - xe )/x;
2260 }
2261 if( x_start < xx )
2262 {
2263 sx = x_start/x;
2264 }
2265 }
2266 m_owner->Scroll( sx, sy );
2267 }
2268
2269 void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
2270 {
2271 wxDataViewModel *model = GetOwner()->GetModel();
2272 wxAutoBufferedPaintDC dc( this );
2273
2274 // prepare the DC
2275 dc.SetBackground(GetBackgroundColour());
2276 dc.Clear();
2277 GetOwner()->PrepareDC( dc );
2278 dc.SetFont( GetFont() );
2279
2280 wxRect update = GetUpdateRegion().GetBox();
2281 m_owner->CalcUnscrolledPosition( update.x, update.y, &update.x, &update.y );
2282
2283 // compute which items needs to be redrawn
2284 unsigned int item_start = wxMax( 0, (update.y / m_lineHeight) );
2285 unsigned int item_count =
2286 wxMin( (int)(((update.y + update.height) / m_lineHeight) - item_start + 1),
2287 (int)(GetRowCount( )- item_start) );
2288 unsigned int item_last = item_start + item_count;
2289
2290 // compute which columns needs to be redrawn
2291 unsigned int cols = GetOwner()->GetColumnCount();
2292 unsigned int col_start = 0;
2293 unsigned int x_start = 0;
2294 for (x_start = 0; col_start < cols; col_start++)
2295 {
2296 wxDataViewColumn *col = GetOwner()->GetColumn(col_start);
2297 if (col->IsHidden())
2298 continue; // skip it!
2299
2300 unsigned int w = col->GetWidth();
2301 if (x_start+w >= (unsigned int)update.x)
2302 break;
2303
2304 x_start += w;
2305 }
2306
2307 unsigned int col_last = col_start;
2308 unsigned int x_last = x_start;
2309 for (; col_last < cols; col_last++)
2310 {
2311 wxDataViewColumn *col = GetOwner()->GetColumn(col_last);
2312 if (col->IsHidden())
2313 continue; // skip it!
2314
2315 if (x_last > (unsigned int)update.GetRight())
2316 break;
2317
2318 x_last += col->GetWidth();
2319 }
2320
2321 // Draw horizontal rules if required
2322 if ( m_owner->HasFlag(wxDV_HORIZ_RULES) )
2323 {
2324 dc.SetPen(m_penRule);
2325 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2326
2327 for (unsigned int i = item_start; i <= item_last+1; i++)
2328 {
2329 int y = i * m_lineHeight;
2330 dc.DrawLine(x_start, y, x_last, y);
2331 }
2332 }
2333
2334 // Draw vertical rules if required
2335 if ( m_owner->HasFlag(wxDV_VERT_RULES) )
2336 {
2337 dc.SetPen(m_penRule);
2338 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2339
2340 int x = x_start;
2341 for (unsigned int i = col_start; i < col_last; i++)
2342 {
2343 wxDataViewColumn *col = GetOwner()->GetColumn(i);
2344 if (col->IsHidden())
2345 continue; // skip it
2346
2347 dc.DrawLine(x, item_start * m_lineHeight,
2348 x, item_last * m_lineHeight);
2349
2350 x += col->GetWidth();
2351 }
2352
2353 // Draw last vertical rule
2354 dc.DrawLine(x, item_start * m_lineHeight,
2355 x, item_last * m_lineHeight);
2356 }
2357
2358 // redraw the background for the items which are selected/current
2359 for (unsigned int item = item_start; item < item_last; item++)
2360 {
2361 bool selected = m_selection.Index( item ) != wxNOT_FOUND;
2362 if (selected || item == m_currentRow)
2363 {
2364 int flags = selected ? (int)wxCONTROL_SELECTED : 0;
2365 if (item == m_currentRow)
2366 flags |= wxCONTROL_CURRENT;
2367 if (m_hasFocus)
2368 flags |= wxCONTROL_FOCUSED;
2369
2370 wxRect rect( x_start, item*m_lineHeight, x_last, m_lineHeight );
2371 wxRendererNative::Get().DrawItemSelectionRect
2372 (
2373 this,
2374 dc,
2375 rect,
2376 flags
2377 );
2378 }
2379 }
2380
2381 wxDataViewColumn *expander = GetOwner()->GetExpanderColumn();
2382 if (!expander)
2383 {
2384 // TODO: last column for RTL support
2385 expander = GetOwner()->GetColumn( 0 );
2386 GetOwner()->SetExpanderColumn(expander);
2387 }
2388
2389 // redraw all cells for all rows which must be repainted and for all columns
2390 wxRect cell_rect;
2391 cell_rect.x = x_start;
2392 cell_rect.height = m_lineHeight; // -1 is for the horizontal rules
2393 for (unsigned int i = col_start; i < col_last; i++)
2394 {
2395 wxDataViewColumn *col = GetOwner()->GetColumn( i );
2396 wxDataViewRenderer *cell = col->GetRenderer();
2397 cell_rect.width = col->GetWidth();
2398
2399 if (col->IsHidden())
2400 continue; // skipt it!
2401
2402
2403 for (unsigned int item = item_start; item < item_last; item++)
2404 {
2405 // get the cell value and set it into the renderer
2406 wxVariant value;
2407 wxDataViewTreeNode * node = GetTreeNodeByRow(item);
2408 if( node == NULL )
2409 {
2410 continue;
2411 }
2412
2413 wxDataViewItem dataitem = node->GetItem();
2414 model->GetValue( value, dataitem, col->GetModelColumn());
2415 cell->SetValue( value );
2416
2417 // update the y offset
2418 cell_rect.y = item * m_lineHeight;
2419
2420 //Draw the expander here.
2421 int indent = node->GetIndentLevel();
2422 if( col == expander )
2423 {
2424 //Calculate the indent first
2425 indent = cell_rect.x + GetOwner()->GetIndent() * indent;
2426
2427 int expander_width = m_lineHeight - 2*EXPANDER_MARGIN;
2428 // change the cell_rect.x to the appropriate pos
2429 int expander_x = indent + EXPANDER_MARGIN , expander_y = cell_rect.y + EXPANDER_MARGIN ;
2430 indent = indent + m_lineHeight ; //try to use the m_lineHeight as the expander space
2431 dc.SetPen( m_penExpander );
2432 dc.SetBrush( wxNullBrush );
2433 if( node->HasChildren() )
2434 {
2435 wxRect rect( expander_x , expander_y, expander_width, expander_width);
2436 int flag = 0;
2437 if (m_underMouse == node)
2438 {
2439 flag |= wxCONTROL_CURRENT;
2440 }
2441 if( node->IsOpen() )
2442 wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, flag|wxCONTROL_EXPANDED );
2443 else
2444 wxRendererNative::Get().DrawTreeItemButton( this, dc, rect, flag);
2445 }
2446 else
2447 {
2448 // I am wandering whether we should draw dot lines between tree nodes
2449 delete node;
2450 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2451 }
2452
2453 //force the expander column to left-center align
2454 cell->SetAlignment( wxALIGN_CENTER_VERTICAL );
2455 }
2456
2457
2458 // cannot be bigger than allocated space
2459 wxSize size = cell->GetSize();
2460 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2461 size.x = wxMin( size.x + 2*PADDING_RIGHTLEFT, cell_rect.width - indent );
2462 size.y = wxMin( size.y + 1*PADDING_TOPBOTTOM, cell_rect.height );
2463
2464 wxRect item_rect(cell_rect.GetTopLeft(), size);
2465 int align = cell->GetAlignment();
2466
2467 // horizontal alignment:
2468 item_rect.x = cell_rect.x;
2469 if (align & wxALIGN_CENTER_HORIZONTAL)
2470 item_rect.x = cell_rect.x + (cell_rect.width / 2) - (size.x / 2);
2471 else if (align & wxALIGN_RIGHT)
2472 item_rect.x = cell_rect.x + cell_rect.width - size.x;
2473 //else: wxALIGN_LEFT is the default
2474
2475 // vertical alignment:
2476 item_rect.y = cell_rect.y;
2477 if (align & wxALIGN_CENTER_VERTICAL)
2478 item_rect.y = cell_rect.y + (cell_rect.height / 2) - (size.y / 2);
2479 else if (align & wxALIGN_BOTTOM)
2480 item_rect.y = cell_rect.y + cell_rect.height - size.y;
2481 //else: wxALIGN_TOP is the default
2482
2483 // add padding
2484 item_rect.x += PADDING_RIGHTLEFT;
2485 //item_rect.y += PADDING_TOPBOTTOM;
2486 item_rect.width = size.x - 2 * PADDING_RIGHTLEFT;
2487 item_rect.height = size.y - 1 * PADDING_TOPBOTTOM;
2488
2489 //Here we add the tree indent
2490 item_rect.x += indent;
2491
2492 int state = 0;
2493 if (m_selection.Index(item) != wxNOT_FOUND)
2494 state |= wxDATAVIEW_CELL_SELECTED;
2495
2496 // TODO: it would be much more efficient to create a clipping
2497 // region for the entire column being rendered (in the OnPaint
2498 // of wxDataViewMainWindow) instead of a single clip region for
2499 // each cell. However it would mean that each renderer should
2500 // respect the given wxRect's top & bottom coords, eventually
2501 // violating only the left & right coords - however the user can
2502 // make its own renderer and thus we cannot be sure of that.
2503 dc.SetClippingRegion( item_rect );
2504 cell->Render( item_rect, &dc, state );
2505 dc.DestroyClippingRegion();
2506 }
2507
2508 cell_rect.x += cell_rect.width;
2509 }
2510 }
2511
2512 int wxDataViewMainWindow::GetCountPerPage() const
2513 {
2514 wxSize size = GetClientSize();
2515 return size.y / m_lineHeight;
2516 }
2517
2518 int wxDataViewMainWindow::GetEndOfLastCol() const
2519 {
2520 int width = 0;
2521 unsigned int i;
2522 for (i = 0; i < GetOwner()->GetColumnCount(); i++)
2523 {
2524 const wxDataViewColumn *c =
2525 wx_const_cast(wxDataViewCtrl*, GetOwner())->GetColumn( i );
2526
2527 if (!c->IsHidden())
2528 width += c->GetWidth();
2529 }
2530 return width;
2531 }
2532
2533 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2534 {
2535 int x = 0;
2536 int y = 0;
2537 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
2538
2539 return y / m_lineHeight;
2540 }
2541
2542 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2543 {
2544 wxSize client_size = GetClientSize();
2545 m_owner->CalcUnscrolledPosition( client_size.x, client_size.y,
2546 &client_size.x, &client_size.y );
2547
2548 //we should deal with the pixel here
2549 unsigned int row = (client_size.y)/m_lineHeight;
2550 if( client_size.y % m_lineHeight < m_lineHeight/2 )
2551 row -= 1;
2552
2553 return wxMin( GetRowCount()-1, row );
2554 }
2555
2556 unsigned int wxDataViewMainWindow::GetRowCount()
2557 {
2558 if ( m_count == -1 )
2559 {
2560 m_count = RecalculateCount();
2561 int width, height;
2562 GetVirtualSize( &width, &height );
2563 height = m_count * m_lineHeight;
2564
2565 SetVirtualSize( width, height );
2566 }
2567 return m_count;
2568 }
2569
2570 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row )
2571 {
2572 m_currentRow = row;
2573
2574 // send event
2575 }
2576
2577 void wxDataViewMainWindow::SelectAllRows( bool on )
2578 {
2579 if (IsEmpty())
2580 return;
2581
2582 if (on)
2583 {
2584 m_selection.Clear();
2585 for (unsigned int i = 0; i < GetRowCount(); i++)
2586 m_selection.Add( i );
2587 Refresh();
2588 }
2589 else
2590 {
2591 unsigned int first_visible = GetFirstVisibleRow();
2592 unsigned int last_visible = GetLastVisibleRow();
2593 unsigned int i;
2594 for (i = 0; i < m_selection.GetCount(); i++)
2595 {
2596 unsigned int row = m_selection[i];
2597 if ((row >= first_visible) && (row <= last_visible))
2598 RefreshRow( row );
2599 }
2600 m_selection.Clear();
2601 }
2602 }
2603
2604 void wxDataViewMainWindow::SelectRow( unsigned int row, bool on )
2605 {
2606 if (m_selection.Index( row ) == wxNOT_FOUND)
2607 {
2608 if (on)
2609 {
2610 m_selection.Add( row );
2611 RefreshRow( row );
2612 }
2613 }
2614 else
2615 {
2616 if (!on)
2617 {
2618 m_selection.Remove( row );
2619 RefreshRow( row );
2620 }
2621 }
2622 }
2623
2624 void wxDataViewMainWindow::SelectRows( unsigned int from, unsigned int to, bool on )
2625 {
2626 if (from > to)
2627 {
2628 unsigned int tmp = from;
2629 from = to;
2630 to = tmp;
2631 }
2632
2633 unsigned int i;
2634 for (i = from; i <= to; i++)
2635 {
2636 if (m_selection.Index( i ) == wxNOT_FOUND)
2637 {
2638 if (on)
2639 m_selection.Add( i );
2640 }
2641 else
2642 {
2643 if (!on)
2644 m_selection.Remove( i );
2645 }
2646 }
2647 RefreshRows( from, to );
2648 }
2649
2650 void wxDataViewMainWindow::Select( const wxArrayInt& aSelections )
2651 {
2652 for (size_t i=0; i < aSelections.GetCount(); i++)
2653 {
2654 int n = aSelections[i];
2655
2656 m_selection.Add( n );
2657 RefreshRow( n );
2658 }
2659 }
2660
2661 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row )
2662 {
2663 if (m_selection.Index( row ) == wxNOT_FOUND)
2664 m_selection.Add( row );
2665 else
2666 m_selection.Remove( row );
2667 RefreshRow( row );
2668 }
2669
2670 bool wxDataViewMainWindow::IsRowSelected( unsigned int row )
2671 {
2672 return (m_selection.Index( row ) != wxNOT_FOUND);
2673 }
2674
2675 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem& item)
2676 {
2677 wxWindow *parent = GetParent();
2678 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, parent->GetId());
2679
2680 le.SetEventObject(parent);
2681 le.SetModel(GetOwner()->GetModel());
2682 le.SetItem( item );
2683
2684 parent->GetEventHandler()->ProcessEvent(le);
2685 }
2686
2687 void wxDataViewMainWindow::RefreshRow( unsigned int row )
2688 {
2689 wxRect rect( 0, row*m_lineHeight, GetEndOfLastCol(), m_lineHeight );
2690 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2691
2692 wxSize client_size = GetClientSize();
2693 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2694 wxRect intersect_rect = client_rect.Intersect( rect );
2695 if (intersect_rect.width > 0)
2696 Refresh( true, &intersect_rect );
2697 }
2698
2699 void wxDataViewMainWindow::RefreshRows( unsigned int from, unsigned int to )
2700 {
2701 if (from > to)
2702 {
2703 unsigned int tmp = to;
2704 to = from;
2705 from = tmp;
2706 }
2707
2708 wxRect rect( 0, from*m_lineHeight, GetEndOfLastCol(), (to-from+1) * m_lineHeight );
2709 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2710
2711 wxSize client_size = GetClientSize();
2712 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2713 wxRect intersect_rect = client_rect.Intersect( rect );
2714 if (intersect_rect.width > 0)
2715 Refresh( true, &intersect_rect );
2716 }
2717
2718 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow )
2719 {
2720 unsigned int count = GetRowCount();
2721 if (firstRow > count)
2722 return;
2723
2724 wxRect rect( 0, firstRow*m_lineHeight, GetEndOfLastCol(), count * m_lineHeight );
2725 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2726
2727 wxSize client_size = GetClientSize();
2728 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2729 wxRect intersect_rect = client_rect.Intersect( rect );
2730 if (intersect_rect.width > 0)
2731 Refresh( true, &intersect_rect );
2732 }
2733
2734 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event)
2735 {
2736 wxCHECK_RET( newCurrent < GetRowCount(),
2737 _T("invalid item index in OnArrowChar()") );
2738
2739 // if there is no selection, we cannot move it anywhere
2740 if (!HasCurrentRow())
2741 return;
2742
2743 unsigned int oldCurrent = m_currentRow;
2744
2745 // in single selection we just ignore Shift as we can't select several
2746 // items anyhow
2747 if ( event.ShiftDown() && !IsSingleSel() )
2748 {
2749 RefreshRow( oldCurrent );
2750
2751 ChangeCurrentRow( newCurrent );
2752
2753 // select all the items between the old and the new one
2754 if ( oldCurrent > newCurrent )
2755 {
2756 newCurrent = oldCurrent;
2757 oldCurrent = m_currentRow;
2758 }
2759
2760 SelectRows( oldCurrent, newCurrent, true );
2761 if (oldCurrent!=newCurrent)
2762 SendSelectionChangedEvent(GetItemByRow(m_selection[0]));
2763 }
2764 else // !shift
2765 {
2766 RefreshRow( oldCurrent );
2767
2768 // all previously selected items are unselected unless ctrl is held
2769 if ( !event.ControlDown() )
2770 SelectAllRows(false);
2771
2772 ChangeCurrentRow( newCurrent );
2773
2774 if ( !event.ControlDown() )
2775 {
2776 SelectRow( m_currentRow, true );
2777 SendSelectionChangedEvent(GetItemByRow(m_currentRow));
2778 }
2779 else
2780 RefreshRow( m_currentRow );
2781 }
2782
2783 //EnsureVisible( m_currentRow );
2784 }
2785
2786 wxRect wxDataViewMainWindow::GetLineRect( unsigned int row ) const
2787 {
2788 wxRect rect;
2789 rect.x = 0;
2790 rect.y = m_lineHeight * row;
2791 rect.width = GetEndOfLastCol();
2792 rect.height = m_lineHeight;
2793
2794 return rect;
2795 }
2796
2797 class RowToItemJob: public DoJob
2798 {
2799 public:
2800 RowToItemJob( unsigned int row , int current ) { this->row = row; this->current = current ;}
2801 virtual ~RowToItemJob(){};
2802
2803 virtual int operator() ( wxDataViewTreeNode * node )
2804 {
2805 current ++;
2806 if( current == static_cast<int>(row))
2807 {
2808 ret = node->GetItem() ;
2809 return DoJob::OK;
2810 }
2811
2812 if( node->GetSubTreeCount() + current < static_cast<int>(row) )
2813 {
2814 current += node->GetSubTreeCount();
2815 return DoJob::IGR;
2816 }
2817 else
2818 {
2819 //If the current has no child node, we can find the desired item of the row number directly.
2820 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2821 if( node->GetNodes().GetCount() == 0)
2822 {
2823 int index = static_cast<int>(row) - current - 1;
2824 ret = node->GetChildren().Item( index );
2825 return DoJob::OK;
2826 }
2827 return DoJob::CONT;
2828 }
2829 }
2830
2831 virtual int operator() ( void * n )
2832 {
2833 current ++;
2834 if( current == static_cast<int>(row))
2835 {
2836 ret = wxDataViewItem( n ) ;
2837 return DoJob::OK;
2838 }
2839 return DoJob::CONT;
2840 }
2841 wxDataViewItem GetResult(){ return ret; }
2842 private:
2843 unsigned int row;
2844 int current ;
2845 wxDataViewItem ret;
2846 };
2847
2848 wxDataViewItem wxDataViewMainWindow::GetItemByRow(unsigned int row) const
2849 {
2850 RowToItemJob job( row, -2 );
2851 Walker( m_root , job );
2852 return job.GetResult();
2853 }
2854
2855 class RowToTreeNodeJob: public DoJob
2856 {
2857 public:
2858 RowToTreeNodeJob( unsigned int row , int current, wxDataViewTreeNode * node )
2859 {
2860 this->row = row;
2861 this->current = current ;
2862 ret = NULL ;
2863 parent = node;
2864 }
2865 virtual ~RowToTreeNodeJob(){};
2866
2867 virtual int operator() ( wxDataViewTreeNode * node )
2868 {
2869 current ++;
2870 if( current == static_cast<int>(row))
2871 {
2872 ret = node ;
2873 return DoJob::OK;
2874 }
2875
2876 if( node->GetSubTreeCount() + current < static_cast<int>(row) )
2877 {
2878 current += node->GetSubTreeCount();
2879 return DoJob::IGR;
2880 }
2881 else
2882 {
2883 parent = node;
2884 //If the current has no child node, we can find the desired item of the row number directly.
2885 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2886 if( node->GetNodes().GetCount() == 0)
2887 {
2888 int index = static_cast<int>(row) - current - 1;
2889 void * n = node->GetChildren().Item( index );
2890 ret = new wxDataViewTreeNode( parent ) ;
2891 ret->SetItem( wxDataViewItem( n ));
2892 ret->SetHasChildren(false);
2893 return DoJob::OK;
2894 }
2895 return DoJob::CONT;
2896 }
2897
2898
2899 }
2900
2901 virtual int operator() ( void * n )
2902 {
2903 current ++;
2904 if( current == static_cast<int>(row))
2905 {
2906 ret = new wxDataViewTreeNode( parent ) ;
2907 ret->SetItem( wxDataViewItem( n ));
2908 ret->SetHasChildren(false);
2909 return DoJob::OK;
2910 }
2911
2912 return DoJob::CONT;
2913 }
2914 wxDataViewTreeNode * GetResult(){ return ret; }
2915 private:
2916 unsigned int row;
2917 int current ;
2918 wxDataViewTreeNode * ret;
2919 wxDataViewTreeNode * parent ;
2920 };
2921
2922
2923 wxDataViewTreeNode * wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row)
2924 {
2925 RowToTreeNodeJob job( row , -2, m_root );
2926 Walker( m_root , job );
2927 return job.GetResult();
2928 }
2929
2930 wxDataViewEvent wxDataViewMainWindow::SendExpanderEvent( wxEventType type, const wxDataViewItem & item )
2931 {
2932 wxWindow *parent = GetParent();
2933 wxDataViewEvent le(type, parent->GetId());
2934
2935 le.SetEventObject(parent);
2936 le.SetModel(GetOwner()->GetModel());
2937 le.SetItem( item );
2938
2939 parent->GetEventHandler()->ProcessEvent(le);
2940 return le;
2941 }
2942
2943 void wxDataViewMainWindow::OnExpanding( unsigned int row )
2944 {
2945 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
2946 if( node != NULL )
2947 {
2948 if( node->HasChildren())
2949 {
2950 if( !node->IsOpen())
2951 {
2952 wxDataViewEvent e = SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING,node->GetItem());
2953 //Check if the user prevent expanding
2954 if( e.GetSkipped() )
2955 return;
2956
2957 node->ToggleOpen();
2958 //Here I build the children of current node
2959 if( node->GetChildrenNumber() == 0 )
2960 {
2961 SortPrepare();
2962 BuildTreeHelper(GetOwner()->GetModel(), node->GetItem(), node);
2963 }
2964 m_count = -1;
2965 UpdateDisplay();
2966 //Send the expanded event
2967 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED,node->GetItem());
2968 }
2969 else
2970 {
2971 SelectRow( row, false );
2972 SelectRow( row + 1, true );
2973 ChangeCurrentRow( row + 1 );
2974 SendSelectionChangedEvent( GetItemByRow(row+1));
2975 }
2976 }
2977 else
2978 delete node;
2979 }
2980 }
2981
2982 void wxDataViewMainWindow::OnCollapsing(unsigned int row)
2983 {
2984 wxDataViewTreeNode * node = GetTreeNodeByRow(row);
2985 if( node != NULL )
2986 {
2987 wxDataViewTreeNode * nd = node;
2988
2989 if( node->HasChildren() && node->IsOpen() )
2990 {
2991 wxDataViewEvent e = SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING,node->GetItem());
2992 if( e.GetSkipped() )
2993 return;
2994 node->ToggleOpen();
2995 m_count = -1;
2996 UpdateDisplay();
2997 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED,nd->GetItem());
2998 }
2999 else
3000 {
3001 node = node->GetParent();
3002 if( node != NULL )
3003 {
3004 int parent = GetRowByItem( node->GetItem() ) ;
3005 if( parent >= 0 )
3006 {
3007 SelectRow( row, false);
3008 SelectRow(parent , true );
3009 ChangeCurrentRow( parent );
3010 SendSelectionChangedEvent( node->GetItem() );
3011 }
3012 }
3013 }
3014 if( !nd->HasChildren())
3015 delete nd;
3016 }
3017 }
3018
3019 wxDataViewTreeNode * wxDataViewMainWindow::FindNode( const wxDataViewItem & item )
3020 {
3021 wxDataViewModel * model = GetOwner()->GetModel();
3022 if( model == NULL )
3023 return NULL;
3024
3025 //Compose the a parent-chain of the finding item
3026 ItemList list;
3027 list.DeleteContents( true );
3028 wxDataViewItem it( item );
3029 while( it.IsOk() )
3030 {
3031 wxDataViewItem * pItem = new wxDataViewItem( it );
3032 list.Insert( pItem );
3033 it = model->GetParent( it );
3034 }
3035
3036 //Find the item along the parent-chain.
3037 //This algorithm is designed to speed up the node-finding method
3038 wxDataViewTreeNode * node = m_root;
3039 for( ItemList::const_iterator iter = list.begin(); iter !=list.end() ; iter++ )
3040 {
3041 if( node->HasChildren() )
3042 {
3043 if( node->GetChildrenNumber() == 0 )
3044 {
3045 SortPrepare();
3046 BuildTreeHelper(model, node->GetItem(), node);
3047 }
3048
3049 wxDataViewTreeNodes nodes = node->GetNodes();
3050 int i = 0;
3051 for (; i < nodes.GetCount(); i ++)
3052 {
3053 if (nodes[i]->GetItem() == (**iter))
3054 {
3055 node = nodes[i];
3056 break;
3057 }
3058 }
3059 if (i == nodes.GetCount())
3060 return NULL;
3061 }
3062 else
3063 return NULL;
3064 }
3065 return node;
3066 }
3067
3068 void wxDataViewMainWindow::HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column )
3069 {
3070 wxDataViewColumn *col = NULL;
3071 unsigned int cols = GetOwner()->GetColumnCount();
3072 unsigned int colnum = 0;
3073 unsigned int x_start = 0;
3074 int x, y;
3075 m_owner->CalcUnscrolledPosition( point.x, point.y, &x, &y );
3076 for (x_start = 0; colnum < cols; colnum++)
3077 {
3078 col = GetOwner()->GetColumn(colnum);
3079 if (col->IsHidden())
3080 continue; // skip it!
3081
3082 unsigned int w = col->GetWidth();
3083 if (x_start+w >= (unsigned int)x)
3084 break;
3085
3086 x_start += w;
3087 }
3088
3089 column = col;
3090 item = GetItemByRow( y/m_lineHeight );
3091 }
3092
3093 wxRect wxDataViewMainWindow::GetItemRect( const wxDataViewItem & item, const wxDataViewColumn* column )
3094 {
3095 int row = GetRowByItem(item);
3096 int y = row*m_lineHeight;
3097 int h = m_lineHeight;
3098 int x = 0;
3099 wxDataViewColumn *col = NULL;
3100 for( int i = 0, cols = GetOwner()->GetColumnCount(); i < cols; i ++ )
3101 {
3102 col = GetOwner()->GetColumn( i );
3103 x += col->GetWidth();
3104 if( GetOwner()->GetColumn(i+1) == column )
3105 break;
3106 }
3107 int w = col->GetWidth();
3108 m_owner->CalcScrolledPosition( x, y, &x, &y );
3109 return wxRect(x, y, w, h);
3110 }
3111
3112 int wxDataViewMainWindow::RecalculateCount()
3113 {
3114 return m_root->GetSubTreeCount();
3115 }
3116
3117 class ItemToRowJob : public DoJob
3118 {
3119 public:
3120 ItemToRowJob(const wxDataViewItem & item, ItemList::const_iterator iter )
3121 { this->item = item ; ret = -1 ; m_iter = iter ; }
3122 virtual ~ItemToRowJob(){};
3123
3124 //Maybe binary search will help to speed up this process
3125 virtual int operator() ( wxDataViewTreeNode * node)
3126 {
3127 ret ++;
3128 if( node->GetItem() == item )
3129 {
3130 return DoJob::OK;
3131 }
3132
3133 if( node->GetItem() == **m_iter )
3134 {
3135 m_iter++ ;
3136 return DoJob::CONT;
3137 }
3138 else
3139 {
3140 ret += node->GetSubTreeCount();
3141 return DoJob::IGR;
3142 }
3143
3144 }
3145
3146 virtual int operator() ( void * n )
3147 {
3148 ret ++;
3149 if( n == item.GetID() )
3150 return DoJob::OK;
3151 return DoJob::CONT;
3152 }
3153 //the row number is begin from zero
3154 int GetResult(){ return ret -1 ; }
3155 private:
3156 ItemList::const_iterator m_iter;
3157 wxDataViewItem item;
3158 int ret;
3159
3160 };
3161
3162 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem & item)
3163 {
3164 wxDataViewModel * model = GetOwner()->GetModel();
3165 if( model == NULL )
3166 return -1;
3167
3168 if( !item.IsOk() )
3169 return -1;
3170
3171 //Compose the a parent-chain of the finding item
3172 ItemList list;
3173 wxDataViewItem * pItem = NULL;
3174 list.DeleteContents( true );
3175 wxDataViewItem it( item );
3176 while( it.IsOk() )
3177 {
3178 pItem = new wxDataViewItem( it );
3179 list.Insert( pItem );
3180 it = model->GetParent( it );
3181 }
3182 pItem = new wxDataViewItem( );
3183 list.Insert( pItem );
3184
3185 ItemToRowJob job( item, list.begin() );
3186 Walker(m_root , job );
3187 return job.GetResult();
3188 }
3189
3190 void BuildTreeHelper( wxDataViewModel * model, wxDataViewItem & item, wxDataViewTreeNode * node)
3191 {
3192 if( !model->IsContainer( item ) )
3193 return ;
3194
3195 wxDataViewItemArray children;
3196 unsigned int num = model->GetChildren( item, children);
3197 int index = 0;
3198 while( index < num )
3199 {
3200 if( model->IsContainer( children[index] ) )
3201 {
3202 wxDataViewTreeNode * n = new wxDataViewTreeNode( node );
3203 n->SetItem(children[index]);
3204 n->SetHasChildren( true ) ;
3205 node->AddNode( n );
3206 }
3207 else
3208 {
3209 node->AddLeaf( children[index].GetID() );
3210 }
3211 index ++;
3212 }
3213 node->SetSubTreeCount( num );
3214 wxDataViewTreeNode * n = node->GetParent();
3215 if( n != NULL)
3216 n->ChangeSubTreeCount(num);
3217
3218 }
3219
3220 void wxDataViewMainWindow::BuildTree(wxDataViewModel * model)
3221 {
3222 //First we define a invalid item to fetch the top-level elements
3223 wxDataViewItem item;
3224 SortPrepare();
3225 BuildTreeHelper( model, item, m_root);
3226 m_count = -1 ;
3227 }
3228
3229 void DestroyTreeHelper( wxDataViewTreeNode * node )
3230 {
3231 if( node->GetNodeNumber() != 0 )
3232 {
3233 int len = node->GetNodeNumber();
3234 int i = 0 ;
3235 wxDataViewTreeNodes nodes = node->GetNodes();
3236 for( ; i < len; i ++ )
3237 {
3238 DestroyTreeHelper(nodes[i]);
3239 }
3240 }
3241 delete node;
3242 }
3243
3244 void wxDataViewMainWindow::DestroyTree()
3245 {
3246 DestroyTreeHelper(m_root);
3247 m_root->SetSubTreeCount(0);
3248 m_count = 0 ;
3249 }
3250
3251 void wxDataViewMainWindow::OnChar( wxKeyEvent &event )
3252 {
3253 if (event.GetKeyCode() == WXK_TAB)
3254 {
3255 wxNavigationKeyEvent nevent;
3256 nevent.SetWindowChange( event.ControlDown() );
3257 nevent.SetDirection( !event.ShiftDown() );
3258 nevent.SetEventObject( GetParent()->GetParent() );
3259 nevent.SetCurrentFocus( m_parent );
3260 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent ))
3261 return;
3262 }
3263
3264 // no item -> nothing to do
3265 if (!HasCurrentRow())
3266 {
3267 event.Skip();
3268 return;
3269 }
3270
3271 // don't use m_linesPerPage directly as it might not be computed yet
3272 const int pageSize = GetCountPerPage();
3273 wxCHECK_RET( pageSize, _T("should have non zero page size") );
3274
3275 switch ( event.GetKeyCode() )
3276 {
3277 case WXK_UP:
3278 if ( m_currentRow > 0 )
3279 OnArrowChar( m_currentRow - 1, event );
3280 break;
3281
3282 case WXK_DOWN:
3283 if ( m_currentRow < GetRowCount() - 1 )
3284 OnArrowChar( m_currentRow + 1, event );
3285 break;
3286 //Add the process for tree expanding/collapsing
3287 case WXK_LEFT:
3288 OnCollapsing(m_currentRow);
3289 break;
3290 case WXK_RIGHT:
3291 OnExpanding( m_currentRow);
3292 break;
3293 case WXK_END:
3294 if (!IsEmpty())
3295 OnArrowChar( GetRowCount() - 1, event );
3296 break;
3297
3298 case WXK_HOME:
3299 if (!IsEmpty())
3300 OnArrowChar( 0, event );
3301 break;
3302
3303 case WXK_PAGEUP:
3304 {
3305 int steps = pageSize - 1;
3306 int index = m_currentRow - steps;
3307 if (index < 0)
3308 index = 0;
3309
3310 OnArrowChar( index, event );
3311 }
3312 break;
3313
3314 case WXK_PAGEDOWN:
3315 {
3316 int steps = pageSize - 1;
3317 unsigned int index = m_currentRow + steps;
3318 unsigned int count = GetRowCount();
3319 if ( index >= count )
3320 index = count - 1;
3321
3322 OnArrowChar( index, event );
3323 }
3324 break;
3325
3326 default:
3327 event.Skip();
3328 }
3329 }
3330
3331 void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
3332 {
3333 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
3334 {
3335 // let the base handle mouse wheel events.
3336 event.Skip();
3337 return;
3338 }
3339
3340 int x = event.GetX();
3341 int y = event.GetY();
3342 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
3343 wxDataViewColumn *col = NULL;
3344
3345 int xpos = 0;
3346 unsigned int cols = GetOwner()->GetColumnCount();
3347 unsigned int i;
3348 for (i = 0; i < cols; i++)
3349 {
3350 wxDataViewColumn *c = GetOwner()->GetColumn( i );
3351 if (c->IsHidden())
3352 continue; // skip it!
3353
3354 if (x < xpos + c->GetWidth())
3355 {
3356 col = c;
3357 break;
3358 }
3359 xpos += c->GetWidth();
3360 }
3361 if (!col)
3362 return;
3363
3364 wxDataViewRenderer *cell = col->GetRenderer();
3365 unsigned int current = y / m_lineHeight;
3366 if ((current > GetRowCount()) || (x > GetEndOfLastCol()))
3367 {
3368 // Unselect all if below the last row ?
3369 return;
3370 }
3371
3372 //Test whether the mouse is hovered on the tree item button
3373 bool hover = false;
3374 if (GetOwner()->GetExpanderColumn() == col)
3375 {
3376 wxDataViewTreeNode * node = GetTreeNodeByRow(current);
3377 if( node!=NULL && node->HasChildren() )
3378 {
3379 int indent = node->GetIndentLevel();
3380 indent = GetOwner()->GetIndent()*indent;
3381 wxRect rect( xpos + indent + EXPANDER_MARGIN, current * m_lineHeight + EXPANDER_MARGIN, m_lineHeight-2*EXPANDER_MARGIN,m_lineHeight-2*EXPANDER_MARGIN);
3382 if( rect.Contains( x, y) )
3383 {
3384 //So the mouse is over the expander
3385 hover = true;
3386 if (m_underMouse && m_underMouse != node)
3387 {
3388 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3389 Refresh(GetRowByItem(m_underMouse->GetItem()));
3390 }
3391 if (m_underMouse != node)
3392 {
3393 //wxLogMessage("Do the row: %d", current);
3394 Refresh(current);
3395 }
3396 m_underMouse = node;
3397 }
3398 }
3399 if (node!=NULL && !node->HasChildren())
3400 delete node;
3401 }
3402 if (!hover)
3403 {
3404 if (m_underMouse != NULL)
3405 {
3406 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3407 Refresh(GetRowByItem(m_underMouse->GetItem()));
3408 m_underMouse = NULL;
3409 }
3410 }
3411
3412 wxDataViewModel *model = GetOwner()->GetModel();
3413
3414 if (event.Dragging())
3415 {
3416 if (m_dragCount == 0)
3417 {
3418 // we have to report the raw, physical coords as we want to be
3419 // able to call HitTest(event.m_pointDrag) from the user code to
3420 // get the item being dragged
3421 m_dragStart = event.GetPosition();
3422 }
3423
3424 m_dragCount++;
3425
3426 if (m_dragCount != 3)
3427 return;
3428
3429 if (event.LeftIsDown())
3430 {
3431 // Notify cell about drag
3432 }
3433 return;
3434 }
3435 else
3436 {
3437 m_dragCount = 0;
3438 }
3439
3440 bool forceClick = false;
3441
3442 if (event.ButtonDClick())
3443 {
3444 m_renameTimer->Stop();
3445 m_lastOnSame = false;
3446 }
3447
3448 if (event.LeftDClick())
3449 {
3450 if ( current == m_lineLastClicked )
3451 {
3452 if (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE)
3453 {
3454 wxDataViewItem item = GetItemByRow(current);
3455 wxVariant value;
3456 model->GetValue( value, item, col->GetModelColumn() );
3457 cell->SetValue( value );
3458 wxRect cell_rect( xpos, current * m_lineHeight,
3459 col->GetWidth(), m_lineHeight );
3460 cell->Activate( cell_rect, model, item, col->GetModelColumn() );
3461
3462 wxWindow *parent = GetParent();
3463 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, parent->GetId());
3464
3465 le.SetEventObject(parent);
3466 le.SetColumn(col->GetModelColumn());
3467 le.SetDataViewColumn(col);
3468 le.SetModel(GetOwner()->GetModel());
3469
3470 parent->GetEventHandler()->ProcessEvent(le);
3471 }
3472 return;
3473 }
3474 else
3475 {
3476 // The first click was on another item, so don't interpret this as
3477 // a double click, but as a simple click instead
3478 forceClick = true;
3479 }
3480 }
3481
3482 if (event.LeftUp())
3483 {
3484 if (m_lineSelectSingleOnUp != (unsigned int)-1)
3485 {
3486 // select single line
3487 SelectAllRows( false );
3488 SelectRow( m_lineSelectSingleOnUp, true );
3489 }
3490
3491 //Process the event of user clicking the expander
3492 bool expander = false;
3493 if (GetOwner()->GetExpanderColumn() == col)
3494 {
3495 wxDataViewTreeNode * node = GetTreeNodeByRow(current);
3496 if( node!=NULL && node->HasChildren() )
3497 {
3498 int indent = node->GetIndentLevel();
3499 indent = GetOwner()->GetIndent()*indent;
3500 wxRect rect( xpos + indent + EXPANDER_MARGIN, current * m_lineHeight + EXPANDER_MARGIN, m_lineHeight-2*EXPANDER_MARGIN,m_lineHeight-2*EXPANDER_MARGIN);
3501 if( rect.Contains( x, y) )
3502 {
3503 expander = true;
3504 if( node->IsOpen() )
3505 OnCollapsing(current);
3506 else
3507 OnExpanding( current );
3508 }
3509 }
3510 }
3511 //If the user click the expander, we do not do editing even if the column with expander are editable
3512 if (m_lastOnSame && !expander )
3513 {
3514 if ((col == m_currentCol) && (current == m_currentRow) &&
3515 (cell->GetMode() == wxDATAVIEW_CELL_EDITABLE) )
3516 {
3517 m_renameTimer->Start( 100, true );
3518 }
3519 }
3520
3521 m_lastOnSame = false;
3522 m_lineSelectSingleOnUp = (unsigned int)-1;
3523 }
3524 else
3525 {
3526 // This is necessary, because after a DnD operation in
3527 // from and to ourself, the up event is swallowed by the
3528 // DnD code. So on next non-up event (which means here and
3529 // now) m_lineSelectSingleOnUp should be reset.
3530 m_lineSelectSingleOnUp = (unsigned int)-1;
3531 }
3532
3533 if (event.RightDown())
3534 {
3535 m_lineBeforeLastClicked = m_lineLastClicked;
3536 m_lineLastClicked = current;
3537
3538 // If the item is already selected, do not update the selection.
3539 // Multi-selections should not be cleared if a selected item is clicked.
3540 if (!IsRowSelected(current))
3541 {
3542 SelectAllRows(false);
3543 ChangeCurrentRow(current);
3544 SelectRow(m_currentRow,true);
3545 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
3546 }
3547
3548 // notify cell about right click
3549 // cell->...
3550
3551 // Allow generation of context menu event
3552 event.Skip();
3553 }
3554 else if (event.MiddleDown())
3555 {
3556 // notify cell about middle click
3557 // cell->...
3558 }
3559 if (event.LeftDown() || forceClick)
3560 {
3561 SetFocus();
3562
3563 m_lineBeforeLastClicked = m_lineLastClicked;
3564 m_lineLastClicked = current;
3565
3566 unsigned int oldCurrentRow = m_currentRow;
3567 bool oldWasSelected = IsRowSelected(m_currentRow);
3568
3569 bool cmdModifierDown = event.CmdDown();
3570 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
3571 {
3572 if ( IsSingleSel() || !IsRowSelected(current) )
3573 {
3574 SelectAllRows( false );
3575 ChangeCurrentRow(current);
3576 SelectRow(m_currentRow,true);
3577 SendSelectionChangedEvent(GetItemByRow( m_currentRow ) );
3578 }
3579 else // multi sel & current is highlighted & no mod keys
3580 {
3581 m_lineSelectSingleOnUp = current;
3582 ChangeCurrentRow(current); // change focus
3583 }
3584 }
3585 else // multi sel & either ctrl or shift is down
3586 {
3587 if (cmdModifierDown)
3588 {
3589 ChangeCurrentRow(current);
3590 ReverseRowSelection(m_currentRow);
3591 SendSelectionChangedEvent(GetItemByRow(m_selection[0]) );
3592 }
3593 else if (event.ShiftDown())
3594 {
3595 ChangeCurrentRow(current);
3596
3597 unsigned int lineFrom = oldCurrentRow,
3598 lineTo = current;
3599
3600 if ( lineTo < lineFrom )
3601 {
3602 lineTo = lineFrom;
3603 lineFrom = m_currentRow;
3604 }
3605
3606 SelectRows(lineFrom, lineTo, true);
3607 SendSelectionChangedEvent(GetItemByRow(m_selection[0]) );
3608 }
3609 else // !ctrl, !shift
3610 {
3611 // test in the enclosing if should make it impossible
3612 wxFAIL_MSG( _T("how did we get here?") );
3613 }
3614 }
3615
3616 if (m_currentRow != oldCurrentRow)
3617 RefreshRow( oldCurrentRow );
3618
3619 wxDataViewColumn *oldCurrentCol = m_currentCol;
3620
3621 // Update selection here...
3622 m_currentCol = col;
3623
3624 m_lastOnSame = !forceClick && ((col == oldCurrentCol) &&
3625 (current == oldCurrentRow)) && oldWasSelected;
3626 }
3627 }
3628
3629 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event )
3630 {
3631 m_hasFocus = true;
3632
3633 if (HasCurrentRow())
3634 Refresh();
3635
3636 event.Skip();
3637 }
3638
3639 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event )
3640 {
3641 m_hasFocus = false;
3642
3643 if (HasCurrentRow())
3644 Refresh();
3645
3646 event.Skip();
3647 }
3648
3649 wxDataViewItem wxDataViewMainWindow::GetSelection() const
3650 {
3651 if( m_selection.GetCount() != 1 )
3652 return wxDataViewItem();
3653
3654 return GetItemByRow( m_selection.Item(0));
3655 }
3656
3657 //-----------------------------------------------------------------------------
3658 // wxDataViewCtrl
3659 //-----------------------------------------------------------------------------
3660 WX_DEFINE_LIST(wxDataViewColumnList);
3661
3662 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
3663
3664 BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase)
3665 EVT_SIZE(wxDataViewCtrl::OnSize)
3666 END_EVENT_TABLE()
3667
3668 wxDataViewCtrl::~wxDataViewCtrl()
3669 {
3670 if (m_notifier)
3671 GetModel()->RemoveNotifier( m_notifier );
3672 }
3673
3674 void wxDataViewCtrl::Init()
3675 {
3676 m_notifier = NULL;
3677 }
3678
3679 bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
3680 const wxPoint& pos, const wxSize& size,
3681 long style, const wxValidator& validator )
3682 {
3683 if (!wxControl::Create( parent, id, pos, size,
3684 style | wxScrolledWindowStyle|wxSUNKEN_BORDER, validator))
3685 return false;
3686
3687 Init();
3688
3689 #ifdef __WXMAC__
3690 MacSetClipChildren( true ) ;
3691 #endif
3692
3693 m_clientArea = new wxDataViewMainWindow( this, wxID_ANY );
3694
3695 if (HasFlag(wxDV_NO_HEADER))
3696 m_headerArea = NULL;
3697 else
3698 m_headerArea = new wxDataViewHeaderWindow( this, wxID_ANY );
3699
3700 SetTargetWindow( m_clientArea );
3701
3702 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
3703 if (m_headerArea)
3704 sizer->Add( m_headerArea, 0, wxGROW );
3705 sizer->Add( m_clientArea, 1, wxGROW );
3706 SetSizer( sizer );
3707
3708 return true;
3709 }
3710
3711 #ifdef __WXMSW__
3712 WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg,
3713 WXWPARAM wParam,
3714 WXLPARAM lParam)
3715 {
3716 WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
3717
3718 #ifndef __WXWINCE__
3719 // we need to process arrows ourselves for scrolling
3720 if ( nMsg == WM_GETDLGCODE )
3721 {
3722 rc |= DLGC_WANTARROWS;
3723 }
3724 #endif
3725
3726 return rc;
3727 }
3728 #endif
3729
3730 void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
3731 {
3732 // We need to override OnSize so that our scrolled
3733 // window a) does call Layout() to use sizers for
3734 // positioning the controls but b) does not query
3735 // the sizer for their size and use that for setting
3736 // the scrollable area as set that ourselves by
3737 // calling SetScrollbar() further down.
3738
3739 Layout();
3740
3741 AdjustScrollbars();
3742 }
3743
3744 bool wxDataViewCtrl::AssociateModel( wxDataViewModel *model )
3745 {
3746 if (!wxDataViewCtrlBase::AssociateModel( model ))
3747 return false;
3748
3749 m_notifier = new wxGenericDataViewModelNotifier( m_clientArea );
3750
3751 model->AddNotifier( m_notifier );
3752
3753 m_clientArea->BuildTree(model);
3754
3755 m_clientArea->UpdateDisplay();
3756
3757 return true;
3758 }
3759
3760 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
3761 {
3762 if (!wxDataViewCtrlBase::AppendColumn(col))
3763 return false;
3764
3765 m_cols.Append( col );
3766 OnColumnChange();
3767 return true;
3768 }
3769
3770 void wxDataViewCtrl::OnColumnChange()
3771 {
3772 if (m_headerArea)
3773 m_headerArea->UpdateDisplay();
3774
3775 m_clientArea->UpdateDisplay();
3776 }
3777
3778 void wxDataViewCtrl::DoSetExpanderColumn()
3779 {
3780 m_clientArea->UpdateDisplay();
3781 }
3782
3783 void wxDataViewCtrl::DoSetIndent()
3784 {
3785 m_clientArea->UpdateDisplay();
3786 }
3787
3788 unsigned int wxDataViewCtrl::GetColumnCount() const
3789 {
3790 return m_cols.GetCount();
3791 }
3792
3793 wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const
3794 {
3795 wxDataViewColumnList::const_iterator iter;
3796 int i = 0;
3797 for (iter = m_cols.begin(); iter!=m_cols.end(); iter++)
3798 {
3799 if (i == pos)
3800 return *iter;
3801
3802 if ((*iter)->IsHidden())
3803 continue;
3804 i ++;
3805 }
3806 return NULL;
3807 }
3808
3809 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
3810 {
3811 wxDataViewColumnList::compatibility_iterator ret = m_cols.Find( column );
3812 if (ret == NULL)
3813 return false;
3814
3815 m_cols.Erase(ret);
3816 delete column;
3817 OnColumnChange();
3818
3819 return true;
3820 }
3821
3822 bool wxDataViewCtrl::ClearColumns()
3823 {
3824 m_cols.clear();
3825 OnColumnChange();
3826 return true;
3827 }
3828
3829 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn *column ) const
3830 {
3831 int ret = 0, dead = 0;
3832 int len = GetColumnCount();
3833 for (int i=0; i<len; i++)
3834 {
3835 wxDataViewColumn * col = GetColumn(i);
3836 if (col->IsHidden())
3837 continue;
3838 ret += col->GetWidth();
3839 if (column==col)
3840 {
3841 CalcScrolledPosition( ret, dead, &ret, &dead );
3842 break;
3843 }
3844 }
3845 return ret;
3846 }
3847
3848 wxDataViewColumn *wxDataViewCtrl::GetSortingColumn() const
3849 {
3850 return NULL;
3851 }
3852
3853 //Selection code with wxDataViewItem as parameters
3854 wxDataViewItem wxDataViewCtrl::GetSelection() const
3855 {
3856 return m_clientArea->GetSelection();
3857 }
3858
3859 int wxDataViewCtrl::GetSelections( wxDataViewItemArray & sel ) const
3860 {
3861 sel.Empty();
3862 wxDataViewSelection selection = m_clientArea->GetSelections();
3863 int len = selection.GetCount();
3864 for( int i = 0; i < len; i ++)
3865 {
3866 unsigned int row = selection[i];
3867 sel.Add( m_clientArea->GetItemByRow( row ) );
3868 }
3869 return len;
3870 }
3871
3872 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray & sel )
3873 {
3874 wxDataViewSelection selection(wxDataViewSelectionCmp) ;
3875 int len = sel.GetCount();
3876 for( int i = 0; i < len; i ++ )
3877 {
3878 int row = m_clientArea->GetRowByItem( sel[i] );
3879 if( row >= 0 )
3880 selection.Add( static_cast<unsigned int>(row) );
3881 }
3882 m_clientArea->SetSelections( selection );
3883 }
3884
3885 void wxDataViewCtrl::Select( const wxDataViewItem & item )
3886 {
3887 int row = m_clientArea->GetRowByItem( item );
3888 if( row >= 0 )
3889 {
3890 //Unselect all rows before select another in the single select mode
3891 if (m_clientArea->IsSingleSel())
3892 m_clientArea->SelectAllRows(false);
3893 m_clientArea->SelectRow(row, true);
3894 }
3895 }
3896
3897 void wxDataViewCtrl::Unselect( const wxDataViewItem & item )
3898 {
3899 int row = m_clientArea->GetRowByItem( item );
3900 if( row >= 0 )
3901 m_clientArea->SelectRow(row, false);
3902 }
3903
3904 bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const
3905 {
3906 int row = m_clientArea->GetRowByItem( item );
3907 if( row >= 0 )
3908 {
3909 return m_clientArea->IsRowSelected(row);
3910 }
3911 return false;
3912 }
3913
3914 //Selection code with row number as parameter
3915 int wxDataViewCtrl::GetSelections( wxArrayInt & sel ) const
3916 {
3917 sel.Empty();
3918 wxDataViewSelection selection = m_clientArea->GetSelections();
3919 int len = selection.GetCount();
3920 for( int i = 0; i < len; i ++)
3921 {
3922 unsigned int row = selection[i];
3923 sel.Add( row );
3924 }
3925 return len;
3926 }
3927
3928 void wxDataViewCtrl::SetSelections( const wxArrayInt & sel )
3929 {
3930 wxDataViewSelection selection(wxDataViewSelectionCmp) ;
3931 int len = sel.GetCount();
3932 for( int i = 0; i < len; i ++ )
3933 {
3934 int row = sel[i];
3935 if( row >= 0 )
3936 selection.Add( static_cast<unsigned int>(row) );
3937 }
3938 m_clientArea->SetSelections( selection );
3939 }
3940
3941 void wxDataViewCtrl::Select( int row )
3942 {
3943 if( row >= 0 )
3944 {
3945 if (m_clientArea->IsSingleSel())
3946 m_clientArea->SelectAllRows(false);
3947 m_clientArea->SelectRow( row, true );
3948 }
3949 }
3950
3951 void wxDataViewCtrl::Unselect( int row )
3952 {
3953 if( row >= 0 )
3954 m_clientArea->SelectRow(row, false);
3955 }
3956
3957 bool wxDataViewCtrl::IsSelected( int row ) const
3958 {
3959 if( row >= 0 )
3960 return m_clientArea->IsRowSelected(row);
3961 return false;
3962 }
3963
3964 void wxDataViewCtrl::SelectRange( int from, int to )
3965 {
3966 wxArrayInt sel;
3967 for( int i = from; i < to; i ++ )
3968 sel.Add( i );
3969 m_clientArea->Select(sel);
3970 }
3971
3972 void wxDataViewCtrl::UnselectRange( int from, int to )
3973 {
3974 wxDataViewSelection sel = m_clientArea->GetSelections();
3975 for( int i = from; i < to; i ++ )
3976 if( sel.Index( i ) != wxNOT_FOUND )
3977 sel.Remove( i );
3978 m_clientArea->SetSelections(sel);
3979 }
3980
3981 void wxDataViewCtrl::SelectAll()
3982 {
3983 m_clientArea->SelectAllRows(true);
3984 }
3985
3986 void wxDataViewCtrl::UnselectAll()
3987 {
3988 m_clientArea->SelectAllRows(false);
3989 }
3990
3991 void wxDataViewCtrl::EnsureVisible( int row, int column )
3992 {
3993 if( row < 0 )
3994 row = 0;
3995 if( row > m_clientArea->GetRowCount() )
3996 row = m_clientArea->GetRowCount();
3997
3998 int first = m_clientArea->GetFirstVisibleRow();
3999 int last = m_clientArea->GetLastVisibleRow();
4000 if( row < first )
4001 m_clientArea->ScrollTo( row, column );
4002 else if( row > last )
4003 m_clientArea->ScrollTo( row - last + first, column );
4004 else
4005 m_clientArea->ScrollTo( first, column );
4006 }
4007
4008 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem & item, const wxDataViewColumn * column )
4009 {
4010 int row = m_clientArea->GetRowByItem(item);
4011 if( row >= 0 )
4012 {
4013 if( column == NULL )
4014 EnsureVisible(row, -1);
4015 else
4016 {
4017 int col = 0;
4018 int len = GetColumnCount();
4019 for( int i = 0; i < len; i ++ )
4020 if( GetColumn(i) == column )
4021 {
4022 col = i;
4023 break;
4024 }
4025 EnsureVisible( row, col );
4026 }
4027 }
4028
4029 }
4030
4031 void wxDataViewCtrl::HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column ) const
4032 {
4033 m_clientArea->HitTest(point, item, column);
4034 }
4035
4036 wxRect wxDataViewCtrl::GetItemRect( const wxDataViewItem & item, const wxDataViewColumn* column ) const
4037 {
4038 return m_clientArea->GetItemRect(item, column);
4039 }
4040
4041 wxDataViewItem wxDataViewCtrl::GetItemByRow( unsigned int row ) const
4042 {
4043 return m_clientArea->GetItemByRow( row );
4044 }
4045
4046 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem & item ) const
4047 {
4048 return m_clientArea->GetRowByItem( item );
4049 }
4050
4051 void wxDataViewCtrl::Expand( const wxDataViewItem & item )
4052 {
4053 int row = m_clientArea->GetRowByItem( item );
4054 if (row != -1)
4055 m_clientArea->Expand(row);
4056 }
4057
4058 void wxDataViewCtrl::Collapse( const wxDataViewItem & item )
4059 {
4060 int row = m_clientArea->GetRowByItem( item );
4061 if (row != -1)
4062 m_clientArea->Collapse(row);
4063 }
4064
4065 #endif
4066 // !wxUSE_GENERICDATAVIEWCTRL
4067
4068 #endif
4069 // wxUSE_DATAVIEWCTRL