0

I am confused about "graphic context"s, using the xcb library. There are some examples around the www, they all show creating one single window with one single graphic context. Of course when setting up several windows, each will have its own graphicx context, or perhaps several.

Could or should I create several graphic contexts for one and the same window? For example one for drawing flowers, another for rivers, a third for text labels? Or is it better to use only one and adapt it to the job at hand?

What is considered good programming style, in this respect?

DYZ
  • 55,249
  • 10
  • 64
  • 93
Karel Adams
  • 185
  • 2
  • 19

1 Answers1

1

You can create as many contexts as you want. The rule of thumb is:

  • If you draw everything in the same style use one context.
  • If you change the style, but not very often, use one context.
  • If you change styles frequently, follow your senses and either use one content (and change its attributes as needed) or create several contents. The latter approach is faster, but if you have too many GCs, you may run out of them (there is a limit on how many GCs a window may have).
DYZ
  • 55,249
  • 10
  • 64
  • 93
  • Is there a way to find out how many I can allocate? I guess that depends on many factors so it ought to be checked in runtime? (and btw, when you mention "contents", would you mean "contexts"? A bit confusing...) – Karel Adams Jan 16 '17 at 21:20
  • 1
    If you need more than a few dozen contextes, you might re-think your strategy... There's no point in allocation a context for each and every GUI element. Also, if you don't need it anymore you can destroy the context. – JvO Jan 16 '17 at 23:34
  • @KarelAdams The manual page on `XCreateGC` (which is a great page, by the way) simply specifies that "XCreateGC can generate `BadAlloc`... The server failed to allocate the requested resource or server memory." – DYZ Jan 16 '17 at 23:41