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