]>
git.saurik.com Git - wxWidgets.git/blob - src/common/rendcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/rendcmn.cpp
3 // Purpose: wxRendererNative common functions
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
32 #include "wx/apptrait.h"
33 #include "wx/renderer.h"
35 #include "wx/ptr_scpd.h"
37 #if wxUSE_DYNLIB_CLASS
38 #include "wx/dynlib.h"
39 #endif // wxUSE_DYNLIB_CLASS
41 // ----------------------------------------------------------------------------
42 // wxRendererPtr: auto pointer holding the global renderer
43 // ----------------------------------------------------------------------------
45 wxDECLARE_SCOPED_PTR(wxRendererNative
, wxRendererPtrBase
)
46 wxDEFINE_SCOPED_PTR(wxRendererNative
, wxRendererPtrBase
)
48 class wxRendererPtr
: public wxRendererPtrBase
51 // return true if we have a renderer, false otherwise
56 // only try to create the renderer once
65 // return the global and unique wxRendererPtr
66 static wxRendererPtr
& Get();
69 wxRendererPtr() : wxRendererPtrBase(NULL
) { m_initialized
= false; }
73 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
76 // ask the traits object to create a renderer for us
77 reset(traits
->CreateRenderer());
83 // just to suppress a gcc warning
84 friend class wxRendererPtrDummyFriend
;
86 DECLARE_NO_COPY_CLASS(wxRendererPtr
)
89 // return the global and unique wxRendererPtr
90 /*static*/ wxRendererPtr
& wxRendererPtr::Get()
92 static wxRendererPtr s_renderer
;
97 #if wxUSE_DYNLIB_CLASS
99 // ----------------------------------------------------------------------------
100 // wxRendererFromDynLib: represents a renderer dynamically loaded from a DLL
101 // ----------------------------------------------------------------------------
103 class wxRendererFromDynLib
: public wxDelegateRendererNative
106 // create the object wrapping the given renderer created from this DLL
108 // we take ownership of the pointer and will delete it (and also unload the
109 // DLL) when we're deleted
110 wxRendererFromDynLib(wxDynamicLibrary
& dll
, wxRendererNative
*renderer
)
111 : wxDelegateRendererNative(*renderer
),
112 m_renderer(renderer
),
113 m_dllHandle(dll
.Detach())
117 virtual ~wxRendererFromDynLib()
120 wxDynamicLibrary::Unload(m_dllHandle
);
124 wxRendererNative
*m_renderer
;
125 wxDllType m_dllHandle
;
128 #endif // wxUSE_DYNLIB_CLASS
130 // ============================================================================
131 // wxRendererNative implementation
132 // ============================================================================
134 wxRendererNative::~wxRendererNative()
136 // empty but necessary
139 // ----------------------------------------------------------------------------
140 // Managing the global renderer
141 // ----------------------------------------------------------------------------
144 wxRendererNative
& wxRendererNative::Get()
146 wxRendererPtr
& renderer
= wxRendererPtr::Get();
148 return renderer
.IsOk() ? *renderer
.get() : GetDefault();
152 wxRendererNative
*wxRendererNative::Set(wxRendererNative
*rendererNew
)
154 wxRendererPtr
& renderer
= wxRendererPtr::Get();
156 wxRendererNative
*rendererOld
= renderer
.release();
158 renderer
.reset(rendererNew
);
164 // ----------------------------------------------------------------------------
165 // Dynamic renderers loading
166 // ----------------------------------------------------------------------------
168 #if wxUSE_DYNLIB_CLASS
171 wxRendererNative
*wxRendererNative::Load(const wxString
& name
)
173 wxString fullname
= wxDynamicLibrary::CanonicalizePluginName(name
);
175 wxDynamicLibrary
dll(fullname
);
176 if ( !dll
.IsLoaded() )
179 // each theme DLL must export a wxCreateRenderer() function with this
181 typedef wxRendererNative
*(*wxCreateRenderer_t
)();
183 wxDYNLIB_FUNCTION(wxCreateRenderer_t
, wxCreateRenderer
, dll
);
184 if ( !pfnwxCreateRenderer
)
187 // create a renderer object
188 wxRendererNative
*renderer
= (*pfnwxCreateRenderer
)();
192 // check that its version is compatible with ours
193 wxRendererVersion ver
= renderer
->GetVersion();
194 if ( !wxRendererVersion::IsCompatible(ver
) )
196 wxLogError(_("Renderer \"%s\" has incompatible version %d.%d and couldn't be loaded."),
197 name
.c_str(), ver
.version
, ver
.age
);
203 // finally wrap the renderer in an object which will delete it and unload
204 // the library when it is deleted and return it to the caller
205 return new wxRendererFromDynLib(dll
, renderer
);
208 #endif // wxUSE_DYNLIB_CLASS