blob: 0adfc837831f37aa1b77a55ec00e2a8bc5ab6a56 [file] [log] [blame]
Jack Jansen07043b41995-11-14 11:32:06 +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 Controls
9from Carbon import List
Jack Jansen07043b41995-11-14 11:32:06 +000010import sys
11import struct
Jack Jansen3c06b9a2001-08-27 21:41:23 +000012import macresource
Jack Jansen07043b41995-11-14 11:32:06 +000013
14#
15# Resource definitions
16ID_MAIN=512
17MAIN_LIST=1
18MAIN_SHOW=2
19
20# Where is the picture window?
21LEFT=200
22TOP=64
23
24def main():
Tim Peters182b5ac2004-07-18 06:16:08 +000025 macresource.need('DLOG', ID_MAIN, "PICTbrowse.rsrc")
26 PICTbrowse()
Jack Jansen07043b41995-11-14 11:32:06 +000027
28class PICTbrowse(FrameWork.Application):
Tim Peters182b5ac2004-07-18 06:16:08 +000029 def __init__(self):
30 # First init menus, etc.
31 FrameWork.Application.__init__(self)
32 # Next create our dialog
33 self.main_dialog = MyDialog(self)
34 # Now open the dialog
35 contents = self.findPICTresources()
36 self.main_dialog.open(ID_MAIN, contents)
37 # Finally, go into the event loop
38 self.mainloop()
39
40 def makeusermenus(self):
41 self.filemenu = m = FrameWork.Menu(self.menubar, "File")
42 self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
43
44 def quit(self, *args):
45 self._quit()
46
47 def showPICT(self, resid):
48 w = PICTwindow(self)
49 w.open(resid)
50 #EasyDialogs.Message('Show PICT %r' % (resid,))
51
52 def findPICTresources(self):
53 num = Res.CountResources('PICT')
54 rv = []
55 for i in range(1, num+1):
56 Res.SetResLoad(0)
57 try:
58 r = Res.GetIndResource('PICT', i)
59 finally:
60 Res.SetResLoad(1)
61 id, type, name = r.GetResInfo()
62 rv.append((id, name))
63 return rv
64
Jack Jansen07043b41995-11-14 11:32:06 +000065class PICTwindow(FrameWork.Window):
Tim Peters182b5ac2004-07-18 06:16:08 +000066 def open(self, (resid, resname)):
67 if not resname:
68 resname = '#%r' % (resid,)
69 self.resid = resid
70 picture = Qd.GetPicture(self.resid)
71 # Get rect for picture
72 print repr(picture.data[:16])
73 sz, t, l, b, r = struct.unpack('hhhhh', picture.data[:10])
74 print 'pict:', t, l, b, r
75 width = r-l
76 height = b-t
77 if width < 64: width = 64
78 elif width > 480: width = 480
79 if height < 64: height = 64
80 elif height > 320: height = 320
81 bounds = (LEFT, TOP, LEFT+width, TOP+height)
82 print 'bounds:', bounds
83
84 self.wid = Win.NewWindow(bounds, resname, 1, 0, -1, 1, 0)
85 self.wid.SetWindowPic(picture)
86 self.do_postopen()
87
Jack Jansen07043b41995-11-14 11:32:06 +000088class MyDialog(FrameWork.DialogWindow):
Tim Peters182b5ac2004-07-18 06:16:08 +000089 "Main dialog window for PICTbrowse"
Jack Jansen07043b41995-11-14 11:32:06 +000090
Tim Peters182b5ac2004-07-18 06:16:08 +000091 def open(self, id, contents):
92 self.id = id
93 FrameWork.DialogWindow.open(self, ID_MAIN)
94 self.dlg.SetDialogDefaultItem(MAIN_SHOW)
95 self.contents = contents
96 self.ctl = self.dlg.GetDialogItemAsControl(MAIN_LIST)
97 h = self.ctl.GetControlData_Handle(Controls.kControlListBoxPart,
98 Controls.kControlListBoxListHandleTag)
99 self.list = List.as_List(h)
100 self.setlist()
Jack Jansen07043b41995-11-14 11:32:06 +0000101
Tim Peters182b5ac2004-07-18 06:16:08 +0000102 def setlist(self):
103 self.list.LDelRow(0, 0)
104 self.list.LSetDrawingMode(0)
105 if self.contents:
106 self.list.LAddRow(len(self.contents), 0)
107 for i in range(len(self.contents)):
108 v = repr(self.contents[i][0])
109 if self.contents[i][1]:
110 v = v + '"' + self.contents[i][1] + '"'
111 self.list.LSetCell(v, (0, i))
112 self.list.LSetDrawingMode(1)
113 self.list.LUpdate(self.wid.GetWindowPort().visRgn)
114
115 def getselection(self):
116 items = []
117 point = (0,0)
118 while 1:
119 ok, point = self.list.LGetSelect(1, point)
120 if not ok:
121 break
122 items.append(point[1])
123 point = point[0], point[1]+1
124 values = []
125 for i in items:
126 values.append(self.contents[i])
127 return values
128
129 def do_show(self, *args):
130 selection = self.getselection()
131 for resid in selection:
132 self.parent.showPICT(resid)
133
134 def do_close(self):
135 self.close()
136
137 def do_itemhit(self, item, event):
138 if item == MAIN_SHOW:
139 self.do_show()
Jack Jansen07043b41995-11-14 11:32:06 +0000140
141main()