1
0

MainFrm.cpp 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257
  1. /*
  2. * MainFrm.cpp
  3. * -----------
  4. * Purpose: Implementation of OpenMPT's main window code.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "Mptrack.h"
  11. #include "Mainfrm.h"
  12. #include "ModDocTemplate.h"
  13. #include "openmpt/sounddevice/SoundDevice.hpp"
  14. #include "openmpt/sounddevice/SoundDeviceBuffer.hpp"
  15. #include "openmpt/sounddevice/SoundDeviceManager.hpp"
  16. #include "../soundlib/AudioReadTarget.h"
  17. #include "ImageLists.h"
  18. #include "Moddoc.h"
  19. #include "Childfrm.h"
  20. #include "Dlsbank.h"
  21. #include "Mpdlgs.h"
  22. #include "FolderScanner.h"
  23. #include "KeyConfigDlg.h"
  24. #include "PathConfigDlg.h"
  25. #include "GeneralConfigDlg.h"
  26. #include "ColorConfigDlg.h"
  27. #include "SampleConfigDlg.h"
  28. #include "AdvancedConfigDlg.h"
  29. #include "AutoSaver.h"
  30. #include "Mainfrm.h"
  31. #include "InputHandler.h"
  32. #include "Globals.h"
  33. #include "ChannelManagerDlg.h"
  34. #include "../common/version.h"
  35. #include "UpdateCheck.h"
  36. #include "CloseMainDialog.h"
  37. #include "SelectPluginDialog.h"
  38. #include "PatternClipboard.h"
  39. #include "PatternFont.h"
  40. #include "../common/mptFileIO.h"
  41. #include "../common/FileReader.h"
  42. #include "../common/Profiler.h"
  43. #include "../soundlib/plugins/PlugInterface.h"
  44. #include "../soundlib/plugins/PluginManager.h"
  45. #include "Vstplug.h"
  46. #include "FileDialog.h"
  47. #include "ProgressDialog.h"
  48. #include <HtmlHelp.h>
  49. #include <Dbt.h> // device change messages
  50. #include "mpt/audio/span.hpp"
  51. OPENMPT_NAMESPACE_BEGIN
  52. #define TIMERID_GUI 1
  53. #define TIMERID_NOTIFY 2
  54. #define MPTTIMER_PERIOD 200
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CMainFrame
  57. IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
  58. BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
  59. //{{AFX_MSG_MAP(CMainFrame)
  60. ON_WM_TIMER()
  61. ON_WM_CLOSE()
  62. ON_WM_CREATE()
  63. ON_WM_RBUTTONDOWN()
  64. ON_WM_DEVICECHANGE()
  65. ON_WM_DROPFILES()
  66. ON_WM_QUERYENDSESSION()
  67. ON_COMMAND(ID_VIEW_OPTIONS, &CMainFrame::OnViewOptions)
  68. ON_COMMAND(ID_PLUGIN_SETUP, &CMainFrame::OnPluginManager)
  69. ON_COMMAND(ID_CLIPBOARD_MANAGER, &CMainFrame::OnClipboardManager)
  70. //ON_COMMAND(ID_HELP, CMDIFrameWnd::OnHelp)
  71. ON_COMMAND(ID_REPORT_BUG, &CMainFrame::OnReportBug)
  72. ON_COMMAND(ID_NEXTOCTAVE, &CMainFrame::OnNextOctave)
  73. ON_COMMAND(ID_PREVOCTAVE, &CMainFrame::OnPrevOctave)
  74. ON_COMMAND_RANGE(ID_FILE_OPENTEMPLATE, ID_FILE_OPENTEMPLATE_LASTINRANGE, &CMainFrame::OnOpenTemplateModule)
  75. ON_COMMAND(ID_ADD_SOUNDBANK, &CMainFrame::OnAddDlsBank)
  76. ON_COMMAND(ID_IMPORT_MIDILIB, &CMainFrame::OnImportMidiLib)
  77. ON_COMMAND(ID_MIDI_RECORD, &CMainFrame::OnMidiRecord)
  78. ON_COMMAND(ID_PANIC, &CMainFrame::OnPanic)
  79. ON_COMMAND(ID_PLAYER_PAUSE, &CMainFrame::OnPlayerPause)
  80. ON_COMMAND_RANGE(ID_EXAMPLE_MODULES, ID_EXAMPLE_MODULES_LASTINRANGE, &CMainFrame::OnExampleSong)
  81. ON_COMMAND_EX(IDD_TREEVIEW, &CMainFrame::OnBarCheck)
  82. ON_COMMAND_EX(ID_NETLINK_MODPLUG, &CMainFrame::OnInternetLink)
  83. ON_COMMAND_EX(ID_NETLINK_TOP_PICKS, &CMainFrame::OnInternetLink)
  84. ON_UPDATE_COMMAND_UI(ID_MIDI_RECORD, &CMainFrame::OnUpdateMidiRecord)
  85. ON_UPDATE_COMMAND_UI(ID_INDICATOR_TIME, &CMainFrame::OnUpdateTime)
  86. ON_UPDATE_COMMAND_UI(ID_INDICATOR_USER, &CMainFrame::OnUpdateUser)
  87. ON_UPDATE_COMMAND_UI(ID_INDICATOR_INFO, &CMainFrame::OnUpdateInfo)
  88. ON_UPDATE_COMMAND_UI(ID_INDICATOR_XINFO,&CMainFrame::OnUpdateXInfo)
  89. ON_UPDATE_COMMAND_UI(IDD_TREEVIEW, &CMainFrame::OnUpdateControlBarMenu)
  90. ON_MESSAGE(WM_MOD_UPDATEPOSITION, &CMainFrame::OnUpdatePosition)
  91. ON_MESSAGE(WM_MOD_INVALIDATEPATTERNS, &CMainFrame::OnInvalidatePatterns)
  92. ON_MESSAGE(WM_MOD_KEYCOMMAND, &CMainFrame::OnCustomKeyMsg)
  93. ON_MESSAGE(WM_MOD_MIDIMAPPING, &CMainFrame::OnViewMIDIMapping)
  94. ON_MESSAGE(WM_MOD_UPDATEVIEWS, &CMainFrame::OnUpdateViews)
  95. ON_MESSAGE(WM_MOD_SETMODIFIED, &CMainFrame::OnSetModified)
  96. #if defined(MPT_ENABLE_UPDATE)
  97. ON_MESSAGE(WM_MOD_UPDATENOTIFY, &CMainFrame::OnToolbarUpdateIndicatorClick)
  98. #endif // MPT_ENABLE_UPDATE
  99. ON_COMMAND(ID_INTERNETUPDATE, &CMainFrame::OnInternetUpdate)
  100. ON_COMMAND(ID_UPDATE_AVAILABLE, &CMainFrame::OnUpdateAvailable)
  101. ON_COMMAND(ID_HELP_SHOWSETTINGSFOLDER, &CMainFrame::OnShowSettingsFolder)
  102. #if defined(MPT_ENABLE_UPDATE)
  103. ON_MESSAGE(MPT_WM_APP_UPDATECHECK_START, &CMainFrame::OnUpdateCheckStart)
  104. ON_MESSAGE(MPT_WM_APP_UPDATECHECK_PROGRESS, &CMainFrame::OnUpdateCheckProgress)
  105. ON_MESSAGE(MPT_WM_APP_UPDATECHECK_CANCELED, &CMainFrame::OnUpdateCheckCanceled)
  106. ON_MESSAGE(MPT_WM_APP_UPDATECHECK_FAILURE, &CMainFrame::OnUpdateCheckFailure)
  107. ON_MESSAGE(MPT_WM_APP_UPDATECHECK_SUCCESS, &CMainFrame::OnUpdateCheckSuccess)
  108. #endif // MPT_ENABLE_UPDATE
  109. ON_COMMAND(ID_HELPSHOW, &CMainFrame::OnHelp)
  110. ON_COMMAND_RANGE(ID_MRU_LIST_FIRST, ID_MRU_LIST_LAST, &CMainFrame::OnOpenMRUItem)
  111. ON_UPDATE_COMMAND_UI(ID_MRU_LIST_FIRST, &CMainFrame::OnUpdateMRUItem)
  112. //}}AFX_MSG_MAP
  113. ON_WM_INITMENU()
  114. ON_WM_KILLFOCUS()
  115. ON_WM_MOUSEWHEEL()
  116. ON_WM_SHOWWINDOW()
  117. ON_WM_ACTIVATEAPP()
  118. END_MESSAGE_MAP()
  119. // Globals
  120. OptionsPage CMainFrame::m_nLastOptionsPage = OPTIONS_PAGE_DEFAULT;
  121. HHOOK CMainFrame::ghKbdHook = NULL;
  122. // GDI
  123. HICON CMainFrame::m_hIcon = NULL;
  124. HFONT CMainFrame::m_hGUIFont = NULL;
  125. HFONT CMainFrame::m_hFixedFont = NULL;
  126. HPEN CMainFrame::penDarkGray = NULL;
  127. HPEN CMainFrame::penGray99 = NULL;
  128. HPEN CMainFrame::penHalfDarkGray = NULL;
  129. HCURSOR CMainFrame::curDragging = NULL;
  130. HCURSOR CMainFrame::curArrow = NULL;
  131. HCURSOR CMainFrame::curNoDrop = NULL;
  132. HCURSOR CMainFrame::curNoDrop2 = NULL;
  133. HCURSOR CMainFrame::curVSplit = NULL;
  134. MODPLUGDIB *CMainFrame::bmpNotes = nullptr;
  135. MODPLUGDIB *CMainFrame::bmpVUMeters = nullptr;
  136. MODPLUGDIB *CMainFrame::bmpPluginVUMeters = nullptr;
  137. COLORREF CMainFrame::gcolrefVuMeter[NUM_VUMETER_PENS*2];
  138. CInputHandler *CMainFrame::m_InputHandler = nullptr;
  139. static UINT indicators[] =
  140. {
  141. ID_SEPARATOR, // status line indicator
  142. ID_INDICATOR_XINFO,
  143. ID_INDICATOR_INFO,
  144. ID_INDICATOR_USER,
  145. ID_INDICATOR_TIME,
  146. };
  147. /////////////////////////////////////////////////////////////////////////////
  148. // CMainFrame construction/destruction
  149. CMainFrame::CMainFrame()
  150. : SoundDevice::CallbackBufferHandler<DithersOpenMPT>(theApp.PRNG())
  151. , m_SoundDeviceFillBufferCriticalSection(CriticalSection::InitialState::Unlocked)
  152. {
  153. m_szUserText[0] = 0;
  154. m_szInfoText[0] = 0;
  155. m_szXInfoText[0]= 0;
  156. MemsetZero(gcolrefVuMeter);
  157. m_InputHandler = new CInputHandler(this);
  158. }
  159. void CMainFrame::Initialize()
  160. {
  161. //Adding version number to the frame title
  162. CString title = GetTitle();
  163. title += _T(" ") + mpt::cfmt::val(Version::Current());
  164. #if defined(MPT_BUILD_RETRO)
  165. title += _T(" RETRO");
  166. #endif // MPT_BUILD_RETRO
  167. if(Build::IsDebugBuild())
  168. {
  169. title += _T(" DEBUG");
  170. }
  171. #ifndef MPT_WITH_VST
  172. title += _T(" NO_VST");
  173. #endif
  174. #ifndef MPT_WITH_DMO
  175. title += _T(" NO_DMO");
  176. #endif
  177. #ifdef NO_PLUGINS
  178. title += _T(" NO_PLUGINS");
  179. #endif
  180. SetTitle(title);
  181. OnUpdateFrameTitle(false);
  182. // Check for valid sound device
  183. SoundDevice::Identifier dev = TrackerSettings::Instance().GetSoundDeviceIdentifier();
  184. if(!theApp.GetSoundDevicesManager()->FindDeviceInfo(dev).IsValid())
  185. {
  186. dev = theApp.GetSoundDevicesManager()->FindDeviceInfoBestMatch(dev).GetIdentifier();
  187. TrackerSettings::Instance().SetSoundDeviceIdentifier(dev);
  188. }
  189. // Setup timer
  190. OnUpdateUser(NULL);
  191. m_nTimer = SetTimer(TIMERID_GUI, MPTTIMER_PERIOD, NULL);
  192. // Setup Keyboard Hook
  193. ghKbdHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, AfxGetInstanceHandle(), GetCurrentThreadId());
  194. // Update the tree
  195. m_wndTree.Init();
  196. CreateExampleModulesMenu();
  197. CreateTemplateModulesMenu();
  198. UpdateMRUList();
  199. }
  200. CMainFrame::~CMainFrame()
  201. {
  202. delete m_InputHandler;
  203. CChannelManagerDlg::DestroySharedInstance();
  204. }
  205. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  206. {
  207. if(CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
  208. return -1;
  209. // Load resources
  210. m_hIcon = theApp.LoadIcon(IDR_MAINFRAME);
  211. // Toolbar and other icons
  212. CDC *dc = GetDC();
  213. const double scaling = Util::GetDPIx(m_hWnd) / 96.0;
  214. static constexpr int miscIconsInvert[] = {IMAGE_PATTERNS, IMAGE_OPLINSTRACTIVE, IMAGE_OPLINSTRMUTE};
  215. static constexpr int patternIconsInvert[] = {TIMAGE_PREVIEW, TIMAGE_MACROEDITOR, TIMAGE_PATTERN_OVERFLOWPASTE, TIMAGE_PATTERN_DETAIL_LO, TIMAGE_PATTERN_DETAIL_MED, TIMAGE_PATTERN_DETAIL_HI, TIMAGE_PATTERN_PLUGINS, TIMAGE_SAMPLE_UNSIGN};
  216. static constexpr int envelopeIconsInvert[] = {IIMAGE_CHECKED, IIMAGE_VOLSWITCH, IIMAGE_PANSWITCH, IIMAGE_PITCHSWITCH, IIMAGE_FILTERSWITCH, IIMAGE_NOPITCHSWITCH, IIMAGE_NOFILTERSWITCH};
  217. m_MiscIcons.Create(IDB_IMAGELIST, 16, 16, IMGLIST_NUMIMAGES, 1, dc, scaling, false, miscIconsInvert);
  218. m_MiscIconsDisabled.Create(IDB_IMAGELIST, 16, 16, IMGLIST_NUMIMAGES, 1, dc, scaling, true, miscIconsInvert);
  219. m_PatternIcons.Create(IDB_PATTERNS, 16, 16, PATTERNIMG_NUMIMAGES, 1, dc, scaling, false, patternIconsInvert);
  220. m_PatternIconsDisabled.Create(IDB_PATTERNS, 16, 16, PATTERNIMG_NUMIMAGES, 1, dc, scaling, true, patternIconsInvert);
  221. m_EnvelopeIcons.Create(IDB_ENVTOOLBAR, 20, 18, ENVIMG_NUMIMAGES, 1, dc, scaling, false, envelopeIconsInvert);
  222. m_SampleIcons.Create(IDB_SMPTOOLBAR, 20, 18, SAMPLEIMG_NUMIMAGES, 1, dc, scaling, false);
  223. ReleaseDC(dc);
  224. NONCLIENTMETRICS metrics;
  225. metrics.cbSize = sizeof(metrics);
  226. SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(metrics), &metrics, 0);
  227. m_hGUIFont = CreateFontIndirect(&metrics.lfMessageFont);
  228. penDarkGray = ::CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));
  229. penGray99 = ::CreatePen(PS_SOLID,0, RGB(0x99, 0x99, 0x99));
  230. penHalfDarkGray = ::CreatePen(PS_DOT, 0, GetSysColor(COLOR_BTNSHADOW));
  231. // Cursors
  232. curDragging = theApp.LoadCursor(IDC_DRAGGING);
  233. curArrow = theApp.LoadStandardCursor(IDC_ARROW);
  234. curNoDrop = theApp.LoadStandardCursor(IDC_NO);
  235. curNoDrop2 = theApp.LoadCursor(IDC_NODRAG);
  236. curVSplit = theApp.LoadCursor(AFX_IDC_HSPLITBAR);
  237. // bitmaps
  238. bmpNotes = LoadDib(MAKEINTRESOURCE(IDB_PATTERNVIEW));
  239. bmpVUMeters = LoadDib(MAKEINTRESOURCE(IDB_VUMETERS));
  240. bmpPluginVUMeters = LoadDib(MAKEINTRESOURCE(IDB_VUMETERS));
  241. // Toolbars
  242. EnableDocking(CBRS_ALIGN_ANY);
  243. if (!m_wndToolBar.Create(this)) return -1;
  244. if (!m_wndStatusBar.Create(this)) return -1;
  245. if (!m_wndTree.Create(this, IDD_TREEVIEW, CBRS_LEFT|CBRS_BORDER_RIGHT, IDD_TREEVIEW)) return -1;
  246. m_wndStatusBar.SetIndicators(indicators, mpt::saturate_cast<int>(std::size(indicators)));
  247. m_wndStatusBar.SetPaneInfo(0, ID_SEPARATOR, SBPS_STRETCH, 256);
  248. m_wndToolBar.Init(this);
  249. m_wndTree.RecalcLayout();
  250. RemoveControlBar(&m_wndStatusBar); //Removing statusbar because corrupted statusbar inifiledata can crash the program.
  251. m_wndTree.EnableDocking(0); //To prevent a crash when there's "Docking=1" for treebar in ini-settings.
  252. // Restore toobar positions
  253. LoadBarState(_T("Toolbars"));
  254. AddControlBar(&m_wndStatusBar); //Restore statusbar to mainframe.
  255. UpdateColors();
  256. if(TrackerSettings::Instance().m_dwMidiSetup & MIDISETUP_ENABLE_RECORD_DEFAULT) midiOpenDevice(false);
  257. #if MPT_COMPILER_MSVC
  258. #pragma warning(push)
  259. #pragma warning(disable:6387) // '_Param_(2)' could be '0': this does not adhere to the specification for the function 'HtmlHelpW'
  260. #endif
  261. ::HtmlHelp(m_hWnd, nullptr, HH_INITIALIZE, reinterpret_cast<DWORD_PTR>(&helpCookie));
  262. #if MPT_COMPILER_MSVC
  263. #pragma warning(pop)
  264. #endif
  265. return 0;
  266. }
  267. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  268. {
  269. return CMDIFrameWnd::PreCreateWindow(cs);
  270. }
  271. BOOL CMainFrame::DestroyWindow()
  272. {
  273. #if MPT_COMPILER_MSVC
  274. #pragma warning(push)
  275. #pragma warning(disable:6387) // '_Param_(2)' could be '0': this does not adhere to the specification for the function 'HtmlHelpW'
  276. #endif
  277. ::HtmlHelp(m_hWnd, nullptr, HH_UNINITIALIZE, reinterpret_cast<DWORD_PTR>(&helpCookie));
  278. #if MPT_COMPILER_MSVC
  279. #pragma warning(pop)
  280. #endif
  281. // Uninstall Keyboard Hook
  282. if (ghKbdHook)
  283. {
  284. UnhookWindowsHookEx(ghKbdHook);
  285. ghKbdHook = NULL;
  286. }
  287. // Kill Timer
  288. if (m_nTimer)
  289. {
  290. KillTimer(m_nTimer);
  291. m_nTimer = 0;
  292. }
  293. if (shMidiIn) midiCloseDevice();
  294. // Delete bitmaps
  295. delete bmpNotes;
  296. bmpNotes = nullptr;
  297. delete bmpVUMeters;
  298. bmpVUMeters = nullptr;
  299. delete bmpPluginVUMeters;
  300. bmpPluginVUMeters = nullptr;
  301. PatternFont::DeleteFontData();
  302. // Kill GDI Objects
  303. #define DeleteGDIObject(h) ::DeleteObject(h); h = NULL;
  304. DeleteGDIObject(penDarkGray);
  305. DeleteGDIObject(m_hGUIFont);
  306. DeleteGDIObject(m_hFixedFont);
  307. DeleteGDIObject(penGray99);
  308. #undef DeleteGDIObject
  309. return CMDIFrameWnd::DestroyWindow();
  310. }
  311. void CMainFrame::OnClose()
  312. {
  313. MPT_TRACE_SCOPE();
  314. if(!(TrackerSettings::Instance().m_dwPatternSetup & PATTERN_NOCLOSEDIALOG))
  315. {
  316. // Show modified documents window
  317. CloseMainDialog dlg;
  318. if(dlg.DoModal() != IDOK)
  319. {
  320. return;
  321. }
  322. }
  323. CChildFrame *pMDIActive = (CChildFrame *)MDIGetActive();
  324. BeginWaitCursor();
  325. if (IsPlaying()) PauseMod();
  326. if (pMDIActive) pMDIActive->SavePosition(TRUE);
  327. if(gpSoundDevice)
  328. {
  329. gpSoundDevice->Stop();
  330. gpSoundDevice->Close();
  331. delete gpSoundDevice;
  332. gpSoundDevice = nullptr;
  333. }
  334. // Save Settings
  335. RemoveControlBar(&m_wndStatusBar); // Remove statusbar so that its state won't get saved.
  336. SaveBarState(_T("Toolbars"));
  337. AddControlBar(&m_wndStatusBar); // Restore statusbar to mainframe.
  338. TrackerSettings::Instance().SaveSettings();
  339. if(m_InputHandler && m_InputHandler->m_activeCommandSet)
  340. {
  341. m_InputHandler->m_activeCommandSet->SaveFile(TrackerSettings::Instance().m_szKbdFile);
  342. }
  343. EndWaitCursor();
  344. CMDIFrameWnd::OnClose();
  345. }
  346. BOOL CMainFrame::OnDeviceChange(UINT nEventType, DWORD_PTR dwData)
  347. {
  348. if(nEventType == DBT_DEVNODES_CHANGED && shMidiIn)
  349. {
  350. // Calling this (or most other MIDI input related functions) makes the MIDI driver realize
  351. // that the connection to USB MIDI devices was lost and send a MIM_CLOSE message.
  352. // Otherwise, after disconnecting a USB MIDI device, the MIDI callback will stay alive forever but return no data.
  353. midiInGetNumDevs();
  354. }
  355. return CMDIFrameWnd::OnDeviceChange(nEventType, dwData);
  356. }
  357. // Drop files from Windows
  358. void CMainFrame::OnDropFiles(HDROP hDropInfo)
  359. {
  360. const UINT nFiles = ::DragQueryFileW(hDropInfo, (UINT)-1, NULL, 0);
  361. CMainFrame::GetMainFrame()->SetForegroundWindow();
  362. for(UINT f = 0; f < nFiles; f++)
  363. {
  364. UINT size = ::DragQueryFile(hDropInfo, f, nullptr, 0) + 1;
  365. std::vector<TCHAR> fileName(size, L'\0');
  366. if(::DragQueryFile(hDropInfo, f, fileName.data(), size))
  367. {
  368. const mpt::PathString file = mpt::PathString::FromNative(fileName.data());
  369. #ifdef MPT_BUILD_DEBUG
  370. // Debug Hack: Quickly scan a folder containing module files (without running out of window handles ;)
  371. if(m_InputHandler->CtrlPressed() && m_InputHandler->AltPressed() && m_InputHandler->ShiftPressed() && file.IsDirectory())
  372. {
  373. FolderScanner scanner(file, FolderScanner::kOnlyFiles | FolderScanner::kFindInSubDirectories);
  374. mpt::PathString scanName;
  375. size_t failed = 0, total = 0;
  376. while(scanner.Next(scanName))
  377. {
  378. InputFile inputFile(scanName, TrackerSettings::Instance().MiscCacheCompleteFileBeforeLoading);
  379. if(!inputFile.IsValid())
  380. continue;
  381. auto sndFile = std::make_unique<CSoundFile>();
  382. MPT_LOG_GLOBAL(LogDebug, "info", U_("Loading ") + scanName.ToUnicode());
  383. if(!sndFile->Create(GetFileReader(inputFile), CSoundFile::loadCompleteModule, nullptr))
  384. {
  385. MPT_LOG_GLOBAL(LogDebug, "info", U_("FAILED: ") + scanName.ToUnicode());
  386. failed++;
  387. }
  388. total++;
  389. }
  390. Reporting::Information(MPT_UFORMAT("Scanned {} files, {} failed")(total, failed));
  391. break;
  392. }
  393. #endif
  394. theApp.OpenDocumentFile(file.ToCString());
  395. }
  396. }
  397. ::DragFinish(hDropInfo);
  398. }
  399. void CMainFrame::OnActivateApp(BOOL active, DWORD /*threadID*/)
  400. {
  401. // Ensure modifiers are reset when we leave the window (e.g. Alt-Tab)
  402. if(!active)
  403. m_InputHandler->SetModifierMask(ModNone);
  404. }
  405. LRESULT CALLBACK CMainFrame::KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
  406. {
  407. static bool s_KeyboardHookReentryFlag = false; // work-around for https://bugs.openmpt.org/view.php?id=713
  408. if(mpt::OS::Windows::IsWine()) // work-around for https://bugs.openmpt.org/view.php?id=713
  409. {
  410. // TODO: Properly fix this in same way or another.
  411. if(code < 0)
  412. {
  413. return CallNextHookEx(ghKbdHook, code, wParam, lParam); // required by spec
  414. }
  415. if(theApp.InGuiThread())
  416. {
  417. if(s_KeyboardHookReentryFlag)
  418. {
  419. // Exit early without calling our hook when re-entering.
  420. // We still need to call other hooks though.
  421. return CallNextHookEx(ghKbdHook, code, wParam, lParam); // required by spec
  422. }
  423. s_KeyboardHookReentryFlag = true;
  424. }
  425. }
  426. bool skipFurtherProcessing = false;
  427. if(code >= 0)
  428. {
  429. // Check if textbox has focus
  430. const bool handledByTextBox = m_InputHandler->IsKeyPressHandledByTextBox(static_cast<DWORD>(wParam), ::GetFocus());
  431. if(!handledByTextBox && m_InputHandler->GeneralKeyEvent(kCtxAllContexts, code, wParam, lParam) != kcNull)
  432. {
  433. if(wParam != VK_ESCAPE)
  434. {
  435. // We've handled the keypress. No need to take it further.
  436. // Unless it was esc, in which case we need it to close Windows
  437. // (there might be other special cases, we'll see.. )
  438. skipFurtherProcessing = true;
  439. }
  440. }
  441. }
  442. LRESULT result = 0;
  443. if(skipFurtherProcessing)
  444. {
  445. result = -1;
  446. } else
  447. {
  448. result = CallNextHookEx(ghKbdHook, code, wParam, lParam);
  449. }
  450. if(mpt::OS::Windows::IsWine()) // work-around for https://bugs.openmpt.org/view.php?id=713
  451. {
  452. if(theApp.InGuiThread())
  453. {
  454. s_KeyboardHookReentryFlag = false;
  455. }
  456. }
  457. return result;
  458. }
  459. BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
  460. {
  461. if((pMsg->message == WM_RBUTTONDOWN) || (pMsg->message == WM_NCRBUTTONDOWN))
  462. {
  463. CWnd* pWnd = CWnd::FromHandlePermanent(pMsg->hwnd);
  464. CControlBar* pBar = NULL;
  465. HWND hwnd = (pWnd) ? pWnd->m_hWnd : NULL;
  466. if ((hwnd) && (pMsg->message == WM_RBUTTONDOWN)) pBar = DYNAMIC_DOWNCAST(CControlBar, pWnd);
  467. if ((pBar != NULL) || ((pMsg->message == WM_NCRBUTTONDOWN) && (pMsg->wParam == HTMENU)))
  468. {
  469. CMenu Menu;
  470. CPoint pt;
  471. GetCursorPos(&pt);
  472. if (Menu.LoadMenu(IDR_TOOLBARS))
  473. {
  474. CMenu* pSubMenu = Menu.GetSubMenu(0);
  475. if (pSubMenu!=NULL) pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,pt.x,pt.y,this);
  476. }
  477. }
  478. }
  479. return CMDIFrameWnd::PreTranslateMessage(pMsg);
  480. }
  481. void CMainFrame::OnUpdateFrameTitle(BOOL bAddToTitle)
  482. {
  483. if ((GetStyle() & FWS_ADDTOTITLE) == 0) return; // leave it alone!
  484. CMDIChildWnd* pActiveChild = nullptr;
  485. CDocument* pDocument = GetActiveDocument();
  486. if (bAddToTitle &&
  487. (pActiveChild = MDIGetActive()) != nullptr &&
  488. (pActiveChild->GetStyle() & WS_MAXIMIZE) == 0 &&
  489. (pDocument != nullptr ||
  490. (pDocument = pActiveChild->GetActiveDocument()) != nullptr))
  491. {
  492. CString title = pDocument->GetTitle();
  493. if(pDocument->IsModified())
  494. title.AppendChar(_T('*'));
  495. UpdateFrameTitleForDocument(title);
  496. } else
  497. {
  498. LPCTSTR lpstrTitle = nullptr;
  499. CString strTitle;
  500. if (pActiveChild != nullptr)
  501. {
  502. strTitle = pActiveChild->GetTitle();
  503. if (!strTitle.IsEmpty())
  504. lpstrTitle = strTitle;
  505. }
  506. UpdateFrameTitleForDocument(lpstrTitle);
  507. }
  508. }
  509. /////////////////////////////////////////////////////////////////////////////
  510. // CMainFrame Sound Library
  511. void CMainFrame::OnTimerNotify()
  512. {
  513. MPT_TRACE_SCOPE();
  514. ASSERT(InGuiThread());
  515. ASSERT(!InNotifyHandler());
  516. m_InNotifyHandler = true;
  517. Notification PendingNotification;
  518. bool found = false;
  519. int64 currenttotalsamples = 0;
  520. if(gpSoundDevice)
  521. {
  522. currenttotalsamples = gpSoundDevice->GetStreamPosition().Frames;
  523. }
  524. {
  525. // advance to the newest notification, drop the obsolete ones
  526. mpt::lock_guard<mpt::mutex> lock(m_NotificationBufferMutex);
  527. const Notification * pnotify = nullptr;
  528. const Notification * p = m_NotifyBuffer.peek_p();
  529. if(p && currenttotalsamples >= p->timestampSamples)
  530. {
  531. pnotify = p;
  532. while(m_NotifyBuffer.peek_next_p() && currenttotalsamples >= m_NotifyBuffer.peek_next_p()->timestampSamples)
  533. {
  534. m_NotifyBuffer.pop();
  535. p = m_NotifyBuffer.peek_p();
  536. pnotify = p;
  537. }
  538. }
  539. if(pnotify)
  540. {
  541. PendingNotification = *pnotify; // copy notification so that we can free the buffer
  542. found = true;
  543. {
  544. m_NotifyBuffer.pop();
  545. }
  546. }
  547. }
  548. if(found)
  549. {
  550. OnUpdatePosition(0, (LPARAM)&PendingNotification);
  551. }
  552. m_InNotifyHandler = false;
  553. ASSERT(!InNotifyHandler());
  554. }
  555. void CMainFrame::SoundDeviceMessage(LogLevel level, const mpt::ustring &str)
  556. {
  557. MPT_TRACE();
  558. Reporting::Message(level, str);
  559. }
  560. void CMainFrame::SoundCallbackPreStart()
  561. {
  562. MPT_TRACE();
  563. m_SoundDeviceClock.SetResolution(1);
  564. }
  565. void CMainFrame::SoundCallbackPostStop()
  566. {
  567. MPT_TRACE();
  568. m_SoundDeviceClock.SetResolution(0);
  569. }
  570. uint64 CMainFrame::SoundCallbackGetReferenceClockNowNanoseconds() const
  571. {
  572. MPT_TRACE();
  573. MPT_ASSERT(!InAudioThread());
  574. return m_SoundDeviceClock.NowNanoseconds();
  575. }
  576. uint64 CMainFrame::SoundCallbackLockedGetReferenceClockNowNanoseconds() const
  577. {
  578. MPT_TRACE();
  579. MPT_ASSERT(InAudioThread());
  580. return m_SoundDeviceClock.NowNanoseconds();
  581. }
  582. bool CMainFrame::SoundCallbackIsLockedByCurrentThread() const
  583. {
  584. MPT_TRACE();
  585. return theApp.GetGlobalMutexRef().IsLockedByCurrentThread();
  586. }
  587. void CMainFrame::SoundCallbackLock()
  588. {
  589. MPT_TRACE_SCOPE();
  590. m_SoundDeviceFillBufferCriticalSection.Enter();
  591. MPT_ASSERT_ALWAYS(m_pSndFile != nullptr);
  592. m_AudioThreadId = GetCurrentThreadId();
  593. mpt::log::Trace::SetThreadId(mpt::log::Trace::ThreadKindAudio, m_AudioThreadId);
  594. }
  595. void CMainFrame::SoundCallbackUnlock()
  596. {
  597. MPT_TRACE_SCOPE();
  598. MPT_ASSERT_ALWAYS(m_pSndFile != nullptr);
  599. m_AudioThreadId = 0;
  600. m_SoundDeviceFillBufferCriticalSection.Leave();
  601. }
  602. class BufferInputWrapper
  603. : public IAudioSource
  604. {
  605. private:
  606. SoundDevice::CallbackBuffer<DithersOpenMPT> &callbackBuffer;
  607. public:
  608. inline BufferInputWrapper(SoundDevice::CallbackBuffer<DithersOpenMPT> &callbackBuffer_)
  609. : callbackBuffer(callbackBuffer_)
  610. {
  611. return;
  612. }
  613. inline void Process(mpt::audio_span_planar<MixSampleInt> buffer) override
  614. {
  615. callbackBuffer.template ReadFixedPoint<MixSampleIntTraits::mix_fractional_bits>(buffer);
  616. }
  617. inline void Process(mpt::audio_span_planar<MixSampleFloat> buffer) override
  618. {
  619. callbackBuffer.Read(buffer);
  620. }
  621. };
  622. class BufferOutputWrapper
  623. : public IAudioTarget
  624. {
  625. private:
  626. SoundDevice::CallbackBuffer<DithersOpenMPT> &callbackBuffer;
  627. public:
  628. inline BufferOutputWrapper(SoundDevice::CallbackBuffer<DithersOpenMPT> &callbackBuffer_)
  629. : callbackBuffer(callbackBuffer_)
  630. {
  631. return;
  632. }
  633. inline void Process(mpt::audio_span_interleaved<MixSampleInt> buffer) override
  634. {
  635. callbackBuffer.template WriteFixedPoint<MixSampleIntTraits::mix_fractional_bits>(buffer);
  636. }
  637. inline void Process(mpt::audio_span_interleaved<MixSampleFloat> buffer) override
  638. {
  639. callbackBuffer.Write(buffer);
  640. }
  641. };
  642. void CMainFrame::SoundCallbackLockedProcessPrepare(SoundDevice::TimeInfo timeInfo)
  643. {
  644. MPT_TRACE_SCOPE();
  645. MPT_ASSERT(InAudioThread());
  646. TimingInfo timingInfo;
  647. timingInfo.OutputLatency = timeInfo.Latency;
  648. timingInfo.StreamFrames = timeInfo.SyncPointStreamFrames;
  649. timingInfo.SystemTimestamp = timeInfo.SyncPointSystemTimestamp;
  650. timingInfo.Speed = timeInfo.Speed;
  651. m_pSndFile->m_TimingInfo = timingInfo;
  652. }
  653. void CMainFrame::SoundCallbackLockedCallback(SoundDevice::CallbackBuffer<DithersOpenMPT> &buffer)
  654. {
  655. MPT_TRACE_SCOPE();
  656. MPT_ASSERT(InAudioThread());
  657. OPENMPT_PROFILE_FUNCTION(Profiler::Audio);
  658. BufferInputWrapper source(buffer);
  659. BufferOutputWrapper target(buffer);
  660. MPT_ASSERT(buffer.GetNumFrames() <= std::numeric_limits<CSoundFile::samplecount_t>::max());
  661. CSoundFile::samplecount_t framesToRender = static_cast<CSoundFile::samplecount_t>(buffer.GetNumFrames());
  662. MPT_ASSERT(framesToRender > 0);
  663. CSoundFile::samplecount_t renderedFrames = m_pSndFile->Read(framesToRender, target, source, std::ref(m_VUMeterOutput), std::ref(m_VUMeterInput));
  664. MPT_ASSERT(renderedFrames <= framesToRender);
  665. [[maybe_unused]] CSoundFile::samplecount_t remainingFrames = framesToRender - renderedFrames;
  666. MPT_ASSERT(remainingFrames >= 0); // remaining buffer is filled with silence automatically
  667. }
  668. void CMainFrame::SoundCallbackLockedProcessDone(SoundDevice::TimeInfo timeInfo)
  669. {
  670. MPT_TRACE_SCOPE();
  671. MPT_ASSERT(InAudioThread());
  672. OPENMPT_PROFILE_FUNCTION(Profiler::Notify);
  673. MPT_ASSERT((timeInfo.RenderStreamPositionAfter.Frames - timeInfo.RenderStreamPositionBefore.Frames) < std::numeric_limits<CSoundFile::samplecount_t>::max());
  674. CSoundFile::samplecount_t framesRendered = static_cast<CSoundFile::samplecount_t>(timeInfo.RenderStreamPositionAfter.Frames - timeInfo.RenderStreamPositionBefore.Frames);
  675. int64 streamPosition = timeInfo.RenderStreamPositionAfter.Frames;
  676. DoNotification(framesRendered, streamPosition);
  677. //m_pSndFile->m_TimingInfo = TimingInfo(); // reset
  678. }
  679. bool CMainFrame::IsAudioDeviceOpen() const
  680. {
  681. MPT_TRACE_SCOPE();
  682. return gpSoundDevice && gpSoundDevice->IsOpen();
  683. }
  684. bool CMainFrame::audioOpenDevice()
  685. {
  686. MPT_TRACE_SCOPE();
  687. const SoundDevice::Identifier deviceIdentifier = TrackerSettings::Instance().GetSoundDeviceIdentifier();
  688. if(!TrackerSettings::Instance().GetMixerSettings().IsValid())
  689. {
  690. Reporting::Error(MPT_UFORMAT("Unable to open sound device '{}': Invalid mixer settings.")(deviceIdentifier));
  691. return false;
  692. }
  693. if(gpSoundDevice && (gpSoundDevice->GetDeviceInfo().GetIdentifier() != deviceIdentifier))
  694. {
  695. gpSoundDevice->Stop();
  696. gpSoundDevice->Close();
  697. delete gpSoundDevice;
  698. gpSoundDevice = nullptr;
  699. }
  700. if(IsAudioDeviceOpen())
  701. {
  702. return true;
  703. }
  704. if(!gpSoundDevice)
  705. {
  706. gpSoundDevice = theApp.GetSoundDevicesManager()->CreateSoundDevice(deviceIdentifier);
  707. }
  708. if(!gpSoundDevice)
  709. {
  710. Reporting::Error(MPT_UFORMAT("Unable to open sound device '{}': Could not find sound device.")(deviceIdentifier));
  711. return false;
  712. }
  713. gpSoundDevice->SetMessageReceiver(this);
  714. gpSoundDevice->SetCallback(this);
  715. SoundDevice::Settings deviceSettings = TrackerSettings::Instance().GetSoundDeviceSettings(deviceIdentifier);
  716. if(!gpSoundDevice->Open(deviceSettings))
  717. {
  718. if(!gpSoundDevice->IsAvailable())
  719. {
  720. Reporting::Error(MPT_UFORMAT("Unable to open sound device '{}': Device not available.")(gpSoundDevice->GetDeviceInfo().GetDisplayName()));
  721. } else
  722. {
  723. Reporting::Error(MPT_UFORMAT("Unable to open sound device '{}'.")(gpSoundDevice->GetDeviceInfo().GetDisplayName()));
  724. }
  725. return false;
  726. }
  727. SampleFormat actualSampleFormat = gpSoundDevice->GetActualSampleFormat();
  728. if(!actualSampleFormat.IsValid())
  729. {
  730. Reporting::Error(MPT_UFORMAT("Unable to open sound device '{}': Unknown sample format.")(gpSoundDevice->GetDeviceInfo().GetDisplayName()));
  731. return false;
  732. }
  733. deviceSettings.sampleFormat = actualSampleFormat;
  734. Dithers().SetMode(deviceSettings.DitherType, deviceSettings.Channels);
  735. TrackerSettings::Instance().MixerSamplerate = gpSoundDevice->GetSettings().Samplerate;
  736. TrackerSettings::Instance().SetSoundDeviceSettings(deviceIdentifier, deviceSettings);
  737. return true;
  738. }
  739. void CMainFrame::audioCloseDevice()
  740. {
  741. MPT_TRACE_SCOPE();
  742. if(gpSoundDevice)
  743. {
  744. gpSoundDevice->Close();
  745. }
  746. if(m_NotifyTimer)
  747. {
  748. KillTimer(m_NotifyTimer);
  749. m_NotifyTimer = 0;
  750. }
  751. ResetNotificationBuffer();
  752. }
  753. void VUMeter::Process(Channel &c, MixSampleInt sample)
  754. {
  755. c.peak = std::max(c.peak, std::abs(sample));
  756. if(sample < MixSampleIntTraits::mix_clip_min || MixSampleIntTraits::mix_clip_max < sample)
  757. {
  758. c.clipped = true;
  759. }
  760. }
  761. void VUMeter::Process(Channel &c, MixSampleFloat sample)
  762. {
  763. Process(c, SC::ConvertToFixedPoint<MixSampleInt, MixSampleFloat, MixSampleIntTraits::mix_fractional_bits>()(sample));
  764. }
  765. void VUMeter::Process(mpt::audio_span_interleaved<const MixSampleInt> buffer)
  766. {
  767. for(std::size_t frame = 0; frame < buffer.size_frames(); ++frame)
  768. {
  769. for(std::size_t channel = 0; channel < std::min(buffer.size_channels(), maxChannels); ++channel)
  770. {
  771. Process(channels[channel], buffer(channel, frame));
  772. }
  773. }
  774. for(std::size_t channel = std::min(buffer.size_channels(), maxChannels); channel < maxChannels; ++channel)
  775. {
  776. channels[channel] = Channel();
  777. }
  778. }
  779. void VUMeter::Process(mpt::audio_span_interleaved<const MixSampleFloat> buffer)
  780. {
  781. for(std::size_t frame = 0; frame < buffer.size_frames(); ++frame)
  782. {
  783. for(std::size_t channel = 0; channel < std::min(buffer.size_channels(), maxChannels); ++channel)
  784. {
  785. Process(channels[channel], buffer(channel, frame));
  786. }
  787. }
  788. for(std::size_t channel = std::min(buffer.size_channels(), maxChannels); channel < maxChannels; ++channel)
  789. {
  790. channels[channel] = Channel();
  791. }
  792. }
  793. void VUMeter::Process(mpt::audio_span_planar<const MixSampleInt> buffer)
  794. {
  795. for(std::size_t frame = 0; frame < buffer.size_frames(); ++frame)
  796. {
  797. for(std::size_t channel = 0; channel < std::min(buffer.size_channels(), maxChannels); ++channel)
  798. {
  799. Process(channels[channel], buffer(channel, frame));
  800. }
  801. }
  802. for(std::size_t channel = std::min(buffer.size_channels(), maxChannels); channel < maxChannels; ++channel)
  803. {
  804. channels[channel] = Channel();
  805. }
  806. }
  807. void VUMeter::Process(mpt::audio_span_planar<const MixSampleFloat> buffer)
  808. {
  809. for(std::size_t frame = 0; frame < buffer.size_frames(); ++frame)
  810. {
  811. for(std::size_t channel = 0; channel < std::min(buffer.size_channels(), maxChannels); ++channel)
  812. {
  813. Process(channels[channel], buffer(channel, frame));
  814. }
  815. }
  816. for(std::size_t channel = std::min(buffer.size_channels(), maxChannels); channel < maxChannels; ++channel)
  817. {
  818. channels[channel] = Channel();
  819. }
  820. }
  821. const float VUMeter::dynamicRange = 48.0f; // corresponds to the current implementation of the UI widget displaying the result
  822. void VUMeter::SetDecaySpeedDecibelPerSecond(float decibelPerSecond)
  823. {
  824. float linearDecayRate = decibelPerSecond / dynamicRange;
  825. decayParam = mpt::saturate_round<int32>(linearDecayRate * static_cast<float>(MixSampleIntTraits::mix_clip_max));
  826. }
  827. void VUMeter::Decay(int32 secondsNum, int32 secondsDen)
  828. {
  829. int32 decay = Util::muldivr(decayParam, secondsNum, secondsDen);
  830. for(std::size_t channel = 0; channel < maxChannels; ++channel)
  831. {
  832. channels[channel].peak = std::max(channels[channel].peak - decay, 0);
  833. }
  834. }
  835. void VUMeter::ResetClipped()
  836. {
  837. for(std::size_t channel = 0; channel < maxChannels; ++channel)
  838. {
  839. channels[channel].clipped = false;
  840. }
  841. }
  842. static void SetVUMeter(std::array<uint32, 4> &masterVU, const VUMeter &vumeter)
  843. {
  844. for(std::size_t channel = 0; channel < VUMeter::maxChannels; ++channel)
  845. {
  846. masterVU[channel] = Clamp(vumeter[channel].peak >> 11, 0, 0x10000);
  847. if(vumeter[channel].clipped)
  848. {
  849. masterVU[channel] |= Notification::ClipVU;
  850. }
  851. }
  852. }
  853. bool CMainFrame::DoNotification(DWORD dwSamplesRead, int64 streamPosition)
  854. {
  855. MPT_TRACE_SCOPE();
  856. ASSERT(InAudioThread());
  857. if(!m_pSndFile) return false;
  858. FlagSet<Notification::Type> notifyType(Notification::Default);
  859. Notification::Item notifyItem = 0;
  860. if(m_pSndFile->m_pModDoc)
  861. {
  862. notifyType = m_pSndFile->m_pModDoc->GetNotificationType();
  863. notifyItem = m_pSndFile->m_pModDoc->GetNotificationItem();
  864. }
  865. // Add an entry to the notification history
  866. Notification notification(notifyType, notifyItem, streamPosition, m_pSndFile->m_PlayState.m_nRow, m_pSndFile->m_PlayState.m_nTickCount, m_pSndFile->m_PlayState.TicksOnRow(), m_pSndFile->m_PlayState.m_nCurrentOrder, m_pSndFile->m_PlayState.m_nPattern, m_pSndFile->GetMixStat(), static_cast<uint8>(m_pSndFile->m_MixerSettings.gnChannels), static_cast<uint8>(m_pSndFile->m_MixerSettings.NumInputChannels));
  867. m_pSndFile->ResetMixStat();
  868. if(m_pSndFile->m_SongFlags[SONG_ENDREACHED]) notification.type.set(Notification::EOS);
  869. if(notifyType[Notification::Sample])
  870. {
  871. // Sample positions
  872. const SAMPLEINDEX smp = notifyItem;
  873. if(smp > 0 && smp <= m_pSndFile->GetNumSamples() && m_pSndFile->GetSample(smp).HasSampleData())
  874. {
  875. for(CHANNELINDEX k = 0; k < MAX_CHANNELS; k++)
  876. {
  877. const ModChannel &chn = m_pSndFile->m_PlayState.Chn[k];
  878. if(chn.pModSample == &m_pSndFile->GetSample(smp) && chn.nLength != 0 // Corrent sample is set up on this channel
  879. && (!chn.dwFlags[CHN_NOTEFADE] || chn.nFadeOutVol)) // And it hasn't completely faded out yet, so it's still playing
  880. {
  881. notification.pos[k] = chn.position.GetInt();
  882. } else
  883. {
  884. notification.pos[k] = Notification::PosInvalid;
  885. }
  886. }
  887. } else
  888. {
  889. // Can't generate a valid notification.
  890. notification.type.reset(Notification::Sample);
  891. }
  892. } else if(notifyType[Notification::VolEnv | Notification::PanEnv | Notification::PitchEnv])
  893. {
  894. // Instrument envelopes
  895. const INSTRUMENTINDEX ins = notifyItem;
  896. EnvelopeType notifyEnv = ENV_VOLUME;
  897. if(notifyType[Notification::PitchEnv])
  898. notifyEnv = ENV_PITCH;
  899. else if(notifyType[Notification::PanEnv])
  900. notifyEnv = ENV_PANNING;
  901. if(ins > 0 && ins <= m_pSndFile->GetNumInstruments() && m_pSndFile->Instruments[ins] != nullptr)
  902. {
  903. for(CHANNELINDEX k = 0; k < MAX_CHANNELS; k++)
  904. {
  905. const ModChannel &chn = m_pSndFile->m_PlayState.Chn[k];
  906. SmpLength pos = Notification::PosInvalid;
  907. if(chn.pModInstrument == m_pSndFile->Instruments[ins] // Correct instrument is set up on this channel
  908. && (chn.nLength || chn.pModInstrument->HasValidMIDIChannel()) // And it's playing something (sample or instrument plugin)
  909. && (!chn.dwFlags[CHN_NOTEFADE] || chn.nFadeOutVol)) // And it hasn't completely faded out yet, so it's still playing
  910. {
  911. const ModChannel::EnvInfo &chnEnv = chn.GetEnvelope(notifyEnv);
  912. if(chnEnv.flags[ENV_ENABLED])
  913. {
  914. pos = chnEnv.nEnvPosition;
  915. if(m_pSndFile->m_playBehaviour[kITEnvelopePositionHandling])
  916. {
  917. // Impulse Tracker envelope handling (see e.g. CSoundFile::IncrementEnvelopePosition in SndMix.cpp for details)
  918. if(pos > 0)
  919. pos--;
  920. else
  921. pos = Notification::PosInvalid; // Envelope isn't playing yet (e.g. when disabling it right when triggering a note)
  922. }
  923. }
  924. }
  925. notification.pos[k] = pos;
  926. }
  927. } else
  928. {
  929. // Can't generate a valid notification.
  930. notification.type.reset(Notification::VolEnv | Notification::PanEnv | Notification::PitchEnv);
  931. }
  932. } else if(notifyType[Notification::VUMeters])
  933. {
  934. // Pattern channel VU meters
  935. for(CHANNELINDEX k = 0; k < m_pSndFile->GetNumChannels(); k++)
  936. {
  937. uint32 vul = m_pSndFile->m_PlayState.Chn[k].nLeftVU;
  938. uint32 vur = m_pSndFile->m_PlayState.Chn[k].nRightVU;
  939. notification.pos[k] = (vul << 8) | (vur);
  940. }
  941. }
  942. {
  943. // Master VU meter
  944. SetVUMeter(notification.masterVUin, m_VUMeterInput);
  945. SetVUMeter(notification.masterVUout, m_VUMeterOutput);
  946. m_VUMeterInput.Decay(dwSamplesRead, m_pSndFile->m_MixerSettings.gdwMixingFreq);
  947. m_VUMeterOutput.Decay(dwSamplesRead, m_pSndFile->m_MixerSettings.gdwMixingFreq);
  948. }
  949. {
  950. mpt::lock_guard<mpt::mutex> lock(m_NotificationBufferMutex);
  951. if(m_NotifyBuffer.write_size() == 0)
  952. {
  953. ASSERT(0);
  954. return FALSE; // drop notification
  955. }
  956. m_NotifyBuffer.push(notification);
  957. }
  958. return true;
  959. }
  960. void CMainFrame::UpdateDspEffects(CSoundFile &sndFile, bool reset)
  961. {
  962. CriticalSection cs;
  963. #ifndef NO_REVERB
  964. sndFile.m_Reverb.m_Settings = TrackerSettings::Instance().m_ReverbSettings;
  965. #endif
  966. #ifndef NO_DSP
  967. sndFile.m_Surround.m_Settings = TrackerSettings::Instance().m_SurroundSettings;
  968. #endif
  969. #ifndef NO_DSP
  970. sndFile.m_MegaBass.m_Settings = TrackerSettings::Instance().m_MegaBassSettings;
  971. #endif
  972. #ifndef NO_EQ
  973. sndFile.SetEQGains(TrackerSettings::Instance().m_EqSettings.Gains, TrackerSettings::Instance().m_EqSettings.Freqs, reset);
  974. #endif
  975. #ifndef NO_DSP
  976. sndFile.m_BitCrush.m_Settings = TrackerSettings::Instance().m_BitCrushSettings;
  977. #endif
  978. sndFile.SetDspEffects(TrackerSettings::Instance().MixerDSPMask);
  979. sndFile.InitPlayer(reset);
  980. }
  981. void CMainFrame::UpdateAudioParameters(CSoundFile &sndFile, bool reset)
  982. {
  983. CriticalSection cs;
  984. if (TrackerSettings::Instance().m_dwPatternSetup & PATTERN_MUTECHNMODE)
  985. TrackerSettings::Instance().MixerFlags |= SNDMIX_MUTECHNMODE;
  986. else
  987. TrackerSettings::Instance().MixerFlags &= ~SNDMIX_MUTECHNMODE;
  988. sndFile.SetMixerSettings(TrackerSettings::Instance().GetMixerSettings());
  989. sndFile.SetResamplerSettings(TrackerSettings::Instance().GetResamplerSettings());
  990. UpdateDspEffects(sndFile, false); // reset done in next line
  991. sndFile.InitPlayer(reset);
  992. }
  993. /////////////////////////////////////////////////////////////////////////////
  994. // CMainFrame diagnostics
  995. #ifdef _DEBUG
  996. void CMainFrame::AssertValid() const
  997. {
  998. CMDIFrameWnd::AssertValid();
  999. }
  1000. void CMainFrame::Dump(CDumpContext& dc) const
  1001. {
  1002. CMDIFrameWnd::Dump(dc);
  1003. }
  1004. #endif //_DEBUG
  1005. /////////////////////////////////////////////////////////////////////////////
  1006. // CMainFrame static helpers
  1007. void CMainFrame::UpdateColors()
  1008. {
  1009. const auto &colors = TrackerSettings::Instance().rgbCustomColors;
  1010. const struct { MODPLUGDIB *bitmap; uint32 lo, med, hi; } meters[] =
  1011. {
  1012. { bmpVUMeters, MODCOLOR_VUMETER_LO, MODCOLOR_VUMETER_MED, MODCOLOR_VUMETER_HI },
  1013. { bmpPluginVUMeters, MODCOLOR_VUMETER_LO_VST, MODCOLOR_VUMETER_MED_VST, MODCOLOR_VUMETER_HI_VST },
  1014. };
  1015. for(auto &meter : meters) if(meter.bitmap != nullptr)
  1016. {
  1017. meter.bitmap->bmiColors[7] = rgb2quad(GetSysColor(COLOR_BTNFACE));
  1018. meter.bitmap->bmiColors[8] = rgb2quad(GetSysColor(COLOR_BTNSHADOW));
  1019. meter.bitmap->bmiColors[15] = rgb2quad(GetSysColor(COLOR_BTNHIGHLIGHT));
  1020. meter.bitmap->bmiColors[10] = rgb2quad(colors[meter.lo]);
  1021. meter.bitmap->bmiColors[11] = rgb2quad(colors[meter.med]);
  1022. meter.bitmap->bmiColors[9] = rgb2quad(colors[meter.hi]);
  1023. meter.bitmap->bmiColors[2] = rgb2quad((colors[meter.lo] >> 1) & 0x7F7F7F);
  1024. meter.bitmap->bmiColors[3] = rgb2quad((colors[meter.med] >> 1) & 0x7F7F7F);
  1025. meter.bitmap->bmiColors[1] = rgb2quad((colors[meter.hi] >> 1) & 0x7F7F7F);
  1026. }
  1027. // Generel tab VU meters
  1028. for(UINT i = 0; i < NUM_VUMETER_PENS * 2; i++)
  1029. {
  1030. int r0,g0,b0, r1,g1,b1;
  1031. int r, g, b;
  1032. int y;
  1033. y = (i >= NUM_VUMETER_PENS) ? (i-NUM_VUMETER_PENS) : i;
  1034. if (y < (NUM_VUMETER_PENS/2))
  1035. {
  1036. r0 = GetRValue(colors[MODCOLOR_VUMETER_LO]);
  1037. g0 = GetGValue(colors[MODCOLOR_VUMETER_LO]);
  1038. b0 = GetBValue(colors[MODCOLOR_VUMETER_LO]);
  1039. r1 = GetRValue(colors[MODCOLOR_VUMETER_MED]);
  1040. g1 = GetGValue(colors[MODCOLOR_VUMETER_MED]);
  1041. b1 = GetBValue(colors[MODCOLOR_VUMETER_MED]);
  1042. } else
  1043. {
  1044. y -= (NUM_VUMETER_PENS/2);
  1045. r0 = GetRValue(colors[MODCOLOR_VUMETER_MED]);
  1046. g0 = GetGValue(colors[MODCOLOR_VUMETER_MED]);
  1047. b0 = GetBValue(colors[MODCOLOR_VUMETER_MED]);
  1048. r1 = GetRValue(colors[MODCOLOR_VUMETER_HI]);
  1049. g1 = GetGValue(colors[MODCOLOR_VUMETER_HI]);
  1050. b1 = GetBValue(colors[MODCOLOR_VUMETER_HI]);
  1051. }
  1052. r = r0 + ((r1 - r0) * y) / (NUM_VUMETER_PENS/2);
  1053. g = g0 + ((g1 - g0) * y) / (NUM_VUMETER_PENS/2);
  1054. b = b0 + ((b1 - b0) * y) / (NUM_VUMETER_PENS/2);
  1055. if (i >= NUM_VUMETER_PENS)
  1056. {
  1057. r = (r*2)/5;
  1058. g = (g*2)/5;
  1059. b = (b*2)/5;
  1060. }
  1061. gcolrefVuMeter[i] = RGB(r, g, b);
  1062. }
  1063. CMainFrame *mainFrm = GetMainFrame();
  1064. if(mainFrm != nullptr)
  1065. {
  1066. mainFrm->m_wndToolBar.m_VuMeter.Invalidate();
  1067. }
  1068. }
  1069. /////////////////////////////////////////////////////////////////////////////
  1070. // CMainFrame operations
  1071. UINT CMainFrame::GetBaseOctave() const
  1072. {
  1073. return m_wndToolBar.GetBaseOctave();
  1074. }
  1075. void CMainFrame::ResetNotificationBuffer()
  1076. {
  1077. MPT_TRACE();
  1078. mpt::lock_guard<mpt::mutex> lock(m_NotificationBufferMutex);
  1079. m_NotifyBuffer.clear();
  1080. }
  1081. bool CMainFrame::PreparePlayback()
  1082. {
  1083. MPT_TRACE_SCOPE();
  1084. // open the audio device to update needed TrackerSettings mixer parameters
  1085. if(!audioOpenDevice()) return false;
  1086. return true;
  1087. }
  1088. bool CMainFrame::StartPlayback()
  1089. {
  1090. MPT_TRACE_SCOPE();
  1091. if(!m_pSndFile) return false; // nothing to play
  1092. if(!IsAudioDeviceOpen()) return false;
  1093. Dithers().Reset();
  1094. if(!gpSoundDevice->Start()) return false;
  1095. if(!m_NotifyTimer)
  1096. {
  1097. if(TrackerSettings::Instance().GUIUpdateInterval.Get() > 0)
  1098. {
  1099. m_NotifyTimer = SetTimer(TIMERID_NOTIFY, TrackerSettings::Instance().GUIUpdateInterval, NULL);
  1100. } else
  1101. {
  1102. m_NotifyTimer = SetTimer(TIMERID_NOTIFY, std::max(int(1), mpt::saturate_round<int>(gpSoundDevice->GetEffectiveBufferAttributes().UpdateInterval * 1000.0)), NULL);
  1103. }
  1104. }
  1105. return true;
  1106. }
  1107. void CMainFrame::StopPlayback()
  1108. {
  1109. MPT_TRACE_SCOPE();
  1110. if(!IsAudioDeviceOpen()) return;
  1111. gpSoundDevice->Stop();
  1112. if(m_NotifyTimer)
  1113. {
  1114. KillTimer(m_NotifyTimer);
  1115. m_NotifyTimer = 0;
  1116. }
  1117. ResetNotificationBuffer();
  1118. if(!gpSoundDevice->GetDeviceCaps().CanKeepDeviceRunning || TrackerSettings::Instance().m_SoundSettingsStopMode == SoundDeviceStopModeClosed)
  1119. {
  1120. audioCloseDevice();
  1121. }
  1122. }
  1123. bool CMainFrame::RestartPlayback()
  1124. {
  1125. MPT_TRACE_SCOPE();
  1126. if(!m_pSndFile) return false; // nothing to play
  1127. if(!IsAudioDeviceOpen()) return false;
  1128. if(!gpSoundDevice->IsPlaying()) return false;
  1129. gpSoundDevice->StopAndAvoidPlayingSilence();
  1130. if(m_NotifyTimer)
  1131. {
  1132. KillTimer(m_NotifyTimer);
  1133. m_NotifyTimer = 0;
  1134. }
  1135. ResetNotificationBuffer();
  1136. return StartPlayback();
  1137. }
  1138. bool CMainFrame::PausePlayback()
  1139. {
  1140. MPT_TRACE_SCOPE();
  1141. if(!IsAudioDeviceOpen()) return false;
  1142. gpSoundDevice->Stop();
  1143. if(m_NotifyTimer)
  1144. {
  1145. KillTimer(m_NotifyTimer);
  1146. m_NotifyTimer = 0;
  1147. }
  1148. ResetNotificationBuffer();
  1149. return true;
  1150. }
  1151. void CMainFrame::GenerateStopNotification()
  1152. {
  1153. Notification mn(Notification::Stop);
  1154. SendMessage(WM_MOD_UPDATEPOSITION, 0, (LPARAM)&mn);
  1155. }
  1156. void CMainFrame::UnsetPlaybackSoundFile()
  1157. {
  1158. MPT_ASSERT_ALWAYS(!gpSoundDevice || !gpSoundDevice->IsPlaying());
  1159. if(m_pSndFile)
  1160. {
  1161. m_pSndFile->SuspendPlugins();
  1162. if(m_pSndFile->GetpModDoc())
  1163. {
  1164. m_wndTree.UpdatePlayPos(m_pSndFile->GetpModDoc(), nullptr);
  1165. }
  1166. m_pSndFile->m_SongFlags.reset(SONG_PAUSED);
  1167. if(m_pSndFile == &m_WaveFile)
  1168. {
  1169. // Unload previewed instrument
  1170. m_WaveFile.Destroy();
  1171. } else
  1172. {
  1173. // Stop sample preview channels
  1174. for(CHANNELINDEX i = m_pSndFile->m_nChannels; i < MAX_CHANNELS; i++)
  1175. {
  1176. if(m_pSndFile->m_PlayState.Chn[i].isPreviewNote)
  1177. {
  1178. m_pSndFile->m_PlayState.Chn[i].nLength = 0;
  1179. m_pSndFile->m_PlayState.Chn[i].position.Set(0);
  1180. }
  1181. }
  1182. }
  1183. }
  1184. m_pSndFile = nullptr;
  1185. m_wndToolBar.SetCurrentSong(nullptr);
  1186. ResetNotificationBuffer();
  1187. }
  1188. void CMainFrame::SetPlaybackSoundFile(CSoundFile *pSndFile)
  1189. {
  1190. MPT_ASSERT_ALWAYS(pSndFile);
  1191. m_pSndFile = pSndFile;
  1192. }
  1193. bool CMainFrame::PlayMod(CModDoc *pModDoc)
  1194. {
  1195. MPT_ASSERT_ALWAYS(!theApp.GetGlobalMutexRef().IsLockedByCurrentThread());
  1196. if(!pModDoc) return false;
  1197. CSoundFile &sndFile = pModDoc->GetSoundFile();
  1198. if(!IsValidSoundFile(sndFile)) return false;
  1199. // if something is playing, pause it
  1200. PausePlayback();
  1201. GenerateStopNotification();
  1202. UnsetPlaybackSoundFile();
  1203. // open audio device if not already open
  1204. if (!PreparePlayback()) return false;
  1205. // set mixing parameters in CSoundFile
  1206. UpdateAudioParameters(sndFile);
  1207. SetPlaybackSoundFile(&sndFile);
  1208. const bool bPaused = m_pSndFile->IsPaused();
  1209. const bool bPatLoop = m_pSndFile->m_SongFlags[SONG_PATTERNLOOP];
  1210. m_pSndFile->m_SongFlags.reset(SONG_FADINGSONG | SONG_ENDREACHED);
  1211. if(!bPatLoop && bPaused) sndFile.m_SongFlags.set(SONG_PAUSED);
  1212. sndFile.SetRepeatCount((TrackerSettings::Instance().gbLoopSong) ? -1 : 0);
  1213. sndFile.InitPlayer(true);
  1214. sndFile.ResumePlugins();
  1215. m_wndToolBar.SetCurrentSong(m_pSndFile);
  1216. m_VUMeterInput = VUMeter();
  1217. m_VUMeterOutput = VUMeter();
  1218. if(!StartPlayback())
  1219. {
  1220. UnsetPlaybackSoundFile();
  1221. return false;
  1222. }
  1223. return true;
  1224. }
  1225. bool CMainFrame::PauseMod(CModDoc *pModDoc)
  1226. {
  1227. MPT_ASSERT_ALWAYS(!theApp.GetGlobalMutexRef().IsLockedByCurrentThread());
  1228. if(pModDoc && (pModDoc != GetModPlaying())) return false;
  1229. if(!IsPlaying()) return true;
  1230. PausePlayback();
  1231. GenerateStopNotification();
  1232. UnsetPlaybackSoundFile();
  1233. StopPlayback();
  1234. return true;
  1235. }
  1236. bool CMainFrame::StopMod(CModDoc *pModDoc)
  1237. {
  1238. MPT_ASSERT_ALWAYS(!theApp.GetGlobalMutexRef().IsLockedByCurrentThread());
  1239. if(pModDoc && (pModDoc != GetModPlaying())) return false;
  1240. if(!IsPlaying()) return true;
  1241. PausePlayback();
  1242. SetElapsedTime(0);
  1243. GenerateStopNotification();
  1244. m_pSndFile->ResetPlayPos();
  1245. m_pSndFile->ResetChannels();
  1246. UnsetPlaybackSoundFile();
  1247. StopPlayback();
  1248. return true;
  1249. }
  1250. bool CMainFrame::StopSoundFile(CSoundFile *pSndFile)
  1251. {
  1252. MPT_ASSERT_ALWAYS(!theApp.GetGlobalMutexRef().IsLockedByCurrentThread());
  1253. if(!IsValidSoundFile(pSndFile)) return false;
  1254. if(pSndFile != m_pSndFile) return false;
  1255. if(!IsPlaying()) return true;
  1256. PausePlayback();
  1257. GenerateStopNotification();
  1258. UnsetPlaybackSoundFile();
  1259. StopPlayback();
  1260. return true;
  1261. }
  1262. bool CMainFrame::PlaySoundFile(CSoundFile *pSndFile)
  1263. {
  1264. MPT_ASSERT_ALWAYS(!theApp.GetGlobalMutexRef().IsLockedByCurrentThread());
  1265. if(!IsValidSoundFile(pSndFile)) return false;
  1266. PausePlayback();
  1267. GenerateStopNotification();
  1268. if(m_pSndFile != pSndFile)
  1269. {
  1270. UnsetPlaybackSoundFile();
  1271. }
  1272. if(!PreparePlayback())
  1273. {
  1274. UnsetPlaybackSoundFile();
  1275. return false;
  1276. }
  1277. UpdateAudioParameters(*pSndFile);
  1278. SetPlaybackSoundFile(pSndFile);
  1279. m_pSndFile->InitPlayer(true);
  1280. if(!StartPlayback())
  1281. {
  1282. UnsetPlaybackSoundFile();
  1283. return false;
  1284. }
  1285. return true;
  1286. }
  1287. bool CMainFrame::PlayDLSInstrument(const CDLSBank &bank, UINT instr, UINT region, ModCommand::NOTE note, int volume)
  1288. {
  1289. bool ok = false;
  1290. BeginWaitCursor();
  1291. {
  1292. CriticalSection cs;
  1293. InitPreview();
  1294. if(bank.ExtractInstrument(m_WaveFile, 1, instr, region))
  1295. {
  1296. PreparePreview(note, volume);
  1297. ok = true;
  1298. }
  1299. }
  1300. EndWaitCursor();
  1301. if(!ok)
  1302. {
  1303. PausePlayback();
  1304. UnsetPlaybackSoundFile();
  1305. StopPlayback();
  1306. return false;
  1307. }
  1308. if(IsPlaying() && (m_pSndFile == &m_WaveFile))
  1309. {
  1310. return true;
  1311. }
  1312. return PlaySoundFile(&m_WaveFile);
  1313. }
  1314. bool CMainFrame::PlaySoundFile(const mpt::PathString &filename, ModCommand::NOTE note, int volume)
  1315. {
  1316. bool ok = false;
  1317. BeginWaitCursor();
  1318. {
  1319. CriticalSection cs;
  1320. static mpt::PathString prevFile;
  1321. // Did we already load this file for previewing? Don't load it again if the preview is still running.
  1322. ok = (prevFile == filename && m_pSndFile == &m_WaveFile);
  1323. if(!ok && !filename.empty())
  1324. {
  1325. InputFile f(filename, TrackerSettings::Instance().MiscCacheCompleteFileBeforeLoading);
  1326. if(f.IsValid())
  1327. {
  1328. FileReader file = GetFileReader(f);
  1329. if(file.IsValid())
  1330. {
  1331. InitPreview();
  1332. m_WaveFile.m_SongFlags.set(SONG_PAUSED);
  1333. // Avoid hanging audio while reading file - we have removed all sample and instrument references before,
  1334. // so it's safe to replace the sample / instrument now.
  1335. cs.Leave();
  1336. ok = m_WaveFile.ReadInstrumentFromFile(1, file, TrackerSettings::Instance().m_MayNormalizeSamplesOnLoad);
  1337. cs.Enter();
  1338. if(!ok)
  1339. {
  1340. // Try reading as sample if reading as instrument fails
  1341. ok = m_WaveFile.ReadSampleFromFile(1, file, TrackerSettings::Instance().m_MayNormalizeSamplesOnLoad);
  1342. m_WaveFile.AllocateInstrument(1, 1);
  1343. }
  1344. }
  1345. }
  1346. }
  1347. if(ok)
  1348. {
  1349. // Write notes to pattern. Also done if we have previously loaded this file, since we might be previewing another note now.
  1350. PreparePreview(note, volume);
  1351. prevFile = filename;
  1352. }
  1353. }
  1354. EndWaitCursor();
  1355. if(!ok)
  1356. {
  1357. PausePlayback();
  1358. UnsetPlaybackSoundFile();
  1359. StopPlayback();
  1360. return false;
  1361. }
  1362. if(IsPlaying() && (m_pSndFile == &m_WaveFile))
  1363. {
  1364. return true;
  1365. }
  1366. return PlaySoundFile(&m_WaveFile);
  1367. }
  1368. bool CMainFrame::PlaySoundFile(CSoundFile &sndFile, INSTRUMENTINDEX nInstrument, SAMPLEINDEX nSample, ModCommand::NOTE note, int volume)
  1369. {
  1370. bool ok = false;
  1371. BeginWaitCursor();
  1372. {
  1373. CriticalSection cs;
  1374. InitPreview();
  1375. m_WaveFile.m_nType = sndFile.GetType();
  1376. if ((nInstrument) && (nInstrument <= sndFile.GetNumInstruments()))
  1377. {
  1378. m_WaveFile.m_nInstruments = 1;
  1379. m_WaveFile.m_nSamples = 32;
  1380. } else
  1381. {
  1382. m_WaveFile.m_nInstruments = 0;
  1383. m_WaveFile.m_nSamples = 1;
  1384. }
  1385. if (nInstrument != INSTRUMENTINDEX_INVALID && nInstrument <= sndFile.GetNumInstruments())
  1386. {
  1387. m_WaveFile.ReadInstrumentFromSong(1, sndFile, nInstrument);
  1388. } else if(nSample != SAMPLEINDEX_INVALID && nSample <= sndFile.GetNumSamples())
  1389. {
  1390. m_WaveFile.ReadSampleFromSong(1, sndFile, nSample);
  1391. }
  1392. PreparePreview(note, volume);
  1393. ok = true;
  1394. }
  1395. EndWaitCursor();
  1396. if(!ok)
  1397. {
  1398. PausePlayback();
  1399. UnsetPlaybackSoundFile();
  1400. StopPlayback();
  1401. return false;
  1402. }
  1403. if(IsPlaying() && (m_pSndFile == &m_WaveFile))
  1404. {
  1405. return true;
  1406. }
  1407. return PlaySoundFile(&m_WaveFile);
  1408. }
  1409. void CMainFrame::InitPreview()
  1410. {
  1411. m_WaveFile.Destroy();
  1412. m_WaveFile.Create(FileReader());
  1413. m_WaveFile.m_nDefaultTempo.Set(125);
  1414. m_WaveFile.m_nDefaultSpeed = 6;
  1415. m_WaveFile.m_nType = MOD_TYPE_MPT;
  1416. m_WaveFile.m_nChannels = 2;
  1417. m_WaveFile.m_nInstruments = 1;
  1418. m_WaveFile.m_nTempoMode = TempoMode::Classic;
  1419. m_WaveFile.Order().assign(1, 0);
  1420. m_WaveFile.Patterns.Insert(0, 2);
  1421. m_WaveFile.m_SongFlags = SONG_LINEARSLIDES;
  1422. }
  1423. void CMainFrame::PreparePreview(ModCommand::NOTE note, int volume)
  1424. {
  1425. m_WaveFile.m_SongFlags.reset(SONG_PAUSED);
  1426. m_WaveFile.SetRepeatCount(-1);
  1427. m_WaveFile.ResetPlayPos();
  1428. const CModDoc *activeDoc = GetActiveDoc();
  1429. if(activeDoc != nullptr && (TrackerSettings::Instance().m_dwPatternSetup & PATTERN_NOEXTRALOUD))
  1430. {
  1431. m_WaveFile.SetMixLevels(activeDoc->GetSoundFile().GetMixLevels());
  1432. m_WaveFile.m_nSamplePreAmp = activeDoc->GetSoundFile().m_nSamplePreAmp;
  1433. } else
  1434. {
  1435. // Preview at 0dB
  1436. m_WaveFile.SetMixLevels(MixLevels::v1_17RC3);
  1437. m_WaveFile.m_nSamplePreAmp = static_cast<uint32>(m_WaveFile.GetPlayConfig().getNormalSamplePreAmp());
  1438. }
  1439. // Avoid global volume ramping when trying samples in the treeview.
  1440. m_WaveFile.m_nDefaultGlobalVolume = m_WaveFile.m_PlayState.m_nGlobalVolume = (volume > 0) ? volume : MAX_GLOBAL_VOLUME;
  1441. if(m_WaveFile.Patterns.IsValidPat(0))
  1442. {
  1443. auto m = m_WaveFile.Patterns[0].GetRow(0);
  1444. if(m_WaveFile.GetNumSamples() > 0)
  1445. {
  1446. m[0].note = note;
  1447. m[0].instr = 1;
  1448. }
  1449. // Infinite loop on second row
  1450. m[1 * 2].command = CMD_POSITIONJUMP;
  1451. m[1 * 2 + 1].command = CMD_PATTERNBREAK;
  1452. m[1 * 2 + 1].param = 1;
  1453. }
  1454. m_WaveFile.InitPlayer(true);
  1455. }
  1456. HWND CMainFrame::GetFollowSong() const
  1457. {
  1458. return GetModPlaying() ? GetModPlaying()->GetFollowWnd() : NULL;
  1459. }
  1460. void CMainFrame::IdleHandlerSounddevice()
  1461. {
  1462. MPT_TRACE_SCOPE();
  1463. if(gpSoundDevice)
  1464. {
  1465. const FlagSet<SoundDevice::RequestFlags> requestFlags = gpSoundDevice->GetRequestFlags();
  1466. if(requestFlags[SoundDevice::RequestFlagClose])
  1467. {
  1468. StopPlayback();
  1469. audioCloseDevice();
  1470. } else if(requestFlags[SoundDevice::RequestFlagReset])
  1471. {
  1472. ResetSoundCard();
  1473. } else if(requestFlags[SoundDevice::RequestFlagRestart])
  1474. {
  1475. RestartPlayback();
  1476. } else
  1477. {
  1478. gpSoundDevice->OnIdle();
  1479. }
  1480. }
  1481. }
  1482. BOOL CMainFrame::ResetSoundCard()
  1483. {
  1484. MPT_TRACE_SCOPE();
  1485. return CMainFrame::SetupSoundCard(TrackerSettings::Instance().GetSoundDeviceSettings(TrackerSettings::Instance().GetSoundDeviceIdentifier()), TrackerSettings::Instance().GetSoundDeviceIdentifier(), TrackerSettings::Instance().m_SoundSettingsStopMode, true);
  1486. }
  1487. BOOL CMainFrame::SetupSoundCard(SoundDevice::Settings deviceSettings, SoundDevice::Identifier deviceIdentifier, SoundDeviceStopMode stoppedMode, bool forceReset)
  1488. {
  1489. MPT_TRACE_SCOPE();
  1490. if(forceReset
  1491. || (TrackerSettings::Instance().GetSoundDeviceIdentifier() != deviceIdentifier)
  1492. || (TrackerSettings::Instance().GetSoundDeviceSettings(deviceIdentifier) != deviceSettings)
  1493. || (TrackerSettings::Instance().m_SoundSettingsStopMode != stoppedMode)
  1494. )
  1495. {
  1496. CModDoc *pActiveMod = nullptr;
  1497. if(IsPlaying())
  1498. {
  1499. if ((m_pSndFile) && (!m_pSndFile->IsPaused())) pActiveMod = GetModPlaying();
  1500. PauseMod();
  1501. }
  1502. if(gpSoundDevice)
  1503. {
  1504. gpSoundDevice->Close();
  1505. }
  1506. TrackerSettings::Instance().m_SoundSettingsStopMode = stoppedMode;
  1507. switch(stoppedMode)
  1508. {
  1509. case SoundDeviceStopModeClosed:
  1510. deviceSettings.KeepDeviceRunning = true;
  1511. break;
  1512. case SoundDeviceStopModeStopped:
  1513. deviceSettings.KeepDeviceRunning = false;
  1514. break;
  1515. case SoundDeviceStopModePlaying:
  1516. deviceSettings.KeepDeviceRunning = true;
  1517. break;
  1518. }
  1519. TrackerSettings::Instance().SetSoundDeviceIdentifier(deviceIdentifier);
  1520. TrackerSettings::Instance().SetSoundDeviceSettings(deviceIdentifier, deviceSettings);
  1521. TrackerSettings::Instance().MixerOutputChannels = deviceSettings.Channels;
  1522. TrackerSettings::Instance().MixerNumInputChannels = deviceSettings.InputChannels;
  1523. TrackerSettings::Instance().MixerSamplerate = deviceSettings.Samplerate;
  1524. if(pActiveMod)
  1525. {
  1526. PlayMod(pActiveMod);
  1527. }
  1528. UpdateWindow();
  1529. } else
  1530. {
  1531. // No need to restart playback
  1532. CriticalSection cs;
  1533. if(GetSoundFilePlaying()) UpdateAudioParameters(*GetSoundFilePlaying(), FALSE);
  1534. }
  1535. return TRUE;
  1536. }
  1537. BOOL CMainFrame::SetupPlayer()
  1538. {
  1539. CriticalSection cs;
  1540. if(GetSoundFilePlaying()) UpdateAudioParameters(*GetSoundFilePlaying(), FALSE);
  1541. return TRUE;
  1542. }
  1543. BOOL CMainFrame::SetupMiscOptions()
  1544. {
  1545. if (TrackerSettings::Instance().m_dwPatternSetup & PATTERN_MUTECHNMODE)
  1546. TrackerSettings::Instance().MixerFlags |= SNDMIX_MUTECHNMODE;
  1547. else
  1548. TrackerSettings::Instance().MixerFlags &= ~SNDMIX_MUTECHNMODE;
  1549. {
  1550. CriticalSection cs;
  1551. if(GetSoundFilePlaying()) UpdateAudioParameters(*GetSoundFilePlaying());
  1552. }
  1553. m_wndToolBar.EnableFlatButtons(TrackerSettings::Instance().m_dwPatternSetup & PATTERN_FLATBUTTONS);
  1554. UpdateTree(nullptr, UpdateHint().MPTOptions());
  1555. UpdateAllViews(UpdateHint().MPTOptions());
  1556. return true;
  1557. }
  1558. void CMainFrame::SetupMidi(DWORD d, UINT n)
  1559. {
  1560. bool deviceChanged = (TrackerSettings::Instance().m_nMidiDevice != n);
  1561. TrackerSettings::Instance().m_dwMidiSetup = d;
  1562. TrackerSettings::Instance().SetMIDIDevice(n);
  1563. if(deviceChanged && shMidiIn)
  1564. {
  1565. // Device has changed, close the old one.
  1566. midiCloseDevice();
  1567. midiOpenDevice();
  1568. }
  1569. }
  1570. void CMainFrame::UpdateAllViews(UpdateHint hint, CObject *pHint)
  1571. {
  1572. CModDocTemplate *pDocTmpl = theApp.GetModDocTemplate();
  1573. if (pDocTmpl)
  1574. {
  1575. for(auto &doc : *pDocTmpl)
  1576. {
  1577. doc->UpdateAllViews(nullptr, hint, pHint);
  1578. }
  1579. }
  1580. }
  1581. void CMainFrame::SetUserText(LPCTSTR lpszText)
  1582. {
  1583. if (lpszText[0] | m_szUserText[0])
  1584. {
  1585. _tcscpy(m_szUserText, lpszText);
  1586. OnUpdateUser(nullptr);
  1587. }
  1588. }
  1589. void CMainFrame::SetInfoText(LPCTSTR lpszText)
  1590. {
  1591. if (lpszText[0] | m_szInfoText[0])
  1592. {
  1593. _tcscpy(m_szInfoText, lpszText);
  1594. OnUpdateInfo(nullptr);
  1595. }
  1596. }
  1597. void CMainFrame::SetXInfoText(LPCTSTR lpszText)
  1598. {
  1599. if (lpszText[0] | m_szXInfoText[0])
  1600. {
  1601. _tcscpy(m_szXInfoText, lpszText);
  1602. OnUpdateInfo(nullptr);
  1603. }
  1604. }
  1605. void CMainFrame::SetHelpText(LPCTSTR lpszText)
  1606. {
  1607. m_wndStatusBar.SetPaneText(0, lpszText);
  1608. }
  1609. void CMainFrame::OnDocumentCreated(CModDoc *pModDoc)
  1610. {
  1611. m_wndTree.OnDocumentCreated(pModDoc);
  1612. UpdateMRUList();
  1613. }
  1614. void CMainFrame::OnDocumentClosed(CModDoc *pModDoc)
  1615. {
  1616. if (pModDoc == GetModPlaying()) PauseMod();
  1617. m_wndTree.OnDocumentClosed(pModDoc);
  1618. }
  1619. void CMainFrame::UpdateTree(CModDoc *pModDoc, UpdateHint hint, CObject *pHint)
  1620. {
  1621. m_wndTree.OnUpdate(pModDoc, hint, pHint);
  1622. }
  1623. void CMainFrame::RefreshDlsBanks()
  1624. {
  1625. m_wndTree.RefreshDlsBanks();
  1626. }
  1627. /////////////////////////////////////////////////////////////////////////////
  1628. // CMainFrame message handlers
  1629. void CMainFrame::OnViewOptions()
  1630. {
  1631. if (m_bOptionsLocked)
  1632. return;
  1633. CPropertySheet dlg(_T("OpenMPT Setup"), this, m_nLastOptionsPage);
  1634. COptionsGeneral general;
  1635. COptionsSoundcard sounddlg(TrackerSettings::Instance().m_SoundDeviceIdentifier);
  1636. COptionsSampleEditor smpeditor;
  1637. COptionsKeyboard keyboard;
  1638. COptionsColors colors;
  1639. COptionsMixer mixerdlg;
  1640. CMidiSetupDlg mididlg(TrackerSettings::Instance().m_dwMidiSetup, TrackerSettings::Instance().GetCurrentMIDIDevice());
  1641. PathConfigDlg pathsdlg;
  1642. #if defined(MPT_ENABLE_UPDATE)
  1643. CUpdateSetupDlg updatedlg;
  1644. #endif // MPT_ENABLE_UPDATE
  1645. COptionsAdvanced advanced;
  1646. COptionsWine winedlg;
  1647. dlg.AddPage(&general);
  1648. dlg.AddPage(&sounddlg);
  1649. dlg.AddPage(&mixerdlg);
  1650. #if !defined(NO_REVERB) || !defined(NO_DSP) || !defined(NO_EQ) || !defined(NO_AGC)
  1651. COptionsPlayer dspdlg;
  1652. dlg.AddPage(&dspdlg);
  1653. #endif
  1654. dlg.AddPage(&smpeditor);
  1655. dlg.AddPage(&keyboard);
  1656. dlg.AddPage(&colors);
  1657. dlg.AddPage(&mididlg);
  1658. dlg.AddPage(&pathsdlg);
  1659. #if defined(MPT_ENABLE_UPDATE)
  1660. dlg.AddPage(&updatedlg);
  1661. #endif // MPT_ENABLE_UPDATE
  1662. dlg.AddPage(&advanced);
  1663. if(mpt::OS::Windows::IsWine()) dlg.AddPage(&winedlg);
  1664. m_bOptionsLocked = true;
  1665. m_SoundCardOptionsDialog = &sounddlg;
  1666. #if defined(MPT_ENABLE_UPDATE)
  1667. m_UpdateOptionsDialog = &updatedlg;
  1668. #endif // MPT_ENABLE_UPDATE
  1669. dlg.DoModal();
  1670. m_SoundCardOptionsDialog = nullptr;
  1671. #if defined(MPT_ENABLE_UPDATE)
  1672. m_UpdateOptionsDialog = nullptr;
  1673. #endif // MPT_ENABLE_UPDATE
  1674. m_bOptionsLocked = false;
  1675. m_wndTree.OnOptionsChanged();
  1676. }
  1677. void CMainFrame::OnPluginManager()
  1678. {
  1679. #ifndef NO_PLUGINS
  1680. PLUGINDEX nPlugslot = PLUGINDEX_INVALID;
  1681. CModDoc* pModDoc = GetActiveDoc();
  1682. if (pModDoc)
  1683. {
  1684. CSoundFile &sndFile = pModDoc->GetSoundFile();
  1685. //Find empty plugin slot
  1686. for (PLUGINDEX nPlug = 0; nPlug < MAX_MIXPLUGINS; nPlug++)
  1687. {
  1688. if (sndFile.m_MixPlugins[nPlug].pMixPlugin == nullptr)
  1689. {
  1690. nPlugslot = nPlug;
  1691. break;
  1692. }
  1693. }
  1694. }
  1695. CSelectPluginDlg dlg(GetActiveDoc(), nPlugslot, this);
  1696. if(dlg.DoModal() == IDOK && pModDoc)
  1697. {
  1698. pModDoc->SetModified();
  1699. //Refresh views
  1700. pModDoc->UpdateAllViews(nullptr, PluginHint().Info().Names().ModType());
  1701. //Refresh Controls
  1702. CChildFrame *pActiveChild = (CChildFrame *)MDIGetActive();
  1703. pActiveChild->ForceRefresh();
  1704. }
  1705. #endif // NO_PLUGINS
  1706. }
  1707. void CMainFrame::OnClipboardManager()
  1708. {
  1709. PatternClipboardDialog::Show();
  1710. }
  1711. void CMainFrame::OnAddDlsBank()
  1712. {
  1713. FileDialog dlg = OpenFileDialog()
  1714. .AllowMultiSelect()
  1715. .ExtensionFilter("All Sound Banks|*.dls;*.sbk;*.sf2;*.sf3;*.sf4;*.mss|"
  1716. "Downloadable Sounds Banks (*.dls)|*.dls;*.mss|"
  1717. "SoundFont 2.0 Banks (*.sf2)|*.sbk;*.sf2;*.sf3;*.sf4|"
  1718. "All Files (*.*)|*.*||");
  1719. if(!dlg.Show()) return;
  1720. BeginWaitCursor();
  1721. bool ok = true;
  1722. for(const auto &file : dlg.GetFilenames())
  1723. {
  1724. ok &= CTrackApp::AddDLSBank(file);
  1725. }
  1726. if(!ok)
  1727. {
  1728. Reporting::Error("At least one selected file was not a valid sound bank.");
  1729. }
  1730. m_wndTree.RefreshDlsBanks();
  1731. EndWaitCursor();
  1732. }
  1733. void CMainFrame::OnImportMidiLib()
  1734. {
  1735. FileDialog dlg = OpenFileDialog()
  1736. .ExtensionFilter("Text and INI files (*.txt,*.ini)|*.txt;*.ini;*.dls;*.sf2;*.sf3;*.sf4;*.sbk|"
  1737. "Downloadable Sound Banks (*.dls)|*.dls;*.mss|"
  1738. "SoundFont 2.0 banks (*.sf2)|*.sbk;*.sf2;*.sf3;*.sf4|"
  1739. "Gravis UltraSound (ultrasnd.ini)|ultrasnd.ini|"
  1740. "All Files (*.*)|*.*||");
  1741. if(!dlg.Show()) return;
  1742. BeginWaitCursor();
  1743. CTrackApp::ImportMidiConfig(dlg.GetFirstFile());
  1744. m_wndTree.RefreshMidiLibrary();
  1745. EndWaitCursor();
  1746. }
  1747. void CMainFrame::OnTimer(UINT_PTR timerID)
  1748. {
  1749. switch(timerID)
  1750. {
  1751. case TIMERID_GUI:
  1752. OnTimerGUI();
  1753. break;
  1754. case TIMERID_NOTIFY:
  1755. OnTimerNotify();
  1756. break;
  1757. }
  1758. }
  1759. void CMainFrame::OnTimerGUI()
  1760. {
  1761. IdleHandlerSounddevice();
  1762. // Display Time in status bar
  1763. CSoundFile::samplecount_t time = 0;
  1764. if(m_pSndFile != nullptr && m_pSndFile->GetSampleRate() != 0)
  1765. {
  1766. time = m_pSndFile->GetTotalSampleCount() / m_pSndFile->GetSampleRate();
  1767. if(time != m_dwTimeSec)
  1768. {
  1769. m_dwTimeSec = time;
  1770. m_nAvgMixChn = m_nMixChn;
  1771. OnUpdateTime(NULL);
  1772. }
  1773. }
  1774. // Idle Time Check
  1775. DWORD curTime = timeGetTime();
  1776. if(m_AutoSaver.IsEnabled())
  1777. {
  1778. bool success = m_AutoSaver.DoSave(curTime);
  1779. if (!success) // autosave failure; bring up options.
  1780. {
  1781. CMainFrame::m_nLastOptionsPage = OPTIONS_PAGE_PATHS;
  1782. OnViewOptions();
  1783. }
  1784. }
  1785. if(m_SoundCardOptionsDialog)
  1786. {
  1787. m_SoundCardOptionsDialog->UpdateStatistics();
  1788. }
  1789. #ifdef USE_PROFILER
  1790. {
  1791. Profiler::Update();
  1792. CWnd * cwnd = CWnd::FromHandle(this->m_hWndMDIClient);
  1793. CClientDC dc(cwnd);
  1794. int height = 16;
  1795. int width = 256;
  1796. std::vector<std::string> catnames = Profiler::GetCategoryNames();
  1797. std::vector<double> cats = Profiler::DumpCategories();
  1798. for(int i=0; i<Profiler::CategoriesCount; i++)
  1799. {
  1800. dc.FillSolidRect(0, i * height, (int)(width * cats[i]), height, RGB(255,0,0));
  1801. dc.FillSolidRect((int)(width * cats[i]), i * height, width - (int)(width * cats[i]), height, RGB(192,192,192));
  1802. RECT rect;
  1803. cwnd->GetClientRect(&rect);
  1804. rect.left += width;
  1805. rect.top += i * height;
  1806. auto s = mpt::ToCString(mpt::Charset::ASCII, mpt::afmt::right(6, mpt::afmt::fix(cats[i] * 100.0, 3)) + "% " + catnames[i]);
  1807. dc.DrawText(s, s.GetLength(), &rect, DT_LEFT);
  1808. }
  1809. RECT rect;
  1810. cwnd->GetClientRect(&rect);
  1811. rect.top += Profiler::CategoriesCount * height;
  1812. auto s = mpt::ToCString(mpt::Charset::ASCII, Profiler::DumpProfiles());
  1813. dc.DrawText(s, s.GetLength(), &rect, DT_LEFT);
  1814. cwnd->Detach();
  1815. }
  1816. #endif
  1817. }
  1818. CModDoc *CMainFrame::GetActiveDoc() const
  1819. {
  1820. CMDIChildWnd *pMDIActive = MDIGetActive();
  1821. if (pMDIActive)
  1822. {
  1823. return static_cast<CModDoc *>(pMDIActive->GetActiveDocument());
  1824. }
  1825. return nullptr;
  1826. }
  1827. CView *CMainFrame::GetActiveView() const
  1828. {
  1829. CMDIChildWnd *pMDIActive = MDIGetActive();
  1830. if (pMDIActive)
  1831. {
  1832. return pMDIActive->GetActiveView();
  1833. }
  1834. return nullptr;
  1835. }
  1836. void CMainFrame::SwitchToActiveView()
  1837. {
  1838. CWnd *wnd = GetActiveView();
  1839. if (wnd)
  1840. {
  1841. // Hack: If the upper view is active, we only get the "container" (the dialog view with the tabs), not the view itself.
  1842. if(!strcmp(wnd->GetRuntimeClass()->m_lpszClassName, "CModControlView"))
  1843. {
  1844. wnd = static_cast<CModControlView *>(wnd)->GetCurrentControlDlg();
  1845. }
  1846. wnd->SetFocus();
  1847. }
  1848. }
  1849. void CMainFrame::OnUpdateTime(CCmdUI *)
  1850. {
  1851. TCHAR s[64];
  1852. wsprintf(s, _T("%u:%02u:%02u"),
  1853. m_dwTimeSec / 3600, (m_dwTimeSec / 60) % 60, m_dwTimeSec % 60);
  1854. if(m_pSndFile != nullptr && m_pSndFile != &m_WaveFile && !m_pSndFile->IsPaused())
  1855. {
  1856. PATTERNINDEX nPat = m_pSndFile->m_PlayState.m_nPattern;
  1857. if(m_pSndFile->Patterns.IsValidIndex(nPat))
  1858. {
  1859. if(nPat < 10) _tcscat(s, _T(" "));
  1860. if(nPat < 100) _tcscat(s, _T(" "));
  1861. wsprintf(s + _tcslen(s), _T(" [%d]"), nPat);
  1862. }
  1863. wsprintf(s + _tcslen(s), _T(" %dch"), m_nAvgMixChn);
  1864. }
  1865. m_wndStatusBar.SetPaneText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_TIME), s, TRUE);
  1866. }
  1867. void CMainFrame::OnUpdateUser(CCmdUI *)
  1868. {
  1869. m_wndStatusBar.SetPaneText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_USER), m_szUserText, TRUE);
  1870. }
  1871. void CMainFrame::OnUpdateInfo(CCmdUI *)
  1872. {
  1873. m_wndStatusBar.SetPaneText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_INFO), m_szInfoText, TRUE);
  1874. }
  1875. void CMainFrame::OnUpdateXInfo(CCmdUI *)
  1876. {
  1877. m_wndStatusBar.SetPaneText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_XINFO), m_szXInfoText, TRUE);
  1878. }
  1879. void CMainFrame::OnPlayerPause()
  1880. {
  1881. if (GetModPlaying())
  1882. {
  1883. GetModPlaying()->OnPlayerPause();
  1884. } else
  1885. {
  1886. PauseMod();
  1887. }
  1888. }
  1889. void CMainFrame::OpenMenuItemFile(const UINT nId, const bool isTemplateFile)
  1890. {
  1891. const UINT nIdBegin = (isTemplateFile) ? ID_FILE_OPENTEMPLATE : ID_EXAMPLE_MODULES;
  1892. const std::vector<mpt::PathString> &vecFilePaths = (isTemplateFile) ? m_TemplateModulePaths : m_ExampleModulePaths;
  1893. const UINT nIndex = nId - nIdBegin;
  1894. if (nIndex < vecFilePaths.size())
  1895. {
  1896. const mpt::PathString& sPath = vecFilePaths[nIndex];
  1897. const bool bExists = sPath.IsFile();
  1898. CDocument *pDoc = nullptr;
  1899. if(bExists)
  1900. {
  1901. pDoc = theApp.GetModDocTemplate()->OpenTemplateFile(sPath, !isTemplateFile);
  1902. }
  1903. if(!pDoc)
  1904. {
  1905. Reporting::Notification(_T("The file '") + sPath.ToCString() + _T("' ") + (bExists ? _T("exists but can't be read.") : _T("does not exist.")));
  1906. }
  1907. } else
  1908. {
  1909. MPT_ASSERT(nId == UINT(isTemplateFile ? ID_FILE_OPENTEMPLATE_LASTINRANGE : ID_EXAMPLE_MODULES_LASTINRANGE));
  1910. FileDialog::PathList files;
  1911. theApp.OpenModulesDialog(files, isTemplateFile ? theApp.GetConfigPath() + P_("TemplateModules") : theApp.GetInstallPath() + P_("ExampleSongs"));
  1912. for(const auto &file : files)
  1913. {
  1914. theApp.OpenDocumentFile(file.ToCString());
  1915. }
  1916. }
  1917. }
  1918. void CMainFrame::OnOpenTemplateModule(UINT nId)
  1919. {
  1920. OpenMenuItemFile(nId, true/*open template menu file*/);
  1921. }
  1922. void CMainFrame::OnExampleSong(UINT nId)
  1923. {
  1924. OpenMenuItemFile(nId, false/*open example menu file*/);
  1925. }
  1926. void CMainFrame::OnOpenMRUItem(UINT nId)
  1927. {
  1928. theApp.OpenDocumentFile(TrackerSettings::Instance().mruFiles[nId - ID_MRU_LIST_FIRST].ToCString());
  1929. }
  1930. void CMainFrame::OnUpdateMRUItem(CCmdUI *cmd)
  1931. {
  1932. cmd->Enable(!TrackerSettings::Instance().mruFiles.empty());
  1933. }
  1934. LRESULT CMainFrame::OnInvalidatePatterns(WPARAM, LPARAM)
  1935. {
  1936. UpdateAllViews(UpdateHint().MPTOptions());
  1937. return TRUE;
  1938. }
  1939. LRESULT CMainFrame::OnUpdatePosition(WPARAM, LPARAM lParam)
  1940. {
  1941. OPENMPT_PROFILE_FUNCTION(Profiler::GUI);
  1942. m_VUMeterOutput.SetDecaySpeedDecibelPerSecond(TrackerSettings::Instance().VuMeterDecaySpeedDecibelPerSecond); // update in notification update in order to avoid querying the settings framework from inside audio thread
  1943. m_VUMeterInput.SetDecaySpeedDecibelPerSecond(TrackerSettings::Instance().VuMeterDecaySpeedDecibelPerSecond); // update in notification update in order to avoid querying the settings framework from inside audio thread
  1944. Notification *pnotify = (Notification *)lParam;
  1945. if (pnotify)
  1946. {
  1947. if(pnotify->type[Notification::EOS])
  1948. {
  1949. PostMessage(WM_COMMAND, ID_PLAYER_STOP);
  1950. }
  1951. //Log("OnUpdatePosition: row=%d time=%lu\n", pnotify->nRow, pnotify->TimestampSamples);
  1952. if(CModDoc *modDoc = GetModPlaying(); modDoc != nullptr)
  1953. {
  1954. m_wndTree.UpdatePlayPos(modDoc, pnotify);
  1955. if (GetFollowSong())
  1956. ::SendMessage(GetFollowSong(), WM_MOD_UPDATEPOSITION, 0, lParam);
  1957. if(m_pSndFile->m_pluginDryWetRatioChanged.any())
  1958. {
  1959. for(PLUGINDEX i = 0; i < MAX_MIXPLUGINS; i++)
  1960. {
  1961. if(m_pSndFile->m_pluginDryWetRatioChanged[i])
  1962. modDoc->PostMessageToAllViews(WM_MOD_PLUGINDRYWETRATIOCHANGED, i);
  1963. }
  1964. m_pSndFile->m_pluginDryWetRatioChanged.reset();
  1965. }
  1966. }
  1967. m_nMixChn = pnotify->mixedChannels;
  1968. bool duplicateMono = false;
  1969. if(pnotify->masterVUinChannels == 1 && pnotify->masterVUoutChannels > 1)
  1970. {
  1971. duplicateMono = true;
  1972. } else if(pnotify->masterVUoutChannels == 1 && pnotify->masterVUinChannels > 1)
  1973. {
  1974. duplicateMono = true;
  1975. }
  1976. uint8 countChan = 0;
  1977. uint32 vu[VUMeter::maxChannels * 2];
  1978. MemsetZero(vu);
  1979. std::copy(pnotify->masterVUin.begin(), pnotify->masterVUin.begin() + pnotify->masterVUinChannels, vu + countChan);
  1980. countChan += pnotify->masterVUinChannels;
  1981. if(pnotify->masterVUinChannels == 1 && duplicateMono)
  1982. {
  1983. std::copy(pnotify->masterVUin.begin(), pnotify->masterVUin.begin() + 1, vu + countChan);
  1984. countChan += 1;
  1985. }
  1986. std::copy(pnotify->masterVUout.begin(), pnotify->masterVUout.begin() + pnotify->masterVUoutChannels, vu + countChan);
  1987. countChan += pnotify->masterVUoutChannels;
  1988. if(pnotify->masterVUoutChannels == 1 && duplicateMono)
  1989. {
  1990. std::copy(pnotify->masterVUout.begin(), pnotify->masterVUout.begin() + 1, vu + countChan);
  1991. countChan += 1;
  1992. }
  1993. m_wndToolBar.m_VuMeter.SetVuMeter(countChan, vu, pnotify->type[Notification::Stop]);
  1994. m_wndToolBar.SetCurrentSong(m_pSndFile);
  1995. }
  1996. return 0;
  1997. }
  1998. LRESULT CMainFrame::OnUpdateViews(WPARAM modDoc, LPARAM hint)
  1999. {
  2000. CModDoc *doc = reinterpret_cast<CModDoc *>(modDoc);
  2001. CModDocTemplate *pDocTmpl = theApp.GetModDocTemplate();
  2002. if(pDocTmpl && pDocTmpl->DocumentExists(doc))
  2003. {
  2004. // Since this message is potentially posted, we first need to verify if the document still exists
  2005. doc->UpdateAllViews(nullptr, UpdateHint::FromLPARAM(hint));
  2006. }
  2007. return 0;
  2008. }
  2009. LRESULT CMainFrame::OnSetModified(WPARAM modDoc, LPARAM)
  2010. {
  2011. CModDoc *doc = reinterpret_cast<CModDoc *>(modDoc);
  2012. CModDocTemplate *pDocTmpl = theApp.GetModDocTemplate();
  2013. if(pDocTmpl && pDocTmpl->DocumentExists(doc))
  2014. {
  2015. // Since this message is potentially posted, we first need to verify if the document still exists
  2016. doc->UpdateFrameCounts();
  2017. }
  2018. return 0;
  2019. }
  2020. void CMainFrame::OnPanic()
  2021. {
  2022. // "Panic button." At the moment, it just resets all VSTi and sample notes.
  2023. if(GetModPlaying())
  2024. GetModPlaying()->OnPanic();
  2025. }
  2026. void CMainFrame::OnPrevOctave()
  2027. {
  2028. UINT n = GetBaseOctave();
  2029. if (n > MIN_BASEOCTAVE) m_wndToolBar.SetBaseOctave(n-1);
  2030. }
  2031. void CMainFrame::OnNextOctave()
  2032. {
  2033. UINT n = GetBaseOctave();
  2034. if (n < MAX_BASEOCTAVE) m_wndToolBar.SetBaseOctave(n+1);
  2035. }
  2036. void CMainFrame::OnReportBug()
  2037. {
  2038. CTrackApp::OpenURL(Build::GetURL(Build::Url::Bugtracker));
  2039. }
  2040. BOOL CMainFrame::OnInternetLink(UINT nID)
  2041. {
  2042. mpt::ustring url;
  2043. switch(nID)
  2044. {
  2045. case ID_NETLINK_MODPLUG: url = Build::GetURL(Build::Url::Website); break;
  2046. case ID_NETLINK_TOP_PICKS: url = Build::GetURL(Build::Url::TopPicks); break;
  2047. }
  2048. if(!url.empty())
  2049. {
  2050. return CTrackApp::OpenURL(url) ? TRUE : FALSE;
  2051. }
  2052. return FALSE;
  2053. }
  2054. void CMainFrame::OnRButtonDown(UINT, CPoint pt)
  2055. {
  2056. CMenu Menu;
  2057. ClientToScreen(&pt);
  2058. if (Menu.LoadMenu(IDR_TOOLBARS))
  2059. {
  2060. CMenu *pSubMenu = Menu.GetSubMenu(0);
  2061. if (pSubMenu != nullptr) pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, this);
  2062. }
  2063. }
  2064. LRESULT CMainFrame::OnCustomKeyMsg(WPARAM wParam, LPARAM lParam)
  2065. {
  2066. switch(wParam)
  2067. {
  2068. case kcViewTree: OnBarCheck(IDD_TREEVIEW); break;
  2069. case kcViewOptions: OnViewOptions(); break;
  2070. case kcViewMain: OnBarCheck(59392 /* MAINVIEW */); break;
  2071. case kcFileImportMidiLib: OnImportMidiLib(); break;
  2072. case kcFileAddSoundBank: OnAddDlsBank(); break;
  2073. case kcPauseSong: OnPlayerPause(); break;
  2074. case kcPrevOctave: OnPrevOctave(); break;
  2075. case kcNextOctave: OnNextOctave(); break;
  2076. case kcFileNew: theApp.OnFileNew(); break;
  2077. case kcFileOpen: theApp.OnFileOpen(); break;
  2078. case kcMidiRecord: OnMidiRecord(); break;
  2079. case kcHelp: OnHelp(); break;
  2080. case kcViewAddPlugin: OnPluginManager(); break;
  2081. case kcNextDocument: MDINext(); break;
  2082. case kcPrevDocument: MDIPrev(); break;
  2083. case kcFileCloseAll: theApp.OnFileCloseAll(); break;
  2084. //D'oh!! moddoc isn't a CWnd so we have to handle its messages and pass them on.
  2085. case kcFileSaveAs:
  2086. case kcFileSaveCopy:
  2087. case kcFileSaveAsWave:
  2088. case kcFileSaveMidi:
  2089. case kcFileSaveOPL:
  2090. case kcFileExportCompat:
  2091. case kcFileClose:
  2092. case kcFileSave:
  2093. case kcViewGeneral:
  2094. case kcViewPattern:
  2095. case kcViewSamples:
  2096. case kcViewInstruments:
  2097. case kcViewComments:
  2098. case kcViewGraph: //rewbs.graph
  2099. case kcViewSongProperties:
  2100. case kcViewTempoSwing:
  2101. case kcViewMIDImapping:
  2102. case kcViewEditHistory:
  2103. case kcViewChannelManager:
  2104. case kcPlayPatternFromCursor:
  2105. case kcPlayPatternFromStart:
  2106. case kcPlaySongFromCursor:
  2107. case kcPlaySongFromStart:
  2108. case kcPlayPauseSong:
  2109. case kcPlaySongFromPattern:
  2110. case kcStopSong:
  2111. case kcToggleLoopSong:
  2112. case kcPanic:
  2113. case kcEstimateSongLength:
  2114. case kcApproxRealBPM:
  2115. case kcTempoIncrease:
  2116. case kcTempoDecrease:
  2117. case kcTempoIncreaseFine:
  2118. case kcTempoDecreaseFine:
  2119. case kcSpeedIncrease:
  2120. case kcSpeedDecrease:
  2121. case kcViewToggle:
  2122. {
  2123. CModDoc *modDoc = GetActiveDoc();
  2124. if (modDoc)
  2125. return GetActiveDoc()->OnCustomKeyMsg(wParam, lParam);
  2126. else if(wParam == kcPlayPauseSong || wParam == kcStopSong)
  2127. StopPreview();
  2128. break;
  2129. }
  2130. case kcSwitchToInstrLibrary:
  2131. if(m_bModTreeHasFocus)
  2132. SwitchToActiveView();
  2133. else
  2134. m_wndTree.SetFocus();
  2135. break;
  2136. //if handled neither by MainFrame nor by ModDoc...
  2137. default:
  2138. //If the treeview has focus, post command to the tree view
  2139. if (m_bModTreeHasFocus)
  2140. return m_wndTree.SendMessageToModTree(WM_MOD_KEYCOMMAND, wParam, lParam);
  2141. if (m_pNoteMapHasFocus)
  2142. return m_pNoteMapHasFocus->SendMessage(WM_MOD_KEYCOMMAND, wParam, lParam);
  2143. if (m_pOrderlistHasFocus)
  2144. return m_pOrderlistHasFocus->SendMessage(WM_MOD_KEYCOMMAND, wParam, lParam);
  2145. //Else send it to the active view
  2146. CMDIChildWnd *pMDIActive = MDIGetActive();
  2147. CWnd *wnd = nullptr;
  2148. if(pMDIActive)
  2149. {
  2150. wnd = pMDIActive->GetActiveView();
  2151. // Hack: If the upper view is active, we only get the "container" (the dialog view with the tabs), not the view itself.
  2152. if(!strcmp(wnd->GetRuntimeClass()->m_lpszClassName, "CModControlView"))
  2153. {
  2154. wnd = static_cast<CModControlView *>(wnd)->GetCurrentControlDlg();
  2155. }
  2156. }
  2157. if(wnd)
  2158. return wnd->SendMessage(WM_MOD_KEYCOMMAND, wParam, lParam);
  2159. return kcNull;
  2160. }
  2161. return wParam;
  2162. }
  2163. void CMainFrame::OnInitMenu(CMenu* pMenu)
  2164. {
  2165. m_InputHandler->SetModifierMask(ModNone);
  2166. CMDIFrameWnd::OnInitMenu(pMenu);
  2167. }
  2168. BOOL CMainFrame::InitRenderer(CSoundFile* pSndFile)
  2169. {
  2170. CriticalSection cs;
  2171. pSndFile->m_bIsRendering = true;
  2172. pSndFile->SuspendPlugins();
  2173. pSndFile->ResumePlugins();
  2174. return true;
  2175. }
  2176. BOOL CMainFrame::StopRenderer(CSoundFile* pSndFile)
  2177. {
  2178. CriticalSection cs;
  2179. pSndFile->SuspendPlugins();
  2180. pSndFile->m_bIsRendering = false;
  2181. return true;
  2182. }
  2183. // We have switched focus to a new module - might need to update effect keys to reflect module type
  2184. bool CMainFrame::UpdateEffectKeys(const CModDoc *modDoc)
  2185. {
  2186. if(modDoc != nullptr)
  2187. {
  2188. return m_InputHandler->SetEffectLetters(modDoc->GetSoundFile().GetModSpecifications());
  2189. }
  2190. return false;
  2191. }
  2192. void CMainFrame::OnKillFocus(CWnd* pNewWnd)
  2193. {
  2194. CMDIFrameWnd::OnKillFocus(pNewWnd);
  2195. //rewbs: ensure modifiers are reset when we leave the window (e.g. alt-tab)
  2196. m_InputHandler->SetModifierMask(ModNone);
  2197. }
  2198. void CMainFrame::OnShowWindow(BOOL bShow, UINT /*nStatus*/)
  2199. {
  2200. static bool firstShow = true;
  2201. if (bShow && !IsWindowVisible() && firstShow)
  2202. {
  2203. firstShow = false;
  2204. WINDOWPLACEMENT wpl;
  2205. GetWindowPlacement(&wpl);
  2206. wpl = theApp.GetSettings().Read<WINDOWPLACEMENT>(U_("Display"), U_("WindowPlacement"), wpl);
  2207. SetWindowPlacement(&wpl);
  2208. }
  2209. }
  2210. void CMainFrame::OnInternetUpdate()
  2211. {
  2212. #if defined(MPT_ENABLE_UPDATE)
  2213. CUpdateCheck::DoManualUpdateCheck();
  2214. #endif // MPT_ENABLE_UPDATE
  2215. }
  2216. void CMainFrame::OnUpdateAvailable()
  2217. {
  2218. #if defined(MPT_ENABLE_UPDATE)
  2219. if(m_updateCheckResult)
  2220. CUpdateCheck::ShowSuccessGUI(false, *m_updateCheckResult);
  2221. else
  2222. CUpdateCheck::DoManualUpdateCheck();
  2223. #endif // MPT_ENABLE_UPDATE
  2224. }
  2225. void CMainFrame::OnShowSettingsFolder()
  2226. {
  2227. theApp.OpenDirectory(theApp.GetConfigPath());
  2228. }
  2229. class CUpdateCheckProgressDialog
  2230. : public CProgressDialog
  2231. {
  2232. public:
  2233. CUpdateCheckProgressDialog(CWnd *parent)
  2234. : CProgressDialog(parent)
  2235. {
  2236. return;
  2237. }
  2238. void Run() override
  2239. {
  2240. }
  2241. };
  2242. static std::unique_ptr<CUpdateCheckProgressDialog> g_UpdateCheckProgressDialog = nullptr;
  2243. #if defined(MPT_ENABLE_UPDATE)
  2244. bool CMainFrame::ShowUpdateIndicator(const UpdateCheckResult &result, const CString &releaseVersion, const CString &infoURL, bool showHighlight)
  2245. {
  2246. m_updateCheckResult = std::make_unique<UpdateCheckResult>(result);
  2247. if(m_wndToolBar.IsVisible())
  2248. {
  2249. return m_wndToolBar.ShowUpdateInfo(releaseVersion, infoURL, showHighlight);
  2250. } else
  2251. {
  2252. GetMenu()->RemoveMenu(ID_UPDATE_AVAILABLE, MF_BYCOMMAND);
  2253. GetMenu()->AppendMenu(MF_STRING, ID_UPDATE_AVAILABLE, _T("[Update Available]"));
  2254. DrawMenuBar();
  2255. return true;
  2256. }
  2257. }
  2258. LRESULT CMainFrame::OnUpdateCheckStart(WPARAM wparam, LPARAM lparam)
  2259. {
  2260. GetMenu()->RemoveMenu(ID_UPDATE_AVAILABLE, MF_BYCOMMAND);
  2261. m_wndToolBar.RemoveUpdateInfo();
  2262. const bool isAutoUpdate = CUpdateCheck::IsAutoUpdateFromMessage(wparam, lparam);
  2263. CString updateText = _T("Checking for updates...");
  2264. if(isAutoUpdate)
  2265. {
  2266. SetHelpText(updateText);
  2267. } else if(m_UpdateOptionsDialog)
  2268. {
  2269. m_UpdateOptionsDialog->SetDlgItemText(IDC_LASTUPDATE, updateText);
  2270. } else
  2271. {
  2272. if(!g_UpdateCheckProgressDialog)
  2273. {
  2274. g_UpdateCheckProgressDialog = std::make_unique<CUpdateCheckProgressDialog>(CMainFrame::GetMainFrame());
  2275. g_UpdateCheckProgressDialog->Create(IDD_PROGRESS, CMainFrame::GetMainFrame());
  2276. g_UpdateCheckProgressDialog->SetTitle(_T("Checking for updates..."));
  2277. g_UpdateCheckProgressDialog->SetText(_T("Checking for updates..."));
  2278. g_UpdateCheckProgressDialog->SetAbortText(_T("&Cancel"));
  2279. g_UpdateCheckProgressDialog->SetRange(0, 100);
  2280. g_UpdateCheckProgressDialog->ShowWindow(SW_SHOWDEFAULT);
  2281. }
  2282. if(g_UpdateCheckProgressDialog)
  2283. {
  2284. g_UpdateCheckProgressDialog->SetProgress(0);
  2285. }
  2286. }
  2287. return TRUE;
  2288. }
  2289. LRESULT CMainFrame::OnUpdateCheckProgress(WPARAM wparam, LPARAM lparam)
  2290. {
  2291. bool isAutoUpdate = wparam != 0;
  2292. CString updateText = MPT_CFORMAT("Checking for updates... {}%")(lparam);
  2293. if(isAutoUpdate)
  2294. {
  2295. SetHelpText(updateText);
  2296. } else if(m_UpdateOptionsDialog)
  2297. {
  2298. m_UpdateOptionsDialog->SetDlgItemText(IDC_LASTUPDATE, updateText);
  2299. } else
  2300. {
  2301. if(g_UpdateCheckProgressDialog)
  2302. {
  2303. g_UpdateCheckProgressDialog->SetProgress(lparam);
  2304. if(g_UpdateCheckProgressDialog->m_abort)
  2305. {
  2306. return FALSE;
  2307. }
  2308. }
  2309. }
  2310. return TRUE;
  2311. }
  2312. LRESULT CMainFrame::OnUpdateCheckCanceled(WPARAM wparam, LPARAM lparam)
  2313. {
  2314. m_updateCheckResult.reset();
  2315. bool isAutoUpdate = CUpdateCheck::IsAutoUpdateFromMessage(wparam, lparam);
  2316. CString updateText = _T("Checking for updates... Canceled.");
  2317. if(isAutoUpdate)
  2318. {
  2319. SetHelpText(updateText);
  2320. } else if(m_UpdateOptionsDialog)
  2321. {
  2322. m_UpdateOptionsDialog->SetDlgItemText(IDC_LASTUPDATE, updateText);
  2323. m_UpdateOptionsDialog->SettingChanged(TrackerSettings::Instance().UpdateLastUpdateCheck.GetPath());
  2324. } else
  2325. {
  2326. if(g_UpdateCheckProgressDialog)
  2327. {
  2328. g_UpdateCheckProgressDialog->DestroyWindow();
  2329. g_UpdateCheckProgressDialog = nullptr;
  2330. }
  2331. }
  2332. if(isAutoUpdate)
  2333. {
  2334. SetHelpText(_T(""));
  2335. }
  2336. return TRUE;
  2337. }
  2338. LRESULT CMainFrame::OnUpdateCheckFailure(WPARAM wparam, LPARAM lparam)
  2339. {
  2340. m_updateCheckResult.reset();
  2341. const bool isAutoUpdate = CUpdateCheck::IsAutoUpdateFromMessage(wparam, lparam);
  2342. const CUpdateCheck::Error &error = CUpdateCheck::MessageAsError(wparam, lparam);
  2343. CString updateText = MPT_CFORMAT("Checking for updates failed: {}")(mpt::get_exception_text<mpt::ustring>(error));
  2344. if(isAutoUpdate)
  2345. {
  2346. SetHelpText(updateText);
  2347. } else if(m_UpdateOptionsDialog)
  2348. {
  2349. m_UpdateOptionsDialog->SetDlgItemText(IDC_LASTUPDATE, updateText);
  2350. m_UpdateOptionsDialog->SettingChanged(TrackerSettings::Instance().UpdateLastUpdateCheck.GetPath());
  2351. } else
  2352. {
  2353. if(g_UpdateCheckProgressDialog)
  2354. {
  2355. g_UpdateCheckProgressDialog->DestroyWindow();
  2356. g_UpdateCheckProgressDialog = nullptr;
  2357. }
  2358. }
  2359. CUpdateCheck::ShowFailureGUI(isAutoUpdate, error);
  2360. if(isAutoUpdate)
  2361. {
  2362. SetHelpText(_T(""));
  2363. }
  2364. return TRUE;
  2365. }
  2366. LRESULT CMainFrame::OnUpdateCheckSuccess(WPARAM wparam, LPARAM lparam)
  2367. {
  2368. m_updateCheckResult.reset();
  2369. const bool isAutoUpdate = CUpdateCheck::IsAutoUpdateFromMessage(wparam, lparam);
  2370. const UpdateCheckResult &result = CUpdateCheck::MessageAsResult(wparam, lparam);
  2371. CUpdateCheck::AcknowledgeSuccess(result);
  2372. if(result.CheckTime != time_t{})
  2373. TrackerSettings::Instance().UpdateLastUpdateCheck = mpt::Date::Unix(result.CheckTime);
  2374. if(!isAutoUpdate)
  2375. {
  2376. if(m_UpdateOptionsDialog)
  2377. {
  2378. m_UpdateOptionsDialog->SetDlgItemText(IDC_LASTUPDATE, _T("Checking for updates... Done."));
  2379. } else
  2380. {
  2381. if(g_UpdateCheckProgressDialog)
  2382. {
  2383. g_UpdateCheckProgressDialog->DestroyWindow();
  2384. g_UpdateCheckProgressDialog = nullptr;
  2385. }
  2386. SetHelpText(_T(""));
  2387. }
  2388. }
  2389. CUpdateCheck::ShowSuccessGUI(isAutoUpdate, result);
  2390. if(isAutoUpdate)
  2391. {
  2392. SetHelpText(_T(""));
  2393. }
  2394. return TRUE;
  2395. }
  2396. LPARAM CMainFrame::OnToolbarUpdateIndicatorClick(WPARAM action, LPARAM)
  2397. {
  2398. const auto popAction = static_cast<UpdateToolTip::PopAction>(action);
  2399. if(popAction != UpdateToolTip::PopAction::ClickBubble)
  2400. return 0;
  2401. if(m_updateCheckResult)
  2402. CUpdateCheck::ShowSuccessGUI(false, *m_updateCheckResult);
  2403. else
  2404. CUpdateCheck::DoManualUpdateCheck();
  2405. return 0;
  2406. }
  2407. #endif // MPT_ENABLE_UPDATE
  2408. void CMainFrame::OnHelp()
  2409. {
  2410. CView *view = GetActiveView();
  2411. const char *page = "";
  2412. if(m_bOptionsLocked)
  2413. {
  2414. switch(m_nLastOptionsPage)
  2415. {
  2416. case OPTIONS_PAGE_GENERAL: page = "::/Setup_General.html"; break;
  2417. case OPTIONS_PAGE_SOUNDCARD: page = "::/Setup_Soundcard.html"; break;
  2418. case OPTIONS_PAGE_MIXER: page = "::/Setup_Mixer.html"; break;
  2419. case OPTIONS_PAGE_PLAYER: page = "::/Setup_DSP.html"; break;
  2420. case OPTIONS_PAGE_SAMPLEDITOR: page = "::/Setup_Samples.html"; break;
  2421. case OPTIONS_PAGE_KEYBOARD: page = "::/Setup_Keyboard.html"; break;
  2422. case OPTIONS_PAGE_COLORS: page = "::/Setup_Display.html"; break;
  2423. case OPTIONS_PAGE_MIDI: page = "::/Setup_MIDI.html"; break;
  2424. case OPTIONS_PAGE_PATHS: page = "::/Setup_Paths_Auto_Save.html"; break;
  2425. case OPTIONS_PAGE_UPDATE: page = "::/Setup_Update.html"; break;
  2426. case OPTIONS_PAGE_ADVANCED: page = "::/Setup_Advanced.html"; break;
  2427. case OPTIONS_PAGE_WINE: page = "::/Setup_Wine.html"; break;
  2428. }
  2429. } else if(view != nullptr)
  2430. {
  2431. const char *className = view->GetRuntimeClass()->m_lpszClassName;
  2432. if(!strcmp("CViewGlobals", className))
  2433. page = "::/General.html";
  2434. else if(!strcmp("CViewPattern", className))
  2435. page = "::/Patterns.html";
  2436. else if(!strcmp("CViewSample", className))
  2437. page = "::/Samples.html";
  2438. else if(!strcmp("CViewInstrument", className))
  2439. page = "::/Instruments.html";
  2440. else if(!strcmp("CViewComments", className))
  2441. page = "::/Comments.html";
  2442. else if(!strcmp("CModControlView", className))
  2443. {
  2444. switch(static_cast<CModControlView*>(view)->GetActivePage())
  2445. {
  2446. case CModControlView::VIEW_GLOBALS: page = "::/General.html"; break;
  2447. case CModControlView::VIEW_PATTERNS: page = "::/Patterns.html"; break;
  2448. case CModControlView::VIEW_SAMPLES: page = "::/Samples.html"; break;
  2449. case CModControlView::VIEW_INSTRUMENTS: page = "::/Instruments.html"; break;
  2450. case CModControlView::VIEW_COMMENTS: page = "::/Comments.html"; break;
  2451. }
  2452. }
  2453. }
  2454. const mpt::PathString helpFile = theApp.GetInstallPath() + P_("OpenMPT Manual.chm") + mpt::PathString::FromUTF8(page) + P_(">OpenMPT");
  2455. if(!::HtmlHelp(m_hWnd, helpFile.AsNative().c_str(), strcmp(page, "") ? HH_DISPLAY_TOC : HH_DISPLAY_TOPIC, NULL))
  2456. {
  2457. Reporting::Error(_T("Could not find help file:\n") + helpFile.ToCString());
  2458. return;
  2459. }
  2460. //::ShowWindow(hwndHelp, SW_SHOWMAXIMIZED);
  2461. }
  2462. LRESULT CMainFrame::OnViewMIDIMapping(WPARAM wParam, LPARAM lParam)
  2463. {
  2464. static bool inMapper = false;
  2465. if(!inMapper)
  2466. {
  2467. inMapper = true;
  2468. CModDoc *doc = GetActiveDoc();
  2469. if(doc != nullptr)
  2470. doc->ViewMIDIMapping(static_cast<PLUGINDEX>(wParam), static_cast<PlugParamIndex>(lParam));
  2471. inMapper = false;
  2472. }
  2473. return 0;
  2474. }
  2475. HMENU CMainFrame::CreateFileMenu(const size_t maxCount, std::vector<mpt::PathString>& paths, const mpt::PathString &folderName, const uint16 idRangeBegin)
  2476. {
  2477. paths.clear();
  2478. HMENU hMenu = ::CreatePopupMenu();
  2479. ASSERT(hMenu != NULL);
  2480. if (hMenu != NULL)
  2481. {
  2482. UINT_PTR filesAdded = 0;
  2483. for(size_t i = 0; i < 2; i++) // 0: app items, 1: user items
  2484. {
  2485. // To avoid duplicates, check whether app path and config path are the same.
  2486. if (i == 1 && mpt::PathString::CompareNoCase(theApp.GetInstallPath(), theApp.GetConfigPath()) == 0)
  2487. break;
  2488. mpt::PathString basePath;
  2489. basePath = (i == 0) ? theApp.GetInstallPath() : theApp.GetConfigPath();
  2490. basePath += folderName;
  2491. if(!basePath.IsDirectory())
  2492. continue;
  2493. FolderScanner scanner(basePath, FolderScanner::kOnlyFiles);
  2494. mpt::PathString fileName;
  2495. while(filesAdded < maxCount && scanner.Next(fileName))
  2496. {
  2497. paths.push_back(fileName);
  2498. CString file = fileName.GetFullFileName().ToCString();
  2499. file.Replace(_T("&"), _T("&&"));
  2500. AppendMenu(hMenu, MF_STRING, idRangeBegin + filesAdded, file);
  2501. filesAdded++;
  2502. }
  2503. }
  2504. if(filesAdded == 0)
  2505. {
  2506. AppendMenu(hMenu, MF_STRING | MF_GRAYED | MF_DISABLED, 0, _T("No items found"));
  2507. } else
  2508. {
  2509. AppendMenu(hMenu, MF_SEPARATOR, 0, 0);
  2510. AppendMenu(hMenu, MF_STRING, idRangeBegin + maxCount, _T("Browse..."));
  2511. }
  2512. }
  2513. return hMenu;
  2514. }
  2515. void CMainFrame::CreateExampleModulesMenu()
  2516. {
  2517. static_assert(nMaxItemsInExampleModulesMenu == ID_EXAMPLE_MODULES_LASTINRANGE - ID_EXAMPLE_MODULES,
  2518. "Make sure that there's a proper range for menu commands in resources.");
  2519. HMENU hMenu = CreateFileMenu(nMaxItemsInExampleModulesMenu, m_ExampleModulePaths, P_("ExampleSongs\\"), ID_EXAMPLE_MODULES);
  2520. CMenu* const pMainMenu = GetMenu();
  2521. if (hMenu && pMainMenu && m_InputHandler)
  2522. VERIFY(pMainMenu->ModifyMenu(ID_EXAMPLE_MODULES, MF_BYCOMMAND | MF_POPUP, (UINT_PTR)hMenu, m_InputHandler->GetMenuText(ID_EXAMPLE_MODULES)));
  2523. else
  2524. ASSERT(false);
  2525. }
  2526. // Hack-ish way to get the file menu (this is necessary because the MDI document icon next to the File menu is a sub menu, too).
  2527. CMenu *CMainFrame::GetFileMenu() const
  2528. {
  2529. CMenu *mainMenu = GetMenu();
  2530. CMenu *fileMenu = mainMenu ? mainMenu->GetSubMenu(0) : nullptr;
  2531. if(fileMenu)
  2532. {
  2533. if(fileMenu->GetMenuItemID(1) != ID_FILE_OPEN)
  2534. fileMenu = mainMenu->GetSubMenu(1);
  2535. ASSERT(fileMenu->GetMenuItemID(1) == ID_FILE_OPEN);
  2536. }
  2537. ASSERT(fileMenu);
  2538. return fileMenu;
  2539. }
  2540. void CMainFrame::CreateTemplateModulesMenu()
  2541. {
  2542. static_assert(nMaxItemsInTemplateModulesMenu == ID_FILE_OPENTEMPLATE_LASTINRANGE - ID_FILE_OPENTEMPLATE,
  2543. "Make sure that there's a proper range for menu commands in resources.");
  2544. HMENU hMenu = CreateFileMenu(nMaxItemsInTemplateModulesMenu, m_TemplateModulePaths, P_("TemplateModules\\"), ID_FILE_OPENTEMPLATE);
  2545. CMenu *pFileMenu = GetFileMenu();
  2546. if (hMenu && pFileMenu && m_InputHandler)
  2547. {
  2548. VERIFY(pFileMenu->RemoveMenu(2, MF_BYPOSITION));
  2549. VERIFY(pFileMenu->InsertMenu(2, MF_BYPOSITION | MF_POPUP, (UINT_PTR)hMenu, m_InputHandler->GetMenuText(ID_FILE_OPENTEMPLATE)));
  2550. }
  2551. else
  2552. ASSERT(false);
  2553. }
  2554. void CMainFrame::UpdateMRUList()
  2555. {
  2556. CMenu *pMenu = GetFileMenu();
  2557. if(!pMenu) return;
  2558. static int firstMenu = -1;
  2559. if(firstMenu == -1)
  2560. {
  2561. int numMenus = pMenu->GetMenuItemCount();
  2562. for(int i = 0; i < numMenus; i++)
  2563. {
  2564. if(pMenu->GetMenuItemID(i) == ID_MRU_LIST_FIRST)
  2565. {
  2566. firstMenu = i;
  2567. break;
  2568. }
  2569. }
  2570. }
  2571. for(int i = ID_MRU_LIST_FIRST; i <= ID_MRU_LIST_LAST; i++)
  2572. {
  2573. pMenu->DeleteMenu(i, MF_BYCOMMAND);
  2574. }
  2575. if(TrackerSettings::Instance().mruFiles.empty())
  2576. {
  2577. // MFC will automatically ignore if we set MF_GRAYED here because of CFrameWnd::m_bAutoMenuEnable.
  2578. // So we will have to install a ON_UPDATE_COMMAND_UI callback...
  2579. pMenu->InsertMenu(firstMenu, MF_STRING | MF_BYPOSITION, ID_MRU_LIST_FIRST, _T("Recent File"));
  2580. } else
  2581. {
  2582. const mpt::PathString workDir = TrackerSettings::Instance().PathSongs.GetWorkingDir();
  2583. const int entries = mpt::saturate_cast<int>(TrackerSettings::Instance().mruFiles.size());
  2584. for(int i = 0; i < entries; i++)
  2585. {
  2586. mpt::winstring s = mpt::tfmt::val(i + 1) + _T(" ");
  2587. // Add mnemonics
  2588. if(i < 9)
  2589. {
  2590. s = _T("&") + s;
  2591. } else if(i == 9)
  2592. {
  2593. s = _T("1&0 ");
  2594. }
  2595. const mpt::PathString &pathMPT = TrackerSettings::Instance().mruFiles[i];
  2596. mpt::winstring path = pathMPT.AsNative();
  2597. if(!mpt::PathString::CompareNoCase(workDir, pathMPT.GetPath()))
  2598. {
  2599. // Only show filename
  2600. path = path.substr(workDir.AsNative().length());
  2601. } else if(path.length() > 30) // Magic number experimentally determined to be equal to MFC's behaviour
  2602. {
  2603. // Shorten path ("C:\Foo\VeryLongString...\Bar.it" => "C:\Foo\...\Bar.it")
  2604. size_t start = path.find_first_of(_T("\\/"), path.find_first_of(_T("\\/")) + 1);
  2605. size_t end = path.find_last_of(_T("\\/"));
  2606. if(start < end)
  2607. {
  2608. path = path.substr(0, start + 1) + _T("...") + path.substr(end);
  2609. }
  2610. }
  2611. path = mpt::String::Replace(path, mpt::winstring(_T("&")), mpt::winstring(_T("&&")));
  2612. s += path;
  2613. pMenu->InsertMenu(firstMenu + i, MF_STRING | MF_BYPOSITION, ID_MRU_LIST_FIRST + i, mpt::ToCString(s));
  2614. }
  2615. }
  2616. }
  2617. BOOL CMainFrame::OnQueryEndSession()
  2618. {
  2619. int modifiedCount = 0;
  2620. for(const auto &modDoc : theApp.GetOpenDocuments())
  2621. {
  2622. if(modDoc->IsModified())
  2623. modifiedCount++;
  2624. }
  2625. #if (_WIN32_WINNT >= _WIN32_WINNT_VISTA)
  2626. ShutdownBlockReasonDestroy(m_hWnd);
  2627. if(modifiedCount > 0)
  2628. {
  2629. ShutdownBlockReasonCreate(m_hWnd,
  2630. MPT_WFORMAT("There {} {} unsaved file{}.")(modifiedCount == 1 ? L"is" : L"are", modifiedCount, modifiedCount == 1 ? L"" : L"s").c_str());
  2631. }
  2632. #endif
  2633. return modifiedCount ? FALSE : TRUE;
  2634. }
  2635. void CMainFrame::NotifyAccessibilityUpdate(CWnd &source)
  2636. {
  2637. if(!IsPlaying() || m_pSndFile->m_SongFlags[SONG_PAUSED])
  2638. source.NotifyWinEvent(EVENT_OBJECT_NAMECHANGE, OBJID_CLIENT, CHILDID_SELF);
  2639. }
  2640. // ITfLanguageProfileNotifySink implementation
  2641. TfLanguageProfileNotifySink::TfLanguageProfileNotifySink()
  2642. {
  2643. HRESULT hr = CoCreateInstance(CLSID_TF_InputProcessorProfiles, NULL, CLSCTX_INPROC_SERVER, IID_ITfInputProcessorProfiles, (void**)&m_pProfiles);
  2644. if(SUCCEEDED(hr))
  2645. {
  2646. hr = m_pProfiles->QueryInterface(IID_ITfSource, (void**)&m_pSource);
  2647. if(SUCCEEDED(hr))
  2648. {
  2649. hr = m_pSource->AdviseSink(IID_ITfLanguageProfileNotifySink,
  2650. static_cast<ITfLanguageProfileNotifySink *>(this),
  2651. &m_dwCookie);
  2652. MPT_ASSERT(SUCCEEDED(hr));
  2653. MPT_ASSERT(m_dwCookie != TF_INVALID_COOKIE);
  2654. }
  2655. }
  2656. }
  2657. TfLanguageProfileNotifySink::~TfLanguageProfileNotifySink()
  2658. {
  2659. if(mpt::OS::Windows::IsWine())
  2660. {
  2661. // Calling UnadviseSink causes a random crash in Wine when computing its function pointer for some reason.
  2662. // Probably a race condition I don't understand, and probably a bug in Wine.
  2663. return;
  2664. }
  2665. if(m_pSource && (m_dwCookie != TF_INVALID_COOKIE))
  2666. {
  2667. m_pSource->UnadviseSink(m_dwCookie);
  2668. }
  2669. m_dwCookie = TF_INVALID_COOKIE;
  2670. if(m_pSource)
  2671. {
  2672. m_pSource->Release();
  2673. }
  2674. m_pSource = nullptr;
  2675. if(m_pProfiles) m_pProfiles->Release();
  2676. m_pProfiles = nullptr;
  2677. }
  2678. HRESULT TfLanguageProfileNotifySink::OnLanguageChange(LANGID /*langid*/, __RPC__out BOOL *pfAccept)
  2679. {
  2680. *pfAccept = TRUE;
  2681. return ResultFromScode(S_OK);
  2682. }
  2683. HRESULT TfLanguageProfileNotifySink::OnLanguageChanged()
  2684. {
  2685. // Input language has changed, so key positions might have changed too.
  2686. CMainFrame *mainFrm = CMainFrame::GetMainFrame();
  2687. if(mainFrm != nullptr)
  2688. {
  2689. mainFrm->UpdateEffectKeys(mainFrm->GetActiveDoc());
  2690. mainFrm->m_InputHandler->SetModifierMask(ModNone);
  2691. }
  2692. return ResultFromScode(S_OK);
  2693. }
  2694. HRESULT TfLanguageProfileNotifySink::QueryInterface(REFIID riid, void **ppvObject)
  2695. {
  2696. if(riid == IID_ITfLanguageProfileNotifySink || riid == IID_IUnknown)
  2697. {
  2698. *ppvObject = static_cast<ITfLanguageProfileNotifySink *>(this);
  2699. AddRef();
  2700. return ResultFromScode(S_OK);
  2701. }
  2702. *ppvObject = nullptr;
  2703. return ResultFromScode(E_NOINTERFACE);
  2704. }
  2705. ULONG TfLanguageProfileNotifySink::AddRef()
  2706. {
  2707. // Don't let COM do anything to this object
  2708. return 1;
  2709. }
  2710. ULONG TfLanguageProfileNotifySink::Release()
  2711. {
  2712. // Don't let COM do anything to this object
  2713. return 1;
  2714. }
  2715. /////////////////////////////////////////////
  2716. //Misc helper functions
  2717. /////////////////////////////////////////////
  2718. void AddPluginNamesToCombobox(CComboBox &CBox, const SNDMIXPLUGIN *plugarray, const bool libraryName, const PLUGINDEX updatePlug)
  2719. {
  2720. #ifndef NO_PLUGINS
  2721. int insertAt = CBox.GetCount();
  2722. if(updatePlug != PLUGINDEX_INVALID)
  2723. {
  2724. const int items = insertAt;
  2725. for(insertAt = 0; insertAt < items; insertAt++)
  2726. {
  2727. auto thisPlugin = static_cast<PLUGINDEX>(CBox.GetItemData(insertAt));
  2728. if(thisPlugin == (updatePlug + 1))
  2729. CBox.DeleteString(insertAt);
  2730. if(thisPlugin >= (updatePlug + 1))
  2731. break;
  2732. }
  2733. }
  2734. mpt::tstring str;
  2735. str.reserve(80);
  2736. for(PLUGINDEX plug = 0; plug < MAX_MIXPLUGINS; plug++)
  2737. {
  2738. if(updatePlug != PLUGINDEX_INVALID && plug != updatePlug)
  2739. continue;
  2740. const SNDMIXPLUGIN &plugin = plugarray[plug];
  2741. str.clear();
  2742. str += MPT_TFORMAT("FX{}: ")(plug + 1);
  2743. const auto plugName = plugin.GetName(), libName = plugin.GetLibraryName();
  2744. str += mpt::ToWin(plugName);
  2745. if(libraryName && plugName != libName && !libName.empty())
  2746. str += _T(" (") + mpt::ToWin(libName) + _T(")");
  2747. else if(plugName.empty() && (!libraryName || libName.empty()))
  2748. str += _T("--");
  2749. #ifdef MPT_WITH_VST
  2750. auto *vstPlug = dynamic_cast<const CVstPlugin *>(plugin.pMixPlugin);
  2751. if(vstPlug != nullptr && vstPlug->isBridged)
  2752. {
  2753. VSTPluginLib &lib = vstPlug->GetPluginFactory();
  2754. str += MPT_TFORMAT(" ({} Bridged)")(lib.GetDllArchNameUser());
  2755. }
  2756. #endif // MPT_WITH_VST
  2757. insertAt = CBox.InsertString(insertAt, str.c_str());
  2758. CBox.SetItemData(insertAt, plug + 1);
  2759. insertAt++;
  2760. }
  2761. #endif // NO_PLUGINS
  2762. }
  2763. void AddPluginParameternamesToCombobox(CComboBox& CBox, SNDMIXPLUGIN& plug)
  2764. {
  2765. #ifndef NO_PLUGINS
  2766. if(plug.pMixPlugin)
  2767. AddPluginParameternamesToCombobox(CBox, *plug.pMixPlugin);
  2768. #endif // NO_PLUGINS
  2769. }
  2770. void AddPluginParameternamesToCombobox(CComboBox& CBox, IMixPlugin& plug)
  2771. {
  2772. #ifndef NO_PLUGINS
  2773. const PlugParamIndex numParams = plug.GetNumParameters();
  2774. plug.CacheParameterNames(0, numParams);
  2775. for(PlugParamIndex i = 0; i < numParams; i++)
  2776. {
  2777. CBox.SetItemData(CBox.AddString(plug.GetFormattedParamName(i)), i);
  2778. }
  2779. #endif // NO_PLUGINS
  2780. }
  2781. OPENMPT_NAMESPACE_END