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