Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
bitmap.py165 linesDownload Raw Back to framework
1import os2 3import win32api4import win32con5import win32ui6from pywin.mfc import docview, window7 8from . import app9 10bStretch = 111 12 13class BitmapDocument(docview.Document):14    "A bitmap document.  Holds the bitmap data itself."15 16    def __init__(self, template):17        docview.Document.__init__(self, template)18        self.bitmap = None19 20    def OnNewDocument(self):21        # I can not create new bitmaps.22        win32ui.MessageBox("Bitmaps can not be created.")23 24    def OnOpenDocument(self, filename):25        self.bitmap = win32ui.CreateBitmap()26        # init data members27        f = open(filename, "rb")28        try:29            try:30                self.bitmap.LoadBitmapFile(f)31            except IOError:32                win32ui.MessageBox("Could not load the bitmap from %s" % filename)33                return 034        finally:35            f.close()36        self.size = self.bitmap.GetSize()37        return 138 39    def DeleteContents(self):40        self.bitmap = None41 42 43class BitmapView(docview.ScrollView):44    "A view of a bitmap.  Obtains data from document."45 46    def __init__(self, doc):47        docview.ScrollView.__init__(self, doc)48        self.width = self.height = 049        # set up message handlers50        self.HookMessage(self.OnSize, win32con.WM_SIZE)51 52    def OnInitialUpdate(self):53        doc = self.GetDocument()54        if doc.bitmap:55            bitmapSize = doc.bitmap.GetSize()56            self.SetScrollSizes(win32con.MM_TEXT, bitmapSize)57 58    def OnSize(self, params):59        lParam = params[3]60        self.width = win32api.LOWORD(lParam)61        self.height = win32api.HIWORD(lParam)62 63    def OnDraw(self, dc):64        # set sizes used for "non stretch" mode.65        doc = self.GetDocument()66        if doc.bitmap is None:67            return68        bitmapSize = doc.bitmap.GetSize()69        if bStretch:70            # stretch BMP.71            viewRect = (0, 0, self.width, self.height)72            bitmapRect = (0, 0, bitmapSize[0], bitmapSize[1])73            doc.bitmap.Paint(dc, viewRect, bitmapRect)74        else:75            # non stretch.76            doc.bitmap.Paint(dc)77 78 79class BitmapFrame(window.MDIChildWnd):80    def OnCreateClient(self, createparams, context):81        borderX = win32api.GetSystemMetrics(win32con.SM_CXFRAME)82        borderY = win32api.GetSystemMetrics(win32con.SM_CYFRAME)83        titleY = win32api.GetSystemMetrics(win32con.SM_CYCAPTION)  # includes border84        # try and maintain default window pos, else adjust if cant fit85        # get the main client window dimensions.86        mdiClient = win32ui.GetMainFrame().GetWindow(win32con.GW_CHILD)87        clientWindowRect = mdiClient.ScreenToClient(mdiClient.GetWindowRect())88        clientWindowSize = (89            clientWindowRect[2] - clientWindowRect[0],90            clientWindowRect[3] - clientWindowRect[1],91        )92        left, top, right, bottom = mdiClient.ScreenToClient(self.GetWindowRect())93        # 		width, height=context.doc.size[0], context.doc.size[1]94        # 		width = width+borderX*295        # 		height= height+titleY+borderY*2-196        # 		if (left+width)>clientWindowSize[0]:97        # 			left = clientWindowSize[0] - width98        # 		if left<0:99        # 			left = 0100        # 			width = clientWindowSize[0]101        # 		if (top+height)>clientWindowSize[1]:102        # 			top = clientWindowSize[1] - height103        # 		if top<0:104        # 			top = 0105        # 			height = clientWindowSize[1]106        # 		self.frame.MoveWindow((left, top, left+width, top+height),0)107        window.MDIChildWnd.OnCreateClient(self, createparams, context)108        return 1109 110 111class BitmapTemplate(docview.DocTemplate):112    def __init__(self):113        docview.DocTemplate.__init__(114            self, win32ui.IDR_PYTHONTYPE, BitmapDocument, BitmapFrame, BitmapView115        )116 117    def MatchDocType(self, fileName, fileType):118        doc = self.FindOpenDocument(fileName)119        if doc:120            return doc121        ext = os.path.splitext(fileName)[1].lower()122        if ext == ".bmp":  # removed due to PIL! or ext=='.ppm':123            return win32ui.CDocTemplate_Confidence_yesAttemptNative124        return win32ui.CDocTemplate_Confidence_maybeAttemptForeign125 126 127# 		return win32ui.CDocTemplate_Confidence_noAttempt128 129# For debugging purposes, when this module may be reloaded many times.130try:131    win32ui.GetApp().RemoveDocTemplate(bitmapTemplate)132except NameError:133    pass134 135bitmapTemplate = BitmapTemplate()136bitmapTemplate.SetDocStrings(137    "\nBitmap\nBitmap\nBitmap (*.bmp)\n.bmp\nPythonBitmapFileType\nPython Bitmap File"138)139win32ui.GetApp().AddDocTemplate(bitmapTemplate)140 141# This works, but just didnt make it through the code reorg.142# class PPMBitmap(Bitmap):143# 	def LoadBitmapFile(self, file ):144# 		magic=file.readline()145# 		if magic <> "P6\n":146# 			raise TypeError, "The file is not a PPM format file"147# 		rowcollist=string.split(file.readline())148# 		cols=string.atoi(rowcollist[0])149# 		rows=string.atoi(rowcollist[1])150# 		file.readline()	# whats this one?151# 		self.bitmap.LoadPPMFile(file,(cols,rows))152 153 154def t():155    bitmapTemplate.OpenDocumentFile("d:\\winnt\\arcade.bmp")156    # OpenBMPFile( 'd:\\winnt\\arcade.bmp')157 158 159def demo():160    import glob161 162    winDir = win32api.GetWindowsDirectory()163    for fileName in glob.glob1(winDir, "*.bmp")[:2]:164        bitmapTemplate.OpenDocumentFile(os.path.join(winDir, fileName))165 
codekingpro/portable-devtools · Team Ai