]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/dataview.cpp
1. don't assert if we're passed an empty defaultDir and full path in
[wxWidgets.git] / src / gtk / dataview.cpp
index 445ba528523311f9479c72e3c016230c3893551b..37a761a59fc51321ba28727ce359592214a9f807 100644 (file)
@@ -251,6 +251,10 @@ wxgtk_list_store_get_column_type (GtkTreeModel *tree_model,
 
     if (wxtype == wxT("string"))
         gtype = G_TYPE_STRING;
+    else
+    {
+        wxFAIL_MSG( _T("non-string columns not supported yet") );
+    }
 
     return gtype;
 }
@@ -305,33 +309,17 @@ wxgtk_list_store_get_value (GtkTreeModel *tree_model,
     {
         wxVariant variant;
         g_value_init( value, G_TYPE_STRING );
-        model->GetValue( variant, (unsigned int) column, (unsigned int) iter->user_data );
-        g_value_set_string( value, wxGTK_CONV(variant.GetString()) );
+        model->GetValue( variant,
+                         (unsigned int) column,
+                         (unsigned int) iter->user_data );
+
+        // FIXME: we should support different encodings here
+        g_value_set_string( value, wxGTK_CONV_SYS(variant.GetString()) );
     }
     else
     {
+        wxFAIL_MSG( _T("non-string columns not supported yet") );
     }
-
-#if 0
-  GtkTreeDataList *list;
-  gint tmp_column = column;
-
-  g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
-  g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
-
-  list = G_SLIST (iter->user_data)->data;
-
-  while (tmp_column-- > 0 && list)
-    list = list->next;
-
-  if (list == NULL)
-    g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
-  else
-    _gtk_tree_data_list_node_to_value (list,
-                                       GTK_LIST_STORE (tree_model)->column_headers[column],
-                                       value);
-#endif
-
 }
 
 static gboolean
@@ -946,7 +934,7 @@ bool wxDataViewTextRenderer::SetValue( const wxVariant &value )
 
     GValue gvalue = { 0, };
     g_value_init( &gvalue, G_TYPE_STRING );
-    g_value_set_string( &gvalue, wxGTK_CONV( tmp ) );
+    g_value_set_string( &gvalue, wxGTK_CONV_FONT( tmp, GetOwner()->GetOwner()->GetFont() ) );
     g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
     g_value_unset( &gvalue );
 
@@ -1221,7 +1209,9 @@ wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
 
         GValue gvalue = { 0, };
         g_value_init( &gvalue, G_TYPE_STRING );
-        g_value_set_boolean( &gvalue, wxGTK_CONV(m_label) );
+
+        // FIXME: font encoding support
+        g_value_set_string( &gvalue, wxGTK_CONV_SYS(m_label) );
         g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
         g_value_unset( &gvalue );
     }
@@ -1242,10 +1232,10 @@ bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
 #ifdef __WXGTK26__
     if (!gtk_check_version(2,6,0))
     {
-        gint tmp = (int) value;
+        gint tmp = (long) value;
         GValue gvalue = { 0, };
         g_value_init( &gvalue, G_TYPE_INT );
-        g_value_set_boolean( &gvalue, tmp );
+        g_value_set_int( &gvalue, tmp );
         g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue );
         g_value_unset( &gvalue );
     }
@@ -1384,6 +1374,27 @@ bool wxDataViewDateRenderer::Activate( wxRect cell, wxDataViewListModel *model,
 // wxDataViewColumn
 // ---------------------------------------------------------
 
+
+static gboolean
+gtk_dataview_header_button_press_callback( GtkWidget *widget,
+                                           GdkEventButton *gdk_event,
+                                           wxDataViewColumn *column )
+{
+    if (gdk_event->type != GDK_BUTTON_PRESS)
+        return TRUE;
+        
+    if (gdk_event->button == 1)
+    {
+        wxDataViewCtrl *dv = column->GetOwner();
+        wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, dv->GetId() );
+        event.SetDataViewColumn( column );
+        event.SetModel( dv->GetModel() );
+        dv->GetEventHandler()->ProcessEvent( event );
+    }
+    
+    return TRUE;
+}
+
 extern "C" {
 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
                             GtkCellRenderer *cell,
@@ -1423,11 +1434,16 @@ wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *c
     int width, int flags ) :
     wxDataViewColumnBase( title, cell, model_column, width, flags )
 {
+    m_isConnected = false;
+
     GtkCellRenderer *renderer = (GtkCellRenderer *) cell->GetGtkHandle();
 
     GtkTreeViewColumn *column = gtk_tree_view_column_new();
-
-    gtk_tree_view_column_set_title( column, wxGTK_CONV(title) );
+    m_column = (void*) column;
+    
+    gtk_tree_view_column_set_clickable( column, true );
+    
+    SetTitle( title );
 
     if (flags & wxDATAVIEW_COL_RESIZABLE)
         gtk_tree_view_column_set_resizable( column, true );
@@ -1437,29 +1453,179 @@ wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *c
         gtk_tree_view_column_set_sort_indicator( column, true );
 
     if (width > 0)
-    {
         gtk_tree_view_column_set_fixed_width( column, width );
-        gtk_tree_view_column_set_sizing( column, GTK_TREE_VIEW_COLUMN_FIXED );
-    }
+    else
+        gtk_tree_view_column_set_fixed_width( column, 70 );  // FIXME
+        
+    gtk_tree_view_column_set_sizing( column, GTK_TREE_VIEW_COLUMN_FIXED );
 
     gtk_tree_view_column_pack_end( column, renderer, FALSE );
 
     gtk_tree_view_column_set_cell_data_func( column, renderer,
         wxGtkTreeCellDataFunc, (gpointer) cell, NULL );
 
+}
+
+wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell, unsigned int model_column,
+    int width, int flags ) :
+    wxDataViewColumnBase( bitmap, cell, model_column, width, flags )
+{
+    m_isConnected = false;
+    
+    GtkCellRenderer *renderer = (GtkCellRenderer *) cell->GetGtkHandle();
+
+    GtkTreeViewColumn *column = gtk_tree_view_column_new();
     m_column = (void*) column;
+
+    SetBitmap( bitmap );
+
+    if (flags & wxDATAVIEW_COL_RESIZABLE)
+        gtk_tree_view_column_set_resizable( column, true );
+    if (flags & wxDATAVIEW_COL_HIDDEN)
+        gtk_tree_view_column_set_visible( column, false );
+    if (flags & wxDATAVIEW_COL_SORTABLE)
+        gtk_tree_view_column_set_sort_indicator( column, true );
+
+    if (width > 0)
+        gtk_tree_view_column_set_fixed_width( column, width );
+    else
+        gtk_tree_view_column_set_fixed_width( column, 70 );  // FIXME
+        
+    gtk_tree_view_column_set_sizing( column, GTK_TREE_VIEW_COLUMN_FIXED );
+
+    gtk_tree_view_column_pack_end( column, renderer, FALSE );
+
+    gtk_tree_view_column_set_cell_data_func( column, renderer,
+        wxGtkTreeCellDataFunc, (gpointer) cell, NULL );
 }
 
 wxDataViewColumn::~wxDataViewColumn()
 {
 }
 
+void wxDataViewColumn::OnInternalIdle()
+{
+    if (m_isConnected)
+        return;
+        
+    if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview))
+    {
+        GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
+        if (column->button)
+        {
+            g_signal_connect(column->button, "button_press_event",
+                      G_CALLBACK (gtk_dataview_header_button_press_callback), this);
+    
+            m_isConnected = true;
+        }
+    }
+}
+
+void wxDataViewColumn::SetOwner( wxDataViewCtrl *owner )
+{
+    wxDataViewColumnBase::SetOwner( owner );
+    
+    GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
+    
+    gtk_tree_view_column_set_title( column, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
+}
+
 void wxDataViewColumn::SetTitle( const wxString &title )
 {
     wxDataViewColumnBase::SetTitle( title );
 
     GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
-    gtk_tree_view_column_set_title( column, wxGTK_CONV(title) );
+    
+    if (m_isConnected)
+    {
+        // disconnect before column->button gets recreated
+        g_signal_handlers_disconnect_by_func( column->button, 
+                      (void*) gtk_dataview_header_button_press_callback, this);
+                      
+        m_isConnected = false;
+    }
+
+    // FIXME: can it really happen that we don't have the owner here??
+    wxDataViewCtrl *ctrl = GetOwner();
+    gtk_tree_view_column_set_title( column, ctrl ? wxGTK_CONV_FONT(title, ctrl->GetFont())
+                                                 : wxGTK_CONV_SYS(title) );
+
+    gtk_tree_view_column_set_widget( column, NULL );
+}
+
+void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap )
+{
+    wxDataViewColumnBase::SetBitmap( bitmap );
+
+    GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
+    if (bitmap.Ok())
+    {
+        GtkImage *gtk_image = GTK_IMAGE( gtk_image_new() );
+        
+        GdkBitmap *mask = (GdkBitmap *) NULL;
+        if (bitmap.GetMask())
+            mask = bitmap.GetMask()->GetBitmap();
+
+        if (bitmap.HasPixbuf())
+        {
+            gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image),
+                                      bitmap.GetPixbuf());
+        }
+        else
+        {
+            gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image),
+                                      bitmap.GetPixmap(), mask);
+        }
+        gtk_widget_show( GTK_WIDGET(gtk_image) );
+        
+        gtk_tree_view_column_set_widget( column, GTK_WIDGET(gtk_image) );
+    }
+    else
+    {
+        gtk_tree_view_column_set_widget( column, NULL );
+    }
+}
+
+void wxDataViewColumn::SetAlignment( wxAlignment align )
+{
+    GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
+    
+    gfloat xalign = 0.0;
+    if (align == wxALIGN_RIGHT)
+        xalign = 1.0;
+    if (align == wxALIGN_CENTER)
+        xalign = 0.5;
+        
+    gtk_tree_view_column_set_alignment( column, xalign );    
+}
+
+void wxDataViewColumn::SetSortable( bool sortable )
+{
+    GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
+    gtk_tree_view_column_set_sort_indicator( column, sortable );
+}
+
+bool wxDataViewColumn::GetSortable()
+{
+    GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
+    return gtk_tree_view_column_get_sort_indicator( column );
+}
+
+void wxDataViewColumn::SetSortOrder( bool ascending )
+{
+    GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
+    
+    if (ascending)
+        gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING );
+    else
+        gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING );
+}
+
+bool wxDataViewColumn::IsSortOrderAscending()
+{
+    GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
+    
+    return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING);
 }
 
 int wxDataViewColumn::GetWidth()
@@ -1484,12 +1650,26 @@ int wxDataViewColumn::GetFixedWidth()
 static void
 wxdataview_selection_changed_callback( GtkTreeSelection* selection, wxDataViewCtrl *dv )
 {
+    if (!GTK_WIDGET_REALIZED(dv->m_widget))
+        return;
+    
     wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ROW_SELECTED, dv->GetId() );
     event.SetRow( dv->GetSelection() );
     event.SetModel( dv->GetModel() );
     dv->GetEventHandler()->ProcessEvent( event );
 }
 
+static void
+wxdataview_row_activated_callback( GtkTreeView* treeview, GtkTreePath *path, 
+                                   GtkTreeViewColumn *column, wxDataViewCtrl *dv )
+{
+    wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ROW_ACTIVATED, dv->GetId() );
+    unsigned int row = (unsigned int)gtk_tree_path_get_indices (path)[0];
+    event.SetRow( row );
+    event.SetModel( dv->GetModel() );
+    dv->GetEventHandler()->ProcessEvent( event );
+}
+
 //-----------------------------------------------------------------------------
 // wxDataViewCtrl
 //-----------------------------------------------------------------------------
@@ -1530,6 +1710,11 @@ bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
     m_treeview = gtk_tree_view_new();
     gtk_container_add (GTK_CONTAINER (m_widget), m_treeview);
 
+#ifdef __WXGTK26__
+    if (!gtk_check_version(2,6,0))
+        gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), TRUE );
+#endif
+
     if (style & wxDV_MULTIPLE)
     {
         GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
@@ -1542,15 +1727,30 @@ bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
 
     m_parent->DoAddChild( this );
 
+    PostCreation(size);
+
     GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
     g_signal_connect_after (selection, "changed",
                             G_CALLBACK (wxdataview_selection_changed_callback), this);
-
-    PostCreation(size);
+    g_signal_connect_after (m_treeview, "row_activated",
+                            G_CALLBACK (wxdataview_row_activated_callback), this);
 
     return true;
 }
 
+void wxDataViewCtrl::OnInternalIdle()
+{
+    wxWindow::OnInternalIdle();
+    
+    unsigned int cols = GetNumberOfColumns();
+    unsigned int i;
+    for (i = 0; i < cols; i++)
+    {
+        wxDataViewColumn *col = GetColumn( i );
+        col->OnInternalIdle();
+    }
+}
+
 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel *model )
 {
     if (!wxDataViewCtrlBase::AssociateModel( model ))
@@ -1719,6 +1919,13 @@ int wxDataViewCtrl::GetSelections(wxArrayInt& aSelections) const
     return 0;
 }
 
+// static
+wxVisualAttributes
+wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
+{
+    return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new);
+}
+
 
 #endif
     // !wxUSE_GENERICDATAVIEWCTRL