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.

179 lines
5.1KB

  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 "SubWidget.hpp"
  21. #include <cairo/cairo.h>
  22. START_NAMESPACE_DGL
  23. // --------------------------------------------------------------------------------------------------------------------
  24. /**
  25. Cairo Graphics context.
  26. */
  27. struct CairoGraphicsContext : GraphicsContext
  28. {
  29. cairo_t* handle;
  30. };
  31. // --------------------------------------------------------------------------------------------------------------------
  32. /**
  33. Cairo Image class.
  34. TODO ...
  35. */
  36. class CairoImage : public ImageBase
  37. {
  38. public:
  39. /**
  40. Constructor for a null Image.
  41. */
  42. CairoImage();
  43. /**
  44. Constructor using raw image data.
  45. @note @a rawData must remain valid for the lifetime of this Image.
  46. */
  47. CairoImage(const char* rawData, uint width, uint height, ImageFormat format);
  48. /**
  49. Constructor using raw image data.
  50. @note @a rawData must remain valid for the lifetime of this Image.
  51. */
  52. CairoImage(const char* rawData, const Size<uint>& size, ImageFormat format);
  53. /**
  54. Constructor using another image data.
  55. */
  56. CairoImage(const CairoImage& image);
  57. /**
  58. Destructor.
  59. */
  60. ~CairoImage() override;
  61. /**
  62. Load image data from memory.
  63. @note @a rawData must remain valid for the lifetime of this Image.
  64. */
  65. void loadFromMemory(const char* rawData,
  66. const Size<uint>& size,
  67. ImageFormat format = kImageFormatBGRA) noexcept override;
  68. /**
  69. Draw this image at position @a pos using the graphics context @a context.
  70. */
  71. void drawAt(const GraphicsContext& context, const Point<int>& pos) override;
  72. /**
  73. TODO document this.
  74. */
  75. CairoImage& operator=(const CairoImage& image) noexcept;
  76. // FIXME this should not be needed
  77. inline void loadFromMemory(const char* rawData, uint w, uint h, ImageFormat format = kImageFormatBGRA)
  78. { loadFromMemory(rawData, Size<uint>(w, h), format); };
  79. inline void draw(const GraphicsContext& context)
  80. { drawAt(context, Point<int>(0, 0)); };
  81. inline void drawAt(const GraphicsContext& context, int x, int y)
  82. { drawAt(context, Point<int>(x, y)); };
  83. private:
  84. cairo_surface_t* surface;
  85. uchar* surfacedata;
  86. int* datarefcount;
  87. };
  88. // --------------------------------------------------------------------------------------------------------------------
  89. /**
  90. CairoWidget, handy class that takes graphics context during onDisplay and passes it in a new function.
  91. */
  92. template <class BaseWidget>
  93. class CairoWidget : public BaseWidget
  94. {
  95. public:
  96. /**
  97. Constructor for a CairoSubWidget.
  98. @see CreateFlags
  99. */
  100. explicit CairoWidget(Widget* const parentGroupWidget);
  101. /**
  102. Constructor for a CairoTopLevelWidget.
  103. @see CreateFlags
  104. */
  105. explicit CairoWidget(Window& windowToMapTo);
  106. /**
  107. Constructor for a CairoStandaloneWindow without parent window.
  108. @see CreateFlags
  109. */
  110. explicit CairoWidget(Application& app);
  111. /**
  112. Constructor for a CairoStandaloneWindow with parent window.
  113. @see CreateFlags
  114. */
  115. explicit CairoWidget(Application& app, Window& parentWindow);
  116. /**
  117. Destructor.
  118. */
  119. virtual ~CairoWidget() {}
  120. protected:
  121. /**
  122. New virtual onDisplay function.
  123. @see onDisplay
  124. */
  125. virtual void onCairoDisplay(const CairoGraphicsContext& context) = 0;
  126. private:
  127. /**
  128. Widget display function.
  129. Implemented internally to pass context into the drawing function.
  130. */
  131. void onDisplay() override
  132. {
  133. const CairoGraphicsContext& context((const CairoGraphicsContext&)BaseWidget::getGraphicsContext());
  134. onCairoDisplay(context);
  135. }
  136. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CairoWidget);
  137. };
  138. typedef CairoWidget<SubWidget> CairoSubWidget;
  139. typedef CairoWidget<TopLevelWidget> CairoTopLevelWidget;
  140. typedef CairoWidget<StandaloneWindow> CairoStandaloneWindow;
  141. // --------------------------------------------------------------------------------------------------------------------
  142. typedef ImageBaseAboutWindow<CairoImage> CairoImageAboutWindow;
  143. // --------------------------------------------------------------------------------------------------------------------
  144. END_NAMESPACE_DGL
  145. #endif