Home | History | Annotate | Download | only in PICTbrowse
      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 Controls
      9 from Carbon import List
     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 MINWIDTH=64
     23 MINHEIGHT=64
     24 MAXWIDTH=320
     25 MAXHEIGHT=320
     26 
     27 def main():
     28     macresource.need('DLOG', ID_MAIN, "PICTbrowse.rsrc")
     29     PICTbrowse()
     30 
     31 class PICTbrowse(FrameWork.Application):
     32     def __init__(self):
     33         # First init menus, etc.
     34         FrameWork.Application.__init__(self)
     35         # Next create our dialog
     36         self.main_dialog = MyDialog(self)
     37         # Now open the dialog
     38         contents = self.findPICTresources()
     39         self.main_dialog.open(ID_MAIN, contents)
     40         # Finally, go into the event loop
     41         self.mainloop()
     42 
     43     def makeusermenus(self):
     44         self.filemenu = m = FrameWork.Menu(self.menubar, "File")
     45         self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
     46 
     47     def quit(self, *args):
     48         self._quit()
     49 
     50     def showPICT(self, resid):
     51         w = PICTwindow(self)
     52         w.open(resid)
     53         #EasyDialogs.Message('Show PICT %r' % (resid,))
     54 
     55     def findPICTresources(self):
     56         num = Res.CountResources('PICT')
     57         rv = []
     58         for i in range(1, num+1):
     59             Res.SetResLoad(0)
     60             try:
     61                 r = Res.GetIndResource('PICT', i)
     62             finally:
     63                 Res.SetResLoad(1)
     64             id, type, name = r.GetResInfo()
     65             rv.append((id, name))
     66         return rv
     67 
     68 class PICTwindow(FrameWork.Window):
     69     def open(self, (resid, resname)):
     70         if not resname:
     71             resname = '#%r' % (resid,)
     72         self.resid = resid
     73         self.picture = Qd.GetPicture(self.resid)
     74         # Get rect for picture
     75         sz, t, l, b, r = struct.unpack('hhhhh', self.picture.data[:10])
     76         self.pictrect = (l, t, r, b)
     77         width = r-l
     78         height = b-t
     79         if width < MINWIDTH: width = MINWIDTH
     80         elif width > MAXWIDTH: width = MAXWIDTH
     81         if height < MINHEIGHT: height = MINHEIGHT
     82         elif height > MAXHEIGHT: height = MAXHEIGHT
     83         bounds = (LEFT, TOP, LEFT+width, TOP+height)
     84 
     85         self.wid = Win.NewWindow(bounds, resname, 1, 0, -1, 1, 0)
     86         self.do_postopen()
     87 
     88     def do_update(self, *args):
     89         currect = self.fitrect()
     90         Qd.DrawPicture(self.picture, currect)
     91 
     92     def fitrect(self):
     93         """Return self.pictrect scaled to fit in window"""
     94         graf = self.dlg.GetWindowPort()
     95         screenrect = graf.GetPortBounds()
     96         picwidth = self.pictrect[2] - self.pictrect[0]
     97         picheight = self.pictrect[3] - self.pictrect[1]
     98         if picwidth > screenrect[2] - screenrect[0]:
     99             factor = float(picwidth) / float(screenrect[2]-screenrect[0])
    100             picwidth = picwidth / factor
    101             picheight = picheight / factor
    102         if picheight > screenrect[3] - screenrect[1]:
    103             factor = float(picheight) / float(screenrect[3]-screenrect[1])
    104             picwidth = picwidth / factor
    105             picheight = picheight / factor
    106         return (screenrect[0], screenrect[1], screenrect[0]+int(picwidth),
    107                         screenrect[1]+int(picheight))
    108 
    109 class MyDialog(FrameWork.DialogWindow):
    110     "Main dialog window for PICTbrowse"
    111 
    112     def open(self, id, contents):
    113         self.id = id
    114         FrameWork.DialogWindow.open(self, ID_MAIN)
    115         self.dlg.SetDialogDefaultItem(MAIN_SHOW)
    116         self.contents = contents
    117         self.ctl = self.dlg.GetDialogItemAsControl(MAIN_LIST)
    118         h = self.ctl.GetControlData_Handle(Controls.kControlListBoxPart,
    119                         Controls.kControlListBoxListHandleTag)
    120         self.list = List.as_List(h)
    121         self.setlist()
    122 
    123     def setlist(self):
    124         self.list.LDelRow(0, 0)
    125         self.list.LSetDrawingMode(0)
    126         if self.contents:
    127             self.list.LAddRow(len(self.contents), 0)
    128             for i in range(len(self.contents)):
    129                 v = repr(self.contents[i][0])
    130                 if self.contents[i][1]:
    131                     v = v + '"' + self.contents[i][1] + '"'
    132                 self.list.LSetCell(v, (0, i))
    133         self.list.LSetDrawingMode(1)
    134         self.list.LUpdate(self.wid.GetWindowPort().visRgn)
    135 
    136     def getselection(self):
    137         items = []
    138         point = (0,0)
    139         while 1:
    140             ok, point = self.list.LGetSelect(1, point)
    141             if not ok:
    142                 break
    143             items.append(point[1])
    144             point = point[0], point[1]+1
    145         values = []
    146         for i in items:
    147             values.append(self.contents[i])
    148         return values
    149 
    150     def do_show(self, *args):
    151         selection = self.getselection()
    152         for resid in selection:
    153             self.parent.showPICT(resid)
    154 
    155     def do_close(self):
    156         self.close()
    157 
    158     def do_itemhit(self, item, event):
    159         if item == MAIN_SHOW:
    160             self.do_show()
    161 
    162 main()
    163