blob: c78960cc29b0c2ce469852f6ebf82d09f29e9e95 [file] [log] [blame]
Jack Jansen93fa45c1995-11-15 15:20:35 +00001"""browsepict - Display all "PICT" resources found"""
2
3import FrameWork
4import EasyDialogs
5import Res
6import Qd
7import Win
Jack Jansen3b23ed91999-12-23 14:45:02 +00008import Controls
Jack Jansen93fa45c1995-11-15 15:20:35 +00009import List
10import sys
11import struct
12
13#
14# Resource definitions
15ID_MAIN=512
16MAIN_LIST=1
17MAIN_SHOW=2
18
19# Where is the picture window?
20LEFT=200
21TOP=64
22MINWIDTH=64
23MINHEIGHT=64
24MAXWIDTH=320
25MAXHEIGHT=320
26
27def main():
28 try:
29 dummy = Res.GetResource('DLOG', ID_MAIN)
30 except Res.Error:
31 try:
Jack Jansen8eff33b2000-06-20 22:01:04 +000032 Res.FSpOpenResFile("PICTbrowse.rsrc", 1)
Jack Jansen93fa45c1995-11-15 15:20:35 +000033 except Res.Error, arg:
34 EasyDialogs.Message("Cannot open PICTbrowse.rsrc: "+arg[1])
35 sys.exit(1)
36 PICTbrowse()
37
38class PICTbrowse(FrameWork.Application):
39 def __init__(self):
40 # First init menus, etc.
41 FrameWork.Application.__init__(self)
42 # Next create our dialog
43 self.main_dialog = MyDialog(self)
44 # Now open the dialog
45 contents = self.findPICTresources()
46 self.main_dialog.open(ID_MAIN, contents)
47 # Finally, go into the event loop
48 self.mainloop()
49
50 def makeusermenus(self):
51 self.filemenu = m = FrameWork.Menu(self.menubar, "File")
52 self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
53
54 def quit(self, *args):
Jack Jansenef5cd051996-09-17 12:39:12 +000055 self._quit()
Jack Jansen93fa45c1995-11-15 15:20:35 +000056
57 def showPICT(self, resid):
58 w = PICTwindow(self)
59 w.open(resid)
60 #EasyDialogs.Message('Show PICT '+`resid`)
61
62 def findPICTresources(self):
63 num = Res.CountResources('PICT')
64 rv = []
65 for i in range(1, num+1):
66 Res.SetResLoad(0)
67 try:
68 r = Res.GetIndResource('PICT', i)
69 finally:
70 Res.SetResLoad(1)
71 id, type, name = r.GetResInfo()
Jack Jansen34d11f02000-03-07 23:40:13 +000072 rv.append((id, name))
Jack Jansen93fa45c1995-11-15 15:20:35 +000073 return rv
74
75class PICTwindow(FrameWork.Window):
76 def open(self, (resid, resname)):
77 if not resname:
78 resname = '#'+`resid`
79 self.resid = resid
80 self.picture = Qd.GetPicture(self.resid)
81 # Get rect for picture
82 sz, t, l, b, r = struct.unpack('hhhhh', self.picture.data[:10])
83 self.pictrect = (l, t, r, b)
84 width = r-l
85 height = b-t
86 if width < MINWIDTH: width = MINWIDTH
87 elif width > MAXWIDTH: width = MAXWIDTH
88 if height < MINHEIGHT: height = MINHEIGHT
89 elif height > MAXHEIGHT: height = MAXHEIGHT
90 bounds = (LEFT, TOP, LEFT+width, TOP+height)
91
92 self.wid = Win.NewWindow(bounds, resname, 1, 0, -1, 1, 0)
93 self.do_postopen()
94
95 def do_update(self, *args):
96 currect = self.fitrect()
97 Qd.DrawPicture(self.picture, currect)
98
99 def fitrect(self):
100 """Return self.pictrect scaled to fit in window"""
101 graf = self.wid.GetWindowPort()
102 screenrect = graf.portRect
103 picwidth = self.pictrect[2] - self.pictrect[0]
104 picheight = self.pictrect[3] - self.pictrect[1]
105 if picwidth > screenrect[2] - screenrect[0]:
106 factor = float(picwidth) / float(screenrect[2]-screenrect[0])
107 picwidth = picwidth / factor
108 picheight = picheight / factor
109 if picheight > screenrect[3] - screenrect[1]:
110 factor = float(picheight) / float(screenrect[3]-screenrect[1])
111 picwidth = picwidth / factor
112 picheight = picheight / factor
113 return (screenrect[0], screenrect[1], screenrect[0]+int(picwidth),
114 screenrect[1]+int(picheight))
115
116class MyDialog(FrameWork.DialogWindow):
117 "Main dialog window for PICTbrowse"
118
119 def open(self, id, contents):
120 self.id = id
121 FrameWork.DialogWindow.open(self, ID_MAIN)
122 self.wid.SetDialogDefaultItem(MAIN_SHOW)
Jack Jansen93fa45c1995-11-15 15:20:35 +0000123 self.contents = contents
Jack Jansen3b23ed91999-12-23 14:45:02 +0000124 self.ctl = self.wid.GetDialogItemAsControl(MAIN_LIST)
125 h = self.ctl.GetControlDataHandle(Controls.kControlListBoxPart,
126 Controls.kControlListBoxListHandleTag)
127 self.list = List.as_List(h)
Jack Jansen93fa45c1995-11-15 15:20:35 +0000128 self.setlist()
129
130 def setlist(self):
131 self.list.LDelRow(0, 0)
132 self.list.LSetDrawingMode(0)
133 if self.contents:
134 self.list.LAddRow(len(self.contents), 0)
135 for i in range(len(self.contents)):
136 v = `self.contents[i][0]`
137 if self.contents[i][1]:
138 v = v + '"' + self.contents[i][1] + '"'
139 self.list.LSetCell(v, (0, i))
140 self.list.LSetDrawingMode(1)
Jack Jansen84949671996-04-10 14:53:29 +0000141 self.list.LUpdate(self.wid.GetWindowPort().visRgn)
Jack Jansen93fa45c1995-11-15 15:20:35 +0000142
Jack Jansen93fa45c1995-11-15 15:20:35 +0000143 def getselection(self):
144 items = []
145 point = (0,0)
146 while 1:
147 ok, point = self.list.LGetSelect(1, point)
148 if not ok:
149 break
150 items.append(point[1])
151 point = point[0], point[1]+1
152 values = []
153 for i in items:
154 values.append(self.contents[i])
155 return values
156
157 def do_show(self, *args):
158 selection = self.getselection()
159 for resid in selection:
160 self.parent.showPICT(resid)
161
Jack Jansen93fa45c1995-11-15 15:20:35 +0000162 def do_close(self):
163 self.close()
164
165 def do_itemhit(self, item, event):
Jack Jansen93fa45c1995-11-15 15:20:35 +0000166 if item == MAIN_SHOW:
167 self.do_show()
168
169main()