4

I am looking for tool that can convert .Glade (or xml) file 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.

User7723337
  • 11,857
  • 27
  • 101
  • 182

3 Answers3

6

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);
ptomato
  • 56,175
  • 13
  • 112
  • 165
  • 1
    I want to see the generated code. I have created one very complex composite widget in glade, and if I start implementing it by manual coding it will take somuch time. so I am looking for a converter that will given me source for this widget which I have created in glade. – User7723337 May 07 '10 at 10:02
  • There is no generated code in Glade 3. If you already made the widget in Glade, why do you want to reimplement it manually?? – ptomato May 07 '10 at 13:47
  • because i want to create a list of it. – User7723337 May 07 '10 at 13:52
  • You can put the above code in the constructor of the widget, and then make a list of it... – ptomato May 07 '10 at 15:20
4

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

Alexey Yakovenko
  • 494
  • 2
  • 10
2

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()
Gearoid Murphy
  • 11,834
  • 17
  • 68
  • 86