4

I work on a project in C using GTK+. This project is for GTK+3.6 minimum.

I would like to set all GTKTextView in monospace font. Before the last update of GTK to GTK3.20, my CSS sheet was working well with :

GtkTextView {
  font: Mono
}

Now, it is not working anymore. However, glade has a property : <property name="monospace">True</property> but this property exists since GTK 3.16 and debian stable is on GTK3.14. Of course I want it portable.

My question is: how I can do it ? How I can set all my gtkTextView to the monospace font ?

murrayc
  • 2,103
  • 4
  • 17
  • 32
lock
  • 411
  • 4
  • 15
  • That CSS is invalid. Try `font: monospace;` instead for the second line. Does it work now? You can also try changing the selector to `textview`. I'll have to look up what the `monospace` property does on GtkTextView. Is there a reason setting a text tag that sets the font isn't optimal? – andlabs May 10 '16 at 11:41
  • Ok. Thank you very much. I wrote textview { font: monospace } and it works. Thank you so much, however I'm sorry I don't understand your last question. – lock May 10 '16 at 12:07
  • It is working well on Linux, but not on OS-X – lock May 10 '16 at 12:20
  • What happens on OS X? – andlabs May 10 '16 at 13:42
  • Nothing. On linux my textview is in monospace. On OS X no. No errors returned by gtk_css_provider_load_from_path – lock May 10 '16 at 13:55
  • That's very weird. Pango clearly checks for `monospace` on OS X and substitutes `"Courier"` (see `pango/pangocoretext-fontmap.c`)... What happens if you just specify `"Courier"`? Does it still not work? – andlabs May 11 '16 at 18:03
  • No. It does not work :( – lock May 12 '16 at 07:02

1 Answers1

4

GTK 3.20 changed the way that CSS selectors work.

Pre-GTK 3.20, this was correct:

GtkTextView {
    font-family: monospace;
}

Starting with GTK 3.20, this is correct:

textview {
    font-family: monospace;
}

If you're not sure about a particular platform, you can check with the GTK Inspector what selector is needed: run your app with GTK_DEBUG=interactive and press Ctrl+Shift+D.

It's worth noting that the pre-GTK 3.20 CSS selectors were experimental and not intended to be stable API.

If you have to support both forms, then your best bet is probably to make two stylesheets and load one or the other depending on the value of GTK_MAJOR_VERSION and GTK_MINOR_VERSION.

ptomato
  • 56,175
  • 13
  • 112
  • 165
  • Thanks for good explanation. It does not work on OS-X but I understand why it was not working anymore on Linux. So now, on Linux it's ok. – lock May 17 '16 at 06:59
  • Can you check with the inspector on OSX to see what's not working about the selector? – ptomato May 17 '16 at 07:47
  • No that's ok now. I've re-installed Gtk and it's now working well !! Thank you for your precious help. Now I can use CSS sheet. My last question is to use CSS in C code: http://stackoverflow.com/questions/37250497/change-text-color-for-gtktoggletoolbutton-in-c-code-gtk3/37253231#37253231 – lock May 17 '16 at 08:03
  • @ptomato would `GtkTextView, textview` not work with the GTK+ CSS engine? – andlabs May 22 '16 at 04:20
  • 1
    Oh, that would work, but would be less explicit, and a pain to untangle when you need to stop supporting < 3.20 for other reasons... – ptomato May 22 '16 at 23:46