Get wxDataViewCtrl cell size (row height) for custom rendering

489 views
Skip to first unread message

Iwbnwif Yiw

unread,
Apr 10, 2015, 4:56:03 PM4/10/15
to wx-u...@googlegroups.com
Hi,

I have a renderer derived from wxDataViewCustomRenderer and I would like the possibility to draw a X that complete fills the cell.

I am using the control with a model derived from wxDataViewIndexListModel so I will talk about 'rows'.

My problem is that I have variable row heights. The height of a row is determined by the size of an object in a different column from the one that I would like to draw the X in.

I can get the width of the cell by GetOwner()->GetWidth() but I cannot find any way of determining the row height. The 'rect' passed to the renderer appears to be whatever is returned by GetSize() and has no relation to the actual cell size.

The only thing I can think of at the moment is to pass all the information required to determine the row height to both the column containing the sized item and the column containing the X.

TIA for any help!

Vadim Zeitlin

unread,
Apr 11, 2015, 1:13:57 PM4/11/15
to wx-u...@googlegroups.com
On Fri, 10 Apr 2015 13:56:03 -0700 (PDT) Iwbnwif Yiw wrote:

IY> I can get the width of the cell by GetOwner()->GetWidth() but I cannot find
IY> any way of determining the row height. The 'rect' passed to the renderer
IY> appears to be whatever is returned by GetSize() and has no relation to the
IY> actual cell size.

At least in the generic version, the rectangle passed to the renderer has
the size of GetSize() only if that size is specified, so if you returned
wxDefaultSize from your overridden GetSize() you should receive the full
cell rectangle in your Render().

I'm less sure about the other version, but looking at the GTK and Cocoa
code, they don't seem to adjust the rectangle at all (which probably means
that we have a bug here as the behaviour is different from the generic
version...). But I could be missing something here, it would be nice if you
could test this.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

Iwbnwif Yiw

unread,
Apr 11, 2015, 3:04:27 PM4/11/15
to wx-u...@googlegroups.com

 At least in the generic version, the rectangle passed to the renderer has
the size of GetSize() only if that size is specified, so if you returned
wxDefaultSize from your overridden GetSize() you should receive the full
cell rectangle in your Render().


Thank you, you are a genius!!

I thought I had been all the way through the source code (looking at the way the background is painted in the generic version for example) and it never occurred to me to return wxDefaultSize.
 
 I'm less sure about the other version, but looking at the GTK and Cocoa
code, they don't seem to adjust the rectangle at all (which probably means
that we have a bug here as the behaviour is different from the generic
version...). But I could be missing something here, it would be nice if you
could test this.

I have just tested this on MSW (Windows 8.1) and Linux (Ubuntu 14.04). By returning wxDefaultSize it works perfectly (i.e. the whole cell size is returned) on both platforms.

Once again many thanks for looking at this!

Iwbnwif Yiw

unread,
May 12, 2016, 2:25:29 PM5/12/16
to wx-users
Hi, sorry to trouble this list with yet another data view question ....

 I'm less sure about the other version, but looking at the GTK and Cocoa 
code, they don't seem to adjust the rectangle at all (which probably means 
that we have a bug here as the behaviour is different from the generic 
version...). But I could be missing something here, it would be nice if you 
could test this. 

When I tested using wxDefaultSize it worked fine, but now I have a problem where on GTK only the rows don't resize after they have been created.

To see this, you can apply the following minimal patch to the dataview sample.

diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp
index fca7630..2d4d58d 100644
--- a/samples/dataview/dataview.cpp
+++ b/samples/dataview/dataview.cpp
@@ -223,7 +223,9 @@ public:
 
     virtual wxSize GetSize() const wxOVERRIDE
     {
-        return wxSize(60,20);
+        int width = GetOwner()->GetWidth();
+        wxPrintf ("Column width is %d\n", width);
+        return wxSize(60,width / 3);
     }
 
     virtual bool SetValue( const wxVariant &value ) wxOVERRIDE
@@ -598,7 +600,7 @@ void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned l
             wxASSERT(!m_ctrl[0] && !m_music_model);
             m_ctrl[0] =
                 new wxDataViewCtrl( parent, ID_MUSIC_CTRL, wxDefaultPosition,
-                                    wxDefaultSize, style );
+                                    wxDefaultSize, style | wxDV_VARIABLE_LINE_HEIGHT);
             m_ctrl[0]->Connect(wxEVT_CHAR,
                                wxKeyEventHandler(MyFrame::OnDataViewChar),
                                NULL, this);

The idea is that if you resize the column width, the height should scale accordingly. This is not my eventual aim, I want to update the cell height if the contents change, but it illustrates the point.

The patch works fine on Windows, but nothing happens on GTK (2), it seems the row height stays the same as when it was added to the control.

This is quite possibly a limitation of the GtkTreeView native control. I googled as much as I could and didn't find any method for forcing a height recalculation. Also gtk_wx_cell_renderer_get_size only seems to be called when a row is added? 

As an aside, how do I build the generic version on linux? I understand from http://stackoverflow.com/questions/36133581/use-generic-implementation-of-wxdataviewctrl-on-osx that there are some defines to be made, but where (using configure)?

Thanks!

Igor Korot

unread,
May 12, 2016, 2:54:21 PM5/12/16
to wx-u...@googlegroups.com
Hi,
Can you check what happen with GTK+3?
Also which version of GTK+2 did you test this on?

Thank you.

>
> This is quite possibly a limitation of the GtkTreeView native control. I
> googled as much as I could and didn't find any method for forcing a height
> recalculation. Also gtk_wx_cell_renderer_get_size only seems to be called
> when a row is added?
>
> As an aside, how do I build the generic version on linux? I understand from
> http://stackoverflow.com/questions/36133581/use-generic-implementation-of-wxdataviewctrl-on-osx
> that there are some defines to be made, but where (using configure)?
>
> Thanks!
>
> --
> Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
>
> To unsubscribe, send email to wx-users+u...@googlegroups.com
> or visit http://groups.google.com/group/wx-users

Iwbnwif Yiw

unread,
May 12, 2016, 3:35:26 PM5/12/16
to wx-users
On Thursday, May 12, 2016 at 7:54:21 PM UTC+1, Igor Korot wrote:

Can you check what happen with GTK+3?
Also which version of GTK+2 did you test this on?

Hi, thanks for you response.

GTK+3 behaves exactly the same as GTK+2.

Versions tested:

GTK+2 : 2.24.23
GTK+3 : 3.10.8

Latest wxWidgets trunk from Github.

Any clues on how to build the generic version of wxDataViewCtrl on Linux - I would like to test that as well.


Iwbnwif Yiw

unread,
May 14, 2016, 4:09:05 PM5/14/16
to wx-users
Any clues on how to build the generic version of wxDataViewCtrl on Linux - I would like to test that as well. 

Okay, I am not sure if this is the correct way or not, but it more or less seems to work. I included the following line in include/wx/defs.h:

#define wxHAS_GENERIC_DATAVIEWCTRL 1

Followed by a rebuild of everything.

So I can say that the generic version behaves the same as the GTK version and differently from the MSW one.

It seems that in the GTK and generic versions, the only way to update the height is to delete the item and re-insert it.


Vadim Zeitlin

unread,
May 14, 2016, 4:15:28 PM5/14/16
to wx-u...@googlegroups.com
On Sat, 14 May 2016 13:09:05 -0700 (PDT) Iwbnwif Yiw wrote:

IY> Okay, I am not sure if this is the correct way or not, but it more or less
IY> seems to work. I included the following line in include/wx/defs.h:
IY>
IY> #define wxHAS_GENERIC_DATAVIEWCTRL 1
IY>
IY> Followed by a rebuild of everything.
IY>
IY> So I can say that the generic version behaves the same as the GTK version
IY> and differently from the MSW one.

This is quite impossible. The MSW version *is* the generic one, there is
no native version under MSW. There is a native GTK version however.

Iwbnwif Yiw

unread,
May 14, 2016, 4:32:33 PM5/14/16
to wx-users

 This is quite impossible. The MSW version *is* the generic one, there is
no native version under MSW. There is a native GTK version however.


Aargh!! Yes, you are of course right. It works in generic and MSW, but not in GTK. Sorry for the confusion. 
Reply all
Reply to author
Forward
0 new messages