/Mac/Demo/PICTbrowse/PICTbrowse.py

http://unladen-swallow.googlecode.com/ · Python · 140 lines · 112 code · 19 blank · 9 comment · 16 complexity · 68b50613b8eec4b41ed430868aca7b25 MD5 · raw file

  1. """browsepict - Display all "PICT" resources found"""
  2. import FrameWork
  3. import EasyDialogs
  4. from Carbon import Res
  5. from Carbon import Qd
  6. from Carbon import Win
  7. from Carbon import Controls
  8. from Carbon import List
  9. import struct
  10. import macresource
  11. #
  12. # Resource definitions
  13. ID_MAIN=512
  14. MAIN_LIST=1
  15. MAIN_SHOW=2
  16. # Where is the picture window?
  17. LEFT=200
  18. TOP=64
  19. def main():
  20. macresource.need('DLOG', ID_MAIN, "PICTbrowse.rsrc")
  21. PICTbrowse()
  22. class PICTbrowse(FrameWork.Application):
  23. def __init__(self):
  24. # First init menus, etc.
  25. FrameWork.Application.__init__(self)
  26. # Next create our dialog
  27. self.main_dialog = MyDialog(self)
  28. # Now open the dialog
  29. contents = self.findPICTresources()
  30. self.main_dialog.open(ID_MAIN, contents)
  31. # Finally, go into the event loop
  32. self.mainloop()
  33. def makeusermenus(self):
  34. self.filemenu = m = FrameWork.Menu(self.menubar, "File")
  35. self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
  36. def quit(self, *args):
  37. self._quit()
  38. def showPICT(self, resid):
  39. w = PICTwindow(self)
  40. w.open(resid)
  41. #EasyDialogs.Message('Show PICT %r' % (resid,))
  42. def findPICTresources(self):
  43. num = Res.CountResources('PICT')
  44. rv = []
  45. for i in range(1, num+1):
  46. Res.SetResLoad(0)
  47. try:
  48. r = Res.GetIndResource('PICT', i)
  49. finally:
  50. Res.SetResLoad(1)
  51. id, type, name = r.GetResInfo()
  52. rv.append((id, name))
  53. return rv
  54. class PICTwindow(FrameWork.Window):
  55. def open(self, (resid, resname)):
  56. if not resname:
  57. resname = '#%r' % (resid,)
  58. self.resid = resid
  59. picture = Qd.GetPicture(self.resid)
  60. # Get rect for picture
  61. print repr(picture.data[:16])
  62. sz, t, l, b, r = struct.unpack('hhhhh', picture.data[:10])
  63. print 'pict:', t, l, b, r
  64. width = r-l
  65. height = b-t
  66. if width < 64: width = 64
  67. elif width > 480: width = 480
  68. if height < 64: height = 64
  69. elif height > 320: height = 320
  70. bounds = (LEFT, TOP, LEFT+width, TOP+height)
  71. print 'bounds:', bounds
  72. self.wid = Win.NewWindow(bounds, resname, 1, 0, -1, 1, 0)
  73. self.wid.SetWindowPic(picture)
  74. self.do_postopen()
  75. class MyDialog(FrameWork.DialogWindow):
  76. "Main dialog window for PICTbrowse"
  77. def open(self, id, contents):
  78. self.id = id
  79. FrameWork.DialogWindow.open(self, ID_MAIN)
  80. self.dlg.SetDialogDefaultItem(MAIN_SHOW)
  81. self.contents = contents
  82. self.ctl = self.dlg.GetDialogItemAsControl(MAIN_LIST)
  83. h = self.ctl.GetControlData_Handle(Controls.kControlListBoxPart,
  84. Controls.kControlListBoxListHandleTag)
  85. self.list = List.as_List(h)
  86. self.setlist()
  87. def setlist(self):
  88. self.list.LDelRow(0, 0)
  89. self.list.LSetDrawingMode(0)
  90. if self.contents:
  91. self.list.LAddRow(len(self.contents), 0)
  92. for i in range(len(self.contents)):
  93. v = repr(self.contents[i][0])
  94. if self.contents[i][1]:
  95. v = v + '"' + self.contents[i][1] + '"'
  96. self.list.LSetCell(v, (0, i))
  97. self.list.LSetDrawingMode(1)
  98. self.list.LUpdate(self.wid.GetWindowPort().visRgn)
  99. def getselection(self):
  100. items = []
  101. point = (0,0)
  102. while 1:
  103. ok, point = self.list.LGetSelect(1, point)
  104. if not ok:
  105. break
  106. items.append(point[1])
  107. point = point[0], point[1]+1
  108. values = []
  109. for i in items:
  110. values.append(self.contents[i])
  111. return values
  112. def do_show(self, *args):
  113. selection = self.getselection()
  114. for resid in selection:
  115. self.parent.showPICT(resid)
  116. def do_close(self):
  117. self.close()
  118. def do_itemhit(self, item, event):
  119. if item == MAIN_SHOW:
  120. self.do_show()
  121. main()