bitmap.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/bitmap.h
  3. // Purpose: wxBitmap class interface
  4. // Author: Vaclav Slavik
  5. // Modified by:
  6. // Created: 22.04.01
  7. // Copyright: (c) wxWidgets team
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_BITMAP_H_BASE_
  11. #define _WX_BITMAP_H_BASE_
  12. // ----------------------------------------------------------------------------
  13. // headers
  14. // ----------------------------------------------------------------------------
  15. #include "wx/string.h"
  16. #include "wx/gdicmn.h" // for wxBitmapType
  17. #include "wx/colour.h"
  18. #include "wx/image.h"
  19. class WXDLLIMPEXP_FWD_CORE wxBitmap;
  20. class WXDLLIMPEXP_FWD_CORE wxBitmapHandler;
  21. class WXDLLIMPEXP_FWD_CORE wxIcon;
  22. class WXDLLIMPEXP_FWD_CORE wxMask;
  23. class WXDLLIMPEXP_FWD_CORE wxPalette;
  24. class WXDLLIMPEXP_FWD_CORE wxDC;
  25. // ----------------------------------------------------------------------------
  26. // wxVariant support
  27. // ----------------------------------------------------------------------------
  28. #if wxUSE_VARIANT
  29. #include "wx/variant.h"
  30. DECLARE_VARIANT_OBJECT_EXPORTED(wxBitmap,WXDLLIMPEXP_CORE)
  31. #endif
  32. // ----------------------------------------------------------------------------
  33. // wxMask represents the transparent area of the bitmap
  34. // ----------------------------------------------------------------------------
  35. // TODO: all implementation of wxMask, except the generic one,
  36. // do not derive from wxMaskBase,,, they should
  37. class WXDLLIMPEXP_CORE wxMaskBase : public wxObject
  38. {
  39. public:
  40. // create the mask from bitmap pixels of the given colour
  41. bool Create(const wxBitmap& bitmap, const wxColour& colour);
  42. #if wxUSE_PALETTE
  43. // create the mask from bitmap pixels with the given palette index
  44. bool Create(const wxBitmap& bitmap, int paletteIndex);
  45. #endif // wxUSE_PALETTE
  46. // create the mask from the given mono bitmap
  47. bool Create(const wxBitmap& bitmap);
  48. protected:
  49. // this function is called from Create() to free the existing mask data
  50. virtual void FreeData() = 0;
  51. // these functions must be overridden to implement the corresponding public
  52. // Create() methods, they shouldn't call FreeData() as it's already called
  53. // by the public wrappers
  54. virtual bool InitFromColour(const wxBitmap& bitmap,
  55. const wxColour& colour) = 0;
  56. virtual bool InitFromMonoBitmap(const wxBitmap& bitmap) = 0;
  57. };
  58. #if defined(__WXDFB__) || \
  59. defined(__WXMAC__) || \
  60. defined(__WXGTK__) || \
  61. defined(__WXCOCOA__) || \
  62. defined(__WXMOTIF__) || \
  63. defined(__WXX11__)
  64. #define wxUSE_BITMAP_BASE 1
  65. #else
  66. #define wxUSE_BITMAP_BASE 0
  67. #endif
  68. // a more readable way to tell
  69. #define wxBITMAP_SCREEN_DEPTH (-1)
  70. // ----------------------------------------------------------------------------
  71. // wxBitmapHelpers: container for various bitmap methods common to all ports.
  72. // ----------------------------------------------------------------------------
  73. // Unfortunately, currently wxBitmap does not inherit from wxBitmapBase on all
  74. // platforms and this is not easy to fix. So we extract at least some common
  75. // methods into this class from which both wxBitmapBase (and hence wxBitmap on
  76. // all platforms where it does inherit from it) and wxBitmap in wxMSW and other
  77. // exceptional ports (only wxPM and old wxCocoa) inherit.
  78. class WXDLLIMPEXP_CORE wxBitmapHelpers
  79. {
  80. public:
  81. // Create a new wxBitmap from the PNG data in the given buffer.
  82. static wxBitmap NewFromPNGData(const void* data, size_t size);
  83. };
  84. // All ports except wxMSW and wxOS2 use wxBitmapHandler and wxBitmapBase as
  85. // base class for wxBitmapHandler; wxMSW and wxOS2 use wxGDIImageHandler as
  86. // base class since it allows some code reuse there.
  87. #if wxUSE_BITMAP_BASE
  88. // ----------------------------------------------------------------------------
  89. // wxBitmapHandler: class which knows how to create/load/save bitmaps in
  90. // different formats
  91. // ----------------------------------------------------------------------------
  92. class WXDLLIMPEXP_CORE wxBitmapHandler : public wxObject
  93. {
  94. public:
  95. wxBitmapHandler() { m_type = wxBITMAP_TYPE_INVALID; }
  96. virtual ~wxBitmapHandler() { }
  97. // NOTE: the following functions should be pure virtuals, but they aren't
  98. // because otherwise almost all ports would have to implement
  99. // them as "return false"...
  100. virtual bool Create(wxBitmap *WXUNUSED(bitmap), const void* WXUNUSED(data),
  101. wxBitmapType WXUNUSED(type), int WXUNUSED(width), int WXUNUSED(height),
  102. int WXUNUSED(depth) = 1)
  103. { return false; }
  104. virtual bool LoadFile(wxBitmap *WXUNUSED(bitmap), const wxString& WXUNUSED(name),
  105. wxBitmapType WXUNUSED(type), int WXUNUSED(desiredWidth),
  106. int WXUNUSED(desiredHeight))
  107. { return false; }
  108. virtual bool SaveFile(const wxBitmap *WXUNUSED(bitmap), const wxString& WXUNUSED(name),
  109. wxBitmapType WXUNUSED(type), const wxPalette *WXUNUSED(palette) = NULL) const
  110. { return false; }
  111. void SetName(const wxString& name) { m_name = name; }
  112. void SetExtension(const wxString& ext) { m_extension = ext; }
  113. void SetType(wxBitmapType type) { m_type = type; }
  114. const wxString& GetName() const { return m_name; }
  115. const wxString& GetExtension() const { return m_extension; }
  116. wxBitmapType GetType() const { return m_type; }
  117. private:
  118. wxString m_name;
  119. wxString m_extension;
  120. wxBitmapType m_type;
  121. DECLARE_ABSTRACT_CLASS(wxBitmapHandler)
  122. };
  123. // ----------------------------------------------------------------------------
  124. // wxBitmap: class which represents platform-dependent bitmap (unlike wxImage)
  125. // ----------------------------------------------------------------------------
  126. class WXDLLIMPEXP_CORE wxBitmapBase : public wxGDIObject,
  127. public wxBitmapHelpers
  128. {
  129. public:
  130. /*
  131. Derived class must implement these:
  132. wxBitmap();
  133. wxBitmap(const wxBitmap& bmp);
  134. wxBitmap(const char bits[], int width, int height, int depth = 1);
  135. wxBitmap(int width, int height, int depth = wxBITMAP_SCREEN_DEPTH);
  136. wxBitmap(const wxSize& sz, int depth = wxBITMAP_SCREEN_DEPTH);
  137. wxBitmap(const char* const* bits);
  138. wxBitmap(const wxString &filename, wxBitmapType type = wxBITMAP_TYPE_XPM);
  139. wxBitmap(const wxImage& image, int depth = wxBITMAP_SCREEN_DEPTH);
  140. static void InitStandardHandlers();
  141. */
  142. virtual bool Create(int width, int height, int depth = wxBITMAP_SCREEN_DEPTH) = 0;
  143. virtual bool Create(const wxSize& sz, int depth = wxBITMAP_SCREEN_DEPTH) = 0;
  144. virtual bool CreateScaled(int w, int h, int d, double logicalScale)
  145. { return Create(w*logicalScale,h*logicalScale,d); }
  146. virtual int GetHeight() const = 0;
  147. virtual int GetWidth() const = 0;
  148. virtual int GetDepth() const = 0;
  149. wxSize GetSize() const
  150. { return wxSize(GetWidth(), GetHeight()); }
  151. // support for scaled bitmaps
  152. virtual double GetScaleFactor() const { return 1.0; }
  153. virtual double GetScaledWidth() const { return GetWidth() / GetScaleFactor(); }
  154. virtual double GetScaledHeight() const { return GetHeight() / GetScaleFactor(); }
  155. virtual wxSize GetScaledSize() const
  156. { return wxSize(GetScaledWidth(), GetScaledHeight()); }
  157. #if wxUSE_IMAGE
  158. virtual wxImage ConvertToImage() const = 0;
  159. // Convert to disabled (dimmed) bitmap.
  160. wxBitmap ConvertToDisabled(unsigned char brightness = 255) const;
  161. #endif // wxUSE_IMAGE
  162. virtual wxMask *GetMask() const = 0;
  163. virtual void SetMask(wxMask *mask) = 0;
  164. virtual wxBitmap GetSubBitmap(const wxRect& rect) const = 0;
  165. virtual bool SaveFile(const wxString &name, wxBitmapType type,
  166. const wxPalette *palette = NULL) const = 0;
  167. virtual bool LoadFile(const wxString &name, wxBitmapType type) = 0;
  168. /*
  169. If raw bitmap access is supported (see wx/rawbmp.h), the following
  170. methods should be implemented:
  171. virtual bool GetRawData(wxRawBitmapData *data) = 0;
  172. virtual void UngetRawData(wxRawBitmapData *data) = 0;
  173. */
  174. #if wxUSE_PALETTE
  175. virtual wxPalette *GetPalette() const = 0;
  176. virtual void SetPalette(const wxPalette& palette) = 0;
  177. #endif // wxUSE_PALETTE
  178. // copies the contents and mask of the given (colour) icon to the bitmap
  179. virtual bool CopyFromIcon(const wxIcon& icon) = 0;
  180. // implementation:
  181. virtual void SetHeight(int height) = 0;
  182. virtual void SetWidth(int width) = 0;
  183. virtual void SetDepth(int depth) = 0;
  184. // Format handling
  185. static inline wxList& GetHandlers() { return sm_handlers; }
  186. static void AddHandler(wxBitmapHandler *handler);
  187. static void InsertHandler(wxBitmapHandler *handler);
  188. static bool RemoveHandler(const wxString& name);
  189. static wxBitmapHandler *FindHandler(const wxString& name);
  190. static wxBitmapHandler *FindHandler(const wxString& extension, wxBitmapType bitmapType);
  191. static wxBitmapHandler *FindHandler(wxBitmapType bitmapType);
  192. //static void InitStandardHandlers();
  193. // (wxBitmap must implement this one)
  194. static void CleanUpHandlers();
  195. // this method is only used by the generic implementation of wxMask
  196. // currently but could be useful elsewhere in the future: it can be
  197. // overridden to quantize the colour to correspond to bitmap colour depth
  198. // if necessary; default implementation simply returns the colour as is
  199. virtual wxColour QuantizeColour(const wxColour& colour) const
  200. {
  201. return colour;
  202. }
  203. protected:
  204. static wxList sm_handlers;
  205. DECLARE_ABSTRACT_CLASS(wxBitmapBase)
  206. };
  207. #endif // wxUSE_BITMAP_BASE
  208. // the wxBITMAP_DEFAULT_TYPE constant defines the default argument value
  209. // for wxBitmap's ctor and wxBitmap::LoadFile() functions.
  210. #if defined(__WXMSW__)
  211. #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_BMP_RESOURCE
  212. #include "wx/msw/bitmap.h"
  213. #elif defined(__WXMOTIF__)
  214. #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_XPM
  215. #include "wx/x11/bitmap.h"
  216. #elif defined(__WXGTK20__)
  217. #ifdef __WINDOWS__
  218. #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_BMP_RESOURCE
  219. #else
  220. #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_XPM
  221. #endif
  222. #include "wx/gtk/bitmap.h"
  223. #elif defined(__WXGTK__)
  224. #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_XPM
  225. #include "wx/gtk1/bitmap.h"
  226. #elif defined(__WXX11__)
  227. #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_XPM
  228. #include "wx/x11/bitmap.h"
  229. #elif defined(__WXDFB__)
  230. #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_BMP_RESOURCE
  231. #include "wx/dfb/bitmap.h"
  232. #elif defined(__WXMAC__)
  233. #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_PICT_RESOURCE
  234. #include "wx/osx/bitmap.h"
  235. #elif defined(__WXCOCOA__)
  236. #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_BMP_RESOURCE
  237. #include "wx/cocoa/bitmap.h"
  238. #elif defined(__WXPM__)
  239. #define wxBITMAP_DEFAULT_TYPE wxBITMAP_TYPE_BMP_RESOURCE
  240. #include "wx/os2/bitmap.h"
  241. #endif
  242. #if wxUSE_IMAGE
  243. inline
  244. wxBitmap
  245. #if wxUSE_BITMAP_BASE
  246. wxBitmapBase::
  247. #else
  248. wxBitmap::
  249. #endif
  250. ConvertToDisabled(unsigned char brightness) const
  251. {
  252. return ConvertToImage().ConvertToDisabled(brightness);
  253. }
  254. #endif // wxUSE_IMAGE
  255. // we must include generic mask.h after wxBitmap definition
  256. #if defined(__WXDFB__)
  257. #define wxUSE_GENERIC_MASK 1
  258. #else
  259. #define wxUSE_GENERIC_MASK 0
  260. #endif
  261. #if wxUSE_GENERIC_MASK
  262. #include "wx/generic/mask.h"
  263. #endif
  264. #endif // _WX_BITMAP_H_BASE_