]> git.saurik.com Git - wxWidgets.git/blob - src/common/docview.cpp
Update with recent changes.
[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 #include "wx/ffile.h"
45 #endif
46
47 #ifdef __WXMAC__
48 #include "wx/filename.h"
49 #endif
50
51 #ifdef __WXGTK__
52 #include "wx/mdi.h"
53 #endif
54
55 #if wxUSE_PRINTING_ARCHITECTURE
56 #include "wx/prntbase.h"
57 #include "wx/printdlg.h"
58 #endif
59
60 #include "wx/msgdlg.h"
61 #include "wx/choicdlg.h"
62 #include "wx/docview.h"
63 #include "wx/confbase.h"
64 #include "wx/file.h"
65 #include "wx/cmdproc.h"
66
67 #include <stdio.h>
68 #include <string.h>
69
70 #if wxUSE_STD_IOSTREAM
71 #include "wx/ioswrap.h"
72 #if wxUSE_IOSTREAMH
73 #include <fstream.h>
74 #else
75 #include <fstream>
76 #endif
77 #else
78 #include "wx/wfstream.h"
79 #endif
80
81 // ----------------------------------------------------------------------------
82 // wxWindows macros
83 // ----------------------------------------------------------------------------
84
85 IMPLEMENT_ABSTRACT_CLASS(wxDocument, wxEvtHandler)
86 IMPLEMENT_ABSTRACT_CLASS(wxView, wxEvtHandler)
87 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate, wxObject)
88 IMPLEMENT_DYNAMIC_CLASS(wxDocManager, wxEvtHandler)
89 IMPLEMENT_CLASS(wxDocChildFrame, wxFrame)
90 IMPLEMENT_CLASS(wxDocParentFrame, wxFrame)
91
92 #if wxUSE_PRINTING_ARCHITECTURE
93 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout, wxPrintout)
94 #endif
95
96 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory, wxObject)
97
98 // ----------------------------------------------------------------------------
99 // function prototypes
100 // ----------------------------------------------------------------------------
101
102 static inline wxString FindExtension(const wxChar *path);
103
104 // ----------------------------------------------------------------------------
105 // local constants
106 // ----------------------------------------------------------------------------
107
108 static const wxChar *s_MRUEntryFormat = wxT("&%d %s");
109
110 // ============================================================================
111 // implementation
112 // ============================================================================
113
114 // ----------------------------------------------------------------------------
115 // local functions
116 // ----------------------------------------------------------------------------
117
118 static wxString FindExtension(const wxChar *path)
119 {
120 wxString ext;
121 wxSplitPath(path, NULL, NULL, &ext);
122
123 // VZ: extensions are considered not case sensitive - is this really a good
124 // idea?
125 return ext.MakeLower();
126 }
127
128 // ----------------------------------------------------------------------------
129 // Definition of wxDocument
130 // ----------------------------------------------------------------------------
131
132 wxDocument::wxDocument(wxDocument *parent)
133 {
134 m_documentModified = FALSE;
135 m_documentParent = parent;
136 m_documentTemplate = (wxDocTemplate *) NULL;
137 m_commandProcessor = (wxCommandProcessor*) NULL;
138 m_savedYet = FALSE;
139 }
140
141 bool wxDocument::DeleteContents()
142 {
143 return TRUE;
144 }
145
146 wxDocument::~wxDocument()
147 {
148 DeleteContents();
149
150 if (m_commandProcessor)
151 delete m_commandProcessor;
152
153 if (GetDocumentManager())
154 GetDocumentManager()->RemoveDocument(this);
155
156 // Not safe to do here, since it'll invoke virtual view functions
157 // expecting to see valid derived objects: and by the time we get here,
158 // we've called destructors higher up.
159 //DeleteAllViews();
160 }
161
162 bool wxDocument::Close()
163 {
164 if (OnSaveModified())
165 return OnCloseDocument();
166 else
167 return FALSE;
168 }
169
170 bool wxDocument::OnCloseDocument()
171 {
172 // Tell all views that we're about to close
173 NotifyClosing();
174 DeleteContents();
175 Modify(FALSE);
176 return TRUE;
177 }
178
179 // Note that this implicitly deletes the document when the last view is
180 // deleted.
181 bool wxDocument::DeleteAllViews()
182 {
183 wxDocManager* manager = GetDocumentManager();
184
185 wxNode *node = m_documentViews.GetFirst();
186 while (node)
187 {
188 wxView *view = (wxView *)node->GetData();
189 if (!view->Close())
190 return FALSE;
191
192 wxNode *next = node->GetNext();
193
194 delete view; // Deletes node implicitly
195 node = next;
196 }
197 // If we haven't yet deleted the document (for example
198 // if there were no views) then delete it.
199 if (manager && manager->GetDocuments().Member(this))
200 delete this;
201
202 return TRUE;
203 }
204
205 wxView *wxDocument::GetFirstView() const
206 {
207 if (m_documentViews.GetCount() == 0)
208 return (wxView *) NULL;
209 return (wxView *)m_documentViews.GetFirst()->GetData();
210 }
211
212 wxDocManager *wxDocument::GetDocumentManager() const
213 {
214 return (m_documentTemplate ? m_documentTemplate->GetDocumentManager() : (wxDocManager*) NULL);
215 }
216
217 bool wxDocument::OnNewDocument()
218 {
219 if (!OnSaveModified())
220 return FALSE;
221
222 if (OnCloseDocument()==FALSE) return FALSE;
223 DeleteContents();
224 Modify(FALSE);
225 SetDocumentSaved(FALSE);
226
227 wxString name;
228 GetDocumentManager()->MakeDefaultName(name);
229 SetTitle(name);
230 SetFilename(name, TRUE);
231
232 return TRUE;
233 }
234
235 bool wxDocument::Save()
236 {
237 if (!IsModified() && m_savedYet)
238 return TRUE;
239
240 if ( m_documentFile.empty() || !m_savedYet )
241 return SaveAs();
242
243 return OnSaveDocument(m_documentFile);
244 }
245
246 bool wxDocument::SaveAs()
247 {
248 wxDocTemplate *docTemplate = GetDocumentTemplate();
249 if (!docTemplate)
250 return FALSE;
251
252 wxString tmp = wxFileSelector(_("Save as"),
253 docTemplate->GetDirectory(),
254 wxFileNameFromPath(GetFilename()),
255 docTemplate->GetDefaultExtension(),
256 docTemplate->GetFileFilter(),
257 wxSAVE | wxOVERWRITE_PROMPT,
258 GetDocumentWindow());
259
260 if (tmp.IsEmpty())
261 return FALSE;
262
263 wxString fileName(tmp);
264 wxString path, name, ext;
265 wxSplitPath(fileName, & path, & name, & ext);
266
267 if (ext.IsEmpty() || ext == wxT(""))
268 {
269 fileName += wxT(".");
270 fileName += docTemplate->GetDefaultExtension();
271 }
272
273 SetFilename(fileName);
274 SetTitle(wxFileNameFromPath(fileName));
275
276 GetDocumentManager()->AddFileToHistory(fileName);
277
278 // Notify the views that the filename has changed
279 wxNode *node = m_documentViews.GetFirst();
280 while (node)
281 {
282 wxView *view = (wxView *)node->GetData();
283 view->OnChangeFilename();
284 node = node->GetNext();
285 }
286
287 return OnSaveDocument(m_documentFile);
288 }
289
290 bool wxDocument::OnSaveDocument(const wxString& file)
291 {
292 if ( !file )
293 return FALSE;
294
295 wxString msgTitle;
296 if (wxTheApp->GetAppName() != wxT(""))
297 msgTitle = wxTheApp->GetAppName();
298 else
299 msgTitle = wxString(_("File error"));
300
301 #if wxUSE_STD_IOSTREAM
302 wxSTD ofstream store(file.mb_str());
303 if (store.fail() || store.bad())
304 #else
305 wxFileOutputStream store(file);
306 if (store.GetLastError() != wxSTREAM_NO_ERROR)
307 #endif
308 {
309 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle, wxOK | wxICON_EXCLAMATION,
310 GetDocumentWindow());
311 // Saving error
312 return FALSE;
313 }
314 if (!SaveObject(store))
315 {
316 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle, wxOK | wxICON_EXCLAMATION,
317 GetDocumentWindow());
318 // Saving error
319 return FALSE;
320 }
321 Modify(FALSE);
322 SetFilename(file);
323 SetDocumentSaved(TRUE);
324 #ifdef __WXMAC__
325 wxFileName fn(file) ;
326 fn.MacSetDefaultTypeAndCreator() ;
327 #endif
328 return TRUE;
329 }
330
331 bool wxDocument::OnOpenDocument(const wxString& file)
332 {
333 if (!OnSaveModified())
334 return FALSE;
335
336 wxString msgTitle;
337 if (wxTheApp->GetAppName() != wxT(""))
338 msgTitle = wxTheApp->GetAppName();
339 else
340 msgTitle = wxString(_("File error"));
341
342 #if wxUSE_STD_IOSTREAM
343 wxSTD ifstream store(file.mb_str());
344 if (store.fail() || store.bad())
345 #else
346 wxFileInputStream store(file);
347 if (store.GetLastError() != wxSTREAM_NO_ERROR)
348 #endif
349 {
350 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK|wxICON_EXCLAMATION,
351 GetDocumentWindow());
352 return FALSE;
353 }
354 #if wxUSE_STD_IOSTREAM
355 LoadObject(store);
356 if ( !store && !store.eof() )
357 #else
358 int res = LoadObject(store).GetLastError();
359 if ((res != wxSTREAM_NO_ERROR) &&
360 (res != wxSTREAM_EOF))
361 #endif
362 {
363 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK|wxICON_EXCLAMATION,
364 GetDocumentWindow());
365 return FALSE;
366 }
367 SetFilename(file, TRUE);
368 Modify(FALSE);
369 m_savedYet = TRUE;
370
371 UpdateAllViews();
372
373 return TRUE;
374 }
375
376 #if wxUSE_STD_IOSTREAM
377 wxSTD istream& wxDocument::LoadObject(wxSTD istream& stream)
378 #else
379 wxInputStream& wxDocument::LoadObject(wxInputStream& stream)
380 #endif
381 {
382 return stream;
383 }
384
385 #if wxUSE_STD_IOSTREAM
386 wxSTD ostream& wxDocument::SaveObject(wxSTD ostream& stream)
387 #else
388 wxOutputStream& wxDocument::SaveObject(wxOutputStream& stream)
389 #endif
390 {
391 return stream;
392 }
393
394 bool wxDocument::Revert()
395 {
396 return FALSE;
397 }
398
399
400 // Get title, or filename if no title, else unnamed
401 bool wxDocument::GetPrintableName(wxString& buf) const
402 {
403 if (m_documentTitle != wxT(""))
404 {
405 buf = m_documentTitle;
406 return TRUE;
407 }
408 else if (m_documentFile != wxT(""))
409 {
410 buf = wxFileNameFromPath(m_documentFile);
411 return TRUE;
412 }
413 else
414 {
415 buf = _("unnamed");
416 return TRUE;
417 }
418 }
419
420 wxWindow *wxDocument::GetDocumentWindow() const
421 {
422 wxView *view = GetFirstView();
423 if (view)
424 return view->GetFrame();
425 else
426 return wxTheApp->GetTopWindow();
427 }
428
429 wxCommandProcessor *wxDocument::OnCreateCommandProcessor()
430 {
431 return new wxCommandProcessor;
432 }
433
434 // TRUE if safe to close
435 bool wxDocument::OnSaveModified()
436 {
437 if (IsModified())
438 {
439 wxString title;
440 GetPrintableName(title);
441
442 wxString msgTitle;
443 if (wxTheApp->GetAppName() != wxT(""))
444 msgTitle = wxTheApp->GetAppName();
445 else
446 msgTitle = wxString(_("Warning"));
447
448 wxString prompt;
449 prompt.Printf(_("Do you want to save changes to document %s?"),
450 (const wxChar *)title);
451 int res = wxMessageBox(prompt, msgTitle,
452 wxYES_NO|wxCANCEL|wxICON_QUESTION,
453 GetDocumentWindow());
454 if (res == wxNO)
455 {
456 Modify(FALSE);
457 return TRUE;
458 }
459 else if (res == wxYES)
460 return Save();
461 else if (res == wxCANCEL)
462 return FALSE;
463 }
464 return TRUE;
465 }
466
467 bool wxDocument::Draw(wxDC& WXUNUSED(context))
468 {
469 return TRUE;
470 }
471
472 bool wxDocument::AddView(wxView *view)
473 {
474 if (!m_documentViews.Member(view))
475 {
476 m_documentViews.Append(view);
477 OnChangedViewList();
478 }
479 return TRUE;
480 }
481
482 bool wxDocument::RemoveView(wxView *view)
483 {
484 (void)m_documentViews.DeleteObject(view);
485 OnChangedViewList();
486 return TRUE;
487 }
488
489 bool wxDocument::OnCreate(const wxString& WXUNUSED(path), long flags)
490 {
491 if (GetDocumentTemplate()->CreateView(this, flags))
492 return TRUE;
493 else
494 return FALSE;
495 }
496
497 // Called after a view is added or removed.
498 // The default implementation deletes the document if
499 // there are no more views.
500 void wxDocument::OnChangedViewList()
501 {
502 if (m_documentViews.GetCount() == 0)
503 {
504 if (OnSaveModified())
505 {
506 delete this;
507 }
508 }
509 }
510
511 void wxDocument::UpdateAllViews(wxView *sender, wxObject *hint)
512 {
513 wxNode *node = m_documentViews.GetFirst();
514 while (node)
515 {
516 wxView *view = (wxView *)node->GetData();
517 if (view != sender)
518 view->OnUpdate(sender, hint);
519 node = node->GetNext();
520 }
521 }
522
523 void wxDocument::NotifyClosing()
524 {
525 wxNode *node = m_documentViews.GetFirst();
526 while (node)
527 {
528 wxView *view = (wxView *)node->GetData();
529 view->OnClosingDocument();
530 node = node->GetNext();
531 }
532 }
533
534 void wxDocument::SetFilename(const wxString& filename, bool notifyViews)
535 {
536 m_documentFile = filename;
537 if ( notifyViews )
538 {
539 // Notify the views that the filename has changed
540 wxNode *node = m_documentViews.GetFirst();
541 while (node)
542 {
543 wxView *view = (wxView *)node->GetData();
544 view->OnChangeFilename();
545 node = node->GetNext();
546 }
547 }
548 }
549
550 // ----------------------------------------------------------------------------
551 // Document view
552 // ----------------------------------------------------------------------------
553
554 wxView::wxView()
555 {
556 // SetDocument(doc);
557 m_viewDocument = (wxDocument*) NULL;
558
559 m_viewTypeName = wxT("");
560 m_viewFrame = (wxFrame *) NULL;
561 }
562
563 wxView::~wxView()
564 {
565 // GetDocumentManager()->ActivateView(this, FALSE, TRUE);
566 m_viewDocument->RemoveView(this);
567 }
568
569 // Extend event processing to search the document's event table
570 bool wxView::ProcessEvent(wxEvent& event)
571 {
572 if ( !GetDocument() || !GetDocument()->ProcessEvent(event) )
573 return wxEvtHandler::ProcessEvent(event);
574 else
575 return TRUE;
576 }
577
578 void wxView::OnActivateView(bool WXUNUSED(activate), wxView *WXUNUSED(activeView), wxView *WXUNUSED(deactiveView))
579 {
580 }
581
582 void wxView::OnPrint(wxDC *dc, wxObject *WXUNUSED(info))
583 {
584 OnDraw(dc);
585 }
586
587 void wxView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
588 {
589 }
590
591 void wxView::OnChangeFilename()
592 {
593 if (GetFrame() && GetDocument())
594 {
595 wxString title;
596
597 GetDocument()->GetPrintableName(title);
598
599 GetFrame()->SetTitle(title);
600 }
601 }
602
603 void wxView::SetDocument(wxDocument *doc)
604 {
605 m_viewDocument = doc;
606 if (doc)
607 doc->AddView(this);
608 }
609
610 bool wxView::Close(bool deleteWindow)
611 {
612 if (OnClose(deleteWindow))
613 return TRUE;
614 else
615 return FALSE;
616 }
617
618 void wxView::Activate(bool activate)
619 {
620 if (GetDocument() && GetDocumentManager())
621 {
622 OnActivateView(activate, this, GetDocumentManager()->GetCurrentView());
623 GetDocumentManager()->ActivateView(this, activate);
624 }
625 }
626
627 bool wxView::OnClose(bool WXUNUSED(deleteWindow))
628 {
629 return GetDocument() ? GetDocument()->Close() : TRUE;
630 }
631
632 #if wxUSE_PRINTING_ARCHITECTURE
633 wxPrintout *wxView::OnCreatePrintout()
634 {
635 return new wxDocPrintout(this);
636 }
637 #endif // wxUSE_PRINTING_ARCHITECTURE
638
639 // ----------------------------------------------------------------------------
640 // wxDocTemplate
641 // ----------------------------------------------------------------------------
642
643 wxDocTemplate::wxDocTemplate(wxDocManager *manager,
644 const wxString& descr,
645 const wxString& filter,
646 const wxString& dir,
647 const wxString& ext,
648 const wxString& docTypeName,
649 const wxString& viewTypeName,
650 wxClassInfo *docClassInfo,
651 wxClassInfo *viewClassInfo,
652 long flags)
653 {
654 m_documentManager = manager;
655 m_description = descr;
656 m_directory = dir;
657 m_defaultExt = ext;
658 m_fileFilter = filter;
659 m_flags = flags;
660 m_docTypeName = docTypeName;
661 m_viewTypeName = viewTypeName;
662 m_documentManager->AssociateTemplate(this);
663
664 m_docClassInfo = docClassInfo;
665 m_viewClassInfo = viewClassInfo;
666 }
667
668 wxDocTemplate::~wxDocTemplate()
669 {
670 m_documentManager->DisassociateTemplate(this);
671 }
672
673 // Tries to dynamically construct an object of the right class.
674 wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags)
675 {
676 if (!m_docClassInfo)
677 return (wxDocument *) NULL;
678 wxDocument *doc = (wxDocument *)m_docClassInfo->CreateObject();
679 doc->SetFilename(path);
680 doc->SetDocumentTemplate(this);
681 GetDocumentManager()->AddDocument(doc);
682 doc->SetCommandProcessor(doc->OnCreateCommandProcessor());
683
684 if (doc->OnCreate(path, flags))
685 return doc;
686 else
687 {
688 if (GetDocumentManager()->GetDocuments().Member(doc))
689 doc->DeleteAllViews();
690 return (wxDocument *) NULL;
691 }
692 }
693
694 wxView *wxDocTemplate::CreateView(wxDocument *doc, long flags)
695 {
696 if (!m_viewClassInfo)
697 return (wxView *) NULL;
698 wxView *view = (wxView *)m_viewClassInfo->CreateObject();
699 view->SetDocument(doc);
700 if (view->OnCreate(doc, flags))
701 {
702 return view;
703 }
704 else
705 {
706 delete view;
707 return (wxView *) NULL;
708 }
709 }
710
711 // The default (very primitive) format detection: check is the extension is
712 // that of the template
713 bool wxDocTemplate::FileMatchesTemplate(const wxString& path)
714 {
715 return GetDefaultExtension().IsSameAs(FindExtension(path));
716 }
717
718 // ----------------------------------------------------------------------------
719 // wxDocManager
720 // ----------------------------------------------------------------------------
721
722 BEGIN_EVENT_TABLE(wxDocManager, wxEvtHandler)
723 EVT_MENU(wxID_OPEN, wxDocManager::OnFileOpen)
724 EVT_MENU(wxID_CLOSE, wxDocManager::OnFileClose)
725 EVT_MENU(wxID_CLOSE_ALL, wxDocManager::OnFileCloseAll)
726 EVT_MENU(wxID_REVERT, wxDocManager::OnFileRevert)
727 EVT_MENU(wxID_NEW, wxDocManager::OnFileNew)
728 EVT_MENU(wxID_SAVE, wxDocManager::OnFileSave)
729 EVT_MENU(wxID_SAVEAS, wxDocManager::OnFileSaveAs)
730 EVT_MENU(wxID_UNDO, wxDocManager::OnUndo)
731 EVT_MENU(wxID_REDO, wxDocManager::OnRedo)
732
733 EVT_UPDATE_UI(wxID_OPEN, wxDocManager::OnUpdateFileOpen)
734 EVT_UPDATE_UI(wxID_CLOSE, wxDocManager::OnUpdateFileClose)
735 EVT_UPDATE_UI(wxID_CLOSE_ALL, wxDocManager::OnUpdateFileClose)
736 EVT_UPDATE_UI(wxID_REVERT, wxDocManager::OnUpdateFileRevert)
737 EVT_UPDATE_UI(wxID_NEW, wxDocManager::OnUpdateFileNew)
738 EVT_UPDATE_UI(wxID_SAVE, wxDocManager::OnUpdateFileSave)
739 EVT_UPDATE_UI(wxID_SAVEAS, wxDocManager::OnUpdateFileSaveAs)
740 EVT_UPDATE_UI(wxID_UNDO, wxDocManager::OnUpdateUndo)
741 EVT_UPDATE_UI(wxID_REDO, wxDocManager::OnUpdateRedo)
742
743 #if wxUSE_PRINTING_ARCHITECTURE
744 EVT_MENU(wxID_PRINT, wxDocManager::OnPrint)
745 EVT_MENU(wxID_PRINT_SETUP, wxDocManager::OnPrintSetup)
746 EVT_MENU(wxID_PREVIEW, wxDocManager::OnPreview)
747
748 EVT_UPDATE_UI(wxID_PRINT, wxDocManager::OnUpdatePrint)
749 EVT_UPDATE_UI(wxID_PRINT_SETUP, wxDocManager::OnUpdatePrintSetup)
750 EVT_UPDATE_UI(wxID_PREVIEW, wxDocManager::OnUpdatePreview)
751 #endif
752 END_EVENT_TABLE()
753
754 wxDocManager* wxDocManager::sm_docManager = (wxDocManager*) NULL;
755
756 wxDocManager::wxDocManager(long flags, bool initialize)
757 {
758 m_defaultDocumentNameCounter = 1;
759 m_flags = flags;
760 m_currentView = (wxView *) NULL;
761 m_maxDocsOpen = 10000;
762 m_fileHistory = (wxFileHistory *) NULL;
763 if (initialize)
764 Initialize();
765 sm_docManager = this;
766 }
767
768 wxDocManager::~wxDocManager()
769 {
770 Clear();
771 if (m_fileHistory)
772 delete m_fileHistory;
773 sm_docManager = (wxDocManager*) NULL;
774 }
775
776 bool wxDocManager::CloseDocuments(bool force)
777 {
778 wxNode *node = m_docs.GetFirst();
779 while (node)
780 {
781 wxDocument *doc = (wxDocument *)node->GetData();
782 wxNode *next = node->GetNext();
783
784 if (!doc->Close() && !force)
785 return FALSE;
786
787 // Implicitly deletes the document when the last
788 // view is removed (deleted)
789 doc->DeleteAllViews();
790
791 // Check document is deleted
792 if (m_docs.Member(doc))
793 delete doc;
794
795 // This assumes that documents are not connected in
796 // any way, i.e. deleting one document does NOT
797 // delete another.
798 node = next;
799 }
800 return TRUE;
801 }
802
803 bool wxDocManager::Clear(bool force)
804 {
805 if (!CloseDocuments(force))
806 return FALSE;
807
808 wxNode *node = m_templates.GetFirst();
809 while (node)
810 {
811 wxDocTemplate *templ = (wxDocTemplate*) node->GetData();
812 wxNode* next = node->GetNext();
813 delete templ;
814 node = next;
815 }
816 return TRUE;
817 }
818
819 bool wxDocManager::Initialize()
820 {
821 m_fileHistory = OnCreateFileHistory();
822 return TRUE;
823 }
824
825 wxFileHistory *wxDocManager::OnCreateFileHistory()
826 {
827 return new wxFileHistory;
828 }
829
830 void wxDocManager::OnFileClose(wxCommandEvent& WXUNUSED(event))
831 {
832 wxDocument *doc = GetCurrentDocument();
833 if (!doc)
834 return;
835 if (doc->Close())
836 {
837 doc->DeleteAllViews();
838 if (m_docs.Member(doc))
839 delete doc;
840 }
841 }
842
843 void wxDocManager::OnFileCloseAll(wxCommandEvent& WXUNUSED(event))
844 {
845 CloseDocuments(FALSE);
846 }
847
848 void wxDocManager::OnFileNew(wxCommandEvent& WXUNUSED(event))
849 {
850 CreateDocument( wxT(""), wxDOC_NEW );
851 }
852
853 void wxDocManager::OnFileOpen(wxCommandEvent& WXUNUSED(event))
854 {
855 if ( !CreateDocument( wxT(""), 0) )
856 {
857 OnOpenFileFailure();
858 }
859 }
860
861 void wxDocManager::OnFileRevert(wxCommandEvent& WXUNUSED(event))
862 {
863 wxDocument *doc = GetCurrentDocument();
864 if (!doc)
865 return;
866 doc->Revert();
867 }
868
869 void wxDocManager::OnFileSave(wxCommandEvent& WXUNUSED(event))
870 {
871 wxDocument *doc = GetCurrentDocument();
872 if (!doc)
873 return;
874 doc->Save();
875 }
876
877 void wxDocManager::OnFileSaveAs(wxCommandEvent& WXUNUSED(event))
878 {
879 wxDocument *doc = GetCurrentDocument();
880 if (!doc)
881 return;
882 doc->SaveAs();
883 }
884
885 void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event))
886 {
887 #if wxUSE_PRINTING_ARCHITECTURE
888 wxView *view = GetCurrentView();
889 if (!view)
890 return;
891
892 wxPrintout *printout = view->OnCreatePrintout();
893 if (printout)
894 {
895 wxPrinter printer;
896 printer.Print(view->GetFrame(), printout, TRUE);
897
898 delete printout;
899 }
900 #endif // wxUSE_PRINTING_ARCHITECTURE
901 }
902
903 void wxDocManager::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
904 {
905 #if wxUSE_PRINTING_ARCHITECTURE
906 wxWindow *parentWin = wxTheApp->GetTopWindow();
907 wxView *view = GetCurrentView();
908 if (view)
909 parentWin = view->GetFrame();
910
911 wxPrintDialogData data;
912
913 wxPrintDialog printerDialog(parentWin, &data);
914 printerDialog.GetPrintDialogData().SetSetupDialog(TRUE);
915 printerDialog.ShowModal();
916 #endif // wxUSE_PRINTING_ARCHITECTURE
917 }
918
919 void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event))
920 {
921 #if wxUSE_PRINTING_ARCHITECTURE
922 wxView *view = GetCurrentView();
923 if (!view)
924 return;
925
926 wxPrintout *printout = view->OnCreatePrintout();
927 if (printout)
928 {
929 // Pass two printout objects: for preview, and possible printing.
930 wxPrintPreviewBase *preview = (wxPrintPreviewBase *) NULL;
931 preview = new wxPrintPreview(printout, view->OnCreatePrintout());
932
933 wxPreviewFrame *frame = new wxPreviewFrame(preview, (wxFrame *)wxTheApp->GetTopWindow(), _("Print Preview"),
934 wxPoint(100, 100), wxSize(600, 650));
935 frame->Centre(wxBOTH);
936 frame->Initialize();
937 frame->Show(TRUE);
938 }
939 #endif // wxUSE_PRINTING_ARCHITECTURE
940 }
941
942 void wxDocManager::OnUndo(wxCommandEvent& WXUNUSED(event))
943 {
944 wxDocument *doc = GetCurrentDocument();
945 if (!doc)
946 return;
947 if (doc->GetCommandProcessor())
948 doc->GetCommandProcessor()->Undo();
949 }
950
951 void wxDocManager::OnRedo(wxCommandEvent& WXUNUSED(event))
952 {
953 wxDocument *doc = GetCurrentDocument();
954 if (!doc)
955 return;
956 if (doc->GetCommandProcessor())
957 doc->GetCommandProcessor()->Redo();
958 }
959
960 // Handlers for UI update commands
961
962 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent& event)
963 {
964 event.Enable( TRUE );
965 }
966
967 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent& event)
968 {
969 wxDocument *doc = GetCurrentDocument();
970 event.Enable( (doc != (wxDocument*) NULL) );
971 }
972
973 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent& event)
974 {
975 wxDocument *doc = GetCurrentDocument();
976 event.Enable( (doc != (wxDocument*) NULL) );
977 }
978
979 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent& event)
980 {
981 event.Enable( TRUE );
982 }
983
984 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent& event)
985 {
986 wxDocument *doc = GetCurrentDocument();
987 event.Enable( doc && doc->IsModified() );
988 }
989
990 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent& event)
991 {
992 wxDocument *doc = GetCurrentDocument();
993 event.Enable( (doc != (wxDocument*) NULL) );
994 }
995
996 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event)
997 {
998 wxDocument *doc = GetCurrentDocument();
999 event.Enable( (doc && doc->GetCommandProcessor() && doc->GetCommandProcessor()->CanUndo()) );
1000 if (doc && doc->GetCommandProcessor())
1001 doc->GetCommandProcessor()->SetMenuStrings();
1002 }
1003
1004 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event)
1005 {
1006 wxDocument *doc = GetCurrentDocument();
1007 event.Enable( (doc && doc->GetCommandProcessor() && doc->GetCommandProcessor()->CanRedo()) );
1008 if (doc && doc->GetCommandProcessor())
1009 doc->GetCommandProcessor()->SetMenuStrings();
1010 }
1011
1012 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent& event)
1013 {
1014 wxDocument *doc = GetCurrentDocument();
1015 event.Enable( (doc != (wxDocument*) NULL) );
1016 }
1017
1018 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent& event)
1019 {
1020 event.Enable( TRUE );
1021 }
1022
1023 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent& event)
1024 {
1025 wxDocument *doc = GetCurrentDocument();
1026 event.Enable( (doc != (wxDocument*) NULL) );
1027 }
1028
1029 wxView *wxDocManager::GetCurrentView() const
1030 {
1031 if (m_currentView)
1032 return m_currentView;
1033 if (m_docs.GetCount() == 1)
1034 {
1035 wxDocument* doc = (wxDocument*) m_docs.GetFirst()->GetData();
1036 return doc->GetFirstView();
1037 }
1038 return (wxView *) NULL;
1039 }
1040
1041 // Extend event processing to search the view's event table
1042 bool wxDocManager::ProcessEvent(wxEvent& event)
1043 {
1044 wxView* view = GetCurrentView();
1045 if (view)
1046 {
1047 if (view->ProcessEvent(event))
1048 return TRUE;
1049 }
1050 return wxEvtHandler::ProcessEvent(event);
1051 }
1052
1053 wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags)
1054 {
1055 wxDocTemplate **templates = new wxDocTemplate *[m_templates.GetCount()];
1056 int n = 0;
1057
1058 for (size_t i = 0; i < m_templates.GetCount(); i++)
1059 {
1060 wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Item(i)->GetData());
1061 if (temp->IsVisible())
1062 {
1063 templates[n] = temp;
1064 n ++;
1065 }
1066 }
1067 if (n == 0)
1068 {
1069 delete[] templates;
1070 return (wxDocument *) NULL;
1071 }
1072
1073 // If we've reached the max number of docs, close the
1074 // first one.
1075 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen )
1076 {
1077 wxDocument *doc = (wxDocument *)GetDocuments().GetFirst()->GetData();
1078 if (doc->Close())
1079 {
1080 // Implicitly deletes the document when
1081 // the last view is deleted
1082 doc->DeleteAllViews();
1083
1084 // Check we're really deleted
1085 if (m_docs.Member(doc))
1086 delete doc;
1087 }
1088 else
1089 {
1090 delete[] templates;
1091 return (wxDocument *) NULL;
1092 }
1093 }
1094
1095 // New document: user chooses a template, unless there's only one.
1096 if (flags & wxDOC_NEW)
1097 {
1098 if (n == 1)
1099 {
1100 wxDocTemplate *temp = templates[0];
1101 delete[] templates;
1102 wxDocument *newDoc = temp->CreateDocument(path, flags);
1103 if (newDoc)
1104 {
1105 newDoc->SetDocumentName(temp->GetDocumentName());
1106 newDoc->SetDocumentTemplate(temp);
1107 newDoc->OnNewDocument();
1108 }
1109 return newDoc;
1110 }
1111
1112 wxDocTemplate *temp = SelectDocumentType(templates, n);
1113 delete[] templates;
1114 if (temp)
1115 {
1116 wxDocument *newDoc = temp->CreateDocument(path, flags);
1117 if (newDoc)
1118 {
1119 newDoc->SetDocumentName(temp->GetDocumentName());
1120 newDoc->SetDocumentTemplate(temp);
1121 newDoc->OnNewDocument();
1122 }
1123 return newDoc;
1124 }
1125 else
1126 return (wxDocument *) NULL;
1127 }
1128
1129 // Existing document
1130 wxDocTemplate *temp = (wxDocTemplate *) NULL;
1131
1132 wxString path2(wxT(""));
1133 if (path != wxT(""))
1134 path2 = path;
1135
1136 if (flags & wxDOC_SILENT)
1137 temp = FindTemplateForPath(path2);
1138 else
1139 temp = SelectDocumentPath(templates, n, path2, flags);
1140
1141 delete[] templates;
1142
1143 if (temp)
1144 {
1145 wxDocument *newDoc = temp->CreateDocument(path2, flags);
1146 if (newDoc)
1147 {
1148 newDoc->SetDocumentName(temp->GetDocumentName());
1149 newDoc->SetDocumentTemplate(temp);
1150 if (!newDoc->OnOpenDocument(path2))
1151 {
1152 newDoc->DeleteAllViews();
1153 // delete newDoc; // Implicitly deleted by DeleteAllViews
1154 return (wxDocument *) NULL;
1155 }
1156 AddFileToHistory(path2);
1157 }
1158 return newDoc;
1159 }
1160
1161 return (wxDocument *) NULL;
1162 }
1163
1164 wxView *wxDocManager::CreateView(wxDocument *doc, long flags)
1165 {
1166 wxDocTemplate **templates = new wxDocTemplate *[m_templates.GetCount()];
1167 int n =0;
1168
1169 for (size_t i = 0; i < m_templates.GetCount(); i++)
1170 {
1171 wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Item(i)->GetData());
1172 if (temp->IsVisible())
1173 {
1174 if (temp->GetDocumentName() == doc->GetDocumentName())
1175 {
1176 templates[n] = temp;
1177 n ++;
1178 }
1179 }
1180 }
1181 if (n == 0)
1182 {
1183 delete[] templates;
1184 return (wxView *) NULL;
1185 }
1186 if (n == 1)
1187 {
1188 wxDocTemplate *temp = templates[0];
1189 delete[] templates;
1190 wxView *view = temp->CreateView(doc, flags);
1191 if (view)
1192 view->SetViewName(temp->GetViewName());
1193 return view;
1194 }
1195
1196 wxDocTemplate *temp = SelectViewType(templates, n);
1197 delete[] templates;
1198 if (temp)
1199 {
1200 wxView *view = temp->CreateView(doc, flags);
1201 if (view)
1202 view->SetViewName(temp->GetViewName());
1203 return view;
1204 }
1205 else
1206 return (wxView *) NULL;
1207 }
1208
1209 // Not yet implemented
1210 void wxDocManager::DeleteTemplate(wxDocTemplate *WXUNUSED(temp), long WXUNUSED(flags))
1211 {
1212 }
1213
1214 // Not yet implemented
1215 bool wxDocManager::FlushDoc(wxDocument *WXUNUSED(doc))
1216 {
1217 return FALSE;
1218 }
1219
1220 wxDocument *wxDocManager::GetCurrentDocument() const
1221 {
1222 wxView *view = GetCurrentView();
1223 if (view)
1224 return view->GetDocument();
1225 else
1226 return (wxDocument *) NULL;
1227 }
1228
1229 // Make a default document name
1230 bool wxDocManager::MakeDefaultName(wxString& name)
1231 {
1232 name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter);
1233 m_defaultDocumentNameCounter++;
1234
1235 return TRUE;
1236 }
1237
1238 // Make a frame title (override this to do something different)
1239 // If docName is empty, a document is not currently active.
1240 wxString wxDocManager::MakeFrameTitle(wxDocument* doc)
1241 {
1242 wxString appName = wxTheApp->GetAppName();
1243 wxString title;
1244 if (!doc)
1245 title = appName;
1246 else
1247 {
1248 wxString docName;
1249 doc->GetPrintableName(docName);
1250 title = docName + wxString(_(" - ")) + appName;
1251 }
1252 return title;
1253 }
1254
1255
1256 // Not yet implemented
1257 wxDocTemplate *wxDocManager::MatchTemplate(const wxString& WXUNUSED(path))
1258 {
1259 return (wxDocTemplate *) NULL;
1260 }
1261
1262 // File history management
1263 void wxDocManager::AddFileToHistory(const wxString& file)
1264 {
1265 if (m_fileHistory)
1266 m_fileHistory->AddFileToHistory(file);
1267 }
1268
1269 void wxDocManager::RemoveFileFromHistory(int i)
1270 {
1271 if (m_fileHistory)
1272 m_fileHistory->RemoveFileFromHistory(i);
1273 }
1274
1275 wxString wxDocManager::GetHistoryFile(int i) const
1276 {
1277 wxString histFile;
1278
1279 if (m_fileHistory)
1280 histFile = m_fileHistory->GetHistoryFile(i);
1281
1282 return histFile;
1283 }
1284
1285 void wxDocManager::FileHistoryUseMenu(wxMenu *menu)
1286 {
1287 if (m_fileHistory)
1288 m_fileHistory->UseMenu(menu);
1289 }
1290
1291 void wxDocManager::FileHistoryRemoveMenu(wxMenu *menu)
1292 {
1293 if (m_fileHistory)
1294 m_fileHistory->RemoveMenu(menu);
1295 }
1296
1297 #if wxUSE_CONFIG
1298 void wxDocManager::FileHistoryLoad(wxConfigBase& config)
1299 {
1300 if (m_fileHistory)
1301 m_fileHistory->Load(config);
1302 }
1303
1304 void wxDocManager::FileHistorySave(wxConfigBase& config)
1305 {
1306 if (m_fileHistory)
1307 m_fileHistory->Save(config);
1308 }
1309 #endif
1310
1311 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu* menu)
1312 {
1313 if (m_fileHistory)
1314 m_fileHistory->AddFilesToMenu(menu);
1315 }
1316
1317 void wxDocManager::FileHistoryAddFilesToMenu()
1318 {
1319 if (m_fileHistory)
1320 m_fileHistory->AddFilesToMenu();
1321 }
1322
1323 int wxDocManager::GetNoHistoryFiles() const
1324 {
1325 if (m_fileHistory)
1326 return m_fileHistory->GetNoHistoryFiles();
1327 else
1328 return 0;
1329 }
1330
1331
1332 // Find out the document template via matching in the document file format
1333 // against that of the template
1334 wxDocTemplate *wxDocManager::FindTemplateForPath(const wxString& path)
1335 {
1336 wxDocTemplate *theTemplate = (wxDocTemplate *) NULL;
1337
1338 // Find the template which this extension corresponds to
1339 for (size_t i = 0; i < m_templates.GetCount(); i++)
1340 {
1341 wxDocTemplate *temp = (wxDocTemplate *)m_templates.Item(i)->GetData();
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.GetFirst();
1955 while (node)
1956 {
1957 wxMenu* menu = (wxMenu*) node->GetData();
1958 if ( m_fileHistoryN == 0 && menu->GetMenuItemCount() )
1959 {
1960 menu->AppendSeparator();
1961 }
1962 menu->Append(wxID_FILE1+m_fileHistoryN, _("[EMPTY]"));
1963 node = node->GetNext();
1964 }
1965 m_fileHistoryN ++;
1966 }
1967 // Shuffle filenames down
1968 for (i = (m_fileHistoryN-1); i > 0; i--)
1969 {
1970 m_fileHistory[i] = m_fileHistory[i-1];
1971 }
1972 m_fileHistory[0] = copystring(file);
1973
1974 // this is the directory of the last opened file
1975 wxString pathCurrent;
1976 wxSplitPath( m_fileHistory[0], &pathCurrent, NULL, NULL );
1977 for (i = 0; i < m_fileHistoryN; i++)
1978 {
1979 if ( m_fileHistory[i] )
1980 {
1981 // if in same directory just show the filename; otherwise the full
1982 // path
1983 wxString pathInMenu, path, filename, ext;
1984 wxSplitPath( m_fileHistory[i], &path, &filename, &ext );
1985 if ( path == pathCurrent )
1986 {
1987 pathInMenu = filename;
1988 if ( !ext.empty() )
1989 pathInMenu = pathInMenu + wxFILE_SEP_EXT + ext;
1990 }
1991 else
1992 {
1993 // absolute path; could also set relative path
1994 pathInMenu = m_fileHistory[i];
1995 }
1996
1997 wxString buf;
1998 buf.Printf(s_MRUEntryFormat, i + 1, pathInMenu.c_str());
1999 wxNode* node = m_fileMenus.GetFirst();
2000 while (node)
2001 {
2002 wxMenu* menu = (wxMenu*) node->GetData();
2003 menu->SetLabel(wxID_FILE1 + i, buf);
2004 node = node->GetNext();
2005 }
2006 }
2007 }
2008 }
2009
2010 void wxFileHistory::RemoveFileFromHistory(int i)
2011 {
2012 wxCHECK_RET( i < m_fileHistoryN,
2013 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2014
2015 // delete the element from the array (could use memmove() too...)
2016 delete [] m_fileHistory[i];
2017
2018 int j;
2019 for ( j = i; j < m_fileHistoryN - 1; j++ )
2020 {
2021 m_fileHistory[j] = m_fileHistory[j + 1];
2022 }
2023
2024 wxNode* node = m_fileMenus.GetFirst();
2025 while ( node )
2026 {
2027 wxMenu* menu = (wxMenu*) node->GetData();
2028
2029
2030 // shuffle filenames up
2031 wxString buf;
2032 for ( j = i; j < m_fileHistoryN - 1; j++ )
2033 {
2034 buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[j]);
2035 menu->SetLabel(wxID_FILE1 + j, buf);
2036 }
2037
2038 node = node->GetNext();
2039
2040 // delete the last menu item which is unused now
2041 if (menu->FindItem(wxID_FILE1 + m_fileHistoryN - 1))
2042 menu->Delete(wxID_FILE1 + m_fileHistoryN - 1);
2043
2044 // delete the last separator too if no more files are left
2045 if ( m_fileHistoryN == 1 )
2046 {
2047 wxMenuItemList::Node *node = menu->GetMenuItems().GetLast();
2048 if ( node )
2049 {
2050 wxMenuItem *menuItem = node->GetData();
2051 if ( menuItem->IsSeparator() )
2052 {
2053 menu->Delete(menuItem);
2054 }
2055 //else: should we search backwards for the last separator?
2056 }
2057 //else: menu is empty somehow
2058 }
2059 }
2060
2061 m_fileHistoryN--;
2062 }
2063
2064 wxString wxFileHistory::GetHistoryFile(int i) const
2065 {
2066 wxString s;
2067 if ( i < m_fileHistoryN )
2068 {
2069 s = m_fileHistory[i];
2070 }
2071 else
2072 {
2073 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2074 }
2075
2076 return s;
2077 }
2078
2079 void wxFileHistory::UseMenu(wxMenu *menu)
2080 {
2081 if (!m_fileMenus.Member(menu))
2082 m_fileMenus.Append(menu);
2083 }
2084
2085 void wxFileHistory::RemoveMenu(wxMenu *menu)
2086 {
2087 m_fileMenus.DeleteObject(menu);
2088 }
2089
2090 #if wxUSE_CONFIG
2091 void wxFileHistory::Load(wxConfigBase& config)
2092 {
2093 m_fileHistoryN = 0;
2094 wxString buf;
2095 buf.Printf(wxT("file%d"), m_fileHistoryN+1);
2096 wxString historyFile;
2097 while ((m_fileHistoryN < m_fileMaxFiles) && config.Read(buf, &historyFile) && (historyFile != wxT("")))
2098 {
2099 m_fileHistory[m_fileHistoryN] = copystring((const wxChar*) historyFile);
2100 m_fileHistoryN ++;
2101 buf.Printf(wxT("file%d"), m_fileHistoryN+1);
2102 historyFile = wxT("");
2103 }
2104 AddFilesToMenu();
2105 }
2106
2107 void wxFileHistory::Save(wxConfigBase& config)
2108 {
2109 int i;
2110 for (i = 0; i < m_fileHistoryN; i++)
2111 {
2112 wxString buf;
2113 buf.Printf(wxT("file%d"), i+1);
2114 config.Write(buf, wxString(m_fileHistory[i]));
2115 }
2116 }
2117 #endif // wxUSE_CONFIG
2118
2119 void wxFileHistory::AddFilesToMenu()
2120 {
2121 if (m_fileHistoryN > 0)
2122 {
2123 wxNode* node = m_fileMenus.GetFirst();
2124 while (node)
2125 {
2126 wxMenu* menu = (wxMenu*) node->GetData();
2127 if (menu->GetMenuItemCount())
2128 {
2129 menu->AppendSeparator();
2130 }
2131
2132 int i;
2133 for (i = 0; i < m_fileHistoryN; i++)
2134 {
2135 if (m_fileHistory[i])
2136 {
2137 wxString buf;
2138 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
2139 menu->Append(wxID_FILE1+i, buf);
2140 }
2141 }
2142 node = node->GetNext();
2143 }
2144 }
2145 }
2146
2147 void wxFileHistory::AddFilesToMenu(wxMenu* menu)
2148 {
2149 if (m_fileHistoryN > 0)
2150 {
2151 if (menu->GetMenuItemCount())
2152 {
2153 menu->AppendSeparator();
2154 }
2155
2156 int i;
2157 for (i = 0; i < m_fileHistoryN; i++)
2158 {
2159 if (m_fileHistory[i])
2160 {
2161 wxString buf;
2162 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
2163 menu->Append(wxID_FILE1+i, buf);
2164 }
2165 }
2166 }
2167 }
2168
2169 // ----------------------------------------------------------------------------
2170 // Permits compatibility with existing file formats and functions that
2171 // manipulate files directly
2172 // ----------------------------------------------------------------------------
2173
2174 #if wxUSE_STD_IOSTREAM
2175
2176 bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream)
2177 {
2178 wxFFile file(filename, _T("rb"));
2179 if ( !file.IsOpened() )
2180 return FALSE;
2181
2182 char buf[4096];
2183
2184 size_t nRead;
2185 do
2186 {
2187 nRead = file.Read(buf, WXSIZEOF(buf));
2188 if ( file.Error() )
2189 return FALSE;
2190
2191 stream.write(buf, nRead);
2192 if ( !stream )
2193 return FALSE;
2194 }
2195 while ( !file.Eof() );
2196
2197 return TRUE;
2198 }
2199
2200 bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename)
2201 {
2202 wxFFile file(filename, _T("wb"));
2203 if ( !file.IsOpened() )
2204 return FALSE;
2205
2206 char buf[4096];
2207 do
2208 {
2209 stream.read(buf, WXSIZEOF(buf));
2210 if ( !stream.bad() ) // fail may be set on EOF, don't use operator!()
2211 {
2212 if ( !file.Write(buf, stream.gcount()) )
2213 return FALSE;
2214 }
2215 }
2216 while ( !stream.eof() );
2217
2218 return TRUE;
2219 }
2220
2221 #else // !wxUSE_STD_IOSTREAM
2222
2223 bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream)
2224 {
2225 wxFFile file(filename, _T("rb"));
2226 if ( !file.IsOpened() )
2227 return FALSE;
2228
2229 char buf[4096];
2230
2231 size_t nRead;
2232 do
2233 {
2234 nRead = file.Read(buf, WXSIZEOF(buf));
2235 if ( file.Error() )
2236 return FALSE;
2237
2238 stream.Write(buf, nRead);
2239 if ( !stream )
2240 return FALSE;
2241 }
2242 while ( !file.Eof() );
2243
2244 return TRUE;
2245 }
2246
2247 bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename)
2248 {
2249 wxFFile file(filename, _T("wb"));
2250 if ( !file.IsOpened() )
2251 return FALSE;
2252
2253 char buf[4096];
2254 do
2255 {
2256 stream.Read(buf, WXSIZEOF(buf));
2257
2258 const size_t nRead = stream.LastRead();
2259 if ( !nRead || !file.Write(buf, nRead) )
2260 return FALSE;
2261 }
2262 while ( !stream.Eof() );
2263
2264 return TRUE;
2265 }
2266
2267 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2268
2269 #endif // wxUSE_DOC_VIEW_ARCHITECTURE
2270