How to set background/foreground on button, entry, or label?



Hi All,

After doing some research and hacking, I wrote some code to set the
foreground / backgroud color of a button, entry, & label. This is still a
confusing part of GTK 2.x., an internet search on this question resulted
in lots of partial answers, vague description on how it should work,
but no clear answer that work across all widgets.

I am posting this code to:
  1. Provide a working example on how I did it.
  2. Maybe the experts can improve on it, or show better methods.
  3. Maybe it (or an improved version) can be put in the faq.

There are still some problems:

1. The Entry widget's cursor doesn'tt change with the 'Text' color. In
   my example, the bg color & cursor is black = invisible cursor!

2. Different widgets use different properites in the style. I still
   don't understand the numberous style->fd, bg, light, dark, mid, text, base.

3. Tried using gtk_widget_modify_fg()/gtk_widget_modify_bg(), but
   never could get them to work on all widgets. So I used
   MyStyleSetItemColor() -> a function from my 1.2x code.

code is below my .signature.

Tony

/-----------------------------------------------------------------------\
| Tony Denault                     | Email: denault irtf ifa hawaii edu |
| Institute for Astronomy          |              Phone: (808) 932-2378 |
| 640 North Aohoku Place           |                Fax: (808) 933-0737 |
| Hilo, Hawaii 96720               |                                    |
\-----------------------------------------------------------------------/

#include <gtk/gtk.h>

void MyStyleSetItemColor( GdkColor color, char item, GtkStyle * style);

int main (int argc, char *argv[])
{
   GtkWidget * base,
             * table,
             * entry,
             * button,
             * label,
             * ebox;

   GtkStyle * style_default,
            * style_entry_YB,
            * style_button_YB;
   GdkColor black = { 0, 0, 0, 0 } ,       // should use gdk_colormap_alloc_color()
            yellow = {0, 65535, 65535, 0 };

   gtk_init(&argc, &argv);

   /* define some styles to apply to my widgets */
   style_default = gtk_widget_get_default_style();

   /* this style works for entry */
   style_entry_YB  = gtk_style_copy( style_default );
   MyStyleSetItemColor( black,   's', style_entry_YB);  // s=base (entry,bg)
   MyStyleSetItemColor( yellow,  't', style_entry_YB ); // t=Text (entry,fg)

   /* this style works for buttons */
   style_button_YB  = gtk_style_copy( style_default );
   MyStyleSetItemColor( black,  'b', style_button_YB );  // b=bg (button,bg)
   MyStyleSetItemColor( yellow, 'f', style_button_YB ); // f=fg (button,fg)

   base = gtk_window_new( GTK_WINDOW_TOPLEVEL );
   table = gtk_table_new( 1, 5, TRUE );
   gtk_container_add (GTK_CONTAINER (base), table);

   /* button - using style_button_YB */
   button = gtk_button_new_with_label( "button");
   gtk_widget_set_style(GTK_BIN (button)->child, style_button_YB);
   gtk_widget_set_style(button, style_button_YB);
   gtk_table_attach_defaults (GTK_TABLE(table), button, 0, 1, 0, 1);
   gtk_widget_show(button);

   /* entry - using style_entry_YB */
   entry = gtk_entry_new( );
   gtk_entry_set_text( GTK_ENTRY(entry), "Entry");
   gtk_entry_set_width_chars( GTK_ENTRY(entry), 5);
   gtk_table_attach_defaults (GTK_TABLE(table), entry, 2, 3, 0, 1);
   gtk_widget_set_style(entry, style_entry_YB);
   gtk_widget_show(entry);

   /* label w/ event box for backgroud color */
   ebox = gtk_event_box_new();
   label = gtk_label_new("label" );
   gtk_container_add( GTK_CONTAINER(ebox), label );
   gtk_widget_set_style( label, style_button_YB);   // change label fg color
   gtk_widget_set_style( ebox, style_button_YB);    // bg color provided by ebox
   gtk_table_attach_defaults (GTK_TABLE(table), ebox, 4, 5, 0, 1);
   gtk_widget_show(label);
   gtk_widget_show(ebox);

   gtk_widget_show(table);
   gtk_widget_show( base );
   gtk_main ();
   return 0;
}

/*-----------------------------------------------------------------------
**  MyStyleSetItemColor() - Helper function to change a style.
**-----------------------------------------------------------------------
*/
void MyStyleSetItemColor(
   GdkColor   color,    /* The allocated color to be added to the style */
   char       item,     /* the item to which the color is to be applied */
                        /* 'f' = foreground; 'b' = background; */
                        /* 'l' = light;      'd' = dark; */
                        /* 'm' = mid;        't' = text; */
                        /* 's' = base.  */
   GtkStyle * style     /* The old style - changes made to a copy */
)
{
   int i;
   switch ( item )
   {
      case 'f':
      case 'F':
         for ( i = 0; i < 5; i++ )
            style->fg[i] = color;
         break;
      case 'b':
      case 'B':
         for ( i = 0; i < 5; i++ )
            style->bg[i] = color;
         break;
      case 'l':
      case 'L':
         for ( i = 0; i < 5; i++ )
            style->light[i] = color;
         break;
      case 'd':
      case 'D':
         for ( i = 0; i < 5; i++ )
            style->dark[i] = color;
         break;
      case 'm':
      case 'M':
         for ( i = 0; i < 5; i++ )
            style->mid[i] = color;
         break;
      case 't':
      case 'T':
         for ( i = 0; i < 5; i++ )
            style->text[i] = color;
         break;
      case 's':
      case 'S':
         for ( i = 0; i < 5; i++ )
            style->base[i] = color;
         break;
   }
}




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