1
0

View_tre.cpp 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505
  1. /*
  2. * View_tre.cpp
  3. * ------------
  4. * Purpose: Tree view for managing open songs, sound files, file browser, ...
  5. * Notes : (currently none)
  6. * Authors: Olivier Lapicque
  7. * OpenMPT Devs
  8. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  9. */
  10. #include "stdafx.h"
  11. #include "Mainfrm.h"
  12. #include "InputHandler.h"
  13. #include "ImageLists.h"
  14. #include "View_tre.h"
  15. #include "Mptrack.h"
  16. #include "Moddoc.h"
  17. #include "Dlsbank.h"
  18. #include "dlg_misc.h"
  19. #include "../common/mptFileIO.h"
  20. #include "../common/FileReader.h"
  21. #include "FileDialog.h"
  22. #include "Globals.h"
  23. #include "ExternalSamples.h"
  24. #include "FolderScanner.h"
  25. #include "LinkResolver.h"
  26. #include "../soundlib/mod_specifications.h"
  27. #include "../soundlib/plugins/PlugInterface.h"
  28. #include "../soundlib/MIDIEvents.h"
  29. #include <winioctl.h>
  30. OPENMPT_NAMESPACE_BEGIN
  31. CSoundFile *CModTree::m_SongFile = nullptr;
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CModTreeDropTarget
  34. BOOL CModTreeDropTarget::Register(CModTree *pWnd)
  35. {
  36. m_pModTree = pWnd;
  37. return COleDropTarget::Register(pWnd);
  38. }
  39. DROPEFFECT CModTreeDropTarget::OnDragEnter(CWnd *pWnd, COleDataObject *pDataObject, DWORD dwKeyState, CPoint point)
  40. {
  41. if((m_pModTree) && (m_pModTree == pWnd))
  42. return m_pModTree->OnDragEnter(pDataObject, dwKeyState, point);
  43. return DROPEFFECT_NONE;
  44. }
  45. DROPEFFECT CModTreeDropTarget::OnDragOver(CWnd *pWnd, COleDataObject *pDataObject, DWORD dwKeyState, CPoint point)
  46. {
  47. if((m_pModTree) && (m_pModTree == pWnd))
  48. return m_pModTree->OnDragOver(pDataObject, dwKeyState, point);
  49. return DROPEFFECT_NONE;
  50. }
  51. BOOL CModTreeDropTarget::OnDrop(CWnd *pWnd, COleDataObject *pDataObject, DROPEFFECT dropEffect, CPoint point)
  52. {
  53. if((m_pModTree) && (m_pModTree == pWnd))
  54. return m_pModTree->OnDrop(pDataObject, dropEffect, point);
  55. return FALSE;
  56. }
  57. ModTreeDocInfo::ModTreeDocInfo(CModDoc &modDoc)
  58. : modDoc(modDoc)
  59. {
  60. const CSoundFile &sndFile = modDoc.GetSoundFile();
  61. tiPatterns.resize(sndFile.Patterns.Size(), nullptr);
  62. tiOrders.resize(sndFile.Order.GetNumSequences());
  63. tiSequences.resize(sndFile.Order.GetNumSequences(), nullptr);
  64. }
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CModTree
  67. BEGIN_MESSAGE_MAP(CModTree, CTreeCtrl)
  68. //{{AFX_MSG_MAP(CViewModTree)
  69. ON_WM_MOUSEMOVE()
  70. ON_WM_LBUTTONUP()
  71. ON_WM_RBUTTONUP()
  72. ON_WM_XBUTTONUP()
  73. ON_WM_KEYDOWN()
  74. ON_WM_DROPFILES()
  75. ON_NOTIFY_REFLECT(NM_DBLCLK, &CModTree::OnItemDblClk)
  76. ON_NOTIFY_REFLECT(NM_RETURN, &CModTree::OnItemReturn)
  77. ON_NOTIFY_REFLECT(NM_RCLICK, &CModTree::OnItemRightClick)
  78. ON_NOTIFY_REFLECT(NM_CLICK, &CModTree::OnItemLeftClick)
  79. ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, &CModTree::OnItemExpanded)
  80. ON_NOTIFY_REFLECT(TVN_BEGINDRAG, &CModTree::OnBeginLDrag)
  81. ON_NOTIFY_REFLECT(TVN_BEGINRDRAG, &CModTree::OnBeginRDrag)
  82. ON_NOTIFY_REFLECT(TVN_BEGINLABELEDIT,&CModTree::OnBeginLabelEdit)
  83. ON_NOTIFY_REFLECT(TVN_ENDLABELEDIT, &CModTree::OnEndLabelEdit)
  84. ON_COMMAND(ID_MODTREE_REFRESH, &CModTree::OnRefreshTree)
  85. ON_COMMAND(ID_MODTREE_EXECUTE, &CModTree::OnExecuteItem)
  86. ON_COMMAND(ID_MODTREE_REMOVE, &CModTree::OnDeleteTreeItem)
  87. ON_COMMAND(ID_MODTREE_PLAY, &CModTree::OnPlayTreeItem)
  88. ON_COMMAND(ID_MODTREE_REFRESHINSTRLIB, &CModTree::OnRefreshInstrLib)
  89. ON_COMMAND(ID_MODTREE_OPENITEM, &CModTree::OnOpenTreeItem)
  90. ON_COMMAND(ID_MODTREE_MUTE, &CModTree::OnMuteTreeItem)
  91. ON_COMMAND(ID_MODTREE_MUTE_ONLY_EFFECTS, &CModTree::OnMuteOnlyEffects)
  92. ON_COMMAND(ID_MODTREE_SOLO, &CModTree::OnSoloTreeItem)
  93. ON_COMMAND(ID_MODTREE_UNMUTEALL, &CModTree::OnUnmuteAllTreeItem)
  94. ON_COMMAND(ID_MODTREE_DUPLICATE, &CModTree::OnDuplicateTreeItem)
  95. ON_COMMAND(ID_MODTREE_INSERT, &CModTree::OnInsertTreeItem)
  96. ON_COMMAND(ID_MODTREE_SWITCHTO, &CModTree::OnSwitchToTreeItem)
  97. ON_COMMAND(ID_MODTREE_CLOSE, &CModTree::OnCloseItem)
  98. ON_COMMAND(ID_MODTREE_SETPATH, &CModTree::OnSetItemPath)
  99. ON_COMMAND(ID_MODTREE_SAVEITEM, &CModTree::OnSaveItem)
  100. ON_COMMAND(ID_MODTREE_SAVEALL, &CModTree::OnSaveAll)
  101. ON_COMMAND(ID_MODTREE_RELOADITEM, &CModTree::OnReloadItem)
  102. ON_COMMAND(ID_MODTREE_RELOADALL, &CModTree::OnReloadAll)
  103. ON_COMMAND(ID_MODTREE_FINDMISSING, &CModTree::OnFindMissing)
  104. ON_COMMAND(ID_MODTREE_RENAME, &CModTree::OnRenameItem)
  105. ON_COMMAND(ID_ADD_SOUNDBANK, &CModTree::OnAddDlsBank)
  106. ON_COMMAND(ID_IMPORT_MIDILIB, &CModTree::OnImportMidiLib)
  107. ON_COMMAND(ID_EXPORT_MIDILIB, &CModTree::OnExportMidiLib)
  108. ON_COMMAND(ID_SOUNDBANK_PROPERTIES, &CModTree::OnSoundBankProperties)
  109. ON_COMMAND(ID_MODTREE_SHOWDIRS, &CModTree::OnShowDirectories)
  110. ON_COMMAND(ID_MODTREE_SHOWALLFILES, &CModTree::OnShowAllFiles)
  111. ON_COMMAND(ID_MODTREE_SOUNDFILESONLY,&CModTree::OnShowSoundFiles)
  112. ON_COMMAND(ID_MODTREE_GOTO_INSDIR, &CModTree::OnGotoInstrumentDir)
  113. ON_COMMAND(ID_MODTREE_GOTO_SMPDIR, &CModTree::OnGotoSampleDir)
  114. ON_MESSAGE(WM_MOD_KEYCOMMAND, &CModTree::OnCustomKeyMsg) //rewbs.customKeys
  115. ON_MESSAGE(WM_MOD_MIDIMSG, &CModTree::OnMidiMsg)
  116. //}}AFX_MSG_MAP
  117. ON_WM_KILLFOCUS()
  118. ON_WM_SETFOCUS()
  119. END_MESSAGE_MAP()
  120. /////////////////////////////////////////////////////////////////////////////
  121. // CViewModTree construction/destruction
  122. CModTree::CModTree(CModTree *pDataTree)
  123. : m_pDataTree(pDataTree)
  124. {
  125. if(m_pDataTree != nullptr)
  126. {
  127. // Set up instrument library monitoring thread
  128. m_hWatchDirKillThread = CreateEvent(nullptr, FALSE, FALSE, nullptr);
  129. m_hSwitchWatchDir = CreateEvent(nullptr, FALSE, FALSE, nullptr);
  130. m_WatchDirThread = std::thread([this]() { MonitorInstrumentLibrary(); });
  131. }
  132. MemsetZero(m_tiMidi);
  133. MemsetZero(m_tiPerc);
  134. #if (_WIN32_WINNT >= _WIN32_WINNT_WIN7)
  135. // Wine does not support natural sorting with SORT_DIGITSASNUMBERS, fall back to normal sorting
  136. if(!::CompareString(LOCALE_USER_DEFAULT, m_stringCompareFlags, _T(""), -1, _T(""), -1))
  137. m_stringCompareFlags &= ~SORT_DIGITSASNUMBERS;
  138. #endif
  139. }
  140. CModTree::~CModTree()
  141. {
  142. delete m_SongFile;
  143. m_SongFile = nullptr;
  144. if(m_pDataTree != nullptr)
  145. {
  146. SetEvent(m_hWatchDirKillThread);
  147. m_WatchDirThread.join();
  148. CloseHandle(m_hSwitchWatchDir);
  149. CloseHandle(m_hWatchDirKillThread);
  150. }
  151. }
  152. void CModTree::Init()
  153. {
  154. m_modExtensions = CSoundFile::GetSupportedExtensions(false);
  155. m_MediaFoundationExtensions = FileType(CSoundFile::GetMediaFoundationFileTypes()).GetExtensions();
  156. DWORD dwRemove = TVS_SINGLEEXPAND;
  157. DWORD dwAdd = TVS_EDITLABELS | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_SHOWSELALWAYS;
  158. if(IsSampleBrowser())
  159. {
  160. dwRemove |= (TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_SHOWSELALWAYS);
  161. dwAdd &= ~(TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_SHOWSELALWAYS);
  162. }
  163. if(TrackerSettings::Instance().m_dwPatternSetup & PATTERN_SINGLEEXPAND)
  164. {
  165. dwRemove &= ~TVS_SINGLEEXPAND;
  166. dwAdd |= TVS_SINGLEEXPAND;
  167. m_dwStatus |= TREESTATUS_SINGLEEXPAND;
  168. }
  169. ModifyStyle(dwRemove, dwAdd);
  170. ModifyStyleEx(0, WS_EX_ACCEPTFILES);
  171. if(!IsSampleBrowser())
  172. {
  173. std::vector<TCHAR> curDir(::GetCurrentDirectory(0, nullptr), '\0');
  174. ::GetCurrentDirectory(static_cast<DWORD>(curDir.size()), curDir.data());
  175. mpt::PathString dirs[] =
  176. {
  177. TrackerSettings::Instance().PathSamples.GetDefaultDir(),
  178. TrackerSettings::Instance().PathInstruments.GetDefaultDir(),
  179. TrackerSettings::Instance().PathSongs.GetDefaultDir(),
  180. mpt::PathString::FromNative(curDir.data())
  181. };
  182. for(auto &path : dirs)
  183. {
  184. m_InstrLibPath = std::move(path);
  185. if(!m_InstrLibPath.empty())
  186. break;
  187. }
  188. m_InstrLibPath.EnsureTrailingSlash();
  189. m_pDataTree->InsLibSetFullPath(m_InstrLibPath, mpt::PathString());
  190. }
  191. SetImageList(&CMainFrame::GetMainFrame()->m_MiscIcons, TVSIL_NORMAL);
  192. if(!IsSampleBrowser())
  193. {
  194. // Create Midi Library
  195. m_hMidiLib = InsertItem(_T("MIDI Library"), IMAGE_FOLDER, IMAGE_FOLDER, TVI_ROOT, TVI_LAST);
  196. for(UINT iMidGrp = 0; iMidGrp < 17; iMidGrp++)
  197. {
  198. InsertItem(TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM, mpt::ToCString(mpt::Charset::ASCII, szMidiGroupNames[iMidGrp]), IMAGE_FOLDER, IMAGE_FOLDER, 0, 0, (MODITEM_HDR_MIDIGROUP << MIDILIB_SHIFT) | iMidGrp, m_hMidiLib, TVI_LAST);
  199. }
  200. }
  201. m_hInsLib = InsertItem(_T("Instrument Library"), IMAGE_FOLDER, IMAGE_FOLDER, TVI_ROOT, TVI_LAST);
  202. SetItemData(m_hInsLib, reinterpret_cast<DWORD_PTR>(m_hInsLib));
  203. RefreshMidiLibrary();
  204. RefreshDlsBanks();
  205. RefreshInstrumentLibrary();
  206. m_DropTarget.Register(this);
  207. }
  208. BOOL CModTree::PreTranslateMessage(MSG *pMsg)
  209. {
  210. if(!pMsg)
  211. return TRUE;
  212. if(m_doLabelEdit)
  213. {
  214. if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
  215. {
  216. // End editing by making edit box lose focus.
  217. SetFocus();
  218. return TRUE;
  219. }
  220. return CTreeCtrl::PreTranslateMessage(pMsg);
  221. }
  222. if(pMsg->message == WM_KEYDOWN)
  223. {
  224. switch(pMsg->wParam)
  225. {
  226. case VK_SPACE:
  227. if(!(pMsg->lParam & 0x40000000))
  228. OnPlayTreeItem();
  229. return TRUE;
  230. case VK_RETURN:
  231. if(!(pMsg->lParam & 0x40000000))
  232. {
  233. HTREEITEM hItem = GetSelectedItem();
  234. if(hItem)
  235. {
  236. if(CMainFrame::GetInputHandler()->CtrlPressed())
  237. {
  238. const ModItem modItem = GetModItem(hItem);
  239. static constexpr ModItemType instrumentTypes[] = {MODITEM_INSLIB_SAMPLE, MODITEM_INSLIB_INSTRUMENT, MODITEM_MIDIINSTRUMENT, MODITEM_MIDIPERCUSSION, MODITEM_DLSBANK_INSTRUMENT};
  240. if(mpt::contains(instrumentTypes, modItem.type))
  241. {
  242. // Ctrl+Enter: Load sample into currently selected sample or instrument slot
  243. // Additionally pressing Shift creates a new sample or instrument slot. Shift key is handled in the Drag&drop handler
  244. CModScrollView *view = static_cast<CModScrollView *>(CMainFrame::GetMainFrame()->GetActiveView());
  245. if(view)
  246. {
  247. const char *className = view->GetRuntimeClass()->m_lpszClassName;
  248. const bool isSampleView = !strcmp("CViewSample", className);
  249. const bool isInstrumentView = !strcmp("CViewInstrument", className);
  250. mpt::PathString fullPath = InsLibGetFullPath(hItem);
  251. DRAGONDROP dropInfo;
  252. m_hItemDrag = hItem;
  253. m_itemDrag = modItem;
  254. if((isSampleView || isInstrumentView) && GetDropInfo(dropInfo, fullPath))
  255. {
  256. view->SendMessage(WM_MOD_DRAGONDROPPING, TRUE, reinterpret_cast<LPARAM>(&dropInfo));
  257. // In case a message box like "create instrument for sample?" showed up
  258. SetFocus();
  259. }
  260. }
  261. } else if(!IsSampleBrowser())
  262. {
  263. // Ctrl+Enter: Edit item
  264. EditLabel(hItem);
  265. }
  266. } else
  267. {
  268. if(!ExecuteItem(hItem))
  269. {
  270. if(ItemHasChildren(hItem))
  271. {
  272. Expand(hItem, TVE_TOGGLE);
  273. }
  274. }
  275. }
  276. }
  277. }
  278. return TRUE;
  279. case VK_TAB:
  280. // Tab: Switch between folder and file view.
  281. if(this == CMainFrame::GetMainFrame()->GetUpperTreeview())
  282. CMainFrame::GetMainFrame()->GetLowerTreeview()->SetFocus();
  283. else
  284. CMainFrame::GetMainFrame()->GetUpperTreeview()->SetFocus();
  285. return TRUE;
  286. case VK_BACK:
  287. // Backspace: Go up one directory
  288. if(GetParentRootItem(GetSelectedItem()) == m_hInsLib || IsSampleBrowser())
  289. {
  290. InstrumentLibraryChDir(P_(".."), !m_SongFileName.empty());
  291. return TRUE;
  292. }
  293. break;
  294. case VK_INSERT:
  295. InsertOrDupItem(!CMainFrame::GetInputHandler()->ShiftPressed());
  296. return TRUE;
  297. case VK_APPS:
  298. // Handle Application (menu) key
  299. if(HTREEITEM item = GetSelectedItem())
  300. {
  301. CRect rect;
  302. GetItemRect(item, &rect, FALSE);
  303. ClientToScreen(rect);
  304. OnItemRightClick(item, rect.TopLeft());
  305. }
  306. return TRUE;
  307. }
  308. } else if(pMsg->message == WM_CHAR)
  309. {
  310. ModItem item = GetModItem(GetSelectedItem());
  311. switch(item.type)
  312. {
  313. case MODITEM_SAMPLE:
  314. case MODITEM_INSTRUMENT:
  315. case MODITEM_MIDIINSTRUMENT:
  316. case MODITEM_MIDIPERCUSSION:
  317. case MODITEM_INSLIB_SAMPLE:
  318. case MODITEM_INSLIB_INSTRUMENT:
  319. case MODITEM_DLSBANK_INSTRUMENT:
  320. // Avoid cycling through tree-view elements on key hold
  321. return true;
  322. }
  323. }
  324. //We handle keypresses before Windows has a chance to handle them (for alt etc..)
  325. if ((pMsg->message == WM_SYSKEYUP) || (pMsg->message == WM_KEYUP) ||
  326. (pMsg->message == WM_SYSKEYDOWN) || (pMsg->message == WM_KEYDOWN))
  327. {
  328. CInputHandler *ih = CMainFrame::GetInputHandler();
  329. //Translate message manually
  330. UINT nChar = (UINT)pMsg->wParam;
  331. UINT nRepCnt = LOWORD(pMsg->lParam);
  332. UINT nFlags = HIWORD(pMsg->lParam);
  333. KeyEventType kT = ih->GetKeyEventType(nFlags);
  334. InputTargetContext ctx = (InputTargetContext)(kCtxViewTree);
  335. if(ih->KeyEvent(ctx, nChar, nRepCnt, nFlags, kT) != kcNull)
  336. return true; // Mapped to a command, no need to pass message on.
  337. }
  338. return CTreeCtrl::PreTranslateMessage(pMsg);
  339. }
  340. mpt::PathString CModTree::InsLibGetFullPath(HTREEITEM hItem) const
  341. {
  342. mpt::PathString fullPath = m_InstrLibPath;
  343. fullPath.EnsureTrailingSlash();
  344. return fullPath + mpt::PathString::FromCString(GetItemText(hItem));
  345. }
  346. bool CModTree::InsLibSetFullPath(const mpt::PathString &libPath, const mpt::PathString &songName)
  347. {
  348. if(!songName.empty() && mpt::PathString::CompareNoCase(m_SongFileName, songName))
  349. {
  350. // Load module for previewing its instruments
  351. InputFile f(libPath + songName, TrackerSettings::Instance().MiscCacheCompleteFileBeforeLoading);
  352. if(f.IsValid())
  353. {
  354. FileReader file = GetFileReader(f);
  355. if(file.IsValid())
  356. {
  357. if(m_SongFile != nullptr)
  358. {
  359. m_SongFile->Destroy();
  360. } else
  361. {
  362. m_SongFile = new(std::nothrow) CSoundFile;
  363. }
  364. if(m_SongFile != nullptr)
  365. {
  366. try
  367. {
  368. if(!m_SongFile->Create(file, CSoundFile::loadNoPatternOrPluginData, nullptr))
  369. {
  370. return false;
  371. }
  372. } catch(mpt::out_of_memory e)
  373. {
  374. mpt::delete_out_of_memory(e);
  375. return false;
  376. } catch(const std::exception &)
  377. {
  378. return false;
  379. }
  380. // Destroy some stuff that we're not going to use anyway.
  381. m_SongFile->Patterns.DestroyPatterns();
  382. m_SongFile->m_songMessage.clear();
  383. }
  384. }
  385. } else
  386. {
  387. return false;
  388. }
  389. }
  390. m_InstrLibPath = libPath;
  391. m_SongFileName = songName;
  392. return true;
  393. }
  394. bool CModTree::SetSoundFile(FileReader &file)
  395. {
  396. std::unique_ptr<CSoundFile> sndFile;
  397. try
  398. {
  399. sndFile = std::make_unique<CSoundFile>();
  400. if(!sndFile->Create(file, CSoundFile::loadNoPatternOrPluginData))
  401. {
  402. return false;
  403. }
  404. } catch(mpt::out_of_memory e)
  405. {
  406. mpt::delete_out_of_memory(e);
  407. return false;
  408. } catch(const std::exception &)
  409. {
  410. return false;
  411. }
  412. if(m_SongFile != nullptr)
  413. {
  414. m_SongFile->Destroy();
  415. delete m_SongFile;
  416. }
  417. m_SongFile = sndFile.release();
  418. m_SongFile->Patterns.DestroyPatterns();
  419. m_SongFile->m_songMessage.clear();
  420. const mpt::PathString fileName = file.GetOptionalFileName().value_or(P_(""));
  421. m_InstrLibPath = fileName.GetPath();
  422. m_SongFileName = fileName.GetFullFileName();
  423. RefreshInstrumentLibrary();
  424. return true;
  425. }
  426. void CModTree::OnOptionsChanged()
  427. {
  428. DWORD dwRemove = TVS_SINGLEEXPAND, dwAdd = 0;
  429. m_dwStatus &= ~TREESTATUS_SINGLEEXPAND;
  430. if(TrackerSettings::Instance().m_dwPatternSetup & PATTERN_SINGLEEXPAND)
  431. {
  432. dwRemove = 0;
  433. dwAdd = TVS_SINGLEEXPAND;
  434. m_dwStatus |= TREESTATUS_SINGLEEXPAND;
  435. }
  436. ModifyStyle(dwRemove, dwAdd);
  437. }
  438. void CModTree::AddDocument(CModDoc &modDoc)
  439. {
  440. const auto [it, inserted] = m_docInfo.try_emplace(&modDoc, modDoc);
  441. if(!inserted)
  442. return;
  443. auto &info = it->second;
  444. UpdateView(info, UpdateHint().ModType());
  445. if(info.hSong)
  446. {
  447. Expand(info.hSong, TVE_EXPAND);
  448. EnsureVisible(info.hSong);
  449. SelectItem(info.hSong);
  450. }
  451. }
  452. void CModTree::RemoveDocument(const CModDoc &modDoc)
  453. {
  454. auto doc = m_docInfo.find(&modDoc);
  455. if(doc == m_docInfo.end())
  456. return;
  457. DeleteItem(doc->second.hSong);
  458. m_docInfo.erase(doc);
  459. }
  460. // Get CModDoc that is associated with a tree item
  461. ModTreeDocInfo *CModTree::GetDocumentInfoFromItem(HTREEITEM hItem)
  462. {
  463. hItem = GetParentRootItem(hItem);
  464. if(hItem != nullptr)
  465. {
  466. // Root item has moddoc pointer
  467. const auto doc = m_docInfo.find(reinterpret_cast<const CModDoc *>(GetItemData(hItem)));
  468. if(doc != m_docInfo.end() && hItem == doc->second.hSong)
  469. {
  470. return &doc->second;
  471. }
  472. }
  473. return nullptr;
  474. }
  475. // Get modtree doc information for a given CModDoc
  476. ModTreeDocInfo *CModTree::GetDocumentInfoFromModDoc(CModDoc &modDoc)
  477. {
  478. auto doc = m_docInfo.find(&modDoc);
  479. if(doc != m_docInfo.end())
  480. return &doc->second;
  481. else
  482. return nullptr;
  483. }
  484. size_t CModTree::GetDLSBankIndexFromItem(HTREEITEM hItem) const
  485. {
  486. return static_cast<size_t>(std::distance(m_tiDLS.begin(), std::find(m_tiDLS.begin(), m_tiDLS.end(), GetParentRootItem(hItem))));
  487. }
  488. CDLSBank *CModTree::GetDLSBankFromItem(HTREEITEM hItem) const
  489. {
  490. const auto bank = GetDLSBankIndexFromItem(hItem);
  491. if(bank < CTrackApp::gpDLSBanks.size())
  492. return CTrackApp::gpDLSBanks[bank].get();
  493. else
  494. return nullptr;
  495. }
  496. /////////////////////////////////////////////////////////////////////////////
  497. // CViewModTree drawing
  498. void CModTree::RefreshMidiLibrary()
  499. {
  500. CString s;
  501. CString stmp;
  502. TVITEM tvi;
  503. const MidiLibrary &midiLib = CTrackApp::GetMidiLibrary();
  504. if(IsSampleBrowser())
  505. return;
  506. // Midi Programs
  507. HTREEITEM parent = GetChildItem(m_hMidiLib);
  508. for(UINT iMidi = 0; iMidi < 128; iMidi++)
  509. {
  510. DWORD dwImage = IMAGE_INSTRMUTE;
  511. s = mpt::cfmt::val(iMidi) + _T(": ") + mpt::ToCString(mpt::Charset::ASCII, szMidiProgramNames[iMidi]);
  512. const LPARAM param = (MODITEM_MIDIINSTRUMENT << MIDILIB_SHIFT) | iMidi;
  513. if(!midiLib[iMidi].empty())
  514. {
  515. s += _T(": ") + midiLib[iMidi].GetFullFileName().ToCString();
  516. dwImage = IMAGE_INSTRUMENTS;
  517. }
  518. if(!m_tiMidi[iMidi])
  519. {
  520. m_tiMidi[iMidi] = InsertItem(TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM,
  521. s, dwImage, dwImage, 0, 0, param, parent, TVI_LAST);
  522. } else
  523. {
  524. tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
  525. tvi.hItem = m_tiMidi[iMidi];
  526. tvi.pszText = stmp.GetBuffer(s.GetLength() + 1);
  527. tvi.cchTextMax = stmp.GetAllocLength();
  528. tvi.iImage = tvi.iSelectedImage = dwImage;
  529. GetItem(&tvi);
  530. s.ReleaseBuffer();
  531. if(s != stmp || tvi.iImage != (int)dwImage)
  532. {
  533. SetItem(m_tiMidi[iMidi], TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM,
  534. s, dwImage, dwImage, 0, 0, param);
  535. }
  536. }
  537. if((iMidi % 8u) == 7u)
  538. {
  539. parent = GetNextSiblingItem(parent);
  540. }
  541. }
  542. // Midi Percussions
  543. for(UINT iPerc = 24; iPerc <= 84; iPerc++)
  544. {
  545. DWORD dwImage = IMAGE_NOSAMPLE;
  546. s = mpt::ToCString(CSoundFile::GetNoteName((ModCommand::NOTE)(iPerc + NOTE_MIN), CSoundFile::GetDefaultNoteNames()))
  547. + _T(": ") + mpt::ToCString(mpt::Charset::ASCII, szMidiPercussionNames[iPerc - 24]);
  548. const LPARAM param = (MODITEM_MIDIPERCUSSION << MIDILIB_SHIFT) | iPerc;
  549. if(!midiLib[iPerc | 0x80].empty())
  550. {
  551. s += _T(": ") + midiLib[iPerc | 0x80].GetFullFileName().ToCString();
  552. dwImage = IMAGE_SAMPLES;
  553. }
  554. if(!m_tiPerc[iPerc])
  555. {
  556. m_tiPerc[iPerc] = InsertItem(TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM,
  557. s, dwImage, dwImage, 0, 0, param, parent, TVI_LAST);
  558. } else
  559. {
  560. tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
  561. tvi.hItem = m_tiPerc[iPerc];
  562. tvi.pszText = stmp.GetBuffer(s.GetLength() + 1);
  563. tvi.cchTextMax = stmp.GetAllocLength();
  564. tvi.iImage = tvi.iSelectedImage = dwImage;
  565. GetItem(&tvi);
  566. s.ReleaseBuffer();
  567. if(s != stmp || tvi.iImage != (int)dwImage)
  568. {
  569. SetItem(m_tiPerc[iPerc], TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
  570. s, dwImage, dwImage, 0, 0, param);
  571. }
  572. }
  573. }
  574. }
  575. void CModTree::RefreshDlsBanks()
  576. {
  577. const mpt::Charset charset = mpt::Charset::Locale;
  578. TCHAR s[256];
  579. HTREEITEM hDlsRoot = m_hMidiLib;
  580. if(IsSampleBrowser())
  581. return;
  582. if(m_tiDLS.size() < CTrackApp::gpDLSBanks.size())
  583. {
  584. m_tiDLS.resize(CTrackApp::gpDLSBanks.size(), nullptr);
  585. }
  586. for(size_t iDls = 0; iDls < CTrackApp::gpDLSBanks.size(); iDls++)
  587. {
  588. if(CTrackApp::gpDLSBanks[iDls])
  589. {
  590. if(!m_tiDLS[iDls])
  591. {
  592. TVSORTCB tvs;
  593. CDLSBank dlsBank = *CTrackApp::gpDLSBanks[iDls];
  594. // Add DLS file folder
  595. m_tiDLS[iDls] = InsertItem(TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM,
  596. dlsBank.GetFileName().GetFullFileName().AsNative().c_str(), IMAGE_FOLDER, IMAGE_FOLDER, 0, 0, iDls, TVI_ROOT, hDlsRoot);
  597. // Memorize Banks
  598. std::map<uint16, HTREEITEM> banks;
  599. // Add Drum Kits folder
  600. HTREEITEM hDrums = InsertItem(TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE | TVIF_PARAM,
  601. _T("Drum Kits"), IMAGE_FOLDER, IMAGE_FOLDER, 0, 0, DLS_DRUM_FOLDER_LPARAM, m_tiDLS[iDls], TVI_LAST);
  602. // Add Instruments
  603. UINT nInstr = dlsBank.GetNumInstruments();
  604. MPT_ASSERT(nInstr <= 0x10000);
  605. for(UINT iIns = 0; iIns < nInstr; iIns++)
  606. {
  607. const DLSINSTRUMENT *pDlsIns = dlsBank.GetInstrument(iIns);
  608. if(pDlsIns)
  609. {
  610. TCHAR szName[256];
  611. wsprintf(szName, _T("%u: %s"), pDlsIns->ulInstrument & 0x7F, mpt::ToCString(charset, pDlsIns->szName).GetString());
  612. const LPARAM lParamInstr = DlsItem::ToLPARAM(static_cast<uint16>(iIns), (pDlsIns->ulInstrument & 0x7F), false);
  613. // Drum Kit
  614. if(pDlsIns->ulBank & F_INSTRUMENT_DRUMS)
  615. {
  616. HTREEITEM hKit = InsertItem(TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM,
  617. szName, IMAGE_FOLDER, IMAGE_FOLDER, 0, 0, lParamInstr, hDrums, TVI_LAST);
  618. MPT_ASSERT(pDlsIns->Regions.size() <= 0x8000);
  619. for(uint32 iRgn = 0; iRgn < static_cast<uint32>(pDlsIns->Regions.size()); iRgn++)
  620. {
  621. if(pDlsIns->Regions[iRgn].IsDummy())
  622. continue;
  623. UINT keymin = pDlsIns->Regions[iRgn].uKeyMin;
  624. UINT keymax = pDlsIns->Regions[iRgn].uKeyMax;
  625. const char *regionName = dlsBank.GetRegionName(iIns, iRgn);
  626. if(regionName == nullptr || !regionName[0])
  627. {
  628. if(keymin >= 24 && keymin <= 84)
  629. regionName = szMidiPercussionNames[keymin - 24];
  630. else
  631. regionName = "";
  632. }
  633. if(keymin >= keymax)
  634. {
  635. wsprintf(szName, _T("%s%u: %s"),
  636. mpt::ToCString(CSoundFile::GetDefaultNoteName(keymin % 12)).GetString(),
  637. keymin / 12,
  638. mpt::ToCString(charset, regionName).GetString());
  639. } else
  640. {
  641. wsprintf(szName, _T("%s%u-%s%u: %s"),
  642. mpt::ToCString(CSoundFile::GetDefaultNoteName(keymin % 12)).GetString(),
  643. keymin / 12,
  644. mpt::ToCString(CSoundFile::GetDefaultNoteName(keymax % 12)).GetString(),
  645. keymax / 12,
  646. mpt::ToCString(charset, regionName).GetString());
  647. }
  648. LPARAM lParam = DlsItem::ToLPARAM(static_cast<uint16>(iIns), static_cast<uint16>(iRgn), true);
  649. InsertItem(TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM,
  650. szName, IMAGE_INSTRUMENTS, IMAGE_INSTRUMENTS, 0, 0, lParam, hKit, TVI_LAST);
  651. }
  652. tvs.hParent = hKit;
  653. tvs.lpfnCompare = ModTreeDrumCompareProc;
  654. tvs.lParam = reinterpret_cast<LPARAM>(CTrackApp::gpDLSBanks[iDls].get());
  655. SortChildrenCB(&tvs);
  656. } else
  657. // Melodic
  658. {
  659. uint16 mbank = (pDlsIns->ulBank & 0x7F7F);
  660. auto hbank = banks.find(mbank);
  661. if(hbank == banks.end())
  662. {
  663. wsprintf(s, (mbank) ? _T("Melodic Bank %02d.%02d") : _T("Melodic"), mbank >> 8, mbank & 0x7F);
  664. // Find out where to insert this bank in the tree
  665. hbank = banks.insert(std::make_pair(mbank, nullptr)).first;
  666. HTREEITEM insertAfter = (hbank == banks.begin()) ? TVI_FIRST : std::prev(hbank)->second;
  667. hbank->second = InsertItem(TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE,
  668. s, IMAGE_FOLDER, IMAGE_FOLDER, 0, 0, 0,
  669. m_tiDLS[iDls], insertAfter);
  670. }
  671. InsertItem(TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM,
  672. szName, IMAGE_INSTRUMENTS, IMAGE_INSTRUMENTS, 0, 0, lParamInstr, hbank->second, TVI_LAST);
  673. }
  674. }
  675. }
  676. // Sort items
  677. for(auto &b : banks)
  678. {
  679. tvs.hParent = b.second;
  680. tvs.lpfnCompare = ModTreeInsLibCompareProc;
  681. tvs.lParam = reinterpret_cast<LPARAM>(this);
  682. SortChildrenCB(&tvs);
  683. }
  684. if(hDrums)
  685. {
  686. tvs.hParent = hDrums;
  687. tvs.lpfnCompare = ModTreeInsLibCompareProc;
  688. tvs.lParam = reinterpret_cast<LPARAM>(this);
  689. SortChildrenCB(&tvs);
  690. }
  691. }
  692. hDlsRoot = m_tiDLS[iDls];
  693. } else
  694. {
  695. if(m_tiDLS[iDls])
  696. {
  697. DeleteItem(m_tiDLS[iDls]);
  698. m_tiDLS[iDls] = nullptr;
  699. }
  700. }
  701. }
  702. }
  703. void CModTree::RefreshInstrumentLibrary()
  704. {
  705. SetRedraw(FALSE);
  706. // Check if the currently selected item should be selected after refreshing
  707. CString selectedName;
  708. if((IsSampleBrowser() || GetParentRootItem(GetSelectedItem()) == m_hInsLib)
  709. && GetItemText(GetSampleBrowser()->m_hInsLib) == (m_SongFileName.empty() ? m_InstrLibPath : m_SongFileName).ToCString())
  710. {
  711. selectedName = GetItemText(GetSelectedItem());
  712. }
  713. if(!m_InstrLibHighlightPath.empty())
  714. {
  715. selectedName = m_InstrLibHighlightPath.ToCString();
  716. }
  717. m_InstrLibHighlightPath = {};
  718. EmptyInstrumentLibrary();
  719. FillInstrumentLibrary(selectedName);
  720. auto selectedItem = GetSelectedItem();
  721. if(selectedItem)
  722. EnsureVisible(selectedItem);
  723. SetRedraw(TRUE);
  724. if(!IsSampleBrowser())
  725. {
  726. m_pDataTree->InsLibSetFullPath(m_InstrLibPath, m_SongFileName);
  727. m_pDataTree->RefreshInstrumentLibrary();
  728. }
  729. }
  730. void CModTree::UpdateView(ModTreeDocInfo &info, UpdateHint hint)
  731. {
  732. TCHAR stmp[256];
  733. TVITEM tvi;
  734. MemsetZero(tvi);
  735. const FlagSet<HintType> hintType = hint.GetType();
  736. if(IsSampleBrowser() || hintType == HINT_NONE)
  737. return;
  738. const CModDoc &modDoc = info.modDoc;
  739. const CSoundFile &sndFile = modDoc.GetSoundFile();
  740. // Create headers
  741. const GeneralHint generalHint = hint.ToType<GeneralHint>();
  742. if(generalHint.GetType()[HINT_MODTYPE | HINT_MODGENERAL] || (!info.hSong))
  743. {
  744. // Module folder + sub folders
  745. CString name = modDoc.GetPathNameMpt().GetFullFileName().ToCString();
  746. if(name.IsEmpty())
  747. name = mpt::PathString::FromCString(modDoc.GetTitle()).SanitizeComponent().ToCString();
  748. if(!info.hSong)
  749. {
  750. info.hSong = InsertItem(TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM, name, IMAGE_FOLDER, IMAGE_FOLDER, 0, 0, reinterpret_cast<LPARAM>(&info.modDoc), TVI_ROOT, TVI_FIRST);
  751. info.hOrders = InsertItem(_T("Sequence"), IMAGE_FOLDER, IMAGE_FOLDER, info.hSong, TVI_LAST);
  752. info.hPatterns = InsertItem(_T("Patterns"), IMAGE_FOLDER, IMAGE_FOLDER, info.hSong, TVI_LAST);
  753. info.hSamples = InsertItem(_T("Samples"), IMAGE_FOLDER, IMAGE_FOLDER, info.hSong, TVI_LAST);
  754. } else if(generalHint.GetType()[HINT_MODGENERAL | HINT_MODTYPE])
  755. {
  756. if(name != GetItemText(info.hSong))
  757. {
  758. SetItemText(info.hSong, name);
  759. }
  760. }
  761. }
  762. if(sndFile.GetModSpecifications().instrumentsMax > 0)
  763. {
  764. if(!info.hInstruments)
  765. info.hInstruments = InsertItem(_T("Instruments"), IMAGE_FOLDER, IMAGE_FOLDER, info.hSong, info.hSamples);
  766. } else
  767. {
  768. if(info.hInstruments)
  769. {
  770. DeleteItem(info.hInstruments);
  771. info.hInstruments = NULL;
  772. }
  773. }
  774. if(!info.hComments)
  775. info.hComments = InsertItem(_T("Comments"), IMAGE_COMMENTS, IMAGE_COMMENTS, info.hSong, TVI_LAST);
  776. // Add effects
  777. const PluginHint pluginHint = hint.ToType<PluginHint>();
  778. if(pluginHint.GetType()[HINT_MODTYPE | HINT_PLUGINNAMES])
  779. {
  780. HTREEITEM hItem = info.hEffects ? GetChildItem(info.hEffects) : nullptr;
  781. PLUGINDEX firstPlug = 0, lastPlug = MAX_MIXPLUGINS - 1;
  782. if(pluginHint.GetPlugin() && hItem)
  783. {
  784. // Only update one specific plugin name
  785. firstPlug = lastPlug = pluginHint.GetPlugin() - 1;
  786. while(hItem && GetItemData(hItem) != firstPlug)
  787. {
  788. hItem = GetNextSiblingItem(hItem);
  789. }
  790. }
  791. bool hasPlugs = false;
  792. for(PLUGINDEX i = firstPlug; i <= lastPlug; i++)
  793. {
  794. const SNDMIXPLUGIN &plugin = sndFile.m_MixPlugins[i];
  795. if(plugin.IsValidPlugin())
  796. {
  797. // Now we can be sure that we want to create this folder.
  798. if(!info.hEffects)
  799. {
  800. info.hEffects = InsertItem(_T("Plugins"), IMAGE_FOLDER, IMAGE_FOLDER, info.hSong, info.hInstruments ? info.hInstruments : info.hSamples);
  801. }
  802. CString s = MPT_CFORMAT("FX{}: {}")(i + 1, mpt::ToCString(plugin.GetName()));
  803. int nImage = IMAGE_NOPLUGIN;
  804. if(plugin.pMixPlugin != nullptr)
  805. nImage = (plugin.pMixPlugin->IsInstrument()) ? IMAGE_PLUGININSTRUMENT : IMAGE_EFFECTPLUGIN;
  806. if(hItem)
  807. {
  808. // Replace existing item
  809. tvi.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
  810. tvi.hItem = hItem;
  811. tvi.pszText = stmp;
  812. tvi.cchTextMax = mpt::saturate_cast<int>(std::size(stmp));
  813. GetItem(&tvi);
  814. if(tvi.iImage != nImage || tvi.lParam != i || s != CString(tvi.pszText))
  815. {
  816. SetItem(hItem, TVIF_TEXT | TVIF_HANDLE | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM, s, nImage, nImage, 0, 0, i);
  817. }
  818. hItem = GetNextSiblingItem(hItem);
  819. } else
  820. {
  821. InsertItem(TVIF_TEXT | TVIF_HANDLE | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM, s, nImage, nImage, 0, 0, i, info.hEffects, TVI_LAST);
  822. }
  823. hasPlugs = true;
  824. }
  825. }
  826. if(!hasPlugs && firstPlug == lastPlug)
  827. {
  828. // If we only updated one plugin, we still need to check all the other slots if there is any plugin in them.
  829. for(const auto &plug : sndFile.m_MixPlugins)
  830. {
  831. if(plug.IsValidPlugin())
  832. {
  833. hasPlugs = true;
  834. break;
  835. }
  836. }
  837. }
  838. if(!hasPlugs && info.hEffects)
  839. {
  840. DeleteItem(info.hEffects);
  841. info.hEffects = nullptr;
  842. } else if(!pluginHint.GetPlugin())
  843. {
  844. // Delete superfluous tree items
  845. while(hItem)
  846. {
  847. HTREEITEM nextItem = GetNextSiblingItem(hItem);
  848. DeleteItem(hItem);
  849. hItem = nextItem;
  850. }
  851. }
  852. }
  853. // Add Orders
  854. const PatternHint patternHint = hint.ToType<PatternHint>();
  855. const SequenceHint seqHint = hint.ToType<SequenceHint>();
  856. if(info.hOrders && (seqHint.GetType()[HINT_MODTYPE | HINT_MODSEQUENCE | HINT_SEQNAMES] || patternHint.GetType()[HINT_PATNAMES]))
  857. {
  858. const PATTERNINDEX nPat = patternHint.GetPattern();
  859. bool adjustParentNode = false; // adjust sequence name of "Sequence" node?
  860. // (only one seq remaining || previously only one sequence): update parent item
  861. if((info.tiSequences.size() > 1 && sndFile.Order.GetNumSequences() == 1) || (info.tiSequences.size() == 1 && sndFile.Order.GetNumSequences() > 1))
  862. {
  863. for(auto &seq : info.tiOrders)
  864. {
  865. for(auto &ord : seq)
  866. {
  867. if(ord)
  868. DeleteItem(ord);
  869. ord = nullptr;
  870. }
  871. }
  872. for(auto &seq : info.tiSequences)
  873. {
  874. if(seq)
  875. DeleteItem(seq);
  876. seq = nullptr;
  877. }
  878. info.tiOrders.resize(sndFile.Order.GetNumSequences());
  879. info.tiSequences.resize(sndFile.Order.GetNumSequences(), nullptr);
  880. adjustParentNode = true;
  881. }
  882. // If there are too many sequences, delete them.
  883. for(size_t seq = sndFile.Order.GetNumSequences(); seq < info.tiSequences.size(); seq++) if (info.tiSequences[seq])
  884. {
  885. for(auto &ord : info.tiOrders[seq]) if (ord)
  886. {
  887. DeleteItem(ord); ord = nullptr;
  888. }
  889. DeleteItem(info.tiSequences[seq]); info.tiSequences[seq] = nullptr;
  890. }
  891. if (info.tiSequences.size() < sndFile.Order.GetNumSequences()) // Resize tiSequences if needed.
  892. {
  893. info.tiSequences.resize(sndFile.Order.GetNumSequences(), nullptr);
  894. info.tiOrders.resize(sndFile.Order.GetNumSequences());
  895. }
  896. HTREEITEM hAncestorNode = info.hOrders;
  897. SEQUENCEINDEX nSeqMin = 0, nSeqMax = sndFile.Order.GetNumSequences() - 1;
  898. SEQUENCEINDEX nHintParam = seqHint.GetSequence();
  899. if(seqHint.GetType()[HINT_SEQNAMES] && (nHintParam <= nSeqMax))
  900. nSeqMin = nSeqMax = nHintParam;
  901. // Adjust caption of the "Sequence" node (if only one sequence exists, it should be labeled with the sequence name)
  902. if((seqHint.GetType()[HINT_SEQNAMES] && sndFile.Order.GetNumSequences() == 1) || adjustParentNode)
  903. {
  904. CString seqName = mpt::ToCString(sndFile.Order(0).GetName());
  905. if(seqName.IsEmpty() || sndFile.Order.GetNumSequences() > 1)
  906. seqName = _T("Sequence");
  907. else
  908. seqName = _T("Sequence: ") + seqName;
  909. SetItem(info.hOrders, TVIF_TEXT, seqName, 0, 0, 0, 0, 0);
  910. }
  911. // go through all sequences
  912. CString seqName;
  913. for(SEQUENCEINDEX seq = nSeqMin; seq <= nSeqMax; seq++)
  914. {
  915. if(sndFile.Order.GetNumSequences() > 1)
  916. {
  917. // more than one sequence -> add folder
  918. if(sndFile.Order(seq).GetName().empty())
  919. {
  920. seqName = MPT_CFORMAT("Sequence {}")(seq + 1);
  921. } else
  922. {
  923. seqName = MPT_CFORMAT("{}: ")(seq + 1);
  924. seqName += mpt::ToCString(sndFile.Order(seq).GetName());
  925. }
  926. UINT state = (seq == sndFile.Order.GetCurrentSequenceIndex()) ? TVIS_BOLD : 0;
  927. if(info.tiSequences[seq] == NULL)
  928. {
  929. info.tiSequences[seq] = InsertItem(seqName, IMAGE_FOLDER, IMAGE_FOLDER, info.hOrders, TVI_LAST);
  930. }
  931. // Update bold item
  932. _tcscpy(stmp, seqName);
  933. tvi.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_STATE | TVIF_PARAM;
  934. tvi.state = 0;
  935. tvi.stateMask = TVIS_BOLD;
  936. tvi.hItem = info.tiSequences[seq];
  937. tvi.pszText = stmp;
  938. tvi.cchTextMax = mpt::saturate_cast<int>(std::size(stmp));
  939. LPARAM param = (seq << SEQU_SHIFT) | ORDERINDEX_INVALID;
  940. GetItem(&tvi);
  941. if(tvi.state != state || tvi.pszText != seqName || tvi.lParam != param)
  942. SetItem(info.tiSequences[seq], TVIF_TEXT | TVIF_STATE | TVIF_PARAM, seqName, 0, 0, state, TVIS_BOLD, param);
  943. hAncestorNode = info.tiSequences[seq];
  944. }
  945. const ORDERINDEX ordLength = sndFile.Order(seq).GetLengthTailTrimmed();
  946. // If there are items past the new sequence length, delete them.
  947. for(size_t nOrd = ordLength; nOrd < info.tiOrders[seq].size(); nOrd++) if (info.tiOrders[seq][nOrd])
  948. {
  949. DeleteItem(info.tiOrders[seq][nOrd]); info.tiOrders[seq][nOrd] = NULL;
  950. }
  951. if (info.tiOrders[seq].size() < ordLength) // Resize tiOrders if needed.
  952. info.tiOrders[seq].resize(ordLength, nullptr);
  953. const bool patNamesOnly = patternHint.GetType()[HINT_PATNAMES];
  954. //if (hintFlagPart == HINT_PATNAMES) && (dwHintParam < sndFile.Order().GetLength())) imin = imax = dwHintParam;
  955. CString patName;
  956. for(ORDERINDEX iOrd = 0; iOrd < ordLength; iOrd++)
  957. {
  958. TCHAR s[256];
  959. s[0] = 0;
  960. if(patNamesOnly && sndFile.Order(seq)[iOrd] != nPat)
  961. continue;
  962. UINT state = (iOrd == info.ordSel && seq == info.seqSel) ? TVIS_BOLD : 0;
  963. if(sndFile.Order(seq)[iOrd] < sndFile.Patterns.Size())
  964. {
  965. patName = mpt::ToCString(sndFile.GetCharsetInternal(), sndFile.Patterns[sndFile.Order(seq)[iOrd]].GetName());
  966. if(!patName.IsEmpty())
  967. {
  968. wsprintf(s, (TrackerSettings::Instance().m_dwPatternSetup & PATTERN_HEXDISPLAY) ? _T("[%02Xh] %u: ") : _T("[%02u] %u: "),
  969. iOrd, sndFile.Order(seq)[iOrd]);
  970. _tcscat(s, patName.GetString());
  971. } else
  972. {
  973. wsprintf(s, (TrackerSettings::Instance().m_dwPatternSetup & PATTERN_HEXDISPLAY) ? _T("[%02Xh] Pattern %u") : _T("[%02u] Pattern %u"),
  974. iOrd, sndFile.Order(seq)[iOrd]);
  975. }
  976. } else
  977. {
  978. if(sndFile.Order(seq)[iOrd] == sndFile.Order.GetIgnoreIndex())
  979. {
  980. // +++ Item
  981. wsprintf(s, _T("[%02u] Skip"), iOrd);
  982. } else
  983. {
  984. // --- Item
  985. wsprintf(s, _T("[%02u] Stop"), iOrd);
  986. }
  987. }
  988. LPARAM param = (seq << SEQU_SHIFT) | iOrd;
  989. if(info.tiOrders[seq][iOrd])
  990. {
  991. tvi.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_STATE;
  992. tvi.state = 0;
  993. tvi.stateMask = TVIS_BOLD;
  994. tvi.hItem = info.tiOrders[seq][iOrd];
  995. tvi.pszText = stmp;
  996. tvi.cchTextMax = mpt::saturate_cast<int>(std::size(stmp));
  997. GetItem(&tvi);
  998. if(tvi.state != state || _tcscmp(s, stmp))
  999. SetItem(info.tiOrders[seq][iOrd], TVIF_TEXT | TVIF_STATE | TVIF_PARAM, s, 0, 0, state, TVIS_BOLD, param);
  1000. } else
  1001. {
  1002. info.tiOrders[seq][iOrd] = InsertItem(TVIF_HANDLE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM, s, IMAGE_PARTITION, IMAGE_PARTITION, 0, 0, param, hAncestorNode, TVI_LAST);
  1003. }
  1004. }
  1005. }
  1006. }
  1007. // Add Patterns
  1008. if(info.hPatterns && patternHint.GetType()[HINT_MODTYPE | HINT_PATNAMES])
  1009. {
  1010. const PATTERNINDEX nPat = patternHint.GetPattern();
  1011. PATTERNINDEX minPat = 0, maxPat = sndFile.Patterns.Size();
  1012. if(patternHint.GetType()[HINT_PATNAMES] && nPat < sndFile.Patterns.Size())
  1013. {
  1014. minPat = nPat;
  1015. maxPat = nPat + 1;
  1016. }
  1017. for(size_t pat = sndFile.Patterns.Size(); pat < info.tiPatterns.size(); pat++)
  1018. {
  1019. DeleteItem(info.tiPatterns[pat]);
  1020. }
  1021. info.tiPatterns.resize(sndFile.Patterns.Size(), nullptr);
  1022. mpt::winstring patName;
  1023. for(PATTERNINDEX pat = minPat; pat < maxPat; pat++)
  1024. {
  1025. TCHAR s[256];
  1026. s[0] = 0;
  1027. if(sndFile.Patterns.IsValidPat(pat))
  1028. {
  1029. patName = mpt::ToWin(sndFile.GetCharsetInternal(), sndFile.Patterns[pat].GetName());
  1030. wsprintf(s, _T("%u"), pat);
  1031. if(!patName.empty())
  1032. {
  1033. _tcscat(s, _T(": "));
  1034. _tcscat(s, patName.c_str());
  1035. }
  1036. if(info.tiPatterns[pat])
  1037. {
  1038. tvi.mask = TVIF_TEXT | TVIF_HANDLE;
  1039. tvi.hItem = info.tiPatterns[pat];
  1040. tvi.pszText = stmp;
  1041. tvi.cchTextMax = mpt::saturate_cast<int>(std::size(stmp));
  1042. GetItem(&tvi);
  1043. if(_tcscmp(s, stmp))
  1044. SetItem(info.tiPatterns[pat], TVIF_TEXT, s, 0, 0, 0, 0, 0);
  1045. } else
  1046. {
  1047. info.tiPatterns[pat] = InsertItem(s, IMAGE_PATTERNS, IMAGE_PATTERNS, info.hPatterns, TVI_LAST);
  1048. }
  1049. SetItemData(info.tiPatterns[pat], pat);
  1050. } else if(pat < info.tiPatterns.size() && info.tiPatterns[pat])
  1051. {
  1052. DeleteItem(info.tiPatterns[pat]);
  1053. info.tiPatterns[pat] = nullptr;
  1054. }
  1055. }
  1056. }
  1057. // Add Samples
  1058. const SampleHint sampleHint = hint.ToType<SampleHint>();
  1059. if(info.hSamples && sampleHint.GetType()[HINT_MODTYPE | HINT_SMPNAMES | HINT_SAMPLEINFO | HINT_SAMPLEDATA])
  1060. {
  1061. const SAMPLEINDEX hintSmp = sampleHint.GetSample();
  1062. SAMPLEINDEX smin = 1, smax = MAX_SAMPLES - 1;
  1063. if(sampleHint.GetType()[HINT_SMPNAMES | HINT_SAMPLEINFO | HINT_SAMPLEDATA] && hintSmp > 0 && hintSmp < MAX_SAMPLES)
  1064. {
  1065. smin = smax = hintSmp;
  1066. }
  1067. HTREEITEM hChild = GetNthChildItem(info.hSamples, smin - 1);
  1068. for(SAMPLEINDEX nSmp = smin; nSmp <= smax; nSmp++)
  1069. {
  1070. TCHAR s[256];
  1071. s[0] = 0;
  1072. HTREEITEM hNextChild = GetNextSiblingItem(hChild);
  1073. if(nSmp <= sndFile.GetNumSamples())
  1074. {
  1075. const ModSample &sample = sndFile.GetSample(nSmp);
  1076. const bool sampleExists = (sample.HasSampleData());
  1077. static constexpr int Images[] =
  1078. {
  1079. IMAGE_NOSAMPLE, IMAGE_NOSAMPLE, IMAGE_NOSAMPLE,
  1080. IMAGE_SAMPLES, IMAGE_SAMPLEACTIVE, IMAGE_SAMPLEMUTE,
  1081. IMAGE_EXTSAMPLE, IMAGE_EXTSAMPLEACTIVE, IMAGE_EXTSAMPLEMUTE,
  1082. IMAGE_OPLINSTR, IMAGE_OPLINSTRACTIVE, IMAGE_OPLINSTRMUTE,
  1083. };
  1084. int image = 0;
  1085. if(sampleExists)
  1086. image = 3;
  1087. if(sample.uFlags[SMP_KEEPONDISK])
  1088. image = 6;
  1089. if(sample.uFlags[CHN_ADLIB])
  1090. image = 9;
  1091. if(info.modDoc.IsSampleMuted(nSmp))
  1092. image += 2;
  1093. else if(info.samplesPlaying[nSmp])
  1094. image++;
  1095. if(sample.uFlags[SMP_KEEPONDISK] && !sampleExists)
  1096. image = IMAGE_EXTSAMPLEMISSING;
  1097. else
  1098. image = Images[image];
  1099. if(sndFile.GetType() == MOD_TYPE_MPT)
  1100. {
  1101. const TCHAR *status = _T("");
  1102. if(sample.uFlags[SMP_KEEPONDISK])
  1103. {
  1104. status = sampleExists ? _T(" [external]") : _T(" [MISSING]");
  1105. }
  1106. wsprintf(s, _T("%3d: %s%s%s"), nSmp, sample.uFlags.test_all(SMP_MODIFIED | SMP_KEEPONDISK) ? _T("* ") : _T(""), mpt::ToCString(sndFile.GetCharsetInternal(), sndFile.m_szNames[nSmp]).GetString(), status);
  1107. } else
  1108. {
  1109. wsprintf(s, _T("%3d: %s"), nSmp, mpt::ToCString(sndFile.GetCharsetInternal(), sndFile.m_szNames[nSmp]).GetString());
  1110. }
  1111. if(!hChild)
  1112. {
  1113. hChild = InsertItem(TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM, s, image, image, 0, 0, nSmp, info.hSamples, TVI_LAST);
  1114. } else
  1115. {
  1116. tvi.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_IMAGE;
  1117. tvi.hItem = hChild;
  1118. tvi.pszText = stmp;
  1119. tvi.cchTextMax = mpt::saturate_cast<int>(std::size(stmp));
  1120. tvi.iImage = tvi.iSelectedImage = image;
  1121. GetItem(&tvi);
  1122. if(tvi.iImage != image || _tcsncmp(s, stmp, std::size(stmp)) || GetItemData(hChild) != nSmp)
  1123. {
  1124. SetItem(hChild, TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM, s, image, image, 0, 0, nSmp);
  1125. }
  1126. }
  1127. } else if(hChild != nullptr)
  1128. {
  1129. DeleteItem(hChild);
  1130. } else
  1131. {
  1132. break;
  1133. }
  1134. hChild = hNextChild;
  1135. }
  1136. }
  1137. // Add Instruments
  1138. const InstrumentHint instrHint = hint.ToType<InstrumentHint>();
  1139. if(info.hInstruments && instrHint.GetType()[HINT_MODTYPE | HINT_INSNAMES | HINT_INSTRUMENT])
  1140. {
  1141. INSTRUMENTINDEX smin = 1, smax = MAX_INSTRUMENTS - 1;
  1142. const INSTRUMENTINDEX hintIns = instrHint.GetInstrument();
  1143. if(instrHint.GetType()[HINT_INSNAMES | HINT_INSTRUMENT] && hintIns > 0 && hintIns < MAX_INSTRUMENTS)
  1144. {
  1145. smin = smax = hintIns;
  1146. }
  1147. HTREEITEM hChild = GetNthChildItem(info.hInstruments, smin - 1);
  1148. for(INSTRUMENTINDEX nIns = smin; nIns <= smax; nIns++)
  1149. {
  1150. TCHAR s[256];
  1151. s[0] = 0;
  1152. HTREEITEM hNextChild = GetNextSiblingItem(hChild);
  1153. if(nIns <= sndFile.GetNumInstruments())
  1154. {
  1155. wsprintf(s, _T("%3u: %s"), nIns, mpt::ToCString(sndFile.GetCharsetInternal(), sndFile.GetInstrumentName(nIns)).GetString());
  1156. int nImage = IMAGE_INSTRUMENTS;
  1157. if(info.instrumentsPlaying[nIns])
  1158. nImage = IMAGE_INSTRACTIVE;
  1159. if(!sndFile.Instruments[nIns] || info.modDoc.IsInstrumentMuted(nIns))
  1160. nImage = IMAGE_INSTRMUTE;
  1161. if(!hChild)
  1162. {
  1163. hChild = InsertItem(TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM, s, nImage, nImage, 0, 0, nIns, info.hInstruments, TVI_LAST);
  1164. } else
  1165. {
  1166. tvi.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_IMAGE;
  1167. tvi.hItem = hChild;
  1168. tvi.pszText = stmp;
  1169. tvi.cchTextMax = mpt::saturate_cast<int>(std::size(stmp));
  1170. tvi.iImage = tvi.iSelectedImage = nImage;
  1171. GetItem(&tvi);
  1172. if(tvi.iImage != nImage || _tcscmp(s, stmp) || GetItemData(hChild) != nIns)
  1173. {
  1174. SetItem(hChild, TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM, s, nImage, nImage, 0, 0, nIns);
  1175. }
  1176. }
  1177. } else if(hChild != nullptr)
  1178. {
  1179. DeleteItem(hChild);
  1180. } else
  1181. {
  1182. break;
  1183. }
  1184. hChild = hNextChild;
  1185. }
  1186. }
  1187. }
  1188. CModTree::ModItem CModTree::GetModItem(HTREEITEM hItem)
  1189. {
  1190. if(!hItem)
  1191. return ModItem(MODITEM_NULL);
  1192. // First, test root items
  1193. if(hItem == m_hInsLib)
  1194. return ModItem(MODITEM_HDR_INSTRUMENTLIB);
  1195. if(hItem == m_hMidiLib)
  1196. return ModItem(MODITEM_HDR_MIDILIB);
  1197. // The immediate parent of the item (NULL if this item is on the root level of the tree)
  1198. HTREEITEM hItemParent = GetParentItem(hItem);
  1199. // Parent of the parent.
  1200. HTREEITEM hItemParentParent = GetParentItem(hItemParent);
  1201. // Get the root parent of the selected item, which can be the item itself.
  1202. HTREEITEM hRootParent = hItem;
  1203. if(!IsSampleBrowser())
  1204. {
  1205. hRootParent = GetParentRootItem(hItem);
  1206. }
  1207. uint32 itemData = static_cast<uint32>(GetItemData(hItem));
  1208. uint32 rootItemData = static_cast<uint32>(GetItemData(hRootParent));
  1209. // Midi Library
  1210. if(hRootParent == m_hMidiLib && hRootParent != hItem && !IsSampleBrowser())
  1211. {
  1212. return ModItem(static_cast<ModItemType>(itemData >> MIDILIB_SHIFT), itemData & MIDILIB_MASK);
  1213. }
  1214. // Instrument Library
  1215. if(hRootParent == m_hInsLib || (IsSampleBrowser() && hItem != m_hInsLib))
  1216. {
  1217. TVITEM tvi;
  1218. tvi.mask = TVIF_IMAGE | TVIF_HANDLE;
  1219. tvi.hItem = hItem;
  1220. tvi.iImage = 0;
  1221. if(GetItem(&tvi))
  1222. {
  1223. switch(tvi.iImage)
  1224. {
  1225. case IMAGE_SAMPLES:
  1226. case IMAGE_OPLINSTR:
  1227. // Sample
  1228. return ModItem(MODITEM_INSLIB_SAMPLE);
  1229. case IMAGE_INSTRUMENTS:
  1230. // Instrument
  1231. return ModItem(MODITEM_INSLIB_INSTRUMENT);
  1232. case IMAGE_FOLDERSONG:
  1233. // Song
  1234. return ModItem(MODITEM_INSLIB_SONG);
  1235. default:
  1236. return ModItem(MODITEM_INSLIB_FOLDER);
  1237. }
  1238. }
  1239. return ModItem(MODITEM_NULL);
  1240. }
  1241. if(IsSampleBrowser())
  1242. return ModItem(MODITEM_NULL);
  1243. // Songs
  1244. if(auto info = GetDocumentInfoFromItem(hRootParent); info != nullptr)
  1245. {
  1246. m_selectedDoc = &info->modDoc;
  1247. if(hItem == info->hSong)
  1248. return ModItem(MODITEM_HDR_SONG);
  1249. if(hRootParent == info->hSong)
  1250. {
  1251. if(hItem == info->hPatterns)
  1252. return ModItem(MODITEM_HDR_PATTERNS);
  1253. if(hItem == info->hOrders)
  1254. return ModItem(MODITEM_HDR_ORDERS);
  1255. if(hItem == info->hSamples)
  1256. return ModItem(MODITEM_HDR_SAMPLES);
  1257. if(hItem == info->hInstruments)
  1258. return ModItem(MODITEM_HDR_INSTRUMENTS);
  1259. if(hItem == info->hEffects)
  1260. return ModItem(MODITEM_HDR_EFFECTS);
  1261. if(hItem == info->hComments)
  1262. return ModItem(MODITEM_COMMENTS);
  1263. // Order List or Sequence item?
  1264. if((hItemParent == info->hOrders) || (hItemParentParent == info->hOrders))
  1265. {
  1266. const auto ord = static_cast<ORDERINDEX>(itemData & SEQU_MASK);
  1267. const auto seq = static_cast<SEQUENCEINDEX>(itemData >> SEQU_SHIFT);
  1268. if(ord == ORDERINDEX_INVALID)
  1269. return ModItem(MODITEM_SEQUENCE, seq);
  1270. else
  1271. return ModItem(MODITEM_ORDER, ord, seq);
  1272. }
  1273. ModItem modItem(MODITEM_NULL, itemData);
  1274. if(hItemParent == info->hPatterns)
  1275. {
  1276. // Pattern
  1277. modItem.type = MODITEM_PATTERN;
  1278. } else if(hItemParent == info->hSamples)
  1279. {
  1280. // Sample
  1281. modItem.type = MODITEM_SAMPLE;
  1282. } else if(hItemParent == info->hInstruments)
  1283. {
  1284. // Instrument
  1285. modItem.type = MODITEM_INSTRUMENT;
  1286. } else if(hItemParent == info->hEffects)
  1287. {
  1288. // Effect
  1289. modItem.type = MODITEM_EFFECT;
  1290. }
  1291. return modItem;
  1292. }
  1293. }
  1294. // DLS banks
  1295. if(itemData < m_tiDLS.size() && hItem == m_tiDLS[itemData])
  1296. return ModItem(MODITEM_DLSBANK_FOLDER, itemData);
  1297. // DLS Instruments
  1298. if(hRootParent != nullptr)
  1299. {
  1300. if(rootItemData < m_tiDLS.size() && m_tiDLS[rootItemData] == hRootParent)
  1301. {
  1302. int image = 0, selImage = 0;
  1303. const bool isFolder = GetItemImage(hItem, image, selImage) && (image == IMAGE_FOLDER || image == IMAGE_OPENFOLDER);
  1304. if(!isFolder || GetItemData(hItemParent) == DLS_DRUM_FOLDER_LPARAM)
  1305. return DlsItem::FromLPARAM(itemData);
  1306. }
  1307. }
  1308. return ModItem(MODITEM_NULL);
  1309. }
  1310. bool CModTree::ExecuteItem(HTREEITEM hItem)
  1311. {
  1312. if(hItem)
  1313. {
  1314. const ModItem modItem = GetModItem(hItem);
  1315. uint32 modItemID = modItem.val1;
  1316. CModDoc *modDoc = m_docInfo.count(m_selectedDoc) ? m_selectedDoc : nullptr;
  1317. switch(modItem.type)
  1318. {
  1319. case MODITEM_COMMENTS:
  1320. if(modDoc)
  1321. modDoc->ActivateView(IDD_CONTROL_COMMENTS, 0);
  1322. return true;
  1323. case MODITEM_SEQUENCE:
  1324. if(modDoc
  1325. && modItemID < modDoc->GetSoundFile().Order.GetNumSequences()
  1326. && !modDoc->GetSoundFile().Order(static_cast<SEQUENCEINDEX>(modItemID)).GetLengthTailTrimmed())
  1327. modDoc->ActivateView(IDD_CONTROL_PATTERNS, (modItemID << SEQU_SHIFT) | SEQU_INDICATOR);
  1328. return true;
  1329. case MODITEM_ORDER:
  1330. if(modDoc)
  1331. modDoc->ActivateView(IDD_CONTROL_PATTERNS, modItemID | (uint32(modItem.val2) << SEQU_SHIFT) | SEQU_INDICATOR);
  1332. return true;
  1333. case MODITEM_PATTERN:
  1334. if(modDoc)
  1335. modDoc->ActivateView(IDD_CONTROL_PATTERNS, modItemID);
  1336. return true;
  1337. case MODITEM_SAMPLE:
  1338. if(modDoc)
  1339. modDoc->ActivateView(IDD_CONTROL_SAMPLES, modItemID);
  1340. return true;
  1341. case MODITEM_INSTRUMENT:
  1342. if(modDoc)
  1343. modDoc->ActivateView(IDD_CONTROL_INSTRUMENTS, modItemID);
  1344. return true;
  1345. case MODITEM_MIDIPERCUSSION:
  1346. modItemID |= 0x80;
  1347. [[fallthrough]];
  1348. case MODITEM_MIDIINSTRUMENT:
  1349. OpenMidiInstrument(modItemID);
  1350. return true;
  1351. case MODITEM_EFFECT:
  1352. case MODITEM_INSLIB_SAMPLE:
  1353. case MODITEM_INSLIB_INSTRUMENT:
  1354. PlayItem(hItem, NOTE_MIDDLEC);
  1355. return true;
  1356. case MODITEM_INSLIB_SONG:
  1357. case MODITEM_INSLIB_FOLDER:
  1358. // If it's a shell link, resolve the link target
  1359. if(auto file = LinkResolver().Resolve(m_InstrLibPath.ToCString() + GetItemText(hItem)); !file.empty())
  1360. {
  1361. SetFullInstrumentLibraryPath(file);
  1362. } else
  1363. {
  1364. InstrumentLibraryChDir(mpt::PathString::FromCString(GetItemText(hItem)), modItem.type == MODITEM_INSLIB_SONG);
  1365. }
  1366. return true;
  1367. case MODITEM_HDR_SONG:
  1368. if(modDoc)
  1369. modDoc->ActivateWindow();
  1370. return true;
  1371. case MODITEM_DLSBANK_INSTRUMENT:
  1372. if(GetItemData(GetParentItem(hItem)) != DLS_DRUM_FOLDER_LPARAM)
  1373. PlayItem(hItem, NOTE_MIDDLEC);
  1374. return true;
  1375. case MODITEM_HDR_INSTRUMENTLIB:
  1376. if(IsSampleBrowser())
  1377. {
  1378. BrowseForFolder dlg(m_InstrLibPath, _T("Select a new instrument library folder..."));
  1379. if(dlg.Show())
  1380. {
  1381. SetFullInstrumentLibraryPath(dlg.GetDirectory());
  1382. }
  1383. }
  1384. return true;
  1385. }
  1386. }
  1387. return false;
  1388. }
  1389. void CModTree::PlayDLSItem(const CDLSBank &dlsBank, const DlsItem &item, ModCommand::NOTE note)
  1390. {
  1391. UINT rgn, instr = item.GetInstr();
  1392. if(item.IsPercussion())
  1393. rgn = item.GetRegion();
  1394. else
  1395. rgn = dlsBank.GetRegionFromKey(instr, note - NOTE_MIN);
  1396. CMainFrame::GetMainFrame()->PlayDLSInstrument(dlsBank, instr, rgn, note);
  1397. }
  1398. BOOL CModTree::PlayItem(HTREEITEM hItem, ModCommand::NOTE note, int volume)
  1399. {
  1400. if(hItem)
  1401. {
  1402. const ModItem modItem = GetModItem(hItem);
  1403. uint32 modItemID = modItem.val1;
  1404. CModDoc *modDoc = m_docInfo.count(m_selectedDoc) ? m_selectedDoc : nullptr;
  1405. switch(modItem.type)
  1406. {
  1407. case MODITEM_SAMPLE:
  1408. if(modDoc)
  1409. {
  1410. if(note == NOTE_NOTECUT)
  1411. {
  1412. modDoc->NoteOff(0, true); // cut previous playing samples
  1413. } else if(note & 0x80)
  1414. {
  1415. modDoc->NoteOff(note & 0x7F, true);
  1416. } else
  1417. {
  1418. modDoc->NoteOff(0, true); // cut previous playing samples
  1419. modDoc->PlayNote(PlayNoteParam(note & 0x7F).Sample(static_cast<SAMPLEINDEX>(modItemID)).Volume(volume));
  1420. }
  1421. }
  1422. return TRUE;
  1423. case MODITEM_INSTRUMENT:
  1424. if(modDoc)
  1425. {
  1426. if(note == NOTE_NOTECUT)
  1427. {
  1428. modDoc->NoteOff(0, true);
  1429. } else if(note & 0x80)
  1430. {
  1431. modDoc->NoteOff(note & 0x7F, true);
  1432. } else
  1433. {
  1434. modDoc->NoteOff(0, true);
  1435. modDoc->PlayNote(PlayNoteParam(note & 0x7F).Instrument(static_cast<INSTRUMENTINDEX>(modItemID)).Volume(volume));
  1436. }
  1437. }
  1438. return TRUE;
  1439. case MODITEM_EFFECT:
  1440. if((modDoc) && (modItemID < MAX_MIXPLUGINS))
  1441. {
  1442. modDoc->TogglePluginEditor(modItemID);
  1443. }
  1444. return TRUE;
  1445. case MODITEM_INSLIB_SAMPLE:
  1446. case MODITEM_INSLIB_INSTRUMENT:
  1447. if(note != NOTE_NOTECUT)
  1448. {
  1449. CMainFrame *pMainFrm = CMainFrame::GetMainFrame();
  1450. if(!m_SongFileName.empty())
  1451. {
  1452. // Preview sample / instrument in module
  1453. const size_t n = ConvertStrTo<size_t>(GetItemText(hItem));
  1454. if(pMainFrm && m_SongFile)
  1455. {
  1456. if(modItem.type == MODITEM_INSLIB_INSTRUMENT)
  1457. {
  1458. pMainFrm->PlaySoundFile(*m_SongFile, static_cast<INSTRUMENTINDEX>(n), SAMPLEINDEX_INVALID, note, volume);
  1459. } else
  1460. {
  1461. pMainFrm->PlaySoundFile(*m_SongFile, INSTRUMENTINDEX_INVALID, static_cast<SAMPLEINDEX>(n), note, volume);
  1462. }
  1463. }
  1464. } else
  1465. {
  1466. // Preview sample / instrument file
  1467. auto file = InsLibGetFullPath(hItem);
  1468. // If it's a shell link, resolve the link target
  1469. if(auto resolvedName = LinkResolver().Resolve(file.AsNative().c_str()); !resolvedName.empty())
  1470. file = std::move(resolvedName);
  1471. if(pMainFrm)
  1472. pMainFrm->PlaySoundFile(file, note, volume);
  1473. }
  1474. } else
  1475. {
  1476. CMainFrame *pMainFrm = CMainFrame::GetMainFrame();
  1477. if(pMainFrm)
  1478. pMainFrm->StopPreview();
  1479. }
  1480. return TRUE;
  1481. case MODITEM_MIDIPERCUSSION:
  1482. modItemID |= 0x80;
  1483. [[fallthrough]];
  1484. case MODITEM_MIDIINSTRUMENT:
  1485. {
  1486. const MidiLibrary &midiLib = CTrackApp::GetMidiLibrary();
  1487. if(modItemID < midiLib.size() && !midiLib[modItemID].empty())
  1488. {
  1489. CMainFrame *pMainFrm = CMainFrame::GetMainFrame();
  1490. CDLSBank *dlsBank = nullptr;
  1491. if(!mpt::PathString::CompareNoCase(m_cachedBankName, midiLib[modItemID]))
  1492. {
  1493. dlsBank = m_cachedBank.get();
  1494. }
  1495. if(dlsBank == nullptr && CDLSBank::IsDLSBank(midiLib[modItemID]))
  1496. {
  1497. m_cachedBank = std::make_unique<CDLSBank>();
  1498. if(m_cachedBank->Open(midiLib[modItemID]))
  1499. {
  1500. m_cachedBankName = midiLib[modItemID];
  1501. dlsBank = m_cachedBank.get();
  1502. }
  1503. }
  1504. if(dlsBank != nullptr)
  1505. {
  1506. uint32 item = 0;
  1507. if(modItemID < 0x80)
  1508. {
  1509. dlsBank->FindInstrument(false, 0xFFFF, modItemID, note - NOTE_MIN, &item);
  1510. PlayDLSItem(*dlsBank, DlsItem(static_cast<uint16>(item)), note);
  1511. } else
  1512. {
  1513. dlsBank->FindInstrument(true, 0xFFFF, 0xFF, modItemID & 0x7F, &item);
  1514. PlayDLSItem(*dlsBank, DlsItem(static_cast<uint16>(item), static_cast<uint16>(dlsBank->GetRegionFromKey(item, modItemID & 0x7F))), note);
  1515. }
  1516. } else
  1517. {
  1518. pMainFrm->PlaySoundFile(midiLib[modItemID], note, volume);
  1519. }
  1520. }
  1521. }
  1522. return TRUE;
  1523. case MODITEM_DLSBANK_INSTRUMENT:
  1524. {
  1525. const DlsItem &item = static_cast<const DlsItem &>(modItem);
  1526. CDLSBank *dlsBank = GetDLSBankFromItem(hItem);
  1527. if(dlsBank != nullptr)
  1528. {
  1529. PlayDLSItem(*dlsBank, item, note);
  1530. return TRUE;
  1531. }
  1532. }
  1533. }
  1534. }
  1535. return FALSE;
  1536. }
  1537. BOOL CModTree::SetMidiInstrument(UINT nIns, const mpt::PathString &fileName)
  1538. {
  1539. MidiLibrary &midiLib = CTrackApp::GetMidiLibrary();
  1540. if(nIns < 128)
  1541. {
  1542. midiLib[nIns] = fileName;
  1543. RefreshMidiLibrary();
  1544. return TRUE;
  1545. }
  1546. return FALSE;
  1547. }
  1548. BOOL CModTree::SetMidiPercussion(UINT nPerc, const mpt::PathString &fileName)
  1549. {
  1550. MidiLibrary &midiLib = CTrackApp::GetMidiLibrary();
  1551. if(nPerc < 128)
  1552. {
  1553. UINT nIns = nPerc | 0x80;
  1554. midiLib[nIns] = fileName;
  1555. RefreshMidiLibrary();
  1556. return TRUE;
  1557. }
  1558. return FALSE;
  1559. }
  1560. static mpt::ustring TreeDeletionString(const mpt::uchar *type, uint32 id, const mpt::ustring &name)
  1561. {
  1562. mpt::ustring s = MPT_UFORMAT("Remove {} {}")(mpt::ustring(type), id);
  1563. if(!name.empty())
  1564. s += U_(": ") + name;
  1565. s.append(1, UC_('?'));
  1566. return s;
  1567. }
  1568. void CModTree::DeleteTreeItem(HTREEITEM hItem)
  1569. {
  1570. const ModItem modItem = GetModItem(hItem);
  1571. uint32 modItemID = modItem.val1;
  1572. CModDoc *modDoc = m_docInfo.count(m_selectedDoc) ? m_selectedDoc : nullptr;
  1573. CSoundFile *sndFile = modDoc ? &modDoc->GetSoundFile() : nullptr;
  1574. if(modItem.IsSongItem() && modDoc == nullptr)
  1575. {
  1576. return;
  1577. }
  1578. switch(modItem.type)
  1579. {
  1580. case MODITEM_SEQUENCE:
  1581. if(sndFile)
  1582. {
  1583. const SEQUENCEINDEX seq = static_cast<SEQUENCEINDEX>(modItemID);
  1584. if(Reporting::Confirm(TreeDeletionString(UL_("sequence"), seq + 1, sndFile->Order(seq).GetName()), false, true) == cnfNo) break;
  1585. sndFile->Order.RemoveSequence(seq);
  1586. modDoc->UpdateAllViews(nullptr, SequenceHint().Data());
  1587. }
  1588. break;
  1589. case MODITEM_ORDER:
  1590. // might be slightly annoying to ask for confirmation here, and it's rather easy to restore the orderlist anyway.
  1591. if(modDoc && modDoc->RemoveOrder(static_cast<SEQUENCEINDEX>(modItem.val2), static_cast<ORDERINDEX>(modItem.val1)))
  1592. {
  1593. modDoc->UpdateAllViews(nullptr, SequenceHint().Data());
  1594. }
  1595. break;
  1596. case MODITEM_PATTERN:
  1597. if(modDoc && sndFile)
  1598. {
  1599. const PATTERNINDEX pat = static_cast<PATTERNINDEX>(modItemID);
  1600. bool isUsed = false;
  1601. // First, find all used patterns in all sequences.
  1602. for(const auto &sequence : sndFile->Order)
  1603. {
  1604. if(sequence.FindOrder(pat) != ORDERINDEX_INVALID)
  1605. {
  1606. isUsed = true;
  1607. break;
  1608. }
  1609. }
  1610. mpt::ustring s = TreeDeletionString(UL_("pattern"), modItemID, mpt::ToUnicode(sndFile->GetCharsetInternal(), sndFile->Patterns[pat].GetName()));
  1611. s += MPT_UFORMAT("\nThis pattern is currently {}used.")(isUsed ? U_("") : U_("un"));
  1612. if(Reporting::Confirm(s, false, isUsed) == cnfYes && modDoc->RemovePattern(pat))
  1613. {
  1614. modDoc->UpdateAllViews(nullptr, PatternHint(pat).Data().Names());
  1615. if(isUsed)
  1616. modDoc->UpdateAllViews(nullptr, SequenceHint().Data()); // Pattern color will change in sequence
  1617. }
  1618. }
  1619. break;
  1620. case MODITEM_SAMPLE:
  1621. if(modDoc && sndFile)
  1622. {
  1623. if(!sndFile->GetSample(static_cast<SAMPLEINDEX>(modItemID)).HasSampleData()
  1624. || Reporting::Confirm(TreeDeletionString(UL_("sample"), modItemID, mpt::ToUnicode(sndFile->GetCharsetInternal(), sndFile->m_szNames[modItemID])), false, true) == cnfYes)
  1625. {
  1626. const SAMPLEINDEX smp = static_cast<SAMPLEINDEX>(modItemID);
  1627. modDoc->GetSampleUndo().PrepareUndo(smp, sundo_replace, "Delete");
  1628. const SAMPLEINDEX oldNumSamples = modDoc->GetNumSamples();
  1629. if(modDoc->RemoveSample(smp))
  1630. {
  1631. modDoc->UpdateAllViews(nullptr, SampleHint(modDoc->GetNumSamples() != oldNumSamples ? 0 : smp).Info().Data().Names());
  1632. }
  1633. }
  1634. }
  1635. break;
  1636. case MODITEM_INSTRUMENT:
  1637. if(modDoc && sndFile)
  1638. {
  1639. if(sndFile->Instruments[modItemID] == nullptr
  1640. || Reporting::Confirm(TreeDeletionString(UL_("instrument"), modItemID, mpt::ToUnicode(sndFile->GetCharsetInternal(), sndFile->Instruments[modItemID]->name)), false, true) == cnfYes)
  1641. {
  1642. const INSTRUMENTINDEX ins = static_cast<INSTRUMENTINDEX>(modItemID);
  1643. modDoc->GetInstrumentUndo().PrepareUndo(ins, "Delete");
  1644. const INSTRUMENTINDEX oldNumInstrs = modDoc->GetNumInstruments();
  1645. if(modDoc->RemoveInstrument(ins))
  1646. {
  1647. modDoc->UpdateAllViews(nullptr, InstrumentHint(modDoc->GetNumInstruments() != oldNumInstrs ? 0 : ins).Info().Envelope().ModType());
  1648. }
  1649. }
  1650. }
  1651. break;
  1652. case MODITEM_EFFECT:
  1653. if(modDoc && Reporting::Confirm(TreeDeletionString(UL_("plugin FX"), modItemID + 1, sndFile->m_MixPlugins[modItemID].GetName()), false, true) == cnfYes)
  1654. {
  1655. modDoc->RemovePlugin(static_cast<PLUGINDEX>(modItemID));
  1656. }
  1657. break;
  1658. case MODITEM_MIDIINSTRUMENT:
  1659. SetMidiInstrument(modItemID, P_(""));
  1660. RefreshMidiLibrary();
  1661. break;
  1662. case MODITEM_MIDIPERCUSSION:
  1663. SetMidiPercussion(modItemID, P_(""));
  1664. RefreshMidiLibrary();
  1665. break;
  1666. case MODITEM_DLSBANK_FOLDER:
  1667. CTrackApp::RemoveDLSBank(modItemID);
  1668. RefreshDlsBanks();
  1669. break;
  1670. case MODITEM_INSLIB_SONG:
  1671. case MODITEM_INSLIB_SAMPLE:
  1672. case MODITEM_INSLIB_INSTRUMENT:
  1673. {
  1674. // Create double-null-terminated path
  1675. const mpt::winstring fullPath = InsLibGetFullPath(hItem).AsNative() + _T('\0');
  1676. SHFILEOPSTRUCT fos;
  1677. MemsetZero(fos);
  1678. fos.hwnd = m_hWnd;
  1679. fos.wFunc = FO_DELETE;
  1680. fos.pFrom = fullPath.c_str();
  1681. fos.fFlags = CMainFrame::GetInputHandler()->ShiftPressed() ? 0 : FOF_ALLOWUNDO;
  1682. if(!SHFileOperation(&fos) && !fos.fAnyOperationsAborted)
  1683. {
  1684. HTREEITEM newSel = GetNextSiblingItem(hItem);
  1685. if(!newSel) newSel = GetPrevSiblingItem(hItem);
  1686. SelectItem(newSel);
  1687. RefreshInstrumentLibrary();
  1688. SetFocus();
  1689. }
  1690. }
  1691. break;
  1692. }
  1693. }
  1694. BOOL CModTree::OpenTreeItem(HTREEITEM hItem)
  1695. {
  1696. const ModItem modItem = GetModItem(hItem);
  1697. switch(modItem.type)
  1698. {
  1699. case MODITEM_INSLIB_SONG:
  1700. theApp.OpenDocumentFile(InsLibGetFullPath(hItem).ToCString());
  1701. break;
  1702. case MODITEM_HDR_INSTRUMENTLIB:
  1703. CTrackApp::OpenDirectory(m_InstrLibPath);
  1704. break;
  1705. case MODITEM_INSLIB_FOLDER:
  1706. // Open path in Explorer
  1707. CTrackApp::OpenDirectory(InsLibGetFullPath(hItem));
  1708. break;
  1709. }
  1710. return TRUE;
  1711. }
  1712. BOOL CModTree::OpenMidiInstrument(DWORD dwItem)
  1713. {
  1714. std::vector<FileType> mediaFoundationTypes = CSoundFile::GetMediaFoundationFileTypes();
  1715. FileDialog dlg = OpenFileDialog()
  1716. .EnableAudioPreview()
  1717. .ExtensionFilter(
  1718. "All Instruments and Banks (*.xi,*.pat,*.iti,*.sfz,*.dls,*.sf2,...)|*.xi;*.pat;*.iti;*.sfz;*.wav;*.w64;*.caf;*.aif;*.aiff;*.sbk;*.sf2;*.sf3;*.sf4;*.dls;*.mss;*.flac;*.opus;*.ogg;*.oga;*.mp1;*.mp2;*.mp3" + ToFilterOnlyString(mediaFoundationTypes, true).ToLocale() + "|"
  1719. "FastTracker II Instruments (*.xi)|*.xi|"
  1720. "GF1 Patches (*.pat)|*.pat|"
  1721. "Wave Files (*.wav)|*.wav|"
  1722. "Wave64 Files (*.w64)|*.w64|"
  1723. "CAF Files (*.caf)|*.caf|"
  1724. #ifdef MPT_WITH_FLAC
  1725. "FLAC Files (*.flac,*.oga)|*.flac;*.oga|"
  1726. #endif // MPT_WITH_FLAC
  1727. #if defined(MPT_WITH_OPUSFILE)
  1728. "Opus Files (*.opus,*.oga)|*.opus;*.oga|"
  1729. #endif // MPT_WITH_OPUSFILE
  1730. #if defined(MPT_WITH_VORBISFILE) || defined(MPT_WITH_STBVORBIS)
  1731. "Ogg Vorbis Files (*.ogg,*.oga)|*.ogg;*.oga|"
  1732. #endif // VORBIS
  1733. #if defined(MPT_ENABLE_MP3_SAMPLES)
  1734. "MPEG Files (*.mp1,*.mp2,*.mp3)|*.mp1;*.mp2;*.mp3|"
  1735. #endif // MPT_ENABLE_MP3_SAMPLES
  1736. #if defined(MPT_WITH_MEDIAFOUNDATION)
  1737. + ToFilterString(mediaFoundationTypes, FileTypeFormatShowExtensions).ToLocale() +
  1738. #endif
  1739. "Impulse Tracker Instruments (*.iti)|*.iti;*.its|"
  1740. "SFZ Instruments (*.sfz)|*.sfz|"
  1741. "SoundFont 2.0 Banks (*.sf2)|*.sbk;*.sf2;*.sf3;*.sf4|"
  1742. "DLS Sound Banks (*.dls;*.mss)|*.dls;*.mss|"
  1743. "All Files (*.*)|*.*||");
  1744. if(!dlg.Show()) return FALSE;
  1745. if(dwItem & 0x80)
  1746. return SetMidiPercussion(dwItem & 0x7F, dlg.GetFirstFile());
  1747. else
  1748. return SetMidiInstrument(dwItem, dlg.GetFirstFile());
  1749. }
  1750. // Empty Instrument Library
  1751. void CModTree::EmptyInstrumentLibrary()
  1752. {
  1753. HTREEITEM h;
  1754. if(!m_hInsLib)
  1755. return;
  1756. if(!IsSampleBrowser())
  1757. {
  1758. DeleteChildren(m_hInsLib);
  1759. } else
  1760. {
  1761. while((h = GetNextItem(m_hInsLib, TVGN_NEXT)) != NULL)
  1762. {
  1763. DeleteItem(h);
  1764. }
  1765. }
  1766. }
  1767. // Refresh Instrument Library
  1768. void CModTree::FillInstrumentLibrary(const TCHAR *selectedItem)
  1769. {
  1770. if(!m_hInsLib)
  1771. return;
  1772. SetRedraw(FALSE);
  1773. if(!m_SongFileName.empty() && IsSampleBrowser() && m_SongFile)
  1774. {
  1775. // Fill browser with samples / instruments of module file
  1776. SetItemText(m_hInsLib, m_SongFileName.AsNative().c_str());
  1777. SetItemImage(m_hInsLib, IMAGE_FOLDERSONG, IMAGE_FOLDERSONG);
  1778. for(INSTRUMENTINDEX ins = 1; ins <= m_SongFile->GetNumInstruments(); ins++)
  1779. {
  1780. ModInstrument *pIns = m_SongFile->Instruments[ins];
  1781. if(pIns)
  1782. {
  1783. TCHAR s[MAX_INSTRUMENTNAME + 10];
  1784. _sntprintf(s, std::size(s), _T("%3d: %s"), ins, mpt::ToWin(m_SongFile->GetCharsetInternal(), pIns->name).c_str());
  1785. InsertInsLibItem(s, IMAGE_INSTRUMENTS, selectedItem);
  1786. }
  1787. }
  1788. for(SAMPLEINDEX smp = 1; smp <= m_SongFile->GetNumSamples(); smp++)
  1789. {
  1790. const ModSample &sample = m_SongFile->GetSample(smp);
  1791. if(sample.HasSampleData() || sample.uFlags[CHN_ADLIB])
  1792. {
  1793. TCHAR s[MAX_SAMPLENAME + 10];
  1794. _sntprintf(s, std::size(s), _T("%3d: %s"), smp, mpt::ToWin(m_SongFile->GetCharsetInternal(), m_SongFile->m_szNames[smp]).c_str());
  1795. InsertInsLibItem(s, sample.uFlags[CHN_ADLIB] ? IMAGE_OPLINSTR : IMAGE_SAMPLES, selectedItem);
  1796. }
  1797. }
  1798. } else if(!m_InstrLibPath.empty())
  1799. {
  1800. if(!IsSampleBrowser())
  1801. {
  1802. SetItemText(m_hInsLib, _T("Instrument Library (") + m_InstrLibPath.ToCString() + _T(")"));
  1803. } else
  1804. {
  1805. SetItemText(m_hInsLib, m_InstrLibPath.ToCString());
  1806. SetItemImage(m_hInsLib, IMAGE_FOLDER, IMAGE_FOLDER);
  1807. }
  1808. // Enumerating Drives...
  1809. if(!IsSampleBrowser())
  1810. {
  1811. CImageList &images = CMainFrame::GetMainFrame()->m_MiscIcons;
  1812. // Avoid adding the same images again and again...
  1813. images.SetImageCount(IMGLIST_NUMIMAGES);
  1814. TCHAR s[] = _T("?:\\");
  1815. for(int drive = 'A'; drive <= 'Z'; drive++)
  1816. {
  1817. s[0] = static_cast<TCHAR>(drive);
  1818. auto driveType = GetDriveType(s);
  1819. if(driveType != DRIVE_UNKNOWN && driveType != DRIVE_NO_ROOT_DIR)
  1820. {
  1821. if(driveType == DRIVE_REMOVABLE)
  1822. {
  1823. TCHAR sDevice[] = _T("\\\\.\\?:");
  1824. sDevice[4] = s[0];
  1825. auto hDevice = CreateFile(sDevice, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
  1826. if(hDevice != INVALID_HANDLE_VALUE)
  1827. {
  1828. // Check if removable media is inserted
  1829. DWORD bytesReturned = 0;
  1830. auto success = DeviceIoControl(hDevice, IOCTL_STORAGE_CHECK_VERIFY2, nullptr, 0, nullptr, 0, &bytesReturned, nullptr);
  1831. CloseHandle(hDevice);
  1832. if(!success && GetLastError() == ERROR_NOT_READY)
  1833. continue;
  1834. }
  1835. }
  1836. SHFILEINFO fileInfo;
  1837. SHGetFileInfo(s, 0, &fileInfo, sizeof(fileInfo), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES); // SHGFI_USEFILEATTRIBUTES speeds up retrieval for offline network shares
  1838. const int imageIndex = fileInfo.hIcon ? images.Add(fileInfo.hIcon) : IMAGE_FOLDER;
  1839. InsertInsLibItem(s, imageIndex < 0 ? IMAGE_FOLDER : imageIndex, selectedItem);
  1840. DestroyIcon(fileInfo.hIcon);
  1841. }
  1842. }
  1843. }
  1844. // Enumerating Directories and samples/instruments
  1845. const mpt::PathString path = m_InstrLibPath + P_("*.*");
  1846. const bool showDirs = !IsSampleBrowser() || TrackerSettings::Instance().showDirsInSampleBrowser;
  1847. const bool showInstrs = IsSampleBrowser();
  1848. enum
  1849. {
  1850. FILTER_FIRST_VALID = 0,
  1851. FILTER_REJECT_FILE = -1,
  1852. FILTER_UNKNOWN_FILE = -2,
  1853. };
  1854. const auto FilterFile = [this, showInstrs, showDirs](const mpt::PathString &fileName) -> int
  1855. {
  1856. static constexpr auto instrExts = {"xi", "iti", "sfz", "sf2", "sf3", "sf4", "sbk", "dls", "mss", "pat"};
  1857. static constexpr auto sampleExts = {"wav", "flac", "ogg", "opus", "mp1", "mp2", "mp3", "smp", "raw", "s3i", "its", "aif", "aiff", "au", "snd", "svx", "voc", "8sv", "8svx", "16sv", "16svx", "w64", "caf", "sb0", "sb2", "sbi"};
  1858. static constexpr auto allExtsBlacklist = {"txt", "diz", "nfo", "doc", "ini", "pdf", "zip", "rar", "lha", "exe", "dll", "lnk", "url"};
  1859. // Get lower-case file extension without dot.
  1860. mpt::PathString extPS = fileName.GetFileExt();
  1861. std::string ext = extPS.ToUTF8();
  1862. if(!ext.empty())
  1863. {
  1864. ext.erase(0, 1);
  1865. ext = mpt::ToLowerCaseAscii(ext);
  1866. extPS = mpt::PathString::FromUTF8(ext);
  1867. }
  1868. bool knownExtension = true;
  1869. if(mpt::contains(instrExts, ext))
  1870. {
  1871. if(showInstrs)
  1872. return IMAGE_INSTRUMENTS;
  1873. } else if(mpt::contains(sampleExts, ext))
  1874. {
  1875. if(showInstrs)
  1876. return IMAGE_SAMPLES;
  1877. } else if(mpt::contains(m_modExtensions, ext))
  1878. {
  1879. if(showDirs || m_showAllFiles)
  1880. return IMAGE_FOLDERSONG;
  1881. } else if(!extPS.empty() && mpt::contains(m_MediaFoundationExtensions, extPS))
  1882. {
  1883. if(showInstrs)
  1884. return IMAGE_SAMPLES;
  1885. } else
  1886. {
  1887. if(showDirs)
  1888. {
  1889. // Amiga-style prefix (i.e. mod.songname)
  1890. std::string prefixExt = fileName.ToUTF8();
  1891. const auto dotPos = prefixExt.find('.');
  1892. if(dotPos != std::string::npos && mpt::contains(m_modExtensions, prefixExt.erase(dotPos)))
  1893. return IMAGE_FOLDERSONG;
  1894. }
  1895. knownExtension = false;
  1896. }
  1897. if(m_showAllFiles && !mpt::contains(allExtsBlacklist, ext))
  1898. return IMAGE_SAMPLES;
  1899. return knownExtension ? FILTER_REJECT_FILE : FILTER_UNKNOWN_FILE;
  1900. };
  1901. HKEY hkey = nullptr;
  1902. bool showHidden = false;
  1903. if(auto cr = RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced"), 0, KEY_READ, &hkey); cr == ERROR_SUCCESS)
  1904. {
  1905. DWORD dataType = REG_NONE;
  1906. DWORD data = 0, datasize = sizeof(data);
  1907. if(RegQueryValueEx(hkey, _T("Hidden"), 0, &dataType, reinterpret_cast<BYTE *>(&data), &datasize) == ERROR_SUCCESS && dataType == REG_DWORD)
  1908. showHidden = (data == 1);
  1909. RegCloseKey(hkey);
  1910. }
  1911. LinkResolver linkResolver;
  1912. HANDLE hFind;
  1913. WIN32_FIND_DATA wfd;
  1914. MemsetZero(wfd);
  1915. if((hFind = FindFirstFile(path.AsNative().c_str(), &wfd)) != INVALID_HANDLE_VALUE)
  1916. {
  1917. do
  1918. {
  1919. // Up Directory
  1920. int type = FILTER_REJECT_FILE;
  1921. if(!_tcscmp(wfd.cFileName, _T("..")))
  1922. {
  1923. if(showDirs)
  1924. type = IMAGE_FOLDERPARENT;
  1925. } else if(wfd.dwFileAttributes & FILE_ATTRIBUTE_OFFLINE)
  1926. {
  1927. // Ignore unavailable files
  1928. continue;
  1929. } else if((wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
  1930. && ((wfd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) || !showHidden))
  1931. {
  1932. // Only show hidden files if Explorer is configured to do so (and never show hidden system files)
  1933. continue;
  1934. } else if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  1935. {
  1936. // Directory
  1937. if(_tcscmp(wfd.cFileName, _T(".")) && showDirs)
  1938. type = IMAGE_FOLDER;
  1939. } else if(wfd.nFileSizeHigh > 0 || wfd.nFileSizeLow >= 9)
  1940. {
  1941. type = FilterFile(mpt::PathString::FromNative(wfd.cFileName));
  1942. if(type == FILTER_UNKNOWN_FILE)
  1943. {
  1944. // Try resolving file as link if it wasn't a module or instrument
  1945. const auto nativePath = (m_InstrLibPath.AsNative() + wfd.cFileName);
  1946. if(const auto resolvedName = linkResolver.Resolve(nativePath.c_str()); !resolvedName.empty())
  1947. {
  1948. if(resolvedName.IsDirectory() && showDirs)
  1949. type = IMAGE_FOLDER;
  1950. else if(resolvedName.IsFile())
  1951. type = FilterFile(resolvedName);
  1952. }
  1953. }
  1954. }
  1955. if(type >= FILTER_FIRST_VALID)
  1956. {
  1957. auto item = InsertInsLibItem(wfd.cFileName, type, selectedItem);
  1958. // Apparently TVIS_CUT cannot be set during insertion
  1959. if(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
  1960. SetItemState(item, TVIS_CUT, TVIS_CUT);
  1961. }
  1962. } while(FindNextFile(hFind, &wfd));
  1963. FindClose(hFind);
  1964. }
  1965. }
  1966. // Sort items
  1967. TVSORTCB tvs;
  1968. tvs.hParent = (!IsSampleBrowser()) ? m_hInsLib : TVI_ROOT;
  1969. tvs.lpfnCompare = ModTreeInsLibCompareNamesProc;
  1970. tvs.lParam = (LPARAM)this;
  1971. SortChildrenCB(&tvs);
  1972. SetRedraw(TRUE);
  1973. {
  1974. mpt::lock_guard<mpt::mutex> l(m_WatchDirMutex);
  1975. if(m_InstrLibPath != m_WatchDir)
  1976. {
  1977. m_WatchDir = m_InstrLibPath;
  1978. SetEvent(m_hSwitchWatchDir);
  1979. }
  1980. }
  1981. }
  1982. // Monitor changes in the instrument library folder.
  1983. void CModTree::MonitorInstrumentLibrary()
  1984. {
  1985. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST);
  1986. mpt::log::Trace::SetThreadId(mpt::log::Trace::ThreadKindWatchdir, GetCurrentThreadId());
  1987. DWORD result;
  1988. mpt::PathString lastWatchDir;
  1989. HANDLE hWatchDir = INVALID_HANDLE_VALUE;
  1990. DWORD64 lastRefresh = Util::GetTickCount64();
  1991. DWORD timeout = INFINITE;
  1992. DWORD interval = TrackerSettings::Instance().FSUpdateInterval;
  1993. do
  1994. {
  1995. {
  1996. mpt::lock_guard<mpt::mutex> l(m_WatchDirMutex);
  1997. if(m_WatchDir != lastWatchDir)
  1998. {
  1999. if(hWatchDir != INVALID_HANDLE_VALUE)
  2000. {
  2001. FindCloseChangeNotification(hWatchDir);
  2002. hWatchDir = INVALID_HANDLE_VALUE;
  2003. lastWatchDir = mpt::PathString();
  2004. }
  2005. if(!m_WatchDir.empty())
  2006. {
  2007. hWatchDir = FindFirstChangeNotification(m_WatchDir.AsNative().c_str(), FALSE, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME);
  2008. lastWatchDir = m_WatchDir;
  2009. }
  2010. }
  2011. }
  2012. const HANDLE waitHandles[3] = {m_hWatchDirKillThread, m_hSwitchWatchDir, hWatchDir};
  2013. result = WaitForMultipleObjects(hWatchDir != INVALID_HANDLE_VALUE ? 3 : 2, waitHandles, FALSE, timeout);
  2014. DWORD64 now = Util::GetTickCount64();
  2015. if(result == WAIT_TIMEOUT)
  2016. {
  2017. PostMessage(WM_COMMAND, ID_MODTREE_REFRESHINSTRLIB);
  2018. lastRefresh = now;
  2019. timeout = INFINITE;
  2020. } else if(result == WAIT_OBJECT_0 + 1)
  2021. {
  2022. // nothing
  2023. // will switch to new dir in next loop iteration
  2024. } else if(result == WAIT_OBJECT_0 + 2)
  2025. {
  2026. FindNextChangeNotification(hWatchDir);
  2027. timeout = 0; // update timeout later
  2028. }
  2029. if(timeout != INFINITE)
  2030. {
  2031. // Update timeout. Can happen either because we just got a change event,
  2032. // or because we got a directory switch event and still have a change
  2033. // notification pending.
  2034. if(now - lastRefresh >= interval)
  2035. {
  2036. PostMessage(WM_COMMAND, ID_MODTREE_REFRESHINSTRLIB);
  2037. lastRefresh = now;
  2038. timeout = INFINITE;
  2039. } else
  2040. {
  2041. timeout = interval - static_cast<DWORD>(now - lastRefresh);
  2042. }
  2043. }
  2044. } while(result != WAIT_OBJECT_0);
  2045. if(hWatchDir != INVALID_HANDLE_VALUE)
  2046. {
  2047. FindCloseChangeNotification(hWatchDir);
  2048. hWatchDir = INVALID_HANDLE_VALUE;
  2049. lastWatchDir = mpt::PathString();
  2050. }
  2051. }
  2052. // Insert sample browser item.
  2053. HTREEITEM CModTree::InsertInsLibItem(const TCHAR *name, int image, const TCHAR *selectIfMatch)
  2054. {
  2055. HTREEITEM item = InsertItem(TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT,
  2056. name,
  2057. image, image,
  2058. 0, 0,
  2059. 0,
  2060. (!IsSampleBrowser()) ? m_hInsLib : TVI_ROOT,
  2061. TVI_LAST);
  2062. SetItemData(item, reinterpret_cast<DWORD_PTR>(item)); // Used by ModTreeInsLibCompareNamesProc
  2063. if(selectIfMatch != nullptr && !_tcscmp(name, selectIfMatch))
  2064. {
  2065. SelectItem(item);
  2066. EnsureVisible(item);
  2067. }
  2068. return item;
  2069. }
  2070. int CModTree::ModTreeInsLibCompareNamesGetItem(HTREEITEM item, CString &resultStr)
  2071. {
  2072. TVITEM tvi;
  2073. tvi.mask = TVIF_TEXT | TVIF_IMAGE;
  2074. tvi.hItem = item;
  2075. int len = std::max(64, resultStr.GetAllocLength());
  2076. while(true)
  2077. {
  2078. tvi.pszText = resultStr.GetBuffer(len);
  2079. tvi.cchTextMax = len + 1;
  2080. if(!GetItem(&tvi))
  2081. return int16_max;
  2082. if(int resultLen = static_cast<int>(_tcsnlen(tvi.pszText, len)); resultLen < len)
  2083. {
  2084. resultStr.ReleaseBuffer(resultLen);
  2085. // Item image indicates sort order
  2086. switch(tvi.iImage)
  2087. {
  2088. case IMAGE_FOLDERPARENT:
  2089. return 1;
  2090. case IMAGE_FOLDER:
  2091. return 2;
  2092. case IMAGE_FOLDERSONG:
  2093. return 3;
  2094. case IMAGE_SAMPLES:
  2095. case IMAGE_OPLINSTR:
  2096. // Only group instruments and samples separately if we're browsing inside a module file
  2097. if(!m_SongFileName.empty())
  2098. return 5;
  2099. [[fallthrough]];
  2100. case IMAGE_INSTRUMENTS:
  2101. default:
  2102. return 4;
  2103. }
  2104. }
  2105. len *= 2;
  2106. }
  2107. }
  2108. int CALLBACK CModTree::ModTreeInsLibCompareNamesProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
  2109. {
  2110. auto that = reinterpret_cast<CModTree *>(lParamSort);
  2111. const auto itemL = reinterpret_cast<HTREEITEM>(lParam1), itemR = reinterpret_cast<HTREEITEM>(lParam2);
  2112. if(itemL == that->m_hInsLib)
  2113. return -1;
  2114. else if(itemR == that->m_hInsLib)
  2115. return 1;
  2116. const int sortOrderL = that->ModTreeInsLibCompareNamesGetItem(itemL, that->m_compareStrL);
  2117. const int sortOrderR = that->ModTreeInsLibCompareNamesGetItem(itemR, that->m_compareStrR);
  2118. if(sortOrderL != sortOrderR)
  2119. return sortOrderL - sortOrderR;
  2120. return ::CompareString(LOCALE_USER_DEFAULT, that->m_stringCompareFlags,
  2121. that->m_compareStrL.GetBuffer(), -1,
  2122. that->m_compareStrR.GetBuffer(), -1) - 2;
  2123. }
  2124. int CALLBACK CModTree::ModTreeInsLibCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM)
  2125. {
  2126. lParam1 &= 0x7FFFFFFF;
  2127. lParam2 &= 0x7FFFFFFF;
  2128. return static_cast<int>(lParam1 - lParam2);
  2129. }
  2130. int CALLBACK CModTree::ModTreeDrumCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM pDLSBank)
  2131. {
  2132. lParam1 &= 0x7FFFFFFF;
  2133. lParam2 &= 0x7FFFFFFF;
  2134. // Same drum instrument?
  2135. if((lParam1 & 0xFFFF) == (lParam2 & 0xFFFF))
  2136. {
  2137. if(pDLSBank)
  2138. {
  2139. // Compare minimum key of these regions
  2140. const DLSINSTRUMENT *pDlsIns = reinterpret_cast<CDLSBank *>(pDLSBank)->GetInstrument(lParam1 & 0xFFFF);
  2141. uint16 region1 = static_cast<uint16>((lParam1 >> 16) & 0xFFFF);
  2142. uint16 region2 = static_cast<uint16>((lParam2 >> 16) & 0xFFFF);
  2143. if(pDlsIns && region1 < pDlsIns->Regions.size() && region2 < pDlsIns->Regions.size())
  2144. {
  2145. lParam1 = pDlsIns->Regions[region1].uKeyMin;
  2146. lParam2 = pDlsIns->Regions[region2].uKeyMin;
  2147. }
  2148. }
  2149. }
  2150. return static_cast<int>(lParam1 - lParam2);
  2151. }
  2152. void CModTree::SetFullInstrumentLibraryPath(mpt::PathString path)
  2153. {
  2154. if(path.IsDirectory())
  2155. {
  2156. path.EnsureTrailingSlash();
  2157. InstrumentLibraryChDir(path, false);
  2158. } else if(path.IsFile())
  2159. {
  2160. // Browse module contents
  2161. CModTree *dirBrowser = CMainFrame::GetMainFrame()->GetUpperTreeview();
  2162. dirBrowser->m_InstrLibPath = path.GetPath();
  2163. dirBrowser->RefreshInstrumentLibrary();
  2164. dirBrowser->InstrumentLibraryChDir(path.GetFullFileName(), true);
  2165. }
  2166. }
  2167. void CModTree::InstrumentLibraryChDir(mpt::PathString dir, bool isSong)
  2168. {
  2169. if(dir.empty())
  2170. return;
  2171. if(IsSampleBrowser())
  2172. {
  2173. CMainFrame::GetMainFrame()->GetUpperTreeview()->InstrumentLibraryChDir(dir, isSong);
  2174. return;
  2175. }
  2176. BeginWaitCursor();
  2177. bool ok = false;
  2178. const bool goUp = (dir == P_(".."));
  2179. m_previousPath = {};
  2180. if(isSong && !goUp)
  2181. {
  2182. ok = m_pDataTree->InsLibSetFullPath(m_InstrLibPath, dir);
  2183. if(ok)
  2184. {
  2185. m_pDataTree->RefreshInstrumentLibrary();
  2186. m_InstrLibHighlightPath = dir;
  2187. }
  2188. } else
  2189. {
  2190. if(goUp)
  2191. {
  2192. if(isSong)
  2193. {
  2194. // Leave song
  2195. m_InstrLibHighlightPath = std::move(m_pDataTree->m_SongFileName);
  2196. dir = m_InstrLibPath;
  2197. } else
  2198. {
  2199. // Go one dir up.
  2200. mpt::winstring prevDir = m_InstrLibPath.GetPath().AsNative();
  2201. mpt::winstring::size_type pos = prevDir.find_last_of(_T("\\/"), prevDir.length() - 2);
  2202. if(pos != mpt::winstring::npos)
  2203. {
  2204. m_InstrLibHighlightPath = mpt::PathString::FromNative(prevDir.substr(pos + 1, prevDir.length() - pos - 2)); // Highlight previously accessed directory
  2205. prevDir = prevDir.substr(0, pos + 1);
  2206. }
  2207. m_previousPath = m_InstrLibHighlightPath;
  2208. dir = mpt::PathString::FromNative(prevDir);
  2209. }
  2210. } else
  2211. {
  2212. // Drives are formatted like "E:\", folders are just folder name without slash.
  2213. do
  2214. {
  2215. if(!dir.HasTrailingSlash())
  2216. {
  2217. dir = m_InstrLibPath + dir;
  2218. dir.EnsureTrailingSlash();
  2219. }
  2220. m_InstrLibHighlightPath = P_(".."); // Highlight first entry
  2221. FolderScanner scan(dir, FolderScanner::kFilesAndDirectories);
  2222. mpt::PathString name;
  2223. if(scan.Next(name) && !scan.Next(name) && name.IsDirectory())
  2224. {
  2225. // There is only one directory and nothing else in the path,
  2226. // so skip this directory and automatically descend further down into the tree.
  2227. dir = name;
  2228. dir.EnsureTrailingSlash();
  2229. continue;
  2230. }
  2231. } while(false);
  2232. }
  2233. if(dir.IsDirectory())
  2234. {
  2235. m_SongFileName = P_("");
  2236. delete m_SongFile;
  2237. m_SongFile = nullptr;
  2238. m_InstrLibPath = dir;
  2239. GetSampleBrowser()->m_InstrLibHighlightPath = m_InstrLibHighlightPath;
  2240. PostMessage(WM_COMMAND, ID_MODTREE_REFRESHINSTRLIB);
  2241. ok = true;
  2242. }
  2243. }
  2244. EndWaitCursor();
  2245. if(ok)
  2246. {
  2247. mpt::lock_guard<mpt::mutex> l(m_WatchDirMutex);
  2248. m_WatchDir = mpt::PathString();
  2249. } else
  2250. {
  2251. Reporting::Error(MPT_CFORMAT("Unable to browse to \"{}\"")(dir), _T("Instrument Library"));
  2252. }
  2253. }
  2254. bool CModTree::GetDropInfo(DRAGONDROP &dropInfo, mpt::PathString &fullPath)
  2255. {
  2256. const auto dragDoc = m_docInfo.find(m_dragDoc);
  2257. dropInfo.sndFile = dragDoc != m_docInfo.end() ? &dragDoc->second.modDoc.GetSoundFile() : nullptr;
  2258. dropInfo.dropType = DRAGONDROP_NOTHING;
  2259. dropInfo.dropItem = m_itemDrag.val1;
  2260. dropInfo.dropParam = 0;
  2261. switch(m_itemDrag.type)
  2262. {
  2263. case MODITEM_ORDER:
  2264. dropInfo.dropType = DRAGONDROP_ORDER;
  2265. break;
  2266. case MODITEM_PATTERN:
  2267. dropInfo.dropType = DRAGONDROP_PATTERN;
  2268. break;
  2269. case MODITEM_SAMPLE:
  2270. dropInfo.dropType = DRAGONDROP_SAMPLE;
  2271. break;
  2272. case MODITEM_INSTRUMENT:
  2273. dropInfo.dropType = DRAGONDROP_INSTRUMENT;
  2274. break;
  2275. case MODITEM_SEQUENCE:
  2276. case MODITEM_HDR_ORDERS:
  2277. dropInfo.dropType = DRAGONDROP_SEQUENCE;
  2278. break;
  2279. case MODITEM_INSLIB_SAMPLE:
  2280. case MODITEM_INSLIB_INSTRUMENT:
  2281. if(!m_SongFileName.empty())
  2282. {
  2283. const uint32 n = ConvertStrTo<uint32>(GetItemText(m_hItemDrag));
  2284. dropInfo.dropType = (m_itemDrag.type == MODITEM_INSLIB_SAMPLE) ? DRAGONDROP_SAMPLE : DRAGONDROP_INSTRUMENT;
  2285. dropInfo.dropItem = n;
  2286. dropInfo.sndFile = m_SongFile;
  2287. dropInfo.dropParam = 0;
  2288. } else
  2289. {
  2290. fullPath = InsLibGetFullPath(m_hItemDrag);
  2291. dropInfo.dropType = DRAGONDROP_SOUNDFILE;
  2292. dropInfo.dropParam = reinterpret_cast<uintptr_t>(&fullPath);
  2293. }
  2294. break;
  2295. case MODITEM_MIDIPERCUSSION:
  2296. dropInfo.dropItem |= 0x80;
  2297. [[fallthrough]];
  2298. case MODITEM_MIDIINSTRUMENT:
  2299. {
  2300. MidiLibrary &midiLib = CTrackApp::GetMidiLibrary();
  2301. if(!midiLib[dropInfo.dropItem & 0xFF].empty())
  2302. {
  2303. fullPath = midiLib[dropInfo.dropItem & 0xFF];
  2304. dropInfo.dropType = DRAGONDROP_MIDIINSTR;
  2305. dropInfo.dropParam = reinterpret_cast<uintptr_t>(&fullPath);
  2306. }
  2307. }
  2308. break;
  2309. case MODITEM_INSLIB_SONG:
  2310. fullPath = InsLibGetFullPath(m_hItemDrag);
  2311. dropInfo.sndFile = nullptr;
  2312. dropInfo.dropType = DRAGONDROP_SONG;
  2313. dropInfo.dropItem = 0;
  2314. dropInfo.dropParam = reinterpret_cast<uintptr_t>(&fullPath);
  2315. break;
  2316. case MODITEM_DLSBANK_INSTRUMENT:
  2317. {
  2318. dropInfo.dropType = DRAGONDROP_DLS;
  2319. dropInfo.dropItem = static_cast<uint32>(GetDLSBankIndexFromItem(m_hItemDrag)); // bank #
  2320. // Melodic: (Instrument)
  2321. // Drums: (0x80000000) | (Region << 16) | (Instrument)
  2322. dropInfo.dropParam = m_itemDrag.val1;
  2323. }
  2324. break;
  2325. }
  2326. return (dropInfo.dropType != DRAGONDROP_NOTHING);
  2327. }
  2328. bool CModTree::CanDrop(HTREEITEM hItem, bool doDrop)
  2329. {
  2330. const ModItem modItemDrop = GetModItem(hItem);
  2331. const uint32 modItemDropID = modItemDrop.val1;
  2332. const uint32 modItemDragID = m_itemDrag.val1;
  2333. const auto dragIter = m_docInfo.find(m_dragDoc);
  2334. const auto selIter = m_docInfo.find(m_selectedDoc);
  2335. const ModTreeDocInfo *infoDrag = dragIter != m_docInfo.end() ? &dragIter->second : nullptr;
  2336. const ModTreeDocInfo *infoDrop = selIter != m_docInfo.end() ? &selIter->second : nullptr;
  2337. CModDoc *modDoc = infoDrop ? &infoDrop->modDoc : nullptr;
  2338. CSoundFile *sndFile = modDoc ? &modDoc->GetSoundFile() : nullptr;
  2339. const bool sameModDoc = infoDrag && (modDoc == &infoDrag->modDoc);
  2340. const bool sameItem = modItemDrop == m_itemDrag && sameModDoc;
  2341. switch(modItemDrop.type)
  2342. {
  2343. case MODITEM_ORDER:
  2344. case MODITEM_SEQUENCE:
  2345. if(m_itemDrag.type == MODITEM_ORDER && modDoc && sameModDoc)
  2346. {
  2347. // Drop an order somewhere
  2348. if(doDrop)
  2349. {
  2350. SEQUENCEINDEX seqFrom = static_cast<SEQUENCEINDEX>(m_itemDrag.val2), seqTo = static_cast<SEQUENCEINDEX>(modItemDrop.val2);
  2351. ORDERINDEX ordFrom = static_cast<ORDERINDEX>(m_itemDrag.val1), ordTo = static_cast<ORDERINDEX>(modItemDrop.val1);
  2352. if(modItemDrop.type == MODITEM_SEQUENCE)
  2353. {
  2354. // Drop on sequence -> attach
  2355. seqTo = static_cast<SEQUENCEINDEX>(modItemDrop.val1);
  2356. ordTo = sndFile->Order(seqTo).GetLengthTailTrimmed();
  2357. }
  2358. if(seqFrom != seqTo || ordFrom != ordTo)
  2359. {
  2360. if(modDoc->MoveOrder(ordFrom, ordTo, true, false, seqFrom, seqTo) == true)
  2361. {
  2362. modDoc->SetModified();
  2363. }
  2364. }
  2365. }
  2366. return true;
  2367. } else if(modItemDrop.type == MODITEM_SEQUENCE && m_itemDrag.type == MODITEM_SEQUENCE && modDoc && sameModDoc)
  2368. {
  2369. if(doDrop && !sameItem)
  2370. {
  2371. // Rearrange sequences
  2372. const SEQUENCEINDEX from = static_cast<SEQUENCEINDEX>(modItemDragID), to = static_cast<SEQUENCEINDEX>(modItemDropID);
  2373. std::vector<SEQUENCEINDEX> newOrder(sndFile->Order.GetNumSequences());
  2374. std::iota(newOrder.begin(), newOrder.end(), SEQUENCEINDEX(0));
  2375. newOrder.erase(newOrder.begin() + from);
  2376. newOrder.insert(newOrder.begin() + to, from);
  2377. modDoc->ReArrangeSequences(newOrder);
  2378. auto curSeq = sndFile->Order.GetCurrentSequenceIndex();
  2379. if(curSeq == from)
  2380. curSeq = to;
  2381. else if(from > curSeq && to <= curSeq)
  2382. curSeq++;
  2383. else if(from < curSeq && to >= curSeq)
  2384. curSeq--;
  2385. sndFile->Order.SetSequence(curSeq);
  2386. modDoc->UpdateAllViews(nullptr, SequenceHint(SEQUENCEINDEX_INVALID).Names().Data());
  2387. modDoc->SetModified();
  2388. SelectItem(hItem);
  2389. }
  2390. return true;
  2391. }
  2392. break;
  2393. case MODITEM_HDR_ORDERS:
  2394. // Drop your sequences here.
  2395. // At the moment, only dropping sequences into another module is possible and it doesn't copy the patterns themselves.
  2396. if((m_itemDrag.type == MODITEM_SEQUENCE || m_itemDrag.type == MODITEM_HDR_ORDERS) && modDoc && sndFile && infoDrag && !sameModDoc)
  2397. {
  2398. if(doDrop && infoDrag != nullptr)
  2399. {
  2400. // copy mod sequence over.
  2401. CSoundFile &dragSndFile = infoDrag->modDoc.GetSoundFile();
  2402. const SEQUENCEINDEX origSeqId = static_cast<SEQUENCEINDEX>(modItemDragID);
  2403. const ModSequence &origSeq = dragSndFile.Order(origSeqId);
  2404. SEQUENCEINDEX sequenceHint = SEQUENCEINDEX_INVALID;
  2405. if(sndFile->GetModSpecifications().sequencesMax > 1)
  2406. {
  2407. sequenceHint = sndFile->Order.AddSequence();
  2408. } else
  2409. {
  2410. if(Reporting::Confirm(_T("Replace the current orderlist?"), _T("Sequence import")) == cnfNo)
  2411. return false;
  2412. sequenceHint = 0;
  2413. }
  2414. sndFile->Order().resize(std::min(sndFile->GetModSpecifications().ordersMax, origSeq.GetLength()), sndFile->Order.GetInvalidPatIndex());
  2415. for(ORDERINDEX nOrd = 0; nOrd < std::min(sndFile->GetModSpecifications().ordersMax, origSeq.GetLengthTailTrimmed()); nOrd++)
  2416. {
  2417. PATTERNINDEX pat = dragSndFile.Order(origSeqId)[nOrd];
  2418. // translate pattern index
  2419. if(pat == dragSndFile.Order.GetInvalidPatIndex())
  2420. pat = sndFile->Order.GetInvalidPatIndex();
  2421. else if(pat == dragSndFile.Order.GetIgnoreIndex() && sndFile->GetModSpecifications().hasIgnoreIndex)
  2422. pat = sndFile->Order.GetIgnoreIndex();
  2423. else if(pat == dragSndFile.Order.GetIgnoreIndex() && !sndFile->GetModSpecifications().hasIgnoreIndex)
  2424. pat = sndFile->Order.GetInvalidPatIndex();
  2425. else if(pat >= sndFile->GetModSpecifications().patternsMax)
  2426. pat = sndFile->Order.GetInvalidPatIndex();
  2427. sndFile->Order()[nOrd] = pat;
  2428. }
  2429. modDoc->UpdateAllViews(nullptr, SequenceHint(sequenceHint).Data());
  2430. modDoc->SetModified();
  2431. }
  2432. return true;
  2433. }
  2434. break;
  2435. case MODITEM_SAMPLE:
  2436. if(m_itemDrag.type == MODITEM_SAMPLE && modDoc && infoDrag != nullptr)
  2437. {
  2438. if(doDrop)
  2439. {
  2440. if(sameModDoc)
  2441. {
  2442. // Reorder samples in a module
  2443. if(sameItem)
  2444. return true;
  2445. const SAMPLEINDEX from = static_cast<SAMPLEINDEX>(modItemDragID - 1), to = static_cast<SAMPLEINDEX>(modItemDropID - 1);
  2446. std::vector<SAMPLEINDEX> newOrder(modDoc->GetNumSamples());
  2447. std::iota(newOrder.begin(), newOrder.end(), SAMPLEINDEX(1));
  2448. newOrder.erase(newOrder.begin() + from);
  2449. newOrder.insert(newOrder.begin() + to, from + 1);
  2450. modDoc->ReArrangeSamples(newOrder);
  2451. } else
  2452. {
  2453. // Load sample into other module
  2454. sndFile->ReadSampleFromSong(static_cast<SAMPLEINDEX>(modItemDropID), infoDrag->modDoc.GetSoundFile(), static_cast<SAMPLEINDEX>(modItemDragID));
  2455. }
  2456. modDoc->UpdateAllViews(nullptr, SampleHint().Info().Data().Names());
  2457. modDoc->UpdateAllViews(nullptr, PatternHint().Data());
  2458. modDoc->UpdateAllViews(nullptr, InstrumentHint().Info());
  2459. modDoc->SetModified();
  2460. SelectItem(hItem);
  2461. }
  2462. return true;
  2463. }
  2464. break;
  2465. case MODITEM_INSTRUMENT:
  2466. if(m_itemDrag.type == MODITEM_INSTRUMENT && modDoc && infoDrag != nullptr)
  2467. {
  2468. if(doDrop)
  2469. {
  2470. if(sameModDoc)
  2471. {
  2472. // Reorder instruments in a module
  2473. if(sameItem)
  2474. return true;
  2475. const INSTRUMENTINDEX from = static_cast<INSTRUMENTINDEX>(modItemDragID - 1), to = static_cast<INSTRUMENTINDEX>(modItemDropID - 1);
  2476. std::vector<INSTRUMENTINDEX> newOrder(modDoc->GetNumInstruments());
  2477. std::iota(newOrder.begin(), newOrder.end(), INSTRUMENTINDEX(1));
  2478. newOrder.erase(newOrder.begin() + from);
  2479. newOrder.insert(newOrder.begin() + to, from + 1);
  2480. modDoc->ReArrangeInstruments(newOrder);
  2481. } else
  2482. {
  2483. // Load instrument into other module
  2484. sndFile->ReadInstrumentFromSong(static_cast<INSTRUMENTINDEX>(modItemDropID), infoDrag->modDoc.GetSoundFile(), static_cast<INSTRUMENTINDEX>(modItemDragID));
  2485. }
  2486. modDoc->UpdateAllViews(nullptr, InstrumentHint().Info().Envelope().Names());
  2487. modDoc->UpdateAllViews(nullptr, PatternHint().Data());
  2488. modDoc->SetModified();
  2489. SelectItem(hItem);
  2490. }
  2491. return true;
  2492. }
  2493. break;
  2494. case MODITEM_MIDIINSTRUMENT:
  2495. case MODITEM_MIDIPERCUSSION:
  2496. if((m_itemDrag.type == MODITEM_INSLIB_SAMPLE) || (m_itemDrag.type == MODITEM_INSLIB_INSTRUMENT))
  2497. {
  2498. if(doDrop)
  2499. {
  2500. mpt::PathString fullPath = InsLibGetFullPath(m_hItemDrag);
  2501. if(modItemDrop.type == MODITEM_MIDIINSTRUMENT)
  2502. SetMidiInstrument(modItemDropID, fullPath);
  2503. else
  2504. SetMidiPercussion(modItemDropID, fullPath);
  2505. }
  2506. return true;
  2507. }
  2508. break;
  2509. }
  2510. return false;
  2511. }
  2512. void CModTree::UpdatePlayPos(CModDoc &modDoc, Notification *pNotify)
  2513. {
  2514. ModTreeDocInfo *info = GetDocumentInfoFromModDoc(modDoc);
  2515. if(info == nullptr)
  2516. return;
  2517. const CSoundFile &sndFile = modDoc.GetSoundFile();
  2518. ORDERINDEX nNewOrd = (pNotify) ? pNotify->order : ORDERINDEX_INVALID;
  2519. SEQUENCEINDEX nNewSeq = sndFile.Order.GetCurrentSequenceIndex();
  2520. if(nNewOrd != info->ordSel || nNewSeq != info->seqSel)
  2521. {
  2522. // Remove bold state from old item
  2523. if(info->seqSel < info->tiOrders.size() && info->ordSel < info->tiOrders[info->seqSel].size())
  2524. SetItemState(info->tiOrders[info->seqSel][info->ordSel], 0, TVIS_BOLD);
  2525. info->ordSel = nNewOrd;
  2526. info->seqSel = nNewSeq;
  2527. if(info->seqSel < info->tiOrders.size() && info->ordSel < info->tiOrders[info->seqSel].size())
  2528. SetItemState(info->tiOrders[info->seqSel][info->ordSel], TVIS_BOLD, TVIS_BOLD);
  2529. else
  2530. UpdateView(*info, SequenceHint().Data());
  2531. }
  2532. // Update sample / instrument playing status icons (will only detect instruments with samples, though)
  2533. if((TrackerSettings::Instance().m_dwPatternSetup & PATTERN_LIVEUPDATETREE) == 0)
  2534. return;
  2535. // TODO: Is there a way to find out if the treeview is actually visible?
  2536. /*static int nUpdateCount = 0;
  2537. nUpdateCount++;
  2538. if(nUpdateCount < 5) return; // don't update too often
  2539. nUpdateCount = 0;*/
  2540. // check whether the lists are actually visible (don't waste resources)
  2541. const bool updateSamples = IsItemExpanded(info->hSamples), updateInstruments = IsItemExpanded(info->hInstruments);
  2542. info->samplesPlaying.reset();
  2543. info->instrumentsPlaying.reset();
  2544. if(!updateSamples && !updateInstruments)
  2545. return;
  2546. for(const auto &chn : sndFile.m_PlayState.Chn)
  2547. {
  2548. if(chn.pCurrentSample != nullptr && chn.nLength != 0 && chn.IsSamplePlaying())
  2549. {
  2550. if(updateSamples)
  2551. {
  2552. for(SAMPLEINDEX nSmp = sndFile.GetNumSamples(); nSmp >= 1; nSmp--)
  2553. {
  2554. if(chn.pModSample == &sndFile.GetSample(nSmp))
  2555. {
  2556. info->samplesPlaying.set(nSmp);
  2557. break;
  2558. }
  2559. }
  2560. }
  2561. if(updateInstruments)
  2562. {
  2563. for(INSTRUMENTINDEX nIns = sndFile.GetNumInstruments(); nIns >= 1; nIns--)
  2564. {
  2565. if(chn.pModInstrument == sndFile.Instruments[nIns])
  2566. {
  2567. info->instrumentsPlaying.set(nIns);
  2568. break;
  2569. }
  2570. }
  2571. }
  2572. }
  2573. }
  2574. // what should be updated?
  2575. if(updateSamples)
  2576. UpdateView(*info, SampleHint().Info());
  2577. if(updateInstruments)
  2578. UpdateView(*info, InstrumentHint().Info());
  2579. }
  2580. /////////////////////////////////////////////////////////////////////////////
  2581. // CViewModTree message handlers
  2582. void CModTree::OnUpdate(CModDoc *pModDoc, UpdateHint hint, CObject *pHint)
  2583. {
  2584. if(pHint == this)
  2585. return;
  2586. for(auto &[doc, docInfo] : m_docInfo)
  2587. {
  2588. if(doc == pModDoc || !pModDoc)
  2589. {
  2590. UpdateView(docInfo, hint);
  2591. if(pModDoc)
  2592. break;
  2593. }
  2594. }
  2595. }
  2596. void CModTree::OnItemExpanded(LPNMHDR pnmhdr, LRESULT *pResult)
  2597. {
  2598. LPNMTREEVIEW pnm = (LPNMTREEVIEW)pnmhdr;
  2599. if((pnm->itemNew.iImage == IMAGE_FOLDER) || (pnm->itemNew.iImage == IMAGE_OPENFOLDER))
  2600. {
  2601. int iNewImage = (pnm->itemNew.state & TVIS_EXPANDED) ? IMAGE_OPENFOLDER : IMAGE_FOLDER;
  2602. SetItemImage(pnm->itemNew.hItem, iNewImage, iNewImage);
  2603. }
  2604. if(pResult)
  2605. *pResult = TRUE;
  2606. }
  2607. void CModTree::OnBeginDrag(HTREEITEM hItem, bool bLeft, LRESULT *pResult)
  2608. {
  2609. if(!(m_dwStatus & TREESTATUS_DRAGGING))
  2610. {
  2611. bool bDrag = false;
  2612. m_hDropWnd = NULL;
  2613. m_hItemDrag = hItem;
  2614. if(m_hItemDrag != NULL)
  2615. {
  2616. if(!ItemHasChildren(m_hItemDrag))
  2617. SelectItem(m_hItemDrag);
  2618. }
  2619. m_itemDrag = GetModItem(m_hItemDrag);
  2620. m_dragDoc = m_selectedDoc;
  2621. switch(m_itemDrag.type)
  2622. {
  2623. case MODITEM_ORDER:
  2624. case MODITEM_PATTERN:
  2625. case MODITEM_SAMPLE:
  2626. case MODITEM_INSTRUMENT:
  2627. case MODITEM_SEQUENCE:
  2628. case MODITEM_MIDIINSTRUMENT:
  2629. case MODITEM_MIDIPERCUSSION:
  2630. case MODITEM_INSLIB_SAMPLE:
  2631. case MODITEM_INSLIB_INSTRUMENT:
  2632. case MODITEM_INSLIB_SONG:
  2633. bDrag = true;
  2634. break;
  2635. case MODITEM_HDR_ORDERS:
  2636. // can we drag an order header? (only in MPTM format and if there's only one sequence)
  2637. {
  2638. const CModDoc *pModDoc = m_docInfo.count(m_dragDoc) ? m_dragDoc : nullptr;
  2639. if(pModDoc && pModDoc->GetSoundFile().Order.GetNumSequences() == 1)
  2640. bDrag = true;
  2641. }
  2642. break;
  2643. default:
  2644. if(m_itemDrag.type == MODITEM_DLSBANK_INSTRUMENT)
  2645. bDrag = true;
  2646. }
  2647. if(bDrag)
  2648. {
  2649. m_dwStatus |= (bLeft) ? TREESTATUS_LDRAG : TREESTATUS_RDRAG;
  2650. m_hItemDrop = NULL;
  2651. SetCapture();
  2652. }
  2653. }
  2654. if(pResult)
  2655. *pResult = TRUE;
  2656. }
  2657. void CModTree::OnBeginRDrag(LPNMHDR pnmhdr, LRESULT *pResult)
  2658. {
  2659. if(pnmhdr)
  2660. {
  2661. LPNMTREEVIEW pnmtv = (LPNMTREEVIEW)pnmhdr;
  2662. OnBeginDrag(pnmtv->itemNew.hItem, false, pResult);
  2663. }
  2664. }
  2665. void CModTree::OnBeginLDrag(LPNMHDR pnmhdr, LRESULT *pResult)
  2666. {
  2667. if(pnmhdr)
  2668. {
  2669. LPNMTREEVIEW pnmtv = (LPNMTREEVIEW)pnmhdr;
  2670. OnBeginDrag(pnmtv->itemNew.hItem, true, pResult);
  2671. }
  2672. }
  2673. void CModTree::OnItemDblClk(LPNMHDR, LRESULT *pResult)
  2674. {
  2675. POINT pt;
  2676. GetCursorPos(&pt);
  2677. ScreenToClient(&pt);
  2678. HTREEITEM hItem = GetSelectedItem();
  2679. if((hItem) && (hItem == HitTest(pt)))
  2680. {
  2681. ExecuteItem(hItem);
  2682. }
  2683. if(pResult)
  2684. *pResult = 0;
  2685. }
  2686. void CModTree::OnItemReturn(LPNMHDR, LRESULT *pResult)
  2687. {
  2688. HTREEITEM hItem = GetSelectedItem();
  2689. if(hItem)
  2690. ExecuteItem(hItem);
  2691. if(pResult)
  2692. *pResult = 0;
  2693. }
  2694. void CModTree::OnItemRightClick(LPNMHDR, LRESULT *pResult)
  2695. {
  2696. CPoint pt, ptClient;
  2697. UINT flags = 0;
  2698. GetCursorPos(&pt);
  2699. ptClient = pt;
  2700. ScreenToClient(&ptClient);
  2701. OnItemRightClick(HitTest(ptClient, &flags), pt);
  2702. if(pResult)
  2703. *pResult = 0;
  2704. }
  2705. void CModTree::OnItemRightClick(HTREEITEM hItem, CPoint pt)
  2706. {
  2707. HMENU hMenu;
  2708. if(m_dwStatus & TREESTATUS_LDRAG)
  2709. {
  2710. if(ItemHasChildren(hItem))
  2711. {
  2712. Expand(hItem, TVE_TOGGLE);
  2713. } else
  2714. {
  2715. m_hItemDrop = NULL;
  2716. m_hDropWnd = NULL;
  2717. OnEndDrag(TREESTATUS_DRAGGING);
  2718. }
  2719. } else
  2720. {
  2721. if(m_dwStatus & TREESTATUS_DRAGGING)
  2722. {
  2723. m_hItemDrop = NULL;
  2724. m_hDropWnd = NULL;
  2725. OnEndDrag(TREESTATUS_DRAGGING);
  2726. }
  2727. hMenu = ::CreatePopupMenu();
  2728. if(hMenu)
  2729. {
  2730. const CModDoc *modDoc = GetDocumentFromItem(hItem);
  2731. const CSoundFile *sndFile = modDoc != nullptr ? &modDoc->GetSoundFile() : nullptr;
  2732. UINT nDefault = 0;
  2733. BOOL bSep = FALSE;
  2734. const ModItem modItem = GetModItem(hItem);
  2735. const uint32 modItemID = modItem.val1;
  2736. SelectItem(hItem);
  2737. switch(modItem.type)
  2738. {
  2739. case MODITEM_HDR_SONG:
  2740. nDefault = ID_MODTREE_EXECUTE;
  2741. AppendMenu(hMenu, MF_STRING, nDefault, _T("&View"));
  2742. AppendMenu(hMenu, MF_STRING, ID_MODTREE_CLOSE, _T("&Close"));
  2743. AppendMenu(hMenu, MF_STRING, ID_MODTREE_RENAME, _T("Re&name"));
  2744. break;
  2745. case MODITEM_COMMENTS:
  2746. nDefault = ID_MODTREE_EXECUTE;
  2747. AppendMenu(hMenu, MF_STRING, nDefault, _T("&View Comments"));
  2748. break;
  2749. case MODITEM_ORDER:
  2750. case MODITEM_PATTERN:
  2751. nDefault = ID_MODTREE_EXECUTE;
  2752. AppendMenu(hMenu, MF_STRING, nDefault, _T("&Edit Pattern"));
  2753. AppendMenu(hMenu, MF_STRING, ID_MODTREE_REMOVE,
  2754. (modItem.type == MODITEM_ORDER) ? _T("&Delete from list") : _T("&Delete Pattern"));
  2755. if(modItem.type == MODITEM_PATTERN && sndFile && sndFile->GetModSpecifications().hasPatternNames)
  2756. AppendMenu(hMenu, MF_STRING, ID_MODTREE_RENAME, _T("Re&name Pattern"));
  2757. else if(modItem.type == MODITEM_ORDER)
  2758. AppendMenu(hMenu, MF_STRING, ID_MODTREE_RENAME, _T("&Set Pattern"));
  2759. break;
  2760. case MODITEM_SEQUENCE:
  2761. if(sndFile)
  2762. {
  2763. bool isCurSeq = false;
  2764. if(sndFile->GetModSpecifications().sequencesMax > 1)
  2765. {
  2766. if(sndFile->Order((SEQUENCEINDEX)modItemID).GetLength() == 0)
  2767. {
  2768. nDefault = ID_MODTREE_SWITCHTO;
  2769. }
  2770. isCurSeq = (sndFile->Order.GetCurrentSequenceIndex() == (SEQUENCEINDEX)modItemID);
  2771. }
  2772. if(!isCurSeq)
  2773. {
  2774. AppendMenu(hMenu, MF_STRING, ID_MODTREE_SWITCHTO, _T("&Switch to Seqeuence"));
  2775. }
  2776. AppendMenu(hMenu, MF_STRING | (sndFile->Order.GetNumSequences() < MAX_SEQUENCES ? 0 : MF_GRAYED), ID_MODTREE_INSERT, _T("&Insert Sequence"));
  2777. AppendMenu(hMenu, MF_STRING | (sndFile->Order.GetNumSequences() < MAX_SEQUENCES ? 0 : MF_GRAYED), ID_MODTREE_DUPLICATE , _T("D&uplicate Sequence"));
  2778. AppendMenu(hMenu, MF_STRING, ID_MODTREE_REMOVE, _T("&Delete Sequence"));
  2779. AppendMenu(hMenu, MF_STRING, ID_MODTREE_RENAME, _T("Re&name Sequence"));
  2780. }
  2781. break;
  2782. case MODITEM_HDR_ORDERS:
  2783. if(sndFile && sndFile->GetModSpecifications().sequencesMax > 1)
  2784. {
  2785. AppendMenu(hMenu, MF_STRING, ID_MODTREE_INSERT, _T("&Insert Sequence"));
  2786. if(sndFile->Order.GetNumSequences() == 1)
  2787. {
  2788. // This is a sequence
  2789. AppendMenu(hMenu, MF_STRING, ID_MODTREE_DUPLICATE, _T("D&uplicate Sequence"));
  2790. AppendMenu(hMenu, MF_STRING, ID_MODTREE_RENAME, _T("Re&name Sequence"));
  2791. }
  2792. }
  2793. break;
  2794. case MODITEM_SAMPLE:
  2795. {
  2796. nDefault = ID_MODTREE_EXECUTE;
  2797. AppendMenu(hMenu, MF_STRING, nDefault, _T("&View Sample"));
  2798. AppendMenu(hMenu, MF_STRING, ID_MODTREE_PLAY, _T("&Play Sample"));
  2799. AppendMenu(hMenu, MF_STRING, ID_MODTREE_INSERT, _T("&Insert Sample"));
  2800. AppendMenu(hMenu, MF_STRING, ID_MODTREE_DUPLICATE, _T("D&uplicate Sample"));
  2801. AppendMenu(hMenu, MF_STRING, ID_MODTREE_REMOVE, _T("&Delete Sample"));
  2802. AppendMenu(hMenu, MF_STRING, ID_MODTREE_RENAME, _T("Re&name Sample"));
  2803. if ((modDoc) && (!modDoc->GetNumInstruments()))
  2804. {
  2805. AppendMenu(hMenu, MF_SEPARATOR, NULL, _T(""));
  2806. AppendMenu(hMenu, (modDoc->IsSampleMuted((SAMPLEINDEX)modItemID) ? MF_CHECKED : 0) | MF_STRING, ID_MODTREE_MUTE, _T("&Mute Sample"));
  2807. AppendMenu(hMenu, MF_STRING, ID_MODTREE_SOLO, _T("S&olo Sample"));
  2808. AppendMenu(hMenu, MF_STRING, ID_MODTREE_UNMUTEALL, _T("&Unmute all"));
  2809. }
  2810. if(sndFile != nullptr)
  2811. {
  2812. SAMPLEINDEX smpID = static_cast<SAMPLEINDEX>(modItem.val1);
  2813. const ModSample &sample = sndFile->GetSample(smpID);
  2814. const bool hasPath = sndFile->SampleHasPath(smpID);
  2815. const bool menuForThisSample = (sample.HasSampleData() && sndFile->GetType() == MOD_TYPE_MPT) || hasPath;
  2816. bool anyPath = false, anyModified = false, anyMissing = false;
  2817. for(SAMPLEINDEX smp = 1; smp <= sndFile->GetNumSamples(); smp++)
  2818. {
  2819. if(sndFile->SampleHasPath(smp) && smp != smpID)
  2820. {
  2821. anyPath = true;
  2822. if(sndFile->GetSample(smp).HasSampleData() && sndFile->GetSample(smp).uFlags[SMP_MODIFIED])
  2823. {
  2824. anyModified = true;
  2825. }
  2826. }
  2827. if(sndFile->IsExternalSampleMissing(smp))
  2828. {
  2829. anyMissing = true;
  2830. }
  2831. if(anyPath && anyModified && anyMissing) break;
  2832. }
  2833. if(menuForThisSample || anyPath || anyModified)
  2834. {
  2835. AppendMenu(hMenu, MF_SEPARATOR, NULL, _T(""));
  2836. if(menuForThisSample) AppendMenu(hMenu, MF_STRING | ((sndFile->GetType() == MOD_TYPE_MPT || hasPath) ? 0 : MF_GRAYED), ID_MODTREE_SETPATH, _T("Set P&ath"));
  2837. if(menuForThisSample) AppendMenu(hMenu, MF_STRING | ((hasPath && sample.HasSampleData() && sample.uFlags[SMP_MODIFIED]) ? 0 : MF_GRAYED), ID_MODTREE_SAVEITEM, _T("&Save"));
  2838. if(anyModified) AppendMenu(hMenu, MF_STRING, ID_MODTREE_SAVEALL, _T("&Save All"));
  2839. if(menuForThisSample) AppendMenu(hMenu, MF_STRING | (hasPath ? 0 : MF_GRAYED), ID_MODTREE_RELOADITEM, _T("&Reload"));
  2840. if(anyPath) AppendMenu(hMenu, MF_STRING, ID_MODTREE_RELOADALL, _T("&Reload All"));
  2841. if(anyMissing) AppendMenu(hMenu, MF_STRING, ID_MODTREE_FINDMISSING, _T("&Find Missing Samples"));
  2842. }
  2843. }
  2844. }
  2845. break;
  2846. case MODITEM_INSTRUMENT:
  2847. {
  2848. nDefault = ID_MODTREE_EXECUTE;
  2849. AppendMenu(hMenu, MF_STRING, nDefault, _T("&View Instrument"));
  2850. AppendMenu(hMenu, MF_STRING, ID_MODTREE_PLAY, _T("&Play Instrument"));
  2851. AppendMenu(hMenu, MF_STRING, ID_MODTREE_INSERT, _T("&Insert Instrument"));
  2852. AppendMenu(hMenu, MF_STRING, ID_MODTREE_DUPLICATE, _T("D&uplicate Instrument"));
  2853. AppendMenu(hMenu, MF_STRING, ID_MODTREE_REMOVE, _T("&Delete Instrument"));
  2854. AppendMenu(hMenu, MF_STRING, ID_MODTREE_RENAME, _T("Re&name Instrument"));
  2855. if (modDoc)
  2856. {
  2857. AppendMenu(hMenu, MF_SEPARATOR, NULL, _T(""));
  2858. AppendMenu(hMenu, (modDoc->IsInstrumentMuted((INSTRUMENTINDEX)modItemID) ? MF_CHECKED : 0) | MF_STRING, ID_MODTREE_MUTE, _T("&Mute Instrument"));
  2859. AppendMenu(hMenu, MF_STRING, ID_MODTREE_SOLO, _T("S&olo Instrument"));
  2860. AppendMenu(hMenu, MF_STRING, ID_MODTREE_UNMUTEALL, _T("&Unmute all"));
  2861. }
  2862. }
  2863. break;
  2864. case MODITEM_HDR_EFFECTS:
  2865. if(sndFile && sndFile->m_loadedPlugins)
  2866. {
  2867. AppendMenu(hMenu, MF_STRING | (AllPluginsBypassed(*sndFile, false) ? MF_CHECKED : 0), ID_MODTREE_MUTE, _T("B&ypass All Plugins"));
  2868. if(HasEffectPlugins(*sndFile))
  2869. AppendMenu(hMenu, MF_STRING | (AllPluginsBypassed(*sndFile, true) ? MF_CHECKED : 0), ID_MODTREE_MUTE_ONLY_EFFECTS, _T("Bypass All &Effects"));
  2870. }
  2871. break;
  2872. case MODITEM_EFFECT:
  2873. {
  2874. nDefault = ID_MODTREE_EXECUTE;
  2875. AppendMenu(hMenu, MF_STRING, nDefault, _T("&Edit"));
  2876. if(modDoc != nullptr)
  2877. {
  2878. AppendMenu(hMenu, (modDoc->GetSoundFile().m_MixPlugins[modItemID].IsBypassed() ? MF_CHECKED : 0) | MF_STRING, ID_MODTREE_MUTE, _T("&Bypass"));
  2879. AppendMenu(hMenu, MF_STRING, ID_MODTREE_REMOVE, _T("&Delete Plugin"));
  2880. }
  2881. }
  2882. break;
  2883. case MODITEM_MIDIINSTRUMENT:
  2884. case MODITEM_MIDIPERCUSSION:
  2885. nDefault = ID_MODTREE_EXECUTE;
  2886. AppendMenu(hMenu, MF_STRING, nDefault, _T("&Map Instrument"));
  2887. AppendMenu(hMenu, MF_STRING, ID_MODTREE_PLAY, _T("&Play Instrument"));
  2888. AppendMenu(hMenu, MF_STRING, ID_MODTREE_REMOVE, _T("&Unmap Instrument"));
  2889. AppendMenu(hMenu, MF_SEPARATOR, NULL, _T(""));
  2890. [[fallthrough]];
  2891. case MODITEM_HDR_MIDILIB:
  2892. case MODITEM_HDR_MIDIGROUP:
  2893. AppendMenu(hMenu, MF_STRING, ID_IMPORT_MIDILIB, _T("&Import MIDI Library"));
  2894. AppendMenu(hMenu, MF_STRING, ID_EXPORT_MIDILIB, _T("E&xport MIDI Library"));
  2895. bSep = TRUE;
  2896. break;
  2897. case MODITEM_HDR_INSTRUMENTLIB:
  2898. if(!IsSampleBrowser())
  2899. break;
  2900. if(!m_SongFileName.empty())
  2901. AppendMenu(hMenu, MF_STRING, ID_MODTREE_CLOSE, _T("&Close Song"));
  2902. [[fallthrough]];
  2903. case MODITEM_INSLIB_FOLDER:
  2904. nDefault = ID_MODTREE_EXECUTE;
  2905. AppendMenu(hMenu, MF_STRING, nDefault, _T("&Browse..."));
  2906. AppendMenu(hMenu, MF_STRING, ID_MODTREE_RENAME, _T("Set &Path"));
  2907. AppendMenu(hMenu, MF_STRING, ID_MODTREE_OPENITEM, _T("&Open in Explorer"));
  2908. {
  2909. auto insDir = TrackerSettings::Instance().PathInstruments.GetDefaultDir();
  2910. auto smpDir = TrackerSettings::Instance().PathSamples.GetDefaultDir();
  2911. if(!insDir.empty() && insDir != m_InstrLibPath)
  2912. AppendMenu(hMenu, MF_STRING, ID_MODTREE_GOTO_INSDIR, _T("Go to &Instrument directory"));
  2913. if(!smpDir.empty() && smpDir != insDir && smpDir != m_InstrLibPath)
  2914. AppendMenu(hMenu, MF_STRING, ID_MODTREE_GOTO_SMPDIR, _T("Go to Sa&mple directory"));
  2915. }
  2916. break;
  2917. case MODITEM_INSLIB_SONG:
  2918. nDefault = ID_MODTREE_EXECUTE;
  2919. AppendMenu(hMenu, MF_STRING, nDefault, _T("&Browse Song..."));
  2920. AppendMenu(hMenu, MF_STRING, ID_MODTREE_OPENITEM, _T("&Edit Song"));
  2921. AppendMenu(hMenu, MF_SEPARATOR, NULL, _T(""));
  2922. AppendMenu(hMenu, MF_STRING, ID_MODTREE_REMOVE, _T("&Delete"));
  2923. break;
  2924. case MODITEM_INSLIB_SAMPLE:
  2925. case MODITEM_INSLIB_INSTRUMENT:
  2926. nDefault = ID_MODTREE_PLAY;
  2927. if(!m_SongFileName.empty())
  2928. {
  2929. AppendMenu(hMenu, MF_STRING, ID_MODTREE_PLAY, _T("&Play"));
  2930. } else
  2931. {
  2932. AppendMenu(hMenu, MF_STRING, ID_MODTREE_PLAY, _T("&Play File"));
  2933. AppendMenu(hMenu, MF_STRING, ID_MODTREE_REMOVE, _T("&Delete"));
  2934. }
  2935. break;
  2936. case MODITEM_DLSBANK_FOLDER:
  2937. nDefault = ID_SOUNDBANK_PROPERTIES;
  2938. AppendMenu(hMenu, MF_STRING, nDefault, _T("&Properties"));
  2939. AppendMenu(hMenu, MF_STRING, ID_MODTREE_REMOVE, _T("Re&move this bank"));
  2940. [[fallthrough]];
  2941. case MODITEM_NULL:
  2942. AppendMenu(hMenu, MF_STRING, ID_ADD_SOUNDBANK, _T("Add Sound &Bank..."));
  2943. bSep = TRUE;
  2944. break;
  2945. case MODITEM_DLSBANK_INSTRUMENT:
  2946. nDefault = ID_MODTREE_PLAY;
  2947. AppendMenu(hMenu, MF_STRING, ID_MODTREE_PLAY, _T("&Play Instrument"));
  2948. break;
  2949. }
  2950. if (nDefault) SetMenuDefaultItem(hMenu, nDefault, FALSE);
  2951. if ((modItem.type == MODITEM_INSLIB_FOLDER)
  2952. || (modItem.type == MODITEM_INSLIB_SONG)
  2953. || (modItem.type == MODITEM_HDR_INSTRUMENTLIB))
  2954. {
  2955. if ((bSep) || (nDefault)) AppendMenu(hMenu, MF_SEPARATOR, NULL, _T(""));
  2956. AppendMenu(hMenu, TrackerSettings::Instance().showDirsInSampleBrowser ? (MF_STRING|MF_CHECKED) : MF_STRING, ID_MODTREE_SHOWDIRS, _T("Show &Directories in Sample Browser"));
  2957. AppendMenu(hMenu, (m_showAllFiles) ? (MF_STRING|MF_CHECKED) : MF_STRING, ID_MODTREE_SHOWALLFILES, _T("Show &All Files"));
  2958. AppendMenu(hMenu, (m_showAllFiles) ? MF_STRING : (MF_STRING|MF_CHECKED), ID_MODTREE_SOUNDFILESONLY, _T("Show &Sound Files"));
  2959. bSep = TRUE;
  2960. }
  2961. if ((bSep) || (nDefault)) AppendMenu(hMenu, MF_SEPARATOR, NULL, _T(""));
  2962. AppendMenu(hMenu, MF_STRING, ID_MODTREE_REFRESH, _T("&Refresh"));
  2963. TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x + 4, pt.y, 0, m_hWnd, NULL);
  2964. DestroyMenu(hMenu);
  2965. }
  2966. }
  2967. }
  2968. void CModTree::OnItemLeftClick(LPNMHDR, LRESULT *pResult)
  2969. {
  2970. if(!(m_dwStatus & TREESTATUS_RDRAG))
  2971. {
  2972. POINT pt;
  2973. UINT flags = 0;
  2974. GetCursorPos(&pt);
  2975. ScreenToClient(&pt);
  2976. HTREEITEM hItem = HitTest(pt, &flags);
  2977. if(hItem != NULL)
  2978. {
  2979. const ModItem modItem = GetModItem(hItem);
  2980. const uint32 modItemID = modItem.val1;
  2981. switch(modItem.type)
  2982. {
  2983. case MODITEM_INSLIB_FOLDER:
  2984. case MODITEM_INSLIB_SONG:
  2985. if(m_dwStatus & TREESTATUS_SINGLEEXPAND)
  2986. ExecuteItem(hItem);
  2987. break;
  2988. case MODITEM_SAMPLE:
  2989. case MODITEM_INSTRUMENT:
  2990. {
  2991. CMainFrame *pMainFrm = CMainFrame::GetMainFrame();
  2992. CFrameWnd *pFrame = pMainFrm->GetActiveFrame();
  2993. if (pFrame)
  2994. {
  2995. pFrame->SendMessage(WM_MOD_INSTRSELECTED,
  2996. (modItem.type == MODITEM_INSTRUMENT) ? TRUE : FALSE,
  2997. (LPARAM)modItemID);
  2998. }
  2999. }
  3000. break;
  3001. case MODITEM_HDR_SONG:
  3002. ExecuteItem(hItem);
  3003. break;
  3004. }
  3005. }
  3006. }
  3007. if(pResult)
  3008. *pResult = 0;
  3009. }
  3010. void CModTree::OnEndDrag(DWORD dwMask)
  3011. {
  3012. if(m_dwStatus & dwMask)
  3013. {
  3014. m_dwStatus &= ~dwMask;
  3015. if(!(m_dwStatus & TREESTATUS_DRAGGING))
  3016. {
  3017. ReleaseCapture();
  3018. SetCursor(CMainFrame::curArrow);
  3019. SelectDropTarget(nullptr);
  3020. if(m_hItemDrop != nullptr)
  3021. {
  3022. CanDrop(m_hItemDrop, true);
  3023. } else if(m_hDropWnd)
  3024. {
  3025. DRAGONDROP dropinfo;
  3026. mpt::PathString fullPath;
  3027. if(GetDropInfo(dropinfo, fullPath))
  3028. {
  3029. if(dropinfo.dropType == DRAGONDROP_SONG)
  3030. {
  3031. theApp.OpenDocumentFile(fullPath.ToCString());
  3032. } else
  3033. {
  3034. ::SendMessage(m_hDropWnd, WM_MOD_DRAGONDROPPING, TRUE, (LPARAM)&dropinfo);
  3035. }
  3036. }
  3037. }
  3038. }
  3039. }
  3040. }
  3041. void CModTree::OnLButtonUp(UINT nFlags, CPoint point)
  3042. {
  3043. OnEndDrag(TREESTATUS_LDRAG);
  3044. CTreeCtrl::OnLButtonUp(nFlags, point);
  3045. }
  3046. void CModTree::OnRButtonUp(UINT nFlags, CPoint point)
  3047. {
  3048. OnEndDrag(TREESTATUS_RDRAG);
  3049. CTreeCtrl::OnRButtonUp(nFlags, point);
  3050. }
  3051. void CModTree::OnXButtonUp(UINT nFlags, UINT nButton, CPoint point)
  3052. {
  3053. bool isSampleBrowser = IsSampleBrowser();
  3054. if(!isSampleBrowser)
  3055. {
  3056. // In the upper panel, only do folder navigation if the mouse cursor is somewhere below the "Instrument Library" item
  3057. CRect rect;
  3058. GetItemRect(m_hInsLib, rect, FALSE);
  3059. if(point.y > rect.top)
  3060. isSampleBrowser = true;
  3061. }
  3062. if(isSampleBrowser)
  3063. {
  3064. if(nButton == XBUTTON1)
  3065. {
  3066. InstrumentLibraryChDir(P_(".."), !m_SongFileName.empty());
  3067. } else if(nButton == XBUTTON2)
  3068. {
  3069. const auto &previousPath = CMainFrame::GetMainFrame()->GetUpperTreeview()->m_previousPath;
  3070. InstrumentLibraryChDir(previousPath, (m_InstrLibPath + previousPath).IsFile());
  3071. }
  3072. }
  3073. CTreeCtrl::OnXButtonUp(nFlags, nButton, point);
  3074. }
  3075. void CModTree::OnMouseMove(UINT nFlags, CPoint point)
  3076. {
  3077. if(m_dwStatus & TREESTATUS_DRAGGING)
  3078. {
  3079. HTREEITEM hItem;
  3080. UINT flags = 0;
  3081. // Bug?
  3082. if(!(nFlags & (MK_LBUTTON | MK_RBUTTON)))
  3083. {
  3084. m_itemDrag = ModItem(MODITEM_NULL);
  3085. m_hItemDrag = NULL;
  3086. OnEndDrag(TREESTATUS_DRAGGING);
  3087. return;
  3088. }
  3089. CMainFrame *pMainFrm = CMainFrame::GetMainFrame();
  3090. if(pMainFrm)
  3091. {
  3092. CRect rect;
  3093. GetClientRect(&rect);
  3094. if(rect.PtInRect(point))
  3095. {
  3096. m_hDropWnd = m_hWnd;
  3097. bool bCanDrop = CanDrop(HitTest(point, &flags), false);
  3098. SetCursor((bCanDrop) ? CMainFrame::curDragging : CMainFrame::curNoDrop2);
  3099. } else
  3100. {
  3101. CPoint screenPt = point;
  3102. ClientToScreen(&screenPt);
  3103. HWND hwnd = ::WindowFromPoint(screenPt);
  3104. if(hwnd != m_hDropWnd)
  3105. {
  3106. bool canDrop = false;
  3107. m_hDropWnd = hwnd;
  3108. if(hwnd == m_hWnd)
  3109. {
  3110. canDrop = true;
  3111. } else if(hwnd != NULL)
  3112. {
  3113. DRAGONDROP dropinfo;
  3114. mpt::PathString fullPath;
  3115. if(GetDropInfo(dropinfo, fullPath))
  3116. {
  3117. if(dropinfo.dropType == DRAGONDROP_SONG)
  3118. {
  3119. canDrop = true;
  3120. } else if(::SendMessage(hwnd, WM_MOD_DRAGONDROPPING, FALSE, (LPARAM)&dropinfo))
  3121. {
  3122. canDrop = true;
  3123. }
  3124. }
  3125. }
  3126. SetCursor(canDrop ? CMainFrame::curDragging : CMainFrame::curNoDrop);
  3127. if(canDrop)
  3128. {
  3129. if(GetDropHilightItem() != m_hItemDrag)
  3130. {
  3131. SelectDropTarget(m_hItemDrag);
  3132. }
  3133. m_hItemDrop = NULL;
  3134. return;
  3135. }
  3136. }
  3137. }
  3138. if((point.x >= -1) && (point.x <= rect.right + GetSystemMetrics(SM_CXVSCROLL)))
  3139. {
  3140. if(point.y <= 0)
  3141. {
  3142. HTREEITEM hfirst = GetFirstVisibleItem();
  3143. if(hfirst != NULL)
  3144. {
  3145. HTREEITEM hprev = GetPrevVisibleItem(hfirst);
  3146. if(hprev != NULL)
  3147. SelectSetFirstVisible(hprev);
  3148. }
  3149. } else if(point.y >= rect.bottom - 1)
  3150. {
  3151. hItem = HitTest(point, &flags);
  3152. HTREEITEM hNext = GetNextItem(hItem, TVGN_NEXTVISIBLE);
  3153. if(hNext != NULL)
  3154. {
  3155. EnsureVisible(hNext);
  3156. }
  3157. }
  3158. }
  3159. if((hItem = HitTest(point, &flags)) != NULL)
  3160. {
  3161. SelectDropTarget(hItem);
  3162. m_hItemDrop = hItem;
  3163. }
  3164. }
  3165. }
  3166. CTreeCtrl::OnMouseMove(nFlags, point);
  3167. }
  3168. void CModTree::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  3169. {
  3170. switch(nChar)
  3171. {
  3172. case VK_DELETE:
  3173. DeleteTreeItem(GetSelectedItem());
  3174. break;
  3175. }
  3176. CTreeCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
  3177. }
  3178. void CModTree::OnRefreshTree()
  3179. {
  3180. BeginWaitCursor();
  3181. for(auto &doc : m_docInfo)
  3182. {
  3183. UpdateView(doc.second, UpdateHint().ModType());
  3184. }
  3185. RefreshMidiLibrary();
  3186. RefreshDlsBanks();
  3187. RefreshInstrumentLibrary();
  3188. EndWaitCursor();
  3189. }
  3190. void CModTree::OnExecuteItem()
  3191. {
  3192. ExecuteItem(GetSelectedItem());
  3193. }
  3194. void CModTree::OnDeleteTreeItem()
  3195. {
  3196. DeleteTreeItem(GetSelectedItem());
  3197. }
  3198. void CModTree::OnPlayTreeItem()
  3199. {
  3200. PlayItem(GetSelectedItem(), NOTE_MIDDLEC);
  3201. }
  3202. void CModTree::OnOpenTreeItem()
  3203. {
  3204. OpenTreeItem(GetSelectedItem());
  3205. }
  3206. void CModTree::OnMuteTreeItem()
  3207. {
  3208. HTREEITEM hItem = GetSelectedItem();
  3209. const ModItem modItem = GetModItem(hItem);
  3210. const uint32 modItemID = modItem.val1;
  3211. ModTreeDocInfo *info = GetDocumentInfoFromItem(hItem);
  3212. if(info)
  3213. {
  3214. CModDoc &modDoc = info->modDoc;
  3215. if((modItem.type == MODITEM_SAMPLE) && !modDoc.GetNumInstruments())
  3216. {
  3217. modDoc.MuteSample((SAMPLEINDEX)modItemID, !modDoc.IsSampleMuted((SAMPLEINDEX)modItemID));
  3218. UpdateView(*info, SampleHint((SAMPLEINDEX)modItemID).Info().Names());
  3219. } else if((modItem.type == MODITEM_INSTRUMENT) && modDoc.GetNumInstruments())
  3220. {
  3221. modDoc.MuteInstrument((INSTRUMENTINDEX)modItemID, !modDoc.IsInstrumentMuted((INSTRUMENTINDEX)modItemID));
  3222. UpdateView(*info, InstrumentHint((INSTRUMENTINDEX)modItemID).Info().Names());
  3223. } else if(modItem.type == MODITEM_EFFECT)
  3224. {
  3225. IMixPlugin *pPlugin = modDoc.GetSoundFile().m_MixPlugins[modItemID].pMixPlugin;
  3226. if(pPlugin == nullptr)
  3227. return;
  3228. pPlugin->ToggleBypass();
  3229. if(modDoc.GetSoundFile().GetModSpecifications().supportsPlugins)
  3230. modDoc.SetModified();
  3231. //UpdateView(*info, PluginHint(static_cast<PLUGINDEX>(modItemID + 1)));
  3232. } else if(modItem.type == MODITEM_HDR_EFFECTS)
  3233. {
  3234. auto &sndFile = modDoc.GetSoundFile();
  3235. BypassAllPlugins(sndFile, !AllPluginsBypassed(sndFile, false), false);
  3236. }
  3237. }
  3238. }
  3239. void CModTree::OnMuteOnlyEffects()
  3240. {
  3241. HTREEITEM hItem = GetSelectedItem();
  3242. const ModItem modItem = GetModItem(hItem);
  3243. ModTreeDocInfo *info = GetDocumentInfoFromItem(hItem);
  3244. if(info)
  3245. {
  3246. CModDoc &modDoc = info->modDoc;
  3247. if(modItem.type == MODITEM_HDR_EFFECTS)
  3248. {
  3249. auto &sndFile = modDoc.GetSoundFile();
  3250. BypassAllPlugins(sndFile, !AllPluginsBypassed(sndFile, true), true);
  3251. }
  3252. }
  3253. }
  3254. void CModTree::OnSoloTreeItem()
  3255. {
  3256. HTREEITEM hItem = GetSelectedItem();
  3257. const ModItem modItem = GetModItem(hItem);
  3258. const uint32 modItemID = modItem.val1;
  3259. ModTreeDocInfo *info = GetDocumentInfoFromItem(hItem);
  3260. if(info)
  3261. {
  3262. CModDoc &modDoc = info->modDoc;
  3263. INSTRUMENTINDEX nInstruments = modDoc.GetNumInstruments();
  3264. if((modItem.type == MODITEM_SAMPLE) && (!nInstruments))
  3265. {
  3266. for(SAMPLEINDEX nSmp = 1; nSmp <= modDoc.GetNumSamples(); nSmp++)
  3267. {
  3268. modDoc.MuteSample(nSmp, nSmp != modItemID);
  3269. }
  3270. UpdateView(*info, SampleHint().Info().Names());
  3271. } else if((modItem.type == MODITEM_INSTRUMENT) && (nInstruments))
  3272. {
  3273. for(INSTRUMENTINDEX nIns = 1; nIns <= nInstruments; nIns++)
  3274. {
  3275. modDoc.MuteInstrument(nIns, nIns != modItemID);
  3276. }
  3277. UpdateView(*info, InstrumentHint().Info().Names());
  3278. }
  3279. }
  3280. }
  3281. void CModTree::OnUnmuteAllTreeItem()
  3282. {
  3283. HTREEITEM hItem = GetSelectedItem();
  3284. const ModItem modItem = GetModItem(hItem);
  3285. ModTreeDocInfo *info = GetDocumentInfoFromItem(hItem);
  3286. if(info)
  3287. {
  3288. CModDoc &modDoc = info->modDoc;
  3289. if((modItem.type == MODITEM_SAMPLE) || (modItem.type == MODITEM_INSTRUMENT))
  3290. {
  3291. for(SAMPLEINDEX nSmp = 1; nSmp <= modDoc.GetNumSamples(); nSmp++)
  3292. {
  3293. modDoc.MuteSample(nSmp, false);
  3294. }
  3295. UpdateView(*info, SampleHint().Info().Names());
  3296. for(INSTRUMENTINDEX nIns = 1; nIns <= modDoc.GetNumInstruments(); nIns++)
  3297. {
  3298. modDoc.MuteInstrument(nIns, false);
  3299. }
  3300. UpdateView(*info, InstrumentHint().Info().Names());
  3301. }
  3302. }
  3303. }
  3304. bool CModTree::HasEffectPlugins(const CSoundFile &sndFile)
  3305. {
  3306. for(const auto &plugin : sndFile.m_MixPlugins)
  3307. {
  3308. if(!plugin.pMixPlugin)
  3309. continue;
  3310. if(!plugin.pMixPlugin->IsInstrument())
  3311. return true;
  3312. }
  3313. return false;
  3314. }
  3315. bool CModTree::AllPluginsBypassed(const CSoundFile &sndFile, bool onlyEffects)
  3316. {
  3317. for(const auto &plugin : sndFile.m_MixPlugins)
  3318. {
  3319. if(!plugin.pMixPlugin)
  3320. continue;
  3321. if(onlyEffects && plugin.pMixPlugin->IsInstrument())
  3322. continue;
  3323. if(!plugin.IsBypassed())
  3324. return false;
  3325. }
  3326. return true;
  3327. }
  3328. void CModTree::BypassAllPlugins(CSoundFile &sndFile, bool bypass, bool onlyEffects)
  3329. {
  3330. bool modified = false;
  3331. for(auto &plugin : sndFile.m_MixPlugins)
  3332. {
  3333. if(!plugin.pMixPlugin)
  3334. continue;
  3335. if(onlyEffects && plugin.pMixPlugin->IsInstrument())
  3336. continue;
  3337. if(plugin.IsBypassed() != bypass)
  3338. {
  3339. plugin.pMixPlugin->Bypass(bypass);
  3340. modified = true;
  3341. }
  3342. }
  3343. if(modified && sndFile.GetModSpecifications().supportsPlugins && sndFile.GetpModDoc())
  3344. sndFile.GetpModDoc()->SetModified();
  3345. }
  3346. // Helper function for generating an insert vector for samples/instruments/sequences
  3347. template <typename T>
  3348. static std::vector<T> GenerateInsertVector(size_t howMany, size_t insertPos, T insertId, T startId)
  3349. {
  3350. std::vector<T> newOrder(howMany);
  3351. std::iota(newOrder.begin(), newOrder.end(), startId);
  3352. newOrder.insert(newOrder.begin() + insertPos, insertId);
  3353. return newOrder;
  3354. }
  3355. void CModTree::InsertOrDupItem(bool insert)
  3356. {
  3357. HTREEITEM hItem = GetSelectedItem();
  3358. const ModItem modItem = GetModItem(hItem);
  3359. const uint32 modItemID = modItem.val1;
  3360. ModTreeDocInfo *info = GetDocumentInfoFromItem(hItem);
  3361. if(info)
  3362. {
  3363. CModDoc &modDoc = info->modDoc;
  3364. CSoundFile &sndFile = modDoc.GetSoundFile();
  3365. if(modItem.type == MODITEM_SEQUENCE || modItem.type == MODITEM_HDR_ORDERS)
  3366. {
  3367. // Duplicate / insert sequence
  3368. const SEQUENCEINDEX newIndex = (modItem.type == MODITEM_HDR_ORDERS) ? sndFile.Order.GetNumSequences() : static_cast<SEQUENCEINDEX>(modItemID + 1);
  3369. std::vector<SEQUENCEINDEX> newOrder = GenerateInsertVector<SEQUENCEINDEX>(sndFile.Order.GetNumSequences(), newIndex, static_cast<SEQUENCEINDEX>(insert ? SEQUENCEINDEX_INVALID : modItemID), 0);
  3370. if(modDoc.ReArrangeSequences(newOrder) != SEQUENCEINDEX_INVALID)
  3371. {
  3372. sndFile.Order.SetSequence(newIndex);
  3373. if(const auto name = sndFile.Order().GetName(); !insert && !name.empty())
  3374. sndFile.Order().SetName(name + U_(" (Copy)"));
  3375. modDoc.UpdateAllViews(nullptr, SequenceHint(SEQUENCEINDEX_INVALID).Names().Data());
  3376. modDoc.SetModified();
  3377. } else
  3378. {
  3379. Reporting::Error("Maximum number of sequences reached.");
  3380. }
  3381. } else if(modItem.type == MODITEM_SAMPLE)
  3382. {
  3383. // Duplicate / insert sample
  3384. std::vector<SAMPLEINDEX> newOrder = GenerateInsertVector<SAMPLEINDEX>(sndFile.GetNumSamples(), modItemID, static_cast<SAMPLEINDEX>(insert ? 0 : modItemID), 1);
  3385. if(modDoc.ReArrangeSamples(newOrder) != SAMPLEINDEX_INVALID)
  3386. {
  3387. modDoc.SetModified();
  3388. modDoc.UpdateAllViews(nullptr, SampleHint().Info().Data().Names());
  3389. modDoc.UpdateAllViews(nullptr, PatternHint().Data());
  3390. } else
  3391. {
  3392. Reporting::Error("Maximum number of samples reached.");
  3393. }
  3394. } else if(modItem.type == MODITEM_INSTRUMENT)
  3395. {
  3396. // Duplicate / insert instrument
  3397. std::vector<INSTRUMENTINDEX> newOrder = GenerateInsertVector<INSTRUMENTINDEX>(sndFile.GetNumInstruments(), modItemID, static_cast<INSTRUMENTINDEX>(insert ? 0 : modItemID), 1);
  3398. if(modDoc.ReArrangeInstruments(newOrder) != INSTRUMENTINDEX_INVALID)
  3399. {
  3400. modDoc.UpdateAllViews(NULL, InstrumentHint().Info().Envelope().Names());
  3401. modDoc.UpdateAllViews(nullptr, PatternHint().Data());
  3402. modDoc.SetModified();
  3403. } else
  3404. {
  3405. Reporting::Error("Maximum number of instruments reached.");
  3406. }
  3407. }
  3408. }
  3409. }
  3410. void CModTree::OnSwitchToTreeItem()
  3411. {
  3412. HTREEITEM hItem = GetSelectedItem();
  3413. const ModItem modItem = GetModItem(hItem);
  3414. CModDoc *pModDoc = GetDocumentFromItem(hItem);
  3415. if(pModDoc && (modItem.type == MODITEM_SEQUENCE))
  3416. {
  3417. pModDoc->ActivateView(IDD_CONTROL_PATTERNS, uint32(modItem.val1 << SEQU_SHIFT) | SEQU_INDICATOR);
  3418. }
  3419. }
  3420. void CModTree::OnSetItemPath()
  3421. {
  3422. HTREEITEM hItem = GetSelectedItem();
  3423. const ModItem modItem = GetModItem(hItem);
  3424. CModDoc *pModDoc = GetDocumentFromItem(hItem);
  3425. if(pModDoc && modItem.val1)
  3426. {
  3427. SAMPLEINDEX smpID = static_cast<SAMPLEINDEX>(modItem.val1);
  3428. const mpt::PathString path = pModDoc->GetSoundFile().GetSamplePath(smpID);
  3429. FileDialog dlg = OpenFileDialog()
  3430. .ExtensionFilter("All Samples|*.wav;*.flac|All files(*.*)|*.*||"); // Only show samples that we actually can save as well.
  3431. if(path.empty())
  3432. dlg.WorkingDirectory(TrackerSettings::Instance().PathSamples.GetWorkingDir());
  3433. else
  3434. dlg.DefaultFilename(path);
  3435. if(!dlg.Show())
  3436. return;
  3437. TrackerSettings::Instance().PathSamples.SetWorkingDir(dlg.GetWorkingDirectory());
  3438. if(dlg.GetFirstFile() != pModDoc->GetSoundFile().GetSamplePath(smpID))
  3439. {
  3440. pModDoc->GetSoundFile().SetSamplePath(smpID, dlg.GetFirstFile());
  3441. pModDoc->SetModified();
  3442. }
  3443. OnReloadItem();
  3444. }
  3445. }
  3446. void CModTree::OnSaveItem()
  3447. {
  3448. HTREEITEM hItem = GetSelectedItem();
  3449. const ModItem modItem = GetModItem(hItem);
  3450. CModDoc *pModDoc = GetDocumentFromItem(hItem);
  3451. if(pModDoc && modItem.val1)
  3452. {
  3453. SAMPLEINDEX smpID = static_cast<SAMPLEINDEX>(modItem.val1);
  3454. pModDoc->SaveSample(smpID);
  3455. if(pModDoc)
  3456. pModDoc->UpdateAllViews(NULL, SampleHint(smpID).Info());
  3457. OnRefreshTree();
  3458. }
  3459. }
  3460. void CModTree::OnSaveAll()
  3461. {
  3462. CModDoc *pModDoc = GetDocumentFromItem(GetSelectedItem());
  3463. if(pModDoc != nullptr)
  3464. {
  3465. pModDoc->SaveAllSamples(false);
  3466. if(pModDoc)
  3467. pModDoc->UpdateAllViews(nullptr, SampleHint().Info());
  3468. OnRefreshTree();
  3469. }
  3470. }
  3471. void CModTree::OnReloadItem()
  3472. {
  3473. HTREEITEM hItem = GetSelectedItem();
  3474. const ModItem modItem = GetModItem(hItem);
  3475. CModDoc *pModDoc = GetDocumentFromItem(hItem);
  3476. if(pModDoc && modItem.val1)
  3477. {
  3478. SAMPLEINDEX smpID = static_cast<SAMPLEINDEX>(modItem.val1);
  3479. CSoundFile &sndFile = pModDoc->GetSoundFile();
  3480. pModDoc->GetSampleUndo().PrepareUndo(smpID, sundo_replace, "Replace");
  3481. if(!sndFile.LoadExternalSample(smpID, sndFile.GetSamplePath(smpID)))
  3482. {
  3483. pModDoc->GetSampleUndo().RemoveLastUndoStep(smpID);
  3484. Reporting::Error(_T("Unable to load sample:\n") + sndFile.GetSamplePath(smpID).AsNative());
  3485. } else
  3486. {
  3487. if(!sndFile.GetSample(smpID).uFlags[SMP_KEEPONDISK])
  3488. {
  3489. pModDoc->SetModified();
  3490. }
  3491. pModDoc->UpdateAllViews(NULL, SampleHint(smpID).Info().Data().Names());
  3492. }
  3493. OnRefreshTree();
  3494. }
  3495. }
  3496. void CModTree::OnReloadAll()
  3497. {
  3498. CModDoc *pModDoc = GetDocumentFromItem(GetSelectedItem());
  3499. if(pModDoc != nullptr)
  3500. {
  3501. CSoundFile &sndFile = pModDoc->GetSoundFile();
  3502. bool anyMissing = false;
  3503. for(SAMPLEINDEX smp = 1; smp <= sndFile.GetNumSamples(); smp++)
  3504. {
  3505. const mpt::PathString &path = sndFile.GetSamplePath(smp);
  3506. if(path.empty())
  3507. continue;
  3508. pModDoc->GetSampleUndo().PrepareUndo(smp, sundo_replace, "Replace");
  3509. if(!sndFile.LoadExternalSample(smp, path))
  3510. {
  3511. pModDoc->GetSampleUndo().RemoveLastUndoStep(smp);
  3512. anyMissing = true;
  3513. } else
  3514. {
  3515. if(!sndFile.GetSample(smp).uFlags[SMP_KEEPONDISK])
  3516. {
  3517. pModDoc->SetModified();
  3518. }
  3519. }
  3520. }
  3521. pModDoc->UpdateAllViews(nullptr, SampleHint().Info().Data().Names());
  3522. OnRefreshTree();
  3523. if(anyMissing)
  3524. {
  3525. OnFindMissing();
  3526. }
  3527. }
  3528. }
  3529. // Find missing external samples
  3530. void CModTree::OnFindMissing()
  3531. {
  3532. CModDoc *pModDoc = GetDocumentFromItem(GetSelectedItem());
  3533. if(pModDoc == nullptr)
  3534. {
  3535. return;
  3536. }
  3537. MissingExternalSamplesDlg dlg(*pModDoc, CMainFrame::GetMainFrame());
  3538. dlg.DoModal();
  3539. }
  3540. void CModTree::OnAddDlsBank()
  3541. {
  3542. CMainFrame *pMainFrm = CMainFrame::GetMainFrame();
  3543. if(pMainFrm)
  3544. pMainFrm->OnAddDlsBank();
  3545. }
  3546. void CModTree::OnImportMidiLib()
  3547. {
  3548. CMainFrame *pMainFrm = CMainFrame::GetMainFrame();
  3549. if(pMainFrm)
  3550. pMainFrm->OnImportMidiLib();
  3551. }
  3552. void CModTree::OnExportMidiLib()
  3553. {
  3554. FileDialog dlg = SaveFileDialog()
  3555. .DefaultExtension("ini")
  3556. .DefaultFilename("mptrack.ini")
  3557. .ExtensionFilter("Text and INI files (*.txt,*.ini)|*.txt;*.ini|"
  3558. "All Files (*.*)|*.*||");
  3559. if(!dlg.Show()) return;
  3560. CTrackApp::ExportMidiConfig(dlg.GetFirstFile());
  3561. }
  3562. ///////////////////////////////////////////////////////////////////////
  3563. // Drop support
  3564. DROPEFFECT CModTree::OnDragEnter(COleDataObject *, DWORD, CPoint)
  3565. {
  3566. return DROPEFFECT_LINK;
  3567. }
  3568. DROPEFFECT CModTree::OnDragOver(COleDataObject *, DWORD, CPoint point)
  3569. {
  3570. UINT flags = 0;
  3571. HTREEITEM hItem = HitTest(point, &flags);
  3572. const ModItem modItem = GetModItem(hItem);
  3573. switch(modItem.type)
  3574. {
  3575. case MODITEM_MIDIINSTRUMENT:
  3576. case MODITEM_MIDIPERCUSSION:
  3577. if(hItem != GetDropHilightItem())
  3578. {
  3579. SelectDropTarget(hItem);
  3580. EnsureVisible(hItem);
  3581. }
  3582. m_hItemDrag = hItem;
  3583. m_itemDrag = modItem;
  3584. return DROPEFFECT_LINK;
  3585. // Folders:
  3586. case MODITEM_HDR_MIDILIB:
  3587. case MODITEM_HDR_MIDIGROUP:
  3588. EnsureVisible(GetChildItem(hItem));
  3589. break;
  3590. }
  3591. return DROPEFFECT_NONE;
  3592. }
  3593. BOOL CModTree::OnDrop(COleDataObject *pDataObject, DROPEFFECT, CPoint)
  3594. {
  3595. STGMEDIUM stgm;
  3596. HDROP hDropInfo;
  3597. UINT nFiles;
  3598. BOOL bOk = FALSE;
  3599. if(!pDataObject)
  3600. return FALSE;
  3601. if(!pDataObject->GetData(CF_HDROP, &stgm))
  3602. return FALSE;
  3603. if(stgm.tymed != TYMED_HGLOBAL)
  3604. return FALSE;
  3605. if(stgm.hGlobal == NULL)
  3606. return FALSE;
  3607. hDropInfo = (HDROP)stgm.hGlobal;
  3608. nFiles = DragQueryFile(hDropInfo, (UINT)-1, NULL, 0);
  3609. if(nFiles)
  3610. {
  3611. UINT size = ::DragQueryFile(hDropInfo, 0, nullptr, 0) + 1;
  3612. std::vector<TCHAR> fileName(size, _T('\0'));
  3613. if(DragQueryFile(hDropInfo, 0, fileName.data(), size))
  3614. {
  3615. switch(m_itemDrag.type)
  3616. {
  3617. case MODITEM_MIDIINSTRUMENT:
  3618. bOk = SetMidiInstrument(m_itemDrag.val1, mpt::PathString::FromNative(fileName.data()));
  3619. break;
  3620. case MODITEM_MIDIPERCUSSION:
  3621. bOk = SetMidiPercussion(m_itemDrag.val1, mpt::PathString::FromNative(fileName.data()));
  3622. break;
  3623. }
  3624. }
  3625. }
  3626. // After dropping some file on the MIDI library, we will need to do this or you
  3627. // won't be able to select a new item until you started a new (internal) dragondrop.
  3628. if(m_hItemDrag)
  3629. {
  3630. OnBeginDrag(m_hItemDrag, true, nullptr);
  3631. OnEndDrag(TREESTATUS_DRAGGING);
  3632. }
  3633. m_itemDrag = ModItem(MODITEM_NULL);
  3634. m_hItemDrag = NULL;
  3635. ::DragFinish(hDropInfo);
  3636. return bOk;
  3637. }
  3638. void CModTree::OnRefreshInstrLib()
  3639. {
  3640. BeginWaitCursor();
  3641. RefreshInstrumentLibrary();
  3642. EndWaitCursor();
  3643. }
  3644. void CModTree::OnShowDirectories()
  3645. {
  3646. TrackerSettings::Instance().showDirsInSampleBrowser = !TrackerSettings::Instance().showDirsInSampleBrowser;
  3647. OnRefreshInstrLib();
  3648. }
  3649. void CModTree::OnShowAllFiles()
  3650. {
  3651. if(!m_showAllFiles)
  3652. {
  3653. m_showAllFiles = true;
  3654. OnRefreshInstrLib();
  3655. }
  3656. }
  3657. void CModTree::OnShowSoundFiles()
  3658. {
  3659. if(m_showAllFiles)
  3660. {
  3661. m_showAllFiles = false;
  3662. OnRefreshInstrLib();
  3663. }
  3664. }
  3665. void CModTree::OnGotoInstrumentDir()
  3666. {
  3667. SetFullInstrumentLibraryPath(TrackerSettings::Instance().PathInstruments.GetDefaultDir());
  3668. }
  3669. void CModTree::OnGotoSampleDir()
  3670. {
  3671. SetFullInstrumentLibraryPath(TrackerSettings::Instance().PathSamples.GetDefaultDir());
  3672. }
  3673. void CModTree::OnSoundBankProperties()
  3674. {
  3675. const ModItem modItem = GetModItem(GetSelectedItem());
  3676. if(modItem.type == MODITEM_DLSBANK_FOLDER
  3677. && modItem.val1 < CTrackApp::gpDLSBanks.size() && CTrackApp::gpDLSBanks[modItem.val1])
  3678. {
  3679. CSoundBankProperties dlg(*CTrackApp::gpDLSBanks[modItem.val1], this);
  3680. dlg.DoModal();
  3681. }
  3682. }
  3683. LRESULT CModTree::OnCustomKeyMsg(WPARAM wParam, LPARAM /*lParam*/)
  3684. {
  3685. CMainFrame *pMainFrm = CMainFrame::GetMainFrame();
  3686. ModCommand::NOTE note = NOTE_NONE;
  3687. const bool start = wParam >= kcTreeViewStartNotes && wParam <= kcTreeViewEndNotes,
  3688. stop = (wParam >= kcTreeViewStartNoteStops && wParam <= kcTreeViewEndNoteStops) && !IsSampleBrowser();
  3689. if(start || stop)
  3690. {
  3691. const ModItem modItem = GetModItem(GetSelectedItem());
  3692. CModDoc *modDoc = m_docInfo.count(m_selectedDoc) ? m_selectedDoc : nullptr;
  3693. const int noteOffset = static_cast<int>(wParam - (start ? kcTreeViewStartNotes : kcTreeViewStartNoteStops));
  3694. note = static_cast<ModCommand::NOTE>(Clamp(NOTE_MIN + pMainFrm->GetBaseOctave() * 12 + noteOffset, NOTE_MIN, NOTE_MAX));
  3695. if(modDoc && modItem.type == MODITEM_INSTRUMENT)
  3696. note = modDoc->GetNoteWithBaseOctave(noteOffset, static_cast<INSTRUMENTINDEX>(modItem.val1));
  3697. } else if(wParam == kcTreeViewStopPreview)
  3698. {
  3699. note = NOTE_NOTECUT;
  3700. }
  3701. if(note != NOTE_NONE)
  3702. {
  3703. if(stop)
  3704. note |= 0x80;
  3705. if(PlayItem(GetSelectedItem(), note))
  3706. return wParam;
  3707. else
  3708. return kcNull;
  3709. }
  3710. return kcNull;
  3711. }
  3712. LRESULT CModTree::OnMidiMsg(WPARAM midiData_, LPARAM)
  3713. {
  3714. uint32 midiData = static_cast<uint32>(midiData_);
  3715. // Handle MIDI messages assigned to shortcuts
  3716. CInputHandler *ih = CMainFrame::GetInputHandler();
  3717. ih->HandleMIDIMessage(kCtxViewTree, midiData) != kcNull
  3718. || ih->HandleMIDIMessage(kCtxAllContexts, midiData) != kcNull;
  3719. uint8 midiByte1 = MIDIEvents::GetDataByte1FromEvent(midiData);
  3720. int volume;
  3721. switch(MIDIEvents::GetTypeFromEvent(midiData))
  3722. {
  3723. case MIDIEvents::evNoteOn:
  3724. volume = MIDIEvents::GetDataByte2FromEvent(midiData);
  3725. if(volume > 0)
  3726. {
  3727. PlayItem(GetSelectedItem(), midiByte1 + NOTE_MIN, Util::muldivr(volume, 256, 127));
  3728. return 1;
  3729. }
  3730. [[fallthrough]];
  3731. case MIDIEvents::evNoteOff:
  3732. PlayItem(GetSelectedItem(), NOTE_NOTECUT);
  3733. return 1;
  3734. }
  3735. return 0;
  3736. }
  3737. void CModTree::OnKillFocus(CWnd *pNewWnd)
  3738. {
  3739. CTreeCtrl::OnKillFocus(pNewWnd);
  3740. CMainFrame::GetMainFrame()->m_bModTreeHasFocus = false;
  3741. if(pNewWnd != nullptr)
  3742. CMainFrame::GetMainFrame()->SetMidiRecordWnd(pNewWnd->m_hWnd);
  3743. }
  3744. void CModTree::OnSetFocus(CWnd *pOldWnd)
  3745. {
  3746. CTreeCtrl::OnSetFocus(pOldWnd);
  3747. CMainFrame::GetMainFrame()->m_bModTreeHasFocus = true;
  3748. CMainFrame::GetMainFrame()->SetMidiRecordWnd(m_hWnd);
  3749. }
  3750. bool CModTree::IsItemExpanded(HTREEITEM hItem)
  3751. {
  3752. // checks if a treeview item is expanded.
  3753. if(hItem == NULL)
  3754. return false;
  3755. TVITEM tvi;
  3756. tvi.mask = TVIF_HANDLE | TVIF_STATE;
  3757. tvi.state = 0;
  3758. tvi.stateMask = TVIS_EXPANDED;
  3759. tvi.hItem = hItem;
  3760. GetItem(&tvi);
  3761. return (tvi.state & TVIS_EXPANDED) != 0;
  3762. }
  3763. void CModTree::OnCloseItem()
  3764. {
  3765. HTREEITEM hItem = GetSelectedItem();
  3766. if(hItem == m_hInsLib && !m_SongFileName.empty())
  3767. {
  3768. InstrumentLibraryChDir(P_(".."), true);
  3769. return;
  3770. }
  3771. CModDoc *pModDoc = GetDocumentFromItem(hItem);
  3772. if(pModDoc == nullptr)
  3773. return;
  3774. // Spam our message to the first available view
  3775. POSITION pos = pModDoc->GetFirstViewPosition();
  3776. if(pos == nullptr)
  3777. return;
  3778. CView *pView = pModDoc->GetNextView(pos);
  3779. if(pView)
  3780. pView->PostMessage(WM_COMMAND, ID_FILE_CLOSE);
  3781. }
  3782. // Delete all children of a tree item
  3783. void CModTree::DeleteChildren(HTREEITEM hItem)
  3784. {
  3785. if(hItem != nullptr)
  3786. {
  3787. HTREEITEM hChildItem;
  3788. while((hChildItem = GetChildItem(hItem)) != nullptr)
  3789. {
  3790. DeleteItem(hChildItem);
  3791. }
  3792. }
  3793. }
  3794. // Get the n-th child of a tree node
  3795. HTREEITEM CModTree::GetNthChildItem(HTREEITEM hItem, int index) const
  3796. {
  3797. HTREEITEM hChildItem = nullptr;
  3798. if(hItem != nullptr && ItemHasChildren(hItem))
  3799. {
  3800. hChildItem = GetChildItem(hItem);
  3801. while(index-- > 0)
  3802. {
  3803. hChildItem = GetNextSiblingItem(hChildItem);
  3804. }
  3805. }
  3806. return hChildItem;
  3807. }
  3808. // Gets the root parent of an item, i.e. if C is a child of B and B is a child of A, GetParentRootItem(C) returns A.
  3809. // A root item is considered to be its own parent, i.e. the returned value is only ever NULL if the input value was NULL.
  3810. HTREEITEM CModTree::GetParentRootItem(HTREEITEM hItem) const
  3811. {
  3812. while(hItem != nullptr)
  3813. {
  3814. const HTREEITEM h = GetParentItem(hItem);
  3815. if(h == nullptr || h == hItem)
  3816. break;
  3817. hItem = h;
  3818. }
  3819. return hItem;
  3820. }
  3821. void CModTree::OnRenameItem()
  3822. {
  3823. EditLabel(GetSelectedItem());
  3824. }
  3825. // Editing sample, instrument, order, pattern, etc. labels
  3826. void CModTree::OnBeginLabelEdit(NMHDR *nmhdr, LRESULT *result)
  3827. {
  3828. NMTVDISPINFO *info = reinterpret_cast<NMTVDISPINFO *>(nmhdr);
  3829. CEdit *editCtrl = GetEditControl();
  3830. if(editCtrl == nullptr)
  3831. return;
  3832. const ModItem modItem = GetModItem(info->item.hItem);
  3833. const CModDoc *modDoc = modItem.IsSongItem() ? GetDocumentFromItem(info->item.hItem) : nullptr;
  3834. mpt::ustring text;
  3835. m_doLabelEdit = false;
  3836. if(modDoc != nullptr)
  3837. {
  3838. const CSoundFile &sndFile = modDoc->GetSoundFile();
  3839. const CModSpecifications &modSpecs = sndFile.GetModSpecifications();
  3840. switch(modItem.type)
  3841. {
  3842. case MODITEM_HDR_SONG:
  3843. text = mpt::ToUnicode(sndFile.GetCharsetInternal(), sndFile.m_songName);
  3844. editCtrl->SetLimitText(modSpecs.modNameLengthMax);
  3845. m_doLabelEdit = true;
  3846. break;
  3847. case MODITEM_ORDER:
  3848. {
  3849. PATTERNINDEX pat = sndFile.Order(static_cast<SEQUENCEINDEX>(modItem.val2)).at(static_cast<ORDERINDEX>(modItem.val1));
  3850. if(pat == sndFile.Order.GetInvalidPatIndex())
  3851. text = UL_("---");
  3852. else if(pat == sndFile.Order.GetIgnoreIndex())
  3853. text = UL_("+++");
  3854. else
  3855. text = mpt::ufmt::val(pat);
  3856. m_doLabelEdit = true;
  3857. }
  3858. break;
  3859. case MODITEM_HDR_ORDERS:
  3860. if(sndFile.Order.GetNumSequences() != 1 || sndFile.GetModSpecifications().sequencesMax <= 1)
  3861. {
  3862. break;
  3863. }
  3864. [[fallthrough]];
  3865. case MODITEM_SEQUENCE:
  3866. if(modItem.val1 < sndFile.Order.GetNumSequences())
  3867. {
  3868. text = sndFile.Order(static_cast<SEQUENCEINDEX>(modItem.val1)).GetName();
  3869. m_doLabelEdit = true;
  3870. }
  3871. break;
  3872. case MODITEM_PATTERN:
  3873. if(modItem.val1 < sndFile.Patterns.GetNumPatterns() && modSpecs.hasPatternNames)
  3874. {
  3875. text = mpt::ToUnicode(sndFile.GetCharsetInternal(), sndFile.Patterns[modItem.val1].GetName());
  3876. editCtrl->SetLimitText(MAX_PATTERNNAME - 1);
  3877. m_doLabelEdit = true;
  3878. }
  3879. break;
  3880. case MODITEM_SAMPLE:
  3881. if(modItem.val1 <= sndFile.GetNumSamples())
  3882. {
  3883. text = mpt::ToUnicode(sndFile.GetCharsetInternal(), sndFile.m_szNames[modItem.val1]);
  3884. editCtrl->SetLimitText(modSpecs.sampleNameLengthMax);
  3885. m_doLabelEdit = true;
  3886. }
  3887. break;
  3888. case MODITEM_INSTRUMENT:
  3889. if(modItem.val1 <= sndFile.GetNumInstruments() && sndFile.Instruments[modItem.val1] != nullptr)
  3890. {
  3891. text = mpt::ToUnicode(sndFile.GetCharsetInternal(), sndFile.Instruments[modItem.val1]->name);
  3892. editCtrl->SetLimitText(modSpecs.instrNameLengthMax);
  3893. m_doLabelEdit = true;
  3894. }
  3895. break;
  3896. }
  3897. } else if(modItem.type == MODITEM_HDR_INSTRUMENTLIB)
  3898. {
  3899. text = m_InstrLibPath.ToUnicode();
  3900. m_doLabelEdit = true;
  3901. }
  3902. if(m_doLabelEdit)
  3903. {
  3904. CMainFrame::GetInputHandler()->Bypass(true);
  3905. editCtrl->SetWindowText(mpt::ToCString(text));
  3906. }
  3907. *result = m_doLabelEdit ? FALSE : TRUE;
  3908. }
  3909. // End editing sample, instrument, order, pattern, etc. labels
  3910. void CModTree::OnEndLabelEdit(NMHDR *nmhdr, LRESULT *result)
  3911. {
  3912. CMainFrame::GetInputHandler()->Bypass(false);
  3913. m_doLabelEdit = false;
  3914. NMTVDISPINFO *info = reinterpret_cast<NMTVDISPINFO *>(nmhdr);
  3915. const ModItem modItem = GetModItem(info->item.hItem);
  3916. CModDoc *modDoc = modItem.IsSongItem() ? GetDocumentFromItem(info->item.hItem) : nullptr;
  3917. *result = FALSE;
  3918. if(info->item.pszText == nullptr)
  3919. return;
  3920. if(modDoc != nullptr)
  3921. {
  3922. CSoundFile &sndFile = modDoc->GetSoundFile();
  3923. const CModSpecifications &modSpecs = sndFile.GetModSpecifications();
  3924. const mpt::ustring itemText = mpt::ToUnicode(CString(info->item.pszText));
  3925. switch(modItem.type)
  3926. {
  3927. case MODITEM_HDR_SONG:
  3928. if(mpt::ToUnicode(sndFile.GetCharsetInternal(), sndFile.m_songName) != itemText)
  3929. {
  3930. sndFile.m_songName = mpt::truncate(mpt::ToCharset(sndFile.GetCharsetInternal(), itemText), modSpecs.modNameLengthMax);
  3931. modDoc->SetModified();
  3932. modDoc->UpdateAllViews(nullptr, GeneralHint().General());
  3933. }
  3934. break;
  3935. case MODITEM_ORDER:
  3936. if(!itemText.empty())
  3937. {
  3938. PATTERNINDEX pat = ConvertStrTo<PATTERNINDEX>(itemText);
  3939. bool valid = true;
  3940. if(itemText[0] == UC_('-'))
  3941. {
  3942. pat = sndFile.Order.GetInvalidPatIndex();
  3943. } else if(itemText[0] == UC_('+'))
  3944. {
  3945. if(modSpecs.hasIgnoreIndex)
  3946. pat = sndFile.Order.GetIgnoreIndex();
  3947. else
  3948. valid = false;
  3949. } else
  3950. {
  3951. valid = (pat < sndFile.Patterns.GetNumPatterns());
  3952. }
  3953. PATTERNINDEX &target = sndFile.Order(static_cast<SEQUENCEINDEX>(modItem.val2))[static_cast<ORDERINDEX>(modItem.val1)];
  3954. if(valid && pat != target)
  3955. {
  3956. target = pat;
  3957. modDoc->SetModified();
  3958. modDoc->UpdateAllViews(nullptr, SequenceHint().Data());
  3959. }
  3960. } else
  3961. {
  3962. MessageBeep(MB_ICONWARNING);
  3963. }
  3964. break;
  3965. case MODITEM_HDR_ORDERS:
  3966. case MODITEM_SEQUENCE:
  3967. if(modItem.val1 < sndFile.Order.GetNumSequences() && sndFile.Order(static_cast<SEQUENCEINDEX>(modItem.val1)).GetName() != itemText)
  3968. {
  3969. sndFile.Order(static_cast<SEQUENCEINDEX>(modItem.val1)).SetName(itemText);
  3970. modDoc->SetModified();
  3971. modDoc->UpdateAllViews(nullptr, SequenceHint(static_cast<SEQUENCEINDEX>(modItem.val1)).Names());
  3972. }
  3973. break;
  3974. case MODITEM_PATTERN:
  3975. if(modItem.val1 < sndFile.Patterns.GetNumPatterns() && modSpecs.hasPatternNames && mpt::ToUnicode(sndFile.GetCharsetInternal(), sndFile.Patterns[modItem.val1].GetName()) != itemText)
  3976. {
  3977. sndFile.Patterns[modItem.val1].SetName(mpt::ToCharset(sndFile.GetCharsetInternal(), itemText));
  3978. modDoc->SetModified();
  3979. modDoc->UpdateAllViews(nullptr, PatternHint(static_cast<PATTERNINDEX>(modItem.val1)).Data().Names());
  3980. }
  3981. break;
  3982. case MODITEM_SAMPLE:
  3983. if(modItem.val1 <= sndFile.GetNumSamples() && mpt::ToUnicode(sndFile.GetCharsetInternal(), sndFile.m_szNames[modItem.val1]) != itemText)
  3984. {
  3985. sndFile.m_szNames[modItem.val1] = mpt::truncate(mpt::ToCharset(sndFile.GetCharsetInternal(), itemText), modSpecs.sampleNameLengthMax);
  3986. modDoc->SetModified();
  3987. modDoc->UpdateAllViews(nullptr, SampleHint(static_cast<SAMPLEINDEX>(modItem.val1)).Info().Names());
  3988. }
  3989. break;
  3990. case MODITEM_INSTRUMENT:
  3991. if(modItem.val1 <= sndFile.GetNumInstruments() && sndFile.Instruments[modItem.val1] != nullptr && mpt::ToUnicode(sndFile.GetCharsetInternal(), sndFile.Instruments[modItem.val1]->name) != itemText)
  3992. {
  3993. sndFile.Instruments[modItem.val1]->name = mpt::truncate(mpt::ToCharset(sndFile.GetCharsetInternal(), itemText), modSpecs.instrNameLengthMax);
  3994. modDoc->SetModified();
  3995. modDoc->UpdateAllViews(nullptr, InstrumentHint(static_cast<INSTRUMENTINDEX>(modItem.val1)).Info().Names());
  3996. }
  3997. break;
  3998. }
  3999. } else if(modItem.type == MODITEM_HDR_INSTRUMENTLIB)
  4000. {
  4001. SetFullInstrumentLibraryPath(mpt::PathString::FromNative(info->item.pszText));
  4002. }
  4003. }
  4004. // Drop files from Windows
  4005. void CModTree::OnDropFiles(HDROP hDropInfo)
  4006. {
  4007. bool refreshDLS = false;
  4008. const UINT nFiles = ::DragQueryFile(hDropInfo, (UINT)-1, NULL, 0);
  4009. CMainFrame::GetMainFrame()->SetForegroundWindow();
  4010. for(UINT f = 0; f < nFiles; f++)
  4011. {
  4012. UINT size = ::DragQueryFile(hDropInfo, f, nullptr, 0) + 1;
  4013. std::vector<TCHAR> fileName(size, _T('\0'));
  4014. if(::DragQueryFile(hDropInfo, f, fileName.data(), size))
  4015. {
  4016. mpt::PathString file(mpt::PathString::FromNative(fileName.data()));
  4017. if(IsSampleBrowser())
  4018. {
  4019. // Set sample browser location to this directory or file
  4020. SetFullInstrumentLibraryPath(file);
  4021. break;
  4022. } else
  4023. {
  4024. if(CTrackApp::AddDLSBank(file))
  4025. {
  4026. refreshDLS = true;
  4027. } else
  4028. {
  4029. // Pass message on
  4030. theApp.OpenDocumentFile(file.ToCString());
  4031. }
  4032. }
  4033. }
  4034. }
  4035. if(refreshDLS)
  4036. {
  4037. RefreshDlsBanks();
  4038. }
  4039. ::DragFinish(hDropInfo);
  4040. }
  4041. OPENMPT_NAMESPACE_END