]> 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 d407c8228561094cfaa5b7deebe9b6fc3e62292b..bdeadffd368cf888284931def78b3056723feea9 100644 (file)
@@ -634,22 +634,35 @@ wxDataViewRenderer::RenderWithAttr(wxDC& dc,
     {
         const wxSize size = GetSize();
 
-        // horizontal alignment:
-        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
+        // 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;
+        }
 
-        // 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
+        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.SetSize(size);
+            item_rect.height = size.y;
+        }
     }
 
     return Render(item_rect, &dc, state);
@@ -704,30 +717,24 @@ wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
 {
 }
 
-void wxDataViewCustomRenderer::RenderText( const wxString &text, int xoffset,
-                                           wxRect cell, wxDC *dc, int state )
-{
-    wxColour col = state & wxDATAVIEW_CELL_SELECTED
-                    ? wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT)
-                    : GetOwner()->GetOwner()->GetForegroundColour();
-
-    wxDataViewItemAttr attr;
-    attr.SetColour(col);
-    RenderText(*dc, cell, wxALIGN_NOT, text, &attr, state, xoffset);
-}
-
 void
 wxDataViewCustomRenderer::RenderText(wxDC& dc,
                                      const wxRect& rect,
                                      int align,
                                      const wxString& text,
                                      const wxDataViewItemAttr *attr,
-                                     int WXUNUSED(state),
+                                     int state,
                                      int xoffset)
 {
-    wxDCTextColourChanger changeFg(dc);
+    wxColour col;
     if ( attr && attr->HasColour() )
-        changeFg.Set(attr->GetColour());
+        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() )
@@ -938,10 +945,6 @@ wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
     m_value = 0;
 }
 
-wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
-{
-}
-
 bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
 {
     m_value = (long) value;
@@ -958,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;
 }
@@ -1106,10 +1115,6 @@ const wxString &varianttype, wxDataViewCellMode mode, int align ) :
     SetAlignment(align);
 }
 
-wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
-{
-}
-
 bool wxDataViewIconTextRenderer::SetValue( const wxVariant &value )
 {
     m_value << value;
@@ -1121,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;
 }