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 326 327 328 329 330 331 332
|
<!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>PmwNoteBookS.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
<FONT COLOR="#DD0000">#######################################################################</FONT>
<FONT COLOR="#DD0000"># File: PmwNoteBook.py</FONT>
<FONT COLOR="#DD0000"># Author: Joseph A. Saltiel - jsaltiel@lucent.com</FONT>
<FONT COLOR="#DD0000"># References: Pmw.0.4</FONT>
<FONT COLOR="#DD0000"># Modules: Tkinter, Pmw, string</FONT>
<FONT COLOR="#DD0000"># PmwNoteBook - This is a NoteBook Mega Widget made for Python.</FONT>
<FONT COLOR="#DD0000"># It is based on the framework and essentially is</FONT>
<FONT COLOR="#DD0000"># an extension of PMW (Python Mega Widgets). It </FONT>
<FONT COLOR="#DD0000"># allows the user to create a notebook, add and del</FONT>
<FONT COLOR="#DD0000"># pages. A page is a frame and the user can grab </FONT>
<FONT COLOR="#DD0000"># and put additional widgets on to it.</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: NoteBook</FONT>
<FONT COLOR="#DD0000"># Description: This defines the notebook widget. This includes </FONT>
<FONT COLOR="#DD0000"># its subcomponents and attributes.</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG><FONT COLOR="#CC6600">class NoteBookS</FONT></STRONG>(Pmw.MegaWidget):
<STRONG> def __init__</STRONG>(self, parent = None, **kw):
<FONT COLOR="#DD0000"># Define the megawidget options.</FONT>
INITOPT = Pmw.INITOPT
optiondefs = (
(<FONT COLOR="#009900">'tabColor'</FONT>, <FONT COLOR="#009900">'blue'</FONT>, None),
(<FONT COLOR="#009900">'canvasColor'</FONT>, <FONT COLOR="#009900">'white'</FONT>, None),
(<FONT COLOR="#009900">'activeColor'</FONT>, <FONT COLOR="#009900">'red'</FONT>, None),
(<FONT COLOR="#009900">'deactiveColor'</FONT>, <FONT COLOR="#009900">'grey'</FONT>, None),
(<FONT COLOR="#009900">'shadeColor'</FONT>, <FONT COLOR="#009900">'#666666'</FONT>, None),
(<FONT COLOR="#009900">'textColor'</FONT>, <FONT COLOR="#009900">'black'</FONT>, None),
(<FONT COLOR="#009900">'textFont'</FONT>, <FONT COLOR="#009900">'-*-helvetica-bold-r-normal--10-*-*-*-*-*'</FONT>, self._setFontLength),
(<FONT COLOR="#009900">'longX'</FONT>, 30, INITOPT),
(<FONT COLOR="#009900">'shortX'</FONT>, 7, INITOPT),
(<FONT COLOR="#009900">'longY'</FONT>, 35, INITOPT),
(<FONT COLOR="#009900">'shortY'</FONT>, 7, INITOPT),
(<FONT COLOR="#009900">'offsetY'</FONT>, 5, INITOPT),
(<FONT COLOR="#009900">'canvasHeight'</FONT>, 250, self._adjustHeight),
(<FONT COLOR="#009900">'canvasWidth'</FONT>, 400, self._adjustWidth),
(<FONT COLOR="#009900">'tabHeight'</FONT>, 40, INITOPT),
)
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"># Create the canvas widget.</FONT>
self._mainCanvas = self.createcomponent(<FONT COLOR="#009900">'mainCanvas'</FONT>,
(), None,
Tkinter.Canvas, (interior,),
bg=self[<FONT COLOR="#009900">'canvasColor'</FONT>], height=self[<FONT COLOR="#009900">'canvasHeight'</FONT>],
width=self[<FONT COLOR="#009900">'canvasWidth'</FONT>], highlightthickness=0,
bd=2, relief=<FONT COLOR="#009900">'raised'</FONT>)
self._mainCanvas.grid(sticky = <FONT COLOR="#009900">'news'</FONT>, ipadx=2, ipady=2)
<FONT COLOR="#DD0000"># Create the tab canvas widget.</FONT>
self._tabCanvas = self.createcomponent(<FONT COLOR="#009900">'tabCanvas'</FONT>,
(), None,
Tkinter.Canvas, (self._mainCanvas,),
bg=self[<FONT COLOR="#009900">'tabColor'</FONT>], height=self[<FONT COLOR="#009900">'tabHeight'</FONT>],
highlightthickness=0)
self._tabCanvas.grid(row=0, column=0, sticky = <FONT COLOR="#009900">'news'</FONT>)
<FONT COLOR="#DD0000"># Create the container canvas widget.</FONT>
self._containerCanvas = self.createcomponent(<FONT COLOR="#009900">'containerCanvas'</FONT>,
(), None,
Tkinter.Canvas, (self._mainCanvas,),
bg=self[<FONT COLOR="#009900">'activeColor'</FONT>], highlightthickness=0,
height=self[<FONT COLOR="#009900">'canvasHeight'</FONT>] - self[<FONT COLOR="#009900">'offsetY'</FONT>] - self[<FONT COLOR="#009900">'longY'</FONT>],
width=self[<FONT COLOR="#009900">'canvasWidth'</FONT>])
self._containerCanvas.grid(row=1, column=0, sticky=<FONT COLOR="#009900">'news'</FONT>)
<FONT COLOR="#DD0000"># Initialise instance variables.</FONT>
self._tabOrder = []
self._pages = {}
self._containers = {}
self._tabNames = {}
self._shade = {}
self._activePage = None
self._numofPages = 0
self._fontLength = 10
self._offsetX = 10
self._lineBorder = self._tabCanvas.create_line(0, self[<FONT COLOR="#009900">'tabHeight'</FONT>]-1, self._tabCanvas.winfo_reqwidth(), self[<FONT COLOR="#009900">'tabHeight'</FONT>]-1, fill=self[<FONT COLOR="#009900">'shadeColor'</FONT>])
self._mutex = 1
<FONT COLOR="#DD0000"># Check keywords and initialise options.</FONT>
self.initialiseoptions(NoteBookS)
<FONT COLOR="#DD0000">#**********************************************************************</FONT>
<FONT COLOR="#DD0000"># CONFIGURATION METHODS </FONT>
<FONT COLOR="#DD0000">#**********************************************************************</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: _setFontLength</FONT>
<FONT COLOR="#DD0000"># Description: This sets the font length. The font length is based</FONT>
<FONT COLOR="#DD0000"># on the font type used. This determines how many</FONT>
<FONT COLOR="#DD0000"># characters may fit on a tab.</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def _setFontLength</STRONG>(self):
try:
length = string.atoi(string.splitfields(self[<FONT COLOR="#009900">'textFont'</FONT>], <FONT COLOR="#009900">'-'</FONT>)[7])
self._fontLength = length
except:
message = <FONT COLOR="#009900">'Invalid Font specified. The 8th field, point size, must be specified'</FONT>
raise ValueError, message
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: _tabSelect</FONT>
<FONT COLOR="#DD0000"># Description: When a tab is selected, we raise it and lower the </FONT>
<FONT COLOR="#DD0000"># the other tabs.</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def _tabSelect</STRONG>(self, event=None):
if self._mutex:
for item in self._containers.keys():
self._containers[item].grid_forget()
for item in self._pages.keys():
if ((event==None) and (self._activePage != item)) or ((event) and (event.widget.gettags(self._pages[item])) == () and (event.widget.gettags(self._tabNames[item])) == ()):
self._tabCanvas.itemconfigure(self._pages[item], fill=self[<FONT COLOR="#009900">'deactiveColor'</FONT>], outline=self[<FONT COLOR="#009900">'deactiveColor'</FONT>])
self._tabCanvas.lower(self._pages[item])
else:
self.raisePage(item, select=None)
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: _adjustHeight</FONT>
<FONT COLOR="#DD0000"># Description: When the user changes the notebook height, we must make</FONT>
<FONT COLOR="#DD0000"># sure that information gets propageted to the proper components.</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def _adjustHeight</STRONG>(self):
frameHeight = self[<FONT COLOR="#009900">'canvasHeight'</FONT>] - self[<FONT COLOR="#009900">'offsetY'</FONT>] - self[<FONT COLOR="#009900">'longY'</FONT>]
self._mainCanvas.configure(height=self[<FONT COLOR="#009900">'canvasHeight'</FONT>])
self._mainCanvas.grid_rowconfigure(1, minsize=frameHeight)
self._containerCanvas.grid_rowconfigure(0, minsize=frameHeight)
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: _adjustWidth</FONT>
<FONT COLOR="#DD0000"># Description: When the user changes the notebook width, we must make</FONT>
<FONT COLOR="#DD0000"># sure that information gets propageted to the proper components.</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def _adjustWidth</STRONG>(self):
self._mainCanvas.configure(width=self[<FONT COLOR="#009900">'canvasWidth'</FONT>])
self._containerCanvas.grid_columnconfigure(0, minsize=self[<FONT COLOR="#009900">'canvasWidth'</FONT>])
<FONT COLOR="#DD0000">#**********************************************************************</FONT>
<FONT COLOR="#DD0000"># PRIVATE METHODS </FONT>
<FONT COLOR="#DD0000">#**********************************************************************</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: _getOffset</FONT>
<FONT COLOR="#DD0000"># Description: This determines where to draw a tab name on the tab</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def _getOffset</STRONG>(self, name):
length = len(name)
if length > (self[<FONT COLOR="#009900">'longX'</FONT>] / self._fontLength):
width = length * self._fontLength
else:
width = self[<FONT COLOR="#009900">'longX'</FONT>]
coords = (self._offsetX, self[<FONT COLOR="#009900">'longY'</FONT>]+self[<FONT COLOR="#009900">'offsetY'</FONT>], self._offsetX, self[<FONT COLOR="#009900">'shortY'</FONT>]+self[<FONT COLOR="#009900">'offsetY'</FONT>], self[<FONT COLOR="#009900">'shortX'</FONT>] + self._offsetX, self[<FONT COLOR="#009900">'offsetY'</FONT>], width + self._offsetX - self[<FONT COLOR="#009900">'shortX'</FONT>], self[<FONT COLOR="#009900">'offsetY'</FONT>], width+self._offsetX, self[<FONT COLOR="#009900">'shortY'</FONT>]+self[<FONT COLOR="#009900">'offsetY'</FONT>], self._offsetX+width, self[<FONT COLOR="#009900">'offsetY'</FONT>]+self[<FONT COLOR="#009900">'longY'</FONT>])
startX = self._offsetX+width/2
startY = self[<FONT COLOR="#009900">'offsetY'</FONT>]+self[<FONT COLOR="#009900">'longY'</FONT>]/2
self._offsetX = self._offsetX+width+4
return coords, startX, startY
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: _drawTab</FONT>
<FONT COLOR="#DD0000"># Description: This draws the tab on the tab Canvas</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def _drawTab</STRONG>(self, name):
self._tabSelect()
coords, startX, startY = self._getOffset(name)
node = self._tabCanvas.create_polygon(coords, fill=self[<FONT COLOR="#009900">'activeColor'</FONT>], outline=self[<FONT COLOR="#009900">'shadeColor'</FONT>])
pageText = self._tabCanvas.create_text(startX, startY, text=name, fill=self[<FONT COLOR="#009900">'textColor'</FONT>], font=self[<FONT COLOR="#009900">'textFont'</FONT>])
self._lineBorder = self._tabCanvas.create_line(0, self[<FONT COLOR="#009900">'tabHeight'</FONT>]-1, self._tabCanvas.winfo_width(), self[<FONT COLOR="#009900">'tabHeight'</FONT>]-1, fill=self[<FONT COLOR="#009900">'shadeColor'</FONT>])
self._tabCanvas.tag_bind(pageText, "<ButtonPress-1>", self._tabSelect)
self._tabCanvas.tag_bind(node, "<ButtonPress-1>", self._tabSelect)
self._pages[name] = node
self._tabNames[name] = pageText
<FONT COLOR="#DD0000">#**********************************************************************</FONT>
<FONT COLOR="#DD0000"># PUBLIC METHODS</FONT>
<FONT COLOR="#DD0000">#**********************************************************************</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: addPage</FONT>
<FONT COLOR="#DD0000"># Description: The user calls this to add a page/tab</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def addPage</STRONG>(self, name):
if self._tabNames.has_key(name):
message = <FONT COLOR="#009900">'Tab name already exists'</FONT>
raise NameError, message
self._activePage = name
self._drawTab(name)
frame = Tkinter.Frame(self._containerCanvas, bg=self[<FONT COLOR="#009900">'activeColor'</FONT>],
height=self[<FONT COLOR="#009900">'canvasHeight'</FONT>] - self[<FONT COLOR="#009900">'offsetY'</FONT>] - self[<FONT COLOR="#009900">'longY'</FONT>])
frame.grid(row=0, column=0, sticky=<FONT COLOR="#009900">'news'</FONT>)
self._numofPages = self._numofPages + 1
self._tabOrder.append(name)
self._containers[name] = frame
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: delPage</FONT>
<FONT COLOR="#DD0000"># Description: The user calls this to delete a page/tab</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def delPage</STRONG> (self, name):
if not(self._tabNames.has_key(name)):
message = <FONT COLOR="#009900">'That tab name does not exist'</FONT>
raise KeyError, message
for item in self._tabOrder:
self._tabCanvas.delete(self._pages[item], self._tabNames[item])
self._tabCanvas.dtag(self._pages[item], self._tabNames[item])
self._tabOrder.remove(name)
self._containers[name].destroy()
del self._pages[name]
del self._tabNames[name]
del self._containers[name]
self._numofPages = self._numofPages - 1
self._offsetX = 10
for each in self._tabOrder:
self._drawTab(each)
if self._numofPages > 0:
self.raisePage(each)
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: getPage</FONT>
<FONT COLOR="#DD0000"># Description: Given a name, returns that frame(page) to the user</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def getPage</STRONG>(self, name):
if self._containers.has_key(name):
return self._containers[name]
else:
message = <FONT COLOR="#009900">'That page does not exist'</FONT>
raise KeyError, message
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: pages</FONT>
<FONT COLOR="#DD0000"># Description: Returns a list of frames(pages) on the current notebook</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def pages</STRONG>(self):
lst = []
for item in self._containers.keys():
lst.append(self._containers[item])
return lst
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: raisePage</FONT>
<FONT COLOR="#DD0000"># Description: Raises the given tab name</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def raisePage</STRONG>(self, name, select=1):
if select:
if not(self._pages.has_key(name)):
message = <FONT COLOR="#009900">'That page does not exist'</FONT>
raise KeyError, message
self._tabSelect()
self._activePage = name
self._containers[name].grid(row=0, column=0, sticky=<FONT COLOR="#009900">'news'</FONT>)
self._tabCanvas.itemconfigure(self._pages[name], fill=self[<FONT COLOR="#009900">'activeColor'</FONT>], outline=self[<FONT COLOR="#009900">'shadeColor'</FONT>])
self._tabCanvas.lift(self._pages[name])
self._tabCanvas.lift(self._tabNames[name])
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: raised</FONT>
<FONT COLOR="#DD0000"># Description: Returns the name of the currently raised tab</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def raised</STRONG>(self):
return self._activePage
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: pageNames</FONT>
<FONT COLOR="#DD0000"># Description: Returns a list of the all the page names</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def pageNames</STRONG>(self):
return self._pages.keys()
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: unBind</FONT>
<FONT COLOR="#DD0000"># Description: Unbind the notebooks bindings</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def unBind</STRONG>(self):
self._mutex = 0
<FONT COLOR="#DD0000">##################################################################</FONT>
<FONT COLOR="#DD0000"># Method: reBind</FONT>
<FONT COLOR="#DD0000"># Description: Re bind the notebooks bindings</FONT>
<FONT COLOR="#DD0000">##################################################################</FONT>
<STRONG> def reBind</STRONG>(self):
self._mutex = 1
</PRE>
</BODY> </HTML>
|