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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
<!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>PmwNoteBookR.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
<FONT COLOR="#0000DD">"""
I really should define a NBFrame widget according to the Tix specifications.
Thus, I could refer to the Tix documentation.
"""</FONT>
import sys
import string
import Tkinter
import Pmw
TRUE = 1
FALSE = 0
DARKGRAY = <FONT COLOR="#009900">'gray60'</FONT>
LIGHTGRAY = <FONT COLOR="#009900">'gray82'</FONT>
<STRONG><FONT COLOR="#CC6600">class NoteBookPage</FONT></STRONG>( Pmw.MegaWidget ):
<STRONG> def __init__</STRONG>(self, notebook, pagename, **kw):
self.notebook = notebook
self.pagename = pagename
<FONT COLOR="#DD0000"># Define the megawidget options.</FONT>
INITOPT = Pmw.INITOPT
optiondefs = (
(<FONT COLOR="#009900">'anchor'</FONT>, <FONT COLOR="#009900">'c'</FONT>, INITOPT),
#(<FONT COLOR="#009900">'bitmap'</FONT>, None, INITOPT),
(<FONT COLOR="#009900">'balloonHelp'</FONT>, <FONT COLOR="#009900">''</FONT>, None),
(<FONT COLOR="#009900">'statusHelp'</FONT>, None, None),
(<FONT COLOR="#009900">'createcmd'</FONT>, None, None),
#(<FONT COLOR="#009900">'image'</FONT>, None, INITOPT),
(<FONT COLOR="#009900">'justify'</FONT>, None, INITOPT),
(<FONT COLOR="#009900">'label'</FONT>, "", INITOPT),
(<FONT COLOR="#009900">'raisecmd'</FONT>, None, None),
(<FONT COLOR="#009900">'lowercmd'</FONT>, None, None),
(<FONT COLOR="#009900">'state'</FONT>, <FONT COLOR="#009900">'normal'</FONT>, self._setstate),
(<FONT COLOR="#009900">'underline'</FONT>, 0, INITOPT),
#(<FONT COLOR="#009900">'wraplength'</FONT>, 0, INITOPT)
)
self.defineoptions(kw, optiondefs)
<FONT COLOR="#DD0000"># Initialise the base class (after defining the options).</FONT>
Pmw.MegaWidget.__init__(self, notebook.interior())
<FONT COLOR="#DD0000"># Create the components.</FONT>
interior = Pmw.MegaWidget.interior(self)
l = Tkinter.Label(self.notebook.component(<FONT COLOR="#009900">'nbframe'</FONT>))
font = l.cget(<FONT COLOR="#009900">'font'</FONT>)
l.destroy()
del l
self._button = self.createcomponent(
<FONT COLOR="#009900">'button'</FONT>,
(), None,
Tkinter.Button, (self.notebook.interior(),),
bd=0, relief=<FONT COLOR="#009900">'flat'</FONT>,
text=self[<FONT COLOR="#009900">'label'</FONT>],
font=font,
underline=self[<FONT COLOR="#009900">'underline'</FONT>],
command=self.lift
)
if not self.notebook[<FONT COLOR="#009900">'balloon'</FONT>] is None:
self.notebook[<FONT COLOR="#009900">'balloon'</FONT>].bind(
self.component(<FONT COLOR="#009900">'button'</FONT>),
self[<FONT COLOR="#009900">'balloonHelp'</FONT>],
self[<FONT COLOR="#009900">'statusHelp'</FONT>]
)
<FONT COLOR="#DD0000"># Check keywords and initialise options.</FONT>
self.initialiseoptions(NoteBookPage)
self._iscreated = FALSE
<STRONG> def _setstate</STRONG>(self):
self.component(<FONT COLOR="#009900">'button'</FONT>).configure(state=self[<FONT COLOR="#009900">'state'</FONT>])
<STRONG> def lift</STRONG>(self):
if not self._iscreated:
if not self[<FONT COLOR="#009900">'createcmd'</FONT>] is None: self[<FONT COLOR="#009900">'createcmd'</FONT>]()
self._iscreated = TRUE
if not self[<FONT COLOR="#009900">'raisecmd'</FONT>] is None: self[<FONT COLOR="#009900">'raisecmd'</FONT>]()
if self.cget(<FONT COLOR="#009900">'state'</FONT>) == <FONT COLOR="#009900">'normal'</FONT>:
self.notebook.lift(self.pagename)
<STRONG> def _lower</STRONG>(self):
<FONT COLOR="#0000DD">"""
Should no be called directly: top is lowered when other is raised !
Whereas the page.lift() calls the notebook.lift(), page._lower() is
called *from* notebook.lift(). Asymmetric indeed, but don't forget
that this is caused by the restriction that only one page may be
raised.
The reason for having this page._lower() method is to call the
lowercmd if defined, just as the page.lift() method calls the
raisecmd.
"""</FONT>
if not self[<FONT COLOR="#009900">'lowercmd'</FONT>] is None: self[<FONT COLOR="#009900">'lowercmd'</FONT>]
<STRONG> def req_size</STRONG>(self):
self.update_idletasks()
return self.winfo_reqwidth(), self.winfo_reqheight()
Pmw.forwardmethods(NoteBookPage, Tkinter.Frame, <FONT COLOR="#009900">'_hull'</FONT>)
<STRONG><FONT COLOR="#CC6600">class NoteBookR</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">'dynamicgeometry'</FONT>, FALSE, INITOPT ),# deplorable option!
(<FONT COLOR="#009900">'balloon'</FONT>, None, None),
(<FONT COLOR="#009900">'ipadx'</FONT>, 4, INITOPT),
(<FONT COLOR="#009900">'ipady'</FONT>, 4, 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 = Pmw.MegaWidget.interior(self)
self._nbframe = self.createcomponent(
<FONT COLOR="#009900">'nbframe'</FONT>,
(), None,
Tkinter.Canvas, (self.component(<FONT COLOR="#009900">'hull'</FONT>),),
bd=0,
height=0,
width=0
)
self._nbframe.pack(side=<FONT COLOR="#009900">'top'</FONT>,padx=0,pady=0)
<FONT COLOR="#DD0000"># Check keywords and initialise options.</FONT>
self.initialiseoptions(NoteBookR)
self._pages = [] <FONT COLOR="#DD0000"># [name1, name2, ...]</FONT>
self._pagedict = {} <FONT COLOR="#DD0000"># { name: NoteBookPageObject}</FONT>
self._currentpage = None <FONT COLOR="#DD0000"># name (!) of currently raised page</FONT>
self._indexpage = -1 <FONT COLOR="#DD0000"># index of currently raised page</FONT>
<FONT COLOR="#DD0000"># Variables used for sizing the Canvas and painting the tabs.</FONT>
self._tabheight = 0
self._tabwidth = 0
self._borderwidth = 2
self._pagewidth = 0
self._pageheight = 0
self._nbwidth = 2*self._borderwidth
self._nbheight = 2*self._borderwidth
self._nbcenterx = 0
self._nbcentery = 0
self._tablineitems = [<FONT COLOR="#009900">'_light'</FONT>,<FONT COLOR="#009900">'_dark'</FONT>, <FONT COLOR="#009900">'_topline'</FONT>,<FONT COLOR="#009900">'_button'</FONT>,<FONT COLOR="#009900">'_bottom'</FONT>]
<FONT COLOR="#DD0000"># XXX Necessary, but clumsy to look at: really should be the </FONT>
<FONT COLOR="#DD0000"># Toplevel configure at which the method should be called, that is,</FONT>
<FONT COLOR="#DD0000"># before other widgets are already displayed.</FONT>
self.bind(<FONT COLOR="#009900">'<Configure>'</FONT>, self.initialise)
<STRONG> def initialise</STRONG>(self,e=None):
self._makereqsize()
self._drawborder()
self.lift(0)
<STRONG> def interior</STRONG>(self):
return self._nbframe
<STRONG> def _drawtab</STRONG>(self,newpage):
tabcanvas = self.component(<FONT COLOR="#009900">'nbframe'</FONT>)
d = self._borderwidth
b = newpage.component(<FONT COLOR="#009900">'button'</FONT>)
w = b.winfo_reqwidth()
if self._tabwidth == 0:
x = self._tabwidth
self._tabwidth = 0
else:
x = self._tabwidth
pagename = newpage.pagename
tabcanvas.create_window(
x+2*d,d+1,window=b,anchor=<FONT COLOR="#009900">'nw'</FONT>,tags=pagename+<FONT COLOR="#009900">'_button'</FONT>)
self._tabwidth = self._tabwidth + 3*d + w
oldwidth = string.atoi(tabcanvas.cget(<FONT COLOR="#009900">'width'</FONT>))
if oldwidth < self._tabwidth:
tabcanvas.configure(width=self._tabwidth)
h = self._tabheight
lightcoords = (
x+d, h,
x+d, 3*d,
x+2*d-1, d,
x+2*d+w-1, d,
x+2*d+w+-2+2*d, d+2*d
)
shadowcoords = (
x+3*d+w, d+2*d+1,
x+3*d+w, h-d+1
)
toplinecoords = (
x, h-2*d,
x, d+2*d-1,
x+2*d-1, d-1,
x+d+w+1, d-1,
x+3*d+w, d+2*d,
x+3*d+w, h-d+1
)
lightkw = {<FONT COLOR="#009900">'fill'</FONT>: <FONT COLOR="#009900">'white'</FONT>, <FONT COLOR="#009900">'width'</FONT>: d, <FONT COLOR="#009900">'tags'</FONT>: pagename+<FONT COLOR="#009900">'_light'</FONT>}
shadowkw = {<FONT COLOR="#009900">'fill'</FONT>: DARKGRAY, <FONT COLOR="#009900">'width'</FONT>: d, <FONT COLOR="#009900">'tags'</FONT>: pagename+<FONT COLOR="#009900">'_dark'</FONT>}
toplinekw = {<FONT COLOR="#009900">'fill'</FONT>: DARKGRAY, <FONT COLOR="#009900">'width'</FONT>: 1,<FONT COLOR="#009900">'tags'</FONT>: pagename+<FONT COLOR="#009900">'_topline'</FONT>}
apply( tabcanvas.create_line, lightcoords, lightkw )
apply( tabcanvas.create_line, shadowcoords, shadowkw )
apply( tabcanvas.create_line, toplinecoords, toplinekw )
tabcanvas.lower( pagename+<FONT COLOR="#009900">'_topline'</FONT> )
tabcanvas.create_line(
x+d, h,
x+4*d+w, h,
fill=<FONT COLOR="#009900">'white'</FONT>,width=d,tags=pagename+<FONT COLOR="#009900">'_bottom'</FONT>
)
<STRONG> def _drawborder</STRONG>(self):
tabcanvas = self.component(<FONT COLOR="#009900">'nbframe'</FONT>)
tw = string.atoi(tabcanvas.cget(<FONT COLOR="#009900">'width'</FONT>))
th = string.atoi(tabcanvas.cget(<FONT COLOR="#009900">'height'</FONT>))
d = self._borderwidth
tabcanvas.delete(<FONT COLOR="#009900">'border'</FONT>)
tabcanvas.create_line(
d,self._tabheight-2*d,
d,self._tabheight+2*self[<FONT COLOR="#009900">'ipady'</FONT>]+self._pageheight,
fill=<FONT COLOR="#009900">'white'</FONT>,
width=self._borderwidth,
tags=<FONT COLOR="#009900">'border borderlight'</FONT>
)
<FONT COLOR="#DD0000">#print self._nbwidth - self._tabwidth</FONT>
<FONT COLOR="#DD0000">#print self._nbwidth - self._pagewidth</FONT>
tabcanvas.create_line(
self._tabwidth, self._tabheight,
self._nbwidth, self._tabheight,
fill=<FONT COLOR="#009900">'white'</FONT>,
width=self._borderwidth,
tags=<FONT COLOR="#009900">'border borderlight'</FONT>
)
tabcanvas.create_line(
d, self._tabheight+2*self[<FONT COLOR="#009900">'ipady'</FONT>]+self._pageheight,
self._nbwidth, self._tabheight+2*self[<FONT COLOR="#009900">'ipady'</FONT>]+self._pageheight,
self._nbwidth, self._tabheight-d,
fill=DARKGRAY,
width=self._borderwidth,
tags=<FONT COLOR="#009900">'border bordershadow'</FONT>
)
<STRONG> def add</STRONG>(self,pagename,**kw):
if self._pagedict.has_key(pagename):
msg = "Attempt to create a second tab with name <FONT COLOR="#009900">'%s'</FONT>." % pagename
raise ValueError, msg
newpage = apply( NoteBookPage, (self, pagename), kw )
setattr(self,pagename,newpage)
tabcanvas = self.component(<FONT COLOR="#009900">'nbframe'</FONT>)
d = self._borderwidth
<FONT COLOR="#DD0000"># Initialization:</FONT>
if not self._tabheight:
b = newpage.component(<FONT COLOR="#009900">'button'</FONT>)
self._tabheight = b.winfo_reqheight() + 2*d
tabcanvas.configure(height=self._tabheight)
INITIALIZE = TRUE
else:
INITIALIZE = FALSE
self._drawtab(newpage)
self._pages.append( (pagename,newpage) )
self._pagedict[pagename] = newpage
if INITIALIZE:
self.lift(pagename)
<STRONG> def _makereqsize</STRONG>(self):
self.update_idletasks()
tabcanvas = self.component(<FONT COLOR="#009900">'nbframe'</FONT>)
reqw = 1
reqh = 1
for page in self._pagedict.values():
w, h = page.req_size()
reqw = max([reqw,w])
reqh = max([reqh,h])
self._pagewidth = reqw
self._pageheight = reqh
self._nbwidth = max([self._tabwidth,self._pagewidth+2*self[<FONT COLOR="#009900">'ipadx'</FONT>]])
self._nbheight = self._tabheight + 2*self[<FONT COLOR="#009900">'ipady'</FONT>] + self._pageheight
self._nbcenterx = self._nbwidth/2
self._nbcentery = self._tabheight + self[<FONT COLOR="#009900">'ipady'</FONT>] + self._pageheight/2
tabcanvas.configure( width=self._nbwidth, height=self._nbheight )
<STRONG> def _undrawtab</STRONG>(self,delpage):
tabcanvas = self.component(<FONT COLOR="#009900">'nbframe'</FONT>)
d = self._borderwidth
b = delpage.component(<FONT COLOR="#009900">'button'</FONT>)
w = b.winfo_reqwidth()
x = self._tabwidth
h = self._tabheight
pagename = delpage.pagename
for item in self._tablineitems:
tabcanvas.delete(pagename+item)
i = 0
for name in map( lambda x: x[0], self._pages):
i = i+1
if name == pagename: break
for name in map( lambda x: x[0], self._pages[i:]):
for item in self._tablineitems:
tabcanvas.move(name+item,-(w+4*d),0)
if self._pagewidth < self._tabwidth:
tabcanvas.configure(width=self._tabwidth)
<STRONG> def tkdelete</STRONG>(self,pagename):
delpage = self._pagedict[pagename]
ip = self._indexpage
if self.raised() == pagename:
if self._indexpage < len(self._pages) - 1:
self.lift( self._pages[self._indexpage + 1][0] )
elif self._indexpage > 0:
self.lift( self._pages[self._indexpage - 1][0] )
else:
self._indexpage = -1
self._currentpage = None
b = delpage.component(<FONT COLOR="#009900">'button'</FONT>)
w = b.winfo_reqwidth()
d = self._borderwidth
tabcanvas = self.component(<FONT COLOR="#009900">'nbframe'</FONT>)
self._tabwidth = self._tabwidth - w - 4*d
self._undrawtab(delpage)
delpage.destroy()
delattr(self,pagename)
del self._pagedict[pagename]
self._pages = self._pages[:ip] + self._pages[ip+1:]
<STRONG> def pagecget</STRONG>(self,pagename,option):
return self._pagedict[pagename].cget(option)
<STRONG> def pageconfigure</STRONG>(self,pagename,**kw):
return apply( self._pagedict[pagename].configure, (), kw )
<STRONG> def pages</STRONG>(self):
return self._pagedict.keys()
<STRONG> def lift</STRONG>(self,pagenameOrIndex):
if type(pagenameOrIndex) == type(<FONT COLOR="#009900">''</FONT>):
pagename = pagenameOrIndex
else:
pagename = self._pages[pagenameOrIndex][0]
tabcanvas = self.component(<FONT COLOR="#009900">'nbframe'</FONT>)
<FONT COLOR="#DD0000"># deal with the present top page</FONT>
if not self._currentpage is None:
tabcanvas.itemconfigure( self._currentpage+<FONT COLOR="#009900">'_bottom'</FONT>, fill=<FONT COLOR="#009900">'white'</FONT>)
tabcanvas.lower( self._currentpage+<FONT COLOR="#009900">'_topline'</FONT> )
self._pagedict[self._currentpage]._lower()
tabcanvas.itemconfigure( pagename+<FONT COLOR="#009900">'_bottom'</FONT>, fill=LIGHTGRAY )
tabcanvas.lift( pagename+<FONT COLOR="#009900">'_topline'</FONT> )
self._currentpage = pagename
self._indexpage = map( lambda x: x[0], self._pages ).index(pagename)
p = self._pagedict[pagename]
tabcanvas.delete(<FONT COLOR="#009900">'pageframe'</FONT>)
tabcanvas.create_window(
self._nbcenterx,
self._nbcentery,
window=p,
anchor=<FONT COLOR="#009900">'c'</FONT>,
tags=<FONT COLOR="#009900">'pageframe'</FONT>
)
tkraise = lift
<STRONG> def raised</STRONG>(self):
return self._currentpage
<STRONG> def page</STRONG>(self,pagename):
return self._pagedict[pagename]
Pmw.forwardmethods(NoteBookR, Tkinter.Frame, <FONT COLOR="#009900">'_hull'</FONT>)
</PRE>
</BODY> </HTML>
|