GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu May 23, 2013 11:08 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Gtk_toggle_button: how can I change the color of the button?
PostPosted: Fri Apr 01, 2011 9:49 pm 
Offline
Familiar Face

Joined: Mon Aug 10, 2009 4:28 am
Posts: 17
Location: Bremen, Germany
Dear forum,

I'm working on a small visualization. I want to have a toggle button for "online" and "offline". One time the button should be green, one time red. After reading a lot of manuals and tutorials, it seems to me, that it is not possible to set the color of the button manually. Is there a way?

Thanks for reading and maybe a hint :-)

Michael


Top
 Profile  
 
 Post subject: Re: Gtk_toggle_button: how can I change the color of the but
PostPosted: Sat Apr 02, 2011 7:14 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 324
Location: Sydney, Australia
There is but you wont find it in the API for GtkButton.
The class system in gtk works by inheritance, with GtkToggleButton inheriting from GtkButton, etc. If you can't find a function you want directly from the particular widget used you may find it from those it inherits from. Colour is quite a general property so you find it quite high up the inheritance tree under GtkWidget.
The function that should do it is: gtk_widget_modify_bg
An example of its use that can be readily adapted to your needs is given in: http://stackoverflow.com/questions/9948 ... get-in-gtk


Top
 Profile  
 
 Post subject: Re: Gtk_toggle_button: how can I change the color of the but
PostPosted: Sun Apr 03, 2011 2:04 am 
Offline
Familiar Face

Joined: Mon Aug 10, 2009 4:28 am
Posts: 17
Location: Bremen, Germany
Hello Paul,

first of all, thanks for your help :-)

Paul Childs wrote:
There is but you wont find it in the API for GtkButton.
The function that should do it is: gtk_widget_modify_bg

An example of its use that can be readily adapted to your needs is given in: http://stackoverflow.com/questions/9948 ... get-in-gtk


I have compiled the example:

---------- SNIP ----------
#include <gtk/gtk.h>

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

int main (int argc, char* argv[])
{
GtkWidget* window;
GtkWidget* button;

gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT (window), "destroy",
G_CALLBACK (on_destroy), NULL);
button = gtk_button_new_with_label("Hello world!");
GdkColor red = {0, 0xffff, 0x0000, 0x0000};
GdkColor green = {0, 0x0000, 0xffff, 0x0000};
GdkColor blue = {0, 0x0000, 0x0000, 0xffff};
gtk_widget_modify_bg(button, GTK_STATE_NORMAL, &red);
gtk_widget_modify_bg(button, GTK_STATE_PRELIGHT, &green);
gtk_widget_modify_bg(button, GTK_STATE_ACTIVE, &blue);
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
---------- SNIP ----------

But the color of the button is not changing. It is still a gray button, and if I press them, it is a bit light blue, like all the other buttons in the system (by other programs).

What is wrong with my system, that I cannot overwrite the color?

Michael


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group