1

Does anybody can tell me how to add a button to call open_file_dialog on GTK Header bar? i did it in appmenu with,

$.activate.connect(() =>{ this.dialog_openFile(); });

but this way dont works on a simple call in headerbar,

i'm a newbie, learning about GTK and Granit.

Regards,

1 Answer 1

1

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"™

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.