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