Skip to content

Commit 08a71b5

Browse files
GurliGebisdonho
authored andcommitted
Create library from the main repository
Ref: notepad-plus-plus/notepad-plus-plus#13389 Close #1
1 parent c29ff2c commit 08a71b5

24 files changed

+1278
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
packages
2+
Packaging/NppShell.msix
3+
.vs
4+
*.user
5+
Debug
6+
Release

EditWithNppExplorerCommandHandler.cpp

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "pch.h"
2+
#include "EditWithNppExplorerCommandHandler.h"
3+
4+
#include "Helpers.h"
5+
6+
using namespace NppShell::CommandHandlers;
7+
using namespace NppShell::Helpers;
8+
9+
const wstring EditWithNppExplorerCommandHandler::GetNppExecutableFullPath()
10+
{
11+
const wstring path = GetInstallationPath();
12+
const wstring fileName = L"\\notepad++.exe";
13+
14+
return L"\"" + path + fileName + L"\"";
15+
}
16+
17+
const wstring EditWithNppExplorerCommandHandler::Title()
18+
{
19+
return L"Edit with Notepad++";
20+
}
21+
22+
const wstring EditWithNppExplorerCommandHandler::Icon()
23+
{
24+
const wstring fileName = GetNppExecutableFullPath();
25+
26+
return fileName;
27+
}
28+
29+
const wstring EditWithNppExplorerCommandHandler::GetCommandLine()
30+
{
31+
const wstring fileName = GetNppExecutableFullPath();
32+
const wstring parameters = L"\"%1\"";
33+
34+
return fileName + L" " + parameters;
35+
}
36+
37+
IFACEMETHODIMP EditWithNppExplorerCommandHandler::Invoke(IShellItemArray* psiItemArray, IBindCtx* pbc) noexcept try
38+
{
39+
UNREFERENCED_PARAMETER(pbc);
40+
41+
if (!psiItemArray)
42+
{
43+
return S_OK;
44+
}
45+
46+
DWORD count;
47+
RETURN_IF_FAILED(psiItemArray->GetCount(&count));
48+
49+
IShellItem* psi = nullptr;
50+
LPWSTR itemName;
51+
52+
for (DWORD i = 0; i < count; ++i)
53+
{
54+
psiItemArray->GetItemAt(i, &psi);
55+
RETURN_IF_FAILED(psi->GetDisplayName(SIGDN_FILESYSPATH, &itemName));
56+
57+
std::wstring cmdline = this->GetCommandLine();
58+
cmdline = cmdline.replace(cmdline.find(L"%1"), 2, itemName);
59+
60+
STARTUPINFO si;
61+
PROCESS_INFORMATION pi;
62+
63+
ZeroMemory(&si, sizeof(si));
64+
si.cb = sizeof(si);
65+
ZeroMemory(&pi, sizeof(pi));
66+
67+
wchar_t* command = (LPWSTR)cmdline.c_str();
68+
69+
if (!CreateProcess(nullptr, command, nullptr, nullptr, false, CREATE_NEW_PROCESS_GROUP, nullptr, nullptr, &si, &pi))
70+
{
71+
return S_OK;
72+
}
73+
74+
CloseHandle(pi.hProcess);
75+
CloseHandle(pi.hThread);
76+
}
77+
78+
return S_OK;
79+
}
80+
CATCH_RETURN();

EditWithNppExplorerCommandHandler.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
#include "pch.h"
3+
4+
#include "ExplorerCommandBase.h"
5+
6+
namespace NppShell::CommandHandlers
7+
{
8+
#ifdef WIN64
9+
class __declspec(uuid("B298D29A-A6ED-11DE-BA8C-A68E55D89593")) EditWithNppExplorerCommandHandler : public ExplorerCommandBase
10+
#else
11+
class __declspec(uuid("00F3C2EC-A6EE-11DE-A03A-EF8F55D89593")) EditWithNppExplorerCommandHandler : public ExplorerCommandBase
12+
#endif
13+
{
14+
public:
15+
const wstring Title() override;
16+
const wstring Icon() override;
17+
18+
IFACEMETHODIMP Invoke(IShellItemArray* psiItemArray, IBindCtx* pbc) noexcept override;
19+
20+
private:
21+
const wstring GetNppExecutableFullPath();
22+
const wstring GetCommandLine();
23+
};
24+
}

ExplorerCommandBase.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "pch.h"
2+
#include "ExplorerCommandBase.h"
3+
4+
using namespace NppShell::CommandHandlers;
5+
6+
const EXPCMDFLAGS ExplorerCommandBase::Flags()
7+
{
8+
return ECF_DEFAULT;
9+
}
10+
11+
const EXPCMDSTATE ExplorerCommandBase::State(IShellItemArray* psiItemArray)
12+
{
13+
UNREFERENCED_PARAMETER(psiItemArray);
14+
15+
return ECS_ENABLED;
16+
}
17+
18+
IFACEMETHODIMP ExplorerCommandBase::GetTitle(IShellItemArray* psiItemArray, LPWSTR* ppszName)
19+
{
20+
UNREFERENCED_PARAMETER(psiItemArray);
21+
22+
wstring title = Title();
23+
SHStrDup(title.data(), ppszName);
24+
25+
return S_OK;
26+
}
27+
28+
IFACEMETHODIMP ExplorerCommandBase::GetIcon(IShellItemArray* psiItemArray, LPWSTR* ppszIcon)
29+
{
30+
UNREFERENCED_PARAMETER(psiItemArray);
31+
32+
wstring icon = Icon();
33+
SHStrDup(icon.data(), ppszIcon);
34+
35+
return S_OK;
36+
}
37+
38+
IFACEMETHODIMP ExplorerCommandBase::GetToolTip(IShellItemArray* psiItemArray, LPWSTR* ppszInfotip)
39+
{
40+
UNREFERENCED_PARAMETER(psiItemArray);
41+
UNREFERENCED_PARAMETER(ppszInfotip);
42+
43+
return E_NOTIMPL;
44+
}
45+
46+
IFACEMETHODIMP ExplorerCommandBase::GetState(IShellItemArray* psiItemArray, BOOL fOkToBeSlow, EXPCMDSTATE* pCmdState)
47+
{
48+
UNREFERENCED_PARAMETER(fOkToBeSlow);
49+
50+
*pCmdState = State(psiItemArray);
51+
return S_OK;
52+
}
53+
54+
IFACEMETHODIMP ExplorerCommandBase::GetFlags(EXPCMDFLAGS* flags)
55+
{
56+
*flags = Flags();
57+
return S_OK;
58+
}
59+
60+
IFACEMETHODIMP ExplorerCommandBase::GetCanonicalName(GUID* pguidCommandName) {
61+
*pguidCommandName = GUID_NULL;
62+
return S_OK;
63+
}
64+
65+
IFACEMETHODIMP ExplorerCommandBase::EnumSubCommands(IEnumExplorerCommand** ppEnum)
66+
{
67+
*ppEnum = nullptr;
68+
return E_NOTIMPL;
69+
}

ExplorerCommandBase.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
#include "pch.h"
3+
4+
namespace NppShell::CommandHandlers
5+
{
6+
class ExplorerCommandBase : public winrt::implements<ExplorerCommandBase, IExplorerCommand>
7+
{
8+
public:
9+
virtual const wstring Title() = 0;
10+
virtual const wstring Icon() = 0;
11+
virtual const EXPCMDFLAGS Flags();
12+
virtual const EXPCMDSTATE State(IShellItemArray* psiItemArray);
13+
14+
IFACEMETHODIMP GetTitle(IShellItemArray* psiItemArray, LPWSTR* ppszName);
15+
IFACEMETHODIMP GetIcon(IShellItemArray* psiItemArray, LPWSTR* ppszIcon);
16+
IFACEMETHODIMP GetToolTip(IShellItemArray* psiItemArray, LPWSTR* ppszInfotip);
17+
IFACEMETHODIMP GetState(IShellItemArray* psiItemArray, BOOL fOkToBeSlow, EXPCMDSTATE* pCmdState);
18+
IFACEMETHODIMP GetFlags(EXPCMDFLAGS* flags);
19+
IFACEMETHODIMP GetCanonicalName(GUID* pguidCommandName);
20+
IFACEMETHODIMP EnumSubCommands(IEnumExplorerCommand** ppEnum);
21+
22+
virtual IFACEMETHODIMP Invoke(IShellItemArray* psiItemArray, IBindCtx* pbc) noexcept = 0;
23+
};
24+
}

Helpers.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "pch.h"
2+
#include "Helpers.h"
3+
4+
using namespace NppShell::Helpers;
5+
6+
extern HMODULE thisModule;
7+
8+
const wstring NppShell::Helpers::GetInstallationPath()
9+
{
10+
wchar_t path[FILENAME_MAX] = { 0 };
11+
GetModuleFileName(thisModule, path, FILENAME_MAX);
12+
return std::filesystem::path(path).parent_path().wstring();
13+
}

Helpers.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
#include "pch.h"
3+
4+
#include <filesystem>
5+
6+
namespace NppShell::Helpers
7+
{
8+
const wstring GetInstallationPath();
9+
}

0 commit comments

Comments
 (0)