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