I have been converting from gtkmm 4.8 to 4.10 and trying to replace the filechooserdialog with the new filedialog. My test code (below) finds that the routine that invokes, for example, dialog->save() will not wait until the handler is complete and I am used to coding as per the filechooserdialog where I could set it to wait until the user has dismissed the dialog. The header file is below:
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
~ExampleWindow() override;
protected:
// Signal handlers:
void on_button_file();
void on_button_quit();
void on_file_dialog_save(const Glib::RefPtr<Gio::AsyncResult> &result,
const Glib::RefPtr<Gtk::FileDialog> &dialog);
// Child widgets:
Gtk::Box m_VBox;
Gtk::Box m_ButtonBox;
Gtk::Button m_Button_Quit;
Gtk::Button m_Button_File;
};
#include "filedialog.h"
ExampleWindow::ExampleWindow()
: m_VBox(Gtk::Orientation::VERTICAL),
m_Button_File("File"),
m_Button_Quit("Quit")
{
set_title("FileDialog example");
set_default_size(100, 100);
m_VBox.set_margin(5);
set_child(m_VBox);
m_VBox.append(m_ButtonBox);
m_ButtonBox.append(m_Button_File);
m_ButtonBox.append(m_Button_Quit);
m_ButtonBox.set_margin(5);
m_Button_File.signal_clicked().connect(
sigc::mem_fun(*this, &ExampleWindow::on_button_file));
m_Button_Quit.signal_clicked().connect(
sigc::mem_fun(*this, &ExampleWindow::on_button_quit));
}
#endif //GTKMM_EXAMPLEWINDOW_H
The cpp file is below:
#include "filedialog.h"
ExampleWindow::ExampleWindow()
: m_VBox(Gtk::Orientation::VERTICAL),
m_Button_File("File"),
m_Button_Quit("Quit")
{
set_title("FileDialog example");
set_default_size(100, 100);
m_VBox.set_margin(5);
set_child(m_VBox);
m_VBox.append(m_ButtonBox);
m_ButtonBox.append(m_Button_File);
m_ButtonBox.append(m_Button_Quit);
m_ButtonBox.set_margin(5);
m_Button_File.signal_clicked().connect(
sigc::mem_fun(*this, &ExampleWindow::on_button_file));
m_Button_Quit.signal_clicked().connect(
sigc::mem_fun(*this, &ExampleWindow::on_button_quit));
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_button_file()
{
auto dialog = Gtk::FileDialog::create();
dialog->set_title("Testing file dialog save");
printf("Created file/save dialog\n");
dialog->save(*this,
sigc::bind(
sigc::mem_fun(
*this,
&ExampleWindow::on_file_dialog_save
),
dialog
)
);
printf("Exited file button handler\n");
}
void ExampleWindow::on_file_dialog_save(const Glib::RefPtr<Gio::AsyncResult> &result,
const Glib::RefPtr<Gtk::FileDialog> &dialog)
{
printf("Before save/finish call\n");
auto file = dialog->save_finish(result);
printf("After save/finish call\n");
}
void ExampleWindow::on_button_quit()
{
set_visible(false);
}
When I run the program, the following output occurs:
Created file/save dialog Exited file button handler Before save/finish call After save/finish call
and thus I can't seem to have the file button wait until the handler is complete.
- Created simple example.
- Plently of internet searches
Further investigation
Added a semaphore, initialised to one, then modified the File button handler as per the following:
void ExampleWindow::on_button_file()
{
auto dialog = Gtk::FileDialog::create();
dialog->set_title("Testing file dialog save");
printf("Created file/save dialog\n");
sem_wait(&m_dialog_semaphore);
printf("Before dialog->save() call\n");
dialog->save(*this,
sigc::bind(
sigc::mem_fun(
*this,
&ExampleWindow::on_file_dialog_save
),
dialog
)
);
sem_wait(&m_dialog_semaphore);
printf("Exited file button handler\n");
}
and then added the following code to the dialog handler:
void ExampleWindow::on_file_dialog_save(const Glib::RefPtr<Gio::AsyncResult> &result,
const Glib::RefPtr<Gtk::FileDialog> &dialog)
{
printf("Before save/finish call\n");
auto file = dialog->save_finish(result);
printf("After save/finish call\n");
sem_post(&m_dialog_semaphore);
}
with the idea being to ensure that the file button handler has to wait until the dialog handler exits and thus the code behaves as I would expect and want to. The program now does not display the dialog and freezes as that point. The only output now is as follows:
Created file/save dialog Before dialog->save() call
It seems that until such time that the file button handler exits that the dialog will not display. This seems odd to me and when I reviewed (again) the sample code in the gtkmm examples book I found that it was way too simple an example and did not answer any of my questions.