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