A couple off-by-one fixes
[wxWidgets.git] / src / stc / gen_iface.py
1 #!/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 license
12 #----------------------------------------------------------------------------
13
14
15 import sys, string, re
16 from fileinput import FileInput
17
18
19 IFACE = './scintilla/include/Scintilla.iface'
20 H_TEMPLATE = './stc.h.in'
21 CPP_TEMPLATE = './stc.cpp.in'
22 H_DEST = '../../include/wx/stc/stc.h'
23 CPP_DEST = './stc.cpp'
24
25
26 # Value prefixes to convert
27 valPrefixes = [('SCI_', ''),
28 ('SC_', ''),
29 ('SCN_', None), # just toss these out...
30 ('SCEN_', None),
31 ('SCE_', ''),
32 ('SCLEX_', 'LEX_'),
33 ('SCK_', 'KEY_'),
34 ('SCFIND_', 'FIND_'),
35 ('SCWS_', 'WS_'),
36 ]
37
38 # Message function values that should have a CMD_ constant as well
39 cmdValues = [ (2300, 2350), 2011, 2013, (2176, 2180) ]
40
41
42 # Map some generic typenames to wx types, using return value syntax
43 retTypeMap = {
44 'position': 'int',
45 'string': 'wxString',
46 'colour': 'wxColour',
47 }
48
49 # Map some generic typenames to wx types, using parameter syntax
50 paramTypeMap = {
51 'position': 'int',
52 'string': 'const wxString&',
53 'colour': 'const wxColour&',
54 'keymod': 'int',
55 }
56
57 # Map of method info that needs tweaked. Either the name needs changed, or
58 # the method definition/implementation. Tuple items are:
59 #
60 # 1. New method name. None to skip the method, 0 to leave the
61 # default name.
62 # 2. Method definition for the .h file, 0 to leave alone
63 # 3. Method implementation for the .cpp file, 0 to leave alone.
64 # 4. tuple of Doc string lines, or 0 to leave alone.
65 #
66 methodOverrideMap = {
67 'AddText' : (0,
68 'void %s(const wxString& text);',
69
70 '''void %s(const wxString& text) {
71 SendMsg(%s, text.Len(), (long)text.c_str());''',
72 0),
73
74 'AddStyledText' : (0,
75 'void %s(const wxString& text);',
76
77 '''void %s(const wxString& text) {
78 SendMsg(%s, text.Len(), (long)text.c_str());''',
79 0),
80
81 'GetViewWS' : ( 'GetViewWhiteSpace', 0, 0, 0),
82 'SetViewWS' : ( 'SetViewWhiteSpace', 0, 0, 0),
83
84 'GetStyledText' : (0,
85 'wxString %s(int startPos, int endPos);',
86
87 '''wxString %s(int startPos, int endPos) {
88 wxString text;
89 int len = endPos - startPos;
90 TextRange tr;
91 tr.lpstrText = text.GetWriteBuf(len*2+1);
92 tr.chrg.cpMin = startPos;
93 tr.chrg.cpMax = endPos;
94 SendMsg(%s, 0, (long)&tr);
95 text.UngetWriteBuf(len*2);
96 return text;''',
97
98 ('Retrieve a buffer of cells.',)),
99
100
101 'PositionFromPoint' : (0,
102 'int %s(wxPoint pt);',
103
104 '''int %s(wxPoint pt) {
105 return SendMsg(%s, pt.x, pt.y);''',
106
107 0),
108
109 'GetCurLine' : (0,
110 'wxString %s(int* OUTPUT=NULL);',
111
112 '''wxString %s(int* linePos) {
113 wxString text;
114 int len = LineLength(GetCurrentLine());
115 char* buf = text.GetWriteBuf(len);
116
117 int pos = SendMsg(%s, len, (long)buf);
118 text.UngetWriteBuf();
119 if (linePos) *linePos = pos;
120
121 return text;''',
122
123 0),
124
125 'SetUsePalette' : (None, 0,0,0),
126
127 'MarkerSetFore' : ('MarkerSetForeground', 0, 0, 0),
128 'MarkerSetBack' : ('MarkerSetBackground', 0, 0, 0),
129
130 'MarkerDefine' : (0,
131 '''void %s(int markerNumber, int markerSymbol,
132 const wxColour& foreground = wxNullColour,
133 const wxColour& background = wxNullColour);''',
134
135 '''void %s(int markerNumber, int markerSymbol,
136 const wxColour& foreground,
137 const wxColour& background) {
138
139 SendMsg(%s, markerNumber, markerSymbol);
140 if (foreground.Ok())
141 MarkerSetForeground(markerNumber, foreground);
142 if (background.Ok())
143 MarkerSetBackground(markerNumber, background);''',
144
145 ('Set the symbol used for a particular marker number,',
146 'and optionally the for and background colours.')),
147
148 'SetMarginTypeN' : ('SetMarginType', 0, 0, 0),
149 'GetMarginTypeN' : ('GetMarginType', 0, 0, 0),
150 'SetMarginWidthN' : ('SetMarginWidth', 0, 0, 0),
151 'GetMarginWidthN' : ('GetMarginWidth', 0, 0, 0),
152 'SetMarginMaskN' : ('SetMarginMask', 0, 0, 0),
153 'GetMarginMaskN' : ('GetMarginMask', 0, 0, 0),
154 'SetMarginSensitiveN' : ('SetMarginSensitive', 0, 0, 0),
155 'GetMarginSensitiveN' : ('GetMarginSensitive', 0, 0, 0),
156
157 'StyleSetFore' : ('StyleSetForeground', 0, 0, 0),
158 'StyleSetBack' : ('StyleSetBackground', 0, 0, 0),
159 'SetSelFore' : ('SetSelForeground', 0, 0, 0),
160 'SetSelBack' : ('SetSelBackground', 0, 0, 0),
161 'SetCaretFore' : ('SetCaretForeground', 0, 0, 0),
162 'StyleSetFont' : ('StyleSetFaceName', 0, 0, 0),
163
164 # need to fix this to map between wx and scintilla encoding flags, leave it out for now...
165 'StyleSetCharacterSet' : (None, 0, 0, 0),
166
167 'AssignCmdKey' : ('CmdKeyAssign',
168 'void %s(int key, int modifiers, int cmd);',
169
170 '''void %s(int key, int modifiers, int cmd) {
171 SendMsg(%s, MAKELONG(key, modifiers), cmd);''',
172
173 0),
174
175 'ClearCmdKey' : ('CmdKeyClear',
176 'void %s(int key, int modifiers);',
177
178 '''void %s(int key, int modifiers) {
179 SendMsg(%s, MAKELONG(key, modifiers));''',
180
181 0),
182
183 'ClearAllCmdKeys' : ('CmdKeyClearAll', 0, 0, 0),
184
185
186 'SetStylingEx' : ('SetStyleBytes',
187 'void %s(int length, char* styleBytes);',
188
189 '''void %s(int length, char* styleBytes) {
190 SendMsg(%s, length, (long)styleBytes);''',
191
192 0),
193
194
195 'IndicSetStyle' : ('IndicatorSetStyle', 0, 0, 0),
196 'IndicGetStyle' : ('IndicatorGetStyle', 0, 0, 0),
197 'IndicSetFore' : ('IndicatorSetForeground', 0, 0, 0),
198 'IndicGetFore' : ('IndicatorGetForeground', 0, 0, 0),
199
200 'AutoCShow' : ('AutoCompShow', 0, 0, 0),
201 'AutoCCancel' : ('AutoCompCancel', 0, 0, 0),
202 'AutoCActive' : ('AutoCompActive', 0, 0, 0),
203 'AutoCPosStart' : ('AutoCompPosStart', 0, 0, 0),
204 'AutoCComplete' : ('AutoCompComplete', 0, 0, 0),
205 'AutoCStops' : ('AutoCompStops', 0, 0, 0),
206 'AutoCSetSeparator' : ('AutoCompSetSeparator', 0, 0, 0),
207 'AutoCGetSeparator' : ('AutoCompGetSeparator', 0, 0, 0),
208 'AutoCSelect' : ('AutoCompSelect', 0, 0, 0),
209 'AutoCSetCancelAtStart' : ('AutoCompSetCancelAtStart', 0, 0, 0),
210 'AutoCGetCancelAtStart' : ('AutoCompGetCancelAtStart', 0, 0, 0),
211 'AutoCSetFillUps' : ('AutoCompSetFillUps', 0, 0, 0),
212 'AutoCSetChooseSingle' : ('AutoCompSetChooseSingle', 0, 0, 0),
213 'AutoCGetChooseSingle' : ('AutoCompGetChooseSingle', 0, 0, 0),
214 'AutoCSetIgnoreCase' : ('AutoCompSetIgnoreCase', 0, 0, 0),
215 'AutoCGetIgnoreCase' : ('AutoCompGetIgnoreCase', 0, 0, 0),
216
217 'SetHScrollBar' : ('SetUseHorizontalScrollBar', 0, 0, 0),
218 'GetHScrollBar' : ('GetUseHorizontalScrollBar', 0, 0, 0),
219
220 'GetCaretFore' : ('GetCaretForeground', 0, 0, 0),
221
222 'GetUsePalette' : (None, 0, 0, 0),
223
224 'FindText' : (0,
225 '''int %s(int minPos, int maxPos,
226 const wxString& text,
227 bool caseSensitive, bool wholeWord);''',
228 '''int %s(int minPos, int maxPos,
229 const wxString& text,
230 bool caseSensitive, bool wholeWord) {
231 TextToFind ft;
232 int flags = 0;
233
234 flags |= caseSensitive ? SCFIND_MATCHCASE : 0;
235 flags |= wholeWord ? SCFIND_WHOLEWORD : 0;
236 ft.chrg.cpMin = minPos;
237 ft.chrg.cpMax = maxPos;
238 ft.lpstrText = (char*)text.c_str();
239
240 return SendMsg(%s, flags, (long)&ft);''',
241 0),
242
243 'FormatRange' : (0,
244 '''int %s(bool doDraw,
245 int startPos,
246 int endPos,
247 wxDC* draw,
248 wxDC* target, // Why does it use two? Can they be the same?
249 wxRect renderRect,
250 wxRect pageRect);''',
251 ''' int %s(bool doDraw,
252 int startPos,
253 int endPos,
254 wxDC* draw,
255 wxDC* target, // Why does it use two? Can they be the same?
256 wxRect renderRect,
257 wxRect pageRect) {
258 RangeToFormat fr;
259
260 fr.hdc = draw;
261 fr.hdcTarget = target;
262 fr.rc.top = renderRect.GetTop();
263 fr.rc.left = renderRect.GetLeft();
264 fr.rc.right = renderRect.GetRight();
265 fr.rc.bottom = renderRect.GetBottom();
266 fr.rcPage.top = pageRect.GetTop();
267 fr.rcPage.left = pageRect.GetLeft();
268 fr.rcPage.right = pageRect.GetRight();
269 fr.rcPage.bottom = pageRect.GetBottom();
270 fr.chrg.cpMin = startPos;
271 fr.chrg.cpMax = endPos;
272
273 return SendMsg(%s, doDraw, (long)&fr);''',
274 0),
275
276
277 'GetLine' : (0,
278 'wxString %s(int line);',
279
280 '''wxString %s(int line) {
281 wxString text;
282 int len = LineLength(line);
283 char* buf = text.GetWriteBuf(len);
284
285 int pos = SendMsg(%s, line, (long)buf);
286 text.UngetWriteBuf();
287
288 return text;''',
289
290 ('Retrieve the contents of a line.',)),
291
292 'SetSel' : ('SetSelection', 0, 0, 0),
293 'GetSelText' : ('GetSelectedText',
294 'wxString %s();',
295
296 '''wxString %s() {
297 wxString text;
298 int start;
299 int end;
300
301 GetSelection(&start, &end);
302 int len = end - start;
303 char* buff = text.GetWriteBuf(len+1);
304
305 SendMsg(%s, 0, (long)buff);
306 text.UngetWriteBuf();
307 return text;''',
308
309 ('Retrieve the selected text.',)),
310
311 'GetTextRange' : (0,
312 'wxString %s(int startPos, int endPos);',
313
314 '''wxString %s(int startPos, int endPos) {
315 wxString text;
316 int len = endPos - startPos;
317 char* buff = text.GetWriteBuf(len+1);
318 TextRange tr;
319 tr.lpstrText = buff;
320 tr.chrg.cpMin = startPos;
321 tr.chrg.cpMax = endPos;
322
323 SendMsg(%s, 0, (long)&tr);
324 text.UngetWriteBuf();
325 return text;''',
326
327 ('Retrieve a range of text.',)),
328
329 'PointXFromPosition' : (None, 0, 0, 0),
330 'PointYFromPosition' : (None, 0, 0, 0),
331
332 'ScrollCaret' : ('EnsureCaretVisible', 0, 0, 0),
333 'ReplaceSel' : ('ReplaceSelection', 0, 0, 0),
334 'Null' : (None, 0, 0, 0),
335
336 'GetText' : (0,
337 'wxString %s();',
338
339 '''wxString %s() {
340 wxString text;
341 int len = GetTextLength()+1;
342 char* buff = text.GetWriteBuf(len+1);
343
344 SendMsg(%s, len, (long)buff);
345 buff[len] = 0;
346 text.UngetWriteBuf();
347 return text;''',
348
349 ('Retrieve all the text in the document.', )),
350
351 'GetDirectFunction' : (None, 0, 0, 0),
352 'GetDirectPointer' : (None, 0, 0, 0),
353
354 'CallTipPosStart' : ('CallTipPosAtStart', 0, 0, 0),
355 'CallTipSetHlt' : ('CallTipSetHighlight', 0, 0, 0),
356 'CallTipSetBack' : ('CallTipSetBackground', 0, 0, 0),
357
358
359 # Remove all methods that are key commands since they can be
360 # executed with CmdKeyExecute
361 'LineDown' : (None, 0, 0, 0),
362 'LineDownExtend' : (None, 0, 0, 0),
363 'LineUp' : (None, 0, 0, 0),
364 'LineUpExtend' : (None, 0, 0, 0),
365 'CharLeft' : (None, 0, 0, 0),
366 'CharLeftExtend' : (None, 0, 0, 0),
367 'CharRight' : (None, 0, 0, 0),
368 'CharRightExtend' : (None, 0, 0, 0),
369 'WordLeft' : (None, 0, 0, 0),
370 'WordLeftExtend' : (None, 0, 0, 0),
371 'WordRight' : (None, 0, 0, 0),
372 'WordRightExtend' : (None, 0, 0, 0),
373 'Home' : (None, 0, 0, 0),
374 'HomeExtend' : (None, 0, 0, 0),
375 'LineEnd' : (None, 0, 0, 0),
376 'LineEndExtend' : (None, 0, 0, 0),
377 'DocumentStart' : (None, 0, 0, 0),
378 'DocumentStartExtend' : (None, 0, 0, 0),
379 'DocumentEnd' : (None, 0, 0, 0),
380 'DocumentEndExtend' : (None, 0, 0, 0),
381 'PageUp' : (None, 0, 0, 0),
382 'PageUpExtend' : (None, 0, 0, 0),
383 'PageDown' : (None, 0, 0, 0),
384 'PageDownExtend' : (None, 0, 0, 0),
385 'EditToggleOvertype' : (None, 0, 0, 0),
386 'Cancel' : (None, 0, 0, 0),
387 'DeleteBack' : (None, 0, 0, 0),
388 'Tab' : (None, 0, 0, 0),
389 'BackTab' : (None, 0, 0, 0),
390 'NewLine' : (None, 0, 0, 0),
391 'FormFeed' : (None, 0, 0, 0),
392 'VCHome' : (None, 0, 0, 0),
393 'VCHomeExtend' : (None, 0, 0, 0),
394 'ZoomIn' : (None, 0, 0, 0),
395 'ZoomOut' : (None, 0, 0, 0),
396 'DelWordLeft' : (None, 0, 0, 0),
397 'DelWordRight' : (None, 0, 0, 0),
398 'LineCut' : (None, 0, 0, 0),
399 'LineDelete' : (None, 0, 0, 0),
400 'LineTranspose' : (None, 0, 0, 0),
401 'LowerCase' : (None, 0, 0, 0),
402 'UpperCase' : (None, 0, 0, 0),
403 'LineScrollDown' : (None, 0, 0, 0),
404 'LineScrollUp' : (None, 0, 0, 0),
405
406
407 'GetDocPointer' : (0,
408 'void* %s();',
409 '''void* %s() {
410 return (void*)SendMsg(%s);''',
411 0),
412
413 'SetDocPointer' : (0,
414 'void %s(void* docPointer);',
415 '''void %s(void* docPointer) {
416 SendMsg(%s, (long)docPointer);''',
417 0),
418
419 'CreateDocument' : (0,
420 'void* %s();',
421 '''void* %s() {
422 return (void*)SendMsg(%s);''',
423 0),
424
425 'AddRefDocument' : (0,
426 'void %s(void* docPointer);',
427 '''void %s(void* docPointer) {
428 SendMsg(%s, (long)docPointer);''',
429 0),
430
431 'ReleaseDocument' : (0,
432 'void %s(void* docPointer);',
433 '''void %s(void* docPointer) {
434 SendMsg(%s, (long)docPointer);''',
435 0),
436
437 'GrabFocus' : (None, 0, 0, 0),
438
439 '' : ('', 0, 0, 0),
440
441 }
442
443 #----------------------------------------------------------------------------
444
445 def processIface(iface, h_tmplt, cpp_tmplt, h_dest, cpp_dest):
446 curDocStrings = []
447 values = []
448 methods = []
449
450 # parse iface file
451 fi = FileInput(iface)
452 for line in fi:
453 line = line[:-1]
454 if line[:2] == '##' or line == '':
455 #curDocStrings = []
456 continue
457
458 op = line[:4]
459 if line[:2] == '# ': # a doc string
460 curDocStrings.append(line[2:])
461
462 elif op == 'val ':
463 parseVal(line[4:], values, curDocStrings)
464 curDocStrings = []
465
466 elif op == 'fun ' or op == 'set ' or op == 'get ':
467 parseFun(line[4:], methods, curDocStrings, values)
468 curDocStrings = []
469
470 elif op == 'cat ':
471 if string.strip(line[4:]) == 'Deprecated':
472 break # skip the rest of the file
473
474 elif op == 'evt ':
475 pass
476
477 else:
478 print '***** Unknown line type: ', line
479
480
481 # process templates
482 data = {}
483 data['VALUES'] = processVals(values)
484 defs, imps = processMethods(methods)
485 data['METHOD_DEFS'] = defs
486 data['METHOD_IMPS'] = imps
487
488 # get template text
489 h_text = open(h_tmplt).read()
490 cpp_text = open(cpp_tmplt).read()
491
492 # do the substitutions
493 h_text = h_text % data
494 cpp_text = cpp_text % data
495
496 # write out destination files
497 open(h_dest, 'w').write(h_text)
498 open(cpp_dest, 'w').write(cpp_text)
499
500
501
502 #----------------------------------------------------------------------------
503
504 def processVals(values):
505 text = []
506 for name, value, docs in values:
507 if docs:
508 text.append('')
509 for x in docs:
510 text.append('// ' + x)
511 text.append('#define %s %s' % (name, value))
512 return string.join(text, '\n')
513
514 #----------------------------------------------------------------------------
515
516 def processMethods(methods):
517 defs = []
518 imps = []
519
520 for retType, name, number, param1, param2, docs in methods:
521 retType = retTypeMap.get(retType, retType)
522 params = makeParamString(param1, param2)
523
524 name, theDef, theImp, docs = checkMethodOverride(name, number, docs)
525
526 if name is None:
527 continue
528
529 # Build the method definition for the .h file
530 if docs:
531 defs.append('')
532 for x in docs:
533 defs.append(' // ' + x)
534 if not theDef:
535 theDef = ' %s %s(%s);' % (retType, name, params)
536 defs.append(theDef)
537
538 # Build the method implementation string
539 if docs:
540 imps.append('')
541 for x in docs:
542 imps.append('// ' + x)
543 if not theImp:
544 theImp = '%s wxStyledTextCtrl::%s(%s) {\n ' % (retType, name, params)
545
546 if retType == 'wxColour':
547 theImp = theImp + 'long c = '
548 elif retType != 'void':
549 theImp = theImp + 'return '
550 theImp = theImp + 'SendMsg(%s, %s, %s)' % (number,
551 makeArgString(param1),
552 makeArgString(param2))
553 if retType == 'bool':
554 theImp = theImp + ' != 0'
555 if retType == 'wxColour':
556 theImp = theImp + ';\n return wxColourFromLong(c)'
557
558 theImp = theImp + ';\n}'
559 imps.append(theImp)
560
561
562 return string.join(defs, '\n'), string.join(imps, '\n')
563
564
565 #----------------------------------------------------------------------------
566
567 def checkMethodOverride(name, number, docs):
568 theDef = theImp = None
569 if methodOverrideMap.has_key(name):
570 item = methodOverrideMap[name]
571
572 if item[0] != 0:
573 name = item[0]
574 if item[1] != 0:
575 theDef = ' ' + (item[1] % name)
576 if item[2] != 0:
577 theImp = item[2] % ('wxStyledTextCtrl::'+name, number) + '\n}'
578 if item[3] != 0:
579 docs = item[3]
580
581 return name, theDef, theImp, docs
582
583 #----------------------------------------------------------------------------
584
585 def makeArgString(param):
586 if not param:
587 return '0'
588
589 typ, name = param
590
591 if typ == 'string':
592 return '(long)%s.c_str()' % name
593 if typ == 'colour':
594 return 'wxColourAsLong(%s)' % name
595
596 return name
597
598 #----------------------------------------------------------------------------
599
600 def makeParamString(param1, param2):
601 def doOne(param):
602 if param:
603 aType = paramTypeMap.get(param[0], param[0])
604 return aType + ' ' + param[1]
605 else:
606 return ''
607
608 st = doOne(param1)
609 if st and param2:
610 st = st + ', '
611 st = st + doOne(param2)
612 return st
613
614
615 #----------------------------------------------------------------------------
616
617 def parseVal(line, values, docs):
618 name, val = string.split(line, '=')
619
620 # remove prefixes such as SCI, etc.
621 for old, new in valPrefixes:
622 lo = len(old)
623 if name[:lo] == old:
624 if new is None:
625 return
626 name = new + name[lo:]
627
628 # add it to the list
629 values.append( ('wxSTC_' + name, val, docs) )
630
631 #----------------------------------------------------------------------------
632
633 funregex = re.compile(r'\s*([a-zA-Z0-9_]+)' # <ws>return type
634 '\s+([a-zA-Z0-9_]+)=' # <ws>name=
635 '([0-9]+)' # number
636 '\(([ a-zA-Z0-9_]*),' # (param,
637 '([ a-zA-Z0-9_]*)\)') # param)
638
639 def parseFun(line, methods, docs, values):
640 def parseParam(param):
641 param = string.strip(param)
642 if param == '':
643 param = None
644 else:
645 param = tuple(string.split(param))
646 return param
647
648 mo = funregex.match(line)
649 if mo is None:
650 print "***** Line doesn't match! : " + line
651
652 retType, name, number, param1, param2 = mo.groups()
653
654 param1 = parseParam(param1)
655 param2 = parseParam(param2)
656
657 # Special case. For the key command functionss we want a value defined too
658 num = string.atoi(number)
659 for v in cmdValues:
660 if (type(v) == type(()) and v[0] <= num < v[1]) or v == num:
661 parseVal('CMD_%s=%s' % (string.upper(name), number), values, ())
662
663 #if retType == 'void' and not param1 and not param2:
664
665 methods.append( (retType, name, number, param1, param2, tuple(docs)) )
666
667
668 #----------------------------------------------------------------------------
669
670
671 def main(args):
672 # TODO: parse command line args to replace default input/output files???
673
674 # Now just do it
675 processIface(IFACE, H_TEMPLATE, CPP_TEMPLATE, H_DEST, CPP_DEST)
676
677
678
679 if __name__ == '__main__':
680 main(sys.argv)
681
682 #----------------------------------------------------------------------------
683
684