title: Changing widget preoperties programmatically in gtkmm3 / gtk3
body: <p>I've started porting some of my gtkmm2 applications to gtkmm3 and I am not clear on the correct mechanism for changing properties such as color on a widget from within the application in response to a change of state. Here is a short example that illustrates what I am trying to achieve which is to invert the colors of the button image and the text depending on some defined state:</p>
  
  <p>I create a window (Gwin) and hard-code a short CSS string that is enough to define the beginning state of the widgets at startup:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
     try
     {
          Gtk::Main *kit(new Gtk::Main(argc, argv));
          GWin *dl(new GWin(sdi, argc, argv));
  
          auto screen = dl-&gt;get_screen();
          Glib::RefPtr&lt;Gtk::CssProvider&gt; css(Gtk::CssProvider::create());
          Glib::RefPtr&lt;Gtk::StyleContext&gt; style(dl-&gt;get_style_context());
  
          Glib::ustring theme(
                        "@define-color bg rgb(57.64%, 48.62%, 36.47%);"
                        "@define-color BtnBg rgb(23.13%, 19.60%, 16.07%);"
                        ".background { background-color: @bg; }"
                        "button"
                        "{"
                        "   background-image:image(@BtnBg);"
                        "   border-color: @bg;"
                        "   color: rgb(80.%, 80.%, 80.%);"
                        "   padding-left: 4px;"
                        "   padding-right: 4px;"
                        "}"
                        "button:hover"
                        "{"
                        "   background-image:image(rgb(77%, 81%, 84%));"
                        "   color: rgb(0.%, 0.%, 0.%);"
                        "}"
                        "#LSK0 { background-image: image(@bg); border-color:@bg }"
                        "#RSK0 { background-image: image(@bg); border-color:@bg }"
                        );
        css-&gt;load_from_data(theme);
        style-&gt;add_provider_for_screen(screen, css, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  
  
  
       dl-&gt;Run();
       delete kit;
     }
     catch(const std::string &amp;e)
     {
        std::cout &lt;&lt; "ERROR: GWin now exits:\n" &lt;&lt; e &lt;&lt; std::endl;
     }
     return(0);
  }
  </code></pre>
  
  <p>Now inside the GWin class, the relevant piece of code is the following signal handler that operates on the mRunButton Gtk::Button which has been initialized with mRunButton.set_name("RunButton"):</p>
  
  <pre><code>void GCDU::onBtnLight(bool on)
  {
     // The GTK3 code
     Glib::RefPtr&lt;Gtk::CssProvider&gt; css(Gtk::CssProvider::create());
     Glib::RefPtr&lt;Gtk::StyleContext&gt; style(mRunButton.get_style_context());
  
     Glib::ustring theme(on ?
                         "#RunButton { background-image: image(rgb(80%, 80%, 80%); color: rgb(0.%, 0.%, 0.%); }"
                         :
                         "#RunButton { background-image: image(rgb(23.13%, 19.60%, 16.07%)); color: rgb(80.%, 80.%, 80.%); }"
                        );
     css-&gt;load_from_data(theme);
     style-&gt;add_provider(css, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  
     // The old GTK2 code
     //Gdk::Color c(mButtonBG);
     //Gdk::Color f;
     //if(on)
     //{
     //   c.set("white");
     //   f.set("black");
     //}
     //else
     //{
     //   f.set("white");
     //}
     //mRunButton.modify_bg(Gtk::STATE_NORMAL, c);
     //mRunButton.get_child()-&gt;modify_fg(Gtk::STATE_NORMAL, f);
  }
  </code></pre>
  
  <p>I have searched around but most questions and their answers appear to be limited to setting the style properties once for a theme at run start or using the now deprecated override_ methods.</p>
  
  <p>Thanks for looking at the question.</p>