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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
<!DOCTYPE HTML PUBLIC "-//Netscape_Microsoft//DTD HTML 3.0//EN">
<HTML>
<!-- This file generated using the Python HTMLgen module. -->
<HEAD>
<META NAME="GENERATOR" CONTENT="HTMLgen 1.1">
<TITLE>PmwScrolledCanvas.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
<FONT COLOR="#DD0000">#######################################################################</FONT>
<FONT COLOR="#DD0000"># File: PmwScrolledCanvas.py</FONT>
<FONT COLOR="#DD0000"># Author: Joseph A. Saltiel - jsaltiel@lucent.com</FONT>
<FONT COLOR="#DD0000"># References: Python Mega Widgets (Pmw)</FONT>
<FONT COLOR="#DD0000"># Modules: Tkinter, Pmw, string</FONT>
<FONT COLOR="#DD0000"># PmwScrolledCanvas - This is a ScrolledCanvas Mega Widget made for </FONT>
<FONT COLOR="#DD0000"># Python. It is based on the framework and </FONT>
<FONT COLOR="#DD0000"># is an extension of PMW (Python Mega Widgets). It </FONT>
<FONT COLOR="#DD0000"># allows the user to create a scrollable canvas.</FONT>
# The user can get the subcomponent <FONT COLOR="#009900">'canvas'</FONT> in which
<FONT COLOR="#DD0000"># to add more elements.</FONT>
<FONT COLOR="#DD0000">#</FONT>
<FONT COLOR="#DD0000"># Copyright (C) 1997 by Lucent Technologies, Inc. & Joseph Saltiel</FONT>
<FONT COLOR="#DD0000">#</FONT>
<FONT COLOR="#DD0000"># This program is free software; you can redistribute it and/or modify</FONT>
<FONT COLOR="#DD0000"># it under the terms of the GNU General Public License as published by</FONT>
<FONT COLOR="#DD0000"># the Free Software Foundation; either version 2 of the License, or</FONT>
<FONT COLOR="#DD0000"># (at your option) any later version.</FONT>
<FONT COLOR="#DD0000">#</FONT>
<FONT COLOR="#DD0000"># This program is distributed in the hope that it will be useful,</FONT>
<FONT COLOR="#DD0000"># but WITHOUT ANY WARRANTY; without even the implied warranty of</FONT>
<FONT COLOR="#DD0000"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</FONT>
<FONT COLOR="#DD0000"># GNU General Public License for more details.</FONT>
<FONT COLOR="#DD0000">#</FONT>
<FONT COLOR="#DD0000"># You should have received a copy of the GNU General Public License</FONT>
<FONT COLOR="#DD0000"># along with this program; if not, write to the Free Software</FONT>
<FONT COLOR="#DD0000"># Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.</FONT>
<FONT COLOR="#DD0000"># </FONT>
<FONT COLOR="#DD0000">#######################################################################</FONT>
import Tkinter
import Pmw
import string
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Class: ScrolledCanvas</FONT>
<FONT COLOR="#DD0000"># Description: This defines the scrolledCanvas mega-widget. This</FONT>
<FONT COLOR="#DD0000"># includes its subcomponents and attributes. To add</FONT>
# additional elements, use <FONT COLOR="#009900">'canvas'</FONT> component as the
<FONT COLOR="#DD0000"># parent (master).</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG><FONT COLOR="#CC6600">class ScrolledCanvas</FONT></STRONG>(Pmw.MegaWidget):
<STRONG> def __init__</STRONG>(self, parent = None, **kw):
<FONT COLOR="#DD0000"># Define the megawidget options.</FONT>
optiondefs = (
(<FONT COLOR="#009900">'height'</FONT>, 100, None),
(<FONT COLOR="#009900">'width'</FONT>, 100, None),
(<FONT COLOR="#009900">'background'</FONT>, <FONT COLOR="#009900">'white'</FONT>, None),
(<FONT COLOR="#009900">'bg'</FONT>, <FONT COLOR="#009900">'white'</FONT>, None),
(<FONT COLOR="#009900">'vscrollmode'</FONT>, <FONT COLOR="#009900">'dynamic'</FONT>, self._vscrollMode),
(<FONT COLOR="#009900">'hscrollmode'</FONT>, <FONT COLOR="#009900">'dynamic'</FONT>, self._hscrollMode),
)
self.defineoptions(kw, optiondefs)
<FONT COLOR="#DD0000"># Initialise the base class (after defining the options).</FONT>
Pmw.MegaWidget.__init__(self, parent)
<FONT COLOR="#DD0000"># Create the components.</FONT>
interior = self.interior()
<FONT COLOR="#DD0000">#set bg color</FONT>
if self[<FONT COLOR="#009900">'background'</FONT>] != <FONT COLOR="#009900">'white'</FONT>:
self[<FONT COLOR="#009900">'bg'</FONT>] = self[<FONT COLOR="#009900">'background'</FONT>]
<FONT COLOR="#DD0000"># Create the frame Canvas widget. This what is actually scrolled</FONT>
self._frameCanvas = self.createcomponent(<FONT COLOR="#009900">'frameCanvas'</FONT>,
(), None,
Tkinter.Canvas, (interior,),
bg=self[<FONT COLOR="#009900">'bg'</FONT>], highlightthickness=0,
scrollregion=(0,0, self[<FONT COLOR="#009900">'width'</FONT>], self[<FONT COLOR="#009900">'height'</FONT>]),
xscrollcommand=self._scrollCanvasX,
yscrollcommand=self._scrollCanvasY)
self._frameCanvas.grid(row = 0, column = 0, sticky = <FONT COLOR="#009900">'news'</FONT>)
interior.grid_rowconfigure(0, weight = 1, minsize = 0)
interior.grid_columnconfigure(0, weight = 1, minsize = 0)
<FONT COLOR="#DD0000">#Create a window on the frameCanvas. This should be the</FONT>
<FONT COLOR="#DD0000">#only element on it.</FONT>
self._imageCanvas = self.createcomponent(<FONT COLOR="#009900">'imageCanvas'</FONT>,
(), None,
Tkinter.Canvas, (interior,),
bg=self[<FONT COLOR="#009900">'bg'</FONT>], highlightthickness=0)
self._imageCanvas.grid_rowconfigure(0, weight=1)
self._imageCanvas.grid_columnconfigure(0, weight=1)
<FONT COLOR="#DD0000">#Create a canvas on the window. This is what the user should</FONT>
<FONT COLOR="#DD0000">#use to add elements.</FONT>
self._canvas = self.createcomponent(<FONT COLOR="#009900">'canvas'</FONT>,
(), None,
Tkinter.Canvas, (self._imageCanvas,),
bg=self[<FONT COLOR="#009900">'bg'</FONT>], highlightthickness=0,
width=self[<FONT COLOR="#009900">'width'</FONT>], height=self[<FONT COLOR="#009900">'height'</FONT>])
self._canvas.grid(sticky = <FONT COLOR="#009900">'news'</FONT>)
tag = <FONT COLOR="#009900">'SCSelect'</FONT> + str(self)
self.bind_class(tag, <FONT COLOR="#009900">'<Configure>'</FONT>, self._updateScroll, <FONT COLOR="#009900">"+"</FONT>)
self._canvas.bindtags(self._canvas.bindtags() + (tag,))
<FONT COLOR="#DD0000"># Create the vertical scrollbar</FONT>
self._vertScrollbar = self.createcomponent(<FONT COLOR="#009900">'vertscrollbar'</FONT>,
(), <FONT COLOR="#009900">'Scrollbar'</FONT>,
Tkinter.Scrollbar, (interior,),
orient=<FONT COLOR="#009900">'vertical'</FONT>, command=self._frameCanvas.yview,
bg=self[<FONT COLOR="#009900">'bg'</FONT>], highlightthickness=0)
<FONT COLOR="#DD0000"># Create the horizontal scrollbar</FONT>
self._horizScrollbar = self.createcomponent(<FONT COLOR="#009900">'horizscrollbar'</FONT>,
(), <FONT COLOR="#009900">'Scrollbar'</FONT>,
Tkinter.Scrollbar, (interior,),
orient=<FONT COLOR="#009900">'horizontal'</FONT>, command=self._frameCanvas.xview,
bg=self[<FONT COLOR="#009900">'bg'</FONT>], highlightthickness=0)
<FONT COLOR="#DD0000">#Create a dummy frame to fill hole in the grid</FONT>
self._dummyFrame = self.createcomponent(<FONT COLOR="#009900">'dummyFrame'</FONT>,
(), <FONT COLOR="#009900">'Frame'</FONT>,
Tkinter.Frame, (interior,),
bg=self[<FONT COLOR="#009900">'bg'</FONT>])
self._dummyFrame.grid(row=1, column=1, sticky=<FONT COLOR="#009900">'news'</FONT>)
interior.configure(bg=self[<FONT COLOR="#009900">'bg'</FONT>])
<FONT COLOR="#DD0000"># Initialise instance variables.</FONT>
self._vertMode = None
self._horizMode = None
self._horizRecurse = 0
self._scrollerX = 0
self._scrollerY = 0
self._canvasWin = self._frameCanvas.create_window(0, 0,
width=self[<FONT COLOR="#009900">'width'</FONT>], height=self[<FONT COLOR="#009900">'height'</FONT>],
anchor=<FONT COLOR="#009900">'nw'</FONT>, window=self._imageCanvas)
<FONT COLOR="#DD0000"># Check keywords and initialise options.</FONT>
self.initialiseoptions(ScrolledCanvas)
<FONT COLOR="#DD0000">#**********************************************************************</FONT>
<FONT COLOR="#DD0000"># CONFIGURATION METHODS </FONT>
<FONT COLOR="#DD0000">#**********************************************************************</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: _updateScroll</FONT>
<FONT COLOR="#DD0000"># Description: The Scroll bar only seems to update on change of </FONT>
<FONT COLOR="#DD0000"># window size. This will force it to update.</FONT>
<FONT COLOR="#DD0000">#################################################################</FONT>
<STRONG> def _updateScroll</STRONG>(self, event=None):
try:
self._scrollCanvasX()
self._scrollCanvasY()
except:
pass
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: _vscrollMode</FONT>
<FONT COLOR="#DD0000"># Description: This sets the vertical scrollbar mode</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def _vscrollMode</STRONG>(self):
mode = self[<FONT COLOR="#009900">'vscrollmode'</FONT>]
if mode == <FONT COLOR="#009900">'static'</FONT>:
self._vertScrollbarDisplay(1)
elif mode == <FONT COLOR="#009900">'dynamic'</FONT> or mode == <FONT COLOR="#009900">'none'</FONT>:
self._vertScrollbarDisplay(0)
else:
message = <FONT COLOR="#009900">'bad vscrollmode option "%s": should be static, dynamic, or none'</FONT> % mode
raise ValueError, message
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: _hscrollMode</FONT>
<FONT COLOR="#DD0000"># Description: This sets the horizontal scrollbar mode.</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def _hscrollMode</STRONG>(self):
mode = self[<FONT COLOR="#009900">'hscrollmode'</FONT>]
if mode == <FONT COLOR="#009900">'static'</FONT>:
self._horizScrollbarDisplay(1)
elif mode == <FONT COLOR="#009900">'dynamic'</FONT> or mode == <FONT COLOR="#009900">'none'</FONT>:
self._horizScrollbarDisplay(0)
else:
message = <FONT COLOR="#009900">'bad hscrollmode option "%s": should be static, dynamic, or none'</FONT> % mode
raise ValueError, message
<FONT COLOR="#DD0000">#**********************************************************************</FONT>
<FONT COLOR="#DD0000"># PRIVATE METHODS </FONT>
<FONT COLOR="#DD0000">#**********************************************************************</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: _vertScrollbarDisplay</FONT>
<FONT COLOR="#DD0000"># Description: This displays or erases the vertical scrollbar</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def _vertScrollbarDisplay</STRONG>(self, mode):
if mode != self._vertMode:
self._vertMode = mode
if self._vertMode:
self._scrollerY = 1
self._vertScrollbar.grid(row = 0, column = 1, sticky = <FONT COLOR="#009900">'ns'</FONT>)
else:
self._scrollerY = 0
self._vertScrollbar.grid_forget()
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: _horizScrollbarDisplay</FONT>
<FONT COLOR="#DD0000"># Description: This displays or erases the horizontal scrollbar</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def _horizScrollbarDisplay</STRONG>(self, mode):
if mode != self._horizMode:
self._horizMode = mode
if self._horizMode:
self._scrollerX = 1
self._horizScrollbar.grid(row = 1, column = 0, sticky = <FONT COLOR="#009900">'ew'</FONT>)
else:
self._scrollerX = 0
self._horizScrollbar.grid_forget()
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: _scrollCanvasX</FONT>
<FONT COLOR="#DD0000"># Description: This controls the X scroll bar</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def _scrollCanvasX</STRONG>(self, first=None, last=None):
if first:
self._horizScrollbar.set(first, last)
interior = self.interior()
newX = self._canvas.winfo_reqwidth()
fixedX = self._frameCanvas.winfo_width()
if (fixedX >= newX) and (self[<FONT COLOR="#009900">'hscrollmode'</FONT>] == <FONT COLOR="#009900">'dynamic'</FONT>) and self._scrollerX:
self._horizScrollbarDisplay(0)
elif (fixedX < newX) and (self[<FONT COLOR="#009900">'hscrollmode'</FONT>] == <FONT COLOR="#009900">'dynamic'</FONT>) and not(self._scrollerX):
self._horizScrollbarDisplay(1)
elements = self._canvas.find_all()
if elements:
self._adjustCanvasSize(sumX = max(fixedX, newX, apply(self._canvas.bbox, elements)[2]))
else:
self._adjustCanvasSize(sumX = max(fixedX, newX))
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: _scrollCanvasY</FONT>
<FONT COLOR="#DD0000"># Description: This controls the Y scroll bar</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def _scrollCanvasY</STRONG>(self, first=None, last=None):
if first:
self._vertScrollbar.set(first, last)
interior = self.interior()
newY = self._canvas.winfo_reqheight()
fixedY = self._frameCanvas.winfo_height()
if (fixedY >= newY) and (self[<FONT COLOR="#009900">'vscrollmode'</FONT>] == <FONT COLOR="#009900">'dynamic'</FONT>) and self._scrollerY:
self._vertScrollbarDisplay(0)
elif (fixedY < newY) and (self[<FONT COLOR="#009900">'vscrollmode'</FONT>] == <FONT COLOR="#009900">'dynamic'</FONT>) and not(self._scrollerY):
self._vertScrollbarDisplay(1)
elements = self._canvas.find_all()
if elements:
self._adjustCanvasSize(sumY = max(fixedY, newY, apply(self._canvas.bbox, elements)[3]))
else:
self._adjustCanvasSize(sumY = max(fixedY, newY))
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: _adjustCanvasSize</FONT>
<FONT COLOR="#DD0000"># Description: This adjust the scroll region to cover the entire display</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def _adjustCanvasSize</STRONG>(self, sumX=None, sumY=None):
change = 0
region = string.splitfields(self._frameCanvas[<FONT COLOR="#009900">'scrollregion'</FONT>])
if sumY:
newY = repr(sumY)
if self._frameCanvas.itemcget(self._canvasWin, <FONT COLOR="#009900">'height'</FONT>) != newY:
region[3] = newY
change = 1
self._frameCanvas.itemconfigure(self._canvasWin, height=sumY)
if sumX:
newX = repr(sumX)
if self._frameCanvas.itemcget(self._canvasWin, <FONT COLOR="#009900">'width'</FONT>) != newX:
change = 1
region[2] = newX
self._frameCanvas.itemconfigure(self._canvasWin, width=sumX)
if change:
newRegion = tuple(region)
self._frameCanvas.configure(scrollregion=newRegion)
<FONT COLOR="#DD0000">#**********************************************************************</FONT>
<FONT COLOR="#DD0000"># PUBLIC METHODS </FONT>
<FONT COLOR="#DD0000">#**********************************************************************</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: updateScrollbars</FONT>
<FONT COLOR="#DD0000"># Description: Public method to force the scrollbars/canvas to update</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def updateScrollbars</STRONG>(self):
self._frameCanvas.update_idletasks()
self. _updateScroll()
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: getCanvas</FONT>
<FONT COLOR="#DD0000"># Description: This returns the canvas to use</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def getCanvas</STRONG>(self):
return self._canvas
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: bbox</FONT>
<FONT COLOR="#DD0000"># Description: Need to explicitly forward this to override the</FONT>
<FONT COLOR="#DD0000"># stupid (grid_)bbox method inherited from </FONT>
<FONT COLOR="#DD0000"># Tkinter.Frame.Grid</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def bbox</STRONG>(self, index):
return self._canvas.bbox(index)
Pmw.forwardmethods(ScrolledCanvas, Tkinter.Canvas, <FONT COLOR="#009900">'_canvas'</FONT>)
</PRE>
</BODY> </HTML>
|