]> git.saurik.com Git - wxWidgets.git/blame - src/common/rendcmn.cpp
updated wxExecute checks in wxGTK to be same as in other Unix ports after recent...
[wxWidgets.git] / src / common / rendcmn.cpp
CommitLineData
f0244295 1///////////////////////////////////////////////////////////////////////////////
9d17ee00 2// Name: src/common/rendcmn.cpp
f0244295
VZ
3// Purpose: wxRendererNative common functions
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 28.07.03
7// RCS-ID: $Id$
8// Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
0a53b9b8 9// License: wxWindows license
f0244295
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#ifndef WX_PRECOMP
3d3a4a25 27 #include "wx/app.h"
194142ed
VZ
28 #include "wx/log.h"
29 #include "wx/intl.h"
f0244295
VZ
30#endif //WX_PRECOMP
31
32#include "wx/apptrait.h"
33#include "wx/renderer.h"
34
35#include "wx/ptr_scpd.h"
36
9f2be125
VZ
37#if wxUSE_DYNLIB_CLASS
38 #include "wx/dynlib.h"
39#endif // wxUSE_DYNLIB_CLASS
40
f0244295
VZ
41// ----------------------------------------------------------------------------
42// wxRendererPtr: auto pointer holding the global renderer
43// ----------------------------------------------------------------------------
44
d0ee33f5
WS
45wxDECLARE_SCOPED_PTR(wxRendererNative, wxRendererPtrBase)
46wxDEFINE_SCOPED_PTR(wxRendererNative, wxRendererPtrBase)
f0244295
VZ
47
48class wxRendererPtr : public wxRendererPtrBase
49{
50public:
f0244295
VZ
51 // return true if we have a renderer, false otherwise
52 bool IsOk()
53 {
54 if ( !m_initialized )
55 {
56 // only try to create the renderer once
57 m_initialized = true;
58
59 DoInit();
60 }
61
62 return get() != NULL;
63 }
64
9f2be125 65 // return the global and unique wxRendererPtr
25bb0f89 66 static wxRendererPtr& Get();
9f2be125 67
f0244295 68private:
9f2be125
VZ
69 wxRendererPtr() : wxRendererPtrBase(NULL) { m_initialized = false; }
70
f0244295
VZ
71 void DoInit()
72 {
73 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
74 if ( traits )
75 {
76 // ask the traits object to create a renderer for us
77 reset(traits->CreateRenderer());
78 }
79 }
80
81 bool m_initialized;
82
04857cb7
VZ
83 // just to suppress a gcc warning
84 friend class wxRendererPtrDummyFriend;
85
f0244295
VZ
86 DECLARE_NO_COPY_CLASS(wxRendererPtr)
87};
88
25bb0f89
MW
89// return the global and unique wxRendererPtr
90/*static*/ wxRendererPtr& wxRendererPtr::Get()
91{
92 static wxRendererPtr s_renderer;
93
94 return s_renderer;
95}
96
9f2be125
VZ
97#if wxUSE_DYNLIB_CLASS
98
99// ----------------------------------------------------------------------------
100// wxRendererFromDynLib: represents a renderer dynamically loaded from a DLL
101// ----------------------------------------------------------------------------
102
103class wxRendererFromDynLib : public wxDelegateRendererNative
104{
105public:
106 // create the object wrapping the given renderer created from this DLL
107 //
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())
114 {
115 }
116
117 virtual ~wxRendererFromDynLib()
118 {
119 delete m_renderer;
120 wxDynamicLibrary::Unload(m_dllHandle);
121 }
122
123private:
124 wxRendererNative *m_renderer;
125 wxDllType m_dllHandle;
126};
127
128#endif // wxUSE_DYNLIB_CLASS
129
f0244295
VZ
130// ============================================================================
131// wxRendererNative implementation
132// ============================================================================
133
04857cb7
VZ
134wxRendererNative::~wxRendererNative()
135{
136 // empty but necessary
137}
138
9f2be125
VZ
139// ----------------------------------------------------------------------------
140// Managing the global renderer
141// ----------------------------------------------------------------------------
142
f0244295
VZ
143/* static */
144wxRendererNative& wxRendererNative::Get()
145{
9f2be125 146 wxRendererPtr& renderer = wxRendererPtr::Get();
f0244295 147
9f2be125 148 return renderer.IsOk() ? *renderer.get() : GetDefault();
f0244295
VZ
149}
150
9f2be125
VZ
151/* static */
152wxRendererNative *wxRendererNative::Set(wxRendererNative *rendererNew)
153{
154 wxRendererPtr& renderer = wxRendererPtr::Get();
155
156 wxRendererNative *rendererOld = renderer.release();
157
158 renderer.reset(rendererNew);
159
160 return rendererOld;
161}
162
163
164// ----------------------------------------------------------------------------
165// Dynamic renderers loading
166// ----------------------------------------------------------------------------
167
168#if wxUSE_DYNLIB_CLASS
169
170/* static */
171wxRendererNative *wxRendererNative::Load(const wxString& name)
172{
173 wxString fullname = wxDynamicLibrary::CanonicalizePluginName(name);
174
175 wxDynamicLibrary dll(fullname);
176 if ( !dll.IsLoaded() )
177 return NULL;
178
179 // each theme DLL must export a wxCreateRenderer() function with this
180 // signature
181 typedef wxRendererNative *(*wxCreateRenderer_t)();
182
183 wxDYNLIB_FUNCTION(wxCreateRenderer_t, wxCreateRenderer, dll);
184 if ( !pfnwxCreateRenderer )
185 return NULL;
186
04857cb7 187 // create a renderer object
9f2be125
VZ
188 wxRendererNative *renderer = (*pfnwxCreateRenderer)();
189 if ( !renderer )
190 return NULL;
191
04857cb7
VZ
192 // check that its version is compatible with ours
193 wxRendererVersion ver = renderer->GetVersion();
194 if ( !wxRendererVersion::IsCompatible(ver) )
195 {
196 wxLogError(_("Renderer \"%s\" has incompatible version %d.%d and couldn't be loaded."),
197 name.c_str(), ver.version, ver.age);
198 delete renderer;
199
200 return NULL;
201 }
202
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
9f2be125
VZ
205 return new wxRendererFromDynLib(dll, renderer);
206}
207
208#endif // wxUSE_DYNLIB_CLASS