All pastes #987760 Raw Edit

Stuff

public python v1 · immutable
#987760 ·published 2008-04-16 22:27 UTC
rendered paste body
import gtkimport gobjectimport urllibimport osDEFAULT_IMAGE_WIDTH = 100TARGET_TYPE_URI_LIST = 80dnd_list = [ ( 'text/uri-list', 0, TARGET_TYPE_URI_LIST ) ]# Setup ListStore to contain images and descriptionmodel = gtk.ListStore(gtk.gdk.Pixbuf,  gobject.TYPE_STRING)# GtkIconViewview = gtk.IconView(model)  # Pass the model stored in a ListStore to the GtkIconViewdef on_drag_data_received(widget, context, x, y, selection, target_type, timestamp):    uri = selection.data.strip()    uri_splitted = uri.split() # we may have more than one file dropped    for uri in uri_splitted:        path = get_file_path_from_dnd_dropped_uri(uri)        print path        try:            pixbuf = gtk.gdk.pixbuf_new_from_file(path)            pix_w = pixbuf.get_width()            pix_h = pixbuf.get_height()            new_h = (pix_h * DEFAULT_IMAGE_WIDTH) / pix_w   # Calculate the scaled height before resizing image            scaled_pix = pixbuf.scale_simple(DEFAULT_IMAGE_WIDTH, new_h, gtk.gdk.INTERP_TILES)            model.append((scaled_pix, im))        except:            passdef get_file_path_from_dnd_dropped_uri(uri):    path = urllib.url2pathname(uri) # escape special chars    path = path.strip('\r\n\x00') # remove \r\n and NULL    # get the path to file    if path.startswith('file:\\\\\\'): # windows        path = path[8:] # 8 is len('file:///')    elif path.startswith('file://'): # nautilus, rox        path = path[7:] # 7 is len('file://')    elif path.startswith('file:'): # xffm        path = path[5:] # 5 is len('file:')    return path# Main Window setupwindow = gtk.Window(gtk.WINDOW_TOPLEVEL)window.set_size_request(400,  240)window.connect("destroy", gtk.main_quit)window.set_title("Python GtkIconView Test")# Add a VBoxvbox = gtk.VBox()window.add(vbox)# Setup Scrolled Windowscrolled_win = gtk.ScrolledWindow()scrolled_win.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)# Create a tuple with image filesimmagini = ("guido_sottozero.jpg", "IMG_0056.JPG", "movies_card.jpg")#immagini = ("guido_sottozero.jpg", "movies_card.jpg")#immagini = ("movies_card.jpg", )for im in immagini:    try:        pixbuf = gtk.gdk.pixbuf_new_from_file(im)        pix_w = pixbuf.get_width()        pix_h = pixbuf.get_height()        new_h = (pix_h * DEFAULT_IMAGE_WIDTH) / pix_w   # Calculate the scaled height before resizing image        scaled_pix = pixbuf.scale_simple(DEFAULT_IMAGE_WIDTH, new_h, gtk.gdk.INTERP_TILES)        model.append((scaled_pix, im))        print im    except:        pass# Setup GtkIconViewview.set_pixbuf_column(0)view.set_text_column(1)view.set_selection_mode(gtk.SELECTION_MULTIPLE)view.connect('drag_data_received', on_drag_data_received)view.drag_dest_set( gtk.DEST_DEFAULT_MOTION | gtk.DEST_DEFAULT_HIGHLIGHT | gtk.DEST_DEFAULT_DROP,                    dnd_list, gtk.gdk.ACTION_COPY)    # Pack objects and show them allscrolled_win.add(view)vbox.pack_start(scrolled_win)window.show_all()gtk.main()