]>
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
7 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
31 #include "wx/apptrait.h"
32 #include "wx/renderer.h"
34 #include "wx/scopedptr.h"
36 #if wxUSE_DYNLIB_CLASS
37 #include "wx/dynlib.h"
38 #endif // wxUSE_DYNLIB_CLASS
40 // ----------------------------------------------------------------------------
41 // wxRendererPtr: auto pointer holding the global renderer
42 // ----------------------------------------------------------------------------
44 typedef wxScopedPtr
<wxRendererNative
> wxRendererPtrBase
;
46 class wxRendererPtr
: public wxRendererPtrBase
49 // return true if we have a renderer, false otherwise
54 // only try to create the renderer once
63 // return the global and unique wxRendererPtr
64 static wxRendererPtr
& Get();
67 wxRendererPtr() : wxRendererPtrBase(NULL
) { m_initialized
= false; }
71 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
74 // ask the traits object to create a renderer for us
75 reset(traits
->CreateRenderer());
81 // just to suppress a gcc warning
82 friend class wxRendererPtrDummyFriend
;
84 wxDECLARE_NO_COPY_CLASS(wxRendererPtr
);
87 // return the global and unique wxRendererPtr
88 /*static*/ wxRendererPtr
& wxRendererPtr::Get()
90 static wxRendererPtr s_renderer
;
95 #if wxUSE_DYNLIB_CLASS
97 // ----------------------------------------------------------------------------
98 // wxRendererFromDynLib: represents a renderer dynamically loaded from a DLL
99 // ----------------------------------------------------------------------------
101 class wxRendererFromDynLib
: public wxDelegateRendererNative
104 // create the object wrapping the given renderer created from this DLL
106 // we take ownership of the pointer and will delete it (and also unload the
107 // DLL) when we're deleted
108 wxRendererFromDynLib(wxDynamicLibrary
& dll
, wxRendererNative
*renderer
)
109 : wxDelegateRendererNative(*renderer
),
110 m_renderer(renderer
),
111 m_dllHandle(dll
.Detach())
115 virtual ~wxRendererFromDynLib()
118 wxDynamicLibrary::Unload(m_dllHandle
);
122 wxRendererNative
*m_renderer
;
123 wxDllType m_dllHandle
;
126 #endif // wxUSE_DYNLIB_CLASS
128 // ============================================================================
129 // wxRendererNative implementation
130 // ============================================================================
132 wxRendererNative::~wxRendererNative()
134 // empty but necessary
137 // ----------------------------------------------------------------------------
138 // Managing the global renderer
139 // ----------------------------------------------------------------------------
142 wxRendererNative
& wxRendererNative::Get()
144 wxRendererPtr
& renderer
= wxRendererPtr::Get();
146 return renderer
.IsOk() ? *renderer
.get() : GetDefault();
150 wxRendererNative
*wxRendererNative::Set(wxRendererNative
*rendererNew
)
152 wxRendererPtr
& renderer
= wxRendererPtr::Get();
154 wxRendererNative
*rendererOld
= renderer
.release();
156 renderer
.reset(rendererNew
);
162 // ----------------------------------------------------------------------------
163 // Dynamic renderers loading
164 // ----------------------------------------------------------------------------
166 #if wxUSE_DYNLIB_CLASS
169 wxRendererNative
*wxRendererNative::Load(const wxString
& name
)
171 wxString fullname
= wxDynamicLibrary::CanonicalizePluginName(name
);
173 wxDynamicLibrary
dll(fullname
);
174 if ( !dll
.IsLoaded() )
177 // each theme DLL must export a wxCreateRenderer() function with this
179 typedef wxRendererNative
*(*wxCreateRenderer_t
)();
181 wxDYNLIB_FUNCTION(wxCreateRenderer_t
, wxCreateRenderer
, dll
);
182 if ( !pfnwxCreateRenderer
)
185 // create a renderer object
186 wxRendererNative
*renderer
= (*pfnwxCreateRenderer
)();
190 // check that its version is compatible with ours
191 wxRendererVersion ver
= renderer
->GetVersion();
192 if ( !wxRendererVersion::IsCompatible(ver
) )
194 wxLogError(_("Renderer \"%s\" has incompatible version %d.%d and couldn't be loaded."),
195 name
.c_str(), ver
.version
, ver
.age
);
201 // finally wrap the renderer in an object which will delete it and unload
202 // the library when it is deleted and return it to the caller
203 return new wxRendererFromDynLib(dll
, renderer
);
206 #endif // wxUSE_DYNLIB_CLASS