How do I include a glade source file into my program?

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
Fox
Posts: 353
Joined: Aug 08, 2006 13:39
Location: Lille, France
Contact:

How do I include a glade source file into my program?

Postby Fox » Mar 08, 2011 16:13

Hi,

I am now writing a little GUI application using glade. Glade is really cool, but as far as I see, I will have to distribute my xml "description" file along with my program, as libglade is using it to draw all menus, buttons, etc...

Is there any way I could include my xml glade file into my FreeBASIC source code?

I saw that I could maybe replace my "glade_xml_new" keyword (that is loading the xml description from within my program) by such one:
glade_xml_new_from_buffer(buffer, int size)

Unfortunately, I don't know how to use it, and have no clue about what a "buffer" is...

Any help from someone that already sorted this out would be appreciated :-)
TJF
Posts: 3609
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Postby TJF » Mar 08, 2011 21:37

I recomment to use GTK+tobac to generate the FB source. It can include the GUI-XML file into the basic source. Just check the button 'Glade in source' in the parameter tab and generate the .bas files.

Also I recomment to use GtkBuilder instead of deprecated libglade. GTK+tobac can handle both, libglade and GtkBuilder format. With GtkBuilder you neither need to ship the GUI-XML file nor libglade along with your binary.
Fox
Posts: 353
Joined: Aug 08, 2006 13:39
Location: Lille, France
Contact:

Postby Fox » Mar 09, 2011 10:47

TJF wrote:I recomment to use GTK+tobac to generate the FB source. It can include the GUI-XML file into the basic source. Just check the button 'Glade in source' in the parameter tab and generate the .bas files.


Hi, thanks for the tip!

In fact, I don't think I need such tool like GTK+tobac (which is a really nice looking tool by the way!), as my project is ridiculously small. Also, I like to have to write some code, without relying on fully automated stuff ;-)

I studied the source code generate by GTK+tobac, and I found there the answer to my question. What I needed is that:

Code: Select all

DIM AS STRING xmlBuffer
xmlBuffer = "here the content of the glade xml file" + CHR(0)
DIM xml AS GladeXML PTR
xml = glade_xml_new_from_buffer(SADD(xmlBuffer), LEN(xmlBuffer), 0, 0)


It works very well now. Thank you!
TJF
Posts: 3609
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Postby TJF » Mar 09, 2011 12:04

Fox wrote:Hi, thanks for the tip!

You're wellcome!

Fox wrote:Also, I like to have to write some code, without relying on fully automated stuff ;-)

GTK+tobac doesn't write custom code for you. It just connects the Glade GUI-XML file to your source. That can be done in different ways, ie with external .glade file or with internal. But you still have to code the features of your app.

What I needed is that:

Take care, there are some pitfalls creating the xmlBuffer-STRING. BTW: GTK+tobac does some compression for the glade file, so big files gets smaller and faster when included into the source.
Fox
Posts: 353
Joined: Aug 08, 2006 13:39
Location: Lille, France
Contact:

Postby Fox » Mar 09, 2011 14:35

TJF wrote:BTW: GTK+tobac does some compression for the glade file, so big files gets smaller and faster when included into the source.


Yeah, and it breaks comboboxes a the same occasion :)
In fact, your converter is removing all end-of-line markers (CR, or CR/LF), and this is not a good thing...

When using comboboxes with libglade, there is an xml property called "items". This "items" section contains the list of selectable items in the combobox. This list is a raw list of lines, without any addidional markup stuff.
GTK+tobac is breaking this, because it removes all LFs, so the combobox end up being a combobox of only one, very long item.

I wrote my own, simplistic glade2bi tool that I use to convert my glade description into a *.bi file I can include into my project. My tool is very basic, and preserves end-of-line markers. Here it is:

Code: Select all

REM
REM This tool converts a glide XML file into a *.bi FreeBASIC file.
REM Written by Mateusz Viste // 09 Mar 2011
REM

#INCLUDE ONCE "file.bi"

FUNCTION txt2bas(rawtext AS STRING) AS STRING
  DIM result AS STRING
  DIM x AS UINTEGER
  FOR x = 1 TO LEN(rawtext)
    SELECT CASE MID(rawtext, x, 1)
      CASE CHR(34)   ' double quote
        result += """"""
      'CASE "a"
      '  result += "@"
      CASE ELSE
        result += MID(rawtext, x, 1)
    END SELECT
  NEXT x
  RETURN result
END FUNCTION



IF LEN(COMMAND(1)) = 0 THEN
  PRINT "Usage: glade2bi file.glade"
  END
END IF

IF FileExists(COMMAND(1)) = 0 THEN
  PRINT "The specified file could not be found"
  END
END IF

DIM AS STRING Bufor

OPEN COMMAND(1) FOR INPUT AS #1
  PRINT "DIM xmlBuffer AS STRING"
  PRINT "xmlBuffer = """""
  WHILE NOT EOF(1)
    LINE INPUT #1, Bufor
    PRINT "xmlBuffer += """ & txt2bas(Bufor) & """ & CHR(10)"
  WEND
CLOSE #1

END

TJF
Posts: 3609
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Postby TJF » Mar 09, 2011 15:13

Fox wrote:Yeah, and it breaks comboboxes a the same occasion :)
In fact, your converter is removing all end-of-line markers (CR, or CR/LF), and this is not a good thing...

Yes, you're right. I know this bug and i allready fixed it for next version.

Anyway, thanks for the report.
TJF
Posts: 3609
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Postby TJF » Mar 09, 2011 15:37

Yeah, and it breaks comboboxes a the same occasion :)

BTW: Filling combo-boxes in the GUI-XML-file is libglade stuff. Its deprecated. I recomment to switch to GtkBuilder and create an GObject (GList) for your combo-box entries. (Do you use Glade 3.6 or above?)
Fox
Posts: 353
Joined: Aug 08, 2006 13:39
Location: Lille, France
Contact:

Postby Fox » Mar 09, 2011 18:26

TJF wrote:BTW: Filling combo-boxes in the GUI-XML-file is libglade stuff. Its deprecated. I recomment to switch to GtkBuilder and create an GObject (GList) for your combo-box entries. (Do you use Glade 3.6 or above?)


I know about libglade being deprecated... The reason I am using it is because I never did any GUI project before, and when I looked on the internet for some hints, I found mainly libglade tutorials... and when I figured out how the whole animal is supposed to work, it worked out of the box. Using GtkBuilder is still black magic to me.

I am using Glade v3.7.0 to draw my interface, and I did saw that it propose to save my file in GtkBuilder format. However, my attempts to use such xml file within FreeBASIC resulted in no window appearing, and libglade spitting many errors at me :) I suppose I missed some major point, because as I understand, libglade shouldn't be even involved in the GtkBuilder world... Also, I almost finished my project, and I am in the "polishing" phase now. Switching to GtkBuilder would probably involve some major changes, and make me go back to the point I was 1 week ago :-P

Return to “Libraries”

Who is online

Users browsing this forum: No registered users and 9 guests