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