OSDN Git Service

Move drag and drop helper code to DragDrop.*
authorsdottaka <none@none>
Sun, 19 May 2013 14:12:03 +0000 (23:12 +0900)
committersdottaka <none@none>
Sun, 19 May 2013 14:12:03 +0000 (23:12 +0900)
Src/Common/DragDrop.cpp [new file with mode: 0644]
Src/Common/DragDrop.h [new file with mode: 0644]
Src/Common/SuperComboBox.cpp
Src/Common/SuperComboBox.h
Src/MainFrm.cpp
Src/Merge.vcproj
Src/Merge.vcxproj
Src/Merge.vcxproj.filters
Src/OpenView.cpp
Src/PathContext.cpp
Src/PathContext.h

diff --git a/Src/Common/DragDrop.cpp b/Src/Common/DragDrop.cpp
new file mode 100644 (file)
index 0000000..2887b10
--- /dev/null
@@ -0,0 +1,56 @@
+#include "DragDrop.h"
+#include "paths.h"
+#include <boost/scoped_array.hpp>
+
+//
+//     OnDropFiles code from CDropEdit
+//     Copyright 1997 Chris Losinger
+//
+//     shortcut expansion code modified from :
+//     CShortcut, 1996 Rob Warner
+//
+
+bool GetDroppedFiles(HDROP dropInfo, std::vector<String>& files)
+{
+// Get the number of pathnames that have been dropped
+       UINT wNumFilesDropped = DragQueryFile(dropInfo, 0xFFFFFFFF, NULL, 0);
+       UINT fileCount = 0;
+
+       // get all file names. but we'll only need the first one.
+       for (WORD x = 0 ; x < wNumFilesDropped; x++)
+       {
+               // Get the number of bytes required by the file's full pathname
+               UINT wPathnameSize = DragQueryFile(dropInfo, x, NULL, 0);
+
+               // Allocate memory to contain full pathname & zero byte
+               wPathnameSize += 1;
+               boost::scoped_array<TCHAR> npszFile(new TCHAR[wPathnameSize]);
+
+               // Copy the pathname into the buffer
+               DragQueryFile(dropInfo, x, npszFile.get(), wPathnameSize);
+
+               if (x < 3)
+               {
+                       files.resize(x + 1);
+                       files[x] = npszFile.get();
+                       fileCount++;
+               }
+       }
+
+       // Free the memory block containing the dropped-file information
+       DragFinish(dropInfo);
+
+       for (UINT i = 0; i < fileCount; i++)
+       {
+               if (paths_IsShortcut(files[i]))
+               {
+                       // if this was a shortcut, we need to expand it to the target path
+                       String expandedFile = ExpandShortcut(files[i]);
+
+                       // if that worked, we should have a real file name
+                       if (!expandedFile.empty())
+                               files[i] = expandedFile;
+               }
+       }
+       return true;
+}
diff --git a/Src/Common/DragDrop.h b/Src/Common/DragDrop.h
new file mode 100644 (file)
index 0000000..d429307
--- /dev/null
@@ -0,0 +1,5 @@
+#include "UnicodeString.h"
+#include <windows.h>
+#include <vector>
+
+bool GetDroppedFiles(HDROP dropInfo, std::vector<String>& files);
index 4f49ecf..db6f5ba 100644 (file)
@@ -3,10 +3,10 @@
 
 #include "StdAfx.h"
 #include "SuperComboBox.h"
+#include "DragDrop.h"
 
 #include <shlwapi.h>
 #include <vector>
-#include <boost/scoped_array.hpp>
 
 #ifdef _DEBUG
 #define new DEBUG_NEW
@@ -456,42 +456,14 @@ int CSuperComboBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
 //
 void CSuperComboBox::OnDropFiles(HDROP dropInfo)
 {
-       // Get the number of pathnames that have been dropped
-       UINT wNumFilesDropped = DragQueryFile(dropInfo, 0xFFFFFFFF, NULL, 0);
-
-       CString firstFile;
-
-       // get all file names. but we'll only need the first one.
-       for (WORD x = 0 ; x < wNumFilesDropped; x++) {
-
-               // Get the number of characters required by the file's full pathname
-               UINT wPathnameSize = DragQueryFile(dropInfo, x, NULL, 0);
-
-               // Allocate memory to contain full pathname & zero byte
-               wPathnameSize += 1;
-               boost::scoped_array<TCHAR> npszFile(new TCHAR[wPathnameSize]);
-
-               // Copy the pathname into the buffer
-               DragQueryFile(dropInfo, x, npszFile.get(), wPathnameSize);
-
-               // we only care about the first
-               if (firstFile==_T(""))
-                       firstFile=npszFile.get();
-       }
-
-       // Free the memory block containing the dropped-file information
-       DragFinish(dropInfo);
-
-       // if this was a shortcut, we need to expand it to the target path
-       CString expandedFile = ExpandShortcut(firstFile);
-
-       // if that worked, we should have a real file name
-       if (!expandedFile.IsEmpty()) 
-               firstFile=expandedFile;
+       std::vector<String> files;
+       GetDroppedFiles(dropInfo, files);
+       if (files.size() == 0)
+               return;
 
        GetParent()->SendMessage(WM_COMMAND, GetDlgCtrlID() +
                (CBN_EDITUPDATE << 16), (LPARAM)m_hWnd);
-       SetWindowText(firstFile);
+       SetWindowText(files[0].c_str());
        GetParent()->SendMessage(WM_COMMAND, GetDlgCtrlID() +
                (CBN_EDITCHANGE << 16), (LPARAM)m_hWnd);
 }
@@ -553,63 +525,3 @@ void CSuperComboBox::OnGetDispInfo(NMHDR *pNotifyStruct, LRESULT *pResult)
        }
        *pResult = 0;
 }
-
-//////////////////////////////////////////////////////////////////
-//     use IShellLink to expand the shortcut
-//     returns the expanded file, or "" on error
-//
-//     original code was part of CShortcut 
-//     1996 by Rob Warner
-//     rhwarner@southeast.net
-//     http://users.southeast.net/~rhwarner
-
-CString CSuperComboBox::ExpandShortcut(CString &inFile)
-{
-       CString outFile;
-
-    // Make sure we have a path
-    ASSERT(!inFile.IsEmpty());
-
-    IShellLink* psl;
-    HRESULT hres;
-    LPTSTR lpsz = inFile.GetBuffer(MAX_PATH);
-
-    // Create instance for shell link
-    hres = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
-        IID_IShellLink, (LPVOID*) &psl);
-    if (SUCCEEDED(hres))
-    {
-        // Get a pointer to the persist file interface
-        IPersistFile* ppf;
-        hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*) &ppf);
-        if (SUCCEEDED(hres))
-        {
-            WCHAR wsz[MAX_PATH];
-#ifdef _UNICODE
-            wcsncpy((wchar_t *)wsz, lpsz, sizeof(wsz)/sizeof(WCHAR));
-#else
-            ::MultiByteToWideChar(CP_ACP, 0, lpsz, -1, wsz, MAX_PATH);
-#endif
-
-            // Load shortcut
-            hres = ppf->Load((LPCOLESTR)wsz, STGM_READ);
-            if (SUCCEEDED(hres)) {
-                               WIN32_FIND_DATA wfd;
-                               // find the path from that
-                               HRESULT hres = psl->GetPath(outFile.GetBuffer(MAX_PATH), 
-                                                               MAX_PATH,
-                                                               &wfd, 
-                                                               SLGP_UNCPRIORITY);
-
-                               outFile.ReleaseBuffer();
-            }
-            ppf->Release();
-        }
-        psl->Release();
-    }
-
-       inFile.ReleaseBuffer();
-
-       // if this fails, outFile == ""
-    return outFile;
-}
index 63cbf55..75cc66e 100644 (file)
@@ -64,7 +64,6 @@ public:
        // Generated message map functions
 protected:
        CString m_strCurSel;
-       CString ExpandShortcut(CString &inFile);
        virtual BOOL OnAddTemplate();
        CString m_strAutoAdd;
        BOOL m_bMustUninitOLE;
index eba1a21..4e6eded 100644 (file)
@@ -30,7 +30,6 @@
 #include <vector>
 #include <htmlhelp.h>  // From HTMLHelp Workshop (incl. in Platform SDK)
 #include <shlwapi.h>
-#include <boost/scoped_array.hpp>
 #include "Constants.h"
 #include "Merge.h"
 #include "UnicodeString.h"
@@ -81,6 +80,7 @@
 #include "OptionsFont.h"
 #include "TFile.h"
 #include "JumpList.h"
+#include "DragDrop.h"
 #include <Poco/Exception.h>
 
 using std::vector;
@@ -2102,57 +2102,12 @@ void CMainFrame::OnToolsGeneratePatch()
        }
 }
 
-/////////////////////////////////////////////////////////////////////////////
-//
-//     OnDropFiles code from CDropEdit
-//     Copyright 1997 Chris Losinger
-//
-//     shortcut expansion code modified from :
-//     CShortcut, 1996 Rob Warner
-//
 void CMainFrame::OnDropFiles(HDROP dropInfo)
 {
-       // Get the number of pathnames that have been dropped
-       UINT wNumFilesDropped = DragQueryFile(dropInfo, 0xFFFFFFFF, NULL, 0);
-       PathContext files;
-       UINT fileCount = 0;
-
-       // get all file names. but we'll only need the first one.
-       for (WORD x = 0 ; x < wNumFilesDropped; x++)
-       {
-               // Get the number of bytes required by the file's full pathname
-               UINT wPathnameSize = DragQueryFile(dropInfo, x, NULL, 0);
-
-               // Allocate memory to contain full pathname & zero byte
-               wPathnameSize += 1;
-               boost::scoped_array<TCHAR> npszFile(new TCHAR[wPathnameSize]);
-
-               // Copy the pathname into the buffer
-               DragQueryFile(dropInfo, x, npszFile.get(), wPathnameSize);
-
-               if (x < 3)
-               {
-                       files.SetSize(x + 1);
-                       files[x] = npszFile.get();
-                       fileCount++;
-               }
-       }
-
-       // Free the memory block containing the dropped-file information
-       DragFinish(dropInfo);
-
-       for (UINT i = 0; i < fileCount; i++)
-       {
-               if (paths_IsShortcut(files[i]))
-               {
-                       // if this was a shortcut, we need to expand it to the target path
-                       CString expandedFile = ExpandShortcut(files[i]).c_str();
-
-                       // if that worked, we should have a real file name
-                       if (!expandedFile.IsEmpty())
-                               files[i] = expandedFile;
-               }
-       }
+       std::vector<String> dropped_files;
+       GetDroppedFiles(dropInfo, dropped_files);
+       PathContext files(dropped_files);
+       const size_t fileCount = files.GetSize();
 
        // If Ctrl pressed, do recursive compare
        bool ctrlKey = !!::GetAsyncKeyState(VK_CONTROL);
@@ -2176,7 +2131,7 @@ void CMainFrame::OnDropFiles(HDROP dropInfo)
 
        // Check if they dropped a project file
        DWORD dwFlags[3] = {FFILEOPEN_NONE, FFILEOPEN_NONE, FFILEOPEN_NONE};
-       if (wNumFilesDropped == 1)
+       if (fileCount == 1)
        {
                if (theApp.IsProjectFile(files[0].c_str()))
                {
index 7d79d60..b6e647e 100644 (file)
                                        </FileConfiguration>\r
                                </File>\r
                                <File\r
+                                       RelativePath=".\Common\DragDrop.cpp">\r
+                                       <FileConfiguration\r
+                                               Name="UnicodeRelease|Win32">\r
+                                               <Tool\r
+                                                       Name="VCCLCompilerTool"\r
+                                                       UsePrecompiledHeader="0"/>\r
+                                       </FileConfiguration>\r
+                                       <FileConfiguration\r
+                                               Name="UnicodeDebug|Win32">\r
+                                               <Tool\r
+                                                       Name="VCCLCompilerTool"\r
+                                                       UsePrecompiledHeader="0"/>\r
+                                       </FileConfiguration>\r
+                                       <FileConfiguration\r
+                                               Name="Debug|Win32">\r
+                                               <Tool\r
+                                                       Name="VCCLCompilerTool"\r
+                                                       UsePrecompiledHeader="0"/>\r
+                                       </FileConfiguration>\r
+                                       <FileConfiguration\r
+                                               Name="Release|Win32">\r
+                                               <Tool\r
+                                                       Name="VCCLCompilerTool"\r
+                                                       UsePrecompiledHeader="0"/>\r
+                                       </FileConfiguration>\r
+                               </File>\r
+                               <File\r
                                        RelativePath="EditorFilepathBar.cpp">\r
                                        <FileConfiguration\r
                                                Name="UnicodeRelease|Win32">\r
                                        RelativePath="DirViewColItems.h">\r
                                </File>\r
                                <File\r
+                                       RelativePath=".\Common\DragDrop.h">\r
+                               </File>\r
+                               <File\r
                                        RelativePath="EditorFilepathBar.h">\r
                                </File>\r
                                <File\r
index 8eda23f..6631537 100644 (file)
       <AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='X64 Release|x64'">%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\r
       <PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='X64 Release|x64'">%(PreprocessorDefinitions)</PreprocessorDefinitions>\r
     </ClCompile>\r
+    <ClCompile Include="Common\DragDrop.cpp">\r
+      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>\r
+      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>\r
+      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='UnicodeDebug|Win32'">NotUsing</PrecompiledHeader>\r
+      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='UnicodeRelease|Win32'">NotUsing</PrecompiledHeader>\r
+      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>\r
+      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>\r
+      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='UnicodeDebug|x64'">NotUsing</PrecompiledHeader>\r
+      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='UnicodeRelease|x64'">NotUsing</PrecompiledHeader>\r
+    </ClCompile>\r
     <ClCompile Include="Common\ExConverter.cpp">\r
       <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>\r
       <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>\r
     <ClInclude Include="codepage.h" />\r
     <ClInclude Include="codepage_detect.h" />\r
     <ClInclude Include="ColorButton.h" />\r
+    <ClInclude Include="Common\DragDrop.h" />\r
     <ClInclude Include="Common\ExConverter.h" />\r
     <ClInclude Include="CompareOptions.h" />\r
     <ClInclude Include="CompareStatisticsDlg.h" />\r
index 608d3c1..180fbf9 100644 (file)
     <ClCompile Include="OptionsDiffColors.cpp">\r
       <Filter>Source Files</Filter>\r
     </ClCompile>\r
+    <ClCompile Include="Common\DragDrop.cpp">\r
+      <Filter>GUI\Source Files</Filter>\r
+    </ClCompile>\r
   </ItemGroup>\r
   <ItemGroup>\r
     <ClInclude Include="charsets.h">\r
     <ClInclude Include="OptionsDiffColors.h">\r
       <Filter>Header Files</Filter>\r
     </ClInclude>\r
+    <ClInclude Include="Common\DragDrop.h">\r
+      <Filter>GUI\Header Files</Filter>\r
+    </ClInclude>\r
   </ItemGroup>\r
   <ItemGroup>\r
     <None Include="res\binarydiff.ico">\r
index 96e39c4..2fdd73d 100644 (file)
@@ -30,7 +30,6 @@
 #include <vector>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <boost/scoped_array.hpp>
 #include "UnicodeString.h"
 #include "Merge.h"
 #include "OpenDoc.h"
@@ -45,6 +44,7 @@
 #include "7zCommon.h"
 #include "Constants.h"
 #include "Picture.h"
+#include "DragDrop.h"
 
 #ifdef _DEBUG
 #define new DEBUG_NEW
@@ -932,74 +932,37 @@ void COpenView::OnHelp()
  */
 void COpenView::OnDropFiles(HDROP dropInfo)
 {
-       // Get the number of pathnames that have been dropped
-       UINT wNumFilesDropped = DragQueryFile(dropInfo, 0xFFFFFFFF, NULL, 0);
-       CString files[3];
-       UINT fileCount = 0;
-
-       // get all file names. but we'll only need the first one.
-       for (WORD x = 0 ; x < wNumFilesDropped; x++)
-       {
-               // Get the number of bytes required by the file's full pathname
-               UINT wPathnameSize = DragQueryFile(dropInfo, x, NULL, 0);
-
-               // Allocate memory to contain full pathname & zero byte
-               wPathnameSize += 1;
-               boost::scoped_array<TCHAR> npszFile(new TCHAR[wPathnameSize]);
-
-               // Copy the pathname into the buffer
-               DragQueryFile(dropInfo, x, npszFile.get(), wPathnameSize);
-
-               if (x < 3)
-               {
-                       files[x] = npszFile.get();
-                       fileCount++;
-               }
-       }
-
-       // Free the memory block containing the dropped-file information
-       DragFinish(dropInfo);
-
-       for (UINT i = 0; i < fileCount; i++)
-       {
-               if (paths_IsShortcut((LPCTSTR)files[i]))
-               {
-                       // if this was a shortcut, we need to expand it to the target path
-                       CString expandedFile = ExpandShortcut((LPCTSTR)files[i]).c_str();
-
-                       // if that worked, we should have a real file name
-                       if (!expandedFile.IsEmpty())
-                               files[i] = expandedFile;
-               }
-       }
+       std::vector<String> files;
+       GetDroppedFiles(dropInfo, files);
+       const size_t fileCount = files.size();
 
        // Add dropped paths to the dialog
        UpdateData(TRUE);
        if (fileCount == 3)
        {
-               m_strPath[0] = files[0];
-               m_strPath[1] = files[1];
-               m_strPath[2] = files[2];
+               m_strPath[0] = files[0].c_str();
+               m_strPath[1] = files[1].c_str();
+               m_strPath[2] = files[2].c_str();
                UpdateData(FALSE);
                UpdateButtonStates();
        }
        else if (fileCount == 2)
        {
-               m_strPath[0] = files[0];
-               m_strPath[1] = files[1];
+               m_strPath[0] = files[0].c_str();
+               m_strPath[1] = files[1].c_str();
                UpdateData(FALSE);
                UpdateButtonStates();
        }
        else if (fileCount == 1)
        {
                if (m_strPath[0].IsEmpty())
-                       m_strPath[0] = files[0];
+                       m_strPath[0] = files[0].c_str();
                else if (m_strPath[1].IsEmpty())
-                       m_strPath[1] = files[0];
+                       m_strPath[1] = files[0].c_str();
                else if (m_strPath[2].IsEmpty())
-                       m_strPath[2] = files[0];
+                       m_strPath[2] = files[0].c_str();
                else
-                       m_strPath[0] = files[0];
+                       m_strPath[0] = files[0].c_str();
                UpdateData(FALSE);
                UpdateButtonStates();
        }
index 6d017c9..8ab4abd 100644 (file)
@@ -97,6 +97,13 @@ PathContext::PathContext(const PathContext &paths)
                m_path[i].SetPath(paths[i]);
 }
 
+PathContext::PathContext(const std::vector<String> &paths)
+{
+       m_nFiles = paths.size();
+       for (int i = 0; i < paths.size(); i++)
+               m_path[i].SetPath(paths[i]);
+}
+
 String PathContext::GetAt(int nIndex) const
 {
        assert(nIndex < m_nFiles);
index 9be348e..4483005 100644 (file)
@@ -11,6 +11,7 @@
 #define _PATH_CONTEXT_H_
 
 #include "UnicodeString.h"
+#include <vector>
 
 class PathContext;
 
@@ -47,6 +48,7 @@ public:
        PathContext(const String& sLeft, const String& sRight);
        PathContext(const String& sLeft, const String& sMiddle, const String& sRight);
        PathContext(const PathContext &paths);
+       PathContext(const std::vector<String>& paths);
 
        String GetAt(int nIndex) const;
        String& GetElement(int nIndex);