bitmap.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import win32ui
  2. import win32con
  3. import win32api
  4. import string
  5. import os
  6. from . import app
  7. import sys
  8. from pywin.mfc import docview, window
  9. bStretch = 1
  10. class BitmapDocument(docview.Document):
  11. "A bitmap document. Holds the bitmap data itself."
  12. def __init__(self, template):
  13. docview.Document.__init__(self, template)
  14. self.bitmap=None
  15. def OnNewDocument(self):
  16. # I can not create new bitmaps.
  17. win32ui.MessageBox("Bitmaps can not be created.")
  18. def OnOpenDocument(self, filename):
  19. self.bitmap=win32ui.CreateBitmap()
  20. # init data members
  21. f = open(filename, 'rb')
  22. try:
  23. try:
  24. self.bitmap.LoadBitmapFile(f)
  25. except IOError:
  26. win32ui.MessageBox("Could not load the bitmap from %s" % filename)
  27. return 0
  28. finally:
  29. f.close()
  30. self.size = self.bitmap.GetSize()
  31. return 1
  32. def DeleteContents(self):
  33. self.bitmap=None
  34. class BitmapView(docview.ScrollView):
  35. "A view of a bitmap. Obtains data from document."
  36. def __init__(self, doc):
  37. docview.ScrollView.__init__(self, doc)
  38. self.width = self.height = 0
  39. # set up message handlers
  40. self.HookMessage (self.OnSize, win32con.WM_SIZE)
  41. def OnInitialUpdate(self):
  42. doc = self.GetDocument()
  43. if doc.bitmap:
  44. bitmapSize = doc.bitmap.GetSize()
  45. self.SetScrollSizes(win32con.MM_TEXT, bitmapSize)
  46. def OnSize (self, params):
  47. lParam = params[3]
  48. self.width = win32api.LOWORD(lParam)
  49. self.height = win32api.HIWORD(lParam)
  50. def OnDraw (self, dc):
  51. # set sizes used for "non stretch" mode.
  52. doc = self.GetDocument()
  53. if doc.bitmap is None: return
  54. bitmapSize = doc.bitmap.GetSize()
  55. if bStretch:
  56. # stretch BMP.
  57. viewRect = (0,0,self.width, self.height)
  58. bitmapRect = (0,0,bitmapSize[0], bitmapSize[1])
  59. doc.bitmap.Paint(dc, viewRect, bitmapRect)
  60. else:
  61. # non stretch.
  62. doc.bitmap.Paint(dc)
  63. class BitmapFrame(window.MDIChildWnd):
  64. def OnCreateClient( self, createparams, context ):
  65. borderX = win32api.GetSystemMetrics(win32con.SM_CXFRAME)
  66. borderY = win32api.GetSystemMetrics(win32con.SM_CYFRAME)
  67. titleY = win32api.GetSystemMetrics(win32con.SM_CYCAPTION) # includes border
  68. # try and maintain default window pos, else adjust if cant fit
  69. # get the main client window dimensions.
  70. mdiClient = win32ui.GetMainFrame().GetWindow(win32con.GW_CHILD)
  71. clientWindowRect=mdiClient.ScreenToClient(mdiClient.GetWindowRect())
  72. clientWindowSize=(clientWindowRect[2]-clientWindowRect[0],clientWindowRect[3]-clientWindowRect[1])
  73. left, top, right, bottom=mdiClient.ScreenToClient(self.GetWindowRect())
  74. # width, height=context.doc.size[0], context.doc.size[1]
  75. # width = width+borderX*2
  76. # height= height+titleY+borderY*2-1
  77. # if (left+width)>clientWindowSize[0]:
  78. # left = clientWindowSize[0] - width
  79. # if left<0:
  80. # left = 0
  81. # width = clientWindowSize[0]
  82. # if (top+height)>clientWindowSize[1]:
  83. # top = clientWindowSize[1] - height
  84. # if top<0:
  85. # top = 0
  86. # height = clientWindowSize[1]
  87. # self.frame.MoveWindow((left, top, left+width, top+height),0)
  88. window.MDIChildWnd.OnCreateClient(self, createparams, context)
  89. return 1
  90. class BitmapTemplate(docview.DocTemplate):
  91. def __init__(self):
  92. docview.DocTemplate.__init__(self, win32ui.IDR_PYTHONTYPE, BitmapDocument, BitmapFrame, BitmapView)
  93. def MatchDocType(self, fileName, fileType):
  94. doc = self.FindOpenDocument(fileName)
  95. if doc: return doc
  96. ext = os.path.splitext(fileName)[1].lower()
  97. if ext =='.bmp': # removed due to PIL! or ext=='.ppm':
  98. return win32ui.CDocTemplate_Confidence_yesAttemptNative
  99. return win32ui.CDocTemplate_Confidence_maybeAttemptForeign
  100. # return win32ui.CDocTemplate_Confidence_noAttempt
  101. # For debugging purposes, when this module may be reloaded many times.
  102. try:
  103. win32ui.GetApp().RemoveDocTemplate(bitmapTemplate)
  104. except NameError:
  105. pass
  106. bitmapTemplate = BitmapTemplate()
  107. bitmapTemplate.SetDocStrings('\nBitmap\nBitmap\nBitmap (*.bmp)\n.bmp\nPythonBitmapFileType\nPython Bitmap File')
  108. win32ui.GetApp().AddDocTemplate(bitmapTemplate)
  109. # This works, but just didnt make it through the code reorg.
  110. #class PPMBitmap(Bitmap):
  111. # def LoadBitmapFile(self, file ):
  112. # magic=file.readline()
  113. # if magic <> "P6\n":
  114. # raise TypeError, "The file is not a PPM format file"
  115. # rowcollist=string.split(file.readline())
  116. # cols=string.atoi(rowcollist[0])
  117. # rows=string.atoi(rowcollist[1])
  118. # file.readline() # whats this one?
  119. # self.bitmap.LoadPPMFile(file,(cols,rows))
  120. def t():
  121. bitmapTemplate.OpenDocumentFile('d:\\winnt\\arcade.bmp')
  122. #OpenBMPFile( 'd:\\winnt\\arcade.bmp')
  123. def demo():
  124. import glob
  125. winDir=win32api.GetWindowsDirectory()
  126. for fileName in glob.glob1(winDir, '*.bmp')[:2]:
  127. bitmapTemplate.OpenDocumentFile(os.path.join(winDir, fileName))