So, I want to draw styled text into a Listbox cell.
I have working code that renders the StyledText via a TextArea control into a Picture , using either TextArea.DrawInto or a StyledTextPrinter from the same TextArea. Then I can use
g.DrawPicture inside CellTextPaint and get to see the styled text.
However, using an intermediate Picture for this is wasteful and also complicates things with HiDPI displays.
So I tried rendering the TextArea directly into the CellTextPaint’s Graphics object. But that consistently leads to a hard crash, usually with segmentation faults, and somewhere inside DrawInto, and with undecypherable stack traces like this:
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 ??? 0xbfffc9a8 0 + 3221211560
1 ??? 0x43998000 0 + 1134133248
A sample project is easy to make:
Add a Listbox and a TextArea to a Window. In the Listbox’ CellTextPaint event put:
TextArea1.DrawInto g, 0, 0
That’s it. Run it, and you’ll get some crash or other weird behavior.
Is this a known issue?
Oh, and there’s one quite “severe” problem with this that I can’t solve at all so far:
I want to keep the drawn background of the cell (e.g. for when it’s selected and has been filled with the highlight color). If I could draw directly from the TextArea into the graphics port, this would be working. But since I have to use a Picture, and that picture does not maintain a proper mask or alpha, drawing that picture into the Cell’s graphic will replace the the non-white background with the picture’s white background.
To see this, change the CellTextPaint code into:
dim p as Picture = self.BitmapForCaching (me.Width, me.Height)
TextArea1.DrawInto p.Graphics, 0, 0
g.DrawPicture p, 10, 0
Run it, put something into the TextArea, then click into the Listbox to select it. You’ll see that the left 10 pixel are drawn with the selection background, the rest is white due to the g.DrawPicture call overpainting it entirely. How do I get the selection color across the entire cell?