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