Ok. First, you'll need a header bar and add a button to it.
private void create_headerbar () {
var headerbar = new Gtk.HeaderBar ();
var button = new Gtk.Button.from_icon_name ("document-open",
Gtk.IconSize.LARGE_TOOLBAR);
button.clicked.connect (() => {
show_file_dialog();
});
headerbar.pack_start (button);
window.set_titlebar (headerbar);
}
The window
at the end is a Gtk.Window
I've declared elsewhere.
That show_file_dialog()
method is the one that will show the dialog.
It's declared next:
private void show_file_dialog(){
var dialog = new Gtk.FileChooserDialog ("Open File",
window,
Gtk.FileChooserAction.OPEN,
"_Cancel",
Gtk.ResponseType.CANCEL,
"_Open",
Gtk.ResponseType.ACCEPT);
var response = dialog.run();
if (response == Gtk.ResponseType.ACCEPT) {
string filename = dialog.get_filename();
//now do something with the filename
}
dialog.dispose(); //no need for the dialog to be around anymore
}
I think this is a good starting point. You'll need to adapt it for your needs like changing the methods' signatures and so...
"Works on my machine"™