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