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