1"""imgbrowse - Display pictures using img""" 2 3import FrameWork 4import EasyDialogs 5from Carbon import Res 6from Carbon import Qd 7from Carbon import QuickDraw 8from Carbon import Win 9#ifrom Carbon mport List 10import struct 11import img 12import imgformat 13import mac_image 14 15 16# Where is the picture window? 17LEFT=200 18TOP=64 19MINWIDTH=64 20MINHEIGHT=64 21MAXWIDTH=320 22MAXHEIGHT=320 23 24 25def main(): 26 print 'hello world' 27 imgbrowse() 28 29class imgbrowse(FrameWork.Application): 30 def __init__(self): 31 # First init menus, etc. 32 FrameWork.Application.__init__(self) 33 self.lastwin = None 34 # Finally, go into the event loop 35 self.mainloop() 36 37 def makeusermenus(self): 38 self.filemenu = m = FrameWork.Menu(self.menubar, "File") 39 self.openitem = FrameWork.MenuItem(m, "Open...", "O", self.opendoc) 40 self.infoitem = FrameWork.MenuItem(m, "Info", "I", self.info) 41 self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit) 42 43 def quit(self, *args): 44 self._quit() 45 46 def opendoc(self, *args): 47 pathname = EasyDialogs.AskFileForOpen() # Any file type 48 if not pathname: 49 return 50 bar = EasyDialogs.ProgressBar('Reading and converting...') 51 try: 52 rdr = img.reader(imgformat.macrgb16, pathname) 53 except img.error, arg: 54 EasyDialogs.Message(repr(arg)) 55 return 56 w, h = rdr.width, rdr.height 57 bar.set(10) 58 data = rdr.read() 59 del bar 60 pixmap = mac_image.mkpixmap(w, h, imgformat.macrgb16, data) 61 self.showimg(w, h, pixmap, data) 62 63 def showimg(self, w, h, pixmap, data): 64 win = imgwindow(self) 65 win.open(w, h, pixmap, data) 66 self.lastwin = win 67 68 def info(self, *args): 69 if self.lastwin: 70 self.lastwin.info() 71 72class imgwindow(FrameWork.Window): 73 def open(self, width, height, pixmap, data): 74 self.pixmap = pixmap 75 self.data = data 76 self.pictrect = (0, 0, width, height) 77 bounds = (LEFT, TOP, LEFT+width, TOP+height) 78 79 self.wid = Win.NewCWindow(bounds, "Picture", 1, 0, -1, 1, 0) 80 self.do_postopen() 81 82 def do_update(self, *args): 83 pass 84 currect = self.fitrect() 85 print 'PICT:', self.pictrect 86 print 'WIND:', currect 87 print 'ARGS:', (self.pixmap, self.wid.GetWindowPort().GetPortBitMapForCopyBits(), self.pictrect, 88 currect, QuickDraw.srcCopy, None) 89 self.info() 90 Qd.CopyBits(self.pixmap, self.wid.GetWindowPort().GetPortBitMapForCopyBits(), self.pictrect, 91 currect, QuickDraw.srcCopy, None) 92 93 def fitrect(self): 94 """Return self.pictrect scaled to fit in window""" 95 graf = self.wid.GetWindowPort() 96 screenrect = graf.GetPortBounds() 97 picwidth = self.pictrect[2] - self.pictrect[0] 98 picheight = self.pictrect[3] - self.pictrect[1] 99 if picwidth > screenrect[2] - screenrect[0]: 100 factor = float(picwidth) / float(screenrect[2]-screenrect[0]) 101 picwidth = picwidth / factor 102 picheight = picheight / factor 103 if picheight > screenrect[3] - screenrect[1]: 104 factor = float(picheight) / float(screenrect[3]-screenrect[1]) 105 picwidth = picwidth / factor 106 picheight = picheight / factor 107 return (screenrect[0], screenrect[1], screenrect[0]+int(picwidth), 108 screenrect[1]+int(picheight)) 109 110 def info(self): 111 graf = self.wid.GetWindowPort() 112 bits = graf.GetPortBitMapForCopyBits() 113 mac_image.dumppixmap(bits.pixmap_data) 114 115main() 116