]>
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')
26 # Value prefixes to convert
27 valPrefixes
= [('SCI_', ''),
29 ('SCN_', None), # just toss these out...
38 # Message function values that should have a CMD_ constant generated
39 cmdValues
= [ (2300, 2349),
52 # Should a funciton be also generated for the CMDs?
56 # Map some generic typenames to wx types, using return value syntax
63 # Map some generic typenames to wx types, using parameter syntax
66 'string': 'const wxString&',
67 'colour': 'const wxColour&',
71 # Map of method info that needs tweaked. Either the name needs changed, or
72 # the method definition/implementation. Tuple items are:
74 # 1. New method name. None to skip the method, 0 to leave the
76 # 2. Method definition for the .h file, 0 to leave alone
77 # 3. Method implementation for the .cpp file, 0 to leave alone.
78 # 4. tuple of Doc string lines, or 0 to leave alone.
82 'void %s(const wxString& text);',
84 '''void %s(const wxString& text) {
85 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
86 SendMsg(%s, strlen(buf), (long)(const char*)buf);''',
90 'void %s(const wxMemoryBuffer& data);',
92 '''void %s(const wxMemoryBuffer& data) {
93 SendMsg(%s, data.GetDataLen(), (long)data.GetData());''',
96 'GetViewWS' : ( 'GetViewWhiteSpace', 0, 0, 0),
97 'SetViewWS' : ( 'SetViewWhiteSpace', 0, 0, 0),
102 return (unsigned char)SendMsg(%s, pos, 0);''',
108 return (unsigned char)SendMsg(%s, pos, 0);''',
113 'wxMemoryBuffer %s(int startPos, int endPos);',
115 '''wxMemoryBuffer %s(int startPos, int endPos) {
117 if (endPos < startPos) {
122 int len = endPos - startPos;
123 if (!len) return buf;
125 tr.lpstrText = (char*)buf.GetWriteBuf(len*2+1);
126 tr.chrg.cpMin = startPos;
127 tr.chrg.cpMax = endPos;
128 len = SendMsg(%s, 0, (long)&tr);
129 buf.UngetWriteBuf(len);
132 ('Retrieve a buffer of cells.',)),
135 'PositionFromPoint' :
137 'int %s(wxPoint pt);',
139 '''int %s(wxPoint pt) {
140 return SendMsg(%s, pt.x, pt.y);''',
145 '#ifdef SWIG\n wxString %s(int* OUTPUT);\n#else\n wxString GetCurLine(int* linePos=NULL);\n#endif',
147 '''wxString %s(int* linePos) {
148 int len = LineLength(GetCurrentLine());
150 if (linePos) *linePos = 0;
151 return wxEmptyString;
154 wxMemoryBuffer mbuf(len+1);
155 char* buf = (char*)mbuf.GetWriteBuf(len+1);
157 int pos = SendMsg(%s, len+1, (long)buf);
158 mbuf.UngetWriteBuf(len);
160 if (linePos) *linePos = pos;
161 return stc2wx(buf);''',
165 'SetUsePalette' : (None, 0,0,0),
167 'MarkerSetFore' : ('MarkerSetForeground', 0, 0, 0),
168 'MarkerSetBack' : ('MarkerSetBackground', 0, 0, 0),
172 '''void %s(int markerNumber, int markerSymbol,
173 const wxColour& foreground = wxNullColour,
174 const wxColour& background = wxNullColour);''',
176 '''void %s(int markerNumber, int markerSymbol,
177 const wxColour& foreground,
178 const wxColour& background) {
180 SendMsg(%s, markerNumber, markerSymbol);
182 MarkerSetForeground(markerNumber, foreground);
184 MarkerSetBackground(markerNumber, background);''',
186 ('Set the symbol used for a particular marker number,',
187 'and optionally the fore and background colours.')),
190 'MarkerDefinePixmap' :
191 ('MarkerDefineBitmap',
192 '''void %s(int markerNumber, const wxBitmap& bmp);''',
193 '''void %s(int markerNumber, const wxBitmap& bmp) {
194 // convert bmp to a xpm in a string
195 wxMemoryOutputStream strm;
196 wxImage img = bmp.ConvertToImage();
197 img.SaveFile(strm, wxBITMAP_TYPE_XPM);
198 size_t len = strm.GetSize();
199 char* buff = new char[len+1];
200 strm.CopyTo(buff, len);
202 SendMsg(%s, markerNumber, (long)buff);
205 ('Define a marker from a bitmap',)),
208 'SetMarginTypeN' : ('SetMarginType', 0, 0, 0),
209 'GetMarginTypeN' : ('GetMarginType', 0, 0, 0),
210 'SetMarginWidthN' : ('SetMarginWidth', 0, 0, 0),
211 'GetMarginWidthN' : ('GetMarginWidth', 0, 0, 0),
212 'SetMarginMaskN' : ('SetMarginMask', 0, 0, 0),
213 'GetMarginMaskN' : ('GetMarginMask', 0, 0, 0),
214 'SetMarginSensitiveN' : ('SetMarginSensitive', 0, 0, 0),
215 'GetMarginSensitiveN' : ('GetMarginSensitive', 0, 0, 0),
217 'StyleSetFore' : ('StyleSetForeground', 0, 0, 0),
218 'StyleSetBack' : ('StyleSetBackground', 0, 0, 0),
219 'SetSelFore' : ('SetSelForeground', 0, 0, 0),
220 'SetSelBack' : ('SetSelBackground', 0, 0, 0),
221 'SetCaretFore' : ('SetCaretForeground', 0, 0, 0),
222 'StyleSetFont' : ('StyleSetFaceName', 0, 0, 0),
226 'void %s(int key, int modifiers, int cmd);',
228 '''void %s(int key, int modifiers, int cmd) {
229 SendMsg(%s, MAKELONG(key, modifiers), cmd);''',
235 'void %s(int key, int modifiers);',
237 '''void %s(int key, int modifiers) {
238 SendMsg(%s, MAKELONG(key, modifiers));''',
241 'ClearAllCmdKeys' : ('CmdKeyClearAll', 0, 0, 0),
246 'void %s(int length, char* styleBytes);',
248 '''void %s(int length, char* styleBytes) {
249 SendMsg(%s, length, (long)styleBytes);''',
253 'IndicSetStyle' : ('IndicatorSetStyle', 0, 0, 0),
254 'IndicGetStyle' : ('IndicatorGetStyle', 0, 0, 0),
255 'IndicSetFore' : ('IndicatorSetForeground', 0, 0, 0),
256 'IndicGetFore' : ('IndicatorGetForeground', 0, 0, 0),
258 'SetWhitespaceFore' : ('SetWhitespaceForeground', 0, 0, 0),
259 'SetWhitespaceBack' : ('SetWhitespaceBackground', 0, 0, 0),
261 'AutoCShow' : ('AutoCompShow', 0, 0, 0),
262 'AutoCCancel' : ('AutoCompCancel', 0, 0, 0),
263 'AutoCActive' : ('AutoCompActive', 0, 0, 0),
264 'AutoCPosStart' : ('AutoCompPosStart', 0, 0, 0),
265 'AutoCComplete' : ('AutoCompComplete', 0, 0, 0),
266 'AutoCStops' : ('AutoCompStops', 0, 0, 0),
267 'AutoCSetSeparator' : ('AutoCompSetSeparator', 0, 0, 0),
268 'AutoCGetSeparator' : ('AutoCompGetSeparator', 0, 0, 0),
269 'AutoCSelect' : ('AutoCompSelect', 0, 0, 0),
270 'AutoCSetCancelAtStart' : ('AutoCompSetCancelAtStart', 0, 0, 0),
271 'AutoCGetCancelAtStart' : ('AutoCompGetCancelAtStart', 0, 0, 0),
272 'AutoCSetFillUps' : ('AutoCompSetFillUps', 0, 0, 0),
273 'AutoCSetChooseSingle' : ('AutoCompSetChooseSingle', 0, 0, 0),
274 'AutoCGetChooseSingle' : ('AutoCompGetChooseSingle', 0, 0, 0),
275 'AutoCSetIgnoreCase' : ('AutoCompSetIgnoreCase', 0, 0, 0),
276 'AutoCGetIgnoreCase' : ('AutoCompGetIgnoreCase', 0, 0, 0),
277 'AutoCSetAutoHide' : ('AutoCompSetAutoHide', 0, 0, 0),
278 'AutoCGetAutoHide' : ('AutoCompGetAutoHide', 0, 0, 0),
279 'AutoCSetDropRestOfWord' : ('AutoCompSetDropRestOfWord', 0,0,0),
280 'AutoCGetDropRestOfWord' : ('AutoCompGetDropRestOfWord', 0,0,0),
281 'AutoCGetTypeSeparator' : ('AutoCompGetTypeSeparator', 0, 0, 0),
282 'AutoCSetTypeSeparator' : ('AutoCompSetTypeSeparator', 0, 0, 0),
283 'AutoCGetCurrent' : ('AutoCompGetCurrent', 0, 0, 0),
287 '''void %s(int type, const wxBitmap& bmp);''',
288 '''void %s(int type, const wxBitmap& bmp) {
289 // convert bmp to a xpm in a string
290 wxMemoryOutputStream strm;
291 wxImage img = bmp.ConvertToImage();
292 img.SaveFile(strm, wxBITMAP_TYPE_XPM);
293 size_t len = strm.GetSize();
294 char* buff = new char[len+1];
295 strm.CopyTo(buff, len);
297 SendMsg(%s, type, (long)buff);
300 ('Register an image for use in autocompletion lists.',)),
303 'ClearRegisteredImages' : (0, 0, 0,
304 ('Clear all the registered images.',)),
307 'SetHScrollBar' : ('SetUseHorizontalScrollBar', 0, 0, 0),
308 'GetHScrollBar' : ('GetUseHorizontalScrollBar', 0, 0, 0),
310 'SetVScrollBar' : ('SetUseVerticalScrollBar', 0, 0, 0),
311 'GetVScrollBar' : ('GetUseVerticalScrollBar', 0, 0, 0),
313 'GetCaretFore' : ('GetCaretForeground', 0, 0, 0),
315 'GetUsePalette' : (None, 0, 0, 0),
319 '''int %s(int minPos, int maxPos, const wxString& text, int flags=0);''',
321 '''int %s(int minPos, int maxPos,
322 const wxString& text,
325 ft.chrg.cpMin = minPos;
326 ft.chrg.cpMax = maxPos;
327 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
328 ft.lpstrText = (char*)(const char*)buf;
330 return SendMsg(%s, flags, (long)&ft);''',
335 '''int %s(bool doDraw,
341 wxRect pageRect);''',
342 ''' int %s(bool doDraw,
351 if (endPos < startPos) {
357 fr.hdcTarget = target;
358 fr.rc.top = renderRect.GetTop();
359 fr.rc.left = renderRect.GetLeft();
360 fr.rc.right = renderRect.GetRight();
361 fr.rc.bottom = renderRect.GetBottom();
362 fr.rcPage.top = pageRect.GetTop();
363 fr.rcPage.left = pageRect.GetLeft();
364 fr.rcPage.right = pageRect.GetRight();
365 fr.rcPage.bottom = pageRect.GetBottom();
366 fr.chrg.cpMin = startPos;
367 fr.chrg.cpMax = endPos;
369 return SendMsg(%s, doDraw, (long)&fr);''',
375 'wxString %s(int line);',
377 '''wxString %s(int line) {
378 int len = LineLength(line);
379 if (!len) return wxEmptyString;
381 wxMemoryBuffer mbuf(len+1);
382 char* buf = (char*)mbuf.GetWriteBuf(len+1);
383 SendMsg(%s, line, (long)buf);
384 mbuf.UngetWriteBuf(len);
386 return stc2wx(buf);''',
388 ('Retrieve the contents of a line.',)),
390 'SetSel' : ('SetSelection', 0, 0, 0),
400 GetSelection(&start, &end);
401 int len = end - start;
402 if (!len) return wxEmptyString;
404 wxMemoryBuffer mbuf(len+2);
405 char* buf = (char*)mbuf.GetWriteBuf(len+1);
406 SendMsg(%s, 0, (long)buf);
407 mbuf.UngetWriteBuf(len);
409 return stc2wx(buf);''',
411 ('Retrieve the selected text.',)),
416 'wxString %s(int startPos, int endPos);',
418 '''wxString %s(int startPos, int endPos) {
419 if (endPos < startPos) {
424 int len = endPos - startPos;
425 if (!len) return wxEmptyString;
426 wxMemoryBuffer mbuf(len+1);
427 char* buf = (char*)mbuf.GetWriteBuf(len);
430 tr.chrg.cpMin = startPos;
431 tr.chrg.cpMax = endPos;
432 SendMsg(%s, 0, (long)&tr);
433 mbuf.UngetWriteBuf(len);
435 return stc2wx(buf);''',
437 ('Retrieve a range of text.',)),
439 'PointXFromPosition' : (None, 0, 0, 0),
440 'PointYFromPosition' : (None, 0, 0, 0),
442 'ScrollCaret' : ('EnsureCaretVisible', 0, 0, 0),
443 'ReplaceSel' : ('ReplaceSelection', 0, 0, 0),
444 'Null' : (None, 0, 0, 0),
451 int len = GetTextLength();
452 wxMemoryBuffer mbuf(len+1); // leave room for the null...
453 char* buf = (char*)mbuf.GetWriteBuf(len+1);
454 SendMsg(%s, len+1, (long)buf);
455 mbuf.UngetWriteBuf(len);
457 return stc2wx(buf);''',
459 ('Retrieve all the text in the document.', )),
461 'GetDirectFunction' : (None, 0, 0, 0),
462 'GetDirectPointer' : (None, 0, 0, 0),
464 'CallTipPosStart' : ('CallTipPosAtStart', 0, 0, 0),
465 'CallTipSetHlt' : ('CallTipSetHighlight', 0, 0, 0),
466 'CallTipSetBack' : ('CallTipSetBackground', 0, 0, 0),
467 'CallTipSetFore' : ('CallTipSetForeground', 0, 0, 0),
468 'CallTipSetForeHlt' : ('CallTipSetForegroundHighlight', 0, 0, 0),
470 'SetHotspotActiveFore' : ('SetHotspotActiveForeground', 0, 0, 0),
471 'SetHotspotActiveBack' : ('SetHotspotActiveBackground', 0, 0, 0),
476 'int %s(const wxString& text);',
479 int %s(const wxString& text) {
480 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
481 return SendMsg(%s, strlen(buf), (long)(const char*)buf);''',
486 'int %s(const wxString& text);',
489 int %s(const wxString& text) {
490 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
491 return SendMsg(%s, strlen(buf), (long)(const char*)buf);''',
496 'int %s(const wxString& text);',
499 int %s(const wxString& text) {
500 wxWX2MBbuf buf = (wxWX2MBbuf)wx2stc(text);
501 return SendMsg(%s, strlen(buf), (long)(const char*)buf);''',
509 return (void*)SendMsg(%s);''',
514 'void %s(void* docPointer);',
515 '''void %s(void* docPointer) {
516 SendMsg(%s, 0, (long)docPointer);''',
523 return (void*)SendMsg(%s);''',
528 'void %s(void* docPointer);',
529 '''void %s(void* docPointer) {
530 SendMsg(%s, 0, (long)docPointer);''',
535 'void %s(void* docPointer);',
536 '''void %s(void* docPointer) {
537 SendMsg(%s, 0, (long)docPointer);''',
543 '''void %s(int codePage) {
545 wxASSERT_MSG(codePage == wxSTC_CP_UTF8,
546 wxT("Only wxSTC_CP_UTF8 may be used when wxUSE_UNICODE is on."));
548 wxASSERT_MSG(codePage != wxSTC_CP_UTF8,
549 wxT("wxSTC_CP_UTF8 may not be used when wxUSE_UNICODE is off."));
551 SendMsg(%s, codePage);''',
552 ("Set the code page used to interpret the bytes of the document as characters.",) ),
555 'GrabFocus' : (None, 0, 0, 0),
557 # Rename some that would otherwise hide the wxWindow methods
558 'SetFocus' : ('SetSTCFocus', 0, 0, 0),
559 'GetFocus' : ('GetSTCFocus', 0, 0, 0),
560 'SetCursor' : ('SetSTCCursor', 0, 0, 0),
561 'GetCursor' : ('GetSTCCursor', 0, 0, 0),
563 'LoadLexerLibrary' : (None, 0,0,0),
570 #----------------------------------------------------------------------------
572 def processIface(iface
, h_tmplt
, cpp_tmplt
, h_dest
, cpp_dest
):
579 fi
= FileInput(iface
)
582 if line
[:2] == '##' or line
== '':
587 if line
[:2] == '# ': # a doc string
588 curDocStrings
.append(line
[2:])
591 parseVal(line
[4:], values
, curDocStrings
)
594 elif op
== 'fun ' or op
== 'set ' or op
== 'get ':
595 parseFun(line
[4:], methods
, curDocStrings
, cmds
)
599 if string
.strip(line
[4:]) == 'Deprecated':
600 break # skip the rest of the file
612 print '***** Unknown line type: ', line
617 data
['VALUES'] = processVals(values
)
618 data
['CMDS'] = processVals(cmds
)
619 defs
, imps
= processMethods(methods
)
620 data
['METHOD_DEFS'] = defs
621 data
['METHOD_IMPS'] = imps
624 h_text
= open(h_tmplt
).read()
625 cpp_text
= open(cpp_tmplt
).read()
627 # do the substitutions
628 h_text
= h_text
% data
629 cpp_text
= cpp_text
% data
631 # write out destination files
632 open(h_dest
, 'w').write(h_text
)
633 open(cpp_dest
, 'w').write(cpp_text
)
637 #----------------------------------------------------------------------------
639 def processVals(values
):
641 for name
, value
, docs
in values
:
645 text
.append('// ' + x
)
646 text
.append('#define %s %s' % (name
, value
))
647 return string
.join(text
, '\n')
649 #----------------------------------------------------------------------------
651 def processMethods(methods
):
655 for retType
, name
, number
, param1
, param2
, docs
in methods
:
656 retType
= retTypeMap
.get(retType
, retType
)
657 params
= makeParamString(param1
, param2
)
659 name
, theDef
, theImp
, docs
= checkMethodOverride(name
, number
, docs
)
664 # Build the method definition for the .h file
668 defs
.append(' // ' + x
)
670 theDef
= ' %s %s(%s);' % (retType
, name
, params
)
673 # Build the method implementation string
677 imps
.append('// ' + x
)
679 theImp
= '%s wxStyledTextCtrl::%s(%s) {\n ' % (retType
, name
, params
)
681 if retType
== 'wxColour':
682 theImp
= theImp
+ 'long c = '
683 elif retType
!= 'void':
684 theImp
= theImp
+ 'return '
685 theImp
= theImp
+ 'SendMsg(%s, %s, %s)' % (number
,
686 makeArgString(param1
),
687 makeArgString(param2
))
688 if retType
== 'bool':
689 theImp
= theImp
+ ' != 0'
690 if retType
== 'wxColour':
691 theImp
= theImp
+ ';\n return wxColourFromLong(c)'
693 theImp
= theImp
+ ';\n}'
697 return string
.join(defs
, '\n'), string
.join(imps
, '\n')
700 #----------------------------------------------------------------------------
702 def checkMethodOverride(name
, number
, docs
):
703 theDef
= theImp
= None
704 if methodOverrideMap
.has_key(name
):
705 item
= methodOverrideMap
[name
]
711 theDef
= ' ' + (item
[1] % name
)
713 theImp
= item
[2] % ('wxStyledTextCtrl::'+name
, number
) + '\n}'
717 print "*************", name
720 return name
, theDef
, theImp
, docs
722 #----------------------------------------------------------------------------
724 def makeArgString(param
):
731 return '(long)(const char*)wx2stc(%s)' % name
733 return 'wxColourAsLong(%s)' % name
737 #----------------------------------------------------------------------------
739 def makeParamString(param1
, param2
):
742 aType
= paramTypeMap
.get(param
[0], param
[0])
743 return aType
+ ' ' + param
[1]
750 st
= st
+ doOne(param2
)
754 #----------------------------------------------------------------------------
756 def parseVal(line
, values
, docs
):
757 name
, val
= string
.split(line
, '=')
759 # remove prefixes such as SCI, etc.
760 for old
, new
in valPrefixes
:
765 name
= new
+ name
[lo
:]
768 values
.append( ('wxSTC_' + name
, val
, docs
) )
770 #----------------------------------------------------------------------------
772 funregex
= re
.compile(r
'\s*([a-zA-Z0-9_]+)' # <ws>return type
773 '\s+([a-zA-Z0-9_]+)=' # <ws>name=
775 '\(([ a-zA-Z0-9_]*),' # (param,
776 '([ a-zA-Z0-9_]*)\)') # param)
778 def parseFun(line
, methods
, docs
, values
):
779 def parseParam(param
):
780 param
= string
.strip(param
)
784 param
= tuple(string
.split(param
))
787 mo
= funregex
.match(line
)
789 print "***** Line doesn't match! : " + line
791 retType
, name
, number
, param1
, param2
= mo
.groups()
793 param1
= parseParam(param1
)
794 param2
= parseParam(param2
)
796 # Special case. For the key command functions we want a value defined too
797 num
= string
.atoi(number
)
799 if (type(v
) == type(()) and v
[0] <= num
<= v
[1]) or v
== num
:
800 parseVal('CMD_%s=%s' % (string
.upper(name
), number
), values
, docs
)
802 # if we are not also doing a function for CMD values, then
803 # just return, otherwise fall through to the append blow.
807 methods
.append( (retType
, name
, number
, param1
, param2
, tuple(docs
)) )
810 #----------------------------------------------------------------------------
814 # TODO: parse command line args to replace default input/output files???
817 processIface(IFACE
, H_TEMPLATE
, CPP_TEMPLATE
, H_DEST
, CPP_DEST
)
821 if __name__
== '__main__':
824 #----------------------------------------------------------------------------