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