blob: 262c650e4ac5cb20e267cc2218d9b2cfa89803ff [file] [log] [blame]
Jack Jansenb340acf2003-01-26 21:40:00 +00001"""imgbrowse - Display pictures using img"""
Jack Jansen4f508ad1995-12-12 15:05:11 +00002
3import FrameWork
4import EasyDialogs
Jack Jansen5a6fdcd2001-08-25 12:15:04 +00005from Carbon import Res
6from Carbon import Qd
7from Carbon import QuickDraw
8from Carbon import Win
9#ifrom Carbon mport List
Jack Jansen4f508ad1995-12-12 15:05:11 +000010import sys
11import struct
12import img
13import imgformat
Jack Jansen4f508ad1995-12-12 15:05:11 +000014import struct
Jack Jansen6f1ee201996-10-04 15:22:37 +000015import mac_image
Jack Jansen4f508ad1995-12-12 15:05:11 +000016
17
18# Where is the picture window?
19LEFT=200
20TOP=64
21MINWIDTH=64
22MINHEIGHT=64
23MAXWIDTH=320
24MAXHEIGHT=320
25
Jack Jansen4f508ad1995-12-12 15:05:11 +000026
27def main():
28 print 'hello world'
29 imgbrowse()
30
31class imgbrowse(FrameWork.Application):
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()
38
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):
Jack Jansenef5cd051996-09-17 12:39:12 +000046 self._quit()
Jack Jansen4f508ad1995-12-12 15:05:11 +000047
48 def opendoc(self, *args):
Jack Jansenb340acf2003-01-26 21:40:00 +000049 pathname = EasyDialogs.AskFileForOpen() # Any file type
50 if not pathname:
Jack Jansen4f508ad1995-12-12 15:05:11 +000051 return
52 bar = EasyDialogs.ProgressBar('Reading and converting...')
Jack Jansen4f508ad1995-12-12 15:05:11 +000053 try:
54 rdr = img.reader(imgformat.macrgb16, pathname)
55 except img.error, arg:
Walter Dörwald70a6b492004-02-12 17:35:32 +000056 EasyDialogs.Message(repr(arg))
Jack Jansen4f508ad1995-12-12 15:05:11 +000057 return
58 w, h = rdr.width, rdr.height
59 bar.set(10)
60 data = rdr.read()
61 del bar
Jack Jansen6f1ee201996-10-04 15:22:37 +000062 pixmap = mac_image.mkpixmap(w, h, imgformat.macrgb16, data)
63 self.showimg(w, h, pixmap, data)
Jack Jansen4f508ad1995-12-12 15:05:11 +000064
Jack Jansen6f1ee201996-10-04 15:22:37 +000065 def showimg(self, w, h, pixmap, data):
Jack Jansen4f508ad1995-12-12 15:05:11 +000066 win = imgwindow(self)
Jack Jansen6f1ee201996-10-04 15:22:37 +000067 win.open(w, h, pixmap, data)
Jack Jansen4f508ad1995-12-12 15:05:11 +000068 self.lastwin = win
69
70 def info(self, *args):
71 if self.lastwin:
72 self.lastwin.info()
73
74class imgwindow(FrameWork.Window):
Jack Jansen6f1ee201996-10-04 15:22:37 +000075 def open(self, width, height, pixmap, data):
Jack Jansen4f508ad1995-12-12 15:05:11 +000076 self.pixmap = pixmap
Jack Jansen6f1ee201996-10-04 15:22:37 +000077 self.data = data
Jack Jansen4f508ad1995-12-12 15:05:11 +000078 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
Jack Jansen362c7cd2002-11-30 00:01:29 +000089 print 'ARGS:', (self.pixmap, self.wid.GetWindowPort().GetPortBitMapForCopyBits(), self.pictrect,
Jack Jansen4f508ad1995-12-12 15:05:11 +000090 currect, QuickDraw.srcCopy, None)
91 self.info()
Jack Jansen362c7cd2002-11-30 00:01:29 +000092 Qd.CopyBits(self.pixmap, self.wid.GetWindowPort().GetPortBitMapForCopyBits(), self.pictrect,
Jack Jansen6f1ee201996-10-04 15:22:37 +000093 currect, QuickDraw.srcCopy, None)
Jack Jansen4f508ad1995-12-12 15:05:11 +000094
95 def fitrect(self):
96 """Return self.pictrect scaled to fit in window"""
97 graf = self.wid.GetWindowPort()
Jack Jansen362c7cd2002-11-30 00:01:29 +000098 screenrect = graf.GetPortBounds()
Jack Jansen4f508ad1995-12-12 15:05:11 +000099 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()
Jack Jansen362c7cd2002-11-30 00:01:29 +0000114 bits = graf.GetPortBitMapForCopyBits()
Jack Jansen6f1ee201996-10-04 15:22:37 +0000115 mac_image.dumppixmap(bits.pixmap_data)
Jack Jansen4f508ad1995-12-12 15:05:11 +0000116
117main()