]> git.lyx.org Git - lyx.git/blob - src/frontends/gnome/accessors.py
A lean, clean and working start to the new, improved gnome frontend.
[lyx.git] / src / frontends / gnome / accessors.py
1 from xml.sax import *
2
3 import re, sys
4
5 ###
6 # Real name.  This RE distinguishes 'filler' widgets from the
7 # widgets we actually care about.
8 ###
9
10 rn = re.compile(r"^r_")
11
12
13 def cppClass(gladeClass):
14     gtk = re.compile(r"^Gtk")
15     gnome = re.compile(r"^Gnome")
16
17     if gtk.search(gladeClass):
18         return "Gtk::" + gladeClass[3:]
19     elif gnome.search(gladeClass):
20         return "Gnome::" + gladeClass[5:]
21     
22 class widget:
23     def __init__(self, clss, name):
24         self.clss = cppClass(clss)
25         self.name = name
26
27     def getAccessor(self):
28         function = ""
29         function += self.clss + " * " + dialog +"::" + self.name
30         function += "() const \n{\n        return getWidget<" + self.clss 
31         function +=  ">(\"" + "r_" + self.name + "\");\n}\n"
32
33         return function
34
35     def getDeclaration(self):
36         function = "/// generated by accessors.py\n"
37         function += self.clss + " * " + self.name
38         function += "() const;\n";
39
40         return function
41
42
43 class GnomeFrontendHandler(ContentHandler):
44
45     def __init__(self):
46
47         self.elemstack = []
48         self.widget = 0
49         self.TODO = []
50         
51     def startElement(self, name, attrs):
52         self.elemstack.append(name)
53     
54     def endElement(self, name):
55         self.elemstack.pop()
56
57         if name == "widget":
58             self.currclass = ""
59         
60     def characters(self, data):
61
62         elem = self.elemstack[-1]
63
64         if elem == "class":
65             self.currclass = data
66         elif elem == "name":
67             if rn.search(data):
68                 self.TODO.append(widget(self.currclass,
69                                         re.sub("^r_", "", data)))
70     def widgets(self):
71         return self.TODO
72
73
74
75 dialog = sys.argv[2]
76 glade = open(sys.argv[1])
77
78
79 ## parse the document
80
81 prs = make_parser()
82 hndlr = GnomeFrontendHandler()
83 prs.setContentHandler(hndlr)
84 prs.parse(glade)
85
86 ## write the definitions to .C_gen
87
88 dotC = open(dialog + ".C_gen", "w+")
89
90 for i in hndlr.widgets():
91     dotC.write( i.getAccessor())
92
93 dotC.close()
94
95 ## write the declarations to .h_gen
96
97 dotH = open(dialog + ".h_gen", "w+")
98
99 for i in hndlr.widgets():
100     dotH.write( i.getDeclaration())
101
102 dotH.close()
103
104
105     
106
107
108