LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-29-2008, 12:33 AM   #1
adz
Senior Member
 
Registered: Jun 2003
Location: Sydney
Distribution: Debian, FreeBSD
Posts: 1,713

Rep: Reputation: 53
C: GTK: invert foreground and background colours.


Hi there.

I'm brushing up on my C by learning some GTK and I'm trying to find a way to invert foreground and background colours on a particular widget of mine (EventBox + Label). I thought an easy way of doing this would be to get the current fg/bg colours and swap them around. I know how to SET colours using gtk_widget_modify_bg () and gtk_widget_modify_fg () but I don't know how to determine what colours are being used by default so that I can supply the reverse to the gtk_widget_modify_* functions.

If anyone knows how to do this or thinks that there is a better way to achieve what I'm trying to do, I'd like to hear from you.

Thanks in advance.
 
Old 08-30-2008, 12:11 PM   #2
wget
LQ Newbie
 
Registered: Apr 2008
Distribution: Slackware 12.0 Kernel 2.6.25, LFS 2.6.25.1 Kernel
Posts: 25

Rep: Reputation: 15
I've been looking at Gtk myself so pretty much a novice in it but I had a look at the docs and managed to swap the colours using the widgets styles. Do know if this is the best way but it's one way. This is the callback function I used:

Code:
static gboolean button_pressed(GtkWidget* eventbox, GdkEventButton* event, GtkLabel* label){

	GdkColor   *eventbox_background, *label_foreground;
	GtkStyle*  label_style;

	eventbox_background = eventbox->style->bg;
	
	label_style = gtk_widget_get_style( GTK_WIDGET(label) );
	label_foreground = label_style->fg;

	gtk_widget_modify_bg( eventbox, GTK_STATE_NORMAL, label_foreground);
	
	gtk_widget_modify_fg( GTK_WIDGET(label), GTK_STATE_NORMAL, eventbox_background);

}
 
Old 08-31-2008, 02:08 AM   #3
adz
Senior Member
 
Registered: Jun 2003
Location: Sydney
Distribution: Debian, FreeBSD
Posts: 1,713

Original Poster
Rep: Reputation: 53
Thanks. That worked, but not without some possible dodginess. You see, my app is hidden by default and only appears when you click the systray icon (it's a calendar app). So, before I can extract the appropriate styles, I need to quickly call gtk_widget_show () followed by gtk_widget_hide () for GTK to "refresh" it's memory about what the current theme actually is. If I don't do this I get fg = black and bg = white always. My default theme is a dark background with light text so it's really apparent when GTK gets it wrong.

Do you have any experience with this? Is what I've done acceptable? It works, that much is clear, but I'm not sure if there isn't a cleaner, more efficient way of doing this or if there may be unintended consequences in doing things this way.

Last edited by adz; 08-31-2008 at 02:13 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
tty background/foreground color neo77777 Linux - General 11 11-25-2014 08:59 AM
background process popping up in foreground noir911 Linux - General 1 12-14-2007 11:53 PM
Foreground and Background program switching isolvesystems Linux - Newbie 2 10-03-2007 09:30 AM
How to bring a background diamon to foreground lieuquocdat Linux - Software 8 07-20-2006 06:53 AM
Background and Foreground Datamike Linux - Software 2 01-20-2006 04:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration