py-cvs-rel2_1 (Rev 1.2) merge
[python/dscho.git] / Mac / Demo / PICTbrowse / PICTbrowse.py
blob04d321c057f6b62429245276c42e2b37c3a58c55
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
23 def main():
24 try:
25 dummy = Res.GetResource('DLOG', ID_MAIN)
26 except Res.Error:
27 try:
28 Res.FSpOpenResFile("PICTbrowse.rsrc", 1)
29 except Res.Error, arg:
30 EasyDialogs.Message("Cannot open PICTbrowse.rsrc: "+arg[1])
31 sys.exit(1)
32 PICTbrowse()
34 class PICTbrowse(FrameWork.Application):
35 def __init__(self):
36 # First init menus, etc.
37 FrameWork.Application.__init__(self)
38 # Next create our dialog
39 self.main_dialog = MyDialog(self)
40 # Now open the dialog
41 contents = self.findPICTresources()
42 self.main_dialog.open(ID_MAIN, contents)
43 # Finally, go into the event loop
44 self.mainloop()
46 def makeusermenus(self):
47 self.filemenu = m = FrameWork.Menu(self.menubar, "File")
48 self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
50 def quit(self, *args):
51 self._quit()
53 def showPICT(self, resid):
54 w = PICTwindow(self)
55 w.open(resid)
56 #EasyDialogs.Message('Show PICT '+`resid`)
58 def findPICTresources(self):
59 num = Res.CountResources('PICT')
60 rv = []
61 for i in range(1, num+1):
62 Res.SetResLoad(0)
63 try:
64 r = Res.GetIndResource('PICT', i)
65 finally:
66 Res.SetResLoad(1)
67 id, type, name = r.GetResInfo()
68 rv.append((id, name))
69 return rv
71 class PICTwindow(FrameWork.Window):
72 def open(self, (resid, resname)):
73 if not resname:
74 resname = '#'+`resid`
75 self.resid = resid
76 picture = Qd.GetPicture(self.resid)
77 # Get rect for picture
78 print `picture.data[:16]`
79 sz, t, l, b, r = struct.unpack('hhhhh', picture.data[:10])
80 print 'pict:', t, l, b, r
81 width = r-l
82 height = b-t
83 if width < 64: width = 64
84 elif width > 480: width = 480
85 if height < 64: height = 64
86 elif height > 320: height = 320
87 bounds = (LEFT, TOP, LEFT+width, TOP+height)
88 print 'bounds:', bounds
90 self.wid = Win.NewWindow(bounds, resname, 1, 0, -1, 1, 0)
91 self.wid.SetWindowPic(picture)
92 self.do_postopen()
94 class MyDialog(FrameWork.DialogWindow):
95 "Main dialog window for PICTbrowse"
97 def open(self, id, contents):
98 self.id = id
99 FrameWork.DialogWindow.open(self, ID_MAIN)
100 self.wid.SetDialogDefaultItem(MAIN_SHOW)
101 self.contents = contents
102 self.ctl = self.wid.GetDialogItemAsControl(MAIN_LIST)
103 h = self.ctl.GetControlData_Handle(Controls.kControlListBoxPart,
104 Controls.kControlListBoxListHandleTag)
105 self.list = List.as_List(h)
106 self.setlist()
108 def setlist(self):
109 self.list.LDelRow(0, 0)
110 self.list.LSetDrawingMode(0)
111 if self.contents:
112 self.list.LAddRow(len(self.contents), 0)
113 for i in range(len(self.contents)):
114 v = `self.contents[i][0]`
115 if self.contents[i][1]:
116 v = v + '"' + self.contents[i][1] + '"'
117 self.list.LSetCell(v, (0, i))
118 self.list.LSetDrawingMode(1)
119 self.list.LUpdate(self.wid.GetWindowPort().visRgn)
121 def getselection(self):
122 items = []
123 point = (0,0)
124 while 1:
125 ok, point = self.list.LGetSelect(1, point)
126 if not ok:
127 break
128 items.append(point[1])
129 point = point[0], point[1]+1
130 values = []
131 for i in items:
132 values.append(self.contents[i])
133 return values
135 def do_show(self, *args):
136 selection = self.getselection()
137 for resid in selection:
138 self.parent.showPICT(resid)
140 def do_close(self):
141 self.close()
143 def do_itemhit(self, item, event):
144 if item == MAIN_SHOW:
145 self.do_show()
147 main()