File: drDragAndDrop.py

package info (click to toggle)
drpython 161-3.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,588 kB
  • ctags: 1,470
  • sloc: python: 15,817; sh: 358; makefile: 43
file content (105 lines) | stat: -rw-r--r-- 3,477 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#	Programmer:	Daniel Pozmanter
#	E-mail:		drpython@bluebottle.com
#	Note:		You must reply to the verification e-mail to get through.
#
#	Copyright 2003-2005 Daniel Pozmanter
#
#	Distributed under the terms of the GPL (GNU Public License)
#
#    DrPython is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
	
#Drag and Drop

import wx, wx.stc

#*******************************************************************************************************
#Submitted by Slim Amamou
#Modified by Daniel Pozmanter
#Modified again by Dan for 3.7.0
#	Reworked to add Text Drag and Drop Support
#	Thanks to Robin Dunn for the code.

class drDropTarget(wx.DropTarget):
	def __init__(self, window):
		wx.DropTarget.__init__(self)
		self.window = window.grandparent
		
		window.Bind(wx.stc.EVT_STC_START_DRAG, self.OnStartDrag, id=window.GetId())
		
		self.data = wx.DataObjectComposite()
		self.dtext = wx.TextDataObject()
		self.dfilename = wx.FileDataObject()
		self.data.Add(self.dtext, True)
		self.data.Add(self.dfilename)
		
		self.draginitiatedfromstc = False
		
		self.moddown = False
		
		self.SetDataObject(self.data)

	def DropFiles(self, filenames):
		filenames = map(lambda x: x.replace("\\", "/"), filenames)
		alreadyopen = self.window.GetAlreadyOpen()
		if len(self.window.txtDocument.filename) > 0:
			for filename in filenames:
				if filename in alreadyopen:
					self.window.setDocumentTo(alreadyopen.index(filename))
				else:
					self.window.OpenFile(filename, True)
		else:
			if filenames[0] in alreadyopen:
				self.window.setDocumentTo(0)
			else:
				self.window.OpenFile(filenames[0], self.window.txtDocumentArray[0].GetModify())
			x = 1
			l = len(filenames)
			while x < l:
				if filenames[x] in alreadyopen:	
					self.window.setDocumentTo(alreadyopen.index(filenames[x]))
				else:			
					self.window.OpenFile(filenames[x], True)
				x = x + 1 
	
	def DropText(self, text, point):
		if self.window.prefs.draganddropmode != 2:
			if self.draginitiatedfromstc:
				if ((self.window.prefs.draganddroptextmode == 0) and self.moddown) or \
				((self.window.prefs.draganddroptextmode == 1) and (not self.moddown)):
					self.window.txtDocument.SetSelectedText('')
			position = self.window.txtDocument.PositionFromPoint(point)
			self.window.txtDocument.InsertText(position, text)
			self.window.txtDocument.GotoPos(position+len(text))
		
	def OnDrop(self, x, y):
		return True
	
	def OnData(self, x, y, theData):
		self.GetData()
		if self.dtext.GetTextLength() > 1:
			self.DropText(self.dtext.GetText(), wx.Point(x, y))
			self.dtext.SetText('')
		else:
			self.DropFiles(self.dfilename.GetFilenames())
		
		self.draginitiatedfromstc = False
				
		return theData

	def OnStartDrag(self, event):
		self.draginitiatedfromstc = True
		
	def SetModifierDown(self, moddown):
		self.moddown = moddown