03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

How to draw<br />

Naturally, you can draw using the system GC and its member functions.<br />

Important<br />

You can assume that the GC was reset before Draw() was called.<br />

Don't reset it yourself and don't set colors and options that you don't<br />

need.<br />

Avoiding wasteful redraws<br />

Drawing outside the invalid region is technically harmless (because such drawing will be<br />

clipped away by the window server whether it's inside your control's boundaries or not), but<br />

it's potentially wasteful. You may be able to save time by confining your drawing activity to<br />

the invalid region; the trade-off is that you will have to do some testing to find out what you<br />

must draw and what you do not need to draw.<br />

That's the purpose of the TRect passed to your Draw() function; it is the bounding<br />

rectangle of the invalid region. If you wish, you can use this to draw (or redraw) only the part<br />

of the control within the passed TRect. It will be worth doing this if the cost of testing is<br />

outweighed by the savings from avoiding irrelevant drawing.<br />

In practice, very few controls gain much by confining their redraw activity entirely to the<br />

bounding rectangle – it's simpler and not much slower to redraw the whole control. As a<br />

result, the majority of controls are coded to ignore the bounding rectangle that's passed. If<br />

you're writing a control that does use the TRect, remember that you still have to obey the<br />

contract to cover the entire invalid region within the boundary of your control, and nothing<br />

outside your control. You may still have to set a clipping region to ensure this – the system<br />

doesn't set one for you.<br />

Note<br />

Early in Symbian OS development, we passed the invalid region (rather than<br />

its bounding rectangle) to Draw(). This turned out to be more trouble than it<br />

was worth. Regions are data structures of arbitrary size, which are much<br />

harder to pass around than TRects, but we had to pass them whether they<br />

were needed or not – and they usually weren't. As a compromise, we passed<br />

the bounding rectangle of the invalid region.<br />

Breaking the const and leave rules<br />

In quite rare circumstances, you may need to do some nondraw processing in Draw(). This<br />

could happen, for instance, if your view is very complicated and you're doing lazy<br />

initialization of some of the associated data structures in order to minimize memory usage.<br />

In this case, you may need to allocate memory during Draw() to hold the results of your<br />

intermediate draw-related calculations, and this allocation could cause a leave. In addition,<br />

you'll want to use a pointer to refer to your newly allocated memory, perhaps in the control.<br />

This requires you to change the pointer value, which would violate the const-ness of<br />

Draw().<br />

The solution, in this case, is to use casting to get rid of const-ness and to put your resourceallocating<br />

code into a leaving function that gets called from a TRAP() within Draw(). You<br />

also have to decide what to draw if your resource allocation fails<br />

11.4 Flicker-free Redraw

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!