Hi,
I’m trying to prompt the user to select a .txt file to use in my Gtk 3 (C) app.
If I simply choose the correct file a press OK or CANCEL everything works, but if i select the txt file and press ESC (instead of clicking OK or cancell button ) the app crash on segmentation fault.
This is my func
static gchar *
SelectFile()
{
gchar *resPath = NULL;
GtkWidget *dialog;
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
const gchar *ui_file = "/hmi/ui_dir/filechooser.ui";
GtkBuilder *builder = gtk_builder_new_from_resource(ui_file);
dialog = GTK_WIDGET(gtk_builder_get_object(builder, "filechooserdialog1"));
// Imposta le opzioni del dialogo
gtk_file_chooser_set_action(GTK_FILE_CHOOSER(dialog), action);
gtk_window_set_title(GTK_WINDOW(dialog), "Select a txt file to upload");
// gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), g_get_home_dir());
GtkFileFilter *filter = gtk_file_filter_new();
gtk_file_filter_set_name(filter,"*.txt");
gtk_file_filter_add_pattern(filter, "*.txt");
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
gint response = gtk_dialog_run(GTK_DIALOG(dialog));
if (response == GTK_RESPONSE_OK)
{
gchar *fileName = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
resPath = g_strdup(fileName);
g_free(fileName);
}
g_object_unref(filter);
g_object_unref(builder);
gtk_window_close(GTK_WINDOW(dialog));
return resPath;
}
and my xml file is like this:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkFileChooserDialog" id="filechooserdialog1">
<property name="name">filechooserdialog1</property>
<property name="can-focus">False</property>
<property name="tooltip-text" translatable="yes">Select a proper txt file filed with hex value separated by space. Ex: 01 02 03 FF AB</property>
<property name="type-hint">dialog</property>
<property name="create-folders">False</property>
<signal name="response" handler="GTK_RESPONSE_ACCEPT" swapped="no"/>
<child internal-child="vbox">
<object class="GtkBox" id="GTK_BOX">
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox" id="GTK_BTNS">
<property name="can-focus">False</property>
<property name="margin-end">10</property>
<property name="layout-style">end</property>
<child>
<object class="GtkButton" id="btn_cancel">
<property name="label" translatable="yes">CANCEL</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<signal name="clicked" handler="onBtnCancel" swapped="no"/>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkSeparator">
<property name="width-request">100</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
</child>
<child>
<object class="GtkSeparator">
<property name="width-request">100</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">5</property>
</packing>
</child>
<child>
<object class="GtkSeparator">
<property name="width-request">100</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">6</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn_ok">
<property name="label" translatable="yes">OK</property>
<property name="name">btn_ok</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<signal name="clicked" handler="onBtn_ok" swapped="no"/>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">7</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
<action-widgets>
<action-widget response="-7">btn_cancel</action-widget>
<action-widget response="-5">btn_ok</action-widget>
</action-widgets>
</object>
</interface>
What i’m doing wrong? Any help is appreciated
Regards
Dario