]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/datavgen.cpp
Implement icon text column using native GTK renderers in wxDVC.
[wxWidgets.git] / src / generic / datavgen.cpp
index 7887465b1c405edc8d937818c7d03b2d5e8edd1c..bdeadffd368cf888284931def78b3056723feea9 100644 (file)
@@ -612,8 +612,6 @@ wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype,
     m_dc = NULL;
     m_align = align;
     m_mode = mode;
-    m_wantsAttr = false;
-    m_hasAttr = false;
 }
 
 wxDataViewRenderer::~wxDataViewRenderer()
@@ -622,6 +620,54 @@ wxDataViewRenderer::~wxDataViewRenderer()
         delete m_dc;
 }
 
+bool
+wxDataViewRenderer::RenderWithAttr(wxDC& dc,
+                                   const wxRect& cell_rect,
+                                   int align,
+                                   const wxDataViewItemAttr *WXUNUSED(attr),
+                                   int state)
+{
+    // adjust the rectangle ourselves to account for the alignment
+
+    wxRect item_rect = cell_rect;
+    if ( align )
+    {
+        const wxSize size = GetSize();
+
+        // take alignment into account only if there is enough space, otherwise
+        // show as much contents as possible
+        //
+        // notice that many existing renderers (e.g. wxDataViewSpinRenderer)
+        // return hard-coded size which can be more than they need and if we
+        // trusted their GetSize() we'd draw the text out of cell bounds
+        // entirely
+
+        if ( size.x < cell_rect.width )
+        {
+            if (align & wxALIGN_CENTER_HORIZONTAL)
+                item_rect.x += (cell_rect.width - size.x)/2;
+            else if (align & wxALIGN_RIGHT)
+                item_rect.x += cell_rect.width - size.x;
+            // else: wxALIGN_LEFT is the default
+
+            item_rect.width = size.x;
+        }
+
+        if ( size.y < cell_rect.height )
+        {
+            if (align & wxALIGN_CENTER_VERTICAL)
+                item_rect.y += (cell_rect.height - size.y)/2;
+            else if (align & wxALIGN_BOTTOM)
+                item_rect.y += cell_rect.height - size.y;
+            // else: wxALIGN_TOP is the default
+
+            item_rect.height = size.y;
+        }
+    }
+
+    return Render(item_rect, &dc, state);
+}
+
 wxDC *wxDataViewRenderer::GetDC()
 {
     if (m_dc == NULL)
@@ -671,17 +717,42 @@ wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
 {
 }
 
-void wxDataViewCustomRenderer::RenderText( const wxString &text, int xoffset,
-                                           wxRect cell, wxDC *dc, int state )
+void
+wxDataViewCustomRenderer::RenderText(wxDC& dc,
+                                     const wxRect& rect,
+                                     int align,
+                                     const wxString& text,
+                                     const wxDataViewItemAttr *attr,
+                                     int state,
+                                     int xoffset)
 {
-    wxDataViewCtrl *view = GetOwner()->GetOwner();
-    wxColour col = (state & wxDATAVIEW_CELL_SELECTED) ?
-                        wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) :
-                        view->GetForegroundColour();
-    dc->SetTextForeground(col);
-    dc->DrawText( text,
-                  cell.x + xoffset,
-                  cell.y + ((cell.height - dc->GetCharHeight()) / 2));
+    wxColour col;
+    if ( attr && attr->HasColour() )
+        col = attr->GetColour();
+    else if ( state & wxDATAVIEW_CELL_SELECTED )
+        col = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
+    else // use default foreground
+        col = GetOwner()->GetOwner()->GetForegroundColour();
+
+    wxDCTextColourChanger changeFg(dc, col);
+
+    wxDCFontChanger changeFont(dc);
+    if ( attr && attr->HasFont() )
+    {
+        wxFont font(dc.GetFont());
+        if ( attr->GetBold() )
+            font.MakeBold();
+        if ( attr->GetItalic() )
+            font.MakeItalic();
+
+        changeFont.Set(font);
+    }
+
+    wxRect rectText = rect;
+    rectText.x += xoffset;
+    rectText.width -= xoffset;
+
+    dc.DrawLabel(text, rectText, align);
 }
 
 // ---------------------------------------------------------
@@ -734,9 +805,14 @@ bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl *editor, wxVarian
     return true;
 }
 
-bool wxDataViewTextRenderer::Render( wxRect cell, wxDC *dc, int state )
+bool
+wxDataViewTextRenderer::RenderWithAttr(wxDC& dc,
+                                       const wxRect& rect,
+                                       int align,
+                                       const wxDataViewItemAttr *attr,
+                                       int state)
 {
-    RenderText( m_text, 0, cell, dc, state );
+    RenderText(dc, rect, align, m_text, attr, state);
     return true;
 }
 
@@ -748,60 +824,6 @@ wxSize wxDataViewTextRenderer::GetSize() const
     return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE);
 }
 
-// ---------------------------------------------------------
-// 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
 // ---------------------------------------------------------
@@ -923,10 +945,6 @@ wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
     m_value = 0;
 }
 
-wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
-{
-}
-
 bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
 {
     m_value = (long) value;
@@ -943,18 +961,24 @@ bool wxDataViewProgressRenderer::GetValue( wxVariant &value ) const
     return true;
 }
 
-bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) )
+bool wxDataViewProgressRenderer::RenderWithAttr(wxDC& dc,
+                                                const wxRect& rect,
+                                                int WXUNUSED(align),
+                                                const wxDataViewItemAttr *attr,
+                                                int WXUNUSED(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 );
+    // deflat the rect to leave a small border between bars in adjacent rows
+    wxRect bar = rect.Deflate(0, 1);
 
-    dc->SetBrush( *wxTRANSPARENT_BRUSH );
-    dc->SetPen( *wxBLACK_PEN );
-    dc->DrawRectangle( cell );
+    dc.SetBrush( *wxTRANSPARENT_BRUSH );
+    dc.SetPen( *wxBLACK_PEN );
+    dc.DrawRectangle( bar );
+
+    bar.width = (int)(bar.width * m_value / 100.);
+    dc.SetPen( *wxTRANSPARENT_PEN );
+    dc.SetBrush( attr && attr->HasColour() ? wxBrush(attr->GetColour())
+                                           : *wxBLUE_BRUSH );
+    dc.DrawRectangle( bar );
 
     return true;
 }
@@ -1091,10 +1115,6 @@ const wxString &varianttype, wxDataViewCellMode mode, int align ) :
     SetAlignment(align);
 }
 
-wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
-{
-}
-
 bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value )
 {
     m_value << value;
@@ -1106,17 +1126,23 @@ bool wxDataViewIconTextRenderer::GetValue( wxVariant& WXUNUSED(value) ) const
     return false;
 }
 
-bool wxDataViewIconTextRenderer::Render( wxRect cell, wxDC *dc, int state )
+bool
+wxDataViewIconTextRenderer::RenderWithAttr(wxDC& dc,
+                                           const wxRect& rect,
+                                           int align,
+                                           const wxDataViewItemAttr *attr,
+                                           int state)
 {
     int xoffset = 0;
-    const wxIcon &icon = m_value.GetIcon();
-    if (icon.IsOk())
+
+    const wxIcon& icon = m_value.GetIcon();
+    if ( icon.IsOk() )
     {
-        dc->DrawIcon( icon, cell.x, cell.y + ((cell.height - icon.GetHeight()) / 2));
-        xoffset =  icon.GetWidth()+4;
+        dc.DrawIcon(icon, rect.x, rect.y + (rect.height - icon.GetHeight())/2);
+        xoffset = icon.GetWidth()+4;
     }
 
-    RenderText( m_value.GetText(), xoffset, cell, dc, state );
+    RenderText(dc, rect, align, m_value.GetText(), attr, state, xoffset);
 
     return true;
 }
@@ -1602,43 +1628,15 @@ wxBitmap wxDataViewMainWindow::CreateItemBitmap( unsigned int row, int &indent )
         model->GetValue( value, item, column->GetModelColumn());
         cell->SetValue( value );
 
-        if (cell->GetWantsAttr())
-        {
-                wxDataViewItemAttr attr;
-                bool ret = model->GetAttr( item, column->GetModelColumn(), attr );
-                if (ret)
-                    cell->SetAttr( attr );
-                cell->SetHasAttr( ret );
-        }
-
-        wxSize size = cell->GetSize();
-        size.x = wxMin( 2*PADDING_RIGHTLEFT + size.x, width );
-        size.y = height;
-        wxRect item_rect(x, 0, size.x, size.y);
-
-        int align = cell->CalculateAlignment();
-        // horizontal alignment:
-        item_rect.x = x;
-        if (align & wxALIGN_CENTER_HORIZONTAL)
-            item_rect.x = x + (width / 2) - (size.x / 2);
-        else if (align & wxALIGN_RIGHT)
-            item_rect.x = x + width - size.x;
-        // else: wxALIGN_LEFT is the default
-
-        // vertical alignment:
-        item_rect.y = 0;
-        if (align & wxALIGN_CENTER_VERTICAL)
-            item_rect.y = (height / 2) - (size.y / 2);
-        else if (align & wxALIGN_BOTTOM)
-            item_rect.y = height - size.y;
-        // else: wxALIGN_TOP is the default
-
-        // add padding
-        item_rect.x += PADDING_RIGHTLEFT;
-        item_rect.width = size.x - 2 * PADDING_RIGHTLEFT;
+        wxRect item_rect(x, 0, width, height);
+        item_rect.Deflate(PADDING_RIGHTLEFT, 0);
 
         // dc.SetClippingRegion( item_rect );
-        cell->Render( item_rect, &dc, 0 );
+        wxDataViewItemAttr attr;
+        const bool
+            hasAttr = model->GetAttr(item, column->GetModelColumn(), attr);
+        cell->RenderWithAttr(dc, item_rect, cell->CalculateAlignment(),
+                                hasAttr ? &attr : NULL, 0);
         // dc.DestroyClippingRegion();
 
         x += width;
@@ -1656,11 +1654,9 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
     wxAutoBufferedPaintDC dc( this );
 
 #ifdef __WXMSW__
+    dc.SetBrush(GetOwner()->GetBackgroundColour());
     dc.SetPen( *wxTRANSPARENT_PEN );
-    dc.SetBrush( wxBrush( GetBackgroundColour()) );
-    dc.SetBrush( *wxWHITE_BRUSH );
-    wxSize size( GetClientSize() );
-    dc.DrawRectangle( 0,0,size.x,size.y );
+    dc.DrawRectangle(GetClientSize());
 #endif
 
     // prepare the DC
@@ -1832,15 +1828,6 @@ 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 cell_rect
             cell_rect.y = GetLineStart( item );
             cell_rect.height = GetLineHeight( item );
@@ -1891,40 +1878,12 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
                 wxDELETE(node);
             }
 
-            // cannot be bigger than allocated space
-            wxSize size = cell->GetSize();
-
-            // Because of the tree structure indent, here we should minus the width
-            // of the cell for drawing
-            size.x = wxMin( size.x + 2*PADDING_RIGHTLEFT, cell_rect.width - indent );
-            // size.y = wxMin( size.y, cell_rect.height );
-            size.y = cell_rect.height;
-
-            wxRect item_rect(cell_rect.GetTopLeft(), size);
-            int align = cell->CalculateAlignment();
+            wxRect item_rect = cell_rect;
+            item_rect.Deflate(PADDING_RIGHTLEFT, 0);
 
-            // horizontal alignment:
-            item_rect.x = cell_rect.x;
-            if (align & wxALIGN_CENTER_HORIZONTAL)
-                item_rect.x = cell_rect.x + (cell_rect.width / 2) - (size.x / 2);
-            else if (align & wxALIGN_RIGHT)
-                item_rect.x = cell_rect.x + cell_rect.width - size.x;
-            // else: wxALIGN_LEFT is the default
-
-            // vertical alignment:
-            item_rect.y = cell_rect.y;
-            if (align & wxALIGN_CENTER_VERTICAL)
-                item_rect.y = cell_rect.y + (cell_rect.height / 2) - (size.y / 2);
-            else if (align & wxALIGN_BOTTOM)
-                item_rect.y = cell_rect.y + cell_rect.height - size.y;
-            // else: wxALIGN_TOP is the default
-
-            // add padding
-            item_rect.x += PADDING_RIGHTLEFT;
-            item_rect.width = size.x - 2 * PADDING_RIGHTLEFT;
-
-            // Here we add the tree indent
+            // account for the tree indent (harmless if we're not indented)
             item_rect.x += indent;
+            item_rect.width -= indent;
 
             int state = 0;
             if (m_hasFocus && (m_selection.Index(item) != wxNOT_FOUND))
@@ -1937,9 +1896,13 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
             //       respect the given wxRect's top & bottom coords, eventually
             //       violating only the left & right coords - however the user can
             //       make its own renderer and thus we cannot be sure of that.
-            dc.SetClippingRegion( item_rect );
-            cell->Render( item_rect, &dc, state );
-            dc.DestroyClippingRegion();
+            wxDCClipper clip(dc, item_rect);
+
+            wxDataViewItemAttr attr;
+            const bool
+                hasAttr = model->GetAttr(dataitem, col->GetModelColumn(), attr);
+            cell->RenderWithAttr(dc, item_rect, cell->CalculateAlignment(),
+                                    hasAttr ? &attr : NULL, state);
         }
 
         cell_rect.x += cell_rect.width;
@@ -2081,7 +2044,7 @@ bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem& parent,
         wxDataViewVirtualListModel *list_model =
             (wxDataViewVirtualListModel*) GetOwner()->GetModel();
         m_count = list_model->GetCount();
-        
+
         if( m_currentRow > GetRowCount() )
             m_currentRow = m_count - 1;
 
@@ -3192,7 +3155,7 @@ wxRect wxDataViewMainWindow::GetItemRect( const wxDataViewItem & item,
     {
         wxDataViewTreeNode* node = GetTreeNodeByRow(row);
         indent = GetOwner()->GetIndent() * node->GetIndentLevel();
-        indent = indent + m_lineHeight;        // use m_lineHeight as the width of the expander
+        indent = indent + m_lineHeight; // use m_lineHeight as the width of the expander
 
         if(!node->HasChildren())
             delete node;
@@ -3215,7 +3178,7 @@ int wxDataViewMainWindow::RecalculateCount()
     {
         wxDataViewVirtualListModel *list_model =
             (wxDataViewVirtualListModel*) GetOwner()->GetModel();
-            
+
         return list_model->GetCount();
     }
     else