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