Hi!
As a beginner pygtk user, I'm trying to make a TreeView which has
multi-line cells. Each node would contain about 1-4 lines of text. If
I were to use a gtk.CellRendererText, only 1 line would be visible,
and the user would have to scroll horizontally to see the rest of the
text. I want the full text to be visible, by having it wrap if it is
more than one line.
The best solution would be using a TextView as a CellRenderer. But it
looks like normal widgets can't be used like that. (Is there a reason
for this? One would think that CellRenderers would be very similar to
normal widgets.)
So I guess my best bet is making a custom CellRenderer. I thought
something like this would work:
---8<--------
import pygtk
pygtk.require('2.0')
import gtk
import gobject
import pango
class MyCellRenderer(gtk.GenericCellRenderer):
def __init__(self):
self.__gobject_init__()
def on_render(self, window, widget, background_area,
cell_area, expose_area, flags):
context = widget.get_pango_context()
layout = pango.Layout(context)
layout.set_text(long_string)
layout.set_wrap(gtk.WRAP_CHAR)
layout.set_width(cell_area.width)
widget.style.paint_layout(window, gtk.STATE_NORMAL, gtk.TRUE,
cell_area, widget, 'footext',
cell_area.x, cell_area.y,
layout)
def on_get_size(self, widget, cell_area):
return 0,0,100,100 # return something big enough
gobject.type_register(MyCellRenderer)
class Tree(gtk.TreeView):
def __init__(self):
self.store = gtk.ListStore(gobject.TYPE_STRING,
gobject.TYPE_PYOBJECT)
gtk.TreeView.__init__(self)
self.set_size_request(300, 200)
self.set_model(self.store)
self.set_headers_visible(gtk.TRUE)
rend = gtk.CellRendererText()
column = gtk.TreeViewColumn('First', rend, text=0)
self.append_column(column)
rend = MyCellRenderer()
column = gtk.TreeViewColumn('Second', rend)
self.append_column(column)
def insert(self, name):
iter = self.store.append()
self.store.set(iter,
0, name)
long_string="""long-long string for testing asdf asdf asdf asdf adsf asdf asdf
asdasdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf
"""
w = gtk.Window()
w.set_position(gtk.WIN_POS_CENTER)
w.connect('delete-event', gtk.mainquit)
t = Tree()
t.insert('baz')
w.add(t)
w.show_all()
gtk.main()
---8<-----------
The immedate problem is that layout.set_width() apparently expects
something other than pixels. If I do layout.set_width(200000),
get_pixel_size() will return a width of 195 pixels. I couldn't find
anything in the documentation about the units set_width() expects or
how to convert number of pixels to it.
Where is this documented, and how could I handle the conversion?
Even if I somehow manage to convert pixels to this unit system, I will
have another problem:
I want the width of the text to adjust to the width of the TreeView
widget. The height of the text should adjust to the amount of text in
that given node. The problem is, that as I understand it, the
CellRenderer has to calculate the size needed in on_get_size(). But I
couldn't find a way to get the size of the TreeWidget (or rather, the
size left for the cell after indentation, the little icon showing
expandedness, etc.) in that method. cell_area is None there. I can
access the width of the cell from on_render(), (it seems to be
cell_area.width) but I don't see a way to change the height of the
cell there.
Is there a way to solve this?
Thanks in advance,
Abel Daniel
_______________________________________________
pygtk mailing list [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/