Moving a GtkEntry to a char*



Hi everyone!

I've tried to get the text from a GtkEntry and then putting it into a 
variable defined as a char* with strncpy because i want it to stay :)

I'll explain myself.

I've defined a gtkEntry called txt_nom_canal:
  GtkWidget *txt_nom_canal;

and on the other side, a struct like this:

  typedef struct tipCanal {
    char *nom;   // here is where i want the string to be put in
    ...
  } tipCanal;

All right. I've tried on initializing, 
  [...]
  tipCanal canal
  canal.nom = " ";
  [...]

That's displayed ok.

Now, I've got the window definition:

  [...]
  txt_nom_canal = gtk_entry_new ();
  gtk_entry_set_max_length(GTK_ENTRY(txt_nom_canal), 20);
  gtk_widget_show (txt_nom_canal);
  [...]

Let's have a little fun:
(canal_vist is a tipCanal)

  gdk_threads_enter(); // I'm multithreading ;)
  printf("1\n");
  strncpy(canal_vist.nom, gtk_entry_get_text(GTK_ENTRY(txt_nom_canal)), 
20);
  printf("2\n");

It just displays "1" and segfaults. Don't know why. Also tried with 
strcpy(nom,entry).



Well, I then tried the following:

  gdk_threads_enter();
  printf("1\n");
  canal_vist.nom = gtk_entry_get_text(GTK_ENTRY(txt_nom_canal));
  printf("2\n);

This works fine (really?) NO! canal_vist, after some seconds, disappears. 
I think it's because gtk_entry_get_text returns a pointer that is reused 
by GTK, so I *must* copy the string somewhere (but i cannot). (At least 
the tutorial tells "The value returned by this function is used 
internally"). It must be with strcpy.

This must be very easy to do and I'm getting crazy! Isn't somewhere over 
there a gtk_entry_put_text_into( the_widget, something_defined_as_char*) ?

Lots of thanks in advance...

.-:Namarie:-.

p.d. Everything, using gtk1.2, so g_strcpy (does it exist?) maybe won't 
work...



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