Re: Unable to set background color for button



Hello,

I would add however that the colour change may not work well with different themes when you provide a custom  css. (The pain with all those different themes ...  in case that matters )
Without knowing the context, will the suggested action approach not suffice since that is already provided by gtk/gtkmm  ( In Adwaita it changes a button to blue, i,e it uses the highlight colour, in Ubuntu (Yaru) it will be orange) etc. 

See here  https://youtu.be/ZmlJMBwjJ0M?t=857 for details
It's in Vala, but  it is self explanatory how to do it in  C++/gtkmm, 

Who knows, it may provide a way to further your path.

I made a completely red night theme once with a custom css. In that case i didn't have worry about the theme colours already provided,.since I overrode pretty much everything. I used an approach very similar to what Mohit already provided, it works fine, BUT, t found providing custom css for small snippets often look bad in different themes. 
Of course, you can just discard all I say and say you only support Adwaita, or whatever theme you use,  but the reality is many distros  don't use adwaita.  If you are okay with that, then, no problem.  

Just thinking out loud as I am typing, perhaps one way to easily blend in with the current theme if there is someway to query the highlight colour ( never tried this), then use that as a guide. to colour you button, write the css in code, save it, load it as already discussed., then you can even connect it to the theme changed signal, so the button colour automatically updates to suit active theme colours when the theme changed signal is emitted.

btw. you can use Oonix theme designer too to style anythings as well an then export the CSS file to use with your project, that is,  in case you don't enjoy too much custom CSS writing I don't ans d you want it to look fancy )..

Cheers,
Bets if luck in your endeavours.

On Wed, 11 Mar 2020 at 05:44, Mohith Manoj via gtkmm-list <gtkmm-list gnome org> wrote:
Like Daniel mentioned, the background-image property overrides the background-color. It's probably because of this, the API to override the background color is deprecated.

Writing a simple custom CSS would solve your problem eternally. Also once you get to know it, it's not that difficult.

Now technically you can create a CSSProvider and load a custom CSS directly from a file (which is better since you can modify it without having to rebuild your executable) then add the provider to the style context of a particular widget. I would prefer to have it apply to the whole app because then all your widgets will have uniform appearance.

May be you have already found a permanent solution. This is what I would do, in case someone is still interested.

mian.c / root window

void CMainWindow::LoadTheme(const Glib::ustring &sPath)
{
  /*
   * Unit Name  :
   * Unit ID    : U-MainWindow.cpp-
   * Author     : mohith (25-Feb-2020, 3:20:06 pm)
   * Comments (if any)
   */
  {
    Glib::RefPtr <Gdk::Screen> refScreen = Gdk::Display::get_default()
      ->get_default_screen();
    Glib::RefPtr <Gtk::CssProvider> refProvider = Gtk::CssProvider::create();

    //Remove existing provider
    if (m_refCurrentCustomStyleProvider)
    {
      Gtk::StyleContext::remove_provider_for_screen(refScreen,
                                                    m_refCurrentCustomStyleProvider);
    }

    try
    {
      refProvider->load_from_path(sPath);

      //Add new provider for screen
      Gtk::StyleContext::add_provider_for_screen(refScreen,
                                                 refProvider,
                                                 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

      m_refCurrentCustomStyleProvider = refProvider;
    } catch(Gtk::CssProviderError &e)
    {
      IDU_CMN_ERROR_OUT << "Error loading custom styles. " << e.what() << std::endl;
    } catch(Glib::Error &e)
    {
      IDU_CMN_ERROR_OUT << "Error loading custom styles. " << e.what() << std::endl;
    }
  }
}

custom_styles.css

button
{
  background-image: unset;
  background-color: @activatable-control-color-sensitive;
}

button:overlay
{
  background-color: @activatable-control-color-highlight;
}

button:backdrop
{
  background-color: @activatable-control-color-insensitive;
}

@define-color activatable-control-color-sensitive white;
@define-color activatable-control-color-highlight #FF9900;
@define-color activatable-control-color-insensitive gray;

Call the CMainWindow::LoadTheme() with the path to your CSS file to load it up.

Regards,
Mohith



Please consider the Environment before printing this e-mail.

The information contained in this message (including any attachments) is confidential and may be privileged or otherwise protected from disclosure.  If you are not the intended recipient, you must not copy this message or attachment or disclose the contents to any other person.  If you have received this transmission in error, please notify the sender immediately by return e-mail and permanently delete this message and any attachments from your system.  Any dissemination, use, review, distribution, printing or copying of this message in whole or in part is strictly prohibited.  Please note that e-mails are susceptible to change.
 
SKANRAY(including its group of companies) shall not be liable for any omission or error in the message, improper or incomplete transmission of the information contained in this communication or for any delay in its receipt or damage to your system.  SKANRAY(or its group of companies) does not guarantee that the integrity of this communication has been maintained or that this communication is free of viruses, interceptions or interference.
_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
https://mail.gnome.org/mailman/listinfo/gtkmm-list


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]