Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CssStyleProvider in Gtkmm3

Tags:

c++

css

gtk

gtk3

gtkmm

I'm programming in c++ with gtkmm3. I want to change some fonts in my program. I read all the things about CssStyleProvider, StyleContext in gtkmm documentation and also in gtk+ documentaion but i couldn't make it work and couldn't find any tutorial about that. I'm trying like that

Glib::ustring data;
data="GtkMenuBar, GtkMenuItem {font-name: Sans 6}"; /*tried with semi-colon, too*/
Glib::RefPtr<Gtk::CssProvider> asd = Gtk::CssProvider::create();
Glib::RefPtr<Gtk::StyleContext> asd2 = Gtk::StyleContext::create();
asd->load_from_data(data);
asd2->add_provider(asd, 0); /*also tried different priorities*/

also tried like that

Glib::ustring data;
data="GtkMenuBar, GtkMenuItem {font-name: Sans 6}"; /*tried with semi-colon, too*/
Glib::RefPtr<Gtk::CssProvider> asd = Gtk::CssProvider::create();
Glib::RefPtr<Gdk::Screen> screen;
Glib::RefPtr<Gtk::StyleContext> asd2 = Gtk::StyleContext::create();
asd->load_from_data(data);
(mainWindow.get())->get_property("screen", screen);
asd2->add_provider(screen, asd, 0); /*also tried different priorities*/

And I tried these before running my main window and after running it but i have no luck. Any help is appreciated.

like image 226
Gökhan Uras Avatar asked Jul 12 '11 15:07

Gökhan Uras


2 Answers

Have you also to tried to use the macro GTK_STYLE_PROVIDER_PRIORITY_APPLICATION instead of 0?

asd2->add_provider(screen, asd, macro GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

Works for me, also on Windows.

Example:

int main(int argc, char **argv) {

  Main app(argc, argv);
  Glib::ustring data = "GtkLabel {color: #ff00ea;font: Comic Sans MS 12}";
  auto css = CssProvider::create();
  if(not css->load_from_data(data)) {
      cerr << "Failed to load css\n";
      std::exit(1);
  }
  Label label("test");
  Window win;
  auto screen = Gdk::Screen::get_default();
  auto ctx = label.get_style_context();
  ctx->add_provider_for_screen(screen, css, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  win.add(label);
  win.show_all();

  app.run(win);
  return 0;
}
like image 104
user1412363 Avatar answered Oct 01 '22 18:10

user1412363


I don't know about gtkmm and css, but css on the web requires a ; after font-name:Sans 6

Other than that, everything you have looks right based on the GTK::CssProvider reference

like image 36
Christian Smith Avatar answered Oct 01 '22 20:10

Christian Smith