/Mac/Demo/PICTbrowse/cicnbrowse.py

http://unladen-swallow.googlecode.com/ · Python · 161 lines · 131 code · 21 blank · 9 comment · 18 complexity · 4153a0a2e0ad752bb72e5004c40f34e6 MD5 · raw file

  1. """browsepict - Display all "cicn" 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. from Carbon import Icn
  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. MINWIDTH=32
  20. MINHEIGHT=32
  21. MAXWIDTH=320
  22. MAXHEIGHT=320
  23. def main():
  24. macresource.need('DLOG', ID_MAIN, "PICTbrowse.rsrc")
  25. CIconbrowse()
  26. class CIconbrowse(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.findcicnresources()
  34. self.main_dialog.open(ID_MAIN, contents)
  35. # Finally, go into the event loop
  36. self.mainloop()
  37. def makeusermenus(self):
  38. self.filemenu = m = FrameWork.Menu(self.menubar, "File")
  39. self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
  40. def quit(self, *args):
  41. self._quit()
  42. def showCIcon(self, resid):
  43. w = CIconwindow(self)
  44. w.open(resid)
  45. #EasyDialogs.Message('Show cicn %r' % (resid,))
  46. def findcicnresources(self):
  47. num = Res.CountResources('cicn')
  48. rv = []
  49. for i in range(1, num+1):
  50. Res.SetResLoad(0)
  51. try:
  52. r = Res.GetIndResource('cicn', i)
  53. finally:
  54. Res.SetResLoad(1)
  55. id, type, name = r.GetResInfo()
  56. rv.append((id, name))
  57. return rv
  58. class CIconwindow(FrameWork.Window):
  59. def open(self, (resid, resname)):
  60. if not resname:
  61. resname = '#%r' % (resid,)
  62. self.resid = resid
  63. self.picture = Icn.GetCIcon(self.resid)
  64. l, t, r, b = 0, 0, 32, 32
  65. self.pictrect = (l, t, r, b)
  66. width = r-l
  67. height = b-t
  68. if width < MINWIDTH: width = MINWIDTH
  69. elif width > MAXWIDTH: width = MAXWIDTH
  70. if height < MINHEIGHT: height = MINHEIGHT
  71. elif height > MAXHEIGHT: height = MAXHEIGHT
  72. bounds = (LEFT, TOP, LEFT+width, TOP+height)
  73. self.wid = Win.NewWindow(bounds, resname, 1, 0, -1, 1, 0)
  74. self.do_postopen()
  75. def do_update(self, *args):
  76. currect = self.fitrect()
  77. Icn.PlotCIcon(currect, self.picture)
  78. def fitrect(self):
  79. """Return self.pictrect scaled to fit in window"""
  80. graf = self.wid.GetWindowPort()
  81. screenrect = graf.GetPortBounds()
  82. picwidth = self.pictrect[2] - self.pictrect[0]
  83. picheight = self.pictrect[3] - self.pictrect[1]
  84. if picwidth > screenrect[2] - screenrect[0]:
  85. factor = float(picwidth) / float(screenrect[2]-screenrect[0])
  86. picwidth = picwidth / factor
  87. picheight = picheight / factor
  88. if picheight > screenrect[3] - screenrect[1]:
  89. factor = float(picheight) / float(screenrect[3]-screenrect[1])
  90. picwidth = picwidth / factor
  91. picheight = picheight / factor
  92. return (screenrect[0], screenrect[1], screenrect[0]+int(picwidth),
  93. screenrect[1]+int(picheight))
  94. class MyDialog(FrameWork.DialogWindow):
  95. "Main dialog window for cicnbrowse"
  96. def open(self, id, contents):
  97. self.id = id
  98. FrameWork.DialogWindow.open(self, ID_MAIN)
  99. self.dlg.SetDialogDefaultItem(MAIN_SHOW)
  100. self.contents = contents
  101. self.ctl = self.dlg.GetDialogItemAsControl(MAIN_LIST)
  102. h = self.ctl.GetControlData_Handle(Controls.kControlListBoxPart,
  103. Controls.kControlListBoxListHandleTag)
  104. self.list = List.as_List(h)
  105. self.setlist()
  106. def setlist(self):
  107. self.list.LDelRow(0, 0)
  108. self.list.LSetDrawingMode(0)
  109. if self.contents:
  110. self.list.LAddRow(len(self.contents), 0)
  111. for i in range(len(self.contents)):
  112. v = repr(self.contents[i][0])
  113. if self.contents[i][1]:
  114. v = v + '"' + self.contents[i][1] + '"'
  115. self.list.LSetCell(v, (0, i))
  116. self.list.LSetDrawingMode(1)
  117. self.list.LUpdate(self.wid.GetWindowPort().visRgn)
  118. def getselection(self):
  119. items = []
  120. point = (0,0)
  121. while 1:
  122. ok, point = self.list.LGetSelect(1, point)
  123. if not ok:
  124. break
  125. items.append(point[1])
  126. point = point[0], point[1]+1
  127. values = []
  128. for i in items:
  129. values.append(self.contents[i])
  130. return values
  131. def do_show(self, *args):
  132. selection = self.getselection()
  133. for resid in selection:
  134. self.parent.showCIcon(resid)
  135. def do_close(self):
  136. self.close()
  137. def do_itemhit(self, item, event):
  138. if item == MAIN_SHOW:
  139. self.do_show()
  140. main()