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.

178 lines
5.2KB

  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 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. Draw this image at position @a pos using the graphics context @a context.
  69. */
  70. void drawAt(const GraphicsContext& context, const Point<int>& pos) override;
  71. /**
  72. TODO document this.
  73. */
  74. CairoImage& operator=(const CairoImage& image) noexcept;
  75. // FIXME this should not be needed
  76. inline void loadFromMemory(const char* rawData, uint w, uint h, ImageFormat format = kImageFormatBGRA)
  77. { loadFromMemory(rawData, Size<uint>(w, h), format); };
  78. inline void draw(const GraphicsContext& context)
  79. { drawAt(context, Point<int>(0, 0)); };
  80. inline void drawAt(const GraphicsContext& context, int x, int y)
  81. { drawAt(context, Point<int>(x, y)); };
  82. private:
  83. cairo_surface_t* surface;
  84. uchar* surfacedata;
  85. int* datarefcount;
  86. };
  87. // --------------------------------------------------------------------------------------------------------------------
  88. /**
  89. CairoWidget, handy class that takes graphics context during onDisplay and passes it in a new function.
  90. */
  91. template <class BaseWidget>
  92. class CairoBaseWidget : public BaseWidget
  93. {
  94. public:
  95. /**
  96. Constructor for a CairoSubWidget.
  97. */
  98. explicit CairoBaseWidget(Widget* const parentGroupWidget);
  99. /**
  100. Constructor for a CairoTopLevelWidget.
  101. */
  102. explicit CairoBaseWidget(Window& windowToMapTo);
  103. /**
  104. Constructor for a CairoStandaloneWindow without parent window.
  105. */
  106. explicit CairoBaseWidget(Application& app);
  107. /**
  108. Constructor for a CairoStandaloneWindow with parent window.
  109. */
  110. explicit CairoBaseWidget(Application& app, Window& parentWindow);
  111. /**
  112. Destructor.
  113. */
  114. virtual ~CairoBaseWidget() {}
  115. protected:
  116. /**
  117. New virtual onDisplay function.
  118. @see onDisplay
  119. */
  120. virtual void onCairoDisplay(const CairoGraphicsContext& context) = 0;
  121. private:
  122. /**
  123. Widget display function.
  124. Implemented internally to pass context into the drawing function.
  125. */
  126. void onDisplay() override
  127. {
  128. const CairoGraphicsContext& context((const CairoGraphicsContext&)BaseWidget::getGraphicsContext());
  129. onCairoDisplay(context);
  130. }
  131. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CairoBaseWidget);
  132. };
  133. typedef CairoBaseWidget<SubWidget> CairoSubWidget;
  134. typedef CairoBaseWidget<TopLevelWidget> CairoTopLevelWidget;
  135. typedef CairoBaseWidget<StandaloneWindow> CairoStandaloneWindow;
  136. // --------------------------------------------------------------------------------------------------------------------
  137. typedef ImageBaseAboutWindow<CairoImage> CairoImageAboutWindow;
  138. typedef ImageBaseButton<CairoImage> CairoImageButton;
  139. typedef ImageBaseKnob<CairoImage> CairoImageKnob;
  140. typedef ImageBaseSlider<CairoImage> CairoImageSlider;
  141. typedef ImageBaseSwitch<CairoImage> CairoImageSwitch;
  142. // --------------------------------------------------------------------------------------------------------------------
  143. END_NAMESPACE_DGL
  144. #endif