]>
Commit | Line | Data |
---|---|---|
f0244295 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: common/rendcmn.cpp | |
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> | |
9 | // License: wxWindows license | |
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" |
f0244295 VZ |
28 | #endif //WX_PRECOMP |
29 | ||
30 | #include "wx/apptrait.h" | |
31 | #include "wx/renderer.h" | |
32 | ||
33 | #include "wx/ptr_scpd.h" | |
34 | ||
9f2be125 VZ |
35 | #if wxUSE_DYNLIB_CLASS |
36 | #include "wx/dynlib.h" | |
37 | #endif // wxUSE_DYNLIB_CLASS | |
38 | ||
f0244295 VZ |
39 | // ---------------------------------------------------------------------------- |
40 | // wxRendererPtr: auto pointer holding the global renderer | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
43 | wxDECLARE_SCOPED_PTR(wxRendererNative, wxRendererPtrBase); | |
44 | wxDEFINE_SCOPED_PTR(wxRendererNative, wxRendererPtrBase); | |
45 | ||
46 | class wxRendererPtr : public wxRendererPtrBase | |
47 | { | |
48 | public: | |
f0244295 VZ |
49 | // return true if we have a renderer, false otherwise |
50 | bool IsOk() | |
51 | { | |
52 | if ( !m_initialized ) | |
53 | { | |
54 | // only try to create the renderer once | |
55 | m_initialized = true; | |
56 | ||
57 | DoInit(); | |
58 | } | |
59 | ||
60 | return get() != NULL; | |
61 | } | |
62 | ||
9f2be125 VZ |
63 | // return the global and unique wxRendererPtr |
64 | static wxRendererPtr& Get() | |
65 | { | |
66 | static wxRendererPtr s_renderer; | |
67 | ||
68 | return s_renderer; | |
69 | } | |
70 | ||
f0244295 | 71 | private: |
9f2be125 VZ |
72 | wxRendererPtr() : wxRendererPtrBase(NULL) { m_initialized = false; } |
73 | ||
f0244295 VZ |
74 | void DoInit() |
75 | { | |
76 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; | |
77 | if ( traits ) | |
78 | { | |
79 | // ask the traits object to create a renderer for us | |
80 | reset(traits->CreateRenderer()); | |
81 | } | |
82 | } | |
83 | ||
84 | bool m_initialized; | |
85 | ||
86 | DECLARE_NO_COPY_CLASS(wxRendererPtr) | |
87 | }; | |
88 | ||
9f2be125 VZ |
89 | #if wxUSE_DYNLIB_CLASS |
90 | ||
91 | // ---------------------------------------------------------------------------- | |
92 | // wxRendererFromDynLib: represents a renderer dynamically loaded from a DLL | |
93 | // ---------------------------------------------------------------------------- | |
94 | ||
95 | class wxRendererFromDynLib : public wxDelegateRendererNative | |
96 | { | |
97 | public: | |
98 | // create the object wrapping the given renderer created from this DLL | |
99 | // | |
100 | // we take ownership of the pointer and will delete it (and also unload the | |
101 | // DLL) when we're deleted | |
102 | wxRendererFromDynLib(wxDynamicLibrary& dll, wxRendererNative *renderer) | |
103 | : wxDelegateRendererNative(*renderer), | |
104 | m_renderer(renderer), | |
105 | m_dllHandle(dll.Detach()) | |
106 | { | |
107 | } | |
108 | ||
109 | virtual ~wxRendererFromDynLib() | |
110 | { | |
111 | delete m_renderer; | |
112 | wxDynamicLibrary::Unload(m_dllHandle); | |
113 | } | |
114 | ||
115 | private: | |
116 | wxRendererNative *m_renderer; | |
117 | wxDllType m_dllHandle; | |
118 | }; | |
119 | ||
120 | #endif // wxUSE_DYNLIB_CLASS | |
121 | ||
f0244295 VZ |
122 | // ============================================================================ |
123 | // wxRendererNative implementation | |
124 | // ============================================================================ | |
125 | ||
9f2be125 VZ |
126 | // ---------------------------------------------------------------------------- |
127 | // Managing the global renderer | |
128 | // ---------------------------------------------------------------------------- | |
129 | ||
f0244295 VZ |
130 | /* static */ |
131 | wxRendererNative& wxRendererNative::Get() | |
132 | { | |
9f2be125 | 133 | wxRendererPtr& renderer = wxRendererPtr::Get(); |
f0244295 | 134 | |
9f2be125 | 135 | return renderer.IsOk() ? *renderer.get() : GetDefault(); |
f0244295 VZ |
136 | } |
137 | ||
9f2be125 VZ |
138 | /* static */ |
139 | wxRendererNative *wxRendererNative::Set(wxRendererNative *rendererNew) | |
140 | { | |
141 | wxRendererPtr& renderer = wxRendererPtr::Get(); | |
142 | ||
143 | wxRendererNative *rendererOld = renderer.release(); | |
144 | ||
145 | renderer.reset(rendererNew); | |
146 | ||
147 | return rendererOld; | |
148 | } | |
149 | ||
150 | ||
151 | // ---------------------------------------------------------------------------- | |
152 | // Dynamic renderers loading | |
153 | // ---------------------------------------------------------------------------- | |
154 | ||
155 | #if wxUSE_DYNLIB_CLASS | |
156 | ||
157 | /* static */ | |
158 | wxRendererNative *wxRendererNative::Load(const wxString& name) | |
159 | { | |
160 | wxString fullname = wxDynamicLibrary::CanonicalizePluginName(name); | |
161 | ||
162 | wxDynamicLibrary dll(fullname); | |
163 | if ( !dll.IsLoaded() ) | |
164 | return NULL; | |
165 | ||
166 | // each theme DLL must export a wxCreateRenderer() function with this | |
167 | // signature | |
168 | typedef wxRendererNative *(*wxCreateRenderer_t)(); | |
169 | ||
170 | wxDYNLIB_FUNCTION(wxCreateRenderer_t, wxCreateRenderer, dll); | |
171 | if ( !pfnwxCreateRenderer ) | |
172 | return NULL; | |
173 | ||
174 | wxRendererNative *renderer = (*pfnwxCreateRenderer)(); | |
175 | if ( !renderer ) | |
176 | return NULL; | |
177 | ||
178 | return new wxRendererFromDynLib(dll, renderer); | |
179 | } | |
180 | ||
181 | #endif // wxUSE_DYNLIB_CLASS | |
182 |