]> git.saurik.com Git - wxWidgets.git/commitdiff
Add wxDataViewTextRendererAttr, blind noop under wxMac
authorRobert Roebling <robert@roebling.de>
Thu, 8 Nov 2007 22:51:58 +0000 (22:51 +0000)
committerRobert Roebling <robert@roebling.de>
Thu, 8 Nov 2007 22:51:58 +0000 (22:51 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@49730 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/dataview.h
include/wx/generic/dataview.h
include/wx/gtk/dataview.h
include/wx/mac/carbon/dataview.h
samples/dataview/dataview.cpp
samples/dataview/null.xpm
src/common/datavcmn.cpp
src/generic/datavgen.cpp
src/gtk/dataview.cpp

index 11092d77365475be02f20413f6cf1545a47a1814..022ff17bd52425a8cced69dfdfcdfa54cd495d79 100644 (file)
@@ -20,7 +20,6 @@
 #include "wx/textctrl.h"
 #include "wx/bitmap.h"
 #include "wx/variant.h"
-#include "wx/listctrl.h"
 #include "wx/dynarray.h"
 #include "wx/icon.h"
 
@@ -118,6 +117,42 @@ private:
 };
 
 
+
+// ----------------------------------------------------------------------------
+// wxDataViewItemAttr: a structure containing the visual attributes of an item
+// ----------------------------------------------------------------------------
+
+// TODO: this should be renamed to wxItemAttr or something general like this
+
+class WXDLLIMPEXP_ADV wxDataViewItemAttr
+{
+public:
+    // ctors
+    wxDataViewItemAttr() 
+    { 
+        m_bold = false;
+        m_italic = false;
+    }
+
+    // setters
+    void SetColour(const wxColour& colour) { m_colour = colour; }
+    void SetBold( bool set ) { m_bold = set; }
+    void SetItalic( bool set ) { m_italic = set; }
+    
+    // accessors
+    bool HasColour() const { return m_colour.Ok(); }
+    const wxColour& GetColour() const { return m_colour; }
+    
+    bool GetBold() const { return m_bold; }
+    bool GetItalic() const { return m_italic; }
+
+private:
+    wxColour m_colour;
+    bool     m_bold;
+    bool     m_italic;
+};
+
+
 // ---------------------------------------------------------
 // wxDataViewModel
 // ---------------------------------------------------------
@@ -143,6 +178,10 @@ public:
     virtual bool SetValue( const wxVariant &variant,
                            const wxDataViewItem &item, unsigned int col ) = 0;
 
+    // Get text attribute, return false of default attributes should be used
+    virtual bool GetAttr( const wxDataViewItem &WXUNUSED(item), unsigned int WXUNUSED(col), wxDataViewItemAttr &WXUNUSED(attr) )
+        { return false; }
+
     // define hierachy
     virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const = 0;
     virtual bool IsContainer( const wxDataViewItem &item ) const = 0;
@@ -197,6 +236,9 @@ public:
     virtual bool SetValue( const wxVariant &variant,
                            unsigned int row, unsigned int col ) = 0;
 
+    virtual bool GetAttr( unsigned int WXUNUSED(row), unsigned int WXUNUSED(col), wxDataViewItemAttr &WXUNUSED(attr) )
+        { return false; }
+        
     void RowPrepended();
     void RowInserted( unsigned int before );
     void RowAppended();
@@ -221,6 +263,7 @@ public:
                            const wxDataViewItem &item, unsigned int col ) const;
     virtual bool SetValue( const wxVariant &variant,
                            const wxDataViewItem &item, unsigned int col );
+    virtual bool GetAttr( const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr );
     virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const;
     virtual bool IsContainer( const wxDataViewItem &item ) const;
     virtual unsigned int GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const;
index efa5a11c7494e1ee877756fd04a94f8e6a0b7dd3..10e8bbf97044847de2fd922344695015d3a004f5 100644 (file)
@@ -78,11 +78,20 @@ public:
 
     // Create DC on request
     virtual wxDC *GetDC();
+    
+    void SetHasAttr( bool set )  { m_hasAttr = set; }
+    void SetAttr( const wxDataViewItemAttr &attr ) { m_attr = attr; }
+    bool GetWantsAttr() { return m_wantsAttr; }
 
 private:
     wxDC                        *m_dc;
     int                          m_align;
     wxDataViewCellMode           m_mode;
+    
+protected:
+    bool                         m_wantsAttr;
+    bool                         m_hasAttr;
+    wxDataViewItemAttr           m_attr;
 
 protected:
     DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRenderer)
@@ -128,13 +137,30 @@ public:
     virtual wxControl* CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value );
     virtual bool GetValueFromEditorCtrl( wxControl* editor, wxVariant &value );
         
-private:
+protected:
     wxString   m_text;
 
 protected:
     DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer)
 };
 
+// ---------------------------------------------------------
+// wxDataViewTextRendererAttr
+// ---------------------------------------------------------
+
+class WXDLLIMPEXP_ADV wxDataViewTextRendererAttr: public wxDataViewTextRenderer
+{
+public:
+    wxDataViewTextRendererAttr( const wxString &varianttype = wxT("string"),
+                            wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
+                            int align = wxDVR_DEFAULT_ALIGNMENT );
+
+    bool Render( wxRect cell, wxDC *dc, int state );
+        
+protected:
+    DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRendererAttr)
+};
+
 // ---------------------------------------------------------
 // wxDataViewBitmapRenderer
 // ---------------------------------------------------------
index 1b6ecf086cb35cd4efd263ddc41bc6925215a5be..c4167713a445c1871308bb54bf6b2d7f2cd71cb6 100644 (file)
@@ -43,6 +43,7 @@ public:
     // implementation
     GtkCellRenderer* GetGtkHandle() { return m_renderer; }
     void GtkInitHandlers();
+    virtual bool GtkHasAttributes() { return false; }
 
 protected:
     GtkCellRenderer   *m_renderer;
@@ -71,6 +72,24 @@ protected:
     DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer)
 };
     
+// --------------------------------------------------------- 
+// wxDataViewTextRendererAttr
+// --------------------------------------------------------- 
+
+class WXDLLIMPEXP_ADV wxDataViewTextRendererAttr: public wxDataViewTextRenderer
+{
+public:
+    wxDataViewTextRendererAttr( const wxString &varianttype = wxT("string"), 
+                            wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT,
+                            int align = wxDVR_DEFAULT_ALIGNMENT );
+
+    // implementation
+    bool GtkHasAttributes() { return true; }
+    
+protected:
+    DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRendererAttr)
+};
+    
 // --------------------------------------------------------- 
 // wxDataViewBitmapRenderer
 // --------------------------------------------------------- 
index b5a4cda39311e89c6f6959105820404d8bbcad04..67b17836cdad1662ded84e517bdcbf12c1770367 100644 (file)
@@ -232,6 +232,22 @@ private:
     DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer)
 };
 
+// ---------------------------------------------------------
+// wxDataViewTextRendererAttr
+// ---------------------------------------------------------
+
+class WXDLLIMPEXP_ADV wxDataViewTextRendererAttr: public wxDataViewTextRenderer
+{
+public:
+//
+// constructors / destructor
+//
+    wxDataViewTextRendererAttr(wxString const& varianttype=wxT("string"), wxDataViewCellMode mode=wxDATAVIEW_CELL_INERT, int align=wxDVR_DEFAULT_ALIGNMENT);
+
+private:
+    DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRendererAttr)
+};
+
 // ---------------------------------------------------------
 // wxDataViewBitmapRenderer
 // ---------------------------------------------------------
index f5cd7bd4bdf4a0afdbeef9d1b7b04922132eefec..62077163facd9ec47e0dbd9608fcb55f282e1e1b 100644 (file)
 #define DATAVIEW_DEFAULT_STYLE          (wxDV_MULTIPLE|wxDV_HORIZ_RULES|wxDV_VERT_RULES)
 
 
-// -------------------------------------
-// MySpinCtrlInPlaceRenderer
-// -------------------------------------
-
-class MySpinCtrlInPlaceRenderer: public wxDataViewCustomRenderer
-{
-public:
-    MySpinCtrlInPlaceRenderer() :
-        wxDataViewCustomRenderer( wxT("long"), wxDATAVIEW_CELL_EDITABLE ) { }
-    
-    
-    virtual bool HasEditorCtrl()
-        { 
-            return true; 
-        }
-    virtual wxControl* CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
-        { 
-            long l = value;
-            return new wxSpinCtrl( parent, wxID_ANY, wxEmptyString, 
-                    labelRect.GetTopLeft(), labelRect.GetSize(), -0, -1, 2010, l );
-        }
-    virtual bool GetValueFromEditorCtrl( wxControl* editor, wxVariant &value )
-        { 
-            wxSpinCtrl *sc = (wxSpinCtrl*) editor;
-            long l = sc->GetValue();
-            value = l;
-            return true;
-        }
-        
-    bool Render( wxRect rect, wxDC *dc, int WXUNUSED(state) )
-    {
-        wxString str;
-        str.Printf( wxT("%d"), (int) m_data );
-        dc->SetTextForeground( *wxBLACK );
-        dc->DrawText( str, rect.x, rect.y );
-        return true;
-    }
-    wxSize GetSize() const
-    {
-        return wxSize(80,16);
-    }
-    bool SetValue( const wxVariant &value )
-    {
-        m_data = value.GetLong();
-        return true;
-    }
-    bool GetValue( wxVariant &value ) const
-    {
-        value = m_data;
-        return true;
-    }
-    
-private:
-    long    m_data;
-};
-
-
-
 // -------------------------------------
 // MyMusicModel
 // -------------------------------------
@@ -444,14 +386,28 @@ public:
         {
             wxDataViewIconText data( "test", m_icon );
             variant << data;
-        }
-        else
+        } else
+        if (col==2)
         {
-            wxString str;
-            str.Printf( "row %d col %d", row, col );
-            variant = str;
+            if ((row % 2) == 1)
+                variant = "Blue";
+            else
+                variant = "Italic";
         }
     }
+    
+    virtual bool GetAttr( unsigned int row, unsigned int col, wxDataViewItemAttr &attr )
+    {
+        if (col != 2)
+            return false;
+            
+        if ((row % 2) == 1)
+            attr.SetColour( *wxBLUE );
+        else
+            attr.SetItalic( true );
+        
+        return true;            
+    }
 
     virtual bool SetValue( const wxVariant &variant, 
                            unsigned int row, unsigned int col )
@@ -651,7 +607,7 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int
     m_music_model = new MyMusicModel;
     m_musicCtrl->AssociateModel( m_music_model.get() );
 
-    wxDataViewColumn *col = m_musicCtrl->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200, 
+    /* wxDataViewColumn *col = */ m_musicCtrl->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200, 
                                      DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
 #if 0 
     // Call this and sorting is enabled
@@ -680,7 +636,10 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int
     
     m_listCtrl->AppendTextColumn( "editable string", 0, wxDATAVIEW_CELL_EDITABLE, 120 );
     m_listCtrl->AppendIconTextColumn( "icon", 1, wxDATAVIEW_CELL_INERT, 60 );
-    m_listCtrl->AppendTextColumn( "index", 2, wxDATAVIEW_CELL_INERT, 120 );
+    
+    wxDataViewTextRendererAttr *ra = new wxDataViewTextRendererAttr;
+    column = new wxDataViewColumn( "attributes", ra, 2 );
+    m_listCtrl->AppendColumn( column );
     
     data_sizer->Add( m_listCtrl, 2, wxGROW );
  
@@ -716,7 +675,7 @@ void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
 
 
 /* XPM */
-static char *small1_xpm[] = {
+static const char *small1_xpm[] = {
 /* columns rows colors chars-per-pixel */
 "16 16 6 1",
 ". c Black",
index abf74843b4187c286a186d010ca299a80ec9d0cf..e6706c1acad6811941590029459b57a46179d351 100644 (file)
@@ -1,5 +1,5 @@
 /* XPM */
-static char * null_xpm[] = {
+static const char * null_xpm[] = {
 "16 16 11 1",
 "      c None",
 ".     c #000000",
index 65c893265326812994d636e332ad2daa5f424102..b5f49859fcc5bf6950f6eaedf3c7e55b39f407d5 100644 (file)
@@ -386,6 +386,11 @@ bool wxDataViewIndexListModel::SetValue( const wxVariant &variant,
     return SetValue( variant, GetRow(item), col );
 }
 
+bool wxDataViewIndexListModel::GetAttr( const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr )
+{
+    return GetAttr( GetRow(item), col, attr );
+}
+
 wxDataViewItem wxDataViewIndexListModel::GetParent( const wxDataViewItem & WXUNUSED(item) ) const
 {
     return wxDataViewItem(0);
index a6c94b1e5d98e17d16abc0e915f54b6e99746a70..adcf1b358224923c1d259ecbcd082107627c95fc 100644 (file)
@@ -607,6 +607,8 @@ wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype,
     m_dc = NULL;
     m_align = align;
     m_mode = mode;
+    m_wantsAttr = false;
+    m_hasAttr = false;
 }
 
 wxDataViewRenderer::~wxDataViewRenderer()
@@ -713,6 +715,60 @@ wxSize wxDataViewTextRenderer::GetSize() const
     return wxSize(80,20);
 }
 
+// ---------------------------------------------------------
+// wxDataViewTextRendererAttr
+// ---------------------------------------------------------
+
+IMPLEMENT_CLASS(wxDataViewTextRendererAttr, wxDataViewTextRenderer)
+
+wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString &varianttype,
+                            wxDataViewCellMode mode, int align ) :
+    wxDataViewTextRenderer( varianttype, mode, align )
+{
+    m_wantsAttr = true;
+}
+
+bool wxDataViewTextRendererAttr::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
+{
+    wxFont font;
+    wxColour colour;
+
+    if (m_hasAttr)
+    {
+        if (m_attr.HasColour())
+        {
+            colour = dc->GetTextForeground();
+            dc->SetTextForeground( m_attr.GetColour() );
+        }
+        
+        if (m_attr.GetBold() || m_attr.GetItalic())
+        {
+             font = dc->GetFont();
+             wxFont myfont = font;
+             if (m_attr.GetBold())
+                 myfont.SetWeight( wxFONTWEIGHT_BOLD );
+             if (m_attr.GetItalic())
+                 myfont.SetStyle( wxFONTSTYLE_ITALIC );
+             dc->SetFont( myfont );
+        }
+    }
+    
+    dc->DrawText( m_text, cell.x, cell.y + ((cell.height - dc->GetCharHeight()) / 2));
+    
+    // restore dc
+    if (m_hasAttr)
+    {
+        if (m_attr.HasColour())
+            dc->SetTextForeground( colour );
+            
+        if (m_attr.GetBold() || m_attr.GetItalic())
+            dc->SetFont( font );
+    }
+    
+    return true;
+}
+        
+
 // ---------------------------------------------------------
 // wxDataViewBitmapRenderer
 // ---------------------------------------------------------
@@ -2363,6 +2419,15 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
 
             model->GetValue( value, dataitem, col->GetModelColumn());
             cell->SetValue( value );
+            
+            if (cell->GetWantsAttr())
+            {
+                wxDataViewItemAttr attr;
+                bool ret = model->GetAttr( dataitem, col->GetModelColumn(), attr );
+                if (ret)
+                    cell->SetAttr( attr );
+                cell->SetHasAttr( ret );
+            }
 
             // update the y offset
             cell_rect.y = item * m_lineHeight;
@@ -3788,7 +3853,7 @@ wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const
 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column )
 {
     wxDataViewColumnList::compatibility_iterator  ret = m_cols.Find( column );
-    if (ret == NULL)
+    if (!ret)
         return false;
 
     m_cols.Erase(ret);
index f7d910d2de85e73b2d241f9816432236c05a370f..169dd8c446b804f45357309b7e94217716dda6d8 100644 (file)
@@ -1475,6 +1475,18 @@ void wxDataViewTextRenderer::SetAlignment( int align )
     g_value_unset( &gvalue );
 }
 
+// --------------------------------------------------------- 
+// wxDataViewTextRendererAttr
+// --------------------------------------------------------- 
+
+IMPLEMENT_CLASS(wxDataViewTextRendererAttr,wxDataViewTextRenderer)
+
+wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString &varianttype, 
+                            wxDataViewCellMode mode, int align ) :
+   wxDataViewTextRenderer( varianttype, mode, align )
+{
+}
+    
 // ---------------------------------------------------------
 // wxDataViewBitmapRenderer
 // ---------------------------------------------------------
@@ -2112,10 +2124,82 @@ static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
 
     cell->SetValue( value );
 
-#if 0
-    wxListItemAttr attr;
-    wx_model->GetAttr( item, attr, cell->GetOwner()->GetModelColumn() );
+    if (cell->GtkHasAttributes())
+    {
+        wxDataViewItemAttr attr;
+        bool colour_set = false;
+        bool style_set = false;
+        bool weight_set = false;
+        
+        if (wx_model->GetAttr( item, cell->GetOwner()->GetModelColumn(), attr ))
+        {
+            // this must be a GtkCellRendererText
+            wxColour colour = attr.GetColour();
+            if (colour.IsOk())
+            {
+                const GdkColor * const gcol = colour.GetColor();
 
+                GValue gvalue = { 0, };
+                g_value_init( &gvalue, GDK_TYPE_COLOR );
+                g_value_set_boxed( &gvalue, gcol );
+                g_object_set_property( G_OBJECT(renderer), "foreground_gdk", &gvalue );
+                g_value_unset( &gvalue );
+                
+                colour_set = true;
+            }
+            
+            if (attr.GetItalic())
+            {
+                GValue gvalue = { 0, };
+                g_value_init( &gvalue, PANGO_TYPE_STYLE );
+                g_value_set_enum( &gvalue, PANGO_STYLE_ITALIC );
+                g_object_set_property( G_OBJECT(renderer), "style", &gvalue );
+                g_value_unset( &gvalue );
+                
+                style_set = true;
+            }
+            
+            if (attr.GetBold())
+            {
+                GValue gvalue = { 0, };
+                g_value_init( &gvalue, PANGO_TYPE_WEIGHT );
+                g_value_set_enum( &gvalue, PANGO_WEIGHT_BOLD );
+                g_object_set_property( G_OBJECT(renderer), "weight", &gvalue );
+                g_value_unset( &gvalue );
+                
+                weight_set = true;
+            }
+        }
+        
+        if (!style_set)
+        {
+            GValue gvalue = { 0, };
+            g_value_init( &gvalue, G_TYPE_BOOLEAN );
+            g_value_set_boolean( &gvalue, FALSE );
+            g_object_set_property( G_OBJECT(renderer), "style-set", &gvalue );
+            g_value_unset( &gvalue );
+        }
+        
+        if (!weight_set)
+        {
+            GValue gvalue = { 0, };
+            g_value_init( &gvalue, G_TYPE_BOOLEAN );
+            g_value_set_boolean( &gvalue, FALSE );
+            g_object_set_property( G_OBJECT(renderer), "weight-set", &gvalue );
+            g_value_unset( &gvalue );
+        }
+        
+        if (!colour_set)
+        {
+            GValue gvalue = { 0, };
+            g_value_init( &gvalue, G_TYPE_BOOLEAN );
+            g_value_set_boolean( &gvalue, FALSE );
+            g_object_set_property( G_OBJECT(renderer), "foreground-set", &gvalue );
+            g_value_unset( &gvalue );
+        }
+    }
+
+#if 0
     if (attr.HasBackgroundColour())
     {
         wxColour colour = attr.GetBackgroundColour();