EditorWidget-win.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*****************************************************************************
  2. Copyright (C) 2016-2017 by Colin Edwards.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. *****************************************************************************/
  14. #include <QGridLayout>
  15. #include "../headers/EditorWidget.h"
  16. void EditorWidget::buildEffectContainer(AEffect *effect)
  17. {
  18. WNDCLASSEXW wcex{sizeof(wcex)};
  19. wcex.lpfnWndProc = DefWindowProcW;
  20. wcex.hInstance = GetModuleHandleW(nullptr);
  21. wcex.lpszClassName = L"Minimal VST host - Guest VST Window Frame";
  22. RegisterClassExW(&wcex);
  23. const auto style = WS_CAPTION | WS_THICKFRAME | WS_OVERLAPPEDWINDOW;
  24. windowHandle =
  25. CreateWindowW(wcex.lpszClassName, TEXT(""), style, 0, 0, 0, 0, nullptr, nullptr, nullptr, nullptr);
  26. // set pointer to vst effect for window long
  27. LONG_PTR wndPtr = (LONG_PTR)effect;
  28. SetWindowLongPtr(windowHandle, -21 /*GWLP_USERDATA*/, wndPtr);
  29. QWidget *widget = QWidget::createWindowContainer(QWindow::fromWinId((WId)windowHandle), nullptr);
  30. widget->move(0, 0);
  31. QGridLayout *layout = new QGridLayout();
  32. layout->setContentsMargins(0, 0, 0, 0);
  33. layout->setSpacing(0);
  34. setLayout(layout);
  35. layout->addWidget(widget);
  36. effect->dispatcher(effect, effEditOpen, 0, 0, windowHandle, 0);
  37. VstRect *vstRect = nullptr;
  38. effect->dispatcher(effect, effEditGetRect, 0, 0, &vstRect, 0);
  39. if (vstRect) {
  40. // on Windows, the size reported by 'effect' is larger than
  41. // its actuall size by a factor of the monitor's ui scale,
  42. // so the window size should be divided by the factor
  43. qreal scale_factor = devicePixelRatioF();
  44. int width = vstRect->right - vstRect->left;
  45. int height = vstRect->bottom - vstRect->top;
  46. width = static_cast<int>(width / scale_factor);
  47. height = static_cast<int>(height / scale_factor);
  48. widget->resize(width, height);
  49. resize(width, height);
  50. } else {
  51. widget->resize(300, 300);
  52. }
  53. }
  54. void EditorWidget::handleResizeRequest(int, int)
  55. {
  56. // Some plugins can't resize automatically (like SPAN by Voxengo),
  57. // so we must resize window manually
  58. // get pointer to vst effect from window long
  59. LONG_PTR wndPtr = (LONG_PTR)GetWindowLongPtrW(windowHandle, -21 /*GWLP_USERDATA*/);
  60. AEffect *effect = (AEffect *)(wndPtr);
  61. VstRect *rec = nullptr;
  62. effect->dispatcher(effect, effEditGetRect, 0, 0, &rec, 0);
  63. if (rec) {
  64. // on Windows, the size reported by 'effect' is larger than
  65. // its actuall size by a factor of the monitor's ui scale,
  66. // so the window size should be divided by the factor
  67. qreal scale_factor = devicePixelRatioF();
  68. int width = rec->right - rec->left;
  69. int height = rec->bottom - rec->top;
  70. width = static_cast<int>(width / scale_factor);
  71. height = static_cast<int>(height / scale_factor);
  72. resize(width, height);
  73. }
  74. }