Re: Problem with GtkEntry



Thanks all for the comments,
Problem solved,

See you,

M

----- Original Message -----
From: "Olexiy Avramchenko" <olexiy irtech cn ua>
To: "Miguel Pignatelli" <pignatelli noraybio com>
Cc: <gtk-app-devel-list gnome org>
Sent: Thursday, November 13, 2003 12:10 PM
Subject: Re: Problem with GtkEntry


Miguel Pignatelli wrote:

Hi,

I have a problem with the widget GtkEntry. Please take a look to the
source code attached. It is a very simple program I am trying to run.
The problem is that I can't display the global variable where is
stored the text I got from de entry widget. I can't find the bug!!!

What is wrong with this code?
Thanx in advance for any help.

1. Duplicate the string with g_strdup(), dont use just a pointer
returned by gtk_entry_get_text(). Dont forget to call g_free() before
g_strdup to avoid memory leaking.
2. Dont do anything related to widgets after gtk_main - at this point
all widgets are destroyed. If you still want to print out the *option*
variable - be sure its initialized (use "destroy" callback on entry).

    Olexiy



----------------------------------------------------------------------------
----


/* example-start radiobuttons radiobuttons.c */

#include <gtk/gtk.h>

gchar *option;

/*----- Option Selection ----*/
void selected_option (GtkWidget *widget, GtkWidget *entry)
{
    g_free (option);
    option = g_strdup ( gtk_entry_get_text (GTK_ENTRY (entry)) );
    g_print ("Iterations: %s\n", option);
}

static void entry_destroy_callback (GtkWidget *entry)
{
    g_free( option );
    option = g_strdup ( gtk_entry_get_text (GTK_ENTRY (entry)) );
}

/*------- Closing Main Window ----------*/
gint close_application( GtkWidget *widget,
                        GdkEvent  *event,
                        gpointer   data )
{
    gtk_main_quit();
    return(FALSE);
}

/*--------- Closing the table --------------*/
void CloseDialog (GtkWidget *widget, gpointer data)
{
    gtk_widget_destroy (widget);
}

GtkWidget *CreateTable ()
{
    GtkWidget *button;
    GtkWidget *table;
    GtkWidget *entry1;
    GtkWidget *label1;

    table = gtk_table_new (6, 4, FALSE);

    entry1 = gtk_entry_new_with_max_length (10);

    gtk_signal_connect (GTK_OBJECT (entry1), "activate",
GTK_SIGNAL_FUNC (selected_option), entry1);
    gtk_signal_connect (GTK_OBJECT (entry1), "destroy",
GTK_SIGNAL_FUNC (entry_destroy_callback), entry1);

    gtk_widget_show (entry1);

    gtk_entry_set_text (GTK_ENTRY (entry1), "30");

    gtk_table_attach_defaults (GTK_TABLE (table), entry1, 2, 3, 1, 2);

/*--- Creating the label ---*/
    label1 = gtk_label_new ("Option: ");
    gtk_widget_show (label1);

    gtk_table_attach_defaults (GTK_TABLE (table), label1, 1, 2, 1, 2);

/*---- Creating "Accept" button ----*/
    button = gtk_button_new_with_label ("Accept");
    gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
                               GTK_SIGNAL_FUNC(CloseDialog),
                               GTK_OBJECT (table));
    gtk_table_attach_defaults (GTK_TABLE (table), button, 2, 3, 5, 6);
    GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
    gtk_widget_grab_default (button);
    gtk_widget_show (button);

    return (table);
}

int main( int argc, char *argv[] )
{
    GtkWidget *window = NULL;
    GtkWidget *table;

    gtk_init(&argc,&argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

    gtk_signal_connect (GTK_OBJECT (window), "delete_event",
                        GTK_SIGNAL_FUNC(close_application),
                        NULL);

    gtk_window_set_title (GTK_WINDOW (window), "DENIMPROT");
    gtk_container_set_border_width (GTK_CONTAINER (window), 0);

    table = CreateTable();

    gtk_container_add (GTK_CONTAINER (window), table);

    gtk_widget_show (table);
    gtk_widget_show (window);

    gtk_main();

    g_print ("Iterations: %s\n", option);
    return(0);
}
/* example-end */





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]