Added 'list_only' option (and modified 'run()' to respect it).
[python/dscho.git] / Mac / Demo / PICTbrowse / PICTbrowse.py
blob412716e638c2d3da2d86581fed8960616d43ba49
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
22 def main():
23 try:
24 dummy = Res.GetResource('DLOG', ID_MAIN)
25 except Res.Error:
26 try:
27 Res.OpenResFile("PICTbrowse.rsrc")
28 except Res.Error, arg:
29 EasyDialogs.Message("Cannot open PICTbrowse.rsrc: "+arg[1])
30 sys.exit(1)
31 PICTbrowse()
33 class PICTbrowse(FrameWork.Application):
34 def __init__(self):
35 # First init menus, etc.
36 FrameWork.Application.__init__(self)
37 # Next create our dialog
38 self.main_dialog = MyDialog(self)
39 # Now open the dialog
40 contents = self.findPICTresources()
41 self.main_dialog.open(ID_MAIN, contents)
42 # Finally, go into the event loop
43 self.mainloop()
45 def makeusermenus(self):
46 self.filemenu = m = FrameWork.Menu(self.menubar, "File")
47 self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
49 def quit(self, *args):
50 self._quit()
52 def showPICT(self, resid):
53 w = PICTwindow(self)
54 w.open(resid)
55 #EasyDialogs.Message('Show PICT '+`resid`)
57 def findPICTresources(self):
58 num = Res.CountResources('PICT')
59 rv = []
60 for i in range(1, num+1):
61 Res.SetResLoad(0)
62 try:
63 r = Res.GetIndResource('PICT', i)
64 finally:
65 Res.SetResLoad(1)
66 id, type, name = r.GetResInfo()
67 rv.append(id, name)
68 return rv
70 class PICTwindow(FrameWork.Window):
71 def open(self, (resid, resname)):
72 if not resname:
73 resname = '#'+`resid`
74 self.resid = resid
75 picture = Qd.GetPicture(self.resid)
76 # Get rect for picture
77 print `picture.data[:16]`
78 sz, t, l, b, r = struct.unpack('hhhhh', picture.data[:10])
79 print 'pict:', t, l, b, r
80 width = r-l
81 height = b-t
82 if width < 64: width = 64
83 elif width > 480: width = 480
84 if height < 64: height = 64
85 elif height > 320: height = 320
86 bounds = (LEFT, TOP, LEFT+width, TOP+height)
87 print 'bounds:', bounds
89 self.wid = Win.NewWindow(bounds, resname, 1, 0, -1, 1, 0)
90 self.wid.SetWindowPic(picture)
91 self.do_postopen()
93 class MyDialog(FrameWork.DialogWindow):
94 "Main dialog window for PICTbrowse"
96 def open(self, id, contents):
97 self.id = id
98 FrameWork.DialogWindow.open(self, ID_MAIN)
99 self.wid.SetDialogDefaultItem(MAIN_SHOW)
100 tp, h, rect = self.wid.GetDialogItem(MAIN_LIST)
101 rect2 = rect[0]+1, rect[1]+1, rect[2]-17, rect[3]-17 # Scroll bar space
102 self.list = List.LNew(rect2, (0, 0, 1, len(contents)), (0,0), 0, self.wid,
103 0, 1, 1, 1)
104 self.contents = contents
105 self.setlist()
107 def setlist(self):
108 self.list.LDelRow(0, 0)
109 self.list.LSetDrawingMode(0)
110 if self.contents:
111 self.list.LAddRow(len(self.contents), 0)
112 for i in range(len(self.contents)):
113 v = `self.contents[i][0]`
114 if self.contents[i][1]:
115 v = v + '"' + self.contents[i][1] + '"'
116 self.list.LSetCell(v, (0, i))
117 self.list.LSetDrawingMode(1)
118 self.list.LUpdate(self.wid.GetWindowPort().visRgn)
120 def do_listhit(self, event):
121 (what, message, when, where, modifiers) = event
122 Qd.SetPort(self.wid)
123 where = Qd.GlobalToLocal(where)
124 print 'LISTHIT', where
125 if self.list.LClick(where, modifiers):
126 self.do_show()
128 def getselection(self):
129 items = []
130 point = (0,0)
131 while 1:
132 ok, point = self.list.LGetSelect(1, point)
133 if not ok:
134 break
135 items.append(point[1])
136 point = point[0], point[1]+1
137 values = []
138 for i in items:
139 values.append(self.contents[i])
140 return values
142 def do_show(self, *args):
143 selection = self.getselection()
144 for resid in selection:
145 self.parent.showPICT(resid)
147 def do_rawupdate(self, window, event):
148 tp, h, rect = self.wid.GetDialogItem(MAIN_LIST)
149 Qd.SetPort(self.wid)
150 Qd.FrameRect(rect)
151 self.list.LUpdate(self.wid.GetWindowPort().visRgn)
153 def do_activate(self, activate, event):
154 self.list.LActivate(activate)
156 def do_close(self):
157 self.close()
159 def do_itemhit(self, item, event):
160 if item == MAIN_LIST:
161 self.do_listhit(event)
162 if item == MAIN_SHOW:
163 self.do_show()
165 main()