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