GtkProgressBar colors: Help with this FAQ



There seems to be an ongoing problem regarding the use of colors
in GtkProgressBar. See, for example, the following threads:
http://mail.gnome.org/archives/gtk-app-devel-list/1999-December/msg00276.htm
l
http://mail.gnome.org/archives/gnome-devel-list/2001-January/msg00094.html
http://mail.gnome.org/archives/gtk-app-devel-list/2001-December/msg00123.htm
l
http://mail.gnome.org/archives/gtk-app-devel-list/2003-May/msg00118.html
http://mail.gnome.org/archives/gtk-list/2003-August/msg00023.html
http://mail.gnome.org/archives/gtk-app-devel-list/2003-April/msg00001.html
http://mail.gnome.org/archives/gtk-list/2003-August/msg00125.html
http://mail.gnome.org/archives/gtk-app-devel-list/2004-February/msg00521.htm
l

In gtk/gtkprogressbar.h one can find four functions 
of the "gtk_progress_bar_set_" variety, none of which address the widget's
use of color. Nor do the properties in GtkProgressBar deal with color.
This post is an effort to elicite a definitive solution, which may become
a new FAQ. (Yes, I am aware of "Setting Colors in GTK+")

Despite warnings about how "setting your own colors" affects the
"consistency
and accessibility the theme", there are times when one requires more from
this widget. For example, the developer in
http://mail.gnome.org/archives/gtk-app-devel-list/2000-April/msg00363.html
wanted a progress-bar button. 

Some developers, as in
http://mail.gnome.org/archives/gtk-app-devel-list/2004-July/msg00119.html
wish to create their own customized progress bars.

In fact, the current design seems to be based on the proposal by Stefan
Jeske:
http://mail.gnome.org/archives/gtk-list/1998-June/msg00647.html


Proposed Solutions to the Color Problem:
It has been suggested that one can color the indicator bar using
gtk_widget_modify_bg(GtkWidget* pbar, GTK_STATE_PRELIGHT, GdkColor* color);
 
while the color of the well can be changed with
gtk_widget_modify_bg(GtkWidget* pbar, GTK_STATE_NORMAL, GdkColor* color);

But these calls do not always work. Several respondents state that the
problem
stems from certain "engine themes" (like bluecurve).

Question 1: Can somebody explain this in more detail? In particular, how do
you "disable a theme", and what are the dangers when porting to another 
machine?

So far, the only solution that I have found employs GtkStyle.
I've included some code (based on one of the posts above) which only 
partially works: the lines (in main)
   SetIndicatorColor(pbar, "green");
   SetWellColor(pbar, "red");
do not work jointly (as seen by reversing the order).

Question 2: Can somebody offer a fix?


Others have suggested using GtkRcStyle and rc files as outlined (briefly!)
in
Pennington's mini-FAQ:
http://ometer.com/gtk-colors.html

Question 3: How do you implement this technique, and how is it
different/better
than the GtkStyle approach?


Test Code:  (Feel free to annotate)
----------------------------------------------------------------------------
-
#include <gtk/gtk.h>

void CloseGUI (GtkWidget* widget, gpointer data) {
   gtk_main_quit();
}

void SetIndicatorColor(GtkWidget* progbar, const gchar *spec) {
   GtkStyle* style;

   style = gtk_style_new ();
   gdk_color_parse (spec, &(style->bg[GTK_STATE_PRELIGHT]));
   gtk_widget_set_style (progbar, style);
   g_object_unref (style);
}

void SetWellColor(GtkWidget* progbar, const gchar *spec) {
   GtkStyle* style;

   style = gtk_style_new ();
   gdk_color_parse (spec, &(style->bg[GTK_STATE_NORMAL]));
   gtk_widget_set_style (progbar, style);
   g_object_unref (style);
}

gint main(gint argc,gchar *argv[]) {
   GtkWidget* window;
   GtkWidget* pbar;
 
   gtk_init (&argc,&argv);
   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   g_signal_connect(G_OBJECT (window), "destroy", 
                    G_CALLBACK (CloseGUI), NULL);

   pbar = gtk_progress_bar_new();
   SetIndicatorColor(pbar, "green");
   SetWellColor(pbar, "red");

   gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pbar),0.5);
   gtk_container_add(GTK_CONTAINER(window),pbar);
   gtk_widget_show_all (window);
   gtk_main();
   return 0;
}
----------------------------------------------------------------------------
- 

As soon as I get a good working solution, I plan to enhance the code
and make it available. Newbies like me can use all the examples
they can find. Thanks for your help.


Mike E.



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