import W import Wcontrols import Qd import Res import macfs import struct class Pict(W.Widget): """Create a widget containing a Quickdraw picture.""" def __init__(self, possize, picture, size=None): W.Widget.__init__(self, possize) self._picture = picture self._size = size self._hscroll = 0 self._vscroll = 0 def open(self): W.Widget.open(self) self.updatescrollbars() def draw(self, visRgn=None): if self._visible: if self._size == None: Qd.DrawPicture(self._picture, self._bounds) else: clip = Qd.NewRgn() Qd.RectRgn(clip, self._bounds) old = Qd.NewRgn() Qd.GetClip(old) Qd.SetClip(clip) Qd.DrawPicture(self._picture, (self._bounds[0]-self._hscroll, self._bounds[1]-self._vscroll, self._bounds[0]-self._hscroll+self._size[0], self._bounds[1]-self._vscroll+self._size[1])) Qd.SetClip(old) def adjust(self, oldbounds): W.Widget.adjust(self, oldbounds) self.updatescrollbars() # handle scrollbars def getscrollbarvalues(self): if not self._size: return None, None vx = Wcontrols._scalebarvalue(0, self._size[0], self._hscroll, self._hscroll+self._bounds[2]-self._bounds[0]) vy = Wcontrols._scalebarvalue(0, self._size[1], self._vscroll, self._vscroll+self._bounds[3]-self._bounds[1]) return vx, vy def updatescrollbars(self): vx, vy = self.getscrollbarvalues() if self._parent._barx: if vx <> None: self._parent._barx.enable(1) self._parent._barx.set(vx) else: self._parent._barx.set(0) self._parent._barx.enable(0) if self._parent._bary: if vy <> None: self._parent._bary.enable(1) self._parent._bary.set(vy) else: self._parent._bary.set(0) self._parent._bary.enable(0) def vscroll(self, value): destheight = self._bounds[3] - self._bounds[1] maxscroll = self._size[1] - destheight if value == "-": self._vscroll = min(self._vscroll+1, maxscroll) elif value == "+": self._vscroll = max(self._vscroll-1, 0) elif value == "--": self._vscroll = min(self._vscroll+destheight, maxscroll) elif value == "++": self._vscroll = max(self._vscroll-destheight, 0) else: self._vscroll = (value * maxscroll) / 32767 self.draw() self.updatescrollbars() def hscroll(self, value): destheight = self._bounds[2] - self._bounds[0] maxscroll = self._size[0] - destheight if value == "-": self._hscroll = min(self._hscroll+1, maxscroll) elif value == "+": self._hscroll = max(self._hscroll-1, 0) elif value == "--": self._hscroll = min(self._hscroll+destheight, maxscroll) elif value == "++": self._hscroll = max(self._hscroll-destheight, 0) else: self._hscroll = (value * maxscroll) / 32767 self.draw() self.updatescrollbars() if __name__ == "__main__": w = W.Window((200,200), "Picture", minsize=(50,50)) PictHandle = Qd.GetPicture(236) sz, t, l, b, r = struct.unpack("hhhhh", PictHandle.data[:10]) pict = Pict((0,0,-15,-15), PictHandle, ((r-l),(b-t))) w._barx = W.Scrollbar((0, -15, -14, 16), pict.hscroll, max = 32767) w._bary = W.Scrollbar((-15, 0, 16, -14), pict.vscroll, max = 32767) w.pict = pict w.open()