]>
Commit | Line | Data |
---|---|---|
5d9ea849 | 1 | ///////////////////////////////////////////////////////////////////////////// |
1e6feb95 | 2 | // Name: src/common/docview.cpp |
c801d85f KB |
3 | // Purpose: Document/view classes |
4 | // Author: Julian Smart | |
c77049a0 | 5 | // Modified by: Vadim Zeitlin |
c801d85f KB |
6 | // Created: 01/02/97 |
7 | // RCS-ID: $Id$ | |
55d99c7a | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
0fb67cd1 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
c801d85f KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
8ecff181 | 24 | #pragma hdrstop |
c801d85f KB |
25 | #endif |
26 | ||
47d67540 | 27 | #if wxUSE_DOC_VIEW_ARCHITECTURE |
c801d85f | 28 | |
246c5004 WS |
29 | #include "wx/docview.h" |
30 | ||
c801d85f | 31 | #ifndef WX_PRECOMP |
8ecff181 | 32 | #include "wx/list.h" |
0fb67cd1 VZ |
33 | #include "wx/string.h" |
34 | #include "wx/utils.h" | |
35 | #include "wx/app.h" | |
36 | #include "wx/dc.h" | |
d0bdc3ca | 37 | #include "wx/dialog.h" |
0fb67cd1 | 38 | #include "wx/menu.h" |
0fb67cd1 | 39 | #include "wx/filedlg.h" |
6de2f8b9 | 40 | #include "wx/intl.h" |
c0369005 | 41 | #include "wx/log.h" |
246c5004 | 42 | #include "wx/msgdlg.h" |
59cf2e49 | 43 | #include "wx/mdi.h" |
949c9f74 | 44 | #include "wx/choicdlg.h" |
f7bd2698 JS |
45 | #endif |
46 | ||
ce4169a4 | 47 | #if wxUSE_PRINTING_ARCHITECTURE |
8ecff181 WS |
48 | #include "wx/prntbase.h" |
49 | #include "wx/printdlg.h" | |
ce4169a4 RR |
50 | #endif |
51 | ||
7f555861 | 52 | #include "wx/confbase.h" |
c77049a0 | 53 | #include "wx/filename.h" |
74b31181 | 54 | #include "wx/file.h" |
c77049a0 | 55 | #include "wx/ffile.h" |
1e6feb95 | 56 | #include "wx/cmdproc.h" |
0f30d8e3 | 57 | #include "wx/tokenzr.h" |
b4a980f4 | 58 | #include "wx/filename.h" |
d8efd219 | 59 | #include "wx/stdpaths.h" |
c77049a0 | 60 | #include "wx/vector.h" |
664e1314 VZ |
61 | #include "wx/scopedarray.h" |
62 | #include "wx/scopedptr.h" | |
3a393f8a | 63 | #include "wx/except.h" |
c801d85f | 64 | |
a533f5c1 | 65 | #if wxUSE_STD_IOSTREAM |
8ecff181 | 66 | #include "wx/ioswrap.h" |
968454db | 67 | #include "wx/beforestd.h" |
8ecff181 WS |
68 | #if wxUSE_IOSTREAMH |
69 | #include <fstream.h> | |
70 | #else | |
71 | #include <fstream> | |
72 | #endif | |
968454db | 73 | #include "wx/afterstd.h" |
a533f5c1 | 74 | #else |
8ecff181 | 75 | #include "wx/wfstream.h" |
c801d85f KB |
76 | #endif |
77 | ||
c77049a0 VZ |
78 | typedef wxVector<wxDocTemplate *> wxDocTemplates; |
79 | ||
0fb67cd1 | 80 | // ---------------------------------------------------------------------------- |
77ffb593 | 81 | // wxWidgets macros |
0fb67cd1 VZ |
82 | // ---------------------------------------------------------------------------- |
83 | ||
1e6feb95 VZ |
84 | IMPLEMENT_ABSTRACT_CLASS(wxDocument, wxEvtHandler) |
85 | IMPLEMENT_ABSTRACT_CLASS(wxView, wxEvtHandler) | |
86 | IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate, wxObject) | |
87 | IMPLEMENT_DYNAMIC_CLASS(wxDocManager, wxEvtHandler) | |
88 | IMPLEMENT_CLASS(wxDocChildFrame, wxFrame) | |
89 | IMPLEMENT_CLASS(wxDocParentFrame, wxFrame) | |
0fb67cd1 | 90 | |
1e6feb95 VZ |
91 | #if wxUSE_PRINTING_ARCHITECTURE |
92 | IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout, wxPrintout) | |
93 | #endif | |
0fb67cd1 | 94 | |
0fb67cd1 VZ |
95 | // ============================================================================ |
96 | // implementation | |
97 | // ============================================================================ | |
98 | ||
99 | // ---------------------------------------------------------------------------- | |
c77049a0 | 100 | // private helpers |
0fb67cd1 VZ |
101 | // ---------------------------------------------------------------------------- |
102 | ||
c77049a0 VZ |
103 | namespace |
104 | { | |
105 | ||
c77049a0 | 106 | wxString FindExtension(const wxString& path) |
0fb67cd1 VZ |
107 | { |
108 | wxString ext; | |
47d281e6 | 109 | wxFileName::SplitPath(path, NULL, NULL, &ext); |
0fb67cd1 VZ |
110 | |
111 | // VZ: extensions are considered not case sensitive - is this really a good | |
112 | // idea? | |
113 | return ext.MakeLower(); | |
114 | } | |
115 | ||
c77049a0 VZ |
116 | } // anonymous namespace |
117 | ||
0fb67cd1 VZ |
118 | // ---------------------------------------------------------------------------- |
119 | // Definition of wxDocument | |
120 | // ---------------------------------------------------------------------------- | |
c801d85f KB |
121 | |
122 | wxDocument::wxDocument(wxDocument *parent) | |
123 | { | |
68379eaf | 124 | m_documentModified = false; |
c77049a0 | 125 | m_documentTemplate = NULL; |
4db97e24 VZ |
126 | |
127 | m_documentParent = parent; | |
128 | if ( parent ) | |
129 | parent->m_childDocuments.push_back(this); | |
130 | ||
c77049a0 | 131 | m_commandProcessor = NULL; |
68379eaf | 132 | m_savedYet = false; |
c801d85f KB |
133 | } |
134 | ||
0fb67cd1 | 135 | bool wxDocument::DeleteContents() |
c801d85f | 136 | { |
68379eaf | 137 | return true; |
c801d85f KB |
138 | } |
139 | ||
0fb67cd1 | 140 | wxDocument::~wxDocument() |
c801d85f | 141 | { |
89d94e04 | 142 | delete m_commandProcessor; |
c801d85f | 143 | |
f6bcfd97 BP |
144 | if (GetDocumentManager()) |
145 | GetDocumentManager()->RemoveDocument(this); | |
c801d85f | 146 | |
4db97e24 VZ |
147 | if ( m_documentParent ) |
148 | m_documentParent->m_childDocuments.remove(this); | |
149 | ||
0fb67cd1 VZ |
150 | // Not safe to do here, since it'll invoke virtual view functions |
151 | // expecting to see valid derived objects: and by the time we get here, | |
152 | // we've called destructors higher up. | |
153 | //DeleteAllViews(); | |
c801d85f | 154 | } |
0fb67cd1 VZ |
155 | |
156 | bool wxDocument::Close() | |
c801d85f | 157 | { |
89d94e04 | 158 | if ( !OnSaveModified() ) |
68379eaf | 159 | return false; |
89d94e04 | 160 | |
4db97e24 VZ |
161 | // When the parent document closes, its children must be closed as well as |
162 | // they can't exist without the parent. | |
163 | ||
164 | // As usual, first check if all children can be closed. | |
165 | DocsList::const_iterator it = m_childDocuments.begin(); | |
166 | for ( DocsList::const_iterator end = m_childDocuments.end(); it != end; ++it ) | |
167 | { | |
168 | if ( !(*it)->OnSaveModified() ) | |
169 | { | |
170 | // Leave the parent document opened if a child can't close. | |
171 | return false; | |
172 | } | |
173 | } | |
174 | ||
175 | // Now that they all did, do close them: as m_childDocuments is modified as | |
176 | // we iterate over it, don't use the usual for-style iteration here. | |
177 | while ( !m_childDocuments.empty() ) | |
178 | { | |
179 | wxDocument * const childDoc = m_childDocuments.front(); | |
180 | ||
181 | // This will call OnSaveModified() once again but it shouldn't do | |
182 | // anything as the document was just saved or marked as not needing to | |
183 | // be saved by the call to OnSaveModified() that returned true above. | |
184 | if ( !childDoc->Close() ) | |
185 | { | |
186 | wxFAIL_MSG( "Closing the child document unexpectedly failed " | |
187 | "after its OnSaveModified() returned true" ); | |
188 | } | |
189 | ||
190 | // Delete the child document by deleting all its views. | |
191 | childDoc->DeleteAllViews(); | |
192 | } | |
193 | ||
194 | ||
89d94e04 | 195 | return OnCloseDocument(); |
c801d85f | 196 | } |
0fb67cd1 VZ |
197 | |
198 | bool wxDocument::OnCloseDocument() | |
c801d85f | 199 | { |
b23e843b | 200 | // Tell all views that we're about to close |
42771621 | 201 | NotifyClosing(); |
0fb67cd1 | 202 | DeleteContents(); |
68379eaf WS |
203 | Modify(false); |
204 | return true; | |
c801d85f KB |
205 | } |
206 | ||
0fb67cd1 VZ |
207 | // Note that this implicitly deletes the document when the last view is |
208 | // deleted. | |
209 | bool wxDocument::DeleteAllViews() | |
c801d85f | 210 | { |
f6bcfd97 BP |
211 | wxDocManager* manager = GetDocumentManager(); |
212 | ||
40702099 VZ |
213 | // first check if all views agree to be closed |
214 | const wxList::iterator end = m_documentViews.end(); | |
215 | for ( wxList::iterator i = m_documentViews.begin(); i != end; ++i ) | |
0fb67cd1 | 216 | { |
40702099 VZ |
217 | wxView *view = (wxView *)*i; |
218 | if ( !view->Close() ) | |
68379eaf | 219 | return false; |
0fb67cd1 | 220 | } |
d5a93fda | 221 | |
40702099 VZ |
222 | // all views agreed to close, now do close them |
223 | if ( m_documentViews.empty() ) | |
224 | { | |
225 | // normally the document would be implicitly deleted when the last view | |
226 | // is, but if don't have any views, do it here instead | |
227 | if ( manager && manager->GetDocuments().Member(this) ) | |
228 | delete this; | |
229 | } | |
230 | else // have views | |
231 | { | |
232 | // as we delete elements we iterate over, don't use the usual "from | |
233 | // begin to end" loop | |
234 | for ( ;; ) | |
235 | { | |
236 | wxView *view = (wxView *)*m_documentViews.begin(); | |
237 | ||
238 | bool isLastOne = m_documentViews.size() == 1; | |
239 | ||
240 | // this always deletes the node implicitly and if this is the last | |
241 | // view also deletes this object itself (also implicitly, great), | |
242 | // so we can't test for m_documentViews.empty() after calling this! | |
243 | delete view; | |
244 | ||
245 | if ( isLastOne ) | |
246 | break; | |
247 | } | |
248 | } | |
f6bcfd97 | 249 | |
68379eaf | 250 | return true; |
c801d85f KB |
251 | } |
252 | ||
6de2f8b9 | 253 | wxView *wxDocument::GetFirstView() const |
c801d85f | 254 | { |
89d94e04 | 255 | if ( m_documentViews.empty() ) |
c77049a0 | 256 | return NULL; |
89d94e04 VZ |
257 | |
258 | return static_cast<wxView *>(m_documentViews.GetFirst()->GetData()); | |
c801d85f KB |
259 | } |
260 | ||
250ab35a JS |
261 | void wxDocument::Modify(bool mod) |
262 | { | |
263 | if (mod != m_documentModified) | |
264 | { | |
265 | m_documentModified = mod; | |
266 | ||
267 | // Allow views to append asterix to the title | |
268 | wxView* view = GetFirstView(); | |
269 | if (view) view->OnChangeFilename(); | |
270 | } | |
271 | } | |
272 | ||
6de2f8b9 | 273 | wxDocManager *wxDocument::GetDocumentManager() const |
c801d85f | 274 | { |
4db97e24 VZ |
275 | // For child documents we use the same document manager as the parent, even |
276 | // though we don't have our own template (as children are not opened/saved | |
277 | // directly). | |
278 | if ( m_documentParent ) | |
279 | return m_documentParent->GetDocumentManager(); | |
280 | ||
c77049a0 | 281 | return m_documentTemplate ? m_documentTemplate->GetDocumentManager() : NULL; |
c801d85f KB |
282 | } |
283 | ||
0fb67cd1 | 284 | bool wxDocument::OnNewDocument() |
c801d85f | 285 | { |
0b108d10 VZ |
286 | // notice that there is no need to neither reset nor even check the |
287 | // modified flag here as the document itself is a new object (this is only | |
288 | // called from CreateDocument()) and so it shouldn't be saved anyhow even | |
289 | // if it is modified -- this could happen if the user code creates | |
290 | // documents pre-filled with some user-entered (and which hence must not be | |
291 | // lost) information | |
c801d85f | 292 | |
68379eaf | 293 | SetDocumentSaved(false); |
c801d85f | 294 | |
724b119a | 295 | const wxString name = GetDocumentManager()->MakeNewDocumentName(); |
0fb67cd1 | 296 | SetTitle(name); |
68379eaf | 297 | SetFilename(name, true); |
0fb67cd1 | 298 | |
68379eaf | 299 | return true; |
c801d85f KB |
300 | } |
301 | ||
0fb67cd1 | 302 | bool wxDocument::Save() |
c801d85f | 303 | { |
de56f240 | 304 | if ( AlreadySaved() ) |
68379eaf | 305 | return true; |
c801d85f | 306 | |
d06a66f5 VZ |
307 | if ( m_documentFile.empty() || !m_savedYet ) |
308 | return SaveAs(); | |
309 | ||
310 | return OnSaveDocument(m_documentFile); | |
c801d85f | 311 | } |
0fb67cd1 VZ |
312 | |
313 | bool wxDocument::SaveAs() | |
c801d85f | 314 | { |
ba681060 VZ |
315 | wxDocTemplate *docTemplate = GetDocumentTemplate(); |
316 | if (!docTemplate) | |
68379eaf | 317 | return false; |
0fb67cd1 | 318 | |
89d94e04 VZ |
319 | #ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS |
320 | wxString filter = docTemplate->GetDescription() + wxT(" (") + | |
321 | docTemplate->GetFileFilter() + wxT(")|") + | |
322 | docTemplate->GetFileFilter(); | |
04129514 JS |
323 | |
324 | // Now see if there are some other template with identical view and document | |
325 | // classes, whose filters may also be used. | |
04129514 JS |
326 | if (docTemplate->GetViewClassInfo() && docTemplate->GetDocClassInfo()) |
327 | { | |
89d94e04 VZ |
328 | wxList::compatibility_iterator |
329 | node = docTemplate->GetDocumentManager()->GetTemplates().GetFirst(); | |
04129514 JS |
330 | while (node) |
331 | { | |
332 | wxDocTemplate *t = (wxDocTemplate*) node->GetData(); | |
68379eaf | 333 | |
04129514 JS |
334 | if (t->IsVisible() && t != docTemplate && |
335 | t->GetViewClassInfo() == docTemplate->GetViewClassInfo() && | |
336 | t->GetDocClassInfo() == docTemplate->GetDocClassInfo()) | |
337 | { | |
338 | // add a '|' to separate this filter from the previous one | |
b494c48b | 339 | if ( !filter.empty() ) |
04129514 | 340 | filter << wxT('|'); |
68379eaf | 341 | |
89d94e04 VZ |
342 | filter << t->GetDescription() |
343 | << wxT(" (") << t->GetFileFilter() << wxT(") |") | |
04129514 JS |
344 | << t->GetFileFilter(); |
345 | } | |
346 | ||
347 | node = node->GetNext(); | |
348 | } | |
349 | } | |
350 | #else | |
351 | wxString filter = docTemplate->GetFileFilter() ; | |
352 | #endif | |
89d94e04 | 353 | |
e0214f1f | 354 | wxString defaultDir = docTemplate->GetDirectory(); |
d8efd219 VZ |
355 | if ( defaultDir.empty() ) |
356 | { | |
e0214f1f | 357 | defaultDir = wxPathOnly(GetFilename()); |
d8efd219 VZ |
358 | if ( defaultDir.empty() ) |
359 | defaultDir = GetDocumentManager()->GetLastDirectory(); | |
360 | } | |
e0214f1f | 361 | |
00e3ea1c | 362 | wxString fileName = wxFileSelector(_("Save As"), |
e0214f1f | 363 | defaultDir, |
015e69f3 | 364 | wxFileNameFromPath(GetFilename()), |
0fb67cd1 | 365 | docTemplate->GetDefaultExtension(), |
04129514 | 366 | filter, |
ff3e84ff | 367 | wxFD_SAVE | wxFD_OVERWRITE_PROMPT, |
0fb67cd1 VZ |
368 | GetDocumentWindow()); |
369 | ||
00e3ea1c | 370 | if (fileName.empty()) |
89d94e04 | 371 | return false; // cancelled by user |
ba681060 | 372 | |
9d4a05be | 373 | // Files that were not saved correctly are not added to the FileHistory. |
00e3ea1c | 374 | if (!OnSaveDocument(fileName)) |
68379eaf | 375 | return false; |
9d4a05be | 376 | |
00e3ea1c FM |
377 | SetTitle(wxFileNameFromPath(fileName)); |
378 | SetFilename(fileName, true); // will call OnChangeFileName automatically | |
379 | ||
fd461854 VZ |
380 | // A file that doesn't use the default extension of its document template |
381 | // cannot be opened via the FileHistory, so we do not add it. | |
382 | if (docTemplate->FileMatchesTemplate(fileName)) | |
383 | { | |
384 | GetDocumentManager()->AddFileToHistory(fileName); | |
385 | } | |
386 | //else: the user will probably not be able to open the file again, so we | |
387 | // could warn about the wrong file-extension here | |
89d94e04 | 388 | |
fd461854 | 389 | return true; |
c801d85f | 390 | } |
0fb67cd1 | 391 | |
c801d85f KB |
392 | bool wxDocument::OnSaveDocument(const wxString& file) |
393 | { | |
0fb67cd1 | 394 | if ( !file ) |
68379eaf | 395 | return false; |
c801d85f | 396 | |
69936aea | 397 | if ( !DoSaveDocument(file) ) |
68379eaf | 398 | return false; |
0fb67cd1 | 399 | |
8c85eb7b VZ |
400 | if ( m_commandProcessor ) |
401 | m_commandProcessor->MarkAsSaved(); | |
402 | ||
68379eaf | 403 | Modify(false); |
0fb67cd1 | 404 | SetFilename(file); |
68379eaf | 405 | SetDocumentSaved(true); |
26eef304 | 406 | #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON |
b8afeb43 SC |
407 | wxFileName fn(file) ; |
408 | fn.MacSetDefaultTypeAndCreator() ; | |
409 | #endif | |
68379eaf | 410 | return true; |
c801d85f | 411 | } |
0fb67cd1 | 412 | |
c801d85f KB |
413 | bool wxDocument::OnOpenDocument(const wxString& file) |
414 | { | |
0b108d10 VZ |
415 | // notice that there is no need to check the modified flag here for the |
416 | // reasons explained in OnNewDocument() | |
c801d85f | 417 | |
69936aea | 418 | if ( !DoOpenDocument(file) ) |
68379eaf | 419 | return false; |
0fb67cd1 | 420 | |
68379eaf | 421 | SetFilename(file, true); |
0b108d10 VZ |
422 | |
423 | // stretching the logic a little this does make sense because the document | |
424 | // had been saved into the file we just loaded it from, it just could have | |
425 | // happened during a previous program execution, it's just that the name of | |
426 | // this method is a bit unfortunate, it should probably have been called | |
427 | // HasAssociatedFileName() | |
428 | SetDocumentSaved(true); | |
0fb67cd1 VZ |
429 | |
430 | UpdateAllViews(); | |
c801d85f | 431 | |
68379eaf | 432 | return true; |
c801d85f | 433 | } |
0fb67cd1 | 434 | |
a533f5c1 | 435 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 436 | wxSTD istream& wxDocument::LoadObject(wxSTD istream& stream) |
23a54e14 RR |
437 | #else |
438 | wxInputStream& wxDocument::LoadObject(wxInputStream& stream) | |
439 | #endif | |
c801d85f | 440 | { |
0fb67cd1 | 441 | return stream; |
c801d85f KB |
442 | } |
443 | ||
23a54e14 | 444 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 445 | wxSTD ostream& wxDocument::SaveObject(wxSTD ostream& stream) |
a533f5c1 | 446 | #else |
23a54e14 RR |
447 | wxOutputStream& wxDocument::SaveObject(wxOutputStream& stream) |
448 | #endif | |
a533f5c1 | 449 | { |
23a54e14 | 450 | return stream; |
a533f5c1 | 451 | } |
c801d85f | 452 | |
0fb67cd1 | 453 | bool wxDocument::Revert() |
c801d85f | 454 | { |
4311588b VZ |
455 | if ( wxMessageBox |
456 | ( | |
457 | _("Discard changes and reload the last saved version?"), | |
458 | wxTheApp->GetAppDisplayName(), | |
459 | wxYES_NO | wxCANCEL | wxICON_QUESTION, | |
460 | GetDocumentWindow() | |
461 | ) != wxYES ) | |
462 | return false; | |
463 | ||
464 | if ( !DoOpenDocument(GetFilename()) ) | |
465 | return false; | |
466 | ||
467 | Modify(false); | |
468 | UpdateAllViews(); | |
469 | ||
470 | return true; | |
c801d85f KB |
471 | } |
472 | ||
473 | ||
474 | // Get title, or filename if no title, else unnamed | |
724b119a | 475 | #if WXWIN_COMPATIBILITY_2_8 |
c801d85f KB |
476 | bool wxDocument::GetPrintableName(wxString& buf) const |
477 | { | |
4c51a665 | 478 | // this function cannot only be overridden by the user code but also |
724b119a VZ |
479 | // called by it so we need to ensure that we return the same thing as |
480 | // GetUserReadableName() but we can't call it because this would result in | |
481 | // an infinite recursion, hence we use the helper DoGetUserReadableName() | |
482 | buf = DoGetUserReadableName(); | |
483 | ||
484 | return true; | |
485 | } | |
486 | #endif // WXWIN_COMPATIBILITY_2_8 | |
487 | ||
488 | wxString wxDocument::GetUserReadableName() const | |
489 | { | |
490 | #if WXWIN_COMPATIBILITY_2_8 | |
491 | // we need to call the old virtual function to ensure that the overridden | |
492 | // version of it is still called | |
493 | wxString name; | |
494 | if ( GetPrintableName(name) ) | |
495 | return name; | |
496 | #endif // WXWIN_COMPATIBILITY_2_8 | |
497 | ||
498 | return DoGetUserReadableName(); | |
499 | } | |
500 | ||
501 | wxString wxDocument::DoGetUserReadableName() const | |
502 | { | |
503 | if ( !m_documentTitle.empty() ) | |
504 | return m_documentTitle; | |
505 | ||
506 | if ( !m_documentFile.empty() ) | |
507 | return wxFileNameFromPath(m_documentFile); | |
508 | ||
509 | return _("unnamed"); | |
c801d85f KB |
510 | } |
511 | ||
6de2f8b9 | 512 | wxWindow *wxDocument::GetDocumentWindow() const |
c801d85f | 513 | { |
89d94e04 VZ |
514 | wxView * const view = GetFirstView(); |
515 | ||
516 | return view ? view->GetFrame() : wxTheApp->GetTopWindow(); | |
c801d85f KB |
517 | } |
518 | ||
0fb67cd1 | 519 | wxCommandProcessor *wxDocument::OnCreateCommandProcessor() |
c801d85f | 520 | { |
0fb67cd1 | 521 | return new wxCommandProcessor; |
c801d85f KB |
522 | } |
523 | ||
68379eaf | 524 | // true if safe to close |
0fb67cd1 | 525 | bool wxDocument::OnSaveModified() |
c801d85f | 526 | { |
c77049a0 VZ |
527 | if ( IsModified() ) |
528 | { | |
529 | switch ( wxMessageBox | |
530 | ( | |
531 | wxString::Format | |
532 | ( | |
4f57a7bf | 533 | _("Do you want to save changes to %s?"), |
c77049a0 VZ |
534 | GetUserReadableName() |
535 | ), | |
536 | wxTheApp->GetAppDisplayName(), | |
b6e96faf | 537 | wxYES_NO | wxCANCEL | wxICON_QUESTION | wxCENTRE |
c77049a0 | 538 | ) ) |
0fb67cd1 | 539 | { |
c77049a0 VZ |
540 | case wxNO: |
541 | Modify(false); | |
542 | break; | |
543 | ||
544 | case wxYES: | |
545 | return Save(); | |
546 | ||
547 | case wxCANCEL: | |
548 | return false; | |
0fb67cd1 | 549 | } |
c801d85f | 550 | } |
c77049a0 | 551 | |
68379eaf | 552 | return true; |
c801d85f KB |
553 | } |
554 | ||
555 | bool wxDocument::Draw(wxDC& WXUNUSED(context)) | |
556 | { | |
68379eaf | 557 | return true; |
c801d85f KB |
558 | } |
559 | ||
560 | bool wxDocument::AddView(wxView *view) | |
561 | { | |
c77049a0 | 562 | if ( !m_documentViews.Member(view) ) |
0fb67cd1 VZ |
563 | { |
564 | m_documentViews.Append(view); | |
565 | OnChangedViewList(); | |
566 | } | |
68379eaf | 567 | return true; |
c801d85f KB |
568 | } |
569 | ||
570 | bool wxDocument::RemoveView(wxView *view) | |
571 | { | |
0fb67cd1 VZ |
572 | (void)m_documentViews.DeleteObject(view); |
573 | OnChangedViewList(); | |
68379eaf | 574 | return true; |
c801d85f KB |
575 | } |
576 | ||
577 | bool wxDocument::OnCreate(const wxString& WXUNUSED(path), long flags) | |
578 | { | |
c77049a0 | 579 | return GetDocumentTemplate()->CreateView(this, flags) != NULL; |
c801d85f KB |
580 | } |
581 | ||
582 | // Called after a view is added or removed. | |
583 | // The default implementation deletes the document if | |
584 | // there are no more views. | |
0fb67cd1 | 585 | void wxDocument::OnChangedViewList() |
c801d85f | 586 | { |
c77049a0 VZ |
587 | if ( m_documentViews.empty() && OnSaveModified() ) |
588 | delete this; | |
c801d85f KB |
589 | } |
590 | ||
591 | void wxDocument::UpdateAllViews(wxView *sender, wxObject *hint) | |
592 | { | |
222ed1d6 | 593 | wxList::compatibility_iterator node = m_documentViews.GetFirst(); |
0fb67cd1 VZ |
594 | while (node) |
595 | { | |
b1d4dd7a | 596 | wxView *view = (wxView *)node->GetData(); |
42771621 | 597 | if (view != sender) |
2b5f62a0 | 598 | view->OnUpdate(sender, hint); |
b1d4dd7a | 599 | node = node->GetNext(); |
0fb67cd1 | 600 | } |
c801d85f KB |
601 | } |
602 | ||
b23e843b JS |
603 | void wxDocument::NotifyClosing() |
604 | { | |
222ed1d6 | 605 | wxList::compatibility_iterator node = m_documentViews.GetFirst(); |
b23e843b JS |
606 | while (node) |
607 | { | |
b1d4dd7a | 608 | wxView *view = (wxView *)node->GetData(); |
b23e843b | 609 | view->OnClosingDocument(); |
b1d4dd7a | 610 | node = node->GetNext(); |
b23e843b JS |
611 | } |
612 | } | |
613 | ||
c801d85f KB |
614 | void wxDocument::SetFilename(const wxString& filename, bool notifyViews) |
615 | { | |
0fb67cd1 | 616 | m_documentFile = filename; |
00e3ea1c FM |
617 | OnChangeFilename(notifyViews); |
618 | } | |
619 | ||
620 | void wxDocument::OnChangeFilename(bool notifyViews) | |
621 | { | |
0fb67cd1 | 622 | if ( notifyViews ) |
c801d85f | 623 | { |
0fb67cd1 | 624 | // Notify the views that the filename has changed |
222ed1d6 | 625 | wxList::compatibility_iterator node = m_documentViews.GetFirst(); |
0fb67cd1 VZ |
626 | while (node) |
627 | { | |
b1d4dd7a | 628 | wxView *view = (wxView *)node->GetData(); |
0fb67cd1 | 629 | view->OnChangeFilename(); |
b1d4dd7a | 630 | node = node->GetNext(); |
0fb67cd1 | 631 | } |
c801d85f | 632 | } |
c801d85f KB |
633 | } |
634 | ||
69936aea VZ |
635 | bool wxDocument::DoSaveDocument(const wxString& file) |
636 | { | |
69936aea | 637 | #if wxUSE_STD_IOSTREAM |
53e2e87c | 638 | wxSTD ofstream store(file.mb_str(), wxSTD ios::binary); |
c09b7217 | 639 | if ( !store ) |
69936aea VZ |
640 | #else |
641 | wxFileOutputStream store(file); | |
c09b7217 | 642 | if ( store.GetLastError() != wxSTREAM_NO_ERROR ) |
69936aea VZ |
643 | #endif |
644 | { | |
e58eaff7 | 645 | wxLogError(_("File \"%s\" could not be opened for writing."), file); |
68379eaf | 646 | return false; |
69936aea | 647 | } |
e58eaff7 | 648 | |
69936aea VZ |
649 | if (!SaveObject(store)) |
650 | { | |
e58eaff7 | 651 | wxLogError(_("Failed to save document to the file \"%s\"."), file); |
68379eaf | 652 | return false; |
69936aea VZ |
653 | } |
654 | ||
68379eaf | 655 | return true; |
69936aea VZ |
656 | } |
657 | ||
658 | bool wxDocument::DoOpenDocument(const wxString& file) | |
659 | { | |
69936aea | 660 | #if wxUSE_STD_IOSTREAM |
53e2e87c | 661 | wxSTD ifstream store(file.mb_str(), wxSTD ios::binary); |
c09b7217 | 662 | if ( !store ) |
69936aea VZ |
663 | #else |
664 | wxFileInputStream store(file); | |
9690b006 | 665 | if (store.GetLastError() != wxSTREAM_NO_ERROR || !store.IsOk()) |
69936aea VZ |
666 | #endif |
667 | { | |
e58eaff7 VZ |
668 | wxLogError(_("File \"%s\" could not be opened for reading."), file); |
669 | return false; | |
670 | } | |
671 | ||
69936aea | 672 | #if wxUSE_STD_IOSTREAM |
e58eaff7 | 673 | LoadObject(store); |
c09b7217 | 674 | if ( !store ) |
69936aea | 675 | #else |
e58eaff7 VZ |
676 | int res = LoadObject(store).GetLastError(); |
677 | if ( res != wxSTREAM_NO_ERROR && res != wxSTREAM_EOF ) | |
69936aea | 678 | #endif |
e58eaff7 VZ |
679 | { |
680 | wxLogError(_("Failed to read document from the file \"%s\"."), file); | |
681 | return false; | |
69936aea VZ |
682 | } |
683 | ||
e58eaff7 | 684 | return true; |
69936aea VZ |
685 | } |
686 | ||
687 | ||
0fb67cd1 VZ |
688 | // ---------------------------------------------------------------------------- |
689 | // Document view | |
690 | // ---------------------------------------------------------------------------- | |
c801d85f | 691 | |
dbdb39b2 | 692 | wxView::wxView() |
c801d85f | 693 | { |
c77049a0 | 694 | m_viewDocument = NULL; |
0fb67cd1 | 695 | |
c77049a0 | 696 | m_viewFrame = NULL; |
a9e2e6e5 VZ |
697 | |
698 | m_docChildFrame = NULL; | |
c801d85f KB |
699 | } |
700 | ||
0fb67cd1 | 701 | wxView::~wxView() |
c801d85f | 702 | { |
d3785012 | 703 | if (m_viewDocument && GetDocumentManager()) |
63c639ec | 704 | GetDocumentManager()->ActivateView(this, false); |
a9e2e6e5 VZ |
705 | |
706 | // reset our frame view first, before removing it from the document as | |
707 | // SetView(NULL) is a simple call while RemoveView() may result in user | |
708 | // code being executed and this user code can, for example, show a message | |
709 | // box which would result in an activation event for m_docChildFrame and so | |
710 | // could reactivate the view being destroyed -- unless we reset it first | |
711 | if ( m_docChildFrame && m_docChildFrame->GetView() == this ) | |
ada1ff0d VZ |
712 | { |
713 | // prevent it from doing anything with us | |
a9e2e6e5 VZ |
714 | m_docChildFrame->SetView(NULL); |
715 | ||
ada1ff0d VZ |
716 | // it doesn't make sense to leave the frame alive if its associated |
717 | // view doesn't exist any more so unconditionally close it as well | |
718 | // | |
719 | // notice that we only get here if m_docChildFrame is non-NULL in the | |
720 | // first place and it will be always NULL if we're deleted because our | |
721 | // frame was closed, so this only catches the case of directly deleting | |
722 | // the view, as it happens if its creation fails in wxDocTemplate:: | |
723 | // CreateView() for example | |
724 | m_docChildFrame->GetWindow()->Destroy(); | |
725 | } | |
726 | ||
a9e2e6e5 VZ |
727 | if ( m_viewDocument ) |
728 | m_viewDocument->RemoveView(this); | |
c801d85f KB |
729 | } |
730 | ||
ada1ff0d VZ |
731 | void wxView::SetDocChildFrame(wxDocChildFrameAnyBase *docChildFrame) |
732 | { | |
733 | SetFrame(docChildFrame ? docChildFrame->GetWindow() : NULL); | |
734 | m_docChildFrame = docChildFrame; | |
735 | } | |
736 | ||
8cc208e3 | 737 | bool wxView::TryBefore(wxEvent& event) |
c801d85f | 738 | { |
bba5e72a | 739 | wxDocument * const doc = GetDocument(); |
44070fd3 | 740 | return doc && doc->ProcessEventLocally(event); |
c801d85f KB |
741 | } |
742 | ||
89d94e04 VZ |
743 | void wxView::OnActivateView(bool WXUNUSED(activate), |
744 | wxView *WXUNUSED(activeView), | |
745 | wxView *WXUNUSED(deactiveView)) | |
c801d85f KB |
746 | { |
747 | } | |
748 | ||
749 | void wxView::OnPrint(wxDC *dc, wxObject *WXUNUSED(info)) | |
750 | { | |
0fb67cd1 | 751 | OnDraw(dc); |
c801d85f KB |
752 | } |
753 | ||
754 | void wxView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint)) | |
755 | { | |
756 | } | |
757 | ||
0fb67cd1 | 758 | void wxView::OnChangeFilename() |
c801d85f | 759 | { |
faa49bfd WS |
760 | // GetFrame can return wxWindow rather than wxTopLevelWindow due to |
761 | // generic MDI implementation so use SetLabel rather than SetTitle. | |
762 | // It should cause SetTitle() for top level windows. | |
763 | wxWindow *win = GetFrame(); | |
764 | if (!win) return; | |
f2506310 | 765 | |
faa49bfd WS |
766 | wxDocument *doc = GetDocument(); |
767 | if (!doc) return; | |
c801d85f | 768 | |
250ab35a JS |
769 | wxString label = doc->GetUserReadableName(); |
770 | if (doc->IsModified()) | |
771 | { | |
772 | label += "*"; | |
773 | } | |
774 | win->SetLabel(label); | |
c801d85f KB |
775 | } |
776 | ||
777 | void wxView::SetDocument(wxDocument *doc) | |
778 | { | |
0fb67cd1 VZ |
779 | m_viewDocument = doc; |
780 | if (doc) | |
781 | doc->AddView(this); | |
c801d85f KB |
782 | } |
783 | ||
784 | bool wxView::Close(bool deleteWindow) | |
785 | { | |
c77049a0 | 786 | return OnClose(deleteWindow); |
c801d85f KB |
787 | } |
788 | ||
789 | void wxView::Activate(bool activate) | |
790 | { | |
bb28b477 | 791 | if (GetDocument() && GetDocumentManager()) |
0fb67cd1 VZ |
792 | { |
793 | OnActivateView(activate, this, GetDocumentManager()->GetCurrentView()); | |
794 | GetDocumentManager()->ActivateView(this, activate); | |
795 | } | |
c801d85f KB |
796 | } |
797 | ||
798 | bool wxView::OnClose(bool WXUNUSED(deleteWindow)) | |
799 | { | |
68379eaf | 800 | return GetDocument() ? GetDocument()->Close() : true; |
c801d85f KB |
801 | } |
802 | ||
47d67540 | 803 | #if wxUSE_PRINTING_ARCHITECTURE |
0fb67cd1 | 804 | wxPrintout *wxView::OnCreatePrintout() |
c801d85f | 805 | { |
0fb67cd1 | 806 | return new wxDocPrintout(this); |
c801d85f | 807 | } |
6de2f8b9 | 808 | #endif // wxUSE_PRINTING_ARCHITECTURE |
c801d85f | 809 | |
0fb67cd1 VZ |
810 | // ---------------------------------------------------------------------------- |
811 | // wxDocTemplate | |
812 | // ---------------------------------------------------------------------------- | |
c801d85f | 813 | |
0fb67cd1 VZ |
814 | wxDocTemplate::wxDocTemplate(wxDocManager *manager, |
815 | const wxString& descr, | |
816 | const wxString& filter, | |
817 | const wxString& dir, | |
818 | const wxString& ext, | |
819 | const wxString& docTypeName, | |
820 | const wxString& viewTypeName, | |
821 | wxClassInfo *docClassInfo, | |
822 | wxClassInfo *viewClassInfo, | |
823 | long flags) | |
c801d85f | 824 | { |
0fb67cd1 | 825 | m_documentManager = manager; |
0fb67cd1 VZ |
826 | m_description = descr; |
827 | m_directory = dir; | |
828 | m_defaultExt = ext; | |
829 | m_fileFilter = filter; | |
830 | m_flags = flags; | |
831 | m_docTypeName = docTypeName; | |
832 | m_viewTypeName = viewTypeName; | |
833 | m_documentManager->AssociateTemplate(this); | |
c801d85f | 834 | |
0fb67cd1 VZ |
835 | m_docClassInfo = docClassInfo; |
836 | m_viewClassInfo = viewClassInfo; | |
c801d85f KB |
837 | } |
838 | ||
0fb67cd1 | 839 | wxDocTemplate::~wxDocTemplate() |
c801d85f | 840 | { |
0fb67cd1 | 841 | m_documentManager->DisassociateTemplate(this); |
c801d85f | 842 | } |
0fb67cd1 VZ |
843 | |
844 | // Tries to dynamically construct an object of the right class. | |
c801d85f KB |
845 | wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags) |
846 | { | |
4c7d530a VZ |
847 | // InitDocument() is supposed to delete the document object if its |
848 | // initialization fails so don't use wxScopedPtr<> here: this is fragile | |
849 | // but unavoidable because the default implementation uses CreateView() | |
850 | // which may -- or not -- create a wxView and if it does create it and its | |
851 | // initialization fails then the view destructor will delete the document | |
852 | // (via RemoveView()) and as we can't distinguish between the two cases we | |
853 | // just have to assume that it always deletes it in case of failure | |
854 | wxDocument * const doc = DoCreateDocument(); | |
855 | ||
856 | return doc && InitDocument(doc, path, flags) ? doc : NULL; | |
217b7140 JS |
857 | } |
858 | ||
c77049a0 VZ |
859 | bool |
860 | wxDocTemplate::InitDocument(wxDocument* doc, const wxString& path, long flags) | |
217b7140 | 861 | { |
319ff574 VS |
862 | wxScopeGuard g = wxMakeObjGuard(*doc, &wxDocument::DeleteAllViews); |
863 | ||
0fb67cd1 VZ |
864 | doc->SetFilename(path); |
865 | doc->SetDocumentTemplate(this); | |
866 | GetDocumentManager()->AddDocument(doc); | |
867 | doc->SetCommandProcessor(doc->OnCreateCommandProcessor()); | |
868 | ||
319ff574 VS |
869 | if ( !doc->OnCreate(path, flags) ) |
870 | return false; | |
89d94e04 | 871 | |
319ff574 VS |
872 | g.Dismiss(); // no need to call DeleteAllViews() anymore |
873 | ||
874 | return true; | |
c801d85f KB |
875 | } |
876 | ||
877 | wxView *wxDocTemplate::CreateView(wxDocument *doc, long flags) | |
878 | { | |
7047d798 VZ |
879 | wxScopedPtr<wxView> view(DoCreateView()); |
880 | if ( !view ) | |
c77049a0 | 881 | return NULL; |
69936aea | 882 | |
0fb67cd1 | 883 | view->SetDocument(doc); |
7047d798 | 884 | if ( !view->OnCreate(doc, flags) ) |
c77049a0 | 885 | return NULL; |
7047d798 VZ |
886 | |
887 | return view.release(); | |
c801d85f KB |
888 | } |
889 | ||
6de2f8b9 VZ |
890 | // The default (very primitive) format detection: check is the extension is |
891 | // that of the template | |
892 | bool wxDocTemplate::FileMatchesTemplate(const wxString& path) | |
893 | { | |
0f30d8e3 WS |
894 | wxStringTokenizer parser (GetFileFilter(), wxT(";")); |
895 | wxString anything = wxT ("*"); | |
896 | while (parser.HasMoreTokens()) | |
897 | { | |
898 | wxString filter = parser.GetNextToken(); | |
899 | wxString filterExt = FindExtension (filter); | |
900 | if ( filter.IsSameAs (anything) || | |
901 | filterExt.IsSameAs (anything) || | |
902 | filterExt.IsSameAs (FindExtension (path)) ) | |
903 | return true; | |
904 | } | |
6de2f8b9 VZ |
905 | return GetDefaultExtension().IsSameAs(FindExtension(path)); |
906 | } | |
907 | ||
69936aea VZ |
908 | wxDocument *wxDocTemplate::DoCreateDocument() |
909 | { | |
910 | if (!m_docClassInfo) | |
c77049a0 | 911 | return NULL; |
69936aea | 912 | |
89d94e04 | 913 | return static_cast<wxDocument *>(m_docClassInfo->CreateObject()); |
69936aea VZ |
914 | } |
915 | ||
916 | wxView *wxDocTemplate::DoCreateView() | |
917 | { | |
918 | if (!m_viewClassInfo) | |
c77049a0 | 919 | return NULL; |
69936aea | 920 | |
89d94e04 | 921 | return static_cast<wxView *>(m_viewClassInfo->CreateObject()); |
69936aea VZ |
922 | } |
923 | ||
0fb67cd1 VZ |
924 | // ---------------------------------------------------------------------------- |
925 | // wxDocManager | |
926 | // ---------------------------------------------------------------------------- | |
927 | ||
c801d85f KB |
928 | BEGIN_EVENT_TABLE(wxDocManager, wxEvtHandler) |
929 | EVT_MENU(wxID_OPEN, wxDocManager::OnFileOpen) | |
930 | EVT_MENU(wxID_CLOSE, wxDocManager::OnFileClose) | |
33a20136 | 931 | EVT_MENU(wxID_CLOSE_ALL, wxDocManager::OnFileCloseAll) |
c801d85f KB |
932 | EVT_MENU(wxID_REVERT, wxDocManager::OnFileRevert) |
933 | EVT_MENU(wxID_NEW, wxDocManager::OnFileNew) | |
934 | EVT_MENU(wxID_SAVE, wxDocManager::OnFileSave) | |
935 | EVT_MENU(wxID_SAVEAS, wxDocManager::OnFileSaveAs) | |
936 | EVT_MENU(wxID_UNDO, wxDocManager::OnUndo) | |
937 | EVT_MENU(wxID_REDO, wxDocManager::OnRedo) | |
f2506310 | 938 | |
c70da1a2 VZ |
939 | // We don't know in advance how many items can there be in the MRU files |
940 | // list so set up OnMRUFile() as a handler for all menu events and do the | |
941 | // check for the id of the menu item clicked inside it. | |
942 | EVT_MENU(wxID_ANY, wxDocManager::OnMRUFile) | |
943 | ||
f2506310 | 944 | EVT_UPDATE_UI(wxID_OPEN, wxDocManager::OnUpdateFileOpen) |
c77049a0 VZ |
945 | EVT_UPDATE_UI(wxID_CLOSE, wxDocManager::OnUpdateDisableIfNoDoc) |
946 | EVT_UPDATE_UI(wxID_CLOSE_ALL, wxDocManager::OnUpdateDisableIfNoDoc) | |
4311588b | 947 | EVT_UPDATE_UI(wxID_REVERT, wxDocManager::OnUpdateFileRevert) |
f2506310 JS |
948 | EVT_UPDATE_UI(wxID_NEW, wxDocManager::OnUpdateFileNew) |
949 | EVT_UPDATE_UI(wxID_SAVE, wxDocManager::OnUpdateFileSave) | |
4db97e24 | 950 | EVT_UPDATE_UI(wxID_SAVEAS, wxDocManager::OnUpdateFileSaveAs) |
f2506310 JS |
951 | EVT_UPDATE_UI(wxID_UNDO, wxDocManager::OnUpdateUndo) |
952 | EVT_UPDATE_UI(wxID_REDO, wxDocManager::OnUpdateRedo) | |
953 | ||
ce4169a4 | 954 | #if wxUSE_PRINTING_ARCHITECTURE |
c801d85f | 955 | EVT_MENU(wxID_PRINT, wxDocManager::OnPrint) |
c801d85f | 956 | EVT_MENU(wxID_PREVIEW, wxDocManager::OnPreview) |
b99017ce | 957 | EVT_MENU(wxID_PRINT_SETUP, wxDocManager::OnPageSetup) |
f2506310 | 958 | |
c77049a0 VZ |
959 | EVT_UPDATE_UI(wxID_PRINT, wxDocManager::OnUpdateDisableIfNoDoc) |
960 | EVT_UPDATE_UI(wxID_PREVIEW, wxDocManager::OnUpdateDisableIfNoDoc) | |
b27367a5 VZ |
961 | // NB: we keep "Print setup" menu item always enabled as it can be used |
962 | // even without an active document | |
b99017ce | 963 | #endif // wxUSE_PRINTING_ARCHITECTURE |
c801d85f KB |
964 | END_EVENT_TABLE() |
965 | ||
c77049a0 | 966 | wxDocManager* wxDocManager::sm_docManager = NULL; |
f2506310 | 967 | |
c77049a0 | 968 | wxDocManager::wxDocManager(long WXUNUSED(flags), bool initialize) |
c801d85f | 969 | { |
c77049a0 VZ |
970 | sm_docManager = this; |
971 | ||
0fb67cd1 | 972 | m_defaultDocumentNameCounter = 1; |
c77049a0 VZ |
973 | m_currentView = NULL; |
974 | m_maxDocsOpen = INT_MAX; | |
975 | m_fileHistory = NULL; | |
976 | if ( initialize ) | |
0fb67cd1 | 977 | Initialize(); |
c801d85f KB |
978 | } |
979 | ||
0fb67cd1 | 980 | wxDocManager::~wxDocManager() |
c801d85f | 981 | { |
0fb67cd1 | 982 | Clear(); |
c77049a0 VZ |
983 | delete m_fileHistory; |
984 | sm_docManager = NULL; | |
c801d85f KB |
985 | } |
986 | ||
b72b1920 JS |
987 | // closes the specified document |
988 | bool wxDocManager::CloseDocument(wxDocument* doc, bool force) | |
989 | { | |
89d94e04 VZ |
990 | if ( !doc->Close() && !force ) |
991 | return false; | |
b72b1920 | 992 | |
89d94e04 VZ |
993 | // Implicitly deletes the document when |
994 | // the last view is deleted | |
995 | doc->DeleteAllViews(); | |
42771621 | 996 | |
89d94e04 VZ |
997 | // Check we're really deleted |
998 | if (m_docs.Member(doc)) | |
999 | delete doc; | |
1000 | ||
1001 | return true; | |
b72b1920 JS |
1002 | } |
1003 | ||
33a20136 | 1004 | bool wxDocManager::CloseDocuments(bool force) |
c801d85f | 1005 | { |
222ed1d6 | 1006 | wxList::compatibility_iterator node = m_docs.GetFirst(); |
0fb67cd1 VZ |
1007 | while (node) |
1008 | { | |
b1d4dd7a | 1009 | wxDocument *doc = (wxDocument *)node->GetData(); |
222ed1d6 | 1010 | wxList::compatibility_iterator next = node->GetNext(); |
68379eaf | 1011 | |
b72b1920 | 1012 | if (!CloseDocument(doc, force)) |
68379eaf | 1013 | return false; |
0fb67cd1 | 1014 | |
0fb67cd1 VZ |
1015 | // This assumes that documents are not connected in |
1016 | // any way, i.e. deleting one document does NOT | |
1017 | // delete another. | |
1018 | node = next; | |
1019 | } | |
68379eaf | 1020 | return true; |
33a20136 VZ |
1021 | } |
1022 | ||
1023 | bool wxDocManager::Clear(bool force) | |
1024 | { | |
1025 | if (!CloseDocuments(force)) | |
68379eaf | 1026 | return false; |
33a20136 | 1027 | |
8af94d3b VZ |
1028 | m_currentView = NULL; |
1029 | ||
222ed1d6 | 1030 | wxList::compatibility_iterator node = m_templates.GetFirst(); |
0fb67cd1 VZ |
1031 | while (node) |
1032 | { | |
b1d4dd7a | 1033 | wxDocTemplate *templ = (wxDocTemplate*) node->GetData(); |
222ed1d6 | 1034 | wxList::compatibility_iterator next = node->GetNext(); |
c801d85f KB |
1035 | delete templ; |
1036 | node = next; | |
0fb67cd1 | 1037 | } |
68379eaf | 1038 | return true; |
c801d85f KB |
1039 | } |
1040 | ||
0fb67cd1 | 1041 | bool wxDocManager::Initialize() |
c801d85f | 1042 | { |
0fb67cd1 | 1043 | m_fileHistory = OnCreateFileHistory(); |
68379eaf | 1044 | return true; |
c801d85f KB |
1045 | } |
1046 | ||
d8efd219 VZ |
1047 | wxString wxDocManager::GetLastDirectory() const |
1048 | { | |
bba6891c | 1049 | // if we haven't determined the last used directory yet, do it now |
d8efd219 VZ |
1050 | if ( m_lastDirectory.empty() ) |
1051 | { | |
bba6891c VZ |
1052 | // we're going to modify m_lastDirectory in this const method, so do it |
1053 | // via non-const self pointer instead of const this one | |
d8efd219 | 1054 | wxDocManager * const self = const_cast<wxDocManager *>(this); |
bba6891c VZ |
1055 | |
1056 | // first try to reuse the directory of the most recently opened file: | |
1057 | // this ensures that if the user opens a file, closes the program and | |
1058 | // runs it again the "Open file" dialog will open in the directory of | |
1059 | // the last file he used | |
0571b62b | 1060 | if ( m_fileHistory && m_fileHistory->GetCount() ) |
bba6891c | 1061 | { |
0571b62b | 1062 | const wxString lastOpened = m_fileHistory->GetHistoryFile(0); |
bba6891c VZ |
1063 | const wxFileName fn(lastOpened); |
1064 | if ( fn.DirExists() ) | |
1065 | { | |
1066 | self->m_lastDirectory = fn.GetPath(); | |
1067 | } | |
1068 | //else: should we try the next one? | |
1069 | } | |
0571b62b | 1070 | //else: no history yet |
bba6891c VZ |
1071 | |
1072 | // if we don't have any files in the history (yet?), use the | |
1073 | // system-dependent default location for the document files | |
1074 | if ( m_lastDirectory.empty() ) | |
1075 | { | |
1076 | self->m_lastDirectory = wxStandardPaths::Get().GetAppDocumentsDir(); | |
1077 | } | |
d8efd219 VZ |
1078 | } |
1079 | ||
1080 | return m_lastDirectory; | |
1081 | } | |
1082 | ||
0fb67cd1 | 1083 | wxFileHistory *wxDocManager::OnCreateFileHistory() |
c801d85f | 1084 | { |
0fb67cd1 | 1085 | return new wxFileHistory; |
c801d85f KB |
1086 | } |
1087 | ||
1088 | void wxDocManager::OnFileClose(wxCommandEvent& WXUNUSED(event)) | |
1089 | { | |
0fb67cd1 | 1090 | wxDocument *doc = GetCurrentDocument(); |
568b12fa VZ |
1091 | if (doc) |
1092 | CloseDocument(doc); | |
c801d85f KB |
1093 | } |
1094 | ||
33a20136 VZ |
1095 | void wxDocManager::OnFileCloseAll(wxCommandEvent& WXUNUSED(event)) |
1096 | { | |
68379eaf | 1097 | CloseDocuments(false); |
33a20136 VZ |
1098 | } |
1099 | ||
c801d85f KB |
1100 | void wxDocManager::OnFileNew(wxCommandEvent& WXUNUSED(event)) |
1101 | { | |
b5412983 | 1102 | CreateNewDocument(); |
c801d85f KB |
1103 | } |
1104 | ||
1105 | void wxDocManager::OnFileOpen(wxCommandEvent& WXUNUSED(event)) | |
1106 | { | |
89d94e04 | 1107 | if ( !CreateDocument("") ) |
5f170f33 VZ |
1108 | { |
1109 | OnOpenFileFailure(); | |
1110 | } | |
c801d85f KB |
1111 | } |
1112 | ||
1113 | void wxDocManager::OnFileRevert(wxCommandEvent& WXUNUSED(event)) | |
1114 | { | |
0fb67cd1 VZ |
1115 | wxDocument *doc = GetCurrentDocument(); |
1116 | if (!doc) | |
1117 | return; | |
1118 | doc->Revert(); | |
c801d85f KB |
1119 | } |
1120 | ||
1121 | void wxDocManager::OnFileSave(wxCommandEvent& WXUNUSED(event)) | |
1122 | { | |
0fb67cd1 VZ |
1123 | wxDocument *doc = GetCurrentDocument(); |
1124 | if (!doc) | |
1125 | return; | |
1126 | doc->Save(); | |
c801d85f KB |
1127 | } |
1128 | ||
1129 | void wxDocManager::OnFileSaveAs(wxCommandEvent& WXUNUSED(event)) | |
1130 | { | |
0fb67cd1 VZ |
1131 | wxDocument *doc = GetCurrentDocument(); |
1132 | if (!doc) | |
1133 | return; | |
1134 | doc->SaveAs(); | |
c801d85f KB |
1135 | } |
1136 | ||
c70da1a2 VZ |
1137 | void wxDocManager::OnMRUFile(wxCommandEvent& event) |
1138 | { | |
1139 | // Check if the id is in the range assigned to MRU list entries. | |
1140 | const int id = event.GetId(); | |
1141 | if ( id >= wxID_FILE1 && | |
f50d8458 | 1142 | id < wxID_FILE1 + static_cast<int>(m_fileHistory->GetCount()) ) |
c70da1a2 VZ |
1143 | { |
1144 | DoOpenMRUFile(id - wxID_FILE1); | |
1145 | } | |
1146 | else | |
1147 | { | |
1148 | event.Skip(); | |
1149 | } | |
1150 | } | |
1151 | ||
1152 | void wxDocManager::DoOpenMRUFile(unsigned n) | |
1153 | { | |
1154 | wxString filename(GetHistoryFile(n)); | |
1155 | if ( filename.empty() ) | |
1156 | return; | |
1157 | ||
1158 | wxString errMsg; // must contain exactly one "%s" if non-empty | |
1159 | if ( wxFile::Exists(filename) ) | |
1160 | { | |
d38eb01c VZ |
1161 | // Try to open it but don't give an error if it failed: this could be |
1162 | // normal, e.g. because the user cancelled opening it, and we don't | |
1163 | // have any useful information to put in the error message anyhow, so | |
1164 | // we assume that in case of an error the appropriate message had been | |
1165 | // already logged. | |
1166 | (void)CreateDocument(filename, wxDOC_SILENT); | |
c70da1a2 VZ |
1167 | } |
1168 | else // file doesn't exist | |
1169 | { | |
d38eb01c | 1170 | OnMRUFileNotExist(n, filename); |
c70da1a2 | 1171 | } |
d38eb01c | 1172 | } |
c70da1a2 | 1173 | |
d38eb01c VZ |
1174 | void wxDocManager::OnMRUFileNotExist(unsigned n, const wxString& filename) |
1175 | { | |
c70da1a2 VZ |
1176 | // remove the file which we can't open from the MRU list |
1177 | RemoveFileFromHistory(n); | |
1178 | ||
1179 | // and tell the user about it | |
d38eb01c VZ |
1180 | wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\n" |
1181 | "It has been removed from the most recently used files list."), | |
c70da1a2 VZ |
1182 | filename); |
1183 | } | |
1184 | ||
b99017ce VZ |
1185 | #if wxUSE_PRINTING_ARCHITECTURE |
1186 | ||
c801d85f KB |
1187 | void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event)) |
1188 | { | |
575e4cd6 | 1189 | wxView *view = GetActiveView(); |
0fb67cd1 VZ |
1190 | if (!view) |
1191 | return; | |
c801d85f | 1192 | |
0fb67cd1 VZ |
1193 | wxPrintout *printout = view->OnCreatePrintout(); |
1194 | if (printout) | |
1195 | { | |
b99017ce VZ |
1196 | wxPrintDialogData printDialogData(m_pageSetupDialogData.GetPrintData()); |
1197 | wxPrinter printer(&printDialogData); | |
68379eaf | 1198 | printer.Print(view->GetFrame(), printout, true); |
c801d85f | 1199 | |
0fb67cd1 VZ |
1200 | delete printout; |
1201 | } | |
c801d85f KB |
1202 | } |
1203 | ||
b99017ce VZ |
1204 | void wxDocManager::OnPageSetup(wxCommandEvent& WXUNUSED(event)) |
1205 | { | |
1206 | wxPageSetupDialog dlg(wxTheApp->GetTopWindow(), &m_pageSetupDialogData); | |
1207 | if ( dlg.ShowModal() == wxID_OK ) | |
1208 | { | |
1209 | m_pageSetupDialogData = dlg.GetPageSetupData(); | |
1210 | } | |
1211 | } | |
1212 | ||
94dc70d1 VZ |
1213 | wxPreviewFrame* wxDocManager::CreatePreviewFrame(wxPrintPreviewBase* preview, |
1214 | wxWindow *parent, | |
1215 | const wxString& title) | |
1216 | { | |
1217 | return new wxPreviewFrame(preview, parent, title); | |
1218 | } | |
94dc70d1 | 1219 | |
c801d85f KB |
1220 | void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event)) |
1221 | { | |
84108812 | 1222 | wxBusyCursor busy; |
575e4cd6 | 1223 | wxView *view = GetActiveView(); |
0fb67cd1 VZ |
1224 | if (!view) |
1225 | return; | |
c801d85f | 1226 | |
0fb67cd1 VZ |
1227 | wxPrintout *printout = view->OnCreatePrintout(); |
1228 | if (printout) | |
1229 | { | |
b99017ce VZ |
1230 | wxPrintDialogData printDialogData(m_pageSetupDialogData.GetPrintData()); |
1231 | ||
0fb67cd1 | 1232 | // Pass two printout objects: for preview, and possible printing. |
89d94e04 | 1233 | wxPrintPreviewBase * |
b99017ce VZ |
1234 | preview = new wxPrintPreview(printout, |
1235 | view->OnCreatePrintout(), | |
1236 | &printDialogData); | |
a1b806b9 | 1237 | if ( !preview->IsOk() ) |
3f8b4a3f JS |
1238 | { |
1239 | delete preview; | |
89d94e04 | 1240 | wxLogError(_("Print preview creation failed.")); |
3f8b4a3f JS |
1241 | return; |
1242 | } | |
0fb67cd1 | 1243 | |
94dc70d1 VZ |
1244 | wxPreviewFrame* frame = CreatePreviewFrame(preview, |
1245 | wxTheApp->GetTopWindow(), | |
1246 | _("Print Preview")); | |
1247 | wxCHECK_RET( frame, "should create a print preview frame" ); | |
1248 | ||
0fb67cd1 VZ |
1249 | frame->Centre(wxBOTH); |
1250 | frame->Initialize(); | |
68379eaf | 1251 | frame->Show(true); |
0fb67cd1 | 1252 | } |
c801d85f | 1253 | } |
b99017ce | 1254 | #endif // wxUSE_PRINTING_ARCHITECTURE |
c801d85f | 1255 | |
34aa6140 | 1256 | void wxDocManager::OnUndo(wxCommandEvent& event) |
c801d85f | 1257 | { |
89d94e04 VZ |
1258 | wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor(); |
1259 | if ( !cmdproc ) | |
1260 | { | |
34aa6140 | 1261 | event.Skip(); |
89d94e04 VZ |
1262 | return; |
1263 | } | |
1264 | ||
1265 | cmdproc->Undo(); | |
c801d85f KB |
1266 | } |
1267 | ||
34aa6140 | 1268 | void wxDocManager::OnRedo(wxCommandEvent& event) |
c801d85f | 1269 | { |
89d94e04 VZ |
1270 | wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor(); |
1271 | if ( !cmdproc ) | |
1272 | { | |
34aa6140 | 1273 | event.Skip(); |
89d94e04 VZ |
1274 | return; |
1275 | } | |
1276 | ||
1277 | cmdproc->Redo(); | |
c801d85f KB |
1278 | } |
1279 | ||
f2506310 JS |
1280 | // Handlers for UI update commands |
1281 | ||
1282 | void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent& event) | |
1283 | { | |
84cae69a FM |
1284 | // CreateDocument() (which is called from OnFileOpen) may succeed |
1285 | // only when there is at least a template: | |
1286 | event.Enable( GetTemplates().GetCount()>0 ); | |
f2506310 JS |
1287 | } |
1288 | ||
c77049a0 | 1289 | void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent& event) |
f2506310 | 1290 | { |
c77049a0 | 1291 | event.Enable( GetCurrentDocument() != NULL ); |
f2506310 JS |
1292 | } |
1293 | ||
4311588b VZ |
1294 | void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent& event) |
1295 | { | |
1296 | wxDocument* doc = GetCurrentDocument(); | |
1297 | event.Enable(doc && doc->IsModified() && doc->GetDocumentSaved()); | |
1298 | } | |
1299 | ||
f2506310 JS |
1300 | void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent& event) |
1301 | { | |
84cae69a FM |
1302 | // CreateDocument() (which is called from OnFileNew) may succeed |
1303 | // only when there is at least a template: | |
1304 | event.Enable( GetTemplates().GetCount()>0 ); | |
f2506310 JS |
1305 | } |
1306 | ||
1307 | void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent& event) | |
1308 | { | |
de56f240 | 1309 | wxDocument * const doc = GetCurrentDocument(); |
4db97e24 VZ |
1310 | event.Enable( doc && !doc->IsChildDocument() && !doc->AlreadySaved() ); |
1311 | } | |
1312 | ||
1313 | void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent& event) | |
1314 | { | |
1315 | wxDocument * const doc = GetCurrentDocument(); | |
1316 | event.Enable( doc && !doc->IsChildDocument() ); | |
f2506310 JS |
1317 | } |
1318 | ||
f2506310 JS |
1319 | void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event) |
1320 | { | |
89d94e04 VZ |
1321 | wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor(); |
1322 | if ( !cmdproc ) | |
34aa6140 | 1323 | { |
af46dd7b VZ |
1324 | // If we don't have any document at all, the menu item should really be |
1325 | // disabled. | |
1326 | if ( !GetCurrentDocument() ) | |
1327 | event.Enable(false); | |
1328 | else // But if we do have it, it might handle wxID_UNDO on its own | |
1329 | event.Skip(); | |
89d94e04 | 1330 | return; |
34aa6140 | 1331 | } |
89d94e04 VZ |
1332 | event.Enable(cmdproc->CanUndo()); |
1333 | cmdproc->SetMenuStrings(); | |
f2506310 JS |
1334 | } |
1335 | ||
1336 | void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event) | |
1337 | { | |
89d94e04 VZ |
1338 | wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor(); |
1339 | if ( !cmdproc ) | |
34aa6140 | 1340 | { |
af46dd7b VZ |
1341 | // Use same logic as in OnUpdateUndo() above. |
1342 | if ( !GetCurrentDocument() ) | |
1343 | event.Enable(false); | |
1344 | else | |
1345 | event.Skip(); | |
89d94e04 | 1346 | return; |
34aa6140 | 1347 | } |
89d94e04 VZ |
1348 | event.Enable(cmdproc->CanRedo()); |
1349 | cmdproc->SetMenuStrings(); | |
f2506310 JS |
1350 | } |
1351 | ||
575e4cd6 | 1352 | wxView *wxDocManager::GetActiveView() const |
637f467a | 1353 | { |
575e4cd6 VZ |
1354 | wxView *view = GetCurrentView(); |
1355 | ||
1356 | if ( !view && !m_docs.empty() ) | |
637f467a | 1357 | { |
575e4cd6 VZ |
1358 | // if we have exactly one document, consider its view to be the current |
1359 | // one | |
1360 | // | |
1361 | // VZ: I'm not exactly sure why is this needed but this is how this | |
1362 | // code used to behave before the bug #9518 was fixed and it seems | |
1363 | // safer to preserve the old logic | |
1364 | wxList::compatibility_iterator node = m_docs.GetFirst(); | |
1365 | if ( !node->GetNext() ) | |
1366 | { | |
1367 | wxDocument *doc = static_cast<wxDocument *>(node->GetData()); | |
1368 | view = doc->GetFirstView(); | |
1369 | } | |
1370 | //else: we have more than one document | |
637f467a | 1371 | } |
575e4cd6 VZ |
1372 | |
1373 | return view; | |
637f467a JS |
1374 | } |
1375 | ||
8cc208e3 | 1376 | bool wxDocManager::TryBefore(wxEvent& event) |
637f467a | 1377 | { |
575e4cd6 | 1378 | wxView * const view = GetActiveView(); |
44070fd3 | 1379 | return view && view->ProcessEventLocally(event); |
637f467a JS |
1380 | } |
1381 | ||
c77049a0 | 1382 | namespace |
c801d85f | 1383 | { |
b1d4dd7a | 1384 | |
c77049a0 VZ |
1385 | // helper function: return only the visible templates |
1386 | wxDocTemplates GetVisibleTemplates(const wxList& allTemplates) | |
1387 | { | |
1388 | // select only the visible templates | |
1389 | const size_t totalNumTemplates = allTemplates.GetCount(); | |
1390 | wxDocTemplates templates; | |
1391 | if ( totalNumTemplates ) | |
c801d85f | 1392 | { |
c77049a0 VZ |
1393 | templates.reserve(totalNumTemplates); |
1394 | ||
1395 | for ( wxList::const_iterator i = allTemplates.begin(), | |
1396 | end = allTemplates.end(); | |
1397 | i != end; | |
1398 | ++i ) | |
0fb67cd1 | 1399 | { |
c77049a0 VZ |
1400 | wxDocTemplate * const temp = (wxDocTemplate *)*i; |
1401 | if ( temp->IsVisible() ) | |
1402 | templates.push_back(temp); | |
0fb67cd1 | 1403 | } |
c801d85f | 1404 | } |
42771621 | 1405 | |
c77049a0 VZ |
1406 | return templates; |
1407 | } | |
0fb67cd1 | 1408 | |
c77049a0 VZ |
1409 | } // anonymous namespace |
1410 | ||
8325504f VZ |
1411 | void wxDocManager::ActivateDocument(wxDocument *doc) |
1412 | { | |
1413 | wxView * const view = doc->GetFirstView(); | |
1414 | if ( !view ) | |
1415 | return; | |
1416 | ||
1417 | view->Activate(true); | |
1418 | if ( wxWindow *win = view->GetFrame() ) | |
3285e9a3 | 1419 | win->Raise(); |
8325504f VZ |
1420 | } |
1421 | ||
c77049a0 VZ |
1422 | wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags) |
1423 | { | |
1424 | // this ought to be const but SelectDocumentType/Path() are not | |
1425 | // const-correct and can't be changed as, being virtual, this risks | |
1426 | // breaking user code overriding them | |
1427 | wxDocTemplates templates(GetVisibleTemplates(m_templates)); | |
1428 | const size_t numTemplates = templates.size(); | |
1429 | if ( !numTemplates ) | |
c801d85f | 1430 | { |
c77049a0 VZ |
1431 | // no templates can be used, can't create document |
1432 | return NULL; | |
c801d85f KB |
1433 | } |
1434 | ||
42771621 | 1435 | |
c77049a0 VZ |
1436 | // normally user should select the template to use but wxDOC_SILENT flag we |
1437 | // choose one ourselves | |
1438 | wxString path = pathOrig; // may be modified below | |
1439 | wxDocTemplate *temp; | |
1440 | if ( flags & wxDOC_SILENT ) | |
1441 | { | |
1442 | wxASSERT_MSG( !path.empty(), | |
1443 | "using empty path with wxDOC_SILENT doesn't make sense" ); | |
42771621 | 1444 | |
c77049a0 VZ |
1445 | temp = FindTemplateForPath(path); |
1446 | if ( !temp ) | |
1447 | { | |
1448 | wxLogWarning(_("The format of file '%s' couldn't be determined."), | |
1449 | path); | |
0fb67cd1 | 1450 | } |
c77049a0 VZ |
1451 | } |
1452 | else // not silent, ask the user | |
1453 | { | |
1454 | // for the new file we need just the template, for an existing one we | |
1455 | // need the template and the path, unless it's already specified | |
1456 | if ( (flags & wxDOC_NEW) || !path.empty() ) | |
1457 | temp = SelectDocumentType(&templates[0], numTemplates); | |
1458 | else | |
1459 | temp = SelectDocumentPath(&templates[0], numTemplates, path, flags); | |
1460 | } | |
0fb67cd1 | 1461 | |
c77049a0 VZ |
1462 | if ( !temp ) |
1463 | return NULL; | |
42771621 | 1464 | |
c77049a0 VZ |
1465 | // check whether the document with this path is already opened |
1466 | if ( !path.empty() ) | |
1467 | { | |
1468 | const wxFileName fn(path); | |
1469 | for ( wxList::const_iterator i = m_docs.begin(); i != m_docs.end(); ++i ) | |
1470 | { | |
1471 | wxDocument * const doc = (wxDocument*)*i; | |
b72b1920 | 1472 | |
c77049a0 | 1473 | if ( fn == doc->GetFilename() ) |
0fb67cd1 | 1474 | { |
c77049a0 | 1475 | // file already open, just activate it and return |
8325504f | 1476 | ActivateDocument(doc); |
54cbdc64 | 1477 | return doc; |
0fb67cd1 | 1478 | } |
0fb67cd1 | 1479 | } |
c801d85f | 1480 | } |
c801d85f | 1481 | |
c801d85f | 1482 | |
c77049a0 | 1483 | // no, we need to create a new document |
c801d85f | 1484 | |
c77049a0 VZ |
1485 | |
1486 | // if we've reached the max number of docs, close the first one. | |
1487 | if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen ) | |
9d4a05be | 1488 | { |
c77049a0 | 1489 | if ( !CloseDocument((wxDocument *)GetDocuments().GetFirst()->GetData()) ) |
9d4a05be | 1490 | { |
c77049a0 VZ |
1491 | // can't open the new document if closing the old one failed |
1492 | return NULL; | |
9d4a05be JS |
1493 | } |
1494 | } | |
c801d85f | 1495 | |
c801d85f | 1496 | |
c77049a0 VZ |
1497 | // do create and initialize the new document finally |
1498 | wxDocument * const docNew = temp->CreateDocument(path, flags); | |
1499 | if ( !docNew ) | |
1500 | return NULL; | |
42771621 | 1501 | |
c77049a0 VZ |
1502 | docNew->SetDocumentName(temp->GetDocumentName()); |
1503 | docNew->SetDocumentTemplate(temp); | |
6489e7ee | 1504 | |
7339d7bd | 1505 | wxTRY |
c77049a0 | 1506 | { |
7339d7bd VZ |
1507 | // call the appropriate function depending on whether we're creating a |
1508 | // new file or opening an existing one | |
1509 | if ( !(flags & wxDOC_NEW ? docNew->OnNewDocument() | |
1510 | : docNew->OnOpenDocument(path)) ) | |
1511 | { | |
1512 | docNew->DeleteAllViews(); | |
1513 | return NULL; | |
1514 | } | |
c801d85f | 1515 | } |
7339d7bd | 1516 | wxCATCH_ALL( docNew->DeleteAllViews(); throw; ) |
3ca6a5f0 | 1517 | |
c77049a0 VZ |
1518 | // add the successfully opened file to MRU, but only if we're going to be |
1519 | // able to reopen it successfully later which requires the template for | |
1520 | // this document to be retrievable from the file extension | |
1521 | if ( !(flags & wxDOC_NEW) && temp->FileMatchesTemplate(path) ) | |
1522 | AddFileToHistory(path); | |
1523 | ||
8325504f VZ |
1524 | // at least under Mac (where views are top level windows) it seems to be |
1525 | // necessary to manually activate the new document to bring it to the | |
1526 | // forefront -- and it shouldn't hurt doing this under the other platforms | |
1527 | ActivateDocument(docNew); | |
1528 | ||
c77049a0 | 1529 | return docNew; |
c801d85f KB |
1530 | } |
1531 | ||
1532 | wxView *wxDocManager::CreateView(wxDocument *doc, long flags) | |
1533 | { | |
c77049a0 VZ |
1534 | wxDocTemplates templates(GetVisibleTemplates(m_templates)); |
1535 | const size_t numTemplates = templates.size(); | |
b1d4dd7a | 1536 | |
c77049a0 VZ |
1537 | if ( numTemplates == 0 ) |
1538 | return NULL; | |
0fb67cd1 | 1539 | |
c77049a0 VZ |
1540 | wxDocTemplate * const |
1541 | temp = numTemplates == 1 ? templates[0] | |
1542 | : SelectViewType(&templates[0], numTemplates); | |
1543 | ||
1544 | if ( !temp ) | |
1545 | return NULL; | |
1546 | ||
1547 | wxView *view = temp->CreateView(doc, flags); | |
1548 | if ( view ) | |
1549 | view->SetViewName(temp->GetViewName()); | |
1550 | return view; | |
c801d85f KB |
1551 | } |
1552 | ||
1553 | // Not yet implemented | |
c77049a0 VZ |
1554 | void |
1555 | wxDocManager::DeleteTemplate(wxDocTemplate *WXUNUSED(temp), long WXUNUSED(flags)) | |
c801d85f KB |
1556 | { |
1557 | } | |
1558 | ||
1559 | // Not yet implemented | |
1560 | bool wxDocManager::FlushDoc(wxDocument *WXUNUSED(doc)) | |
1561 | { | |
68379eaf | 1562 | return false; |
c801d85f KB |
1563 | } |
1564 | ||
6de2f8b9 | 1565 | wxDocument *wxDocManager::GetCurrentDocument() const |
c801d85f | 1566 | { |
575e4cd6 | 1567 | wxView * const view = GetActiveView(); |
89d94e04 VZ |
1568 | return view ? view->GetDocument() : NULL; |
1569 | } | |
1570 | ||
1571 | wxCommandProcessor *wxDocManager::GetCurrentCommandProcessor() const | |
1572 | { | |
1573 | wxDocument * const doc = GetCurrentDocument(); | |
1574 | return doc ? doc->GetCommandProcessor() : NULL; | |
c801d85f KB |
1575 | } |
1576 | ||
724b119a VZ |
1577 | // Make a default name for a new document |
1578 | #if WXWIN_COMPATIBILITY_2_8 | |
1579 | bool wxDocManager::MakeDefaultName(wxString& WXUNUSED(name)) | |
1580 | { | |
1581 | // we consider that this function can only be overridden by the user code, | |
1582 | // not called by it as it only makes sense to call it internally, so we | |
1583 | // don't bother to return anything from here | |
1584 | return false; | |
1585 | } | |
1586 | #endif // WXWIN_COMPATIBILITY_2_8 | |
1587 | ||
1588 | wxString wxDocManager::MakeNewDocumentName() | |
c801d85f | 1589 | { |
724b119a | 1590 | wxString name; |
53c6e7cc | 1591 | |
724b119a VZ |
1592 | #if WXWIN_COMPATIBILITY_2_8 |
1593 | if ( !MakeDefaultName(name) ) | |
1594 | #endif // WXWIN_COMPATIBILITY_2_8 | |
1595 | { | |
1596 | name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter); | |
1597 | m_defaultDocumentNameCounter++; | |
1598 | } | |
1599 | ||
1600 | return name; | |
c801d85f KB |
1601 | } |
1602 | ||
f2506310 JS |
1603 | // Make a frame title (override this to do something different) |
1604 | // If docName is empty, a document is not currently active. | |
1605 | wxString wxDocManager::MakeFrameTitle(wxDocument* doc) | |
1606 | { | |
9cf3d218 | 1607 | wxString appName = wxTheApp->GetAppDisplayName(); |
f2506310 JS |
1608 | wxString title; |
1609 | if (!doc) | |
1610 | title = appName; | |
1611 | else | |
1612 | { | |
724b119a | 1613 | wxString docName = doc->GetUserReadableName(); |
f2506310 JS |
1614 | title = docName + wxString(_(" - ")) + appName; |
1615 | } | |
1616 | return title; | |
1617 | } | |
1618 | ||
1619 | ||
c801d85f KB |
1620 | // Not yet implemented |
1621 | wxDocTemplate *wxDocManager::MatchTemplate(const wxString& WXUNUSED(path)) | |
1622 | { | |
c77049a0 | 1623 | return NULL; |
c801d85f KB |
1624 | } |
1625 | ||
1626 | // File history management | |
1627 | void wxDocManager::AddFileToHistory(const wxString& file) | |
1628 | { | |
0fb67cd1 VZ |
1629 | if (m_fileHistory) |
1630 | m_fileHistory->AddFileToHistory(file); | |
c801d85f KB |
1631 | } |
1632 | ||
e49c85af | 1633 | void wxDocManager::RemoveFileFromHistory(size_t i) |
0c5d3e1c VZ |
1634 | { |
1635 | if (m_fileHistory) | |
1636 | m_fileHistory->RemoveFileFromHistory(i); | |
1637 | } | |
1638 | ||
e49c85af | 1639 | wxString wxDocManager::GetHistoryFile(size_t i) const |
c801d85f | 1640 | { |
0fb67cd1 VZ |
1641 | wxString histFile; |
1642 | ||
1643 | if (m_fileHistory) | |
1644 | histFile = m_fileHistory->GetHistoryFile(i); | |
1645 | ||
1646 | return histFile; | |
c801d85f KB |
1647 | } |
1648 | ||
1649 | void wxDocManager::FileHistoryUseMenu(wxMenu *menu) | |
1650 | { | |
0fb67cd1 VZ |
1651 | if (m_fileHistory) |
1652 | m_fileHistory->UseMenu(menu); | |
c801d85f KB |
1653 | } |
1654 | ||
7f555861 | 1655 | void wxDocManager::FileHistoryRemoveMenu(wxMenu *menu) |
c801d85f | 1656 | { |
0fb67cd1 VZ |
1657 | if (m_fileHistory) |
1658 | m_fileHistory->RemoveMenu(menu); | |
c801d85f KB |
1659 | } |
1660 | ||
702ca7c0 | 1661 | #if wxUSE_CONFIG |
9c8116f8 | 1662 | void wxDocManager::FileHistoryLoad(const wxConfigBase& config) |
c801d85f | 1663 | { |
0fb67cd1 VZ |
1664 | if (m_fileHistory) |
1665 | m_fileHistory->Load(config); | |
7f555861 JS |
1666 | } |
1667 | ||
1668 | void wxDocManager::FileHistorySave(wxConfigBase& config) | |
1669 | { | |
0fb67cd1 VZ |
1670 | if (m_fileHistory) |
1671 | m_fileHistory->Save(config); | |
7f555861 | 1672 | } |
ac57418f | 1673 | #endif |
7f555861 JS |
1674 | |
1675 | void wxDocManager::FileHistoryAddFilesToMenu(wxMenu* menu) | |
1676 | { | |
0fb67cd1 VZ |
1677 | if (m_fileHistory) |
1678 | m_fileHistory->AddFilesToMenu(menu); | |
7f555861 JS |
1679 | } |
1680 | ||
1681 | void wxDocManager::FileHistoryAddFilesToMenu() | |
1682 | { | |
0fb67cd1 VZ |
1683 | if (m_fileHistory) |
1684 | m_fileHistory->AddFilesToMenu(); | |
c801d85f KB |
1685 | } |
1686 | ||
7af6b69e | 1687 | size_t wxDocManager::GetHistoryFilesCount() const |
c801d85f | 1688 | { |
7af6b69e | 1689 | return m_fileHistory ? m_fileHistory->GetCount() : 0; |
c801d85f KB |
1690 | } |
1691 | ||
1692 | ||
6de2f8b9 VZ |
1693 | // Find out the document template via matching in the document file format |
1694 | // against that of the template | |
c801d85f KB |
1695 | wxDocTemplate *wxDocManager::FindTemplateForPath(const wxString& path) |
1696 | { | |
c77049a0 | 1697 | wxDocTemplate *theTemplate = NULL; |
c801d85f | 1698 | |
0fb67cd1 | 1699 | // Find the template which this extension corresponds to |
b1d4dd7a | 1700 | for (size_t i = 0; i < m_templates.GetCount(); i++) |
c801d85f | 1701 | { |
b1d4dd7a | 1702 | wxDocTemplate *temp = (wxDocTemplate *)m_templates.Item(i)->GetData(); |
6de2f8b9 | 1703 | if ( temp->FileMatchesTemplate(path) ) |
0fb67cd1 VZ |
1704 | { |
1705 | theTemplate = temp; | |
1706 | break; | |
1707 | } | |
c801d85f | 1708 | } |
0fb67cd1 | 1709 | return theTemplate; |
c801d85f KB |
1710 | } |
1711 | ||
1712 | // Prompts user to open a file, using file specs in templates. | |
9d4a05be JS |
1713 | // Must extend the file selector dialog or implement own; OR |
1714 | // match the extension to the template extension. | |
df875e59 | 1715 | |
c801d85f | 1716 | wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, |
0fb67cd1 VZ |
1717 | int noTemplates, |
1718 | wxString& path, | |
1719 | long WXUNUSED(flags), | |
1720 | bool WXUNUSED(save)) | |
c801d85f | 1721 | { |
89d94e04 | 1722 | #ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS |
ba681060 VZ |
1723 | wxString descrBuf; |
1724 | ||
89d94e04 | 1725 | for (int i = 0; i < noTemplates; i++) |
c801d85f | 1726 | { |
ba681060 VZ |
1727 | if (templates[i]->IsVisible()) |
1728 | { | |
1729 | // add a '|' to separate this filter from the previous one | |
b494c48b | 1730 | if ( !descrBuf.empty() ) |
223d09f6 | 1731 | descrBuf << wxT('|'); |
ba681060 VZ |
1732 | |
1733 | descrBuf << templates[i]->GetDescription() | |
223d09f6 | 1734 | << wxT(" (") << templates[i]->GetFileFilter() << wxT(") |") |
0fb67cd1 | 1735 | << templates[i]->GetFileFilter(); |
ba681060 | 1736 | } |
c801d85f | 1737 | } |
a4294b78 | 1738 | #else |
223d09f6 | 1739 | wxString descrBuf = wxT("*.*"); |
c77049a0 | 1740 | wxUnusedVar(noTemplates); |
a4294b78 | 1741 | #endif |
c801d85f | 1742 | |
3ca6a5f0 | 1743 | int FilterIndex = -1; |
f6bcfd97 | 1744 | |
f6fe9f9c | 1745 | wxString pathTmp = wxFileSelectorEx(_("Open File"), |
d8efd219 | 1746 | GetLastDirectory(), |
b494c48b | 1747 | wxEmptyString, |
6de2f8b9 | 1748 | &FilterIndex, |
97aa1a74 VZ |
1749 | descrBuf, |
1750 | wxFD_OPEN | wxFD_FILE_MUST_EXIST); | |
ba681060 | 1751 | |
c77049a0 | 1752 | wxDocTemplate *theTemplate = NULL; |
b494c48b | 1753 | if (!pathTmp.empty()) |
0fb67cd1 | 1754 | { |
f6bcfd97 BP |
1755 | if (!wxFileExists(pathTmp)) |
1756 | { | |
1757 | wxString msgTitle; | |
9cf3d218 VZ |
1758 | if (!wxTheApp->GetAppDisplayName().empty()) |
1759 | msgTitle = wxTheApp->GetAppDisplayName(); | |
f6bcfd97 BP |
1760 | else |
1761 | msgTitle = wxString(_("File error")); | |
02718e6a | 1762 | |
89d94e04 VZ |
1763 | wxMessageBox(_("Sorry, could not open this file."), |
1764 | msgTitle, | |
b6e96faf | 1765 | wxOK | wxICON_EXCLAMATION | wxCENTRE); |
f6bcfd97 | 1766 | |
b494c48b | 1767 | path = wxEmptyString; |
c77049a0 | 1768 | return NULL; |
f6bcfd97 | 1769 | } |
d8efd219 VZ |
1770 | |
1771 | SetLastDirectory(wxPathOnly(pathTmp)); | |
ac0ac824 | 1772 | |
0fb67cd1 | 1773 | path = pathTmp; |
0fb67cd1 | 1774 | |
3ca6a5f0 BP |
1775 | // first choose the template using the extension, if this fails (i.e. |
1776 | // wxFileSelectorEx() didn't fill it), then use the path | |
1777 | if ( FilterIndex != -1 ) | |
6de2f8b9 | 1778 | theTemplate = templates[FilterIndex]; |
3ca6a5f0 BP |
1779 | if ( !theTemplate ) |
1780 | theTemplate = FindTemplateForPath(path); | |
9d4a05be JS |
1781 | if ( !theTemplate ) |
1782 | { | |
89d94e04 VZ |
1783 | // Since we do not add files with non-default extensions to the |
1784 | // file history this can only happen if the application changes the | |
1785 | // allowed templates in runtime. | |
1786 | wxMessageBox(_("Sorry, the format for this file is unknown."), | |
1787 | _("Open File"), | |
b6e96faf | 1788 | wxOK | wxICON_EXCLAMATION | wxCENTRE); |
9d4a05be | 1789 | } |
0fb67cd1 VZ |
1790 | } |
1791 | else | |
1792 | { | |
89d94e04 | 1793 | path.clear(); |
0fb67cd1 | 1794 | } |
3ca6a5f0 BP |
1795 | |
1796 | return theTemplate; | |
c801d85f KB |
1797 | } |
1798 | ||
1799 | wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates, | |
52b9ca21 | 1800 | int noTemplates, bool sort) |
0fb67cd1 | 1801 | { |
222ed1d6 | 1802 | wxArrayString strings; |
89d94e04 | 1803 | wxScopedArray<wxDocTemplate *> data(new wxDocTemplate *[noTemplates]); |
0fb67cd1 VZ |
1804 | int i; |
1805 | int n = 0; | |
42771621 VZ |
1806 | |
1807 | for (i = 0; i < noTemplates; i++) | |
1808 | { | |
1809 | if (templates[i]->IsVisible()) | |
1810 | { | |
1811 | int j; | |
68379eaf | 1812 | bool want = true; |
42771621 VZ |
1813 | for (j = 0; j < n; j++) |
1814 | { | |
bb28b477 | 1815 | //filter out NOT unique documents + view combinations |
42771621 | 1816 | if ( templates[i]->m_docTypeName == data[j]->m_docTypeName && |
bb28b477 JS |
1817 | templates[i]->m_viewTypeName == data[j]->m_viewTypeName |
1818 | ) | |
68379eaf | 1819 | want = false; |
42771621 | 1820 | } |
bb28b477 JS |
1821 | |
1822 | if ( want ) | |
42771621 VZ |
1823 | { |
1824 | strings.Add(templates[i]->m_description); | |
1825 | ||
1826 | data[n] = templates[i]; | |
1827 | n ++; | |
1828 | } | |
1829 | } | |
1830 | } // for | |
1831 | ||
1832 | if (sort) | |
1833 | { | |
a2a03d78 | 1834 | strings.Sort(); // ascending sort |
42771621 VZ |
1835 | // Yes, this will be slow, but template lists |
1836 | // are typically short. | |
1837 | int j; | |
1838 | n = strings.Count(); | |
1839 | for (i = 0; i < n; i++) | |
1840 | { | |
1841 | for (j = 0; j < noTemplates; j++) | |
1842 | { | |
1843 | if (strings[i] == templates[j]->m_description) | |
1844 | data[i] = templates[j]; | |
1845 | } | |
1846 | } | |
1847 | } | |
17260efd VZ |
1848 | |
1849 | wxDocTemplate *theTemplate; | |
1850 | ||
1851 | switch ( n ) | |
0fb67cd1 | 1852 | { |
17260efd VZ |
1853 | case 0: |
1854 | // no visible templates, hence nothing to choose from | |
1855 | theTemplate = NULL; | |
1856 | break; | |
0fb67cd1 | 1857 | |
17260efd | 1858 | case 1: |
c77049a0 | 1859 | // don't propose the user to choose if he has no choice |
17260efd VZ |
1860 | theTemplate = data[0]; |
1861 | break; | |
1862 | ||
1863 | default: | |
1864 | // propose the user to choose one of several | |
1865 | theTemplate = (wxDocTemplate *)wxGetSingleChoiceData | |
1866 | ( | |
1867 | _("Select a document template"), | |
1868 | _("Templates"), | |
1869 | strings, | |
b6e96faf | 1870 | (void **)data.get() |
17260efd VZ |
1871 | ); |
1872 | } | |
f6bcfd97 | 1873 | |
0fb67cd1 | 1874 | return theTemplate; |
c801d85f KB |
1875 | } |
1876 | ||
1877 | wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates, | |
52b9ca21 | 1878 | int noTemplates, bool sort) |
c801d85f | 1879 | { |
222ed1d6 | 1880 | wxArrayString strings; |
89d94e04 | 1881 | wxScopedArray<wxDocTemplate *> data(new wxDocTemplate *[noTemplates]); |
0fb67cd1 VZ |
1882 | int i; |
1883 | int n = 0; | |
42771621 | 1884 | |
0fb67cd1 | 1885 | for (i = 0; i < noTemplates; i++) |
c801d85f | 1886 | { |
17260efd VZ |
1887 | wxDocTemplate *templ = templates[i]; |
1888 | if ( templ->IsVisible() && !templ->GetViewName().empty() ) | |
0fb67cd1 | 1889 | { |
42771621 | 1890 | int j; |
68379eaf | 1891 | bool want = true; |
42771621 VZ |
1892 | for (j = 0; j < n; j++) |
1893 | { | |
bb28b477 | 1894 | //filter out NOT unique views |
42771621 | 1895 | if ( templates[i]->m_viewTypeName == data[j]->m_viewTypeName ) |
68379eaf | 1896 | want = false; |
42771621 | 1897 | } |
bb28b477 JS |
1898 | |
1899 | if ( want ) | |
1900 | { | |
42771621 VZ |
1901 | strings.Add(templ->m_viewTypeName); |
1902 | data[n] = templ; | |
1903 | n ++; | |
1904 | } | |
0fb67cd1 | 1905 | } |
c801d85f | 1906 | } |
f6bcfd97 | 1907 | |
42771621 VZ |
1908 | if (sort) |
1909 | { | |
a2a03d78 | 1910 | strings.Sort(); // ascending sort |
42771621 VZ |
1911 | // Yes, this will be slow, but template lists |
1912 | // are typically short. | |
1913 | int j; | |
1914 | n = strings.Count(); | |
1915 | for (i = 0; i < n; i++) | |
1916 | { | |
1917 | for (j = 0; j < noTemplates; j++) | |
1918 | { | |
1919 | if (strings[i] == templates[j]->m_viewTypeName) | |
1920 | data[i] = templates[j]; | |
1921 | } | |
1922 | } | |
1923 | } | |
8658ef93 | 1924 | |
17260efd VZ |
1925 | wxDocTemplate *theTemplate; |
1926 | ||
1927 | // the same logic as above | |
1928 | switch ( n ) | |
1929 | { | |
1930 | case 0: | |
c77049a0 | 1931 | theTemplate = NULL; |
17260efd VZ |
1932 | break; |
1933 | ||
1934 | case 1: | |
1935 | theTemplate = data[0]; | |
1936 | break; | |
1937 | ||
1938 | default: | |
1939 | theTemplate = (wxDocTemplate *)wxGetSingleChoiceData | |
1940 | ( | |
1941 | _("Select a document view"), | |
1942 | _("Views"), | |
1943 | strings, | |
b6e96faf | 1944 | (void **)data.get() |
17260efd VZ |
1945 | ); |
1946 | ||
1947 | } | |
1948 | ||
0fb67cd1 | 1949 | return theTemplate; |
c801d85f KB |
1950 | } |
1951 | ||
1952 | void wxDocManager::AssociateTemplate(wxDocTemplate *temp) | |
1953 | { | |
0fb67cd1 VZ |
1954 | if (!m_templates.Member(temp)) |
1955 | m_templates.Append(temp); | |
c801d85f KB |
1956 | } |
1957 | ||
1958 | void wxDocManager::DisassociateTemplate(wxDocTemplate *temp) | |
1959 | { | |
0fb67cd1 | 1960 | m_templates.DeleteObject(temp); |
c801d85f KB |
1961 | } |
1962 | ||
a70d268a VZ |
1963 | wxDocTemplate* wxDocManager::FindTemplate(const wxClassInfo* classinfo) |
1964 | { | |
1965 | for ( wxList::compatibility_iterator node = m_templates.GetFirst(); | |
1966 | node; | |
1967 | node = node->GetNext() ) | |
1968 | { | |
1969 | wxDocTemplate* t = wxStaticCast(node->GetData(), wxDocTemplate); | |
1970 | if ( t->GetDocClassInfo() == classinfo ) | |
1971 | return t; | |
1972 | } | |
1973 | ||
1974 | return NULL; | |
1975 | } | |
1976 | ||
c801d85f KB |
1977 | // Add and remove a document from the manager's list |
1978 | void wxDocManager::AddDocument(wxDocument *doc) | |
1979 | { | |
0fb67cd1 VZ |
1980 | if (!m_docs.Member(doc)) |
1981 | m_docs.Append(doc); | |
c801d85f KB |
1982 | } |
1983 | ||
1984 | void wxDocManager::RemoveDocument(wxDocument *doc) | |
1985 | { | |
0fb67cd1 | 1986 | m_docs.DeleteObject(doc); |
c801d85f KB |
1987 | } |
1988 | ||
1989 | // Views or windows should inform the document manager | |
1990 | // when a view is going in or out of focus | |
18759f78 VZ |
1991 | void wxDocManager::ActivateView(wxView *view, bool activate) |
1992 | { | |
1993 | if ( activate ) | |
0fb67cd1 | 1994 | { |
18759f78 VZ |
1995 | m_currentView = view; |
1996 | } | |
1997 | else // deactivate | |
1998 | { | |
1999 | if ( m_currentView == view ) | |
2000 | { | |
2001 | // don't keep stale pointer | |
c77049a0 | 2002 | m_currentView = NULL; |
18759f78 | 2003 | } |
0fb67cd1 | 2004 | } |
c801d85f KB |
2005 | } |
2006 | ||
0fb67cd1 | 2007 | // ---------------------------------------------------------------------------- |
a9e2e6e5 | 2008 | // wxDocChildFrameAnyBase |
0fb67cd1 | 2009 | // ---------------------------------------------------------------------------- |
c801d85f | 2010 | |
a9e2e6e5 | 2011 | bool wxDocChildFrameAnyBase::CloseView(wxCloseEvent& event) |
c801d85f | 2012 | { |
8cc208e3 VZ |
2013 | if ( m_childView ) |
2014 | { | |
cf30eac2 VZ |
2015 | // notice that we must call wxView::Close() and OnClose() called from |
2016 | // it in any case, even if we know that we are going to close anyhow | |
2017 | if ( !m_childView->Close(false) && event.CanVeto() ) | |
a9e2e6e5 VZ |
2018 | { |
2019 | event.Veto(); | |
2020 | return false; | |
2021 | } | |
89d94e04 | 2022 | |
a9e2e6e5 | 2023 | m_childView->Activate(false); |
ada1ff0d VZ |
2024 | |
2025 | // it is important to reset m_childView frame pointer to NULL before | |
2026 | // deleting it because while normally it is the frame which deletes the | |
2027 | // view when it's closed, the view also closes the frame if it is | |
2028 | // deleted directly not by us as indicated by its doc child frame | |
2029 | // pointer still being set | |
2030 | m_childView->SetDocChildFrame(NULL); | |
5276b0a5 | 2031 | wxDELETE(m_childView); |
89d94e04 | 2032 | } |
387a3b02 | 2033 | |
89d94e04 | 2034 | m_childDocument = NULL; |
0fb67cd1 | 2035 | |
a9e2e6e5 | 2036 | return true; |
c801d85f KB |
2037 | } |
2038 | ||
0fb67cd1 | 2039 | // ---------------------------------------------------------------------------- |
c48bdb0c | 2040 | // wxDocParentFrameAnyBase |
0fb67cd1 | 2041 | // ---------------------------------------------------------------------------- |
c801d85f | 2042 | |
47d67540 | 2043 | #if wxUSE_PRINTING_ARCHITECTURE |
c801d85f | 2044 | |
302674fe VZ |
2045 | namespace |
2046 | { | |
2047 | ||
2048 | wxString GetAppropriateTitle(const wxView *view, const wxString& titleGiven) | |
2049 | { | |
2050 | wxString title(titleGiven); | |
2051 | if ( title.empty() ) | |
2052 | { | |
2053 | if ( view && view->GetDocument() ) | |
2054 | title = view->GetDocument()->GetUserReadableName(); | |
2055 | else | |
2056 | title = _("Printout"); | |
2057 | } | |
2058 | ||
2059 | return title; | |
2060 | } | |
2061 | ||
2062 | } // anonymous namespace | |
2063 | ||
0fb67cd1 | 2064 | wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title) |
302674fe | 2065 | : wxPrintout(GetAppropriateTitle(view, title)) |
c801d85f | 2066 | { |
0fb67cd1 | 2067 | m_printoutView = view; |
c801d85f KB |
2068 | } |
2069 | ||
2070 | bool wxDocPrintout::OnPrintPage(int WXUNUSED(page)) | |
2071 | { | |
0fb67cd1 VZ |
2072 | wxDC *dc = GetDC(); |
2073 | ||
2074 | // Get the logical pixels per inch of screen and printer | |
2075 | int ppiScreenX, ppiScreenY; | |
2076 | GetPPIScreen(&ppiScreenX, &ppiScreenY); | |
999836aa | 2077 | wxUnusedVar(ppiScreenY); |
0fb67cd1 VZ |
2078 | int ppiPrinterX, ppiPrinterY; |
2079 | GetPPIPrinter(&ppiPrinterX, &ppiPrinterY); | |
999836aa | 2080 | wxUnusedVar(ppiPrinterY); |
0fb67cd1 VZ |
2081 | |
2082 | // This scales the DC so that the printout roughly represents the | |
2083 | // the screen scaling. The text point size _should_ be the right size | |
2084 | // but in fact is too small for some reason. This is a detail that will | |
2085 | // need to be addressed at some point but can be fudged for the | |
2086 | // moment. | |
2087 | float scale = (float)((float)ppiPrinterX/(float)ppiScreenX); | |
2088 | ||
2089 | // Now we have to check in case our real page size is reduced | |
2090 | // (e.g. because we're drawing to a print preview memory DC) | |
2091 | int pageWidth, pageHeight; | |
2092 | int w, h; | |
2093 | dc->GetSize(&w, &h); | |
2094 | GetPageSizePixels(&pageWidth, &pageHeight); | |
999836aa | 2095 | wxUnusedVar(pageHeight); |
0fb67cd1 VZ |
2096 | |
2097 | // If printer pageWidth == current DC width, then this doesn't | |
2098 | // change. But w might be the preview bitmap width, so scale down. | |
2099 | float overallScale = scale * (float)(w/(float)pageWidth); | |
2100 | dc->SetUserScale(overallScale, overallScale); | |
2101 | ||
2102 | if (m_printoutView) | |
2103 | { | |
2104 | m_printoutView->OnDraw(dc); | |
2105 | } | |
68379eaf | 2106 | return true; |
c801d85f KB |
2107 | } |
2108 | ||
2109 | bool wxDocPrintout::HasPage(int pageNum) | |
2110 | { | |
0fb67cd1 | 2111 | return (pageNum == 1); |
c801d85f KB |
2112 | } |
2113 | ||
2114 | bool wxDocPrintout::OnBeginDocument(int startPage, int endPage) | |
2115 | { | |
0fb67cd1 | 2116 | if (!wxPrintout::OnBeginDocument(startPage, endPage)) |
68379eaf | 2117 | return false; |
c801d85f | 2118 | |
68379eaf | 2119 | return true; |
c801d85f KB |
2120 | } |
2121 | ||
89d94e04 VZ |
2122 | void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage, |
2123 | int *selPageFrom, int *selPageTo) | |
c801d85f | 2124 | { |
0fb67cd1 VZ |
2125 | *minPage = 1; |
2126 | *maxPage = 1; | |
2127 | *selPageFrom = 1; | |
2128 | *selPageTo = 1; | |
c801d85f KB |
2129 | } |
2130 | ||
0fb67cd1 | 2131 | #endif // wxUSE_PRINTING_ARCHITECTURE |
c801d85f | 2132 | |
0fb67cd1 VZ |
2133 | // ---------------------------------------------------------------------------- |
2134 | // Permits compatibility with existing file formats and functions that | |
2135 | // manipulate files directly | |
2136 | // ---------------------------------------------------------------------------- | |
c801d85f | 2137 | |
a533f5c1 | 2138 | #if wxUSE_STD_IOSTREAM |
fb6e5b17 | 2139 | |
dd107c50 | 2140 | bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream) |
c801d85f | 2141 | { |
ce0822e1 | 2142 | #if wxUSE_FFILE |
9a83f860 | 2143 | wxFFile file(filename, wxT("rb")); |
ce0822e1 VZ |
2144 | #elif wxUSE_FILE |
2145 | wxFile file(filename, wxFile::read); | |
2146 | #endif | |
fb6e5b17 | 2147 | if ( !file.IsOpened() ) |
68379eaf | 2148 | return false; |
c801d85f | 2149 | |
fb6e5b17 VZ |
2150 | char buf[4096]; |
2151 | ||
2152 | size_t nRead; | |
2153 | do | |
2154 | { | |
2155 | nRead = file.Read(buf, WXSIZEOF(buf)); | |
2156 | if ( file.Error() ) | |
68379eaf | 2157 | return false; |
fb6e5b17 VZ |
2158 | |
2159 | stream.write(buf, nRead); | |
2160 | if ( !stream ) | |
68379eaf | 2161 | return false; |
fb6e5b17 VZ |
2162 | } |
2163 | while ( !file.Eof() ); | |
c801d85f | 2164 | |
68379eaf | 2165 | return true; |
c801d85f KB |
2166 | } |
2167 | ||
dd107c50 | 2168 | bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename) |
c801d85f | 2169 | { |
ce0822e1 | 2170 | #if wxUSE_FFILE |
9a83f860 | 2171 | wxFFile file(filename, wxT("wb")); |
ce0822e1 VZ |
2172 | #elif wxUSE_FILE |
2173 | wxFile file(filename, wxFile::write); | |
2174 | #endif | |
fb6e5b17 | 2175 | if ( !file.IsOpened() ) |
68379eaf | 2176 | return false; |
c801d85f | 2177 | |
fb6e5b17 VZ |
2178 | char buf[4096]; |
2179 | do | |
0fb67cd1 | 2180 | { |
fb6e5b17 VZ |
2181 | stream.read(buf, WXSIZEOF(buf)); |
2182 | if ( !stream.bad() ) // fail may be set on EOF, don't use operator!() | |
2183 | { | |
2184 | if ( !file.Write(buf, stream.gcount()) ) | |
68379eaf | 2185 | return false; |
fb6e5b17 | 2186 | } |
0fb67cd1 | 2187 | } |
fb6e5b17 VZ |
2188 | while ( !stream.eof() ); |
2189 | ||
68379eaf | 2190 | return true; |
c801d85f | 2191 | } |
fb6e5b17 VZ |
2192 | |
2193 | #else // !wxUSE_STD_IOSTREAM | |
2194 | ||
dc1efb1d JS |
2195 | bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream) |
2196 | { | |
ce0822e1 | 2197 | #if wxUSE_FFILE |
9a83f860 | 2198 | wxFFile file(filename, wxT("rb")); |
ce0822e1 VZ |
2199 | #elif wxUSE_FILE |
2200 | wxFile file(filename, wxFile::read); | |
2201 | #endif | |
fb6e5b17 | 2202 | if ( !file.IsOpened() ) |
68379eaf | 2203 | return false; |
dc1efb1d | 2204 | |
fb6e5b17 VZ |
2205 | char buf[4096]; |
2206 | ||
2207 | size_t nRead; | |
2208 | do | |
2209 | { | |
2210 | nRead = file.Read(buf, WXSIZEOF(buf)); | |
2211 | if ( file.Error() ) | |
68379eaf | 2212 | return false; |
fb6e5b17 VZ |
2213 | |
2214 | stream.Write(buf, nRead); | |
2215 | if ( !stream ) | |
68379eaf | 2216 | return false; |
fb6e5b17 VZ |
2217 | } |
2218 | while ( !file.Eof() ); | |
dc1efb1d | 2219 | |
68379eaf | 2220 | return true; |
dc1efb1d JS |
2221 | } |
2222 | ||
2223 | bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename) | |
2224 | { | |
ce0822e1 | 2225 | #if wxUSE_FFILE |
9a83f860 | 2226 | wxFFile file(filename, wxT("wb")); |
ce0822e1 VZ |
2227 | #elif wxUSE_FILE |
2228 | wxFile file(filename, wxFile::write); | |
2229 | #endif | |
fb6e5b17 | 2230 | if ( !file.IsOpened() ) |
68379eaf | 2231 | return false; |
dc1efb1d | 2232 | |
fb6e5b17 | 2233 | char buf[4096]; |
07f87b6b | 2234 | for ( ;; ) |
dc1efb1d | 2235 | { |
fb6e5b17 VZ |
2236 | stream.Read(buf, WXSIZEOF(buf)); |
2237 | ||
2238 | const size_t nRead = stream.LastRead(); | |
07f87b6b VZ |
2239 | if ( !nRead ) |
2240 | { | |
2241 | if ( stream.Eof() ) | |
2242 | break; | |
2243 | ||
2244 | return false; | |
2245 | } | |
2246 | ||
2247 | if ( !file.Write(buf, nRead) ) | |
68379eaf | 2248 | return false; |
dc1efb1d | 2249 | } |
fb6e5b17 | 2250 | |
68379eaf | 2251 | return true; |
dc1efb1d | 2252 | } |
fb6e5b17 VZ |
2253 | |
2254 | #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM | |
c801d85f | 2255 | |
0fb67cd1 | 2256 | #endif // wxUSE_DOC_VIEW_ARCHITECTURE |