Skip to content

Commit 283cfe7

Browse files
committed
All generated scintilla files working
- added generated files to track changes - CreateWrapper.py now generating all files automatically - enums and scintilla commands working fine Signed-off-by: Dave Brotherstone <davegb@pobox.com>
1 parent 75b8a19 commit 283cfe7

13 files changed

Lines changed: 9646 additions & 1876 deletions

PythonScript/project/PythonScript2010.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
</ItemGroup>
8888
<ItemGroup>
8989
<ClCompile Include="..\src\AboutDialog.cpp" />
90+
<ClCompile Include="..\src\EnumsWrapper.cpp" />
9091
<ClCompile Include="..\src\PythonHandler.cpp" />
9192
<ClCompile Include="..\src\PythonScript.cpp" />
9293
<ClCompile Include="..\src\ScintillaCells.cpp" />
@@ -98,9 +99,11 @@
9899
</ItemGroup>
99100
<ItemGroup>
100101
<ClInclude Include="..\src\AboutDialog.h" />
102+
<ClInclude Include="..\src\Enums.h" />
101103
<ClInclude Include="..\src\PythonHandler.h" />
102104
<ClInclude Include="..\src\PythonScript.h" />
103105
<ClInclude Include="..\src\resource.h" />
106+
<ClInclude Include="..\src\SciLexer.h" />
104107
<ClInclude Include="..\src\Scintilla.h" />
105108
<ClInclude Include="..\src\ScintillaCells.h" />
106109
<ClInclude Include="..\src\ScintillaPython.h" />

PythonScript/project/PythonScript2010.vcxproj.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
<ClCompile Include="..\src\ScintillaWrapperGenerated.cpp">
4646
<Filter>Source Files</Filter>
4747
</ClCompile>
48+
<ClCompile Include="..\src\EnumsWrapper.cpp">
49+
<Filter>Source Files</Filter>
50+
</ClCompile>
4851
</ItemGroup>
4952
<ItemGroup>
5053
<ClInclude Include="..\src\AboutDialog.h">
@@ -77,6 +80,12 @@
7780
<ClInclude Include="..\src\ScintillaCells.h">
7881
<Filter>Header Files</Filter>
7982
</ClInclude>
83+
<ClInclude Include="..\src\Enums.h">
84+
<Filter>Header Files</Filter>
85+
</ClInclude>
86+
<ClInclude Include="..\src\SciLexer.h">
87+
<Filter>Header Files</Filter>
88+
</ClInclude>
8089
</ItemGroup>
8190
<ItemGroup>
8291
<ResourceCompile Include="..\src\PythonScript.rc">

PythonScript/src/BoostScintilla.cpp

Lines changed: 570 additions & 0 deletions
Large diffs are not rendered by default.

PythonScript/src/CreateWrapper.py

Lines changed: 147 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# DONE - excluded Type: formatrange
2323
# DONE Type: textrange
2424
# GetStyledText needs exception -currently DANGEROUS - needs twice as many bytes as currently
25-
25+
# Keymod type
2626

2727
import sys
2828
import os
@@ -34,21 +34,26 @@
3434
'string' : 'boost::python::str',
3535
'position' : 'int',
3636
'cells' : 'ScintillaCells',
37-
'colour' : 'boost::python::tuple'
37+
'colour' : 'boost::python::tuple',
38+
'keymod' : 'int' # Temporary hack - need this to be a real type
3839
}
3940

4041
castsL = {
4142
'boost::python::str' : lambda name: "reinterpret_cast<LPARAM>(static_cast<const char*>(extract<const char *>(" + name + ")))",
42-
'colour': lambda name: "MAKECOLOUR(name)".format(name)
43+
# Hack - assume a tuple is a colour
44+
'boost::python::tuple': lambda name: "MAKECOLOUR(" + name + ")".format(name)
4345
}
4446

4547
castsW = {
4648
'boost::python::str' : lambda name: "reinterpret_cast<WPARAM>(static_cast<const char*>(extract<const char *>(" + name + ")))",
47-
'colour': lambda name: "MAKECOLOUR(name)".format(name)
49+
# Hack - assume a tuple is a colour
50+
'boost::python::tuple': lambda name: "MAKECOLOUR(" + name + ")".format(name)
4851
}
4952

5053
castsRet = {
51-
'bool' : lambda val: 'static_cast<bool>(' + val + ')'
54+
'bool' : lambda val: 'return static_cast<bool>(' + val + ')',
55+
'boost::python::tuple': lambda val: 'int retVal = callScintilla(' + val + ');\n\treturn make_tuple(COLOUR_RED(retVal), COLOUR_GREEN(retVal), COLOUR_BLUE(retVal))'
56+
5257
}
5358

5459

@@ -73,10 +78,10 @@ def castWparam(ty, name):
7378
return castsW.get(ty, lambda n: n)(name)
7479

7580
def castReturn(ty, val):
76-
return castsRet.get(ty, lambda v: v)(val)
81+
return castsRet.get(ty, lambda v: 'return ' + v)(val)
7782

7883
def cellsBody(v, out):
79-
out.write("\treturn callScintilla(" + symbolName(v) + ", " + v["Param2Name"] + ".length(), " + v["Param2Name"] + ".cellsPtr());\n")
84+
out.write("\treturn callScintilla(" + symbolName(v) + ", " + v["Param2Name"] + ".length(), reinterpret_cast<LPARAM>(" + v["Param2Name"] + ".cells()));\n")
8085

8186
def constString(v, out):
8287
out.write("\tconst char *raw = extract<const char *>(" + v["Param2Name"] + ");\n")
@@ -86,7 +91,7 @@ def retString(v, out):
8691
out.write("\tint resultLength = callScintilla(" + symbolName(v) + ");\n")
8792
out.write("\tchar *result = (char *)malloc(resultLength + 1);\n")
8893
out.write("\tcallScintilla(" + symbolName(v) + ", resultLength + 1, reinterpret_cast<LPARAM>(result));\n")
89-
out.write("\tresult[resultLength] = '\0';\n")
94+
out.write("\tresult[resultLength] = '\\0';\n")
9095
out.write("\tstr o = str((const char *)result);\n")
9196
out.write("\tfree(result);\n")
9297
out.write("\treturn o;\n")
@@ -95,19 +100,19 @@ def retStringNoLength(v, out):
95100
out.write("\tint resultLength = callScintilla(" + symbolName(v) + ");\n")
96101
out.write("\tchar *result = (char *)malloc(resultLength + 1);\n")
97102
out.write("\tcallScintilla(" + symbolName(v) + ", 0, reinterpret_cast<LPARAM>(result));\n")
98-
out.write("\tresult[resultLength] = '\0';\n")
103+
out.write("\tresult[resultLength] = '\\0';\n")
99104
out.write("\tstr o = str((const char *)result);\n")
100105
out.write("\tfree(result);\n")
101106
out.write("\treturn o;\n")
102107

103108
def findTextBody(v, out):
104-
out.write('\tSci_FindText src;\n')
109+
out.write('\tSci_TextToFind src;\n')
105110
out.write('\tsrc.chrg.cpMin = start;\n')
106111
out.write('\tsrc.chrg.cpMax = end;\n')
107112
out.write('\tsrc.lpstrText = const_cast<char*>((const char *)extract<const char *>({0}));\n'.format(v['Param2Name']))
108-
out.write('\tint result = callScintilla({0}, {1}, &src);\n'.format(symbolName(v), v["Param1Name"]))
113+
out.write('\tint result = callScintilla({0}, {1}, reinterpret_cast<LPARAM>(&src));\n'.format(symbolName(v), v["Param1Name"]))
109114
out.write('\tif (-1 == result)\n')
110-
out.write('\t{\n\t\tPy_INCREF(Py_None);\n\t\treturn Py_None;\n\t}\n')
115+
out.write('\t{\n\t\treturn object();\n\t}\n')
111116
out.write('\telse\n\t{\n\t\treturn make_tuple(src.chrgText.cpMin, src.chrgText.cpMax);\n\t}\n')
112117

113118

@@ -122,13 +127,39 @@ def getTextRangeBody(v, out):
122127
out.write('\tsrc.chrg.cpMin = start;\n')
123128
out.write('\tsrc.chrg.cpMax = end;\n')
124129
out.write('\tsrc.lpstrText = new char[(end-start) + 1];\n')
125-
out.write('\tcallScintilla({0}, 0, &src);\n'.format(symbolName(v)))
130+
out.write('\tcallScintilla({0}, 0, reinterpret_cast<LPARAM>(&src));\n'.format(symbolName(v)))
126131
out.write('\tstr ret(src.lpstrText);\n')
127132
out.write('\tdelete src.lpstrText;\n')
128-
out.write('\treturn src;\n')
133+
out.write('\treturn ret;\n')
129134

130-
131135

136+
def getStyledTextBody(v, out):
137+
out.write('\tSci_TextRange src;\n')
138+
out.write('\tif (end < start)\n')
139+
out.write('\t{\n')
140+
out.write('\t\tint temp = start;\n')
141+
out.write('\t\tstart = start;\n')
142+
out.write('\t\tend = temp;\n')
143+
out.write('\t}\n')
144+
out.write('\tsrc.chrg.cpMin = start;\n')
145+
out.write('\tsrc.chrg.cpMax = end;\n')
146+
out.write('\tsrc.lpstrText = new char[((end-start) * 2) + 2];\n')
147+
out.write('\tcallScintilla({0}, 0, reinterpret_cast<LPARAM>(&src));\n'.format(symbolName(v)))
148+
out.write('\tlist styles;\n')
149+
out.write('\tchar *result = new char[(end-start) + 1];\n')
150+
out.write('\tfor(int pos = 0; pos < (end - start); pos++)\n')
151+
out.write('\t{\n')
152+
out.write('\t\tresult[pos] = src.lpstrText[pos * 2];\n')
153+
out.write('\t\tstyles.append((int)(src.lpstrText[(pos * 2) + 1]));\n')
154+
out.write('\t}\n')
155+
out.write("\tresult[end-start] = '\\0';\n")
156+
out.write('\tstr resultStr(const_cast<const char*>(result));\n')
157+
out.write('\tdelete src.lpstrText;\n')
158+
out.write('\tdelete result;\n')
159+
out.write('\treturn make_tuple(resultStr, styles);\n')
160+
161+
162+
132163
def standardBody(v, out):
133164
call = 'callScintilla(' + symbolName(v)
134165

@@ -144,7 +175,7 @@ def standardBody(v, out):
144175

145176

146177
if (v["ReturnType"] != 'void'):
147-
out.write('\treturn ')
178+
out.write('\t')
148179
out.write(castReturn(v["ReturnType"], call))
149180
else:
150181
out.write('\t' + call)
@@ -192,9 +223,13 @@ def writeParams(param1Type, param1Name, param2Type, param2Name):
192223
('int', 'length', 'stringresult', '') : ('boost::python::str', '' , '', retString),
193224
('', '', 'stringresult', '') : ('boost::python::str', '' , '', retStringNoLength),
194225
('int', 'length', 'cells', '') : ('int', '', 'ScintillaCells', cellsBody),
195-
('int', '', 'findtext', 'ft') : ('boost::python::tuple', 'int', 'findtext', findTextBody),
226+
('int', '', 'findtext', 'ft') : ('boost::python::object', 'int', 'findtext', findTextBody),
196227
('', '', 'textrange', 'tr') : ('boost::python::str', '', 'textrange', getTextRangeBody)
197-
}
228+
}
229+
230+
specialCases = {
231+
'GetStyledText' : ('boost::python::tuple', 'int', 'start', 'int', 'end', getStyledTextBody)
232+
}
198233

199234
def getSignature(v):
200235
sig = v["ReturnType"] + " ScintillaWrapper::" + v["Name"] + "("
@@ -205,27 +240,32 @@ def getSignature(v):
205240
def writeCppFile(f,out):
206241
out.write('#include "stdafx.h"\n')
207242
out.write('#include "ScintillaWrapper.h"\n')
243+
out.write('#include "Scintilla.h"\n')
208244
out.write('\n\n')
245+
209246
for name in f.order:
210247
v = f.features[name]
211248
if v["Category"] != "Deprecated":
212249
if v["FeatureType"] in ["fun", "get", "set"]:
213250

214251
if v["Name"] in exclusions:
215252
continue
216-
217-
sig = mapSignature((v["Param1Type"], v["Param1Name"], v["Param2Type"], v["Param2Name"]))
218-
returnType = "int"
219-
if sig is not None:
220-
v["ReturnType"] = sig[0]
221-
v["Param1Type"] = sig[1]
222-
v["Param2Type"] = sig[2]
223-
body = sig[3]
253+
254+
if v["Name"] in specialCases:
255+
(v["ReturnType"], v["Param1Type"], v["Param1Name"], v["Param2Type"], v["Param2Name"], body) = specialCases[v["Name"]]
224256
else:
225-
v["ReturnType"] = mapType(v["ReturnType"])
226-
v["Param1Type"] = mapType(v["Param1Type"])
227-
v["Param2Type"] = mapType(v["Param2Type"])
228-
body = standardBody
257+
sig = mapSignature((v["Param1Type"], v["Param1Name"], v["Param2Type"], v["Param2Name"]))
258+
returnType = "int"
259+
if sig is not None:
260+
v["ReturnType"] = sig[0]
261+
v["Param1Type"] = sig[1]
262+
v["Param2Type"] = sig[2]
263+
body = sig[3]
264+
else:
265+
v["ReturnType"] = mapType(v["ReturnType"])
266+
v["Param1Type"] = mapType(v["Param1Type"])
267+
v["Param2Type"] = mapType(v["Param2Type"])
268+
body = standardBody
229269

230270
out.write("/** " + "\n * ".join(v["Comment"]) + "\n */\n")
231271

@@ -234,6 +274,9 @@ def writeCppFile(f,out):
234274
body(v, out)
235275
out.write("}\n\n")
236276

277+
278+
279+
237280
def writeHFile(f,out):
238281
for name in f.order:
239282
v = f.features[name]
@@ -256,12 +299,15 @@ def writeHFile(f,out):
256299
v["Param2Type"] = mapType(v["Param2Type"])
257300
body = standardBody
258301

259-
out.write("/** " + "\n * ".join(v["Comment"]) + "\n */\n")
260-
261-
out.write(getSignature(v))
262-
out.write(";\n")
302+
out.write("\t/** " + "\n\t * ".join(v["Comment"]) + "\n */\n")
263303

304+
out.write("\t");
305+
out.write(getSignature(v).replace(' ScintillaWrapper::', ' '))
306+
out.write(";\n\n")
264307

308+
def formatPythonName(name):
309+
return name[0:1].lower() + name[1:]
310+
265311
def writeBoostWrapFile(f,out):
266312
for name in f.order:
267313
v = f.features[name]
@@ -271,10 +317,63 @@ def writeBoostWrapFile(f,out):
271317
if v["Name"] in exclusions:
272318
continue
273319

274-
out.write('\t\t.def("{0}", &ScintillaWrapper::{0}, \"'.format(v["Name"]))
275-
out.write("\\n".join(v["Comment"]).replace('"','\\"'))
320+
out.write('\t\t.def("{0}", &ScintillaWrapper::{1}, \"'.format(formatPythonName(v["Name"]), v["Name"]))
321+
out.write("\\n".join(v["Comment"]).replace('\\','\\\\').replace('"','\\"').replace('\\\\n', '\\n'))
276322
out.write('\")\n')
277323

324+
def writeEnumsHFile(f, out):
325+
for name in f.enums:
326+
v = f.enums[name]
327+
if v.get('Values'):
328+
out.write('enum {0}\n'.format(name))
329+
out.write('{\n\t')
330+
join = ""
331+
332+
for val in v.get('Values'):
333+
out.write('{0}PYSCR_{1} = {1}'.format(join, val[0]))
334+
join = ',\n\t'
335+
out.write('\n};\n\n')
336+
337+
def writeEnumsWrapperFile(f, out):
338+
for name in f.enums:
339+
v = f.enums[name]
340+
if v.get('Values'):
341+
out.write('\tenum_<{0}>("{1}")'.format(name, name.upper()))
342+
for val in v['Values']:
343+
out.write('\n\t\t.value("{0}", PYSCR_{1})'.format(val[0][len(v['Value']):].upper(), val[0]))
344+
out.write(';\n\n')
345+
346+
347+
348+
def findEnum(f, name):
349+
for e in f.enums:
350+
l = len(f.enums[e]["Value"])
351+
if f.enums[e]["Value"] == name[:l]:
352+
return e
353+
return None
354+
355+
356+
def findEnumValues(f):
357+
f.enums = {}
358+
for name in f.order:
359+
v = f.features[name]
360+
if v["FeatureType"] == 'enu':
361+
f.enums[name] = { 'Name' : name, 'Value': v["Value"] }
362+
363+
for name in f.order:
364+
365+
v = f.features[name]
366+
if v["FeatureType"] == 'val':
367+
enum = findEnum(f, name)
368+
369+
370+
if enum is not None:
371+
if f.enums[enum].get("Values", None) == None:
372+
f.enums[enum]["Values"] = []
373+
f.enums[enum]["Values"] += [(name, v["Value"])]
374+
375+
376+
278377
def CopyWithInsertion(input, output, genfn, definition):
279378
copying = 1
280379
for line in input.readlines():
@@ -308,16 +407,27 @@ def Regenerate(filename, genfn, definition):
308407
os.unlink(filename)
309408
os.rename(tempname, filename)
310409

410+
411+
412+
413+
414+
415+
416+
311417
f = Face.Face()
312418
try:
419+
313420
f.ReadFromFile("Scintilla.iface")
421+
findEnumValues(f)
314422
cpp = open("ScintillaWrapperGenerated.cpp", 'w')
315423
writeCppFile (f, cpp)
316424
cpp.close()
317425
Regenerate("ScintillaWrapper.h", writeHFile, f)
318426
Regenerate("ScintillaPython.cpp", writeBoostWrapFile, f)
319-
427+
Regenerate("Enums.h", writeEnumsHFile, f)
428+
Regenerate("EnumsWrapper.cpp", writeEnumsWrapperFile, f)
320429
#Regenerate("SciLexer.h", printLexHFile, f)
321430
#print("Maximum ID is %s" % max([x for x in f.values if int(x) < 3000]))
322431
except:
323432
raise
433+

0 commit comments

Comments
 (0)