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

Re: Color in GtkText



On Tue, 24 Nov 1998 sml13@cornell.edu wrote:
> I had a similar problem that I posted to GTK-list a while back, and 
> no-one really gave me a satisfactory response other than "you should use 
> themes to do this."  Well, no, I want it to be a static property of 
> the app so themes aren't an option.  What I wanted to do is set 
> the color of the text and background of a gtk_entry by doing (I thought):
> 
>     time = gtk_entry_new ();
>     style = gtk_style_new ();
>     style->font = gdk_font_load 
> ("-adobe-helvetica-bold-r-normal-*-*-240-*-*-p-*-iso8859-1");
>     style->text[GTK_STATE_NORMAL].red = 0;
>     style->text[GTK_STATE_NORMAL].green = 1;
>     style->text[GTK_STATE_NORMAL].blue = 0;
>     style->bg[GTK_STATE_NORMAL] = style->black;
>     gtk_widget_set_style (time, style);
> 
> You see, I want my time display to have alien green digits (this is for a 
> CD Player) on a black background...but this doesn't work!  I DO NOT want 
> this to vary with the themes, so theming to this isn't an option.  I want 
> to know how to do this (and, generallly, set the attributs of any widget 
> like this) PROGRAMMATICALLY.  Any ideas?

You need to call gdk_color_alloc() to get the color, and don't forget
that the red, green, blue fields are 16 bit quantities.  Something
like this seems to work best for me:

	GdkColor c;

	/* Create the new widget */
	time = gtk_entry_new();

	/* Allocate the new color */
	c.red = 0;
	c.green = 0xffff;
	c.blue = 0;
	gdk_color_alloc(gtk_widget_get_colormap(time), &c);

	/* Get a copy of the current style */
	style = gtk_style_copy(gtk_widget_get_style(time));

	/* Set the text color */
	style->text[GTK_STATE_NORMAL] = c;

	/* Throw away the old font */
	gdk_font_unref(style->font);

	/* Set the new font */
	style->font = font;

	/* Set the new style */
	gtk_widget_set_style(time, style);


Nigel Gamble                                    nigel@nrg.org
Mountain View, CA, USA.                         http://www.nrg.org/



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