Mercurial > dwindows
comparison win/dw.c @ 578:e4c5b03c7ce8
Add support for .ico files to dw_window_set_icon()
Refactor common code into single image handler
author | mhessling@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Wed, 23 Mar 2005 09:26:36 +0000 |
parents | 314abd650968 |
children | 67dfd0cea50d |
comparison
equal
deleted
inserted
replaced
577:ac067e8ea2b4 | 578:e4c5b03c7ce8 |
---|---|
3156 | 3156 |
3157 ShowWindow(array[pageid]->hwnd, SW_SHOWNORMAL); | 3157 ShowWindow(array[pageid]->hwnd, SW_SHOWNORMAL); |
3158 } | 3158 } |
3159 } | 3159 } |
3160 | 3160 |
3161 /* This function determines the handle for a supplied image filename | |
3162 */ | |
3163 int _dw_get_image_handle(char *filename, HANDLE *icon, HBITMAP *hbitmap) | |
3164 { | |
3165 int len, windowtype = 0; | |
3166 char *file = malloc(strlen(filename) + 5); | |
3167 | |
3168 *hbitmap = 0; | |
3169 *icon = 0; | |
3170 | |
3171 if(!file) | |
3172 { | |
3173 return 0; | |
3174 } | |
3175 | |
3176 strcpy(file, filename); | |
3177 | |
3178 /* check if we can read from this file (it exists and read permission) */ | |
3179 if(access(file, 04) == 0) | |
3180 { | |
3181 len = strlen( file ); | |
3182 if ( len < 4 ) | |
3183 { | |
3184 free(file); | |
3185 return 0; | |
3186 } | |
3187 if ( stricmp( file + len - 4, ".ico" ) == 0 ) | |
3188 { | |
3189 *icon = LoadImage(NULL, file, IMAGE_ICON, 0, 0, LR_LOADFROMFILE); | |
3190 windowtype = BS_ICON; | |
3191 } | |
3192 else if ( stricmp( file + len - 4, ".bmp" ) == 0 ) | |
3193 { | |
3194 *hbitmap = (HBITMAP)LoadImage(NULL, file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); | |
3195 windowtype = BS_BITMAP; | |
3196 } | |
3197 free(file); | |
3198 } | |
3199 else | |
3200 { | |
3201 /* Try with .ico extension first...*/ | |
3202 strcat(file, ".ico"); | |
3203 if(access(file, 04) == 0) | |
3204 { | |
3205 *icon = LoadImage(NULL, file, IMAGE_ICON, 0, 0, LR_LOADFROMFILE); | |
3206 windowtype = BS_ICON; | |
3207 } | |
3208 else | |
3209 { | |
3210 strcpy(file, filename); | |
3211 strcat(file, ".bmp"); | |
3212 if(access(file, 04) == 0) | |
3213 { | |
3214 *hbitmap = (HBITMAP)LoadImage(NULL, file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); | |
3215 windowtype = BS_BITMAP; | |
3216 } | |
3217 } | |
3218 free(file); | |
3219 } | |
3220 | |
3221 return windowtype; | |
3222 } | |
3223 | |
3161 /* | 3224 /* |
3162 * Initializes the Dynamic Windows engine. | 3225 * Initializes the Dynamic Windows engine. |
3163 * Parameters: | 3226 * Parameters: |
3164 * newthread: True if this is the only thread. | 3227 * newthread: True if this is the only thread. |
3165 * False if there is already a message loop running. | 3228 * False if there is already a message loop running. |
4560 HWND tmp; | 4623 HWND tmp; |
4561 BubbleButton *bubble; | 4624 BubbleButton *bubble; |
4562 HBITMAP hbitmap = 0; | 4625 HBITMAP hbitmap = 0; |
4563 HANDLE icon = 0; | 4626 HANDLE icon = 0; |
4564 int windowtype = 0, len; | 4627 int windowtype = 0, len; |
4565 char *file = malloc(strlen(filename) + 5); | 4628 |
4566 | 4629 if(!(bubble = calloc(1, sizeof(BubbleButton)))) |
4567 if(!file || !(bubble = calloc(1, sizeof(BubbleButton)))) | |
4568 { | |
4569 if(file) | |
4570 free(file); | |
4571 return 0; | 4630 return 0; |
4572 } | 4631 |
4573 | 4632 windowtype = _dw_get_image_handle(filename, &icon, &hbitmap); |
4574 strcpy(file, filename); | |
4575 | |
4576 /* check if we can read from this file (it exists and read permission) */ | |
4577 if(access(file, 04) == 0) | |
4578 { | |
4579 len = strlen( file ); | |
4580 if ( len < 4 ) | |
4581 { | |
4582 free(file); | |
4583 return 0; | |
4584 } | |
4585 if ( stricmp( file + len - 4, ".ico" ) == 0 ) | |
4586 { | |
4587 icon = LoadImage(NULL, file, IMAGE_ICON, 0, 0, LR_LOADFROMFILE); | |
4588 windowtype = BS_ICON; | |
4589 } | |
4590 else if ( stricmp( file + len - 4, ".bmp" ) == 0 ) | |
4591 { | |
4592 hbitmap = (HBITMAP)LoadImage(NULL, file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); | |
4593 windowtype = BS_BITMAP; | |
4594 } | |
4595 } | |
4596 else | |
4597 { | |
4598 /* Try with .ico extension first...*/ | |
4599 strcat(file, ".ico"); | |
4600 if(access(file, 04) == 0) | |
4601 { | |
4602 icon = LoadImage(NULL, file, IMAGE_ICON, 0, 0, LR_LOADFROMFILE); | |
4603 windowtype = BS_ICON; | |
4604 } | |
4605 else | |
4606 { | |
4607 strcpy(file, filename); | |
4608 strcat(file, ".bmp"); | |
4609 if(access(file, 04) == 0) | |
4610 { | |
4611 hbitmap = (HBITMAP)LoadImage(NULL, file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); | |
4612 windowtype = BS_BITMAP; | |
4613 } | |
4614 } | |
4615 } | |
4616 | 4633 |
4617 tmp = CreateWindow(BUTTONCLASSNAME, | 4634 tmp = CreateWindow(BUTTONCLASSNAME, |
4618 "", | 4635 "", |
4619 WS_CHILD | BS_PUSHBUTTON | | 4636 WS_CHILD | BS_PUSHBUTTON | |
4620 windowtype | WS_CLIPCHILDREN | | 4637 windowtype | WS_CLIPCHILDREN | |
4642 { | 4659 { |
4643 SendMessage(tmp, BM_SETIMAGE, | 4660 SendMessage(tmp, BM_SETIMAGE, |
4644 (WPARAM) IMAGE_BITMAP, | 4661 (WPARAM) IMAGE_BITMAP, |
4645 (LPARAM) hbitmap); | 4662 (LPARAM) hbitmap); |
4646 } | 4663 } |
4647 free(file); | |
4648 return tmp; | 4664 return tmp; |
4649 } | 4665 } |
4650 | 4666 |
4651 /* | 4667 /* |
4652 * Create a new spinbutton window (widget) to be packed. | 4668 * Create a new spinbutton window (widget) to be packed. |
4873 | 4889 |
4874 SendMessage(handle, WM_SETICON, | 4890 SendMessage(handle, WM_SETICON, |
4875 (WPARAM) IMAGE_ICON, | 4891 (WPARAM) IMAGE_ICON, |
4876 (LPARAM) hicon); | 4892 (LPARAM) hicon); |
4877 } | 4893 } |
4878 | |
4879 /* | 4894 /* |
4880 * Sets the bitmap used for a given static window. | 4895 * Sets the bitmap used for a given static window. |
4881 * Parameters: | 4896 * Parameters: |
4882 * handle: Handle to the window. | 4897 * handle: Handle to the window. |
4883 * id: An ID to be used to specify the icon, | 4898 * id: An ID to be used to specify the icon, |
4888 */ | 4903 */ |
4889 void API dw_window_set_bitmap(HWND handle, unsigned long id, char *filename) | 4904 void API dw_window_set_bitmap(HWND handle, unsigned long id, char *filename) |
4890 { | 4905 { |
4891 HBITMAP hbitmap; | 4906 HBITMAP hbitmap; |
4892 HBITMAP oldbitmap = (HBITMAP)SendMessage(handle, STM_GETIMAGE, IMAGE_BITMAP, 0); | 4907 HBITMAP oldbitmap = (HBITMAP)SendMessage(handle, STM_GETIMAGE, IMAGE_BITMAP, 0); |
4908 HICON icon; | |
4909 HICON oldicon = (HICON)SendMessage(handle, STM_GETIMAGE, IMAGE_ICON, 0); | |
4893 | 4910 |
4894 if(id) | 4911 if(id) |
4912 { | |
4895 hbitmap = LoadBitmap(DWInstance, MAKEINTRESOURCE(id)); | 4913 hbitmap = LoadBitmap(DWInstance, MAKEINTRESOURCE(id)); |
4914 icon = LoadImage(DWInstance, MAKEINTRESOURCE(id), IMAGE_ICON, 0, 0, LR_SHARED); | |
4915 } | |
4896 else if(filename) | 4916 else if(filename) |
4897 { | 4917 { |
4898 char *file = malloc(strlen(filename) + 5); | 4918 _dw_get_image_handle(filename, &icon, &hbitmap); |
4899 | 4919 if (icon == 0 && hbitmap == 0) |
4900 if (!file) | |
4901 return; | 4920 return; |
4902 | 4921 } |
4903 strcpy(file, filename); | 4922 |
4904 | 4923 if(icon) |
4905 /* check if we can read from this file (it exists and read permission) */ | 4924 { |
4906 if(access(file, 04) != 0) | 4925 SendMessage(handle, BM_SETIMAGE, |
4907 { | 4926 (WPARAM) IMAGE_ICON, |
4908 /* Try with .bmp extention */ | 4927 (LPARAM) icon); |
4909 strcat(file, ".bmp"); | 4928 } |
4910 if(access(file, 04) != 0) | 4929 else if(hbitmap) |
4911 { | 4930 { |
4912 free(file); | 4931 SendMessage(handle, BM_SETIMAGE, |
4913 return; | 4932 (WPARAM) IMAGE_BITMAP, |
4914 } | 4933 (LPARAM) hbitmap); |
4915 } | 4934 } |
4916 | |
4917 hbitmap = (HBITMAP)LoadImage(NULL, file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); | |
4918 free(file); | |
4919 } | |
4920 else | |
4921 return; | |
4922 | |
4923 SendMessage(handle, STM_SETIMAGE, | |
4924 (WPARAM) IMAGE_BITMAP, | |
4925 (LPARAM) hbitmap); | |
4926 | 4935 |
4927 if(oldbitmap) | 4936 if(oldbitmap) |
4928 DeleteObject(oldbitmap); | 4937 DeleteObject(oldbitmap); |
4929 } | 4938 if(oldicon) |
4939 DeleteObject(oldicon); | |
4940 } | |
4941 | |
4930 | 4942 |
4931 /* | 4943 /* |
4932 * Sets the text used for a given window. | 4944 * Sets the text used for a given window. |
4933 * Parameters: | 4945 * Parameters: |
4934 * handle: Handle to the window. | 4946 * handle: Handle to the window. |
7117 * handle: Handle to the window (widget) to be optimized. | 7129 * handle: Handle to the window (widget) to be optimized. |
7118 */ | 7130 */ |
7119 void API dw_container_optimize(HWND handle) | 7131 void API dw_container_optimize(HWND handle) |
7120 { | 7132 { |
7121 ContainerInfo *cinfo = (ContainerInfo *)GetWindowLongPtr(handle, GWLP_USERDATA); | 7133 ContainerInfo *cinfo = (ContainerInfo *)GetWindowLongPtr(handle, GWLP_USERDATA); |
7122 | |
7123 if(cinfo && cinfo->columns == 1) | 7134 if(cinfo && cinfo->columns == 1) |
7124 { | 7135 { |
7125 ListView_SetColumnWidth(handle, 0, LVSCW_AUTOSIZE); | 7136 ListView_SetColumnWidth(handle, 0, LVSCW_AUTOSIZE); |
7126 } | 7137 } |
7127 else if(cinfo && cinfo->columns > 1) | 7138 else if(cinfo && cinfo->columns > 1) |