From The Couch 1 – Canvas Control

Here is the first in a new series of articles called “From the Couch”. As far as I’m concerned, the best part of a laptop is not that you can travel out of town with it, but that you can travel to the couch with it. I wouldn’t recommend doing any really serious programming while you watch TV, but with the quality of shows and the quantity of commercials these days, I seem to be able to follow a show and poke around on the laptop at the same time fairly easily. Of course, I’d probably get more done if I turned off the TV, but then I wouldn’t be “relaxing” 🙂 Not to mention, the heat output from my laptop keeps me warm on those chilly winter evenings. The only annoying thing is that my battery no longer lasts an evening on the couch so I have to trail a power cord.

Anyway, back to the purpose of this first installment of “From the Couch”. I’ve had several requests for a “Canvas” control to support drawing graphics in Suneido. I haven’t got around to it because I haven’t run into any pressing need for it. But it seemed like a suitably fun task for a Friday night (if you’re a programming junkie like me).

I’ll present a slightly simplified version here and incorporate the complete code in stdlib in the next release. I’ll assume a basic understanding of Windows GUI programming. Here is CanvasControl itself:

Since we want to respond to (implement) Windows messages, we derive the class from WndProc (which in turn derives from Hwnd which wraps a Windows window handle). (Note: WndProc { … } is equivalent to class : WndProc { … } ) I’ve set Xstretch and Ystretch to 1 so the CanvasControl will fill its window and be resizable.

In the New method (the constructor) we create a window using the SuWhiteArrow window class (registered in Init). This class, as the name implies, has a white background and an arrow mouse cursor. We also call SubClass to subclass the window so we can respond to messages like PAINT (WM_PAINT). Finally, we initialize an instance member (.items) to be an Object. This will hold the list of items (lines, shapes) that we draw on the canvas.

My first inclination was to have methods in Canvas for “drawing” different things – for example, Line, Rectangle, Ellipse, and Text methods. But the problem with this is that any time someone wants to add a new type of canvas item they have to modify CanvasControl. And pretty soon, CanvasControl has a zillion methods in it. Instead, I decided it would be more flexible to simply have an AddItem method that would add any kind of “drawable” item to the canvas. The only requirement on these items is that they have a Paint method. This approach makes it easy to create new types of canvas items without bloating CanvasControl.

The AddItem method allows us to add items to the canvas. Note: You can’t call this method “Add” because that’s a built-in method that can’t be redefined. Note: In keeping with Windows GUI programming AddItem does not actually draw the item. Instead it calls Repaint (defined in Hwnd) to “invalidate” the window, causing a paint message to be generated. (A more efficient implementation would only invalidate the area of the added item.) If you add multiple items, the invalidates will be combined so only one paint message is issued.

Finally, the heart of CanvasControl is the PAINT method which is called in response to the WM_PAINT message from Windows. As usual, the paint method is bracketed by BeginPaint and EndPaint. To draw the items, we simply loop through them and call a Paint method on each, passing it the handle to the device context. Obviously, each item has to have a suitable Paint method or this scheme will fail miserably! Lastly, we return 0 to indicate we handled the message.

You can now run this from the WorkSpace with:

CanvasControl()
That’s it for CanvasControl. But it’s pretty boring without anything to draw on it. Here are a few basic canvas items that are simply wrappers around some simple GDI functions. I’ve prefixed all the names with “Canvas” to avoid any name clashes.

Notice the “.Ctrl” after CanvasControl(). This is needed because calling a control creates a top level window to contain the control and it is this top level window that is returned. To get a reference to our control we need to get the window’s control.

You should see something like this. (But with a different border style if you’re not on XP.)

One thing you may be wondering about is how you would add support for different pens and brushes. You could add pen and brush arguments to all the canvas items but you’d end up duplicating a lot of code. Instead, we can use the Decorator design pattern and make an item that “wraps” another item and sets the pen or brush for it. Since Windows treats pens and brushes very similarly, we can even use the same decorator for both. Here is a decorator that uses stock objects. These are a little simpler to handle because we don’t have to worry about destroying the objects.

So now if we want a black ellipse we can simply wrap a CanvasStockObject around a CanvasEllipse:

c = CanvasControl().Ctrl
c.AddItem(CanvasStockObject(SO.BLACK_BRUSH,
CanvasEllipse(50, 50, 250, 150))
You should see something like:
We can also apply the Composite design pattern to compose canvas items from other canvas items: