Search code examples
cwindowsgtkgtkentry

Prevent GTK Entry from hiding text input when pressing TAB


I've a fairly strange problem using GTK 3.0 on Windows XP, 32-bit edition and compiling with MinGW. I've developped a small popup for network configuration with three text entries which are populated by a conf struct (not being shown here).

On first appearance, the first text entry's value (10) is invisible, and has to be revealed by left clicking on the box:

Screenshot showing GUI

After left-clicking : enter image description here

Moreover, by repeatedly pressing TAB, I can hide every text entries ! However, once an entry has been modified, it's not hidden anymore.

I've tried a lot of things (set_visibility, set_overwrite, "activation" signal, "focus" signal) but so far I haven't been able to solve the issue.

Here is the source code : Github Gist

And the Makefile (change the gtk src folder on your computer) : Github Gist

#include <gtk/gtk.h>
#include <glib/gstdio.h>

/*
 * Network Configuration window being popped-up
 */
typedef struct network_conf_t
{
    GtkWidget *window        ;
    GtkWidget *vbox_frames           ;
    GtkWidget *recv_frame            ;
    GtkWidget *send_frame            ;
    GtkWidget *recv_frame_hbox       ;
    GtkWidget *recv_port_number_label;
    GtkWidget *recv_port_number_entry;
    GtkWidget *vbox                  ;
    GtkWidget *hbox                  ;
    GtkWidget *hbox2                 ;
    GtkWidget * ip_address_label     ;
    GtkWidget * port_number_label    ;
    GtkWidget *ip_address_entry      ;
    GtkWidget *port_number_entry     ;

} network_conf;


unsigned int launch_netconf_popup()
{
    network_conf netconf;


    /*
       Window configuration
    */
    netconf.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_events           (           netconf.window , GDK_FOCUS_CHANGE_MASK);
    gtk_window_set_resizable        (GTK_WINDOW(netconf.window), FALSE);
    gtk_window_set_skip_taskbar_hint(GTK_WINDOW(netconf.window), TRUE );
    gtk_window_set_title            (GTK_WINDOW(netconf.window), "Network Configuration" );
    gtk_window_set_skip_pager_hint  (GTK_WINDOW(netconf.window), TRUE );
    g_signal_connect(G_OBJECT (netconf.window), "delete-event", G_CALLBACK(gtk_main_quit), NULL);

    gtk_window_set_position         (GTK_WINDOW(netconf.window), GTK_WIN_POS_MOUSE);



    netconf.vbox_frames = gtk_box_new  (GTK_ORIENTATION_VERTICAL, 2);
    netconf.recv_frame  = gtk_frame_new( "Receive Options");
    netconf.send_frame  = gtk_frame_new( "Send Options"  );

    gtk_container_add (GTK_CONTAINER (netconf.window),         netconf.vbox_frames);
    gtk_container_add (GTK_CONTAINER (netconf.vbox_frames),     netconf.recv_frame);
    gtk_container_add (GTK_CONTAINER (netconf.vbox_frames),     netconf.send_frame);


    /*
       Reception Port
    */
    netconf.recv_frame_hbox           = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 20);
    netconf.recv_port_number_label = gtk_label_new ("Port :");
    netconf.recv_port_number_entry = gtk_entry_new_with_buffer( gtk_entry_buffer_new("10", 8  ) );

    //gtk_entry_set_alignment ( GTK_ENTRY(netconf.recv_port_number_entry), 0x1 );   


    gtk_container_add   (GTK_CONTAINER (netconf.recv_frame), netconf.recv_frame_hbox);
    gtk_box_pack_start  (GTK_BOX (netconf.recv_frame_hbox) , netconf.recv_port_number_label, FALSE, FALSE, 5);
    gtk_box_pack_end    (GTK_BOX (netconf.recv_frame_hbox) , netconf.recv_port_number_entry, FALSE, FALSE, 5);

    /*
       Send Port and IP
    */
    netconf.vbox                = gtk_box_new(GTK_ORIENTATION_VERTICAL  , 2 );
    netconf.hbox                = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 20);
    netconf.hbox2               = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 20);
    netconf.ip_address_label    = gtk_label_new("IP address :"  );
    netconf.port_number_label   = gtk_label_new( "Port :"       );
    netconf.port_number_entry   = gtk_entry_new_with_buffer( gtk_entry_buffer_new("0", 8  ) );
    netconf.ip_address_entry    = gtk_entry_new_with_buffer( gtk_entry_buffer_new("127.0.0.1", 16 ));


    //gtk_entry_set_alignment( GTK_ENTRY(netconf.ip_address_entry) , 0x1    );
    //gtk_entry_set_alignment( GTK_ENTRY(netconf.port_number_entry), 0x1    );  

    gtk_container_add (GTK_CONTAINER (netconf.send_frame), netconf.vbox );    
    gtk_container_add (GTK_CONTAINER (netconf.vbox)      , netconf.hbox );
    gtk_container_add (GTK_CONTAINER (netconf.vbox)      , netconf.hbox2);

    gtk_box_pack_start  (GTK_BOX (netconf.hbox) , netconf.ip_address_label , FALSE, FALSE, 5);
    gtk_box_pack_start  (GTK_BOX (netconf.hbox2), netconf.port_number_label, FALSE, FALSE, 5);
    gtk_box_pack_end    (GTK_BOX (netconf.hbox) , netconf.ip_address_entry , FALSE, FALSE, 5);
    gtk_box_pack_end    (GTK_BOX (netconf.hbox2), netconf.port_number_entry, FALSE, FALSE, 5);

    gtk_widget_show_all(netconf.window);


    return 0x00;
}



int main(int argc, char **argv) {

    gtk_init(0, NULL);

    launch_netconf_popup();

    gtk_main();
    return 0;
}

Solution

  • This:

    gtk_entry_new_with_buffer(gtk_entry_buffer_new("10", 8))
    

    is a bug.

    If you check the documentation for gtk_entry_buffer_new(), you will see that the second argument is not the maximum length of the buffer's contents; it's the length of the default content, i.e. the first argument. And "10" is not 8 characters long, obviously. It should be 2 (or -1 to make GTK+ figure out the length).

    I expect this causes some confusion when GTK+ tries to treat your string as being longer than it is, probably triggers some undefined behavior (but I didn't try this).