File: dockarea.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 (186 lines) | stat: -rw-r--r-- 5,355 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
# -*- 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.extensions import pychan
from fife.extensions.pychan import widgets
import scripts.editor
from fife import fife
from panel import Panel
from faketabwidget import FakeTabWidget
from resizablebase import ResizableBase

class DockArea(widgets.VBox, ResizableBase):
	def __init__(self, side, resizable=True, *args, **kwargs):
		widgets.VBox.__init__(self, margins=(0,0,0,0), *args, **kwargs)
		ResizableBase.__init__(self, *args, **kwargs)
		
		self.cursor_id = 0
		self.cursor_type = 0
		
		self.vexpand=0
		self.hexpand=0
		
		self.side = side
		self.resizable_top = (side == "bottom")
		self.resizable_left = (side == "right")
		self.resizable_right = (side == "left")
		self.resizable_bottom = (side == "top")
		
		self.gui = None
		
		self.buildGui()
		self.tabwidgets = []
		self.panels = []
		
	def getDockLocation(self, x, y):
		placeAfter = None
		placeBefore = None
		placeIn = None
		
		if x >= 0 and y >= 0:
			# See if widget was dropped on a tabwidget
			for tabwidget in self.tabwidgets:
				absX, absY = tabwidget.getAbsolutePos()
					
				if absX <= x and absY <= y \
						and absX+tabwidget.width >= x and absY+tabwidget.height >= y:
					# Check if the user tried to place it in front, or after this widget
					if self.side == "left" or self.side == "right":
						if y < absY+10:
							placeBefore = tabwidget
						elif y > absY+tabwidget.height-10:
							placeAfter = tabwidget
					else:
						if x < absX+10:
							placeBefore = tabwidget
						elif x > absX+tabwidget.width-10:
							placeAfter = tabwidget
					if placeAfter is None and placeBefore is None:
						placeIn = tabwidget
					break
					
		return (placeIn, placeBefore, placeAfter)
		
	def dockChild(self, child, x, y):
		for panel in self.panels:
			if panel[0] == child:
				return
	
		child.dockarea = self
		child.setDocked(True)
		
		placeIn, placeBefore, placeAfter = self.getDockLocation(x, y)
	
		if placeIn is None:
			tabwidget = FakeTabWidget(resizable=True)
			tabwidget.hexpand=1
			tabwidget.vexpand=1
		
			if self.side == "left" or self.side == "right":
				tabwidget.resizable_bottom = True
			else:
				tabwidget.resizable_right = True
			self.tabwidgets.append(tabwidget)
			
			if placeBefore:
				self.gui.insertChildBefore(tabwidget, placeBefore)
			elif placeAfter:
				self.gui.insertChild(tabwidget, self.gui.children.index(placeAfter)+1)
			else:
				self.gui.addChild(tabwidget)
		else:
			tabwidget = placeIn

		tab = tabwidget.addTab(child, child.title)
		self.panels.append( (child, tabwidget) )
		
		def undock(event):
			if event.getButton() != 2: # Right click
				return
				
			self.undockChild(child)
			
		tab[2].capture(undock, "mouseClicked")
		self.adaptLayout()
		
	def undockChild(self, child, childIsCaller=False):
		tabwidget = None
		for panel in self.panels[:]:
			if panel[0] == child:
				tabwidget = panel[1]
				self.panels.remove(panel)
				break
		else:
			return
			
		tabwidget.removeTab(child)
		if childIsCaller is False:
			child.setDocked(False)
		
		if len(tabwidget.tabs) <= 0:
			self.gui.removeChild(tabwidget)
			self.tabwidgets.remove(tabwidget)
			
			# This stops a guichan exception when a widget with modul focus gets focused.
			# It is not pretty, but crashes aren't pretty either
			tabwidget.__del__()
			del tabwidget
				
		self.adaptLayout()
		
	def buildGui(self):
		if self.gui:
			self.removeChild(self.gui)
		
		if self.side == "left" or self.side == "right":
			self.gui = widgets.VBox()
		else:
			self.gui = widgets.HBox()
			
		self.gui.vexpand = 1
		self.gui.hexpand = 1
		
		self.addChild(self.gui)
	
	def mouseReleased(self, event):
		if self._resize:
			if self._rLeft or self._rRight:
				# Resize children
				for child in self.gui.findChildren(parent=self.gui):
					child.min_size = (self.width, child.min_size[1])
					child.max_size = (self.width, child.max_size[1])
					
			if self._rTop or self._rBottom:
				# Resize children
				for child in self.gui.findChildren(parent=self.gui):
					child.min_size = (child.min_size[0], self.height)
					child.max_size = (child.max_size[0], self.height)
					
			self.gui.max_size = (self.width, self.height)
			
			ResizableBase.mouseReleased(self, event)
			self.min_size = (0,0) # Override changes done in ResizableBase
		
		#FIXME: This is a little bit of a hack to fix Ticket #444
		self.adaptLayout()