Return value from dialog


Hello all,
I need again help with my gtk4 app in C.

I need get response from dialog - which button was pressed.
In following code I have two functions that create dialog - but with different iccons and buttons.

In these functions is also some commented code which I want that runs after closing dialog and it should answer me, which button was pressed.

Please could anybody helps me how to change code to do it:

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

using namespace std;

int DialogoveOkno (GtkWidget *win,GtkMessageType ikonka, GtkButtonsType tlacitka, string zprava)
    GtkWidget *dialog = NULL;

    dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, ikonka, tlacitka, "%s",zprava.c_str());

    g_signal_connect_swapped (dialog, "response", G_CALLBACK (gtk_window_destroy),dialog);

void Btn1Act (GtkWidget* wid, GtkWidget* data) {
    cout << "btn1" << endl;

    int retVal = DialogoveOkno(GTK_WIDGET(data),GTK_MESSAGE_ERROR, GTK_BUTTONS_OK_CANCEL, "msg 1");
    cout << "ret=" << retVal << endl;
    if (retVal == OK) {
        cout << "you pressed OK" << endl;
    } else {
        cout << "you pressed CANCEL" << endl;

void Btn2Act (GtkWidget* wid, GtkWidget* data) {
    cout << "btn2" << endl;

    int retVal = DialogoveOkno(GTK_WIDGET(data),GTK_MESSAGE_WARNING, GTK_BUTTONS_YES_NO, "msg 2");
    cout << "ret=" << retVal << endl;
    if (retVal == YES) {
        cout << "you pressed YES" << endl;
    } else {
        cout << "you pressed NO" << endl;

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);

    GtkWidget *btn1 = gtk_button_new_with_label("1");
    gtk_box_append  (GTK_BOX (vBox), btn1);
    g_signal_connect(GTK_BUTTON(btn1), "clicked", G_CALLBACK(Btn1Act), win);

    GtkWidget *btn2 = gtk_button_new_with_label("2");
    gtk_box_append  (GTK_BOX (vBox), btn2);
    g_signal_connect(GTK_BUTTON(btn2), "clicked", G_CALLBACK(Btn2Act), 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 ("", 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;

Instead of connecting to gtk_window_destroy, use your own signal callback (non-swapped) and evaluate the response code/call gtk_window_destroy there,

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