Replacing deprecated function (gtk_file_chooser_dialog_new ) for file choosing

,

Hello all,
i have migrated from gtk4.6 to 4.10 and I have found that function gtk_file_chooser_dialog_new is deprecated there.

I have tryied use new function gtk_file_dialog_open, but I do not understand documentation and I can not find any example.

Please could any body help me how to call function Response2 (from OpenDialog2) in my testing app and how to read there name of choosed file.
Or give me any link with example.

Thanks a lot:

here is my testing app code:

#include <gtk/gtk.h>
#include <iostream>
#include <string>

using namespace std;

GtkWidget *labText;

static void Response2 (GtkDialog *dialog, int response)
{
    //gtk_file_dialog_open_finish(dialog, )
}

void OpenDialog2 (GtkWidget* wid, GtkWidget* data)
 {
        GtkFileDialog *dialog = gtk_file_dialog_new();

    gtk_file_dialog_set_title(dialog, "My title");

    gtk_file_dialog_open(dialog, GTK_WINDOW(data),NULL,NULL,data);
}

static void Response (GtkDialog *dialog, int response)
{
    if (response == GTK_RESPONSE_ACCEPT)
    {
         g_autoptr (GFile) file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));

         string fileName = g_file_get_path(file);
        cout << "file name: " << fileName << endl;
        gtk_label_set_text(GTK_LABEL(labText),fileName.c_str());
    }
    gtk_window_destroy (GTK_WINDOW (dialog));
}

void OpenDialog (GtkWidget* wid, GtkWidget* data)
{
    //from 4.10 deprecated
    GtkWidget *dialog;
    GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;


    dialog = gtk_file_chooser_dialog_new ("Open File", GTK_WINDOW(data), action, ("_Cancel"), GTK_RESPONSE_CANCEL,
                                          ("_Open"), GTK_RESPONSE_ACCEPT, NULL);

    gtk_widget_show (dialog);
    g_signal_connect (dialog, "response", G_CALLBACK (Response),  NULL);
}

static void appActivate (GApplication *app, gpointer user_data)
{
    GtkWidget *win = gtk_application_window_new (GTK_APPLICATION (app));

    GtkWidget  *vBox = gtk_box_new (GTK_ORIENTATION_VERTICAL,10);
    gtk_window_set_child (GTK_WINDOW (win), vBox);

    labText = gtk_label_new("startovaci text");
    gtk_box_append (GTK_BOX (vBox), labText);

    GtkWidget *btnOpen = gtk_button_new_with_label("choose file 4.6");
    gtk_box_append  (GTK_BOX (vBox), btnOpen);
    g_signal_connect(GTK_BUTTON(btnOpen), "clicked", G_CALLBACK(OpenDialog), win);

    GtkWidget *btnOpen2 = gtk_button_new_with_label("choose file 4.10");
    gtk_box_append  (GTK_BOX (vBox), btnOpen2);
    g_signal_connect(GTK_BUTTON(btnOpen2), "clicked", G_CALLBACK(OpenDialog2), win);

    GtkWidget *btnBack = gtk_button_new_with_label("Close");
    gtk_box_append  (GTK_BOX (vBox), btnBack);
    g_signal_connect_swapped(GTK_BUTTON(btnBack), "clicked", G_CALLBACK(gtk_window_destroy), win);

    gtk_widget_show (win);
}

int main(int argc, char **argv)
{
    GtkApplication *app;
    app = gtk_application_new ("testing.app", G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app, "activate", G_CALLBACK (appActivate), NULL);
    return g_application_run (G_APPLICATION (app), NULL, NULL);
    g_object_unref (app);

    return 0;
}

You need to pass the Response2 callback to gtk_file_dialog_open(); the GtkFileDialog API is not signal based: it’s callback based (which translates to async/await on higher level languages with those constructs).

The Response2 callback must be modified to match a GAsyncReadyCallback function:

static void
Response2 (GObject *gobject, GAsyncResult *result, gpointer data)
{
  g_autoptr (GError) error = NULL;
  g_autoptr (GFile) file = gtk_file_dialog_open_finish (GTK_FILE_DIALOG (gobject), result, &error);

  if (error != NULL) {
    // the operation failed, or it was cancelled by the user
  } else {
    // do something with the file
  }
}

Thank you verry much!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.