[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]
Re: Background & foreground color of buttons & label
- From: "Donna S. Martin" <donna omartin com>
- To: <gtk-app-devel-list redhat com>
- Subject: Re: Background & foreground color of buttons & label
- Date: Fri, 1 Oct 1999 15:12:20 -0600
Tony,
The label widget is a window-less widget, so you can't
set its background color. Normally, you need
to set the background color of its parent, but in your case,
the parent is a box and is in charge of the button as well. I believe
if you set the box's background color, and have any padding
you'll see the color there, too. So, you could alternatively insert an
event box as the label's parent (which has a window) and set the event-box's
background color.
Hope this helps!
Donna
P.S. There is also the standard "it's not a good idea to mess
with the colors because you're overriding user settings" which
is not considered nice (there's always exceptions).........since
you don't know what the user settings are, you could end up creating
something that can't be read.
----- Original Message -----
From: Tony Denault <denault@hawaii.edu>
To: <gtk-app-devel-list@redhat.com>
Sent: Thursday, September 30, 1999 5:36 PM
Subject: Background & foreground color of buttons & label
>
> How can specify the [background/foreground] color of my [button/label]?
>
> Everytime this question gets posted, there are suggestions given. However,
> each answer is slightly different and don't provide a complete answer.
> (There is nothing like an actually working example!)
>
> This should have been put in the FAQ or an example program in the GTK+
> distrubtion should be provided.
>
> I am also struggling with this problem. I wish to change the color
> of my widget during run time. I tried writing a small example
> program which _almost_ does the trick. It displays a button and label.
> Clicking the button will change the foregound/background colors. (however,
> the background color of the label doesn't change? - Help!)
>
> I'm not sure if this is the right way or best technique.
> I'll throw it out here for critique - And please do so.
> Maybe a gtk 'expert' can provide a better solution.
>
> Source code below my signature.
>
> Regards,
> Tony
>
>
/---------------------------------------------------------------------------
--\
> | Tony Denault | Internet:
denault@irtf.ifa.hawaii.edu |
> | NASA IRTF, Institute of Astronomy | Phone: (808)
974-4206 |
> | 1175 Manono St., Bldg 393 | Fax: (808)
974-4207 |
> | Hilo, Hawaii 96720 |
|
>
\---------------------------------------------------------------------------
--/
>
>
/***************************************************************************
> ** bls.c - Buttons, Labels, & styles
>
***************************************************************************
> */
>
> /* include files */
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> #include <gtk/gtk.h>
>
> /* function prototypes */
>
> int main( int argc, char *argv[] );
> void hitme_cb( GtkWidget *widget, gpointer data );
> void MyStyleSetItemColor( GdkColor color, char item, GtkStyle * style);
>
> /* global variables */
>
> GtkStyle * Style_RedBlue;
> GtkStyle * Style_GreenBlue;
> GtkStyle * Style_Default;
> GtkWidget * MyLabel;
>
> GdkColor Red = {0, 0xbbbb, 0, 0};
> GdkColor Blue = {0, 0, 0, 0xbbbb};
> GdkColor Green = {0, 0, 0xbbbb, 0};
>
> /*-----------------------------------------------------------------------
> ** main()
> **-----------------------------------------------------------------------
> */
> int main( int argc, char *argv[] )
> {
> GtkWidget * base_window;
> GtkWidget * box;
> GtkWidget * widget;
> GdkColormap * colormap;
>
> /* initialize gtk & and create base_window. */
> gtk_init(&argc, &argv);
>
> base_window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
>
> /* allocate some colors */
> colormap = gdk_colormap_get_system();
> gdk_colormap_alloc_color(colormap, &Red, FALSE, TRUE );
> gdk_colormap_alloc_color(colormap, &Blue, FALSE, TRUE );
> gdk_colormap_alloc_color(colormap, &Green, FALSE, TRUE );
>
> /* allocate style & set foreground/backgroud colors */
> Style_Default = gtk_widget_get_default_style();
> Style_RedBlue = gtk_style_copy( Style_Default );
> Style_GreenBlue = gtk_style_copy( Style_Default );
>
> MyStyleSetItemColor( Red, 'f', Style_RedBlue );
> MyStyleSetItemColor( Blue, 'b', Style_RedBlue );
>
> MyStyleSetItemColor( Green, 'f', Style_GreenBlue );
> MyStyleSetItemColor( Blue, 'b', Style_GreenBlue );
>
> /* container with button(Hit Me) & Label (Hello) */
> box = gtk_hbox_new( FALSE, 0 );
> gtk_container_add (GTK_CONTAINER (base_window), box);
> gtk_widget_show( box );
>
> widget = gtk_button_new_with_label( " Hit Me ");
> gtk_signal_connect (GTK_OBJECT (widget), "clicked",
> GTK_SIGNAL_FUNC(hitme_cb), (gpointer) NULL);
> gtk_box_pack_start( GTK_BOX(box), widget, TRUE, TRUE, 0);
> gtk_widget_show(widget);
>
> widget = gtk_label_new(" Hello ");
> gtk_box_pack_start( GTK_BOX(box), widget, TRUE, TRUE, 0);
> gtk_widget_show(widget);
> MyLabel = widget;
>
> gtk_widget_show( base_window );
> gtk_main ();
>
> return 0;
> }
>
>
/*--------------------------------------------------------------------------
--
> ** hitme_cb() - standard button cb.
>
**--------------------------------------------------------------------------
--
> */
> void hitme_cb( GtkWidget *widget, gpointer data )
> {
> char msg[60];
> static int counter;
>
> /* toggle the background/foreground of the button.
> ** note, foreground is changed by referencing the button's label
(widget->child).
> */
> if( counter % 2 )
> {
> gtk_widget_set_style( GTK_WIDGET(GTK_BIN(widget)->child),
> Style_RedBlue );
> gtk_widget_set_style( widget, Style_RedBlue );
> }
> else
> {
> gtk_widget_set_style( GTK_WIDGET(GTK_BIN(widget)->child),
> Style_GreenBlue );
> gtk_widget_set_style( widget, Style_GreenBlue );
> }
>
> /* toggle the background/foreground of the MyLabel.
> */
> if( counter % 2 )
> {
> gtk_widget_set_style( MyLabel, Style_RedBlue );
> }
> else
> {
> gtk_widget_set_style( MyLabel, Style_GreenBlue );
> }
> sprintf( msg, " counter = %d ", counter);
> gtk_label_set_text( GTK_LABEL(MyLabel), msg );
>
> counter++;
> }
>
> /*-----------------------------------------------------------------------
> ** 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++ )
> 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;
> }
> }
>
> /**** the end ****/
>
>
>
> --
> To unsubscribe: mail gtk-app-devel-list-request@redhat.com with
> "unsubscribe" as the Subject.
>
> Mailing list concerns should be mailed to <listmaster@redhat.com>
>
>
>
[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]