forked from burner/sweet.hpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathg2cpp.py
executable file
·82 lines (65 loc) · 1.84 KB
/
g2cpp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python
import xml.etree.ElementTree as ET
import sys
names = []
nameCls = dict()
def recu(r):
t = None
if r.tag == "object":
n = r.attrib["id"]
nameCls[n] = r.attrib["class"]
names.append(n)
for c in r:
recu(c)
def buildDataMember(f):
f.write("protected:\n")
f.write("\tGlib::RefPtr<Gtk::Builder> _builder;\n")
for n in names:
if n[0:3] == "tab":
continue
f.write("\tGtk::{}* {};\n".format(nameCls[n][3:], n))
def getCorrentFirstName():
for i in names:
if nameCls[i] == "GtkWindow" or nameCls[i] == "GtkDialog":
return i
def buildConstructorAndCast(f, cn):
f.write("\npublic:\n")
f.write("\tinline {}() {{\n".format(cn))
f.write("\t\t_builder = Gtk::Builder::create_from_string(glade_file);\n")
for i in names:
f.write("\t\t_builder->get_widget(\"{}\", {});\n".format(i, i))
f.write("\t}\n\n")
f.write("\tinline operator {}*() {{\n".format(nameCls[getCorrentFirstName()][3:]))
f.write("\t\treturn {};\n".format(getCorrentFirstName()))
f.write("\t}\n")
def printer(fn,cn,pc):
h = fn.rfind("/")
nfn = fn[:h+1]+"g2cpp_"+fn[h+1:]
pci = pc.rfind("/")
f = open(nfn, "w", encoding="utf-8")
#f = codecs.open('in','r','utf8')
#f = open(nfn, "w")
text = open(cn, "r", encoding="utf-8")
f.write("#ifndef {}_HPP\n"
"#define {}_HPP\n\n"
"#include <gtkmm.h>\n\n"
"using namespace Gtk;\n\n"
"class {} {{\n".format("G2CPP_"+fn[h+1:-10].upper(), "G2CPP_"+fn[h+1:-10].upper(), pc[pci+1:-6]))
f.write("\tconst char* glade_file = \n")
for l in text:
f.write('"'+l.replace('"', '\\"')[:-1] + "\"\n")
f.write(";\n")
buildDataMember(f)
#buildConstructorAndCast(f, pc[pci+1:-6])
buildConstructorAndCast(f, pc[pci+1:-6])
f.write("};\n\n#endif\n")
#fn = "glade"
fn = sys.argv[1]
print(fn)
tree = ET.parse(fn)
root = tree.getroot()
for i in root:
recu(i)
#print(names)
#print("\nproperites")
printer(fn+".hpp",fn,fn)