[SHELL32] Enable to change shortcut icon (#784)
[reactos.git] / dll / win32 / shell32 / CShellLink.cpp
index dfbd057..7efddc9 100644 (file)
@@ -6,6 +6,7 @@
  *      Copyright 2009  Andrew Hill
  *      Copyright 2013  Dominik Hornung
  *      Copyright 2017  Hermes Belusca-Maito
+ *      Copyright 2018  Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -267,6 +268,8 @@ CShellLink::CShellLink()
     m_bRunAs = FALSE;
     m_bDirty = FALSE;
     m_pDBList = NULL;
+    m_bInInit = FALSE;
+    m_hIcon = NULL;
 
     m_sLinkPath = NULL;
     m_iIdOpen = -1;
@@ -346,12 +349,13 @@ HRESULT STDMETHODCALLTYPE CShellLink::Save(LPCOLESTR pszFileName, BOOL fRemember
 
         if (SUCCEEDED(hr))
         {
-            if (m_sLinkPath)
-                HeapFree(GetProcessHeap(), 0, m_sLinkPath);
+            if (pszFileName != m_sLinkPath)
+            {
+                if (m_sLinkPath)
+                    HeapFree(GetProcessHeap(), 0, m_sLinkPath);
 
-            m_sLinkPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (wcslen(pszFileName) + 1) * sizeof(WCHAR));
-            if (m_sLinkPath)
-                wcscpy(m_sLinkPath, pszFileName);
+                m_sLinkPath = strdupW(pszFileName);
+            }
 
             m_bDirty = FALSE;
         }
@@ -1032,11 +1036,9 @@ static HRESULT ShellLink_UpdatePath(LPCWSTR sPathRel, LPCWSTR path, LPCWSTR sWor
         if (!*abs_path)
             wcscpy(abs_path, sPathRel);
 
-        *psPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (wcslen(abs_path) + 1) * sizeof(WCHAR));
+        *psPath = strdupW(abs_path);
         if (!*psPath)
             return E_OUTOFMEMORY;
-
-        wcscpy(*psPath, abs_path);
     }
 
     return S_OK;
@@ -1414,11 +1416,12 @@ HRESULT STDMETHODCALLTYPE CShellLink::Resolve(HWND hwnd, DWORD fFlags)
         bSuccess = SHGetPathFromIDListW(m_pPidl, buffer);
         if (bSuccess && *buffer)
         {
-            m_sPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (wcslen(buffer) + 1) * sizeof(WCHAR));
-            if (!m_sPath)
-                return E_OUTOFMEMORY;
-
-            wcscpy(m_sPath, buffer);
+            if (buffer != m_sPath)
+            {
+                m_sPath = strdupW(buffer);
+                if (!m_sPath)
+                    return E_OUTOFMEMORY;
+            }
 
             m_bDirty = TRUE;
         }
@@ -1431,11 +1434,13 @@ HRESULT STDMETHODCALLTYPE CShellLink::Resolve(HWND hwnd, DWORD fFlags)
     // FIXME: Strange to do that here...
     if (!m_sIcoPath && m_sPath)
     {
-        m_sIcoPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (wcslen(m_sPath) + 1) * sizeof(WCHAR));
-        if (!m_sIcoPath)
-            return E_OUTOFMEMORY;
+        if (m_sIcoPath != m_sPath)
+        {
+            m_sIcoPath = strdupW(m_sPath);
+            if (!m_sIcoPath)
+                return E_OUTOFMEMORY;
+        }
 
-        wcscpy(m_sIcoPath, m_sPath);
         m_Header.nIconIndex = 0;
 
         m_bDirty = TRUE;
@@ -1560,12 +1565,12 @@ HRESULT STDMETHODCALLTYPE CShellLink::SetDescription(LPCWSTR pszName)
     HeapFree(GetProcessHeap(), 0, m_sDescription);
     if (pszName)
     {
-        m_sDescription = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
-                                         (wcslen(pszName) + 1) * sizeof(WCHAR));
-        if (!m_sDescription)
-            return E_OUTOFMEMORY;
-
-        wcscpy(m_sDescription, pszName);
+        if (m_sDescription != pszName)
+        {
+            m_sDescription = strdupW(pszName);
+            if (!m_sDescription)
+                return E_OUTOFMEMORY;
+        }
     }
     else
         m_sDescription = NULL;
@@ -1595,11 +1600,12 @@ HRESULT STDMETHODCALLTYPE CShellLink::SetWorkingDirectory(LPCWSTR pszDir)
     HeapFree(GetProcessHeap(), 0, m_sWorkDir);
     if (pszDir)
     {
-        m_sWorkDir = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
-                                     (wcslen(pszDir) + 1) * sizeof(WCHAR));
-        if (!m_sWorkDir)
-            return E_OUTOFMEMORY;
-        wcscpy(m_sWorkDir, pszDir);
+        if (m_sWorkDir != pszDir)
+        {
+            m_sWorkDir = strdupW(pszDir);
+            if (!m_sWorkDir)
+                return E_OUTOFMEMORY;
+        }
     }
     else
         m_sWorkDir = NULL;
@@ -1629,12 +1635,12 @@ HRESULT STDMETHODCALLTYPE CShellLink::SetArguments(LPCWSTR pszArgs)
     HeapFree(GetProcessHeap(), 0, m_sArgs);
     if (pszArgs)
     {
-        m_sArgs = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
-                                  (wcslen(pszArgs) + 1) * sizeof(WCHAR));
-        if (!m_sArgs)
-            return E_OUTOFMEMORY;
-
-        wcscpy(m_sArgs, pszArgs);
+        if (m_sArgs != pszArgs)
+        {
+            m_sArgs = strdupW(pszArgs);
+            if (!m_sArgs)
+                return E_OUTOFMEMORY;
+        }
     }
     else
         m_sArgs = NULL;
@@ -1669,12 +1675,13 @@ HRESULT STDMETHODCALLTYPE CShellLink::GetIconLocation(LPWSTR pszIconPath, INT cc
 
             SHExpandEnvironmentStringsW(pInfo->szwTarget, szPath, _countof(szPath));
 
-            m_sIcoPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
-                                           (wcslen(szPath) + 1) * sizeof(WCHAR));
-            if (!m_sIcoPath)
-                return E_OUTOFMEMORY;
+            if (m_sIcoPath != szPath)
+            {
+                m_sIcoPath = strdupW(szPath);
+                if (!m_sIcoPath)
+                    return E_OUTOFMEMORY;
+            }
 
-            wcscpy(m_sIcoPath, szPath);
             m_Header.dwFlags |= SLDF_HAS_ICONLOCATION;
 
             m_bDirty = TRUE;
@@ -1732,42 +1739,55 @@ HRESULT STDMETHODCALLTYPE CShellLink::GetIconLocation(UINT uFlags, PWSTR pszIcon
      */
     uFlags |= GIL_FORSHORTCUT;
 
-    if (m_pPidl || m_sPath)
+    hr = GetIconLocation(pszIconFile, cchMax, piIndex);
+    if (FAILED(hr))
     {
-        /* first look for an icon using the PIDL (if present) */
         if (m_pPidl)
             hr = SHELL_PidlGetIconLocationW(m_pPidl, uFlags, pszIconFile, cchMax, piIndex, pwFlags);
-        else
-            hr = E_FAIL;
+    }
 
-#if 0 // FIXME: Analyse further whether this is needed...
-        /* if we couldn't find an icon yet, look for it using the file system path */
-        if (FAILED(hr) && m_sPath)
-        {
-            LPITEMIDLIST pidl;
-            CComPtr<IShellFolder> pdsk;
+    return hr;
+}
 
-            hr = SHGetDesktopFolder(&pdsk);
+HRESULT STDMETHODCALLTYPE
+CShellLink::Extract(PCWSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
+{
+    HRESULT hr = NOERROR;
+    UINT cxyLarge = LOWORD(nIconSize), cxySmall = HIWORD(nIconSize);
 
-            /* LPITEMIDLIST pidl = ILCreateFromPathW(sPath); */
-            hr = pdsk->ParseDisplayName(0, NULL, m_sPath, NULL, &pidl, NULL);
-            if (SUCCEEDED(hr))
-            {
-                hr = SHELL_PidlGetIconLocationW(pidl, uFlags, pszIconFile, cchMax, piIndex, pwFlags);
-                SHFree(pidl);
-            }
-        }
-#endif
-        return hr;
+    if (phiconLarge)
+    {
+        *phiconLarge = NULL;
+        PrivateExtractIconsW(pszFile, nIconIndex, cxyLarge, cxyLarge, phiconLarge, NULL, 1, 0);
+
+        if (*phiconLarge == NULL)
+            hr = S_FALSE;
     }
 
-    return S_OK;
-}
+    if (phiconSmall)
+    {
+        *phiconSmall = NULL;
+        PrivateExtractIconsW(pszFile, nIconIndex, cxySmall, cxySmall, phiconSmall, NULL, 1, 0);
 
-HRESULT STDMETHODCALLTYPE CShellLink::Extract(PCWSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
-{
-    UNIMPLEMENTED;
-    return E_FAIL;
+        if (*phiconSmall == NULL)
+            hr = S_FALSE;
+    }
+
+    if (hr == S_FALSE)
+    {
+        if (phiconLarge && *phiconLarge)
+        {
+            DestroyIcon(*phiconLarge);
+            *phiconLarge = NULL;
+        }
+        if (phiconSmall && *phiconSmall)
+        {
+            DestroyIcon(*phiconSmall);
+            *phiconSmall = NULL;
+        }
+    }
+
+    return hr;
 }
 
 #if 0
@@ -1924,12 +1944,13 @@ HRESULT STDMETHODCALLTYPE CShellLink::SetIconLocation(LPCWSTR pszIconPath, INT i
         HeapFree(GetProcessHeap(), 0, m_sIcoPath);
         m_sIcoPath = NULL;
 
-        m_sIcoPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
-                                     (wcslen(pszIconPath) + 1) * sizeof(WCHAR));
-        if (!m_sIcoPath)
-            return E_OUTOFMEMORY;
+        if (m_sIcoPath != pszIconPath)
+        {
+            m_sIcoPath = strdupW(pszIconPath);
+            if (!m_sIcoPath)
+                return E_OUTOFMEMORY;
+        }
 
-        wcscpy(m_sIcoPath, pszIconPath);
         m_Header.dwFlags |= SLDF_HAS_ICONLOCATION;
     }
 
@@ -1948,11 +1969,12 @@ HRESULT STDMETHODCALLTYPE CShellLink::SetRelativePath(LPCWSTR pszPathRel, DWORD
     HeapFree(GetProcessHeap(), 0, m_sPathRel);
     if (pszPathRel)
     {
-        m_sPathRel = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
-                                      (wcslen(pszPathRel) + 1) * sizeof(WCHAR));
-        if (!m_sPathRel)
-            return E_OUTOFMEMORY;
-        wcscpy(m_sPathRel, pszPathRel);
+        if (m_sPathRel != pszPathRel)
+        {
+            m_sPathRel = strdupW(pszPathRel);
+            if (!m_sPathRel)
+                return E_OUTOFMEMORY;
+        }
     }
     else
         m_sPathRel = NULL;
@@ -2294,11 +2316,13 @@ HRESULT CShellLink::SetTargetFromPIDLOrPath(LPCITEMIDLIST pidl, LPCWSTR pszFile)
 
     /* Update the cached path (for link info) */
     ShellLink_GetVolumeInfo(pszFile, &volume);
-    m_sPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0,
-                              (wcslen(pszFile) + 1) * sizeof(WCHAR));
-    if (!m_sPath)
-        return E_OUTOFMEMORY;
-    wcscpy(m_sPath, pszFile);
+
+    if (m_sPath != pszFile)
+    {
+        m_sPath = strdupW(pszFile);
+        if (!m_sPath)
+            return E_OUTOFMEMORY;
+    }
 
     m_bDirty = TRUE;
     return hr;
@@ -2658,13 +2682,13 @@ INT_PTR CALLBACK ExtendedShortcutProc(HWND hwndDlg, UINT uMsg,
         case WM_INITDIALOG:
             if (lParam)
             {
-                HWND hDlgCtrl = GetDlgItem(hwndDlg, 14000);
+                HWND hDlgCtrl = GetDlgItem(hwndDlg, IDC_SHORTEX_RUN_DIFFERENT);
                 SendMessage(hDlgCtrl, BM_SETCHECK, BST_CHECKED, 0);
             }
             return TRUE;
         case WM_COMMAND:
         {
-            HWND hDlgCtrl = GetDlgItem(hwndDlg, 14000);
+            HWND hDlgCtrl = GetDlgItem(hwndDlg, IDC_SHORTEX_RUN_DIFFERENT);
             if (LOWORD(wParam) == IDOK)
             {
                 if (SendMessage(hDlgCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
@@ -2676,7 +2700,7 @@ INT_PTR CALLBACK ExtendedShortcutProc(HWND hwndDlg, UINT uMsg,
             {
                 EndDialog(hwndDlg, -1);
             }
-            else if (LOWORD(wParam) == 14000)
+            else if (LOWORD(wParam) == IDC_SHORTEX_RUN_DIFFERENT)
             {
                 if (SendMessage(hDlgCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
                     SendMessage(hDlgCtrl, BM_SETCHECK, BST_UNCHECKED, 0);
@@ -2735,195 +2759,246 @@ LPWSTR SH_GetTargetTypeByPath(LPCWSTR lpcwFullPath)
     return wszBuf;
 }
 
-/**************************************************************************
- * SH_ShellLinkDlgProc
- *
- * dialog proc of the shortcut property dialog
- */
-
-INT_PTR CALLBACK CShellLink::SH_ShellLinkDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
+BOOL CShellLink::OnInitDialog(HWND hwndDlg, HWND hwndFocus, LPARAM lParam)
 {
-    CShellLink *pThis = reinterpret_cast<CShellLink *>(GetWindowLongPtr(hwndDlg, DWLP_USER));
+    TRACE("CShellLink::OnInitDialog(hwnd %p hwndFocus %p lParam %p)\n", hwndDlg, hwndFocus, lParam);
 
-    switch(uMsg)
+    TRACE("m_sArgs: %S sComponent: %S m_sDescription: %S m_sIcoPath: %S m_sPath: %S m_sPathRel: %S sProduct: %S m_sWorkDir: %S\n", m_sArgs, sComponent, m_sDescription,
+          m_sIcoPath, m_sPath, m_sPathRel, sProduct, m_sWorkDir);
+
+    m_bInInit = TRUE;
+
+    /* Get file information */
+    // FIXME! FIXME! Shouldn't we use m_sIcoPath, m_Header.nIconIndex instead???
+    SHFILEINFOW fi;
+    if (!SHGetFileInfoW(m_sLinkPath, 0, &fi, sizeof(fi), SHGFI_TYPENAME | SHGFI_ICON))
     {
-        case WM_INITDIALOG:
+        ERR("SHGetFileInfoW failed for %ls (%lu)\n", m_sLinkPath, GetLastError());
+        fi.szTypeName[0] = L'\0';
+        fi.hIcon = NULL;
+    }
+
+    if (fi.hIcon)
+    {
+        if (m_hIcon)
+            DestroyIcon(m_hIcon);
+        m_hIcon = fi.hIcon;
+        SendDlgItemMessageW(hwndDlg, IDC_SHORTCUT_ICON, STM_SETICON, (WPARAM)m_hIcon, 0);
+    }
+    else
+        ERR("ExtractIconW failed %ls %u\n", m_sIcoPath, m_Header.nIconIndex);
+
+    /* Target type */
+    if (m_sPath)
+        SetDlgItemTextW(hwndDlg, IDC_SHORTCUT_TYPE_EDIT, SH_GetTargetTypeByPath(m_sPath));
+
+    /* Target location */
+    if (m_sPath)
+    {
+        WCHAR target[MAX_PATH];
+        StringCchCopyW(target, _countof(target), m_sPath);
+        PathRemoveFileSpecW(target);
+        SetDlgItemTextW(hwndDlg, IDC_SHORTCUT_LOCATION_EDIT, PathFindFileNameW(target));
+    }
+
+    /* Target path */
+    if (m_sPath)
+    {
+        WCHAR newpath[2*MAX_PATH] = L"\0";
+        if (wcschr(m_sPath, ' '))
+            StringCchPrintfExW(newpath, _countof(newpath), NULL, NULL, 0, L"\"%ls\"", m_sPath);
+        else
+            StringCchCopyExW(newpath, _countof(newpath), m_sPath, NULL, NULL, 0);
+
+        if (m_sArgs && m_sArgs[0])
         {
-            LPPROPSHEETPAGEW ppsp = (LPPROPSHEETPAGEW)lParam;
-            if (ppsp == NULL)
-                break;
+            StringCchCatW(newpath, _countof(newpath), L" ");
+            StringCchCatW(newpath, _countof(newpath), m_sArgs);
+        }
+        SetDlgItemTextW(hwndDlg, IDC_SHORTCUT_TARGET_TEXT, newpath);
+    }
 
-            TRACE("ShellLink_DlgProc (WM_INITDIALOG hwnd %p lParam %p ppsplParam %x)\n", hwndDlg, lParam, ppsp->lParam);
+    /* Working dir */
+    if (m_sWorkDir)
+        SetDlgItemTextW(hwndDlg, IDC_SHORTCUT_START_IN_EDIT, m_sWorkDir);
 
-            pThis = reinterpret_cast<CShellLink *>(ppsp->lParam);
-            SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pThis);
+    /* Description */
+    if (m_sDescription)
+        SetDlgItemTextW(hwndDlg, IDC_SHORTCUT_COMMENT_EDIT, m_sDescription);
 
-            TRACE("m_sArgs: %S sComponent: %S m_sDescription: %S m_sIcoPath: %S m_sPath: %S m_sPathRel: %S sProduct: %S m_sWorkDir: %S\n", pThis->m_sArgs, pThis->sComponent, pThis->m_sDescription,
-                  pThis->m_sIcoPath, pThis->m_sPath, pThis->m_sPathRel, pThis->sProduct, pThis->m_sWorkDir);
+    m_bInInit = FALSE;
 
-            /* Get file information */
-            // FIXME! FIXME! Shouldn't we use pThis->m_sIcoPath, pThis->m_Header.nIconIndex instead???
-            SHFILEINFOW fi;
-            if (!SHGetFileInfoW(pThis->m_sLinkPath, 0, &fi, sizeof(fi), SHGFI_TYPENAME | SHGFI_ICON))
-            {
-                ERR("SHGetFileInfoW failed for %ls (%lu)\n", pThis->m_sLinkPath, GetLastError());
-                fi.szTypeName[0] = L'\0';
-                fi.hIcon = NULL;
-            }
+    return TRUE;
+}
 
-            if (fi.hIcon) // TODO: destroy icon
-                SendDlgItemMessageW(hwndDlg, 14000, STM_SETICON, (WPARAM)fi.hIcon, 0);
-            else
-                ERR("ExtractIconW failed %ls %u\n", pThis->m_sIcoPath, pThis->m_Header.nIconIndex);
+void CShellLink::OnCommand(HWND hwndDlg, int id, HWND hwndCtl, UINT codeNotify)
+{
+    switch (id)
+    {
+        case IDC_SHORTCUT_FIND:
+            SHOpenFolderAndSelectItems(m_pPidl, 0, NULL, 0);
+            ///
+            /// FIXME
+            /// open target directory
+            ///
+            return;
 
-            /* Target type */
-            if (pThis->m_sPath)
-                SetDlgItemTextW(hwndDlg, 14005, SH_GetTargetTypeByPath(pThis->m_sPath));
+        case IDC_SHORTCUT_CHANGE_ICON:
+        {
+            WCHAR wszPath[MAX_PATH] = L"";
 
-            /* Target location */
-            if (pThis->m_sPath)
+            if (m_sIcoPath)
+                wcscpy(wszPath, m_sIcoPath);
+            else
+                FindExecutableW(m_sPath, NULL, wszPath);
+
+            INT IconIndex = m_Header.nIconIndex;
+            if (PickIconDlg(hwndDlg, wszPath, _countof(wszPath), &IconIndex))
             {
-                WCHAR target[MAX_PATH];
-                StringCchCopyW(target, _countof(target), pThis->m_sPath);
-                PathRemoveFileSpecW(target);
-                SetDlgItemTextW(hwndDlg, 14007, PathFindFileNameW(target));
+                SetIconLocation(wszPath, IconIndex);
+                PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
+
+                HICON hIconLarge = CreateShortcutIcon(wszPath, IconIndex);
+                if (hIconLarge)
+                {
+                    if (m_hIcon)
+                        DestroyIcon(m_hIcon);
+                    m_hIcon = hIconLarge;
+                    SendDlgItemMessageW(hwndDlg, IDC_SHORTCUT_ICON, STM_SETICON, (WPARAM)m_hIcon, 0);
+                }
             }
+            return;
+        }
 
-            /* Target path */
-            if (pThis->m_sPath)
+        case IDC_SHORTCUT_ADVANCED:
+        {
+            INT_PTR result = DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_SHORTCUT_EXTENDED_PROPERTIES), hwndDlg, ExtendedShortcutProc, (LPARAM)m_bRunAs);
+            if (result == 1 || result == 0)
             {
-                WCHAR newpath[2*MAX_PATH] = L"\0";
-                if (wcschr(pThis->m_sPath, ' '))
-                    StringCchPrintfExW(newpath, _countof(newpath), NULL, NULL, 0, L"\"%ls\"", pThis->m_sPath);
-                else
-                    StringCchCopyExW(newpath, _countof(newpath), pThis->m_sPath, NULL, NULL, 0);
-
-                if (pThis->m_sArgs && pThis->m_sArgs[0])
+                if (m_bRunAs != result)
                 {
-                    StringCchCatW(newpath, _countof(newpath), L" ");
-                    StringCchCatW(newpath, _countof(newpath), pThis->m_sArgs);
+                    PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
                 }
-                SetDlgItemTextW(hwndDlg, 14009, newpath);
+
+                m_bRunAs = result;
             }
+            return;
+        }
+    }
+    if (codeNotify == EN_CHANGE)
+    {
+        if (!m_bInInit)
+            PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
+    }
+}
+
+LRESULT CShellLink::OnNotify(HWND hwndDlg, int idFrom, LPNMHDR pnmhdr)
+{
+    WCHAR wszBuf[MAX_PATH];
+    LPPSHNOTIFY lppsn = (LPPSHNOTIFY)pnmhdr;
+
+    if (lppsn->hdr.code == PSN_APPLY)
+    {
+        /* set working directory */
+        GetDlgItemTextW(hwndDlg, IDC_SHORTCUT_START_IN_EDIT, wszBuf, _countof(wszBuf));
+        SetWorkingDirectory(wszBuf);
 
-            /* Working dir */
-            if (pThis->m_sWorkDir)
-                SetDlgItemTextW(hwndDlg, 14011, pThis->m_sWorkDir);
+        /* set link destination */
+        GetDlgItemTextW(hwndDlg, IDC_SHORTCUT_TARGET_TEXT, wszBuf, _countof(wszBuf));
+        LPWSTR lpszArgs = NULL;
+        LPWSTR unquoted = strdupW(wszBuf);
+        StrTrimW(unquoted, L" ");
 
-            /* Description */
-            if (pThis->m_sDescription)
-                SetDlgItemTextW(hwndDlg, 14019, pThis->m_sDescription);
+        if (!PathFileExistsW(unquoted))
+        {
+            lpszArgs = PathGetArgsW(unquoted);
+            PathRemoveArgsW(unquoted);
+            StrTrimW(lpszArgs, L" ");
+        }
+        if (unquoted[0] == '"' && unquoted[wcslen(unquoted) - 1] == '"')
+            PathUnquoteSpacesW(unquoted);
 
+        WCHAR *pwszExt = PathFindExtensionW(unquoted);
+        if (!wcsicmp(pwszExt, L".lnk"))
+        {
+            // FIXME load localized error msg
+            MessageBoxW(hwndDlg, L"You cannot create a link to a shortcut", L"Error", MB_ICONERROR);
+            SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE);
             return TRUE;
         }
 
-        case WM_NOTIFY:
+        if (!PathFileExistsW(unquoted))
         {
-            LPPSHNOTIFY lppsn = (LPPSHNOTIFY)lParam;
-            if (lppsn->hdr.code == PSN_APPLY)
-            {
-                WCHAR wszBuf[MAX_PATH];
-                /* set working directory */
-                GetDlgItemTextW(hwndDlg, 14011, wszBuf, _countof(wszBuf));
-                pThis->SetWorkingDirectory(wszBuf);
-                /* set link destination */
-                GetDlgItemTextW(hwndDlg, 14009, wszBuf, _countof(wszBuf));
-                LPWSTR lpszArgs = NULL;
-                LPWSTR unquoted = strdupW(wszBuf);
-                StrTrimW(unquoted, L" ");
-                if (!PathFileExistsW(unquoted))
-                {
-                    lpszArgs = PathGetArgsW(unquoted);
-                    PathRemoveArgsW(unquoted);
-                    StrTrimW(lpszArgs, L" ");
-                }
-                if (unquoted[0] == '"' && unquoted[wcslen(unquoted)-1] == '"')
-                    PathUnquoteSpacesW(unquoted);
+            // FIXME load localized error msg
+            MessageBoxW(hwndDlg, L"The specified file name in the target box is invalid", L"Error", MB_ICONERROR);
+            SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE);
+            return TRUE;
+        }
 
+        SetPath(unquoted);
+        if (lpszArgs)
+            SetArguments(lpszArgs);
+        else
+            SetArguments(L"\0");
 
-                WCHAR *pwszExt = PathFindExtensionW(unquoted);
-                if (!wcsicmp(pwszExt, L".lnk"))
-                {
-                    // FIXME load localized error msg
-                    MessageBoxW(hwndDlg, L"You cannot create a link to a shortcut", L"Error", MB_ICONERROR);
-                    SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE);
-                    return TRUE;
-                }
+        HeapFree(GetProcessHeap(), 0, unquoted);
 
-                if (!PathFileExistsW(unquoted))
-                {
-                    // FIXME load localized error msg
-                    MessageBoxW(hwndDlg, L"The specified file name in the target box is invalid", L"Error", MB_ICONERROR);
-                    SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE);
-                    return TRUE;
-                }
+        TRACE("This %p m_sLinkPath %S\n", this, m_sLinkPath);
+        Save(m_sLinkPath, TRUE);
+        SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATHW, m_sLinkPath, NULL);
+        SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
+        return TRUE;
+    }
+    return FALSE;
+}
 
-                pThis->SetPath(unquoted);
-                if (lpszArgs)
-                    pThis->SetArguments(lpszArgs);
-                else
-                    pThis->SetArguments(L"\0");
+void CShellLink::OnDestroy(HWND hwndDlg)
+{
+    if (m_hIcon)
+    {
+        DestroyIcon(m_hIcon);
+        m_hIcon = NULL;
+    }
+}
+
+/**************************************************************************
+ * SH_ShellLinkDlgProc
+ *
+ * dialog proc of the shortcut property dialog
+ */
 
-                HeapFree(GetProcessHeap(), 0, unquoted);
+INT_PTR CALLBACK
+CShellLink::SH_ShellLinkDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+    LPPROPSHEETPAGEW ppsp;
+    CShellLink *pThis = reinterpret_cast<CShellLink *>(GetWindowLongPtr(hwndDlg, DWLP_USER));
 
-                TRACE("This %p m_sLinkPath %S\n", pThis, pThis->m_sLinkPath);
-                pThis->Save(pThis->m_sLinkPath, TRUE);
-                SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
-                return TRUE;
-            }
-            break;
-        }
+    switch (uMsg)
+    {
+        case WM_INITDIALOG:
+            ppsp = (LPPROPSHEETPAGEW)lParam;
+            if (ppsp == NULL)
+                break;
+
+            pThis = reinterpret_cast<CShellLink *>(ppsp->lParam);
+            SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pThis);
+            return pThis->OnInitDialog(hwndDlg, (HWND)(wParam), lParam);
+
+        case WM_NOTIFY:
+            return pThis->OnNotify(hwndDlg, (int)wParam, (NMHDR *)lParam);
 
         case WM_COMMAND:
-            switch(LOWORD(wParam))
-            {
-                case 14020:
-                    SHOpenFolderAndSelectItems(pThis->m_pPidl, 0, NULL, 0);
-                    ///
-                    /// FIXME
-                    /// open target directory
-                    ///
-                    return TRUE;
-
-                case 14021:
-                {
-                    WCHAR wszPath[MAX_PATH] = L"";
-
-                    if (pThis->m_sIcoPath)
-                        wcscpy(wszPath, pThis->m_sIcoPath);
-                    INT IconIndex = pThis->m_Header.nIconIndex;
-                    if (PickIconDlg(hwndDlg, wszPath, _countof(wszPath), &IconIndex))
-                    {
-                        pThis->SetIconLocation(wszPath, IconIndex);
-                        ///
-                        /// FIXME redraw icon
-                        ///
-                    }
-                    return TRUE;
-                }
+            pThis->OnCommand(hwndDlg, LOWORD(wParam), (HWND)lParam, HIWORD(wParam));
+            break;
 
-                case 14022:
-                {
-                    INT_PTR result = DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_SHORTCUT_EXTENDED_PROPERTIES), hwndDlg, ExtendedShortcutProc, (LPARAM)pThis->m_bRunAs);
-                    if (result == 1 || result == 0)
-                    {
-                        if (pThis->m_bRunAs != result)
-                        {
-                            PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
-                        }
-
-                        pThis->m_bRunAs = result;
-                    }
-                    return TRUE;
-                }
-            }
-            if (HIWORD(wParam) == EN_CHANGE)
-                PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
+        case WM_DESTROY:
+            pThis->OnDestroy(hwndDlg);
             break;
 
         default:
             break;
     }
+
     return FALSE;
 }
 
@@ -3054,3 +3129,64 @@ HRESULT WINAPI IShellLink_ConstructFromFile(IShellFolder * psf, LPCITEMIDLIST pi
 
     return IShellLink_ConstructFromPath(path, riid, ppv);
 }
+
+HICON CShellLink::CreateShortcutIcon(LPCWSTR wszIconPath, INT IconIndex)
+{
+    const INT cx = GetSystemMetrics(SM_CXICON), cy = GetSystemMetrics(SM_CYICON);
+    const COLORREF crMask = GetSysColor(COLOR_3DFACE);
+    HDC hDC;
+    HIMAGELIST himl = ImageList_Create(cx, cy, ILC_COLOR32 | ILC_MASK, 1, 1);
+    HICON hIcon = NULL, hNewIcon = NULL;
+    HICON hShortcut = (HICON)LoadImageW(shell32_hInstance, MAKEINTRESOURCE(IDI_SHELL_SHORTCUT),
+                                        IMAGE_ICON, cx, cy, 0);
+
+    ::ExtractIconExW(wszIconPath, IconIndex, &hIcon, NULL, 1);
+    if (!hIcon || !hShortcut || !himl)
+        goto cleanup;
+
+    hDC = CreateCompatibleDC(NULL);
+    if (hDC)
+    {
+        // create 32bpp bitmap
+        BITMAPINFO bi;
+        ZeroMemory(&bi, sizeof(bi));
+        bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
+        bi.bmiHeader.biWidth = cx;
+        bi.bmiHeader.biHeight = cy;
+        bi.bmiHeader.biPlanes = 1;
+        bi.bmiHeader.biBitCount = 32;
+        LPVOID pvBits;
+        HBITMAP hbm = CreateDIBSection(hDC, &bi, DIB_RGB_COLORS, &pvBits, NULL, 0);
+        if (hbm)
+        {
+            // draw the icon image
+            HGDIOBJ hbmOld = SelectObject(hDC, hbm);
+            {
+                HBRUSH hbr = CreateSolidBrush(crMask);
+                RECT rc = { 0, 0, cx, cy };
+                FillRect(hDC, &rc, hbr);
+                DeleteObject(hbr);
+
+                DrawIconEx(hDC, 0, 0, hIcon, cx, cy, 0, NULL, DI_NORMAL);
+                DrawIconEx(hDC, 0, 0, hShortcut, cx, cy, 0, NULL, DI_NORMAL);
+            }
+            SelectObject(hDC, hbmOld);
+
+            INT iAdded = ImageList_AddMasked(himl, hbm, crMask);
+            hNewIcon = ImageList_GetIcon(himl, iAdded, ILD_NORMAL | ILD_TRANSPARENT);
+
+            DeleteObject(hbm);
+        }
+        DeleteDC(hDC);
+    }
+
+cleanup:
+    if (hIcon)
+        DestroyIcon(hIcon);
+    if (hShortcut)
+        DestroyIcon(hShortcut);
+    if (himl)
+        ImageList_Destroy(himl);
+
+    return hNewIcon;
+}