gdiimage.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/msw/gdiimage.h
  3. // Purpose: wxGDIImage class: base class for wxBitmap, wxIcon, wxCursor
  4. // under MSW
  5. // Author: Vadim Zeitlin
  6. // Modified by:
  7. // Created: 20.11.99
  8. // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  9. // Licence: wxWindows licence
  10. ///////////////////////////////////////////////////////////////////////////////
  11. // NB: this is a private header, it is not intended to be directly included by
  12. // user code (but may be included from other, public, wxWin headers
  13. #ifndef _WX_MSW_GDIIMAGE_H_
  14. #define _WX_MSW_GDIIMAGE_H_
  15. #include "wx/gdiobj.h" // base class
  16. #include "wx/gdicmn.h" // wxBITMAP_TYPE_INVALID
  17. #include "wx/list.h"
  18. class WXDLLIMPEXP_FWD_CORE wxGDIImageRefData;
  19. class WXDLLIMPEXP_FWD_CORE wxGDIImageHandler;
  20. class WXDLLIMPEXP_FWD_CORE wxGDIImage;
  21. WX_DECLARE_EXPORTED_LIST(wxGDIImageHandler, wxGDIImageHandlerList);
  22. // ----------------------------------------------------------------------------
  23. // wxGDIImageRefData: common data fields for all derived classes
  24. // ----------------------------------------------------------------------------
  25. class WXDLLIMPEXP_CORE wxGDIImageRefData : public wxGDIRefData
  26. {
  27. public:
  28. wxGDIImageRefData()
  29. {
  30. m_width = m_height = m_depth = 0;
  31. m_handle = 0;
  32. }
  33. wxGDIImageRefData(const wxGDIImageRefData& data) : wxGDIRefData()
  34. {
  35. m_width = data.m_width;
  36. m_height = data.m_height;
  37. m_depth = data.m_depth;
  38. // can't copy handles like this, derived class copy ctor must do it!
  39. m_handle = NULL;
  40. }
  41. // accessors
  42. virtual bool IsOk() const { return m_handle != 0; }
  43. void SetSize(int w, int h) { m_width = w; m_height = h; }
  44. // free the ressources we allocated
  45. virtual void Free() = 0;
  46. // for compatibility, the member fields are public
  47. // the size of the image
  48. int m_width, m_height;
  49. // the depth of the image
  50. int m_depth;
  51. // the handle to it
  52. union
  53. {
  54. WXHANDLE m_handle; // for untyped access
  55. WXHBITMAP m_hBitmap;
  56. WXHICON m_hIcon;
  57. WXHCURSOR m_hCursor;
  58. };
  59. };
  60. // ----------------------------------------------------------------------------
  61. // wxGDIImage: this class supports GDI image handlers which may be registered
  62. // dynamically and will be used for loading/saving the images in the specified
  63. // format. It also falls back to wxImage if no appropriate image is found.
  64. // ----------------------------------------------------------------------------
  65. class WXDLLIMPEXP_CORE wxGDIImage : public wxGDIObject
  66. {
  67. public:
  68. // handlers list interface
  69. static wxGDIImageHandlerList& GetHandlers() { return ms_handlers; }
  70. static void AddHandler(wxGDIImageHandler *handler);
  71. static void InsertHandler(wxGDIImageHandler *handler);
  72. static bool RemoveHandler(const wxString& name);
  73. static wxGDIImageHandler *FindHandler(const wxString& name);
  74. static wxGDIImageHandler *FindHandler(const wxString& extension, long type);
  75. static wxGDIImageHandler *FindHandler(long type);
  76. static void InitStandardHandlers();
  77. static void CleanUpHandlers();
  78. // access to the ref data casted to the right type
  79. wxGDIImageRefData *GetGDIImageData() const
  80. { return (wxGDIImageRefData *)m_refData; }
  81. // accessors
  82. WXHANDLE GetHandle() const
  83. { return IsNull() ? 0 : GetGDIImageData()->m_handle; }
  84. void SetHandle(WXHANDLE handle)
  85. { AllocExclusive(); GetGDIImageData()->m_handle = handle; }
  86. int GetWidth() const { return IsNull() ? 0 : GetGDIImageData()->m_width; }
  87. int GetHeight() const { return IsNull() ? 0 : GetGDIImageData()->m_height; }
  88. int GetDepth() const { return IsNull() ? 0 : GetGDIImageData()->m_depth; }
  89. wxSize GetSize() const
  90. {
  91. return IsNull() ? wxSize(0,0) :
  92. wxSize(GetGDIImageData()->m_width, GetGDIImageData()->m_height);
  93. }
  94. void SetWidth(int w) { AllocExclusive(); GetGDIImageData()->m_width = w; }
  95. void SetHeight(int h) { AllocExclusive(); GetGDIImageData()->m_height = h; }
  96. void SetDepth(int d) { AllocExclusive(); GetGDIImageData()->m_depth = d; }
  97. void SetSize(int w, int h)
  98. {
  99. AllocExclusive();
  100. GetGDIImageData()->SetSize(w, h);
  101. }
  102. void SetSize(const wxSize& size) { SetSize(size.x, size.y); }
  103. // forward some of base class virtuals to wxGDIImageRefData
  104. bool FreeResource(bool force = false);
  105. virtual WXHANDLE GetResourceHandle() const;
  106. protected:
  107. // create the data for the derived class here
  108. virtual wxGDIImageRefData *CreateData() const = 0;
  109. // implement the wxGDIObject method in terms of our, more specific, one
  110. virtual wxGDIRefData *CreateGDIRefData() const { return CreateData(); }
  111. // we can't [efficiently] clone objects of this class
  112. virtual wxGDIRefData *
  113. CloneGDIRefData(const wxGDIRefData *WXUNUSED(data)) const
  114. {
  115. wxFAIL_MSG( wxT("must be implemented if used") );
  116. return NULL;
  117. }
  118. static wxGDIImageHandlerList ms_handlers;
  119. };
  120. // ----------------------------------------------------------------------------
  121. // wxGDIImageHandler: a class which knows how to load/save wxGDIImages.
  122. // ----------------------------------------------------------------------------
  123. class WXDLLIMPEXP_CORE wxGDIImageHandler : public wxObject
  124. {
  125. public:
  126. // ctor
  127. wxGDIImageHandler() { m_type = wxBITMAP_TYPE_INVALID; }
  128. wxGDIImageHandler(const wxString& name,
  129. const wxString& ext,
  130. wxBitmapType type)
  131. : m_name(name), m_extension(ext), m_type(type) { }
  132. // accessors
  133. void SetName(const wxString& name) { m_name = name; }
  134. void SetExtension(const wxString& ext) { m_extension = ext; }
  135. void SetType(wxBitmapType type) { m_type = type; }
  136. const wxString& GetName() const { return m_name; }
  137. const wxString& GetExtension() const { return m_extension; }
  138. wxBitmapType GetType() const { return m_type; }
  139. // real handler operations: to implement in derived classes
  140. virtual bool Create(wxGDIImage *image,
  141. const void* data,
  142. wxBitmapType flags,
  143. int width, int height, int depth = 1) = 0;
  144. virtual bool Load(wxGDIImage *image,
  145. const wxString& name,
  146. wxBitmapType flags,
  147. int desiredWidth, int desiredHeight) = 0;
  148. virtual bool Save(const wxGDIImage *image,
  149. const wxString& name,
  150. wxBitmapType type) const = 0;
  151. protected:
  152. wxString m_name;
  153. wxString m_extension;
  154. wxBitmapType m_type;
  155. };
  156. #endif // _WX_MSW_GDIIMAGE_H_