Embed resources like ui files
nyckmaia opened this issue ยท 9 comments
I would like to know if is possible to embed the UI files into the final binary.
Thank you,
I think you can use gtk_builder_new_from_string()
:
https://developer.gnome.org/gtk3/stable/GtkBuilder.html#gtk-builder-new-from-string
I have not found any use of that function in any example of gtk-fortran, but I have found a Python example here:
https://discourse.gnome.org/t/how-to-create-menus-for-apps-using-python/2413/18
Don't forget to add the c_null_char
constant to the Fortran string before calling gtk_builder_new_from_string()
.
Thank you @vmagnin for your quick response...
I'm a newer in Fortran and GTK...
If gtk-fortran
package does NOT have the gtk_builder_new_from_string()
wrapper, so it's not possible to do it directly in Fortran, right?
If yes, can I call the pure C GTK function?
How can I do that?
Welcome, in the project! Tell me more on the branch and version you are using.
In the latest gtk-fortran files in the gtk3 branch, I have found that function in the src/gtk-auto.f90
:
! GDK_AVAILABLE_IN_3_10
!GtkBuilder * gtk_builder_new_from_string (const gchar *string, gssize length);
function gtk_builder_new_from_string(string, length) bind(c)
use iso_c_binding, only: c_ptr, c_char, c_size_t
type(c_ptr) :: gtk_builder_new_from_string
character(kind=c_char), dimension(*) :: string
integer(c_size_t), value :: length
end function
It is possible that in an older version of gtk-fortran that function was not detected. But you can download the current version.
UPDATED: if don't want to install a newer gtk-fortran version, you could also simply copy/paste the upper interface into your program... and link with the gtk lib.
@vmagnin great!
Well, I saw that GTK 4.0 is available.
So I'm trying to use this version with fortran-gtk
.
Which is the better way to get embed the Glade XML string into the Fortran binary?
Copy and paste inside the *.f90 file?
My Setup is:
- Windows 10 x64
- MSYS 2 gfortran 10.2
- MSYS 2 gtk4.0
- MSYS 2 glade
OK, you can use the gtk4 branch, the gtk_builder_new_from_string()
is available in it (all the available Fortran/C interfaces are listed in the file https://github.com/vmagnin/gtk-fortran/blob/gtk4/src/gtk-fortran-index.csv).
Yes, you will have to create a Fortran string and copy and paste the UI definitions inside the *.f90 file. You will have to figure out how to put a multi-line string into a Fortran string, and add the final c_null_char with //c_null_char
Concerning gtk-fortran in MSYS2, also note that for the moment the plplot-fortran driver is missing... See msys2/MINGW-packages#7664 (comment)
@vmagnin thank you for all information!
In my case, the generated Glade XML file will be a larger file...
So, to do the continuation lines manually in Fortran using &
will be very tedious....
And another thing: The Fortran-2008 Standard have a continuation lines limit of 255 lines.
http://fortranwiki.org/fortran/show/Continuation+lines
So I think that may be there is another way to embed the Glade XML file inside the Fortran binary...I don't know.
Thank you again!
OK, if the file is big I think it is better to distribute it alongside the binary, like in our gtkbuilder.f90 and gtkbuilder2.f90 examples which need the gtkbuilder.glade file in the same directory as their binaries. But of course, perhaps you don't want the user to modify that file...
I am not aware of any mechanism which could allow the compiler to include the XML file into the binary... And I can not figure out how the binary could access that file... But there is many options in the linker ld
... Perhaps @jerryd could help.
Maybe, another solution could be to use several strings and use gtk_builder_add_from_string ()
several times...
Perhaps you could put the XML into a string in a C file, generate an object file, and link with the Fortran file using ISO_C_BINDING... Just an idea...