PageRenderTime 422ms CodeModel.GetById 138ms app.highlight 105ms RepoModel.GetById 117ms app.codeStats 0ms

/Mac/Demo/PICTbrowse/PICTbrowse.py

http://unladen-swallow.googlecode.com/
Python | 140 lines | 140 code | 0 blank | 0 comment | 1 complexity | 68b50613b8eec4b41ed430868aca7b25 MD5 | raw file
  1"""browsepict - Display all "PICT" resources found"""
  2
  3import FrameWork
  4import EasyDialogs
  5from Carbon import Res
  6from Carbon import Qd
  7from Carbon import Win
  8from Carbon import Controls
  9from Carbon import List
 10import struct
 11import macresource
 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
 22
 23def main():
 24    macresource.need('DLOG', ID_MAIN, "PICTbrowse.rsrc")
 25    PICTbrowse()
 26
 27class 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
 64class 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
 87class 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        self.contents = contents
 95        self.ctl = self.dlg.GetDialogItemAsControl(MAIN_LIST)
 96        h = self.ctl.GetControlData_Handle(Controls.kControlListBoxPart,
 97                        Controls.kControlListBoxListHandleTag)
 98        self.list = List.as_List(h)
 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 getselection(self):
115        items = []
116        point = (0,0)
117        while 1:
118            ok, point = self.list.LGetSelect(1, point)
119            if not ok:
120                break
121            items.append(point[1])
122            point = point[0], point[1]+1
123        values = []
124        for i in items:
125            values.append(self.contents[i])
126        return values
127
128    def do_show(self, *args):
129        selection = self.getselection()
130        for resid in selection:
131            self.parent.showPICT(resid)
132
133    def do_close(self):
134        self.close()
135
136    def do_itemhit(self, item, event):
137        if item == MAIN_SHOW:
138            self.do_show()
139
140main()