]> git.saurik.com Git - wxWidgets.git/blob - src/generic/datavgen.cpp
only change the bitmap size for the borderless controls
[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 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #if wxUSE_DATAVIEWCTRL
18
19 #include "wx/dataview.h"
20
21 #ifdef wxUSE_GENERICDATAVIEWCTRL
22
23 #ifndef WX_PRECOMP
24 #ifdef __WXMSW__
25 #include "wx/msw/wrapwin.h"
26 #endif
27 #include "wx/sizer.h"
28 #include "wx/log.h"
29 #include "wx/dcclient.h"
30 #include "wx/timer.h"
31 #include "wx/settings.h"
32 #endif
33
34 #include "wx/stockitem.h"
35 #include "wx/calctrl.h"
36 #include "wx/popupwin.h"
37 #include "wx/renderer.h"
38 #include "wx/dcbuffer.h"
39 #include "wx/icon.h"
40
41 //-----------------------------------------------------------------------------
42 // classes
43 //-----------------------------------------------------------------------------
44
45 class wxDataViewCtrl;
46
47 //-----------------------------------------------------------------------------
48 // wxDataViewHeaderWindow
49 //-----------------------------------------------------------------------------
50
51 class wxDataViewHeaderWindow: public wxWindow
52 {
53 public:
54 wxDataViewHeaderWindow( wxDataViewCtrl *parent,
55 wxWindowID id,
56 const wxPoint &pos = wxDefaultPosition,
57 const wxSize &size = wxDefaultSize,
58 const wxString &name = wxT("wxdataviewctrlheaderwindow") );
59 virtual ~wxDataViewHeaderWindow();
60
61 void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; }
62 wxDataViewCtrl *GetOwner() { return m_owner; }
63
64 void OnPaint( wxPaintEvent &event );
65 void OnMouse( wxMouseEvent &event );
66 void OnSetFocus( wxFocusEvent &event );
67
68 private:
69 wxDataViewCtrl *m_owner;
70 wxCursor *m_resizeCursor;
71
72 private:
73 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindow)
74 DECLARE_EVENT_TABLE()
75 };
76
77 //-----------------------------------------------------------------------------
78 // wxDataViewRenameTimer
79 //-----------------------------------------------------------------------------
80
81 class wxDataViewRenameTimer: public wxTimer
82 {
83 private:
84 wxDataViewMainWindow *m_owner;
85
86 public:
87 wxDataViewRenameTimer( wxDataViewMainWindow *owner );
88 void Notify();
89 };
90
91 //-----------------------------------------------------------------------------
92 // wxDataViewTextCtrlWrapper: wraps a wxTextCtrl for inline editing
93 //-----------------------------------------------------------------------------
94
95 class wxDataViewTextCtrlWrapper : public wxEvtHandler
96 {
97 public:
98 // NB: text must be a valid object but not Create()d yet
99 wxDataViewTextCtrlWrapper( wxDataViewMainWindow *owner,
100 wxTextCtrl *text,
101 wxDataViewListModel *model,
102 unsigned int col, unsigned int row,
103 wxRect cellLabel );
104
105 wxTextCtrl *GetText() const { return m_text; }
106
107 void AcceptChangesAndFinish();
108
109 protected:
110 void OnChar( wxKeyEvent &event );
111 void OnKeyUp( wxKeyEvent &event );
112 void OnKillFocus( wxFocusEvent &event );
113
114 bool AcceptChanges();
115 void Finish();
116
117 private:
118 wxDataViewMainWindow *m_owner;
119 wxTextCtrl *m_text;
120 wxString m_startValue;
121 wxDataViewListModel *m_model;
122 unsigned int m_col;
123 unsigned int m_row;
124 bool m_finished;
125 bool m_aboutToFinish;
126
127 DECLARE_EVENT_TABLE()
128 };
129
130 //-----------------------------------------------------------------------------
131 // wxDataViewMainWindow
132 //-----------------------------------------------------------------------------
133
134 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection, WXDLLIMPEXP_ADV);
135
136 class wxDataViewMainWindow: public wxWindow
137 {
138 public:
139 wxDataViewMainWindow( wxDataViewCtrl *parent,
140 wxWindowID id,
141 const wxPoint &pos = wxDefaultPosition,
142 const wxSize &size = wxDefaultSize,
143 const wxString &name = wxT("wxdataviewctrlmainwindow") );
144 virtual ~wxDataViewMainWindow();
145
146 // notifications from wxDataViewListModel
147 bool RowAppended();
148 bool RowPrepended();
149 bool RowInserted( unsigned int before );
150 bool RowDeleted( unsigned int row );
151 bool RowChanged( unsigned int row );
152 bool ValueChanged( unsigned int col, unsigned int row );
153 bool RowsReordered( unsigned int *new_order );
154 bool Cleared();
155
156 void SetOwner( wxDataViewCtrl* owner ) { m_owner = owner; }
157 wxDataViewCtrl *GetOwner() { return m_owner; }
158
159 void OnPaint( wxPaintEvent &event );
160 void OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event);
161 void OnChar( wxKeyEvent &event );
162 void OnMouse( wxMouseEvent &event );
163 void OnSetFocus( wxFocusEvent &event );
164 void OnKillFocus( wxFocusEvent &event );
165
166 void UpdateDisplay();
167 void RecalculateDisplay();
168 void OnInternalIdle();
169
170 void OnRenameTimer();
171 void FinishEditing( wxTextCtrl *text );
172
173 void ScrollWindow( int dx, int dy, const wxRect *rect );
174
175 bool HasCurrentRow() { return m_currentRow != (unsigned int)-1; }
176 void ChangeCurrentRow( unsigned int row );
177
178 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE); };
179 bool IsEmpty() { return GetRowCount() == 0; }
180
181 int GetCountPerPage();
182 int GetEndOfLastCol();
183 unsigned int GetFirstVisibleRow();
184 unsigned int GetLastVisibleRow();
185 unsigned int GetRowCount();
186
187 void SelectAllRows( bool on );
188 void SelectRow( unsigned int row, bool on );
189 void SelectRows( unsigned int from, unsigned int to, bool on );
190 void ReverseRowSelection( unsigned int row );
191 bool IsRowSelected( unsigned int row );
192
193 void RefreshRow( unsigned int row );
194 void RefreshRows( unsigned int from, unsigned int to );
195 void RefreshRowsAfter( unsigned int firstRow );
196
197 private:
198 wxDataViewCtrl *m_owner;
199 int m_lineHeight;
200 bool m_dirty;
201
202 wxDataViewColumn *m_currentCol;
203 unsigned int m_currentRow;
204 wxDataViewSelection m_selection;
205
206 wxDataViewRenameTimer *m_renameTimer;
207 wxDataViewTextCtrlWrapper *m_textctrlWrapper;
208 bool m_lastOnSame;
209
210 bool m_hasFocus;
211
212 int m_dragCount;
213 wxPoint m_dragStart;
214
215 // for double click logic
216 unsigned int m_lineLastClicked,
217 m_lineBeforeLastClicked,
218 m_lineSelectSingleOnUp;
219
220 private:
221 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow)
222 DECLARE_EVENT_TABLE()
223 };
224
225 // ---------------------------------------------------------
226 // wxGenericDataViewListModelNotifier
227 // ---------------------------------------------------------
228
229 class wxGenericDataViewListModelNotifier: public wxDataViewListModelNotifier
230 {
231 public:
232 wxGenericDataViewListModelNotifier( wxDataViewMainWindow *mainWindow )
233 { m_mainWindow = mainWindow; }
234
235 virtual bool RowAppended()
236 { return m_mainWindow->RowAppended(); }
237 virtual bool RowPrepended()
238 { return m_mainWindow->RowPrepended(); }
239 virtual bool RowInserted( unsigned int before )
240 { return m_mainWindow->RowInserted( before ); }
241 virtual bool RowDeleted( unsigned int row )
242 { return m_mainWindow->RowDeleted( row ); }
243 virtual bool RowChanged( unsigned int row )
244 { return m_mainWindow->RowChanged( row ); }
245 virtual bool ValueChanged( unsigned int col, unsigned int row )
246 { return m_mainWindow->ValueChanged( col, row ); }
247 virtual bool RowsReordered( unsigned int *new_order )
248 { return m_mainWindow->RowsReordered( new_order ); }
249 virtual bool Cleared()
250 { return m_mainWindow->Cleared(); }
251
252 wxDataViewMainWindow *m_mainWindow;
253 };
254
255 // ---------------------------------------------------------
256 // wxDataViewRenderer
257 // ---------------------------------------------------------
258
259 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase)
260
261 wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, wxDataViewCellMode mode ) :
262 wxDataViewRendererBase( varianttype, mode )
263 {
264 m_dc = NULL;
265 }
266
267 wxDataViewRenderer::~wxDataViewRenderer()
268 {
269 if (m_dc)
270 delete m_dc;
271 }
272
273 wxDC *wxDataViewRenderer::GetDC()
274 {
275 if (m_dc == NULL)
276 {
277 if (GetOwner() == NULL)
278 return NULL;
279 if (GetOwner()->GetOwner() == NULL)
280 return NULL;
281 m_dc = new wxClientDC( GetOwner()->GetOwner() );
282 }
283
284 return m_dc;
285 }
286
287 // ---------------------------------------------------------
288 // wxDataViewCustomRenderer
289 // ---------------------------------------------------------
290
291 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
292
293 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
294 wxDataViewCellMode mode ) :
295 wxDataViewRenderer( varianttype, mode )
296 {
297 }
298
299 // ---------------------------------------------------------
300 // wxDataViewTextRenderer
301 // ---------------------------------------------------------
302
303 IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewCustomRenderer)
304
305 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, wxDataViewCellMode mode ) :
306 wxDataViewCustomRenderer( varianttype, mode )
307 {
308 }
309
310 bool wxDataViewTextRenderer::SetValue( const wxVariant &value )
311 {
312 m_text = value.GetString();
313
314 return true;
315 }
316
317 bool wxDataViewTextRenderer::GetValue( wxVariant& WXUNUSED(value) )
318 {
319 return false;
320 }
321
322 bool wxDataViewTextRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
323 {
324 dc->DrawText( m_text, cell.x, cell.y );
325
326 return true;
327 }
328
329 wxSize wxDataViewTextRenderer::GetSize()
330 {
331 return wxSize(80,20);
332 }
333
334 // ---------------------------------------------------------
335 // wxDataViewBitmapRenderer
336 // ---------------------------------------------------------
337
338 IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewCustomRenderer)
339
340 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, wxDataViewCellMode mode ) :
341 wxDataViewCustomRenderer( varianttype, mode )
342 {
343 }
344
345 bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
346 {
347 if (value.GetType() == wxT("wxBitmap"))
348 m_bitmap << value;
349 if (value.GetType() == wxT("wxIcon"))
350 m_icon << value;
351
352 return true;
353 }
354
355 bool wxDataViewBitmapRenderer::GetValue( wxVariant& WXUNUSED(value) )
356 {
357 return false;
358 }
359
360 bool wxDataViewBitmapRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
361 {
362 if (m_bitmap.Ok())
363 dc->DrawBitmap( m_bitmap, cell.x, cell.y );
364 else if (m_icon.Ok())
365 dc->DrawIcon( m_icon, cell.x, cell.y );
366
367 return true;
368 }
369
370 wxSize wxDataViewBitmapRenderer::GetSize()
371 {
372 if (m_bitmap.Ok())
373 return wxSize( m_bitmap.GetWidth(), m_bitmap.GetHeight() );
374 else if (m_icon.Ok())
375 return wxSize( m_icon.GetWidth(), m_icon.GetHeight() );
376
377 return wxSize(16,16);
378 }
379
380 // ---------------------------------------------------------
381 // wxDataViewToggleRenderer
382 // ---------------------------------------------------------
383
384 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer, wxDataViewCustomRenderer)
385
386 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
387 wxDataViewCellMode mode ) :
388 wxDataViewCustomRenderer( varianttype, mode )
389 {
390 m_toggle = false;
391 }
392
393 bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
394 {
395 m_toggle = value.GetBool();
396
397 return true;
398 }
399
400 bool wxDataViewToggleRenderer::GetValue( wxVariant &WXUNUSED(value) )
401 {
402 return false;
403 }
404
405 bool wxDataViewToggleRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
406 {
407 // User wxRenderer here
408
409 wxRect rect;
410 rect.x = cell.x + cell.width/2 - 10;
411 rect.width = 20;
412 rect.y = cell.y + cell.height/2 - 10;
413 rect.height = 20;
414
415 int flags = 0;
416 if (m_toggle)
417 flags |= wxCONTROL_CHECKED;
418 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE)
419 flags |= wxCONTROL_DISABLED;
420
421 wxRendererNative::Get().DrawCheckBox(
422 GetOwner()->GetOwner(),
423 *dc,
424 rect,
425 flags );
426
427 return true;
428 }
429
430 bool wxDataViewToggleRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewListModel *model, unsigned int col, unsigned int row )
431 {
432 bool value = !m_toggle;
433 wxVariant variant = value;
434 model->SetValue( variant, col, row );
435 model->ValueChanged( col, row );
436 return true;
437 }
438
439 wxSize wxDataViewToggleRenderer::GetSize()
440 {
441 return wxSize(20,20);
442 }
443
444 // ---------------------------------------------------------
445 // wxDataViewProgressRenderer
446 // ---------------------------------------------------------
447
448 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer)
449
450 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
451 const wxString &varianttype, wxDataViewCellMode mode ) :
452 wxDataViewCustomRenderer( varianttype, mode )
453 {
454 m_label = label;
455 m_value = 0;
456 }
457
458 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
459 {
460 }
461
462 bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
463 {
464 m_value = (long) value;
465
466 if (m_value < 0) m_value = 0;
467 if (m_value > 100) m_value = 100;
468
469 return true;
470 }
471
472 bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
473 {
474 double pct = (double)m_value / 100.0;
475 wxRect bar = cell;
476 bar.width = (int)(cell.width * pct);
477 dc->SetPen( *wxTRANSPARENT_PEN );
478 dc->SetBrush( *wxBLUE_BRUSH );
479 dc->DrawRectangle( bar );
480
481 dc->SetBrush( *wxTRANSPARENT_BRUSH );
482 dc->SetPen( *wxBLACK_PEN );
483 dc->DrawRectangle( cell );
484
485 return true;
486 }
487
488 wxSize wxDataViewProgressRenderer::GetSize()
489 {
490 return wxSize(40,12);
491 }
492
493 // ---------------------------------------------------------
494 // wxDataViewDateRenderer
495 // ---------------------------------------------------------
496
497 class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow
498 {
499 public:
500 wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value,
501 wxDataViewListModel *model, unsigned int col, unsigned int row ) :
502 wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
503 {
504 m_model = model;
505 m_col = col;
506 m_row = row;
507 m_cal = new wxCalendarCtrl( this, wxID_ANY, *value );
508 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
509 sizer->Add( m_cal, 1, wxGROW );
510 SetSizer( sizer );
511 sizer->Fit( this );
512 }
513
514 void OnCalendar( wxCalendarEvent &event );
515
516 wxCalendarCtrl *m_cal;
517 wxDataViewListModel *m_model;
518 unsigned int m_col;
519 unsigned int m_row;
520
521 protected:
522 virtual void OnDismiss()
523 {
524 }
525
526 private:
527 DECLARE_EVENT_TABLE()
528 };
529
530 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow)
531 EVT_CALENDAR( wxID_ANY, wxDataViewDateRendererPopupTransient::OnCalendar )
532 END_EVENT_TABLE()
533
534 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event )
535 {
536 wxDateTime date = event.GetDate();
537 wxVariant value = date;
538 m_model->SetValue( value, m_col, m_row );
539 m_model->ValueChanged( m_col, m_row );
540 DismissAndNotify();
541 }
542
543 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer)
544
545 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype,
546 wxDataViewCellMode mode ) :
547 wxDataViewCustomRenderer( varianttype, mode )
548 {
549 }
550
551 bool wxDataViewDateRenderer::SetValue( const wxVariant &value )
552 {
553 m_date = value.GetDateTime();
554
555 return true;
556 }
557
558 bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
559 {
560 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
561 wxString tmp = m_date.FormatDate();
562 dc->DrawText( tmp, cell.x, cell.y );
563
564 return true;
565 }
566
567 wxSize wxDataViewDateRenderer::GetSize()
568 {
569 wxDataViewCtrl* view = GetOwner()->GetOwner();
570 wxString tmp = m_date.FormatDate();
571 wxCoord x,y,d;
572 view->GetTextExtent( tmp, &x, &y, &d );
573 return wxSize(x,y+d);
574 }
575
576 bool wxDataViewDateRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewListModel *model, unsigned int col, unsigned int row )
577 {
578 wxVariant variant;
579 model->GetValue( variant, col, row );
580 wxDateTime value = variant.GetDateTime();
581
582 wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient(
583 GetOwner()->GetOwner()->GetParent(), &value, model, col, row );
584 wxPoint pos = wxGetMousePosition();
585 popup->Move( pos );
586 popup->Layout();
587 popup->Popup( popup->m_cal );
588
589 return true;
590 }
591
592 // ---------------------------------------------------------
593 // wxDataViewColumn
594 // ---------------------------------------------------------
595
596 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn, wxDataViewColumnBase)
597
598 wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell, unsigned int model_column,
599 int width, int flags ) :
600 wxDataViewColumnBase( title, cell, model_column, width, flags )
601 {
602 m_width = width;
603 if (m_width < 0)
604 m_width = 80;
605 }
606
607 wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell, unsigned int model_column,
608 int width, int flags ) :
609 wxDataViewColumnBase( bitmap, cell, model_column, width, flags )
610 {
611 m_width = width;
612 if (m_width < 0)
613 m_width = 30;
614 }
615
616 void wxDataViewColumn::SetAlignment( wxAlignment WXUNUSED(align) )
617 {
618 // TODO
619 }
620
621 void wxDataViewColumn::SetSortable( bool WXUNUSED(sortable) )
622 {
623 // TODO
624 }
625
626 bool wxDataViewColumn::GetSortable()
627 {
628 // TODO
629 return false;
630 }
631
632 void wxDataViewColumn::SetSortOrder( bool WXUNUSED(ascending) )
633 {
634 // TODO
635 }
636
637 bool wxDataViewColumn::IsSortOrderAscending()
638 {
639 // TODO
640 return true;
641 }
642
643
644 wxDataViewColumn::~wxDataViewColumn()
645 {
646 }
647
648 void wxDataViewColumn::SetTitle( const wxString &title )
649 {
650 wxDataViewColumnBase::SetTitle( title );
651
652 }
653
654 void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap )
655 {
656 wxDataViewColumnBase::SetBitmap( bitmap );
657
658 }
659
660 int wxDataViewColumn::GetWidth()
661 {
662 return m_width;
663 }
664
665 //-----------------------------------------------------------------------------
666 // wxDataViewHeaderWindow
667 //-----------------------------------------------------------------------------
668
669 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindow, wxWindow)
670
671 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow,wxWindow)
672 EVT_PAINT (wxDataViewHeaderWindow::OnPaint)
673 EVT_MOUSE_EVENTS (wxDataViewHeaderWindow::OnMouse)
674 EVT_SET_FOCUS (wxDataViewHeaderWindow::OnSetFocus)
675 END_EVENT_TABLE()
676
677 wxDataViewHeaderWindow::wxDataViewHeaderWindow( wxDataViewCtrl *parent, wxWindowID id,
678 const wxPoint &pos, const wxSize &size, const wxString &name ) :
679 wxWindow( parent, id, pos, size, 0, name )
680 {
681 SetOwner( parent );
682
683 m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE );
684
685 wxVisualAttributes attr = wxPanel::GetClassDefaultAttributes();
686 SetBackgroundStyle( wxBG_STYLE_CUSTOM );
687 SetOwnForegroundColour( attr.colFg );
688 SetOwnBackgroundColour( attr.colBg );
689 if (!m_hasFont)
690 SetOwnFont( attr.font );
691 }
692
693 wxDataViewHeaderWindow::~wxDataViewHeaderWindow()
694 {
695 delete m_resizeCursor;
696 }
697
698 void wxDataViewHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
699 {
700 int w, h;
701 GetClientSize( &w, &h );
702
703 wxAutoBufferedPaintDC dc( this );
704
705 dc.SetBackground(GetBackgroundColour());
706 dc.Clear();
707
708 int xpix;
709 m_owner->GetScrollPixelsPerUnit( &xpix, NULL );
710
711 int x;
712 m_owner->GetViewStart( &x, NULL );
713
714 // account for the horz scrollbar offset
715 dc.SetDeviceOrigin( -x * xpix, 0 );
716
717 dc.SetFont( GetFont() );
718
719 unsigned int cols = GetOwner()->GetNumberOfColumns();
720 unsigned int i;
721 int xpos = 0;
722 for (i = 0; i < cols; i++)
723 {
724 wxDataViewColumn *col = GetOwner()->GetColumn( i );
725 int width = col->GetWidth();
726
727 int cw = width;
728 int ch = h;
729
730 wxRendererNative::Get().DrawHeaderButton
731 (
732 this,
733 dc,
734 wxRect(xpos, 0, cw, ch-1),
735 m_parent->IsEnabled() ? 0
736 : (int)wxCONTROL_DISABLED
737 );
738
739 dc.DrawText( col->GetTitle(), xpos+3, 3 );
740
741 xpos += width;
742 }
743 }
744
745 void wxDataViewHeaderWindow::OnMouse( wxMouseEvent &WXUNUSED(event) )
746 {
747 }
748
749 void wxDataViewHeaderWindow::OnSetFocus( wxFocusEvent &event )
750 {
751 GetParent()->SetFocus();
752 event.Skip();
753 }
754
755 //-----------------------------------------------------------------------------
756 // wxDataViewRenameTimer
757 //-----------------------------------------------------------------------------
758
759 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow *owner )
760 {
761 m_owner = owner;
762 }
763
764 void wxDataViewRenameTimer::Notify()
765 {
766 m_owner->OnRenameTimer();
767 }
768
769 //-----------------------------------------------------------------------------
770 // wxDataViewTextCtrlWrapper: wraps a wxTextCtrl for inline editing
771 //-----------------------------------------------------------------------------
772
773 BEGIN_EVENT_TABLE(wxDataViewTextCtrlWrapper, wxEvtHandler)
774 EVT_CHAR (wxDataViewTextCtrlWrapper::OnChar)
775 EVT_KEY_UP (wxDataViewTextCtrlWrapper::OnKeyUp)
776 EVT_KILL_FOCUS (wxDataViewTextCtrlWrapper::OnKillFocus)
777 END_EVENT_TABLE()
778
779 wxDataViewTextCtrlWrapper::wxDataViewTextCtrlWrapper(
780 wxDataViewMainWindow *owner,
781 wxTextCtrl *text,
782 wxDataViewListModel *model,
783 unsigned int col, unsigned int row,
784 wxRect rectLabel )
785 {
786 m_owner = owner;
787 m_model = model;
788 m_row = row;
789 m_col = col;
790 m_text = text;
791
792 m_finished = false;
793 m_aboutToFinish = false;
794
795 wxVariant value;
796 model->GetValue( value, col, row );
797 m_startValue = value.GetString();
798
799 m_owner->GetOwner()->CalcScrolledPosition(
800 rectLabel.x, rectLabel.y, &rectLabel.x, &rectLabel.y );
801
802 m_text->Create( owner, wxID_ANY, m_startValue,
803 wxPoint(rectLabel.x-2,rectLabel.y-2),
804 wxSize(rectLabel.width+7,rectLabel.height+4) );
805 m_text->SetFocus();
806
807 m_text->PushEventHandler(this);
808 }
809
810 void wxDataViewTextCtrlWrapper::AcceptChangesAndFinish()
811 {
812 m_aboutToFinish = true;
813
814 // Notify the owner about the changes
815 AcceptChanges();
816
817 // Even if vetoed, close the control (consistent with MSW)
818 Finish();
819 }
820
821 void wxDataViewTextCtrlWrapper::OnChar( wxKeyEvent &event )
822 {
823 switch ( event.m_keyCode )
824 {
825 case WXK_RETURN:
826 AcceptChangesAndFinish();
827 break;
828
829 case WXK_ESCAPE:
830 // m_owner->OnRenameCancelled( m_itemEdited );
831 Finish();
832 break;
833
834 default:
835 event.Skip();
836 }
837 }
838
839 void wxDataViewTextCtrlWrapper::OnKeyUp( wxKeyEvent &event )
840 {
841 if (m_finished)
842 {
843 event.Skip();
844 return;
845 }
846
847 // auto-grow the textctrl
848 wxSize parentSize = m_owner->GetSize();
849 wxPoint myPos = m_text->GetPosition();
850 wxSize mySize = m_text->GetSize();
851 int sx, sy;
852 m_text->GetTextExtent(m_text->GetValue() + _T("MM"), &sx, &sy);
853 if (myPos.x + sx > parentSize.x)
854 sx = parentSize.x - myPos.x;
855 if (mySize.x > sx)
856 sx = mySize.x;
857 m_text->SetSize(sx, wxDefaultCoord);
858
859 event.Skip();
860 }
861
862 void wxDataViewTextCtrlWrapper::OnKillFocus( wxFocusEvent &event )
863 {
864 if ( !m_finished && !m_aboutToFinish )
865 {
866 AcceptChanges();
867 //if ( !AcceptChanges() )
868 // m_owner->OnRenameCancelled( m_itemEdited );
869
870 Finish();
871 }
872
873 // We must let the native text control handle focus
874 event.Skip();
875 }
876
877 bool wxDataViewTextCtrlWrapper::AcceptChanges()
878 {
879 const wxString value = m_text->GetValue();
880
881 if ( value == m_startValue )
882 // nothing changed, always accept
883 return true;
884
885 // if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
886 // vetoed by the user
887 // return false;
888
889 // accepted, do rename the item
890 wxVariant variant;
891 variant = value;
892 m_model->SetValue( variant, m_col, m_row );
893 m_model->ValueChanged( m_col, m_row );
894
895 return true;
896 }
897
898 void wxDataViewTextCtrlWrapper::Finish()
899 {
900 if ( !m_finished )
901 {
902 m_finished = true;
903
904 m_text->RemoveEventHandler(this);
905 m_owner->FinishEditing(m_text);
906
907 // delete later
908 wxPendingDelete.Append( this );
909 }
910 }
911
912 //-----------------------------------------------------------------------------
913 // wxDataViewMainWindow
914 //-----------------------------------------------------------------------------
915
916 int LINKAGEMODE wxDataViewSelectionCmp( unsigned int row1, unsigned int row2 )
917 {
918 if (row1 > row2) return 1;
919 if (row1 == row2) return 0;
920 return -1;
921 }
922
923
924 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow, wxWindow)
925
926 BEGIN_EVENT_TABLE(wxDataViewMainWindow,wxWindow)
927 EVT_PAINT (wxDataViewMainWindow::OnPaint)
928 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse)
929 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus)
930 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus)
931 EVT_CHAR (wxDataViewMainWindow::OnChar)
932 END_EVENT_TABLE()
933
934 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl *parent, wxWindowID id,
935 const wxPoint &pos, const wxSize &size, const wxString &name ) :
936 wxWindow( parent, id, pos, size, wxWANTS_CHARS, name ),
937 m_selection( wxDataViewSelectionCmp )
938
939 {
940 SetOwner( parent );
941
942 m_lastOnSame = false;
943 m_renameTimer = new wxDataViewRenameTimer( this );
944 m_textctrlWrapper = NULL;
945
946 // TODO: user better initial values/nothing selected
947 m_currentCol = NULL;
948 m_currentRow = 0;
949
950 // TODO: we need to calculate this smartly
951 m_lineHeight = 20;
952
953 m_dragCount = 0;
954 m_dragStart = wxPoint(0,0);
955 m_lineLastClicked = (unsigned int) -1;
956 m_lineBeforeLastClicked = (unsigned int) -1;
957 m_lineSelectSingleOnUp = (unsigned int) -1;
958
959 m_hasFocus = false;
960
961 SetBackgroundStyle( wxBG_STYLE_CUSTOM );
962 SetBackgroundColour( *wxWHITE );
963
964 UpdateDisplay();
965 }
966
967 wxDataViewMainWindow::~wxDataViewMainWindow()
968 {
969 delete m_renameTimer;
970 }
971
972 void wxDataViewMainWindow::OnRenameTimer()
973 {
974 // We have to call this here because changes may just have
975 // been made and no screen update taken place.
976 if ( m_dirty )
977 wxSafeYield();
978
979
980 int xpos = 0;
981 unsigned int cols = GetOwner()->GetNumberOfColumns();
982 unsigned int i;
983 for (i = 0; i < cols; i++)
984 {
985 wxDataViewColumn *c = GetOwner()->GetColumn( i );
986 if (c == m_currentCol)
987 break;
988 xpos += c->GetWidth();
989 }
990 wxRect labelRect( xpos, m_currentRow * m_lineHeight, m_currentCol->GetWidth(), m_lineHeight );
991
992 wxClassInfo *textControlClass = CLASSINFO(wxTextCtrl);
993
994 wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
995 m_textctrlWrapper = new wxDataViewTextCtrlWrapper(this, text, GetOwner()->GetModel(),
996 m_currentCol->GetModelColumn(), m_currentRow, labelRect );
997 }
998
999 void wxDataViewMainWindow::FinishEditing( wxTextCtrl *text )
1000 {
1001 delete text;
1002 m_textctrlWrapper = NULL;
1003 SetFocus();
1004 // SetFocusIgnoringChildren();
1005 }
1006
1007 bool wxDataViewMainWindow::RowAppended()
1008 {
1009 return false;
1010 }
1011
1012 bool wxDataViewMainWindow::RowPrepended()
1013 {
1014 return false;
1015 }
1016
1017 bool wxDataViewMainWindow::RowInserted( unsigned int WXUNUSED(before) )
1018 {
1019 return false;
1020 }
1021
1022 bool wxDataViewMainWindow::RowDeleted( unsigned int WXUNUSED(row) )
1023 {
1024 return false;
1025 }
1026
1027 bool wxDataViewMainWindow::RowChanged( unsigned int WXUNUSED(row) )
1028 {
1029 return false;
1030 }
1031
1032 bool wxDataViewMainWindow::ValueChanged( unsigned int WXUNUSED(col), unsigned int row )
1033 {
1034 wxRect rect( 0, row*m_lineHeight, 10000, m_lineHeight );
1035 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1036 Refresh( true, &rect );
1037
1038 return true;
1039 }
1040
1041 bool wxDataViewMainWindow::RowsReordered( unsigned int *WXUNUSED(new_order) )
1042 {
1043 Refresh();
1044
1045 return true;
1046 }
1047
1048 bool wxDataViewMainWindow::Cleared()
1049 {
1050 return false;
1051 }
1052
1053 void wxDataViewMainWindow::UpdateDisplay()
1054 {
1055 m_dirty = true;
1056 }
1057
1058 void wxDataViewMainWindow::OnInternalIdle()
1059 {
1060 wxWindow::OnInternalIdle();
1061
1062 if (m_dirty)
1063 {
1064 RecalculateDisplay();
1065 m_dirty = false;
1066 }
1067 }
1068
1069 void wxDataViewMainWindow::RecalculateDisplay()
1070 {
1071 wxDataViewListModel *model = GetOwner()->GetModel();
1072 if (!model)
1073 {
1074 Refresh();
1075 return;
1076 }
1077
1078 int width = 0;
1079 unsigned int cols = GetOwner()->GetNumberOfColumns();
1080 unsigned int i;
1081 for (i = 0; i < cols; i++)
1082 {
1083 wxDataViewColumn *col = GetOwner()->GetColumn( i );
1084 width += col->GetWidth();
1085 }
1086
1087 int height = model->GetNumberOfRows() * m_lineHeight;
1088
1089 SetVirtualSize( width, height );
1090 GetOwner()->SetScrollRate( 10, m_lineHeight );
1091
1092 Refresh();
1093 }
1094
1095 void wxDataViewMainWindow::ScrollWindow( int dx, int dy, const wxRect *rect )
1096 {
1097 wxWindow::ScrollWindow( dx, dy, rect );
1098 GetOwner()->m_headerArea->ScrollWindow( dx, 0 );
1099 }
1100
1101 void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1102 {
1103 wxAutoBufferedPaintDC dc( this );
1104
1105 dc.SetBackground(GetBackgroundColour());
1106 dc.Clear();
1107
1108 GetOwner()->PrepareDC( dc );
1109
1110 dc.SetFont( GetFont() );
1111
1112 wxRect update = GetUpdateRegion().GetBox();
1113 m_owner->CalcUnscrolledPosition( update.x, update.y, &update.x, &update.y );
1114
1115 wxDataViewListModel *model = GetOwner()->GetModel();
1116
1117 unsigned int item_start = wxMax( 0, (update.y / m_lineHeight) );
1118 unsigned int item_count = wxMin( (int)(((update.y + update.height) / m_lineHeight) - item_start + 1),
1119 (int)(model->GetNumberOfRows()-item_start) );
1120
1121
1122
1123 unsigned int item;
1124 for (item = item_start; item < item_start+item_count; item++)
1125 {
1126 if (m_selection.Index( item ) != wxNOT_FOUND)
1127 {
1128 int flags = wxCONTROL_SELECTED;
1129 if (item == m_currentRow)
1130 flags |= wxCONTROL_CURRENT;
1131 if (m_hasFocus)
1132 flags |= wxCONTROL_FOCUSED;
1133 wxRect rect( 0, item*m_lineHeight+1, GetEndOfLastCol(), m_lineHeight-2 );
1134 wxRendererNative::Get().DrawItemSelectionRect
1135 (
1136 this,
1137 dc,
1138 rect,
1139 flags
1140 );
1141 }
1142 else
1143 {
1144 if (item == m_currentRow)
1145 {
1146 int flags = wxCONTROL_CURRENT;
1147 if (m_hasFocus)
1148 flags |= wxCONTROL_FOCUSED; // should have no effect
1149 wxRect rect( 0, item*m_lineHeight+1, GetEndOfLastCol(), m_lineHeight-2 );
1150 wxRendererNative::Get().DrawItemSelectionRect
1151 (
1152 this,
1153 dc,
1154 rect,
1155 flags
1156 );
1157
1158 }
1159 }
1160 }
1161
1162 wxRect cell_rect;
1163 cell_rect.x = 0;
1164 cell_rect.height = m_lineHeight;
1165 unsigned int cols = GetOwner()->GetNumberOfColumns();
1166 unsigned int i;
1167 for (i = 0; i < cols; i++)
1168 {
1169 wxDataViewColumn *col = GetOwner()->GetColumn( i );
1170 wxDataViewRenderer *cell = col->GetRenderer();
1171 cell_rect.width = col->GetWidth();
1172
1173 for (item = item_start; item < item_start+item_count; item++)
1174 {
1175 cell_rect.y = item*m_lineHeight;
1176 wxVariant value;
1177 model->GetValue( value, col->GetModelColumn(), item );
1178 cell->SetValue( value );
1179 wxSize size = cell->GetSize();
1180 // cannot be bigger than allocated space
1181 size.x = wxMin( size.x, cell_rect.width );
1182 size.y = wxMin( size.y, cell_rect.height );
1183 // TODO: check for left/right/centre alignment here
1184 wxRect item_rect;
1185 // for now: centre
1186 item_rect.x = cell_rect.x + (cell_rect.width / 2) - (size.x / 2);
1187 item_rect.y = cell_rect.y + (cell_rect.height / 2) - (size.y / 2);
1188
1189 item_rect.width = size.x;
1190 item_rect.height= size.y;
1191 cell->Render( item_rect, &dc, 0 );
1192 }
1193
1194 cell_rect.x += cell_rect.width;
1195 }
1196 }
1197
1198 int wxDataViewMainWindow::GetCountPerPage()
1199 {
1200 wxSize size = GetClientSize();
1201 return size.y / m_lineHeight;
1202 }
1203
1204 int wxDataViewMainWindow::GetEndOfLastCol()
1205 {
1206 int width = 0;
1207 unsigned int i;
1208 for (i = 0; i < GetOwner()->GetNumberOfColumns(); i++)
1209 {
1210 wxDataViewColumn *c = GetOwner()->GetColumn( i );
1211 width += c->GetWidth();
1212 }
1213 return width;
1214 }
1215
1216 unsigned int wxDataViewMainWindow::GetFirstVisibleRow()
1217 {
1218 int x = 0;
1219 int y = 0;
1220 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
1221
1222 return y / m_lineHeight;
1223 }
1224
1225 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
1226 {
1227 wxSize client_size = GetClientSize();
1228 m_owner->CalcUnscrolledPosition( client_size.x, client_size.y, &client_size.x, &client_size.y );
1229
1230 return wxMin( GetRowCount()-1, ((unsigned)client_size.y/m_lineHeight)+1 );
1231 }
1232
1233 unsigned int wxDataViewMainWindow::GetRowCount()
1234 {
1235 return GetOwner()->GetModel()->GetNumberOfRows();
1236 }
1237
1238 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row )
1239 {
1240 m_currentRow = row;
1241
1242 // send event
1243 }
1244
1245 void wxDataViewMainWindow::SelectAllRows( bool on )
1246 {
1247 if (IsEmpty())
1248 return;
1249
1250 if (on)
1251 {
1252 m_selection.Clear();
1253 for (unsigned int i = 0; i < GetRowCount(); i++)
1254 m_selection.Add( i );
1255 Refresh();
1256 }
1257 else
1258 {
1259 unsigned int first_visible = GetFirstVisibleRow();
1260 unsigned int last_visible = GetLastVisibleRow();
1261 unsigned int i;
1262 for (i = 0; i < m_selection.GetCount(); i++)
1263 {
1264 unsigned int row = m_selection[i];
1265 if ((row >= first_visible) && (row <= last_visible))
1266 RefreshRow( row );
1267 }
1268 m_selection.Clear();
1269 }
1270 }
1271
1272 void wxDataViewMainWindow::SelectRow( unsigned int row, bool on )
1273 {
1274 if (m_selection.Index( row ) == wxNOT_FOUND)
1275 {
1276 if (on)
1277 {
1278 m_selection.Add( row );
1279 RefreshRow( row );
1280 }
1281 }
1282 else
1283 {
1284 if (!on)
1285 {
1286 m_selection.Remove( row );
1287 RefreshRow( row );
1288 }
1289 }
1290 }
1291
1292 void wxDataViewMainWindow::SelectRows( unsigned int from, unsigned int to, bool on )
1293 {
1294 if (from > to)
1295 {
1296 unsigned int tmp = from;
1297 from = to;
1298 to = tmp;
1299 }
1300
1301 unsigned int i;
1302 for (i = from; i <= to; i++)
1303 {
1304 if (m_selection.Index( i ) == wxNOT_FOUND)
1305 {
1306 if (on)
1307 m_selection.Add( i );
1308 }
1309 else
1310 {
1311 if (!on)
1312 m_selection.Remove( i );
1313 }
1314 }
1315 RefreshRows( from, to );
1316 }
1317
1318 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row )
1319 {
1320 if (m_selection.Index( row ) == wxNOT_FOUND)
1321 m_selection.Add( row );
1322 else
1323 m_selection.Remove( row );
1324 RefreshRow( row );
1325 }
1326
1327 bool wxDataViewMainWindow::IsRowSelected( unsigned int row )
1328 {
1329 return (m_selection.Index( row ) != wxNOT_FOUND);
1330 }
1331
1332 void wxDataViewMainWindow::RefreshRow( unsigned int row )
1333 {
1334 wxRect rect( 0, row*m_lineHeight, GetEndOfLastCol(), m_lineHeight );
1335 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1336
1337 wxSize client_size = GetClientSize();
1338 wxRect client_rect( 0, 0, client_size.x, client_size.y );
1339 wxRect intersect_rect = client_rect.Intersect( rect );
1340 if (intersect_rect.width > 0)
1341 Refresh( true, &intersect_rect );
1342 }
1343
1344 void wxDataViewMainWindow::RefreshRows( unsigned int from, unsigned int to )
1345 {
1346 if (from > to)
1347 {
1348 unsigned int tmp = to;
1349 to = from;
1350 from = tmp;
1351 }
1352
1353 wxRect rect( 0, from*m_lineHeight, GetEndOfLastCol(), (to-from+1) * m_lineHeight );
1354 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1355
1356 wxSize client_size = GetClientSize();
1357 wxRect client_rect( 0, 0, client_size.x, client_size.y );
1358 wxRect intersect_rect = client_rect.Intersect( rect );
1359 if (intersect_rect.width > 0)
1360 Refresh( true, &intersect_rect );
1361 }
1362
1363 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow )
1364 {
1365 unsigned int count = GetRowCount();
1366 if (firstRow > count)
1367 return;
1368
1369 wxRect rect( 0, firstRow*m_lineHeight, GetEndOfLastCol(), count * m_lineHeight );
1370 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1371
1372 wxSize client_size = GetClientSize();
1373 wxRect client_rect( 0, 0, client_size.x, client_size.y );
1374 wxRect intersect_rect = client_rect.Intersect( rect );
1375 if (intersect_rect.width > 0)
1376 Refresh( true, &intersect_rect );
1377 }
1378
1379 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent, const wxKeyEvent& event)
1380 {
1381 wxCHECK_RET( newCurrent < GetRowCount(),
1382 _T("invalid item index in OnArrowChar()") );
1383
1384 // if there is no selection, we cannot move it anywhere
1385 if (!HasCurrentRow())
1386 return;
1387
1388 unsigned int oldCurrent = m_currentRow;
1389
1390 // in single selection we just ignore Shift as we can't select several
1391 // items anyhow
1392 if ( event.ShiftDown() && !IsSingleSel() )
1393 {
1394 RefreshRow( oldCurrent );
1395
1396 ChangeCurrentRow( newCurrent );
1397
1398 // select all the items between the old and the new one
1399 if ( oldCurrent > newCurrent )
1400 {
1401 newCurrent = oldCurrent;
1402 oldCurrent = m_currentRow;
1403 }
1404
1405 SelectRows( oldCurrent, newCurrent, true );
1406 }
1407 else // !shift
1408 {
1409 RefreshRow( oldCurrent );
1410
1411 // all previously selected items are unselected unless ctrl is held
1412 if ( !event.ControlDown() )
1413 SelectAllRows(false);
1414
1415 ChangeCurrentRow( newCurrent );
1416
1417 if ( !event.ControlDown() )
1418 SelectRow( m_currentRow, true );
1419 else
1420 RefreshRow( m_currentRow );
1421 }
1422
1423 // MoveToFocus();
1424 }
1425
1426 void wxDataViewMainWindow::OnChar( wxKeyEvent &event )
1427 {
1428 if (event.GetKeyCode() == WXK_TAB)
1429 {
1430 wxNavigationKeyEvent nevent;
1431 nevent.SetWindowChange( event.ControlDown() );
1432 nevent.SetDirection( !event.ShiftDown() );
1433 nevent.SetEventObject( GetParent()->GetParent() );
1434 nevent.SetCurrentFocus( m_parent );
1435 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent ))
1436 return;
1437 }
1438
1439 // no item -> nothing to do
1440 if (!HasCurrentRow())
1441 {
1442 event.Skip();
1443 return;
1444 }
1445
1446 // don't use m_linesPerPage directly as it might not be computed yet
1447 const int pageSize = GetCountPerPage();
1448 wxCHECK_RET( pageSize, _T("should have non zero page size") );
1449
1450 switch ( event.GetKeyCode() )
1451 {
1452 case WXK_UP:
1453 if ( m_currentRow > 0 )
1454 OnArrowChar( m_currentRow - 1, event );
1455 break;
1456
1457 case WXK_DOWN:
1458 if ( m_currentRow < GetRowCount() - 1 )
1459 OnArrowChar( m_currentRow + 1, event );
1460 break;
1461
1462 case WXK_END:
1463 if (!IsEmpty())
1464 OnArrowChar( GetRowCount() - 1, event );
1465 break;
1466
1467 case WXK_HOME:
1468 if (!IsEmpty())
1469 OnArrowChar( 0, event );
1470 break;
1471
1472 case WXK_PAGEUP:
1473 {
1474 int steps = pageSize - 1;
1475 int index = m_currentRow - steps;
1476 if (index < 0)
1477 index = 0;
1478
1479 OnArrowChar( index, event );
1480 }
1481 break;
1482
1483 case WXK_PAGEDOWN:
1484 {
1485 int steps = pageSize - 1;
1486 unsigned int index = m_currentRow + steps;
1487 unsigned int count = GetRowCount();
1488 if ( index >= count )
1489 index = count - 1;
1490
1491 OnArrowChar( index, event );
1492 }
1493 break;
1494
1495 default:
1496 event.Skip();
1497 }
1498 }
1499
1500 void wxDataViewMainWindow::OnMouse( wxMouseEvent &event )
1501 {
1502 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
1503 {
1504 // let the base handle mouse wheel events.
1505 event.Skip();
1506 return;
1507 }
1508
1509 int x = event.GetX();
1510 int y = event.GetY();
1511 m_owner->CalcUnscrolledPosition( x, y, &x, &y );
1512
1513 wxDataViewColumn *col = NULL;
1514
1515 int xpos = 0;
1516 unsigned int cols = GetOwner()->GetNumberOfColumns();
1517 unsigned int i;
1518 for (i = 0; i < cols; i++)
1519 {
1520 wxDataViewColumn *c = GetOwner()->GetColumn( i );
1521 if (x < xpos + c->GetWidth())
1522 {
1523 col = c;
1524 break;
1525 }
1526 xpos += c->GetWidth();
1527 }
1528 if (!col)
1529 return;
1530 wxDataViewRenderer *cell = col->GetRenderer();
1531
1532 unsigned int current = y / m_lineHeight;
1533
1534 if ((current > GetRowCount()) || (x > GetEndOfLastCol()))
1535 {
1536 // Unselect all if below the last row ?
1537 return;
1538 }
1539
1540 wxDataViewListModel *model = GetOwner()->GetModel();
1541
1542 if (event.Dragging())
1543 {
1544 if (m_dragCount == 0)
1545 {
1546 // we have to report the raw, physical coords as we want to be
1547 // able to call HitTest(event.m_pointDrag) from the user code to
1548 // get the item being dragged
1549 m_dragStart = event.GetPosition();
1550 }
1551
1552 m_dragCount++;
1553
1554 if (m_dragCount != 3)
1555 return;
1556
1557 if (event.LeftIsDown())
1558 {
1559 // Notify cell about drag
1560 }
1561 return;
1562 }
1563 else
1564 {
1565 m_dragCount = 0;
1566 }
1567
1568 bool forceClick = false;
1569
1570 if (event.ButtonDClick())
1571 {
1572 m_renameTimer->Stop();
1573 m_lastOnSame = false;
1574 }
1575
1576 if (event.LeftDClick())
1577 {
1578 if ( current == m_lineLastClicked )
1579 {
1580 if (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE)
1581 {
1582 wxVariant value;
1583 model->GetValue( value, col->GetModelColumn(), current );
1584 cell->SetValue( value );
1585 wxRect cell_rect( xpos, current * m_lineHeight, col->GetWidth(), m_lineHeight );
1586 cell->Activate( cell_rect, model, col->GetModelColumn(), current );
1587 }
1588 return;
1589 }
1590 else
1591 {
1592 // The first click was on another item, so don't interpret this as
1593 // a double click, but as a simple click instead
1594 forceClick = true;
1595 }
1596 }
1597
1598 if (event.LeftUp())
1599 {
1600 if (m_lineSelectSingleOnUp != (unsigned int)-1)
1601 {
1602 // select single line
1603 SelectAllRows( false );
1604 SelectRow( m_lineSelectSingleOnUp, true );
1605 }
1606
1607 if (m_lastOnSame)
1608 {
1609 if ((col == m_currentCol) && (current == m_currentRow) &&
1610 (cell->GetMode() == wxDATAVIEW_CELL_EDITABLE) )
1611 {
1612 m_renameTimer->Start( 100, true );
1613 }
1614 }
1615
1616 m_lastOnSame = false;
1617 m_lineSelectSingleOnUp = (unsigned int)-1;
1618 }
1619 else
1620 {
1621 // This is necessary, because after a DnD operation in
1622 // from and to ourself, the up event is swallowed by the
1623 // DnD code. So on next non-up event (which means here and
1624 // now) m_lineSelectSingleOnUp should be reset.
1625 m_lineSelectSingleOnUp = (unsigned int)-1;
1626 }
1627
1628 if (event.RightDown())
1629 {
1630 m_lineBeforeLastClicked = m_lineLastClicked;
1631 m_lineLastClicked = current;
1632
1633 // If the item is already selected, do not update the selection.
1634 // Multi-selections should not be cleared if a selected item is clicked.
1635 if (!IsRowSelected(current))
1636 {
1637 SelectAllRows(false);
1638 ChangeCurrentRow(current);
1639 SelectRow(m_currentRow,true);
1640 }
1641
1642 // notify cell about right click
1643 // cell->...
1644
1645 // Allow generation of context menu event
1646 event.Skip();
1647 }
1648 else if (event.MiddleDown())
1649 {
1650 // notify cell about middle click
1651 // cell->...
1652 }
1653 if (event.LeftDown() || forceClick)
1654 {
1655 #ifdef __WXMSW__
1656 SetFocus();
1657 #endif
1658
1659 m_lineBeforeLastClicked = m_lineLastClicked;
1660 m_lineLastClicked = current;
1661
1662 unsigned int oldCurrentRow = m_currentRow;
1663 bool oldWasSelected = IsRowSelected(m_currentRow);
1664
1665 bool cmdModifierDown = event.CmdDown();
1666 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
1667 {
1668 if ( IsSingleSel() || !IsRowSelected(current) )
1669 {
1670 SelectAllRows( false );
1671
1672 ChangeCurrentRow(current);
1673
1674 SelectRow(m_currentRow,true);
1675 }
1676 else // multi sel & current is highlighted & no mod keys
1677 {
1678 m_lineSelectSingleOnUp = current;
1679 ChangeCurrentRow(current); // change focus
1680 }
1681 }
1682 else // multi sel & either ctrl or shift is down
1683 {
1684 if (cmdModifierDown)
1685 {
1686 ChangeCurrentRow(current);
1687
1688 ReverseRowSelection(m_currentRow);
1689 }
1690 else if (event.ShiftDown())
1691 {
1692 ChangeCurrentRow(current);
1693
1694 unsigned int lineFrom = oldCurrentRow,
1695 lineTo = current;
1696
1697 if ( lineTo < lineFrom )
1698 {
1699 lineTo = lineFrom;
1700 lineFrom = m_currentRow;
1701 }
1702
1703 SelectRows(lineFrom, lineTo, true);
1704 }
1705 else // !ctrl, !shift
1706 {
1707 // test in the enclosing if should make it impossible
1708 wxFAIL_MSG( _T("how did we get here?") );
1709 }
1710 }
1711
1712 if (m_currentRow != oldCurrentRow)
1713 RefreshRow( oldCurrentRow );
1714
1715 wxDataViewColumn *oldCurrentCol = m_currentCol;
1716
1717 // Update selection here...
1718 m_currentCol = col;
1719
1720 m_lastOnSame = !forceClick && ((col == oldCurrentCol) && (current == oldCurrentRow)) && oldWasSelected;
1721 }
1722 }
1723
1724 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent &event )
1725 {
1726 m_hasFocus = true;
1727
1728 if (HasCurrentRow())
1729 Refresh();
1730
1731 event.Skip();
1732 }
1733
1734 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent &event )
1735 {
1736 m_hasFocus = false;
1737
1738 if (HasCurrentRow())
1739 Refresh();
1740
1741 event.Skip();
1742 }
1743
1744 //-----------------------------------------------------------------------------
1745 // wxDataViewCtrl
1746 //-----------------------------------------------------------------------------
1747
1748 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
1749
1750 BEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase)
1751 EVT_SIZE(wxDataViewCtrl::OnSize)
1752 END_EVENT_TABLE()
1753
1754 wxDataViewCtrl::~wxDataViewCtrl()
1755 {
1756 if (m_notifier)
1757 GetModel()->RemoveNotifier( m_notifier );
1758 }
1759
1760 void wxDataViewCtrl::Init()
1761 {
1762 m_notifier = NULL;
1763 }
1764
1765 bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
1766 const wxPoint& pos, const wxSize& size,
1767 long style, const wxValidator& validator )
1768 {
1769 if (!wxControl::Create( parent, id, pos, size, style | wxScrolledWindowStyle|wxSUNKEN_BORDER, validator))
1770 return false;
1771
1772 Init();
1773
1774 #ifdef __WXMAC__
1775 MacSetClipChildren( true ) ;
1776 #endif
1777
1778 m_clientArea = new wxDataViewMainWindow( this, wxID_ANY );
1779 #ifdef __WXMSW__
1780 m_headerArea = new wxDataViewHeaderWindow( this, wxID_ANY, wxDefaultPosition, wxSize(wxDefaultCoord,22) );
1781 #else
1782 m_headerArea = new wxDataViewHeaderWindow( this, wxID_ANY, wxDefaultPosition, wxSize(wxDefaultCoord,25) );
1783 #endif
1784
1785 SetTargetWindow( m_clientArea );
1786
1787 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
1788 sizer->Add( m_headerArea, 0, wxGROW );
1789 sizer->Add( m_clientArea, 1, wxGROW );
1790 SetSizer( sizer );
1791
1792 return true;
1793 }
1794
1795 #ifdef __WXMSW__
1796 WXLRESULT wxDataViewCtrl::MSWWindowProc(WXUINT nMsg,
1797 WXWPARAM wParam,
1798 WXLPARAM lParam)
1799 {
1800 WXLRESULT rc = wxDataViewCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
1801
1802 #ifndef __WXWINCE__
1803 // we need to process arrows ourselves for scrolling
1804 if ( nMsg == WM_GETDLGCODE )
1805 {
1806 rc |= DLGC_WANTARROWS;
1807 }
1808 #endif
1809
1810 return rc;
1811 }
1812 #endif
1813
1814 void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
1815 {
1816 // We need to override OnSize so that our scrolled
1817 // window a) does call Layout() to use sizers for
1818 // positioning the controls but b) does not query
1819 // the sizer for their size and use that for setting
1820 // the scrollable area as set that ourselves by
1821 // calling SetScrollbar() further down.
1822
1823 Layout();
1824
1825 AdjustScrollbars();
1826 }
1827
1828 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel *model )
1829 {
1830 if (!wxDataViewCtrlBase::AssociateModel( model ))
1831 return false;
1832
1833 m_notifier = new wxGenericDataViewListModelNotifier( m_clientArea );
1834
1835 model->AddNotifier( m_notifier );
1836
1837 m_clientArea->UpdateDisplay();
1838
1839 return true;
1840 }
1841
1842 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
1843 {
1844 if (!wxDataViewCtrlBase::AppendColumn(col))
1845 return false;
1846
1847 m_clientArea->UpdateDisplay();
1848
1849 return true;
1850 }
1851
1852 void wxDataViewCtrl::SetSelection( int WXUNUSED(row) )
1853 {
1854 // FIXME - TODO
1855 }
1856
1857 void wxDataViewCtrl::SetSelectionRange( unsigned int WXUNUSED(from), unsigned int WXUNUSED(to) )
1858 {
1859 // FIXME - TODO
1860 }
1861
1862 void wxDataViewCtrl::SetSelections( const wxArrayInt& WXUNUSED(aSelections) )
1863 {
1864 // FIXME - TODO
1865 }
1866
1867 void wxDataViewCtrl::Unselect( unsigned int WXUNUSED(row) )
1868 {
1869 // FIXME - TODO
1870 }
1871
1872 bool wxDataViewCtrl::IsSelected( unsigned int WXUNUSED(row) ) const
1873 {
1874 // FIXME - TODO
1875
1876 return false;
1877 }
1878
1879 int wxDataViewCtrl::GetSelection() const
1880 {
1881 // FIXME - TODO
1882
1883 return -1;
1884 }
1885
1886 int wxDataViewCtrl::GetSelections(wxArrayInt& WXUNUSED(aSelections) ) const
1887 {
1888 // FIXME - TODO
1889
1890 return 0;
1891 }
1892
1893 #endif
1894 // !wxUSE_GENERICDATAVIEWCTRL
1895
1896 #endif
1897 // wxUSE_DATAVIEWCTRL