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