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