Multiple images for win32 icon
Multiple images for win32 icon


Index: pyglet/window/win32/__init__.py
===================================================================
--- pyglet/window/win32/__init__.py	(revision 721)
+++ pyglet/window/win32/__init__.py	(working copy)
@@ -482,40 +482,63 @@
         return Win32MouseCursor(cursor)

     def set_icon(self, *images):
-        # TODO multiple icon sizes
-
         # XXX Undocumented AFAICT, but XP seems happy to resize an image
-        # of any size, so no scaling to 16x16,32x32 necessary.
-        image = images[0].image_data
-        image.format = 'BGRA'
-        image.pitch = len(image.format) * image.width
+        # of any size, so no scaling necessary.

-        info = BITMAPINFO()
-        info.bmiHeader.biSize = sizeof(info.bmiHeader)
-        info.bmiHeader.biWidth = image.width
-        info.bmiHeader.biHeight = image.height
-        info.bmiHeader.biPlanes = 1
-        info.bmiHeader.biBitCount = 32
-        info.bmiHeader.biCompression = BI_RGB
+        def best_image(width, height):
+            # A heuristic for finding closest sized image to required size.
+            image = images[0]
+            for img in images:
+                if img.width == width and img.height == height:
+                    # Exact match always used
+                    return img
+                elif img.width >= width and \
+                     img.width * img.height > image.width * image.height:
+                    # At least wide enough, and largest area
+                    image = img
+            return image

-        dc = _gdi32.CreateDCA('DISPLAY', None, None, None)
-        bitmap = _gdi32.CreateDIBitmap(
-            dc, info.bmiHeader, CBM_INIT, image.data, info, DIB_RGB_COLORS)
+        def get_icon(image):
+            image.format = 'BGRA'
+            image.pitch = len(image.format) * image.width

-        # XXX Undocumented AFAICT, XP seems happy to use an 8-bit alpha
-        # as mask instead of a bitmask.
-        image.format = 'AAAA'
-        bitmap_mask = _gdi32.CreateDIBitmap(
-            dc, info.bmiHeader, CBM_INIT, image.data, info, DIB_RGB_COLORS)
+            info = BITMAPINFO()
+            info.bmiHeader.biSize = sizeof(info.bmiHeader)
+            info.bmiHeader.biWidth = image.width
+            info.bmiHeader.biHeight = image.height
+            info.bmiHeader.biPlanes = 1
+            info.bmiHeader.biBitCount = 32
+            info.bmiHeader.biCompression = BI_RGB

-        iconinfo = ICONINFO()
-        iconinfo.fIcon = True
-        iconinfo.hbmMask = bitmap_mask
-        iconinfo.hbmColor = bitmap
-        icon = _user32.CreateIconIndirect(byref(iconinfo))
+            dc = _gdi32.CreateDCA('DISPLAY', None, None, None)
+            bitmap = _gdi32.CreateDIBitmap(
+                dc, info.bmiHeader, CBM_INIT, image.data, info, DIB_RGB_COLORS)

+            # XXX Undocumented AFAICT, XP seems happy to use an 8-bit alpha
+            # as mask instead of a bitmask.
+            image.format = 'AAAA'
+            bitmap_mask = _gdi32.CreateDIBitmap(
+                dc, info.bmiHeader, CBM_INIT, image.data, info, DIB_RGB_COLORS)
+
+            iconinfo = ICONINFO()
+            iconinfo.fIcon = True
+            iconinfo.hbmMask = bitmap_mask
+            iconinfo.hbmColor = bitmap
+            icon = _user32.CreateIconIndirect(byref(iconinfo))
+            return icon
+
+        # Set large icon
+        image = best_image(_user32.GetSystemMetrics(SM_CXICON),
+                           _user32.GetSystemMetrics(SM_CYICON))
+        icon = get_icon(image)
         _user32.SetClassLongA(self._hwnd, GCL_HICON, icon)

+        # Set small icon
+        image = best_image(_user32.GetSystemMetrics(SM_CXSMICON),
+                           _user32.GetSystemMetrics(SM_CYSMICON))
+        icon = get_icon(image)
+        _user32.SetClassLongA(self._hwnd, GCL_HICONSM, icon)
+
     # Private util

     def _client_to_window_size(self, width, height):

arifgursel/pyglet,arifgursel/pyglet,Austin503/pyglet,cledio66/pyglet,Austin503/pyglet,qbektrix/pyglet,google-code-export/pyglet,mpasternak/pyglet-fix-issue-552,google-code-export/pyglet,qbektrix/pyglet,mpasternak/pyglet-fix-issue-518-522,qbektrix/pyglet,arifgursel/pyglet,odyaka341/pyglet,Alwnikrotikz/pyglet,kmonsoor/pyglet,shaileshgoogler/pyglet,arifgursel/pyglet,odyaka341/pyglet,mpasternak/michaldtz-fix-552,mpasternak/pyglet-fix-issue-552,Alwnikrotikz/pyglet,cledio66/pyglet,mpasternak/michaldtz-fix-552,shaileshgoogler/pyglet,shaileshgoogler/pyglet,xshotD/pyglet,Austin503/pyglet,gdkar/pyglet,google-code-export/pyglet,qbektrix/pyglet,mpasternak/michaldtz-fixes-518-522,Alwnikrotikz/pyglet,mpasternak/michaldtz-fix-552,mpasternak/michaldtz-fixes-518-522,xshotD/pyglet,kmonsoor/pyglet,mpasternak/pyglet-fix-issue-552,odyaka341/pyglet,gdkar/pyglet,kmonsoor/pyglet,mpasternak/michaldtz-fix-552,google-code-export/pyglet,mpasternak/michaldtz-fixes-518-522,gdkar/pyglet,cledio66/pyglet,kmonsoor/pyglet,mpasternak/pyglet-fix-issue-518-522,arifgursel/pyglet,odyaka341/pyglet,xshotD/pyglet,xshotD/pyglet,shaileshgoogler/pyglet,Austin503/pyglet,Alwnikrotikz/pyglet,mpasternak/pyglet-fix-issue-518-522,mpasternak/pyglet-fix-issue-518-522,gdkar/pyglet,mpasternak/michaldtz-fixes-518-522,xshotD/pyglet,gdkar/pyglet,Alwnikrotikz/pyglet,mpasternak/pyglet-fix-issue-552,google-code-export/pyglet,cledio66/pyglet,odyaka341/pyglet,kmonsoor/pyglet,qbektrix/pyglet,Austin503/pyglet,cledio66/pyglet,shaileshgoogler/pyglet