Fix an amazing number of typos & malformed sentences reported by Detlef
[python/dscho.git] / Mac / Demo / PICTbrowse / PICTbrowse2.py
blobc2d9c17dda4d824ee165752d5c0b76dd0be32c06
1 """browsepict - Display all "PICT" resources found"""
3 import FrameWork
4 import EasyDialogs
5 import Res
6 import Qd
7 import Win
8 import List
9 import sys
10 import struct
13 # Resource definitions
14 ID_MAIN=512
15 MAIN_LIST=1
16 MAIN_SHOW=2
18 # Where is the picture window?
19 LEFT=200
20 TOP=64
21 MINWIDTH=64
22 MINHEIGHT=64
23 MAXWIDTH=320
24 MAXHEIGHT=320
26 def main():
27 try:
28 dummy = Res.GetResource('DLOG', ID_MAIN)
29 except Res.Error:
30 try:
31 Res.OpenResFile("PICTbrowse.rsrc")
32 except Res.Error, arg:
33 EasyDialogs.Message("Cannot open PICTbrowse.rsrc: "+arg[1])
34 sys.exit(1)
35 PICTbrowse()
37 class PICTbrowse(FrameWork.Application):
38 def __init__(self):
39 # First init menus, etc.
40 FrameWork.Application.__init__(self)
41 # Next create our dialog
42 self.main_dialog = MyDialog(self)
43 # Now open the dialog
44 contents = self.findPICTresources()
45 self.main_dialog.open(ID_MAIN, contents)
46 # Finally, go into the event loop
47 self.mainloop()
49 def makeusermenus(self):
50 self.filemenu = m = FrameWork.Menu(self.menubar, "File")
51 self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
53 def quit(self, *args):
54 self._quit()
56 def showPICT(self, resid):
57 w = PICTwindow(self)
58 w.open(resid)
59 #EasyDialogs.Message('Show PICT '+`resid`)
61 def findPICTresources(self):
62 num = Res.CountResources('PICT')
63 rv = []
64 for i in range(1, num+1):
65 Res.SetResLoad(0)
66 try:
67 r = Res.GetIndResource('PICT', i)
68 finally:
69 Res.SetResLoad(1)
70 id, type, name = r.GetResInfo()
71 rv.append(id, name)
72 return rv
74 class PICTwindow(FrameWork.Window):
75 def open(self, (resid, resname)):
76 if not resname:
77 resname = '#'+`resid`
78 self.resid = resid
79 self.picture = Qd.GetPicture(self.resid)
80 # Get rect for picture
81 sz, t, l, b, r = struct.unpack('hhhhh', self.picture.data[:10])
82 self.pictrect = (l, t, r, b)
83 width = r-l
84 height = b-t
85 if width < MINWIDTH: width = MINWIDTH
86 elif width > MAXWIDTH: width = MAXWIDTH
87 if height < MINHEIGHT: height = MINHEIGHT
88 elif height > MAXHEIGHT: height = MAXHEIGHT
89 bounds = (LEFT, TOP, LEFT+width, TOP+height)
91 self.wid = Win.NewWindow(bounds, resname, 1, 0, -1, 1, 0)
92 self.do_postopen()
94 def do_update(self, *args):
95 currect = self.fitrect()
96 Qd.DrawPicture(self.picture, currect)
98 def fitrect(self):
99 """Return self.pictrect scaled to fit in window"""
100 graf = self.wid.GetWindowPort()
101 screenrect = graf.portRect
102 picwidth = self.pictrect[2] - self.pictrect[0]
103 picheight = self.pictrect[3] - self.pictrect[1]
104 if picwidth > screenrect[2] - screenrect[0]:
105 factor = float(picwidth) / float(screenrect[2]-screenrect[0])
106 picwidth = picwidth / factor
107 picheight = picheight / factor
108 if picheight > screenrect[3] - screenrect[1]:
109 factor = float(picheight) / float(screenrect[3]-screenrect[1])
110 picwidth = picwidth / factor
111 picheight = picheight / factor
112 return (screenrect[0], screenrect[1], screenrect[0]+int(picwidth),
113 screenrect[1]+int(picheight))
115 class MyDialog(FrameWork.DialogWindow):
116 "Main dialog window for PICTbrowse"
118 def open(self, id, contents):
119 self.id = id
120 FrameWork.DialogWindow.open(self, ID_MAIN)
121 self.wid.SetDialogDefaultItem(MAIN_SHOW)
122 tp, h, rect = self.wid.GetDialogItem(MAIN_LIST)
123 rect2 = rect[0]+1, rect[1]+1, rect[2]-17, rect[3]-17 # Scroll bar space
124 self.list = List.LNew(rect2, (0, 0, 1, len(contents)), (0,0), 0, self.wid,
125 0, 1, 1, 1)
126 self.contents = contents
127 self.setlist()
129 def setlist(self):
130 self.list.LDelRow(0, 0)
131 self.list.LSetDrawingMode(0)
132 if self.contents:
133 self.list.LAddRow(len(self.contents), 0)
134 for i in range(len(self.contents)):
135 v = `self.contents[i][0]`
136 if self.contents[i][1]:
137 v = v + '"' + self.contents[i][1] + '"'
138 self.list.LSetCell(v, (0, i))
139 self.list.LSetDrawingMode(1)
140 self.list.LUpdate(self.wid.GetWindowPort().visRgn)
142 def do_listhit(self, event):
143 (what, message, when, where, modifiers) = event
144 Qd.SetPort(self.wid)
145 where = Qd.GlobalToLocal(where)
146 print 'LISTHIT', where
147 if self.list.LClick(where, modifiers):
148 self.do_show()
150 def getselection(self):
151 items = []
152 point = (0,0)
153 while 1:
154 ok, point = self.list.LGetSelect(1, point)
155 if not ok:
156 break
157 items.append(point[1])
158 point = point[0], point[1]+1
159 values = []
160 for i in items:
161 values.append(self.contents[i])
162 return values
164 def do_show(self, *args):
165 selection = self.getselection()
166 for resid in selection:
167 self.parent.showPICT(resid)
169 def do_rawupdate(self, window, event):
170 tp, h, rect = self.wid.GetDialogItem(MAIN_LIST)
171 Qd.SetPort(self.wid)
172 Qd.FrameRect(rect)
173 self.list.LUpdate(self.wid.GetWindowPort().visRgn)
175 def do_activate(self, activate, event):
176 self.list.LActivate(activate)
178 def do_close(self):
179 self.close()
181 def do_itemhit(self, item, event):
182 if item == MAIN_LIST:
183 self.do_listhit(event)
184 if item == MAIN_SHOW:
185 self.do_show()
187 main()