Re: gtk_label_set_text



On Friday 07 January 2005 17:51, Dean McCullough wrote:

> I would like to use code simular to the following:
>
> char lab[10];
> sprintf(lab,"Level %d",level);
> gtk_label_set_text( GtkLabel *Llabel,
>                           lab);
>
> However, this does not work, since the second parameter to
> gtk_label_set_text is type const char.
>
> Any ideas of a simple way to accomplish this?

Put something like this into your foo-utils.h:

#include <gtk/gtklabel.h>

void  
foo_label_set_text  (GtkLabel *label, 
                               const gchar *format, ...) G_GNUC_PRINTF (2,3);

and into foo-utils.c:

#include <stdarg.h>
#include <gtk/gtklabel.h>

void              
foo_label_set_text (GtkLabel *label, const gchar *format, ...)
{
 va_list args;
 gchar   *s;

 g_return_if_fail (GTK_IS_LABEL (label));
 g_return_if_fail (format != NULL);

 if (format == NULL)
 {
  gtk_label_set_text ((GtkLabel*) label, "");
  return;
 }

 va_start (args, format);
 s = g_strdup_vprintf (format, args);
 va_end (args);
 
 gtk_label_set_text ((GtkLabel*) label, s);
 
 g_free (s);
}

and then do

   foo_label_set_text (GTK_LABEL (Llabel), "Level %d", level);

in your code. 

No idea why something like this has never been put into Gtk+ :-/

Cheers
 -Tim



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