blob: 8600bb2a1e1a4cc9fec5351d02b7e75cb31bd31f [file] [log] [blame]
Jack Jansen3b23ed91999-12-23 14:45:02 +00001"""browsepict - Display all "PICT" resources found"""
2
3import FrameWork
4import EasyDialogs
Jack Jansen5a6fdcd2001-08-25 12:15:04 +00005from Carbon import Res
6from Carbon import Qd
7from Carbon import Win
8from Carbon import List
Jack Jansen3b23ed91999-12-23 14:45:02 +00009import sys
10import struct
Jack Jansen3c06b9a2001-08-27 21:41:23 +000011import macresource
Jack Jansen3b23ed91999-12-23 14:45:02 +000012
13#
14# Resource definitions
15ID_MAIN=512
16MAIN_LIST=1
17MAIN_SHOW=2
18
19# Where is the picture window?
20LEFT=200
21TOP=64
22
23def main():
Tim Peters182b5ac2004-07-18 06:16:08 +000024 macresource.need('DLOG', ID_MAIN, "oldPICTbrowse.rsrc")
25 PICTbrowse()
Jack Jansen3b23ed91999-12-23 14:45:02 +000026
27class PICTbrowse(FrameWork.Application):
Tim Peters182b5ac2004-07-18 06:16:08 +000028 def __init__(self):
29 # First init menus, etc.
30 FrameWork.Application.__init__(self)
31 # Next create our dialog
32 self.main_dialog = MyDialog(self)
33 # Now open the dialog
34 contents = self.findPICTresources()
35 self.main_dialog.open(ID_MAIN, contents)
36 # Finally, go into the event loop
37 self.mainloop()
38
39 def makeusermenus(self):
40 self.filemenu = m = FrameWork.Menu(self.menubar, "File")
41 self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
42
43 def quit(self, *args):
44 self._quit()
45
46 def showPICT(self, resid):
47 w = PICTwindow(self)
48 w.open(resid)
49 #EasyDialogs.Message('Show PICT %r' % (resid,))
50
51 def findPICTresources(self):
52 num = Res.CountResources('PICT')
53 rv = []
54 for i in range(1, num+1):
55 Res.SetResLoad(0)
56 try:
57 r = Res.GetIndResource('PICT', i)
58 finally:
59 Res.SetResLoad(1)
60 id, type, name = r.GetResInfo()
61 rv.append((id, name))
62 return rv
63
Jack Jansen3b23ed91999-12-23 14:45:02 +000064class PICTwindow(FrameWork.Window):
Tim Peters182b5ac2004-07-18 06:16:08 +000065 def open(self, (resid, resname)):
66 if not resname:
67 resname = '#%r' % (resid,)
68 self.resid = resid
69 picture = Qd.GetPicture(self.resid)
70 # Get rect for picture
71 print repr(picture.data[:16])
72 sz, t, l, b, r = struct.unpack('hhhhh', picture.data[:10])
73 print 'pict:', t, l, b, r
74 width = r-l
75 height = b-t
76 if width < 64: width = 64
77 elif width > 480: width = 480
78 if height < 64: height = 64
79 elif height > 320: height = 320
80 bounds = (LEFT, TOP, LEFT+width, TOP+height)
81 print 'bounds:', bounds
82
83 self.wid = Win.NewWindow(bounds, resname, 1, 0, -1, 1, 0)
84 self.wid.SetWindowPic(picture)
85 self.do_postopen()
86
Jack Jansen3b23ed91999-12-23 14:45:02 +000087class MyDialog(FrameWork.DialogWindow):
Tim Peters182b5ac2004-07-18 06:16:08 +000088 "Main dialog window for PICTbrowse"
Jack Jansen3b23ed91999-12-23 14:45:02 +000089
Tim Peters182b5ac2004-07-18 06:16:08 +000090 def open(self, id, contents):
91 self.id = id
92 FrameWork.DialogWindow.open(self, ID_MAIN)
93 self.dlg.SetDialogDefaultItem(MAIN_SHOW)
94 tp, h, rect = self.dlg.GetDialogItem(MAIN_LIST)
95 rect2 = rect[0]+1, rect[1]+1, rect[2]-17, rect[3]-17 # Scroll bar space
96 self.list = List.LNew(rect2, (0, 0, 1, len(contents)), (0,0), 0, self.wid,
97 0, 1, 1, 1)
98 self.contents = contents
99 self.setlist()
Jack Jansen3b23ed91999-12-23 14:45:02 +0000100
Tim Peters182b5ac2004-07-18 06:16:08 +0000101 def setlist(self):
102 self.list.LDelRow(0, 0)
103 self.list.LSetDrawingMode(0)
104 if self.contents:
105 self.list.LAddRow(len(self.contents), 0)
106 for i in range(len(self.contents)):
107 v = repr(self.contents[i][0])
108 if self.contents[i][1]:
109 v = v + '"' + self.contents[i][1] + '"'
110 self.list.LSetCell(v, (0, i))
111 self.list.LSetDrawingMode(1)
112 self.list.LUpdate(self.wid.GetWindowPort().visRgn)
113
114 def do_listhit(self, event):
115 (what, message, when, where, modifiers) = event
116 Qd.SetPort(self.wid)
117 where = Qd.GlobalToLocal(where)
118 print 'LISTHIT', where
119 if self.list.LClick(where, modifiers):
120 self.do_show()
121
122 def getselection(self):
123 items = []
124 point = (0,0)
125 while 1:
126 ok, point = self.list.LGetSelect(1, point)
127 if not ok:
128 break
129 items.append(point[1])
130 point = point[0], point[1]+1
131 values = []
132 for i in items:
133 values.append(self.contents[i])
134 return values
135
136 def do_show(self, *args):
137 selection = self.getselection()
138 for resid in selection:
139 self.parent.showPICT(resid)
140
141 def do_rawupdate(self, window, event):
142 tp, h, rect = self.dlg.GetDialogItem(MAIN_LIST)
143 Qd.SetPort(self.wid)
144 Qd.FrameRect(rect)
145 self.list.LUpdate(self.wid.GetWindowPort().visRgn)
146
147 def do_activate(self, activate, event):
148 self.list.LActivate(activate)
149
150 def do_close(self):
151 self.close()
152
153 def do_itemhit(self, item, event):
154 if item == MAIN_LIST:
155 self.do_listhit(event)
156 if item == MAIN_SHOW:
157 self.do_show()
Jack Jansen3b23ed91999-12-23 14:45:02 +0000158
159main()