document/view STL and modern C++ Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
sergio
Knows some wx things
Knows some wx things
Posts: 39
Joined: Fri Sep 01, 2006 8:19 pm
Location: Costa Rica

document/view STL and modern C++

Post by sergio »

Hi,

does someone knows if there is a problem/bug when combining the doc/view architecture and the STL? I did the classical "doodles" example using openGL instead of the traditional device context approach:

Code: Select all

/******************************************************************************/
void Canvas::OnPaint(wxPaintEvent& event)
{
     wxPaintDC dc(this);   
     SetCurrent();
     glClear(GL_COLOR_BUFFER_BIT);
     glPushMatrix();
     if(m_View)
          m_View->OnDraw(&dc);
     glPopMatrix();
     SwapBuffers();
}
/******************************************************************************/

Code: Select all

/******************************************************************************/
void GraphicsView::OnDraw(wxDC *dc)
{
     GraphicsDoc *doc=(GraphicsDoc *)GetDocument();
     std::list<Shape *>::const_iterator pos; 
     for(pos=doc->m_Shapes.begin();pos!=doc->m_Shapes.end();++pos) //code crashes somewhere around here
          (*pos)->Draw(); 
}
/******************************************************************************/

I had so strange problems (if I changed the container to a vector then everything was fine for example) that I decided to go back to the primitive "frame" architecture I'm used to:

Code: Select all

/******************************************************************************/
void Canvas::OnPaint(wxPaintEvent& event)
{
     wxPaintDC dc(this);   
     SetCurrent();
     glClear(GL_COLOR_BUFFER_BIT);
     glPushMatrix();
     std::list<Shape *>::const_iterator pos;
     for(pos=m_Shapes.begin();pos!=m_Shapes.end();++pos)
          (*pos)->Draw();   
     glPopMatrix();
     SwapBuffers();
}
/******************************************************************************/
the only real difference here is that I call paint for the whole list FROM the Canvas (since m_Shapes is now a member of Canvas and not of GraphicsDoc as before) but now it works perfect. What am I doing wrong?
Thanks,

Sergio

P.D: why doesn't dynamic_cast work properly and instead the unsafer C downcasting system works fine? I mean:

Code: Select all

/******************************************************************************/
Canvas::Canvas(wxWindow *parent, wxWindowID id,const wxPoint& pos, const wxSize& size)
    : wxGLCanvas(parent, (wxGLCanvas*) NULL, id, pos, size)
    
{    
     m_Frame = (GraphicsFrame *)parent; //dynamic_cast<GraphicsFrame> *(parent) makes this crash!!!
     SetCurrent();
     glClearColor(0.5,0.5,0.5,0.0);
     glOrtho(-10.0,10.0,-10.0,10.0,-10.0,10.0);
}    
/******************************************************************************/
daddydave
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 214
Joined: Wed Jun 15, 2005 3:31 am
Location: United States
Contact:

Re: document/view STL and modern C++

Post by daddydave »

sergio wrote:

Code: Select all

     m_Frame = (GraphicsFrame *)parent; //dynamic_cast<GraphicsFrame> *(parent) makes this crash!!!
Just a guess, but shouldn't that be

Code: Select all

dynamic_cast<GraphicsFrame*>(parent) 
instead of

Code: Select all

dynamic_cast<GraphicsFrame> *(parent) 
?
sergio
Knows some wx things
Knows some wx things
Posts: 39
Joined: Fri Sep 01, 2006 8:19 pm
Location: Costa Rica

Post by sergio »

Yup, you're right. Just a mistake when typing the message though but the tested code was written correctly. Thanks anyway. Anyone please?
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 »

maybe the parent isn't a frame?
Have you tried static_cast<YourFrame*> ?
sergio
Knows some wx things
Knows some wx things
Posts: 39
Joined: Fri Sep 01, 2006 8:19 pm
Location: Costa Rica

Post by sergio »

Anyone? I find it sooo strange that I have to use C casting instead of modern C++ techniques....
sergio
Knows some wx things
Knows some wx things
Posts: 39
Joined: Fri Sep 01, 2006 8:19 pm
Location: Costa Rica

Post by sergio »

Jorg told me why, if anyone is interested:

http://wiki.wxwidgets.org/docbrowse.cgi ... rview.html

Regards,

Sergio
Post Reply