# Keep CSS style while state change

**URL:** <https://www.ruby-forum.com/t/keep-css-style-while-state-change/237108>\
**Category:** Ruby-Gnome 2\
**Created:** [October 24, 2013, 6:36pm UTC](https://www.ruby-forum.com/t/keep-css-style-while-state-change/237108 "2013-10-24T18:36:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [October 24, 2013, 6:36pm UTC](https://www.ruby-forum.com/t/keep-css-style-while-state-change/237108/1 "2013-10-24T18:36:01Z")

</div>

Hi,

I have some buttons with images, where I have changed the style with  
CSS. When I now change the state of the button from sensitive to  
insensitive, the image in the button get the default theme style. That  
is obvious, cause this image is a newly created widget, that has not the  
CSS style attached. How can I get around this? One way would be, to  
create a special “insensitive”-image and change this every time the  
button state changes on my own. But this is ugly, cause it would revoke  
split from code to style. Has anyone a better idea?

Cheers, detlef

---

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [October 27, 2013, 11:29am UTC](https://www.ruby-forum.com/t/keep-css-style-while-state-change/237108/2 "2013-10-27T11:29:50Z")

</div>

Hi,

Could you show us a sample script that reproduces your  
problem? If you show it, someone may help you. 🙂

## Thanks,

kou

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“[ruby-gnome2-devel-en] keep CSS style while state change” on Thu, 24  
Oct 2013 18:22:06 +0200,

---

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [October 27, 2013, 2:46pm UTC](https://www.ruby-forum.com/t/keep-css-style-while-state-change/237108/3 "2013-10-27T14:46:05Z")

</div>

Hi,

sure, here we go:

You can adjust the value from 0 to 4. If the limits are reached, the  
button for this direction will get insensitive and “loses” it style

Cheers, detlef

#!/usr/bin/env ruby

require ‘gtk3’

class Win \< Gtk::Window  
def initialize  
super  
box = Gtk::Box.new :horizontal  
self.add box

```
     img = Gtk::Image.new :stock => Gtk::Stock::REMOVE, :size =>

```

:small\_toolbar  
@downButton = Gtk::Button.new  
@downButton \<\< img  
@downButton.signal\_connect(:clicked){clicked :down}  
@downButton.set\_name ‘numeric\_controll’

```
     @value = 1
     @label = Gtk::Label.new
     set_label

     img = Gtk::Image.new :stock => Gtk::Stock::ADD, :size =>

```

:small\_toolbar  
@upButton = Gtk::Button.new  
@upButton \<\< img  
@upButton.signal\_connect(:clicked){clicked :up}  
@upButton.set\_name ‘numeric\_controll’

```
     box.pack_start @downButton
     box.pack_start @label
     box.pack_start @upButton

     provider = Gtk::CssProvider.new
     provider.load :data => '#numeric_controll {
         color: white;
         background-color: #223;
         border-style: solid;
         border-width: 2px;
         border-color: #2528a4;
     }'

     styleContext = Gtk::StyleContext.new
     styleContext.add_provider provider, GLib::MAXUINT

     apply_css(self, provider)
     show_all
 end

 def apply_css(widget, provider)
     widget.style_context.add_provider(provider, GLib::MAXUINT)
     if widget.is_a?(Gtk::Container)
         widget.each_forall do |child|
             apply_css(child, provider)
         end
     end
 end

 def clicked dir
     if dir == :down
         if @value >= 0
             @value -=1
             @downButton.sensitive = @value > 0
             @upButton.sensitive = true
         end
     else
         if @value <= 4
             @value +=1
             @upButton.sensitive = @value < 4
             @downButton.sensitive = true
         end
     end
     set_label
 end

 def set_label
     text = @value.to_s
     @label.markup = "<span size=\"#{20 * 1024}\"

```

font\_weight=“bold”\>#{text}"  
end  
end

win = Win.new  
Gtk.main

Am 27.10.2013 11:28, schrieb Kouhei S.:

---

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [October 28, 2013, 4:08am UTC](https://www.ruby-forum.com/t/keep-css-style-while-state-change/237108/4 "2013-10-28T04:08:14Z")

</div>

Hello,

Please tell us your environment?  
I want to see the results of the following commands:

```
$ ruby -v

```

and

```
$ ruby -r gtk3 -e "p Gtk::VERSION"

```

## Regards,

Masafumi Y.  
GitHub: myokoym

2013/10/27 Detlef R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [October 28, 2013, 4:28pm UTC](https://www.ruby-forum.com/t/keep-css-style-while-state-change/237108/5 "2013-10-28T16:28:26Z")

</div>

Hi,

ruby -v

ruby 1.9.3p448 (2013-06-27 revision 41675) [x86\_64-linux]

ruby -r gtk3 -e “p Gtk::VERSION”

[3, 8, 4]

Cheers, detlef

Am 28.10.2013 04:07, schrieb Masafumi Y.:
