1
0

DefaultVstEditor.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * DefaultVstEditor.cpp
  3. * --------------------
  4. * Purpose: Implementation of the default plugin editor that is used if a plugin does not provide an own editor GUI.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "DefaultVstEditor.h"
  11. #include "../soundlib/Sndfile.h"
  12. #include "../soundlib/plugins/PlugInterface.h"
  13. OPENMPT_NAMESPACE_BEGIN
  14. #ifndef NO_PLUGINS
  15. // Window proportions
  16. struct Measurements
  17. {
  18. enum
  19. {
  20. edSpacing = 5, // Spacing between elements
  21. edLineHeight = 20, // Line of a single parameter line
  22. edEditWidth = 45, // Width of the parameter edit box
  23. edPerMilWidth = 30, // Width of the per mil label
  24. edRightWidth = edEditWidth + edSpacing + edPerMilWidth, // Width of the right part of a parameter control set (edit box, param value)
  25. edTotalHeight = 2 * edLineHeight + edSpacing, // Height of one set of controls
  26. };
  27. const int spacing;
  28. const int lineHeight;
  29. const int editWidth;
  30. const int perMilWidth;
  31. const int rightWidth;
  32. const int totalHeight;
  33. Measurements(HWND hWnd)
  34. : spacing(Util::ScalePixels(edSpacing, hWnd))
  35. , lineHeight(Util::ScalePixels(edLineHeight, hWnd))
  36. , editWidth(Util::ScalePixels(edEditWidth, hWnd))
  37. , perMilWidth(Util::ScalePixels(edPerMilWidth, hWnd))
  38. , rightWidth(Util::ScalePixels(edRightWidth, hWnd))
  39. , totalHeight(Util::ScalePixels(edTotalHeight, hWnd))
  40. { }
  41. };
  42. // Create a set of parameter controls
  43. ParamControlSet::ParamControlSet(CWnd *parent, const CRect &rect, int setID, const Measurements &m)
  44. {
  45. // Offset of components on the right side
  46. const int horizSplit = rect.left + rect.Width() - m.rightWidth;
  47. // Parameter name
  48. nameLabel.Create(_T(""), WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE, CRect(rect.left, rect.top, horizSplit - m.spacing, rect.top + m.lineHeight), parent);
  49. nameLabel.SetFont(parent->GetFont());
  50. // Parameter value as reported by the plugin
  51. valueLabel.Create(_T(""), WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE, CRect(horizSplit, rect.top, rect.right, rect.top + m.lineHeight), parent);
  52. valueLabel.SetFont(parent->GetFont());
  53. // Parameter value slider
  54. valueSlider.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP /* | TBS_NOTICKS | TBS_BOTH */ | TBS_AUTOTICKS, CRect(rect.left, rect.bottom - m.lineHeight, horizSplit - m.spacing, rect.bottom), parent, ID_PLUGINEDITOR_SLIDERS_BASE + setID);
  55. valueSlider.SetFont(parent->GetFont());
  56. valueSlider.SetRange(0, PARAM_RESOLUTION);
  57. valueSlider.SetTicFreq(PARAM_RESOLUTION / 10);
  58. // Parameter value edit box
  59. valueEdit.CreateEx(WS_EX_CLIENTEDGE, _T("EDIT"), NULL, WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL | ES_NUMBER, CRect(horizSplit, rect.bottom - m.lineHeight, horizSplit + m.editWidth, rect.bottom), parent, ID_PLUGINEDITOR_EDIT_BASE + setID);
  60. valueEdit.SetFont(parent->GetFont());
  61. // "Per mil" label
  62. perMilLabel.Create(mpt::ToCString(mpt::Charset::UTF8, "\xE2\x80\xB0"), WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE, CRect(horizSplit + m.editWidth + m.spacing, rect.bottom - m.lineHeight, rect.right, rect.bottom), parent);
  63. perMilLabel.SetFont(parent->GetFont());
  64. }
  65. ParamControlSet::~ParamControlSet()
  66. {
  67. nameLabel.DestroyWindow();
  68. valueLabel.DestroyWindow();
  69. valueSlider.DestroyWindow();
  70. valueEdit.DestroyWindow();
  71. perMilLabel.DestroyWindow();
  72. }
  73. // Enable a set of parameter controls
  74. void ParamControlSet::EnableControls(bool enable)
  75. {
  76. const BOOL b = enable ? TRUE : FALSE;
  77. nameLabel.EnableWindow(b);
  78. valueLabel.EnableWindow(b);
  79. valueSlider.EnableWindow(b);
  80. valueEdit.EnableWindow(b);
  81. perMilLabel.EnableWindow(b);
  82. }
  83. // Reset the content of a set of parameter controls
  84. void ParamControlSet::ResetContent()
  85. {
  86. nameLabel.SetWindowText(_T(""));
  87. valueLabel.SetWindowText(_T(""));
  88. valueSlider.SetPos(0);
  89. valueEdit.SetWindowText(_T(""));
  90. }
  91. void ParamControlSet::SetParamName(const CString &name)
  92. {
  93. nameLabel.SetWindowText(name);
  94. }
  95. void ParamControlSet::SetParamValue(int value, const CString &text)
  96. {
  97. valueSlider.SetPos(value);
  98. if (&valueEdit != valueEdit.GetFocus())
  99. {
  100. // Don't update textbox when it has focus, else this will prevent user from changing the content.
  101. CString paramValue;
  102. paramValue.Format(_T("%0000d"), value);
  103. valueEdit.SetWindowText(paramValue);
  104. }
  105. valueLabel.SetWindowText(text);
  106. }
  107. int ParamControlSet::GetParamValueFromSlider() const
  108. {
  109. return valueSlider.GetPos();
  110. }
  111. int ParamControlSet::GetParamValueFromEdit() const
  112. {
  113. TCHAR s[16];
  114. valueEdit.GetWindowText(s, 16);
  115. int val = _tstoi(s);
  116. Limit(val, int(0), int(PARAM_RESOLUTION));
  117. return val;
  118. }
  119. BEGIN_MESSAGE_MAP(CDefaultVstEditor, CAbstractVstEditor)
  120. //{{AFX_MSG_MAP(CDefaultVstEditor)
  121. ON_CONTROL_RANGE(EN_CHANGE, ID_PLUGINEDITOR_EDIT_BASE, ID_PLUGINEDITOR_EDIT_BASE + NUM_PLUGINEDITOR_PARAMETERS - 1, &CDefaultVstEditor::OnParamTextboxChanged)
  122. //}}AFX_MSG_MAP
  123. ON_WM_HSCROLL()
  124. ON_WM_VSCROLL()
  125. ON_WM_MOUSEWHEEL()
  126. END_MESSAGE_MAP()
  127. void CDefaultVstEditor::DoDataExchange(CDataExchange* pDX)
  128. {
  129. CDialog::DoDataExchange(pDX);
  130. //{{AFX_DATA_MAP(CDefaultVstEditor)
  131. DDX_Control(pDX, IDC_SCROLLBAR1, paramScroller);
  132. //}}AFX_DATA_MAP
  133. }
  134. CDefaultVstEditor::CDefaultVstEditor(IMixPlugin &plugin) : CAbstractVstEditor(plugin)
  135. {
  136. m_nControlLock = 0;
  137. paramOffset = 0;
  138. controls.clear();
  139. }
  140. CDefaultVstEditor::~CDefaultVstEditor()
  141. {
  142. for(size_t i = 0; i < controls.size(); i++)
  143. {
  144. delete controls[i];
  145. }
  146. }
  147. void CDefaultVstEditor::CreateControls()
  148. {
  149. // Already initialized.
  150. if(!controls.empty())
  151. {
  152. return;
  153. }
  154. Measurements m(m_hWnd);
  155. CRect window;
  156. GetWindowRect(&window);
  157. CRect rect;
  158. GetClientRect(&rect);
  159. int origHeight = rect.bottom;
  160. // Get parameter scroll bar dimensions and move it to the right side of the window
  161. CRect scrollRect;
  162. paramScroller.GetClientRect(&scrollRect);
  163. scrollRect.bottom = rect.bottom;
  164. scrollRect.MoveToX(rect.right - scrollRect.right);
  165. // Ignore this space in our calculation from now on.
  166. rect.right -= scrollRect.Width();
  167. controls.clear();
  168. // Create a bit of border space
  169. rect.DeflateRect(m.spacing, m.spacing);
  170. rect.bottom = m.totalHeight;
  171. for(int i = 0; i < NUM_PLUGINEDITOR_PARAMETERS; i++)
  172. {
  173. try
  174. {
  175. controls.push_back(new ParamControlSet(this, rect, i, m));
  176. rect.OffsetRect(0, m.totalHeight + m.spacing);
  177. } catch(mpt::out_of_memory e)
  178. {
  179. mpt::delete_out_of_memory(e);
  180. }
  181. }
  182. // Calculate new ideal window height.
  183. const int heightChange = (rect.bottom - m.totalHeight + m.spacing) - origHeight;
  184. // Update parameter scroll bar height
  185. scrollRect.bottom += heightChange;
  186. paramScroller.MoveWindow(scrollRect, FALSE);
  187. // Resize window height
  188. window.bottom += heightChange;
  189. MoveWindow(&window);
  190. // Set scrollbar page size.
  191. SCROLLINFO sbInfo;
  192. paramScroller.GetScrollInfo(&sbInfo);
  193. sbInfo.nPage = NUM_PLUGINEDITOR_PARAMETERS;
  194. paramScroller.SetScrollInfo(&sbInfo);
  195. UpdateControls(true);
  196. }
  197. void CDefaultVstEditor::UpdateControls(bool updateParamNames)
  198. {
  199. const PlugParamIndex numParams = m_VstPlugin.GetNumParameters();
  200. const PlugParamIndex scrollMax = numParams - std::min(numParams, static_cast<PlugParamIndex>(NUM_PLUGINEDITOR_PARAMETERS));
  201. LimitMax(paramOffset, scrollMax);
  202. int curScrollMin, curScrollMax;
  203. paramScroller.GetScrollRange(&curScrollMin, &curScrollMax);
  204. if(static_cast<PlugParamIndex>(curScrollMax) != scrollMax)
  205. {
  206. // Number of parameters changed - update scrollbar limits
  207. paramScroller.SetScrollRange(0, scrollMax);
  208. paramScroller.SetScrollPos(paramOffset);
  209. paramScroller.EnableWindow(scrollMax > 0 ? TRUE : FALSE);
  210. updateParamNames = true;
  211. }
  212. m_VstPlugin.CacheParameterNames(paramOffset, std::min(paramOffset + NUM_PLUGINEDITOR_PARAMETERS, numParams));
  213. for(PlugParamIndex i = 0; i < NUM_PLUGINEDITOR_PARAMETERS; i++)
  214. {
  215. const PlugParamIndex param = paramOffset + i;
  216. if(param >= numParams)
  217. {
  218. // This param doesn't exist.
  219. controls[i]->EnableControls(false);
  220. controls[i]->ResetContent();
  221. continue;
  222. }
  223. controls[i]->EnableControls();
  224. if(updateParamNames)
  225. {
  226. // Update param name
  227. controls[i]->SetParamName(m_VstPlugin.GetFormattedParamName(param));
  228. }
  229. UpdateParamDisplay(param);
  230. }
  231. }
  232. void CDefaultVstEditor::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  233. {
  234. CSliderCtrl* pScrolledSlider = reinterpret_cast<CSliderCtrl*>(pScrollBar);
  235. // Check if any of the value sliders were affected.
  236. for(size_t i = 0; i < controls.size(); i++)
  237. {
  238. if ((pScrolledSlider->GetDlgCtrlID() == controls[i]->GetSliderID()) && (nSBCode != SB_ENDSCROLL))
  239. {
  240. OnParamSliderChanged(controls[i]->GetSliderID());
  241. break;
  242. }
  243. }
  244. CAbstractVstEditor::OnHScroll(nSBCode, nPos, pScrollBar);
  245. }
  246. void CDefaultVstEditor::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  247. {
  248. if (pScrollBar == &paramScroller)
  249. {
  250. // Get the minimum and maximum scrollbar positions.
  251. int minpos;
  252. int maxpos;
  253. pScrollBar->GetScrollRange(&minpos, &maxpos);
  254. //maxpos = pScrollBar->GetScrollLimit();
  255. SCROLLINFO sbInfo;
  256. paramScroller.GetScrollInfo(&sbInfo);
  257. // Get the current position of scroll box.
  258. int curpos = pScrollBar->GetScrollPos();
  259. // Determine the new position of scroll box.
  260. switch(nSBCode)
  261. {
  262. case SB_LEFT: // Scroll to far left.
  263. curpos = minpos;
  264. break;
  265. case SB_RIGHT: // Scroll to far right.
  266. curpos = maxpos;
  267. break;
  268. case SB_ENDSCROLL: // End scroll.
  269. break;
  270. case SB_LINELEFT: // Scroll left.
  271. if(curpos > minpos)
  272. curpos--;
  273. break;
  274. case SB_LINERIGHT: // Scroll right.
  275. if(curpos < maxpos)
  276. curpos++;
  277. break;
  278. case SB_PAGELEFT: // Scroll one page left.
  279. if(curpos > minpos)
  280. {
  281. curpos = std::max(minpos, curpos - static_cast<int>(sbInfo.nPage));
  282. }
  283. break;
  284. case SB_PAGERIGHT: // Scroll one page right.
  285. if(curpos < maxpos)
  286. {
  287. curpos = std::min(maxpos, curpos + static_cast<int>(sbInfo.nPage));
  288. }
  289. break;
  290. case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position
  291. curpos = nPos; // of the scroll box at the end of the drag operation.
  292. break;
  293. case SB_THUMBTRACK: // Drag scroll box to specified position. nPos is the
  294. curpos = nPos; // position that the scroll box has been dragged to.
  295. break;
  296. }
  297. // Set the new position of the thumb (scroll box).
  298. pScrollBar->SetScrollPos(curpos);
  299. paramOffset = curpos;
  300. UpdateControls(true);
  301. }
  302. CAbstractVstEditor::OnVScroll(nSBCode, nPos, pScrollBar);
  303. }
  304. BOOL CDefaultVstEditor::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
  305. {
  306. MPT_UNREFERENCED_PARAMETER(nFlags);
  307. MPT_UNREFERENCED_PARAMETER(pt);
  308. // Mouse wheel - scroll parameter list
  309. int minpos, maxpos;
  310. paramScroller.GetScrollRange(&minpos, &maxpos);
  311. if(minpos != maxpos)
  312. {
  313. paramOffset -= mpt::signum(zDelta);
  314. Limit(paramOffset, PlugParamIndex(minpos), PlugParamIndex(maxpos));
  315. paramScroller.SetScrollPos(paramOffset);
  316. UpdateControls(true);
  317. }
  318. return CAbstractVstEditor::OnMouseWheel(nFlags, zDelta, pt);
  319. }
  320. bool CDefaultVstEditor::OpenEditor(CWnd *parent)
  321. {
  322. Create(IDD_DEFAULTPLUGINEDITOR, parent);
  323. CreateControls();
  324. return CAbstractVstEditor::OpenEditor(parent);
  325. }
  326. // Called when a change occurs to the parameter textbox
  327. // If the change is triggered by the user, we'll need to notify the plugin and update
  328. // the other GUI controls
  329. void CDefaultVstEditor::OnParamTextboxChanged(UINT id)
  330. {
  331. if (m_nControlLock)
  332. {
  333. // Lock will be set if the GUI change was triggered internally (in UpdateParamDisplays).
  334. // We're only interested in handling changes triggered by the user.
  335. return;
  336. }
  337. const PlugParamIndex param = paramOffset + id - ID_PLUGINEDITOR_EDIT_BASE;
  338. // Extract value and update
  339. SetParam(param, controls[param - paramOffset]->GetParamValueFromEdit());
  340. }
  341. // Called when a change occurs to the parameter slider
  342. // If the change is triggered by the user, we'll need to notify the plugin and update
  343. // the other GUI controls
  344. void CDefaultVstEditor::OnParamSliderChanged(UINT id)
  345. {
  346. if (m_nControlLock)
  347. {
  348. // Lock will be set if the GUI change was triggered internally (in UpdateParamDisplays).
  349. // We're only interested in handling changes triggered by the user.
  350. return;
  351. }
  352. const PlugParamIndex param = paramOffset + id - ID_PLUGINEDITOR_SLIDERS_BASE;
  353. // Extract value and update
  354. SetParam(param, controls[param - paramOffset]->GetParamValueFromSlider());
  355. }
  356. // Update a given parameter to a given value and notify plugin
  357. void CDefaultVstEditor::SetParam(PlugParamIndex param, int value)
  358. {
  359. if(param >= m_VstPlugin.GetNumParameters())
  360. {
  361. return;
  362. }
  363. m_VstPlugin.SetScaledUIParam(param, static_cast<PlugParamValue>(value) / static_cast<PlugParamValue>(PARAM_RESOLUTION));
  364. // Update other GUI controls
  365. UpdateParamDisplay(param);
  366. // Act as if an automation message has been sent by the plugin (record param changes, set document modified, etc...)
  367. m_VstPlugin.AutomateParameter(param);
  368. }
  369. //Update all GUI controls with the new param value
  370. void CDefaultVstEditor::UpdateParamDisplay(PlugParamIndex param)
  371. {
  372. if(m_nControlLock || param < paramOffset || param >= paramOffset + NUM_PLUGINEDITOR_PARAMETERS)
  373. {
  374. //Just to make sure we're not here as a consequence of an internal GUI change, and avoid modifying a parameter that doesn't exist on the GUI.
  375. return;
  376. }
  377. // Get the actual parameter value from the plugin
  378. const int val = static_cast<int>(m_VstPlugin.GetScaledUIParam(param) * static_cast<float>(PARAM_RESOLUTION) + 0.5f);
  379. // Update the GUI controls
  380. // Set lock to indicate that the changes to the GUI are internal - no need to notify the plug and re-update GUI.
  381. m_nControlLock++;
  382. controls[param - paramOffset]->SetParamValue(val, m_VstPlugin.GetFormattedParamValue(param));
  383. // Unset lock - done with internal GUI updates.
  384. m_nControlLock--;
  385. }
  386. #endif // NO_PLUGINS
  387. OPENMPT_NAMESPACE_END