]> git.saurik.com Git - wxWidgets.git/blob - src/common/docview.cpp
moved OLE files group under MSW files -- this is more logical
[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.GetFirst();
185 while (node)
186 {
187 wxView *view = (wxView *)node->GetData();
188 if (!view->Close())
189 return FALSE;
190
191 wxNode *next = node->GetNext();
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.GetCount() == 0)
207 return (wxView *) NULL;
208 return (wxView *)m_documentViews.GetFirst()->GetData();
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.GetFirst();
279 while (node)
280 {
281 wxView *view = (wxView *)node->GetData();
282 view->OnChangeFilename();
283 node = node->GetNext();
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.GetCount() == 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.GetFirst();
513 while (node)
514 {
515 wxView *view = (wxView *)node->GetData();
516 if (view != sender)
517 view->OnUpdate(sender, hint);
518 node = node->GetNext();
519 }
520 }
521
522 void wxDocument::NotifyClosing()
523 {
524 wxNode *node = m_documentViews.GetFirst();
525 while (node)
526 {
527 wxView *view = (wxView *)node->GetData();
528 view->OnClosingDocument();
529 node = node->GetNext();
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.GetFirst();
540 while (node)
541 {
542 wxView *view = (wxView *)node->GetData();
543 view->OnChangeFilename();
544 node = node->GetNext();
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.GetFirst();
778 while (node)
779 {
780 wxDocument *doc = (wxDocument *)node->GetData();
781 wxNode *next = node->GetNext();
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.GetFirst();
808 while (node)
809 {
810 wxDocTemplate *templ = (wxDocTemplate*) node->GetData();
811 wxNode* next = node->GetNext();
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.GetCount() == 1)
1033 {
1034 wxDocument* doc = (wxDocument*) m_docs.GetFirst()->GetData();
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.GetCount()];
1055 int n = 0;
1056
1057 for (size_t i = 0; i < m_templates.GetCount(); i++)
1058 {
1059 wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Item(i)->GetData());
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 ( (int)GetDocuments().GetCount() >= m_maxDocsOpen )
1075 {
1076 wxDocument *doc = (wxDocument *)GetDocuments().GetFirst()->GetData();
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.GetCount()];
1166 int n =0;
1167
1168 for (size_t i = 0; i < m_templates.GetCount(); i++)
1169 {
1170 wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Item(i)->GetData());
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 for (size_t i = 0; i < m_templates.GetCount(); i++)
1339 {
1340 wxDocTemplate *temp = (wxDocTemplate *)m_templates.Item(i)->GetData();
1341 if ( temp->FileMatchesTemplate(path) )
1342 {
1343 theTemplate = temp;
1344 break;
1345 }
1346 }
1347 return theTemplate;
1348 }
1349
1350 // Try to get a more suitable parent frame than the top window,
1351 // for selection dialogs. Otherwise you may get an unexpected
1352 // window being activated when a dialog is shown.
1353 static wxWindow* wxFindSuitableParent()
1354 {
1355 wxWindow* parent = wxTheApp->GetTopWindow();
1356
1357 wxWindow* focusWindow = wxWindow::FindFocus();
1358 if (focusWindow)
1359 {
1360 while (focusWindow &&
1361 !focusWindow->IsKindOf(CLASSINFO(wxDialog)) &&
1362 !focusWindow->IsKindOf(CLASSINFO(wxFrame)))
1363
1364 focusWindow = focusWindow->GetParent();
1365
1366 if (focusWindow)
1367 parent = focusWindow;
1368 }
1369 return parent;
1370 }
1371
1372 // Prompts user to open a file, using file specs in templates.
1373 // How to implement in wxWindows? Must extend the file selector
1374 // dialog or implement own; OR match the extension to the
1375 // template extension.
1376
1377 wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates,
1378 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1379 int noTemplates,
1380 #else
1381 int WXUNUSED(noTemplates),
1382 #endif
1383 wxString& path,
1384 long WXUNUSED(flags),
1385 bool WXUNUSED(save))
1386 {
1387 // We can only have multiple filters in Windows and GTK
1388 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1389 wxString descrBuf;
1390
1391 int i;
1392 for (i = 0; i < noTemplates; i++)
1393 {
1394 if (templates[i]->IsVisible())
1395 {
1396 // add a '|' to separate this filter from the previous one
1397 if ( !descrBuf.IsEmpty() )
1398 descrBuf << wxT('|');
1399
1400 descrBuf << templates[i]->GetDescription()
1401 << wxT(" (") << templates[i]->GetFileFilter() << wxT(") |")
1402 << templates[i]->GetFileFilter();
1403 }
1404 }
1405 #else
1406 wxString descrBuf = wxT("*.*");
1407 #endif
1408
1409 int FilterIndex = -1;
1410
1411 wxWindow* parent = wxFindSuitableParent();
1412
1413 wxString pathTmp = wxFileSelectorEx(_("Select a file"),
1414 m_lastDirectory,
1415 wxT(""),
1416 &FilterIndex,
1417 descrBuf,
1418 0,
1419 parent);
1420
1421 wxDocTemplate *theTemplate = (wxDocTemplate *)NULL;
1422 if (!pathTmp.IsEmpty())
1423 {
1424 if (!wxFileExists(pathTmp))
1425 {
1426 wxString msgTitle;
1427 if (!wxTheApp->GetAppName().IsEmpty())
1428 msgTitle = wxTheApp->GetAppName();
1429 else
1430 msgTitle = wxString(_("File error"));
1431
1432 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION,
1433 parent);
1434
1435 path = wxT("");
1436 return (wxDocTemplate *) NULL;
1437 }
1438 m_lastDirectory = wxPathOnly(pathTmp);
1439
1440 path = pathTmp;
1441
1442 // first choose the template using the extension, if this fails (i.e.
1443 // wxFileSelectorEx() didn't fill it), then use the path
1444 if ( FilterIndex != -1 )
1445 theTemplate = templates[FilterIndex];
1446 if ( !theTemplate )
1447 theTemplate = FindTemplateForPath(path);
1448 }
1449 else
1450 {
1451 path = wxT("");
1452 }
1453
1454 return theTemplate;
1455
1456 #if 0
1457 // In all other windowing systems, until we have more advanced
1458 // file selectors, we must select the document type (template) first, and
1459 // _then_ pop up the file selector.
1460 wxDocTemplate *temp = SelectDocumentType(templates, noTemplates);
1461 if (!temp)
1462 return (wxDocTemplate *) NULL;
1463
1464 wxChar *pathTmp = wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1465 temp->GetDefaultExtension(),
1466 temp->GetFileFilter(),
1467 0, wxTheApp->GetTopWindow());
1468
1469 if (pathTmp)
1470 {
1471 path = pathTmp;
1472 return temp;
1473 }
1474 else
1475 return (wxDocTemplate *) NULL;
1476 #endif // 0
1477 }
1478
1479 wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates,
1480 int noTemplates, bool sort)
1481 {
1482 wxArrayString strings(sort);
1483 wxDocTemplate **data = new wxDocTemplate *[noTemplates];
1484 int i;
1485 int n = 0;
1486
1487 for (i = 0; i < noTemplates; i++)
1488 {
1489 if (templates[i]->IsVisible())
1490 {
1491 int j;
1492 bool want = TRUE;
1493 for (j = 0; j < n; j++)
1494 {
1495 //filter out NOT unique documents + view combinations
1496 if ( templates[i]->m_docTypeName == data[j]->m_docTypeName &&
1497 templates[i]->m_viewTypeName == data[j]->m_viewTypeName
1498 )
1499 want = FALSE;
1500 }
1501
1502 if ( want )
1503 {
1504 strings.Add(templates[i]->m_description);
1505
1506 data[n] = templates[i];
1507 n ++;
1508 }
1509 }
1510 } // for
1511
1512 if (sort)
1513 {
1514 // Yes, this will be slow, but template lists
1515 // are typically short.
1516 int j;
1517 n = strings.Count();
1518 for (i = 0; i < n; i++)
1519 {
1520 for (j = 0; j < noTemplates; j++)
1521 {
1522 if (strings[i] == templates[j]->m_description)
1523 data[i] = templates[j];
1524 }
1525 }
1526 }
1527
1528 wxDocTemplate *theTemplate;
1529
1530 switch ( n )
1531 {
1532 case 0:
1533 // no visible templates, hence nothing to choose from
1534 theTemplate = NULL;
1535 break;
1536
1537 case 1:
1538 // don't propose the user to choose if he heas no choice
1539 theTemplate = data[0];
1540 break;
1541
1542 default:
1543 // propose the user to choose one of several
1544 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1545 (
1546 _("Select a document template"),
1547 _("Templates"),
1548 strings,
1549 (void **)data,
1550 wxFindSuitableParent()
1551 );
1552 }
1553
1554 delete[] data;
1555
1556 return theTemplate;
1557 }
1558
1559 wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates,
1560 int noTemplates, bool sort)
1561 {
1562 wxArrayString strings(sort);
1563 wxDocTemplate **data = new wxDocTemplate *[noTemplates];
1564 int i;
1565 int n = 0;
1566
1567 for (i = 0; i < noTemplates; i++)
1568 {
1569 wxDocTemplate *templ = templates[i];
1570 if ( templ->IsVisible() && !templ->GetViewName().empty() )
1571 {
1572 int j;
1573 bool want = TRUE;
1574 for (j = 0; j < n; j++)
1575 {
1576 //filter out NOT unique views
1577 if ( templates[i]->m_viewTypeName == data[j]->m_viewTypeName )
1578 want = FALSE;
1579 }
1580
1581 if ( want )
1582 {
1583 strings.Add(templ->m_viewTypeName);
1584 data[n] = templ;
1585 n ++;
1586 }
1587 }
1588 }
1589
1590 if (sort)
1591 {
1592 // Yes, this will be slow, but template lists
1593 // are typically short.
1594 int j;
1595 n = strings.Count();
1596 for (i = 0; i < n; i++)
1597 {
1598 for (j = 0; j < noTemplates; j++)
1599 {
1600 if (strings[i] == templates[j]->m_viewTypeName)
1601 data[i] = templates[j];
1602 }
1603 }
1604 }
1605
1606 wxDocTemplate *theTemplate;
1607
1608 // the same logic as above
1609 switch ( n )
1610 {
1611 case 0:
1612 theTemplate = (wxDocTemplate *)NULL;
1613 break;
1614
1615 case 1:
1616 theTemplate = data[0];
1617 break;
1618
1619 default:
1620 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1621 (
1622 _("Select a document view"),
1623 _("Views"),
1624 strings,
1625 (void **)data,
1626 wxFindSuitableParent()
1627 );
1628
1629 }
1630
1631 delete[] data;
1632 return theTemplate;
1633 }
1634
1635 void wxDocManager::AssociateTemplate(wxDocTemplate *temp)
1636 {
1637 if (!m_templates.Member(temp))
1638 m_templates.Append(temp);
1639 }
1640
1641 void wxDocManager::DisassociateTemplate(wxDocTemplate *temp)
1642 {
1643 m_templates.DeleteObject(temp);
1644 }
1645
1646 // Add and remove a document from the manager's list
1647 void wxDocManager::AddDocument(wxDocument *doc)
1648 {
1649 if (!m_docs.Member(doc))
1650 m_docs.Append(doc);
1651 }
1652
1653 void wxDocManager::RemoveDocument(wxDocument *doc)
1654 {
1655 m_docs.DeleteObject(doc);
1656 }
1657
1658 // Views or windows should inform the document manager
1659 // when a view is going in or out of focus
1660 void wxDocManager::ActivateView(wxView *view, bool activate, bool WXUNUSED(deleting))
1661 {
1662 // If we're deactiving, and if we're not actually deleting the view, then
1663 // don't reset the current view because we may be going to
1664 // a window without a view.
1665 // WHAT DID I MEAN BY THAT EXACTLY?
1666 /*
1667 if (deleting)
1668 {
1669 if (m_currentView == view)
1670 m_currentView = NULL;
1671 }
1672 else
1673 */
1674 {
1675 if (activate)
1676 m_currentView = view;
1677 else
1678 m_currentView = (wxView *) NULL;
1679 }
1680 }
1681
1682 // ----------------------------------------------------------------------------
1683 // Default document child frame
1684 // ----------------------------------------------------------------------------
1685
1686 BEGIN_EVENT_TABLE(wxDocChildFrame, wxFrame)
1687 EVT_ACTIVATE(wxDocChildFrame::OnActivate)
1688 EVT_CLOSE(wxDocChildFrame::OnCloseWindow)
1689 END_EVENT_TABLE()
1690
1691 wxDocChildFrame::wxDocChildFrame(wxDocument *doc,
1692 wxView *view,
1693 wxFrame *frame,
1694 wxWindowID id,
1695 const wxString& title,
1696 const wxPoint& pos,
1697 const wxSize& size,
1698 long style,
1699 const wxString& name)
1700 : wxFrame(frame, id, title, pos, size, style, name)
1701 {
1702 m_childDocument = doc;
1703 m_childView = view;
1704 if (view)
1705 view->SetFrame(this);
1706 }
1707
1708 wxDocChildFrame::~wxDocChildFrame()
1709 {
1710 }
1711
1712 // Extend event processing to search the view's event table
1713 bool wxDocChildFrame::ProcessEvent(wxEvent& event)
1714 {
1715 if (m_childView)
1716 m_childView->Activate(TRUE);
1717
1718 if ( !m_childView || ! m_childView->ProcessEvent(event) )
1719 {
1720 // Only hand up to the parent if it's a menu command
1721 if (!event.IsKindOf(CLASSINFO(wxCommandEvent)) || !GetParent() || !GetParent()->ProcessEvent(event))
1722 return wxEvtHandler::ProcessEvent(event);
1723 else
1724 return TRUE;
1725 }
1726 else
1727 return TRUE;
1728 }
1729
1730 void wxDocChildFrame::OnActivate(wxActivateEvent& event)
1731 {
1732 wxFrame::OnActivate(event);
1733
1734 if (m_childView)
1735 m_childView->Activate(event.GetActive());
1736 }
1737
1738 void wxDocChildFrame::OnCloseWindow(wxCloseEvent& event)
1739 {
1740 if (m_childView)
1741 {
1742 bool ans = FALSE;
1743 if (!event.CanVeto())
1744 ans = TRUE; // Must delete.
1745 else
1746 ans = m_childView->Close(FALSE); // FALSE means don't delete associated window
1747
1748 if (ans)
1749 {
1750 m_childView->Activate(FALSE);
1751 delete m_childView;
1752 m_childView = (wxView *) NULL;
1753 m_childDocument = (wxDocument *) NULL;
1754
1755 this->Destroy();
1756 }
1757 else
1758 event.Veto();
1759 }
1760 else
1761 event.Veto();
1762 }
1763
1764 // ----------------------------------------------------------------------------
1765 // Default parent frame
1766 // ----------------------------------------------------------------------------
1767
1768 BEGIN_EVENT_TABLE(wxDocParentFrame, wxFrame)
1769 EVT_MENU(wxID_EXIT, wxDocParentFrame::OnExit)
1770 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, wxDocParentFrame::OnMRUFile)
1771 EVT_CLOSE(wxDocParentFrame::OnCloseWindow)
1772 END_EVENT_TABLE()
1773
1774 wxDocParentFrame::wxDocParentFrame(wxDocManager *manager,
1775 wxFrame *frame,
1776 wxWindowID id,
1777 const wxString& title,
1778 const wxPoint& pos,
1779 const wxSize& size,
1780 long style,
1781 const wxString& name)
1782 : wxFrame(frame, id, title, pos, size, style, name)
1783 {
1784 m_docManager = manager;
1785 }
1786
1787 void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event))
1788 {
1789 Close();
1790 }
1791
1792 void wxDocParentFrame::OnMRUFile(wxCommandEvent& event)
1793 {
1794 int n = event.GetId() - wxID_FILE1; // the index in MRU list
1795 wxString filename(m_docManager->GetHistoryFile(n));
1796 if ( !filename.IsEmpty() )
1797 {
1798 // verify that the file exists before doing anything else
1799 if ( wxFile::Exists(filename) )
1800 {
1801 // try to open it
1802 (void)m_docManager->CreateDocument(filename, wxDOC_SILENT);
1803 }
1804 else
1805 {
1806 // remove the bogus filename from the MRU list and notify the user
1807 // about it
1808 m_docManager->RemoveFileFromHistory(n);
1809
1810 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1811 filename.c_str());
1812 }
1813 }
1814 }
1815
1816 // Extend event processing to search the view's event table
1817 bool wxDocParentFrame::ProcessEvent(wxEvent& event)
1818 {
1819 // Try the document manager, then do default processing
1820 if (!m_docManager || !m_docManager->ProcessEvent(event))
1821 return wxEvtHandler::ProcessEvent(event);
1822 else
1823 return TRUE;
1824 }
1825
1826 // Define the behaviour for the frame closing
1827 // - must delete all frames except for the main one.
1828 void wxDocParentFrame::OnCloseWindow(wxCloseEvent& event)
1829 {
1830 if (m_docManager->Clear(!event.CanVeto()))
1831 {
1832 this->Destroy();
1833 }
1834 else
1835 event.Veto();
1836 }
1837
1838 #if wxUSE_PRINTING_ARCHITECTURE
1839
1840 wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title)
1841 : wxPrintout(title)
1842 {
1843 m_printoutView = view;
1844 }
1845
1846 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page))
1847 {
1848 wxDC *dc = GetDC();
1849
1850 // Get the logical pixels per inch of screen and printer
1851 int ppiScreenX, ppiScreenY;
1852 GetPPIScreen(&ppiScreenX, &ppiScreenY);
1853 int ppiPrinterX, ppiPrinterY;
1854 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
1855
1856 // This scales the DC so that the printout roughly represents the
1857 // the screen scaling. The text point size _should_ be the right size
1858 // but in fact is too small for some reason. This is a detail that will
1859 // need to be addressed at some point but can be fudged for the
1860 // moment.
1861 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
1862
1863 // Now we have to check in case our real page size is reduced
1864 // (e.g. because we're drawing to a print preview memory DC)
1865 int pageWidth, pageHeight;
1866 int w, h;
1867 dc->GetSize(&w, &h);
1868 GetPageSizePixels(&pageWidth, &pageHeight);
1869
1870 // If printer pageWidth == current DC width, then this doesn't
1871 // change. But w might be the preview bitmap width, so scale down.
1872 float overallScale = scale * (float)(w/(float)pageWidth);
1873 dc->SetUserScale(overallScale, overallScale);
1874
1875 if (m_printoutView)
1876 {
1877 m_printoutView->OnDraw(dc);
1878 }
1879 return TRUE;
1880 }
1881
1882 bool wxDocPrintout::HasPage(int pageNum)
1883 {
1884 return (pageNum == 1);
1885 }
1886
1887 bool wxDocPrintout::OnBeginDocument(int startPage, int endPage)
1888 {
1889 if (!wxPrintout::OnBeginDocument(startPage, endPage))
1890 return FALSE;
1891
1892 return TRUE;
1893 }
1894
1895 void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
1896 {
1897 *minPage = 1;
1898 *maxPage = 1;
1899 *selPageFrom = 1;
1900 *selPageTo = 1;
1901 }
1902
1903 #endif // wxUSE_PRINTING_ARCHITECTURE
1904
1905 // ----------------------------------------------------------------------------
1906 // File history processor
1907 // ----------------------------------------------------------------------------
1908
1909 wxFileHistory::wxFileHistory(int maxFiles)
1910 {
1911 m_fileMaxFiles = maxFiles;
1912 m_fileHistoryN = 0;
1913 m_fileHistory = new wxChar *[m_fileMaxFiles];
1914 }
1915
1916 wxFileHistory::~wxFileHistory()
1917 {
1918 int i;
1919 for (i = 0; i < m_fileHistoryN; i++)
1920 delete[] m_fileHistory[i];
1921 delete[] m_fileHistory;
1922 }
1923
1924 // File history management
1925 void wxFileHistory::AddFileToHistory(const wxString& file)
1926 {
1927 int i;
1928
1929 // Check we don't already have this file
1930 for (i = 0; i < m_fileHistoryN; i++)
1931 {
1932 if ( m_fileHistory[i] && (file == m_fileHistory[i]) )
1933 {
1934 // we do have it, move it to the top of the history
1935 RemoveFileFromHistory (i);
1936 AddFileToHistory (file);
1937 return;
1938 }
1939 }
1940
1941 // if we already have a full history, delete the one at the end
1942 if ( m_fileMaxFiles == m_fileHistoryN )
1943 {
1944 RemoveFileFromHistory (m_fileHistoryN - 1);
1945 AddFileToHistory (file);
1946 return;
1947 }
1948
1949 // Add to the project file history:
1950 // Move existing files (if any) down so we can insert file at beginning.
1951 if (m_fileHistoryN < m_fileMaxFiles)
1952 {
1953 wxNode* node = m_fileMenus.GetFirst();
1954 while (node)
1955 {
1956 wxMenu* menu = (wxMenu*) node->GetData();
1957 if ( m_fileHistoryN == 0 && menu->GetMenuItemCount() )
1958 {
1959 menu->AppendSeparator();
1960 }
1961 menu->Append(wxID_FILE1+m_fileHistoryN, _("[EMPTY]"));
1962 node = node->GetNext();
1963 }
1964 m_fileHistoryN ++;
1965 }
1966 // Shuffle filenames down
1967 for (i = (m_fileHistoryN-1); i > 0; i--)
1968 {
1969 m_fileHistory[i] = m_fileHistory[i-1];
1970 }
1971 m_fileHistory[0] = copystring(file);
1972
1973 // this is the directory of the last opened file
1974 wxString pathCurrent;
1975 wxSplitPath( m_fileHistory[0], &pathCurrent, NULL, NULL );
1976 for (i = 0; i < m_fileHistoryN; i++)
1977 {
1978 if ( m_fileHistory[i] )
1979 {
1980 // if in same directory just show the filename; otherwise the full
1981 // path
1982 wxString pathInMenu, path, filename, ext;
1983 wxSplitPath( m_fileHistory[i], &path, &filename, &ext );
1984 if ( path == pathCurrent )
1985 {
1986 pathInMenu = filename;
1987 if ( !ext.empty() )
1988 pathInMenu = pathInMenu + wxFILE_SEP_EXT + ext;
1989 }
1990 else
1991 {
1992 // absolute path; could also set relative path
1993 pathInMenu = m_fileHistory[i];
1994 }
1995
1996 wxString buf;
1997 buf.Printf(s_MRUEntryFormat, i + 1, pathInMenu.c_str());
1998 wxNode* node = m_fileMenus.GetFirst();
1999 while (node)
2000 {
2001 wxMenu* menu = (wxMenu*) node->GetData();
2002 menu->SetLabel(wxID_FILE1 + i, buf);
2003 node = node->GetNext();
2004 }
2005 }
2006 }
2007 }
2008
2009 void wxFileHistory::RemoveFileFromHistory(int i)
2010 {
2011 wxCHECK_RET( i < m_fileHistoryN,
2012 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2013
2014 // delete the element from the array (could use memmove() too...)
2015 delete [] m_fileHistory[i];
2016
2017 int j;
2018 for ( j = i; j < m_fileHistoryN - 1; j++ )
2019 {
2020 m_fileHistory[j] = m_fileHistory[j + 1];
2021 }
2022
2023 wxNode* node = m_fileMenus.GetFirst();
2024 while ( node )
2025 {
2026 wxMenu* menu = (wxMenu*) node->GetData();
2027
2028
2029 // shuffle filenames up
2030 wxString buf;
2031 for ( j = i; j < m_fileHistoryN - 1; j++ )
2032 {
2033 buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[j]);
2034 menu->SetLabel(wxID_FILE1 + j, buf);
2035 }
2036
2037 node = node->GetNext();
2038
2039 // delete the last menu item which is unused now
2040 if (menu->FindItem(wxID_FILE1 + m_fileHistoryN - 1))
2041 menu->Delete(wxID_FILE1 + m_fileHistoryN - 1);
2042
2043 // delete the last separator too if no more files are left
2044 if ( m_fileHistoryN == 1 )
2045 {
2046 wxMenuItemList::Node *node = menu->GetMenuItems().GetLast();
2047 if ( node )
2048 {
2049 wxMenuItem *menuItem = node->GetData();
2050 if ( menuItem->IsSeparator() )
2051 {
2052 menu->Delete(menuItem);
2053 }
2054 //else: should we search backwards for the last separator?
2055 }
2056 //else: menu is empty somehow
2057 }
2058 }
2059
2060 m_fileHistoryN--;
2061 }
2062
2063 wxString wxFileHistory::GetHistoryFile(int i) const
2064 {
2065 wxString s;
2066 if ( i < m_fileHistoryN )
2067 {
2068 s = m_fileHistory[i];
2069 }
2070 else
2071 {
2072 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2073 }
2074
2075 return s;
2076 }
2077
2078 void wxFileHistory::UseMenu(wxMenu *menu)
2079 {
2080 if (!m_fileMenus.Member(menu))
2081 m_fileMenus.Append(menu);
2082 }
2083
2084 void wxFileHistory::RemoveMenu(wxMenu *menu)
2085 {
2086 m_fileMenus.DeleteObject(menu);
2087 }
2088
2089 #if wxUSE_CONFIG
2090 void wxFileHistory::Load(wxConfigBase& config)
2091 {
2092 m_fileHistoryN = 0;
2093 wxString buf;
2094 buf.Printf(wxT("file%d"), m_fileHistoryN+1);
2095 wxString historyFile;
2096 while ((m_fileHistoryN < m_fileMaxFiles) && config.Read(buf, &historyFile) && (historyFile != wxT("")))
2097 {
2098 m_fileHistory[m_fileHistoryN] = copystring((const wxChar*) historyFile);
2099 m_fileHistoryN ++;
2100 buf.Printf(wxT("file%d"), m_fileHistoryN+1);
2101 historyFile = wxT("");
2102 }
2103 AddFilesToMenu();
2104 }
2105
2106 void wxFileHistory::Save(wxConfigBase& config)
2107 {
2108 int i;
2109 for (i = 0; i < m_fileHistoryN; i++)
2110 {
2111 wxString buf;
2112 buf.Printf(wxT("file%d"), i+1);
2113 config.Write(buf, wxString(m_fileHistory[i]));
2114 }
2115 }
2116 #endif // wxUSE_CONFIG
2117
2118 void wxFileHistory::AddFilesToMenu()
2119 {
2120 if (m_fileHistoryN > 0)
2121 {
2122 wxNode* node = m_fileMenus.GetFirst();
2123 while (node)
2124 {
2125 wxMenu* menu = (wxMenu*) node->GetData();
2126 if (menu->GetMenuItemCount())
2127 {
2128 menu->AppendSeparator();
2129 }
2130
2131 int i;
2132 for (i = 0; i < m_fileHistoryN; i++)
2133 {
2134 if (m_fileHistory[i])
2135 {
2136 wxString buf;
2137 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
2138 menu->Append(wxID_FILE1+i, buf);
2139 }
2140 }
2141 node = node->GetNext();
2142 }
2143 }
2144 }
2145
2146 void wxFileHistory::AddFilesToMenu(wxMenu* menu)
2147 {
2148 if (m_fileHistoryN > 0)
2149 {
2150 if (menu->GetMenuItemCount())
2151 {
2152 menu->AppendSeparator();
2153 }
2154
2155 int i;
2156 for (i = 0; i < m_fileHistoryN; i++)
2157 {
2158 if (m_fileHistory[i])
2159 {
2160 wxString buf;
2161 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
2162 menu->Append(wxID_FILE1+i, buf);
2163 }
2164 }
2165 }
2166 }
2167
2168 // ----------------------------------------------------------------------------
2169 // Permits compatibility with existing file formats and functions that
2170 // manipulate files directly
2171 // ----------------------------------------------------------------------------
2172
2173 #if wxUSE_STD_IOSTREAM
2174 bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream)
2175 {
2176 FILE *fd1;
2177 int ch;
2178
2179 if ((fd1 = wxFopen (filename.fn_str(), _T("rb"))) == NULL)
2180 return FALSE;
2181
2182 while ((ch = getc (fd1)) != EOF)
2183 stream << (unsigned char)ch;
2184
2185 fclose (fd1);
2186 return TRUE;
2187 }
2188
2189 bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename)
2190 {
2191 FILE *fd1;
2192 int ch;
2193
2194 if ((fd1 = wxFopen (filename.fn_str(), _T("wb"))) == NULL)
2195 {
2196 return FALSE;
2197 }
2198
2199 while (!stream.eof())
2200 {
2201 ch = stream.get();
2202 if (!stream.eof())
2203 putc (ch, fd1);
2204 }
2205 fclose (fd1);
2206 return TRUE;
2207 }
2208 #else
2209 bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream)
2210 {
2211 FILE *fd1;
2212 int ch;
2213
2214 if ((fd1 = wxFopen (filename, wxT("rb"))) == NULL)
2215 return FALSE;
2216
2217 while ((ch = getc (fd1)) != EOF)
2218 stream.PutC((char) ch);
2219
2220 fclose (fd1);
2221 return TRUE;
2222 }
2223
2224 bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename)
2225 {
2226 FILE *fd1;
2227 char ch;
2228
2229 if ((fd1 = wxFopen (filename, wxT("wb"))) == NULL)
2230 {
2231 return FALSE;
2232 }
2233
2234 int len = stream.GetSize();
2235 // TODO: is this the correct test for EOF?
2236 while (stream.TellI() < (len - 1))
2237 {
2238 ch = stream.GetC();
2239 putc (ch, fd1);
2240 }
2241 fclose (fd1);
2242 return TRUE;
2243 }
2244 #endif
2245
2246 #endif // wxUSE_DOC_VIEW_ARCHITECTURE
2247