]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/env python | |
2 | #---------------------------------------------------------------------------- | |
3 | # Name: gen_iface.py | |
4 | # Purpose: Generate stc.h and stc.cpp from the info in Scintilla.iface | |
5 | # | |
6 | # Author: Robin Dunn | |
7 | # | |
8 | # Created: 5-Sept-2000 | |
9 | # RCS-ID: $Id$ | |
10 | # Copyright: (c) 2000 by Total Control Software | |
11 | # Licence: wxWindows licence | |
12 | #---------------------------------------------------------------------------- | |
13 | ||
14 | ||
15 | import sys, string, re, os | |
16 | from fileinput import FileInput | |
17 | ||
18 | ||
19 | IFACE = os.path.abspath('./scintilla/include/Scintilla.iface') | |
20 | H_TEMPLATE = os.path.abspath('./stc.h.in') | |
21 | CPP_TEMPLATE = os.path.abspath('./stc.cpp.in') | |
22 | H_DEST = os.path.abspath('../../include/wx/stc/stc.h') | |
23 | CPP_DEST = os.path.abspath('./stc.cpp') | |
24 | if len(sys.argv) > 1 and sys.argv[1] == '--wxpython': | |
25 | DOCSTR_DEST = os.path.abspath('../../../wxPython/src/_stc_gendocs.i') | |
26 | else: | |
27 | DOCSTR_DEST = '/dev/null' | |
28 | ||
29 | ||
30 | # Value prefixes to convert | |
31 | valPrefixes = [('SCI_', ''), | |
32 | ('SC_', ''), | |
33 | ('SCN_', None), # just toss these out... | |
34 | ('SCEN_', None), | |
35 | ('SC_EFF', None), | |
36 | ('SCE_', ''), | |
37 | ('SCLEX_', 'LEX_'), | |
38 | ('SCK_', 'KEY_'), | |
39 | ('SCFIND_', 'FIND_'), | |
40 | ('SCWS_', 'WS_'), | |
41 | ] | |
42 | ||
43 | # Message function values that should have a CMD_ constant generated | |
44 | cmdValues = [ 2011, | |
45 | 2013, | |
46 | (2176, 2180), | |
47 | (2300, 2349), | |
48 | (2390, 2393), | |
49 | (2395, 2396), | |
50 | 2404, | |
51 | (2413, 2416), | |
52 | (2426, 2442), | |
53 | (2450, 2455), | |
54 | 2518, | |
55 | ] | |
56 | ||
57 | ||
58 | # Should a funciton be also generated for the CMDs? | |
59 | FUNC_FOR_CMD = 1 | |
60 | ||
61 | ||
62 | # Map some generic typenames to wx types, using return value syntax | |
63 | retTypeMap = { | |
64 | 'position': 'int', | |
65 | 'string': 'wxString', | |
66 | 'colour': 'wxColour', | |
67 | } | |
68 | ||
69 | # Map some generic typenames to wx types, using parameter syntax | |
70 | paramTypeMap = { | |
71 | 'position': 'int', | |
72 | 'string': 'const wxString&', | |
73 | 'colour': 'const wxColour&', | |
74 | 'keymod': 'int', | |
75 | } | |
76 | ||
77 | # Map of method info that needs tweaked. Either the name needs changed, or | |
78 | # the method definition/implementation. Tuple items are: | |
79 | # | |
80 | # 1. New method name. None to skip the method, 0 to leave the | |
81 | # default name. | |
82 | # 2. Method definition for the .h file, 0 to leave alone | |
83 | # 3. Method implementation for the .cpp file, 0 to leave alone. | |
84 | # 4. tuple of Doc string lines, or 0 to leave alone. | |
85 | # | |
86 | methodOverrideMap = { | |
87 | 'AddText' : (0, | |
88 | 'void %s(const wxString& text);', | |
89 | ||
90 | '''void %s(const wxString& text) { | |
91 | const wxWX2MBbuf buf = wx2stc(text); | |
92 | SendMsg(%s, wx2stclen(text, buf), (sptr_t)(const char*)buf);''', | |
93 | 0), | |
94 | ||
95 | 'AddStyledText' : (0, | |
96 | 'void %s(const wxMemoryBuffer& data);', | |
97 | ||
98 | '''void %s(const wxMemoryBuffer& data) { | |
99 | SendMsg(%s, data.GetDataLen(), (sptr_t)data.GetData());''', | |
100 | 0), | |
101 | ||
102 | 'AppendText' : (0, | |
103 | 'void %s(const wxString& text);', | |
104 | ||
105 | '''void %s(const wxString& text) { | |
106 | const wxWX2MBbuf buf = wx2stc(text); | |
107 | SendMsg(%s, wx2stclen(text, buf), (sptr_t)(const char*)buf);''', | |
108 | 0), | |
109 | ||
110 | 'GetViewWS' : ( 'GetViewWhiteSpace', 0, 0, 0), | |
111 | 'SetViewWS' : ( 'SetViewWhiteSpace', 0, 0, 0), | |
112 | ||
113 | 'GetCharAt' : | |
114 | ( 0, 0, | |
115 | '''int %s(int pos) const { | |
116 | return (unsigned char)SendMsg(%s, pos, 0);''', | |
117 | 0), | |
118 | ||
119 | 'GetStyleAt' : | |
120 | ( 0, 0, | |
121 | '''int %s(int pos) const { | |
122 | return (unsigned char)SendMsg(%s, pos, 0);''', | |
123 | 0), | |
124 | ||
125 | 'GetStyledText' : | |
126 | (0, | |
127 | 'wxMemoryBuffer %s(int startPos, int endPos);', | |
128 | ||
129 | '''wxMemoryBuffer %s(int startPos, int endPos) { | |
130 | wxMemoryBuffer buf; | |
131 | if (endPos < startPos) { | |
132 | int temp = startPos; | |
133 | startPos = endPos; | |
134 | endPos = temp; | |
135 | } | |
136 | int len = endPos - startPos; | |
137 | if (!len) return buf; | |
138 | TextRange tr; | |
139 | tr.lpstrText = (char*)buf.GetWriteBuf(len*2+1); | |
140 | tr.chrg.cpMin = startPos; | |
141 | tr.chrg.cpMax = endPos; | |
142 | len = SendMsg(%s, 0, (sptr_t)&tr); | |
143 | buf.UngetWriteBuf(len); | |
144 | return buf;''', | |
145 | ||
146 | ('Retrieve a buffer of cells.',)), | |
147 | ||
148 | ||
149 | 'PositionFromPoint' : | |
150 | (0, | |
151 | 'int %s(wxPoint pt) const;', | |
152 | ||
153 | '''int %s(wxPoint pt) const { | |
154 | return SendMsg(%s, pt.x, pt.y);''', | |
155 | 0), | |
156 | ||
157 | 'GetCurLine' : | |
158 | (0, | |
159 | '#ifdef SWIG\n wxString %s(int* OUTPUT);\n#else\n wxString GetCurLine(int* linePos=NULL);\n#endif', | |
160 | ||
161 | '''wxString %s(int* linePos) { | |
162 | int len = LineLength(GetCurrentLine()); | |
163 | if (!len) { | |
164 | if (linePos) *linePos = 0; | |
165 | return wxEmptyString; | |
166 | } | |
167 | ||
168 | wxMemoryBuffer mbuf(len+1); | |
169 | char* buf = (char*)mbuf.GetWriteBuf(len+1); | |
170 | ||
171 | int pos = SendMsg(%s, len+1, (sptr_t)buf); | |
172 | mbuf.UngetWriteBuf(len); | |
173 | mbuf.AppendByte(0); | |
174 | if (linePos) *linePos = pos; | |
175 | return stc2wx(buf);''', | |
176 | ||
177 | 0), | |
178 | ||
179 | 'SetUsePalette' : (None, 0,0,0), | |
180 | ||
181 | 'MarkerSetFore' : ('MarkerSetForeground', 0, 0, 0), | |
182 | 'MarkerSetBack' : ('MarkerSetBackground', 0, 0, 0), | |
183 | ||
184 | 'MarkerSymbolDefined' : ('GetMarkerSymbolDefined', 0, 0, 0), | |
185 | ||
186 | 'MarkerDefine' : | |
187 | (0, | |
188 | '''void %s(int markerNumber, int markerSymbol, | |
189 | const wxColour& foreground = wxNullColour, | |
190 | const wxColour& background = wxNullColour);''', | |
191 | ||
192 | '''void %s(int markerNumber, int markerSymbol, | |
193 | const wxColour& foreground, | |
194 | const wxColour& background) { | |
195 | ||
196 | SendMsg(%s, markerNumber, markerSymbol); | |
197 | if (foreground.IsOk()) | |
198 | MarkerSetForeground(markerNumber, foreground); | |
199 | if (background.IsOk()) | |
200 | MarkerSetBackground(markerNumber, background);''', | |
201 | ||
202 | ('Set the symbol used for a particular marker number,', | |
203 | 'and optionally the fore and background colours.')), | |
204 | ||
205 | ||
206 | 'MarkerDefinePixmap' : | |
207 | ('MarkerDefineBitmap', | |
208 | '''void %s(int markerNumber, const wxBitmap& bmp);''', | |
209 | '''void %s(int markerNumber, const wxBitmap& bmp) { | |
210 | // convert bmp to a xpm in a string | |
211 | wxMemoryOutputStream strm; | |
212 | wxImage img = bmp.ConvertToImage(); | |
213 | if (img.HasAlpha()) | |
214 | img.ConvertAlphaToMask(); | |
215 | img.SaveFile(strm, wxBITMAP_TYPE_XPM); | |
216 | size_t len = strm.GetSize(); | |
217 | char* buff = new char[len+1]; | |
218 | strm.CopyTo(buff, len); | |
219 | buff[len] = 0; | |
220 | SendMsg(%s, markerNumber, (sptr_t)buff); | |
221 | delete [] buff; | |
222 | ''', | |
223 | ('Define a marker from a bitmap',)), | |
224 | ||
225 | ||
226 | 'SetMarginTypeN' : ('SetMarginType', 0, 0, 0), | |
227 | 'GetMarginTypeN' : ('GetMarginType', 0, 0, 0), | |
228 | 'SetMarginWidthN' : ('SetMarginWidth', 0, 0, 0), | |
229 | 'GetMarginWidthN' : ('GetMarginWidth', 0, 0, 0), | |
230 | 'SetMarginMaskN' : ('SetMarginMask', 0, 0, 0), | |
231 | 'GetMarginMaskN' : ('GetMarginMask', 0, 0, 0), | |
232 | 'SetMarginSensitiveN' : ('SetMarginSensitive', 0, 0, 0), | |
233 | 'GetMarginSensitiveN' : ('GetMarginSensitive', 0, 0, 0), | |
234 | ||
235 | 'MarginGetText' : | |
236 | (0, | |
237 | 'wxString %s(int line) const;', | |
238 | ||
239 | '''wxString %s(int line) const { | |
240 | long msg = %s; | |
241 | long len = SendMsg(msg, line, 0); | |
242 | ||
243 | wxMemoryBuffer mbuf(len+1); | |
244 | char* buf = (char*)mbuf.GetWriteBuf(len+1); | |
245 | SendMsg(msg, line, (sptr_t)buf); | |
246 | mbuf.UngetWriteBuf(len); | |
247 | mbuf.AppendByte(0); | |
248 | return stc2wx(buf);''', | |
249 | 0), | |
250 | ||
251 | 'MarginGetStyles' : | |
252 | (0, | |
253 | 'wxString %s(int line) const;', | |
254 | ||
255 | '''wxString %s(int line) const { | |
256 | long msg = %s; | |
257 | long len = SendMsg(msg, line, 0); | |
258 | ||
259 | wxMemoryBuffer mbuf(len+1); | |
260 | char* buf = (char*)mbuf.GetWriteBuf(len+1); | |
261 | SendMsg(msg, line, (sptr_t)buf); | |
262 | mbuf.UngetWriteBuf(len); | |
263 | mbuf.AppendByte(0); | |
264 | return stc2wx(buf);''', | |
265 | 0), | |
266 | ||
267 | 'SetAdditionalSelFore' : ('SetAdditionalSelForeground', 0, 0, 0), | |
268 | 'SetAdditionalSelBack' : ('SetAdditionalSelBackground', 0, 0, 0), | |
269 | 'SetAdditionalCaretFore' : ('SetAdditionalCaretForeground', 0, 0, 0), | |
270 | 'GetAdditionalCaretFore' : ('GetAdditionalCaretForeground', 0, 0, 0), | |
271 | ||
272 | 'AnnotationGetText' : | |
273 | (0, | |
274 | 'wxString %s(int line) const;', | |
275 | ||
276 | '''wxString %s(int line) const { | |
277 | long msg = %s; | |
278 | long len = SendMsg(msg, line, 0); | |
279 | ||
280 | wxMemoryBuffer mbuf(len+1); | |
281 | char* buf = (char*)mbuf.GetWriteBuf(len+1); | |
282 | SendMsg(msg, line, (sptr_t)buf); | |
283 | mbuf.UngetWriteBuf(len); | |
284 | mbuf.AppendByte(0); | |
285 | return stc2wx(buf);''', | |
286 | 0), | |
287 | ||
288 | 'AnnotationGetStyles' : | |
289 | (0, | |
290 | 'wxString %s(int line) const;', | |
291 | ||
292 | '''wxString %s(int line) const { | |
293 | long msg = %s; | |
294 | long len = SendMsg(msg, line, 0); | |
295 | ||
296 | wxMemoryBuffer mbuf(len+1); | |
297 | char* buf = (char*)mbuf.GetWriteBuf(len+1); | |
298 | SendMsg(msg, line, (sptr_t)buf); | |
299 | mbuf.UngetWriteBuf(len); | |
300 | mbuf.AppendByte(0); | |
301 | return stc2wx(buf);''', | |
302 | 0), | |
303 | ||
304 | 'StyleGetFore' : ('StyleGetForeground', 0, 0, 0), | |
305 | 'StyleGetBack' : ('StyleGetBackground', 0, 0, 0), | |
306 | 'StyleSetFore' : ('StyleSetForeground', 0, 0, 0), | |
307 | 'StyleSetBack' : ('StyleSetBackground', 0, 0, 0), | |
308 | 'SetSelFore' : ('SetSelForeground', 0, 0, 0), | |
309 | 'SetSelBack' : ('SetSelBackground', 0, 0, 0), | |
310 | 'SetCaretFore' : ('SetCaretForeground', 0, 0, 0), | |
311 | 'StyleGetFont' : | |
312 | ('StyleGetFaceName', | |
313 | 'wxString %s(int style);', | |
314 | '''wxString %s(int style) { | |
315 | long msg = %s; | |
316 | long len = SendMsg(msg, style, 0); | |
317 | wxMemoryBuffer mbuf(len+1); | |
318 | char* buf = (char*)mbuf.GetWriteBuf(len+1); | |
319 | SendMsg(msg, style, (sptr_t)buf); | |
320 | mbuf.UngetWriteBuf(len); | |
321 | mbuf.AppendByte(0); | |
322 | return stc2wx(buf);''', | |
323 | ('Get the font facename of a style',)), | |
324 | 'StyleSetFont' : ('StyleSetFaceName', 0, 0, 0), | |
325 | 'StyleSetCharacterSet' : (None, 0, 0, 0), | |
326 | ||
327 | 'AssignCmdKey' : | |
328 | ('CmdKeyAssign', | |
329 | 'void %s(int key, int modifiers, int cmd);', | |
330 | ||
331 | '''void %s(int key, int modifiers, int cmd) { | |
332 | SendMsg(%s, MAKELONG(key, modifiers), cmd);''', | |
333 | 0), | |
334 | ||
335 | ||
336 | 'ClearCmdKey' : | |
337 | ('CmdKeyClear', | |
338 | 'void %s(int key, int modifiers);', | |
339 | ||
340 | '''void %s(int key, int modifiers) { | |
341 | SendMsg(%s, MAKELONG(key, modifiers));''', | |
342 | 0), | |
343 | ||
344 | 'ClearAllCmdKeys' : ('CmdKeyClearAll', 0, 0, 0), | |
345 | ||
346 | ||
347 | 'SetStylingEx' : | |
348 | ('SetStyleBytes', | |
349 | 'void %s(int length, char* styleBytes);', | |
350 | ||
351 | '''void %s(int length, char* styleBytes) { | |
352 | SendMsg(%s, length, (sptr_t)styleBytes);''', | |
353 | 0), | |
354 | ||
355 | ||
356 | 'IndicSetAlpha' : ('IndicatorSetAlpha', 0, 0, 0), | |
357 | 'IndicGetAlpha' : ('IndicatorGetAlpha', 0, 0, 0), | |
358 | 'IndicSetStyle' : ('IndicatorSetStyle', 0, 0, 0), | |
359 | 'IndicGetStyle' : ('IndicatorGetStyle', 0, 0, 0), | |
360 | 'IndicSetFore' : ('IndicatorSetForeground', 0, 0, 0), | |
361 | 'IndicGetFore' : ('IndicatorGetForeground', 0, 0, 0), | |
362 | 'IndicSetUnder': ('IndicatorSetUnder', 0, 0, 0), | |
363 | 'IndicGetUnder': ('IndicatorGetUnder', 0, 0, 0), | |
364 | ||
365 | 'SetWhitespaceFore' : ('SetWhitespaceForeground', 0, 0, 0), | |
366 | 'SetWhitespaceBack' : ('SetWhitespaceBackground', 0, 0, 0), | |
367 | ||
368 | 'AutoCShow' : ('AutoCompShow', 0, 0, 0), | |
369 | 'AutoCCancel' : ('AutoCompCancel', 0, 0, 0), | |
370 | 'AutoCActive' : ('AutoCompActive', 0, 0, 0), | |
371 | 'AutoCPosStart' : ('AutoCompPosStart', 0, 0, 0), | |
372 | 'AutoCComplete' : ('AutoCompComplete', 0, 0, 0), | |
373 | 'AutoCStops' : ('AutoCompStops', 0, 0, 0), | |
374 | 'AutoCSetSeparator' : ('AutoCompSetSeparator', 0, 0, 0), | |
375 | 'AutoCGetSeparator' : ('AutoCompGetSeparator', 0, 0, 0), | |
376 | 'AutoCSelect' : ('AutoCompSelect', 0, 0, 0), | |
377 | 'AutoCSetCancelAtStart' : ('AutoCompSetCancelAtStart', 0, 0, 0), | |
378 | 'AutoCGetCancelAtStart' : ('AutoCompGetCancelAtStart', 0, 0, 0), | |
379 | 'AutoCSetFillUps' : ('AutoCompSetFillUps', 0, 0, 0), | |
380 | 'AutoCSetChooseSingle' : ('AutoCompSetChooseSingle', 0, 0, 0), | |
381 | 'AutoCGetChooseSingle' : ('AutoCompGetChooseSingle', 0, 0, 0), | |
382 | 'AutoCSetIgnoreCase' : ('AutoCompSetIgnoreCase', 0, 0, 0), | |
383 | 'AutoCGetIgnoreCase' : ('AutoCompGetIgnoreCase', 0, 0, 0), | |
384 | 'AutoCSetAutoHide' : ('AutoCompSetAutoHide', 0, 0, 0), | |
385 | 'AutoCGetAutoHide' : ('AutoCompGetAutoHide', 0, 0, 0), | |
386 | 'AutoCSetDropRestOfWord' : ('AutoCompSetDropRestOfWord', 0,0,0), | |
387 | 'AutoCGetDropRestOfWord' : ('AutoCompGetDropRestOfWord', 0,0,0), | |
388 | 'AutoCGetTypeSeparator' : ('AutoCompGetTypeSeparator', 0, 0, 0), | |
389 | 'AutoCSetTypeSeparator' : ('AutoCompSetTypeSeparator', 0, 0, 0), | |
390 | 'AutoCGetCurrent' : ('AutoCompGetCurrent', 0, 0, 0), | |
391 | 'AutoCGetCurrentText' : (None, 0, 0, 0), | |
392 | 'AutoCSetMaxWidth' : ('AutoCompSetMaxWidth', 0, 0, 0), | |
393 | 'AutoCGetMaxWidth' : ('AutoCompGetMaxWidth', 0, 0, 0), | |
394 | 'AutoCSetMaxHeight' : ('AutoCompSetMaxHeight', 0, 0, 0), | |
395 | 'AutoCGetMaxHeight' : ('AutoCompGetMaxHeight', 0, 0, 0), | |
396 | 'AutoCGetMaxHeight' : ('AutoCompGetMaxHeight', 0, 0, 0), | |
397 | ||
398 | 'RegisterImage' : | |
399 | (0, | |
400 | '''void %s(int type, const wxBitmap& bmp);''', | |
401 | '''void %s(int type, const wxBitmap& bmp) { | |
402 | // convert bmp to a xpm in a string | |
403 | wxMemoryOutputStream strm; | |
404 | wxImage img = bmp.ConvertToImage(); | |
405 | if (img.HasAlpha()) | |
406 | img.ConvertAlphaToMask(); | |
407 | img.SaveFile(strm, wxBITMAP_TYPE_XPM); | |
408 | size_t len = strm.GetSize(); | |
409 | char* buff = new char[len+1]; | |
410 | strm.CopyTo(buff, len); | |
411 | buff[len] = 0; | |
412 | SendMsg(%s, type, (sptr_t)buff); | |
413 | delete [] buff; | |
414 | ''', | |
415 | ('Register an image for use in autocompletion lists.',)), | |
416 | ||
417 | ||
418 | 'ClearRegisteredImages' : (0, 0, 0, | |
419 | ('Clear all the registered images.',)), | |
420 | ||
421 | ||
422 | 'SetHScrollBar' : ('SetUseHorizontalScrollBar', 0, 0, 0), | |
423 | 'GetHScrollBar' : ('GetUseHorizontalScrollBar', 0, 0, 0), | |
424 | ||
425 | 'SetVScrollBar' : ('SetUseVerticalScrollBar', 0, 0, 0), | |
426 | 'GetVScrollBar' : ('GetUseVerticalScrollBar', 0, 0, 0), | |
427 | ||
428 | 'GetCaretFore' : ('GetCaretForeground', 0, 0, 0), | |
429 | ||
430 | 'GetUsePalette' : (None, 0, 0, 0), | |
431 | ||
432 | 'FindText' : | |
433 | (0, | |
434 | '''int %s(int minPos, int maxPos, const wxString& text, int flags=0);''', | |
435 | ||
436 | '''int %s(int minPos, int maxPos, | |
437 | const wxString& text, | |
438 | int flags) { | |
439 | TextToFind ft; | |
440 | ft.chrg.cpMin = minPos; | |
441 | ft.chrg.cpMax = maxPos; | |
442 | const wxWX2MBbuf buf = wx2stc(text); | |
443 | ft.lpstrText = (char*)(const char*)buf; | |
444 | ||
445 | return SendMsg(%s, flags, (sptr_t)&ft);''', | |
446 | 0), | |
447 | ||
448 | 'FormatRange' : | |
449 | (0, | |
450 | '''int %s(bool doDraw, | |
451 | int startPos, | |
452 | int endPos, | |
453 | wxDC* draw, | |
454 | wxDC* target, | |
455 | wxRect renderRect, | |
456 | wxRect pageRect);''', | |
457 | ''' int %s(bool doDraw, | |
458 | int startPos, | |
459 | int endPos, | |
460 | wxDC* draw, | |
461 | wxDC* target, | |
462 | wxRect renderRect, | |
463 | wxRect pageRect) { | |
464 | RangeToFormat fr; | |
465 | ||
466 | if (endPos < startPos) { | |
467 | int temp = startPos; | |
468 | startPos = endPos; | |
469 | endPos = temp; | |
470 | } | |
471 | fr.hdc = draw; | |
472 | fr.hdcTarget = target; | |
473 | fr.rc.top = renderRect.GetTop(); | |
474 | fr.rc.left = renderRect.GetLeft(); | |
475 | fr.rc.right = renderRect.GetRight(); | |
476 | fr.rc.bottom = renderRect.GetBottom(); | |
477 | fr.rcPage.top = pageRect.GetTop(); | |
478 | fr.rcPage.left = pageRect.GetLeft(); | |
479 | fr.rcPage.right = pageRect.GetRight(); | |
480 | fr.rcPage.bottom = pageRect.GetBottom(); | |
481 | fr.chrg.cpMin = startPos; | |
482 | fr.chrg.cpMax = endPos; | |
483 | ||
484 | return SendMsg(%s, doDraw, (sptr_t)&fr);''', | |
485 | 0), | |
486 | ||
487 | ||
488 | 'GetLine' : | |
489 | (0, | |
490 | 'wxString %s(int line) const;', | |
491 | ||
492 | '''wxString %s(int line) const { | |
493 | int len = LineLength(line); | |
494 | if (!len) return wxEmptyString; | |
495 | ||
496 | wxMemoryBuffer mbuf(len+1); | |
497 | char* buf = (char*)mbuf.GetWriteBuf(len+1); | |
498 | SendMsg(%s, line, (sptr_t)buf); | |
499 | mbuf.UngetWriteBuf(len); | |
500 | mbuf.AppendByte(0); | |
501 | return stc2wx(buf);''', | |
502 | ||
503 | ('Retrieve the contents of a line.',)), | |
504 | ||
505 | 'SetSel' : (None, 0,0,0), #'SetSelection', 0, 0, 0), | |
506 | ||
507 | 'GetSelText' : | |
508 | ('GetSelectedText', | |
509 | 'wxString %s();', | |
510 | ||
511 | '''wxString %s() { | |
512 | const int len = SendMsg(SCI_GETSELTEXT, 0, (sptr_t)0); | |
513 | if (!len) return wxEmptyString; | |
514 | ||
515 | wxMemoryBuffer mbuf(len+2); | |
516 | char* buf = (char*)mbuf.GetWriteBuf(len+1); | |
517 | SendMsg(%s, 0, (sptr_t)buf); | |
518 | mbuf.UngetWriteBuf(len); | |
519 | mbuf.AppendByte(0); | |
520 | return stc2wx(buf);''', | |
521 | ||
522 | ('Retrieve the selected text.',)), | |
523 | ||
524 | ||
525 | 'GetTextRange' : | |
526 | (0, | |
527 | 'wxString %s(int startPos, int endPos);', | |
528 | ||
529 | '''wxString %s(int startPos, int endPos) { | |
530 | if (endPos < startPos) { | |
531 | int temp = startPos; | |
532 | startPos = endPos; | |
533 | endPos = temp; | |
534 | } | |
535 | int len = endPos - startPos; | |
536 | if (!len) return wxEmptyString; | |
537 | wxMemoryBuffer mbuf(len+1); | |
538 | char* buf = (char*)mbuf.GetWriteBuf(len); | |
539 | TextRange tr; | |
540 | tr.lpstrText = buf; | |
541 | tr.chrg.cpMin = startPos; | |
542 | tr.chrg.cpMax = endPos; | |
543 | SendMsg(%s, 0, (sptr_t)&tr); | |
544 | mbuf.UngetWriteBuf(len); | |
545 | mbuf.AppendByte(0); | |
546 | return stc2wx(buf);''', | |
547 | ||
548 | ('Retrieve a range of text.',)), | |
549 | ||
550 | 'PointXFromPosition' : (None, 0, 0, 0), | |
551 | 'PointYFromPosition' : (None, 0, 0, 0), | |
552 | ||
553 | 'ScrollCaret' : ('EnsureCaretVisible', 0, 0, 0), | |
554 | 'ReplaceSel' : ('ReplaceSelection', 0, 0, 0), | |
555 | 'Null' : (None, 0, 0, 0), | |
556 | ||
557 | 'GetText' : | |
558 | (0, | |
559 | 'wxString %s() const;', | |
560 | ||
561 | '''wxString %s() const { | |
562 | int len = GetTextLength(); | |
563 | wxMemoryBuffer mbuf(len+1); // leave room for the null... | |
564 | char* buf = (char*)mbuf.GetWriteBuf(len+1); | |
565 | SendMsg(%s, len+1, (sptr_t)buf); | |
566 | mbuf.UngetWriteBuf(len); | |
567 | mbuf.AppendByte(0); | |
568 | return stc2wx(buf);''', | |
569 | ||
570 | ('Retrieve all the text in the document.', )), | |
571 | ||
572 | 'GetDirectFunction' : (None, 0, 0, 0), | |
573 | 'GetDirectPointer' : (None, 0, 0, 0), | |
574 | ||
575 | 'CallTipPosStart' : ('CallTipPosAtStart', 0, 0, 0), | |
576 | 'CallTipSetHlt' : ('CallTipSetHighlight', 0, 0, 0), | |
577 | 'CallTipSetBack' : ('CallTipSetBackground', 0, 0, 0), | |
578 | 'CallTipSetFore' : ('CallTipSetForeground', 0, 0, 0), | |
579 | 'CallTipSetForeHlt' : ('CallTipSetForegroundHighlight', 0, 0, 0), | |
580 | ||
581 | 'SetHotspotActiveFore' : ('SetHotspotActiveForeground', 0, 0, 0), | |
582 | 'SetHotspotActiveBack' : ('SetHotspotActiveBackground', 0, 0, 0), | |
583 | 'GetHotspotActiveFore' : ('GetHotspotActiveForeground', 0, 0, 0), | |
584 | 'GetHotspotActiveBack' : ('GetHotspotActiveBackground', 0, 0, 0), | |
585 | ||
586 | 'GetCaretLineBack' : ('GetCaretLineBackground', 0, 0, 0), | |
587 | 'SetCaretLineBack' : ('SetCaretLineBackground', 0, 0, 0), | |
588 | ||
589 | 'ReplaceTarget' : | |
590 | (0, | |
591 | 'int %s(const wxString& text);', | |
592 | ||
593 | ''' | |
594 | int %s(const wxString& text) { | |
595 | const wxWX2MBbuf buf = wx2stc(text); | |
596 | return SendMsg(%s, wx2stclen(text, buf), (sptr_t)(const char*)buf);''', | |
597 | 0), | |
598 | ||
599 | 'ReplaceTargetRE' : | |
600 | (0, | |
601 | 'int %s(const wxString& text);', | |
602 | ||
603 | ''' | |
604 | int %s(const wxString& text) { | |
605 | const wxWX2MBbuf buf = wx2stc(text); | |
606 | return SendMsg(%s, wx2stclen(text, buf), (sptr_t)(const char*)buf);''', | |
607 | 0), | |
608 | ||
609 | 'SearchInTarget' : | |
610 | (0, | |
611 | 'int %s(const wxString& text);', | |
612 | ||
613 | ''' | |
614 | int %s(const wxString& text) { | |
615 | const wxWX2MBbuf buf = wx2stc(text); | |
616 | return SendMsg(%s, wx2stclen(text, buf), (sptr_t)(const char*)buf);''', | |
617 | 0), | |
618 | ||
619 | # not sure what to do about these yet | |
620 | 'TargetAsUTF8' : ( None, 0, 0, 0), | |
621 | 'SetLengthForEncode' : ( None, 0, 0, 0), | |
622 | 'EncodedFromUTF8' : ( None, 0, 0, 0), | |
623 | ||
624 | ||
625 | 'GetProperty' : | |
626 | (0, | |
627 | 'wxString %s(const wxString& key);', | |
628 | ||
629 | '''wxString %s(const wxString& key) { | |
630 | int len = SendMsg(SCI_GETPROPERTY, (sptr_t)(const char*)wx2stc(key), 0); | |
631 | if (!len) return wxEmptyString; | |
632 | ||
633 | wxMemoryBuffer mbuf(len+1); | |
634 | char* buf = (char*)mbuf.GetWriteBuf(len+1); | |
635 | SendMsg(%s, (uptr_t)(const char*)wx2stc(key), (sptr_t)buf); | |
636 | mbuf.UngetWriteBuf(len); | |
637 | mbuf.AppendByte(0); | |
638 | return stc2wx(buf);''', | |
639 | ("Retrieve a 'property' value previously set with SetProperty.",)), | |
640 | ||
641 | 'GetPropertyExpanded' : | |
642 | (0, | |
643 | 'wxString %s(const wxString& key);', | |
644 | ||
645 | '''wxString %s(const wxString& key) { | |
646 | int len = SendMsg(SCI_GETPROPERTYEXPANDED, (uptr_t)(const char*)wx2stc(key), 0); | |
647 | if (!len) return wxEmptyString; | |
648 | ||
649 | wxMemoryBuffer mbuf(len+1); | |
650 | char* buf = (char*)mbuf.GetWriteBuf(len+1); | |
651 | SendMsg(%s, (uptr_t)(const char*)wx2stc(key), (sptr_t)buf); | |
652 | mbuf.UngetWriteBuf(len); | |
653 | mbuf.AppendByte(0); | |
654 | return stc2wx(buf);''', | |
655 | ("Retrieve a 'property' value previously set with SetProperty,", | |
656 | "with '$()' variable replacement on returned buffer.")), | |
657 | ||
658 | 'GetPropertyInt' : (0, 0, 0, | |
659 | ("Retrieve a 'property' value previously set with SetProperty,", | |
660 | "interpreted as an int AFTER any '$()' variable replacement.")), | |
661 | ||
662 | ||
663 | 'GetDocPointer' : | |
664 | (0, | |
665 | 'void* %s();', | |
666 | '''void* %s() { | |
667 | return (void*)SendMsg(%s);''', | |
668 | 0), | |
669 | ||
670 | 'SetDocPointer' : | |
671 | (0, | |
672 | 'void %s(void* docPointer);', | |
673 | '''void %s(void* docPointer) { | |
674 | SendMsg(%s, 0, (sptr_t)docPointer);''', | |
675 | 0), | |
676 | ||
677 | 'CreateDocument' : | |
678 | (0, | |
679 | 'void* %s();', | |
680 | '''void* %s() { | |
681 | return (void*)SendMsg(%s);''', | |
682 | 0), | |
683 | ||
684 | 'AddRefDocument' : | |
685 | (0, | |
686 | 'void %s(void* docPointer);', | |
687 | '''void %s(void* docPointer) { | |
688 | SendMsg(%s, 0, (sptr_t)docPointer);''', | |
689 | 0), | |
690 | ||
691 | 'ReleaseDocument' : | |
692 | (0, | |
693 | 'void %s(void* docPointer);', | |
694 | '''void %s(void* docPointer) { | |
695 | SendMsg(%s, 0, (sptr_t)docPointer);''', | |
696 | 0), | |
697 | ||
698 | 'SetCodePage' : | |
699 | (0, | |
700 | 0, | |
701 | '''void %s(int codePage) { | |
702 | #if wxUSE_UNICODE | |
703 | wxASSERT_MSG(codePage == wxSTC_CP_UTF8, | |
704 | wxT("Only wxSTC_CP_UTF8 may be used when wxUSE_UNICODE is on.")); | |
705 | #else | |
706 | wxASSERT_MSG(codePage != wxSTC_CP_UTF8, | |
707 | wxT("wxSTC_CP_UTF8 may not be used when wxUSE_UNICODE is off.")); | |
708 | #endif | |
709 | SendMsg(%s, codePage);''', | |
710 | ("Set the code page used to interpret the bytes of the document as characters.",) ), | |
711 | ||
712 | ||
713 | 'GrabFocus' : (None, 0, 0, 0), | |
714 | ||
715 | # Rename some that would otherwise hide the wxWindow methods | |
716 | 'SetFocus' : ('SetSTCFocus', 0, 0, 0), | |
717 | 'GetFocus' : ('GetSTCFocus', 0, 0, 0), | |
718 | 'SetCursor' : ('SetSTCCursor', 0, 0, 0), | |
719 | 'GetCursor' : ('GetSTCCursor', 0, 0, 0), | |
720 | ||
721 | 'LoadLexerLibrary' : (None, 0,0,0), | |
722 | ||
723 | 'SetPositionCache' : ('SetPositionCacheSize', 0, 0, 0), | |
724 | 'GetPositionCache' : ('GetPositionCacheSize', 0, 0, 0), | |
725 | ||
726 | 'GetLexerLanguage' : (None, 0, 0, 0), | |
727 | 'SetFontQuality' : (None, 0, 0, 0), | |
728 | 'GetFontQuality' : (None, 0, 0, 0), | |
729 | 'SetSelection' : (None, 0, 0, 0), | |
730 | ||
731 | 'GetCharacterPointer' : (0, | |
732 | 'const char* %s();', | |
733 | 'const char* %s() {\n' | |
734 | ' return (const char*)SendMsg(%s, 0, 0);', | |
735 | 0), | |
736 | ||
737 | ||
738 | '' : ('', 0, 0, 0), | |
739 | ||
740 | } | |
741 | ||
742 | # all Scintilla getters are transformed into const member of wxSTC class but | |
743 | # some non-getter methods are also logically const and this set contains their | |
744 | # names (notice that it's useless to include here methods manually overridden | |
745 | # above) | |
746 | constNonGetterMethods = ( | |
747 | 'LineFromPosition', | |
748 | 'PositionFromLine', | |
749 | 'LineLength', | |
750 | 'CanPaste', | |
751 | 'CanRedo', | |
752 | 'CanUndo', | |
753 | ) | |
754 | ||
755 | #---------------------------------------------------------------------------- | |
756 | ||
757 | def processIface(iface, h_tmplt, cpp_tmplt, h_dest, cpp_dest, docstr_dest): | |
758 | curDocStrings = [] | |
759 | values = [] | |
760 | methods = [] | |
761 | cmds = [] | |
762 | ||
763 | # parse iface file | |
764 | fi = FileInput(iface) | |
765 | for line in fi: | |
766 | line = line[:-1] | |
767 | if line[:2] == '##' or line == '': | |
768 | #curDocStrings = [] | |
769 | continue | |
770 | ||
771 | op = line[:4] | |
772 | if line[:2] == '# ': # a doc string | |
773 | curDocStrings.append(line[2:]) | |
774 | ||
775 | elif op == 'val ': | |
776 | parseVal(line[4:], values, curDocStrings) | |
777 | curDocStrings = [] | |
778 | ||
779 | elif op == 'fun ' or op == 'set ' or op == 'get ': | |
780 | parseFun(line[4:], methods, curDocStrings, cmds, op == 'get ') | |
781 | curDocStrings = [] | |
782 | ||
783 | elif op == 'cat ': | |
784 | if line[4:].strip() == 'Deprecated': | |
785 | break # skip the rest of the file | |
786 | ||
787 | elif op == 'evt ': | |
788 | pass | |
789 | ||
790 | elif op == 'enu ': | |
791 | pass | |
792 | ||
793 | elif op == 'lex ': | |
794 | pass | |
795 | ||
796 | else: | |
797 | print('***** Unknown line type: %s' % line) | |
798 | ||
799 | ||
800 | # process templates | |
801 | data = {} | |
802 | data['VALUES'] = processVals(values) | |
803 | data['CMDS'] = processVals(cmds) | |
804 | defs, imps, docstrings = processMethods(methods) | |
805 | data['METHOD_DEFS'] = defs | |
806 | data['METHOD_IMPS'] = imps | |
807 | ||
808 | # get template text | |
809 | h_text = open(h_tmplt).read() | |
810 | cpp_text = open(cpp_tmplt).read() | |
811 | ||
812 | # do the substitutions | |
813 | h_text = h_text % data | |
814 | cpp_text = cpp_text % data | |
815 | ||
816 | # write out destination files | |
817 | open(h_dest, 'w').write(h_text) | |
818 | open(cpp_dest, 'w').write(cpp_text) | |
819 | open(docstr_dest, 'w').write(docstrings) | |
820 | ||
821 | ||
822 | ||
823 | def joinWithNewLines(values): | |
824 | return '\n'.join(values) | |
825 | ||
826 | #---------------------------------------------------------------------------- | |
827 | ||
828 | def processVals(values): | |
829 | text = [] | |
830 | for name, value, docs in values: | |
831 | if docs: | |
832 | text.append('') | |
833 | for x in docs: | |
834 | text.append('// ' + x) | |
835 | text.append('#define %s %s' % (name, value)) | |
836 | return joinWithNewLines(text) | |
837 | ||
838 | #---------------------------------------------------------------------------- | |
839 | ||
840 | def processMethods(methods): | |
841 | defs = [] | |
842 | imps = [] | |
843 | dstr = [] | |
844 | ||
845 | for retType, name, number, param1, param2, docs, is_const in methods: | |
846 | retType = retTypeMap.get(retType, retType) | |
847 | params = makeParamString(param1, param2) | |
848 | ||
849 | name, theDef, theImp, docs = checkMethodOverride(name, number, docs) | |
850 | ||
851 | if name is None: | |
852 | continue | |
853 | ||
854 | # Build docstrings | |
855 | st = 'DocStr(wxStyledTextCtrl::%s,\n' \ | |
856 | '"%s", "");\n' % (name, joinWithNewLines(docs)) | |
857 | dstr.append(st) | |
858 | ||
859 | # Build the method definition for the .h file | |
860 | if docs: | |
861 | defs.append('') | |
862 | for x in docs: | |
863 | defs.append(' // ' + x) | |
864 | if not theDef: | |
865 | theDef = ' %s %s(%s)' % (retType, name, params) | |
866 | if is_const: | |
867 | theDef = theDef + ' const' | |
868 | theDef = theDef + ';' | |
869 | defs.append(theDef) | |
870 | ||
871 | # Build the method implementation string | |
872 | if docs: | |
873 | imps.append('') | |
874 | for x in docs: | |
875 | imps.append('// ' + x) | |
876 | if not theImp: | |
877 | theImp = '%s wxStyledTextCtrl::%s(%s)' % (retType, name, params) | |
878 | if is_const: | |
879 | theImp = theImp + ' const' | |
880 | theImp = theImp + '\n{\n ' | |
881 | if retType == 'wxColour': | |
882 | theImp = theImp + 'long c = ' | |
883 | elif retType != 'void': | |
884 | theImp = theImp + 'return ' | |
885 | theImp = theImp + 'SendMsg(%s, %s, %s)' % (number, | |
886 | makeArgString(param1), | |
887 | makeArgString(param2)) | |
888 | if retType == 'bool': | |
889 | theImp = theImp + ' != 0' | |
890 | if retType == 'wxColour': | |
891 | theImp = theImp + ';\n return wxColourFromLong(c)' | |
892 | ||
893 | theImp = theImp + ';\n}' | |
894 | imps.append(theImp) | |
895 | ||
896 | ||
897 | return joinWithNewLines(defs), joinWithNewLines(imps), joinWithNewLines(dstr) | |
898 | ||
899 | ||
900 | #---------------------------------------------------------------------------- | |
901 | ||
902 | def checkMethodOverride(name, number, docs): | |
903 | theDef = theImp = None | |
904 | if name in methodOverrideMap: | |
905 | item = methodOverrideMap[name] | |
906 | ||
907 | try: | |
908 | if item[0] != 0: | |
909 | name = item[0] | |
910 | if item[1] != 0: | |
911 | theDef = ' ' + (item[1] % name) | |
912 | if item[2] != 0: | |
913 | theImp = item[2] % ('wxStyledTextCtrl::'+name, number) + '\n}' | |
914 | if item[3] != 0: | |
915 | docs = item[3] | |
916 | except: | |
917 | print("************* " + name) | |
918 | raise | |
919 | ||
920 | return name, theDef, theImp, docs | |
921 | ||
922 | #---------------------------------------------------------------------------- | |
923 | ||
924 | def makeArgString(param): | |
925 | if not param: | |
926 | return '0' | |
927 | ||
928 | typ, name = param | |
929 | ||
930 | if typ == 'string': | |
931 | return '(sptr_t)(const char*)wx2stc(%s)' % name | |
932 | if typ == 'colour': | |
933 | return 'wxColourAsLong(%s)' % name | |
934 | ||
935 | return name | |
936 | ||
937 | #---------------------------------------------------------------------------- | |
938 | ||
939 | def makeParamString(param1, param2): | |
940 | def doOne(param): | |
941 | if param: | |
942 | aType = paramTypeMap.get(param[0], param[0]) | |
943 | return aType + ' ' + param[1] | |
944 | else: | |
945 | return '' | |
946 | ||
947 | st = doOne(param1) | |
948 | if st and param2: | |
949 | st = st + ', ' | |
950 | st = st + doOne(param2) | |
951 | return st | |
952 | ||
953 | ||
954 | #---------------------------------------------------------------------------- | |
955 | ||
956 | def parseVal(line, values, docs): | |
957 | name, val = line.split('=') | |
958 | ||
959 | # remove prefixes such as SCI, etc. | |
960 | for old, new in valPrefixes: | |
961 | lo = len(old) | |
962 | if name[:lo] == old: | |
963 | if new is None: | |
964 | return | |
965 | name = new + name[lo:] | |
966 | ||
967 | # add it to the list | |
968 | values.append( ('wxSTC_' + name, val, docs) ) | |
969 | ||
970 | #---------------------------------------------------------------------------- | |
971 | ||
972 | funregex = re.compile(r'\s*([a-zA-Z0-9_]+)' # <ws>return type | |
973 | '\s+([a-zA-Z0-9_]+)=' # <ws>name= | |
974 | '([0-9]+)' # number | |
975 | '\(([ a-zA-Z0-9_]*),' # (param, | |
976 | '([ a-zA-Z0-9_]*),*\)') # param) | |
977 | ||
978 | def parseFun(line, methods, docs, values, is_const): | |
979 | def parseParam(param): | |
980 | param = param.strip() | |
981 | if param == '': | |
982 | param = None | |
983 | else: | |
984 | param = tuple(param.split()) | |
985 | return param | |
986 | ||
987 | mo = funregex.match(line) | |
988 | if mo is None: | |
989 | print("***** Line doesn't match! : %s" % line) | |
990 | ||
991 | retType, name, number, param1, param2 = mo.groups() | |
992 | ||
993 | param1 = parseParam(param1) | |
994 | param2 = parseParam(param2) | |
995 | ||
996 | # Special case. For the key command functions we want a value defined too | |
997 | num = int(number) | |
998 | for v in cmdValues: | |
999 | if (type(v) == type(()) and v[0] <= num <= v[1]) or v == num: | |
1000 | parseVal('CMD_%s=%s' % (name.upper(), number), values, docs) | |
1001 | ||
1002 | # if we are not also doing a function for CMD values, then | |
1003 | # just return, otherwise fall through to the append blow. | |
1004 | if not FUNC_FOR_CMD: | |
1005 | return | |
1006 | ||
1007 | methods.append( (retType, name, number, param1, param2, tuple(docs), | |
1008 | is_const or name in constNonGetterMethods) ) | |
1009 | ||
1010 | ||
1011 | #---------------------------------------------------------------------------- | |
1012 | ||
1013 | ||
1014 | def main(args): | |
1015 | # TODO: parse command line args to replace default input/output files??? | |
1016 | ||
1017 | if not os.path.exists(IFACE): | |
1018 | print('Please run this script from src/stc subdirectory.') | |
1019 | sys.exit(1) | |
1020 | ||
1021 | # Now just do it | |
1022 | processIface(IFACE, H_TEMPLATE, CPP_TEMPLATE, H_DEST, CPP_DEST, DOCSTR_DEST) | |
1023 | ||
1024 | ||
1025 | ||
1026 | if __name__ == '__main__': | |
1027 | main(sys.argv) | |
1028 | ||
1029 | #---------------------------------------------------------------------------- | |
1030 |