Cairo.hpp (5757B)
1 /* 2 * DISTRHO Plugin Framework (DPF) 3 * Copyright (C) 2012-2022 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 17 #ifndef DGL_CAIRO_HPP_INCLUDED 18 #define DGL_CAIRO_HPP_INCLUDED 19 20 #include "ImageBase.hpp" 21 #include "ImageBaseWidgets.hpp" 22 23 #include <cairo.h> 24 25 START_NAMESPACE_DGL 26 27 // -------------------------------------------------------------------------------------------------------------------- 28 29 /** 30 Cairo Graphics context. 31 */ 32 struct CairoGraphicsContext : GraphicsContext 33 { 34 cairo_t* handle; 35 }; 36 37 // -------------------------------------------------------------------------------------------------------------------- 38 39 /** 40 Cairo Image class. 41 42 TODO ... 43 */ 44 class CairoImage : public ImageBase 45 { 46 public: 47 /** 48 Constructor for a null Image. 49 */ 50 CairoImage(); 51 52 /** 53 Constructor using raw image data. 54 @note @a rawData must remain valid for the lifetime of this Image. 55 */ 56 CairoImage(const char* rawData, uint width, uint height, ImageFormat format); 57 58 /** 59 Constructor using raw image data. 60 @note @a rawData must remain valid for the lifetime of this Image. 61 */ 62 CairoImage(const char* rawData, const Size<uint>& size, ImageFormat format); 63 64 /** 65 Constructor using another image data. 66 */ 67 CairoImage(const CairoImage& image); 68 69 /** 70 Destructor. 71 */ 72 ~CairoImage() override; 73 74 /** 75 Load raw image data from memory. 76 @note @a rawData must remain valid for the lifetime of this Image. 77 */ 78 void loadFromMemory(const char* rawData, 79 const Size<uint>& size, 80 ImageFormat format = kImageFormatBGRA) noexcept override; 81 82 /** 83 Load PNG image from memory. 84 Image size is read from PNG contents. 85 @note @a pngData must remain valid for the lifetime of this Image. 86 */ 87 void loadFromPNG(const char* pngData, uint dataSize) noexcept; 88 89 /** 90 Draw this image at position @a pos using the graphics context @a context. 91 */ 92 void drawAt(const GraphicsContext& context, const Point<int>& pos) override; 93 94 /** 95 Get the cairo surface currently associated with this image. 96 FIXME might be removed 97 */ 98 inline cairo_surface_t* getSurface() const noexcept 99 { 100 return surface; 101 } 102 103 /** 104 TODO document this. 105 */ 106 CairoImage& operator=(const CairoImage& image) noexcept; 107 108 // FIXME this should not be needed 109 inline void loadFromMemory(const char* rdata, uint w, uint h, ImageFormat fmt = kImageFormatBGRA) 110 { loadFromMemory(rdata, Size<uint>(w, h), fmt); }; 111 inline void draw(const GraphicsContext& context) 112 { drawAt(context, Point<int>(0, 0)); }; 113 inline void drawAt(const GraphicsContext& context, int x, int y) 114 { drawAt(context, Point<int>(x, y)); }; 115 116 private: 117 cairo_surface_t* surface; 118 uchar* surfacedata; 119 int* datarefcount; 120 }; 121 122 // -------------------------------------------------------------------------------------------------------------------- 123 124 /** 125 CairoWidget, handy class that takes graphics context during onDisplay and passes it in a new function. 126 */ 127 template <class BaseWidget> 128 class CairoBaseWidget : public BaseWidget 129 { 130 public: 131 /** 132 Constructor for a CairoSubWidget. 133 */ 134 explicit CairoBaseWidget(Widget* const parentGroupWidget); 135 136 /** 137 Constructor for a CairoTopLevelWidget. 138 */ 139 explicit CairoBaseWidget(Window& windowToMapTo); 140 141 /** 142 Constructor for a CairoStandaloneWindow without parent window. 143 */ 144 explicit CairoBaseWidget(Application& app); 145 146 /** 147 Constructor for a CairoStandaloneWindow with parent window. 148 */ 149 explicit CairoBaseWidget(Application& app, Window& parentWindow); 150 151 /** 152 Destructor. 153 */ 154 ~CairoBaseWidget() override {} 155 156 protected: 157 /** 158 New virtual onDisplay function. 159 @see onDisplay 160 */ 161 virtual void onCairoDisplay(const CairoGraphicsContext& context) = 0; 162 163 private: 164 /** 165 Widget display function. 166 Implemented internally to pass context into the drawing function. 167 */ 168 void onDisplay() override 169 { 170 const CairoGraphicsContext& context((const CairoGraphicsContext&)BaseWidget::getGraphicsContext()); 171 onCairoDisplay(context); 172 } 173 174 DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CairoBaseWidget); 175 }; 176 177 typedef CairoBaseWidget<SubWidget> CairoSubWidget; 178 typedef CairoBaseWidget<TopLevelWidget> CairoTopLevelWidget; 179 typedef CairoBaseWidget<StandaloneWindow> CairoStandaloneWindow; 180 181 // -------------------------------------------------------------------------------------------------------------------- 182 183 typedef ImageBaseAboutWindow<CairoImage> CairoImageAboutWindow; 184 typedef ImageBaseButton<CairoImage> CairoImageButton; 185 typedef ImageBaseKnob<CairoImage> CairoImageKnob; 186 typedef ImageBaseSlider<CairoImage> CairoImageSlider; 187 typedef ImageBaseSwitch<CairoImage> CairoImageSwitch; 188 189 // -------------------------------------------------------------------------------------------------------------------- 190 191 END_NAMESPACE_DGL 192 193 #endif