comparison win/dw.c @ 1662:620df4222086

Fixes for dw_file_browse() on Windows in Unicode mode.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Wed, 18 Apr 2012 07:28:19 +0000
parents 28775ce2d26c
children a76fb5f1a6c8
comparison
equal deleted inserted replaced
1661:fc135161f2b1 1662:620df4222086
11316 env->DWSubVersion = DW_SUB_VERSION; 11316 env->DWSubVersion = DW_SUB_VERSION;
11317 #endif 11317 #endif
11318 } 11318 }
11319 11319
11320 /* Helper to make sure all /s are \s */ 11320 /* Helper to make sure all /s are \s */
11321 void _to_dos(char *dst, char *src) 11321 void _to_dos(TCHAR *dst, TCHAR *src)
11322 { 11322 {
11323 int x = 0; 11323 int x = 0;
11324 11324
11325 while(src[x]) 11325 while(src[x])
11326 { 11326 {
11327 if(src[x] == '/') 11327 if(src[x] == TEXT('/'))
11328 dst[x] = '\\'; 11328 dst[x] = TEXT('\\');
11329 else 11329 else
11330 dst[x] = src[x]; 11330 dst[x] = src[x];
11331 x++; 11331 x++;
11332 } 11332 }
11333 dst[x] = 0; 11333 dst[x] = 0;
11345 * the file path on success. 11345 * the file path on success.
11346 * 11346 *
11347 */ 11347 */
11348 char * API dw_file_browse(char *title, char *defpath, char *ext, int flags) 11348 char * API dw_file_browse(char *title, char *defpath, char *ext, int flags)
11349 { 11349 {
11350 OPENFILENAME of; 11350 OPENFILENAME of = {0};
11351 char filenamebuf[1001] = {0}; 11351 TCHAR filenamebuf[1001] = {0};
11352 char filterbuf[1001] = {0}; 11352 TCHAR filterbuf[1001] = {0};
11353 TCHAR *exten = UTF8toWide(ext);
11354 TCHAR *dpath = UTF8toWide(defpath);
11353 int rc; 11355 int rc;
11354 11356
11355 if ( flags == DW_DIRECTORY_OPEN ) 11357 if ( flags == DW_DIRECTORY_OPEN )
11356 { 11358 {
11357 /* If we aren't building a DLL, use the more simple browser */ 11359 /* If we aren't building a DLL, use the more simple browser */
11358 #ifndef BUILD_DLL 11360 #ifndef BUILD_DLL
11359 BROWSEINFO bi; 11361 BROWSEINFO bi = {0};
11360 TCHAR szDir[MAX_PATH]; 11362 TCHAR szDir[MAX_PATH];
11361 LPITEMIDLIST pidl; 11363 LPITEMIDLIST pidl;
11362 LPMALLOC pMalloc; 11364 LPMALLOC pMalloc;
11363 11365
11364 if (SUCCEEDED(SHGetMalloc(&pMalloc))) 11366 if (SUCCEEDED(SHGetMalloc(&pMalloc)))
11365 { 11367 {
11366 ZeroMemory(&bi,sizeof(bi));
11367 bi.hwndOwner = NULL; 11368 bi.hwndOwner = NULL;
11368 bi.pszDisplayName = 0; 11369 bi.pszDisplayName = 0;
11369 bi.pidlRoot = 0; 11370 bi.pidlRoot = 0;
11370 bi.lpszTitle = title; 11371 bi.lpszTitle = title;
11371 bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT; 11372 bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
11377 if (SHGetPathFromIDList(pidl,szDir)) 11378 if (SHGetPathFromIDList(pidl,szDir))
11378 { 11379 {
11379 strncpy(filenamebuf,szDir,1000); 11380 strncpy(filenamebuf,szDir,1000);
11380 } 11381 }
11381 11382
11382 // In C++: pMalloc->Free(pidl); pMalloc->Release(); 11383 /* In C++: pMalloc->Free(pidl); pMalloc->Release(); */
11383 pMalloc->lpVtbl->Free(pMalloc,pidl); 11384 pMalloc->lpVtbl->Free(pMalloc,pidl);
11384 pMalloc->lpVtbl->Release(pMalloc); 11385 pMalloc->lpVtbl->Release(pMalloc);
11385 return _strdup(filenamebuf); 11386 return _strdup(filenamebuf);
11386 } 11387 }
11387 } 11388 }
11392 (LPCTSTR)title, 11393 (LPCTSTR)title,
11393 (LPTSTR)filenamebuf, 11394 (LPTSTR)filenamebuf,
11394 1000, 11395 1000,
11395 FALSE ) ) 11396 FALSE ) )
11396 { 11397 {
11397 return _strdup( filenamebuf ); 11398 return _strdup( WideToUTF8(filenamebuf) );
11398 } 11399 }
11399 #endif 11400 #endif
11400 } 11401 }
11401 else 11402 else
11402 { 11403 {
11407 /* 11408 /*
11408 * The following mess is because sprintf() trunates at first \0 11409 * The following mess is because sprintf() trunates at first \0
11409 * and format of filter is eg: "c files (*.c)\0*.c\0All Files\0*.*\0\0" 11410 * and format of filter is eg: "c files (*.c)\0*.c\0All Files\0*.*\0\0"
11410 */ 11411 */
11411 int len; 11412 int len;
11412 char *ptr = filterbuf; 11413 TCHAR *ptr = filterbuf;
11413 memset( filterbuf, 0, sizeof(filterbuf) ); 11414 len = _stprintf( ptr, TEXT("%s Files (*.%s)"), exten, exten );
11414 len = sprintf( ptr, "%s Files (*.%s)", ext, ext ); 11415 ptr = ptr + len + 1; /* past first \0 */
11415 ptr = ptr + len + 1; // past first \0 11416 len = _stprintf( ptr, TEXT("*.%s"), exten );
11416 len = sprintf( ptr, "*.%s", ext ); 11417 ptr = ptr + len + 1; /* past next \0 */
11417 ptr = ptr + len + 1; // past next \0 11418 len = _stprintf( ptr, TEXT("All Files") );
11418 len = sprintf( ptr, "All Files" ); 11419 ptr = ptr + len + 1; /* past next \0 */
11419 ptr = ptr + len + 1; // past next \0 11420 len = _stprintf( ptr, TEXT("*.*") );
11420 len = sprintf( ptr, "*.*" );
11421 } 11421 }
11422 11422
11423 memset( &of, 0, sizeof(OPENFILENAME) ); 11423 memset( &of, 0, sizeof(OPENFILENAME) );
11424 11424
11425 of.lStructSize = sizeof(OPENFILENAME); 11425 of.lStructSize = sizeof(OPENFILENAME);
11426 of.hwndOwner = HWND_DESKTOP; 11426 of.hwndOwner = HWND_DESKTOP;
11427 of.hInstance = DWInstance; 11427 of.hInstance = DWInstance;
11428 of.lpstrTitle = UTF8toWide(title); 11428 of.lpstrTitle = UTF8toWide(title);
11429 of.lpstrInitialDir = TEXT("."); 11429 of.lpstrInitialDir = TEXT(".");
11430 if(att != INVALID_FILE_ATTRIBUTES && (att & FILE_ATTRIBUTE_DIRECTORY)) 11430 if(att != INVALID_FILE_ATTRIBUTES && (att & FILE_ATTRIBUTE_DIRECTORY))
11431 of.lpstrInitialDir = UTF8toWide(defpath); 11431 of.lpstrInitialDir = dpath;
11432 else if(defpath) 11432 else if(defpath)
11433 _to_dos(filenamebuf, defpath); 11433 _to_dos(filenamebuf, dpath);
11434 of.lpstrFile = UTF8toWide(filenamebuf); 11434 of.lpstrFile = filenamebuf;
11435 of.lpstrFilter = UTF8toWide(filterbuf); 11435 of.lpstrFilter = filterbuf;
11436 of.nFilterIndex = 1; 11436 of.nFilterIndex = 1;
11437 of.nMaxFile = 1000; 11437 of.nMaxFile = 1000;
11438 /*of.lpstrDefExt = ext;*/ 11438 /*of.lpstrDefExt = ext;*/
11439 of.Flags = OFN_NOCHANGEDIR; 11439 of.Flags = OFN_NOCHANGEDIR;
11440 11440