Re: [gtk-list] How can I change the color ?



> 
> Hi,
Hi,

> 
> I'm using some code to create a tree using the gtk_tree widget.
> I want some items of the tree to have a different color
> so I use the code :
> 
> gdk_color_parse( "red", &fgcolor);
> fgstyle = gtk_widget_get_style (GTK_WIDGET (item) );
> fgstyle->fg[GTK_STATE_NORMAL] = fgcolor;
> 
> but all I do is change the color of all widgets ...
When you use gtk_widget_get_style, you get back a pointer on default style
which is used by all widget.

And when you modify this style, you modify all widget style.

You must create a new style, modify it, and attach it at your widget.
But that not work with gtktreeitem which is a composite widget.

It seem that style are not propagate to child of container (bug or not, i don't
known).

> 
> Do I have to create a new style and how do I do that ?
Yes ... this sample of code work (on my cvs release)

      ...
      gdk_color_parse( color[i], &fgcolor); /* with on color name
					       by item */	

      fgstyle = gtk_style_new();
      fgstyle->fg[GTK_STATE_NORMAL] = fgcolor;

      gtk_widget_push_style(fgstyle);

      item = gtk_tree_item_new_with_label(color[i]);
      gtk_widget_set_style(item, fgstyle);

      gtk_widget_pop_style();

      gtk_tree_append(GTK_TREE(root_subtree), item);
      ...

I don't known if this is the best solution but the result is good.

> And how do I "copy" the default style to a new style ?
> 
> Thanx for any help ...
> Mike &

There is a little sample next with a small tree with color.

a++

========================================================================
#include <gtk/gtk.h>

void
test(GtkWidget* parent)
{
  GtkWidget* root;
  GtkWidget* root_item;
  GtkWidget* root_subtree;
  GtkWidget* item;
  GtkStyle* fgstyle;
  GdkColor fgcolor;
  gint i;

  char* color[3] = {
    "red",
    "green",
    "blue"
  };

  root = gtk_tree_new();
  gtk_box_pack_start(GTK_BOX(parent), root, TRUE, TRUE, 0);
  gtk_widget_show(root);
  
  root_item = gtk_tree_item_new_with_label("root");
  gtk_tree_append(GTK_TREE(root), root_item);
  gtk_widget_show(root_item);

  root_subtree = gtk_tree_new();

  for(i = 0; i<3; i++)
    {
      gdk_color_parse( color[i], &fgcolor);

      fgstyle = gtk_style_new();
      fgstyle->fg[GTK_STATE_NORMAL] = fgcolor;

      gtk_widget_push_style(fgstyle);

      item = gtk_tree_item_new_with_label(color[i]);
      gtk_widget_set_style(item, fgstyle);

      gtk_widget_pop_style();

      gtk_tree_append(GTK_TREE(root_subtree), item);
      gtk_widget_show(item);
    }

  gtk_tree_item_set_subtree(GTK_TREE_ITEM(root_item), root_subtree);
  gtk_tree_item_expand(GTK_TREE_ITEM(root_item));
}

void 
main(int argc, char** argv) 
{
  GtkWidget* window;
  GtkWidget* box1;
  GtkWidget* separator;
  GtkWidget* button;

  gtk_init (&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "Color Tree");
  gtk_signal_connect (GTK_OBJECT (window), "delete_event",
		      (GtkSignalFunc) gtk_main_quit, NULL);
  box1 = gtk_vbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(window), box1);
  gtk_widget_show(box1);

  test(box1);

  separator = gtk_hseparator_new();
  gtk_box_pack_start(GTK_BOX (box1), separator, FALSE, TRUE, 0);
  gtk_widget_show (separator);

  button = gtk_button_new_with_label("Close");
  gtk_box_pack_start(GTK_BOX (box1), button, FALSE, TRUE, 0);
  gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
			    (GtkSignalFunc)gtk_main_quit, 
			    NULL);
  gtk_widget_show(button);

  gtk_widget_show(window);

  gtk_main();
}

-- 
-----------------------------------------------------------------------
    _/ _/_/  _/  _/_/   _/_/_/ _/_/_/ Bolliet Jerome
   _/ _/ _/ _/ _/  _/   /  _/     _/  email: bolliet@in2p3.fr
  _/ _/  _/_/    _/   _/_/_/   _/_/   
 _/ _/    _/   _/    _/         _/    Equipe Administration Systeme
_/ _/    _/  _/_/_/ _/     _/_/_/     email: sysunix@in2p3.fr
Centre de Calcul - Campus de la Doua
  27 boulevard du 11 novembre 1918    Tel.: 04 78 93 08 80
   69622 - VILLEURBANNE - FRANCE      Fax.: 04 78 94 30 54
-----------------------------------------------------------------------



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