Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Writing an auxiliary function that returns a null terminated array of C strings #278

Open
vmagnin opened this issue Aug 31, 2023 · 4 comments

Comments

@vmagnin
Copy link
Owner

vmagnin commented Aug 31, 2023

Some GTK functions expect a null terminated array of C strings. For example, in examples/bazaar.f90 we have this piece of code for gtk_about_dialog_set_authors():

	character(len=14, kind=c_char), dimension(2), parameter :: authors = &
										  & ["Vincent MAGNIN", "James TAPPIN  "]
	character(kind=c_char), dimension(:), allocatable :: string
	character(kind=c_char), pointer, dimension(:) :: credit
	type(c_ptr), dimension(:), allocatable :: c_ptr_array
	...
	! To add authors we need a pointer toward a null terminated array of strings.
	! This code comes from src/gtk-hl-dialog.f90:
	allocate(c_ptr_array(size(authors)+1))

	do i = 1, size(authors)
	  call f_c_string(authors(i), string)
	  allocate(credit(size(string)))
	  ! A Fortran pointer toward the Fortran string:
	  credit(:) = string(:)
	  ! Store the C address in the array:
	  c_ptr_array(i) = c_loc(credit(1))
	  nullify(credit)
	end do
	! The array must be null terminated:
	c_ptr_array(size(authors)+1) = c_null_ptr
	! https://docs.gtk.org/gtk3/method.AboutDialog.set_authors.html
	call gtk_about_dialog_set_authors(dialog, c_ptr_array)
	deallocate(c_ptr_array)

It would be interesting to write in src/gtk-sup.f90 an auxiliary function that returns such a null terminated array of C strings. It would be called by examples/bazaar.f90, examples/menubar.f90 and src/gtk-hl-dialog.f90.

@jtappin
Copy link
Collaborator

jtappin commented Sep 15, 2023

Hi, I've not had much time to look at GTK-Fortran lately, and also I'm not really keen to get involved with GTK4.

I think that would be semi-trivial.
I'm also looking at a way of doing a c→f string conversion that gets the lengths from the null character rather than from the length returned by strlen as that can cause trouble if there are multibyte characters in the string.

@vmagnin
Copy link
Owner Author

vmagnin commented Sep 15, 2023

Hi James @jtappin

string conversion functions don't depend on the GTK version, so such a function could be added in the gtk3 and gtk4 branches. Those functions are still in src/gtk-sup.f90:
https://github.com/vmagnin/gtk-fortran/blob/gtk4/src/gtk-sup.f90

I have noticed that in GLib there is that function:
https://docs.gtk.org/glib/func.utf8_strlen.html

And I know that modern Fortran can support UTF8 (ISO/IEC 10646), but the standard says that it may be implemented or not by the compiler.

@jtappin
Copy link
Collaborator

jtappin commented Sep 22, 2023

Here is a patch to add an interface that will return an array of NULL terminated strings if CSTR argument is a 2-D allocatable array.
sup.txt

I think that's actually how I originally did it, but changed it because test widgets want a single string with line feeds to separate the lines.

P.S. Having a quick play about, I think that actually what I need for the automatic c→f converter is to use strlen and override what the text widget tells me about the length of the text.

@vmagnin
Copy link
Owner Author

vmagnin commented Sep 22, 2023

Here is a patch to add an interface that will return an array of NULL terminated strings if CSTR argument is a 2-D allocatable array.
sup.txt

Thanks a lot @jtappin
I have applied the patch in the gtk4 branch and backported the code in the gtk3 branch. When I will have some time, I will try to use that function in the gtk4 examples where it can be useful.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants