How do I use colors?



Hi,

I'd like to insert some colorful text into a Gtktext widget.  How do I
specify the colors to that function?

I tried:

    #include <gtk/gtk.h>

    GdkColor red;

    void
    insert_red_text (GtkText *text, char *str)
    {
      if (!gdk_color_parse ("red", &red))
	printf ("can't parse color\n");
      else
	{
	  printf ("color: %02x %02x %02x\n", red.red, red.green, red.blue);
	  gtk_text_insert (text, NULL, &red, NULL, str, -1);
	}
    }

    int
    main (int argc, char **argv)
    {
      GtkWidget *window, *text;
      gtk_init (&argc, &argv);

      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      text  = gtk_text_new (NULL, NULL);
      gtk_container_add (GTK_CONTAINER (window), text);

      gtk_widget_realize (text);
      insert_red_text (GTK_TEXT (text), "Hi!");

      gtk_widget_show (text);
      gtk_widget_show (window);

      gtk_main ();
      return 0;
    }

and "Hi!" came out in green!

I suspect I have to allocate the color to some colormap somehow, or
something.  But how?  And which colormap?

My main point here is how to convert a color name such as "red" into a
useable GdkColor structure.  The conversion should be as independent
from any widget as possible because I want to do automatically in my
Guile bindings.

And, btw, GtkText stores pointers to GdkColors in its text properties.
Is this ok?  Are all GdkColor structures expected to have an infinite
lifetime?



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