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