I have an application with two buttons ( open and close ) and I try to set default on button 2 (close button).
I am trying to work with the gtk_window_set_default() and based on the manual before this call I need to do another call of the gtk_widget_set_can_default() function, which I did.
With the help of the gtk_window_activate_default() function I check activate default.
The problem is that, when I hit enter the open button gets pressed and not the close button like I was expected.
I think that I misunderstood this functions.
Here is a demo program:
#include <gtk/gtk.h>
int main ( void )
{
GtkWidget *window;
GtkWidget *grid;
GtkWidget *button1;
GtkWidget *button2;
/// ***
gtk_init ( NULL, NULL );
/// ***
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
grid = gtk_grid_new();
gtk_container_add( GTK_CONTAINER( window ), grid );
button1 = gtk_button_new_with_label( "Open" );
g_object_set( button1, "margin", 50, NULL );
button2 = gtk_button_new_with_label( "Close" );
gtk_widget_set_name( button2, "close" );
g_object_set( button2, "margin", 50, NULL );
GtkWidget *get_default;
gtk_widget_set_can_default( button2, TRUE );
gtk_window_set_default( GTK_WINDOW( window ), button2 );
get_default = gtk_window_get_default_widget( GTK_WINDOW( window ) );
const gchar *const get_name = gtk_widget_get_name( get_default );
if ( gtk_window_activate_default ( GTK_WINDOW( window ) ) )
{
g_print( "The default windget got Activated\n" );
g_print( "The default Widget is now %s\n", get_name );
}
gtk_grid_attach( GTK_GRID( grid ), button1, 0, 0, 1, 1 );
gtk_grid_attach( GTK_GRID( grid ), button2, 1, 0, 1, 1 );
/// *** Show the Window
///gtk_widget_show( window );
gtk_widget_show_all( window );
gtk_main ();
}
Output:
The default windget got Activated
The default Widget is now close
The close button is indeed de default one, but when I hit enter the Open button gets clicked.
I know that this thing does
gtk_window_set_accept_focus ()
gtk_window_set_focus ()
gtk_window_get_focus ()
But I am not sure how gtk_window_set_default() works.