From: Robert Roebling Date: Fri, 24 Feb 2006 13:19:23 +0000 (+0000) Subject: added wxDataViewToggleCell X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/605c2c4ac6ea7312c6e691caaa18f119fdf3020d added wxDataViewToggleCell git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@37706 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/dataview.h b/include/wx/dataview.h index 0b2ae8d09f..b4844dcc57 100644 --- a/include/wx/dataview.h +++ b/include/wx/dataview.h @@ -204,7 +204,8 @@ public: virtual bool AssociateModel( wxDataViewListModel *model ); wxDataViewListModel* GetModel(); - virtual bool AppendStringColumn( const wxString &label, size_t model_column ); + virtual bool AppendTextColumn( const wxString &label, size_t model_column ); + virtual bool AppendToggleColumn( const wxString &label, size_t model_column ); virtual bool AppendColumn( wxDataViewColumn *col ); virtual size_t GetNumberOfColumns(); virtual bool DeleteColumn( size_t pos ); diff --git a/include/wx/gtk/dataview.h b/include/wx/gtk/dataview.h index e1e1fab2cc..a86ebe1ed2 100644 --- a/include/wx/gtk/dataview.h +++ b/include/wx/gtk/dataview.h @@ -58,6 +58,23 @@ protected: DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextCell) }; +// --------------------------------------------------------- +// wxDataViewToggleCell +// --------------------------------------------------------- + +class wxDataViewToggleCell: public wxDataViewCell +{ +public: + wxDataViewToggleCell( const wxString &varianttype = wxT("bool"), + wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT ); + + bool SetValue( const wxVariant &value ); + bool GetValue( wxVariant &value ); + +protected: + DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewToggleCell) +}; + // --------------------------------------------------------- // wxDataViewColumn // --------------------------------------------------------- diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp index 2ce239a6bb..8662b913ce 100644 --- a/samples/dataview/dataview.cpp +++ b/samples/dataview/dataview.cpp @@ -38,18 +38,30 @@ public: size_t i; for (i = 0; i < 1000; i++) m_list.Add( wxT("Test") ); + for (i = 0; i < 500; i++) + { m_bools.Add( 0 ); m_bools.Add( 1 ); } } virtual size_t GetNumberOfRows() { return 1000; } virtual size_t GetNumberOfCols() - { return 3; } + { return 4; } + // as reported by wxVariant virtual wxString GetColType( size_t col ) - { return wxT("string"); } + { + if (col == 3) + return wxT("bool"); + + return wxT("string"); + } virtual wxVariant GetValue( size_t col, size_t row ) - { + { + if (col == 3) + { + return (bool) m_bools[row]; + } else if (col == 2) { return m_list[row]; @@ -63,6 +75,10 @@ public: } virtual bool SetValue( wxVariant &value, size_t col, size_t row ) { + if (col == 3) + { + m_bools[row] = (int) value.GetBool(); + } else if (col == 2) { m_list[row] = value.GetString(); @@ -71,6 +87,7 @@ public: } wxArrayString m_list; + wxArrayInt m_bools; }; // ------------------------------------- @@ -112,7 +129,7 @@ IMPLEMENT_APP (MyApp) bool MyApp::OnInit(void) { - MyFrame *frame = new MyFrame(NULL, _T("Dynamic wxWidgets App"), 50, 50, 450, 340); + MyFrame *frame = new MyFrame(NULL, _T("Dynamic wxWidgets App"), 50, 50, 600, 340); frame->Show(true); @@ -157,21 +174,25 @@ MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h): MyTextModel *model = new MyTextModel; dataview_left->AssociateModel( model ); - dataview_left->AppendStringColumn( wxT("first"), 0 ); - dataview_left->AppendStringColumn( wxT("second"), 1 ); - wxDataViewTextCell *cell = new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE ); - wxDataViewColumn *column = new wxDataViewColumn( wxT("editable"), cell, 2 ); + dataview_left->AppendTextColumn( wxT("first"), 0 ); + dataview_left->AppendTextColumn( wxT("second"), 1 ); + wxDataViewTextCell *text_cell = new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE ); + wxDataViewColumn *column = new wxDataViewColumn( wxT("editable"), text_cell, 2 ); dataview_left->AppendColumn( column ); + dataview_left->AppendToggleColumn( wxT("fourth"), 3 ); // Right wxDataViewCtrl using the same model dataview_right = new wxDataViewCtrl( this, -1 ); dataview_right->AssociateModel( model ); - cell = new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE ); - column = new wxDataViewColumn( wxT("editable"), cell, 2 ); + text_cell = new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE ); + column = new wxDataViewColumn( wxT("editable"), text_cell, 2 ); + 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 ); + column = new wxDataViewColumn( wxT("bool"), toggle_cell, 3 ); dataview_right->AppendColumn( column ); - dataview_right->AppendStringColumn( wxT("first"), 0 ); - dataview_right->AppendStringColumn( wxT("second"), 1 ); wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL ); sizer->Add( dataview_left, 1, wxGROW ); diff --git a/src/common/datavcmn.cpp b/src/common/datavcmn.cpp index e3f74ccac1..7dfb742a16 100644 --- a/src/common/datavcmn.cpp +++ b/src/common/datavcmn.cpp @@ -189,11 +189,16 @@ wxDataViewListModel* wxDataViewCtrlBase::GetModel() return m_model; } -bool wxDataViewCtrlBase::AppendStringColumn( const wxString &label, size_t model_column ) +bool wxDataViewCtrlBase::AppendTextColumn( const wxString &label, size_t model_column ) { return AppendColumn( new wxDataViewColumn( label, new wxDataViewTextCell(), model_column ) ); } +bool wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, size_t model_column ) +{ + return AppendColumn( new wxDataViewColumn( label, new wxDataViewToggleCell(), model_column ) ); +} + bool wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col ) { m_cols.Append( (wxObject*) col ); diff --git a/src/gtk/dataview.cpp b/src/gtk/dataview.cpp index 9a45b2bbdd..7f9c937697 100644 --- a/src/gtk/dataview.cpp +++ b/src/gtk/dataview.cpp @@ -473,7 +473,16 @@ bool wxGtkDataViewListModelNotifier::RowAppended() bool wxGtkDataViewListModelNotifier::RowPrepended() { - return false; + GtkTreeIter iter; + iter.stamp = m_gtk_store->stamp; + iter.user_data = (gpointer) 0; + + GtkTreePath *path = gtk_tree_path_new (); + gtk_tree_path_append_index (path, (gint) 0); + gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter); + gtk_tree_path_free (path); + + return true; } bool wxGtkDataViewListModelNotifier::RowInserted( size_t before ) @@ -597,9 +606,93 @@ bool wxDataViewTextCell::GetValue( wxVariant &value ) } // --------------------------------------------------------- -// wxDataViewColumn +// wxDataViewToggleCell // --------------------------------------------------------- +extern "C" { +static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer, + gchar *path, gpointer user_data ); +} + +static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer, + gchar *path, gpointer user_data ) +{ + wxDataViewToggleCell *cell = (wxDataViewToggleCell*) user_data; + + // get old value + GValue gvalue = { 0, }; + g_value_init( &gvalue, G_TYPE_BOOLEAN ); + g_object_get_property( G_OBJECT(renderer), "active", &gvalue ); + bool tmp = g_value_get_boolean( &gvalue ); + g_value_unset( &gvalue ); + // invert it + tmp = !tmp; + + wxVariant value = tmp; + if (!cell->Validate( value )) + return; + + wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel(); + + GtkTreePath *gtk_path = gtk_tree_path_new_from_string( path ); + size_t model_row = (size_t)gtk_tree_path_get_indices (gtk_path)[0]; + gtk_tree_path_free( gtk_path ); + + size_t model_col = cell->GetOwner()->GetModelColumn(); + + model->SetValue( value, model_col, model_row ); + model->ValueChanged( model_col, model_row ); +} + +IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleCell, wxDataViewCell) + +wxDataViewToggleCell::wxDataViewToggleCell( const wxString &varianttype, + wxDataViewCellMode mode ) : + wxDataViewCell( varianttype, mode ) +{ + m_renderer = (void*) gtk_cell_renderer_toggle_new(); + + if (m_mode & wxDATAVIEW_CELL_EDITABLE) + { + GValue gvalue = { 0, }; + g_value_init( &gvalue, G_TYPE_BOOLEAN ); + g_value_set_boolean( &gvalue, true ); + g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue ); + g_value_unset( &gvalue ); + + g_signal_connect_after( m_renderer, "toggled", G_CALLBACK(wxGtkToggleRendererToggledCallback), this ); + } +} + +bool wxDataViewToggleCell::SetValue( const wxVariant &value ) +{ + bool tmp = value; + + GValue gvalue = { 0, }; + g_value_init( &gvalue, G_TYPE_BOOLEAN ); + g_value_set_boolean( &gvalue, tmp ); + g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue ); + g_value_unset( &gvalue ); + + return true; +} + +bool wxDataViewToggleCell::GetValue( wxVariant &value ) +{ + GValue gvalue = { 0, }; + g_value_init( &gvalue, G_TYPE_BOOLEAN ); + g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue ); + bool tmp = g_value_get_boolean( &gvalue ); + g_value_unset( &gvalue ); + + value = tmp; + + return true; +} + +// --------------------------------------------------------- +// wxDataViewColumn +// --------------------------------------------------------- extern "C" { static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,