added wxDataViewToggleCell
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@37706 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
parent
f081c944e8
commit
605c2c4ac6
@ -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 );
|
||||
|
@ -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
|
||||
// ---------------------------------------------------------
|
||||
|
@ -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 );
|
||||
|
@ -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 );
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user