Vido
Vido

Reputation: 232

How to connect g_signal_connect in gtk without callig new function with G_CALLBACK ()

So I have code which go like this, my main

int main(int argc, char **argv)
{
button = GTK_WIDGET( gtk_builder_get_object( builder, "button2" ) );
g_signal_connect(button, "clicked", G_CALLBACK (show_dialog), NULL);
}

Then I have show_dialog which go like this

static void
show_dialog ( GtkWidget *button,
          gint       response_id,
          gpointer   user_data )
{
GtkBuilder *builder;
GtkWidget *filechoosedialog;

builder = gtk_builder_new();
gtk_builder_add_from_file( builder, GLADE_FILE, NULL );
fiilechoosedialog = GTK_WIDGET( gtk_builder_get_object(  
                                builder, 
                                "filechooserdialog1" ) );
/* Run dialog */
gtk_dialog_run( GTK_DIALOG( filechoosedialog ) );
gtk_widget_hide( filechoosedialog );

gtk_builder_connect_signals( builder, NULL );

g_object_unref( G_OBJECT(builder));
}

So now I would like to add this part to show dialog so I could detect when Open and Cancel buttons are pressed. In Glade I seted response id of Open button to be -5 and cancel to be -6 which corespond to GTK_RESPONSE_OK and GTK_RESPONSE_CANCEL but when I press Open button I do not get g_print executed

switch (response_id)
{
  case GTK_RESPONSE_OK:

        g_print ("Selected filename: %s\n", filename);
        g_print ("response idd: %d\n", response_id);
     break;
  default:

     break;
}

gtk_widget_destroy (GTK_WIDGET (filechoosedialog));

Upvotes: 0

Views: 568

Answers (1)

drahnr
drahnr

Reputation: 6886

GtkFileChooserDialog is a subclass to GtkDialog, so using gint gtk_dialog_run(GtkDialog *dialog); should work, which returns the response code.


// get hold of the return value of `gtk_run_dialog(...)`, lookup the type
response_id = gtk_dialog_run( GTK_DIALOG( filechoosedialog ) );
switch (response_id)
{
  case GTK_RESPONSE_OK:
        g_print ("Selected filename: %s\n", filename);
        g_print ("response idd: %d\n", response_id);
     break;
  default:
        g_print ("do'h! no file selected!\n");
     break;
}

// I am pretty sure you do not need this, this approach does not use the `updated` signal
gtk_builder_connect_signals( builder, NULL );

Upvotes: 1

Related Questions