The onPaint handler
This widget is very simple so only the OnPaint handler is required to draw the widget.
The OnPaint handler will be a public handler which takes no parameters and does not return a value.
Add the OnPaint definition to the widget code.
There are a number of steps to take in the OnPaint handler. The first step is to draw the unrotated text in the widget.
- Work out the size of the widget by getting its width and height.
- Work out the rectangle of the area we have to draw into.
- Draw the text at the center of the available space.
Drawing the Text
For the basic widget we will use “LiveCode” as the text.
- Declare variables for the text to be displayed and height and width of the canvas.
- Update the variables with the relevant values.
- Declare a rectangle variable.
- Put the rectangle describing the area of the canvas into the rectangle variable.
- Draw the text in the center of the canvas.
variable tWidth as Real put my width into tWidth put my height into tHeight put "LiveCode" into tText variable tRectangle as Rectangle put rectangle [0,0,tWidth,tHeight] into tRectangle fill text tText at center of tRectangle on this canvas end handler
- my width: Returns the width of the widget.
- my height: Returns the height of the widget.
- rectangle: Specifies an area, consists of a list of 4 integers left, top, right, bottom e.g. put rectangle [0,0,tWidth,tHeight] into tRectangle
- fill text at: Renders text on a canvas e.g. fill text “Widget Label” at top left of rectangle [50, 100, 250, 200] on this canvas
For more on each of these keywords see the API entries in the LiveCode Dictionary.
Rotating text
To draw rotated text we
- Rotate the canvas.
- Get the rectangle of the rotated canvas as a path.
- Rotate the path back.
- Get the rectangle of the un-rotated path.
- Draw the text within the un-rotated rectangle, on the rotated canvas.
This draws the text horizontally, but relative to the position of the canvas. Because the canvas is rotated the text also appears rotated.
Drawing rotated text
- Rotate the canvas by the value stored in the mRotation variable.
- Declare a rectangle variable tRectangle and a path variable tPath.
- Put the rectangle path of the canvas into tPath.
- Rotate the path by -mRotation, setting it to a horizontal rectangle.
- Get the bounding box of the un-rotated path and put it into the tRectatangle. variable, this gives the rectangle the completely encloses un-rotated the path in tPath
- Draw the text at the center of the rectangle given by tRectangle.
The text in drawn in a horizontal rectangle, but relative to the canvas, so appears drawn on an angle.
end handler