2011-03-14 56 views
0

我一直無法得到拖放驗證在PyGTK的工作。我沒有想法,想要第二個意見。GTK拖動和拖放驗證X11光標鎖定內drag_get_data

我的目標是隻允許包含的.jpg被丟棄的文件。

具體而言,每當我請widget.drag_get_data拖運動內回調X11光標鎖定。製作調試繁瑣和加重,因爲我要殺死X11並重新啓動一切。

這裏是我的源代碼,我認爲這個問題在drag_motion_cb和drag_data_received_cb方法具體所在。我已經離開了我之前嘗試過的註釋部分。

使用谷歌代碼搜索搜索drag_get_data不顯示任何其他人做先進的驗證。所以我猜別人也失敗了。

我出出主意,並最終將只使用簡單的DnD在我的Linux端口(沒有適當的驗證),如果我能不知道這一點。

在此先感謝。

import pygtk pygtk.require('2.0') import gobject import gtk

TARGET_TEXT_URI_LIST = 0

drop_targets = [ ("text/uri-list", 0, TARGET_TEXT_URI_LIST) ]

class TestApp(gobject.GObject): builder = gtk.Builder() window = None button = None

def init(self): gobject.GObject.init(self)

assert self.builder != None 
self.builder.add_from_file("MainWindow.glade"); 
self.window = self.builder.get_object("window1") 
self.button = self.builder.get_object("button1") 

self.window.connect("destroy", gtk.main_quit) 

self.button.drag_dest_set(gtk.DEST_DEFAULT_ALL, drop_targets, gtk.gdk.ACTION_COPY|gtk.gdk.ACTION_LINK|gtk.gdk.ACTION_MOVE) 

self.button.connect("drag-data-received", self.drag_data_received_cb) 
self.button.connect("drag-drop", self.drag_drop_cb) 
self.button.connect("drag-motion", self.drag_motion_cb) 
self.button.connect("drag-leave", self.drag_leave_cb) 

self.window.show_all() 

drop_data_ready =假 drop_occurred =假 drop_highlight =假 drop_data =無

DEF drag_data_received_cb(個體,插件,語境,X,Y,數據,信息,時間戳): 打印「 drag_data_received_cb」

# Check to see if we have the drop data yet. 
if False == self.drop_data_ready: 
    # If this is data we expected or can handle, then process it. 
    if TARGET_TEXT_URI_LIST == info and data.get_format() == 8 and data.get_length() > 0: 
    self.drop_data = data.get_uris() 
    self.drop_data_ready = True 
    context.drag_status(gtk.gdk.ACTION_COPY, timestamp) 

# Actual drop handling code. 
if True == self.drop_occurred: 
    # Reset state. 
    self.drop_occurred = False 

    print "RECEIVED DROP",self.drop_data 

    # Notify whether we handled the drop or not. 
    context.finish(True,False,timestamp) 

    # Clean up. 
    self.drag_leave_cb(widget, context, timestamp) 
return True 

DEF drag_drop_cb(個體,插件,語境,X,Y,時間戳): 目標=無線dget.drag_dest_find_target(上下文,widget.drag_dest_get_target_list())

# Is it something we can handle? 

if target == gtk.gdk.atom_intern("text/uri-list", False): 
    # Tell data recieved handler (do_drag_data_received) we can actually handle the drop. 
    self.drop_occurred = True 

    widget.drag_get_data(context,target,timestamp) 

    # We can handle this data type. 
    return True 
else: 
    # We cannot handle the drop. 
    return False 
pass 

DEF drag_motion_cb(個體,插件,語境,X,Y,時間戳):

if not self.drop_data_ready: 
    widget.drag_get_data(context, gtk.gdk.atom_intern("text/uri-list",False), timestamp) 
    return False 



""" 
target = widget.drag_dest_find_target(context, widget.drag_dest_get_target_list()) 
if target == gtk.gdk.atom_intern("text/uri-list", False): 
    if True == self.drop_data_ready: 
    pass 
    else: 


    #widget.drag_get_data(context, target, timestamp) 
    pass 
    """ 



context.drag_status(gtk.gdk.ACTION_COPY, timestamp) 
""" 

if target == gtk.gdk.atom_intern("text/uri-list", False): 
    if True == self.drop_data_ready: 
    if repr(drop_data).find(".jpg") != -1: 
     # Tell Gdk we can handle this. 
     context.drag_status(gtk.gdk.ACTION_COPY, timestamp) 

     # Draw drop highlight (but only once). 
     if False == self.drop_highlight: 
     widget.drag_highlight() 
     self.drop_highlight = True 

     # Return here, don't fall through. 
     return True 
    else: 
    # Request drag data from the source. 
    widget.drag_get_data(context, target, timestamp) 

    # Fall-through to not allowing. 
""" 

# not something we can handle 
#context.drag_status(0, timestamp) # Don't allow drop. 
return True 


pass 

DEF drag_leave_cb(個體,小窗口,上下文,時間戳): #清理拖動數據。 如果真== self.drop_data_ready: self.drop_data =無 self.drop_data_ready =假

# Un-draw the highlight. 
if True == self.drop_highlight: 
    widget.drag_unhighlight() 
    self.drop_highlight = False 
pass 

gobject.type_register(TestApp)

高清的main(): 車= TestApp() gtk.main()

如果 == '主要': 的main()

+0

粘貼這些大碼像引擎收錄。com,因爲它可能沒有正確顯示(滾動條內的滾動條和混合縮進) – saeedgnu 2011-03-15 06:04:14

回答

0

我想你一定地方做這樣的事情在drag_data_received_cb

def drag_data_received_cb(self, obj, context, x, y, selection, target_id, etime): 
    try: 
     dtype = selection.get_data_type() 
    except AttributeError:## Old PyGTK 
     dtype = selection.type 
    if dtype=='image/jpeg': 
     image_data = selection.data 
     ## do somthing with image_data 
     return True 
    return False