DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
5.6KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DGL_CAIRO_HPP_INCLUDED
  17. #define DGL_CAIRO_HPP_INCLUDED
  18. #include "ImageBase.hpp"
  19. #include "ImageBaseWidgets.hpp"
  20. #include <cairo/cairo.h>
  21. START_NAMESPACE_DGL
  22. // --------------------------------------------------------------------------------------------------------------------
  23. /**
  24. Cairo Graphics context.
  25. */
  26. struct CairoGraphicsContext : GraphicsContext
  27. {
  28. cairo_t* handle;
  29. };
  30. // --------------------------------------------------------------------------------------------------------------------
  31. /**
  32. Cairo Image class.
  33. TODO ...
  34. */
  35. class CairoImage : public ImageBase
  36. {
  37. public:
  38. /**
  39. Constructor for a null Image.
  40. */
  41. CairoImage();
  42. /**
  43. Constructor using raw image data.
  44. @note @a rawData must remain valid for the lifetime of this Image.
  45. */
  46. CairoImage(const char* rawData, uint width, uint height, ImageFormat format);
  47. /**
  48. Constructor using raw image data.
  49. @note @a rawData must remain valid for the lifetime of this Image.
  50. */
  51. CairoImage(const char* rawData, const Size<uint>& size, ImageFormat format);
  52. /**
  53. Constructor using another image data.
  54. */
  55. CairoImage(const CairoImage& image);
  56. /**
  57. Destructor.
  58. */
  59. ~CairoImage() override;
  60. /**
  61. Load raw image data from memory.
  62. @note @a rawData must remain valid for the lifetime of this Image.
  63. */
  64. void loadFromMemory(const char* rawData,
  65. const Size<uint>& size,
  66. ImageFormat format = kImageFormatBGRA) noexcept override;
  67. /**
  68. Load PNG image from memory.
  69. Image size is read from PNG contents.
  70. @note @a pngData must remain valid for the lifetime of this Image.
  71. */
  72. void loadFromPNG(const char* pngData, uint dataSize) noexcept;
  73. /**
  74. Draw this image at position @a pos using the graphics context @a context.
  75. */
  76. void drawAt(const GraphicsContext& context, const Point<int>& pos) override;
  77. /**
  78. Get the cairo surface currently associated with this image.
  79. FIXME might be removed
  80. */
  81. inline cairo_surface_t* getSurface() const noexcept
  82. {
  83. return surface;
  84. }
  85. /**
  86. TODO document this.
  87. */
  88. CairoImage& operator=(const CairoImage& image) noexcept;
  89. // FIXME this should not be needed
  90. inline void loadFromMemory(const char* rawData, uint w, uint h, ImageFormat format = kImageFormatBGRA)
  91. { loadFromMemory(rawData, Size<uint>(w, h), format); };
  92. inline void draw(const GraphicsContext& context)
  93. { drawAt(context, Point<int>(0, 0)); };
  94. inline void drawAt(const GraphicsContext& context, int x, int y)
  95. { drawAt(context, Point<int>(x, y)); };
  96. private:
  97. cairo_surface_t* surface;
  98. uchar* surfacedata;
  99. int* datarefcount;
  100. };
  101. // --------------------------------------------------------------------------------------------------------------------
  102. /**
  103. CairoWidget, handy class that takes graphics context during onDisplay and passes it in a new function.
  104. */
  105. template <class BaseWidget>
  106. class CairoBaseWidget : public BaseWidget
  107. {
  108. public:
  109. /**
  110. Constructor for a CairoSubWidget.
  111. */
  112. explicit CairoBaseWidget(Widget* const parentGroupWidget);
  113. /**
  114. Constructor for a CairoTopLevelWidget.
  115. */
  116. explicit CairoBaseWidget(Window& windowToMapTo);
  117. /**
  118. Constructor for a CairoStandaloneWindow without parent window.
  119. */
  120. explicit CairoBaseWidget(Application& app);
  121. /**
  122. Constructor for a CairoStandaloneWindow with parent window.
  123. */
  124. explicit CairoBaseWidget(Application& app, Window& parentWindow);
  125. /**
  126. Destructor.
  127. */
  128. virtual ~CairoBaseWidget() {}
  129. protected:
  130. /**
  131. New virtual onDisplay function.
  132. @see onDisplay
  133. */
  134. virtual void onCairoDisplay(const CairoGraphicsContext& context) = 0;
  135. private:
  136. /**
  137. Widget display function.
  138. Implemented internally to pass context into the drawing function.
  139. */
  140. void onDisplay() override
  141. {
  142. const CairoGraphicsContext& context((const CairoGraphicsContext&)BaseWidget::getGraphicsContext());
  143. onCairoDisplay(context);
  144. }
  145. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CairoBaseWidget);
  146. };
  147. typedef CairoBaseWidget<SubWidget> CairoSubWidget;
  148. typedef CairoBaseWidget<TopLevelWidget> CairoTopLevelWidget;
  149. typedef CairoBaseWidget<StandaloneWindow> CairoStandaloneWindow;
  150. // --------------------------------------------------------------------------------------------------------------------
  151. typedef ImageBaseAboutWindow<CairoImage> CairoImageAboutWindow;
  152. typedef ImageBaseButton<CairoImage> CairoImageButton;
  153. typedef ImageBaseKnob<CairoImage> CairoImageKnob;
  154. typedef ImageBaseSlider<CairoImage> CairoImageSlider;
  155. typedef ImageBaseSwitch<CairoImage> CairoImageSwitch;
  156. // --------------------------------------------------------------------------------------------------------------------
  157. END_NAMESPACE_DGL
  158. #endif