From 0fdc2321bc8a1ccf967c26b656b207cae9d03925 Mon Sep 17 00:00:00 2001 From: Robert Roebling Date: Thu, 23 Mar 2006 11:44:57 +0000 Subject: [PATCH] Implement basic MVC logic (change an item in one view and all views update themselves) and made wxDataViewToggleCell react to mouse clicks as a test for that (generic code). git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@38298 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/dataview.h | 3 +- include/wx/generic/dataview.h | 1 + samples/dataview/dataview.cpp | 2 +- src/generic/datavgen.cpp | 61 +++++++++++++++++++++++++++++++++-- src/gtk/dataview.cpp | 2 +- 5 files changed, 64 insertions(+), 5 deletions(-) diff --git a/include/wx/dataview.h b/include/wx/dataview.h index 211edc7577..2e47397f53 100644 --- a/include/wx/dataview.h +++ b/include/wx/dataview.h @@ -219,7 +219,8 @@ public: virtual bool GetValue( wxVariant &value ) { return true; } virtual bool Validate( wxVariant &value ) { return true; } - wxString GetVariantType() { return m_variantType; } + wxString GetVariantType() { return m_variantType; } + wxDataViewCellMode GetMode() { return m_mode; } void SetOwner( wxDataViewColumn *owner ) { m_owner = owner; } wxDataViewColumn* GetOwner() { return m_owner; } diff --git a/include/wx/generic/dataview.h b/include/wx/generic/dataview.h index c1df3d63dc..a662c899d9 100644 --- a/include/wx/generic/dataview.h +++ b/include/wx/generic/dataview.h @@ -112,6 +112,7 @@ public: bool GetValue( wxVariant &value ); bool Render( wxRect cell, wxDC *dc, int state ); + bool Activate( wxRect cell, wxDataViewListModel *model, size_t col, size_t row ); wxSize GetSize(); private: diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp index f19731e5bc..c66602fa80 100644 --- a/samples/dataview/dataview.cpp +++ b/samples/dataview/dataview.cpp @@ -376,7 +376,7 @@ MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h): dataview_right->AppendColumn( column ); dataview_right->AppendTextColumn( wxT("first"), 0 ); dataview_right->AppendTextColumn( wxT("second"), 1 ); - wxDataViewToggleCell *toggle_cell = new wxDataViewToggleCell( wxT("bool"), wxDATAVIEW_CELL_EDITABLE ); + wxDataViewToggleCell *toggle_cell = new wxDataViewToggleCell( wxT("bool"), wxDATAVIEW_CELL_ACTIVATABLE ); column = new wxDataViewColumn( wxT("bool"), toggle_cell, 3 ); dataview_right->AppendColumn( column ); diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index 6866fa7203..4317d495d6 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -252,7 +252,10 @@ bool wxDataViewToggleCell::Render( wxRect cell, wxDC *dc, int state ) { // User wxRenderer here - dc->SetPen( *wxBLACK_PEN ); + if (GetMode() == wxDATAVIEW_CELL_ACTIVATABLE) + dc->SetPen( *wxBLACK_PEN ); + else + dc->SetPen( *wxGREY_PEN ); dc->SetBrush( *wxTRANSPARENT_BRUSH ); wxRect rect; rect.x = cell.x + cell.width/2 - 10; @@ -273,6 +276,15 @@ bool wxDataViewToggleCell::Render( wxRect cell, wxDC *dc, int state ) return true; } +bool wxDataViewToggleCell::Activate( wxRect cell, wxDataViewListModel *model, size_t col, size_t row ) +{ + bool value = !m_toggle; + wxVariant variant = value; + model->SetValue( variant, col, row ); + model->ValueChanged( col, row ); + return true; +} + wxSize wxDataViewToggleCell::GetSize() { return wxSize(20,20); @@ -595,7 +607,11 @@ bool wxDataViewMainWindow::RowChanged( size_t row ) bool wxDataViewMainWindow::ValueChanged( size_t col, size_t row ) { - return false; + wxRect rect( 0, row*m_lineHeight, 10000, m_lineHeight ); + m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y ); + Refresh( true, &rect ); + + return true; } bool wxDataViewMainWindow::RowsReordered( size_t *new_order ) @@ -699,6 +715,47 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &event ) void wxDataViewMainWindow::OnMouse( wxMouseEvent &event ) { + int x = event.GetX(); + int y = event.GetY(); + m_owner->CalcUnscrolledPosition( x, y, &x, &y ); + + wxDataViewColumn *col = NULL; + + int xpos = 0; + size_t cols = GetOwner()->GetNumberOfColumns(); + size_t i; + for (i = 0; i < cols; i++) + { + wxDataViewColumn *c = GetOwner()->GetColumn( i ); + if (x < xpos + c->GetWidth()) + { + col = c; + break; + } + xpos += c->GetWidth(); + } + if (!col) + return; + wxDataViewCell *cell = col->GetCell(); + + size_t row = y / m_lineHeight; + + wxDataViewListModel *model = GetOwner()->GetModel(); + + if (event.LeftDClick()) + { + if (cell->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE) + { + wxVariant value; + model->GetValue( value, col->GetModelColumn(), row ); + cell->SetValue( value ); + wxRect cell_rect( xpos, row * m_lineHeight, col->GetWidth(), m_lineHeight ); + cell->Activate( cell_rect, model, col->GetModelColumn(), row ); + } + + return; + } + event.Skip(); } diff --git a/src/gtk/dataview.cpp b/src/gtk/dataview.cpp index b11b02de65..d0d6b5d919 100644 --- a/src/gtk/dataview.cpp +++ b/src/gtk/dataview.cpp @@ -997,7 +997,7 @@ wxDataViewToggleCell::wxDataViewToggleCell( const wxString &varianttype, { m_renderer = (void*) gtk_cell_renderer_toggle_new(); - if (m_mode & wxDATAVIEW_CELL_EDITABLE) + if (m_mode & wxDATAVIEW_CELL_ACTIVATABLE) { g_signal_connect_after( m_renderer, "toggled", G_CALLBACK(wxGtkToggleRendererToggledCallback), this ); }