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.

138 lines
4.2KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2016 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_WIDGET_PRIVATE_DATA_HPP_INCLUDED
  17. #define DGL_WIDGET_PRIVATE_DATA_HPP_INCLUDED
  18. #include "../Widget.hpp"
  19. #include "../Window.hpp"
  20. #include <vector>
  21. START_NAMESPACE_DGL
  22. // -----------------------------------------------------------------------
  23. struct Widget::PrivateData {
  24. Widget* const self;
  25. Window& parent;
  26. Point<int> absolutePos;
  27. Size<uint> size;
  28. std::vector<Widget*> subWidgets;
  29. uint id;
  30. bool needsFullViewport;
  31. bool needsScaling;
  32. bool skipDisplay;
  33. bool visible;
  34. PrivateData(Widget* const s, Window& p, Widget* groupWidget, bool addToSubWidgets)
  35. : self(s),
  36. parent(p),
  37. absolutePos(0, 0),
  38. size(0, 0),
  39. subWidgets(),
  40. id(0),
  41. needsFullViewport(false),
  42. needsScaling(false),
  43. skipDisplay(false),
  44. visible(true)
  45. {
  46. if (addToSubWidgets && groupWidget != nullptr)
  47. {
  48. skipDisplay = true;
  49. groupWidget->pData->subWidgets.push_back(self);
  50. }
  51. }
  52. ~PrivateData()
  53. {
  54. subWidgets.clear();
  55. }
  56. void display(const uint width, const uint height)
  57. {
  58. if (skipDisplay || size.isInvalid() || ! visible)
  59. return;
  60. bool needsDisableScissor = false;
  61. // reset color
  62. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  63. if (needsFullViewport || (absolutePos.isZero() && size == Size<uint>(width, height)))
  64. {
  65. // full viewport size
  66. glViewport(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height));
  67. }
  68. else if (needsScaling)
  69. {
  70. // limit viewport to widget bounds
  71. glViewport(absolutePos.getX(),
  72. static_cast<int>(height - self->getHeight()) - absolutePos.getY(),
  73. static_cast<GLsizei>(self->getWidth()),
  74. static_cast<GLsizei>(self->getHeight()));
  75. }
  76. else
  77. {
  78. // only set viewport pos
  79. glViewport(absolutePos.getX(),
  80. /*static_cast<int>(height - self->getHeight())*/ - absolutePos.getY(),
  81. static_cast<GLsizei>(width),
  82. static_cast<GLsizei>(height));
  83. // then cut the outer bounds
  84. glScissor(absolutePos.getX(),
  85. static_cast<int>(height - self->getHeight()) - absolutePos.getY(),
  86. static_cast<GLsizei>(self->getWidth()),
  87. static_cast<GLsizei>(self->getHeight()));
  88. glEnable(GL_SCISSOR_TEST);
  89. needsDisableScissor = true;
  90. }
  91. // display widget
  92. self->onDisplay();
  93. if (needsDisableScissor)
  94. {
  95. glDisable(GL_SCISSOR_TEST);
  96. needsDisableScissor = false;
  97. }
  98. displaySubWidgets(width, height);
  99. }
  100. void displaySubWidgets(const uint width, const uint height)
  101. {
  102. for (std::vector<Widget*>::iterator it = subWidgets.begin(); it != subWidgets.end(); ++it)
  103. {
  104. Widget* const widget(*it);
  105. DISTRHO_SAFE_ASSERT_CONTINUE(widget->pData != this);
  106. widget->pData->display(width, height);
  107. }
  108. }
  109. DISTRHO_DECLARE_NON_COPY_STRUCT(PrivateData)
  110. };
  111. // -----------------------------------------------------------------------
  112. END_NAMESPACE_DGL
  113. #endif // DGL_WIDGET_PRIVATE_DATA_HPP_INCLUDED