Tool to convert .Glade (or xml) file to C source

女生的网名这么多〃 提交于 2019-12-04 12:33:35

glade2 works for me. though if you are using new glade3 features it won't help you..

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);

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()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!