示例#1
0
文件: gvimext.cpp 项目: mischief/vim
STDMETHODIMP CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)
{
    HRESULT hr = E_INVALIDARG;

    // If HIWORD(lpcmi->lpVerb) then we have been called programmatically
    // and lpVerb is a command that should be invoked.  Otherwise, the shell
    // has called us, and LOWORD(lpcmi->lpVerb) is the menu ID the user has
    // selected.  Actually, it's (menu ID - idCmdFirst) from QueryContextMenu().
    if (!HIWORD(lpcmi->lpVerb))
    {
	UINT idCmd = LOWORD(lpcmi->lpVerb);

	if (idCmd >= m_edit_existing_off)
	{
	    // Existing with vim instance
	    hr = PushToWindow(lpcmi->hwnd,
		    lpcmi->lpDirectory,
		    lpcmi->lpVerb,
		    lpcmi->lpParameters,
		    lpcmi->nShow,
		    idCmd - m_edit_existing_off);
	}
	else
	{
	    switch (idCmd)
	    {
		case 0:
		    hr = InvokeGvim(lpcmi->hwnd,
			    lpcmi->lpDirectory,
			    lpcmi->lpVerb,
			    lpcmi->lpParameters,
			    lpcmi->nShow);
		    break;
		case 1:
		    hr = InvokeSingleGvim(lpcmi->hwnd,
			    lpcmi->lpDirectory,
			    lpcmi->lpVerb,
			    lpcmi->lpParameters,
			    lpcmi->nShow,
			    0);
		    break;
		case 2:
		    hr = InvokeSingleGvim(lpcmi->hwnd,
			    lpcmi->lpDirectory,
			    lpcmi->lpVerb,
			    lpcmi->lpParameters,
			    lpcmi->nShow,
			    1);
		    break;
	    }
	}
    }
    return hr;
}
STDMETHODIMP CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)
{
    HRESULT hr = E_INVALIDARG;

    // If HIWORD(lpcmi->lpVerb) then we have been called programmatically
    // and lpVerb is a command that should be invoked.  Otherwise, the shell
    // has called us, and LOWORD(lpcmi->lpVerb) is the menu ID the user has
    // selected.  Actually, it's (menu ID - idCmdFirst) from QueryContextMenu().
    if (!HIWORD(lpcmi->lpVerb))
    {
	UINT idCmd = LOWORD(lpcmi->lpVerb);

	if (idCmd >= m_edit_existing_off)
	{
            if(m_bUseTab)
            {
                //Using the following syntax: :new tab c:\temp\dave.txt
                HWND hWnd = m_hWnd[idCmd - m_edit_existing_off];
                //get process id out of window handle
                DWORD dwProcessID;
                GetWindowThreadProcessId(hWnd, &dwProcessID);
                //get a handle to the process
                HANDLE hProcess; 
                hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, dwProcessID); 
                //bring the window to the foreground
                // Show and bring vim instance to foreground
                if (IsIconic(hWnd) != 0)
                    ShowWindow(hWnd, SW_RESTORE);
                else
                    ShowWindow(hWnd, SW_SHOW);
                SetForegroundWindow(hWnd);
                //send files to the window
                //Size of 2*BUFSIZE should be big enough for the Vim Command text string
                char	szVimCmdText[BUFSIZE*2];
                char	m_szFileUserClickedOn[BUFSIZE];
                UINT i;
                //send ESC to the window first to make sure that the subsequent Vim command text will function as intended
                SendToWindow(hProcess, "{INS}");
                SendToWindow(hProcess, "{ESC}");
                for (i = 0; i < cbFiles; i++)
                {
                    //Send each subsequent file using the following syntax: :tab new c:\temp\dave.txt
                    DragQueryFile((HDROP)medium.hGlobal,
                            i,
                            m_szFileUserClickedOn,
                            sizeof(m_szFileUserClickedOn));
                    strcpy(szVimCmdText, ":tab new ");
                    strcat(szVimCmdText, m_szFileUserClickedOn);
                    strcat(szVimCmdText, "{ENTER}");
                    SendToWindow(hProcess, szVimCmdText);
                }
                //make sure that we close this handle
                CloseHandle (hProcess);
            }
            else
            {
                // Edit with Existing vim instance
                hr = PushToWindow(lpcmi->hwnd,
                        lpcmi->lpDirectory,
                        lpcmi->lpVerb,
                        lpcmi->lpParameters,
                        lpcmi->nShow,
                        idCmd - m_edit_existing_off);
            }
	}
	else
	{
	    switch (idCmd)
	    {
		case 0:
		    hr = InvokeGvim(lpcmi->hwnd,
			    lpcmi->lpDirectory,
			    lpcmi->lpVerb,
			    lpcmi->lpParameters,
			    lpcmi->nShow);
		    break;
		case 1:
		    hr = InvokeSingleGvim(lpcmi->hwnd,
			    lpcmi->lpDirectory,
			    lpcmi->lpVerb,
			    lpcmi->lpParameters,
			    lpcmi->nShow,
			    0);
		    break;
		case 2:
		    hr = InvokeSingleGvim(lpcmi->hwnd,
			    lpcmi->lpDirectory,
			    lpcmi->lpVerb,
			    lpcmi->lpParameters,
			    lpcmi->nShow,
			    1);
		    break;
	    }
	}
    }
    return hr;
}