diff --git a/include/wx/display.h b/include/wx/display.h index 1c8d04379d..3213d79d97 100644 --- a/include/wx/display.h +++ b/include/wx/display.h @@ -50,7 +50,6 @@ public: // dtor is not virtual as this is a concrete class not meant to be derived // from - ~wxDisplay(); // return the number of available displays, valid parameters to diff --git a/include/wx/private/display.h b/include/wx/private/display.h index cbaa3a2690..9e0f399c28 100644 --- a/include/wx/private/display.h +++ b/include/wx/private/display.h @@ -11,6 +11,7 @@ #define _WX_PRIVATE_DISPLAY_H_ #include "wx/gdicmn.h" // for wxRect +#include "wx/vector.h" // ---------------------------------------------------------------------------- // wxDisplayFactory: allows to create wxDisplay objects @@ -20,12 +21,20 @@ class WXDLLIMPEXP_CORE wxDisplayFactory { public: wxDisplayFactory() { } - virtual ~wxDisplayFactory() { } + virtual ~wxDisplayFactory(); - // create a new display object - // - // it can return a NULL pointer if the display creation failed - virtual wxDisplayImpl *CreateDisplay(unsigned n) = 0; + // Create the display if necessary using CreateDisplay(), otherwise just + // get it from cache. + wxDisplayImpl* GetDisplay(unsigned n) + { + if ( m_impls.empty() ) + m_impls.resize(GetCount()); + else if ( m_impls[n] ) + return m_impls[n]; + + m_impls[n] = CreateDisplay(n); + return m_impls[n]; + } // get the total number of displays virtual unsigned GetCount() = 0; @@ -37,6 +46,18 @@ public: // // the window pointer must not be NULL (i.e. caller should check it) virtual int GetFromWindow(const wxWindow *window); + +protected: + // create a new display object + // + // it can return a NULL pointer if the display creation failed + virtual wxDisplayImpl *CreateDisplay(unsigned n) = 0; + +private: + // On-demand populated vector of wxDisplayImpl objects. + wxVector m_impls; + + wxDECLARE_NO_COPY_CLASS(wxDisplayFactory); }; // ---------------------------------------------------------------------------- diff --git a/src/common/dpycmn.cpp b/src/common/dpycmn.cpp index 8e14dde43f..9e59f0aeba 100644 --- a/src/common/dpycmn.cpp +++ b/src/common/dpycmn.cpp @@ -118,12 +118,7 @@ wxDisplay::wxDisplay(unsigned n) wxASSERT_MSG( n == 0 || n < GetCount(), wxT("An invalid index was passed to wxDisplay") ); - m_impl = Factory().CreateDisplay(n); -} - -wxDisplay::~wxDisplay() -{ - delete m_impl; + m_impl = Factory().GetDisplay(n); } // ---------------------------------------------------------------------------- @@ -230,6 +225,15 @@ bool wxDisplay::ChangeMode(const wxVideoMode& mode) // wxDisplayFactory implementation // ============================================================================ +wxDisplayFactory::~wxDisplayFactory() +{ + for ( size_t n = 0; n < m_impls.size(); ++n ) + { + // It can be null, that's ok. + delete m_impls[n]; + } +} + int wxDisplayFactory::GetFromWindow(const wxWindow *window) { // consider that the window belongs to the display containing its centre