Autosyncing with Wine HEAD
[reactos.git] / reactos / dll / win32 / comdlg32 / filedlg.c
index 67fefd1..bc7b51b 100644 (file)
@@ -196,7 +196,7 @@ static void    FILEDLG95_SHELL_Clean(HWND hwnd);
 static BOOL    FILEDLG95_SHELL_BrowseToDesktop(HWND hwnd);
 
 /* Functions used by the EDIT box */
-static int FILEDLG95_FILENAME_GetFileNames (HWND hwnd, LPWSTR * lpstrFileList, UINT * sizeUsed, char separator);
+static int FILEDLG95_FILENAME_GetFileNames (HWND hwnd, LPWSTR * lpstrFileList, UINT * sizeUsed);
 
 /* Functions used by the filetype combo box */
 static HRESULT FILEDLG95_FILETYPE_Init(HWND hwnd);
@@ -858,11 +858,10 @@ LRESULT SendCustomDlgNotificationMessage(HWND hwndParentDlg, UINT uCode)
     return hook_result;
 }
 
-static INT_PTR FILEDLG95_Handle_GetFilePath(HWND hwnd, DWORD size, LPVOID buffer)
+static INT_PTR FILEDLG95_Handle_GetFilePath(HWND hwnd, DWORD size, LPVOID result)
 {
-    UINT sizeUsed = 0, n, total;
-    LPWSTR lpstrFileList = NULL;
-    WCHAR lpstrCurrentDir[MAX_PATH];
+    UINT len, total;
+    WCHAR *p, *buffer;
     FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
 
     TRACE("CDM_GETFILEPATH:\n");
@@ -871,80 +870,29 @@ static INT_PTR FILEDLG95_Handle_GetFilePath(HWND hwnd, DWORD size, LPVOID buffer
         return -1;
 
     /* get path and filenames */
-    COMDLG32_GetDisplayNameOf(fodInfos->ShellInfos.pidlAbsCurrent, lpstrCurrentDir);
-    n = FILEDLG95_FILENAME_GetFileNames(hwnd, &lpstrFileList, &sizeUsed, ' ');
-
-    TRACE("path >%s< filespec >%s< %d files\n",
-         debugstr_w(lpstrCurrentDir),debugstr_w(lpstrFileList),n);
-
-    if( fodInfos->unicode )
-    {
-        LPWSTR bufW = buffer;
-        total = lstrlenW(lpstrCurrentDir) + 1 + sizeUsed;
-
-        /* Prepend the current path */
-        n = lstrlenW(lpstrCurrentDir) + 1;
-        memcpy( bufW, lpstrCurrentDir, min(n,size) * sizeof(WCHAR));
-        if(n<size)
-        {
-            /* 'n' includes trailing \0 */
-            bufW[n-1] = '\\';
-            memcpy( &bufW[n], lpstrFileList, (size-n)*sizeof(WCHAR) );
-        }
-        TRACE("returned -> %s\n",debugstr_wn(bufW, total));
-    }
-    else
+    len = SendMessageW( fodInfos->DlgInfos.hwndFileName, WM_GETTEXTLENGTH, 0, 0 );
+    buffer = HeapAlloc( GetProcessHeap(), 0, (len + 2 + MAX_PATH) * sizeof(WCHAR) );
+    COMDLG32_GetDisplayNameOf( fodInfos->ShellInfos.pidlAbsCurrent, buffer );
+    if (len)
     {
-        LPSTR bufA = buffer;
-        total = WideCharToMultiByte(CP_ACP, 0, lpstrCurrentDir, -1, 
-                                    NULL, 0, NULL, NULL);
-        total += WideCharToMultiByte(CP_ACP, 0, lpstrFileList, sizeUsed, 
-                                    NULL, 0, NULL, NULL);
-
-        /* Prepend the current path */
-        n = WideCharToMultiByte(CP_ACP, 0, lpstrCurrentDir, -1, 
-                                bufA, size, NULL, NULL);
-
-        if(n<size)
-        {
-            /* 'n' includes trailing \0 */
-            bufA[n-1] = '\\';
-            WideCharToMultiByte(CP_ACP, 0, lpstrFileList, sizeUsed, 
-                                &bufA[n], size-n, NULL, NULL);
-        }
-
-        TRACE("returned -> %s\n",debugstr_an(bufA, total));
+        p = buffer + strlenW(buffer);
+        *p++ = '\\';
+        SendMessageW( fodInfos->DlgInfos.hwndFileName, WM_GETTEXT, len + 1, (LPARAM)p );
     }
-    MemFree(lpstrFileList);
-
-    return total;
-}
-
-static INT_PTR FILEDLG95_Handle_GetFileSpec(HWND hwnd, DWORD size, LPVOID buffer)
-{
-    UINT sizeUsed = 0;
-    LPWSTR lpstrFileList = NULL;
-    FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
-
-    TRACE("CDM_GETSPEC:\n");
-
-    FILEDLG95_FILENAME_GetFileNames(hwnd, &lpstrFileList, &sizeUsed, ' ');
-    if( fodInfos->unicode )
+    if (fodInfos->unicode)
     {
-        LPWSTR bufW = buffer;
-        memcpy( bufW, lpstrFileList, sizeof(WCHAR)*sizeUsed );
+        total = strlenW( buffer) + 1;
+        if (result) lstrcpynW( result, buffer, size );
+        TRACE( "CDM_GETFILEPATH: returning %u %s\n", total, debugstr_w(result));
     }
     else
     {
-        LPSTR bufA = buffer;
-        sizeUsed = WideCharToMultiByte( CP_ACP, 0, lpstrFileList, sizeUsed,
-                                        NULL, 0, NULL, NULL);
-        WideCharToMultiByte(CP_ACP, 0, lpstrFileList, sizeUsed,
-                            bufA, size, NULL, NULL);
+        total = WideCharToMultiByte( CP_ACP, 0, buffer, -1, NULL, 0, NULL, NULL );
+        if (total <= size) WideCharToMultiByte( CP_ACP, 0, buffer, -1, result, size, NULL, NULL );
+        TRACE( "CDM_GETFILEPATH: returning %u %s\n", total, debugstr_a(result));
     }
-    MemFree(lpstrFileList);
-
-    return sizeUsed;
+    HeapFree( GetProcessHeap(), 0, buffer );
+    return total;
 }
 
 /***********************************************************************
@@ -980,8 +928,22 @@ static INT_PTR FILEDLG95_HandleCustomDialogMessages(HWND hwnd, UINT uMsg, WPARAM
             retval = lstrlenW(lpstrPath);
             break;
 
+        case CDM_GETFOLDERIDLIST:
+            retval = COMDLG32_PIDL_ILGetSize(fodInfos->ShellInfos.pidlAbsCurrent);
+            if (retval <= wParam)
+                memcpy((void*)lParam, fodInfos->ShellInfos.pidlAbsCurrent, retval);
+            break;
+
         case CDM_GETSPEC:
-            retval = FILEDLG95_Handle_GetFileSpec(hwnd, (UINT)wParam, (LPSTR)lParam);
+            TRACE("CDM_GETSPEC:\n");
+            retval = SendMessageW(fodInfos->DlgInfos.hwndFileName, WM_GETTEXTLENGTH, 0, 0) + 1;
+            if (lParam)
+            {
+                if (fodInfos->unicode)
+                    SendMessageW(fodInfos->DlgInfos.hwndFileName, WM_GETTEXT, wParam, lParam);
+                else
+                    SendMessageA(fodInfos->DlgInfos.hwndFileName, WM_GETTEXT, wParam, lParam);
+            }
             break;
 
         case CDM_SETCONTROLTEXT:
@@ -1373,9 +1335,9 @@ static LRESULT FILEDLG95_InitControls(HWND hwnd)
       if (handledPath == FALSE && (win2000plus || win98plus)) {
           fodInfos->initdir = MemAlloc(MAX_PATH*sizeof(WCHAR));
 
-          if(FAILED(COMDLG32_SHGetFolderPathW(hwnd, CSIDL_PERSONAL, 0, 0, fodInfos->initdir)))
+          if(!COMDLG32_SHGetFolderPathW(hwnd, CSIDL_PERSONAL, 0, 0, fodInfos->initdir))
           {
-            if(FAILED(COMDLG32_SHGetFolderPathW(hwnd, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, 0, 0, fodInfos->initdir)))
+            if(!COMDLG32_SHGetFolderPathW(hwnd, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, 0, 0, fodInfos->initdir))
             {
                 /* last fallback */
                 GetCurrentDirectoryW(MAX_PATH, fodInfos->initdir);
@@ -1798,7 +1760,7 @@ BOOL FILEDLG95_OnOpen(HWND hwnd)
   TRACE("hwnd=%p\n", hwnd);
 
   /* get the files from the edit control */
-  nFileCount = FILEDLG95_FILENAME_GetFileNames(hwnd, &lpstrFileList, &sizeUsed, '\0');
+  nFileCount = FILEDLG95_FILENAME_GetFileNames(hwnd, &lpstrFileList, &sizeUsed);
 
   /* try if the user selected a folder in the shellview */
   if(nFileCount == 0)
@@ -1834,7 +1796,7 @@ BOOL FILEDLG95_OnOpen(HWND hwnd)
 
   TRACE("current directory=%s\n", debugstr_w(lpstrPathAndFile));
 
-  /* if the user specifyed a fully qualified path use it */
+  /* if the user specified a fully qualified path use it */
   if(PathIsRelativeW(lpstrFileList))
   {
     lstrcatW(lpstrPathAndFile, lpstrFileList);
@@ -2140,7 +2102,6 @@ BOOL FILEDLG95_OnOpen(HWND hwnd)
         if(lstrlenW(lpstrPathAndFile) < fodInfos->ofnInfos->nMaxFile -
             ((fodInfos->ofnInfos->Flags & OFN_ALLOWMULTISELECT) ? 1 : 0))
         {
-          LPWSTR lpszTemp;
 
           /* fill destination buffer */
           if (fodInfos->ofnInfos->lpstrFile)
@@ -2164,13 +2125,31 @@ BOOL FILEDLG95_OnOpen(HWND hwnd)
              }
           }
 
-          /* set filename offset */
-          lpszTemp = PathFindFileNameW(lpstrPathAndFile);
-          fodInfos->ofnInfos->nFileOffset = (lpszTemp - lpstrPathAndFile);
+          if(fodInfos->unicode)
+          {
+              LPWSTR lpszTemp;
+
+              /* set filename offset */
+              lpszTemp = PathFindFileNameW(lpstrPathAndFile);
+              fodInfos->ofnInfos->nFileOffset = (lpszTemp - lpstrPathAndFile);
+
+              /* set extension offset */
+              lpszTemp = PathFindExtensionW(lpstrPathAndFile);
+              fodInfos->ofnInfos->nFileExtension = (*lpszTemp) ? (lpszTemp - lpstrPathAndFile) + 1 : 0;
+          }
+          else
+          {
+               LPSTR lpszTemp;
+               LPOPENFILENAMEA ofn = (LPOPENFILENAMEA)fodInfos->ofnInfos;
+
+              /* set filename offset */
+              lpszTemp = PathFindFileNameA(ofn->lpstrFile);
+              fodInfos->ofnInfos->nFileOffset = (lpszTemp - ofn->lpstrFile);
 
-          /* set extension offset */
-          lpszTemp = PathFindExtensionW(lpstrPathAndFile);
-          fodInfos->ofnInfos->nFileExtension = (*lpszTemp) ? (lpszTemp - lpstrPathAndFile) + 1 : 0;
+              /* set extension offset */
+              lpszTemp = PathFindExtensionA(ofn->lpstrFile);
+              fodInfos->ofnInfos->nFileExtension = (*lpszTemp) ? (lpszTemp - ofn->lpstrFile) + 1 : 0;
+          }
 
           /* set the lpstrFileTitle */
           if(fodInfos->ofnInfos->lpstrFileTitle)
@@ -3188,7 +3167,7 @@ static HRESULT COMDLG32_StrRetToStrNW (LPWSTR dest, DWORD len, LPSTRRET src, LPI
  * The delimiter is specified by the parameter 'separator',
  *  usually either a space or a nul
  */
-static int FILEDLG95_FILENAME_GetFileNames (HWND hwnd, LPWSTR * lpstrFileList, UINT * sizeUsed, char separator)
+static int FILEDLG95_FILENAME_GetFileNames (HWND hwnd, LPWSTR * lpstrFileList, UINT * sizeUsed)
 {
        FileOpenDlgInfos *fodInfos  = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
        UINT nStrCharCount = 0; /* index in src buffer */
@@ -3220,29 +3199,26 @@ static int FILEDLG95_FILENAME_GetFileNames (HWND hwnd, LPWSTR * lpstrFileList, U
            while ((lpstrEdit[nStrCharCount]!='"') && (nStrCharCount <= nStrLen))
            {
              (*lpstrFileList)[nFileIndex++] = lpstrEdit[nStrCharCount];
-             (*sizeUsed)++;
              nStrCharCount++;
            }
-           (*lpstrFileList)[nFileIndex++] = separator;
-           (*sizeUsed)++;
+           (*lpstrFileList)[nFileIndex++] = 0;
            nFileCount++;
          }
          nStrCharCount++;
        }
 
        /* single, unquoted string */
-       if ((nStrLen > 0) && (*sizeUsed == 0) )
+       if ((nStrLen > 0) && (nFileIndex == 0) )
        {
          lstrcpyW(*lpstrFileList, lpstrEdit);
          nFileIndex = lstrlenW(lpstrEdit) + 1;
-         (*sizeUsed) = nFileIndex;
          nFileCount = 1;
        }
 
-       /* trailing \0 */
-       (*lpstrFileList)[nFileIndex] = '\0';
-       (*sizeUsed)++;
+        /* trailing \0 */
+        (*lpstrFileList)[nFileIndex++] = '\0';
 
+        *sizeUsed = nFileIndex;
        MemFree(lpstrEdit);
        return nFileCount;
 }
@@ -3658,12 +3634,20 @@ static void CALLBACK FD32_UpdateResult(const FD31_DATA *lfs)
 
     if (priv->ofnA)
     {
+        LPSTR lpszTemp;
         if (ofnW->nMaxFile &&
             !WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFile, -1,
                                   priv->ofnA->lpstrFile, ofnW->nMaxFile, NULL, NULL ))
             priv->ofnA->lpstrFile[ofnW->nMaxFile-1] = 0;
-        priv->ofnA->nFileOffset = ofnW->nFileOffset;
-        priv->ofnA->nFileExtension = ofnW->nFileExtension;
+
+        /* offsets are not guaranteed to be the same in WCHAR to MULTIBYTE conversion */
+        /* set filename offset */
+        lpszTemp = PathFindFileNameA(priv->ofnA->lpstrFile);
+        priv->ofnA->nFileOffset = (lpszTemp - priv->ofnA->lpstrFile);
+
+        /* set extension offset */
+        lpszTemp = PathFindExtensionA(priv->ofnA->lpstrFile);
+        priv->ofnA->nFileExtension = (*lpszTemp) ? (lpszTemp - priv->ofnA->lpstrFile) + 1 : 0;
     }
 }
 
@@ -3788,7 +3772,7 @@ static INT_PTR CALLBACK FD32_FileOpenDlgProc(HWND hWnd, UINT wMsg,
  *
  * Creates a win31 style dialog box for the user to select a file to open/save.
  */
-static BOOL GetFileName31A(LPOPENFILENAMEA lpofn, /* addess of structure with data*/
+static BOOL GetFileName31A(LPOPENFILENAMEA lpofn, /* address of structure with data*/
                            UINT dlgType /* type dialogue : open/save */
                            )
 {
@@ -3819,7 +3803,7 @@ static BOOL GetFileName31A(LPOPENFILENAMEA lpofn, /* addess of structure with da
  *
  * Creates a win31 style dialog box for the user to select a file to open/save
  */
-static BOOL GetFileName31W(LPOPENFILENAMEW lpofn, /* addess of structure with data*/
+static BOOL GetFileName31W(LPOPENFILENAMEW lpofn, /* address of structure with data*/
                            UINT dlgType /* type dialogue : open/save */
                            )
 {