1 """
2 - /*
3 - * Copyright 2008 Google Inc.
4 - * Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
5 - * Copyright (C) 2010 Rich Newpol <rich.newpol@gmail.com>
6 - *
7 - * Licensed under the Apache License, Version 2.0 (the "License") you may not
8 - * use this file except in compliance with the License. You may obtain a copy
9 - * of the License at
10 - *
11 - * http:#www.apache.org/licenses/LICENSE-2.0
12 - *
13 - * Unless required by applicable law or agreed to in writing, software
14 - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 - * License for the specific language governing permissions and limitations
17 - * under the License.
18 - */
19 """
20
21 from __pyjamas__ import console
22 from pyjamas import DOM
23 from pyjamas import Window
24 from pyjamas import DeferredCommand
25 from pyjamas.EventController import EventGenerator
26 from pyjamas.ui import GlassWidget
27 from pyjamas.ui.SimplePanel import SimplePanel
28 from pyjamas.ui.AbsolutePanel import AbsolutePanel
29 from pyjamas.ui.ScrollPanel import ScrollPanel
30 from pyjamas.ui.MouseListener import MouseHandler, fireMouseEvent
31 from pyjamas.ui import Event
32
33
35 """ a splitter is just a SimplePanel which can receive mouse events """
36
37 - def __init__(self, splitPanel, **kwargs):
47
58
59
60 -class SplitPanel(AbsolutePanel, MouseHandler, EventGenerator):
61 """ Provides the SplitPanel baseclass functionality
62 A SplitPanel is an AbsolutePanel containing an HTMLTable
63 with three cells. The first cell holds the first ScrollPanel,
64 while the center cell holds a Splitter, and the last cell
65 holds the other ScrollPanel.
66 """
67
68 - def __init__(self, vertical=False, **kwargs):
69
70 if not 'StyleName' in kwargs:
71 if vertical:
72 kwargs['StyleName'] = "gwt-VerticalSplitPanel"
73 else:
74 kwargs['StyleName'] = "gwt-HorizontalSplitPanel"
75
76 self._drag_start = None
77 self._pos = "50%"
78
79 self._vertical = vertical
80
81 AbsolutePanel.__init__(self, **kwargs)
82 MouseHandler.__init__(self)
83
84 self.addListenedEvent("Resize")
85
86 self._container1 = ScrollPanel()
87
88 self._container2 = ScrollPanel()
89
90 self._splitter = SplitPanelSplitter(self)
91
92 self._splitter.addMouseListener(self)
93
94 self.addMouseListener(self)
95
96 AbsolutePanel.add(self, self._container1, 0, 0)
97 AbsolutePanel.add(self, self._splitter, 0, 0)
98 AbsolutePanel.add(self, self._container2, 0, 0)
99
100
101 if vertical:
102 self._splitter.setStyleName("vsplitter")
103 self._splitter.setWidth("100%")
104 self._container1.setWidth("100%")
105 self._container2.setWidth("100%")
106
107 DOM.setStyleAttribute(self._splitter.getElement(),
108 "cursor", "n-resize")
109 else:
110 self._splitter.setStyleName("hsplitter")
111 self._splitter.setHeight("100%")
112 self._container1.setHeight("100%")
113 self._container2.setHeight("100%")
114
115 DOM.setStyleAttribute(self._splitter.getElement(),
116 "cursor", "e-resize")
117
121
122
124 finalized = False
125 if self._vertical:
126 if pos is None:
127 pos = self._container1.getOffsetHeight()
128 space = self.getOffsetHeight()
129 sz = self._splitter.getOffsetHeight()
130 if space > 0 and sz > 0 and pos > 0:
131
132 if pos > space - sz:
133 pos = space - sz
134 self._container1.setHeight(pos)
135 self.setWidgetPosition(self._splitter, 0, pos)
136 self.setWidgetPosition(self._container2, 0, pos + sz)
137 self._container2.setHeight(space - (pos + sz))
138 finalized = True
139 else:
140 if pos is None:
141 pos = self._container1.getOffsetWidth()
142 space = self.getOffsetWidth()
143 sz = self._splitter.getOffsetWidth()
144 if space > 0 and sz > 0 and pos > 0:
145
146 if pos > space - sz:
147 pos = space - sz
148 self._container1.setWidth(pos)
149 self.setWidgetPosition(self._splitter, pos, 0)
150 self.setWidgetPosition(self._container2, pos + sz, 0)
151 self._container2.setWidth(space - (pos + sz))
152 finalized = True
153 if finalized:
154 self.dispatchResizeEvent(self, pos)
155 return finalized
156
157
166
168 return self._drag_start is not None
169
170
172 if self._drag_start is None:
173
174 if self._vertical:
175 self._drag_start = y
176 else:
177 self._drag_start = x
178
179
180 GlassWidget.show(self)
181
182
183
185
186
187 if not self._isDragging():
188 return
189
190
191 if self._vertical:
192 self._pos = y - self._drag_start
193 else:
194 self._pos = x - self._drag_start
195
196 if self._pos < 1:
197 self._pos = 1
198
199 self.setSplitPosition()
200
211
212
215
216
217
221
222
223
224
225
226
227
228
229
231 if pos is not None:
232
233 self._pos = pos
234 else:
235 pos = self._pos
236 if pos < 1:
237 pos = 1
238 self._pos = pos
239
240 if self._vertical:
241 self._container1.setHeight(pos)
242 else:
243 self._container1.setWidth(pos)
244
245 finalized = False
246 if isinstance(pos, int):
247 finalized = self._finalizePositions(pos)
248
249 if not finalized:
250 DeferredCommand.add(self._finalizePositions)
251
256
261
262
263 - def add(self, widget):
264 if self.getWidget(0) == None:
265 self.setWidget(0, widget)
266 elif self.getWidget(1) == None:
267 self.setWidget(1, widget)
268 else:
269 console.error("SimplePanel can only contain one child widget")
270
271
279
280
285
286
289
290
293
294
297
298
301
302
305
308