bitmap.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/os2/bitmap.h
  3. // Purpose: wxBitmap class
  4. // Author: David Webster
  5. // Modified by:
  6. // Created: 11/28/99
  7. // Copyright: (c) David Webster
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_BITMAP_H_
  11. #define _WX_BITMAP_H_
  12. #include "wx/os2/private.h"
  13. #include "wx/os2/gdiimage.h"
  14. #include "wx/gdicmn.h"
  15. #include "wx/palette.h"
  16. class WXDLLIMPEXP_FWD_CORE wxDC;
  17. class WXDLLIMPEXP_FWD_CORE wxControl;
  18. class WXDLLIMPEXP_FWD_CORE wxBitmap;
  19. class WXDLLIMPEXP_FWD_CORE wxBitmapHandler;
  20. class WXDLLIMPEXP_FWD_CORE wxIcon;
  21. class WXDLLIMPEXP_FWD_CORE wxMask;
  22. class WXDLLIMPEXP_FWD_CORE wxCursor;
  23. class WXDLLIMPEXP_FWD_CORE wxControl;
  24. class WXDLLIMPEXP_FWD_CORE wxPixelDataBase;
  25. // ----------------------------------------------------------------------------
  26. // Bitmap data
  27. //
  28. // NB: this class is private, but declared here to make it possible inline
  29. // wxBitmap functions accessing it
  30. // ----------------------------------------------------------------------------
  31. class WXDLLIMPEXP_CORE wxBitmapRefData : public wxGDIImageRefData
  32. {
  33. public:
  34. wxBitmapRefData();
  35. wxBitmapRefData(const wxBitmapRefData &tocopy);
  36. virtual ~wxBitmapRefData() { Free(); }
  37. virtual void Free();
  38. public:
  39. int m_nNumColors;
  40. wxPalette m_vBitmapPalette;
  41. int m_nQuality;
  42. // OS2-specific
  43. // ------------
  44. wxDC* m_pSelectedInto;
  45. //
  46. // Optional mask for transparent drawing
  47. //
  48. wxMask* m_pBitmapMask;
  49. }; // end of CLASS wxBitmapRefData
  50. // ----------------------------------------------------------------------------
  51. // wxBitmap: a mono or colour bitmap
  52. // ----------------------------------------------------------------------------
  53. class WXDLLIMPEXP_CORE wxBitmap : public wxGDIImage,
  54. public wxBitmapHelpers
  55. {
  56. public:
  57. // default ctor creates an invalid bitmap, you must Create() it later
  58. wxBitmap() { Init(); }
  59. // Copy constructors
  60. inline wxBitmap(const wxBitmap& rBitmap)
  61. : wxGDIImage(rBitmap)
  62. {
  63. Init();
  64. SetHandle(rBitmap.GetHandle());
  65. }
  66. // Initialize with raw data
  67. wxBitmap( const char bits[]
  68. ,int nWidth
  69. ,int nHeight
  70. ,int nDepth = 1
  71. );
  72. // Initialize with XPM data
  73. wxBitmap(const char* const* bits);
  74. #ifdef wxNEEDS_CHARPP
  75. // needed for old GCC
  76. wxBitmap(char** data)
  77. {
  78. *this = wxBitmap(const_cast<const char* const*>(data));
  79. }
  80. #endif
  81. // Load a resource
  82. wxBitmap( int nId
  83. ,wxBitmapType lType = wxBITMAP_DEFAULT_TYPE
  84. );
  85. // For compatiability with other ports, under OS/2 does same as default ctor
  86. inline wxBitmap( const wxString& WXUNUSED(rFilename)
  87. ,wxBitmapType WXUNUSED(lType)
  88. )
  89. { Init(); }
  90. // New constructor for generalised creation from data
  91. wxBitmap( const void* pData
  92. ,wxBitmapType lType
  93. ,int nWidth
  94. ,int nHeight
  95. ,int nDepth = 1
  96. );
  97. // If depth is omitted, will create a bitmap compatible with the display
  98. wxBitmap( int nWidth, int nHeight, int nDepth = -1 )
  99. {
  100. Init();
  101. (void)Create(nWidth, nHeight, nDepth);
  102. }
  103. wxBitmap( const wxSize& sz, int nDepth = -1 )
  104. {
  105. Init();
  106. (void)Create(sz, nDepth);
  107. }
  108. wxBitmap( const wxImage& image, int depth = -1 )
  109. { (void)CreateFromImage(image, depth); }
  110. // we must have this, otherwise icons are silently copied into bitmaps using
  111. // the copy ctor but the resulting bitmap is invalid!
  112. inline wxBitmap(const wxIcon& rIcon)
  113. { Init(); CopyFromIcon(rIcon); }
  114. wxBitmap& operator=(const wxIcon& rIcon)
  115. {
  116. (void)CopyFromIcon(rIcon);
  117. return(*this);
  118. }
  119. wxBitmap& operator=(const wxCursor& rCursor)
  120. {
  121. (void)CopyFromCursor(rCursor);
  122. return (*this);
  123. }
  124. virtual ~wxBitmap();
  125. wxImage ConvertToImage() const;
  126. wxBitmap ConvertToDisabled(unsigned char brightness = 255) const;
  127. // get the given part of bitmap
  128. wxBitmap GetSubBitmap(const wxRect& rRect) const;
  129. // copies the contents and mask of the given (colour) icon to the bitmap
  130. bool CopyFromIcon(const wxIcon& rIcon);
  131. // copies the contents and mask of the given cursor to the bitmap
  132. bool CopyFromCursor(const wxCursor& rCursor);
  133. virtual bool Create( int nWidth
  134. ,int nHeight
  135. ,int nDepth = wxBITMAP_SCREEN_DEPTH
  136. );
  137. virtual bool Create(const wxSize& sz, int depth = wxBITMAP_SCREEN_DEPTH)
  138. { return Create(sz.GetWidth(), sz.GetHeight(), depth); }
  139. virtual bool Create(int width, int height, const wxDC& WXUNUSED(dc))
  140. { return Create(width,height); }
  141. virtual bool Create( const void* pData
  142. ,wxBitmapType lType
  143. ,int nWidth
  144. ,int nHeight
  145. ,int nDepth = 1
  146. );
  147. virtual bool LoadFile( int nId
  148. ,wxBitmapType lType = wxBITMAP_DEFAULT_TYPE
  149. );
  150. virtual bool LoadFile( const wxString& rName
  151. ,wxBitmapType lType = wxBITMAP_DEFAULT_TYPE
  152. );
  153. virtual bool SaveFile( const wxString& rName
  154. ,wxBitmapType lType
  155. ,const wxPalette* pCmap = NULL
  156. );
  157. inline wxBitmapRefData* GetBitmapData() const
  158. { return (wxBitmapRefData *)m_refData; }
  159. // raw bitmap access support functions
  160. void *GetRawData(wxPixelDataBase& data, int bpp);
  161. void UngetRawData(wxPixelDataBase& data);
  162. inline int GetQuality() const
  163. { return (GetBitmapData() ? GetBitmapData()->m_nQuality : 0); }
  164. void SetQuality(int nQ);
  165. wxPalette* GetPalette() const
  166. { return (GetBitmapData() ? (& GetBitmapData()->m_vBitmapPalette) : NULL); }
  167. void SetPalette(const wxPalette& rPalette);
  168. inline wxMask* GetMask() const
  169. { return (GetBitmapData() ? GetBitmapData()->m_pBitmapMask : NULL); }
  170. void SetMask(wxMask* pMask) ;
  171. // Implementation
  172. public:
  173. inline void SetHBITMAP(WXHBITMAP hBmp)
  174. { SetHandle((WXHANDLE)hBmp); }
  175. inline WXHBITMAP GetHBITMAP() const
  176. { return (WXHBITMAP)GetHandle(); }
  177. inline void SetSelectedInto(wxDC* pDc)
  178. { if (GetBitmapData()) GetBitmapData()->m_pSelectedInto = pDc; }
  179. inline wxDC* GetSelectedInto() const
  180. { return (GetBitmapData() ? GetBitmapData()->m_pSelectedInto : NULL); }
  181. inline bool IsMono(void) const { return m_bIsMono; }
  182. // An OS/2 version that probably doesn't do anything like the msw version
  183. wxBitmap GetBitmapForDC(wxDC& rDc) const;
  184. protected:
  185. // common part of all ctors
  186. void Init();
  187. inline virtual wxGDIImageRefData* CreateData() const
  188. { return new wxBitmapRefData; }
  189. bool CreateFromImage(const wxImage& image, int depth);
  190. virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const;
  191. private:
  192. bool CopyFromIconOrCursor(const wxGDIImage& rIcon);
  193. bool m_bIsMono;
  194. DECLARE_DYNAMIC_CLASS(wxBitmap)
  195. }; // end of CLASS wxBitmap
  196. // ----------------------------------------------------------------------------
  197. // wxMask: a mono bitmap used for drawing bitmaps transparently.
  198. // ----------------------------------------------------------------------------
  199. class WXDLLIMPEXP_CORE wxMask : public wxObject
  200. {
  201. public:
  202. wxMask();
  203. wxMask( const wxMask& tocopy);
  204. // Construct a mask from a bitmap and a colour indicating the transparent
  205. // area
  206. wxMask( const wxBitmap& rBitmap
  207. ,const wxColour& rColour
  208. );
  209. // Construct a mask from a bitmap and a palette index indicating the
  210. // transparent area
  211. wxMask( const wxBitmap& rBitmap
  212. ,int nPaletteIndex
  213. );
  214. // Construct a mask from a mono bitmap (copies the bitmap).
  215. wxMask(const wxBitmap& rBitmap);
  216. // construct a mask from the givne bitmap handle
  217. wxMask(WXHBITMAP hBmp)
  218. { m_hMaskBitmap = hBmp; }
  219. virtual ~wxMask();
  220. bool Create( const wxBitmap& bitmap
  221. ,const wxColour& rColour
  222. );
  223. bool Create( const wxBitmap& rBitmap
  224. ,int nPaletteIndex
  225. );
  226. bool Create(const wxBitmap& rBitmap);
  227. // Implementation
  228. WXHBITMAP GetMaskBitmap() const
  229. { return m_hMaskBitmap; }
  230. void SetMaskBitmap(WXHBITMAP hBmp)
  231. { m_hMaskBitmap = hBmp; }
  232. protected:
  233. WXHBITMAP m_hMaskBitmap;
  234. DECLARE_DYNAMIC_CLASS(wxMask)
  235. }; // end of CLASS wxMask
  236. // ----------------------------------------------------------------------------
  237. // wxBitmapHandler is a class which knows how to load/save bitmaps to/from file
  238. // ----------------------------------------------------------------------------
  239. class WXDLLIMPEXP_CORE wxBitmapHandler : public wxGDIImageHandler
  240. {
  241. public:
  242. inline wxBitmapHandler()
  243. { m_lType = wxBITMAP_TYPE_INVALID; }
  244. inline wxBitmapHandler( const wxString& rName
  245. ,const wxString& rExt
  246. ,wxBitmapType lType
  247. )
  248. : wxGDIImageHandler( rName
  249. ,rExt
  250. ,lType)
  251. {
  252. }
  253. // keep wxBitmapHandler derived from wxGDIImageHandler compatible with the
  254. // old class which worked only with bitmaps
  255. virtual bool Create( wxBitmap* pBitmap
  256. ,const void* pData
  257. ,wxBitmapType lType
  258. ,int nWidth
  259. ,int nHeight
  260. ,int nDepth = 1
  261. );
  262. virtual bool LoadFile( wxBitmap* pBitmap
  263. ,int nId
  264. ,wxBitmapType lType
  265. ,int nDesiredWidth
  266. ,int nDesiredHeight
  267. );
  268. virtual bool LoadFile( wxBitmap* pBitmap
  269. ,const wxString& rName
  270. ,wxBitmapType lType
  271. ,int nDesiredWidth
  272. ,int nDesiredHeight
  273. );
  274. virtual bool SaveFile( wxBitmap* pBitmap
  275. ,const wxString& rName
  276. ,wxBitmapType lType
  277. ,const wxPalette* pPalette = NULL
  278. ) const;
  279. virtual bool Create( wxGDIImage* pImage
  280. ,const void* pData
  281. ,wxBitmapType lFlags
  282. ,int nWidth
  283. ,int nHeight
  284. ,int nDepth = 1
  285. );
  286. virtual bool Load( wxGDIImage* pImage
  287. ,int nId
  288. ,wxBitmapType lFlags
  289. ,int nDesiredWidth
  290. ,int nDesiredHeight
  291. );
  292. virtual bool Save( const wxGDIImage* pImage
  293. ,const wxString& rName
  294. ,wxBitmapType lType
  295. ) const;
  296. private:
  297. inline virtual bool Load( wxGDIImage* WXUNUSED(pImage)
  298. ,const wxString& WXUNUSED(rName)
  299. ,WXHANDLE WXUNUSED(hPs)
  300. ,wxBitmapType WXUNUSED(lFlags)
  301. ,int WXUNUSED(nDesiredWidth)
  302. ,int WXUNUSED(nDesiredHeight)
  303. )
  304. { return false; }
  305. DECLARE_DYNAMIC_CLASS(wxBitmapHandler)
  306. }; // end of CLASS wxBitmapHandler
  307. #endif
  308. // _WX_BITMAP_H_