bitmap.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/msw/bitmap.h
  3. // Purpose: wxBitmap class
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created: 01/02/97
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_BITMAP_H_
  11. #define _WX_BITMAP_H_
  12. #include "wx/msw/gdiimage.h"
  13. #include "wx/math.h"
  14. #include "wx/palette.h"
  15. class WXDLLIMPEXP_FWD_CORE wxBitmap;
  16. class WXDLLIMPEXP_FWD_CORE wxBitmapHandler;
  17. class WXDLLIMPEXP_FWD_CORE wxBitmapRefData;
  18. class WXDLLIMPEXP_FWD_CORE wxControl;
  19. class WXDLLIMPEXP_FWD_CORE wxCursor;
  20. class WXDLLIMPEXP_FWD_CORE wxDC;
  21. #if wxUSE_WXDIB
  22. class WXDLLIMPEXP_FWD_CORE wxDIB;
  23. #endif
  24. class WXDLLIMPEXP_FWD_CORE wxIcon;
  25. class WXDLLIMPEXP_FWD_CORE wxMask;
  26. class WXDLLIMPEXP_FWD_CORE wxPalette;
  27. class WXDLLIMPEXP_FWD_CORE wxPixelDataBase;
  28. // What kind of transparency should a bitmap copied from an icon or cursor
  29. // have?
  30. enum wxBitmapTransparency
  31. {
  32. wxBitmapTransparency_Auto, // default: copy alpha if the source has it
  33. wxBitmapTransparency_None, // never create alpha
  34. wxBitmapTransparency_Always // always use alpha
  35. };
  36. // ----------------------------------------------------------------------------
  37. // wxBitmap: a mono or colour bitmap
  38. // NOTE: for wxMSW we don't use the wxBitmapBase base class declared in bitmap.h!
  39. // ----------------------------------------------------------------------------
  40. class WXDLLIMPEXP_CORE wxBitmap : public wxGDIImage,
  41. public wxBitmapHelpers
  42. {
  43. public:
  44. // default ctor creates an invalid bitmap, you must Create() it later
  45. wxBitmap() { }
  46. // Initialize with raw data
  47. wxBitmap(const char bits[], int width, int height, int depth = 1);
  48. // Initialize with XPM data
  49. wxBitmap(const char* const* data);
  50. #ifdef wxNEEDS_CHARPP
  51. wxBitmap(char** data)
  52. {
  53. *this = wxBitmap(const_cast<const char* const*>(data));
  54. }
  55. #endif
  56. // Load a file or resource
  57. wxBitmap(const wxString& name, wxBitmapType type = wxBITMAP_DEFAULT_TYPE);
  58. // New constructor for generalised creation from data
  59. wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth = 1);
  60. // Create a new, uninitialized bitmap of the given size and depth (if it
  61. // is omitted, will create a bitmap compatible with the display)
  62. //
  63. // NB: this ctor will create a DIB for 24 and 32bpp bitmaps, use ctor
  64. // taking a DC argument if you want to force using DDB in this case
  65. wxBitmap(int width, int height, int depth = -1) { (void)Create(width, height, depth); }
  66. wxBitmap(const wxSize& sz, int depth = -1) { (void)Create(sz, depth); }
  67. // Create a bitmap compatible with the given DC
  68. wxBitmap(int width, int height, const wxDC& dc);
  69. #if wxUSE_IMAGE
  70. // Convert from wxImage
  71. wxBitmap(const wxImage& image, int depth = -1)
  72. { (void)CreateFromImage(image, depth); }
  73. // Create a DDB compatible with the given DC from wxImage
  74. wxBitmap(const wxImage& image, const wxDC& dc)
  75. { (void)CreateFromImage(image, dc); }
  76. #endif // wxUSE_IMAGE
  77. // we must have this, otherwise icons are silently copied into bitmaps using
  78. // the copy ctor but the resulting bitmap is invalid!
  79. wxBitmap(const wxIcon& icon,
  80. wxBitmapTransparency transp = wxBitmapTransparency_Auto)
  81. {
  82. CopyFromIcon(icon, transp);
  83. }
  84. wxBitmap& operator=(const wxIcon& icon)
  85. {
  86. (void)CopyFromIcon(icon);
  87. return *this;
  88. }
  89. wxBitmap& operator=(const wxCursor& cursor)
  90. {
  91. (void)CopyFromCursor(cursor);
  92. return *this;
  93. }
  94. virtual ~wxBitmap();
  95. #if wxUSE_IMAGE
  96. wxImage ConvertToImage() const;
  97. wxBitmap ConvertToDisabled(unsigned char brightness = 255) const;
  98. #endif // wxUSE_IMAGE
  99. // get the given part of bitmap
  100. wxBitmap GetSubBitmap( const wxRect& rect ) const;
  101. // NB: This should not be called from user code. It is for wx internal
  102. // use only.
  103. wxBitmap GetSubBitmapOfHDC( const wxRect& rect, WXHDC hdc ) const;
  104. // copies the contents and mask of the given (colour) icon to the bitmap
  105. bool CopyFromIcon(const wxIcon& icon,
  106. wxBitmapTransparency transp = wxBitmapTransparency_Auto);
  107. // copies the contents and mask of the given cursor to the bitmap
  108. bool CopyFromCursor(const wxCursor& cursor,
  109. wxBitmapTransparency transp = wxBitmapTransparency_Auto);
  110. #if wxUSE_WXDIB
  111. // copies from a device independent bitmap
  112. bool CopyFromDIB(const wxDIB& dib);
  113. bool IsDIB() const;
  114. bool ConvertToDIB();
  115. #endif
  116. virtual bool Create(int width, int height, int depth = wxBITMAP_SCREEN_DEPTH);
  117. virtual bool Create(const wxSize& sz, int depth = wxBITMAP_SCREEN_DEPTH)
  118. { return Create(sz.GetWidth(), sz.GetHeight(), depth); }
  119. virtual bool Create(int width, int height, const wxDC& dc);
  120. virtual bool Create(const void* data, wxBitmapType type, int width, int height, int depth = 1);
  121. virtual bool CreateScaled(int w, int h, int d, double logicalScale)
  122. { return Create(wxRound(w*logicalScale), wxRound(h*logicalScale), d); }
  123. virtual bool LoadFile(const wxString& name, wxBitmapType type = wxBITMAP_DEFAULT_TYPE);
  124. virtual bool SaveFile(const wxString& name, wxBitmapType type, const wxPalette *cmap = NULL) const;
  125. wxBitmapRefData *GetBitmapData() const
  126. { return (wxBitmapRefData *)m_refData; }
  127. // raw bitmap access support functions
  128. void *GetRawData(wxPixelDataBase& data, int bpp);
  129. void UngetRawData(wxPixelDataBase& data);
  130. #if wxUSE_PALETTE
  131. wxPalette* GetPalette() const;
  132. void SetPalette(const wxPalette& palette);
  133. #endif // wxUSE_PALETTE
  134. wxMask *GetMask() const;
  135. void SetMask(wxMask *mask);
  136. // these functions are internal and shouldn't be used, they risk to
  137. // disappear in the future
  138. bool HasAlpha() const;
  139. void UseAlpha();
  140. void ResetAlpha();
  141. // support for scaled bitmaps
  142. virtual double GetScaleFactor() const { return 1.0; }
  143. virtual double GetScaledWidth() const { return GetWidth() / GetScaleFactor(); }
  144. virtual double GetScaledHeight() const { return GetHeight() / GetScaleFactor(); }
  145. virtual wxSize GetScaledSize() const
  146. { return wxSize(wxRound(GetScaledWidth()), wxRound(GetScaledHeight())); }
  147. // implementation only from now on
  148. // -------------------------------
  149. // Set alpha flag to true if this is a 32bpp bitmap which has any non-0
  150. // values in its alpha channel.
  151. void MSWUpdateAlpha();
  152. public:
  153. void SetHBITMAP(WXHBITMAP bmp) { SetHandle((WXHANDLE)bmp); }
  154. WXHBITMAP GetHBITMAP() const { return (WXHBITMAP)GetHandle(); }
  155. void SetSelectedInto(wxDC *dc);
  156. wxDC *GetSelectedInto() const;
  157. protected:
  158. virtual wxGDIImageRefData *CreateData() const;
  159. virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const;
  160. // creates an uninitialized bitmap, called from Create()s above
  161. bool DoCreate(int w, int h, int depth, WXHDC hdc);
  162. #if wxUSE_IMAGE
  163. // creates the bitmap from wxImage, supposed to be called from ctor
  164. bool CreateFromImage(const wxImage& image, int depth);
  165. // creates a DDB from wxImage, supposed to be called from ctor
  166. bool CreateFromImage(const wxImage& image, const wxDC& dc);
  167. // common part of the 2 methods above (hdc may be 0)
  168. bool CreateFromImage(const wxImage& image, int depth, WXHDC hdc);
  169. #endif // wxUSE_IMAGE
  170. private:
  171. // common part of CopyFromIcon/CopyFromCursor for Win32
  172. bool
  173. CopyFromIconOrCursor(const wxGDIImage& icon,
  174. wxBitmapTransparency transp = wxBitmapTransparency_Auto);
  175. DECLARE_DYNAMIC_CLASS(wxBitmap)
  176. };
  177. // ----------------------------------------------------------------------------
  178. // wxMask: a mono bitmap used for drawing bitmaps transparently.
  179. // ----------------------------------------------------------------------------
  180. class WXDLLIMPEXP_CORE wxMask : public wxObject
  181. {
  182. public:
  183. wxMask();
  184. // Copy constructor
  185. wxMask(const wxMask &mask);
  186. // Construct a mask from a bitmap and a colour indicating the transparent
  187. // area
  188. wxMask(const wxBitmap& bitmap, const wxColour& colour);
  189. // Construct a mask from a bitmap and a palette index indicating the
  190. // transparent area
  191. wxMask(const wxBitmap& bitmap, int paletteIndex);
  192. // Construct a mask from a mono bitmap (copies the bitmap).
  193. wxMask(const wxBitmap& bitmap);
  194. // construct a mask from the givne bitmap handle
  195. wxMask(WXHBITMAP hbmp) { m_maskBitmap = hbmp; }
  196. virtual ~wxMask();
  197. bool Create(const wxBitmap& bitmap, const wxColour& colour);
  198. bool Create(const wxBitmap& bitmap, int paletteIndex);
  199. bool Create(const wxBitmap& bitmap);
  200. wxBitmap GetBitmap() const;
  201. // Implementation
  202. WXHBITMAP GetMaskBitmap() const { return m_maskBitmap; }
  203. void SetMaskBitmap(WXHBITMAP bmp) { m_maskBitmap = bmp; }
  204. protected:
  205. WXHBITMAP m_maskBitmap;
  206. DECLARE_DYNAMIC_CLASS(wxMask)
  207. };
  208. // ----------------------------------------------------------------------------
  209. // wxBitmapHandler is a class which knows how to load/save bitmaps to/from file
  210. // NOTE: for wxMSW we don't use the wxBitmapHandler class declared in bitmap.h!
  211. // ----------------------------------------------------------------------------
  212. class WXDLLIMPEXP_CORE wxBitmapHandler : public wxGDIImageHandler
  213. {
  214. public:
  215. wxBitmapHandler() { }
  216. wxBitmapHandler(const wxString& name, const wxString& ext, wxBitmapType type)
  217. : wxGDIImageHandler(name, ext, type) { }
  218. // implement wxGDIImageHandler's pure virtuals:
  219. virtual bool Create(wxGDIImage *image,
  220. const void* data,
  221. wxBitmapType type,
  222. int width, int height, int depth = 1);
  223. virtual bool Load(wxGDIImage *image,
  224. const wxString& name,
  225. wxBitmapType type,
  226. int desiredWidth, int desiredHeight);
  227. virtual bool Save(const wxGDIImage *image,
  228. const wxString& name,
  229. wxBitmapType type) const;
  230. // make wxBitmapHandler compatible with the wxBitmapHandler interface
  231. // declared in bitmap.h, even if it's derived from wxGDIImageHandler:
  232. virtual bool Create(wxBitmap *bitmap,
  233. const void* data,
  234. wxBitmapType type,
  235. int width, int height, int depth = 1);
  236. virtual bool LoadFile(wxBitmap *bitmap,
  237. const wxString& name,
  238. wxBitmapType type,
  239. int desiredWidth, int desiredHeight);
  240. virtual bool SaveFile(const wxBitmap *bitmap,
  241. const wxString& name,
  242. wxBitmapType type,
  243. const wxPalette *palette = NULL) const;
  244. private:
  245. DECLARE_DYNAMIC_CLASS(wxBitmapHandler)
  246. };
  247. #endif
  248. // _WX_BITMAP_H_