Source and warning cleaning after enabling wxUSE_DATAVIEWCTRL in wxMSW builds.
[wxWidgets.git] / src / generic / datavgen.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_DATAVIEWCTRL
14
15 #include "wx/dataview.h"
16
17 #ifdef wxUSE_GENERICDATAVIEWCTRL
18
19 #ifndef WX_PRECOMP
20 #include "wx/sizer.h"
21 #include "wx/log.h"
22 #endif
23
24 #include "wx/stockitem.h"
25 #include "wx/dcclient.h"
26 #include "wx/calctrl.h"
27 #include "wx/popupwin.h"
28 #include "wx/renderer.h"
29
30 #ifdef __WXMSW__
31 #include "wx/msw/wrapwin.h"
32 #endif
33
34 //-----------------------------------------------------------------------------
35 // classes
36 //-----------------------------------------------------------------------------
37
38 class wxDataViewCtrl;
39
40 //-----------------------------------------------------------------------------
41 // wxDataViewHeaderWindow
42 //-----------------------------------------------------------------------------
43
44 class wxDataViewHeaderWindow: public wxWindow
45 {
46 public:
47 wxDataViewHeaderWindow( wxDataViewCtrl *parent,
48 wxWindowID id,
49 const wxPoint &pos = wxDefaultPosition,
50 const wxSize &size = wxDefaultSize,
51 const wxString &name = wxT("wxdataviewctrlheaderwindow") );
52 ~wxDataViewHeaderWindow();
53
54 void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; }
55 wxDataViewCtrl *GetOwner() { return m_owner; }
56
57 void OnPaint( wxPaintEvent &event );
58 void OnMouse( wxMouseEvent &event );
59 void OnSetFocus( wxFocusEvent &event );
60
61 private:
62 wxDataViewCtrl *m_owner;
63 wxCursor *m_resizeCursor;
64
65 private:
66 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindow)
67 DECLARE_EVENT_TABLE()
68 };
69
70 //-----------------------------------------------------------------------------
71 // wxDataViewMainWindow
72 //-----------------------------------------------------------------------------
73
74 class wxDataViewMainWindow: public wxWindow
75 {
76 public:
77 wxDataViewMainWindow( wxDataViewCtrl *parent,
78 wxWindowID id,
79 const wxPoint &pos = wxDefaultPosition,
80 const wxSize &size = wxDefaultSize,
81 const wxString &name = wxT("wxdataviewctrlmainwindow") );
82 ~wxDataViewMainWindow();
83
84 // notifications from wxDataViewListModel
85 bool RowAppended();
86 bool RowPrepended();
87 bool RowInserted( size_t before );
88 bool RowDeleted( size_t row );
89 bool RowChanged( size_t row );
90 bool ValueChanged( size_t col, size_t row );
91 bool RowsReordered( size_t *new_order );
92 bool Cleared();
93
94 void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; }
95 wxDataViewCtrl *GetOwner() { return m_owner; }
96
97 void OnPaint( wxPaintEvent &event );
98 void OnMouse( wxMouseEvent &event );
99 void OnSetFocus( wxFocusEvent &event );
100
101 void UpdateDisplay();
102 void RecalculateDisplay();
103 void OnInternalIdle();
104
105 void ScrollWindow( int dx, int dy, const wxRect *rect );
106 private:
107 wxDataViewCtrl *m_owner;
108 int m_lineHeight;
109 bool m_dirty;
110
111 private:
112 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow)
113 DECLARE_EVENT_TABLE()
114 };
115
116 // ---------------------------------------------------------
117 // wxGenericDataViewListModelNotifier
118 // ---------------------------------------------------------
119
120 class wxGenericDataViewListModelNotifier: public wxDataViewListModelNotifier
121 {
122 public:
123 wxGenericDataViewListModelNotifier( wxDataViewMainWindow *mainWindow )
124 { m_mainWindow = mainWindow; }
125
126 virtual bool RowAppended()
127 { return m_mainWindow->RowAppended(); }
128 virtual bool RowPrepended()
129 { return m_mainWindow->RowPrepended(); }
130 virtual bool RowInserted( size_t before )
131 { return m_mainWindow->RowInserted( before ); }
132 virtual bool RowDeleted( size_t row )
133 { return m_mainWindow->RowDeleted( row ); }
134 virtual bool RowChanged( size_t row )
135 { return m_mainWindow->RowChanged( row ); }
136 virtual bool ValueChanged( size_t col, size_t row )
137 { return m_mainWindow->ValueChanged( col, row ); }
138 virtual bool RowsReordered( size_t *new_order )
139 { return m_mainWindow->RowsReordered( new_order ); }
140 virtual bool Cleared()
141 { return m_mainWindow->Cleared(); }
142
143 wxDataViewMainWindow *m_mainWindow;
144 };
145
146 // ---------------------------------------------------------
147 // wxDataViewCell
148 // ---------------------------------------------------------
149
150 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCell, wxDataViewCellBase)
151
152 wxDataViewCell::wxDataViewCell( const wxString &varianttype, wxDataViewCellMode mode ) :
153 wxDataViewCellBase( varianttype, mode )
154 {
155 m_dc = NULL;
156 }
157
158 wxDataViewCell::~wxDataViewCell()
159 {
160 if (m_dc)
161 delete m_dc;
162 }
163
164 wxDC *wxDataViewCell::GetDC()
165 {
166 if (m_dc == NULL)
167 {
168 if (GetOwner() == NULL)
169 return NULL;
170 if (GetOwner()->GetOwner() == NULL)
171 return NULL;
172 m_dc = new wxClientDC( GetOwner()->GetOwner() );
173 }
174
175 return m_dc;
176 }
177
178 // ---------------------------------------------------------
179 // wxDataViewCustomCell
180 // ---------------------------------------------------------
181
182 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomCell, wxDataViewCell)
183
184 wxDataViewCustomCell::wxDataViewCustomCell( const wxString &varianttype,
185 wxDataViewCellMode mode ) :
186 wxDataViewCell( varianttype, mode )
187 {
188 }
189
190 // ---------------------------------------------------------
191 // wxDataViewTextCell
192 // ---------------------------------------------------------
193
194 IMPLEMENT_ABSTRACT_CLASS(wxDataViewTextCell, wxDataViewCustomCell)
195
196 wxDataViewTextCell::wxDataViewTextCell( const wxString &varianttype, wxDataViewCellMode mode ) :
197 wxDataViewCustomCell( varianttype, mode )
198 {
199 }
200
201 bool wxDataViewTextCell::SetValue( const wxVariant &value )
202 {
203 m_text = value.GetString();
204
205 return true;
206 }
207
208 bool wxDataViewTextCell::GetValue( wxVariant& WXUNUSED(value) )
209 {
210 return false;
211 }
212
213 bool wxDataViewTextCell::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
214 {
215 dc->DrawText( m_text, cell.x, cell.y );
216
217 return true;
218 }
219
220 wxSize wxDataViewTextCell::GetSize()
221 {
222 return wxSize(80,20);
223 }
224
225 // ---------------------------------------------------------
226 // wxDataViewToggleCell
227 // ---------------------------------------------------------
228
229 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleCell, wxDataViewCustomCell)
230
231 wxDataViewToggleCell::wxDataViewToggleCell( const wxString &varianttype,
232 wxDataViewCellMode mode ) :
233 wxDataViewCustomCell( varianttype, mode )
234 {
235 m_toggle = false;
236 }
237
238 bool wxDataViewToggleCell::SetValue( const wxVariant &value )
239 {
240 m_toggle = value.GetBool();
241
242 return true;;
243 }
244
245 bool wxDataViewToggleCell::GetValue( wxVariant &WXUNUSED(value) )
246 {
247 return false;
248 }
249
250 bool wxDataViewToggleCell::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
251 {
252 // User wxRenderer here
253
254 if (GetMode() == wxDATAVIEW_CELL_ACTIVATABLE)
255 dc->SetPen( *wxBLACK_PEN );
256 else
257 dc->SetPen( *wxGREY_PEN );
258 dc->SetBrush( *wxTRANSPARENT_BRUSH );
259 wxRect rect;
260 rect.x = cell.x + cell.width/2 - 10;
261 rect.width = 20;
262 rect.y = cell.y + cell.height/2 - 10;
263 rect.height = 20;
264 dc->DrawRectangle( rect );
265 if (m_toggle)
266 {
267 rect.x += 2;
268 rect.y += 2;
269 rect.width -= 4;
270 rect.height -= 4;
271 dc->DrawLine( rect.x, rect.y, rect.x+rect.width, rect.y+rect.height );
272 dc->DrawLine( rect.x+rect.width, rect.y, rect.x, rect.y+rect.height );
273 }
274
275 return true;
276 }
277
278 bool wxDataViewToggleCell::Activate( wxRect WXUNUSED(cell), wxDataViewListModel *model, size_t col, size_t row )
279 {
280 bool value = !m_toggle;
281 wxVariant variant = value;
282 model->SetValue( variant, col, row );
283 model->ValueChanged( col, row );
284 return true;
285 }
286
287 wxSize wxDataViewToggleCell::GetSize()
288 {
289 return wxSize(20,20);
290 }
291
292 // ---------------------------------------------------------
293 // wxDataViewProgressCell
294 // ---------------------------------------------------------
295
296 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressCell, wxDataViewCustomCell)
297
298 wxDataViewProgressCell::wxDataViewProgressCell( const wxString &label,
299 const wxString &varianttype, wxDataViewCellMode mode ) :
300 wxDataViewCustomCell( varianttype, mode )
301 {
302 m_label = label;
303 m_value = 0;
304 }
305
306 wxDataViewProgressCell::~wxDataViewProgressCell()
307 {
308 }
309
310 bool wxDataViewProgressCell::SetValue( const wxVariant &value )
311 {
312 m_value = (long) value;
313
314 if (m_value < 0) m_value = 0;
315 if (m_value > 100) m_value = 100;
316
317 return true;
318 }
319
320 bool wxDataViewProgressCell::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
321 {
322 double pct = (double)m_value / 100.0;
323 wxRect bar = cell;
324 bar.width = (int)(cell.width * pct);
325 dc->SetPen( *wxTRANSPARENT_PEN );
326 dc->SetBrush( *wxBLUE_BRUSH );
327 dc->DrawRectangle( bar );
328
329 dc->SetBrush( *wxTRANSPARENT_BRUSH );
330 dc->SetPen( *wxBLACK_PEN );
331 dc->DrawRectangle( cell );
332
333 return true;
334 }
335
336 wxSize wxDataViewProgressCell::GetSize()
337 {
338 return wxSize(40,12);
339 }
340
341 // ---------------------------------------------------------
342 // wxDataViewDateCell
343 // ---------------------------------------------------------
344
345 class wxDataViewDateCellPopupTransient: public wxPopupTransientWindow
346 {
347 public:
348 wxDataViewDateCellPopupTransient( wxWindow* parent, wxDateTime *value,
349 wxDataViewListModel *model, size_t col, size_t row ) :
350 wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
351 {
352 m_model = model;
353 m_col = col;
354 m_row = row;
355 m_cal = new wxCalendarCtrl( this, wxID_ANY, *value );
356 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
357 sizer->Add( m_cal, 1, wxGROW );
358 SetSizer( sizer );
359 sizer->Fit( this );
360 }
361
362 virtual void OnDismiss()
363 {
364 }
365
366 void OnCalendar( wxCalendarEvent &event );
367
368 wxCalendarCtrl *m_cal;
369 wxDataViewListModel *m_model;
370 size_t m_col;
371 size_t m_row;
372
373 private:
374 DECLARE_EVENT_TABLE()
375 };
376
377 BEGIN_EVENT_TABLE(wxDataViewDateCellPopupTransient,wxPopupTransientWindow)
378 EVT_CALENDAR( wxID_ANY, wxDataViewDateCellPopupTransient::OnCalendar )
379 END_EVENT_TABLE()
380
381 void wxDataViewDateCellPopupTransient::OnCalendar( wxCalendarEvent &event )
382 {
383 wxDateTime date = event.GetDate();
384 wxVariant value = date;
385 m_model->SetValue( value, m_col, m_row );
386 m_model->ValueChanged( m_col, m_row );
387 DismissAndNotify();
388 }
389
390 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateCell, wxDataViewCustomCell)
391
392 wxDataViewDateCell::wxDataViewDateCell( const wxString &varianttype,
393 wxDataViewCellMode mode ) :
394 wxDataViewCustomCell( varianttype, mode )
395 {
396 }
397
398 bool wxDataViewDateCell::SetValue( const wxVariant &value )
399 {
400 m_date = value.GetDateTime();
401
402 return true;
403 }
404
405 bool wxDataViewDateCell::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
406 {
407 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
408 wxString tmp = m_date.FormatDate();
409 dc->DrawText( tmp, cell.x, cell.y );
410
411 return true;
412 }
413
414 wxSize wxDataViewDateCell::GetSize()
415 {
416 wxDataViewCtrl* view = GetOwner()->GetOwner();
417 wxString tmp = m_date.FormatDate();
418 wxCoord x,y,d;
419 view->GetTextExtent( tmp, &x, &y, &d );
420 return wxSize(x,y+d);
421 }
422
423 bool wxDataViewDateCell::Activate( wxRect WXUNUSED(cell), wxDataViewListModel *model, size_t col, size_t row )
424 {
425 wxVariant variant;
426 model->GetValue( variant, col, row );
427 wxDateTime value = variant.GetDateTime();
428
429 wxDataViewDateCellPopupTransient *popup = new wxDataViewDateCellPopupTransient(
430 GetOwner()->GetOwner()->GetParent(), &value, model, col, row );
431 wxPoint pos = wxGetMousePosition();
432 popup->Move( pos );
433 popup->Layout();
434 popup->Popup( popup->m_cal );
435
436 return true;
437 }
438
439 // ---------------------------------------------------------
440 // wxDataViewColumn
441 // ---------------------------------------------------------
442
443 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn, wxDataViewColumnBase)
444
445 wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewCell *cell,
446 size_t model_column, int flags ) :
447 wxDataViewColumnBase( title, cell, model_column, flags )
448 {
449 m_width = 80;
450 }
451
452 wxDataViewColumn::~wxDataViewColumn()
453 {
454 }
455
456 void wxDataViewColumn::SetTitle( const wxString &title )
457 {
458 wxDataViewColumnBase::SetTitle( title );
459
460 }
461
462 //-----------------------------------------------------------------------------
463 // wxDataViewHeaderWindow
464 //-----------------------------------------------------------------------------
465
466 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindow, wxWindow)
467
468 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow,wxWindow)
469 EVT_PAINT (wxDataViewHeaderWindow::OnPaint)
470 EVT_MOUSE_EVENTS (wxDataViewHeaderWindow::OnMouse)
471 EVT_SET_FOCUS (wxDataViewHeaderWindow::OnSetFocus)
472 END_EVENT_TABLE()
473
474 wxDataViewHeaderWindow::wxDataViewHeaderWindow( wxDataViewCtrl *parent, wxWindowID id,
475 const wxPoint &pos, const wxSize &size, const wxString &name ) :
476 wxWindow( parent, id, pos, size, 0, name )
477 {
478 SetOwner( parent );
479
480 m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE );
481
482 wxVisualAttributes attr = wxPanel::GetClassDefaultAttributes();
483 SetOwnForegroundColour( attr.colFg );
484 SetOwnBackgroundColour( attr.colBg );
485 if (!m_hasFont)
486 SetOwnFont( attr.font );
487 }
488
489 wxDataViewHeaderWindow::~wxDataViewHeaderWindow()
490 {
491 delete m_resizeCursor;
492 }
493
494 void wxDataViewHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
495 {
496 int w, h;
497 GetClientSize( &w, &h );
498
499 wxPaintDC dc( this );
500
501 int xpix;
502 m_owner->GetScrollPixelsPerUnit( &xpix, NULL );
503
504 int x;
505 m_owner->GetViewStart( &x, NULL );
506
507 // account for the horz scrollbar offset
508 dc.SetDeviceOrigin( -x * xpix, 0 );
509
510 dc.SetFont( GetFont() );
511
512 size_t cols = GetOwner()->GetNumberOfColumns();
513 size_t i;
514 int xpos = 0;
515 for (i = 0; i < cols; i++)
516 {
517 wxDataViewColumn *col = GetOwner()->GetColumn( i );
518 int width = col->GetWidth();
519
520 // the width of the rect to draw: make it smaller to fit entirely
521 // inside the column rect
522 #ifdef __WXMAC__
523 int cw = width;
524 int ch = h;
525 #else
526 int cw = width - 2;
527 int ch = h - 2;
528 #endif
529
530 wxRendererNative::Get().DrawHeaderButton
531 (
532 this,
533 dc,
534 wxRect(xpos, 0, cw, ch),
535 m_parent->IsEnabled() ? 0
536 : (int)wxCONTROL_DISABLED
537 );
538
539 dc.DrawText( col->GetTitle(), xpos+3, 3 );
540
541 xpos += width;
542 }
543 }
544
545 void wxDataViewHeaderWindow::OnMouse( wxMouseEvent &WXUNUSED(event) )
546 {
547 }
548
549 void wxDataViewHeaderWindow::OnSetFocus( wxFocusEvent &event )
550 {
551 event.Skip();
552 }
553
554 //-----------------------------------------------------------------------------
555 // wxDataViewMainWindow
556 //-----------------------------------------------------------------------------
557
558 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow, wxWindow)
559
560 BEGIN_EVENT_TABLE(wxDataViewMainWindow,wxWindow)
561 EVT_PAINT (wxDataViewMainWindow::OnPaint)
562 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse)
563 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus)
564 END_EVENT_TABLE()
565
566 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID id,
567 const wxPoint &pos, const wxSize &size, const wxString &name ) :
568 wxWindow( parent, id, pos, size, 0, name )
569 {
570 SetOwner( parent );
571
572 // We need to calculate this smartly..
573 m_lineHeight = 20;
574
575 UpdateDisplay();
576 }
577
578 wxDataViewMainWindow::~wxDataViewMainWindow()
579 {
580 }
581
582 bool wxDataViewMainWindow::RowAppended()
583 {
584 return false;
585 }
586
587 bool wxDataViewMainWindow::RowPrepended()
588 {
589 return false;
590 }
591
592 bool wxDataViewMainWindow::RowInserted( size_t WXUNUSED(before) )
593 {
594 return false;
595 }
596
597 bool wxDataViewMainWindow::RowDeleted( size_t WXUNUSED(row) )
598 {
599 return false;
600 }
601
602 bool wxDataViewMainWindow::RowChanged( size_t WXUNUSED(row) )
603 {
604 return false;
605 }
606
607 bool wxDataViewMainWindow::ValueChanged( size_t WXUNUSED(col), size_t row )
608 {
609 wxRect rect( 0, row*m_lineHeight, 10000, m_lineHeight );
610 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
611 Refresh( true, &rect );
612
613 return true;
614 }
615
616 bool wxDataViewMainWindow::RowsReordered( size_t *WXUNUSED(new_order) )
617 {
618 return false;
619 }
620
621 bool wxDataViewMainWindow::Cleared()
622 {
623 return false;
624 }
625
626 void wxDataViewMainWindow::UpdateDisplay()
627 {
628 m_dirty = true;
629 }
630
631 void wxDataViewMainWindow::OnInternalIdle()
632 {
633 wxWindow::OnInternalIdle();
634
635 if (m_dirty)
636 {
637 RecalculateDisplay();
638 m_dirty = false;
639 }
640 }
641
642 void wxDataViewMainWindow::RecalculateDisplay()
643 {
644 wxDataViewListModel *model = GetOwner()->GetModel();
645 if (!model)
646 {
647 Refresh();
648 return;
649 }
650
651 int width = 0;
652 size_t cols = GetOwner()->GetNumberOfColumns();
653 size_t i;
654 for (i = 0; i < cols; i++)
655 {
656 wxDataViewColumn *col = GetOwner()->GetColumn( i );
657 width += col->GetWidth();
658 }
659
660 int height = model->GetNumberOfRows() * m_lineHeight;
661
662 SetVirtualSize( width, height );
663 GetOwner()->SetScrollRate( 10, m_lineHeight );
664
665 Refresh();
666 }
667
668 void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
669 {
670 wxWindow::ScrollWindow( dx, dy, rect );
671 GetOwner()->m_headerArea->ScrollWindow( dx, 0 );
672 }
673
674 void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
675 {
676 wxPaintDC dc( this );
677
678 GetOwner()->PrepareDC( dc );
679
680 dc.SetFont( GetFont() );
681
682 wxRect update = GetUpdateRegion().GetBox();
683 m_owner->CalcUnscrolledPosition( update.x, update.y, &update.x, &update.y );
684
685 wxDataViewListModel *model = GetOwner()->GetModel();
686
687 size_t item_start = update.y / m_lineHeight;
688 size_t item_count = (update.height / m_lineHeight) + 1;
689
690 wxRect cell_rect;
691 cell_rect.x = 0;
692 cell_rect.height = m_lineHeight;
693 size_t cols = GetOwner()->GetNumberOfColumns();
694 size_t i;
695 for (i = 0; i < cols; i++)
696 {
697 wxDataViewColumn *col = GetOwner()->GetColumn( i );
698 wxDataViewCell *cell = col->GetCell();
699 cell_rect.width = col->GetWidth();
700
701 size_t item;
702 for (item = item_start; item <= item_start+item_count; item++)
703 {
704 cell_rect.y = item*m_lineHeight;
705 wxVariant value;
706 model->GetValue( value, col->GetModelColumn(), item );
707 cell->SetValue( value );
708 wxSize size = cell->GetSize();
709 // cannot be bigger than allocated space
710 size.x = wxMin( size.x, cell_rect.width );
711 size.y = wxMin( size.y, cell_rect.height );
712 // TODO: check for left/right/centre alignment here
713 wxRect item_rect;
714 // for now: centre
715 item_rect.x = cell_rect.x + (cell_rect.width / 2) - (size.x / 2);
716 item_rect.y = cell_rect.y + (cell_rect.height / 2) - (size.y / 2);
717
718 item_rect.width = size.x;
719 item_rect.height= size.y;
720 cell->Render( item_rect, &dc, 0 );
721 }
722
723 cell_rect.x += cell_rect.width;
724 }
725 }
726
727 void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
728 {
729 int x = event.GetX();
730 int y = event.GetY();
731 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
732
733 wxDataViewColumn *col = NULL;
734
735 int xpos = 0;
736 size_t cols = GetOwner()->GetNumberOfColumns();
737 size_t i;
738 for (i = 0; i < cols; i++)
739 {
740 wxDataViewColumn *c = GetOwner()->GetColumn( i );
741 if (x < xpos + c->GetWidth())
742 {
743 col = c;
744 break;
745 }
746 xpos += c->GetWidth();
747 }
748 if (!col)
749 return;
750 wxDataViewCell *cell = col->GetCell();
751
752 size_t row = y / m_lineHeight;
753
754 wxDataViewListModel *model = GetOwner()->GetModel();
755
756 if (event.LeftDClick())
757 {
758 if (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE)
759 {
760 wxVariant value;
761 model->GetValue( value, col->GetModelColumn(), row );
762 cell->SetValue( value );
763 wxRect cell_rect( xpos, row * m_lineHeight, col->GetWidth(), m_lineHeight );
764 cell->Activate( cell_rect, model, col->GetModelColumn(), row );
765 }
766
767 return;
768 }
769
770 event.Skip();
771 }
772
773 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event )
774 {
775 event.Skip();
776 }
777
778 //-----------------------------------------------------------------------------
779 // wxDataViewCtrl
780 //-----------------------------------------------------------------------------
781
782 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
783
784 BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase)
785 EVT_SIZE(wxDataViewCtrl::OnSize)
786 END_EVENT_TABLE()
787
788 wxDataViewCtrl::~wxDataViewCtrl()
789 {
790 if (m_notifier)
791 GetModel()->RemoveNotifier( m_notifier );
792 }
793
794 void wxDataViewCtrl::Init()
795 {
796 m_notifier = NULL;
797 }
798
799 bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
800 const wxPoint& pos, const wxSize& size,
801 long style, const wxValidator& validator )
802 {
803 if (!wxControl::Create( parent, id, pos, size, style | wxScrolledWindowStyle|wxSUNKEN_BORDER, validator))
804 return false;
805
806 Init();
807
808 #ifdef __WXMAC__
809 MacSetClipChildren( true ) ;
810 #endif
811
812 m_clientArea = new wxDataViewMainWindow( this, wxID_ANY );
813 m_headerArea = new wxDataViewHeaderWindow( this, wxID_ANY, wxDefaultPosition, wxSize(wxDefaultCoord,25) );
814
815 SetTargetWindow( m_clientArea );
816
817 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
818 sizer->Add( m_headerArea, 0, wxGROW );
819 sizer->Add( m_clientArea, 1, wxGROW );
820 SetSizer( sizer );
821
822 return true;
823 }
824
825 #ifdef __WXMSW__
826 WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg,
827 WXWPARAM wParam,
828 WXLPARAM lParam)
829 {
830 WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
831
832 #ifndef __WXWINCE__
833 // we need to process arrows ourselves for scrolling
834 if ( nMsg == WM_GETDLGCODE )
835 {
836 rc |= DLGC_WANTARROWS;
837 }
838 #endif
839
840 return rc;
841 }
842 #endif
843
844 void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
845 {
846 // We need to override OnSize so that our scrolled
847 // window a) does call Layout() to use sizers for
848 // positioning the controls but b) does not query
849 // the sizer for their size and use that for setting
850 // the scrollable area as set that ourselves by
851 // calling SetScrollbar() further down.
852
853 Layout();
854
855 AdjustScrollbars();
856 }
857
858 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel *model )
859 {
860 if (!wxDataViewCtrlBase::AssociateModel( model ))
861 return false;
862
863 m_notifier = new wxGenericDataViewListModelNotifier( m_clientArea );
864
865 model->AddNotifier( m_notifier );
866
867 m_clientArea->UpdateDisplay();
868
869 return true;
870 }
871
872 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
873 {
874 if (!wxDataViewCtrlBase::AppendColumn(col))
875 return false;
876
877 m_clientArea->UpdateDisplay();
878
879 return true;
880 }
881
882 #endif
883 // !wxUSE_GENERICDATAVIEWCTRL
884
885 #endif
886 // wxUSE_DATAVIEWCTRL
887