I am looking for tool that can convert .Glade (or xml) 开发者_如何学Pythonfile to C source.
I have tried g2c (Glade To C Translator) but i am looking for windows binary.Any one does know any good tool for window.
Thanks,
PP.You don't need a tool. Just write a script in your favorite scripting language to format your glade file as a C string literal:
For example, let's call it glade_file.c
:
const gchar *my_glade_file =
"<interface>"
"<object class=\"GtkDialog\">"
"<et-cetera />"
"</object>"
"</interface>";
Compile glade_file.c
into your program, then do this when you build your interface:
extern const gchar *my_glade_file;
result = gtk_builder_add_from_string(builder, my_glade_file, -1, &error);
glade2 works for me. though if you are using new glade3 features it won't help you..
I had to write a little python script to do this, hope this helps others:
import xml.dom.minidom
class XmlWriter:
def __init__(self):
self.snippets = []
def write(self, data):
if data.isspace(): return
self.snippets.append(data)
def __str__(self):
return ''.join(self.snippets)
if __name__ == "__main__":
writer = XmlWriter()
xml = xml.dom.minidom.parse("example.glade")
xml.writexml(writer)
strippedXml = ("%s" % (writer)).replace('"', '\\"')
byteCount = len(strippedXml)
baseOffset=0
stripSize=64
output = open("example.h", 'w')
output.write("static const char gladestring [] = \n{\n")
while baseOffset < byteCount:
skipTrailingQuote = 0
if baseOffset+stripSize < byteCount and strippedXml[baseOffset+stripSize] == '"':
skipTrailingQuote = 1
output.write(' "%s"\n' % (strippedXml[baseOffset:baseOffset+stripSize+skipTrailingQuote]))
baseOffset += stripSize + skipTrailingQuote
output.write("};\n")
output.close()
精彩评论