wxMSWGetColumnClicked must be dllexport'd to be used from another (adv) DLL
[wxWidgets.git] / src / generic / datavgen.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Otto Wyss
6 // Id: $Id$
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_DATAVIEWCTRL
19
20 #include "wx/dataview.h"
21
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
23
24 #ifndef WX_PRECOMP
25 #ifdef __WXMSW__
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
29 #endif
30 #include "wx/sizer.h"
31 #include "wx/log.h"
32 #include "wx/dcclient.h"
33 #include "wx/timer.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #endif
37
38 #include "wx/stockitem.h"
39 #include "wx/calctrl.h"
40 #include "wx/popupwin.h"
41 #include "wx/renderer.h"
42 #include "wx/dcbuffer.h"
43 #include "wx/icon.h"
44
45 //-----------------------------------------------------------------------------
46 // classes
47 //-----------------------------------------------------------------------------
48
49 class wxDataViewCtrl;
50
51 static const int SCROLL_UNIT_X = 15;
52
53 // the cell padding on the left/right
54 static const int PADDING_RIGHTLEFT = 3;
55
56 // the cell padding on the top/bottom
57 static const int PADDING_TOPBOTTOM = 1;
58
59
60 //-----------------------------------------------------------------------------
61 // wxDataViewHeaderWindow
62 //-----------------------------------------------------------------------------
63
64 #define USE_NATIVE_HEADER_WINDOW 1
65
66 class wxDataViewHeaderWindowBase : public wxControl
67 {
68 public:
69 wxDataViewHeaderWindowBase()
70 { m_owner = NULL; }
71
72 bool Create(wxDataViewCtrl *parent, wxWindowID id,
73 const wxPoint &pos, const wxSize &size,
74 const wxString &name)
75 {
76 return wxWindow::Create(parent, id, pos, size, wxNO_BORDER, name);
77 }
78
79 void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; }
80 wxDataViewCtrl *GetOwner() { return m_owner; }
81
82 // called on column addition/removal
83 virtual void UpdateDisplay() { /* by default, do nothing */ }
84
85 // returns the n-th column
86 virtual wxDataViewColumn *GetColumn(unsigned int n)
87 {
88 wxASSERT(m_owner);
89 wxDataViewColumn *ret = m_owner->GetColumn(n);
90 wxASSERT(ret);
91
92 return ret;
93 }
94
95 protected:
96 wxDataViewCtrl *m_owner;
97
98 // sends an event generated from the n-th wxDataViewColumn
99 void SendEvent(wxEventType type, unsigned int n);
100 };
101
102 // on wxMSW the header window (only that part however) can be made native!
103 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
104
105 #define COLUMN_WIDTH_OFFSET 2
106 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
107
108 class wxDataViewHeaderWindowMSW : public wxDataViewHeaderWindowBase
109 {
110 public:
111
112 wxDataViewHeaderWindowMSW( wxDataViewCtrl *parent,
113 wxWindowID id,
114 const wxPoint &pos = wxDefaultPosition,
115 const wxSize &size = wxDefaultSize,
116 const wxString &name = wxT("wxdataviewctrlheaderwindow") )
117 {
118 Create(parent, id, pos, size, name);
119 }
120
121 bool Create(wxDataViewCtrl *parent, wxWindowID id,
122 const wxPoint &pos, const wxSize &size,
123 const wxString &name);
124
125 ~wxDataViewHeaderWindowMSW();
126
127 // called when any column setting is changed and/or changed
128 // the column count
129 virtual void UpdateDisplay();
130
131 // called when the main window gets scrolled
132 virtual void ScrollWindow(int dx, int dy, const wxRect *rect = NULL);
133
134 protected:
135 virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
136 virtual void DoSetSize(int x, int y, int width, int height, int sizeFlags);
137
138 unsigned int GetColumnIdxFromHeader(NMHEADER *nmHDR);
139
140 wxDataViewColumn *GetColumnFromHeader(NMHEADER *nmHDR)
141 { return GetColumn(GetColumnIdxFromHeader(nmHDR)); }
142
143 private:
144 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW)
145 };
146
147 #else // !defined(__WXMSW__)
148
149 #define HEADER_WINDOW_HEIGHT 25
150 #define HEADER_HORIZ_BORDER 5
151 #define HEADER_VERT_BORDER 3
152 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
153
154 class wxGenericDataViewHeaderWindow : public wxDataViewHeaderWindowBase
155 {
156 public:
157 wxGenericDataViewHeaderWindow( wxDataViewCtrl *parent,
158 wxWindowID id,
159 const wxPoint &pos = wxDefaultPosition,
160 const wxSize &size = wxDefaultSize,
161 const wxString &name = wxT("wxdataviewctrlheaderwindow") )
162 {
163 Init();
164 Create(parent, id, pos, size, name);
165 }
166
167 bool Create(wxDataViewCtrl *parent, wxWindowID id,
168 const wxPoint &pos, const wxSize &size,
169 const wxString &name);
170
171 ~wxGenericDataViewHeaderWindow()
172 {
173 delete m_resizeCursor;
174 }
175
176 // event handlers:
177
178 void OnPaint( wxPaintEvent &event );
179 void OnMouse( wxMouseEvent &event );
180 void OnSetFocus( wxFocusEvent &event );
181
182
183 protected:
184
185 // vars used for column resizing:
186
187 wxCursor *m_resizeCursor;
188 const wxCursor *m_currentCursor;
189 bool m_isDragging;
190
191 bool m_dirty; // needs refresh?
192 int m_column; // index of the column being resized
193 int m_currentX; // divider line position in logical (unscrolled) coords
194 int m_minX; // minimal position beyond which the divider line
195 // can't be dragged in logical coords
196
197 // the pen used to draw the current column width drag line
198 // when resizing the columsn
199 wxPen m_penCurrent;
200
201
202 // internal utilities:
203
204 void Init()
205 {
206 m_currentCursor = (wxCursor *) NULL;
207 m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE );
208
209 m_isDragging = false;
210 m_dirty = false;
211
212 m_column = wxNOT_FOUND;
213 m_currentX = 0;
214 m_minX = 0;
215
216 wxColour col = wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT);
217 m_penCurrent = wxPen(col, 1, wxSOLID);
218 }
219
220 void DrawCurrent();
221 void AdjustDC(wxDC& dc);
222
223 private:
224 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow)
225 DECLARE_EVENT_TABLE()
226 };
227
228 #endif // defined(__WXMSW__)
229
230 //-----------------------------------------------------------------------------
231 // wxDataViewRenameTimer
232 //-----------------------------------------------------------------------------
233
234 class wxDataViewRenameTimer: public wxTimer
235 {
236 private:
237 wxDataViewMainWindow *m_owner;
238
239 public:
240 wxDataViewRenameTimer( wxDataViewMainWindow *owner );
241 void Notify();
242 };
243
244 //-----------------------------------------------------------------------------
245 // wxDataViewTextCtrlWrapper: wraps a wxTextCtrl for inline editing
246 //-----------------------------------------------------------------------------
247
248 class wxDataViewTextCtrlWrapper : public wxEvtHandler
249 {
250 public:
251 // NB: text must be a valid object but not Create()d yet
252 wxDataViewTextCtrlWrapper( wxDataViewMainWindow *owner,
253 wxTextCtrl *text,
254 wxDataViewListModel *model,
255 unsigned int col, unsigned int row,
256 wxRect cellLabel );
257
258 wxTextCtrl *GetText() const { return m_text; }
259
260 void AcceptChangesAndFinish();
261
262 protected:
263 void OnChar( wxKeyEvent &event );
264 void OnKeyUp( wxKeyEvent &event );
265 void OnKillFocus( wxFocusEvent &event );
266
267 bool AcceptChanges();
268 void Finish();
269
270 private:
271 wxDataViewMainWindow *m_owner;
272 wxTextCtrl *m_text;
273 wxString m_startValue;
274 wxDataViewListModel *m_model;
275 unsigned int m_col;
276 unsigned int m_row;
277 bool m_finished;
278 bool m_aboutToFinish;
279
280 DECLARE_EVENT_TABLE()
281 };
282
283 //-----------------------------------------------------------------------------
284 // wxDataViewMainWindow
285 //-----------------------------------------------------------------------------
286
287 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection,
288 WXDLLIMPEXP_ADV);
289
290 class wxDataViewMainWindow: public wxWindow
291 {
292 public:
293 wxDataViewMainWindow( wxDataViewCtrl *parent,
294 wxWindowID id,
295 const wxPoint &pos = wxDefaultPosition,
296 const wxSize &size = wxDefaultSize,
297 const wxString &name = wxT("wxdataviewctrlmainwindow") );
298 virtual ~wxDataViewMainWindow();
299
300 // notifications from wxDataViewListModel
301 bool RowAppended();
302 bool RowPrepended();
303 bool RowInserted( unsigned int before );
304 bool RowDeleted( unsigned int row );
305 bool RowChanged( unsigned int row );
306 bool ValueChanged( unsigned int col, unsigned int row );
307 bool RowsReordered( unsigned int *new_order );
308 bool Cleared();
309
310 void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; }
311 wxDataViewCtrl *GetOwner() { return m_owner; }
312 const wxDataViewCtrl *GetOwner() const { return m_owner; }
313
314 void OnPaint( wxPaintEvent &event );
315 void OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event);
316 void OnChar( wxKeyEvent &event );
317 void OnMouse( wxMouseEvent &event );
318 void OnSetFocus( wxFocusEvent &event );
319 void OnKillFocus( wxFocusEvent &event );
320
321 void UpdateDisplay();
322 void RecalculateDisplay();
323 void OnInternalIdle();
324
325 void OnRenameTimer();
326 void FinishEditing( wxTextCtrl *text );
327
328 void ScrollWindow( int dx, int dy, const wxRect *rect = NULL );
329
330 bool HasCurrentRow() { return m_currentRow != (unsigned int)-1; }
331 void ChangeCurrentRow( unsigned int row );
332
333 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE); };
334 bool IsEmpty() { return GetRowCount() == 0; }
335
336 int GetCountPerPage() const;
337 int GetEndOfLastCol() const;
338 unsigned int GetFirstVisibleRow() const;
339 unsigned int GetLastVisibleRow() const;
340 unsigned int GetRowCount() const;
341
342 void Select( const wxArrayInt& aSelections );
343 void SelectAllRows( bool on );
344 void SelectRow( unsigned int row, bool on );
345 void SelectRows( unsigned int from, unsigned int to, bool on );
346 void ReverseRowSelection( unsigned int row );
347 bool IsRowSelected( unsigned int row );
348
349 void RefreshRow( unsigned int row );
350 void RefreshRows( unsigned int from, unsigned int to );
351 void RefreshRowsAfter( unsigned int firstRow );
352
353 // returns the colour to be used for drawing the rules
354 wxColour GetRuleColour() const
355 {
356 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT);
357 }
358
359 //void EnsureVisible( unsigned int row );
360 wxRect GetLineRect( unsigned int row ) const;
361
362 private:
363 wxDataViewCtrl *m_owner;
364 int m_lineHeight;
365 bool m_dirty;
366
367 wxDataViewColumn *m_currentCol;
368 unsigned int m_currentRow;
369 wxDataViewSelection m_selection;
370
371 wxDataViewRenameTimer *m_renameTimer;
372 wxDataViewTextCtrlWrapper *m_textctrlWrapper;
373 bool m_lastOnSame;
374
375 bool m_hasFocus;
376
377 int m_dragCount;
378 wxPoint m_dragStart;
379
380 // for double click logic
381 unsigned int m_lineLastClicked,
382 m_lineBeforeLastClicked,
383 m_lineSelectSingleOnUp;
384
385 // the pen used to draw horiz/vertical rules
386 wxPen m_penRule;
387
388 private:
389 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow)
390 DECLARE_EVENT_TABLE()
391 };
392
393 // ---------------------------------------------------------
394 // wxGenericDataViewListModelNotifier
395 // ---------------------------------------------------------
396
397 class wxGenericDataViewListModelNotifier: public wxDataViewListModelNotifier
398 {
399 public:
400 wxGenericDataViewListModelNotifier( wxDataViewMainWindow *mainWindow )
401 { m_mainWindow = mainWindow; }
402
403 virtual bool RowAppended()
404 { return m_mainWindow->RowAppended(); }
405 virtual bool RowPrepended()
406 { return m_mainWindow->RowPrepended(); }
407 virtual bool RowInserted( unsigned int before )
408 { return m_mainWindow->RowInserted( before ); }
409 virtual bool RowDeleted( unsigned int row )
410 { return m_mainWindow->RowDeleted( row ); }
411 virtual bool RowChanged( unsigned int row )
412 { return m_mainWindow->RowChanged( row ); }
413 virtual bool ValueChanged( unsigned int col, unsigned int row )
414 { return m_mainWindow->ValueChanged( col, row ); }
415 virtual bool RowsReordered( unsigned int *new_order )
416 { return m_mainWindow->RowsReordered( new_order ); }
417 virtual bool Cleared()
418 { return m_mainWindow->Cleared(); }
419
420 wxDataViewMainWindow *m_mainWindow;
421 };
422
423 // ---------------------------------------------------------
424 // wxDataViewRenderer
425 // ---------------------------------------------------------
426
427 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase)
428
429 wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype,
430 wxDataViewCellMode mode,
431 int align) :
432 wxDataViewRendererBase( varianttype, mode, align )
433 {
434 m_dc = NULL;
435 m_align = align;
436 m_mode = mode;
437 }
438
439 wxDataViewRenderer::~wxDataViewRenderer()
440 {
441 if (m_dc)
442 delete m_dc;
443 }
444
445 wxDC *wxDataViewRenderer::GetDC()
446 {
447 if (m_dc == NULL)
448 {
449 if (GetOwner() == NULL)
450 return NULL;
451 if (GetOwner()->GetOwner() == NULL)
452 return NULL;
453 m_dc = new wxClientDC( GetOwner()->GetOwner() );
454 }
455
456 return m_dc;
457 }
458
459
460 // ---------------------------------------------------------
461 // wxDataViewCustomRenderer
462 // ---------------------------------------------------------
463
464 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
465
466 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
467 wxDataViewCellMode mode, int align ) :
468 wxDataViewRenderer( varianttype, mode, align )
469 {
470 }
471
472 // ---------------------------------------------------------
473 // wxDataViewTextRenderer
474 // ---------------------------------------------------------
475
476 IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewCustomRenderer)
477
478 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype,
479 wxDataViewCellMode mode, int align ) :
480 wxDataViewCustomRenderer( varianttype, mode, align )
481 {
482 }
483
484 bool wxDataViewTextRenderer::SetValue( const wxVariant &value )
485 {
486 m_text = value.GetString();
487
488 return true;
489 }
490
491 bool wxDataViewTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const
492 {
493 return false;
494 }
495
496 bool wxDataViewTextRenderer::Render( wxRect cell, wxDC *dc, int state )
497 {
498 wxDataViewCtrl *view = GetOwner()->GetOwner();
499 wxColour col = (state & wxDATAVIEW_CELL_SELECTED) ?
500 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) :
501 view->GetForegroundColour();
502
503 dc->SetTextForeground(col);
504 dc->DrawText( m_text, cell.x, cell.y );
505
506 return true;
507 }
508
509 wxSize wxDataViewTextRenderer::GetSize() const
510 {
511 const wxDataViewCtrl *view = GetView();
512 if (!m_text.empty())
513 {
514 int x,y;
515 view->GetTextExtent( m_text, &x, &y );
516 return wxSize( x, y );
517 }
518 return wxSize(80,20);
519 }
520
521 // ---------------------------------------------------------
522 // wxDataViewBitmapRenderer
523 // ---------------------------------------------------------
524
525 IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewCustomRenderer)
526
527 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype,
528 wxDataViewCellMode mode, int align ) :
529 wxDataViewCustomRenderer( varianttype, mode, align )
530 {
531 }
532
533 bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
534 {
535 if (value.GetType() == wxT("wxBitmap"))
536 m_bitmap << value;
537 if (value.GetType() == wxT("wxIcon"))
538 m_icon << value;
539
540 return true;
541 }
542
543 bool wxDataViewBitmapRenderer::GetValue( wxVariant& WXUNUSED(value) ) const
544 {
545 return false;
546 }
547
548 bool wxDataViewBitmapRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
549 {
550 if (m_bitmap.Ok())
551 dc->DrawBitmap( m_bitmap, cell.x, cell.y );
552 else if (m_icon.Ok())
553 dc->DrawIcon( m_icon, cell.x, cell.y );
554
555 return true;
556 }
557
558 wxSize wxDataViewBitmapRenderer::GetSize() const
559 {
560 if (m_bitmap.Ok())
561 return wxSize( m_bitmap.GetWidth(), m_bitmap.GetHeight() );
562 else if (m_icon.Ok())
563 return wxSize( m_icon.GetWidth(), m_icon.GetHeight() );
564
565 return wxSize(16,16);
566 }
567
568 // ---------------------------------------------------------
569 // wxDataViewToggleRenderer
570 // ---------------------------------------------------------
571
572 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer, wxDataViewCustomRenderer)
573
574 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
575 wxDataViewCellMode mode, int align ) :
576 wxDataViewCustomRenderer( varianttype, mode, align )
577 {
578 m_toggle = false;
579 }
580
581 bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
582 {
583 m_toggle = value.GetBool();
584
585 return true;
586 }
587
588 bool wxDataViewToggleRenderer::GetValue( wxVariant &WXUNUSED(value) ) const
589 {
590 return false;
591 }
592
593 bool wxDataViewToggleRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
594 {
595 // User wxRenderer here
596
597 wxRect rect;
598 rect.x = cell.x + cell.width/2 - 10;
599 rect.width = 20;
600 rect.y = cell.y + cell.height/2 - 10;
601 rect.height = 20;
602
603 int flags = 0;
604 if (m_toggle)
605 flags |= wxCONTROL_CHECKED;
606 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE)
607 flags |= wxCONTROL_DISABLED;
608
609 wxRendererNative::Get().DrawCheckBox(
610 GetOwner()->GetOwner(),
611 *dc,
612 rect,
613 flags );
614
615 return true;
616 }
617
618 bool wxDataViewToggleRenderer::Activate( wxRect WXUNUSED(cell),
619 wxDataViewListModel *model,
620 unsigned int col, unsigned int row )
621 {
622 bool value = !m_toggle;
623 wxVariant variant = value;
624 model->SetValue( variant, col, row );
625 model->ValueChanged( col, row );
626 return true;
627 }
628
629 wxSize wxDataViewToggleRenderer::GetSize() const
630 {
631 return wxSize(20,20);
632 }
633
634 // ---------------------------------------------------------
635 // wxDataViewProgressRenderer
636 // ---------------------------------------------------------
637
638 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer)
639
640 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
641 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
642 wxDataViewCustomRenderer( varianttype, mode, align )
643 {
644 m_label = label;
645 m_value = 0;
646 }
647
648 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
649 {
650 }
651
652 bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
653 {
654 m_value = (long) value;
655
656 if (m_value < 0) m_value = 0;
657 if (m_value > 100) m_value = 100;
658
659 return true;
660 }
661
662 bool wxDataViewProgressRenderer::GetValue( wxVariant &value ) const
663 {
664 value = (long) m_value;
665 return true;
666 }
667
668 bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
669 {
670 double pct = (double)m_value / 100.0;
671 wxRect bar = cell;
672 bar.width = (int)(cell.width * pct);
673 dc->SetPen( *wxTRANSPARENT_PEN );
674 dc->SetBrush( *wxBLUE_BRUSH );
675 dc->DrawRectangle( bar );
676
677 dc->SetBrush( *wxTRANSPARENT_BRUSH );
678 dc->SetPen( *wxBLACK_PEN );
679 dc->DrawRectangle( cell );
680
681 return true;
682 }
683
684 wxSize wxDataViewProgressRenderer::GetSize() const
685 {
686 return wxSize(40,12);
687 }
688
689 // ---------------------------------------------------------
690 // wxDataViewDateRenderer
691 // ---------------------------------------------------------
692
693 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
694
695 #if wxUSE_DATE_RENDERER_POPUP
696
697 class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow
698 {
699 public:
700 wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value,
701 wxDataViewListModel *model, unsigned int col, unsigned int row ) :
702 wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
703 {
704 m_model = model;
705 m_col = col;
706 m_row = row;
707 m_cal = new wxCalendarCtrl( this, wxID_ANY, *value );
708 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
709 sizer->Add( m_cal, 1, wxGROW );
710 SetSizer( sizer );
711 sizer->Fit( this );
712 }
713
714 void OnCalendar( wxCalendarEvent &event );
715
716 wxCalendarCtrl *m_cal;
717 wxDataViewListModel *m_model;
718 unsigned int m_col;
719 unsigned int m_row;
720
721 protected:
722 virtual void OnDismiss()
723 {
724 }
725
726 private:
727 DECLARE_EVENT_TABLE()
728 };
729
730 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow)
731 EVT_CALENDAR( wxID_ANY, wxDataViewDateRendererPopupTransient::OnCalendar )
732 END_EVENT_TABLE()
733
734 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event )
735 {
736 wxDateTime date = event.GetDate();
737 wxVariant value = date;
738 m_model->SetValue( value, m_col, m_row );
739 m_model->ValueChanged( m_col, m_row );
740 DismissAndNotify();
741 }
742
743 #endif // wxUSE_DATE_RENDERER_POPUP
744
745 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer)
746
747 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype,
748 wxDataViewCellMode mode, int align ) :
749 wxDataViewCustomRenderer( varianttype, mode, align )
750 {
751 }
752
753 bool wxDataViewDateRenderer::SetValue( const wxVariant &value )
754 {
755 m_date = value.GetDateTime();
756
757 return true;
758 }
759
760 bool wxDataViewDateRenderer::GetValue( wxVariant &value ) const
761 {
762 value = m_date;
763 return true;
764 }
765
766 bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
767 {
768 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
769 wxString tmp = m_date.FormatDate();
770 dc->DrawText( tmp, cell.x, cell.y );
771
772 return true;
773 }
774
775 wxSize wxDataViewDateRenderer::GetSize() const
776 {
777 const wxDataViewCtrl* view = GetView();
778 wxString tmp = m_date.FormatDate();
779 wxCoord x,y,d;
780 view->GetTextExtent( tmp, &x, &y, &d );
781 return wxSize(x,y+d);
782 }
783
784 bool wxDataViewDateRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewListModel *model,
785 unsigned int col, unsigned int row )
786 {
787 wxVariant variant;
788 model->GetValue( variant, col, row );
789 wxDateTime value = variant.GetDateTime();
790
791 #if wxUSE_DATE_RENDERER_POPUP
792 wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient(
793 GetOwner()->GetOwner()->GetParent(), &value, model, col, row );
794 wxPoint pos = wxGetMousePosition();
795 popup->Move( pos );
796 popup->Layout();
797 popup->Popup( popup->m_cal );
798 #else // !wxUSE_DATE_RENDERER_POPUP
799 wxMessageBox(value.Format());
800 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
801 return true;
802 }
803
804 // ---------------------------------------------------------
805 // wxDataViewColumn
806 // ---------------------------------------------------------
807
808 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn, wxDataViewColumnBase)
809
810 wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell,
811 unsigned int model_column,
812 int width, wxAlignment align, int flags ) :
813 wxDataViewColumnBase( title, cell, model_column, width, align, flags )
814 {
815 SetAlignment(align);
816 SetTitle(title);
817 SetFlags(flags);
818
819 Init(width < 0 ? wxDVC_DEFAULT_WIDTH : width);
820 }
821
822 wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell,
823 unsigned int model_column,
824 int width, wxAlignment align, int flags ) :
825 wxDataViewColumnBase( bitmap, cell, model_column, width, align, flags )
826 {
827 SetAlignment(align);
828 SetFlags(flags);
829
830 Init(width < 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH : width);
831 }
832
833 wxDataViewColumn::~wxDataViewColumn()
834 {
835 }
836
837 void wxDataViewColumn::Init( int width )
838 {
839 m_width = width;
840 m_minWidth = wxDVC_DEFAULT_MINWIDTH;
841 }
842
843 void wxDataViewColumn::SetResizeable( bool resizeable )
844 {
845 if (resizeable)
846 m_flags |= wxDATAVIEW_COL_RESIZABLE;
847 else
848 m_flags &= ~wxDATAVIEW_COL_RESIZABLE;
849 }
850
851 void wxDataViewColumn::SetHidden( bool hidden )
852 {
853 if (hidden)
854 m_flags |= wxDATAVIEW_COL_HIDDEN;
855 else
856 m_flags &= ~wxDATAVIEW_COL_HIDDEN;
857
858 // tell our owner to e.g. update its scrollbars:
859 if (GetOwner())
860 GetOwner()->OnColumnChange();
861 }
862
863 void wxDataViewColumn::SetSortable( bool sortable )
864 {
865 if (sortable)
866 m_flags |= wxDATAVIEW_COL_SORTABLE;
867 else
868 m_flags &= ~wxDATAVIEW_COL_SORTABLE;
869 }
870
871 void wxDataViewColumn::SetSortOrder( bool WXUNUSED(ascending) )
872 {
873 // TODO
874 }
875
876 bool wxDataViewColumn::IsSortOrderAscending() const
877 {
878 // TODO
879 return true;
880 }
881
882 void wxDataViewColumn::SetInternalWidth( int width )
883 {
884 m_width = width;
885
886 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
887 if (m_owner && m_owner->m_clientArea)
888 m_owner->m_clientArea->RecalculateDisplay();
889 }
890
891 void wxDataViewColumn::SetWidth( int width )
892 {
893 m_owner->m_headerArea->UpdateDisplay();
894
895 SetInternalWidth(width);
896 }
897
898
899 //-----------------------------------------------------------------------------
900 // wxDataViewHeaderWindowBase
901 //-----------------------------------------------------------------------------
902
903 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type, unsigned int n)
904 {
905 wxWindow *parent = GetParent();
906 wxDataViewEvent le(type, parent->GetId());
907
908 le.SetEventObject(parent);
909 le.SetColumn(n);
910 le.SetDataViewColumn(GetColumn(n));
911 le.SetModel(GetOwner()->GetModel());
912
913 // for events created by wxDataViewHeaderWindow the
914 // row / value fields are not valid
915
916 parent->GetEventHandler()->ProcessEvent(le);
917 }
918
919 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
920
921 // implemented in msw/listctrl.cpp:
922 int WXDLLIMPEXP_CORE wxMSWGetColumnClicked(NMHDR *nmhdr, POINT *ptClick);
923
924 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW, wxWindow)
925
926 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl *parent, wxWindowID id,
927 const wxPoint &pos, const wxSize &size,
928 const wxString &name )
929 {
930 m_owner = parent;
931
932 if ( !CreateControl(parent, id, pos, size, 0, wxDefaultValidator, name) )
933 return false;
934
935 int x = pos.x == wxDefaultCoord ? 0 : pos.x,
936 y = pos.y == wxDefaultCoord ? 0 : pos.y,
937 w = size.x == wxDefaultCoord ? 1 : size.x,
938 h = size.y == wxDefaultCoord ? 22 : size.y;
939
940 // create the native WC_HEADER window:
941 WXHWND hwndParent = (HWND)parent->GetHandle();
942 WXDWORD msStyle = WS_CHILD | HDS_BUTTONS | HDS_HORZ | HDS_HOTTRACK | HDS_FULLDRAG;
943 m_hWnd = CreateWindowEx(0,
944 WC_HEADER,
945 (LPCTSTR) NULL,
946 msStyle,
947 x, y, w, h,
948 (HWND)hwndParent,
949 (HMENU)-1,
950 wxGetInstance(),
951 (LPVOID) NULL);
952 if (m_hWnd == NULL)
953 {
954 wxLogLastError(_T("CreateWindowEx"));
955 return false;
956 }
957
958 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
959 // to call wxDataViewHeaderWindow::MSWOnNotify
960 SubclassWin(m_hWnd);
961
962 // the following is required to get the default win's font for
963 // header windows and must be done befor sending the HDM_LAYOUT msg
964 SetFont(GetFont());
965
966 RECT rcParent;
967 HDLAYOUT hdl;
968 WINDOWPOS wp;
969
970 // Retrieve the bounding rectangle of the parent window's
971 // client area, and then request size and position values
972 // from the header control.
973 ::GetClientRect((HWND)hwndParent, &rcParent);
974
975 hdl.prc = &rcParent;
976 hdl.pwpos = &wp;
977 if (!SendMessage((HWND)m_hWnd, HDM_LAYOUT, 0, (LPARAM) &hdl))
978 {
979 wxLogLastError(_T("SendMessage"));
980 return false;
981 }
982
983 // Set the size, position, and visibility of the header control.
984 SetWindowPos((HWND)m_hWnd,
985 wp.hwndInsertAfter,
986 wp.x, wp.y,
987 wp.cx, wp.cy,
988 wp.flags | SWP_SHOWWINDOW);
989
990 // set our size hints: wxDataViewCtrl will put this wxWindow inside
991 // a wxBoxSizer and in order to avoid super-big header windows,
992 // we need to set our height as fixed
993 SetMinSize(wxSize(-1, wp.cy));
994 SetMaxSize(wxSize(-1, wp.cy));
995
996 return true;
997 }
998
999 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1000 {
1001 UnsubclassWin();
1002 }
1003
1004 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1005 {
1006 // remove old columns
1007 for (int j=0, max=Header_GetItemCount((HWND)m_hWnd); j < max; j++)
1008 Header_DeleteItem((HWND)m_hWnd, 0);
1009
1010 // add the updated array of columns to the header control
1011 unsigned int cols = GetOwner()->GetColumnCount();
1012 unsigned int added = 0;
1013 for (unsigned int i = 0; i < cols; i++)
1014 {
1015 wxDataViewColumn *col = GetColumn( i );
1016 if (col->IsHidden())
1017 continue; // don't add it!
1018
1019 HDITEM hdi;
1020 hdi.mask = HDI_TEXT | HDI_FORMAT | HDI_WIDTH;
1021 hdi.pszText = (wxChar *) col->GetTitle().c_str();
1022 hdi.cxy = col->GetWidth();
1023 hdi.cchTextMax = sizeof(hdi.pszText)/sizeof(hdi.pszText[0]);
1024 hdi.fmt = HDF_LEFT | HDF_STRING;
1025
1026 // lParam is reserved for application's use:
1027 // we store there the column index to use it later in MSWOnNotify
1028 // (since columns may have been hidden)
1029 hdi.lParam = (LPARAM)i;
1030
1031 // the native wxMSW implementation of the header window
1032 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1033 // on the right: to correct this effect we make the column
1034 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1035 if (i == 0)
1036 hdi.cxy += COLUMN_WIDTH_OFFSET;
1037
1038 switch (col->GetAlignment())
1039 {
1040 case wxALIGN_LEFT:
1041 hdi.fmt |= HDF_LEFT;
1042 break;
1043 case wxALIGN_CENTER:
1044 case wxALIGN_CENTER_HORIZONTAL:
1045 hdi.fmt |= HDF_CENTER;
1046 break;
1047 case wxALIGN_RIGHT:
1048 hdi.fmt |= HDF_RIGHT;
1049 break;
1050
1051 default:
1052 // such alignment is not allowed for the column header!
1053 wxFAIL;
1054 }
1055
1056 SendMessage((HWND)m_hWnd, HDM_INSERTITEM,
1057 (WPARAM)added, (LPARAM)&hdi);
1058 added++;
1059 }
1060 }
1061
1062 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER *nmHDR)
1063 {
1064 unsigned int idx;
1065
1066 // NOTE: we don't just return nmHDR->iItem because when there are
1067 // hidden columns, nmHDR->iItem may be different from
1068 // nmHDR->pitem->lParam
1069
1070 if (nmHDR->pitem && nmHDR->pitem->mask & HDI_LPARAM)
1071 {
1072 idx = (unsigned int)nmHDR->pitem->lParam;
1073 return idx;
1074 }
1075
1076 HDITEM item;
1077 item.mask = HDI_LPARAM;
1078 Header_GetItem((HWND)m_hWnd, nmHDR->iItem, &item);
1079
1080 return (unsigned int)item.lParam;
1081 }
1082
1083 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1084 {
1085 NMHDR *nmhdr = (NMHDR *)lParam;
1086
1087 // is it a message from the header?
1088 if ( nmhdr->hwndFrom != (HWND)m_hWnd )
1089 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
1090
1091 NMHEADER *nmHDR = (NMHEADER *)nmhdr;
1092 switch ( nmhdr->code )
1093 {
1094 case HDN_BEGINTRACK:
1095 // user has started to resize a column:
1096 // do we need to veto it?
1097 if (!GetColumn(nmHDR->iItem)->IsResizeable())
1098 {
1099 // veto it!
1100 *result = TRUE;
1101 }
1102 break;
1103
1104 case HDN_BEGINDRAG:
1105 // user has started to reorder a column
1106 break;
1107
1108 case HDN_ITEMCHANGING:
1109 if (nmHDR->pitem != NULL &&
1110 (nmHDR->pitem->mask & HDI_WIDTH) != 0)
1111 {
1112 int minWidth = GetColumnFromHeader(nmHDR)->GetMinWidth();
1113 if (nmHDR->pitem->cxy < minWidth)
1114 {
1115 // do not allow the user to resize this column under
1116 // its minimal width:
1117 *result = TRUE;
1118 }
1119 }
1120 break;
1121
1122 case HDN_ITEMCHANGED: // user is resizing a column
1123 case HDN_ENDTRACK: // user has finished resizing a column
1124 case HDN_ENDDRAG: // user has finished reordering a column
1125
1126 // update the width of the modified column:
1127 if (nmHDR->pitem != NULL &&
1128 (nmHDR->pitem->mask & HDI_WIDTH) != 0)
1129 {
1130 unsigned int idx = GetColumnIdxFromHeader(nmHDR);
1131 unsigned int w = nmHDR->pitem->cxy;
1132 wxDataViewColumn *col = GetColumn(idx);
1133
1134 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1135 if (idx == 0 && w > COLUMN_WIDTH_OFFSET)
1136 w -= COLUMN_WIDTH_OFFSET;
1137
1138 if (w >= (unsigned)col->GetMinWidth())
1139 col->SetInternalWidth(w);
1140 }
1141 break;
1142
1143 case HDN_ITEMCLICK:
1144 {
1145 unsigned int idx = GetColumnIdxFromHeader(nmHDR);
1146 wxEventType evt = nmHDR->iButton == 0 ?
1147 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK :
1148 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK;
1149 SendEvent(evt, idx);
1150 }
1151 break;
1152
1153 case NM_RCLICK:
1154 {
1155 // NOTE: for some reason (i.e. for a bug in Windows)
1156 // the HDN_ITEMCLICK notification is not sent on
1157 // right clicks, so we need to handle NM_RCLICK
1158
1159 POINT ptClick;
1160 int column = wxMSWGetColumnClicked(nmhdr, &ptClick);
1161 if (column != wxNOT_FOUND)
1162 {
1163 HDITEM item;
1164 item.mask = HDI_LPARAM;
1165 Header_GetItem((HWND)m_hWnd, column, &item);
1166
1167 // 'idx' may be different from 'column' if there are
1168 // hidden columns...
1169 unsigned int idx = (unsigned int)item.lParam;
1170 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK,
1171 idx);
1172 }
1173 }
1174 break;
1175
1176 case HDN_GETDISPINFOW:
1177 // see wxListCtrl::MSWOnNotify for more info!
1178 break;
1179
1180 case HDN_ITEMDBLCLICK:
1181 {
1182 unsigned int idx = GetColumnIdxFromHeader(nmHDR);
1183 int w = GetOwner()->GetBestColumnWidth(idx);
1184
1185 // update the native control:
1186 HDITEM hd;
1187 ZeroMemory(&hd, sizeof(hd));
1188 hd.mask = HDI_WIDTH;
1189 hd.cxy = w;
1190 Header_SetItem(GetHwnd(),
1191 nmHDR->iItem, // NOTE: we don't want 'idx' here!
1192 &hd);
1193
1194 // update the wxDataViewColumn class:
1195 GetColumn(idx)->SetInternalWidth(w);
1196 }
1197 break;
1198
1199 default:
1200 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
1201 }
1202
1203 return true;
1204 }
1205
1206 void wxDataViewHeaderWindowMSW::ScrollWindow(int WXUNUSED(dx), int WXUNUSED(dy),
1207 const wxRect *WXUNUSED(rect))
1208 {
1209 wxSize ourSz = GetClientSize();
1210 wxSize ownerSz = m_owner->GetClientSize();
1211
1212 // where should the (logical) origin of this window be placed?
1213 int x1 = 0, y1 = 0;
1214 m_owner->CalcUnscrolledPosition(0, 0, &x1, &y1);
1215
1216 // put this window on top of our parent and
1217 SetWindowPos((HWND)m_hWnd, HWND_TOP, -x1, 0,
1218 ownerSz.GetWidth() + x1, ourSz.GetHeight(),
1219 SWP_SHOWWINDOW);
1220 }
1221
1222 void wxDataViewHeaderWindowMSW::DoSetSize(int WXUNUSED(x), int WXUNUSED(y),
1223 int WXUNUSED(w), int WXUNUSED(h),
1224 int WXUNUSED(f))
1225 {
1226 // the wxDataViewCtrl's internal wxBoxSizer will call this function when
1227 // the wxDataViewCtrl window gets resized: the following dummy call
1228 // to ScrollWindow() is required in order to get this header window
1229 // correctly repainted when it's (horizontally) scrolled:
1230
1231 ScrollWindow(0, 0);
1232 }
1233
1234 #else // !defined(__WXMSW__)
1235
1236 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow, wxWindow)
1237 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow, wxWindow)
1238 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint)
1239 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse)
1240 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus)
1241 END_EVENT_TABLE()
1242
1243 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl *parent, wxWindowID id,
1244 const wxPoint &pos, const wxSize &size,
1245 const wxString &name )
1246 {
1247 m_owner = parent;
1248
1249 if (!wxDataViewHeaderWindowBase::Create(parent, id, pos, size, name))
1250 return false;
1251
1252 wxVisualAttributes attr = wxPanel::GetClassDefaultAttributes();
1253 SetBackgroundStyle( wxBG_STYLE_CUSTOM );
1254 SetOwnForegroundColour( attr.colFg );
1255 SetOwnBackgroundColour( attr.colBg );
1256 if (!m_hasFont)
1257 SetOwnFont( attr.font );
1258
1259 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1260 // a wxBoxSizer and in order to avoid super-big header windows,
1261 // we need to set our height as fixed
1262 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT));
1263 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT));
1264
1265 return true;
1266 }
1267
1268 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1269 {
1270 int w, h;
1271 GetClientSize( &w, &h );
1272
1273 wxAutoBufferedPaintDC dc( this );
1274
1275 dc.SetBackground(GetBackgroundColour());
1276 dc.Clear();
1277
1278 int xpix;
1279 m_owner->GetScrollPixelsPerUnit( &xpix, NULL );
1280
1281 int x;
1282 m_owner->GetViewStart( &x, NULL );
1283
1284 // account for the horz scrollbar offset
1285 dc.SetDeviceOrigin( -x * xpix, 0 );
1286
1287 dc.SetFont( GetFont() );
1288
1289 unsigned int cols = GetOwner()->GetColumnCount();
1290 unsigned int i;
1291 int xpos = 0;
1292 for (i = 0; i < cols; i++)
1293 {
1294 wxDataViewColumn *col = GetColumn( i );
1295 if (col->IsHidden())
1296 continue; // skip it!
1297
1298 int cw = col->GetWidth();
1299 int ch = h;
1300
1301 wxRendererNative::Get().DrawHeaderButton
1302 (
1303 this,
1304 dc,
1305 wxRect(xpos, 0, cw, ch-1),
1306 m_parent->IsEnabled() ? 0
1307 : (int)wxCONTROL_DISABLED
1308 );
1309
1310 // align as required the column title:
1311 int x = xpos;
1312 wxSize titleSz = dc.GetTextExtent(col->GetTitle());
1313 switch (col->GetAlignment())
1314 {
1315 case wxALIGN_LEFT:
1316 x += HEADER_HORIZ_BORDER;
1317 break;
1318 case wxALIGN_CENTER:
1319 case wxALIGN_CENTER_HORIZONTAL:
1320 x += (cw - titleSz.GetWidth() - 2 * HEADER_HORIZ_BORDER)/2;
1321 break;
1322 case wxALIGN_RIGHT:
1323 x += cw - titleSz.GetWidth() - HEADER_HORIZ_BORDER;
1324 break;
1325 }
1326
1327 // always center the title vertically:
1328 int y = wxMax((ch - titleSz.GetHeight()) / 2, HEADER_VERT_BORDER);
1329
1330 dc.SetClippingRegion( xpos+HEADER_HORIZ_BORDER,
1331 HEADER_VERT_BORDER,
1332 wxMax(cw - 2 * HEADER_HORIZ_BORDER, 1), // width
1333 wxMax(ch - 2 * HEADER_VERT_BORDER, 1)); // height
1334 dc.DrawText( col->GetTitle(), x, y );
1335 dc.DestroyClippingRegion();
1336
1337 xpos += cw;
1338 }
1339 }
1340
1341 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent &event )
1342 {
1343 GetParent()->SetFocus();
1344 event.Skip();
1345 }
1346
1347 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent &event )
1348 {
1349 // we want to work with logical coords
1350 int x;
1351 m_owner->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
1352 int y = event.GetY();
1353
1354 if (m_isDragging)
1355 {
1356 // we don't draw the line beyond our window,
1357 // but we allow dragging it there
1358 int w = 0;
1359 GetClientSize( &w, NULL );
1360 m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
1361 w -= 6;
1362
1363 // erase the line if it was drawn
1364 if (m_currentX < w)
1365 DrawCurrent();
1366
1367 if (event.ButtonUp())
1368 {
1369 m_isDragging = false;
1370 if (HasCapture())
1371 ReleaseMouse();
1372
1373 m_dirty = true;
1374
1375 GetColumn(m_column)->SetWidth(m_currentX - m_minX);
1376
1377 Refresh();
1378 GetOwner()->Refresh();
1379 }
1380 else
1381 {
1382 m_currentX = wxMax(m_minX + 7, x);
1383
1384 // draw in the new location
1385 if (m_currentX < w) DrawCurrent();
1386 }
1387
1388 }
1389 else // not dragging
1390 {
1391 m_minX = 0;
1392 m_column = wxNOT_FOUND;
1393
1394 bool hit_border = false;
1395
1396 // end of the current column
1397 int xpos = 0;
1398
1399 // find the column where this event occured
1400 int countCol = m_owner->GetColumnCount();
1401 for (int column = 0; column < countCol; column++)
1402 {
1403 wxDataViewColumn *p = GetColumn(column);
1404
1405 if (p->IsHidden())
1406 continue; // skip if not shown
1407
1408 xpos += p->GetWidth();
1409 m_column = column;
1410 if ((abs(x-xpos) < 3) && (y < 22))
1411 {
1412 hit_border = true;
1413 break;
1414 }
1415
1416 if (x < xpos)
1417 {
1418 // inside the column
1419 break;
1420 }
1421
1422 m_minX = xpos;
1423 }
1424
1425 if (m_column == wxNOT_FOUND)
1426 return;
1427
1428 bool resizeable = GetColumn(m_column)->IsResizeable();
1429 if (event.LeftDClick() && resizeable)
1430 {
1431 GetColumn(m_column)->SetWidth(GetOwner()->GetBestColumnWidth(m_column));
1432 Refresh();
1433 }
1434 else if (event.LeftDown() || event.RightUp())
1435 {
1436 if (hit_border && event.LeftDown() && resizeable)
1437 {
1438 m_isDragging = true;
1439 CaptureMouse();
1440 m_currentX = x;
1441 DrawCurrent();
1442 }
1443 else // click on a column
1444 {
1445 wxEventType evt = event.LeftDown() ?
1446 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK :
1447 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK;
1448 SendEvent(evt, m_column);
1449 }
1450 }
1451 else if (event.Moving())
1452 {
1453 if (hit_border && resizeable)
1454 m_currentCursor = m_resizeCursor;
1455 else
1456 m_currentCursor = wxSTANDARD_CURSOR;
1457
1458 SetCursor(*m_currentCursor);
1459 }
1460 }
1461 }
1462
1463 void wxGenericDataViewHeaderWindow::DrawCurrent()
1464 {
1465 int x1 = m_currentX;
1466 int y1 = 0;
1467 ClientToScreen (&x1, &y1);
1468
1469 int x2 = m_currentX-1;
1470 #ifdef __WXMSW__
1471 ++x2; // but why ????
1472 #endif
1473 int y2 = 0;
1474 m_owner->GetClientSize( NULL, &y2 );
1475 m_owner->ClientToScreen( &x2, &y2 );
1476
1477 wxScreenDC dc;
1478 dc.SetLogicalFunction(wxINVERT);
1479 dc.SetPen(m_penCurrent);
1480 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1481 AdjustDC(dc);
1482 dc.DrawLine(x1, y1, x2, y2);
1483 }
1484
1485 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC& dc)
1486 {
1487 int xpix, x;
1488
1489 m_owner->GetScrollPixelsPerUnit( &xpix, NULL );
1490 m_owner->GetViewStart( &x, NULL );
1491
1492 // shift the DC origin to match the position of the main window horizontal
1493 // scrollbar: this allows us to always use logical coords
1494 dc.SetDeviceOrigin( -x * xpix, 0 );
1495 }
1496
1497 #endif // defined(__WXMSW__)
1498
1499 //-----------------------------------------------------------------------------
1500 // wxDataViewRenameTimer
1501 //-----------------------------------------------------------------------------
1502
1503 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow *owner )
1504 {
1505 m_owner = owner;
1506 }
1507
1508 void wxDataViewRenameTimer::Notify()
1509 {
1510 m_owner->OnRenameTimer();
1511 }
1512
1513 //-----------------------------------------------------------------------------
1514 // wxDataViewTextCtrlWrapper: wraps a wxTextCtrl for inline editing
1515 //-----------------------------------------------------------------------------
1516
1517 BEGIN_EVENT_TABLE(wxDataViewTextCtrlWrapper, wxEvtHandler)
1518 EVT_CHAR (wxDataViewTextCtrlWrapper::OnChar)
1519 EVT_KEY_UP (wxDataViewTextCtrlWrapper::OnKeyUp)
1520 EVT_KILL_FOCUS (wxDataViewTextCtrlWrapper::OnKillFocus)
1521 END_EVENT_TABLE()
1522
1523 wxDataViewTextCtrlWrapper::wxDataViewTextCtrlWrapper(
1524 wxDataViewMainWindow *owner,
1525 wxTextCtrl *text,
1526 wxDataViewListModel *model,
1527 unsigned int col, unsigned int row,
1528 wxRect rectLabel )
1529 {
1530 m_owner = owner;
1531 m_model = model;
1532 m_row = row;
1533 m_col = col;
1534 m_text = text;
1535
1536 m_finished = false;
1537 m_aboutToFinish = false;
1538
1539 wxVariant value;
1540 model->GetValue( value, col, row );
1541 m_startValue = value.GetString();
1542
1543 m_owner->GetOwner()->CalcScrolledPosition(
1544 rectLabel.x, rectLabel.y, &rectLabel.x, &rectLabel.y );
1545
1546 m_text->Create( owner, wxID_ANY, m_startValue,
1547 wxPoint(rectLabel.x-2,rectLabel.y-2),
1548 wxSize(rectLabel.width+7,rectLabel.height+4) );
1549 m_text->SetFocus();
1550
1551 m_text->PushEventHandler(this);
1552 }
1553
1554 void wxDataViewTextCtrlWrapper::AcceptChangesAndFinish()
1555 {
1556 m_aboutToFinish = true;
1557
1558 // Notify the owner about the changes
1559 AcceptChanges();
1560
1561 // Even if vetoed, close the control (consistent with MSW)
1562 Finish();
1563 }
1564
1565 void wxDataViewTextCtrlWrapper::OnChar( wxKeyEvent &event )
1566 {
1567 switch ( event.m_keyCode )
1568 {
1569 case WXK_RETURN:
1570 AcceptChangesAndFinish();
1571 break;
1572
1573 case WXK_ESCAPE:
1574 // m_owner->OnRenameCancelled( m_itemEdited );
1575 Finish();
1576 break;
1577
1578 default:
1579 event.Skip();
1580 }
1581 }
1582
1583 void wxDataViewTextCtrlWrapper::OnKeyUp( wxKeyEvent &event )
1584 {
1585 if (m_finished)
1586 {
1587 event.Skip();
1588 return;
1589 }
1590
1591 // auto-grow the textctrl
1592 wxSize parentSize = m_owner->GetSize();
1593 wxPoint myPos = m_text->GetPosition();
1594 wxSize mySize = m_text->GetSize();
1595 int sx, sy;
1596 m_text->GetTextExtent(m_text->GetValue() + _T("MM"), &sx, &sy);
1597 if (myPos.x + sx > parentSize.x)
1598 sx = parentSize.x - myPos.x;
1599 if (mySize.x > sx)
1600 sx = mySize.x;
1601 m_text->SetSize(sx, wxDefaultCoord);
1602
1603 event.Skip();
1604 }
1605
1606 void wxDataViewTextCtrlWrapper::OnKillFocus( wxFocusEvent &event )
1607 {
1608 if ( !m_finished && !m_aboutToFinish )
1609 {
1610 AcceptChanges();
1611 //if ( !AcceptChanges() )
1612 // m_owner->OnRenameCancelled( m_itemEdited );
1613
1614 Finish();
1615 }
1616
1617 // We must let the native text control handle focus
1618 event.Skip();
1619 }
1620
1621 bool wxDataViewTextCtrlWrapper::AcceptChanges()
1622 {
1623 const wxString value = m_text->GetValue();
1624
1625 if ( value == m_startValue )
1626 // nothing changed, always accept
1627 return true;
1628
1629 // if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
1630 // vetoed by the user
1631 // return false;
1632
1633 // accepted, do rename the item
1634 wxVariant variant;
1635 variant = value;
1636 m_model->SetValue( variant, m_col, m_row );
1637 m_model->ValueChanged( m_col, m_row );
1638
1639 return true;
1640 }
1641
1642 void wxDataViewTextCtrlWrapper::Finish()
1643 {
1644 if ( !m_finished )
1645 {
1646 m_finished = true;
1647
1648 m_text->RemoveEventHandler(this);
1649 m_owner->FinishEditing(m_text);
1650
1651 // delete later
1652 wxPendingDelete.Append( this );
1653 }
1654 }
1655
1656 //-----------------------------------------------------------------------------
1657 // wxDataViewMainWindow
1658 //-----------------------------------------------------------------------------
1659
1660 int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 )
1661 {
1662 if (row1 > row2) return 1;
1663 if (row1 == row2) return 0;
1664 return -1;
1665 }
1666
1667
1668 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow, wxWindow)
1669
1670 BEGIN_EVENT_TABLE(wxDataViewMainWindow,wxWindow)
1671 EVT_PAINT (wxDataViewMainWindow::OnPaint)
1672 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse)
1673 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus)
1674 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus)
1675 EVT_CHAR (wxDataViewMainWindow::OnChar)
1676 END_EVENT_TABLE()
1677
1678 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID id,
1679 const wxPoint &pos, const wxSize &size, const wxString &name ) :
1680 wxWindow( parent, id, pos, size, wxWANTS_CHARS, name ),
1681 m_selection( wxDataViewSelectionCmp )
1682
1683 {
1684 SetOwner( parent );
1685
1686 m_lastOnSame = false;
1687 m_renameTimer = new wxDataViewRenameTimer( this );
1688 m_textctrlWrapper = NULL;
1689
1690 // TODO: user better initial values/nothing selected
1691 m_currentCol = NULL;
1692 m_currentRow = 0;
1693
1694 // TODO: we need to calculate this smartly
1695 m_lineHeight =
1696 #ifdef __WXMSW__
1697 17;
1698 #else
1699 20;
1700 #endif
1701 wxASSERT(m_lineHeight > 2*PADDING_TOPBOTTOM);
1702
1703 m_dragCount = 0;
1704 m_dragStart = wxPoint(0,0);
1705 m_lineLastClicked = (unsigned int) -1;
1706 m_lineBeforeLastClicked = (unsigned int) -1;
1707 m_lineSelectSingleOnUp = (unsigned int) -1;
1708
1709 m_hasFocus = false;
1710
1711 SetBackgroundStyle( wxBG_STYLE_CUSTOM );
1712 SetBackgroundColour( *wxWHITE );
1713
1714 m_penRule = wxPen(GetRuleColour(), 1, wxSOLID);
1715
1716 UpdateDisplay();
1717 }
1718
1719 wxDataViewMainWindow::~wxDataViewMainWindow()
1720 {
1721 delete m_renameTimer;
1722 }
1723
1724 void wxDataViewMainWindow::OnRenameTimer()
1725 {
1726 // We have to call this here because changes may just have
1727 // been made and no screen update taken place.
1728 if ( m_dirty )
1729 wxSafeYield();
1730
1731
1732 int xpos = 0;
1733 unsigned int cols = GetOwner()->GetColumnCount();
1734 unsigned int i;
1735 for (i = 0; i < cols; i++)
1736 {
1737 wxDataViewColumn *c = GetOwner()->GetColumn( i );
1738 if (c->IsHidden())
1739 continue; // skip it!
1740
1741 if (c == m_currentCol)
1742 break;
1743 xpos += c->GetWidth();
1744 }
1745 wxRect labelRect( xpos, m_currentRow * m_lineHeight,
1746 m_currentCol->GetWidth(), m_lineHeight );
1747
1748 wxClassInfo *textControlClass = CLASSINFO(wxTextCtrl);
1749
1750 wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
1751 m_textctrlWrapper = new wxDataViewTextCtrlWrapper(this, text, GetOwner()->GetModel(),
1752 m_currentCol->GetModelColumn(), m_currentRow, labelRect );
1753 }
1754
1755 void wxDataViewMainWindow::FinishEditing( wxTextCtrl *text )
1756 {
1757 delete text;
1758 m_textctrlWrapper = NULL;
1759 SetFocus();
1760 // SetFocusIgnoringChildren();
1761 }
1762
1763 bool wxDataViewMainWindow::RowAppended()
1764 {
1765 return false;
1766 }
1767
1768 bool wxDataViewMainWindow::RowPrepended()
1769 {
1770 return false;
1771 }
1772
1773 bool wxDataViewMainWindow::RowInserted( unsigned int WXUNUSED(before) )
1774 {
1775 return false;
1776 }
1777
1778 bool wxDataViewMainWindow::RowDeleted( unsigned int WXUNUSED(row) )
1779 {
1780 return false;
1781 }
1782
1783 bool wxDataViewMainWindow::RowChanged( unsigned int WXUNUSED(row) )
1784 {
1785 return false;
1786 }
1787
1788 bool wxDataViewMainWindow::ValueChanged( unsigned int WXUNUSED(col), unsigned int row )
1789 {
1790 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
1791 #define MAX_VIRTUAL_WIDTH 100000
1792
1793 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
1794 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1795 Refresh( true, &rect );
1796
1797 return true;
1798 }
1799
1800 bool wxDataViewMainWindow::RowsReordered( unsigned int *WXUNUSED(new_order) )
1801 {
1802 Refresh();
1803
1804 return true;
1805 }
1806
1807 bool wxDataViewMainWindow::Cleared()
1808 {
1809 return false;
1810 }
1811
1812 void wxDataViewMainWindow::UpdateDisplay()
1813 {
1814 m_dirty = true;
1815 }
1816
1817 void wxDataViewMainWindow::OnInternalIdle()
1818 {
1819 wxWindow::OnInternalIdle();
1820
1821 if (m_dirty)
1822 {
1823 RecalculateDisplay();
1824 m_dirty = false;
1825 }
1826 }
1827
1828 void wxDataViewMainWindow::RecalculateDisplay()
1829 {
1830 wxDataViewListModel *model = GetOwner()->GetModel();
1831 if (!model)
1832 {
1833 Refresh();
1834 return;
1835 }
1836
1837 int width = GetEndOfLastCol();
1838 int height = model->GetRowCount() * m_lineHeight;
1839
1840 SetVirtualSize( width, height );
1841 GetOwner()->SetScrollRate( 10, m_lineHeight );
1842
1843 Refresh();
1844 }
1845
1846 void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
1847 {
1848 wxWindow::ScrollWindow( dx, dy, rect );
1849
1850 if (GetOwner()->m_headerArea)
1851 GetOwner()->m_headerArea->ScrollWindow( dx, 0 );
1852 }
1853
1854 void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1855 {
1856 wxDataViewListModel *model = GetOwner()->GetModel();
1857 wxAutoBufferedPaintDC dc( this );
1858
1859 // prepare the DC
1860 dc.SetBackground(GetBackgroundColour());
1861 dc.Clear();
1862 GetOwner()->PrepareDC( dc );
1863 dc.SetFont( GetFont() );
1864
1865 wxRect update = GetUpdateRegion().GetBox();
1866 m_owner->CalcUnscrolledPosition( update.x, update.y, &update.x, &update.y );
1867
1868 // compute which items needs to be redrawn
1869 unsigned int item_start = wxMax( 0, (update.y / m_lineHeight) );
1870 unsigned int item_count =
1871 wxMin( (int)(((update.y + update.height) / m_lineHeight) - item_start + 1),
1872 (int)(model->GetRowCount() - item_start) );
1873 unsigned int item_last = item_start + item_count;
1874
1875 // compute which columns needs to be redrawn
1876 unsigned int cols = GetOwner()->GetColumnCount();
1877 unsigned int col_start = 0;
1878 unsigned int x_start = 0;
1879 for (x_start = 0; col_start < cols; col_start++)
1880 {
1881 wxDataViewColumn *col = GetOwner()->GetColumn(col_start);
1882 if (col->IsHidden())
1883 continue; // skip it!
1884
1885 unsigned int w = col->GetWidth();
1886 if (x_start+w >= (unsigned int)update.x)
1887 break;
1888
1889 x_start += w;
1890 }
1891
1892 unsigned int col_last = col_start;
1893 unsigned int x_last = x_start;
1894 for (; col_last < cols; col_last++)
1895 {
1896 wxDataViewColumn *col = GetOwner()->GetColumn(col_last);
1897 if (col->IsHidden())
1898 continue; // skip it!
1899
1900 if (x_last > (unsigned int)update.GetRight())
1901 break;
1902
1903 x_last += col->GetWidth();
1904 }
1905
1906 // Draw horizontal rules if required
1907 if ( m_owner->HasFlag(wxDV_HORIZ_RULES) )
1908 {
1909 dc.SetPen(m_penRule);
1910 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1911
1912 for (unsigned int i = item_start; i <= item_last+1; i++)
1913 {
1914 int y = i * m_lineHeight;
1915 dc.DrawLine(x_start, y, x_last, y);
1916 }
1917 }
1918
1919 // Draw vertical rules if required
1920 if ( m_owner->HasFlag(wxDV_VERT_RULES) )
1921 {
1922 dc.SetPen(m_penRule);
1923 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1924
1925 int x = x_start;
1926 for (unsigned int i = col_start; i < col_last; i++)
1927 {
1928 wxDataViewColumn *col = GetOwner()->GetColumn(i);
1929 if (col->IsHidden())
1930 continue; // skip it
1931
1932 dc.DrawLine(x, item_start * m_lineHeight,
1933 x, item_last * m_lineHeight);
1934
1935 x += col->GetWidth();
1936 }
1937
1938 // Draw last vertical rule
1939 dc.DrawLine(x, item_start * m_lineHeight,
1940 x, item_last * m_lineHeight);
1941 }
1942
1943 // redraw the background for the items which are selected/current
1944 for (unsigned int item = item_start; item < item_last; item++)
1945 {
1946 bool selected = m_selection.Index( item ) != wxNOT_FOUND;
1947 if (selected || item == m_currentRow)
1948 {
1949 int flags = selected ? (int)wxCONTROL_SELECTED : 0;
1950 if (item == m_currentRow)
1951 flags |= wxCONTROL_CURRENT;
1952 if (m_hasFocus)
1953 flags |= wxCONTROL_FOCUSED;
1954
1955 wxRect rect( x_start, item*m_lineHeight, x_last, m_lineHeight );
1956 wxRendererNative::Get().DrawItemSelectionRect
1957 (
1958 this,
1959 dc,
1960 rect,
1961 flags
1962 );
1963 }
1964 }
1965
1966 // redraw all cells for all rows which must be repainted and for all columns
1967 wxRect cell_rect;
1968 cell_rect.x = x_start;
1969 cell_rect.height = m_lineHeight; // -1 is for the horizontal rules
1970 for (unsigned int i = col_start; i < col_last; i++)
1971 {
1972 wxDataViewColumn *col = GetOwner()->GetColumn( i );
1973 wxDataViewRenderer *cell = col->GetRenderer();
1974 cell_rect.width = col->GetWidth();
1975
1976 if (col->IsHidden())
1977 continue; // skipt it!
1978
1979 for (unsigned int item = item_start; item < item_last; item++)
1980 {
1981 // get the cell value and set it into the renderer
1982 wxVariant value;
1983 model->GetValue( value, col->GetModelColumn(), item );
1984 cell->SetValue( value );
1985
1986 // update the y offset
1987 cell_rect.y = item * m_lineHeight;
1988
1989 // cannot be bigger than allocated space
1990 wxSize size = cell->GetSize();
1991 size.x = wxMin( size.x + 2*PADDING_RIGHTLEFT, cell_rect.width );
1992 size.y = wxMin( size.y + 2*PADDING_TOPBOTTOM, cell_rect.height );
1993
1994 wxRect item_rect(cell_rect.GetTopLeft(), size);
1995 int align = cell->GetAlignment();
1996
1997 // horizontal alignment:
1998 item_rect.x = cell_rect.x;
1999 if (align & wxALIGN_CENTER_HORIZONTAL)
2000 item_rect.x = cell_rect.x + (cell_rect.width / 2) - (size.x / 2);
2001 else if (align & wxALIGN_RIGHT)
2002 item_rect.x = cell_rect.x + cell_rect.width - size.x;
2003 //else: wxALIGN_LEFT is the default
2004
2005 // vertical alignment:
2006 item_rect.y = cell_rect.y;
2007 if (align & wxALIGN_CENTER_VERTICAL)
2008 item_rect.y = cell_rect.y + (cell_rect.height / 2) - (size.y / 2);
2009 else if (align & wxALIGN_BOTTOM)
2010 item_rect.y = cell_rect.y + cell_rect.height - size.y;
2011 //else: wxALIGN_TOP is the default
2012
2013 // add padding
2014 item_rect.x += PADDING_RIGHTLEFT;
2015 item_rect.y += PADDING_TOPBOTTOM;
2016 item_rect.width = size.x - 2 * PADDING_RIGHTLEFT;
2017 item_rect.height = size.y - 2 * PADDING_TOPBOTTOM;
2018
2019 int state = 0;
2020 if (m_selection.Index(item) != wxNOT_FOUND)
2021 state |= wxDATAVIEW_CELL_SELECTED;
2022
2023 // TODO: it would be much more efficient to create a clipping
2024 // region for the entire column being rendered (in the OnPaint
2025 // of wxDataViewMainWindow) instead of a single clip region for
2026 // each cell. However it would mean that each renderer should
2027 // respect the given wxRect's top & bottom coords, eventually
2028 // violating only the left & right coords - however the user can
2029 // make its own renderer and thus we cannot be sure of that.
2030 dc.SetClippingRegion( item_rect );
2031 cell->Render( item_rect, &dc, state );
2032 dc.DestroyClippingRegion();
2033 }
2034
2035 cell_rect.x += cell_rect.width;
2036 }
2037 }
2038
2039 int wxDataViewMainWindow::GetCountPerPage() const
2040 {
2041 wxSize size = GetClientSize();
2042 return size.y / m_lineHeight;
2043 }
2044
2045 int wxDataViewMainWindow::GetEndOfLastCol() const
2046 {
2047 int width = 0;
2048 unsigned int i;
2049 for (i = 0; i < GetOwner()->GetColumnCount(); i++)
2050 {
2051 const wxDataViewColumn *c =
2052 wx_const_cast(wxDataViewCtrl*, GetOwner())->GetColumn( i );
2053
2054 if (!c->IsHidden())
2055 width += c->GetWidth();
2056 }
2057 return width;
2058 }
2059
2060 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2061 {
2062 int x = 0;
2063 int y = 0;
2064 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
2065
2066 return y / m_lineHeight;
2067 }
2068
2069 unsigned int wxDataViewMainWindow::GetLastVisibleRow() const
2070 {
2071 wxSize client_size = GetClientSize();
2072 m_owner->CalcUnscrolledPosition( client_size.x, client_size.y,
2073 &client_size.x, &client_size.y );
2074
2075 return wxMin( GetRowCount()-1, ((unsigned)client_size.y/m_lineHeight)+1 );
2076 }
2077
2078 unsigned int wxDataViewMainWindow::GetRowCount() const
2079 {
2080 return wx_const_cast(wxDataViewCtrl*, GetOwner())->GetModel()->GetRowCount();
2081 }
2082
2083 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row )
2084 {
2085 m_currentRow = row;
2086
2087 // send event
2088 }
2089
2090 void wxDataViewMainWindow::SelectAllRows( bool on )
2091 {
2092 if (IsEmpty())
2093 return;
2094
2095 if (on)
2096 {
2097 m_selection.Clear();
2098 for (unsigned int i = 0; i < GetRowCount(); i++)
2099 m_selection.Add( i );
2100 Refresh();
2101 }
2102 else
2103 {
2104 unsigned int first_visible = GetFirstVisibleRow();
2105 unsigned int last_visible = GetLastVisibleRow();
2106 unsigned int i;
2107 for (i = 0; i < m_selection.GetCount(); i++)
2108 {
2109 unsigned int row = m_selection[i];
2110 if ((row >= first_visible) && (row <= last_visible))
2111 RefreshRow( row );
2112 }
2113 m_selection.Clear();
2114 }
2115 }
2116
2117 void wxDataViewMainWindow::SelectRow( unsigned int row, bool on )
2118 {
2119 if (m_selection.Index( row ) == wxNOT_FOUND)
2120 {
2121 if (on)
2122 {
2123 m_selection.Add( row );
2124 RefreshRow( row );
2125 }
2126 }
2127 else
2128 {
2129 if (!on)
2130 {
2131 m_selection.Remove( row );
2132 RefreshRow( row );
2133 }
2134 }
2135 }
2136
2137 void wxDataViewMainWindow::SelectRows( unsigned int from, unsigned int to, bool on )
2138 {
2139 if (from > to)
2140 {
2141 unsigned int tmp = from;
2142 from = to;
2143 to = tmp;
2144 }
2145
2146 unsigned int i;
2147 for (i = from; i <= to; i++)
2148 {
2149 if (m_selection.Index( i ) == wxNOT_FOUND)
2150 {
2151 if (on)
2152 m_selection.Add( i );
2153 }
2154 else
2155 {
2156 if (!on)
2157 m_selection.Remove( i );
2158 }
2159 }
2160 RefreshRows( from, to );
2161 }
2162
2163 void wxDataViewMainWindow::Select( const wxArrayInt& aSelections )
2164 {
2165 for (size_t i=0; i < aSelections.GetCount(); i++)
2166 {
2167 int n = aSelections[i];
2168
2169 m_selection.Add( n );
2170 RefreshRow( n );
2171 }
2172 }
2173
2174 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row )
2175 {
2176 if (m_selection.Index( row ) == wxNOT_FOUND)
2177 m_selection.Add( row );
2178 else
2179 m_selection.Remove( row );
2180 RefreshRow( row );
2181 }
2182
2183 bool wxDataViewMainWindow::IsRowSelected( unsigned int row )
2184 {
2185 return (m_selection.Index( row ) != wxNOT_FOUND);
2186 }
2187
2188 void wxDataViewMainWindow::RefreshRow( unsigned int row )
2189 {
2190 wxRect rect( 0, row*m_lineHeight, GetEndOfLastCol(), m_lineHeight );
2191 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2192
2193 wxSize client_size = GetClientSize();
2194 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2195 wxRect intersect_rect = client_rect.Intersect( rect );
2196 if (intersect_rect.width > 0)
2197 Refresh( true, &intersect_rect );
2198 }
2199
2200 void wxDataViewMainWindow::RefreshRows( unsigned int from, unsigned int to )
2201 {
2202 if (from > to)
2203 {
2204 unsigned int tmp = to;
2205 to = from;
2206 from = tmp;
2207 }
2208
2209 wxRect rect( 0, from*m_lineHeight, GetEndOfLastCol(), (to-from+1) * m_lineHeight );
2210 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2211
2212 wxSize client_size = GetClientSize();
2213 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2214 wxRect intersect_rect = client_rect.Intersect( rect );
2215 if (intersect_rect.width > 0)
2216 Refresh( true, &intersect_rect );
2217 }
2218
2219 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow )
2220 {
2221 unsigned int count = GetRowCount();
2222 if (firstRow > count)
2223 return;
2224
2225 wxRect rect( 0, firstRow*m_lineHeight, GetEndOfLastCol(), count * m_lineHeight );
2226 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2227
2228 wxSize client_size = GetClientSize();
2229 wxRect client_rect( 0, 0, client_size.x, client_size.y );
2230 wxRect intersect_rect = client_rect.Intersect( rect );
2231 if (intersect_rect.width > 0)
2232 Refresh( true, &intersect_rect );
2233 }
2234
2235 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event)
2236 {
2237 wxCHECK_RET( newCurrent < GetRowCount(),
2238 _T("invalid item index in OnArrowChar()") );
2239
2240 // if there is no selection, we cannot move it anywhere
2241 if (!HasCurrentRow())
2242 return;
2243
2244 unsigned int oldCurrent = m_currentRow;
2245
2246 // in single selection we just ignore Shift as we can't select several
2247 // items anyhow
2248 if ( event.ShiftDown() && !IsSingleSel() )
2249 {
2250 RefreshRow( oldCurrent );
2251
2252 ChangeCurrentRow( newCurrent );
2253
2254 // select all the items between the old and the new one
2255 if ( oldCurrent > newCurrent )
2256 {
2257 newCurrent = oldCurrent;
2258 oldCurrent = m_currentRow;
2259 }
2260
2261 SelectRows( oldCurrent, newCurrent, true );
2262 }
2263 else // !shift
2264 {
2265 RefreshRow( oldCurrent );
2266
2267 // all previously selected items are unselected unless ctrl is held
2268 if ( !event.ControlDown() )
2269 SelectAllRows(false);
2270
2271 ChangeCurrentRow( newCurrent );
2272
2273 if ( !event.ControlDown() )
2274 SelectRow( m_currentRow, true );
2275 else
2276 RefreshRow( m_currentRow );
2277 }
2278
2279 //EnsureVisible( m_currentRow );
2280 }
2281
2282 wxRect wxDataViewMainWindow::GetLineRect( unsigned int row ) const
2283 {
2284 wxRect rect;
2285 rect.x = 0;
2286 rect.y = m_lineHeight * row;
2287 rect.width = GetEndOfLastCol();
2288 rect.height = m_lineHeight;
2289
2290 return rect;
2291 }
2292
2293 void wxDataViewMainWindow::OnChar( wxKeyEvent &event )
2294 {
2295 if (event.GetKeyCode() == WXK_TAB)
2296 {
2297 wxNavigationKeyEvent nevent;
2298 nevent.SetWindowChange( event.ControlDown() );
2299 nevent.SetDirection( !event.ShiftDown() );
2300 nevent.SetEventObject( GetParent()->GetParent() );
2301 nevent.SetCurrentFocus( m_parent );
2302 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent ))
2303 return;
2304 }
2305
2306 // no item -> nothing to do
2307 if (!HasCurrentRow())
2308 {
2309 event.Skip();
2310 return;
2311 }
2312
2313 // don't use m_linesPerPage directly as it might not be computed yet
2314 const int pageSize = GetCountPerPage();
2315 wxCHECK_RET( pageSize, _T("should have non zero page size") );
2316
2317 switch ( event.GetKeyCode() )
2318 {
2319 case WXK_UP:
2320 if ( m_currentRow > 0 )
2321 OnArrowChar( m_currentRow - 1, event );
2322 break;
2323
2324 case WXK_DOWN:
2325 if ( m_currentRow < GetRowCount() - 1 )
2326 OnArrowChar( m_currentRow + 1, event );
2327 break;
2328
2329 case WXK_END:
2330 if (!IsEmpty())
2331 OnArrowChar( GetRowCount() - 1, event );
2332 break;
2333
2334 case WXK_HOME:
2335 if (!IsEmpty())
2336 OnArrowChar( 0, event );
2337 break;
2338
2339 case WXK_PAGEUP:
2340 {
2341 int steps = pageSize - 1;
2342 int index = m_currentRow - steps;
2343 if (index < 0)
2344 index = 0;
2345
2346 OnArrowChar( index, event );
2347 }
2348 break;
2349
2350 case WXK_PAGEDOWN:
2351 {
2352 int steps = pageSize - 1;
2353 unsigned int index = m_currentRow + steps;
2354 unsigned int count = GetRowCount();
2355 if ( index >= count )
2356 index = count - 1;
2357
2358 OnArrowChar( index, event );
2359 }
2360 break;
2361
2362 default:
2363 event.Skip();
2364 }
2365 }
2366
2367 void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
2368 {
2369 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
2370 {
2371 // let the base handle mouse wheel events.
2372 event.Skip();
2373 return;
2374 }
2375
2376 int x = event.GetX();
2377 int y = event.GetY();
2378 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
2379
2380 wxDataViewColumn *col = NULL;
2381
2382 int xpos = 0;
2383 unsigned int cols = GetOwner()->GetColumnCount();
2384 unsigned int i;
2385 for (i = 0; i < cols; i++)
2386 {
2387 wxDataViewColumn *c = GetOwner()->GetColumn( i );
2388 if (c->IsHidden())
2389 continue; // skip it!
2390
2391 if (x < xpos + c->GetWidth())
2392 {
2393 col = c;
2394 break;
2395 }
2396 xpos += c->GetWidth();
2397 }
2398 if (!col)
2399 return;
2400 wxDataViewRenderer *cell = col->GetRenderer();
2401
2402 unsigned int current = y / m_lineHeight;
2403
2404 if ((current > GetRowCount()) || (x > GetEndOfLastCol()))
2405 {
2406 // Unselect all if below the last row ?
2407 return;
2408 }
2409
2410 wxDataViewListModel *model = GetOwner()->GetModel();
2411
2412 if (event.Dragging())
2413 {
2414 if (m_dragCount == 0)
2415 {
2416 // we have to report the raw, physical coords as we want to be
2417 // able to call HitTest(event.m_pointDrag) from the user code to
2418 // get the item being dragged
2419 m_dragStart = event.GetPosition();
2420 }
2421
2422 m_dragCount++;
2423
2424 if (m_dragCount != 3)
2425 return;
2426
2427 if (event.LeftIsDown())
2428 {
2429 // Notify cell about drag
2430 }
2431 return;
2432 }
2433 else
2434 {
2435 m_dragCount = 0;
2436 }
2437
2438 bool forceClick = false;
2439
2440 if (event.ButtonDClick())
2441 {
2442 m_renameTimer->Stop();
2443 m_lastOnSame = false;
2444 }
2445
2446 if (event.LeftDClick())
2447 {
2448 if ( current == m_lineLastClicked )
2449 {
2450 if (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE)
2451 {
2452 wxVariant value;
2453 model->GetValue( value, col->GetModelColumn(), current );
2454 cell->SetValue( value );
2455 wxRect cell_rect( xpos, current * m_lineHeight,
2456 col->GetWidth(), m_lineHeight );
2457 cell->Activate( cell_rect, model, col->GetModelColumn(), current );
2458 }
2459 return;
2460 }
2461 else
2462 {
2463 // The first click was on another item, so don't interpret this as
2464 // a double click, but as a simple click instead
2465 forceClick = true;
2466 }
2467 }
2468
2469 if (event.LeftUp())
2470 {
2471 if (m_lineSelectSingleOnUp != (unsigned int)-1)
2472 {
2473 // select single line
2474 SelectAllRows( false );
2475 SelectRow( m_lineSelectSingleOnUp, true );
2476 }
2477
2478 if (m_lastOnSame)
2479 {
2480 if ((col == m_currentCol) && (current == m_currentRow) &&
2481 (cell->GetMode() == wxDATAVIEW_CELL_EDITABLE) )
2482 {
2483 m_renameTimer->Start( 100, true );
2484 }
2485 }
2486
2487 m_lastOnSame = false;
2488 m_lineSelectSingleOnUp = (unsigned int)-1;
2489 }
2490 else
2491 {
2492 // This is necessary, because after a DnD operation in
2493 // from and to ourself, the up event is swallowed by the
2494 // DnD code. So on next non-up event (which means here and
2495 // now) m_lineSelectSingleOnUp should be reset.
2496 m_lineSelectSingleOnUp = (unsigned int)-1;
2497 }
2498
2499 if (event.RightDown())
2500 {
2501 m_lineBeforeLastClicked = m_lineLastClicked;
2502 m_lineLastClicked = current;
2503
2504 // If the item is already selected, do not update the selection.
2505 // Multi-selections should not be cleared if a selected item is clicked.
2506 if (!IsRowSelected(current))
2507 {
2508 SelectAllRows(false);
2509 ChangeCurrentRow(current);
2510 SelectRow(m_currentRow,true);
2511 }
2512
2513 // notify cell about right click
2514 // cell->...
2515
2516 // Allow generation of context menu event
2517 event.Skip();
2518 }
2519 else if (event.MiddleDown())
2520 {
2521 // notify cell about middle click
2522 // cell->...
2523 }
2524 if (event.LeftDown() || forceClick)
2525 {
2526 #ifdef __WXMSW__
2527 SetFocus();
2528 #endif
2529
2530 m_lineBeforeLastClicked = m_lineLastClicked;
2531 m_lineLastClicked = current;
2532
2533 unsigned int oldCurrentRow = m_currentRow;
2534 bool oldWasSelected = IsRowSelected(m_currentRow);
2535
2536 bool cmdModifierDown = event.CmdDown();
2537 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
2538 {
2539 if ( IsSingleSel() || !IsRowSelected(current) )
2540 {
2541 SelectAllRows( false );
2542
2543 ChangeCurrentRow(current);
2544
2545 SelectRow(m_currentRow,true);
2546 }
2547 else // multi sel & current is highlighted & no mod keys
2548 {
2549 m_lineSelectSingleOnUp = current;
2550 ChangeCurrentRow(current); // change focus
2551 }
2552 }
2553 else // multi sel & either ctrl or shift is down
2554 {
2555 if (cmdModifierDown)
2556 {
2557 ChangeCurrentRow(current);
2558
2559 ReverseRowSelection(m_currentRow);
2560 }
2561 else if (event.ShiftDown())
2562 {
2563 ChangeCurrentRow(current);
2564
2565 unsigned int lineFrom = oldCurrentRow,
2566 lineTo = current;
2567
2568 if ( lineTo < lineFrom )
2569 {
2570 lineTo = lineFrom;
2571 lineFrom = m_currentRow;
2572 }
2573
2574 SelectRows(lineFrom, lineTo, true);
2575 }
2576 else // !ctrl, !shift
2577 {
2578 // test in the enclosing if should make it impossible
2579 wxFAIL_MSG( _T("how did we get here?") );
2580 }
2581 }
2582
2583 if (m_currentRow != oldCurrentRow)
2584 RefreshRow( oldCurrentRow );
2585
2586 wxDataViewColumn *oldCurrentCol = m_currentCol;
2587
2588 // Update selection here...
2589 m_currentCol = col;
2590
2591 m_lastOnSame = !forceClick && ((col == oldCurrentCol) &&
2592 (current == oldCurrentRow)) && oldWasSelected;
2593 }
2594 }
2595
2596 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event )
2597 {
2598 m_hasFocus = true;
2599
2600 if (HasCurrentRow())
2601 Refresh();
2602
2603 event.Skip();
2604 }
2605
2606 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event )
2607 {
2608 m_hasFocus = false;
2609
2610 if (HasCurrentRow())
2611 Refresh();
2612
2613 event.Skip();
2614 }
2615
2616 //-----------------------------------------------------------------------------
2617 // wxDataViewCtrl
2618 //-----------------------------------------------------------------------------
2619
2620 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
2621
2622 BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase)
2623 EVT_SIZE(wxDataViewCtrl::OnSize)
2624 END_EVENT_TABLE()
2625
2626 wxDataViewCtrl::~wxDataViewCtrl()
2627 {
2628 if (m_notifier)
2629 GetModel()->RemoveNotifier( m_notifier );
2630 }
2631
2632 void wxDataViewCtrl::Init()
2633 {
2634 m_notifier = NULL;
2635 }
2636
2637 bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
2638 const wxPoint& pos, const wxSize& size,
2639 long style, const wxValidator& validator )
2640 {
2641 if (!wxControl::Create( parent, id, pos, size,
2642 style | wxScrolledWindowStyle|wxSUNKEN_BORDER, validator))
2643 return false;
2644
2645 Init();
2646
2647 #ifdef __WXMAC__
2648 MacSetClipChildren( true ) ;
2649 #endif
2650
2651 m_clientArea = new wxDataViewMainWindow( this, wxID_ANY );
2652
2653 if (HasFlag(wxDV_NO_HEADER))
2654 m_headerArea = NULL;
2655 else
2656 m_headerArea = new wxDataViewHeaderWindow( this, wxID_ANY );
2657
2658 SetTargetWindow( m_clientArea );
2659
2660 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
2661 if (m_headerArea)
2662 sizer->Add( m_headerArea, 0, wxGROW );
2663 sizer->Add( m_clientArea, 1, wxGROW );
2664 SetSizer( sizer );
2665
2666 return true;
2667 }
2668
2669 #ifdef __WXMSW__
2670 WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg,
2671 WXWPARAM wParam,
2672 WXLPARAM lParam)
2673 {
2674 WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
2675
2676 #ifndef __WXWINCE__
2677 // we need to process arrows ourselves for scrolling
2678 if ( nMsg == WM_GETDLGCODE )
2679 {
2680 rc |= DLGC_WANTARROWS;
2681 }
2682 #endif
2683
2684 return rc;
2685 }
2686 #endif
2687
2688 void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
2689 {
2690 // We need to override OnSize so that our scrolled
2691 // window a) does call Layout() to use sizers for
2692 // positioning the controls but b) does not query
2693 // the sizer for their size and use that for setting
2694 // the scrollable area as set that ourselves by
2695 // calling SetScrollbar() further down.
2696
2697 Layout();
2698
2699 AdjustScrollbars();
2700 }
2701
2702 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel *model )
2703 {
2704 if (!wxDataViewCtrlBase::AssociateModel( model ))
2705 return false;
2706
2707 m_notifier = new wxGenericDataViewListModelNotifier( m_clientArea );
2708
2709 model->AddNotifier( m_notifier );
2710
2711 m_clientArea->UpdateDisplay();
2712
2713 return true;
2714 }
2715
2716 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
2717 {
2718 if (!wxDataViewCtrlBase::AppendColumn(col))
2719 return false;
2720
2721 OnColumnChange();
2722 return true;
2723 }
2724
2725 void wxDataViewCtrl::OnColumnChange()
2726 {
2727 if (m_headerArea)
2728 m_headerArea->UpdateDisplay();
2729
2730 m_clientArea->UpdateDisplay();
2731 }
2732
2733 void wxDataViewCtrl::SetSelection( int row )
2734 {
2735 m_clientArea->SelectRow(row, true);
2736 }
2737
2738 void wxDataViewCtrl::SetSelectionRange( unsigned int from, unsigned int to )
2739 {
2740 m_clientArea->SelectRows(from, to, true);
2741 }
2742
2743 void wxDataViewCtrl::SetSelections( const wxArrayInt& aSelections )
2744 {
2745 m_clientArea->Select(aSelections);
2746 }
2747
2748 void wxDataViewCtrl::Unselect( unsigned int WXUNUSED(row) )
2749 {
2750 // FIXME - TODO
2751 }
2752
2753 bool wxDataViewCtrl::IsSelected( unsigned int WXUNUSED(row) ) const
2754 {
2755 // FIXME - TODO
2756
2757 return false;
2758 }
2759
2760 int wxDataViewCtrl::GetSelection() const
2761 {
2762 // FIXME - TODO
2763
2764 return -1;
2765 }
2766
2767 int wxDataViewCtrl::GetSelections(wxArrayInt& WXUNUSED(aSelections) ) const
2768 {
2769 // FIXME - TODO
2770
2771 return 0;
2772 }
2773
2774 #endif
2775 // !wxUSE_GENERICDATAVIEWCTRL
2776
2777 #endif
2778 // wxUSE_DATAVIEWCTRL