Skip to content

Commit 994d44c

Browse files
committed
Created wrapped Scintilla class, accessible from Python
- Scintilla class created to handle all scintilla messages - instance called "buffer" added to __main__ module - Ready for Autogeneration in ScintillaPython.cpp and ScintillaWrapper.cpp
1 parent 4dada5d commit 994d44c

10 files changed

Lines changed: 1145 additions & 62 deletions

PythonScript/project/PythonScript2010.vcxproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@
8989
<ClCompile Include="..\src\AboutDialog.cpp" />
9090
<ClCompile Include="..\src\PythonHandler.cpp" />
9191
<ClCompile Include="..\src\PythonScript.cpp" />
92+
<ClCompile Include="..\src\ScintillaPython.cpp" />
93+
<ClCompile Include="..\src\ScintillaWrapper.cpp" />
9294
<ClCompile Include="..\src\stdafx.cpp" />
9395
<ClCompile Include="..\src\WcharMbcsConverter.cpp" />
9496
</ItemGroup>
@@ -97,6 +99,9 @@
9799
<ClInclude Include="..\src\PythonHandler.h" />
98100
<ClInclude Include="..\src\PythonScript.h" />
99101
<ClInclude Include="..\src\resource.h" />
102+
<ClInclude Include="..\src\Scintilla.h" />
103+
<ClInclude Include="..\src\ScintillaPython.h" />
104+
<ClInclude Include="..\src\ScintillaWrapper.h" />
100105
<ClInclude Include="..\src\stdafx.h" />
101106
<ClInclude Include="..\src\WcharMbcsConverter.h" />
102107
</ItemGroup>

PythonScript/project/PythonScript2010.vcxproj.filters

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
<ClCompile Include="..\src\WcharMbcsConverter.cpp">
3434
<Filter>Source Files</Filter>
3535
</ClCompile>
36+
<ClCompile Include="..\src\ScintillaWrapper.cpp">
37+
<Filter>Source Files</Filter>
38+
</ClCompile>
39+
<ClCompile Include="..\src\ScintillaPython.cpp">
40+
<Filter>Source Files</Filter>
41+
</ClCompile>
3642
</ItemGroup>
3743
<ItemGroup>
3844
<ClInclude Include="..\src\AboutDialog.h">
@@ -53,6 +59,15 @@
5359
<ClInclude Include="..\src\WcharMbcsConverter.h">
5460
<Filter>Header Files</Filter>
5561
</ClInclude>
62+
<ClInclude Include="..\src\ScintillaWrapper.h">
63+
<Filter>Header Files</Filter>
64+
</ClInclude>
65+
<ClInclude Include="..\src\Scintilla.h">
66+
<Filter>Header Files</Filter>
67+
</ClInclude>
68+
<ClInclude Include="..\src\ScintillaPython.h">
69+
<Filter>Header Files</Filter>
70+
</ClInclude>
5671
</ItemGroup>
5772
<ItemGroup>
5873
<ResourceCompile Include="..\src\PythonScript.rc">

PythonScript/src/PythonHandler.cpp

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33

44
#include "Python.h"
55

6+
#include "ScintillaWrapper.h"
7+
#include "ScintillaPython.h"
8+
69
using namespace std;
710

8-
PythonHandler::PythonHandler(char *pluginsDir, char *configDir)
11+
PythonHandler::PythonHandler(char *pluginsDir, char *configDir, HWND nppHandle, HWND scintilla1Handle, HWND scintilla2Handle)
912
: m_machineBaseDir(pluginsDir),
10-
m_userBaseDir(configDir)
13+
m_userBaseDir(configDir),
14+
m_nppHandle(nppHandle),
15+
m_scintilla1Handle(scintilla1Handle),
16+
m_scintilla2Handle(scintilla2Handle)
1117
{
1218
m_machineBaseDir.append("\\PythonScript\\");
1319
m_userBaseDir.append("\\PythonScript\\");
20+
21+
mp_scintilla = createScintillaWrapper();
1422
}
1523

1624

@@ -23,14 +31,23 @@ PythonHandler::~PythonHandler(void)
2331
}
2432
}
2533

34+
ScintillaWrapper* PythonHandler::createScintillaWrapper()
35+
{
36+
// Default to 1st scintilla handle initially
37+
return new ScintillaWrapper(m_scintilla1Handle);
38+
}
39+
2640

2741
void PythonHandler::initPython()
2842
{
2943
if (Py_IsInitialized())
3044
return;
3145

46+
47+
preinitScintillaModule();
48+
3249
Py_Initialize();
33-
50+
3451

3552
// Init paths
3653
char initBuffer[1024];
@@ -55,12 +72,7 @@ void PythonHandler::initPython()
5572

5673
void PythonHandler::initModules()
5774
{
58-
//Py_InitModule("notepad", notepadMethods);
59-
//Py_InitModule("buffer", bufferMethods);
60-
61-
//PyRun_SimpleString("import notepad\n"
62-
// "import buffer\n");
63-
75+
importScintilla(mp_scintilla);
6476
}
6577

6678

@@ -69,7 +81,7 @@ void PythonHandler::runStartupScripts()
6981
// Machine scripts (N++\Plugins\PythonScript\scripts dir)
7082
string startupPath(m_machineBaseDir);
7183
startupPath.append("scripts\\startup.py");
72-
if (fileExists(startupPath.c_str()))
84+
if (::PathFileExistsA(startupPath.c_str()))
7385
{
7486

7587
runScript(startupPath);
@@ -78,32 +90,14 @@ void PythonHandler::runStartupScripts()
7890
// User scripts ($CONFIGDIR$\PythonScript\scripts dir)
7991
startupPath = m_userBaseDir;
8092
startupPath.append("scripts\\startup.py");
81-
if (fileExists(startupPath.c_str()))
93+
if (::PathFileExistsA(startupPath.c_str()))
8294
{
8395
runScript(startupPath);
8496
}
8597

8698
}
8799

88100

89-
bool PythonHandler::fileExists(const char *fileName)
90-
{
91-
DWORD fileAttr;
92-
93-
fileAttr = GetFileAttributesA(fileName);
94-
if (0xFFFFFFFF == fileAttr)
95-
return false;
96-
return true;
97-
}
98-
99-
void PythonHandler::reinitPython()
100-
{
101-
if (Py_IsInitialized())
102-
Py_Finalize();
103-
104-
initPython();
105-
}
106-
107101

108102
bool PythonHandler::runScript(const string& scriptFile)
109103
{
@@ -113,12 +107,10 @@ bool PythonHandler::runScript(const string& scriptFile)
113107
bool PythonHandler::runScript(const char *filename)
114108
{
115109
bool retVal = false;
116-
int filenameLength = strlen(filename) + 1;
117-
char *copyFilename = (char*) malloc(filenameLength);
118-
strcpy_s(copyFilename, filenameLength, filename);
119110

120111
// Why doesn't PyFile_FromString take a const?
121-
PyObject* pyFile = PyFile_FromString(copyFilename, "r");
112+
// It doesn't modify the string, so const_cast is safe
113+
PyObject* pyFile = PyFile_FromString(const_cast<char*>(filename), "r");
122114
if (pyFile)
123115
{
124116
PyRun_SimpleFile(PyFile_AsFile(pyFile), filename);

PythonScript/src/PythonHandler.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
1+
#pragma once
2+
3+
14
#include "stdafx.h"
25

6+
// Forward def
7+
class ScintillaWrapper;
38

4-
#pragma once
59
class PythonHandler
610
{
711
public:
8-
PythonHandler::PythonHandler(char *pluginsDir, char *configDir);
12+
PythonHandler::PythonHandler(char *pluginsDir, char *configDir, HWND nppHandle, HWND scintilla1Handle, HWND scintilla2Handle);
913
~PythonHandler();
1014

1115
bool runScript(const char *filename);
1216
bool runScript(const std::string& filename);
1317

1418

19+
20+
1521
void initPython();
16-
void reinitPython();
1722
void runStartupScripts();
1823

24+
protected:
25+
virtual ScintillaWrapper* createScintillaWrapper();
26+
27+
// Handles
28+
HWND m_nppHandle;
29+
HWND m_scintilla1Handle;
30+
HWND m_scintilla2Handle;
1931

2032
private:
2133
// Private methods
2234
void initModules();
23-
24-
bool fileExists(const char *fileName);
25-
26-
2735

2836
// Private member vars
2937
std::string m_machineBaseDir;
3038
std::string m_userBaseDir;
39+
ScintillaWrapper *mp_scintilla;
40+
3141
};
3242

PythonScript/src/PythonScript.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -95,33 +95,17 @@ PyObject* outputRedirect(PyObject* pSelf, PyObject* pArgs)
9595
return Py_None;
9696
}
9797

98-
98+
/*
9999
100100
static PyMethodDef outputRedirectionMethods[] = {
101101
102102
// "Python name", C Ffunction Code, Argument Flags, __doc__ description
103103
{"nppoutput", outputRedirect, METH_VARARGS, "Output redirector for Notepad++."},
104104
{NULL, NULL, 0, NULL}
105105
};
106+
*/
106107

107108

108-
const char *greet()
109-
{
110-
return "hello from boost";
111-
}
112-
113-
const char *doubles(const char* s)
114-
{
115-
return s;
116-
}
117-
118-
119-
BOOST_PYTHON_MODULE(greettest)
120-
{
121-
def("greet", greet);
122-
def("doubles", doubles);
123-
}
124-
125109

126110
extern "C" __declspec(dllexport) void setInfo(NppData notepadPlusData)
127111
{
@@ -163,11 +147,12 @@ HWND getCurrentHScintilla(int which)
163147

164148
void initialise()
165149
{
166-
pythonHandler = new PythonHandler(g_pluginDir, g_configDir);
167-
PyImport_AppendInittab( "greettest", &initgreettest);
150+
pythonHandler = new PythonHandler(g_pluginDir, g_configDir, nppData._nppHandle, nppData._scintillaMainHandle, nppData._scintillaSecondHandle);
151+
152+
// PyImport_AppendInittab( "greettest", &initgreettest);
168153
pythonHandler->initPython();
169154

170-
Py_InitModule("nppoutput", outputRedirectionMethods);
155+
// Py_InitModule("nppoutput", outputRedirectionMethods);
171156
pythonHandler->runStartupScripts();
172157
/* PyRun_SimpleString(
173158
"import sys\n"
@@ -177,14 +162,15 @@ void initialise()
177162
"\t\tnppoutput.nppoutput(str)\n"
178163
"sys.stdout = StdoutRedirect()\n"
179164
"sys.stderr = StdoutRedirect()\n");
180-
*/
165+
181166
// PyImport_ImportModule("greettest");
182167
object main_module((
183168
handle<>(borrowed(PyImport_AddModule("__main__")))));
184169
185170
object main_namespace = main_module.attr("__dict__");
186171
object greettest_module( (handle<>(PyImport_ImportModule("greettest"))) );
187172
main_namespace["greettest"] = greettest_module;
173+
*/
188174
}
189175

190176
extern "C" __declspec(dllexport) void beNotified(SCNotification *notifyCode)

0 commit comments

Comments
 (0)