[Wnd-commit] wnd/wnd/controls imagelist.py,1.1.1.1,1.2
Status: Alpha
Brought to you by:
jurner
|
From: jürgen u. <cer...@us...> - 2005-05-15 09:24:09
|
Update of /cvsroot/wnd/wnd/wnd/controls In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30332 Modified Files: imagelist.py Log Message: new class SystemImagelist + bugfixes Index: imagelist.py =================================================================== RCS file: /cvsroot/wnd/wnd/wnd/controls/imagelist.py,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** imagelist.py 29 Apr 2005 15:21:34 -0000 1.1.1.1 --- imagelist.py 15 May 2005 09:23:38 -0000 1.2 *************** *** 11,22 **** --- 11,33 ---- from wnd import gdi from wnd.wintypes import (comctl32, + kernel32, + shell32, c_int, byref, + sizeof, + WINFUNCTYPE, + POINTER, memmove, POINT, + BOOL, + HANDLE, + INT, + SHFILEINFO, create_string_buffer) from wnd.fwtypes import TrackHandler from ctypes.com import COMObject from ctypes.com.storage import IStream + from wnd.api import winos + comctl32.InitCommonControls() *************** *** 89,92 **** --- 100,111 ---- "mask" : 16,"image" : 32,"rop" : 64,"overlaymask" : 3840} + FILEINFO_FLAGS={'linkoverlay' : 32768, + 'selected' : 65536, + 'largeicon' : 0, + 'smallicon' : 1, + 'openicon' : 2, + 'shelliconsize' : 4} #'sysiconindex' : 4096 + + #*********************************************** class ImagelistFromHandle(object): *************** *** 95,99 **** def __init__(self, handle): self.handle = handle ! TrackHandler.Register('imagelists', self.handle) def Write(self): --- 114,118 ---- def __init__(self, handle): self.handle = handle ! def Write(self): *************** *** 143,147 **** flag |= INDEXTOOVERLAYMASK(n) else: ! try: flag |= drawflags[n] except: raise ValueError("invalid style: %s" % n) handle = comctl32.ImageList_GetIcon(self.handle, i, flag) --- 162,166 ---- flag |= INDEXTOOVERLAYMASK(n) else: ! try: flag |= IMGL_DRAWFLAGS[n] except: raise ValueError("invalid style: %s" % n) handle = comctl32.ImageList_GetIcon(self.handle, i, flag) *************** *** 224,228 **** if not result: raise RuntimeError("could not draw image") ! def DrawEx(self, DC, i, x, y, w, h, colorBck, colorFg, *flags): CLR_NONE = 4294967295 CLR_DEFAULT = 4278190080 --- 243,247 ---- if not result: raise RuntimeError("could not draw image") ! def DrawEx(self, DC, i, x, y, w, h, colorBk, colorFg, *flags): CLR_NONE = 4294967295 CLR_DEFAULT = 4278190080 *************** *** 264,268 **** def __len__(self): return comctl32.ImageList_GetImageCount(self.handle) ! #def __del__(self): self.close() def Close(self): if self.handle: --- 283,302 ---- def __len__(self): return comctl32.ImageList_GetImageCount(self.handle) ! ! def Close(self): ! if hasattr(self, 'handle'): ! self.__delattr__('handle') ! else: ! raise RuntimeError("imagelist is closed") ! ! ! #************************************************** ! #************************************************** ! class DisposableImagelist(ImagelistFromHandle): ! ! def __init__(self, handle): ! self.handle= handle ! TrackHandler.Register('imagelists', self.handle) ! def Close(self): if self.handle: *************** *** 270,280 **** if not comctl32.ImageList_Destroy(self.handle): raise RuntimeError("could not destroy imagelist") ! self.handle = 0 ! ! #************************************************** #************************************************** ! class Imagelist(ImagelistFromHandle): def __init__(self, w, h, size, maxsize, *flags): --- 304,313 ---- if not comctl32.ImageList_Destroy(self.handle): raise RuntimeError("could not destroy imagelist") ! self.handle = 0 #************************************************** ! #************************************************** ! class Imagelist(DisposableImagelist): def __init__(self, w, h, size, maxsize, *flags): *************** *** 286,293 **** if not handle: raise RuntimeError("could not create imagelist") ! ImagelistFromHandle.__init__(self, handle) ! #************************************************ ! class ImagelistFromBytes(ImagelistFromHandle): def __init__(self, bytes): --- 319,327 ---- if not handle: raise RuntimeError("could not create imagelist") ! DisposableImagelist.__init__(self, handle) ! #************************************************** ! #************************************************** ! class ImagelistFromBytes(DisposableImagelist): def __init__(self, bytes): *************** *** 295,304 **** handle = comctl32.ImageList_Read(byref(self.stream.IStream)) if not handle: raise RuntimeError("could not create imagelist from bytes") ! ImagelistFromHandle.__init__(self, handle) del self.stream.IStream del self.stream #**************************************************** ! class ImagelistFromFile(ImagelistFromHandle): def __init__(self, path, w): --- 329,338 ---- handle = comctl32.ImageList_Read(byref(self.stream.IStream)) if not handle: raise RuntimeError("could not create imagelist from bytes") ! DisposableImagelist.__init__(self, handle) del self.stream.IStream del self.stream #**************************************************** ! class ImagelistFromFile(DisposableImagelist): def __init__(self, path, w): *************** *** 317,326 **** if not handle: raise RuntimeError("could not load imagelist from file") ! ImagelistFromHandle.__init__(self, handle) #************************************************** #************************************************** class DragImage: def __init__(self, imagelist): --- 351,459 ---- if not handle: raise RuntimeError("could not load imagelist from file") ! DisposableImagelist.__init__(self, handle) #************************************************** + #************************************************** + class SystemImagelist(object): + + def __init__(self, size='small'): + + GPA = kernel32.GetProcAddress + isNT= winos.IsNT() + if isNT: + ## ?? check what happens on successive inits + ## should be the same handle returned, at least I hope so + GPA.restype = WINFUNCTYPE(BOOL, BOOL) + try: + IconInit = GPA(shell32._handle, 660) + IconInit(1) + except: + raise RuntimeError, "could not retrieve IconInit api" + + GPA.restype = WINFUNCTYPE(INT, POINTER(HANDLE), POINTER(HANDLE)) + try: + SH_GetImageLists = GPA(shell32._handle, 71) + except: + raise RuntimeError, "could not retrieve shell_GetImageLists api" + + + small = HANDLE() + large = HANDLE() + if not SH_GetImageLists(byref(large), byref(small)): + raise RuntimeError, "could not retrieve system imagelist" + + if not (large.value and small.value): + raise RuntimeError, "could not retrieve system imagelist" + + if size=='small': + self.handle= small.value + else: + self.handle= large.value + + + + def GetIconIndex(self, path, *flags): + flag = 16384 # SHGFI_SYSICONINDEX + for i in flags: + try: flag |= FILEINFO_FLAGS[i] + except: raise ValueError("invalid flag: %s" % i) + + fileattributes = 0 + if path[0]=='*': + fileattributes = 128 # FILE_ATTRIBUTE_NORMAL + flag |= 16 # SHGFI_USEFILEATTRIBUTES + + if path=='directory': + fileattributes = 16 # FILE_ATTRIBUTE_DIRECTORY + flag |= 16 # SHGFI_USEFILEATTRIBUTES + + fi = SHFILEINFO() + if not shell32.SHGetFileInfoA(path, fileattributes, byref(fi), sizeof(SHFILEINFO), flag): + raise RuntimeError("could not retrieve icon") + return fi.iIcon + + + def Draw(self, DC, i, x, y, *flags): + flag = 0 + for n in flags: + if isinstance(n, (int, long)): flag |= INDEXTOOVERLAYMASK(n) + else: + try: flag |= IMGL_DRAWFLAGS[n] + except: raise ValueError("invalid draw flag: %s" % n) + if not comctl32.ImageList_Draw(self.handle, i, DC.handle, x, y, flag): + raise RuntimeError("could not draw image") + + def DrawEx(self, DC, i, x, y, w, h, colorBk, colorFg, *flags): + CLR_NONE = 4294967295 + CLR_DEFAULT = 4278190080 + if not colorBk: colorBk = CLR_NONE + elif colorBk=='default': colorBk = CLR_DEFAULT + else: colorBk = RGB(*colorBk) + if not colorFg: colorFg = CLR_NONE + elif colorFg=='default': colorFg = CLR_DEFAULT + else: colorFg = colorBk + flag = 0 + for n in flags: + if isinstance(n, int): flag |= INDEXTOOVERLAYMASK(n) + else: + try: flag |= IMGL_DRAWFLAGS[n] + except: raise ValueError("invalid draw flag: %s" % n) + result = comctl32.ImageList_DrawEx(self.handle, i, DC.handle, x, y, w, h, colorBk, colorFg, flag) + if not result: raise RuntimeError("could not draw image") + + def GetIconSize(self): + w = c_int() + h = c_int() + if not comctl32.ImageList_GetIconSize(self.handle, byref(w), byref(h)): + raise RuntimeError("could not retrieve icon size") + return w.value, h.value + + + def Close(self): + pass #************************************************** + #************************************************** class DragImage: def __init__(self, imagelist): |