-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMyDocManager.cpp
275 lines (228 loc) · 7.29 KB
/
MyDocManager.cpp
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// WinDjView
// Copyright (C) 2004-2015 Andrew Zhezherun
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// http://www.gnu.org/copyleft/gpl.html
#include "stdafx.h"
#include "WinDjView.h"
#include "MyDocManager.h"
#include "MainFrm.h"
#include "DjVuDoc.h"
#include "DjVuView.h"
#include "MyFileDialog.h"
#include "FullscreenWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMyDocManager
IMPLEMENT_DYNAMIC(CMyDocManager, CDocManager)
CMyDocManager::CMyDocManager()
{
}
CMyDocManager::~CMyDocManager()
{
}
void AppendFilterSuffix(CString& filter, OPENFILENAME& ofn,
CDocTemplate* pTemplate, CString* pstrDefaultExt)
{
// From MFC: _AfxAppendFilterSuffix
ASSERT_VALID(pTemplate);
ASSERT_KINDOF(CDocTemplate, pTemplate);
CString strExtensions, strFilterName;
if (pTemplate->GetDocString(strExtensions, CDocTemplate::filterExt)
&& !strExtensions.IsEmpty()
&& pTemplate->GetDocString(strFilterName, CDocTemplate::filterName)
&& !strFilterName.IsEmpty())
{
filter += strFilterName;
ASSERT(!filter.IsEmpty()); // must have a file type name
filter += (TCHAR)'\0'; // next string please
CString strFilterExt;
int nExt = 0;
while (AfxExtractSubString(strFilterExt, strExtensions, nExt++, ';') &&
!strFilterExt.IsEmpty())
{
// see if extension matches
ASSERT(strFilterExt[0] == '.');
// a file based document template - add to filter list
if (pstrDefaultExt != NULL && nExt == 1)
{
// set the default extension
*pstrDefaultExt = ((LPCTSTR)strFilterExt) + 1; // skip the '.'
ofn.lpstrDefExt = (LPTSTR)(LPCTSTR)(*pstrDefaultExt);
ofn.nFilterIndex = ofn.nMaxCustFilter + 1; // 1 based number
}
// add to filter
if (nExt > 1)
filter += (TCHAR)';';
filter += (TCHAR)'*';
filter += strFilterExt;
}
filter += (TCHAR)'\0'; // next string please
ofn.nMaxCustFilter++;
}
}
// CMyDocManager member functions
BOOL CMyDocManager::DoPromptFileName(CString& fileName, UINT nIDSTitle,
DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate)
{
// From MFC: CDocManager::DoPromptFileName
CMyFileDialog dlgFile(!!bOpenFileDialog);
CString title;
VERIFY(title.LoadString(nIDSTitle == AFX_IDS_OPENFILE ? IDS_OPENFILE : nIDSTitle));
dlgFile.m_ofn.Flags |= lFlags;
CString strFilter;
CString strDefault;
if (pTemplate != NULL)
{
ASSERT_VALID(pTemplate);
AppendFilterSuffix(strFilter, dlgFile.m_ofn, pTemplate, &strDefault);
}
else
{
// do for all doc template
POSITION pos = m_templateList.GetHeadPosition();
BOOL bFirst = TRUE;
while (pos != NULL)
{
CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetNext(pos);
AppendFilterSuffix(strFilter, dlgFile.m_ofn, pTemplate,
bFirst ? &strDefault : NULL);
bFirst = FALSE;
}
}
// append the "*.*" all files filter
CString allFilter;
VERIFY(allFilter.LoadString(IDS_ALLFILTER));
strFilter += allFilter;
strFilter += (TCHAR)'\0'; // next string please
strFilter += _T("*.*");
strFilter += (TCHAR)'\0'; // last string
dlgFile.m_ofn.nMaxCustFilter++;
dlgFile.m_ofn.lpstrFilter = strFilter;
dlgFile.m_ofn.lpstrTitle = title;
dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);
INT_PTR nResult = dlgFile.DoModal();
fileName.ReleaseBuffer();
return (nResult == IDOK);
}
CDocument* CMyDocManager::OpenDocumentFile(LPCTSTR lpszFileName)
{
// From MFC: CDocManager::OpenDocumentFile
CString strFileName = lpszFileName;
strFileName.TrimLeft();
strFileName.TrimRight();
if (strFileName[0] == '\"')
strFileName.Delete(0);
int nPos = strFileName.ReverseFind('\"');
if (nPos != -1)
strFileName.Delete(nPos);
CString strQuery, strPage;
nPos = strFileName.Find('?');
if (nPos != -1)
{
strQuery = strFileName.Mid(nPos + 1);
strFileName = strFileName.Left(nPos);
}
bool bPathTooLong = false;
TCHAR szPath[_MAX_PATH];
if (!AfxFullPath(szPath, strFileName))
bPathTooLong = true;
if (bPathTooLong || !PathFileExists(szPath))
{
// Try extracting page number
nPos = strFileName.ReverseFind('#');
if (nPos != -1)
{
strPage = strFileName.Mid(nPos + 1);
strFileName = strFileName.Left(nPos);
if (!AfxFullPath(szPath, strFileName))
bPathTooLong = true;
}
}
if (bPathTooLong)
{
AfxMessageBox(FormatString(IDS_PATH_TOO_LONG, szPath), MB_ICONEXCLAMATION | MB_OK);
return NULL;
}
TCHAR szLinkName[_MAX_PATH];
if (AfxResolveShortcut(GetMainWnd(), szPath, szLinkName, _MAX_PATH))
lstrcpy(szPath, szLinkName);
// find the highest confidence
CDocTemplate::Confidence bestMatch = CDocTemplate::noAttempt;
CDocTemplate* pBestTemplate = NULL;
CDocument* pOpenDocument = NULL;
CMainFrame* pOldMainFrm = (CMainFrame*) theApp.m_pMainWnd;
POSITION pos = m_templateList.GetHeadPosition();
while (pos != NULL)
{
CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetNext(pos);
ASSERT_KINDOF(CDocTemplate, pTemplate);
CDocTemplate::Confidence match;
ASSERT(pOpenDocument == NULL);
match = pTemplate->MatchDocType(szPath, pOpenDocument);
if (match > bestMatch)
{
bestMatch = match;
pBestTemplate = pTemplate;
}
if (match == CDocTemplate::yesAlreadyOpen)
break;
}
if (pOpenDocument != NULL)
{
POSITION pos = pOpenDocument->GetFirstViewPosition();
if (pos != NULL)
{
CView* pView = pOpenDocument->GetNextView(pos);
ASSERT_VALID(pView);
CMainFrame* pMainFrm = (CMainFrame*) pView->GetTopLevelFrame();
pMainFrm->ActivateDocument(pOpenDocument);
}
else
TRACE(_T("Error: Can not find a view for document to activate.\n"));
}
if (pOpenDocument == NULL)
{
if (pBestTemplate == NULL)
{
AfxMessageBox(AFX_IDP_FAILED_TO_OPEN_DOC);
return NULL;
}
pOpenDocument = pBestTemplate->OpenDocumentFile(szPath);
}
if (pOpenDocument != NULL)
{
CDjVuDoc* pDoc = (CDjVuDoc*) pOpenDocument;
CDjVuView* pView = pDoc->GetDjVuView();
CMainFrame* pMainFrm = pView->GetMainFrame();
// CDocManager::OnDDECommand shows the previous main window.
// If it was in the fullscreen mode, hide it back.
if (pOldMainFrm != NULL && pOldMainFrm != pMainFrm && pOldMainFrm->IsFullscreenMode())
pOldMainFrm->ShowWindow(SW_HIDE);
if (!strPage.IsEmpty())
pView->GoToURL(MakeUTF8String(_T("#") + strPage));
}
return pOpenDocument;
}
void CMyDocManager::OnFileOpen()
{
// prompt the user (with all document templates)
CString strPathName;
if (!DoPromptFileName(strPathName, AFX_IDS_OPENFILE,
OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, TRUE, NULL))
return;
theApp.OpenDocumentFile(strPathName);
}