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