Ctrl_ins.cpp 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213
  1. /*
  2. * Ctrl_ins.cpp
  3. * ------------
  4. * Purpose: Instrument tab, upper panel.
  5. * Notes : (currently none)
  6. * Authors: Olivier Lapicque
  7. * OpenMPT Devs
  8. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  9. */
  10. #include "stdafx.h"
  11. #include "Mptrack.h"
  12. #include "Mainfrm.h"
  13. #include "InputHandler.h"
  14. #include "Childfrm.h"
  15. #include "ImageLists.h"
  16. #include "Moddoc.h"
  17. #include "../soundlib/mod_specifications.h"
  18. #include "../soundlib/plugins/PlugInterface.h"
  19. #include "Globals.h"
  20. #include "Ctrl_ins.h"
  21. #include "View_ins.h"
  22. #include "dlg_misc.h"
  23. #include "TuningDialog.h"
  24. #include "../common/misc_util.h"
  25. #include "../common/mptStringBuffer.h"
  26. #include "SelectPluginDialog.h"
  27. #include "../common/mptFileIO.h"
  28. #include "../common/FileReader.h"
  29. #include "FileDialog.h"
  30. OPENMPT_NAMESPACE_BEGIN
  31. /////////////////////////////////////////////////////////////////////////
  32. // CNoteMapWnd
  33. BEGIN_MESSAGE_MAP(CNoteMapWnd, CStatic)
  34. ON_WM_ERASEBKGND()
  35. ON_WM_PAINT()
  36. ON_WM_SETFOCUS()
  37. ON_WM_KILLFOCUS()
  38. ON_WM_LBUTTONDOWN()
  39. ON_WM_MBUTTONDOWN()
  40. ON_WM_RBUTTONDOWN()
  41. ON_WM_LBUTTONDBLCLK()
  42. ON_WM_MOUSEWHEEL()
  43. ON_COMMAND(ID_NOTEMAP_TRANS_UP, &CNoteMapWnd::OnMapTransposeUp)
  44. ON_COMMAND(ID_NOTEMAP_TRANS_DOWN, &CNoteMapWnd::OnMapTransposeDown)
  45. ON_COMMAND(ID_NOTEMAP_COPY_NOTE, &CNoteMapWnd::OnMapCopyNote)
  46. ON_COMMAND(ID_NOTEMAP_COPY_SMP, &CNoteMapWnd::OnMapCopySample)
  47. ON_COMMAND(ID_NOTEMAP_RESET, &CNoteMapWnd::OnMapReset)
  48. ON_COMMAND(ID_NOTEMAP_TRANSPOSE_SAMPLES, &CNoteMapWnd::OnTransposeSamples)
  49. ON_COMMAND(ID_NOTEMAP_REMOVE, &CNoteMapWnd::OnMapRemove)
  50. ON_COMMAND(ID_INSTRUMENT_SAMPLEMAP, &CNoteMapWnd::OnEditSampleMap)
  51. ON_COMMAND(ID_INSTRUMENT_DUPLICATE, &CNoteMapWnd::OnInstrumentDuplicate)
  52. ON_MESSAGE(WM_MOD_KEYCOMMAND, &CNoteMapWnd::OnCustomKeyMsg)
  53. ON_COMMAND_RANGE(ID_NOTEMAP_EDITSAMPLE, ID_NOTEMAP_EDITSAMPLE + MAX_SAMPLES, &CNoteMapWnd::OnEditSample)
  54. END_MESSAGE_MAP()
  55. BOOL CNoteMapWnd::PreTranslateMessage(MSG* pMsg)
  56. {
  57. if(!pMsg)
  58. return TRUE;
  59. uint32 wParam = static_cast<uint32>(pMsg->wParam);
  60. {
  61. //We handle keypresses before Windows has a chance to handle them (for alt etc..)
  62. if ((pMsg->message == WM_SYSKEYUP) || (pMsg->message == WM_KEYUP) ||
  63. (pMsg->message == WM_SYSKEYDOWN) || (pMsg->message == WM_KEYDOWN))
  64. {
  65. CInputHandler* ih = CMainFrame::GetInputHandler();
  66. //Translate message manually
  67. UINT nChar = wParam;
  68. UINT nRepCnt = LOWORD(pMsg->lParam);
  69. UINT nFlags = HIWORD(pMsg->lParam);
  70. KeyEventType kT = ih->GetKeyEventType(nFlags);
  71. InputTargetContext ctx = (InputTargetContext)(kCtxInsNoteMap);
  72. if (ih->KeyEvent(ctx, nChar, nRepCnt, nFlags, kT) != kcNull)
  73. return true; // Mapped to a command, no need to pass message on.
  74. // a bit of a hack...
  75. ctx = (InputTargetContext)(kCtxCtrlInstruments);
  76. if (ih->KeyEvent(ctx, nChar, nRepCnt, nFlags, kT) != kcNull)
  77. return true; // Mapped to a command, no need to pass message on.
  78. }
  79. }
  80. //The key was not handled by a command, but it might still be useful
  81. if (pMsg->message == WM_CHAR) //key is a character
  82. {
  83. UINT nFlags = HIWORD(pMsg->lParam);
  84. KeyEventType kT = CMainFrame::GetInputHandler()->GetKeyEventType(nFlags);
  85. if (kT == kKeyEventDown)
  86. if (HandleChar(wParam))
  87. return true;
  88. }
  89. else if (pMsg->message == WM_KEYDOWN) //key is not a character
  90. {
  91. if(HandleNav(wParam))
  92. return true;
  93. // Handle Application (menu) key
  94. if(wParam == VK_APPS)
  95. {
  96. CRect clientRect;
  97. GetClientRect(clientRect);
  98. clientRect.bottom = clientRect.top + mpt::align_up(clientRect.Height(), m_cyFont);
  99. OnRButtonDown(0, clientRect.CenterPoint());
  100. }
  101. }
  102. else if (pMsg->message == WM_KEYUP) //stop notes on key release
  103. {
  104. if (((pMsg->wParam >= '0') && (pMsg->wParam <= '9')) || (pMsg->wParam == ' ') ||
  105. ((pMsg->wParam >= VK_NUMPAD0) && (pMsg->wParam <= VK_NUMPAD9)))
  106. {
  107. StopNote();
  108. return true;
  109. }
  110. }
  111. return CStatic::PreTranslateMessage(pMsg);
  112. }
  113. void CNoteMapWnd::PrepareUndo(const char *description)
  114. {
  115. m_modDoc.GetInstrumentUndo().PrepareUndo(m_nInstrument, description);
  116. }
  117. void CNoteMapWnd::SetCurrentInstrument(INSTRUMENTINDEX nIns)
  118. {
  119. if (nIns != m_nInstrument)
  120. {
  121. if (nIns < MAX_INSTRUMENTS) m_nInstrument = nIns;
  122. // create missing instrument if needed
  123. CSoundFile &sndFile = m_modDoc.GetSoundFile();
  124. if(m_nInstrument > 0 && m_nInstrument <= sndFile.GetNumInstruments() && sndFile.Instruments[m_nInstrument] == nullptr)
  125. {
  126. ModInstrument *instrument = sndFile.AllocateInstrument(m_nInstrument);
  127. if(instrument == nullptr)
  128. return;
  129. m_modDoc.InitializeInstrument(instrument);
  130. }
  131. Invalidate(FALSE);
  132. UpdateAccessibleTitle();
  133. }
  134. }
  135. void CNoteMapWnd::SetCurrentNote(UINT nNote)
  136. {
  137. if(nNote != m_nNote && ModCommand::IsNote(static_cast<ModCommand::NOTE>(nNote + NOTE_MIN)))
  138. {
  139. m_nNote = nNote;
  140. Invalidate(FALSE);
  141. UpdateAccessibleTitle();
  142. }
  143. }
  144. void CNoteMapWnd::OnPaint()
  145. {
  146. CPaintDC dc(this);
  147. CRect rcClient;
  148. GetClientRect(&rcClient);
  149. const auto highlightBrush = GetSysColorBrush(COLOR_HIGHLIGHT), windowBrush = GetSysColorBrush(COLOR_WINDOW);
  150. const auto colorText = GetSysColor(COLOR_WINDOWTEXT);
  151. const auto colorTextSel = GetSysColor(COLOR_HIGHLIGHTTEXT);
  152. auto oldFont = dc.SelectObject(CMainFrame::GetGUIFont());
  153. dc.SetBkMode(TRANSPARENT);
  154. if ((m_cxFont <= 0) || (m_cyFont <= 0))
  155. {
  156. CSize sz;
  157. sz = dc.GetTextExtent(_T("C#0."), 4);
  158. m_cyFont = sz.cy + 2;
  159. m_cxFont = rcClient.right / 3;
  160. }
  161. dc.IntersectClipRect(&rcClient);
  162. const CSoundFile &sndFile = m_modDoc.GetSoundFile();
  163. if (m_cxFont > 0 && m_cyFont > 0)
  164. {
  165. const bool focus = (::GetFocus() == m_hWnd);
  166. const ModInstrument *pIns = sndFile.Instruments[m_nInstrument];
  167. CRect rect;
  168. int nNotes = (rcClient.bottom + m_cyFont - 1) / m_cyFont;
  169. int nPos = m_nNote - (nNotes/2);
  170. int ypaint = 0;
  171. mpt::winstring s;
  172. for (int ynote=0; ynote<nNotes; ynote++, ypaint+=m_cyFont, nPos++)
  173. {
  174. // Note
  175. bool isValidPos = (nPos >= 0) && (nPos < NOTE_MAX - NOTE_MIN + 1);
  176. if (isValidPos)
  177. {
  178. s = mpt::ToWin(sndFile.GetNoteName(static_cast<ModCommand::NOTE>(nPos + 1), m_nInstrument));
  179. s.resize(4);
  180. } else
  181. {
  182. s.clear();
  183. }
  184. rect.SetRect(0, ypaint, m_cxFont, ypaint+m_cyFont);
  185. DrawButtonRect(dc, &rect, s.c_str(), FALSE, FALSE);
  186. // Mapped Note
  187. bool highlight = ((focus) && (nPos == (int)m_nNote));
  188. rect.left = rect.right;
  189. rect.right = m_cxFont*2-1;
  190. s = _T("...");
  191. if(pIns != nullptr && isValidPos && (pIns->NoteMap[nPos] != NOTE_NONE))
  192. {
  193. ModCommand::NOTE n = pIns->NoteMap[nPos];
  194. if(ModCommand::IsNote(n))
  195. {
  196. s = mpt::ToWin(sndFile.GetNoteName(n, m_nInstrument));
  197. s.resize(4);
  198. } else
  199. {
  200. s = _T("???");
  201. }
  202. }
  203. FillRect(dc, &rect, highlight ? highlightBrush : windowBrush);
  204. if(nPos == (int)m_nNote && !m_bIns)
  205. {
  206. rect.InflateRect(-1, -1);
  207. dc.DrawFocusRect(&rect);
  208. rect.InflateRect(1, 1);
  209. }
  210. dc.SetTextColor(highlight ? colorTextSel : colorText);
  211. dc.DrawText(s.c_str(), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER | DT_NOPREFIX);
  212. // Sample
  213. highlight = (focus && nPos == (int)m_nNote);
  214. rect.left = rcClient.left + m_cxFont * 2 + 3;
  215. rect.right = rcClient.right;
  216. s = _T(" ..");
  217. if(pIns && nPos >= 0 && nPos < NOTE_MAX && pIns->Keyboard[nPos])
  218. {
  219. s = mpt::tfmt::right(3, mpt::tfmt::dec(pIns->Keyboard[nPos]));
  220. }
  221. FillRect(dc, &rect, highlight ? highlightBrush : windowBrush);
  222. if((nPos == (int)m_nNote) && (m_bIns))
  223. {
  224. rect.InflateRect(-1, -1);
  225. dc.DrawFocusRect(&rect);
  226. rect.InflateRect(1, 1);
  227. }
  228. dc.SetTextColor((highlight) ? colorTextSel : colorText);
  229. dc.DrawText(s.c_str(), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER | DT_NOPREFIX);
  230. }
  231. rect.SetRect(rcClient.left + m_cxFont * 2 - 1, rcClient.top, rcClient.left + m_cxFont * 2 + 3, ypaint);
  232. DrawButtonRect(dc, &rect, _T(""), FALSE, FALSE);
  233. if (ypaint < rcClient.bottom)
  234. {
  235. rect.SetRect(rcClient.left, ypaint, rcClient.right, rcClient.bottom);
  236. FillRect(dc, &rect, GetSysColorBrush(COLOR_BTNFACE));
  237. }
  238. }
  239. dc.SelectObject(oldFont);
  240. }
  241. void CNoteMapWnd::OnSetFocus(CWnd *pOldWnd)
  242. {
  243. CStatic::OnSetFocus(pOldWnd);
  244. Invalidate(FALSE);
  245. CMainFrame::GetMainFrame()->m_pNoteMapHasFocus = this;
  246. m_undo = true;
  247. }
  248. void CNoteMapWnd::OnKillFocus(CWnd *pNewWnd)
  249. {
  250. CStatic::OnKillFocus(pNewWnd);
  251. Invalidate(FALSE);
  252. CMainFrame::GetMainFrame()->m_pNoteMapHasFocus = nullptr;
  253. }
  254. void CNoteMapWnd::OnLButtonDown(UINT, CPoint pt)
  255. {
  256. if ((pt.x >= m_cxFont) && (pt.x < m_cxFont*2) && (m_bIns))
  257. {
  258. m_bIns = false;
  259. Invalidate(FALSE);
  260. }
  261. if ((pt.x > m_cxFont*2) && (pt.x <= m_cxFont*3) && (!m_bIns))
  262. {
  263. m_bIns = true;
  264. Invalidate(FALSE);
  265. }
  266. if ((pt.x >= 0) && (m_cyFont))
  267. {
  268. CRect rcClient;
  269. GetClientRect(&rcClient);
  270. int nNotes = (rcClient.bottom + m_cyFont - 1) / m_cyFont;
  271. int n = (pt.y / m_cyFont) + m_nNote - (nNotes/2);
  272. if(n >= 0)
  273. {
  274. SetCurrentNote(n);
  275. }
  276. }
  277. SetFocus();
  278. }
  279. void CNoteMapWnd::OnLButtonDblClk(UINT, CPoint)
  280. {
  281. // Double-click edits sample map
  282. OnEditSampleMap();
  283. }
  284. void CNoteMapWnd::OnRButtonDown(UINT, CPoint pt)
  285. {
  286. CInputHandler* ih = CMainFrame::GetInputHandler();
  287. CSoundFile &sndFile = m_modDoc.GetSoundFile();
  288. ModInstrument *pIns = sndFile.Instruments[m_nInstrument];
  289. if (pIns)
  290. {
  291. HMENU hMenu = ::CreatePopupMenu();
  292. HMENU hSubMenu = ::CreatePopupMenu();
  293. if (hMenu)
  294. {
  295. AppendMenu(hMenu, MF_STRING, ID_INSTRUMENT_SAMPLEMAP, ih->GetKeyTextFromCommand(kcInsNoteMapEditSampleMap, _T("Edit Sample &Map")));
  296. if (hSubMenu)
  297. {
  298. // Create sub menu with a list of all samples that are referenced by this instrument.
  299. for(auto sample : pIns->GetSamples())
  300. {
  301. if(sample <= sndFile.GetNumSamples())
  302. {
  303. AppendMenu(hSubMenu, MF_STRING, ID_NOTEMAP_EDITSAMPLE + sample, MPT_CFORMAT("{}: {}")(sample, mpt::ToCString(sndFile.GetCharsetInternal(), sndFile.m_szNames[sample])));
  304. }
  305. }
  306. AppendMenu(hMenu, MF_POPUP, reinterpret_cast<UINT_PTR>(hSubMenu), ih->GetKeyTextFromCommand(kcInsNoteMapEditSample, _T("&Edit Sample")));
  307. AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
  308. }
  309. AppendMenu(hMenu, MF_STRING, ID_NOTEMAP_COPY_SMP, ih->GetKeyTextFromCommand(kcInsNoteMapCopyCurrentSample, MPT_CFORMAT("Map All Notes to &Sample {}")(pIns->Keyboard[m_nNote])));
  310. if(sndFile.GetType() != MOD_TYPE_XM)
  311. {
  312. if(ModCommand::IsNote(pIns->NoteMap[m_nNote]))
  313. {
  314. AppendMenu(hMenu, MF_STRING, ID_NOTEMAP_COPY_NOTE, ih->GetKeyTextFromCommand(kcInsNoteMapCopyCurrentNote, MPT_CFORMAT("Map All &Notes to {}")(mpt::ToCString(sndFile.GetNoteName(pIns->NoteMap[m_nNote], m_nInstrument)))));
  315. }
  316. AppendMenu(hMenu, MF_STRING, ID_NOTEMAP_TRANS_UP, ih->GetKeyTextFromCommand(kcInsNoteMapTransposeUp, _T("Transpose Map &Up")));
  317. AppendMenu(hMenu, MF_STRING, ID_NOTEMAP_TRANS_DOWN, ih->GetKeyTextFromCommand(kcInsNoteMapTransposeDown, _T("Transpose Map &Down")));
  318. }
  319. AppendMenu(hMenu, MF_STRING, ID_NOTEMAP_RESET, ih->GetKeyTextFromCommand(kcInsNoteMapReset, _T("&Reset Note Mapping")));
  320. AppendMenu(hMenu, MF_STRING | (pIns->CanConvertToDefaultNoteMap().empty() ? MF_GRAYED : 0), ID_NOTEMAP_TRANSPOSE_SAMPLES, ih->GetKeyTextFromCommand(kcInsNoteMapTransposeSamples, _T("&Transpose Samples / Reset Map")));
  321. AppendMenu(hMenu, MF_STRING, ID_NOTEMAP_REMOVE, ih->GetKeyTextFromCommand(kcInsNoteMapRemove, _T("Remo&ve All Samples")));
  322. AppendMenu(hMenu, MF_STRING, ID_INSTRUMENT_DUPLICATE, ih->GetKeyTextFromCommand(kcInstrumentCtrlDuplicate, _T("Duplicate &Instrument")));
  323. SetMenuDefaultItem(hMenu, ID_INSTRUMENT_SAMPLEMAP, FALSE);
  324. ClientToScreen(&pt);
  325. ::TrackPopupMenu(hMenu, TPM_LEFTALIGN|TPM_RIGHTBUTTON, pt.x, pt.y, 0, m_hWnd, NULL);
  326. ::DestroyMenu(hMenu);
  327. if (hSubMenu) ::DestroyMenu(hSubMenu);
  328. }
  329. }
  330. }
  331. BOOL CNoteMapWnd::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
  332. {
  333. SetCurrentNote(m_nNote - mpt::signum(zDelta));
  334. return CStatic::OnMouseWheel(nFlags, zDelta, pt);
  335. }
  336. void CNoteMapWnd::OnMapCopyNote()
  337. {
  338. ModInstrument *pIns = m_modDoc.GetSoundFile().Instruments[m_nInstrument];
  339. if (pIns)
  340. {
  341. m_undo = true;
  342. bool bModified = false;
  343. auto n = pIns->NoteMap[m_nNote];
  344. for (auto &key : pIns->NoteMap) if (key != n)
  345. {
  346. if(!bModified)
  347. {
  348. PrepareUndo("Map Notes");
  349. }
  350. key = n;
  351. bModified = true;
  352. }
  353. if (bModified)
  354. {
  355. m_pParent.SetModified(InstrumentHint().Info(), false);
  356. Invalidate(FALSE);
  357. }
  358. }
  359. }
  360. void CNoteMapWnd::OnMapCopySample()
  361. {
  362. ModInstrument *pIns = m_modDoc.GetSoundFile().Instruments[m_nInstrument];
  363. if (pIns)
  364. {
  365. m_undo = true;
  366. bool bModified = false;
  367. auto n = pIns->Keyboard[m_nNote];
  368. for (auto &sample : pIns->Keyboard) if (sample != n)
  369. {
  370. if(!bModified)
  371. {
  372. PrepareUndo("Map Samples");
  373. }
  374. sample = n;
  375. bModified = true;
  376. }
  377. if (bModified)
  378. {
  379. m_pParent.SetModified(InstrumentHint().Info(), false);
  380. Invalidate(FALSE);
  381. UpdateAccessibleTitle();
  382. }
  383. }
  384. }
  385. void CNoteMapWnd::OnMapReset()
  386. {
  387. ModInstrument *pIns = m_modDoc.GetSoundFile().Instruments[m_nInstrument];
  388. if (pIns)
  389. {
  390. m_undo = true;
  391. bool modified = false;
  392. for (size_t i = 0; i < std::size(pIns->NoteMap); i++) if (pIns->NoteMap[i] != i + 1)
  393. {
  394. if(!modified)
  395. {
  396. PrepareUndo("Reset Note Map");
  397. }
  398. pIns->NoteMap[i] = static_cast<ModCommand::NOTE>(i + 1);
  399. modified = true;
  400. }
  401. if(modified)
  402. {
  403. m_pParent.SetModified(InstrumentHint().Info(), false);
  404. Invalidate(FALSE);
  405. UpdateAccessibleTitle();
  406. }
  407. }
  408. }
  409. void CNoteMapWnd::OnTransposeSamples()
  410. {
  411. auto &sndFile = m_modDoc.GetSoundFile();
  412. ModInstrument *pIns = sndFile.Instruments[m_nInstrument];
  413. if(!pIns)
  414. return;
  415. const auto samples = pIns->CanConvertToDefaultNoteMap();
  416. if(samples.empty())
  417. return;
  418. PrepareUndo("Transpose Samples");
  419. for(const auto &[smp, transpose] : samples)
  420. {
  421. if(smp > sndFile.GetNumSamples())
  422. continue;
  423. m_modDoc.GetSampleUndo().PrepareUndo(smp, sundo_none, "Transpose");
  424. auto &sample = sndFile.GetSample(smp);
  425. if(sndFile.UseFinetuneAndTranspose())
  426. sample.RelativeTone += transpose;
  427. else
  428. sample.Transpose(transpose / 12.0);
  429. m_modDoc.UpdateAllViews(nullptr, SampleHint(smp).Info(), &m_pParent);
  430. }
  431. pIns->ResetNoteMap();
  432. m_pParent.SetModified(InstrumentHint().Info(), false);
  433. Invalidate(FALSE);
  434. }
  435. void CNoteMapWnd::OnMapRemove()
  436. {
  437. ModInstrument *pIns = m_modDoc.GetSoundFile().Instruments[m_nInstrument];
  438. if (pIns)
  439. {
  440. m_undo = true;
  441. bool modified = false;
  442. for (auto &sample: pIns->Keyboard) if (sample != 0)
  443. {
  444. if(!modified)
  445. {
  446. PrepareUndo("Remove Sample Assocations");
  447. }
  448. sample = 0;
  449. modified = true;
  450. }
  451. if(modified)
  452. {
  453. m_pParent.SetModified(InstrumentHint().Info(), false);
  454. Invalidate(FALSE);
  455. UpdateAccessibleTitle();
  456. }
  457. }
  458. }
  459. void CNoteMapWnd::OnMapTransposeUp()
  460. {
  461. MapTranspose(1);
  462. }
  463. void CNoteMapWnd::OnMapTransposeDown()
  464. {
  465. MapTranspose(-1);
  466. }
  467. void CNoteMapWnd::MapTranspose(int nAmount)
  468. {
  469. if(nAmount == 0 || m_modDoc.GetModType() == MOD_TYPE_XM) return;
  470. ModInstrument *pIns = m_modDoc.GetSoundFile().Instruments[m_nInstrument];
  471. if((nAmount == 12 || nAmount == -12))
  472. {
  473. // Special case for instrument-specific tunings
  474. nAmount = m_modDoc.GetInstrumentGroupSize(m_nInstrument) * mpt::signum(nAmount);
  475. }
  476. m_undo = true;
  477. if (pIns)
  478. {
  479. bool modified = false;
  480. for(NOTEINDEXTYPE i = 0; i < NOTE_MAX; i++)
  481. {
  482. int n = pIns->NoteMap[i];
  483. if ((n > NOTE_MIN && nAmount < 0) || (n < NOTE_MAX && nAmount > 0))
  484. {
  485. n = Clamp(n + nAmount, NOTE_MIN, NOTE_MAX);
  486. if(n != pIns->NoteMap[i])
  487. {
  488. if(!modified)
  489. {
  490. PrepareUndo("Transpose Map");
  491. }
  492. pIns->NoteMap[i] = static_cast<uint8>(n);
  493. modified = true;
  494. }
  495. }
  496. }
  497. if(modified)
  498. {
  499. m_pParent.SetModified(InstrumentHint().Info(), false);
  500. Invalidate(FALSE);
  501. UpdateAccessibleTitle();
  502. }
  503. }
  504. }
  505. void CNoteMapWnd::OnEditSample(UINT nID)
  506. {
  507. UINT nSample = nID - ID_NOTEMAP_EDITSAMPLE;
  508. m_pParent.EditSample(nSample);
  509. }
  510. void CNoteMapWnd::OnEditSampleMap()
  511. {
  512. m_undo = true;
  513. m_pParent.PostMessage(WM_COMMAND, ID_INSTRUMENT_SAMPLEMAP);
  514. }
  515. void CNoteMapWnd::OnInstrumentDuplicate()
  516. {
  517. m_undo = true;
  518. m_pParent.PostMessage(WM_COMMAND, ID_INSTRUMENT_DUPLICATE);
  519. }
  520. LRESULT CNoteMapWnd::OnCustomKeyMsg(WPARAM wParam, LPARAM lParam)
  521. {
  522. ModInstrument *pIns = m_modDoc.GetSoundFile().Instruments[m_nInstrument];
  523. // Handle notes
  524. if (wParam >= kcInsNoteMapStartNotes && wParam <= kcInsNoteMapEndNotes)
  525. {
  526. // Special case: number keys override notes if we're in the sample # column.
  527. const auto key = KeyCombination::FromLPARAM(lParam).KeyCode();
  528. if(m_bIns && ((key >= '0' && key <= '9') || (key == ' ')))
  529. HandleChar(key);
  530. else
  531. EnterNote(m_modDoc.GetNoteWithBaseOctave(static_cast<int>(wParam - kcInsNoteMapStartNotes), m_nInstrument));
  532. return wParam;
  533. }
  534. if (wParam >= kcInsNoteMapStartNoteStops && wParam <= kcInsNoteMapEndNoteStops)
  535. {
  536. StopNote();
  537. return wParam;
  538. }
  539. // Other shortcuts
  540. switch(wParam)
  541. {
  542. case kcInsNoteMapTransposeDown: MapTranspose(-1); return wParam;
  543. case kcInsNoteMapTransposeUp: MapTranspose(1); return wParam;
  544. case kcInsNoteMapTransposeOctDown: MapTranspose(-12); return wParam;
  545. case kcInsNoteMapTransposeOctUp: MapTranspose(12); return wParam;
  546. case kcInsNoteMapCopyCurrentSample: OnMapCopySample(); return wParam;
  547. case kcInsNoteMapCopyCurrentNote: OnMapCopyNote(); return wParam;
  548. case kcInsNoteMapReset: OnMapReset(); return wParam;
  549. case kcInsNoteMapTransposeSamples: OnTransposeSamples(); return wParam;
  550. case kcInsNoteMapRemove: OnMapRemove(); return wParam;
  551. case kcInsNoteMapEditSample: if(pIns) OnEditSample(pIns->Keyboard[m_nNote] + ID_NOTEMAP_EDITSAMPLE); return wParam;
  552. case kcInsNoteMapEditSampleMap: OnEditSampleMap(); return wParam;
  553. // Parent shortcuts (also displayed in context menu of this control)
  554. case kcInstrumentCtrlDuplicate: OnInstrumentDuplicate(); return wParam;
  555. case kcNextInstrument: m_pParent.PostMessage(WM_COMMAND, ID_NEXTINSTRUMENT); return wParam;
  556. case kcPrevInstrument: m_pParent.PostMessage(WM_COMMAND, ID_PREVINSTRUMENT); return wParam;
  557. }
  558. return kcNull;
  559. }
  560. void CNoteMapWnd::EnterNote(UINT note)
  561. {
  562. CSoundFile &sndFile = m_modDoc.GetSoundFile();
  563. ModInstrument *pIns = sndFile.Instruments[m_nInstrument];
  564. if ((pIns) && (m_nNote < NOTE_MAX))
  565. {
  566. if (!m_bIns && (sndFile.GetType() & (MOD_TYPE_IT|MOD_TYPE_MPT)))
  567. {
  568. UINT n = pIns->NoteMap[m_nNote];
  569. bool ok = false;
  570. if ((note >= sndFile.GetModSpecifications().noteMin) && (note <= sndFile.GetModSpecifications().noteMax))
  571. {
  572. n = note;
  573. ok = true;
  574. }
  575. if (n != pIns->NoteMap[m_nNote])
  576. {
  577. StopNote(); // Stop old note according to current instrument settings
  578. pIns->NoteMap[m_nNote] = static_cast<ModCommand::NOTE>(n);
  579. m_pParent.SetModified(InstrumentHint().Info(), false);
  580. Invalidate(FALSE);
  581. UpdateAccessibleTitle();
  582. }
  583. if(ok)
  584. {
  585. PlayNote(m_nNote);
  586. }
  587. }
  588. }
  589. }
  590. bool CNoteMapWnd::HandleChar(WPARAM c)
  591. {
  592. CSoundFile &sndFile = m_modDoc.GetSoundFile();
  593. ModInstrument *pIns = sndFile.Instruments[m_nInstrument];
  594. if ((pIns) && (m_nNote < NOTE_MAX))
  595. {
  596. if ((m_bIns) && (((c >= '0') && (c <= '9')) || (c == ' '))) //in sample # column
  597. {
  598. UINT n = m_nOldIns;
  599. if (c != ' ')
  600. {
  601. n = (10 * pIns->Keyboard[m_nNote] + (c - '0')) % 10000;
  602. if ((n >= MAX_SAMPLES) || ((sndFile.m_nSamples < 1000) && (n >= 1000))) n = (n % 1000);
  603. if ((n >= MAX_SAMPLES) || ((sndFile.m_nSamples < 100) && (n >= 100))) n = (n % 100); else
  604. if ((n > 31) && (sndFile.m_nSamples < 32) && (n % 10)) n = (n % 10);
  605. }
  606. if (n != pIns->Keyboard[m_nNote])
  607. {
  608. if(m_undo)
  609. {
  610. PrepareUndo("Enter Instrument");
  611. m_undo = false;
  612. }
  613. StopNote(); // Stop old note according to current instrument settings
  614. pIns->Keyboard[m_nNote] = static_cast<SAMPLEINDEX>(n);
  615. m_pParent.SetModified(InstrumentHint().Info(), false);
  616. Invalidate(FALSE);
  617. UpdateAccessibleTitle();
  618. PlayNote(m_nNote);
  619. }
  620. if (c == ' ')
  621. {
  622. SetCurrentNote(m_nNote + 1);
  623. PlayNote(m_nNote);
  624. }
  625. return true;
  626. }
  627. else if ((!m_bIns) && (sndFile.m_nType & (MOD_TYPE_IT | MOD_TYPE_MPT))) //in note column
  628. {
  629. uint32 n = pIns->NoteMap[m_nNote];
  630. if ((c >= '0') && (c <= '9'))
  631. {
  632. if (n)
  633. n = static_cast<uint32>(((n - 1) % 12) + (c - '0') * 12 + 1);
  634. else
  635. n = static_cast<uint32>((m_nNote % 12) + (c - '0') * 12 + 1);
  636. } else if (c == ' ')
  637. {
  638. n = (m_nOldNote) ? m_nOldNote : m_nNote+1;
  639. }
  640. if (n != pIns->NoteMap[m_nNote])
  641. {
  642. if(m_undo)
  643. {
  644. PrepareUndo("Enter Note");
  645. m_undo = false;
  646. }
  647. StopNote(); // Stop old note according to current instrument settings
  648. pIns->NoteMap[m_nNote] = static_cast<ModCommand::NOTE>(n);
  649. m_pParent.SetModified(InstrumentHint().Info(), false);
  650. Invalidate(FALSE);
  651. UpdateAccessibleTitle();
  652. }
  653. if(c == ' ')
  654. {
  655. SetCurrentNote(m_nNote + 1);
  656. }
  657. PlayNote(m_nNote);
  658. return true;
  659. }
  660. }
  661. return false;
  662. }
  663. bool CNoteMapWnd::HandleNav(WPARAM k)
  664. {
  665. bool redraw = false;
  666. //HACK: handle numpad (convert numpad number key to normal number key)
  667. if ((k >= VK_NUMPAD0) && (k <= VK_NUMPAD9)) return HandleChar(k-VK_NUMPAD0+'0');
  668. switch(k)
  669. {
  670. case VK_RIGHT:
  671. if (!m_bIns) { m_bIns = true; redraw = true; } else
  672. if (m_nNote < NOTE_MAX - NOTE_MIN) { m_nNote++; m_bIns = false; redraw = true; }
  673. break;
  674. case VK_LEFT:
  675. if (m_bIns) { m_bIns = false; redraw = true; } else
  676. if (m_nNote) { m_nNote--; m_bIns = true; redraw = true; }
  677. break;
  678. case VK_UP:
  679. if (m_nNote > 0) { m_nNote--; redraw = true; }
  680. break;
  681. case VK_DOWN:
  682. if (m_nNote < NOTE_MAX - 1) { m_nNote++; redraw = true; }
  683. break;
  684. case VK_PRIOR:
  685. if (m_nNote > 3) { m_nNote -= 3; redraw = true; } else
  686. if (m_nNote > 0) { m_nNote = 0; redraw = true; }
  687. break;
  688. case VK_NEXT:
  689. if (m_nNote+3 < NOTE_MAX) { m_nNote += 3; redraw = true; } else
  690. if (m_nNote < NOTE_MAX - NOTE_MIN) { m_nNote = NOTE_MAX - NOTE_MIN; redraw = true; }
  691. break;
  692. case VK_HOME:
  693. if(m_nNote > 0) { m_nNote = 0; redraw = true; }
  694. break;
  695. case VK_END:
  696. if(m_nNote < NOTE_MAX - NOTE_MIN) { m_nNote = NOTE_MAX - NOTE_MIN; redraw = true; }
  697. break;
  698. // case VK_TAB:
  699. // return true;
  700. case VK_RETURN:
  701. {
  702. ModInstrument *pIns = m_modDoc.GetSoundFile().Instruments[m_nInstrument];
  703. if(pIns)
  704. {
  705. if (m_bIns)
  706. m_nOldIns = pIns->Keyboard[m_nNote];
  707. else
  708. m_nOldNote = pIns->NoteMap[m_nNote];
  709. }
  710. }
  711. return true;
  712. default:
  713. return false;
  714. }
  715. if(redraw)
  716. {
  717. m_undo = true;
  718. Invalidate(FALSE);
  719. UpdateAccessibleTitle();
  720. }
  721. return true;
  722. }
  723. void CNoteMapWnd::PlayNote(UINT note)
  724. {
  725. if(m_nPlayingNote != NOTE_NONE)
  726. {
  727. // No polyphony in notemap window
  728. StopNote();
  729. }
  730. m_nPlayingNote = static_cast<ModCommand::NOTE>(note + NOTE_MIN);
  731. m_noteChannel = m_modDoc.PlayNote(PlayNoteParam(m_nPlayingNote).Instrument(m_nInstrument));
  732. }
  733. void CNoteMapWnd::StopNote()
  734. {
  735. if(!ModCommand::IsNote(m_nPlayingNote)) return;
  736. m_modDoc.NoteOff(m_nPlayingNote, true, m_nInstrument, m_noteChannel);
  737. m_nPlayingNote = NOTE_NONE;
  738. }
  739. void CNoteMapWnd::UpdateAccessibleTitle()
  740. {
  741. CMainFrame::GetMainFrame()->NotifyAccessibilityUpdate(*this);
  742. }
  743. // Accessible description for screen readers
  744. HRESULT CNoteMapWnd::get_accName(VARIANT varChild, BSTR *pszName)
  745. {
  746. const auto *ins = m_modDoc.GetSoundFile().Instruments[m_nInstrument];
  747. if(!ins || m_nNote >= std::size(ins->NoteMap))
  748. return CStatic::get_accName(varChild, pszName);
  749. const auto &sndFile = m_modDoc.GetSoundFile();
  750. CString str = mpt::ToCString(sndFile.GetNoteName(static_cast<ModCommand::NOTE>(m_nNote + NOTE_MIN), m_nInstrument)) + _T(": ");
  751. if(ins->Keyboard[m_nNote] == 0)
  752. {
  753. str += _T("no sample");
  754. } else
  755. {
  756. auto mappedNote = ins->NoteMap[m_nNote];
  757. str += MPT_CFORMAT("sample {} at {}")(ins->Keyboard[m_nNote], mpt::ToCString(sndFile.GetNoteName(mappedNote, m_nInstrument)));
  758. }
  759. *pszName = str.AllocSysString();
  760. return S_OK;
  761. }
  762. /////////////////////////////////////////////////////////////////////////
  763. // CCtrlInstruments
  764. #define MAX_ATTACK_LENGTH 2001
  765. #define MAX_ATTACK_VALUE (MAX_ATTACK_LENGTH - 1) // 16 bit unsigned max
  766. BEGIN_MESSAGE_MAP(CCtrlInstruments, CModControlDlg)
  767. //{{AFX_MSG_MAP(CCtrlInstruments)
  768. ON_WM_VSCROLL()
  769. ON_WM_HSCROLL()
  770. ON_WM_XBUTTONUP()
  771. ON_NOTIFY(TBN_DROPDOWN, IDC_TOOLBAR1, &CCtrlInstruments::OnTbnDropDownToolBar)
  772. ON_COMMAND(IDC_INSTRUMENT_NEW, &CCtrlInstruments::OnInstrumentNew)
  773. ON_COMMAND(IDC_INSTRUMENT_OPEN, &CCtrlInstruments::OnInstrumentOpen)
  774. ON_COMMAND(IDC_INSTRUMENT_SAVEAS, &CCtrlInstruments::OnInstrumentSave)
  775. ON_COMMAND(IDC_SAVE_ONE, &CCtrlInstruments::OnInstrumentSaveOne)
  776. ON_COMMAND(IDC_SAVE_ALL, &CCtrlInstruments::OnInstrumentSaveAll)
  777. ON_COMMAND(IDC_INSTRUMENT_PLAY, &CCtrlInstruments::OnInstrumentPlay)
  778. ON_COMMAND(ID_PREVINSTRUMENT, &CCtrlInstruments::OnPrevInstrument)
  779. ON_COMMAND(ID_NEXTINSTRUMENT, &CCtrlInstruments::OnNextInstrument)
  780. ON_COMMAND(ID_INSTRUMENT_DUPLICATE, &CCtrlInstruments::OnInstrumentDuplicate)
  781. ON_COMMAND(IDC_CHECK1, &CCtrlInstruments::OnSetPanningChanged)
  782. ON_COMMAND(IDC_CHECK2, &CCtrlInstruments::OnEnableCutOff)
  783. ON_COMMAND(IDC_CHECK3, &CCtrlInstruments::OnEnableResonance)
  784. ON_COMMAND(IDC_INSVIEWPLG, &CCtrlInstruments::TogglePluginEditor)
  785. ON_EN_CHANGE(IDC_EDIT_INSTRUMENT, &CCtrlInstruments::OnInstrumentChanged)
  786. ON_EN_CHANGE(IDC_SAMPLE_NAME, &CCtrlInstruments::OnNameChanged)
  787. ON_EN_CHANGE(IDC_SAMPLE_FILENAME, &CCtrlInstruments::OnFileNameChanged)
  788. ON_EN_CHANGE(IDC_EDIT7, &CCtrlInstruments::OnFadeOutVolChanged)
  789. ON_EN_CHANGE(IDC_EDIT8, &CCtrlInstruments::OnGlobalVolChanged)
  790. ON_EN_CHANGE(IDC_EDIT9, &CCtrlInstruments::OnPanningChanged)
  791. ON_EN_CHANGE(IDC_EDIT10, &CCtrlInstruments::OnMPRChanged)
  792. ON_EN_KILLFOCUS(IDC_EDIT10, &CCtrlInstruments::OnMPRKillFocus)
  793. ON_EN_CHANGE(IDC_EDIT11, &CCtrlInstruments::OnMBKChanged)
  794. ON_EN_CHANGE(IDC_EDIT15, &CCtrlInstruments::OnPPSChanged)
  795. ON_EN_CHANGE(IDC_PITCHWHEELDEPTH, &CCtrlInstruments::OnPitchWheelDepthChanged)
  796. ON_EN_CHANGE(IDC_EDIT2, &CCtrlInstruments::OnAttackChanged)
  797. ON_EN_SETFOCUS(IDC_SAMPLE_NAME, &CCtrlInstruments::OnEditFocus)
  798. ON_EN_SETFOCUS(IDC_SAMPLE_FILENAME, &CCtrlInstruments::OnEditFocus)
  799. ON_EN_SETFOCUS(IDC_EDIT7, &CCtrlInstruments::OnEditFocus)
  800. ON_EN_SETFOCUS(IDC_EDIT8, &CCtrlInstruments::OnEditFocus)
  801. ON_EN_SETFOCUS(IDC_EDIT9, &CCtrlInstruments::OnEditFocus)
  802. ON_EN_SETFOCUS(IDC_EDIT10, &CCtrlInstruments::OnEditFocus)
  803. ON_EN_SETFOCUS(IDC_EDIT11, &CCtrlInstruments::OnEditFocus)
  804. ON_EN_SETFOCUS(IDC_EDIT15, &CCtrlInstruments::OnEditFocus)
  805. ON_EN_SETFOCUS(IDC_PITCHWHEELDEPTH, &CCtrlInstruments::OnEditFocus)
  806. ON_EN_SETFOCUS(IDC_EDIT2, &CCtrlInstruments::OnEditFocus)
  807. ON_EN_SETFOCUS(IDC_EDIT_PITCHTEMPOLOCK, &CCtrlInstruments::OnEditFocus)
  808. ON_CBN_SELCHANGE(IDC_COMBO1, &CCtrlInstruments::OnNNAChanged)
  809. ON_CBN_SELCHANGE(IDC_COMBO2, &CCtrlInstruments::OnDCTChanged)
  810. ON_CBN_SELCHANGE(IDC_COMBO3, &CCtrlInstruments::OnDCAChanged)
  811. ON_CBN_SELCHANGE(IDC_COMBO4, &CCtrlInstruments::OnPPCChanged)
  812. ON_CBN_SELCHANGE(IDC_COMBO5, &CCtrlInstruments::OnMCHChanged)
  813. ON_CBN_SELCHANGE(IDC_COMBO6, &CCtrlInstruments::OnMixPlugChanged)
  814. ON_CBN_DROPDOWN(IDC_COMBO6, &CCtrlInstruments::OnOpenPluginList)
  815. ON_CBN_SELCHANGE(IDC_COMBO9, &CCtrlInstruments::OnResamplingChanged)
  816. ON_CBN_SELCHANGE(IDC_FILTERMODE, &CCtrlInstruments::OnFilterModeChanged)
  817. ON_CBN_SELCHANGE(IDC_PLUGIN_VOLUMESTYLE, &CCtrlInstruments::OnPluginVolumeHandlingChanged)
  818. ON_COMMAND(IDC_PLUGIN_VELOCITYSTYLE, &CCtrlInstruments::OnPluginVelocityHandlingChanged)
  819. ON_COMMAND(ID_INSTRUMENT_SAMPLEMAP, &CCtrlInstruments::OnEditSampleMap)
  820. ON_CBN_SELCHANGE(IDC_COMBOTUNING, &CCtrlInstruments::OnCbnSelchangeCombotuning)
  821. ON_EN_CHANGE(IDC_EDIT_PITCHTEMPOLOCK, &CCtrlInstruments::OnEnChangeEditPitchTempoLock)
  822. ON_BN_CLICKED(IDC_CHECK_PITCHTEMPOLOCK, &CCtrlInstruments::OnBnClickedCheckPitchtempolock)
  823. ON_EN_KILLFOCUS(IDC_EDIT_PITCHTEMPOLOCK, &CCtrlInstruments::OnEnKillFocusEditPitchTempoLock)
  824. ON_EN_KILLFOCUS(IDC_EDIT7, &CCtrlInstruments::OnEnKillFocusEditFadeOut)
  825. //}}AFX_MSG_MAP
  826. END_MESSAGE_MAP()
  827. void CCtrlInstruments::DoDataExchange(CDataExchange* pDX)
  828. {
  829. CModControlDlg::DoDataExchange(pDX);
  830. //{{AFX_DATA_MAP(CCtrlInstruments)
  831. DDX_Control(pDX, IDC_TOOLBAR1, m_ToolBar);
  832. DDX_Control(pDX, IDC_NOTEMAP, m_NoteMap);
  833. DDX_Control(pDX, IDC_SAMPLE_NAME, m_EditName);
  834. DDX_Control(pDX, IDC_SAMPLE_FILENAME, m_EditFileName);
  835. DDX_Control(pDX, IDC_SPIN_INSTRUMENT, m_SpinInstrument);
  836. DDX_Control(pDX, IDC_COMBO1, m_ComboNNA);
  837. DDX_Control(pDX, IDC_COMBO2, m_ComboDCT);
  838. DDX_Control(pDX, IDC_COMBO3, m_ComboDCA);
  839. DDX_Control(pDX, IDC_COMBO4, m_ComboPPC);
  840. DDX_Control(pDX, IDC_COMBO5, m_CbnMidiCh);
  841. DDX_Control(pDX, IDC_COMBO6, m_CbnMixPlug);
  842. DDX_Control(pDX, IDC_COMBO9, m_CbnResampling);
  843. DDX_Control(pDX, IDC_FILTERMODE, m_CbnFilterMode);
  844. DDX_Control(pDX, IDC_EDIT7, m_EditFadeOut);
  845. DDX_Control(pDX, IDC_SPIN7, m_SpinFadeOut);
  846. DDX_Control(pDX, IDC_SPIN8, m_SpinGlobalVol);
  847. DDX_Control(pDX, IDC_SPIN9, m_SpinPanning);
  848. DDX_Control(pDX, IDC_SPIN10, m_SpinMidiPR);
  849. DDX_Control(pDX, IDC_SPIN11, m_SpinMidiBK);
  850. DDX_Control(pDX, IDC_SPIN12, m_SpinPPS);
  851. DDX_Control(pDX, IDC_EDIT8, m_EditGlobalVol);
  852. DDX_Control(pDX, IDC_EDIT9, m_EditPanning);
  853. DDX_Control(pDX, IDC_CHECK1, m_CheckPanning);
  854. DDX_Control(pDX, IDC_CHECK2, m_CheckCutOff);
  855. DDX_Control(pDX, IDC_CHECK3, m_CheckResonance);
  856. DDX_Control(pDX, IDC_SLIDER1, m_SliderVolSwing);
  857. DDX_Control(pDX, IDC_SLIDER2, m_SliderPanSwing);
  858. DDX_Control(pDX, IDC_SLIDER3, m_SliderCutOff);
  859. DDX_Control(pDX, IDC_SLIDER4, m_SliderResonance);
  860. DDX_Control(pDX, IDC_SLIDER6, m_SliderCutSwing);
  861. DDX_Control(pDX, IDC_SLIDER7, m_SliderResSwing);
  862. DDX_Control(pDX, IDC_SLIDER5, m_SliderAttack);
  863. DDX_Control(pDX, IDC_SPIN1, m_SpinAttack);
  864. DDX_Control(pDX, IDC_COMBOTUNING, m_ComboTuning);
  865. DDX_Control(pDX, IDC_CHECK_PITCHTEMPOLOCK, m_CheckPitchTempoLock);
  866. DDX_Control(pDX, IDC_PLUGIN_VOLUMESTYLE, m_CbnPluginVolumeHandling);
  867. DDX_Control(pDX, IDC_PLUGIN_VELOCITYSTYLE, velocityStyle);
  868. DDX_Control(pDX, IDC_SPIN2, m_SpinPWD);
  869. //}}AFX_DATA_MAP
  870. }
  871. CCtrlInstruments::CCtrlInstruments(CModControlView &parent, CModDoc &document)
  872. : CModControlDlg(parent, document)
  873. , m_NoteMap(*this, document)
  874. {
  875. m_nLockCount = 1;
  876. }
  877. CRuntimeClass *CCtrlInstruments::GetAssociatedViewClass()
  878. {
  879. return RUNTIME_CLASS(CViewInstrument);
  880. }
  881. void CCtrlInstruments::OnEditFocus()
  882. {
  883. m_startedEdit = false;
  884. }
  885. BOOL CCtrlInstruments::OnInitDialog()
  886. {
  887. CModControlDlg::OnInitDialog();
  888. m_bInitialized = FALSE;
  889. SetRedraw(FALSE);
  890. m_ToolBar.SetExtendedStyle(m_ToolBar.GetExtendedStyle() | TBSTYLE_EX_DRAWDDARROWS);
  891. m_ToolBar.Init(CMainFrame::GetMainFrame()->m_PatternIcons,CMainFrame::GetMainFrame()->m_PatternIconsDisabled);
  892. m_ToolBar.AddButton(IDC_INSTRUMENT_NEW, TIMAGE_INSTR_NEW, TBSTYLE_BUTTON | TBSTYLE_DROPDOWN);
  893. m_ToolBar.AddButton(IDC_INSTRUMENT_OPEN, TIMAGE_OPEN);
  894. m_ToolBar.AddButton(IDC_INSTRUMENT_SAVEAS, TIMAGE_SAVE, TBSTYLE_BUTTON | TBSTYLE_DROPDOWN);
  895. m_ToolBar.AddButton(IDC_INSTRUMENT_PLAY, TIMAGE_PREVIEW);
  896. m_SpinInstrument.SetRange(0, 0);
  897. m_SpinInstrument.EnableWindow(FALSE);
  898. // NNA
  899. m_ComboNNA.AddString(_T("Note Cut"));
  900. m_ComboNNA.AddString(_T("Continue"));
  901. m_ComboNNA.AddString(_T("Note Off"));
  902. m_ComboNNA.AddString(_T("Note Fade"));
  903. // DCT
  904. m_ComboDCT.AddString(_T("Disabled"));
  905. m_ComboDCT.AddString(_T("Note"));
  906. m_ComboDCT.AddString(_T("Sample"));
  907. m_ComboDCT.AddString(_T("Instrument"));
  908. m_ComboDCT.AddString(_T("Plugin"));
  909. // DCA
  910. m_ComboDCA.AddString(_T("Note Cut"));
  911. m_ComboDCA.AddString(_T("Note Off"));
  912. m_ComboDCA.AddString(_T("Note Fade"));
  913. // FadeOut Volume
  914. m_SpinFadeOut.SetRange(0, 8192);
  915. // Global Volume
  916. m_SpinGlobalVol.SetRange(0, 64);
  917. // Panning
  918. m_SpinPanning.SetRange(0, (m_modDoc.GetModType() & MOD_TYPE_IT) ? 64 : 256);
  919. // Midi Program
  920. m_SpinMidiPR.SetRange(0, 128);
  921. // Midi Bank
  922. m_SpinMidiBK.SetRange(0, 16384);
  923. // MIDI Pitch Wheel Depth
  924. m_EditPWD.SubclassDlgItem(IDC_PITCHWHEELDEPTH, this);
  925. m_EditPWD.AllowFractions(false);
  926. const auto resamplingModes = Resampling::AllModes();
  927. m_CbnResampling.SetItemData(m_CbnResampling.AddString(_T("Default")), SRCMODE_DEFAULT);
  928. for(auto mode : resamplingModes)
  929. {
  930. m_CbnResampling.SetItemData(m_CbnResampling.AddString(CTrackApp::GetResamplingModeName(mode, 1, false)), mode);
  931. }
  932. m_CbnFilterMode.SetItemData(m_CbnFilterMode.AddString(_T("Channel default")), static_cast<DWORD_PTR>(FilterMode::Unchanged));
  933. m_CbnFilterMode.SetItemData(m_CbnFilterMode.AddString(_T("Force lowpass")), static_cast<DWORD_PTR>(FilterMode::LowPass));
  934. m_CbnFilterMode.SetItemData(m_CbnFilterMode.AddString(_T("Force highpass")), static_cast<DWORD_PTR>(FilterMode::HighPass));
  935. //VST velocity/volume handling
  936. m_CbnPluginVolumeHandling.AddString(_T("MIDI volume"));
  937. m_CbnPluginVolumeHandling.AddString(_T("Dry/Wet ratio"));
  938. m_CbnPluginVolumeHandling.AddString(_T("None"));
  939. // Vol/Pan Swing
  940. m_SliderVolSwing.SetRange(0, 100);
  941. m_SliderPanSwing.SetRange(0, 64);
  942. m_SliderCutSwing.SetRange(0, 64);
  943. m_SliderResSwing.SetRange(0, 64);
  944. // Filter
  945. m_SliderCutOff.SetRange(0x00, 0x7F);
  946. m_SliderResonance.SetRange(0x00, 0x7F);
  947. // Pitch/Pan Separation
  948. m_EditPPS.SubclassDlgItem(IDC_EDIT15, this);
  949. m_EditPPS.AllowFractions(false);
  950. m_SpinPPS.SetRange(-32, +32);
  951. // Pitch/Pan Center
  952. SetWindowLongPtr(m_ComboPPC.m_hWnd, GWLP_USERDATA, 0);
  953. // Volume ramping (attack)
  954. m_SliderAttack.SetRange(0,MAX_ATTACK_VALUE);
  955. m_SpinAttack.SetRange(0,MAX_ATTACK_VALUE);
  956. m_SpinInstrument.SetFocus();
  957. m_EditPWD.EnableWindow(FALSE);
  958. BuildTuningComboBox();
  959. CheckDlgButton(IDC_CHECK_PITCHTEMPOLOCK, BST_UNCHECKED);
  960. m_EditPitchTempoLock.SubclassDlgItem(IDC_EDIT_PITCHTEMPOLOCK, this);
  961. m_EditPitchTempoLock.AllowNegative(false);
  962. m_EditPitchTempoLock.SetLimitText(9);
  963. SetRedraw(TRUE);
  964. return FALSE;
  965. }
  966. void CCtrlInstruments::RecalcLayout()
  967. {
  968. }
  969. void CCtrlInstruments::OnTbnDropDownToolBar(NMHDR *pNMHDR, LRESULT *pResult)
  970. {
  971. CInputHandler *ih = CMainFrame::GetInputHandler();
  972. NMTOOLBAR *pToolBar = reinterpret_cast<NMTOOLBAR *>(pNMHDR);
  973. ClientToScreen(&(pToolBar->rcButton)); // TrackPopupMenu uses screen coords
  974. const int offset = Util::ScalePixels(4, m_hWnd); // Compared to the main toolbar, the offset seems to be a bit wrong here...?
  975. int x = pToolBar->rcButton.left + offset, y = pToolBar->rcButton.bottom + offset;
  976. CMenu menu;
  977. switch(pToolBar->iItem)
  978. {
  979. case IDC_INSTRUMENT_NEW:
  980. {
  981. menu.CreatePopupMenu();
  982. menu.AppendMenu(MF_STRING, ID_INSTRUMENT_DUPLICATE, ih->GetKeyTextFromCommand(kcInstrumentCtrlDuplicate, _T("Duplicate &Instrument")));
  983. menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, this);
  984. menu.DestroyMenu();
  985. }
  986. break;
  987. case IDC_INSTRUMENT_SAVEAS:
  988. {
  989. menu.CreatePopupMenu();
  990. menu.AppendMenu(MF_STRING, IDC_SAVE_ALL, _T("Save &All..."));
  991. menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, this);
  992. menu.DestroyMenu();
  993. }
  994. break;
  995. }
  996. *pResult = 0;
  997. }
  998. void CCtrlInstruments::PrepareUndo(const char *description)
  999. {
  1000. m_startedEdit = true;
  1001. m_modDoc.GetInstrumentUndo().PrepareUndo(m_nInstrument, description);
  1002. }
  1003. // Set document as modified and update other views.
  1004. // updateAll: Update all views including this one. Otherwise, only update update other views.
  1005. void CCtrlInstruments::SetModified(InstrumentHint hint, bool updateAll)
  1006. {
  1007. m_modDoc.SetModified();
  1008. m_modDoc.UpdateAllViews(nullptr, hint.SetData(m_nInstrument), updateAll ? nullptr : this);
  1009. }
  1010. BOOL CCtrlInstruments::SetCurrentInstrument(UINT nIns, BOOL bUpdNum)
  1011. {
  1012. if (m_sndFile.m_nInstruments < 1) return FALSE;
  1013. if ((nIns < 1) || (nIns > m_sndFile.m_nInstruments)) return FALSE;
  1014. LockControls();
  1015. if ((m_nInstrument != nIns) || (!m_bInitialized))
  1016. {
  1017. m_nInstrument = static_cast<INSTRUMENTINDEX>(nIns);
  1018. m_NoteMap.SetCurrentInstrument(m_nInstrument);
  1019. UpdateView(InstrumentHint(m_nInstrument).Info().Envelope(), NULL);
  1020. } else
  1021. {
  1022. // Just in case
  1023. m_NoteMap.SetCurrentInstrument(m_nInstrument);
  1024. }
  1025. if (bUpdNum)
  1026. {
  1027. SetDlgItemInt(IDC_EDIT_INSTRUMENT, m_nInstrument);
  1028. m_SpinInstrument.SetRange(1, m_sndFile.GetNumInstruments());
  1029. m_SpinInstrument.EnableWindow((m_sndFile.GetNumInstruments()) ? TRUE : FALSE);
  1030. // Is this a bug ?
  1031. m_SliderCutOff.Invalidate(FALSE);
  1032. m_SliderResonance.Invalidate(FALSE);
  1033. // Volume ramping (attack)
  1034. m_SliderAttack.Invalidate(FALSE);
  1035. }
  1036. SendViewMessage(VIEWMSG_SETCURRENTINSTRUMENT, m_nInstrument);
  1037. UnlockControls();
  1038. return TRUE;
  1039. }
  1040. void CCtrlInstruments::OnActivatePage(LPARAM lParam)
  1041. {
  1042. CModControlDlg::OnActivatePage(lParam);
  1043. if (lParam < 0)
  1044. {
  1045. int nIns = m_parent.GetInstrumentChange();
  1046. if (nIns > 0) lParam = nIns;
  1047. } else if(lParam > 0)
  1048. {
  1049. m_parent.InstrumentChanged(static_cast<INSTRUMENTINDEX>(lParam));
  1050. }
  1051. UpdatePluginList();
  1052. CChildFrame *pFrame = (CChildFrame *)GetParentFrame();
  1053. INSTRUMENTVIEWSTATE &instrumentState = pFrame->GetInstrumentViewState();
  1054. if(instrumentState.initialInstrument != 0)
  1055. {
  1056. m_nInstrument = instrumentState.initialInstrument;
  1057. instrumentState.initialInstrument = 0;
  1058. }
  1059. SetCurrentInstrument(static_cast<INSTRUMENTINDEX>((lParam > 0) ? lParam : m_nInstrument));
  1060. // Initial Update
  1061. if (!m_bInitialized) UpdateView(InstrumentHint(m_nInstrument).Info().Envelope().ModType(), NULL);
  1062. PostViewMessage(VIEWMSG_LOADSTATE, (LPARAM)&instrumentState);
  1063. SwitchToView();
  1064. // Combo boxes randomly disappear without this... why?
  1065. Invalidate();
  1066. }
  1067. void CCtrlInstruments::OnDeactivatePage()
  1068. {
  1069. m_modDoc.NoteOff(0, true);
  1070. CChildFrame *pFrame = (CChildFrame *)GetParentFrame();
  1071. if ((pFrame) && (m_hWndView)) SendViewMessage(VIEWMSG_SAVESTATE, (LPARAM)&pFrame->GetInstrumentViewState());
  1072. CModControlDlg::OnDeactivatePage();
  1073. }
  1074. LRESULT CCtrlInstruments::OnModCtrlMsg(WPARAM wParam, LPARAM lParam)
  1075. {
  1076. switch(wParam)
  1077. {
  1078. case CTRLMSG_GETCURRENTINSTRUMENT:
  1079. return m_nInstrument;
  1080. break;
  1081. case CTRLMSG_INS_PREVINSTRUMENT:
  1082. OnPrevInstrument();
  1083. break;
  1084. case CTRLMSG_INS_NEXTINSTRUMENT:
  1085. OnNextInstrument();
  1086. break;
  1087. case CTRLMSG_INS_OPENFILE:
  1088. if(lParam)
  1089. return OpenInstrument(*reinterpret_cast<const mpt::PathString *>(lParam));
  1090. break;
  1091. case CTRLMSG_INS_SONGDROP:
  1092. if(lParam)
  1093. {
  1094. const auto &dropInfo = *reinterpret_cast<const DRAGONDROP *>(lParam);
  1095. if(dropInfo.sndFile)
  1096. return OpenInstrument(*dropInfo.sndFile, static_cast<INSTRUMENTINDEX>(dropInfo.dropItem));
  1097. }
  1098. break;
  1099. case CTRLMSG_INS_NEWINSTRUMENT:
  1100. return InsertInstrument(false) ? 1 : 0;
  1101. case CTRLMSG_SETCURRENTINSTRUMENT:
  1102. SetCurrentInstrument(static_cast<INSTRUMENTINDEX>(lParam));
  1103. break;
  1104. case CTRLMSG_INS_SAMPLEMAP:
  1105. OnEditSampleMap();
  1106. break;
  1107. case IDC_INSTRUMENT_NEW:
  1108. OnInstrumentNew();
  1109. break;
  1110. case IDC_INSTRUMENT_OPEN:
  1111. OnInstrumentOpen();
  1112. break;
  1113. case IDC_INSTRUMENT_SAVEAS:
  1114. OnInstrumentSave();
  1115. break;
  1116. default:
  1117. return CModControlDlg::OnModCtrlMsg(wParam, lParam);
  1118. }
  1119. return 0;
  1120. }
  1121. void CCtrlInstruments::UpdateView(UpdateHint hint, CObject *pObj)
  1122. {
  1123. if(pObj == this)
  1124. return;
  1125. if (hint.GetType()[HINT_MPTOPTIONS])
  1126. {
  1127. m_ToolBar.UpdateStyle();
  1128. hint.ModType(); // For possibly updating note names in Pitch/Pan Separation dropdown
  1129. }
  1130. LockControls();
  1131. if(hint.ToType<PluginHint>().GetType()[HINT_MIXPLUGINS | HINT_PLUGINNAMES])
  1132. {
  1133. OnMixPlugChanged();
  1134. }
  1135. if(hint.ToType<GeneralHint>().GetType()[HINT_TUNINGS])
  1136. {
  1137. BuildTuningComboBox();
  1138. }
  1139. UnlockControls();
  1140. const InstrumentHint instrHint = hint.ToType<InstrumentHint>();
  1141. FlagSet<HintType> hintType = instrHint.GetType();
  1142. if(!m_bInitialized)
  1143. hintType.set(HINT_MODTYPE);
  1144. if(!hintType[HINT_MODTYPE | HINT_INSTRUMENT | HINT_ENVELOPE | HINT_INSNAMES])
  1145. return;
  1146. const INSTRUMENTINDEX updateIns = instrHint.GetInstrument();
  1147. if(updateIns != m_nInstrument && updateIns != 0 && !hintType[HINT_MODTYPE])
  1148. return;
  1149. LockControls();
  1150. const ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  1151. if(hintType[HINT_MODTYPE])
  1152. {
  1153. auto &specs = m_sndFile.GetModSpecifications();
  1154. // Limit text fields
  1155. m_EditName.SetLimitText(specs.instrNameLengthMax);
  1156. m_EditFileName.SetLimitText(specs.instrFilenameLengthMax);
  1157. const BOOL bITandMPT = ((m_sndFile.GetType() & (MOD_TYPE_IT | MOD_TYPE_MPT)) && (m_sndFile.GetNumInstruments())) ? TRUE : FALSE;
  1158. const BOOL bITandXM = ((m_sndFile.GetType() & (MOD_TYPE_IT | MOD_TYPE_MPT | MOD_TYPE_XM)) && (m_sndFile.GetNumInstruments())) ? TRUE : FALSE;
  1159. const BOOL bMPTOnly = ((m_sndFile.GetType() == MOD_TYPE_MPT) && (m_sndFile.GetNumInstruments())) ? TRUE : FALSE;
  1160. ::EnableWindow(::GetDlgItem(m_hWnd, IDC_EDIT10), bITandXM);
  1161. ::EnableWindow(::GetDlgItem(m_hWnd, IDC_EDIT11), bITandXM);
  1162. ::EnableWindow(::GetDlgItem(m_hWnd, IDC_EDIT7), bITandXM);
  1163. m_EditName.EnableWindow(bITandXM);
  1164. m_EditFileName.EnableWindow(bITandMPT);
  1165. m_CbnMidiCh.EnableWindow(bITandXM);
  1166. m_CbnMixPlug.EnableWindow(bITandXM);
  1167. m_SpinMidiPR.EnableWindow(bITandXM);
  1168. m_SpinMidiBK.EnableWindow(bITandXM);
  1169. const bool extendedFadeoutRange = !(m_sndFile.GetType() & MOD_TYPE_IT);
  1170. m_SpinFadeOut.EnableWindow(bITandXM);
  1171. m_SpinFadeOut.SetRange(0, extendedFadeoutRange ? 32767 : 8192);
  1172. m_EditFadeOut.SetLimitText(extendedFadeoutRange ? 5 : 4);
  1173. // XM-style fade-out is 32 times more precise than IT
  1174. UDACCEL accell[2];
  1175. accell[0].nSec = 0;
  1176. accell[0].nInc = (m_sndFile.GetType() == MOD_TYPE_IT ? 32 : 1);
  1177. accell[1].nSec = 2;
  1178. accell[1].nInc = 5 * accell[0].nInc;
  1179. m_SpinFadeOut.SetAccel(mpt::saturate_cast<int>(std::size(accell)), accell);
  1180. // Panning ranges (0...64 for IT, 0...256 for MPTM)
  1181. m_SpinPanning.SetRange(0, (m_sndFile.GetType() & MOD_TYPE_IT) ? 64 : 256);
  1182. // Pitch Wheel Depth
  1183. if(m_sndFile.GetType() == MOD_TYPE_XM)
  1184. m_SpinPWD.SetRange(0, 36);
  1185. else
  1186. m_SpinPWD.SetRange(-128, 127);
  1187. m_EditPWD.EnableWindow(bITandXM);
  1188. m_SpinPWD.EnableWindow(bITandXM);
  1189. m_NoteMap.EnableWindow(bITandXM);
  1190. m_ComboNNA.EnableWindow(bITandMPT);
  1191. m_SliderVolSwing.EnableWindow(bITandMPT);
  1192. m_SliderPanSwing.EnableWindow(bITandMPT);
  1193. m_ComboDCT.EnableWindow(bITandMPT);
  1194. m_ComboDCA.EnableWindow(bITandMPT);
  1195. m_ComboPPC.EnableWindow(bITandMPT);
  1196. m_SpinPPS.EnableWindow(bITandMPT);
  1197. m_EditGlobalVol.EnableWindow(bITandMPT);
  1198. m_SpinGlobalVol.EnableWindow(bITandMPT);
  1199. m_EditPanning.EnableWindow(bITandMPT);
  1200. m_SpinPanning.EnableWindow(bITandMPT);
  1201. m_CheckPanning.EnableWindow(bITandMPT);
  1202. m_EditPPS.EnableWindow(bITandMPT);
  1203. m_CheckCutOff.EnableWindow(bITandMPT);
  1204. m_CheckResonance.EnableWindow(bITandMPT);
  1205. m_SliderCutOff.EnableWindow(bITandMPT);
  1206. m_SliderResonance.EnableWindow(bITandMPT);
  1207. m_ComboTuning.EnableWindow(bMPTOnly);
  1208. m_EditPitchTempoLock.EnableWindow(bMPTOnly);
  1209. m_CheckPitchTempoLock.EnableWindow(bMPTOnly);
  1210. // MIDI Channel
  1211. // XM has no "mapped" MIDI channels.
  1212. m_CbnMidiCh.ResetContent();
  1213. for(int ich = MidiNoChannel; ich <= (bITandMPT ? MidiMappedChannel : MidiLastChannel); ich++)
  1214. {
  1215. CString s;
  1216. if (ich == MidiNoChannel)
  1217. s = _T("None");
  1218. else if (ich == MidiMappedChannel)
  1219. s = _T("Mapped");
  1220. else
  1221. s.Format(_T("%i"), ich);
  1222. m_CbnMidiCh.SetItemData(m_CbnMidiCh.AddString(s), ich);
  1223. }
  1224. }
  1225. if(hintType[HINT_MODTYPE | HINT_INSTRUMENT | HINT_INSNAMES])
  1226. {
  1227. if(pIns)
  1228. m_EditName.SetWindowText(mpt::ToCString(m_sndFile.GetCharsetInternal(), pIns->name));
  1229. else
  1230. m_EditName.SetWindowText(_T(""));
  1231. }
  1232. if(hintType[HINT_MODTYPE | HINT_INSTRUMENT])
  1233. {
  1234. m_SpinInstrument.SetRange(1, m_sndFile.m_nInstruments);
  1235. m_SpinInstrument.EnableWindow((m_sndFile.m_nInstruments) ? TRUE : FALSE);
  1236. // Backwards compatibility with legacy IT/XM modules that use now deprecated hack features.
  1237. m_SliderCutSwing.EnableWindow(pIns != nullptr && (m_sndFile.GetType() == MOD_TYPE_MPT || pIns->nCutSwing != 0));
  1238. m_SliderResSwing.EnableWindow(pIns != nullptr && (m_sndFile.GetType() == MOD_TYPE_MPT || pIns->nResSwing != 0));
  1239. m_CbnFilterMode.EnableWindow (pIns != nullptr && (m_sndFile.GetType() == MOD_TYPE_MPT || pIns->filterMode != FilterMode::Unchanged));
  1240. m_CbnResampling.EnableWindow (pIns != nullptr && (m_sndFile.GetType() == MOD_TYPE_MPT || pIns->resampling != SRCMODE_DEFAULT));
  1241. m_SliderAttack.EnableWindow (pIns != nullptr && (m_sndFile.GetType() == MOD_TYPE_MPT || pIns->nVolRampUp));
  1242. ::EnableWindow(::GetDlgItem(m_hWnd, IDC_EDIT2), pIns != nullptr && (m_sndFile.GetType() == MOD_TYPE_MPT || pIns->nVolRampUp));
  1243. if (pIns)
  1244. {
  1245. m_EditFileName.SetWindowText(mpt::ToCString(m_sndFile.GetCharsetInternal(), pIns->filename));
  1246. // Fade Out Volume
  1247. SetDlgItemInt(IDC_EDIT7, pIns->nFadeOut);
  1248. // Global Volume
  1249. SetDlgItemInt(IDC_EDIT8, pIns->nGlobalVol);
  1250. // Panning
  1251. SetDlgItemInt(IDC_EDIT9, (m_modDoc.GetModType() & MOD_TYPE_IT) ? (pIns->nPan / 4) : pIns->nPan);
  1252. m_CheckPanning.SetCheck(pIns->dwFlags[INS_SETPANNING] ? TRUE : FALSE);
  1253. // Midi
  1254. if (pIns->nMidiProgram>0 && pIns->nMidiProgram<=128)
  1255. SetDlgItemInt(IDC_EDIT10, pIns->nMidiProgram);
  1256. else
  1257. SetDlgItemText(IDC_EDIT10, _T("---"));
  1258. if (pIns->wMidiBank && pIns->wMidiBank <= 16384)
  1259. SetDlgItemInt(IDC_EDIT11, pIns->wMidiBank);
  1260. else
  1261. SetDlgItemText(IDC_EDIT11, _T("---"));
  1262. if (pIns->nMidiChannel < 18)
  1263. {
  1264. m_CbnMidiCh.SetCurSel(pIns->nMidiChannel);
  1265. } else
  1266. {
  1267. m_CbnMidiCh.SetCurSel(0);
  1268. }
  1269. if (pIns->nMixPlug <= MAX_MIXPLUGINS)
  1270. {
  1271. m_CbnMixPlug.SetCurSel(pIns->nMixPlug);
  1272. } else
  1273. {
  1274. m_CbnMixPlug.SetCurSel(0);
  1275. }
  1276. OnMixPlugChanged();
  1277. for(int resMode = 0; resMode<m_CbnResampling.GetCount(); resMode++)
  1278. {
  1279. if(pIns->resampling == m_CbnResampling.GetItemData(resMode))
  1280. {
  1281. m_CbnResampling.SetCurSel(resMode);
  1282. break;
  1283. }
  1284. }
  1285. for(int fltMode = 0; fltMode<m_CbnFilterMode.GetCount(); fltMode++)
  1286. {
  1287. if(pIns->filterMode == static_cast<FilterMode>(m_CbnFilterMode.GetItemData(fltMode)))
  1288. {
  1289. m_CbnFilterMode.SetCurSel(fltMode);
  1290. break;
  1291. }
  1292. }
  1293. // NNA, DCT, DCA
  1294. m_ComboNNA.SetCurSel(static_cast<int>(pIns->nNNA));
  1295. m_ComboDCT.SetCurSel(static_cast<int>(pIns->nDCT));
  1296. m_ComboDCA.SetCurSel(static_cast<int>(pIns->nDNA));
  1297. // Pitch/Pan Separation
  1298. if(hintType[HINT_MODTYPE] || pIns->pTuning != (CTuning *)GetWindowLongPtr(m_ComboPPC.m_hWnd, GWLP_USERDATA))
  1299. {
  1300. // Tuning may have changed, and thus the note names need to be updated
  1301. m_ComboPPC.SetRedraw(FALSE);
  1302. m_ComboPPC.ResetContent();
  1303. AppendNotesToControlEx(m_ComboPPC, m_sndFile, m_nInstrument, NOTE_MIN, NOTE_MAX);
  1304. SetWindowLongPtr(m_ComboPPC.m_hWnd, GWLP_USERDATA, (LONG_PTR)pIns->pTuning);
  1305. m_ComboPPC.SetRedraw(TRUE);
  1306. }
  1307. m_ComboPPC.SetCurSel(pIns->nPPC);
  1308. ASSERT((uint8)m_ComboPPC.GetItemData(m_ComboPPC.GetCurSel()) == pIns->nPPC + NOTE_MIN);
  1309. SetDlgItemInt(IDC_EDIT15, pIns->nPPS);
  1310. // Filter
  1311. if (m_sndFile.GetType() & (MOD_TYPE_IT | MOD_TYPE_MPT))
  1312. {
  1313. m_CheckCutOff.SetCheck((pIns->IsCutoffEnabled()) ? TRUE : FALSE);
  1314. m_CheckResonance.SetCheck((pIns->IsResonanceEnabled()) ? TRUE : FALSE);
  1315. m_SliderVolSwing.SetPos(pIns->nVolSwing);
  1316. m_SliderPanSwing.SetPos(pIns->nPanSwing);
  1317. m_SliderResSwing.SetPos(pIns->nResSwing);
  1318. m_SliderCutSwing.SetPos(pIns->nCutSwing);
  1319. m_SliderCutOff.SetPos(pIns->GetCutoff());
  1320. m_SliderResonance.SetPos(pIns->GetResonance());
  1321. UpdateFilterText();
  1322. }
  1323. // Volume ramping (attack)
  1324. int n = pIns->nVolRampUp; //? MAX_ATTACK_LENGTH - pIns->nVolRampUp : 0;
  1325. m_SliderAttack.SetPos(n);
  1326. if(n == 0) SetDlgItemText(IDC_EDIT2, _T("default"));
  1327. else SetDlgItemInt(IDC_EDIT2,n);
  1328. UpdateTuningComboBox();
  1329. // Only enable Pitch/Tempo Lock for MPTM files or legacy files that have this property enabled.
  1330. m_CheckPitchTempoLock.EnableWindow((m_sndFile.GetType() == MOD_TYPE_MPT || pIns->pitchToTempoLock.GetRaw() > 0) ? TRUE : FALSE);
  1331. CheckDlgButton(IDC_CHECK_PITCHTEMPOLOCK, pIns->pitchToTempoLock.GetRaw() > 0 ? BST_CHECKED : BST_UNCHECKED);
  1332. m_EditPitchTempoLock.EnableWindow(pIns->pitchToTempoLock.GetRaw() > 0 ? TRUE : FALSE);
  1333. if(pIns->pitchToTempoLock.GetRaw() > 0)
  1334. {
  1335. m_EditPitchTempoLock.SetTempoValue(pIns->pitchToTempoLock);
  1336. }
  1337. // Pitch Wheel Depth
  1338. SetDlgItemInt(IDC_PITCHWHEELDEPTH, pIns->midiPWD, TRUE);
  1339. if(m_sndFile.GetType() & (MOD_TYPE_XM|MOD_TYPE_IT|MOD_TYPE_MPT))
  1340. {
  1341. BOOL enableVol = (m_CbnMixPlug.GetCurSel() > 0 && !m_sndFile.m_playBehaviour[kMIDICCBugEmulation]) ? TRUE : FALSE;
  1342. velocityStyle.EnableWindow(enableVol);
  1343. m_CbnPluginVolumeHandling.EnableWindow(enableVol);
  1344. }
  1345. } else
  1346. {
  1347. m_EditFileName.SetWindowText(_T(""));
  1348. velocityStyle.EnableWindow(FALSE);
  1349. m_CbnPluginVolumeHandling.EnableWindow(FALSE);
  1350. if(m_nInstrument > m_sndFile.GetNumInstruments())
  1351. SetCurrentInstrument(m_sndFile.GetNumInstruments());
  1352. }
  1353. m_NoteMap.Invalidate(FALSE);
  1354. m_ComboNNA.Invalidate(FALSE);
  1355. m_ComboDCT.Invalidate(FALSE);
  1356. m_ComboDCA.Invalidate(FALSE);
  1357. m_ComboPPC.Invalidate(FALSE);
  1358. m_CbnMidiCh.Invalidate(FALSE);
  1359. m_CbnMixPlug.Invalidate(FALSE);
  1360. m_CbnResampling.Invalidate(FALSE);
  1361. m_CbnFilterMode.Invalidate(FALSE);
  1362. m_CbnPluginVolumeHandling.Invalidate(FALSE);
  1363. m_ComboTuning.Invalidate(FALSE);
  1364. }
  1365. if(hint.ToType<PluginHint>().GetType()[HINT_MIXPLUGINS | HINT_PLUGINNAMES | HINT_MODTYPE])
  1366. {
  1367. UpdatePluginList();
  1368. }
  1369. if (!m_bInitialized)
  1370. {
  1371. // First update
  1372. m_bInitialized = TRUE;
  1373. UnlockControls();
  1374. }
  1375. UnlockControls();
  1376. }
  1377. void CCtrlInstruments::UpdateFilterText()
  1378. {
  1379. if(m_nInstrument)
  1380. {
  1381. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  1382. if(pIns)
  1383. {
  1384. TCHAR s[32];
  1385. // In IT Compatible mode, it is enough to just have resonance enabled to turn on the filter.
  1386. const bool resEnabled = (pIns->IsResonanceEnabled() && pIns->GetResonance() > 0 && m_sndFile.m_playBehaviour[kITFilterBehaviour]);
  1387. if((pIns->IsCutoffEnabled() && pIns->GetCutoff() < 0x7F) || resEnabled)
  1388. {
  1389. const BYTE cutoff = (resEnabled && !pIns->IsCutoffEnabled()) ? 0x7F : pIns->GetCutoff();
  1390. wsprintf(s, _T("Z%02X (%u Hz)"), cutoff, m_sndFile.CutOffToFrequency(cutoff));
  1391. } else if(pIns->IsCutoffEnabled())
  1392. {
  1393. _tcscpy(s, _T("Z7F (Off)"));
  1394. } else
  1395. {
  1396. _tcscpy(s, _T("No Change"));
  1397. }
  1398. SetDlgItemText(IDC_FILTERTEXT, s);
  1399. }
  1400. }
  1401. }
  1402. bool CCtrlInstruments::OpenInstrument(const mpt::PathString &fileName)
  1403. {
  1404. BeginWaitCursor();
  1405. InputFile f(fileName, TrackerSettings::Instance().MiscCacheCompleteFileBeforeLoading);
  1406. if(!f.IsValid())
  1407. {
  1408. EndWaitCursor();
  1409. return false;
  1410. }
  1411. FileReader file = GetFileReader(f);
  1412. bool first = false, ok = false;
  1413. if (file.IsValid())
  1414. {
  1415. if (!m_sndFile.GetNumInstruments())
  1416. {
  1417. first = true;
  1418. m_sndFile.m_nInstruments = 1;
  1419. m_modDoc.SetModified();
  1420. }
  1421. if (!m_nInstrument) m_nInstrument = 1;
  1422. ScopedLogCapturer log(m_modDoc, _T("Instrument Import"), this);
  1423. PrepareUndo("Replace Instrument");
  1424. if (m_sndFile.ReadInstrumentFromFile(m_nInstrument, file, TrackerSettings::Instance().m_MayNormalizeSamplesOnLoad))
  1425. {
  1426. ok = true;
  1427. } else
  1428. {
  1429. m_modDoc.GetInstrumentUndo().RemoveLastUndoStep(m_nInstrument);
  1430. }
  1431. }
  1432. if(!ok && first)
  1433. {
  1434. // Undo adding the instrument
  1435. delete m_sndFile.Instruments[1];
  1436. m_sndFile.m_nInstruments = 0;
  1437. } else if(ok && first)
  1438. {
  1439. m_NoteMap.SetCurrentInstrument(1);
  1440. }
  1441. EndWaitCursor();
  1442. if(ok)
  1443. {
  1444. TrackerSettings::Instance().PathInstruments.SetWorkingDir(fileName, true);
  1445. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  1446. if (pIns)
  1447. {
  1448. mpt::PathString name, ext;
  1449. fileName.SplitPath(nullptr, nullptr, &name, &ext);
  1450. if (!pIns->name[0] && m_sndFile.GetModSpecifications().instrNameLengthMax > 0)
  1451. {
  1452. pIns->name = mpt::truncate(name.ToLocale(), m_sndFile.GetModSpecifications().instrNameLengthMax);
  1453. }
  1454. if (!pIns->filename[0] && m_sndFile.GetModSpecifications().instrFilenameLengthMax > 0)
  1455. {
  1456. name += ext;
  1457. pIns->filename = mpt::truncate(name.ToLocale(), m_sndFile.GetModSpecifications().instrFilenameLengthMax);
  1458. }
  1459. SetCurrentInstrument(m_nInstrument);
  1460. InstrumentHint hint = InstrumentHint().Info().Envelope().Names();
  1461. if(first) hint.ModType();
  1462. SetModified(hint, true);
  1463. } else ok = FALSE;
  1464. } else
  1465. {
  1466. // Try loading as module
  1467. ok = CMainFrame::GetMainFrame()->SetTreeSoundfile(file);
  1468. if(ok) return true;
  1469. }
  1470. SampleHint hint = SampleHint().Info().Data().Names();
  1471. if (first) hint.ModType();
  1472. m_modDoc.UpdateAllViews(nullptr, hint);
  1473. if (!ok) ErrorBox(IDS_ERR_FILETYPE, this);
  1474. return ok;
  1475. }
  1476. bool CCtrlInstruments::OpenInstrument(const CSoundFile &sndFile, INSTRUMENTINDEX nInstr)
  1477. {
  1478. if((!nInstr) || (nInstr > sndFile.GetNumInstruments())) return false;
  1479. BeginWaitCursor();
  1480. CriticalSection cs;
  1481. bool first = false;
  1482. if (!m_sndFile.GetNumInstruments())
  1483. {
  1484. first = true;
  1485. m_sndFile.m_nInstruments = 1;
  1486. SetCurrentInstrument(1);
  1487. first = true;
  1488. }
  1489. PrepareUndo("Replace Instrument");
  1490. m_sndFile.ReadInstrumentFromSong(m_nInstrument, sndFile, nInstr);
  1491. cs.Leave();
  1492. {
  1493. InstrumentHint hint = InstrumentHint().Info().Envelope().Names();
  1494. if (first) hint.ModType();
  1495. SetModified(hint, true);
  1496. }
  1497. {
  1498. SampleHint hint = SampleHint().Info().Data().Names();
  1499. if (first) hint.ModType();
  1500. m_modDoc.UpdateAllViews(nullptr, hint, this);
  1501. }
  1502. EndWaitCursor();
  1503. return true;
  1504. }
  1505. BOOL CCtrlInstruments::EditSample(UINT nSample)
  1506. {
  1507. if ((nSample > 0) && (nSample < MAX_SAMPLES))
  1508. {
  1509. m_parent.PostMessage(WM_MOD_ACTIVATEVIEW, IDD_CONTROL_SAMPLES, nSample);
  1510. return TRUE;
  1511. }
  1512. return FALSE;
  1513. }
  1514. BOOL CCtrlInstruments::GetToolTipText(UINT uId, LPTSTR pszText)
  1515. {
  1516. //Note: pszText points to a TCHAR array of length 256 (see CChildFrame::OnToolTipText).
  1517. //Note2: If there's problems in getting tooltips showing for certain tools,
  1518. // setting the tab order may have effect.
  1519. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  1520. if(pIns == nullptr) return FALSE;
  1521. if ((pszText) && (uId))
  1522. {
  1523. CWnd *wnd = GetDlgItem(uId);
  1524. bool isEnabled = wnd != nullptr && wnd->IsWindowEnabled() != FALSE;
  1525. const auto plusMinus = mpt::ToWin(mpt::Charset::UTF8, "\xC2\xB1");
  1526. const TCHAR *s = nullptr;
  1527. CommandID cmd = kcNull;
  1528. switch(uId)
  1529. {
  1530. case IDC_INSTRUMENT_NEW: s = _T("Insert Instrument (Hold Shift to duplicate)"); cmd = kcInstrumentNew; break;
  1531. case IDC_INSTRUMENT_OPEN: s = _T("Import Instrument"); cmd = kcInstrumentLoad; break;
  1532. case IDC_INSTRUMENT_SAVEAS: s = _T("Save Instrument"); cmd = kcInstrumentSave; break;
  1533. case IDC_INSTRUMENT_PLAY: s = _T("Play Instrument"); break;
  1534. case IDC_EDIT_PITCHTEMPOLOCK:
  1535. case IDC_CHECK_PITCHTEMPOLOCK:
  1536. // Pitch/Tempo lock
  1537. if(isEnabled)
  1538. {
  1539. const CModSpecifications& specs = m_sndFile.GetModSpecifications();
  1540. wsprintf(pszText, _T("Tempo Range: %u - %u"), specs.GetTempoMin().GetInt(), specs.GetTempoMax().GetInt());
  1541. } else
  1542. {
  1543. _tcscpy(pszText, _T("Only available in MPTM format"));
  1544. }
  1545. return TRUE;
  1546. case IDC_EDIT7:
  1547. // Fade Out
  1548. if(!pIns->nFadeOut)
  1549. _tcscpy(pszText, _T("Fade disabled"));
  1550. else
  1551. wsprintf(pszText, _T("%u ticks (Higher value <-> Faster fade out)"), 0x8000 / pIns->nFadeOut);
  1552. return TRUE;
  1553. case IDC_EDIT8:
  1554. // Global volume
  1555. if(isEnabled)
  1556. _tcscpy(pszText, CModDoc::LinearToDecibels(GetDlgItemInt(IDC_EDIT8), 64.0));
  1557. else
  1558. _tcscpy(pszText, _T("Only available in IT / MPTM format"));
  1559. return TRUE;
  1560. case IDC_EDIT9:
  1561. // Panning
  1562. if(isEnabled)
  1563. _tcscpy(pszText, CModDoc::PanningToString(pIns->nPan, 128));
  1564. else
  1565. _tcscpy(pszText, _T("Only available in IT / MPTM format"));
  1566. return TRUE;
  1567. #ifndef NO_PLUGINS
  1568. case IDC_EDIT10:
  1569. case IDC_EDIT11:
  1570. // Show plugin program name when hovering program or bank edits
  1571. if(pIns->nMixPlug > 0 && pIns->nMidiProgram != 0)
  1572. {
  1573. const SNDMIXPLUGIN &plugin = m_sndFile.m_MixPlugins[pIns->nMixPlug - 1];
  1574. if(plugin.pMixPlugin != nullptr)
  1575. {
  1576. int32 prog = pIns->nMidiProgram - 1;
  1577. if(pIns->wMidiBank > 1) prog += 128 * (pIns->wMidiBank - 1);
  1578. _tcscpy(pszText, plugin.pMixPlugin->GetFormattedProgramName(prog));
  1579. }
  1580. }
  1581. return TRUE;
  1582. #endif // NO_PLUGINS
  1583. case IDC_PLUGIN_VELOCITYSTYLE:
  1584. case IDC_PLUGIN_VOLUMESTYLE:
  1585. // Plugin volume handling
  1586. if(pIns->nMixPlug < 1) return FALSE;
  1587. if(m_sndFile.m_playBehaviour[kMIDICCBugEmulation])
  1588. {
  1589. velocityStyle.EnableWindow(FALSE);
  1590. m_CbnPluginVolumeHandling.EnableWindow(FALSE);
  1591. _tcscpy(pszText, _T("To enable, clear Plugin volume command bug emulation flag from Song Properties"));
  1592. return TRUE;
  1593. } else
  1594. {
  1595. if(uId == IDC_PLUGIN_VELOCITYSTYLE)
  1596. {
  1597. _tcscpy(pszText, _T("Volume commands (vxx) next to a note are sent as note velocity instead."));
  1598. return TRUE;
  1599. }
  1600. return FALSE;
  1601. }
  1602. case IDC_COMBO5:
  1603. // MIDI Channel
  1604. s = _T("Mapped: MIDI channel corresponds to pattern channel modulo 16");
  1605. break;
  1606. case IDC_SLIDER1:
  1607. if(isEnabled)
  1608. wsprintf(pszText, _T("%s%d%% volume variation"), plusMinus.c_str(), pIns->nVolSwing);
  1609. else
  1610. _tcscpy(pszText, _T("Only available in IT / MPTM format"));
  1611. return TRUE;
  1612. case IDC_SLIDER2:
  1613. if(isEnabled)
  1614. wsprintf(pszText, _T("%s%d panning variation"), plusMinus.c_str(), pIns->nPanSwing);
  1615. else
  1616. _tcscpy(pszText, _T("Only available in IT / MPTM format"));
  1617. return TRUE;
  1618. case IDC_SLIDER3:
  1619. if(isEnabled)
  1620. wsprintf(pszText, _T("%u"), pIns->GetCutoff());
  1621. else
  1622. _tcscpy(pszText, _T("Only available in IT / MPTM format"));
  1623. return TRUE;
  1624. case IDC_SLIDER4:
  1625. if(isEnabled)
  1626. wsprintf(pszText, _T("%u (%i dB)"), pIns->GetResonance(), Util::muldivr(pIns->GetResonance(), 24, 128));
  1627. else
  1628. _tcscpy(pszText, _T("Only available in IT / MPTM format"));
  1629. return TRUE;
  1630. case IDC_SLIDER6:
  1631. if(isEnabled)
  1632. wsprintf(pszText, _T("%s%d cutoff variation"), plusMinus.c_str(), pIns->nCutSwing);
  1633. else
  1634. _tcscpy(pszText, _T("Only available in MPTM format"));
  1635. return TRUE;
  1636. case IDC_SLIDER7:
  1637. if(isEnabled)
  1638. wsprintf(pszText, _T("%s%d resonance variation"), plusMinus.c_str(), pIns->nResSwing);
  1639. else
  1640. _tcscpy(pszText, _T("Only available in MPTM format"));
  1641. return TRUE;
  1642. case IDC_PITCHWHEELDEPTH:
  1643. s = _T("Set this to the actual Pitch Wheel Depth used in your plugin on this channel.");
  1644. break;
  1645. case IDC_INSVIEWPLG: // Open Editor
  1646. if(!isEnabled)
  1647. s = _T("No Plugin Loaded");
  1648. break;
  1649. case IDC_SPIN9: // Pan
  1650. case IDC_CHECK1: // Pan
  1651. case IDC_COMBO1: // NNA
  1652. case IDC_COMBO2: // DCT
  1653. case IDC_COMBO3: // DNA
  1654. case IDC_COMBO4: // PPC
  1655. case IDC_SPIN12: // PPS
  1656. case IDC_EDIT15: // PPS
  1657. if(!isEnabled)
  1658. s = _T("Only available in IT / MPTM format");
  1659. break;
  1660. case IDC_COMBOTUNING: // Tuning
  1661. case IDC_COMBO9: // Resampling:
  1662. case IDC_SLIDER5: // Ramping
  1663. case IDC_SPIN1: // Ramping
  1664. case IDC_EDIT2: // Ramping
  1665. if(!isEnabled)
  1666. s = _T("Only available in MPTM format");
  1667. break;
  1668. }
  1669. if(s != nullptr)
  1670. {
  1671. _tcscpy(pszText, s);
  1672. if(cmd != kcNull)
  1673. {
  1674. auto keyText = CMainFrame::GetInputHandler()->m_activeCommandSet->GetKeyTextFromCommand(cmd, 0);
  1675. if (!keyText.IsEmpty())
  1676. _tcscat(pszText, MPT_TFORMAT(" ({})")(keyText).c_str());
  1677. }
  1678. return TRUE;
  1679. }
  1680. }
  1681. return FALSE;
  1682. }
  1683. ////////////////////////////////////////////////////////////////////////////
  1684. // CCtrlInstruments Messages
  1685. void CCtrlInstruments::OnInstrumentChanged()
  1686. {
  1687. if(!IsLocked())
  1688. {
  1689. UINT n = GetDlgItemInt(IDC_EDIT_INSTRUMENT);
  1690. if ((n > 0) && (n <= m_sndFile.GetNumInstruments()) && (n != m_nInstrument))
  1691. {
  1692. SetCurrentInstrument(n, FALSE);
  1693. m_parent.InstrumentChanged(n);
  1694. }
  1695. }
  1696. }
  1697. void CCtrlInstruments::OnPrevInstrument()
  1698. {
  1699. if(m_nInstrument > 1)
  1700. SetCurrentInstrument(m_nInstrument - 1);
  1701. else
  1702. SetCurrentInstrument(m_sndFile.GetNumInstruments());
  1703. m_parent.InstrumentChanged(m_nInstrument);
  1704. }
  1705. void CCtrlInstruments::OnNextInstrument()
  1706. {
  1707. if(m_nInstrument < m_sndFile.GetNumInstruments())
  1708. SetCurrentInstrument(m_nInstrument + 1);
  1709. else
  1710. SetCurrentInstrument(1);
  1711. m_parent.InstrumentChanged(m_nInstrument);
  1712. }
  1713. void CCtrlInstruments::OnInstrumentNew()
  1714. {
  1715. InsertInstrument(m_sndFile.GetNumInstruments() > 0 && CMainFrame::GetInputHandler()->ShiftPressed());
  1716. SwitchToView();
  1717. }
  1718. bool CCtrlInstruments::InsertInstrument(bool duplicate)
  1719. {
  1720. const bool hasInstruments = m_sndFile.GetNumInstruments() > 0;
  1721. INSTRUMENTINDEX ins = m_modDoc.InsertInstrument(SAMPLEINDEX_INVALID, (duplicate && hasInstruments) ? m_nInstrument : INSTRUMENTINDEX_INVALID);
  1722. if (ins == INSTRUMENTINDEX_INVALID)
  1723. return false;
  1724. if (!hasInstruments) m_modDoc.UpdateAllViews(nullptr, InstrumentHint().Info().Names().ModType());
  1725. SetCurrentInstrument(ins);
  1726. m_modDoc.UpdateAllViews(nullptr, InstrumentHint(ins).Info().Envelope().Names());
  1727. m_parent.InstrumentChanged(m_nInstrument);
  1728. return true;
  1729. }
  1730. void CCtrlInstruments::OnInstrumentOpen()
  1731. {
  1732. static int nLastIndex = 0;
  1733. std::vector<FileType> mediaFoundationTypes = CSoundFile::GetMediaFoundationFileTypes();
  1734. FileDialog dlg = OpenFileDialog()
  1735. .AllowMultiSelect()
  1736. .EnableAudioPreview()
  1737. .ExtensionFilter(
  1738. "All Instruments (*.xi,*.pat,*.iti,*.sfz,...)|*.xi;*.pat;*.iti;*.sfz;*.flac;*.wav;*.w64;*.caf;*.aif;*.aiff;*.au;*.snd;*.sbk;*.sf2;*.sf3;*.sf4;*.dls;*.oga;*.ogg;*.opus;*.s3i;*.sb0;*.sb2;*.sbi;*.brr" + ToFilterOnlyString(mediaFoundationTypes, true).ToLocale() + "|"
  1739. "FastTracker II Instruments (*.xi)|*.xi|"
  1740. "GF1 Patches (*.pat)|*.pat|"
  1741. "Impulse Tracker Instruments (*.iti)|*.iti|"
  1742. "SFZ Instruments (*.sfz)|*.sfz|"
  1743. "SoundFont 2.0 Banks (*.sf2)|*.sbk;*.sf2;*.sf3;*.sf4|"
  1744. "DLS Sound Banks (*.dls)|*.dls|"
  1745. "All Files (*.*)|*.*||")
  1746. .WorkingDirectory(TrackerSettings::Instance().PathInstruments.GetWorkingDir())
  1747. .FilterIndex(&nLastIndex);
  1748. if(!dlg.Show(this)) return;
  1749. TrackerSettings::Instance().PathInstruments.SetWorkingDir(dlg.GetWorkingDirectory());
  1750. const FileDialog::PathList &files = dlg.GetFilenames();
  1751. for(size_t counter = 0; counter < files.size(); counter++)
  1752. {
  1753. //If loading multiple instruments, advancing to next instrument and creating
  1754. //new instrument if necessary.
  1755. if(counter > 0)
  1756. {
  1757. if(m_nInstrument >= MAX_INSTRUMENTS - 1)
  1758. break;
  1759. else
  1760. m_nInstrument++;
  1761. if(m_nInstrument > m_sndFile.GetNumInstruments())
  1762. OnInstrumentNew();
  1763. }
  1764. if(!OpenInstrument(files[counter]))
  1765. ErrorBox(IDS_ERR_FILEOPEN, this);
  1766. }
  1767. m_parent.InstrumentChanged(m_nInstrument);
  1768. SwitchToView();
  1769. }
  1770. void CCtrlInstruments::OnInstrumentSave()
  1771. {
  1772. SaveInstrument(CMainFrame::GetInputHandler()->ShiftPressed());
  1773. }
  1774. void CCtrlInstruments::SaveInstrument(bool doBatchSave)
  1775. {
  1776. if(!doBatchSave && m_sndFile.Instruments[m_nInstrument] == nullptr)
  1777. {
  1778. SwitchToView();
  1779. return;
  1780. }
  1781. mpt::PathString fileName;
  1782. if(!doBatchSave)
  1783. {
  1784. const ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  1785. if(pIns->filename[0])
  1786. fileName = mpt::PathString::FromLocale(pIns->filename);
  1787. else
  1788. fileName = mpt::PathString::FromLocale(pIns->name);
  1789. } else
  1790. {
  1791. // Save all samples
  1792. fileName = m_sndFile.GetpModDoc()->GetPathNameMpt().GetFileName();
  1793. if(fileName.empty()) fileName = P_("untitled");
  1794. fileName += P_(" - %instrument_number% - ");
  1795. if(m_sndFile.GetModSpecifications().sampleFilenameLengthMax == 0)
  1796. fileName += P_("%instrument_name%");
  1797. else
  1798. fileName += P_("%instrument_filename%");
  1799. }
  1800. SanitizeFilename(fileName);
  1801. int index;
  1802. if(TrackerSettings::Instance().compressITI)
  1803. index = 2;
  1804. else if(m_sndFile.GetType() == MOD_TYPE_XM)
  1805. index = 4;
  1806. else
  1807. index = 1;
  1808. FileDialog dlg = SaveFileDialog()
  1809. .DefaultExtension(m_sndFile.GetType() == MOD_TYPE_XM ? "xi" : "iti")
  1810. .DefaultFilename(fileName)
  1811. .ExtensionFilter(
  1812. "Impulse Tracker Instruments (*.iti)|*.iti|"
  1813. "Compressed Impulse Tracker Instruments (*.iti)|*.iti|"
  1814. "Impulse Tracker Instruments with external Samples (*.iti)|*.iti|"
  1815. "FastTracker II Instruments (*.xi)|*.xi|"
  1816. "SFZ Instruments with WAV (*.sfz)|*.sfz|"
  1817. "SFZ Instruments with FLAC (*.sfz)|*.sfz||")
  1818. .WorkingDirectory(TrackerSettings::Instance().PathInstruments.GetWorkingDir())
  1819. .FilterIndex(&index);
  1820. if(!dlg.Show(this)) return;
  1821. BeginWaitCursor();
  1822. INSTRUMENTINDEX minIns = m_nInstrument, maxIns = m_nInstrument;
  1823. if(doBatchSave)
  1824. {
  1825. minIns = 1;
  1826. maxIns = m_sndFile.GetNumInstruments();
  1827. }
  1828. auto numberFmt = mpt::FormatSpec().Dec().FillNul().Width(1 + static_cast<int>(std::log10(maxIns)));
  1829. CString instrName, instrFilename;
  1830. bool ok = true;
  1831. const bool saveXI = !mpt::PathString::CompareNoCase(dlg.GetExtension(), P_("xi"));
  1832. const bool saveSFZ = !mpt::PathString::CompareNoCase(dlg.GetExtension(), P_("sfz"));
  1833. const bool doCompress = index == 2 || index == 6;
  1834. const bool allowExternal = index == 3;
  1835. for(INSTRUMENTINDEX ins = minIns; ins <= maxIns; ins++)
  1836. {
  1837. const ModInstrument *pIns = m_sndFile.Instruments[ins];
  1838. if(pIns != nullptr)
  1839. {
  1840. fileName = dlg.GetFirstFile();
  1841. if(doBatchSave)
  1842. {
  1843. instrName = mpt::ToCString(m_sndFile.GetCharsetInternal(), pIns->name[0] ? pIns->GetName() : "untitled");
  1844. instrFilename = mpt::ToCString(m_sndFile.GetCharsetInternal(), pIns->filename[0] ? pIns->GetFilename() : pIns->GetName());
  1845. SanitizeFilename(instrName);
  1846. SanitizeFilename(instrFilename);
  1847. mpt::ustring fileNameW = fileName.ToUnicode();
  1848. fileNameW = mpt::String::Replace(fileNameW, U_("%instrument_number%"), mpt::ufmt::fmt(ins, numberFmt));
  1849. fileNameW = mpt::String::Replace(fileNameW, U_("%instrument_filename%"), mpt::ToUnicode(instrFilename));
  1850. fileNameW = mpt::String::Replace(fileNameW, U_("%instrument_name%"), mpt::ToUnicode(instrName));
  1851. fileName = mpt::PathString::FromUnicode(fileNameW);
  1852. }
  1853. try
  1854. {
  1855. ScopedLogCapturer logcapturer(m_modDoc);
  1856. mpt::SafeOutputFile sf(fileName, std::ios::binary, mpt::FlushModeFromBool(TrackerSettings::Instance().MiscFlushFileBuffersOnSave));
  1857. mpt::ofstream &f = sf;
  1858. if(!f)
  1859. {
  1860. ok = false;
  1861. continue;
  1862. }
  1863. f.exceptions(f.exceptions() | std::ios::badbit | std::ios::failbit);
  1864. if (saveXI)
  1865. ok &= m_sndFile.SaveXIInstrument(ins, f);
  1866. else if (saveSFZ)
  1867. ok &= m_sndFile.SaveSFZInstrument(ins, f, fileName, doCompress);
  1868. else
  1869. ok &= m_sndFile.SaveITIInstrument(ins, f, fileName, doCompress, allowExternal);
  1870. } catch(const std::exception &)
  1871. {
  1872. ok = false;
  1873. }
  1874. }
  1875. }
  1876. EndWaitCursor();
  1877. if (!ok)
  1878. ErrorBox(IDS_ERR_SAVEINS, this);
  1879. else
  1880. TrackerSettings::Instance().PathInstruments.SetWorkingDir(dlg.GetWorkingDirectory());
  1881. SwitchToView();
  1882. }
  1883. void CCtrlInstruments::OnInstrumentPlay()
  1884. {
  1885. if (m_modDoc.IsNotePlaying(NOTE_MIDDLEC, 0, m_nInstrument))
  1886. {
  1887. m_modDoc.NoteOff(NOTE_MIDDLEC, true, m_nInstrument);
  1888. } else
  1889. {
  1890. m_modDoc.PlayNote(PlayNoteParam(NOTE_MIDDLEC).Instrument(m_nInstrument));
  1891. }
  1892. SwitchToView();
  1893. }
  1894. void CCtrlInstruments::OnNameChanged()
  1895. {
  1896. if (!IsLocked())
  1897. {
  1898. CString tmp;
  1899. m_EditName.GetWindowText(tmp);
  1900. const std::string s = mpt::ToCharset(m_sndFile.GetCharsetInternal(), tmp);
  1901. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  1902. if ((pIns) && (s != pIns->name))
  1903. {
  1904. if(!m_startedEdit) PrepareUndo("Set Name");
  1905. pIns->name = s;
  1906. SetModified(InstrumentHint().Names(), false);
  1907. }
  1908. }
  1909. }
  1910. void CCtrlInstruments::OnFileNameChanged()
  1911. {
  1912. if (!IsLocked())
  1913. {
  1914. CString tmp;
  1915. m_EditFileName.GetWindowText(tmp);
  1916. const std::string s = mpt::ToCharset(m_sndFile.GetCharsetInternal(), tmp);
  1917. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  1918. if ((pIns) && (s != pIns->filename))
  1919. {
  1920. if(!m_startedEdit) PrepareUndo("Set Filename");
  1921. pIns->filename = s;
  1922. SetModified(InstrumentHint().Names(), false);
  1923. }
  1924. }
  1925. }
  1926. void CCtrlInstruments::OnFadeOutVolChanged()
  1927. {
  1928. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  1929. if ((!IsLocked()) && (pIns))
  1930. {
  1931. int minval = 0, maxval = 32767;
  1932. m_SpinFadeOut.GetRange(minval, maxval);
  1933. int nVol = GetDlgItemInt(IDC_EDIT7);
  1934. Limit(nVol, minval, maxval);
  1935. if(nVol != (int)pIns->nFadeOut)
  1936. {
  1937. if(!m_startedEdit) PrepareUndo("Set Fade Out");
  1938. pIns->nFadeOut = nVol;
  1939. SetModified(InstrumentHint().Info(), false);
  1940. }
  1941. }
  1942. }
  1943. void CCtrlInstruments::OnGlobalVolChanged()
  1944. {
  1945. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  1946. if ((!IsLocked()) && (pIns))
  1947. {
  1948. int nVol = GetDlgItemInt(IDC_EDIT8);
  1949. Limit(nVol, 0, 64);
  1950. if (nVol != (int)pIns->nGlobalVol)
  1951. {
  1952. if(!m_startedEdit) PrepareUndo("Set Global Volume");
  1953. // Live-adjust volume
  1954. pIns->nGlobalVol = nVol;
  1955. for(auto &chn : m_sndFile.m_PlayState.Chn)
  1956. {
  1957. if(chn.pModInstrument == pIns)
  1958. {
  1959. chn.UpdateInstrumentVolume(chn.pModSample, pIns);
  1960. }
  1961. }
  1962. SetModified(InstrumentHint().Info(), false);
  1963. }
  1964. }
  1965. }
  1966. void CCtrlInstruments::OnSetPanningChanged()
  1967. {
  1968. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  1969. if ((!IsLocked()) && (pIns))
  1970. {
  1971. const bool b = m_CheckPanning.GetCheck() != BST_UNCHECKED;
  1972. PrepareUndo("Toggle Panning");
  1973. pIns->dwFlags.set(INS_SETPANNING, b);
  1974. if(b && m_sndFile.GetType() & (MOD_TYPE_IT|MOD_TYPE_MPT))
  1975. {
  1976. bool smpPanningInUse = false;
  1977. const std::set<SAMPLEINDEX> referencedSamples = pIns->GetSamples();
  1978. for(auto sample : referencedSamples)
  1979. {
  1980. if(sample <= m_sndFile.GetNumSamples() && m_sndFile.GetSample(sample).uFlags[CHN_PANNING])
  1981. {
  1982. smpPanningInUse = true;
  1983. break;
  1984. }
  1985. }
  1986. if(smpPanningInUse)
  1987. {
  1988. if(Reporting::Confirm(_T("Some of the samples used in the instrument have \"Set Pan\" enabled. "
  1989. "Sample panning overrides instrument panning for the notes associated with such samples. "
  1990. "Do you wish to disable panning from those samples so that the instrument pan setting is effective "
  1991. "for the whole instrument?")) == cnfYes)
  1992. {
  1993. for (auto sample : referencedSamples)
  1994. {
  1995. if(sample <= m_sndFile.GetNumSamples())
  1996. m_sndFile.GetSample(sample).uFlags.reset(CHN_PANNING);
  1997. }
  1998. m_modDoc.UpdateAllViews(nullptr, SampleHint().Info().ModType(), this);
  1999. }
  2000. }
  2001. }
  2002. SetModified(InstrumentHint().Info(), false);
  2003. }
  2004. }
  2005. void CCtrlInstruments::OnPanningChanged()
  2006. {
  2007. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2008. if ((!IsLocked()) && (pIns))
  2009. {
  2010. int nPan = GetDlgItemInt(IDC_EDIT9);
  2011. if(m_modDoc.GetModType() & MOD_TYPE_IT) // IT panning ranges from 0 to 64
  2012. nPan *= 4;
  2013. if (nPan < 0) nPan = 0;
  2014. if (nPan > 256) nPan = 256;
  2015. if (nPan != (int)pIns->nPan)
  2016. {
  2017. if(!m_startedEdit) PrepareUndo("Set Panning");
  2018. pIns->nPan = nPan;
  2019. SetModified(InstrumentHint().Info(), false);
  2020. }
  2021. }
  2022. }
  2023. void CCtrlInstruments::OnNNAChanged()
  2024. {
  2025. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2026. if ((!IsLocked()) && (pIns))
  2027. {
  2028. const auto nna = static_cast<NewNoteAction>(m_ComboNNA.GetCurSel());
  2029. if(pIns->nNNA != nna)
  2030. {
  2031. PrepareUndo("Set New Note Action");
  2032. pIns->nNNA = nna;
  2033. SetModified(InstrumentHint().Info(), false);
  2034. }
  2035. }
  2036. }
  2037. void CCtrlInstruments::OnDCTChanged()
  2038. {
  2039. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2040. if ((!IsLocked()) && (pIns))
  2041. {
  2042. const auto dct = static_cast<DuplicateCheckType>(m_ComboDCT.GetCurSel());
  2043. if(pIns->nDCT != dct)
  2044. {
  2045. PrepareUndo("Set Duplicate Check Type");
  2046. pIns->nDCT = dct;
  2047. SetModified(InstrumentHint().Info(), false);
  2048. }
  2049. }
  2050. }
  2051. void CCtrlInstruments::OnDCAChanged()
  2052. {
  2053. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2054. if ((!IsLocked()) && (pIns))
  2055. {
  2056. const auto dna = static_cast<DuplicateNoteAction>(m_ComboDCA.GetCurSel());
  2057. if (pIns->nDNA != dna)
  2058. {
  2059. PrepareUndo("Set Duplicate Check Action");
  2060. pIns->nDNA = dna;
  2061. SetModified(InstrumentHint().Info(), false);
  2062. }
  2063. }
  2064. }
  2065. void CCtrlInstruments::OnMPRChanged()
  2066. {
  2067. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2068. if ((!IsLocked()) && (pIns))
  2069. {
  2070. int n = GetDlgItemInt(IDC_EDIT10);
  2071. if ((n >= 0) && (n <= 128))
  2072. {
  2073. if (pIns->nMidiProgram != n)
  2074. {
  2075. if(!m_startedEdit) PrepareUndo("Set MIDI Program");
  2076. pIns->nMidiProgram = static_cast<uint8>(n);
  2077. SetModified(InstrumentHint().Info(), false);
  2078. }
  2079. }
  2080. // we will not set the midi bank/program if it is 0
  2081. if(n == 0)
  2082. {
  2083. LockControls();
  2084. SetDlgItemText(IDC_EDIT10, _T("---"));
  2085. UnlockControls();
  2086. }
  2087. }
  2088. }
  2089. void CCtrlInstruments::OnMPRKillFocus()
  2090. {
  2091. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2092. if ((!IsLocked()) && (pIns))
  2093. {
  2094. int n = GetDlgItemInt(IDC_EDIT10);
  2095. if (n > 128)
  2096. {
  2097. n--;
  2098. pIns->nMidiProgram = static_cast<uint8>(n % 128 + 1);
  2099. pIns->wMidiBank = static_cast<uint16>(n / 128 + 1);
  2100. SetModified(InstrumentHint().Info(), false);
  2101. LockControls();
  2102. SetDlgItemInt(IDC_EDIT10, pIns->nMidiProgram);
  2103. SetDlgItemInt(IDC_EDIT11, pIns->wMidiBank);
  2104. UnlockControls();
  2105. }
  2106. }
  2107. }
  2108. void CCtrlInstruments::OnMBKChanged()
  2109. {
  2110. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2111. if ((!IsLocked()) && (pIns))
  2112. {
  2113. uint16 w = static_cast<uint16>(GetDlgItemInt(IDC_EDIT11));
  2114. if(w >= 0 && w <= 16384 && pIns->wMidiBank != w)
  2115. {
  2116. if(!m_startedEdit) PrepareUndo("Set MIDI Bank");
  2117. pIns->wMidiBank = w;
  2118. SetModified(InstrumentHint().Info(), false);
  2119. }
  2120. // we will not set the midi bank/program if it is 0
  2121. if(w == 0)
  2122. {
  2123. LockControls();
  2124. SetDlgItemText(IDC_EDIT11, _T("---"));
  2125. UnlockControls();
  2126. }
  2127. }
  2128. }
  2129. void CCtrlInstruments::OnMCHChanged()
  2130. {
  2131. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2132. if(!IsLocked() && pIns)
  2133. {
  2134. uint8 ch = static_cast<uint8>(m_CbnMidiCh.GetItemData(m_CbnMidiCh.GetCurSel()));
  2135. if(pIns->nMidiChannel != ch)
  2136. {
  2137. PrepareUndo("Set MIDI Channel");
  2138. pIns->nMidiChannel = ch;
  2139. SetModified(InstrumentHint().Info(), false);
  2140. }
  2141. }
  2142. }
  2143. void CCtrlInstruments::OnResamplingChanged()
  2144. {
  2145. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2146. if ((!IsLocked()) && (pIns))
  2147. {
  2148. ResamplingMode n = static_cast<ResamplingMode>(m_CbnResampling.GetItemData(m_CbnResampling.GetCurSel()));
  2149. if (pIns->resampling != n)
  2150. {
  2151. PrepareUndo("Set Resampling");
  2152. pIns->resampling = n;
  2153. SetModified(InstrumentHint().Info(), false);
  2154. }
  2155. }
  2156. }
  2157. void CCtrlInstruments::OnMixPlugChanged()
  2158. {
  2159. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2160. PLUGINDEX nPlug = static_cast<PLUGINDEX>(m_CbnMixPlug.GetItemData(m_CbnMixPlug.GetCurSel()));
  2161. bool wasOpenedWithMouse = m_openendPluginListWithMouse;
  2162. m_openendPluginListWithMouse = false;
  2163. if (pIns)
  2164. {
  2165. BOOL enableVol = (nPlug < 1 || m_sndFile.m_playBehaviour[kMIDICCBugEmulation]) ? FALSE : TRUE;
  2166. velocityStyle.EnableWindow(enableVol);
  2167. m_CbnPluginVolumeHandling.EnableWindow(enableVol);
  2168. if(nPlug >= 0 && nPlug <= MAX_MIXPLUGINS)
  2169. {
  2170. bool active = !IsLocked();
  2171. if (active && pIns->nMixPlug != nPlug)
  2172. {
  2173. PrepareUndo("Set Plugin");
  2174. pIns->nMixPlug = nPlug;
  2175. SetModified(InstrumentHint().Info(), false);
  2176. }
  2177. velocityStyle.SetCheck(pIns->pluginVelocityHandling == PLUGIN_VELOCITYHANDLING_CHANNEL ? BST_CHECKED : BST_UNCHECKED);
  2178. m_CbnPluginVolumeHandling.SetCurSel(pIns->pluginVolumeHandling);
  2179. #ifndef NO_PLUGINS
  2180. if(pIns->nMixPlug)
  2181. {
  2182. // we have selected a plugin that's not "no plugin"
  2183. const SNDMIXPLUGIN &plugin = m_sndFile.m_MixPlugins[pIns->nMixPlug - 1];
  2184. if(!plugin.IsValidPlugin() && active && wasOpenedWithMouse)
  2185. {
  2186. // No plugin in this slot yet: Ask user to add one.
  2187. CSelectPluginDlg dlg(&m_modDoc, nPlug - 1, this);
  2188. if (dlg.DoModal() == IDOK)
  2189. {
  2190. if(m_sndFile.GetModSpecifications().supportsPlugins)
  2191. {
  2192. m_modDoc.SetModified();
  2193. }
  2194. UpdatePluginList();
  2195. m_modDoc.UpdateAllViews(nullptr, PluginHint(nPlug).Info().Names());
  2196. }
  2197. }
  2198. if(plugin.pMixPlugin != nullptr)
  2199. {
  2200. GetDlgItem(IDC_INSVIEWPLG)->EnableWindow(true);
  2201. if(active && plugin.pMixPlugin->IsInstrument())
  2202. {
  2203. if(pIns->nMidiChannel == MidiNoChannel)
  2204. {
  2205. // If this plugin can recieve MIDI events and we have no MIDI channel
  2206. // selected for this instrument, automatically select MIDI channel 1.
  2207. pIns->nMidiChannel = MidiFirstChannel;
  2208. UpdateView(InstrumentHint(m_nInstrument).Info());
  2209. }
  2210. if(pIns->midiPWD == 0)
  2211. {
  2212. pIns->midiPWD = 2;
  2213. }
  2214. // If we just dialled up an instrument plugin, zap the sample assignments.
  2215. const std::set<SAMPLEINDEX> referencedSamples = pIns->GetSamples();
  2216. bool hasSamples = false;
  2217. for(auto sample : referencedSamples)
  2218. {
  2219. if(sample > 0 && sample <= m_sndFile.GetNumSamples() && m_sndFile.GetSample(sample).HasSampleData())
  2220. {
  2221. hasSamples = true;
  2222. break;
  2223. }
  2224. }
  2225. if(!hasSamples || Reporting::Confirm("Remove sample associations of this instrument?") == cnfYes)
  2226. {
  2227. pIns->AssignSample(0);
  2228. m_NoteMap.Invalidate();
  2229. }
  2230. }
  2231. return;
  2232. }
  2233. }
  2234. #endif // NO_PLUGINS
  2235. }
  2236. }
  2237. ::EnableWindow(::GetDlgItem(m_hWnd, IDC_INSVIEWPLG), false);
  2238. }
  2239. void CCtrlInstruments::OnPPSChanged()
  2240. {
  2241. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2242. if ((!IsLocked()) && (pIns))
  2243. {
  2244. int n = GetDlgItemInt(IDC_EDIT15);
  2245. if ((n >= -32) && (n <= 32))
  2246. {
  2247. if (pIns->nPPS != (signed char)n)
  2248. {
  2249. if(!m_startedEdit) PrepareUndo("Set Pitch/Pan Separation");
  2250. pIns->nPPS = (signed char)n;
  2251. SetModified(InstrumentHint().Info(), false);
  2252. }
  2253. }
  2254. }
  2255. }
  2256. void CCtrlInstruments::OnPPCChanged()
  2257. {
  2258. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2259. if ((!IsLocked()) && (pIns))
  2260. {
  2261. int n = m_ComboPPC.GetCurSel();
  2262. if(n >= 0 && n <= NOTE_MAX - NOTE_MIN)
  2263. {
  2264. if (pIns->nPPC != n)
  2265. {
  2266. PrepareUndo("Set Pitch/Pan Center");
  2267. pIns->nPPC = static_cast<decltype(pIns->nPPC)>(n);
  2268. SetModified(InstrumentHint().Info(), false);
  2269. }
  2270. }
  2271. }
  2272. }
  2273. void CCtrlInstruments::OnAttackChanged()
  2274. {
  2275. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2276. if(!IsLocked() && pIns)
  2277. {
  2278. int n = Clamp(static_cast<int>(GetDlgItemInt(IDC_EDIT2)), 0, MAX_ATTACK_VALUE);
  2279. auto newRamp = static_cast<decltype(pIns->nVolRampUp)>(n);
  2280. if(pIns->nVolRampUp != newRamp)
  2281. {
  2282. if(!m_startedEdit)
  2283. PrepareUndo("Set Ramping");
  2284. pIns->nVolRampUp = newRamp;
  2285. SetModified(InstrumentHint().Info(), false);
  2286. }
  2287. m_SliderAttack.SetPos(n);
  2288. if(CSpinButtonCtrl *spin = (CSpinButtonCtrl *)GetDlgItem(IDC_SPIN1))
  2289. spin->SetPos(n);
  2290. LockControls();
  2291. if (n == 0) SetDlgItemText(IDC_EDIT2, _T("default"));
  2292. UnlockControls();
  2293. }
  2294. }
  2295. void CCtrlInstruments::OnEnableCutOff()
  2296. {
  2297. const bool enableCutOff = IsDlgButtonChecked(IDC_CHECK2) != BST_UNCHECKED;
  2298. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2299. if (pIns)
  2300. {
  2301. PrepareUndo("Toggle Cutoff");
  2302. pIns->SetCutoff(pIns->GetCutoff(), enableCutOff);
  2303. for(auto &chn : m_sndFile.m_PlayState.Chn)
  2304. {
  2305. if (chn.pModInstrument == pIns)
  2306. {
  2307. if(enableCutOff)
  2308. chn.nCutOff = pIns->GetCutoff();
  2309. else
  2310. chn.nCutOff = 0x7F;
  2311. }
  2312. }
  2313. }
  2314. UpdateFilterText();
  2315. SetModified(InstrumentHint().Info(), false);
  2316. SwitchToView();
  2317. }
  2318. void CCtrlInstruments::OnEnableResonance()
  2319. {
  2320. const bool enableReso = IsDlgButtonChecked(IDC_CHECK3) != BST_UNCHECKED;
  2321. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2322. if (pIns)
  2323. {
  2324. PrepareUndo("Toggle Resonance");
  2325. pIns->SetResonance(pIns->GetResonance(), enableReso);
  2326. for(auto &chn : m_sndFile.m_PlayState.Chn)
  2327. {
  2328. if (chn.pModInstrument == pIns)
  2329. {
  2330. if (enableReso)
  2331. chn.nResonance = pIns->GetResonance();
  2332. else
  2333. chn.nResonance = 0;
  2334. }
  2335. }
  2336. }
  2337. UpdateFilterText();
  2338. SetModified(InstrumentHint().Info(), false);
  2339. SwitchToView();
  2340. }
  2341. void CCtrlInstruments::OnFilterModeChanged()
  2342. {
  2343. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2344. if ((!IsLocked()) && (pIns))
  2345. {
  2346. FilterMode instFiltermode = static_cast<FilterMode>(m_CbnFilterMode.GetItemData(m_CbnFilterMode.GetCurSel()));
  2347. if(pIns->filterMode != instFiltermode)
  2348. {
  2349. PrepareUndo("Set Filter Mode");
  2350. pIns->filterMode = instFiltermode;
  2351. SetModified(InstrumentHint().Info(), false);
  2352. //Update channel settings where this instrument is active, if required.
  2353. if(instFiltermode != FilterMode::Unchanged)
  2354. {
  2355. for(auto &chn : m_sndFile.m_PlayState.Chn)
  2356. {
  2357. if(chn.pModInstrument == pIns)
  2358. chn.nFilterMode = instFiltermode;
  2359. }
  2360. }
  2361. }
  2362. }
  2363. }
  2364. void CCtrlInstruments::OnVScroll(UINT nCode, UINT nPos, CScrollBar *pSB)
  2365. {
  2366. // Give focus back to envelope editor when stopping to scroll spin buttons (for instrument preview keyboard focus)
  2367. CModControlDlg::OnVScroll(nCode, nPos, pSB);
  2368. if (nCode == SB_ENDSCROLL) SwitchToView();
  2369. }
  2370. void CCtrlInstruments::OnHScroll(UINT nCode, UINT nPos, CScrollBar *pSB)
  2371. {
  2372. CModControlDlg::OnHScroll(nCode, nPos, pSB);
  2373. if ((m_nInstrument) && (!IsLocked()) && (nCode != SB_ENDSCROLL))
  2374. {
  2375. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2376. if(!pIns)
  2377. return;
  2378. auto *pSlider = reinterpret_cast<const CSliderCtrl *>(pSB);
  2379. int32 n = pSlider->GetPos();
  2380. bool filterChanged = false;
  2381. if(pSlider == &m_SliderAttack)
  2382. {
  2383. // Volume ramping (attack)
  2384. if(pIns->nVolRampUp != n)
  2385. {
  2386. if(!m_startedHScroll)
  2387. {
  2388. PrepareUndo("Set Ramping");
  2389. m_startedHScroll = true;
  2390. }
  2391. pIns->nVolRampUp = static_cast<decltype(pIns->nVolRampUp)>(n);
  2392. SetDlgItemInt(IDC_EDIT2, n);
  2393. SetModified(InstrumentHint().Info(), false);
  2394. }
  2395. } else if(pSlider == &m_SliderVolSwing)
  2396. {
  2397. // Volume Swing
  2398. if((n >= 0) && (n <= 100) && (n != pIns->nVolSwing))
  2399. {
  2400. if(!m_startedHScroll)
  2401. {
  2402. PrepareUndo("Set Volume Random Variation");
  2403. m_startedHScroll = true;
  2404. }
  2405. pIns->nVolSwing = static_cast<uint8>(n);
  2406. SetModified(InstrumentHint().Info(), false);
  2407. }
  2408. } else if(pSlider == &m_SliderPanSwing)
  2409. {
  2410. // Pan Swing
  2411. if((n >= 0) && (n <= 64) && (n != pIns->nPanSwing))
  2412. {
  2413. if(!m_startedHScroll)
  2414. {
  2415. PrepareUndo("Set Panning Random Variation");
  2416. m_startedHScroll = true;
  2417. }
  2418. pIns->nPanSwing = static_cast<uint8>(n);
  2419. SetModified(InstrumentHint().Info(), false);
  2420. }
  2421. } else if(pSlider == &m_SliderCutSwing)
  2422. {
  2423. // Cutoff swing
  2424. if((n >= 0) && (n <= 64) && (n != pIns->nCutSwing))
  2425. {
  2426. if(!m_startedHScroll)
  2427. {
  2428. PrepareUndo("Set Cutoff Random Variation");
  2429. m_startedHScroll = true;
  2430. }
  2431. pIns->nCutSwing = static_cast<uint8>(n);
  2432. SetModified(InstrumentHint().Info(), false);
  2433. }
  2434. } else if(pSlider == &m_SliderResSwing)
  2435. {
  2436. // Resonance swing
  2437. if((n >= 0) && (n <= 64) && (n != pIns->nResSwing))
  2438. {
  2439. if(!m_startedHScroll)
  2440. {
  2441. PrepareUndo("Set Resonance Random Variation");
  2442. m_startedHScroll = true;
  2443. }
  2444. pIns->nResSwing = static_cast<uint8>(n);
  2445. SetModified(InstrumentHint().Info(), false);
  2446. }
  2447. } else if(pSlider == &m_SliderCutOff)
  2448. {
  2449. // Filter Cutoff
  2450. if((n >= 0) && (n < 0x80) && (n != (int)(pIns->GetCutoff())))
  2451. {
  2452. if(!m_startedHScroll)
  2453. {
  2454. PrepareUndo("Set Cutoff");
  2455. m_startedHScroll = true;
  2456. }
  2457. pIns->SetCutoff(static_cast<uint8>(n), pIns->IsCutoffEnabled());
  2458. SetModified(InstrumentHint().Info(), false);
  2459. UpdateFilterText();
  2460. filterChanged = true;
  2461. }
  2462. } else if(pSlider == &m_SliderResonance)
  2463. {
  2464. // Filter Resonance
  2465. if((n >= 0) && (n < 0x80) && (n != (int)(pIns->GetResonance())))
  2466. {
  2467. if(!m_startedHScroll)
  2468. {
  2469. PrepareUndo("Set Resonance");
  2470. m_startedHScroll = true;
  2471. }
  2472. pIns->SetResonance(static_cast<uint8>(n), pIns->IsResonanceEnabled());
  2473. SetModified(InstrumentHint().Info(), false);
  2474. UpdateFilterText();
  2475. filterChanged = true;
  2476. }
  2477. }
  2478. // Update channels
  2479. if(filterChanged)
  2480. {
  2481. for(auto &chn : m_sndFile.m_PlayState.Chn)
  2482. {
  2483. if(chn.pModInstrument == pIns)
  2484. {
  2485. if(pIns->IsCutoffEnabled())
  2486. chn.nCutOff = pIns->GetCutoff();
  2487. if(pIns->IsResonanceEnabled())
  2488. chn.nResonance = pIns->GetResonance();
  2489. }
  2490. }
  2491. }
  2492. } else if(nCode == SB_ENDSCROLL)
  2493. {
  2494. m_startedHScroll = false;
  2495. }
  2496. if ((nCode == SB_ENDSCROLL) || (nCode == SB_THUMBPOSITION))
  2497. {
  2498. SwitchToView();
  2499. }
  2500. }
  2501. void CCtrlInstruments::OnEditSampleMap()
  2502. {
  2503. if(m_nInstrument)
  2504. {
  2505. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2506. if (pIns)
  2507. {
  2508. PrepareUndo("Edit Sample Map");
  2509. CSampleMapDlg dlg(m_sndFile, m_nInstrument, this);
  2510. if (dlg.DoModal() == IDOK)
  2511. {
  2512. SetModified(InstrumentHint().Info(), true);
  2513. m_NoteMap.Invalidate(FALSE);
  2514. } else
  2515. {
  2516. m_modDoc.GetInstrumentUndo().RemoveLastUndoStep(m_nInstrument);
  2517. }
  2518. }
  2519. }
  2520. }
  2521. void CCtrlInstruments::TogglePluginEditor()
  2522. {
  2523. if(m_nInstrument)
  2524. {
  2525. m_modDoc.TogglePluginEditor(static_cast<PLUGINDEX>(m_CbnMixPlug.GetItemData(m_CbnMixPlug.GetCurSel()) - 1), CMainFrame::GetInputHandler()->ShiftPressed());
  2526. }
  2527. }
  2528. BOOL CCtrlInstruments::PreTranslateMessage(MSG *pMsg)
  2529. {
  2530. if(pMsg)
  2531. {
  2532. //We handle keypresses before Windows has a chance to handle them (for alt etc..)
  2533. if ((pMsg->message == WM_SYSKEYUP) || (pMsg->message == WM_KEYUP) ||
  2534. (pMsg->message == WM_SYSKEYDOWN) || (pMsg->message == WM_KEYDOWN))
  2535. {
  2536. CInputHandler* ih = CMainFrame::GetInputHandler();
  2537. //Translate message manually
  2538. UINT nChar = static_cast<UINT>(pMsg->wParam);
  2539. UINT nRepCnt = LOWORD(pMsg->lParam);
  2540. UINT nFlags = HIWORD(pMsg->lParam);
  2541. KeyEventType kT = ih->GetKeyEventType(nFlags);
  2542. InputTargetContext ctx = (InputTargetContext)(kCtxCtrlInstruments);
  2543. if (ih->KeyEvent(ctx, nChar, nRepCnt, nFlags, kT) != kcNull)
  2544. return true; // Mapped to a command, no need to pass message on.
  2545. }
  2546. }
  2547. return CModControlDlg::PreTranslateMessage(pMsg);
  2548. }
  2549. LRESULT CCtrlInstruments::OnCustomKeyMsg(WPARAM wParam, LPARAM /*lParam*/)
  2550. {
  2551. switch(wParam)
  2552. {
  2553. case kcInstrumentCtrlLoad: OnInstrumentOpen(); return wParam;
  2554. case kcInstrumentCtrlSave: OnInstrumentSaveOne(); return wParam;
  2555. case kcInstrumentCtrlNew: InsertInstrument(false); return wParam;
  2556. case kcInstrumentCtrlDuplicate: InsertInstrument(true); return wParam;
  2557. }
  2558. return kcNull;
  2559. }
  2560. void CCtrlInstruments::OnCbnSelchangeCombotuning()
  2561. {
  2562. if (IsLocked()) return;
  2563. ModInstrument *instr = m_sndFile.Instruments[m_nInstrument];
  2564. if(instr == nullptr)
  2565. return;
  2566. size_t sel = m_ComboTuning.GetCurSel();
  2567. if(sel == 0) //Setting IT behavior
  2568. {
  2569. CriticalSection cs;
  2570. PrepareUndo("Reset Tuning");
  2571. instr->SetTuning(nullptr);
  2572. cs.Leave();
  2573. SetModified(InstrumentHint().Info(), true);
  2574. return;
  2575. }
  2576. sel -= 1;
  2577. if(sel < m_sndFile.GetTuneSpecificTunings().GetNumTunings())
  2578. {
  2579. CriticalSection cs;
  2580. PrepareUndo("Set Tuning");
  2581. instr->SetTuning(m_sndFile.GetTuneSpecificTunings().GetTuning(sel));
  2582. cs.Leave();
  2583. SetModified(InstrumentHint().Info(), true);
  2584. return;
  2585. }
  2586. //Case: Chosen tuning editor to be displayed.
  2587. //Creating vector for the CTuningDialog.
  2588. CTuningDialog td(this, m_nInstrument, m_sndFile);
  2589. td.DoModal();
  2590. if(td.GetModifiedStatus(&m_sndFile.GetTuneSpecificTunings()))
  2591. {
  2592. m_modDoc.SetModified();
  2593. }
  2594. //Recreating tuning combobox so that possible
  2595. //new tuning(s) come visible.
  2596. BuildTuningComboBox();
  2597. m_modDoc.UpdateAllViews(nullptr, GeneralHint().Tunings());
  2598. m_modDoc.UpdateAllViews(nullptr, InstrumentHint().Info());
  2599. }
  2600. void CCtrlInstruments::UpdateTuningComboBox()
  2601. {
  2602. if(m_nInstrument > m_sndFile.GetNumInstruments()
  2603. || m_sndFile.Instruments[m_nInstrument] == nullptr) return;
  2604. ModInstrument* const pIns = m_sndFile.Instruments[m_nInstrument];
  2605. if(pIns->pTuning == nullptr)
  2606. {
  2607. m_ComboTuning.SetCurSel(0);
  2608. return;
  2609. }
  2610. for(size_t i = 0; i < m_sndFile.GetTuneSpecificTunings().GetNumTunings(); i++)
  2611. {
  2612. if(pIns->pTuning == m_sndFile.GetTuneSpecificTunings().GetTuning(i))
  2613. {
  2614. m_ComboTuning.SetCurSel((int)(i + 1));
  2615. return;
  2616. }
  2617. }
  2618. Reporting::Notification(MPT_CFORMAT("Tuning {} was not found. Setting to default tuning.")(mpt::ToCString(m_sndFile.Instruments[m_nInstrument]->pTuning->GetName())));
  2619. CriticalSection cs;
  2620. pIns->SetTuning(m_sndFile.GetDefaultTuning());
  2621. m_modDoc.SetModified();
  2622. }
  2623. void CCtrlInstruments::OnPluginVelocityHandlingChanged()
  2624. {
  2625. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2626. if(!IsLocked() && pIns != nullptr)
  2627. {
  2628. PlugVelocityHandling n = velocityStyle.GetCheck() != BST_UNCHECKED ? PLUGIN_VELOCITYHANDLING_CHANNEL : PLUGIN_VELOCITYHANDLING_VOLUME;
  2629. if(n != pIns->pluginVelocityHandling)
  2630. {
  2631. PrepareUndo("Set Velocity Handling");
  2632. if(n == PLUGIN_VELOCITYHANDLING_VOLUME && m_CbnPluginVolumeHandling.GetCurSel() == PLUGIN_VOLUMEHANDLING_IGNORE)
  2633. {
  2634. // This combination doesn't make sense.
  2635. m_CbnPluginVolumeHandling.SetCurSel(PLUGIN_VOLUMEHANDLING_MIDI);
  2636. }
  2637. pIns->pluginVelocityHandling = n;
  2638. SetModified(InstrumentHint().Info(), false);
  2639. }
  2640. }
  2641. }
  2642. void CCtrlInstruments::OnPluginVolumeHandlingChanged()
  2643. {
  2644. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2645. if(!IsLocked() && pIns != nullptr)
  2646. {
  2647. PlugVolumeHandling n = static_cast<PlugVolumeHandling>(m_CbnPluginVolumeHandling.GetCurSel());
  2648. if(n != pIns->pluginVolumeHandling)
  2649. {
  2650. PrepareUndo("Set Volume Handling");
  2651. if(velocityStyle.GetCheck() == BST_UNCHECKED && n == PLUGIN_VOLUMEHANDLING_IGNORE)
  2652. {
  2653. // This combination doesn't make sense.
  2654. velocityStyle.SetCheck(BST_CHECKED);
  2655. }
  2656. pIns->pluginVolumeHandling = n;
  2657. SetModified(InstrumentHint().Info(), false);
  2658. }
  2659. }
  2660. }
  2661. void CCtrlInstruments::OnPitchWheelDepthChanged()
  2662. {
  2663. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2664. if(!IsLocked() && pIns != nullptr)
  2665. {
  2666. int pwd = GetDlgItemInt(IDC_PITCHWHEELDEPTH, NULL, TRUE);
  2667. int lower = -128, upper = 127;
  2668. m_SpinPWD.GetRange32(lower, upper);
  2669. Limit(pwd, lower, upper);
  2670. if(pwd != pIns->midiPWD)
  2671. {
  2672. if(!m_startedEdit) PrepareUndo("Set Pitch Wheel Depth");
  2673. pIns->midiPWD = static_cast<int8>(pwd);
  2674. SetModified(InstrumentHint().Info(), false);
  2675. }
  2676. }
  2677. }
  2678. void CCtrlInstruments::OnBnClickedCheckPitchtempolock()
  2679. {
  2680. if(IsLocked() || !m_nInstrument) return;
  2681. INSTRUMENTINDEX firstIns = m_nInstrument, lastIns = m_nInstrument;
  2682. if(CMainFrame::GetInputHandler()->ShiftPressed())
  2683. {
  2684. firstIns = 1;
  2685. lastIns = m_sndFile.GetNumInstruments();
  2686. }
  2687. m_EditPitchTempoLock.EnableWindow(IsDlgButtonChecked(IDC_CHECK_PITCHTEMPOLOCK));
  2688. TEMPO ptl(0, 0);
  2689. bool isZero = false;
  2690. if(IsDlgButtonChecked(IDC_CHECK_PITCHTEMPOLOCK))
  2691. {
  2692. //Checking what value to put for the wPitchToTempoLock.
  2693. if(m_EditPitchTempoLock.GetWindowTextLength() > 0)
  2694. {
  2695. ptl = m_EditPitchTempoLock.GetTempoValue();
  2696. }
  2697. if(!ptl.GetRaw())
  2698. {
  2699. ptl = m_sndFile.m_nDefaultTempo;
  2700. }
  2701. m_EditPitchTempoLock.SetTempoValue(ptl);
  2702. isZero = true;
  2703. }
  2704. for(INSTRUMENTINDEX i = firstIns; i <= lastIns; i++)
  2705. {
  2706. if(m_sndFile.Instruments[i] != nullptr && (m_sndFile.Instruments[i]->pitchToTempoLock.GetRaw() == 0) == isZero)
  2707. {
  2708. m_modDoc.GetInstrumentUndo().PrepareUndo(i, "Set Pitch/Tempo Lock");
  2709. m_sndFile.Instruments[i]->pitchToTempoLock = ptl;
  2710. m_modDoc.SetModified();
  2711. }
  2712. }
  2713. m_modDoc.UpdateAllViews(nullptr, InstrumentHint().Info(), this);
  2714. }
  2715. void CCtrlInstruments::OnEnChangeEditPitchTempoLock()
  2716. {
  2717. if(IsLocked() || !m_nInstrument || !m_sndFile.Instruments[m_nInstrument]) return;
  2718. TEMPO ptlTempo = m_EditPitchTempoLock.GetTempoValue();
  2719. Limit(ptlTempo, m_sndFile.GetModSpecifications().GetTempoMin(), m_sndFile.GetModSpecifications().GetTempoMax());
  2720. if(m_sndFile.Instruments[m_nInstrument]->pitchToTempoLock != ptlTempo)
  2721. {
  2722. if(!m_startedEdit) PrepareUndo("Set Pitch/Tempo Lock");
  2723. m_sndFile.Instruments[m_nInstrument]->pitchToTempoLock = ptlTempo;
  2724. m_modDoc.SetModified(); // Only update other views after killing focus
  2725. }
  2726. }
  2727. void CCtrlInstruments::OnEnKillFocusEditPitchTempoLock()
  2728. {
  2729. //Checking that tempo value is in correct range.
  2730. if(IsLocked()) return;
  2731. TEMPO ptlTempo = m_EditPitchTempoLock.GetTempoValue();
  2732. bool changed = false;
  2733. const CModSpecifications& specs = m_sndFile.GetModSpecifications();
  2734. if(ptlTempo < specs.GetTempoMin())
  2735. {
  2736. ptlTempo = specs.GetTempoMin();
  2737. changed = true;
  2738. } else if(ptlTempo > specs.GetTempoMax())
  2739. {
  2740. ptlTempo = specs.GetTempoMax();
  2741. changed = true;
  2742. }
  2743. if(changed)
  2744. {
  2745. m_EditPitchTempoLock.SetTempoValue(ptlTempo);
  2746. m_modDoc.SetModified();
  2747. }
  2748. m_modDoc.UpdateAllViews(nullptr, InstrumentHint().Info(), this);
  2749. }
  2750. void CCtrlInstruments::OnEnKillFocusEditFadeOut()
  2751. {
  2752. if(IsLocked() || !m_nInstrument || !m_sndFile.Instruments[m_nInstrument]) return;
  2753. if(m_modDoc.GetModType() == MOD_TYPE_IT)
  2754. {
  2755. // Coarse fade-out in IT files
  2756. BOOL success;
  2757. uint32 fadeout = (GetDlgItemInt(IDC_EDIT7, &success, FALSE) + 16) & ~31;
  2758. if(success && fadeout != m_sndFile.Instruments[m_nInstrument]->nFadeOut)
  2759. {
  2760. SetDlgItemInt(IDC_EDIT7, fadeout, FALSE);
  2761. }
  2762. }
  2763. }
  2764. void CCtrlInstruments::BuildTuningComboBox()
  2765. {
  2766. m_ComboTuning.SetRedraw(FALSE);
  2767. m_ComboTuning.ResetContent();
  2768. m_ComboTuning.AddString(_T("OpenMPT IT behaviour")); //<-> Instrument pTuning pointer == NULL
  2769. for(const auto &tuning : m_sndFile.GetTuneSpecificTunings())
  2770. {
  2771. m_ComboTuning.AddString(mpt::ToCString(tuning->GetName()));
  2772. }
  2773. m_ComboTuning.AddString(_T("Control Tunings..."));
  2774. UpdateTuningComboBox();
  2775. m_ComboTuning.SetRedraw(TRUE);
  2776. }
  2777. void CCtrlInstruments::UpdatePluginList()
  2778. {
  2779. m_CbnMixPlug.SetRedraw(FALSE);
  2780. m_CbnMixPlug.Clear();
  2781. m_CbnMixPlug.ResetContent();
  2782. #ifndef NO_PLUGINS
  2783. m_CbnMixPlug.SetItemData(m_CbnMixPlug.AddString(_T("No plugin")), 0);
  2784. AddPluginNamesToCombobox(m_CbnMixPlug, m_sndFile.m_MixPlugins, false);
  2785. #endif // NO_PLUGINS
  2786. m_CbnMixPlug.Invalidate(FALSE);
  2787. m_CbnMixPlug.SetRedraw(TRUE);
  2788. ModInstrument *pIns = m_sndFile.Instruments[m_nInstrument];
  2789. if ((pIns) && (pIns->nMixPlug <= MAX_MIXPLUGINS)) m_CbnMixPlug.SetCurSel(pIns->nMixPlug);
  2790. }
  2791. void CCtrlInstruments::OnXButtonUp(UINT nFlags, UINT nButton, CPoint point)
  2792. {
  2793. if(nButton == XBUTTON1) OnPrevInstrument();
  2794. else if(nButton == XBUTTON2) OnNextInstrument();
  2795. CModControlDlg::OnXButtonUp(nFlags, nButton, point);
  2796. }
  2797. OPENMPT_NAMESPACE_END