将.Glade(或xml)文件转换为C源的工具

时间:2010-05-07 08:30:10

标签: c windows gtk glade

我正在寻找可以将.Glade(或xml)文件转换为C源的工具 我试过g2c(Glade To C Translator),但我正在寻找Windows二进制文件。

任何人都知道窗口的任何好工具。

谢谢,
PP。

3 个答案:

答案 0 :(得分:5)

您不需要工具。只需用您喜欢的脚本语言编写脚本,即可将您的glade文件格式化为C字符串文字:

例如,我们称之为glade_file.c

const gchar *my_glade_file = 
    "<interface>"
        "<object class=\"GtkDialog\">"
            "<et-cetera />"
        "</object>"
    "</interface>";

glade_file.c编译到您的程序中,然后在构建界面时执行此操作:

extern const gchar *my_glade_file;
result = gtk_builder_add_from_string(builder, my_glade_file, -1, &error);

答案 1 :(得分:4)

glade2适合我。虽然如果你使用新的glade3功能,它将无助于你......

答案 2 :(得分:1)

我不得不写一个小的python脚本来做这件事,希望这有助于其他人:

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