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