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