On Mon, 09 Feb 2004 11:39:37 +0000
Graham Ashton <[EMAIL PROTECTED]> wrote:

> I was wondering if it's possible to do row specific tooltips.
> 
> The Tooltips.set_tip method takes a widget as it's first parameter, so
> I was wondering whether or not I'd need to insert an invisible widget
> (e.g. EventBox) inside my Treeview somehow.
> 
> My other hypothesis involves binding a callback to the mouse movement
> signals that works out which row the mouse is currently hovering over
> and sets the whole TreeView's tooltip depending on which row it's
> over. It's a bit stinky though, and I get the distinct impression that
> it might be really hard (hence this message, asking if there's an
> easier way).

some times ago i composed an example for what you want. i had the
intention to let it "rest" for a while, then going throug again, but
did't do it yet.

the tooltips with the example are actually popup-windows. it has at
least the advantage that one can put into all kind of stuff.
the tips are created for cells but it would likely not difficult to do
the same trick only for rows.

although this version is ugly and raw it could be helpful for you or
others. maybe someone can provide improvements too.


walter
#!/usr/bin/env python

try:
    import pygtk; pygtk.require("2.0")
except:
    pass
import gtk
import gobject

data = [["zero","ZERO"],["one","ONE"],["two","TWO"],["three","THREE"]]

class PopupExample:

    current_path = None
    current_column = None
    
    def onTreeviewLeaveNotify(self, treeview, event, popup_win):
        self.current_column = None
        self.current_path = None
        popup_win.hide()
    
    def onTreeviewMotionNotify(self, treeview, event):
    
        current_path, current_column = self.getCurrentCellData(treeview, event)[:2]
        
        if self.current_path != current_path:
            self.current_path = current_path
            treeview.emit("path-cross-event", event)
        
        if self.current_column != current_column:
            self.current_column = current_column
            treeview.emit("column-cross-event", event)

    
    def getCurrentCellData(self, treeview, event):
    
        try:
            current_path, current_column = treeview.get_path_at_pos(int(event.x), 
int(event.y))[:2]
        except:
            return (None, None, None, None, None, None)
            
        current_cell_area = treeview.get_cell_area(current_path, current_column)
        treeview_root_coords = treeview.get_bin_window().get_origin()
        
        cell_x = treeview_root_coords[0] + current_cell_area.x
        cell_y = treeview_root_coords[1] + current_cell_area.y
        
        cell_x_ = cell_x + current_cell_area.width
        cell_y_ = cell_y + current_cell_area.height
        
        return (current_path, current_column, cell_x, cell_y, cell_x_, cell_y_)


    def handlePopup(self, treeview, event, popup_win):
        current_path, current_column, cell_x, cell_y, cell_x_, cell_y_ = \
                self.getCurrentCellData(treeview, event)
        if cell_x != None:
            popup_win.get_child().set_text(str(current_path) + " -- " + 
current_column.get_title())
            popup_width, popup_height = popup_win.get_size()
            pos_x, pos_y = self.computeTooltipPosition(treeview, cell_x, cell_y, 
cell_x_, cell_y_, popup_width, popup_height, event)
            popup_win.move(int(pos_x) , int(pos_y))
            popup_win.show_all()
        else:
            popup_win.hide()
    
    def emitCellCrossSignal(self, treeview, event):
        treeview.emit("cell-cross-event", event)
    
    def computeTooltipPosition(self, treeview, cell_x, cell_y, cell_x_, cell_y_, 
popup_width, popup_height, event):
        screen_width = gtk.gdk.screen_width()
        screeen_height = gtk.gdk.screen_height()
    
        pos_x = treeview.get_bin_window().get_origin()[0] + event.x - popup_width/2
        if pos_x < 0:
            pos_x = 0
        elif pos_x + popup_width > screen_width:
            pos_x = screen_width - popup_width
            print "here"
        
            
        pos_y = cell_y_ + 3
        if pos_y + popup_height > screeen_height:
            pos_y = cell_y - 3 - popup_height
        
        return (pos_x , pos_y)
        
    def __init__(self):
    
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.connect("delete_event", gtk.mainquit)
        window.set_default_size(200,250)
        
        scrolledwin = gtk.ScrolledWindow()
        scrolledwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        window.add(scrolledwin)
        
        model = gtk.ListStore(str, str)
        for item in data:
            iter = model.append()
            model.set(iter,
                      0, item[0],
                      1, item[1])
        
        treeview = gtk.TreeView(model)
        scrolledwin.add(treeview)
        
        renderer = gtk.CellRendererText()
        column = gtk.TreeViewColumn("Header", renderer, text=0)
        treeview.append_column(column)
        
        renderer = gtk.CellRendererText()
        column = gtk.TreeViewColumn("HEADER", renderer, text=1)
        treeview.append_column(column)
        
        popup_win = gtk.Window(gtk.WINDOW_POPUP)
        label = gtk.Label()
        popup_win.add(label)
        
        gobject.signal_new("path-cross-event", gtk.TreeView, gobject.SIGNAL_RUN_LAST, 
gobject.TYPE_BOOLEAN, (gtk.gdk.Event,))
        gobject.signal_new("column-cross-event", gtk.TreeView, 
gobject.SIGNAL_RUN_LAST, gobject.TYPE_BOOLEAN, (gtk.gdk.Event,))
        gobject.signal_new("cell-cross-event", gtk.TreeView, gobject.SIGNAL_RUN_LAST, 
gobject.TYPE_BOOLEAN, (gtk.gdk.Event,))
        
        treeview.connect("leave-notify-event", self.onTreeviewLeaveNotify, popup_win)
        treeview.connect("motion-notify-event", self.onTreeviewMotionNotify)
        
        treeview.connect("path-cross-event", self.emitCellCrossSignal)
        treeview.connect("column-cross-event", self.emitCellCrossSignal)
        
        treeview.connect("cell-cross-event", self.handlePopup, popup_win)
        
        window.show_all()

def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    PopupExample()
    main()
_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to