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