Debian Codegraph @

editor/editorDoc.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/editor/editorDoc.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003-08-26 03:57:37 $
23 * $Author: kevinb $
24 *
25 * Editor Document (Level/Mission)
26 *
27 * $Log: not supported by cvs2svn $
28 *
29 * 13 4/09/99 11:59a Samir
30 * lock keypad updates out if loading level.
31 *
32 * 12 3/22/99 6:26p Matt
33 * Cleaned up error handling in cfile and editor level loads.
34 *
35 * 11 3/11/99 10:51a Nate
36 * Added deletion of "Untitled" Dallas files when doing a New from editor
37 *
38 * 10 2/02/99 3:43p Josh
39 *
40 * 9 2/01/99 2:41p Nate
41 * Added Dallas handling to New, Open, and Save
42 *
43 * 8 9/02/98 4:08p Matt
44 * Don't reset the active texture when creating a new "document"
45 *
46 * 7 2/18/98 2:02p Samir
47 * Fixed editor open and new/close problems.
48 *
49 * 6 2/17/98 8:38p Samir
50 * Commented out OleDocuement code.
51 *
52 * 5 1/29/98 2:15p Samir
53 * Implemented ObjectListModeless and Toolbar button.
54 *
55 * 4 11/06/97 2:48p Matt
56 * Check for error when saving, and put up large message box
57 *
58 * 3 10/16/97 2:32p Samir
59 * Fixed up some timing problems.
60 *
61 * 2 8/19/97 5:58p Samir
62 * OLE additions
63 *
64 * 3 2/11/97 1:42p Samir
65 * Moved file opening and closing to editorDoc where it should be.
66 *
67 * $NoKeywords: $
68 */
69
70 // editorDoc.cpp : implementation of the CEditorDoc class
71 //
72
73 #include "stdafx.h"
74 #include "editor.h"
75 #include "SrvrItem.h" // ADDED FOR OLE SUPPORT
76 #include "editorDoc.h"
77 #include "ObjectListDialog.h"
78 #include "keypaddialog.h"
79 #include "MainFrm.h"
80
81 #include <string.h>
82
83 #include "d3edit.h"
84 #include "mono.h"
85 #include "game.h"
86 #include "HFile.h"
87 #include "cfile.h"
88 #include "ddio.h"
89 #include "manage.h"
90 #include "DallasMainDlg.h"
91
92 #ifdef _DEBUG
93 #define new DEBUG_NEW
94 #undef THIS_FILE
95 static char THIS_FILE[] = __FILE__;
96 #endif
97
98 // Function to copy a Dallas script file
99 int CopyScriptFile(char *old_file, char *new_file) {
100 char old_fullpath[_MAX_PATH];
101 char new_fullpath[_MAX_PATH];
102
103 ddio_MakePath(old_fullpath, LocalScriptDir, old_file, NULL);
104 ddio_MakePath(new_fullpath, LocalScriptDir, new_file, NULL);
105
106 return (cf_CopyFile(new_fullpath, old_fullpath));
107 }
108
109 /////////////////////////////////////////////////////////////////////////////
110 // CEditorDoc
111
112 IMPLEMENT_DYNCREATE(CEditorDoc, COleServerDoc)
113
114 BEGIN_MESSAGE_MAP(CEditorDoc, COleServerDoc)
115 //{{AFX_MSG_MAP(CEditorDoc)
116 // NOTE - the ClassWizard will add and remove mapping macros here.
117 // DO NOT EDIT what you see in these blocks of generated code!
118 //}}AFX_MSG_MAP
119 END_MESSAGE_MAP()
120
121 /////////////////////////////////////////////////////////////////////////////
122 // CEditorDoc construction/destruction
123
124 CEditorDoc::CEditorDoc() {
125 // Use OLE compound files
126 EnableCompoundFile();
127 }
128
129 CEditorDoc::~CEditorDoc() {}
130
131 /////////////////////////////////////////////////////////////////////////////
132 // CEditorDoc server implementation
133
134 COleServerItem *CEditorDoc::OnGetEmbeddedItem() {
135 // OnGetEmbeddedItem is called by the framework to get the COleServerItem
136 // that is associated with the document. It is only called when necessary.
137
138 CEditorSrvrItem *pItem = new CEditorSrvrItem(this);
139 ASSERT_VALID(pItem);
140 return pItem;
141 }
142
143 /////////////////////////////////////////////////////////////////////////////
144 // CEditorDoc serialization
145
146 void CEditorDoc::Serialize(CArchive &ar) {
147 if (ar.IsStoring()) {
148 // TODO: add storing code here
149 } else {
150 // TODO: add loading code here
151 }
152 }
153
154 /////////////////////////////////////////////////////////////////////////////
155 // CEditorDoc diagnostics
156
157 #ifdef _DEBUG
158 void CEditorDoc::AssertValid() const { COleServerDoc::AssertValid(); }
159
160 void CEditorDoc::Dump(CDumpContext &dc) const { COleServerDoc::Dump(dc); }
161 #endif //_DEBUG
162
163 /////////////////////////////////////////////////////////////////////////////
164 // CEditorDoc commands
165
166 BOOL CEditorDoc::OnNewDocument() {
167 // If Dallas is running, don't create a new document
168 if (theApp.m_DallasModelessDlgPtr != NULL) {
169 OutrageMessageBox("The Dallas Script Editor is open!\n\n"
170 "You must close down Dallas before creating a New Level...");
171 return FALSE;
172 }
173
174 if (!COleServerDoc::OnNewDocument())
175 return FALSE;
176
177 // TODO: add reinitialization code here
178 // (SDI documents will reuse this document)
179
180 // Reinitialize Editor State.
181 mprintf(0, "Creating new level...\n");
182 // D3EditState.texdlg_texture = 0;
183
184 // Create new mine
185 CreateNewMine();
186 SetModifiedFlag(FALSE);
187 theApp.main_doc = this;
188
189 // Remove any "Untitled" dallas script files
190 char fullpath[_MAX_PATH];
191 ddio_MakePath(fullpath, LocalScriptDir, "Untitled.cpp", NULL);
192 ddio_DeleteFile(fullpath);
193 ddio_MakePath(fullpath, LocalScriptDir, "Untitled.dll", NULL);
194 ddio_DeleteFile(fullpath);
195 ddio_MakePath(fullpath, LocalScriptDir, "Untitled.msg", NULL);
196 ddio_DeleteFile(fullpath);
197
198 return TRUE;
199 }
200
201 BOOL CEditorDoc::OnOpenDocument(LPCTSTR lpszPathName) {
202 BOOL opened;
203
204 // If Dallas is running, don't open the document
205 if (theApp.m_DallasModelessDlgPtr != NULL) {
206 OutrageMessageBox("The Dallas Script Editor is open!\n\n"
207 "You must close down Dallas before loading a new level...");
208 return FALSE;
209 }
210
211 // if (!COleServerDoc::OnOpenDocument(lpszPathName))
212 // return FALSE;
213
214 // TODO: Add your specialized creation code here
215 // does the level always load correctly?
216 mprintf(0, "Opening level %s...\n", lpszPathName);
217
218 theApp.pause();
219 CKeypadDialog::Deactivate();
220 opened = EditorLoadLevel((char *)lpszPathName);
221 CKeypadDialog::Activate();
222 theApp.resume();
223
224 if (!opened) {
225 SetModifiedFlag(TRUE); // say that the old level was trashed
226 return FALSE;
227 }
228
229 if (D3EditState.objmodeless_on)
230 theApp.main_frame->m_ObjModeless->Refresh();
231
232 SetModifiedFlag(FALSE);
233 theApp.main_doc = this;
234
235 return true;
236 }
237
238 BOOL CEditorDoc::OnSaveDocument(LPCTSTR lpszPathName) {
239 // TODO: Add your specialized code here and/or call the base class
240 char filename[256];
241 BOOL retval;
242
243 strcpy(filename, lpszPathName);
244 if (strchr(filename, '.') == 0)
245 strcat(filename, ".d3l"); // check for extension
246
247 mprintf(0, "Saving level %s...\n", lpszPathName);
248 retval = (BOOL)EditorSaveLevel((char *)lpszPathName);
249
250 if (retval)
251 SetModifiedFlag(FALSE);
252 else
253 OutrageMessageBox("Error saving level -- Level not saved.\n\n"
254 "If you get this error trying to save a file you loaded from the network, "
255 "here's a workaround you can use to save your file:\n\n"
256 " 1. Use File/Save As to save the file with a temporary name.\n"
257 " 2. Use File/New to clear out the level.\n"
258 " 3. Load the level from the temporary file.\n"
259 " 4. Use File/Save As to save with the real name.\n\n"
260 "Matt & Samir are working on this problem.");
261
262 // If the Save was successfull, handle the Dallas coordination
263 if (retval) {
264
265 // If Dallas is up, change its filenames
266 if (theApp.m_DallasModelessDlgPtr != NULL) {
267 theApp.m_DallasModelessDlgPtr->SetAllFilenamesToThis((char *)lpszPathName);
268 }
269
270 // Copy Dallas files (if doing a 'Save As')
271 char old_filename[_MAX_PATH + 1];
272 char new_filename[_MAX_PATH + 1];
273
274 CString old_level_fname, new_level_fname;
275 old_level_fname = GetPathName();
276 new_level_fname = (char *)lpszPathName;
277
278 if (old_level_fname.IsEmpty())
279 strcpy(old_filename, "Untitled");
280 else
281 ddio_SplitPath(old_level_fname.GetBuffer(0), NULL, old_filename, NULL);
282
283 if (new_level_fname.IsEmpty())
284 strcpy(new_filename, "Untitled");
285 else
286 ddio_SplitPath(new_level_fname.GetBuffer(0), NULL, new_filename, NULL);
287
288 // Make sure names are different (ie 'Save As')
289 if (stricmp(old_filename, new_filename) != 0) {
290 CString old_file, new_file;
291
292 old_file.Format("%s.cpp", old_filename);
293 new_file.Format("%s.cpp", new_filename);
294 CopyScriptFile(old_file.GetBuffer(0), new_file.GetBuffer(0));
295
296 old_file.Format("%s.msg", old_filename);
297 new_file.Format("%s.msg", new_filename);
298 CopyScriptFile(old_file.GetBuffer(0), new_file.GetBuffer(0));
299
300 old_file.Format("%s.dll", old_filename);
301 new_file.Format("%s.dll", new_filename);
302 CopyScriptFile(old_file.GetBuffer(0), new_file.GetBuffer(0));
303 }
304 }
305
306 return retval;
307 }
308
309 void CEditorDoc::OnCloseDocument() {
310 // TODO: Add your specialized code here and/or call the base class
311 COleServerDoc::OnCloseDocument();
312 mprintf(0, "Closing current level...\n");
313
314 theApp.main_doc = NULL;
315 }
316
317 BOOL CEditorDoc::SaveModified() {
318 BOOL return_val; // tells system what to do (save, no, etc)
319
320 return_val = COleServerDoc::SaveModified();
321
322 return return_val;
323 }