01.13.06
wxWindows tricks: observer pattern
Implementing a observer (publish-subscriber) pattern for drawing bussiness data on GUI is bit tricky since you are out of the event loop so the GUI will not be refreshed till the application return to this loop.
To make a windows refreshes out of the event loop wxWindows offer ::wxSafeYield() which flushes events suchs as paint ones and prevents of user events.
Below some lines of code illustrating the use of SafeYield:
Drawing and printing with wxWindows
void
VideoFrame::refresh(Observado *o)
{
wxMemoryDC dc;
wxBitmap tmp;
Features *ofeat, *efeat;
Frame f = (Frame ) o;
//
// new empty image
//
wxImage i(f->getWidth(), f->getHeight());
if (bitmap)
delete bitmap;
//
// Set data from the Frame
//
i.SetData(f->getData());
tmp = wxBitmap(i);
//
// Select the bitmap to draw on it
//
dc.SelectObject(tmp);
ofeat = f->getOrigFeatures();
efeat = f->getEndFeatures();
dc.SetPen(*wxGREEN_PEN);
dc.BeginDrawing();
ofeat->head();
efeat->head();
for (int j = 0; j < ofeat->size(); j ++) {
Point p0, pf;
p0 = ofeat->getPoint();
pf = efeat->getPoint();
dc.DrawLine(p0.x, p0.y, pf.x, pf.y);
ofeat->nextPoint();
efeat->nextPoint();
}
dc.EndDrawing();
//
// Detaching the bitmap so we can use it
//
dc.SelectObject( wxNullBitmap );
bitmap = new wxBitmap(tmp);
paintMe();
}
void
VideoFrame::paintMe()
{
wxClientDC dc(this);
PrepareDC(dc);
dc.DrawBitmap(*bitmap, 0, 0, TRUE);
//
// Send an OnPaint to the window
///
this->Refresh();
//
// Flush events: Calling the OnPaint method
//
::wxSafeYield();
}