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.

177 lines
5.0KB

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