1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/*
* ModDocTemplate.cpp
* ------------------
* Purpose: CDocTemplate and CModDocManager specialization for CModDoc.
* Notes : (currently none)
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "FolderScanner.h"
#include "Mainfrm.h"
#include "Moddoc.h"
#include "ModDocTemplate.h"
#include "Reporting.h"
#include "SelectPluginDialog.h"
#include "../soundlib/plugins/PluginManager.h"
OPENMPT_NAMESPACE_BEGIN
#ifdef MPT_ALL_LOGGING
#define DDEDEBUG
#endif
CDocument *CModDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName, BOOL addToMru, BOOL makeVisible)
{
const mpt::PathString filename = (lpszPathName ? mpt::PathString::FromCString(lpszPathName) : mpt::PathString());
// First, remove document from MRU list.
if(addToMru)
{
theApp.RemoveMruItem(filename);
}
CDocument *pDoc = CMultiDocTemplate::OpenDocumentFile(filename.empty() ? nullptr : filename.ToCString().GetString(), addToMru, makeVisible);
if(pDoc)
{
CMainFrame *pMainFrm = CMainFrame::GetMainFrame();
if (pMainFrm) pMainFrm->OnDocumentCreated(static_cast<CModDoc *>(pDoc));
} else if(!filename.empty() && CMainFrame::GetMainFrame() && addToMru)
{
// Opening the document failed
CMainFrame::GetMainFrame()->UpdateMRUList();
}
return pDoc;
}
CDocument *CModDocTemplate::OpenTemplateFile(const mpt::PathString &filename, bool isExampleTune)
{
CDocument *doc = OpenDocumentFile(filename.ToCString(), isExampleTune ? TRUE : FALSE, TRUE);
if(doc)
{
CModDoc *modDoc = static_cast<CModDoc *>(doc);
// Clear path so that saving will not take place in templates/examples folder.
modDoc->ClearFilePath();
if(!isExampleTune)
{
CMultiDocTemplate::SetDefaultTitle(modDoc);
m_nUntitledCount++;
// Name has changed...
CMainFrame::GetMainFrame()->UpdateTree(modDoc, GeneralHint().General());
// Reset edit history for template files
CSoundFile &sndFile = modDoc->GetSoundFile();
sndFile.GetFileHistory().clear();
sndFile.m_dwCreatedWithVersion = Version::Current();
sndFile.m_dwLastSavedWithVersion = Version();
sndFile.m_modFormat = ModFormatDetails();
sndFile.m_songArtist = TrackerSettings::Instance().defaultArtist;
if(sndFile.GetType() != MOD_TYPE_MPT)
{
// Always enforce most compatible playback for legacy module types
sndFile.m_playBehaviour = sndFile.GetDefaultPlaybackBehaviour(sndFile.GetType());
}
doc->UpdateAllViews(nullptr, UpdateHint().ModType().AsLPARAM());
} else
{
// Remove extension from title, so that saving the file will not suggest a filename like e.g. "example.it.it".
const CString title = modDoc->GetTitle();
const int dotPos = title.ReverseFind(_T('.'));
if(dotPos >= 0)
{
modDoc->SetTitle(title.Left(dotPos));
}
}
}
return doc;
}
void CModDocTemplate::AddDocument(CDocument *doc)
{
CMultiDocTemplate::AddDocument(doc);
m_documents.insert(static_cast<CModDoc *>(doc));
}
void CModDocTemplate::RemoveDocument(CDocument *doc)
{
CMultiDocTemplate::RemoveDocument(doc);
m_documents.erase(static_cast<CModDoc *>(doc));
}
bool CModDocTemplate::DocumentExists(const CModDoc *doc) const
{
return m_documents.count(const_cast<CModDoc *>(doc)) != 0;
}
CDocument *CModDocManager::OpenDocumentFile(LPCTSTR lpszFileName, BOOL bAddToMRU)
{
const mpt::PathString filename = (lpszFileName ? mpt::PathString::FromCString(lpszFileName) : mpt::PathString());
if(filename.IsDirectory())
{
FolderScanner scanner(filename, FolderScanner::kOnlyFiles | FolderScanner::kFindInSubDirectories);
mpt::PathString file;
CDocument *pDoc = nullptr;
while(scanner.Next(file))
{
pDoc = OpenDocumentFile(file.ToCString(), bAddToMRU);
}
return pDoc;
}
if(const auto fileExt = filename.GetFileExt(); !mpt::PathString::CompareNoCase(fileExt, P_(".dll")) || !mpt::PathString::CompareNoCase(fileExt, P_(".vst3")))
{
if(auto plugManager = theApp.GetPluginManager(); plugManager != nullptr)
{
if(auto plugLib = plugManager->AddPlugin(filename, TrackerSettings::Instance().BrokenPluginsWorkaroundVSTMaskAllCrashes); plugLib != nullptr)
{
if(!CSelectPluginDlg::VerifyPlugin(plugLib, nullptr))
{
plugManager->RemovePlugin(plugLib);
}
return nullptr;
}
}
}
CDocument *pDoc = CDocManager::OpenDocumentFile(lpszFileName, bAddToMRU);
if(pDoc == nullptr && !filename.empty())
{
if(!filename.IsFile())
{
Reporting::Error(MPT_CFORMAT("Unable to open \"{}\": file does not exist.")(filename.ToCString()));
theApp.RemoveMruItem(filename);
CMainFrame::GetMainFrame()->UpdateMRUList();
} else
{
// Case: Valid path but opening failed.
const int numDocs = theApp.GetOpenDocumentCount();
Reporting::Notification(MPT_CFORMAT("Opening \"{}\" failed. This can happen if "
"no more modules can be opened or if the file type was not "
"recognised (currently there {} {} document{} open).")(
filename.ToCString(), (numDocs == 1) ? CString(_T("is")) : CString(_T("are")), numDocs, (numDocs == 1) ? CString(_T("")) : CString(_T("s"))));
}
}
return pDoc;
}
BOOL CModDocManager::OnDDECommand(LPTSTR lpszCommand)
{
BOOL bResult, bActivate;
#ifdef DDEDEBUG
MPT_LOG_GLOBAL(LogDebug, "DDE", U_("OnDDECommand: ") + mpt::ToUnicode(mpt::winstring(lpszCommand)));
#endif
// Handle any DDE commands recognized by your application
// and return TRUE. See implementation of CWinApp::OnDDEComand
// for example of parsing the DDE command string.
bResult = FALSE;
bActivate = FALSE;
if ((lpszCommand) && lpszCommand[0] && (theApp.m_pMainWnd))
{
std::size_t len = _tcslen(lpszCommand);
std::vector<TCHAR> s(lpszCommand, lpszCommand + len + 1);
len--;
while((len > 0) && _tcschr(_T("(){}[]\'\" "), s[len]))
{
s[len--] = 0;
}
TCHAR *pszCmd = s.data();
while (pszCmd[0] == _T('[')) pszCmd++;
TCHAR *pszData = pszCmd;
while ((pszData[0] != _T('(')) && (pszData[0]))
{
if (((BYTE)pszData[0]) <= (BYTE)' ') *pszData = 0;
pszData++;
}
while ((*pszData) && (_tcschr(_T("(){}[]\'\" "), *pszData)))
{
*pszData = 0;
pszData++;
}
// Edit/Open
if ((!lstrcmpi(pszCmd, _T("Edit")))
|| (!lstrcmpi(pszCmd, _T("Open"))))
{
if (pszData[0])
{
bResult = TRUE;
bActivate = TRUE;
OpenDocumentFile(pszData);
}
} else
// New
if (!lstrcmpi(pszCmd, _T("New")))
{
OpenDocumentFile(_T(""));
bResult = TRUE;
bActivate = TRUE;
}
#ifdef DDEDEBUG
MPT_LOG_GLOBAL(LogDebug, "DDE", MPT_UFORMAT("{}({})")(mpt::winstring(pszCmd), mpt::winstring(pszData)));
#endif
if ((bActivate) && (theApp.m_pMainWnd->m_hWnd))
{
if (theApp.m_pMainWnd->IsIconic()) theApp.m_pMainWnd->ShowWindow(SW_RESTORE);
theApp.m_pMainWnd->SetActiveWindow();
}
}
// Return FALSE for any DDE commands you do not handle.
#ifdef DDEDEBUG
if (!bResult)
{
MPT_LOG_GLOBAL(LogDebug, "DDE", U_("WARNING: failure in CModDocManager::OnDDECommand()"));
}
#endif
return bResult;
}
OPENMPT_NAMESPACE_END
|