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