Re: [gtk-list] Colors for entry widgets



On Fri, 31 Mar 2000, Joachim Backes wrote:

 Hi,

> Hi,
> 
> how do I set the background color und foreground color for text- or
> entry widgets dynamically?

  You should copy the default style of the widget, alter the colors you wish
for each state you wish, and apply the style to the widget.
  I attached my program I coded when was researching this issue long ago.
It compiles as C++ (didn't try it as C). Type something in the entry in the
main window - the text will be red when length of input is odd and will be
default color if length is even.
 
> Regards
> 
> Joachim Backes
> 
> --
> 
> Joachim Backes <backes@rhrk.uni-kl.de>       | Univ. of Kaiserslautern
> Computer Center, Supercomputing Division     | Phone: +49-631-205-2438 
> D-67653 Kaiserslautern, PO Box 3049, Germany | Fax:   +49-631-205-3056 
> ---------------------------------------------+------------------------
> WWW: http://sgi400.rhrk.uni-kl.de/home/staff/backes.html  
> 
> -- 
> To unsubscribe: mail -s unsubscribe gtk-list-request@redhat.com < /dev/null
> 

 Best regards,
  -Vlad
-------------------
#include <gtk/gtk.h>
#include <stdio.h>

GtkStyle* newstyle,*defstyle;

//this sets color of text to red when the length of entered text is odd
void entry_changed(GtkWidget* entry)
{
    if (!newstyle) {
	//this is called only once
	newstyle = gtk_style_copy(gtk_widget_get_style(entry));
	defstyle = gtk_style_copy(gtk_widget_get_style(entry));
	GdkColor tcolor;
	gdk_color_parse("red",&tcolor);
	for(int i=0;i<5;++i)
	    newstyle->fg[i] = tcolor;    
    };
    if (strlen(gtk_entry_get_text(GTK_ENTRY(entry)))&1) {
	gtk_widget_set_style(entry,newstyle);
    } else
	gtk_widget_set_style(entry,defstyle);
};

/*to test how changing styles affects other widgets*/
void btn_clicked()
{
    GtkWidget *window;
    GtkWidget *button;
    GtkWidget *box1,*hbox,*vbox,*e;
    GtkTable *table;

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "Timeout test");

    e = gtk_entry_new();
    gtk_container_add(GTK_CONTAINER(window),e);
    
    gtk_widget_show_all(window);
};

int main(int argc,char** argv)
{
    GtkWidget *window;
    GtkWidget *button;
    GtkWidget *box1,*hbox,*vbox,*e;
    GtkTable *table;
	
    gtk_init (&argc, &argv);
		    
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "Timeout test");
    
    vbox = gtk_vbox_new(0,4);
    gtk_container_add(GTK_CONTAINER(window),vbox);
    
    button = gtk_button_new_with_label("New window");
    gtk_box_pack_start(GTK_BOX(vbox),button,1,1,0);
    gtk_signal_connect(GTK_OBJECT(button),"clicked",GTK_SIGNAL_FUNC(btn_clicked),NULL);
    
    e = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(vbox),e,1,1,0);
    gtk_signal_connect(GTK_OBJECT(e),"changed",GTK_SIGNAL_FUNC(entry_changed),NULL);
    
    gtk_widget_show_all(window);
    gtk_main ();    
};



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