comparison win/dw.c @ 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 9bbe090d0250
children 8d231cab845d
comparison
equal deleted inserted replaced
1819:d784b85b632e 1820:ece7befa9f3d
6619 SendMessage(handle, WM_SETICON, 6619 SendMessage(handle, WM_SETICON,
6620 (WPARAM) IMAGE_ICON, 6620 (WPARAM) IMAGE_ICON,
6621 (LPARAM) hicon); 6621 (LPARAM) hicon);
6622 } 6622 }
6623 6623
6624 /* Internal function to set bitmap for the next two functions */
6625 void _dw_window_set_bitmap(HWND handle, HICON icon, HBITMAP hbitmap)
6626 {
6627 HBITMAP oldbitmap = 0;
6628 HANDLE oldicon = 0;
6629 TCHAR tmpbuf[100] = {0};
6630
6631 if (!icon && !hbitmap)
6632 return;
6633
6634 GetClassName(handle, tmpbuf, 99);
6635
6636 if(_tcsnicmp(tmpbuf, BUTTONCLASSNAME, _tcslen(BUTTONCLASSNAME)+1)==0)
6637 {
6638 oldbitmap = (HBITMAP)SendMessage(handle, BM_GETIMAGE, IMAGE_BITMAP, 0);
6639 oldicon = (HICON)SendMessage(handle, BM_GETIMAGE, IMAGE_ICON, 0);
6640 SendMessage(handle, BM_SETIMAGE,
6641 (icon ? (WPARAM)IMAGE_ICON : (WPARAM)IMAGE_BITMAP),
6642 (icon ? (LPARAM)icon : (LPARAM)hbitmap));
6643 }
6644 #ifdef TOOLBAR
6645 /* Bitmap Buttons */
6646 else if(_tcsnicmp(tmpbuf, TOOLBARCLASSNAME, _tcslen(TOOLBARCLASSNAME)+1) == 0)
6647 {
6648 HIMAGELIST imlist = (HIMAGELIST)SendMessage(handle, TB_GETIMAGELIST, 0, 0);
6649 HIMAGELIST dimlist = (HIMAGELIST)SendMessage(handle, TB_GETDISABLEDIMAGELIST, 0, 0);
6650 BITMAP bmi = { 0 };
6651
6652 if(hbitmap)
6653 {
6654 GetObject(hbitmap, sizeof(BITMAP), &bmi);
6655 imlist = ImageList_Create(bmi.bmWidth, bmi.bmHeight, ILC_COLOR32, 1, 0);
6656 ImageList_Replace(imlist, 0, hbitmap, NULL);
6657 dimlist = ImageList_Create(bmi.bmWidth, bmi.bmHeight, ILC_COLOR32, 1, 0);
6658 _to_grayscale(hbitmap, bmi.bmWidth, bmi.bmHeight);
6659 ImageList_Replace(dimlist, 0, hbitmap, NULL);
6660 DeleteObject(hbitmap);
6661 }
6662 else if(icon)
6663 {
6664 ICONINFO iconinfo;
6665
6666 GetIconInfo(icon, &iconinfo);
6667 GetObject(iconinfo.hbmColor, sizeof(BITMAP), &bmi);
6668 imlist = ImageList_Create(bmi.bmWidth, bmi.bmHeight, ILC_COLOR32 | ILC_MASK, 1, 0);
6669 ImageList_ReplaceIcon(imlist, 0, icon);
6670 dimlist = ImageList_Create(bmi.bmWidth, bmi.bmHeight, ILC_COLOR32 | ILC_MASK, 1, 0);
6671 _to_grayscale(iconinfo.hbmColor, bmi.bmWidth, bmi.bmHeight);
6672 ImageList_Replace(dimlist, 0, iconinfo.hbmColor, iconinfo.hbmMask);
6673 DeleteObject(iconinfo.hbmColor);
6674 DeleteObject(iconinfo.hbmMask);
6675 DestroyIcon(icon);
6676 }
6677 }
6678 #endif
6679 else
6680 {
6681 oldbitmap = (HBITMAP)SendMessage(handle, STM_GETIMAGE, IMAGE_BITMAP, 0);
6682 oldicon = (HICON)SendMessage(handle, STM_GETIMAGE, IMAGE_ICON, 0);
6683 SendMessage(handle, STM_SETIMAGE,
6684 (icon ? (WPARAM)IMAGE_ICON : (WPARAM)IMAGE_BITMAP),
6685 (icon ? (LPARAM)icon : (LPARAM)hbitmap));
6686 }
6687
6688 if(oldbitmap)
6689 DeleteObject(oldbitmap);
6690 if(oldicon)
6691 DeleteObject(oldicon);
6692
6693 /* If we changed the bitmap... */
6694 {
6695 Item *item = _box_item(handle);
6696
6697 /* Check to see if any of the sizes need to be recalculated */
6698 if(item && (item->origwidth == -1 || item->origheight == -1))
6699 {
6700 _control_size(handle, item->origwidth == -1 ? &item->width : NULL, item->origheight == -1 ? &item->height : NULL);
6701 /* Queue a redraw on the top-level window */
6702 _dw_redraw(_toplevel_window(handle), TRUE);
6703 }
6704 }
6705 }
6706
6624 /* 6707 /*
6625 * Sets the bitmap used for a given static window. 6708 * Sets the bitmap used for a given static window.
6626 * Parameters: 6709 * Parameters:
6627 * handle: Handle to the window. 6710 * handle: Handle to the window.
6628 * id: An ID to be used to specify the icon, 6711 * id: An ID to be used to specify the icon,
6632 * NULL if you use the id param) 6715 * NULL if you use the id param)
6633 */ 6716 */
6634 void API dw_window_set_bitmap(HWND handle, unsigned long id, char *filename) 6717 void API dw_window_set_bitmap(HWND handle, unsigned long id, char *filename)
6635 { 6718 {
6636 HBITMAP hbitmap = 0; 6719 HBITMAP hbitmap = 0;
6637 HBITMAP oldbitmap = (HBITMAP)SendMessage(handle, STM_GETIMAGE, IMAGE_BITMAP, 0);
6638 HANDLE icon = 0; 6720 HANDLE icon = 0;
6639 HANDLE oldicon = (HICON)SendMessage(handle, STM_GETIMAGE, IMAGE_ICON, 0);
6640 6721
6641 if(id) 6722 if(id)
6642 { 6723 {
6643 hbitmap = LoadImage(DWInstance, MAKEINTRESOURCE(id), IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS | LR_SHARED); 6724 hbitmap = LoadImage(DWInstance, MAKEINTRESOURCE(id), IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS | LR_SHARED);
6644 icon = LoadImage(DWInstance, MAKEINTRESOURCE(id), IMAGE_ICON, 0, 0, LR_SHARED); 6725 icon = LoadImage(DWInstance, MAKEINTRESOURCE(id), IMAGE_ICON, 0, 0, LR_SHARED);
6648 #ifdef GDIPLUS 6729 #ifdef GDIPLUS
6649 hbitmap = _dw_load_bitmap(filename, NULL); 6730 hbitmap = _dw_load_bitmap(filename, NULL);
6650 #else 6731 #else
6651 _dw_get_image_handle(filename, &icon, &hbitmap); 6732 _dw_get_image_handle(filename, &icon, &hbitmap);
6652 #endif 6733 #endif
6653 if (icon == 0 && hbitmap == 0) 6734 }
6654 return; 6735
6655 } 6736 _dw_window_set_bitmap(handle, icon, hbitmap);
6656
6657 if(icon)
6658 {
6659 SendMessage(handle, BM_SETIMAGE,
6660 (WPARAM) IMAGE_ICON,
6661 (LPARAM) icon);
6662 SendMessage(handle, STM_SETIMAGE,
6663 (WPARAM) IMAGE_ICON,
6664 (LPARAM) icon);
6665 }
6666 else if(hbitmap)
6667 {
6668 SendMessage(handle, BM_SETIMAGE,
6669 (WPARAM) IMAGE_BITMAP,
6670 (LPARAM) hbitmap);
6671 SendMessage(handle, STM_SETIMAGE,
6672 (WPARAM) IMAGE_BITMAP,
6673 (LPARAM) hbitmap);
6674 }
6675
6676 if(hbitmap && oldbitmap)
6677 DeleteObject(oldbitmap);
6678 else if(icon && oldicon)
6679 DeleteObject(oldicon);
6680
6681 /* If we changed the bitmap... */
6682 {
6683 Item *item = _box_item(handle);
6684
6685 /* Check to see if any of the sizes need to be recalculated */
6686 if(item && (item->origwidth == -1 || item->origheight == -1))
6687 {
6688 _control_size(handle, item->origwidth == -1 ? &item->width : NULL, item->origheight == -1 ? &item->height : NULL);
6689 /* Queue a redraw on the top-level window */
6690 _dw_redraw(_toplevel_window(handle), TRUE);
6691 }
6692 }
6693 } 6737 }
6694 6738
6695 /* 6739 /*
6696 * Sets the bitmap used for a given static window from data. 6740 * Sets the bitmap used for a given static window from data.
6697 * Parameters: 6741 * Parameters:
6704 * len: length of data 6748 * len: length of data
6705 */ 6749 */
6706 void API dw_window_set_bitmap_from_data(HWND handle, unsigned long id, char *data, int len) 6750 void API dw_window_set_bitmap_from_data(HWND handle, unsigned long id, char *data, int len)
6707 { 6751 {
6708 HBITMAP hbitmap=0; 6752 HBITMAP hbitmap=0;
6709 HBITMAP oldbitmap = (HBITMAP)SendMessage(handle, STM_GETIMAGE, IMAGE_BITMAP, 0);
6710 HICON icon=0; 6753 HICON icon=0;
6711 HICON oldicon = (HICON)SendMessage(handle, STM_GETIMAGE, IMAGE_ICON, 0);
6712 char *file; 6754 char *file;
6713 FILE *fp; 6755 FILE *fp;
6714 6756
6715 if (data) 6757 if (data)
6716 { 6758 {
6747 { 6789 {
6748 hbitmap = LoadBitmap( DWInstance, MAKEINTRESOURCE(id) ); 6790 hbitmap = LoadBitmap( DWInstance, MAKEINTRESOURCE(id) );
6749 icon = LoadImage( DWInstance, MAKEINTRESOURCE(id), IMAGE_ICON, 0, 0, LR_SHARED ); 6791 icon = LoadImage( DWInstance, MAKEINTRESOURCE(id), IMAGE_ICON, 0, 0, LR_SHARED );
6750 } 6792 }
6751 6793
6752 if ( icon ) 6794 _dw_window_set_bitmap(handle, icon, hbitmap);
6753 {
6754 SendMessage( handle, BM_SETIMAGE,
6755 (WPARAM) IMAGE_ICON,
6756 (LPARAM) icon );
6757 SendMessage( handle, STM_SETIMAGE,
6758 (WPARAM) IMAGE_ICON,
6759 (LPARAM) icon );
6760 }
6761 else if ( hbitmap )
6762 {
6763 SendMessage( handle, BM_SETIMAGE,
6764 (WPARAM) IMAGE_BITMAP,
6765 (LPARAM) hbitmap );
6766 SendMessage( handle, STM_SETIMAGE,
6767 (WPARAM) IMAGE_BITMAP,
6768 (LPARAM) hbitmap );
6769 }
6770
6771 if( hbitmap && oldbitmap )
6772 DeleteObject( oldbitmap );
6773 else if ( icon && oldicon )
6774 DeleteObject( oldicon );
6775 } 6795 }
6776 6796
6777 6797
6778 /* 6798 /*
6779 * Sets the text used for a given window. 6799 * Sets the text used for a given window.