_make_boundary(): Fix for SF bug #745478, broken boundary calculation
[python/dscho.git] / Mac / Demo / PICTbrowse / PICTbrowse.py
blob41ffa5c1d87e306a792d8231b0220ed9ea924f7e
1 """browsepict - Display all "PICT" resources found"""
3 import FrameWork
4 import EasyDialogs
5 from Carbon import Res
6 from Carbon import Qd
7 from Carbon import Win
8 from Carbon import Controls
9 from Carbon import List
10 import sys
11 import struct
12 import macresource
15 # Resource definitions
16 ID_MAIN=512
17 MAIN_LIST=1
18 MAIN_SHOW=2
20 # Where is the picture window?
21 LEFT=200
22 TOP=64
24 def main():
25 macresource.need('DLOG', ID_MAIN, "PICTbrowse.rsrc")
26 PICTbrowse()
28 class 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()
40 def makeusermenus(self):
41 self.filemenu = m = FrameWork.Menu(self.menubar, "File")
42 self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
44 def quit(self, *args):
45 self._quit()
47 def showPICT(self, resid):
48 w = PICTwindow(self)
49 w.open(resid)
50 #EasyDialogs.Message('Show PICT '+`resid`)
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
65 class 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
84 self.wid = Win.NewWindow(bounds, resname, 1, 0, -1, 1, 0)
85 self.wid.SetWindowPic(picture)
86 self.do_postopen()
88 class MyDialog(FrameWork.DialogWindow):
89 "Main dialog window for PICTbrowse"
91 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()
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)
113 self.list.LUpdate(self.wid.GetWindowPort().visRgn)
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
129 def do_show(self, *args):
130 selection = self.getselection()
131 for resid in selection:
132 self.parent.showPICT(resid)
134 def do_close(self):
135 self.close()
137 def do_itemhit(self, item, event):
138 if item == MAIN_SHOW:
139 self.do_show()
141 main()