-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyDocTemplate.cpp
More file actions
196 lines (160 loc) · 5.22 KB
/
MyDocTemplate.cpp
File metadata and controls
196 lines (160 loc) · 5.22 KB
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
// 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 "MyDocTemplate.h"
#include "DjVuView.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMyDocTemplate
IMPLEMENT_DYNAMIC(CMyDocTemplate, CMultiDocTemplate)
CMyDocTemplate::CMyDocTemplate(UINT nIDResource, CRuntimeClass* pDocClass,
CRuntimeClass* pMDIChildClass, CRuntimeClass* pViewClass)
: CMultiDocTemplate(nIDResource, pDocClass, NULL, pViewClass),
m_pMDIChildClass(pMDIChildClass)
{
ASSERT(pMDIChildClass == NULL ||
pMDIChildClass->IsDerivedFrom(RUNTIME_CLASS(CWnd)));
}
CMyDocTemplate::~CMyDocTemplate()
{
}
// CMyDocTemplate member functions
CDocTemplate::Confidence CMyDocTemplate::MatchDocType(
LPCTSTR lpszPathName, CDocument*& rpDocMatch)
{
// From MFC: CDocTemplate::MatchDocType
ASSERT(lpszPathName != NULL);
rpDocMatch = NULL;
// go through all documents
POSITION pos = GetFirstDocPosition();
while (pos != NULL)
{
CDocument* pDoc = GetNextDoc(pos);
if (AfxComparePath(pDoc->GetPathName(), lpszPathName))
{
// already open
rpDocMatch = pDoc;
return yesAlreadyOpen;
}
}
// see if it matches our default suffix
CString strExtensions;
GetDocString(strExtensions, CDocTemplate::filterExt);
CString strFilterExt;
int nExt = 0;
while (AfxExtractSubString(strFilterExt, strExtensions, nExt++, ';') &&
!strFilterExt.IsEmpty())
{
// see if extension matches
ASSERT(strFilterExt[0] == '.');
LPCTSTR lpszDot = _tcsrchr(lpszPathName, '.');
if (lpszDot != NULL && lstrcmpi(lpszDot, strFilterExt) == 0)
return yesAttemptNative; // extension matches, looks like ours
}
// otherwise we will guess it may work
return yesAttemptForeign;
}
void CMyDocTemplate::UpdateTemplate()
{
if (!m_strDocStrings.LoadString(m_nIDResource) || m_strDocStrings.IsEmpty())
{
TRACE1("Warning: no document names in string for template #%d.\n", m_nIDResource);
}
}
void CMyDocTemplate::InitialUpdateMDIChild(CWnd* pMDIChild, CDocument* pDoc, BOOL bMakeVisible)
{
CMainFrame* pMainFrame = (CMainFrame*) theApp.m_pMainWnd;
// Create a new MDI Frame window
if (theApp.m_bTopLevelDocs && pMainFrame->GetActiveView() != NULL)
pMainFrame = theApp.CreateMainFrame();
ASSERT(pMainFrame != NULL);
pMainFrame->AddMDIChild(pMDIChild, pDoc);
pMDIChild->SendMessage(WM_INITIALUPDATE, 0, 0);
pMDIChild->SendMessageToDescendants(WM_INITIALUPDATE, 0, 0, true, true);
pMainFrame->ActivateDocument(pDoc);
pMainFrame->RedrawWindow(NULL, NULL, RDW_FRAME | RDW_UPDATENOW | RDW_ALLCHILDREN);
}
CWnd* CMyDocTemplate::CreateEmptyMDIChild()
{
return CreateNewMDIChild(NULL);
}
CWnd* CMyDocTemplate::CreateNewMDIChild(CDocument* pDoc)
{
CMainFrame* pMainFrame = (CMainFrame*) theApp.m_pMainWnd;
ASSERT(pMainFrame != NULL);
CCreateContext context;
context.m_pCurrentFrame = NULL;
context.m_pCurrentDoc = pDoc;
context.m_pNewViewClass = m_pViewClass;
context.m_pNewDocTemplate = this;
CWnd* pMDIChild = (CWnd*) m_pMDIChildClass->CreateObject();
if (pMDIChild == NULL)
{
TRACE(_T("Warning: Dynamic create of MDI child %hs failed.\n"),
m_pMDIChildClass->m_lpszClassName);
return NULL;
}
if (!pMDIChild->Create(NULL, NULL, WS_CHILD, CRect(0, 0, 0, 0), pMainFrame, AFX_IDW_PANE_FIRST, &context))
{
TRACE(_T("Warning: could not create MDI child.\n"));
return NULL;
}
return pMDIChild;
}
CDocument* CMyDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName, BOOL bMakeVisible)
{
if (lpszPathName == NULL)
{
TRACE(_T("Creating new documents is disabled.\n"));
return NULL;
}
CWaitCursor wait;
CDocument* pDocument = CreateNewDocument();
if (pDocument == NULL)
{
TRACE(_T("CDocTemplate::CreateNewDocument returned NULL.\n"));
AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
return NULL;
}
ASSERT_VALID(pDocument);
BOOL bAutoDelete = pDocument->m_bAutoDelete;
pDocument->m_bAutoDelete = false; // don't destroy if something goes wrong
CWnd* pMDIChild = CreateNewMDIChild(pDocument);
pDocument->m_bAutoDelete = bAutoDelete;
if (pMDIChild == NULL)
{
AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
delete pDocument; // explicit delete on error
return NULL;
}
ASSERT_VALID(pMDIChild);
// open an existing document
if (!pDocument->OnOpenDocument(lpszPathName))
{
// user has be alerted to what failed in OnOpenDocument
TRACE(_T("CDocument::OnOpenDocument returned FALSE.\n"));
pMDIChild->DestroyWindow();
return NULL;
}
pDocument->SetPathName(lpszPathName);
InitialUpdateMDIChild(pMDIChild, pDocument, bMakeVisible);
return pDocument;
}