Move wxVariant support for GDI classes to their
[wxWidgets.git] / src / common / bmpbase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/bmpbase.cpp
3 // Purpose: wxBitmapBase
4 // Author: VaclavSlavik
5 // Created: 2001/04/11
6 // RCS-ID: $Id$
7 // Copyright: (c) 2001, Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #include "wx/bitmap.h"
19
20 #ifndef WX_PRECOMP
21 #include "wx/colour.h"
22 #endif // WX_PRECOMP
23
24 // ----------------------------------------------------------------------------
25 // wxBitmapBase
26 // ----------------------------------------------------------------------------
27
28 #if wxUSE_BITMAP_BASE
29
30 #ifndef WX_PRECOMP
31 #include "wx/log.h"
32 #include "wx/utils.h"
33 #include "wx/palette.h"
34 #include "wx/icon.h"
35 #include "wx/image.h"
36 #include "wx/module.h"
37 #endif // WX_PRECOMP
38
39
40 #if wxUSE_VARIANT
41 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxBitmap,WXDLLEXPORT)
42 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxIcon,WXDLLEXPORT)
43 #endif
44
45
46 IMPLEMENT_ABSTRACT_CLASS(wxBitmapBase, wxGDIObject)
47 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandlerBase,wxObject)
48
49 wxList wxBitmapBase::sm_handlers;
50
51 void wxBitmapBase::AddHandler(wxBitmapHandlerBase *handler)
52 {
53 sm_handlers.Append(handler);
54 }
55
56 void wxBitmapBase::InsertHandler(wxBitmapHandlerBase *handler)
57 {
58 sm_handlers.Insert(handler);
59 }
60
61 bool wxBitmapBase::RemoveHandler(const wxString& name)
62 {
63 wxBitmapHandler *handler = FindHandler(name);
64 if ( handler )
65 {
66 sm_handlers.DeleteObject(handler);
67 return true;
68 }
69 else
70 return false;
71 }
72
73 wxBitmapHandler *wxBitmapBase::FindHandler(const wxString& name)
74 {
75 wxList::compatibility_iterator node = sm_handlers.GetFirst();
76 while ( node )
77 {
78 wxBitmapHandler *handler = (wxBitmapHandler *)node->GetData();
79 if ( handler->GetName() == name )
80 return handler;
81 node = node->GetNext();
82 }
83 return NULL;
84 }
85
86 wxBitmapHandler *wxBitmapBase::FindHandler(const wxString& extension, wxBitmapType bitmapType)
87 {
88 wxList::compatibility_iterator node = sm_handlers.GetFirst();
89 while ( node )
90 {
91 wxBitmapHandler *handler = (wxBitmapHandler *)node->GetData();
92 if ( handler->GetExtension() == extension &&
93 (bitmapType == wxBITMAP_TYPE_ANY || handler->GetType() == bitmapType) )
94 return handler;
95 node = node->GetNext();
96 }
97 return NULL;
98 }
99
100 wxBitmapHandler *wxBitmapBase::FindHandler(wxBitmapType bitmapType)
101 {
102 wxList::compatibility_iterator node = sm_handlers.GetFirst();
103 while ( node )
104 {
105 wxBitmapHandler *handler = (wxBitmapHandler *)node->GetData();
106 if (handler->GetType() == bitmapType)
107 return handler;
108 node = node->GetNext();
109 }
110 return NULL;
111 }
112
113 void wxBitmapBase::CleanUpHandlers()
114 {
115 wxList::compatibility_iterator node = sm_handlers.GetFirst();
116 while ( node )
117 {
118 wxBitmapHandler *handler = (wxBitmapHandler *)node->GetData();
119 wxList::compatibility_iterator next = node->GetNext();
120 delete handler;
121 sm_handlers.Erase(node);
122 node = next;
123 }
124 }
125
126 class wxBitmapBaseModule: public wxModule
127 {
128 DECLARE_DYNAMIC_CLASS(wxBitmapBaseModule)
129 public:
130 wxBitmapBaseModule() {}
131 bool OnInit() { wxBitmap::InitStandardHandlers(); return true; };
132 void OnExit() { wxBitmap::CleanUpHandlers(); };
133 };
134
135 IMPLEMENT_DYNAMIC_CLASS(wxBitmapBaseModule, wxModule)
136
137 #endif // wxUSE_BITMAP_BASE
138
139 // ----------------------------------------------------------------------------
140 // wxMaskBase
141 // ----------------------------------------------------------------------------
142
143 bool wxMaskBase::Create(const wxBitmap& bitmap, const wxColour& colour)
144 {
145 FreeData();
146
147 return InitFromColour(bitmap, colour);
148 }
149
150 #if wxUSE_PALETTE
151
152 bool wxMaskBase::Create(const wxBitmap& bitmap, int paletteIndex)
153 {
154 wxPalette *pal = bitmap.GetPalette();
155
156 wxCHECK_MSG( pal, false,
157 wxT("Cannot create mask from palette index of a bitmap without palette") );
158
159 unsigned char r,g,b;
160 pal->GetRGB(paletteIndex, &r, &g, &b);
161
162 return Create(bitmap, wxColour(r, g, b));
163 }
164
165 #endif // wxUSE_PALETTE
166
167 bool wxMaskBase::Create(const wxBitmap& bitmap)
168 {
169 FreeData();
170
171 return InitFromMonoBitmap(bitmap);
172 }