GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Oct 01, 2014 2:18 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Retrieving the selection background color of a entry widget
PostPosted: Thu Mar 07, 2013 7:53 pm 
Offline

Joined: Thu Mar 07, 2013 3:13 pm
Posts: 3
Hello,

I'm trying to retrieve the selection background color of a entry widget but the RGB values I get does not look like the one used by the widget. The reason I want to get the selection background is because we have some custom widgets and we want to use the same color for highlighting text as other widgets.

Here is the code I use, please note that I'm currently using GDK version 2 so my problem occurs in the #else of the following code:

Code:
PCLColor GTKGetSelectedBackground(GtkWidget * wWidget)
{
#if GDK_MAJOR_VERSION > 2
    GdkRGBA sColor;

    GtkStyleContext * wStyle = gtk_widget_get_style_context(wWidget);

    gtk_style_context_get_background_color(wStyle, GTK_STATE_FLAG_SELECTED,
                                                                &sColor);

    return PCLColor((uchar) (sColor.red   * 255),
                            (uchar) (sColor.green * 255),
                            (uchar) (sColor.blue  * 255));
#else
    GdkColor sColor = gtk_widget_get_style(wWidget)->bg[GTK_STATE_SELECTED];

    return PCLColor((uchar) ((double) sColor.red    / USHRT_MAX * 255),
                            (uchar) ((double) sColor.green / USHRT_MAX * 255),
                            (uchar) ((double) sColor.blue   / USHRT_MAX * 255));
#endif
}

GtkWidget * wOffScreenWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);

// Highlight Color.
GtkWidget * wTextList = gtk_tree_view_new();
gtk_container_add(GTK_CONTAINER(wOffScreenWindow), wTextList);
oHighlightBackground = GTKGetSelectedBackground(wTextList);
oHighlightForeground = GTKGetSelectedForeground(wTextList);
gtk_widget_destroy(wTextList);

gtk_widget_destroy(wOffScreenWindow);


The returned color is Red=75, Green=105, Blue=131, but it doesn't look like the color used by the entry widget. I tried to get the color from the "base" array of the GtkStyle but the values are the same. I even tried to use the color map of the widget, but the pixel RGB values are the same. What am I missing?

Thanks,

Phelippe Neveu


Top
 Profile  
 
 Post subject: Re: Retrieving the selection background color of a entry wid
PostPosted: Fri Mar 15, 2013 8:24 pm 
Offline

Joined: Thu Mar 07, 2013 3:13 pm
Posts: 3
Here is some more information about my problem.

The GdkColor returned has the following values: Red: 19275, Green: 26985, Blue: 33667. The converted values: Red: 75, Green: 105, Blue: 131. I first thought that maybe the conversion I did from (unsigned short) to (unsigned char) made my color lose precision, but it doesn't in this case. I basically changed my code to use a new GTKColor class to hold the color in double so I don't lose any precision. But I still have the same problem. When I look at the color in the color selector, I can see that the color I draw matches the color returned by my function. But it isn't the color it should be. I'm using a cairo device to draw the color. Could it be that the GdkGC does some conversion of some kind? Should I be using a GdkGC for GTK+ 2.X and a cairo_t for GTK+ 3.X?

Help would be greatly appreciated.

Thanks,

Phelippe Neveu


Top
 Profile  
 
 Post subject: Re: Retrieving the selection background color of a entry wid
PostPosted: Wed Mar 20, 2013 11:31 pm 
Offline

Joined: Thu Mar 07, 2013 3:13 pm
Posts: 3
I found the answer to my question. Basically the background value wasn't the good one because the window wasn't realized. I fixed the problem by adding a call to gtk_widget_realize(wOffScreenWindow).

Cheers,

Phelippe Neveu


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 5 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