hydrus/include/ClientDragDrop.py

84 lines
3.1 KiB
Python
Raw Normal View History

2017-05-17 21:53:02 +00:00
import HydrusGlobals as HG
2015-07-29 19:11:35 +00:00
import wx
class FileDropTarget( wx.PyDropTarget ):
2017-08-23 21:34:25 +00:00
def __init__( self, filenames_callable = None, url_callable = None, page_callable = None ):
2015-07-29 19:11:35 +00:00
wx.PyDropTarget.__init__( self )
2017-05-17 21:53:02 +00:00
self._filenames_callable = filenames_callable
self._url_callable = url_callable
2017-08-23 21:34:25 +00:00
self._page_callable = page_callable
2017-05-17 21:53:02 +00:00
2015-07-29 19:11:35 +00:00
self._receiving_data_object = wx.DataObjectComposite()
self._hydrus_media_data_object = wx.CustomDataObject( 'application/hydrus-media' )
2017-08-23 21:34:25 +00:00
self._hydrus_page_tab_data_object = wx.CustomDataObject( 'application/hydrus-page-tab' )
2015-07-29 19:11:35 +00:00
self._file_data_object = wx.FileDataObject()
2017-05-17 21:53:02 +00:00
self._text_data_object = wx.TextDataObject()
2015-07-29 19:11:35 +00:00
self._receiving_data_object.Add( self._hydrus_media_data_object, True )
2017-08-23 21:34:25 +00:00
self._receiving_data_object.Add( self._hydrus_page_tab_data_object )
2015-07-29 19:11:35 +00:00
self._receiving_data_object.Add( self._file_data_object )
2017-05-17 21:53:02 +00:00
self._receiving_data_object.Add( self._text_data_object )
2015-07-29 19:11:35 +00:00
self.SetDataObject( self._receiving_data_object )
def OnData( self, x, y, result ):
if self.GetData():
received_format = self._receiving_data_object.GetReceivedFormat()
2017-05-17 21:53:02 +00:00
received_format_type = received_format.GetType()
if received_format_type == wx.DF_FILENAME and self._filenames_callable is not None:
2015-07-29 19:11:35 +00:00
paths = self._file_data_object.GetFilenames()
2017-09-27 21:52:54 +00:00
wx.CallAfter( self._filenames_callable, paths ) # callafter to terminate dnd event now
result = wx.DragNone
2015-07-29 19:11:35 +00:00
2017-05-17 21:53:02 +00:00
elif received_format_type in ( wx.DF_TEXT, wx.DF_UNICODETEXT ) and self._url_callable is not None:
text = self._text_data_object.GetText()
2017-09-27 21:52:54 +00:00
wx.CallAfter( self._url_callable, text ) # callafter to terminate dnd event now
result = wx.DragCopy
2017-05-17 21:53:02 +00:00
2015-07-29 19:11:35 +00:00
else:
try:
format_id = received_format.GetId()
except:
format_id = None
if format_id == 'application/hydrus-media':
2017-09-27 21:52:54 +00:00
result = wx.DragCancel
2015-07-29 19:11:35 +00:00
2017-08-23 21:34:25 +00:00
if format_id == 'application/hydrus-page-tab' and self._page_callable is not None:
page_key = self._hydrus_page_tab_data_object.GetData()
2017-09-27 21:52:54 +00:00
wx.CallAfter( self._page_callable, page_key ) # callafter to terminate dnd event now
result = wx.DragMove
2017-08-23 21:34:25 +00:00
2015-07-29 19:11:35 +00:00
return result
2017-09-27 21:52:54 +00:00
# setting OnDragOver to return copy gives Linux trouble with page tab drops with shift held down