1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2019 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 #include "WidgetPrivateData.hpp"
18 
19 #ifdef DGL_CAIRO
20 # include "../Cairo.hpp"
21 #endif
22 #ifdef DGL_OPENGL
23 # include "../OpenGL.hpp"
24 #endif
25 
26 START_NAMESPACE_DGL
27 
28 // -----------------------------------------------------------------------
29 
display(const uint width,const uint height,const double scaling,const bool renderingSubWidget)30 void Widget::PrivateData::display(const uint width,
31                                   const uint height,
32                                   const double scaling,
33                                   const bool renderingSubWidget)
34 {
35     if ((skipDisplay && ! renderingSubWidget) || size.isInvalid() || ! visible)
36         return;
37 
38 #ifdef DGL_OPENGL
39     bool needsDisableScissor = false;
40 
41     // reset color
42     glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
43 
44     if (needsFullViewport || (absolutePos.isZero() && size == Size<uint>(width, height)))
45     {
46         // full viewport size
47         glViewport(0,
48                     -(height * scaling - height),
49                     width * scaling,
50                     height * scaling);
51     }
52     else if (needsScaling)
53     {
54         // limit viewport to widget bounds
55         glViewport(absolutePos.getX(),
56                     height - self->getHeight() - absolutePos.getY(),
57                     self->getWidth(),
58                     self->getHeight());
59     }
60     else
61     {
62         // only set viewport pos
63         glViewport(absolutePos.getX() * scaling,
64                     -std::round((height * scaling - height) + (absolutePos.getY() * scaling)),
65                     std::round(width * scaling),
66                     std::round(height * scaling));
67 
68         // then cut the outer bounds
69         glScissor(absolutePos.getX() * scaling,
70                   height - std::round((self->getHeight() + absolutePos.getY()) * scaling),
71                   std::round(self->getWidth() * scaling),
72                   std::round(self->getHeight() * scaling));
73 
74         glEnable(GL_SCISSOR_TEST);
75         needsDisableScissor = true;
76     }
77 #endif
78 
79 #ifdef DGL_CAIRO
80     cairo_t* cr = parent.getGraphicsContext().cairo;
81     cairo_matrix_t matrix;
82     cairo_get_matrix(cr, &matrix);
83     cairo_translate(cr, absolutePos.getX(), absolutePos.getY());
84     // TODO: scaling and cropping
85 #endif
86 
87     // display widget
88     self->onDisplay();
89 
90 #ifdef DGL_CAIRO
91     cairo_set_matrix(cr, &matrix);
92 #endif
93 
94 #ifdef DGL_OPENGL
95     if (needsDisableScissor)
96     {
97         glDisable(GL_SCISSOR_TEST);
98         needsDisableScissor = false;
99     }
100 #endif
101 
102     displaySubWidgets(width, height, scaling);
103 }
104 
105 // -----------------------------------------------------------------------
106 
107 END_NAMESPACE_DGL
108