[Wnd-commit] wnd/wnd/api/clipformats .CVSIGNORE,NONE,1.1 __init__.py,NONE,1.1 dropeffect_performed.p
Status: Alpha
Brought to you by:
jurner
|
From: jürgen u. <cer...@us...> - 2005-07-02 08:56:14
|
Update of /cvsroot/wnd/wnd/wnd/api/clipformats In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25263 Added Files: .CVSIGNORE __init__.py dropeffect_performed.py dropeffect_prefered.py filename.py filenamemap.py hdropex.py hdropfiles.py htmlraw.py idlistarray.py null.py oemtext.py text.py unicodetext.py Log Message: bit of this and a bit of that --- NEW FILE: filenamemap.py --- from wnd.api.clipformats import * from ctypes import c_char_p, windll shell32= windll.shell32 #************************************************************* # dropped files format ("FileNameMap") #************************************************************* def ValueToHandle(paths): hMem= None ## unicode version ## #isunicode= False #if isunicode: # paths= '%s\x00\x00' % '\x00'.join(paths) # n= len(paths)*2 #else: paths= '%s\x00\x00' % '\x00'.join(paths) n= len(paths) hMem= kernel32.GlobalAlloc(GMEM_FIXED, n) if hMem: memmove(hMem, paths, n) return hMem def HandleToValue(hMem): paths= [] n= kernel32.GlobalSize(hMem) if n: p= create_string_buffer(n) mem= kernel32.GlobalLock(hMem) if mem: memmove(p, mem, n) x= 0 for n, i in enumerate(p): if i=='\x00': paths.append(p[x:n]) x= n +1 try: if p[n+1]=='\x00': break except: paths= [] break kernel32.GlobalUnlock(hMem) return paths #************************************************************* #************************************************************* class filenamemap(object): fmt= FORMATETC() fmt.cfFormat = RegisterClipboardFormat("FileNameMap") fmt.dwAspect = DVASPECT_CONTENT fmt.lindex = INDEX_ALL fmt.tymed = TYMED_HGLOBAL def __init__(self, data=None): self.stg= STGMEDIUM() self.stg.tymed = self.fmt.tymed if data: self._set_value(data) def _set_value(self, data): if data==None: self.stg.hGlobal= 0 else: if self.stg.hGlobal: self.stg.hGlobal= 0 self.stg.hGlobal = ValueToHandle(data) def _get_value(self): if self.stg.hGlobal: return HandleToValue(self.stg.hGlobal) def Close(self): self.stg.hGlobal= 0 def __eq__(self, other): return IsSameFormat(self, other) def __repr__(self): return Repr(self) value= property(_get_value, _set_value) #**************************************************************************** def test(): testformat(filenamemap, ['c:\\p', 'c:\\klmnopwqw','c:\\ssst']) if __name__=='__main__': test() --- NEW FILE: .CVSIGNORE --- *.pyc *.pyo --- NEW FILE: oemtext.py --- from wnd.api.clipformats import * from ctypes import c_char_p #************************************************************* # oem text format (CF_OEMTEXT) #************************************************************* CF_OEMTEXT = 7 class oemtext(object): fmt= FORMATETC() fmt.cfFormat = CF_OEMTEXT fmt.dwAspect = DVASPECT_CONTENT fmt.lindex = INDEX_ALL fmt.tymed = TYMED_HGLOBAL def __init__(self, text=None): self.stg= STGMEDIUM() self.stg.tymed = self.fmt.tymed if text: self._set_value(text) def _set_value(self, text): if text==None: self.stg.hGlobal= 0 else: if self.stg.hGlobal: self.stg.hGlobal= 0 self.stg.hGlobal = StringToHandle(text) def _get_value(self): if self.stg.hGlobal: kernel32.GlobalLock.restype= c_char_p value= kernel32.GlobalLock(self.stg.hGlobal) kernel32.GlobalUnlock(self.stg.hGlobal) return value def Close(self): self.stg.hGlobal= 0 def __eq__(self, other): return IsSameFormat(self, other) def __repr__(self): return Repr(self) value= property(_get_value, _set_value) #***************************************************************** def test(): testformat(oemtext, 'aaa\n bbb ccc\n ddd\n') if __name__=='__main__': test() --- NEW FILE: dropeffect_performed.py --- from wnd.api.clipformats import * from ctypes import c_ulong #************************************************************* # performed drop effect ("Performed DropEffect") #************************************************************* class Ddropeffect_performed(object): fmt= FORMATETC() fmt.cfFormat = RegisterClipboardFormat("Performed DropEffect") fmt.dwAspect = DVASPECT_CONTENT fmt.lindex = INDEX_ALL fmt.tymed = TYMED_HGLOBAL def __init__(self, value=None): self.stg= STGMEDIUM() self.stg.tymed = self.fmt.tymed if value: self._set_value(value) def _set_value(self, value): if value==None: self.stg.hGlobal= 0 else: if self.stg.hGlobal: self.stg.hGlobal= 0 hMem = kernel32.GlobalAlloc(GMEM_FIXED, sizeof(c_ulong)) memmove(hMem, buffer(c_ulong(value))[:], sizeof(c_ulong)) self.stg.hGlobal= hMem def _get_value(self): if self.stg.hGlobal: kernel32.GlobalLock.restype= POINTER(c_ulong) value= kernel32.GlobalLock(self.stg.hGlobal) kernel32.GlobalUnlock(self.stg.hGlobal) if value: return value[0] def Close(self): self.stg.hGlobal= 0 def __eq__(self, other): return IsSameFormat(self, other) def __repr__(self): return Repr(self) value= property(_get_value, _set_value) #**************************************************************************** def test(): DROPEFFECT_COPY = 1 testformat(dropeffect_performed, DROPEFFECT_COPY) if __name__=='__main__': test() --- NEW FILE: idlistarray.py --- from wnd.api.clipformats import * from wnd.api.shell import Malloc, PidlGetSize, PIDL, ITEMIDLIST #************************************************************* # idlist array "Shell IDList Array" #************************************************************* class CIDA(Structure): _fields_ = [("cidl", UINT), # n-offsets in offset array ("aoffset", UINT*1)] # var length offset array # the structure is followed by an array of IDLISTs, 1st the absolute of the folder, # then the relative of the items. aoffset is the offset of the next item from the # beginning of the structure. def HandleToValue(hMem): if hMem: n= kernel32.GlobalSize(hMem) if n >= sizeof(CIDA): kernel32.GlobalLock.restype= POINTER(CIDA) pCida= kernel32.GlobalLock(hMem) if pCida: ## get the offset array ## cidl seems to be +1 -- aoffset[1], is not counted addr= addressof(pCida[0]) offs= (UINT*(pCida[0].cidl+1)).from_address(addr + \ sizeof(UINT)) # copy ITEMIDLISTs from global mem to shell allocated mem out= [] for i in offs: error= True if addr +i <= addr + n: il= ITEMIDLIST.from_address(int(addr +i)) size= PidlGetSize(byref(il)) pMem= Malloc.Alloc(size) if pMem: if memmove(pMem, byref(il), size): out.append(PIDL(ITEMIDLIST.from_address(pMem))) error= False if error: for i in out: shell.PidlFree(i) return None return out def ValueToHandle(data): if data: ## prep the CIDA struct and fill in the offset members ## have to work with two arrays here cida= CIDA(len(data)-1) arrOffs= (UINT*(len(data)-1))() offs= sizeof(cida) + sizeof(arrOffs) for n, i in enumerate(data): if n==0: cida.aoffset[0]= offs else: arrOffs[n-1]= offs # append later offs += PidlGetSize(i) hMem= kernel32.GlobalAlloc(GMEM_FIXED, offs) if hMem: ## move the CIDA structure + array to global mem p= hMem memmove(hMem, byref(cida), sizeof(cida)) p += sizeof(cida) memmove(p, byref(arrOffs), sizeof(arrOffs)) p += sizeof(arrOffs) ## move the pIdls to global mem n= PidlGetSize(data[0]) memmove(p, data[0], n) p += n for n, i in enumerate(data[1:]): n= PidlGetSize(i) memmove(p, i, n) p += n return hMem return 0 #************************************************************* #************************************************************* class idlistarray(object): fmt= FORMATETC() fmt.cfFormat = RegisterClipboardFormat("Shell IDList Array") fmt.dwAspect = DVASPECT_CONTENT fmt.lindex = INDEX_ALL fmt.tymed = TYMED_HGLOBAL def __init__(self, data=None): self.stg= STGMEDIUM() self.stg.tymed = self.fmt.tymed if data: self._set_value(data) def _set_value(self, data): if data==None: self.stg.hGlobal= 0 else: if self.stg.hGlobal: self.stg.hGlobal= 0 self.stg.hGlobal = ValueToHandle(data) def _get_value(self): if self.stg.hGlobal: return HandleToValue(self.stg.hGlobal) def Close(self): self.stg.hGlobal= 0 def __eq__(self, other): return IsSameFormat(self, other) def __repr__(self): return Repr(self) value= property(_get_value, _set_value) #**************************************************************************** def test(): from wnd.api import shell from wnd.api import clipboard from wnd.api.ole import dataobject print '** test clipformatIDLISTARRAY*************************************' ## get some friendly pIdls ## pass them to our format and set it to clipboard pIdls, paths= [], [] sh= shell.ShellNamespace() sh.OpenSpecialFolder(shell.CLSIDL_DRIVES) pIdls.append(sh.GetCwd()) paths.append(sh.GetParseName()) for i in sh: pIdls.append(i) paths.append(sh.GetParseName(i)) il=idlistarray(pIdls) for i in pIdls: shell.PidlFree(i) pIdls= [] print 'set clipboard=%s' % paths[0] for i in paths[1:]: print ' %s' % i do= dataobject.DataObject(il) clipboard.SetDataObject(do) clipboard.Flush() il.value= None ## retrive the format from clipboard do2= clipboard.GetDataObject() if do2.HasFormat(il): do2.GetData(il) pIdls= il.value il.value= None paths2= [] if pIdls: sh.SetCwd(pIdls[0]) paths2.append(sh.GetParseName()) for i in pIdls[1:]: paths2.append(sh.GetParseName(i)) for i in pIdls: shell.PidlFree(i) print 'get clipboard=%s' % paths2[0] for i in paths2[1:]: print ' %s' % i else: print 'get clipboard=' if paths==paths2: print '<success>' else: print '<error>' print if __name__=='__main__': test() --- NEW FILE: null.py --- from wnd.api.clipformats import * #************************************************************************** # null format # # empty format or used to retrieve data or to setup a custom format # # return value of getattr('value') is allways the memory handle # of the storage medium # # fmt is defined at compile time, too, to make the test for # IsSameFormat(NULL, other) work. #************************************************************************ class null(object): fmt= FORMATETC() ## makes __eq__ work on the class and on the instance ## cos the instance may change in format fmt.cfFormat = 0 fmt.dwAspect = DVASPECT_CONTENT fmt.lindex = INDEX_ALL fmt.tymed = TYMED_NULL def __init__(self, fmt=None, stg=None): if fmt: self.fmt= fmt else: self.fmt= FORMATETC() self.fmt.cfFormat = 0 self.fmt.dwAspect = DVASPECT_CONTENT self.fmt.lindex = INDEX_ALL self.fmt.tymed = TYMED_NULL if stg: self.stg= stg else: self.stg= STGMEDIUM() self.stg.tymed = self.fmt.tymed def _set_value(self, hMem): if hMem==None: self.stg.hGlobal= 0 else: if self.stg.hGlobal: self.stg.hGlobal= 0 self.stg.hGlobal = hMem def _get_value(self): if self.stg.hGlobal: return self.stg.hGlobal def Close(self): self.stg.hGlobal= 0 def __eq__(self, other): return IsSameFormat(self, other) def __repr__(self): return Repr(self) value= property(_get_value, _set_value) --- NEW FILE: filename.py --- from wnd.api.clipformats import * from ctypes import c_char_p #************************************************************* # filename format ("FileName") #************************************************************* class filename(object): fmt= FORMATETC() fmt.cfFormat = RegisterClipboardFormat("FileName") fmt.dwAspect = DVASPECT_CONTENT fmt.lindex = INDEX_ALL fmt.tymed = TYMED_HGLOBAL def __init__(self, path=None): self.stg= STGMEDIUM() self.stg.tymed = self.fmt.tymed if path: self._set_value(path) def _set_value(self, path): if path==None: self.stg.hGlobal= 0 else: if self.stg.hGlobal: self.stg.hGlobal= 0 self.stg.hGlobal = StringToHandle(path) def _get_value(self): if self.stg.hGlobal: kernel32.GlobalLock.restype= c_char_p value= kernel32.GlobalLock(self.stg.hGlobal) kernel32.GlobalUnlock(self.stg.hGlobal) return value def Close(self): self.stg.hGlobal= 0 def __eq__(self, other): return IsSameFormat(self, other) def __repr__(self): return Repr(self) value= property(_get_value, _set_value) #**************************************************************************** def test(): testformat(filename, 'c:\\klmnopwqw.ddd') if __name__=='__main__': test() --- NEW FILE: dropeffect_prefered.py --- from wnd.api.clipformats import * from ctypes import c_ulong #************************************************************* # prefered drop effect ("Preferred DropEffect") #************************************************************* class dropeffect_prefered(object): fmt= FORMATETC() fmt.cfFormat = RegisterClipboardFormat("Preferred DropEffect") fmt.dwAspect = DVASPECT_CONTENT fmt.lindex = INDEX_ALL fmt.tymed = TYMED_HGLOBAL def __init__(self, value=None): self.stg= STGMEDIUM() self.stg.tymed = self.fmt.tymed if value: self._set_value(value) def _set_value(self, value): if value==None: self.stg.hGlobal= 0 else: if self.stg.hGlobal: self.stg.hGlobal= 0 hMem = kernel32.GlobalAlloc(GMEM_FIXED, sizeof(c_ulong)) memmove(hMem, buffer(c_ulong(value))[:], sizeof(c_ulong)) self.stg.hGlobal= hMem def _get_value(self): if self.stg.hGlobal: kernel32.GlobalLock.restype= POINTER(c_ulong) value= kernel32.GlobalLock(self.stg.hGlobal) kernel32.GlobalUnlock(self.stg.hGlobal) if value: return value[0] def Close(self): self.stg.hGlobal= 0 def __eq__(self, other): return IsSameFormat(self, other) def __repr__(self): return Repr(self) value= property(_get_value, _set_value) #**************************************************************************** def test(): DROPEFFECT_COPY = 1 testformat(dropeffect_prefered, DROPEFFECT_COPY) if __name__=='__main__': test() --- NEW FILE: htmlraw.py --- from wnd.api.clipformats import * from ctypes import c_char_p #************************************************************* # raw html format (CF_HDROP) #************************************************************* class htmlraw(object): fmt= FORMATETC() fmt.cfFormat = RegisterClipboardFormat("HTML Format") fmt.dwAspect = DVASPECT_CONTENT fmt.lindex = INDEX_ALL fmt.tymed = TYMED_HGLOBAL def __init__(self, html=None): self.stg= STGMEDIUM() self.stg.tymed = self.fmt.tymed if html: self._set_value(html) def _set_value(self, html): if html==None: self.stg.hGlobal= 0 else: if self.stg.hGlobal: self.stg.hGlobal= 0 self.stg.hGlobal = StringToHandle(html) def _get_value(self): if self.stg.hGlobal: kernel32.GlobalLock.restype= c_char_p value= kernel32.GlobalLock(self.stg.hGlobal) kernel32.GlobalUnlock(self.stg.hGlobal) return value def Close(self): self.stg.hGlobal= 0 def __eq__(self, other): return IsSameFormat(self, other) def __repr__(self): return Repr(self) value= property(_get_value, _set_value) #****************************************************************************** ## just a simple test, not a test of the format def test(): testformat(htmlraw, 'aaa\n bbb ccc\n ddd\n') if __name__=='__main__': test() --- NEW FILE: __init__.py --- """helpers and data for clipboard and clipformats""" import imp, os from ctypes import windll from wnd.api.ole.wintypes import * kernel32= windll.kernel32 user32= windll.user32 #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: _PATH= os.path.split(__file__)[0] #************************************************************************** #************************************************************************** CF_ASPECT = { DVASPECT_CONTENT:'content', DVASPECT_THUMBNAIL:'thumbnail', DVASPECT_ICON:'icon', DVASPECT_DOCPRINT:'docprint' } CF_TYMED = { TYMED_NULL:'null', TYMED_HGLOBAL:'hglobal', TYMED_FILE:'file', TYMED_ISTREAM:'stream', TYMED_ISTORAGE:'storage', TYMED_GDI:'gdi', TYMED_MFPICT:'mfpict', TYMED_ENHMF:'enhmf' } CF_INDEX= { INDEX_ALL:'all', } RegisterClipboardFormat= user32.RegisterClipboardFormatA CF_NAMES= ['null', 'text', 'bitmap', 'metafilepict', 'sylk', 'dif', 'tiff', 'oemtext', 'dib', 'palette', 'pendata', 'riff', 'wave', 'unicodetext', 'enhmetafile', 'hdrop', 'locale', 'dibv5',] FW_NAMES= { "html format": 'html', "shell idlist array": 'idlarray', "shell object offsets": 'objoffset', "net resource": 'netres', "filegroupdescriptor": 'filegroup', "filecontents": 'filecotents', "filename": 'filename', "printerfriendlyname": 'printername', "filenamemap": 'filenamemap', "uniformresourcelocator": 'url', "preferred dropeffect": 'dropeffect_prefered', "performed dropeffect": 'dropeffect_performed', "paste succeeded": 'pastesuceeded', "inshelldragloop": 'dragloop', } def GetFormatName(format, nBuffer=56): if format.fmt.cfFormat < 18: return CF_NAMES[format.fmt.cfFormat] p= create_string_buffer(nBuffer) user32.GetClipboardFormatNameA(format.fmt.cfFormat, p, nBuffer) if p.value: return p.value def GetFrameworkName(format): if format.fmt.cfFormat < 18: return CF_NAMES[format.fmt.cfFormat] try: return FW_NAMES[GetFormatName(format).lower()] except: pass def GetFormatNameN(nFormat, nBuffer=56): if nFormat < 18: return CF_NAMES[nFormat] p= create_string_buffer(nBuffer) user32.GetClipboardFormatNameA(nFormat) if p.value: return p.value def GetFrameworkNameN(nFormat): if nFormat < 18: return CF_NAMES[nFormat] try: return FW_NAMES[GetFormatNameN(nFormat).lower()] except: pass ## import format wrappers dynamically class _cf(object): def __getattribute__(self, name): try: mod= globals()[name] return getattr(mod, name) except: try: mod= imp.load_source(name, os.path.join(_PATH, '%s.py' % name)) globals()[name]= mod return getattr(mod, name) except IOError: raise ValueError, "no matching clipformat found: %s" % name except Exception, d: raise d cf= _cf() #******************************************************************* # helper methods for clipformat wrapper classes #******************************************************************* def IsSameFormat(fmt1, fmt2): if fmt1.fmt.cfFormat== fmt2.fmt.cfFormat: if fmt1.fmt.dwAspect==fmt2.fmt.dwAspect: if (fmt1.fmt.tymed == fmt2.fmt.tymed == 0) or \ (fmt1.fmt.tymed & fmt2.fmt.tymed): return True return False def Repr(format): name= GetFormatName(format) if name==None: name= 'unknown' if format.fmt.tymed== 0: tymed= CF_TYMED[0] else: out= [] for value, medname in CF_TYMED.items(): if format.fmt.tymed & value: out.append(medname) tymed= '/'.join(out) return "<wnd.cf.%s at %x {format=%s} {aspect=%s} {tymed=%s} {index=%s}>" % \ (format.__class__.__name__, id(format), name, CF_ASPECT[format.fmt.dwAspect], tymed, format.fmt.lindex ) GMEM_FIXED = 0 ## returns a chunk of global memory containing the passed string ## use GlobaFree to free it def StringToHandle(text): if isinstance(text, str): n= len(text) +1 elif isinstance(text, unicode): n= (len(text)*2) +2 else: raise ValueError, "string or unicode expected found: %s" % type(text) # no GlobalLock/unlock needed for GMEM_FIXED hMem = kernel32.GlobalAlloc(GMEM_FIXED, n) if hMem: if not memmove(hMem, text, n): raise MemoryError, "could not memmove text" else: raise MemoryError, "could alloc memory" return hMem ## test helper def testformat(FMT, data): from wnd.api import clipboard from wnd.api.ole import dataobject print '** test clipformat %s*************************************' % FMT.__name__ print 'set clipboard=%s' % repr(data) fmt= FMT(data) try: do= dataobject.DataObject(fmt) clipboard.SetDataObject(do) clipboard.Flush() fmt.value= None do2= clipboard.GetDataObject() if do2.HasFormat(fmt): print fmt.value do2.GetData(fmt) print 'get clipboard=%s' % repr(fmt.value) if fmt.value==data: print '<success>' else: print '<error>' finally: fmt.Close() print --- NEW FILE: hdropfiles.py --- from wnd.api.clipformats import * from ctypes import c_char_p, windll shell32= windll.shell32 #************************************************************* # dropped files format CF_HDROP #************************************************************* class DROPFILES(Structure): _fields_ = [("pFiles", DWORD), ("pt", POINT), ("fNC", BOOL), ("fWide", BOOL)] def ValueToHandle(paths): szeof= sizeof(DROPFILES) df= DROPFILES(szeof) hMem= None ## unicode version ## #isunicode= False #if isunicode: # paths= '%s\x00\x00' % '\x00'.join(paths) # n= len(paths)*2 #else: paths= '%s\x00\x00' % '\x00'.join(paths) n= len(paths) hMem= kernel32.GlobalAlloc(GMEM_FIXED, szeof + n) if hMem: memmove(hMem, byref(df), szeof) memmove(hMem+szeof, paths, n) return hMem def HandleToValue(hMem): out= [] result= shell32.DragQueryFileA(hMem, 0xFFFFFFFF, None, 0) p= create_string_buffer(260) for i in range(result): shell32.DragQueryFile(hMem, i, p, 260) out.append(p.value) return out ## seems to be quite some discussion about wether if zo pull in DropFinish ## for closing the mem handle or not. Noone knows and so do I. #************************************************************* #************************************************************* CF_HDROP = 15 class hdropfiles(object): fmt= FORMATETC() fmt.cfFormat = CF_HDROP fmt.dwAspect = DVASPECT_CONTENT fmt.lindex = INDEX_ALL fmt.tymed = TYMED_HGLOBAL def __init__(self, data=None): self.stg= STGMEDIUM() self.stg.tymed = self.fmt.tymed if data: self._set_value(data) def _set_value(self, data): if data==None: self.stg.hGlobal= 0 else: if self.stg.hGlobal: self.stg.hGlobal= 0 self.stg.hGlobal = ValueToHandle(data) def _get_value(self): if self.stg.hGlobal: return HandleToValue(self.stg.hGlobal) def Close(self): self.stg.hGlobal= 0 def __eq__(self, other): return IsSameFormat(self, other) def __repr__(self): return Repr(self) value= property(_get_value, _set_value) #**************************************************************************** def test(): testformat(hdropfiles, ['c:\\p', 'c:\\klmnopwqw','c:\\ssst']) if __name__=='__main__': test() --- NEW FILE: text.py --- from wnd.api.clipformats import * from ctypes import c_char_p #************************************************************* # ansi text format (CF_TEXT) #************************************************************* class CIDA(Structure): _fields_ = [("cidl", UINT), ("aoffset", UINT*1)] CF_TEXT= 1 class text(object): fmt= FORMATETC() fmt.cfFormat = CF_TEXT fmt.dwAspect = DVASPECT_CONTENT fmt.lindex = INDEX_ALL fmt.tymed = TYMED_HGLOBAL def __init__(self, text=None): self.stg= STGMEDIUM() self.stg.tymed = self.fmt.tymed if text: self._set_value(text) def _set_value(self, text): if text==None: self.stg.hGlobal= 0 else: if self.stg.hGlobal: self.stg.hGlobal= 0 self.stg.hGlobal = StringToHandle(text) def _get_value(self): if self.stg.hGlobal: kernel32.GlobalLock.restype= c_char_p value= kernel32.GlobalLock(self.stg.hGlobal) kernel32.GlobalUnlock(self.stg.hGlobal) return value def Close(self): self.stg.hGlobal= 0 def __eq__(self, other): return IsSameFormat(self, other) def __repr__(self): return Repr(self) value= property(_get_value, _set_value) #**************************************************************************** def test(): testformat(text,'aaa\n bbb ccc\n ddd\n') if __name__=='__main__': test() --- NEW FILE: unicodetext.py --- from wnd.api.clipformats import * from ctypes import c_wchar_p import locale LOCALE= locale.getdefaultlocale()[1] #************************************************************* # unicode text format (CF_UBICODETEXT) #************************************************************* CF_UNICODETEXT = 13 class unicodetext(object): fmt= FORMATETC() fmt.cfFormat = CF_UNICODETEXT fmt.dwAspect = DVASPECT_CONTENT fmt.lindex = INDEX_ALL fmt.tymed = TYMED_HGLOBAL def __init__(self, text=None): self.stg= STGMEDIUM() self.stg.tymed = self.fmt.tymed if text: self._set_value(text) def _set_value(self, text): if text==None: self.stg.hGlobal= 0 else: if self.stg.hGlobal: self.stg.hGlobal= 0 self.stg.hGlobal = StringToHandle(text) def _get_value(self): if self.stg.hGlobal: kernel32.GlobalLock.restype= c_wchar_p value= kernel32.GlobalLock(self.stg.hGlobal) kernel32.GlobalUnlock(self.stg.hGlobal) return value def Close(self): self.stg.hGlobal= 0 def __eq__(self, other): return IsSameFormat(self, other) def __repr__(self): return Repr(self) value= property(_get_value, _set_value) #**************************************************************************** def test(): testformat(unicodetext, u'aaa\n bbb ccc\n ddd\n') if __name__=='__main__': test() --- NEW FILE: hdropex.py --- from wnd.api.clipformats import * from ctypes import c_char_p, windll #************************************************************* # extended dropped files format CF_HDROP #************************************************************* class DROPFILES(Structure): _fields_ = [("pFiles", DWORD), ("pt", POINT), ("fNC", BOOL), ("fWide", BOOL)] def ValueToHandle(x, y, fClientarea, isunicode, paths): szeof= sizeof(DROPFILES) df= DROPFILES(szeof, (x, y), fClientarea and [0][0] or 1, isunicode and 1 or 0) hMem= None if isunicode: paths= '%s\x00\x00' % '\x00'.join(paths) n= len(paths)*2 else: paths= '%s\x00\x00' % '\x00'.join(paths) n= len(paths) hMem= kernel32.GlobalAlloc(GMEM_FIXED, szeof + n) if hMem: memmove(hMem, byref(df), szeof) memmove(hMem+szeof, paths, n) return hMem def HandleToValue(hMem): n= kernel32.GlobalSize(hMem) szeof= sizeof(DROPFILES) if n >= szeof: pdf= POINTER(DROPFILES).from_address(int(hMem)) ## int(hMem) ?? ## ctypes loves to raise here.. ## <int expected> df= pdf[0] paths= [] if df.pFiles >= szeof: if df.fWide: p= create_unicode_buffer(n-szeof) else: p= create_string_buffer(n-szeof) memmove(p, addressof(df)+szeof, n-szeof) x= 0 for n, i in enumerate(p): if i=='\x00': paths.append(p[x:n]) x= n +1 try: if p[n+1]=='\x00': break except: paths= [] break return (df.pt.x, df.pt.y, df.fNC, df.fWide, paths) #************************************************************* #************************************************************* CF_HDROP = 15 class hdropex(object): fmt= FORMATETC() fmt.cfFormat = CF_HDROP fmt.dwAspect = DVASPECT_CONTENT fmt.lindex = INDEX_ALL fmt.tymed = TYMED_HGLOBAL def __init__(self, data=None): self.stg= STGMEDIUM() self.stg.tymed = self.fmt.tymed if data: self._set_value(data) def _set_value(self, data): if data==None: self.stg.hGlobal= 0 else: if self.stg.hGlobal: self.stg.hGlobal= 0 self.stg.hGlobal = ValueToHandle(*data) def _get_value(self): if self.stg.hGlobal: return HandleToValue(self.stg.hGlobal) def Close(self): self.stg.hGlobal= 0 def __eq__(self, other): return IsSameFormat(self, other) def __repr__(self): return Repr(self) value= property(_get_value, _set_value) #**************************************************************************** def test(): testformat(hdropex, (12, 44, 1, 0, ['c:\\p', 'c:\\klmnopwqw','c:\\ssst'])) if __name__=='__main__': test() |