I have a C project (on Linux) which does not use GTK at all, but I would like to use GTK only for some specific tasks like selecting a file (file chooser dialog). So I have no GTK parent window, no gtk main loop, etc, I only want a file chooser dialog, which should block the execution of my program till user selected a file (or cancelled) and I don't use GTK then after that ever. What I've tried:
https://developer.gnome.org/gtk3/stable/GtkFileChooserDialog.html
I used the code at "Typical usage", first example. I put gtk_init(&argc, &argv) at the start of my program, and when I need the file chooser, I call a function with code from that example (I use parent as NULL, since there is no parent). The result is a flashing window for a fraction of second, then SIGSEGV. Before that I have this message:
Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.
I already read questions/answers on this message here at stackoverflow, but crashing the application is a more serious thing for me. I've also tried to put this:
gtk_widget_show_all(dialog);
after gtk_file_chooser_dialog_new() which causes no crash, I can select the file, but then I have the SIGSEGV again around gtk_file_chooser_get_filename().
When using gdb, I got this:
Program received signal SIGSEGV, Segmentation fault.
__GI___pthread_mutex_lock (mutex=0x3c3) at ../nptl/pthread_mutex_lock.c:67
Can you help me what mistake I did? I am not familiar with GTK programming too much, so I tried to use examples from the manual, but it does not seem to work. Thanks a lot in advance!
You cannot use widgets without a gtk mainloop.Well, it seems I was wrong. My sincere apologies! I had actually tried to do this before and the conclusions were exactly what I described. But the issue has bugged me for the last days, so I did some more digging and experimenting and came up with the following program:
You can call the dialog from another program (or even the terminal) with
If
default file
is provided (no brackets), it'll be selected. If a file is selected, it will be printed onstdout
, else the program will return with error=1There is still a small issue with running a widget without main window, which causes a message to be sent to
stderr
:GtkDialog mapped without a transient parent. This is discouraged
. I think this is really a bug (and it might be solved in a more recent version of gtk3). As the message is sent tostderr
, it shouldn't interfere with normal use.