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