Re: [gtk-list] Set Label Text



On Thu, 04 Mar 1999 13:08:22 +0100, gtk-list@redhat.com (Andreas Scherf) wrote:
> i want to overwrite a label with a message for a specified time and want
> to bring the original message back after that.
> But the following code doesn´t do that:
> 
> void ndisplay_text(GtkWidget * label,gchar *text,guint sec)
> {
> char * tmpstring;
> gtk_label_get(GTK_LABEL(label),&tmpstring);
> gtk_label_set(GTK_LABEL(label),text);
> gtk_widget_show(label);
> sleep(sec);
> gtk_label_set_text(GTK_LABEL(label),tmpstring);
> }
> 
> The line with the gtk_label_set destroys my tmpstring how could i change
> this routine to work correctly ??

That's true because AFAIK you get the pointer to the real string (someone
correct me if I'm wrong), not a pointer to a copy of that string. 


void ndisplay_text(GtkWidget * label,gchar *text,guint sec)
{
  gchar * tmpstring;
  gchar * copystring;

  gtk_label_get(GTK_LABEL(label),&tmpstring);

  copystring = g_strdup(tmpstring);

  gtk_label_set(GTK_LABEL(label), text);
  gtk_widget_show(label);

  sleep(sec);

  gtk_label_set_text(GTK_LABEL(label),copystring);
  gtk_widget_show(label);

  g_free(copystring);
}


I don't think it's a good idea to sleep in GTK+, because this will make
your GUI not responsive at all. I think you should try a g_timer together
with a gtk_idle_add() function. Of course, this "trivial task" is "left as
an excersise to the reader" ;-) [1].


Erik

[1] Read: I don't have example code and I'm too lazy to write it, but I
    think it will work that way.

-- 
J.A.K. (Erik) Mouw, Information and Communication Theory Group, Department
of Electrical Engineering, Faculty of Information Technology and Systems,
Delft University of Technology, PO BOX 5031,  2600 GA Delft, The Netherlands
Phone: +31-15-2785859  Fax: +31-15-2781843  Email J.A.K.Mouw@its.tudelft.nl
WWW: http://www-ict.its.tudelft.nl/~erik/




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