blob: 41ffa5c1d87e306a792d8231b0220ed9ea924f7e [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():
Jack Jansen3c06b9a2001-08-27 21:41:23 +000025 macresource.need('DLOG', ID_MAIN, "PICTbrowse.rsrc")
Jack Jansen07043b41995-11-14 11:32:06 +000026 PICTbrowse()
27
28class PICTbrowse(FrameWork.Application):
29 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):
Jack Jansenef5cd051996-09-17 12:39:12 +000045 self._quit()
Jack Jansen07043b41995-11-14 11:32:06 +000046
47 def showPICT(self, resid):
48 w = PICTwindow(self)
49 w.open(resid)
50 #EasyDialogs.Message('Show PICT '+`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()
Jack Jansen34d11f02000-03-07 23:40:13 +000062 rv.append((id, name))
Jack Jansen07043b41995-11-14 11:32:06 +000063 return rv
64
65class PICTwindow(FrameWork.Window):
66 def open(self, (resid, resname)):
67 if not resname:
68 resname = '#'+`resid`
69 self.resid = resid
70 picture = Qd.GetPicture(self.resid)
71 # Get rect for picture
72 print `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
88class MyDialog(FrameWork.DialogWindow):
89 "Main dialog window for PICTbrowse"
90
91 def open(self, id, contents):
92 self.id = id
93 FrameWork.DialogWindow.open(self, ID_MAIN)
Jack Jansencbed91b2001-08-03 13:31:36 +000094 self.dlg.SetDialogDefaultItem(MAIN_SHOW)
Jack Jansen07043b41995-11-14 11:32:06 +000095 self.contents = contents
Jack Jansencbed91b2001-08-03 13:31:36 +000096 self.ctl = self.dlg.GetDialogItemAsControl(MAIN_LIST)
Jack Jansene7bfc912001-01-09 22:22:58 +000097 h = self.ctl.GetControlData_Handle(Controls.kControlListBoxPart,
Jack Jansen3b23ed91999-12-23 14:45:02 +000098 Controls.kControlListBoxListHandleTag)
99 self.list = List.as_List(h)
Jack Jansen07043b41995-11-14 11:32:06 +0000100 self.setlist()
101
102 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 = `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)
Jack Jansen84949671996-04-10 14:53:29 +0000113 self.list.LUpdate(self.wid.GetWindowPort().visRgn)
Jack Jansen07043b41995-11-14 11:32:06 +0000114
Jack Jansen07043b41995-11-14 11:32:06 +0000115 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
Jack Jansen07043b41995-11-14 11:32:06 +0000134 def do_close(self):
135 self.close()
136
137 def do_itemhit(self, item, event):
Jack Jansen07043b41995-11-14 11:32:06 +0000138 if item == MAIN_SHOW:
139 self.do_show()
140
141main()