Reputation: 6616
I am trying to make a Digital IC simulation using wxWidgets. I have decent knowledge of C++, but wxwidgets is relatively new.
As a first step, I need to display a breadboard. My solution is
a class myFrame (extends wxframe), creates instance of class breadboard (extends wxpanel).
breadboard has member
wxMemoryDC bbBuffer
In constructor of breadboard i draw using following code snippet
wxBitmap bmp(660, 450, -1);
bbBuffer.SelectObject(bmp);
bbBuffer.SetBackground(*wxWHITE_BRUSH);
bbBuffer.Clear();
bbBuffer.SetPen(wxPen(wxColor(80, 80, 80), 1));
bbBuffer.DrawRectangle(10, 10, 640, 430);//more functions like this one follow
in render function i do this -
void BreadBoard::render(wxDC &dc)
{
dc.Blit(wxPoint(10, 20), bbBuffer.GetSize(), &bbBuffer, wxPoint(0, 0));
}
I also draw wires using mouseEvent on wxClientDC and bbBuffer (this is too simple, later i will write it to XML file, but not now)
Is this a good design strategy? Later on it will get complex, so how should i proceed?
Upvotes: 0
Views: 1235
Reputation: 20616
Personally, I would not make class breadboard an extension of wxPanel. It seems cleaner to have breadboard simply contain your code and not be a mixture.
You can create a panel as a child of myFrame.
class MyFrame {
wxPanel * myPanel;
...
MyFrame::MyFrame(
...
myPanel = new wxPanel(this,-1,wxPoint(0,0),wxSize(2400,1200)) ;
...
Then when you are ready to display the breadboard's bitmap, a method of MyFrame can do a GetDC on the panel, and blit the image from the breadboard's DC to the panel's DC, or pass the panel DC to a method of the breadboard class so that the breadboard can do the blit without exposing its memory DC.
void MyFrame::Render()
{
myBreadboard.Render( wxClientDC( myPanel ) );
}
What are the advantages?
Conceptually clearer, in my mind anyway.
More obvious how to draw your breadboard to another DC, e.g. a printer. You can simply pass the printer DC to the breadboard blit method.
I see that your method render() does the blit. Some comments on this method:
You are passing in a wxDC parameter. What will this be? I am guessing that this will usually be the wxDC obtained by calling GetDC on this - which is kind of confusing and unnecessary. You could do the call to GetDC inside the render() method, which makes your code cleaner, but now you cannot use render to draw anywhere except to the panel implemented by breadboard which will eventually be a snag.
You have hard coded the location where the breadboard is drawn. Bad idea! Better to pass the location as a parameter, with a sensible default.
Upvotes: 2