Dear all,
thank you in advance for reading me, and hopefully helping out with my problem.
As mentioned above I am migrating my program to GTK4, and I am having some troubles
with the GtkDialog, in particular because the ‘gtk_dialog_run
’ command has been removed.
I have already browse the discussion threads and I have seen that this has been discussed but
not in the way I need, so I will be more specific for my purpose.
In my program I often use GtkDialog to ask for user input, find here a short piece of code
that is suppose to handle thing for GTK3 or GTK4 depending on the use of -DGTK4 at build:
void add_box_child_start (GtkWidget * widg, GtkWidget * child, gboolean expand, gboolean fill, int padding)
{
#ifdef GTK4
gtk_box_append (GTK_BOX(widg), child);
#else
gtk_box_pack_start (GTK_BOX(widg), child, expand, fill, padding);
#endif
}
GtkWidget * create_entry (GCallback handler_a, GCallback handler_b, gboolean key_release, gpointer data)
{
GtkWidget * entry = gtk_entry_new ();
gtk_entry_set_alignment (GTK_ENTRY(entry), 1.0);
if (handler_a != NULL) g_signal_connect (G_OBJECT (entry), "activate", handler_a, data);
#ifdef GTK4
if (key_release) g_signal_connect (G_OBJECT (entry), "changed", handler_b, data);
#else
if (handler_b != NULL) g_signal_connect (G_OBJECT (entry), "focus-out-event", handler_b, data);
if (key_release) g_signal_connect (G_OBJECT (entry), "key-release-event", handler_b, data);
#endif
return entry;
}
int user_param;
const gchar * entry_get_text (GtkEntry * entry)
{
#ifdef GTK4
return gtk_editable_get_text (GTK_EDITABLE(entry));
#else
return gtk_entry_get_text (entry);
#endif
}
G_MODULE_EXPORT void set_param (GtkEntry * entry, gpointer data)
{
int c = GPOINTER_TO_INT(data);
const gchar * m = entry_get_text (entry);
double v = atof(m);
user_param = (int) v;
}
#ifdef GTK4
G_MODULE_EXPORT void to_get_param (GtkEditable * widg, gpointer data)
#else
G_MODULE_EXPORT gboolean to_get_param (GtkWidget * widg, GdkEventFocus * event, gpointer data)
#endif
{
get_param (GTK_ENTRY(widg), data);
#ifndef GTK4
return FALSE;
#endif
}
G_MODULE_EXPORT void run_get_param (GtkDialog * dial, gint response_id, gpointer data)
{
destroy_this_widget (GTK_WIDGET(dial));
}
int get_param_from_user ()
{
GtkWidget * dialog = gtk_dialog_new ();
GtkWidget * entry;
GtkWidget * vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
GtkWidget * hbox;
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
add_box_child_start (vbox, hbox, FALSE, FALSE, 5);
add_box_child_start (hbox, gtk_label_new("Enter value here:"), FALSE, FALSE, 5);
entry = create_entry (G_CALLBACK (get_param), G_CALLBACK (to_get_param));
add_box_child_start (hbox, entry, FALSE, FALSE, 5);
user_param = -1;
gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
g_signal_connect (G_OBJECT(dialog), "response", G_CALLBACK(run_get_param), NULL);
show_the_widgets (dial);
// Here comes the issue:
/*
Previously with GTK3 only I add something that looked like:
if (gtk_dialog_run (dialog) == GTK_RESPONSE_ACCEPT)
{
return user_param;
}
else
{
return -1;
}
It was easy to ask to get a reponse before moving forward.
*/
// Now the following does not work, and I am kind of stuck:
return user_param;
}
int param = get_param_from_user ();
So how could I write that return function “int get_param_from_user ()
” to get that value from a GTK4 dialog using only the “response
” signal for the GtkDialog ?
Thanks in advance for your help.
Sébastien