1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Document/view classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "docview.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #if wxUSE_DOC_VIEW_ARCHITECTURE
30 #include "wx/string.h"
34 #include "wx/dialog.h"
37 #include "wx/filedlg.h"
45 #include "wx/msgdlg.h"
46 #include "wx/choicdlg.h"
47 #include "wx/docview.h"
48 #include "wx/printdlg.h"
49 #include "wx/confbase.h"
54 #include "wx/ioswrap.h"
62 #if !USE_SHARED_LIBRARY
63 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
64 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
65 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
66 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
67 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
68 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
69 #if wxUSE_PRINTING_ARCHITECTURE
70 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
72 IMPLEMENT_CLASS(wxCommand
, wxObject
)
73 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
74 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
75 // IMPLEMENT_DYNAMIC_CLASS(wxPrintInfo, wxObject)
79 * Definition of wxDocument
82 wxDocument::wxDocument(wxDocument
*parent
)
84 m_documentModified
=FALSE
;
87 m_documentParent
=parent
;
88 m_documentTemplate
= (wxDocTemplate
*) NULL
;
89 m_documentTypeName
= "";
93 bool wxDocument::DeleteContents(void)
98 wxDocument::~wxDocument(void)
102 if (m_commandProcessor
)
103 delete m_commandProcessor
;
105 GetDocumentManager()->RemoveDocument(this);
107 // Not safe to do here, since it'll
108 // invoke virtual view functions expecting to see
109 // valid derived objects: and by the time we get
110 // here, we've called destructors higher up.
114 bool wxDocument::Close(void)
116 if (OnSaveModified())
117 return OnCloseDocument();
122 bool wxDocument::OnCloseDocument(void)
129 // Note that this implicitly deletes the document when
130 // the last view is deleted.
131 bool wxDocument::DeleteAllViews(void)
133 wxNode
*node
= m_documentViews
.First();
136 wxView
*view
= (wxView
*)node
->Data();
140 wxNode
*next
= node
->Next();
142 delete view
; // Deletes node implicitly
148 wxView
*wxDocument::GetFirstView(void) const
150 if (m_documentViews
.Number() == 0)
151 return (wxView
*) NULL
;
152 return (wxView
*)m_documentViews
.First()->Data();
155 wxDocManager
*wxDocument::GetDocumentManager(void) const
157 return m_documentTemplate
->GetDocumentManager();
160 bool wxDocument::OnNewDocument(void)
162 if (!OnSaveModified())
165 if (OnCloseDocument()==FALSE
) return FALSE
;
168 SetDocumentSaved(FALSE
);
171 GetDocumentManager()->MakeDefaultName(name
);
173 SetFilename(name
, TRUE
);
178 bool wxDocument::Save(void)
182 if (!IsModified()) return TRUE
;
183 if (m_documentFile
== "" || !m_savedYet
)
186 ret
= OnSaveDocument(m_documentFile
);
188 SetDocumentSaved(TRUE
);
192 bool wxDocument::SaveAs(void)
194 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
198 wxString tmp
= wxFileSelector(_("Save as"),
199 docTemplate
->GetDirectory(),
201 docTemplate
->GetDefaultExtension(),
202 docTemplate
->GetFileFilter(),
203 wxSAVE
| wxOVERWRITE_PROMPT
,
204 GetDocumentWindow());
209 wxString
fileName(tmp
);
213 wxSplitPath(fileName
, & path
, & name
, & ext
);
215 if (ext
.IsEmpty() || ext
== "")
218 fileName
+= docTemplate
->GetDefaultExtension();
221 SetFilename(fileName
);
222 SetTitle(wxFileNameFromPath(fileName
));
224 GetDocumentManager()->AddFileToHistory(fileName
);
226 // Notify the views that the filename has changed
227 wxNode
*node
= m_documentViews
.First();
230 wxView
*view
= (wxView
*)node
->Data();
231 view
->OnChangeFilename();
235 return OnSaveDocument(m_documentFile
);
238 bool wxDocument::OnSaveDocument(const wxString
& file
)
244 if (wxTheApp
->GetAppName() != "")
245 msgTitle
= wxTheApp
->GetAppName();
247 msgTitle
= wxString(_("File error"));
249 ofstream
store(file
);
250 if (store
.fail() || store
.bad())
252 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
253 GetDocumentWindow());
257 if (SaveObject(store
)==FALSE
)
259 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
260 GetDocumentWindow());
269 bool wxDocument::OnOpenDocument(const wxString
& file
)
271 if (!OnSaveModified())
275 if (wxTheApp
->GetAppName() != "")
276 msgTitle
= wxTheApp
->GetAppName();
278 msgTitle
= wxString(_("File error"));
280 ifstream
store(file
);
281 if (store
.fail() || store
.bad())
283 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
284 GetDocumentWindow());
287 if (LoadObject(store
)==FALSE
)
289 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
290 GetDocumentWindow());
293 SetFilename(file
, TRUE
);
302 istream
& wxDocument::LoadObject(istream
& stream
)
304 // wxObject::LoadObject(stream);
309 ostream
& wxDocument::SaveObject(ostream
& stream
)
311 // wxObject::SaveObject(stream);
316 bool wxDocument::Revert(void)
322 // Get title, or filename if no title, else unnamed
323 bool wxDocument::GetPrintableName(wxString
& buf
) const
325 if (m_documentTitle
!= "")
327 buf
= m_documentTitle
;
330 else if (m_documentFile
!= "")
332 buf
= wxFileNameFromPath(m_documentFile
);
342 wxWindow
*wxDocument::GetDocumentWindow(void) const
344 wxView
*view
= GetFirstView();
346 return view
->GetFrame();
348 return wxTheApp
->GetTopWindow();
351 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor(void)
353 return new wxCommandProcessor
;
356 // TRUE if safe to close
357 bool wxDocument::OnSaveModified(void)
362 GetPrintableName(title
);
365 if (wxTheApp
->GetAppName() != "")
366 msgTitle
= wxTheApp
->GetAppName();
368 msgTitle
= wxString(_("Warning"));
371 prompt
.Printf(_("Do you want to save changes to document %s?"),
372 (const char *)title
);
373 int res
= wxMessageBox(prompt
, msgTitle
,
374 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
375 GetDocumentWindow());
381 else if (res
== wxYES
)
383 else if (res
== wxCANCEL
)
389 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
394 bool wxDocument::AddView(wxView
*view
)
396 if (!m_documentViews
.Member(view
))
398 m_documentViews
.Append(view
);
404 bool wxDocument::RemoveView(wxView
*view
)
406 (void)m_documentViews
.DeleteObject(view
);
411 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
413 if (GetDocumentTemplate()->CreateView(this, flags
))
419 // Called after a view is added or removed.
420 // The default implementation deletes the document if
421 // there are no more views.
422 void wxDocument::OnChangedViewList(void)
424 if (m_documentViews
.Number() == 0)
426 if (OnSaveModified())
433 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
435 wxNode
*node
= m_documentViews
.First();
438 wxView
*view
= (wxView
*)node
->Data();
439 view
->OnUpdate(sender
, hint
);
444 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
446 m_documentFile
= filename
;
449 // Notify the views that the filename has changed
450 wxNode
*node
= m_documentViews
.First();
453 wxView
*view
= (wxView
*)node
->Data();
454 view
->OnChangeFilename();
468 m_viewDocument
= (wxDocument
*) NULL
;
471 m_viewFrame
= (wxFrame
*) NULL
;
474 wxView::~wxView(void)
476 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
477 m_viewDocument
->RemoveView(this);
480 // Extend event processing to search the document's event table
481 bool wxView::ProcessEvent(wxEvent
& event
)
483 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
484 return wxEvtHandler::ProcessEvent(event
);
489 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
493 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
498 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
502 void wxView::OnChangeFilename(void)
504 if (GetFrame() && GetDocument())
507 GetDocument()->GetPrintableName(name
);
509 GetFrame()->SetTitle(name
);
513 void wxView::SetDocument(wxDocument
*doc
)
515 m_viewDocument
= doc
;
520 bool wxView::Close(bool deleteWindow
)
522 if (OnClose(deleteWindow
))
528 void wxView::Activate(bool activate
)
530 if (GetDocumentManager())
532 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
533 GetDocumentManager()->ActivateView(this, activate
);
537 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
539 return GetDocument() ? GetDocument()->Close() : TRUE
;
542 #if wxUSE_PRINTING_ARCHITECTURE
543 wxPrintout
*wxView::OnCreatePrintout(void)
545 return new wxDocPrintout(this);
554 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
,
555 const wxString
& filter
, const wxString
& dir
, const wxString
& ext
,
556 const wxString
& docTypeName
, const wxString
& viewTypeName
,
557 wxClassInfo
*docClassInfo
, wxClassInfo
*viewClassInfo
, long flags
)
559 m_documentManager
= manager
;
561 m_description
= descr
;
564 m_fileFilter
= filter
;
566 m_docTypeName
= docTypeName
;
567 m_viewTypeName
= viewTypeName
;
568 m_documentManager
->AssociateTemplate(this);
570 m_docClassInfo
= docClassInfo
;
571 m_viewClassInfo
= viewClassInfo
;
574 wxDocTemplate::~wxDocTemplate(void)
576 m_documentManager
->DisassociateTemplate(this);
579 // Tries to dynamically construct an object of the right
581 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
584 return (wxDocument
*) NULL
;
585 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
586 doc
->SetFilename(path
);
587 doc
->SetDocumentTemplate(this);
588 GetDocumentManager()->AddDocument(doc
);
589 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
591 if (doc
->OnCreate(path
, flags
))
596 return (wxDocument
*) NULL
;
600 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
602 if (!m_viewClassInfo
)
603 return (wxView
*) NULL
;
604 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
605 view
->SetDocument(doc
);
606 if (view
->OnCreate(doc
, flags
))
613 return (wxView
*) NULL
;
617 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
618 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
619 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
620 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
621 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
622 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
623 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
624 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
625 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
626 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
627 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
628 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
631 wxDocManager::wxDocManager(long flags
, bool initialize
)
633 m_defaultDocumentNameCounter
= 1;
635 m_currentView
= (wxView
*) NULL
;
636 m_maxDocsOpen
= 10000;
637 m_fileHistory
= (wxFileHistory
*) NULL
;
642 wxDocManager::~wxDocManager(void)
646 delete m_fileHistory
;
649 bool wxDocManager::Clear(bool force
)
651 wxNode
*node
= m_docs
.First();
654 wxDocument
*doc
= (wxDocument
*)node
->Data();
655 wxNode
*next
= node
->Next();
657 if (!doc
->Close() && !force
)
660 // Implicitly deletes the document when the last
661 // view is removed (deleted)
662 doc
->DeleteAllViews();
664 // Check document is deleted
665 if (m_docs
.Member(doc
))
668 // This assumes that documents are not connected in
669 // any way, i.e. deleting one document does NOT
673 node
= m_templates
.First();
676 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
677 wxNode
* next
= node
->Next();
684 bool wxDocManager::Initialize(void)
686 m_fileHistory
= OnCreateFileHistory();
690 wxFileHistory
*wxDocManager::OnCreateFileHistory(void)
692 return new wxFileHistory
;
695 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
697 wxDocument
*doc
= GetCurrentDocument();
702 doc
->DeleteAllViews();
703 if (m_docs
.Member(doc
))
708 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
710 CreateDocument(wxString(""), wxDOC_NEW
);
713 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
715 CreateDocument(wxString(""), 0);
718 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
720 wxDocument
*doc
= GetCurrentDocument();
726 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
728 wxDocument
*doc
= GetCurrentDocument();
734 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
736 wxDocument
*doc
= GetCurrentDocument();
742 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
744 wxView
*view
= GetCurrentView();
748 wxPrintout
*printout
= view
->OnCreatePrintout();
752 printer
.Print(view
->GetFrame(), printout
, TRUE
);
758 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
760 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
761 wxView
*view
= GetCurrentView();
763 parentWin
= view
->GetFrame();
765 wxPrintDialogData data
;
767 wxPrintDialog
printerDialog(parentWin
, & data
);
768 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
769 printerDialog
.ShowModal();
772 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
774 wxView
*view
= GetCurrentView();
778 wxPrintout
*printout
= view
->OnCreatePrintout();
781 // Pass two printout objects: for preview, and possible printing.
782 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
783 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
785 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
786 wxPoint(100, 100), wxSize(600, 650));
787 frame
->Centre(wxBOTH
);
793 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
795 wxDocument
*doc
= GetCurrentDocument();
798 if (doc
->GetCommandProcessor())
799 doc
->GetCommandProcessor()->Undo();
802 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
804 wxDocument
*doc
= GetCurrentDocument();
807 if (doc
->GetCommandProcessor())
808 doc
->GetCommandProcessor()->Redo();
811 wxView
*wxDocManager::GetCurrentView(void) const
814 return m_currentView
;
815 if (m_docs
.Number() == 1)
817 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
818 return doc
->GetFirstView();
820 return (wxView
*) NULL
;
823 // Extend event processing to search the view's event table
824 bool wxDocManager::ProcessEvent(wxEvent
& event
)
826 wxView
* view
= GetCurrentView();
829 if (view
->ProcessEvent(event
))
832 return wxEvtHandler::ProcessEvent(event
);
835 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
837 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
840 for (i
= 0; i
< m_templates
.Number(); i
++)
842 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
843 if (temp
->IsVisible())
852 return (wxDocument
*) NULL
;
855 // If we've reached the max number of docs, close the
857 if (GetDocuments().Number() >= m_maxDocsOpen
)
859 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
862 // Implicitly deletes the document when
863 // the last view is deleted
864 doc
->DeleteAllViews();
866 // Check we're really deleted
867 if (m_docs
.Member(doc
))
871 return (wxDocument
*) NULL
;
874 // New document: user chooses a template, unless there's only one.
875 if (flags
& wxDOC_NEW
)
879 wxDocTemplate
*temp
= templates
[0];
881 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
884 newDoc
->SetDocumentName(temp
->GetDocumentName());
885 newDoc
->SetDocumentTemplate(temp
);
886 newDoc
->OnNewDocument();
891 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
895 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
898 newDoc
->SetDocumentName(temp
->GetDocumentName());
899 newDoc
->SetDocumentTemplate(temp
);
900 newDoc
->OnNewDocument();
905 return (wxDocument
*) NULL
;
909 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
915 if (flags
& wxDOC_SILENT
)
916 temp
= FindTemplateForPath(path2
);
918 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
924 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
927 newDoc
->SetDocumentName(temp
->GetDocumentName());
928 newDoc
->SetDocumentTemplate(temp
);
929 if (!newDoc
->OnOpenDocument(path2
))
932 return (wxDocument
*) NULL
;
934 AddFileToHistory(path2
);
939 return (wxDocument
*) NULL
;
942 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
944 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
947 for (i
= 0; i
< m_templates
.Number(); i
++)
949 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
950 if (temp
->IsVisible())
952 if (temp
->GetDocumentName() == doc
->GetDocumentName())
962 return (wxView
*) NULL
;
966 wxDocTemplate
*temp
= templates
[0];
968 wxView
*view
= temp
->CreateView(doc
, flags
);
970 view
->SetViewName(temp
->GetViewName());
974 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
978 wxView
*view
= temp
->CreateView(doc
, flags
);
980 view
->SetViewName(temp
->GetViewName());
984 return (wxView
*) NULL
;
987 // Not yet implemented
988 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
992 // Not yet implemented
993 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
998 wxDocument
*wxDocManager::GetCurrentDocument(void) const
1001 return m_currentView
->GetDocument();
1003 return (wxDocument
*) NULL
;
1006 // Make a default document name
1007 bool wxDocManager::MakeDefaultName(wxString
& name
)
1009 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1010 m_defaultDocumentNameCounter
++;
1015 // Not yet implemented
1016 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1018 return (wxDocTemplate
*) NULL
;
1021 // File history management
1022 void wxDocManager::AddFileToHistory(const wxString
& file
)
1025 m_fileHistory
->AddFileToHistory(file
);
1028 wxString
wxDocManager::GetHistoryFile(int i
) const
1031 return wxString(m_fileHistory
->GetHistoryFile(i
));
1033 return wxString("");
1036 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1039 m_fileHistory
->UseMenu(menu
);
1042 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1045 m_fileHistory
->RemoveMenu(menu
);
1049 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1052 m_fileHistory
->Load(config
);
1055 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1058 m_fileHistory
->Save(config
);
1062 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1065 m_fileHistory
->AddFilesToMenu(menu
);
1068 void wxDocManager::FileHistoryAddFilesToMenu()
1071 m_fileHistory
->AddFilesToMenu();
1074 int wxDocManager::GetNoHistoryFiles(void) const
1077 return m_fileHistory
->GetNoHistoryFiles();
1082 static char *FindExtension(char *path
)
1084 static char ext
[10];
1085 int len
= strlen(path
);
1089 for (i
= (len
-1); i
> 0; i
--)
1095 for (j
= i
+1; j
< len
; j
++)
1096 ext
[(int)(j
-(i
+1))] = (char)wxToLower(path
[j
]); // NOTE Should not use tolower under UNIX
1101 return (char *) NULL
;
1103 else return (char *) NULL
;
1107 // Given a path, try to find a matching template. Won't
1108 // always work, of course.
1109 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1111 char *theExt
= FindExtension((char *)(const char *)path
);
1113 return (wxDocTemplate
*) NULL
;
1114 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1116 if (m_templates
.Number() == 1)
1117 return (wxDocTemplate
*)m_templates
.First()->Data();
1119 // Find the template which this extension corresponds to
1121 for (i
= 0; i
< m_templates
.Number(); i
++)
1123 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1124 if (strcmp(temp
->GetDefaultExtension(), theExt
) == 0)
1133 // Prompts user to open a file, using file specs in templates.
1134 // How to implement in wxWindows? Must extend the file selector
1135 // dialog or implement own; OR match the extension to the
1136 // template extension.
1139 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1140 int noTemplates
, wxString
& path
, long WXUNUSED(flags
), bool WXUNUSED(save
))
1142 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**WXUNUSED(templates
),
1143 int WXUNUSED(noTemplates
), wxString
& path
, long WXUNUSED(flags
), bool WXUNUSED(save
))
1146 // We can only have multiple filters in Windows
1151 for (i
= 0; i
< noTemplates
; i
++)
1153 if (templates
[i
]->IsVisible())
1155 // add a '|' to separate this filter from the previous one
1156 if ( !descrBuf
.IsEmpty() )
1159 descrBuf
<< templates
[i
]->GetDescription()
1160 << " (" << templates
[i
]->GetFileFilter() << ") |"
1161 << templates
[i
]->GetFileFilter();
1165 wxString descrBuf
= "*.*";
1168 wxString pathTmp
= wxFileSelector(_("Select a file"), "", "", "",
1169 descrBuf
, 0, wxTheApp
->GetTopWindow());
1171 if (!pathTmp
.IsEmpty())
1174 char *theExt
= FindExtension((char *)(const char *)path
);
1176 return (wxDocTemplate
*) NULL
;
1178 // This is dodgy in that we're selecting the template on the
1179 // basis of the file extension, which may not be a standard
1180 // one. We really want to know exactly which template was
1181 // chosen by using a more advanced file selector.
1182 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1188 return (wxDocTemplate
*) NULL
;
1191 // In all other windowing systems, until we have more advanced
1192 // file selectors, we must select the document type (template) first, and
1193 // _then_ pop up the file selector.
1194 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1196 return (wxDocTemplate
*) NULL
;
1198 char *pathTmp
= wxFileSelector(_("Select a file"), "", "",
1199 temp
->GetDefaultExtension(),
1200 temp
->GetFileFilter(),
1201 0, wxTheApp
->GetTopWindow());
1209 return (wxDocTemplate
*) NULL
;
1213 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1216 char **strings
= new char *[noTemplates
];
1217 char **data
= new char *[noTemplates
];
1220 for (i
= 0; i
< noTemplates
; i
++)
1222 if (templates
[i
]->IsVisible())
1224 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1225 data
[n
] = (char *)templates
[i
];
1233 return (wxDocTemplate
*) NULL
;
1237 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1243 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document template"), _("Templates"), n
,
1250 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1253 char **strings
= new char *[noTemplates
];
1254 char **data
= new char *[noTemplates
];
1257 for (i
= 0; i
< noTemplates
; i
++)
1259 if (templates
[i
]->IsVisible() && (templates
[i
]->GetViewName() != ""))
1261 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1262 data
[n
] = (char *)templates
[i
];
1266 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document view"), _("Views"), n
,
1273 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1275 if (!m_templates
.Member(temp
))
1276 m_templates
.Append(temp
);
1279 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1281 m_templates
.DeleteObject(temp
);
1284 // Add and remove a document from the manager's list
1285 void wxDocManager::AddDocument(wxDocument
*doc
)
1287 if (!m_docs
.Member(doc
))
1291 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1293 m_docs
.DeleteObject(doc
);
1296 // Views or windows should inform the document manager
1297 // when a view is going in or out of focus
1298 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1300 // If we're deactiving, and if we're not actually deleting the view, then
1301 // don't reset the current view because we may be going to
1302 // a window without a view.
1303 // WHAT DID I MEAN BY THAT EXACTLY?
1307 if (m_currentView == view)
1308 m_currentView = NULL;
1314 m_currentView
= view
;
1316 m_currentView
= (wxView
*) NULL
;
1321 * Default document child frame
1324 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1325 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1326 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1329 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1330 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1331 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1333 m_childDocument
= doc
;
1336 view
->SetFrame(this);
1339 wxDocChildFrame::~wxDocChildFrame(void)
1343 // Extend event processing to search the view's event table
1344 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1347 m_childView
->Activate(TRUE
);
1349 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1351 // Only hand up to the parent if it's a menu command
1352 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1353 return wxEvtHandler::ProcessEvent(event
);
1361 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1363 wxFrame::OnActivate(event
);
1366 m_childView
->Activate(event
.GetActive());
1369 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1374 if (!event
.CanVeto())
1375 ans
= TRUE
; // Must delete.
1377 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1381 m_childView
->Activate(FALSE
);
1383 m_childView
= (wxView
*) NULL
;
1384 m_childDocument
= (wxDocument
*) NULL
;
1396 * Default parent frame
1399 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1400 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1401 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1402 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1405 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1406 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1407 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1409 m_docManager
= manager
;
1412 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1417 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1419 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1421 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1424 // Extend event processing to search the view's event table
1425 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1427 // Try the document manager, then do default processing
1428 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1429 return wxEvtHandler::ProcessEvent(event
);
1434 // Define the behaviour for the frame closing
1435 // - must delete all frames except for the main one.
1436 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1438 if (m_docManager
->Clear(!event
.CanVeto()))
1446 #if wxUSE_PRINTING_ARCHITECTURE
1448 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
):
1449 wxPrintout(WXSTRINGCAST title
)
1451 m_printoutView
= view
;
1454 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1458 // Get the logical pixels per inch of screen and printer
1459 int ppiScreenX
, ppiScreenY
;
1460 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1461 int ppiPrinterX
, ppiPrinterY
;
1462 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1464 // This scales the DC so that the printout roughly represents the
1465 // the screen scaling. The text point size _should_ be the right size
1466 // but in fact is too small for some reason. This is a detail that will
1467 // need to be addressed at some point but can be fudged for the
1469 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1471 // Now we have to check in case our real page size is reduced
1472 // (e.g. because we're drawing to a print preview memory DC)
1473 int pageWidth
, pageHeight
;
1475 dc
->GetSize(&w
, &h
);
1476 GetPageSizePixels(&pageWidth
, &pageHeight
);
1478 // If printer pageWidth == current DC width, then this doesn't
1479 // change. But w might be the preview bitmap width, so scale down.
1480 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1481 dc
->SetUserScale(overallScale
, overallScale
);
1485 m_printoutView
->OnDraw(dc
);
1490 bool wxDocPrintout::HasPage(int pageNum
)
1492 return (pageNum
== 1);
1495 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1497 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1503 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1514 * Command processing framework
1517 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1519 m_canUndo
= canUndoIt
;
1520 m_commandName
= name
;
1523 wxCommand::~wxCommand(void)
1527 // Command processor
1528 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1530 m_maxNoCommands
= maxCommands
;
1531 m_currentCommand
= (wxNode
*) NULL
;
1532 m_commandEditMenu
= (wxMenu
*) NULL
;
1535 wxCommandProcessor::~wxCommandProcessor(void)
1540 // Pass a command to the processor. The processor calls Do();
1541 // if successful, is appended to the command history unless
1542 // storeIt is FALSE.
1543 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1545 bool success
= command
->Do();
1546 if (success
&& storeIt
)
1548 if (m_commands
.Number() == m_maxNoCommands
)
1550 wxNode
*firstNode
= m_commands
.First();
1551 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1552 delete firstCommand
;
1556 // Correct a bug: we must chop off the current 'branch'
1557 // so that we're at the end of the command list.
1558 if (!m_currentCommand
)
1562 wxNode
*node
= m_currentCommand
->Next();
1565 wxNode
*next
= node
->Next();
1566 delete (wxCommand
*)node
->Data();
1572 m_commands
.Append(command
);
1573 m_currentCommand
= m_commands
.Last();
1579 bool wxCommandProcessor::Undo(void)
1581 if (m_currentCommand
)
1583 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1584 if (command
->CanUndo())
1586 bool success
= command
->Undo();
1589 m_currentCommand
= m_currentCommand
->Previous();
1598 bool wxCommandProcessor::Redo(void)
1600 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
1601 wxNode
*redoNode
= (wxNode
*) NULL
;
1602 if (m_currentCommand
&& m_currentCommand
->Next())
1604 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1605 redoNode
= m_currentCommand
->Next();
1609 if (m_commands
.Number() > 0)
1611 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1612 redoNode
= m_commands
.First();
1618 bool success
= redoCommand
->Do();
1621 m_currentCommand
= redoNode
;
1629 bool wxCommandProcessor::CanUndo(void) const
1631 if (m_currentCommand
)
1632 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1636 bool wxCommandProcessor::CanRedo(void) const
1638 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() == (wxNode
*) NULL
))
1641 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() != (wxNode
*) NULL
))
1644 if ((m_currentCommand
== (wxNode
*) NULL
) && (m_commands
.Number() > 0))
1650 void wxCommandProcessor::Initialize(void)
1652 m_currentCommand
= m_commands
.Last();
1656 void wxCommandProcessor::SetMenuStrings(void)
1658 if (m_commandEditMenu
)
1661 if (m_currentCommand
)
1663 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1664 wxString
commandName(command
->GetName());
1665 if (commandName
== "") commandName
= _("Unnamed command");
1666 bool canUndo
= command
->CanUndo();
1668 buf
= wxString(_("&Undo ")) + commandName
;
1670 buf
= wxString(_("Can't &Undo ")) + commandName
;
1672 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1673 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1675 // We can redo, if we're not at the end of the history.
1676 if (m_currentCommand
->Next())
1678 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1679 wxString
redoCommandName(redoCommand
->GetName());
1680 if (redoCommandName
== "") redoCommandName
= _("Unnamed command");
1681 buf
= wxString(_("&Redo ")) + redoCommandName
;
1682 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1683 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1687 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1688 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1693 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
1694 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1696 if (m_commands
.Number() == 0)
1698 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1699 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1703 // currentCommand is NULL but there are commands: this means that
1704 // we've undone to the start of the list, but can redo the first.
1705 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1706 wxString
redoCommandName(redoCommand
->GetName());
1707 if (redoCommandName
== "") redoCommandName
= _("Unnamed command");
1708 buf
= wxString(_("&Redo ")) + redoCommandName
;
1709 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1710 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1716 void wxCommandProcessor::ClearCommands(void)
1718 wxNode
*node
= m_commands
.First();
1721 wxCommand
*command
= (wxCommand
*)node
->Data();
1724 node
= m_commands
.First();
1726 m_currentCommand
= (wxNode
*) NULL
;
1731 * File history processor
1734 wxFileHistory::wxFileHistory(int maxFiles
)
1736 m_fileMaxFiles
= maxFiles
;
1738 m_fileHistory
= new char *[m_fileMaxFiles
];
1741 wxFileHistory::~wxFileHistory(void)
1744 for (i
= 0; i
< m_fileHistoryN
; i
++)
1745 delete[] m_fileHistory
[i
];
1746 delete[] m_fileHistory
;
1749 // File history management
1750 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1753 // Check we don't already have this file
1754 for (i
= 0; i
< m_fileHistoryN
; i
++)
1756 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1760 // Add to the project file history:
1761 // Move existing files (if any) down so we can insert file at beginning.
1763 // First delete filename that has popped off the end of the array (if any)
1764 if (m_fileHistoryN
== m_fileMaxFiles
)
1766 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1767 m_fileHistory
[m_fileMaxFiles
-1] = (char *) NULL
;
1769 if (m_fileHistoryN
< m_fileMaxFiles
)
1771 wxNode
* node
= m_fileMenus
.First();
1774 wxMenu
* menu
= (wxMenu
*) node
->Data();
1775 if (m_fileHistoryN
== 0)
1776 menu
->AppendSeparator();
1777 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1778 node
= node
->Next();
1782 // Shuffle filenames down
1783 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1785 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1787 m_fileHistory
[0] = copystring(file
);
1789 for (i
= 0; i
< m_fileHistoryN
; i
++)
1790 if (m_fileHistory
[i
])
1793 buf
.Printf("&%d %s", i
+1, m_fileHistory
[i
]);
1794 wxNode
* node
= m_fileMenus
.First();
1797 wxMenu
* menu
= (wxMenu
*) node
->Data();
1798 menu
->SetLabel(wxID_FILE1
+i
, buf
);
1799 node
= node
->Next();
1804 wxString
wxFileHistory::GetHistoryFile(int i
) const
1806 if (i
< m_fileHistoryN
)
1807 return wxString(m_fileHistory
[i
]);
1809 return wxString("");
1812 void wxFileHistory::UseMenu(wxMenu
*menu
)
1814 if (!m_fileMenus
.Member(menu
))
1815 m_fileMenus
.Append(menu
);
1818 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
1820 m_fileMenus
.DeleteObject(menu
);
1824 void wxFileHistory::Load(wxConfigBase
& config
)
1828 buf
.Printf("file%d", m_fileHistoryN
+1);
1829 wxString historyFile
;
1830 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= ""))
1832 m_fileHistory
[m_fileHistoryN
] = copystring((const char*) historyFile
);
1834 buf
.Printf("file%d", m_fileHistoryN
+1);
1840 void wxFileHistory::Save(wxConfigBase
& config
)
1843 for (i
= 0; i
< m_fileHistoryN
; i
++)
1846 buf
.Printf("file%d", i
+1);
1847 config
.Write(buf
, wxString(m_fileHistory
[i
]));
1852 void wxFileHistory::AddFilesToMenu()
1854 if (m_fileHistoryN
> 0)
1856 wxNode
* node
= m_fileMenus
.First();
1859 wxMenu
* menu
= (wxMenu
*) node
->Data();
1860 menu
->AppendSeparator();
1862 for (i
= 0; i
< m_fileHistoryN
; i
++)
1864 if (m_fileHistory
[i
])
1867 buf
.Printf("&%d %s", i
+1, m_fileHistory
[i
]);
1868 menu
->Append(wxID_FILE1
+i
, buf
);
1871 node
= node
->Next();
1876 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
1878 if (m_fileHistoryN
> 0)
1880 menu
->AppendSeparator();
1882 for (i
= 0; i
< m_fileHistoryN
; i
++)
1884 if (m_fileHistory
[i
])
1887 buf
.Printf("&%d %s", i
+1, m_fileHistory
[i
]);
1888 menu
->Append(wxID_FILE1
+i
, buf
);
1899 wxPrintInfo::wxPrintInfo(void)
1904 wxPrintInfo::~wxPrintInfo(void)
1910 * Permits compatibility with existing file formats and functions
1911 * that manipulate files directly
1914 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1919 if ((fd1
= fopen (WXSTRINGCAST filename
, "rb")) == NULL
)
1922 while ((ch
= getc (fd1
)) != EOF
)
1923 stream
<< (unsigned char)ch
;
1929 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1934 if ((fd1
= fopen (WXSTRINGCAST filename
, "wb")) == NULL
)
1939 while (!stream
.eof())
1950 // End wxUSE_DOC_VIEW_ARCHITECTURE