changeset 1820:ece7befa9f3d

Initial rewrite of dw_window_set_bitmap() to work on bitmap buttons on Windows.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 25 Oct 2012 23:15:46 +0000
parents d784b85b632e
children 69f9aa1e1b1e
files win/dw.c
diffstat 1 files changed, 87 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/win/dw.c	Fri Oct 19 22:35:23 2012 +0000
+++ b/win/dw.c	Thu Oct 25 23:15:46 2012 +0000
@@ -6621,6 +6621,89 @@
             (LPARAM) hicon);
 }
 
+/* Internal function to set bitmap for the next two functions */
+void _dw_window_set_bitmap(HWND handle, HICON icon, HBITMAP hbitmap)
+{
+   HBITMAP oldbitmap = 0;
+   HANDLE oldicon = 0;
+   TCHAR tmpbuf[100] = {0};
+   
+   if (!icon && !hbitmap)
+      return;
+
+   GetClassName(handle, tmpbuf, 99);
+  
+   if(_tcsnicmp(tmpbuf, BUTTONCLASSNAME, _tcslen(BUTTONCLASSNAME)+1)==0)
+   {
+      oldbitmap = (HBITMAP)SendMessage(handle, BM_GETIMAGE, IMAGE_BITMAP, 0);
+      oldicon = (HICON)SendMessage(handle, BM_GETIMAGE, IMAGE_ICON, 0);
+      SendMessage(handle, BM_SETIMAGE,
+               (icon ? (WPARAM)IMAGE_ICON : (WPARAM)IMAGE_BITMAP),
+               (icon ? (LPARAM)icon : (LPARAM)hbitmap));
+   }
+#ifdef TOOLBAR   
+   /* Bitmap Buttons */
+   else if(_tcsnicmp(tmpbuf, TOOLBARCLASSNAME, _tcslen(TOOLBARCLASSNAME)+1) == 0)
+   {
+      HIMAGELIST imlist = (HIMAGELIST)SendMessage(handle, TB_GETIMAGELIST, 0, 0);
+      HIMAGELIST dimlist = (HIMAGELIST)SendMessage(handle, TB_GETDISABLEDIMAGELIST, 0, 0);
+      BITMAP bmi = { 0 };
+     
+      if(hbitmap)
+      {
+         GetObject(hbitmap, sizeof(BITMAP), &bmi);
+         imlist = ImageList_Create(bmi.bmWidth, bmi.bmHeight, ILC_COLOR32, 1, 0);
+         ImageList_Replace(imlist, 0, hbitmap, NULL);
+         dimlist = ImageList_Create(bmi.bmWidth, bmi.bmHeight, ILC_COLOR32, 1, 0);
+         _to_grayscale(hbitmap, bmi.bmWidth, bmi.bmHeight);
+         ImageList_Replace(dimlist, 0, hbitmap, NULL);
+         DeleteObject(hbitmap);
+      }
+      else if(icon)
+      {
+         ICONINFO iconinfo;
+         
+         GetIconInfo(icon, &iconinfo);
+         GetObject(iconinfo.hbmColor, sizeof(BITMAP), &bmi);
+         imlist = ImageList_Create(bmi.bmWidth, bmi.bmHeight, ILC_COLOR32 | ILC_MASK, 1, 0);
+         ImageList_ReplaceIcon(imlist, 0, icon);
+         dimlist = ImageList_Create(bmi.bmWidth, bmi.bmHeight, ILC_COLOR32 | ILC_MASK, 1, 0);
+         _to_grayscale(iconinfo.hbmColor, bmi.bmWidth, bmi.bmHeight);
+         ImageList_Replace(dimlist, 0, iconinfo.hbmColor, iconinfo.hbmMask);
+         DeleteObject(iconinfo.hbmColor);
+         DeleteObject(iconinfo.hbmMask);
+         DestroyIcon(icon);
+      }
+   }
+#endif   
+   else
+   {
+      oldbitmap = (HBITMAP)SendMessage(handle, STM_GETIMAGE, IMAGE_BITMAP, 0);
+      oldicon = (HICON)SendMessage(handle, STM_GETIMAGE, IMAGE_ICON, 0);
+      SendMessage(handle, STM_SETIMAGE,
+               (icon ? (WPARAM)IMAGE_ICON : (WPARAM)IMAGE_BITMAP),
+               (icon ? (LPARAM)icon : (LPARAM)hbitmap));
+   }
+
+   if(oldbitmap)
+      DeleteObject(oldbitmap);
+   if(oldicon)
+      DeleteObject(oldicon);
+
+   /* If we changed the bitmap... */
+   {
+      Item *item = _box_item(handle);
+       
+      /* Check to see if any of the sizes need to be recalculated */
+      if(item && (item->origwidth == -1 || item->origheight == -1))
+      {
+         _control_size(handle, item->origwidth == -1 ? &item->width : NULL, item->origheight == -1 ? &item->height : NULL); 
+         /* Queue a redraw on the top-level window */
+         _dw_redraw(_toplevel_window(handle), TRUE);
+      }
+   }
+}
+
 /*
  * Sets the bitmap used for a given static window.
  * Parameters:
@@ -6634,9 +6717,7 @@
 void API dw_window_set_bitmap(HWND handle, unsigned long id, char *filename)
 {
    HBITMAP hbitmap = 0;
-   HBITMAP oldbitmap = (HBITMAP)SendMessage(handle, STM_GETIMAGE, IMAGE_BITMAP, 0);
    HANDLE icon = 0;
-   HANDLE oldicon = (HICON)SendMessage(handle, STM_GETIMAGE, IMAGE_ICON, 0);
 
    if(id)
    {
@@ -6650,46 +6731,9 @@
 #else
       _dw_get_image_handle(filename, &icon, &hbitmap);
 #endif
-      if (icon == 0 && hbitmap == 0)
-         return;
-   }
-
-   if(icon)
-   {
-      SendMessage(handle, BM_SETIMAGE,
-               (WPARAM) IMAGE_ICON,
-               (LPARAM) icon);
-      SendMessage(handle, STM_SETIMAGE,
-               (WPARAM) IMAGE_ICON,
-               (LPARAM) icon);
-   }
-   else if(hbitmap)
-   {
-      SendMessage(handle, BM_SETIMAGE,
-               (WPARAM) IMAGE_BITMAP,
-               (LPARAM) hbitmap);
-      SendMessage(handle, STM_SETIMAGE,
-               (WPARAM) IMAGE_BITMAP,
-               (LPARAM) hbitmap);
-   }
-
-   if(hbitmap && oldbitmap)
-      DeleteObject(oldbitmap);
-   else if(icon && oldicon)
-      DeleteObject(oldicon);
-
-   /* If we changed the bitmap... */
-   {
-      Item *item = _box_item(handle);
-       
-      /* Check to see if any of the sizes need to be recalculated */
-      if(item && (item->origwidth == -1 || item->origheight == -1))
-      {
-         _control_size(handle, item->origwidth == -1 ? &item->width : NULL, item->origheight == -1 ? &item->height : NULL); 
-         /* Queue a redraw on the top-level window */
-         _dw_redraw(_toplevel_window(handle), TRUE);
-      }
-   }
+   }
+   
+   _dw_window_set_bitmap(handle, icon, hbitmap);
 }
 
 /*
@@ -6706,9 +6750,7 @@
 void API dw_window_set_bitmap_from_data(HWND handle, unsigned long id, char *data, int len)
 {
    HBITMAP hbitmap=0;
-   HBITMAP oldbitmap = (HBITMAP)SendMessage(handle, STM_GETIMAGE, IMAGE_BITMAP, 0);
    HICON icon=0;
-   HICON oldicon = (HICON)SendMessage(handle, STM_GETIMAGE, IMAGE_ICON, 0);
    char *file;
    FILE *fp;
 
@@ -6749,29 +6791,7 @@
       icon = LoadImage( DWInstance, MAKEINTRESOURCE(id), IMAGE_ICON, 0, 0, LR_SHARED );
    }
 
-   if ( icon )
-   {
-      SendMessage( handle, BM_SETIMAGE,
-                   (WPARAM) IMAGE_ICON,
-                   (LPARAM) icon );
-      SendMessage( handle, STM_SETIMAGE,
-                   (WPARAM) IMAGE_ICON,
-                   (LPARAM) icon );
-   }
-   else if ( hbitmap )
-   {
-      SendMessage( handle, BM_SETIMAGE,
-                   (WPARAM) IMAGE_BITMAP,
-                   (LPARAM) hbitmap );
-      SendMessage( handle, STM_SETIMAGE,
-                   (WPARAM) IMAGE_BITMAP,
-                   (LPARAM) hbitmap );
-   }
-
-   if( hbitmap && oldbitmap )
-      DeleteObject( oldbitmap );
-   else if ( icon && oldicon )
-      DeleteObject( oldicon );
+   _dw_window_set_bitmap(handle, icon, hbitmap);
 }