Bump version to 0.9.1.
[python/dscho.git] / Mac / Demo / PICTbrowse / PICTbrowse2.py
blobc78960cc29b0c2ce469852f6ebf82d09f29e9e95
1 """browsepict - Display all "PICT" resources found"""
3 import FrameWork
4 import EasyDialogs
5 import Res
6 import Qd
7 import Win
8 import Controls
9 import List
10 import sys
11 import struct
14 # Resource definitions
15 ID_MAIN=512
16 MAIN_LIST=1
17 MAIN_SHOW=2
19 # Where is the picture window?
20 LEFT=200
21 TOP=64
22 MINWIDTH=64
23 MINHEIGHT=64
24 MAXWIDTH=320
25 MAXHEIGHT=320
27 def main():
28 try:
29 dummy = Res.GetResource('DLOG', ID_MAIN)
30 except Res.Error:
31 try:
32 Res.FSpOpenResFile("PICTbrowse.rsrc", 1)
33 except Res.Error, arg:
34 EasyDialogs.Message("Cannot open PICTbrowse.rsrc: "+arg[1])
35 sys.exit(1)
36 PICTbrowse()
38 class PICTbrowse(FrameWork.Application):
39 def __init__(self):
40 # First init menus, etc.
41 FrameWork.Application.__init__(self)
42 # Next create our dialog
43 self.main_dialog = MyDialog(self)
44 # Now open the dialog
45 contents = self.findPICTresources()
46 self.main_dialog.open(ID_MAIN, contents)
47 # Finally, go into the event loop
48 self.mainloop()
50 def makeusermenus(self):
51 self.filemenu = m = FrameWork.Menu(self.menubar, "File")
52 self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
54 def quit(self, *args):
55 self._quit()
57 def showPICT(self, resid):
58 w = PICTwindow(self)
59 w.open(resid)
60 #EasyDialogs.Message('Show PICT '+`resid`)
62 def findPICTresources(self):
63 num = Res.CountResources('PICT')
64 rv = []
65 for i in range(1, num+1):
66 Res.SetResLoad(0)
67 try:
68 r = Res.GetIndResource('PICT', i)
69 finally:
70 Res.SetResLoad(1)
71 id, type, name = r.GetResInfo()
72 rv.append((id, name))
73 return rv
75 class PICTwindow(FrameWork.Window):
76 def open(self, (resid, resname)):
77 if not resname:
78 resname = '#'+`resid`
79 self.resid = resid
80 self.picture = Qd.GetPicture(self.resid)
81 # Get rect for picture
82 sz, t, l, b, r = struct.unpack('hhhhh', self.picture.data[:10])
83 self.pictrect = (l, t, r, b)
84 width = r-l
85 height = b-t
86 if width < MINWIDTH: width = MINWIDTH
87 elif width > MAXWIDTH: width = MAXWIDTH
88 if height < MINHEIGHT: height = MINHEIGHT
89 elif height > MAXHEIGHT: height = MAXHEIGHT
90 bounds = (LEFT, TOP, LEFT+width, TOP+height)
92 self.wid = Win.NewWindow(bounds, resname, 1, 0, -1, 1, 0)
93 self.do_postopen()
95 def do_update(self, *args):
96 currect = self.fitrect()
97 Qd.DrawPicture(self.picture, currect)
99 def fitrect(self):
100 """Return self.pictrect scaled to fit in window"""
101 graf = self.wid.GetWindowPort()
102 screenrect = graf.portRect
103 picwidth = self.pictrect[2] - self.pictrect[0]
104 picheight = self.pictrect[3] - self.pictrect[1]
105 if picwidth > screenrect[2] - screenrect[0]:
106 factor = float(picwidth) / float(screenrect[2]-screenrect[0])
107 picwidth = picwidth / factor
108 picheight = picheight / factor
109 if picheight > screenrect[3] - screenrect[1]:
110 factor = float(picheight) / float(screenrect[3]-screenrect[1])
111 picwidth = picwidth / factor
112 picheight = picheight / factor
113 return (screenrect[0], screenrect[1], screenrect[0]+int(picwidth),
114 screenrect[1]+int(picheight))
116 class MyDialog(FrameWork.DialogWindow):
117 "Main dialog window for PICTbrowse"
119 def open(self, id, contents):
120 self.id = id
121 FrameWork.DialogWindow.open(self, ID_MAIN)
122 self.wid.SetDialogDefaultItem(MAIN_SHOW)
123 self.contents = contents
124 self.ctl = self.wid.GetDialogItemAsControl(MAIN_LIST)
125 h = self.ctl.GetControlDataHandle(Controls.kControlListBoxPart,
126 Controls.kControlListBoxListHandleTag)
127 self.list = List.as_List(h)
128 self.setlist()
130 def setlist(self):
131 self.list.LDelRow(0, 0)
132 self.list.LSetDrawingMode(0)
133 if self.contents:
134 self.list.LAddRow(len(self.contents), 0)
135 for i in range(len(self.contents)):
136 v = `self.contents[i][0]`
137 if self.contents[i][1]:
138 v = v + '"' + self.contents[i][1] + '"'
139 self.list.LSetCell(v, (0, i))
140 self.list.LSetDrawingMode(1)
141 self.list.LUpdate(self.wid.GetWindowPort().visRgn)
143 def getselection(self):
144 items = []
145 point = (0,0)
146 while 1:
147 ok, point = self.list.LGetSelect(1, point)
148 if not ok:
149 break
150 items.append(point[1])
151 point = point[0], point[1]+1
152 values = []
153 for i in items:
154 values.append(self.contents[i])
155 return values
157 def do_show(self, *args):
158 selection = self.getselection()
159 for resid in selection:
160 self.parent.showPICT(resid)
162 def do_close(self):
163 self.close()
165 def do_itemhit(self, item, event):
166 if item == MAIN_SHOW:
167 self.do_show()
169 main()