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