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