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