File: resizablebase.py

package info (click to toggle)
fife 0.3.3%2Br3-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 16,312 kB
  • sloc: cpp: 83,234; python: 16,261; sh: 10,134; xml: 3,435; makefile: 265; objc: 245; ansic: 229
file content (195 lines) | stat: -rw-r--r-- 5,965 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# -*- coding: utf-8 -*-

# ####################################################################
#  Copyright (C) 2005-2009 by the FIFE team
#  http://www.fifengine.de
#  This file is part of FIFE.
#
#  FIFE is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2.1 of the License, or (at your option) any later version.
#
#  This library 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
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the
#  Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
# ####################################################################

from fife import fife
import scripts
from fife.extensions import pychan

class ResizableBase(object):
	def __init__(self, resizable=True, *args, **kwargs):
		self._engine = scripts.editor.getEditor().getEngine()
	
		self.resizable = resizable
		self.resizable_top = True
		self.resizable_left = True
		self.resizable_right = True
		self.resizable_bottom = True
		
		self._resize = False
		
		self.capture(self.mouseEntered, "mouseEntered", "ResizableBase")
		self.capture(self.mouseExited, "mouseExited", "ResizableBase")
		self.capture(self.mouseMoved, "mouseMoved", "ResizableBase")
		self.capture(self.mouseDragged, "mouseDragged", "ResizableBase")
		self.capture(self.mousePressed, "mousePressed", "ResizableBase")
		self.capture(self.mouseReleased, "mouseReleased", "ResizableBase")
		
		self.cursor_id = 0
		self.cursor_type = fife.CURSOR_NATIVE
		self.cursor_image = None
		self.cursor_animation = None

	def _saveCursor(self):
		cursor = self._engine.getCursor()
		self.cursor_type = cursor.getType()
		
		if self.cursor_type == fife.CURSOR_NATIVE:
			self.cursor_id = cursor.getId()
			self.cursor_image = None
			self.cursor_animation = None
			
		elif self.cursor_type == fife.CURSOR_IMAGE:
			self.cursor_id = 0
			self.cursor_image = cursor.getImage()
			self.cursor_animation = None
		
		elif self.cursor_type == fife.CURSOR_ANIMATION:
			self.cursor_id = 0
			self.cursor_image = None
			self.cursor_animation = cursor.getAnimation()
	
	def _restoreCursor(self):
		cursor = self._engine.getCursor()
		
		if self.cursor_type == fife.CURSOR_NATIVE:
			cursor.set(self.cursor_id)
		elif self.cursor_type == fife.CURSOR_IMAGE:
			cursor.set(self.cursor_image)
		elif self.cursor_type == fife.CURSOR_ANIMATION:
			cursor.set(self.cursor_animation)
	
	def mouseEntered(self, event):
		# Save cursor
		if self.resizable and self._resize is False:
			self._saveCursor()
		
	def mouseMoved(self, event):
		if self.resizable is False: 
			return
			
		# Hack to allow consuming mousemove events
		key = (event.getX(), event.getY())
		if _mousemoveevent.has_key( key ) is False:
			_mousemoveevent.clear()
			_mousemoveevent[key] = event
		elif _mousemoveevent[key].isConsumed():
			return
			
			
		titleheight = 0
		if hasattr(self.real_widget, "getTitleBarHeight"):
			titleheight = self.real_widget.getTitleBarHeight()

		cursor = self._engine.getCursor()
		
		left	= event.getX() < 5 and self.resizable_left
		right	= event.getX() > self.width-5 and self.resizable_right
		top		= event.getY() < 5 and self.resizable_top
		bottom	= event.getY() - titleheight > self.height-5 and self.resizable_bottom

		if left and top:
			cursor.set(fife.NC_RESIZENW)
		elif right and top:
			cursor.set(fife.NC_RESIZENE)
		elif left and bottom:
			cursor.set(fife.NC_RESIZESW)
		elif right and bottom:
			cursor.set(fife.NC_RESIZESE)
		elif left:
			cursor.set(fife.NC_RESIZEW)
		elif right:
			cursor.set(fife.NC_RESIZEE)
		elif top:
			cursor.set(fife.NC_RESIZEN)
		elif bottom:
			cursor.set(fife.NC_RESIZES)
		else:
			self._restoreCursor()
			return
			
		event.consume()
		_mousemoveevent[key].consume()
		
	def mouseExited(self, event):
		# Reset cursor to whatever it was before it entered this window
		if self.resizable and self._resize is False:
			self._restoreCursor()
		
	def mouseDragged(self, event):
		if self.resizable and self._resize:
			diffX = event.getX()
			diffY = event.getY()
			
			# Resize horizontally
			if self._rLeft:
				self.x += diffX
				self.width -= diffX
			elif self._rRight:
				self.width = diffX

			# Resize vertically
			if self._rTop:
				self.y += diffY
				self.height -= diffY
			elif self._rBottom:
				titleheight = 0
				if hasattr(self.real_widget, "getTitleBarHeight"):
					titleheight = self.real_widget.getTitleBarHeight()
				self.height = diffY-titleheight

	def mousePressed(self, event):
		if self.resizable is False:
			return
			
		titleheight = 0
		if hasattr(self.real_widget, "getTitleBarHeight"):
			titleheight = self.real_widget.getTitleBarHeight()
			
		self._rLeft		= event.getX() < 5 and self.resizable_left
		self._rRight	= event.getX() > self.width-5 and self.resizable_right
		self._rTop		= event.getY() < 5 and self.resizable_top
		self._rBottom	= event.getY() - titleheight > self.height-5 and self.resizable_bottom
		
		if self._rLeft or self._rRight or self._rTop or self._rBottom:
			self._resize = True
			self.min_size = (30, 30)
			event.consume()
		
	def mouseReleased(self, event):
		if self._resize:
			self.min_size = (self.width, self.height)
			self.adaptLayout()
			event.consume()
			
			titleheight = 0
			if hasattr(self.real_widget, "getTitleBarHeight"):
				titleheight = self.real_widget.getTitleBarHeight()
		
			self._resize = False
			if event.getX() <= 0 or event.getX() >= self.width \
					or event.getY() <= 0 or event.getY() >= self.height+titleheight:
				self.mouseExited(event)
				
# Ugly hack to allow consumption of mousemove events
_mousemoveevent = {}