+// ---------------------------------------------------------
+// wxDataViewCell
+// ---------------------------------------------------------
+
+IMPLEMENT_ABSTRACT_CLASS(wxDataViewCell, wxDataViewCellBase)
+
+wxDataViewCell::wxDataViewCell( const wxString &varianttype, wxDataViewCellMode mode ) :
+ wxDataViewCellBase( varianttype, mode )
+{
+ m_renderer = NULL;
+}
+
+// ---------------------------------------------------------
+// wxDataViewTextCell
+// ---------------------------------------------------------
+
+extern "C" {
+static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
+ gchar *arg1, gchar *arg2, gpointer user_data );
+}
+
+static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
+ gchar *arg1, gchar *arg2, gpointer user_data )
+{
+ wxDataViewTextCell *cell = (wxDataViewTextCell*) user_data;
+
+ wxString tmp = wxGTK_CONV_BACK( arg2 );
+ wxVariant value = tmp;
+ if (!cell->Validate( value ))
+ return;
+
+ wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
+
+ GtkTreePath *path = gtk_tree_path_new_from_string( arg1 );
+ size_t model_row = (size_t)gtk_tree_path_get_indices (path)[0];
+ gtk_tree_path_free( path );
+
+ size_t model_col = cell->GetOwner()->GetModelColumn();
+
+ model->SetValue( value, model_col, model_row );
+ model->ValueChanged( model_col, model_row );
+}
+
+IMPLEMENT_ABSTRACT_CLASS(wxDataViewTextCell, wxDataViewCell)
+
+wxDataViewTextCell::wxDataViewTextCell( const wxString &varianttype, wxDataViewCellMode mode ) :
+ wxDataViewCell( varianttype, mode )
+{
+ m_renderer = (void*) gtk_cell_renderer_text_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), "editable", &gvalue );
+ g_value_unset( &gvalue );
+
+ g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
+ }
+}
+
+bool wxDataViewTextCell::SetValue( const wxVariant &value )
+{
+ wxString tmp = value;
+
+ GValue gvalue = { 0, };
+ g_value_init( &gvalue, G_TYPE_STRING );
+ g_value_set_string( &gvalue, wxGTK_CONV( tmp ) );
+ g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
+ g_value_unset( &gvalue );
+
+ return true;
+}
+
+bool wxDataViewTextCell::GetValue( wxVariant &value )
+{
+ GValue gvalue = { 0, };
+ g_value_init( &gvalue, G_TYPE_STRING );
+ g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
+ wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &gvalue ) );
+ g_value_unset( &gvalue );
+
+ value = tmp;
+
+ return true;
+}
+
+// ---------------------------------------------------------
+// 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_ACTIVATABLE)
+ {
+ g_signal_connect_after( m_renderer, "toggled", G_CALLBACK(wxGtkToggleRendererToggledCallback), this );
+ }
+ else
+ {
+
+ GValue gvalue = { 0, };
+ g_value_init( &gvalue, G_TYPE_BOOLEAN );
+ g_value_set_boolean( &gvalue, false );
+ g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue );
+ g_value_unset( &gvalue );
+
+ GValue gvalue2 = { 0, };
+ g_value_init( &gvalue2, gtk_cell_renderer_mode_get_type() );
+ g_value_set_enum( &gvalue2, GTK_CELL_RENDERER_MODE_INERT );
+ g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue2 );
+ g_value_unset( &gvalue2 );
+
+ }
+}
+
+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;
+}
+
+// ---------------------------------------------------------
+// wxDataViewCustomCell
+// ---------------------------------------------------------
+
+class wxDataViewCtrlDC: public wxWindowDC
+{
+public:
+ wxDataViewCtrlDC( wxDataViewCtrl *window )
+ {
+ GtkWidget *widget = window->m_treeview;
+ // Set later
+ m_window = NULL;
+
+ m_context = window->GtkGetPangoDefaultContext();
+ m_layout = pango_layout_new( m_context );
+ m_fontdesc = pango_font_description_copy( widget->style->font_desc );
+
+ m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
+
+ // Set m_window later
+ // SetUpDC();
+ // m_owner = window;
+ }
+};
+
+// ---------------------------------------------------------
+// wxDataViewCustomCell
+// ---------------------------------------------------------
+
+IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomCell, wxDataViewCell)
+
+wxDataViewCustomCell::wxDataViewCustomCell( const wxString &varianttype,
+ wxDataViewCellMode mode, bool no_init ) :
+ wxDataViewCell( varianttype, mode )
+{
+ m_dc = NULL;
+
+ if (no_init)
+ m_renderer = NULL;
+ else
+ Init();
+}
+
+bool wxDataViewCustomCell::Init()
+{
+ GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new();
+ renderer->cell = this;
+
+ m_renderer = (void*) renderer;
+
+ if (m_mode & wxDATAVIEW_CELL_ACTIVATABLE)
+ {
+ GValue gvalue = { 0, };
+ g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
+ g_value_set_enum( &gvalue, GTK_CELL_RENDERER_MODE_ACTIVATABLE );
+ g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue );
+ g_value_unset( &gvalue );
+ }
+
+ return true;
+}
+
+wxDataViewCustomCell::~wxDataViewCustomCell()
+{
+ if (m_dc)
+ delete m_dc;
+}
+
+wxDC *wxDataViewCustomCell::GetDC()
+{
+ if (m_dc == NULL)
+ {
+ if (GetOwner() == NULL)
+ return NULL;
+ if (GetOwner()->GetOwner() == NULL)
+ return NULL;
+ m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() );
+ }
+
+ return m_dc;
+}
+
+// ---------------------------------------------------------
+// wxDataViewProgressCell
+// ---------------------------------------------------------
+
+IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressCell, wxDataViewCustomCell)
+
+wxDataViewProgressCell::wxDataViewProgressCell( const wxString &label,
+ const wxString &varianttype, wxDataViewCellMode mode ) :
+ wxDataViewCustomCell( varianttype, mode, true )
+{
+ m_label = label;
+ m_value = 0;
+
+#ifdef __WXGTK26__
+ if (!gtk_check_version(2,6,0))
+ {
+ m_renderer = (void*) gtk_cell_renderer_progress_new();
+
+ GValue gvalue = { 0, };
+ g_value_init( &gvalue, G_TYPE_STRING );
+ g_value_set_boolean( &gvalue, wxGTK_CONV(m_label) );
+ g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
+ g_value_unset( &gvalue );
+ }
+ else
+#endif
+ {
+ // Use custom cell code
+ wxDataViewCustomCell::Init();
+ }
+}
+
+wxDataViewProgressCell::~wxDataViewProgressCell()
+{
+}
+
+bool wxDataViewProgressCell::SetValue( const wxVariant &value )
+{
+#ifdef __WXGTK26__
+ if (!gtk_check_version(2,6,0))
+ {
+ gint tmp = (int) value;
+ GValue gvalue = { 0, };
+ g_value_init( &gvalue, G_TYPE_INT );
+ g_value_set_boolean( &gvalue, tmp );
+ g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue );
+ g_value_unset( &gvalue );
+ }
+ else
+#endif
+ {
+ m_value = (long) value;
+
+ if (m_value < 0) m_value = 0;
+ if (m_value > 100) m_value = 100;
+ }
+
+ return true;
+}
+
+bool wxDataViewProgressCell::Render( wxRect cell, wxDC *dc, int state )
+{
+ double pct = (double)m_value / 100.0;
+ wxRect bar = cell;
+ bar.width = (int)(cell.width * pct);
+ dc->SetPen( *wxTRANSPARENT_PEN );
+ dc->SetBrush( *wxBLUE_BRUSH );
+ dc->DrawRectangle( bar );
+
+ dc->SetBrush( *wxTRANSPARENT_BRUSH );
+ dc->SetPen( *wxBLACK_PEN );
+ dc->DrawRectangle( cell );
+
+ return true;
+}
+
+wxSize wxDataViewProgressCell::GetSize()
+{
+ return wxSize(40,12);
+}
+
+// ---------------------------------------------------------
+// wxDataViewDateCell
+// ---------------------------------------------------------
+
+class wxDataViewDateCellPopupTransient: public wxPopupTransientWindow
+{
+public:
+ wxDataViewDateCellPopupTransient( wxWindow* parent, wxDateTime *value,
+ wxDataViewListModel *model, size_t col, size_t row ) :
+ wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
+ {
+ m_model = model;
+ m_col = col;
+ m_row = row;
+ m_cal = new wxCalendarCtrl( this, -1, *value );
+ wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
+ sizer->Add( m_cal, 1, wxGROW );
+ SetSizer( sizer );
+ sizer->Fit( this );
+ }
+
+ virtual void OnDismiss()
+ {
+ }
+
+ void OnCalendar( wxCalendarEvent &event );
+
+ wxCalendarCtrl *m_cal;
+ wxDataViewListModel *m_model;
+ size_t m_col;
+ size_t m_row;
+
+private:
+ DECLARE_EVENT_TABLE()
+};
+
+BEGIN_EVENT_TABLE(wxDataViewDateCellPopupTransient,wxPopupTransientWindow)
+ EVT_CALENDAR( -1, wxDataViewDateCellPopupTransient::OnCalendar )
+END_EVENT_TABLE()
+
+void wxDataViewDateCellPopupTransient::OnCalendar( wxCalendarEvent &event )
+{
+ wxDateTime date = event.GetDate();
+ wxVariant value = date;
+ m_model->SetValue( value, m_col, m_row );
+ m_model->ValueChanged( m_col, m_row );
+ DismissAndNotify();
+}
+
+IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateCell, wxDataViewCustomCell)
+
+wxDataViewDateCell::wxDataViewDateCell( const wxString &varianttype,
+ wxDataViewCellMode mode ) :
+ wxDataViewCustomCell( varianttype, mode )
+{
+}
+
+bool wxDataViewDateCell::SetValue( const wxVariant &value )
+{
+ m_date = value.GetDateTime();
+
+ return true;
+}
+
+bool wxDataViewDateCell::Render( wxRect cell, wxDC *dc, int state )
+{
+ dc->SetFont( GetOwner()->GetOwner()->GetFont() );
+ wxString tmp = m_date.FormatDate();
+ dc->DrawText( tmp, cell.x, cell.y );
+
+ return true;
+}
+
+wxSize wxDataViewDateCell::GetSize()
+{
+ wxDataViewCtrl* view = GetOwner()->GetOwner();
+ wxString tmp = m_date.FormatDate();
+ wxCoord x,y,d;
+ view->GetTextExtent( tmp, &x, &y, &d );
+ return wxSize(x,y+d);
+}
+
+bool wxDataViewDateCell::Activate( wxRect cell, wxDataViewListModel *model, size_t col, size_t row )
+{
+ wxVariant variant;
+ model->GetValue( variant, col, row );
+ wxDateTime value = variant.GetDateTime();
+
+ wxDataViewDateCellPopupTransient *popup = new wxDataViewDateCellPopupTransient(
+ GetOwner()->GetOwner()->GetParent(), &value, model, col, row );
+ wxPoint pos = wxGetMousePosition();
+ popup->Move( pos );
+ popup->Layout();
+ popup->Popup( popup->m_cal );
+
+ return true;
+}
+
+// ---------------------------------------------------------
+// wxDataViewColumn
+// ---------------------------------------------------------
+
+extern "C" {
+static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
+ GtkCellRenderer *cell,
+ GtkTreeModel *model,
+ GtkTreeIter *iter,
+ gpointer data );
+}
+
+
+static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
+ GtkCellRenderer *renderer,
+ GtkTreeModel *model,
+ GtkTreeIter *iter,
+ gpointer data )
+{
+ g_return_if_fail (GTK_IS_WX_LIST_STORE (model));
+ GtkWxListStore *list_store = (GtkWxListStore *) model;
+
+ wxDataViewCell *cell = (wxDataViewCell*) data;
+
+ size_t model_row = (size_t) iter->user_data;
+
+ wxVariant value;
+ list_store->model->GetValue( value, cell->GetOwner()->GetModelColumn(), model_row );
+
+ if (value.GetType() != cell->GetVariantType())
+ wxLogError( wxT("Wrong type\n") );
+
+ cell->SetValue( value );
+}
+
+IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn, wxDataViewColumnBase)
+
+wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewCell *cell,
+ size_t model_column, int flags ) :
+ wxDataViewColumnBase( title, cell, model_column, flags )
+{
+ GtkCellRenderer *renderer = (GtkCellRenderer *) cell->GetGtkHandle();
+
+ GtkTreeViewColumn *column = gtk_tree_view_column_new();
+
+ gtk_tree_view_column_set_title( column, wxGTK_CONV(title) );
+
+ gtk_tree_view_column_pack_start( column, renderer, TRUE );
+
+ gtk_tree_view_column_set_cell_data_func( column, renderer,
+ wxGtkTreeCellDataFunc, (gpointer) cell, NULL );
+
+ m_column = (void*) column;
+}
+
+wxDataViewColumn::~wxDataViewColumn()
+{
+}
+
+void wxDataViewColumn::SetTitle( const wxString &title )
+{
+ wxDataViewColumnBase::SetTitle( title );
+
+ GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
+ gtk_tree_view_column_set_title( column, wxGTK_CONV(title) );
+}