How to implement a painter?
The actual drawing of PPlot depends on a concrete implementation of the
following abstract Painter class
class Painter {
public:
// basic
virtual void DrawLine (int inX1, int inY1, int inX2, int inY2)=0;
virtual void FillRect (int inX, int inY, int inW, int inH)=0;
virtual void SetClipRect (int inX, int inY, int inW, int inH)=0;
// widget geometry
virtual long GetWidth () const=0;
virtual long GetHeight () const=0;
// color
virtual void SetLineColor (int inR, int inG, int inB)=0;
virtual void SetFillColor (int inR, int inG, int inB)=0;
//text
virtual long CalculateTextDrawSize (const char *inString)=0;
virtual long GetFontHeight () const =0;
virtual void DrawText (int inX, int inY, const char *inString)=0;
virtual void DrawRotatedText (int inX, int inY, float inDegrees,
const char *inString)=0;
};
Here are some guidelines on how to do that.
Test code
You probably already know how to make the example plots. You can make a special
plot (intended to test the painter class) similarly
NativePPlot *theNativePPot = new NativePPlot ();
MakePainterTester (theNativePPlot->mPPlot);
You should get a result like this.
coordinate system
The PPlot plot engine uses in all calls to the Painter class "screen
coordinates". The screen coordinate system is relative to the widget. The
top-left position of the widget corresponds to (x=0,y=0) and the bottom-right
to (x=widget-width, y=widget-height).
basic functions
Once you understand the coordinate system, the first three functions, DrawLine,
FillRect and SetClipRect are self explanatory.
Geometry functions
The next two functions, GetWidth and GetHeight, should
return, as you might have guessed, the Width and the Height of the widget.
Color
Colors are specified by RGB tuples, so that (0,0,0) = black, and
(255,255,255) = white. The function SetLineColor should set the
color for lines and text, and SetFillColor should set the color
used in FillRect.
Text functions
In order to align texts, PPlot needs to know how large a string will be
when it is drawn in the particular font that you are using. That is the purpose
of the function GetTextDrawSize: it should return the draw width
of the string. The function GetFontHeight should return the Height
of the particular font that you use. The function DrawText should
draw a text relative to the position (x,y), pointing to the bottom-left of
the text. Then there is the inevitable DrawRotatedText. The idea
is that you pin down the horizontal text at position (x,y), pointing again
to the bottom-left of the text. Next you rotate the required nr. of
degrees about this point. (Because the y coordinate runs up on going down
on the screen the sign of the angle is reversed.) You can check the implementations
with the "PainterTester" plot that
features a horizontal and vertical text bordered by an "ascent" line and
a "descent" line.