Search code examples
wxwidgetsvirtual-functions

How should a 'Virtual ListCtrl' access the 'Model Data' to populate it's list?


I have a class called MVC which provides separation and acts as a broker between wxFrame/UI and the Model Data.

class diagram

To keep things simple, MVC was made a member variable of wxFrame.

class MAIN_FRAME: public wxFrame
{       

public:    
    MAIN_FRAME();

public:
    MVC MODEL;   
};

wxFrame implements a 'virtual wxListCtrl' to request the list data from MVC.

However, I'm not clear on the best way for the 'virtual ListCtrl' to access the MVC, specifically in the overloaded function which requests the list data. The problem being that MyVirtualListCtrl is separate from wxFrame so doesn't have scope to access wxFrame member variable MVC:

wxString MyVirtualListCtrl::OnGetItemText( long item_, long col_  ) const
{

   // If no data then populate list cells with "Empty". 
   if( this->MVC.empty() )
   {
       return _( "Empty" ) ;
   }

   // Use item and column to return the correct data for that particular cell.

   // mock solution
   >>wxString s = MVC.get_data( item_, col_ );<<
   >>return s;<<

}

Solution

  • When constructing your MyVirtualListCtrl class, you can give it a reference to the MAIN_FRAME or, arguably better, directly to its MODEL and then just use it. As you can safely assume that MAIN_FRAME (and hence its model) will outlive MyVirtualListCtrl, because the frame children are destroyed when the frame itself is, this is safe from the object life-time point of view.