comparison win/dw.c @ 1090:b905fd8e7fd1

Use _tempnam() on Windows instead of tmpnam() for dw_*_from_data() functions. This overcomes bug in Windows tmpnam() which does not always generate a temporary file name in a directory that can written to. Modified dwtest.c to handle file errors more gracefully.
author mhessling@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 28 Jun 2011 23:38:59 +0000
parents fa219e997fb8
children 03cd2f3e929d
comparison
equal deleted inserted replaced
1089:b58032a619b9 1090:b905fd8e7fd1
44 #define IS_XPPLUS (dwComctlVer >= PACKVERSION(5,82)) 44 #define IS_XPPLUS (dwComctlVer >= PACKVERSION(5,82))
45 45
46 #ifndef MIN 46 #ifndef MIN
47 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 47 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
48 #endif 48 #endif
49
50 /*
51 * For the dw*from_data() functions, a temporary file is required to write
52 * the contents of the image to so it can be loaded by the Win32 API
53 * We use _tempnam() which uses TMP env variable by default. It can be passed
54 * an alternate temporary directory if TMP is not set, so we get the value
55 * of TEMP and store it here.
56 */
57 static char _dw_alternate_temp_dir[MAX_PATH];
49 58
50 FILE *dbgfp = NULL; 59 FILE *dbgfp = NULL;
51 60
52 int main(int argc, char *argv[]); 61 int main(int argc, char *argv[]);
53 62
3538 int API dw_init(int newthread, int argc, char *argv[]) 3547 int API dw_init(int newthread, int argc, char *argv[])
3539 { 3548 {
3540 WNDCLASS wc; 3549 WNDCLASS wc;
3541 int z; 3550 int z;
3542 INITCOMMONCONTROLSEX icc; 3551 INITCOMMONCONTROLSEX icc;
3543 char *fname; 3552 char *fname, *alttmpdir;
3544 HFONT oldfont; 3553 HFONT oldfont;
3545 3554
3546 /* Initialize our thread local storage */ 3555 /* Initialize our thread local storage */
3547 _foreground = TlsAlloc(); 3556 _foreground = TlsAlloc();
3548 _background = TlsAlloc(); 3557 _background = TlsAlloc();
3664 if ( (fname = getenv( "DWINDOWS_DEBUGFILE" ) ) != NULL ) 3673 if ( (fname = getenv( "DWINDOWS_DEBUGFILE" ) ) != NULL )
3665 { 3674 {
3666 dbgfp = fopen( fname, "w" ); 3675 dbgfp = fopen( fname, "w" );
3667 } 3676 }
3668 /* 3677 /*
3678 * Get an alternate temporary directory in case TMP doesn't exist
3679 */
3680 if ( (alttmpdir = getenv( "TEMP" ) ) == NULL )
3681 {
3682 strcpy( _dw_alternate_temp_dir, "c:\\tmp" );
3683 }
3684 else
3685 {
3686 strcpy( _dw_alternate_temp_dir, alttmpdir );
3687 }
3688 /*
3669 * Get screen size. Used to make calls to dw_screen_width() 3689 * Get screen size. Used to make calls to dw_screen_width()
3670 * and dw_screen-height() quicker, but to alos limit the 3690 * and dw_screen-height() quicker, but to alos limit the
3671 * default size of windows. 3691 * default size of windows.
3672 */ 3692 */
3673 screenx = GetSystemMetrics(SM_CXSCREEN); 3693 screenx = GetSystemMetrics(SM_CXSCREEN);
4001 int Italic, Bold; 4021 int Italic, Bold;
4002 char *myFontName; 4022 char *myFontName;
4003 int z, size = 9; 4023 int z, size = 9;
4004 LOGFONT lf; 4024 LOGFONT lf;
4005 HDC hdc = GetDC(handle); 4025 HDC hdc = GetDC(handle);
4006 4026
4007 for(z=0;z<strlen(fontname);z++) 4027 for(z=0;z<strlen(fontname);z++)
4008 { 4028 {
4009 if(fontname[z]=='.') 4029 if(fontname[z]=='.')
4010 break; 4030 break;
4011 } 4031 }
4073 * fontname: Font name in Dynamic Windows format. 4093 * fontname: Font name in Dynamic Windows format.
4074 */ 4094 */
4075 void API dw_font_set_default(char *fontname) 4095 void API dw_font_set_default(char *fontname)
4076 { 4096 {
4077 HFONT oldfont = _DefaultFont; 4097 HFONT oldfont = _DefaultFont;
4078 4098
4079 _DefaultFont = _acquire_font(HWND_DESKTOP, fontname); 4099 _DefaultFont = _acquire_font(HWND_DESKTOP, fontname);
4080 if(oldfont) 4100 if(oldfont)
4081 { 4101 {
4082 DeleteObject(oldfont); 4102 DeleteObject(oldfont);
4083 } 4103 }
4105 if ( thisbox && thisbox->grouphwnd != (HWND)NULL ) 4125 if ( thisbox && thisbox->grouphwnd != (HWND)NULL )
4106 { 4126 {
4107 handle = thisbox->grouphwnd; 4127 handle = thisbox->grouphwnd;
4108 } 4128 }
4109 } 4129 }
4110 4130
4111 /* This needs to be after we get the correct handle */ 4131 /* This needs to be after we get the correct handle */
4112 oldfont = (HFONT)SendMessage(handle, WM_GETFONT, 0, 0); 4132 oldfont = (HFONT)SendMessage(handle, WM_GETFONT, 0, 0);
4113 hfont = _acquire_font(handle, fontname); 4133 hfont = _acquire_font(handle, fontname);
4114 4134
4115 if (fontname) 4135 if (fontname)
4116 { 4136 {
4117 if(cinfo) 4137 if(cinfo)
4118 { 4138 {
4119 strcpy(cinfo->fontname, fontname); 4139 strcpy(cinfo->fontname, fontname);
4149 CHOOSEFONT cf = { 0 }; 4169 CHOOSEFONT cf = { 0 };
4150 LOGFONT lf = { 0 }; 4170 LOGFONT lf = { 0 };
4151 char *str = NULL; 4171 char *str = NULL;
4152 char *bold = ""; 4172 char *bold = "";
4153 char *italic = ""; 4173 char *italic = "";
4154 4174
4155 if(currfont && *currfont) 4175 if(currfont && *currfont)
4156 lf = _get_logfont(NULL, currfont); 4176 lf = _get_logfont(NULL, currfont);
4157 4177
4158 cf.lStructSize = sizeof(cf); 4178 cf.lStructSize = sizeof(cf);
4159 cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT; 4179 cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
4160 cf.lpLogFont = &lf; 4180 cf.lpLogFont = &lf;
4161 4181
4162 if(ChooseFont(&cf)) 4182 if(ChooseFont(&cf))
4163 { 4183 {
4164 str = (char *)malloc( 100 ); 4184 str = (char *)malloc( 100 );
4165 if ( str ) 4185 if ( str )
4166 { 4186 {
4167 int height; 4187 int height;
4168 HDC hdc = GetDC(NULL); 4188 HDC hdc = GetDC(NULL);
4169 4189
4170 if ( lf.lfWeight > FW_MEDIUM ) 4190 if ( lf.lfWeight > FW_MEDIUM )
4171 bold = " Bold"; 4191 bold = " Bold";
4172 if ( lf.lfItalic ) 4192 if ( lf.lfItalic )
4173 italic = " Italic"; 4193 italic = " Italic";
4174 height = MulDiv(abs(lf.lfHeight), 72, GetDeviceCaps (hdc, LOGPIXELSY)); 4194 height = MulDiv(abs(lf.lfHeight), 72, GetDeviceCaps (hdc, LOGPIXELSY));
4211 str = (char *)malloc( 100 ); 4231 str = (char *)malloc( 100 );
4212 if ( str ) 4232 if ( str )
4213 { 4233 {
4214 int height; 4234 int height;
4215 HDC hdc = GetDC(handle); 4235 HDC hdc = GetDC(handle);
4216 4236
4217 if ( lf.lfWeight > FW_MEDIUM ) 4237 if ( lf.lfWeight > FW_MEDIUM )
4218 bold = " Bold"; 4238 bold = " Bold";
4219 if ( lf.lfItalic ) 4239 if ( lf.lfItalic )
4220 italic = " Italic"; 4240 italic = " Italic";
4221 height = MulDiv(abs(lf.lfHeight), 72, GetDeviceCaps (hdc, LOGPIXELSY)); 4241 height = MulDiv(abs(lf.lfHeight), 72, GetDeviceCaps (hdc, LOGPIXELSY));
5404 FILE *fp; 5424 FILE *fp;
5405 int windowtype; 5425 int windowtype;
5406 5426
5407 if ( !(bubble = calloc(1, sizeof(BubbleButton))) ) 5427 if ( !(bubble = calloc(1, sizeof(BubbleButton))) )
5408 return 0; 5428 return 0;
5409 file = tmpnam( NULL ); 5429 file = _tempnam( _dw_alternate_temp_dir, "dw" );
5410 if ( file != NULL ) 5430 if ( file != NULL )
5411 { 5431 {
5412 fp = fopen( file, "wb" ); 5432 fp = fopen( file, "wb" );
5413 if ( fp != NULL ) 5433 if ( fp != NULL )
5414 { 5434 {
5426 } 5446 }
5427 } 5447 }
5428 else 5448 else
5429 { 5449 {
5430 unlink( file ); 5450 unlink( file );
5451 free( file );
5431 return 0; 5452 return 0;
5432 } 5453 }
5433 unlink( file ); 5454 unlink( file );
5455 free( file );
5434 } 5456 }
5435 5457
5436 tmp = CreateWindow( BUTTONCLASSNAME, 5458 tmp = CreateWindow( BUTTONCLASSNAME,
5437 "", 5459 "",
5438 WS_CHILD | BS_PUSHBUTTON | 5460 WS_CHILD | BS_PUSHBUTTON |
5767 hbitmap = LoadBitmap( DWInstance, MAKEINTRESOURCE(id) ); 5789 hbitmap = LoadBitmap( DWInstance, MAKEINTRESOURCE(id) );
5768 icon = LoadImage( DWInstance, MAKEINTRESOURCE(id), IMAGE_ICON, 0, 0, LR_SHARED ); 5790 icon = LoadImage( DWInstance, MAKEINTRESOURCE(id), IMAGE_ICON, 0, 0, LR_SHARED );
5769 } 5791 }
5770 else if (data) 5792 else if (data)
5771 { 5793 {
5772 file = tmpnam( NULL ); 5794 file = _tempnam( _dw_alternate_temp_dir, "dw" );
5773 if ( file != NULL ) 5795 if ( file != NULL )
5774 { 5796 {
5775 fp = fopen( file, "wb" ); 5797 fp = fopen( file, "wb" );
5776 if ( fp != NULL ) 5798 if ( fp != NULL )
5777 { 5799 {
5783 icon = LoadImage( NULL, file, IMAGE_ICON, 0, 0, LR_LOADFROMFILE ); 5805 icon = LoadImage( NULL, file, IMAGE_ICON, 0, 0, LR_LOADFROMFILE );
5784 } 5806 }
5785 else 5807 else
5786 { 5808 {
5787 unlink( file ); 5809 unlink( file );
5810 free( file );
5788 return; 5811 return;
5789 } 5812 }
5790 unlink( file ); 5813 unlink( file );
5814 free( file );
5791 } 5815 }
5792 if (icon == 0 && hbitmap == 0) 5816 if (icon == 0 && hbitmap == 0)
5793 return; 5817 return;
5794 } 5818 }
5795 5819
5961 thisbox = (Box *)GetWindowLongPtr(box, GWLP_USERDATA); 5985 thisbox = (Box *)GetWindowLongPtr(box, GWLP_USERDATA);
5962 if(thisbox) 5986 if(thisbox)
5963 { 5987 {
5964 int z, x = 0; 5988 int z, x = 0;
5965 Item *tmpitem, *thisitem = thisbox->items; 5989 Item *tmpitem, *thisitem = thisbox->items;
5966 5990
5967 /* Do some sanity bounds checking */ 5991 /* Do some sanity bounds checking */
5968 if(index < 0) 5992 if(index < 0)
5969 index = 0; 5993 index = 0;
5970 if(index > thisbox->count) 5994 if(index > thisbox->count)
5971 index = thisbox->count; 5995 index = thisbox->count;
6046 /* 6070 /*
6047 * Pack windows (widgets) into a box at an arbitrary location. 6071 * Pack windows (widgets) into a box at an arbitrary location.
6048 * Parameters: 6072 * Parameters:
6049 * box: Window handle of the box to be packed into. 6073 * box: Window handle of the box to be packed into.
6050 * item: Window handle of the item to be back. 6074 * item: Window handle of the item to be back.
6051 * index: 0 based index of packed items. 6075 * index: 0 based index of packed items.
6052 * width: Width in pixels of the item or -1 to be self determined. 6076 * width: Width in pixels of the item or -1 to be self determined.
6053 * height: Height in pixels of the item or -1 to be self determined. 6077 * height: Height in pixels of the item or -1 to be self determined.
6054 * hsize: TRUE if the window (widget) should expand horizontally to fill space given. 6078 * hsize: TRUE if the window (widget) should expand horizontally to fill space given.
6055 * vsize: TRUE if the window (widget) should expand vertically to fill space given. 6079 * vsize: TRUE if the window (widget) should expand vertically to fill space given.
6056 * pad: Number of pixels of padding around the item. 6080 * pad: Number of pixels of padding around the item.
6071 * vsize: TRUE if the window (widget) should expand vertically to fill space given. 6095 * vsize: TRUE if the window (widget) should expand vertically to fill space given.
6072 * pad: Number of pixels of padding around the item. 6096 * pad: Number of pixels of padding around the item.
6073 */ 6097 */
6074 void API dw_box_pack_start(HWND box, HWND item, int width, int height, int hsize, int vsize, int pad) 6098 void API dw_box_pack_start(HWND box, HWND item, int width, int height, int hsize, int vsize, int pad)
6075 { 6099 {
6076 /* 65536 is the table limit on GTK... 6100 /* 65536 is the table limit on GTK...
6077 * seems like a high enough value we will never hit it here either. 6101 * seems like a high enough value we will never hit it here either.
6078 */ 6102 */
6079 _dw_box_pack(box, item, 65536, width, height, hsize, vsize, pad, "dw_box_pack_start()"); 6103 _dw_box_pack(box, item, 65536, width, height, hsize, vsize, pad, "dw_box_pack_start()");
6080 } 6104 }
6081 6105
7636 char *file; 7660 char *file;
7637 FILE *fp; 7661 FILE *fp;
7638 7662
7639 if ( !data ) 7663 if ( !data )
7640 return 0; 7664 return 0;
7641 file = tmpnam( NULL ); 7665 file = _tempnam( _dw_alternate_temp_dir, "dw" );
7642 if ( file != NULL ) 7666 if ( file != NULL )
7643 { 7667 {
7644 fp = fopen( file, "wb" ); 7668 fp = fopen( file, "wb" );
7645 if ( fp != NULL ) 7669 if ( fp != NULL )
7646 { 7670 {
7649 icon = LoadImage( NULL, file, IMAGE_ICON, 0, 0, LR_LOADFROMFILE ); 7673 icon = LoadImage( NULL, file, IMAGE_ICON, 0, 0, LR_LOADFROMFILE );
7650 } 7674 }
7651 else 7675 else
7652 { 7676 {
7653 unlink( file ); 7677 unlink( file );
7678 free( file );
7654 return 0; 7679 return 0;
7655 } 7680 }
7656 unlink( file ); 7681 unlink( file );
7682 free( file );
7657 } 7683 }
7658 return (HICN)icon; 7684 return (HICN)icon;
7659 } 7685 }
7660 7686
7661 /* 7687 /*
8885 8911
8886 hdc = GetDC(handle); 8912 hdc = GetDC(handle);
8887 8913
8888 pixmap->handle = handle; 8914 pixmap->handle = handle;
8889 8915
8890 file = tmpnam( NULL ); 8916 file = _tempnam( _dw_alternate_temp_dir, "dw" );
8891 if ( file != NULL ) 8917 if ( file != NULL )
8892 { 8918 {
8893 fp = fopen( file, "wb" ); 8919 fp = fopen( file, "wb" );
8894 if ( fp != NULL ) 8920 if ( fp != NULL )
8895 { 8921 {
8899 pixmap->depth = _read_bitmap_header(file); 8925 pixmap->depth = _read_bitmap_header(file);
8900 } 8926 }
8901 else 8927 else
8902 { 8928 {
8903 unlink( file ); 8929 unlink( file );
8930 free( file );
8904 free( pixmap ); 8931 free( pixmap );
8905 return NULL; 8932 return NULL;
8906 } 8933 }
8907 unlink( file ); 8934 unlink( file );
8935 free( file );
8908 } 8936 }
8909 8937
8910 if ( !pixmap->hbm ) 8938 if ( !pixmap->hbm )
8911 { 8939 {
8912 free( pixmap ); 8940 free( pixmap );