]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/gen_iface.py
2 #----------------------------------------------------------------------------
4 # Purpose: Generate stc.h and stc.cpp from the info in Scintilla.iface
10 # Copyright: (c) 2000 by Total Control Software
11 # Licence: wxWindows license
12 #----------------------------------------------------------------------------
15 import sys
, string
, re
, os
16 from fileinput
import FileInput
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 DOCSTR_DEST
= os
.path
.abspath('../../../wxPython/contrib/stc/_stc_gendocs.i')
27 # Value prefixes to convert
28 valPrefixes
= [('SCI_', ''),
30 ('SCN_', None), # just toss these out...
39 # Message function values that should have a CMD_ constant generated
40 cmdValues
= [ (2300, 2349),
53 # Should a funciton be also generated for the CMDs?
57 # Map some generic typenames to wx types, using return value syntax
64 # Map some generic typenames to wx types, using parameter syntax
67 'string': 'const wxString&',
68 'colour': 'const wxColour&',
72 # Map of method info that needs tweaked. Either the name needs changed, or
73 # the method definition/implementation. Tuple items are:
75 # 1. New method name. None to skip the method, 0 to leave the
77 # 2. Method definition for the .h file, 0 to leave alone
78 # 3. Method implementation for the .cpp file, 0 to leave alone.
79 # 4. tuple of Doc string lines, or 0 to leave alone.
83 'void %s(const wxString& text);',
85 '''void %s(const wxString& text) {
86 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
87 SendMsg(%s, strlen(buf), (long)(const char*)buf);''',
91 'void %s(const wxMemoryBuffer& data);',
93 '''void %s(const wxMemoryBuffer& data) {
94 SendMsg(%s, data.GetDataLen(), (long)data.GetData());''',
98 'void %s(const wxString& text);',
100 '''void %s(const wxString& text) {
101 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
102 SendMsg(%s, strlen(buf), (long)(const char*)buf);''',
105 'GetViewWS' : ( 'GetViewWhiteSpace', 0, 0, 0),
106 'SetViewWS' : ( 'SetViewWhiteSpace', 0, 0, 0),
111 return (unsigned char)SendMsg(%s, pos, 0);''',
117 return (unsigned char)SendMsg(%s, pos, 0);''',
122 'wxMemoryBuffer %s(int startPos, int endPos);',
124 '''wxMemoryBuffer %s(int startPos, int endPos) {
126 if (endPos < startPos) {
131 int len = endPos - startPos;
132 if (!len) return buf;
134 tr.lpstrText = (char*)buf.GetWriteBuf(len*2+1);
135 tr.chrg.cpMin = startPos;
136 tr.chrg.cpMax = endPos;
137 len = SendMsg(%s, 0, (long)&tr);
138 buf.UngetWriteBuf(len);
141 ('Retrieve a buffer of cells.',)),
144 'PositionFromPoint' :
146 'int %s(wxPoint pt);',
148 '''int %s(wxPoint pt) {
149 return SendMsg(%s, pt.x, pt.y);''',
154 '#ifdef SWIG\n wxString %s(int* OUTPUT);\n#else\n wxString GetCurLine(int* linePos=NULL);\n#endif',
156 '''wxString %s(int* linePos) {
157 int len = LineLength(GetCurrentLine());
159 if (linePos) *linePos = 0;
160 return wxEmptyString;
163 wxMemoryBuffer mbuf(len+1);
164 char* buf = (char*)mbuf.GetWriteBuf(len+1);
166 int pos = SendMsg(%s, len+1, (long)buf);
167 mbuf.UngetWriteBuf(len);
169 if (linePos) *linePos = pos;
170 return stc2wx(buf);''',
174 'SetUsePalette' : (None, 0,0,0),
176 'MarkerSetFore' : ('MarkerSetForeground', 0, 0, 0),
177 'MarkerSetBack' : ('MarkerSetBackground', 0, 0, 0),
181 '''void %s(int markerNumber, int markerSymbol,
182 const wxColour& foreground = wxNullColour,
183 const wxColour& background = wxNullColour);''',
185 '''void %s(int markerNumber, int markerSymbol,
186 const wxColour& foreground,
187 const wxColour& background) {
189 SendMsg(%s, markerNumber, markerSymbol);
191 MarkerSetForeground(markerNumber, foreground);
193 MarkerSetBackground(markerNumber, background);''',
195 ('Set the symbol used for a particular marker number,',
196 'and optionally the fore and background colours.')),
199 'MarkerDefinePixmap' :
200 ('MarkerDefineBitmap',
201 '''void %s(int markerNumber, const wxBitmap& bmp);''',
202 '''void %s(int markerNumber, const wxBitmap& bmp) {
203 // convert bmp to a xpm in a string
204 wxMemoryOutputStream strm;
205 wxImage img = bmp.ConvertToImage();
206 img.SaveFile(strm, wxBITMAP_TYPE_XPM);
207 size_t len = strm.GetSize();
208 char* buff = new char[len+1];
209 strm.CopyTo(buff, len);
211 SendMsg(%s, markerNumber, (long)buff);
214 ('Define a marker from a bitmap',)),
217 'SetMarginTypeN' : ('SetMarginType', 0, 0, 0),
218 'GetMarginTypeN' : ('GetMarginType', 0, 0, 0),
219 'SetMarginWidthN' : ('SetMarginWidth', 0, 0, 0),
220 'GetMarginWidthN' : ('GetMarginWidth', 0, 0, 0),
221 'SetMarginMaskN' : ('SetMarginMask', 0, 0, 0),
222 'GetMarginMaskN' : ('GetMarginMask', 0, 0, 0),
223 'SetMarginSensitiveN' : ('SetMarginSensitive', 0, 0, 0),
224 'GetMarginSensitiveN' : ('GetMarginSensitive', 0, 0, 0),
226 'StyleSetFore' : ('StyleSetForeground', 0, 0, 0),
227 'StyleSetBack' : ('StyleSetBackground', 0, 0, 0),
228 'SetSelFore' : ('SetSelForeground', 0, 0, 0),
229 'SetSelBack' : ('SetSelBackground', 0, 0, 0),
230 'SetCaretFore' : ('SetCaretForeground', 0, 0, 0),
231 'StyleSetFont' : ('StyleSetFaceName', 0, 0, 0),
235 'void %s(int key, int modifiers, int cmd);',
237 '''void %s(int key, int modifiers, int cmd) {
238 SendMsg(%s, MAKELONG(key, modifiers), cmd);''',
244 'void %s(int key, int modifiers);',
246 '''void %s(int key, int modifiers) {
247 SendMsg(%s, MAKELONG(key, modifiers));''',
250 'ClearAllCmdKeys' : ('CmdKeyClearAll', 0, 0, 0),
255 'void %s(int length, char* styleBytes);',
257 '''void %s(int length, char* styleBytes) {
258 SendMsg(%s, length, (long)styleBytes);''',
262 'IndicSetStyle' : ('IndicatorSetStyle', 0, 0, 0),
263 'IndicGetStyle' : ('IndicatorGetStyle', 0, 0, 0),
264 'IndicSetFore' : ('IndicatorSetForeground', 0, 0, 0),
265 'IndicGetFore' : ('IndicatorGetForeground', 0, 0, 0),
267 'SetWhitespaceFore' : ('SetWhitespaceForeground', 0, 0, 0),
268 'SetWhitespaceBack' : ('SetWhitespaceBackground', 0, 0, 0),
270 'AutoCShow' : ('AutoCompShow', 0, 0, 0),
271 'AutoCCancel' : ('AutoCompCancel', 0, 0, 0),
272 'AutoCActive' : ('AutoCompActive', 0, 0, 0),
273 'AutoCPosStart' : ('AutoCompPosStart', 0, 0, 0),
274 'AutoCComplete' : ('AutoCompComplete', 0, 0, 0),
275 'AutoCStops' : ('AutoCompStops', 0, 0, 0),
276 'AutoCSetSeparator' : ('AutoCompSetSeparator', 0, 0, 0),
277 'AutoCGetSeparator' : ('AutoCompGetSeparator', 0, 0, 0),
278 'AutoCSelect' : ('AutoCompSelect', 0, 0, 0),
279 'AutoCSetCancelAtStart' : ('AutoCompSetCancelAtStart', 0, 0, 0),
280 'AutoCGetCancelAtStart' : ('AutoCompGetCancelAtStart', 0, 0, 0),
281 'AutoCSetFillUps' : ('AutoCompSetFillUps', 0, 0, 0),
282 'AutoCSetChooseSingle' : ('AutoCompSetChooseSingle', 0, 0, 0),
283 'AutoCGetChooseSingle' : ('AutoCompGetChooseSingle', 0, 0, 0),
284 'AutoCSetIgnoreCase' : ('AutoCompSetIgnoreCase', 0, 0, 0),
285 'AutoCGetIgnoreCase' : ('AutoCompGetIgnoreCase', 0, 0, 0),
286 'AutoCSetAutoHide' : ('AutoCompSetAutoHide', 0, 0, 0),
287 'AutoCGetAutoHide' : ('AutoCompGetAutoHide', 0, 0, 0),
288 'AutoCSetDropRestOfWord' : ('AutoCompSetDropRestOfWord', 0,0,0),
289 'AutoCGetDropRestOfWord' : ('AutoCompGetDropRestOfWord', 0,0,0),
290 'AutoCGetTypeSeparator' : ('AutoCompGetTypeSeparator', 0, 0, 0),
291 'AutoCSetTypeSeparator' : ('AutoCompSetTypeSeparator', 0, 0, 0),
292 'AutoCGetCurrent' : ('AutoCompGetCurrent', 0, 0, 0),
296 '''void %s(int type, const wxBitmap& bmp);''',
297 '''void %s(int type, const wxBitmap& bmp) {
298 // convert bmp to a xpm in a string
299 wxMemoryOutputStream strm;
300 wxImage img = bmp.ConvertToImage();
301 img.SaveFile(strm, wxBITMAP_TYPE_XPM);
302 size_t len = strm.GetSize();
303 char* buff = new char[len+1];
304 strm.CopyTo(buff, len);
306 SendMsg(%s, type, (long)buff);
309 ('Register an image for use in autocompletion lists.',)),
312 'ClearRegisteredImages' : (0, 0, 0,
313 ('Clear all the registered images.',)),
316 'SetHScrollBar' : ('SetUseHorizontalScrollBar', 0, 0, 0),
317 'GetHScrollBar' : ('GetUseHorizontalScrollBar', 0, 0, 0),
319 'SetVScrollBar' : ('SetUseVerticalScrollBar', 0, 0, 0),
320 'GetVScrollBar' : ('GetUseVerticalScrollBar', 0, 0, 0),
322 'GetCaretFore' : ('GetCaretForeground', 0, 0, 0),
324 'GetUsePalette' : (None, 0, 0, 0),
328 '''int %s(int minPos, int maxPos, const wxString& text, int flags=0);''',
330 '''int %s(int minPos, int maxPos,
331 const wxString& text,
334 ft.chrg.cpMin = minPos;
335 ft.chrg.cpMax = maxPos;
336 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
337 ft.lpstrText = (char*)(const char*)buf;
339 return SendMsg(%s, flags, (long)&ft);''',
344 '''int %s(bool doDraw,
350 wxRect pageRect);''',
351 ''' int %s(bool doDraw,
360 if (endPos < startPos) {
366 fr.hdcTarget = target;
367 fr.rc.top = renderRect.GetTop();
368 fr.rc.left = renderRect.GetLeft();
369 fr.rc.right = renderRect.GetRight();
370 fr.rc.bottom = renderRect.GetBottom();
371 fr.rcPage.top = pageRect.GetTop();
372 fr.rcPage.left = pageRect.GetLeft();
373 fr.rcPage.right = pageRect.GetRight();
374 fr.rcPage.bottom = pageRect.GetBottom();
375 fr.chrg.cpMin = startPos;
376 fr.chrg.cpMax = endPos;
378 return SendMsg(%s, doDraw, (long)&fr);''',
384 'wxString %s(int line);',
386 '''wxString %s(int line) {
387 int len = LineLength(line);
388 if (!len) return wxEmptyString;
390 wxMemoryBuffer mbuf(len+1);
391 char* buf = (char*)mbuf.GetWriteBuf(len+1);
392 SendMsg(%s, line, (long)buf);
393 mbuf.UngetWriteBuf(len);
395 return stc2wx(buf);''',
397 ('Retrieve the contents of a line.',)),
399 'SetSel' : ('SetSelection', 0, 0, 0),
409 GetSelection(&start, &end);
410 int len = end - start;
411 if (!len) return wxEmptyString;
413 wxMemoryBuffer mbuf(len+2);
414 char* buf = (char*)mbuf.GetWriteBuf(len+1);
415 SendMsg(%s, 0, (long)buf);
416 mbuf.UngetWriteBuf(len);
418 return stc2wx(buf);''',
420 ('Retrieve the selected text.',)),
425 'wxString %s(int startPos, int endPos);',
427 '''wxString %s(int startPos, int endPos) {
428 if (endPos < startPos) {
433 int len = endPos - startPos;
434 if (!len) return wxEmptyString;
435 wxMemoryBuffer mbuf(len+1);
436 char* buf = (char*)mbuf.GetWriteBuf(len);
439 tr.chrg.cpMin = startPos;
440 tr.chrg.cpMax = endPos;
441 SendMsg(%s, 0, (long)&tr);
442 mbuf.UngetWriteBuf(len);
444 return stc2wx(buf);''',
446 ('Retrieve a range of text.',)),
448 'PointXFromPosition' : (None, 0, 0, 0),
449 'PointYFromPosition' : (None, 0, 0, 0),
451 'ScrollCaret' : ('EnsureCaretVisible', 0, 0, 0),
452 'ReplaceSel' : ('ReplaceSelection', 0, 0, 0),
453 'Null' : (None, 0, 0, 0),
460 int len = GetTextLength();
461 wxMemoryBuffer mbuf(len+1); // leave room for the null...
462 char* buf = (char*)mbuf.GetWriteBuf(len+1);
463 SendMsg(%s, len+1, (long)buf);
464 mbuf.UngetWriteBuf(len);
466 return stc2wx(buf);''',
468 ('Retrieve all the text in the document.', )),
470 'GetDirectFunction' : (None, 0, 0, 0),
471 'GetDirectPointer' : (None, 0, 0, 0),
473 'CallTipPosStart' : ('CallTipPosAtStart', 0, 0, 0),
474 'CallTipSetHlt' : ('CallTipSetHighlight', 0, 0, 0),
475 'CallTipSetBack' : ('CallTipSetBackground', 0, 0, 0),
476 'CallTipSetFore' : ('CallTipSetForeground', 0, 0, 0),
477 'CallTipSetForeHlt' : ('CallTipSetForegroundHighlight', 0, 0, 0),
479 'SetHotspotActiveFore' : ('SetHotspotActiveForeground', 0, 0, 0),
480 'SetHotspotActiveBack' : ('SetHotspotActiveBackground', 0, 0, 0),
485 'int %s(const wxString& text);',
488 int %s(const wxString& text) {
489 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
490 return SendMsg(%s, strlen(buf), (long)(const char*)buf);''',
495 'int %s(const wxString& text);',
498 int %s(const wxString& text) {
499 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
500 return SendMsg(%s, strlen(buf), (long)(const char*)buf);''',
505 'int %s(const wxString& text);',
508 int %s(const wxString& text) {
509 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
510 return SendMsg(%s, strlen(buf), (long)(const char*)buf);''',
513 # not sure what to do about these yet
514 'TargetAsUTF8' : ( None, 0, 0, 0),
515 'SetLengthForEncode' : ( None, 0, 0, 0),
516 'EncodedFromUTF8' : ( None, 0, 0, 0),
523 return (void*)SendMsg(%s);''',
528 'void %s(void* docPointer);',
529 '''void %s(void* docPointer) {
530 SendMsg(%s, 0, (long)docPointer);''',
537 return (void*)SendMsg(%s);''',
542 'void %s(void* docPointer);',
543 '''void %s(void* docPointer) {
544 SendMsg(%s, 0, (long)docPointer);''',
549 'void %s(void* docPointer);',
550 '''void %s(void* docPointer) {
551 SendMsg(%s, 0, (long)docPointer);''',
557 '''void %s(int codePage) {
559 wxASSERT_MSG(codePage == wxSTC_CP_UTF8,
560 wxT("Only wxSTC_CP_UTF8 may be used when wxUSE_UNICODE is on."));
562 wxASSERT_MSG(codePage != wxSTC_CP_UTF8,
563 wxT("wxSTC_CP_UTF8 may not be used when wxUSE_UNICODE is off."));
565 SendMsg(%s, codePage);''',
566 ("Set the code page used to interpret the bytes of the document as characters.",) ),
569 'GrabFocus' : (None, 0, 0, 0),
571 # Rename some that would otherwise hide the wxWindow methods
572 'SetFocus' : ('SetSTCFocus', 0, 0, 0),
573 'GetFocus' : ('GetSTCFocus', 0, 0, 0),
574 'SetCursor' : ('SetSTCCursor', 0, 0, 0),
575 'GetCursor' : ('GetSTCCursor', 0, 0, 0),
577 'LoadLexerLibrary' : (None, 0,0,0),
584 #----------------------------------------------------------------------------
586 def processIface(iface
, h_tmplt
, cpp_tmplt
, h_dest
, cpp_dest
, docstr_dest
):
593 fi
= FileInput(iface
)
596 if line
[:2] == '##' or line
== '':
601 if line
[:2] == '# ': # a doc string
602 curDocStrings
.append(line
[2:])
605 parseVal(line
[4:], values
, curDocStrings
)
608 elif op
== 'fun ' or op
== 'set ' or op
== 'get ':
609 parseFun(line
[4:], methods
, curDocStrings
, cmds
)
613 if string
.strip(line
[4:]) == 'Deprecated':
614 break # skip the rest of the file
626 print '***** Unknown line type: ', line
631 data
['VALUES'] = processVals(values
)
632 data
['CMDS'] = processVals(cmds
)
633 defs
, imps
, docstrings
= processMethods(methods
)
634 data
['METHOD_DEFS'] = defs
635 data
['METHOD_IMPS'] = imps
638 h_text
= open(h_tmplt
).read()
639 cpp_text
= open(cpp_tmplt
).read()
641 # do the substitutions
642 h_text
= h_text
% data
643 cpp_text
= cpp_text
% data
645 # write out destination files
646 open(h_dest
, 'w').write(h_text
)
647 open(cpp_dest
, 'w').write(cpp_text
)
648 open(docstr_dest
, 'w').write(docstrings
)
652 #----------------------------------------------------------------------------
654 def processVals(values
):
656 for name
, value
, docs
in values
:
660 text
.append('// ' + x
)
661 text
.append('#define %s %s' % (name
, value
))
662 return string
.join(text
, '\n')
664 #----------------------------------------------------------------------------
666 def processMethods(methods
):
671 for retType
, name
, number
, param1
, param2
, docs
in methods
:
672 retType
= retTypeMap
.get(retType
, retType
)
673 params
= makeParamString(param1
, param2
)
675 name
, theDef
, theImp
, docs
= checkMethodOverride(name
, number
, docs
)
681 st
= 'DocStr(wxStyledTextCtrl::%s,\n' \
682 '"%s", "");\n' % (name
, '\n'.join(docs
))
685 # Build the method definition for the .h file
689 defs
.append(' // ' + x
)
691 theDef
= ' %s %s(%s);' % (retType
, name
, params
)
694 # Build the method implementation string
698 imps
.append('// ' + x
)
700 theImp
= '%s wxStyledTextCtrl::%s(%s) {\n ' % (retType
, name
, params
)
702 if retType
== 'wxColour':
703 theImp
= theImp
+ 'long c = '
704 elif retType
!= 'void':
705 theImp
= theImp
+ 'return '
706 theImp
= theImp
+ 'SendMsg(%s, %s, %s)' % (number
,
707 makeArgString(param1
),
708 makeArgString(param2
))
709 if retType
== 'bool':
710 theImp
= theImp
+ ' != 0'
711 if retType
== 'wxColour':
712 theImp
= theImp
+ ';\n return wxColourFromLong(c)'
714 theImp
= theImp
+ ';\n}'
718 return '\n'.join(defs
), '\n'.join(imps
), '\n'.join(dstr
)
721 #----------------------------------------------------------------------------
723 def checkMethodOverride(name
, number
, docs
):
724 theDef
= theImp
= None
725 if methodOverrideMap
.has_key(name
):
726 item
= methodOverrideMap
[name
]
732 theDef
= ' ' + (item
[1] % name
)
734 theImp
= item
[2] % ('wxStyledTextCtrl::'+name
, number
) + '\n}'
738 print "*************", name
741 return name
, theDef
, theImp
, docs
743 #----------------------------------------------------------------------------
745 def makeArgString(param
):
752 return '(long)(const char*)wx2stc(%s)' % name
754 return 'wxColourAsLong(%s)' % name
758 #----------------------------------------------------------------------------
760 def makeParamString(param1
, param2
):
763 aType
= paramTypeMap
.get(param
[0], param
[0])
764 return aType
+ ' ' + param
[1]
771 st
= st
+ doOne(param2
)
775 #----------------------------------------------------------------------------
777 def parseVal(line
, values
, docs
):
778 name
, val
= string
.split(line
, '=')
780 # remove prefixes such as SCI, etc.
781 for old
, new
in valPrefixes
:
786 name
= new
+ name
[lo
:]
789 values
.append( ('wxSTC_' + name
, val
, docs
) )
791 #----------------------------------------------------------------------------
793 funregex
= re
.compile(r
'\s*([a-zA-Z0-9_]+)' # <ws>return type
794 '\s+([a-zA-Z0-9_]+)=' # <ws>name=
796 '\(([ a-zA-Z0-9_]*),' # (param,
797 '([ a-zA-Z0-9_]*)\)') # param)
799 def parseFun(line
, methods
, docs
, values
):
800 def parseParam(param
):
801 param
= string
.strip(param
)
805 param
= tuple(string
.split(param
))
808 mo
= funregex
.match(line
)
810 print "***** Line doesn't match! : " + line
812 retType
, name
, number
, param1
, param2
= mo
.groups()
814 param1
= parseParam(param1
)
815 param2
= parseParam(param2
)
817 # Special case. For the key command functions we want a value defined too
818 num
= string
.atoi(number
)
820 if (type(v
) == type(()) and v
[0] <= num
<= v
[1]) or v
== num
:
821 parseVal('CMD_%s=%s' % (string
.upper(name
), number
), values
, docs
)
823 # if we are not also doing a function for CMD values, then
824 # just return, otherwise fall through to the append blow.
828 methods
.append( (retType
, name
, number
, param1
, param2
, tuple(docs
)) )
831 #----------------------------------------------------------------------------
835 # TODO: parse command line args to replace default input/output files???
838 processIface(IFACE
, H_TEMPLATE
, CPP_TEMPLATE
, H_DEST
, CPP_DEST
, DOCSTR_DEST
)
842 if __name__
== '__main__':
845 #----------------------------------------------------------------------------