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