Re: How to change GTK+3 label color dynamically

2017-03-16 Thread Rúben Rodrigues
Thanks! In some containers we can't change color of label? I have labels 
inside GtkLayout, and don't change color with Css but other labels out 
of this layout change.

Why don't works?

I do this dynamically but don't works too:

GtkCssProvider *css_provider = gtk_css_provider_new();
 gtk_css_provider_load_from_data (css_provider,
  "#label_Sensors {"
  "  color: red;"
  "}",
  -1,
  NULL);
 gtk_style_context_add_provider 
(gtk_widget_get_style_context(gtk_builder_get_object(builder,"Home_Sensors_Spare_window")),
 GTK_STYLE_PROVIDER (css_provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
 g_object_unref(css_provider);




On 16-03-2017 10:23, Stefan Salewski wrote:
> On Thu, 2017-03-16 at 09:55 +, Rúben Rodrigues wrote:
>> Hi,
>>
>> THanks. This is dynamically? I need in c language if possible :S
>>
>> Thanks
> Yes. The example
>
> https://github.com/ngtk3/nim-gtk3/blob/master/test/colors.nim
>
> is dynamically, I used it to generate some color schemes. Entered color
> hex value in a text entry widget, and that color was applied.
>
> As I said, it was working for 3.18. I think for 3.20 and above some CSS
> names have changed, I had not the time to really investigate that.
>
> I have no C code currently, I think my Nim code was inspired by Python
> code, but I have no link to that python code currently. For C it is the
> same pattern -- generate a CSS string and apply it.

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: How to change GTK+3 label color dynamically

2017-03-16 Thread Richard Shann
From: R?ben Rodrigues 

> On 16-03-2017 11:20, Tilo Villwock wrote:
> > Am Donnerstag, den 16.03.2017, 09:55 + schrieb R?ben Rodrigues:
> >> Hi,
> >>
> >> THanks. This is dynamically? I need in c language if possible :S
> > Dynamic version:
> >
> > ...
> >
> > static GtkCssProvider* provider = NULL;
> >
> > static void
> > set_label_color(GtkWidget* label, const char* color)
> > {
> >  const char* format = "label { color: %s; }";
> >  size_t length = strlen(format) - 2 + 1;
> >  char style[length];
> >  sprintf(style, format, color);
> >
> >  if (provider == NULL) {
> >  /* only create and add the provider the first time */
> >  provider = gtk_css_provider_new();
> >  gtk_style_context_add_provider(
> >  gtk_widget_get_style_context(label),
> >  GTK_STYLE_PROVIDER(provider),
> >  GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
> >  g_object_unref(provider);
> >  }
> >
> >  gtk_css_provider_load_from_data(provider, style, -1, NULL);
> > }
> >
> > ...
> >
> > This might not be ideal in all situations. Just make sure you don't
> > create a new provider everytime you set a new color. Hope that helps.
> >
> > --Tilo
> 

> Hi,
> 
> This is what i have, and don't works...
> 

I have this function (for any widget type):

void set_background_color(GtkWidget *w, gchar *color)
{
GtkCssProvider *gcp;
GtkStyleContext *gsc;
gsc = gtk_widget_get_style_context(w);
const gchar *type = g_type_name (G_TYPE_FROM_INSTANCE (w));
gchar *str = g_strdup_printf ("%s {background-color: %s;}", type,
color);
gcp= gtk_css_provider_new();
gtk_css_provider_load_from_data(gcp, str, -1, 0);
g_free (str);
gtk_style_context_add_provider(gsc, GTK_STYLE_PROVIDER(gcp), 
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}

I don't know if there is a memory leak here though...

HTH

Richard Shann

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: How to change GTK+3 label color dynamically

2017-03-16 Thread Rúben Rodrigues
You are right. I don't know why don't works before, because I had 
already tested and doesn't work... BU apparently now works with 
gdk_screen_get_default();

THanks for your help.



On 16-03-2017 14:07, Tilo Villwock wrote:
> Am Donnerstag, den 16.03.2017, 13:50 + schrieb Rúben Rodrigues:
>> I get this error:
>>
>> gtk-CRITICAL **: gtk_style_context_add_provider_for_screen:
>> assertion
>> 'GDK_IS_SCREEN (screen)' failed
>>
>> COde:
>> gtk_style_context_add_provider_for_screen(gtk_widget_get_style_contex
>> t(GTK_WIDGET(gtk_builder_get_object(builder,"window_Main"))),
>>   GTK_STYLE_PROVIDER (css_provider),
>>   G_MAXUINT);
> Of course you do. The first argument is meant to be a GdkScreen object.
> Which makes me wonder if you even read the documentation. Anyways, you
> can obtain the object by invoking
>
>  gdk_screen_get_default()
>
> and the last argument is supposed to be a priority constant like
>
>  GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
>
> Look them up. I'd also recommend installing devhelp and gtk3-docs if
> you haven't already.
>
> --Tilo

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: How to change GTK+3 label color dynamically

2017-03-16 Thread Rúben Rodrigues
OK, now works:

 if(css_provider == NULL){
 css_provider = gtk_css_provider_new();

apply_css(gtk_builder_get_object(builder,"window_Main"),css_provider);
 g_object_unref(css_provider);

 }
 gtk_css_provider_load_from_data (css_provider,"#label_Sensors { 
color: red; }",-1,NULL);


static void apply_css(GtkWidget *widget, GtkStyleProvider *provider)
{
gtk_style_context_add_provider(gtk_widget_get_style_context(widget), 
GTK_STYLE_PROVIDER(provider),G_MAXUINT);
 if(GTK_IS_CONTAINER(widget))
 gtk_container_forall(GTK_CONTAINER(widget),(GtkCallback) 
apply_css,provider);

}

I need to create a different selector for each of label that i want to 
change to red color?


On 16-03-2017 13:19, Emmanuele Bassi wrote:
> You keep using an id in the selector, but you never show how you set
> the id on the widget. I suspect you think the buildable ID you use
> with GtkBuilder is also the ID used when theming — which is definitely
> not the case. You will need gtk_widget_set_name(), instead. Of course,
> that comes with its own set of caveats, most importantly: there's no
> uniqueness of widget name enforced by GTK+ itself.
>
> If you want to avoid all of this, use a CSS class; you can add a class
> programmatically using gtk_style_context_add_class() or you can
> specify the class in the GtkBuilder XML file.
>
> Ciao,
>   Emmanuele.
>
>
> On 16 March 2017 at 12:49, Rúben Rodrigues  wrote:
>> I provide my code in previous emails.
>>
>> This is my code now:
>>
>> if(css_provider == NULL){
>>   css_provider = gtk_css_provider_new();
>>
>>   gtk_style_context_add_provider
>> (gtk_widget_get_style_context(GTK_WIDGET(gtk_builder_get_object(builder,"window_Main"))),
>>   GTK_STYLE_PROVIDER (css_provider),
>>   GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
>>   g_object_unref(css_provider);
>>
>>   }
>>   gtk_css_provider_load_from_data (css_provider,"#label_Sensors {
>> color: red; }",-1,NULL);
>>
>>
>> On 16-03-2017 12:41, Tilo Villwock wrote:
>>> Am Donnerstag, den 16.03.2017, 11:58 + schrieb Rúben Rodrigues:
 Hi,

 This is what i have, and don't works...
>>> The code I provided works. What GTK3 version are you using? Also nobody
>>> will be able to help you if you don't show us your code.
>>>
>>> --Tilo
>> ___
>> gtk-app-devel-list mailing list
>> gtk-app-devel-list@gnome.org
>> https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
>
>

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: How to change GTK+3 label color dynamically

2017-03-16 Thread Tilo Villwock
Am Donnerstag, den 16.03.2017, 13:50 + schrieb Rúben Rodrigues:
> I get this error:
> 
> gtk-CRITICAL **: gtk_style_context_add_provider_for_screen:
> assertion 
> 'GDK_IS_SCREEN (screen)' failed
> 
> COde: 
> gtk_style_context_add_provider_for_screen(gtk_widget_get_style_contex
> t(GTK_WIDGET(gtk_builder_get_object(builder,"window_Main"))),
>  GTK_STYLE_PROVIDER (css_provider),
>  G_MAXUINT);

Of course you do. The first argument is meant to be a GdkScreen object.
Which makes me wonder if you even read the documentation. Anyways, you
can obtain the object by invoking

gdk_screen_get_default()

and the last argument is supposed to be a priority constant like

GTK_STYLE_PROVIDER_PRIORITY_APPLICATION

Look them up. I'd also recommend installing devhelp and gtk3-docs if
you haven't already.

--Tilo
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: How to change GTK+3 label color dynamically

2017-03-16 Thread Rúben Rodrigues
I get this error:

gtk-CRITICAL **: gtk_style_context_add_provider_for_screen: assertion 
'GDK_IS_SCREEN (screen)' failed

COde: 
gtk_style_context_add_provider_for_screen(gtk_widget_get_style_context(GTK_WIDGET(gtk_builder_get_object(builder,"window_Main"))),
 GTK_STYLE_PROVIDER (css_provider),
 G_MAXUINT);

On 16-03-2017 13:19, Tilo Villwock wrote:
> Am Donnerstag, den 16.03.2017, 12:49 + schrieb Rúben Rodrigues:
>> I provide my code in previous emails.
>>
>> This is my code now:
>>
>> if(css_provider == NULL){
>>   css_provider = gtk_css_provider_new();
>>
>>   gtk_style_context_add_provider
>> (gtk_widget_get_style_context(GTK_WIDGET(gtk_builder_get_object(build
>> er,"window_Main"))),
>>   GTK_STYLE_PROVIDER (css_provider),
>>   GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
>>   g_object_unref(css_provider);
>>
>>   }
>>   gtk_css_provider_load_from_data (css_provider,"#label_Sensors {
>> color: red; }",-1,NULL);
> My apologies. You are right. I missed the earlier one. However, quoting
> the documentation for gtk_style_context_add_provider:
>
> "Note that a style provider added by this function only affects the
> style of the widget to which context belongs. If you want to affect the
> style of all widgets, use gtk_style_context_add_provider_for_screen()."
>
> I'm guessing `window_Main` is a window? So either you attach the
> provider to all the labels you want to change or you use
> gtk_style_context_add_provider_for_screen.
>
> Hope that helps.
>
> --Tilo

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: How to change GTK+3 label color dynamically

2017-03-16 Thread Rúben Rodrigues
No! This is the ID: https://ibin.co/3FlPSBGuokBB.png

and this is the widget name https://ibin.co/3FlPnPaDN7Zl.png that i used 
in css provider. This is not the problem because in start of the program 
i change the label to other color to understand if css provider is 
working. And works, but when i tried in the middle of the code change 
again with this code don't works..

How can i add a Css class? How it works? I tried insert style classes in 
glade, but the value that i write disappear.

Thanks


On 16-03-2017 13:19, Emmanuele Bassi wrote:
> You keep using an id in the selector, but you never show how you set
> the id on the widget. I suspect you think the buildable ID you use
> with GtkBuilder is also the ID used when theming — which is definitely
> not the case. You will need gtk_widget_set_name(), instead. Of course,
> that comes with its own set of caveats, most importantly: there's no
> uniqueness of widget name enforced by GTK+ itself.
>
> If you want to avoid all of this, use a CSS class; you can add a class
> programmatically using gtk_style_context_add_class() or you can
> specify the class in the GtkBuilder XML file.
>
> Ciao,
>   Emmanuele.
>
>
> On 16 March 2017 at 12:49, Rúben Rodrigues  wrote:
>> I provide my code in previous emails.
>>
>> This is my code now:
>>
>> if(css_provider == NULL){
>>   css_provider = gtk_css_provider_new();
>>
>>   gtk_style_context_add_provider
>> (gtk_widget_get_style_context(GTK_WIDGET(gtk_builder_get_object(builder,"window_Main"))),
>>   GTK_STYLE_PROVIDER (css_provider),
>>   GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
>>   g_object_unref(css_provider);
>>
>>   }
>>   gtk_css_provider_load_from_data (css_provider,"#label_Sensors {
>> color: red; }",-1,NULL);
>>
>>
>> On 16-03-2017 12:41, Tilo Villwock wrote:
>>> Am Donnerstag, den 16.03.2017, 11:58 + schrieb Rúben Rodrigues:
 Hi,

 This is what i have, and don't works...
>>> The code I provided works. What GTK3 version are you using? Also nobody
>>> will be able to help you if you don't show us your code.
>>>
>>> --Tilo
>> ___
>> gtk-app-devel-list mailing list
>> gtk-app-devel-list@gnome.org
>> https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
>
>

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: How to change GTK+3 label color dynamically

2017-03-16 Thread Emmanuele Bassi
You keep using an id in the selector, but you never show how you set
the id on the widget. I suspect you think the buildable ID you use
with GtkBuilder is also the ID used when theming — which is definitely
not the case. You will need gtk_widget_set_name(), instead. Of course,
that comes with its own set of caveats, most importantly: there's no
uniqueness of widget name enforced by GTK+ itself.

If you want to avoid all of this, use a CSS class; you can add a class
programmatically using gtk_style_context_add_class() or you can
specify the class in the GtkBuilder XML file.

Ciao,
 Emmanuele.


On 16 March 2017 at 12:49, Rúben Rodrigues  wrote:
> I provide my code in previous emails.
>
> This is my code now:
>
> if(css_provider == NULL){
>  css_provider = gtk_css_provider_new();
>
>  gtk_style_context_add_provider
> (gtk_widget_get_style_context(GTK_WIDGET(gtk_builder_get_object(builder,"window_Main"))),
>  GTK_STYLE_PROVIDER (css_provider),
>  GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
>  g_object_unref(css_provider);
>
>  }
>  gtk_css_provider_load_from_data (css_provider,"#label_Sensors {
> color: red; }",-1,NULL);
>
>
> On 16-03-2017 12:41, Tilo Villwock wrote:
>> Am Donnerstag, den 16.03.2017, 11:58 + schrieb Rúben Rodrigues:
>>> Hi,
>>>
>>> This is what i have, and don't works...
>> The code I provided works. What GTK3 version are you using? Also nobody
>> will be able to help you if you don't show us your code.
>>
>> --Tilo
>
> ___
> gtk-app-devel-list mailing list
> gtk-app-devel-list@gnome.org
> https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



-- 
https://www.bassi.io
[@] ebassi [@gmail.com]
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: How to change GTK+3 label color dynamically

2017-03-16 Thread Rúben Rodrigues
I provide my code in previous emails.

This is my code now:

if(css_provider == NULL){
 css_provider = gtk_css_provider_new();

 gtk_style_context_add_provider 
(gtk_widget_get_style_context(GTK_WIDGET(gtk_builder_get_object(builder,"window_Main"))),
 GTK_STYLE_PROVIDER (css_provider),
 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
 g_object_unref(css_provider);

 }
 gtk_css_provider_load_from_data (css_provider,"#label_Sensors { 
color: red; }",-1,NULL);


On 16-03-2017 12:41, Tilo Villwock wrote:
> Am Donnerstag, den 16.03.2017, 11:58 + schrieb Rúben Rodrigues:
>> Hi,
>>
>> This is what i have, and don't works...
> The code I provided works. What GTK3 version are you using? Also nobody
> will be able to help you if you don't show us your code.
>
> --Tilo

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: How to change GTK+3 label color dynamically

2017-03-16 Thread Rúben Rodrigues
Hi,

This is what i have, and don't works...


On 16-03-2017 11:20, Tilo Villwock wrote:
> Am Donnerstag, den 16.03.2017, 09:55 + schrieb Rúben Rodrigues:
>> Hi,
>>
>> THanks. This is dynamically? I need in c language if possible :S
> Dynamic version:
>
> ...
>
> static GtkCssProvider* provider = NULL;
>
> static void
> set_label_color(GtkWidget* label, const char* color)
> {
>  const char* format = "label { color: %s; }";
>  size_t length = strlen(format) - 2 + 1;
>  char style[length];
>  sprintf(style, format, color);
>
>  if (provider == NULL) {
>  /* only create and add the provider the first time */
>  provider = gtk_css_provider_new();
>  gtk_style_context_add_provider(
>  gtk_widget_get_style_context(label),
>  GTK_STYLE_PROVIDER(provider),
>  GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
>  g_object_unref(provider);
>  }
>
>  gtk_css_provider_load_from_data(provider, style, -1, NULL);
> }
>
> ...
>
> This might not be ideal in all situations. Just make sure you don't
> create a new provider everytime you set a new color. Hope that helps.
>
> --Tilo

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: How to change GTK+3 label color dynamically

2017-03-16 Thread Tilo Villwock
Am Donnerstag, den 16.03.2017, 09:55 + schrieb Rúben Rodrigues:
> Hi,
> 
> THanks. This is dynamically? I need in c language if possible :S

Dynamic version:

...

static GtkCssProvider* provider = NULL;

static void
set_label_color(GtkWidget* label, const char* color)
{
const char* format = "label { color: %s; }";
size_t length = strlen(format) - 2 + 1;
char style[length];
sprintf(style, format, color);

if (provider == NULL) {
/* only create and add the provider the first time */
provider = gtk_css_provider_new();
gtk_style_context_add_provider(
gtk_widget_get_style_context(label),
GTK_STYLE_PROVIDER(provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g_object_unref(provider);
}

gtk_css_provider_load_from_data(provider, style, -1, NULL);
}

...

This might not be ideal in all situations. Just make sure you don't
create a new provider everytime you set a new color. Hope that helps.

--Tilo
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: How to change GTK+3 label color dynamically

2017-03-16 Thread Stefan Salewski
On Thu, 2017-03-16 at 09:55 +, Rúben Rodrigues wrote:
> Hi,
> 
> THanks. This is dynamically? I need in c language if possible :S
> 
> Thanks

Yes. The example

https://github.com/ngtk3/nim-gtk3/blob/master/test/colors.nim

is dynamically, I used it to generate some color schemes. Entered color
hex value in a text entry widget, and that color was applied.

As I said, it was working for 3.18. I think for 3.20 and above some CSS
names have changed, I had not the time to really investigate that.

I have no C code currently, I think my Nim code was inspired by Python
code, but I have no link to that python code currently. For C it is the
same pattern -- generate a CSS string and apply it.
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: How to change GTK+3 label color dynamically

2017-03-16 Thread Rúben Rodrigues
Hi,

THanks. This is dynamically? I need in c language if possible :S

Thanks


On 16-03-2017 07:29, Stefan Salewski wrote:
> On Wed, 2017-03-15 at 15:11 +, Rúben Rodrigues wrote:
>> Hi,
>>
>> Now i need to change GtkLabel color dinamically, so i thing that i
>> can't
>> use Css to do this.. I do this in Gtk2:
>>
>> gtk_widget_modify_fg(GTK_WIDGET(gtk_builder_get_object(builder,"label
>> ")),GTK_STATE_NORMAL,
>> );
>>
>> How can i change the color in Gtk3?
>>
>>   
> You can do it with CSS. But it has changed a bit for 3.20. You may
> compare this thread:
>
> https://mail.gnome.org/archives/gtk-list/2016-November/msg6.html
>
> Well, that was not for label widget, but for tooltip. But label widget
> should behave similar. And with similar code I once set foreground and
> background for button widgets -- I had that working for 3.18, but I
> have not yet fixed that for 3.20. (https://github.com/ngtk3/nim-gtk3/bl
> ob/master/test/colors.nim)

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: How to change GTK+3 label color dynamically

2017-03-16 Thread Stefan Salewski
On Wed, 2017-03-15 at 15:11 +, Rúben Rodrigues wrote:
> Hi,
> 
> Now i need to change GtkLabel color dinamically, so i thing that i
> can't 
> use Css to do this.. I do this in Gtk2:
> 
> gtk_widget_modify_fg(GTK_WIDGET(gtk_builder_get_object(builder,"label
> ")),GTK_STATE_NORMAL, 
> );
> 
> How can i change the color in Gtk3?
> 
> 

You can do it with CSS. But it has changed a bit for 3.20. You may
compare this thread:

https://mail.gnome.org/archives/gtk-list/2016-November/msg6.html

Well, that was not for label widget, but for tooltip. But label widget
should behave similar. And with similar code I once set foreground and
background for button widgets -- I had that working for 3.18, but I
have not yet fixed that for 3.20. (https://github.com/ngtk3/nim-gtk3/bl
ob/master/test/colors.nim)
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list