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();
465 wxView::wxView(wxDocument
*doc
)
470 m_viewFrame
= (wxFrame
*) NULL
;
473 wxView::~wxView(void)
475 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
476 m_viewDocument
->RemoveView(this);
479 // Extend event processing to search the document's event table
480 bool wxView::ProcessEvent(wxEvent
& event
)
482 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
483 return wxEvtHandler::ProcessEvent(event
);
488 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
492 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
497 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
501 void wxView::OnChangeFilename(void)
503 if (GetFrame() && GetDocument())
506 GetDocument()->GetPrintableName(name
);
508 GetFrame()->SetTitle(name
);
512 void wxView::SetDocument(wxDocument
*doc
)
514 m_viewDocument
= doc
;
519 bool wxView::Close(bool deleteWindow
)
521 if (OnClose(deleteWindow
))
527 void wxView::Activate(bool activate
)
529 if (GetDocumentManager())
531 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
532 GetDocumentManager()->ActivateView(this, activate
);
536 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
538 return GetDocument() ? GetDocument()->Close() : TRUE
;
541 #if wxUSE_PRINTING_ARCHITECTURE
542 wxPrintout
*wxView::OnCreatePrintout(void)
544 return new wxDocPrintout(this);
553 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
,
554 const wxString
& filter
, const wxString
& dir
, const wxString
& ext
,
555 const wxString
& docTypeName
, const wxString
& viewTypeName
,
556 wxClassInfo
*docClassInfo
, wxClassInfo
*viewClassInfo
, long flags
)
558 m_documentManager
= manager
;
560 m_description
= descr
;
563 m_fileFilter
= filter
;
565 m_docTypeName
= docTypeName
;
566 m_viewTypeName
= viewTypeName
;
567 m_documentManager
->AssociateTemplate(this);
569 m_docClassInfo
= docClassInfo
;
570 m_viewClassInfo
= viewClassInfo
;
573 wxDocTemplate::~wxDocTemplate(void)
575 m_documentManager
->DisassociateTemplate(this);
578 // Tries to dynamically construct an object of the right
580 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
583 return (wxDocument
*) NULL
;
584 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
585 doc
->SetFilename(path
);
586 doc
->SetDocumentTemplate(this);
587 GetDocumentManager()->AddDocument(doc
);
588 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
590 if (doc
->OnCreate(path
, flags
))
595 return (wxDocument
*) NULL
;
599 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
601 if (!m_viewClassInfo
)
602 return (wxView
*) NULL
;
603 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
604 view
->SetDocument(doc
);
605 if (view
->OnCreate(doc
, flags
))
612 return (wxView
*) NULL
;
616 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
617 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
618 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
619 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
620 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
621 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
622 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
623 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
624 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
625 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
626 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
627 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
630 wxDocManager::wxDocManager(long flags
, bool initialize
)
632 m_defaultDocumentNameCounter
= 1;
634 m_currentView
= (wxView
*) NULL
;
635 m_maxDocsOpen
= 10000;
636 m_fileHistory
= (wxFileHistory
*) NULL
;
641 wxDocManager::~wxDocManager(void)
645 delete m_fileHistory
;
648 bool wxDocManager::Clear(bool force
)
650 wxNode
*node
= m_docs
.First();
653 wxDocument
*doc
= (wxDocument
*)node
->Data();
654 wxNode
*next
= node
->Next();
656 if (!doc
->Close() && !force
)
659 // Implicitly deletes the document when the last
660 // view is removed (deleted)
661 doc
->DeleteAllViews();
663 // Check document is deleted
664 if (m_docs
.Member(doc
))
667 // This assumes that documents are not connected in
668 // any way, i.e. deleting one document does NOT
672 node
= m_templates
.First();
675 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
676 wxNode
* next
= node
->Next();
683 bool wxDocManager::Initialize(void)
685 m_fileHistory
= OnCreateFileHistory();
689 wxFileHistory
*wxDocManager::OnCreateFileHistory(void)
691 return new wxFileHistory
;
694 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
696 wxDocument
*doc
= GetCurrentDocument();
701 doc
->DeleteAllViews();
702 if (m_docs
.Member(doc
))
707 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
709 CreateDocument(wxString(""), wxDOC_NEW
);
712 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
714 CreateDocument(wxString(""), 0);
717 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
719 wxDocument
*doc
= GetCurrentDocument();
725 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
727 wxDocument
*doc
= GetCurrentDocument();
733 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
735 wxDocument
*doc
= GetCurrentDocument();
741 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
743 wxView
*view
= GetCurrentView();
747 wxPrintout
*printout
= view
->OnCreatePrintout();
751 printer
.Print(view
->GetFrame(), printout
, TRUE
);
757 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
759 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
760 wxView
*view
= GetCurrentView();
762 parentWin
= view
->GetFrame();
766 wxPrintDialog
printerDialog(parentWin
, & data
);
767 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
768 printerDialog
.ShowModal();
771 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
773 wxView
*view
= GetCurrentView();
777 wxPrintout
*printout
= view
->OnCreatePrintout();
780 // Pass two printout objects: for preview, and possible printing.
781 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
782 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
784 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
785 wxPoint(100, 100), wxSize(600, 650));
786 frame
->Centre(wxBOTH
);
792 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
794 wxDocument
*doc
= GetCurrentDocument();
797 if (doc
->GetCommandProcessor())
798 doc
->GetCommandProcessor()->Undo();
801 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
803 wxDocument
*doc
= GetCurrentDocument();
806 if (doc
->GetCommandProcessor())
807 doc
->GetCommandProcessor()->Redo();
810 wxView
*wxDocManager::GetCurrentView(void) const
813 return m_currentView
;
814 if (m_docs
.Number() == 1)
816 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
817 return doc
->GetFirstView();
819 return (wxView
*) NULL
;
822 // Extend event processing to search the view's event table
823 bool wxDocManager::ProcessEvent(wxEvent
& event
)
825 wxView
* view
= GetCurrentView();
828 if (view
->ProcessEvent(event
))
831 return wxEvtHandler::ProcessEvent(event
);
834 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
836 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
839 for (i
= 0; i
< m_templates
.Number(); i
++)
841 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
842 if (temp
->IsVisible())
851 return (wxDocument
*) NULL
;
854 // If we've reached the max number of docs, close the
856 if (GetDocuments().Number() >= m_maxDocsOpen
)
858 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
861 // Implicitly deletes the document when
862 // the last view is deleted
863 doc
->DeleteAllViews();
865 // Check we're really deleted
866 if (m_docs
.Member(doc
))
870 return (wxDocument
*) NULL
;
873 // New document: user chooses a template, unless there's only one.
874 if (flags
& wxDOC_NEW
)
878 wxDocTemplate
*temp
= templates
[0];
880 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
883 newDoc
->SetDocumentName(temp
->GetDocumentName());
884 newDoc
->SetDocumentTemplate(temp
);
885 newDoc
->OnNewDocument();
890 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
894 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
897 newDoc
->SetDocumentName(temp
->GetDocumentName());
898 newDoc
->SetDocumentTemplate(temp
);
899 newDoc
->OnNewDocument();
904 return (wxDocument
*) NULL
;
908 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
914 if (flags
& wxDOC_SILENT
)
915 temp
= FindTemplateForPath(path2
);
917 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
923 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
926 newDoc
->SetDocumentName(temp
->GetDocumentName());
927 newDoc
->SetDocumentTemplate(temp
);
928 if (!newDoc
->OnOpenDocument(path2
))
931 return (wxDocument
*) NULL
;
933 AddFileToHistory(path2
);
938 return (wxDocument
*) NULL
;
941 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
943 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
946 for (i
= 0; i
< m_templates
.Number(); i
++)
948 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
949 if (temp
->IsVisible())
951 if (temp
->GetDocumentName() == doc
->GetDocumentName())
961 return (wxView
*) NULL
;
965 wxDocTemplate
*temp
= templates
[0];
967 wxView
*view
= temp
->CreateView(doc
, flags
);
969 view
->SetViewName(temp
->GetViewName());
973 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
977 wxView
*view
= temp
->CreateView(doc
, flags
);
979 view
->SetViewName(temp
->GetViewName());
983 return (wxView
*) NULL
;
986 // Not yet implemented
987 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
991 // Not yet implemented
992 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
997 wxDocument
*wxDocManager::GetCurrentDocument(void) const
1000 return m_currentView
->GetDocument();
1002 return (wxDocument
*) NULL
;
1005 // Make a default document name
1006 bool wxDocManager::MakeDefaultName(wxString
& name
)
1008 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1009 m_defaultDocumentNameCounter
++;
1014 // Not yet implemented
1015 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1017 return (wxDocTemplate
*) NULL
;
1020 // File history management
1021 void wxDocManager::AddFileToHistory(const wxString
& file
)
1024 m_fileHistory
->AddFileToHistory(file
);
1027 wxString
wxDocManager::GetHistoryFile(int i
) const
1030 return wxString(m_fileHistory
->GetHistoryFile(i
));
1032 return wxString("");
1035 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1038 m_fileHistory
->UseMenu(menu
);
1041 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1044 m_fileHistory
->RemoveMenu(menu
);
1048 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1051 m_fileHistory
->Load(config
);
1054 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1057 m_fileHistory
->Save(config
);
1061 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1064 m_fileHistory
->AddFilesToMenu(menu
);
1067 void wxDocManager::FileHistoryAddFilesToMenu()
1070 m_fileHistory
->AddFilesToMenu();
1073 int wxDocManager::GetNoHistoryFiles(void) const
1076 return m_fileHistory
->GetNoHistoryFiles();
1081 static char *FindExtension(char *path
)
1083 static char ext
[10];
1084 int len
= strlen(path
);
1088 for (i
= (len
-1); i
> 0; i
--)
1094 for (j
= i
+1; j
< len
; j
++)
1095 ext
[(int)(j
-(i
+1))] = (char)wxToLower(path
[j
]); // NOTE Should not use tolower under UNIX
1100 return (char *) NULL
;
1102 else return (char *) NULL
;
1106 // Given a path, try to find a matching template. Won't
1107 // always work, of course.
1108 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1110 char *theExt
= FindExtension((char *)(const char *)path
);
1112 return (wxDocTemplate
*) NULL
;
1113 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1115 if (m_templates
.Number() == 1)
1116 return (wxDocTemplate
*)m_templates
.First()->Data();
1118 // Find the template which this extension corresponds to
1120 for (i
= 0; i
< m_templates
.Number(); i
++)
1122 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1123 if (strcmp(temp
->GetDefaultExtension(), theExt
) == 0)
1132 // Prompts user to open a file, using file specs in templates.
1133 // How to implement in wxWindows? Must extend the file selector
1134 // dialog or implement own; OR match the extension to the
1135 // template extension.
1138 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1139 int noTemplates
, wxString
& path
, long WXUNUSED(flags
), bool WXUNUSED(save
))
1141 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**WXUNUSED(templates
),
1142 int WXUNUSED(noTemplates
), wxString
& path
, long WXUNUSED(flags
), bool WXUNUSED(save
))
1145 // We can only have multiple filters in Windows
1150 for (i
= 0; i
< noTemplates
; i
++)
1152 if (templates
[i
]->IsVisible())
1154 // add a '|' to separate this filter from the previous one
1155 if ( !descrBuf
.IsEmpty() )
1158 descrBuf
<< templates
[i
]->GetDescription()
1159 << " (" << templates
[i
]->GetFileFilter() << ") |"
1160 << templates
[i
]->GetFileFilter();
1164 wxString descrBuf
= "*.*";
1167 wxString pathTmp
= wxFileSelector(_("Select a file"), "", "", "",
1168 descrBuf
, 0, wxTheApp
->GetTopWindow());
1170 if (!pathTmp
.IsEmpty())
1173 char *theExt
= FindExtension((char *)(const char *)path
);
1175 return (wxDocTemplate
*) NULL
;
1177 // This is dodgy in that we're selecting the template on the
1178 // basis of the file extension, which may not be a standard
1179 // one. We really want to know exactly which template was
1180 // chosen by using a more advanced file selector.
1181 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1187 return (wxDocTemplate
*) NULL
;
1190 // In all other windowing systems, until we have more advanced
1191 // file selectors, we must select the document type (template) first, and
1192 // _then_ pop up the file selector.
1193 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1195 return (wxDocTemplate
*) NULL
;
1197 char *pathTmp
= wxFileSelector(_("Select a file"), "", "",
1198 temp
->GetDefaultExtension(),
1199 temp
->GetFileFilter(),
1200 0, wxTheApp
->GetTopWindow());
1208 return (wxDocTemplate
*) NULL
;
1212 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1215 char **strings
= new char *[noTemplates
];
1216 char **data
= new char *[noTemplates
];
1219 for (i
= 0; i
< noTemplates
; i
++)
1221 if (templates
[i
]->IsVisible())
1223 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1224 data
[n
] = (char *)templates
[i
];
1232 return (wxDocTemplate
*) NULL
;
1236 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1242 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document template"), _("Templates"), n
,
1249 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1252 char **strings
= new char *[noTemplates
];
1253 char **data
= new char *[noTemplates
];
1256 for (i
= 0; i
< noTemplates
; i
++)
1258 if (templates
[i
]->IsVisible() && (templates
[i
]->GetViewName() != ""))
1260 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1261 data
[n
] = (char *)templates
[i
];
1265 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document view"), _("Views"), n
,
1272 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1274 if (!m_templates
.Member(temp
))
1275 m_templates
.Append(temp
);
1278 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1280 m_templates
.DeleteObject(temp
);
1283 // Add and remove a document from the manager's list
1284 void wxDocManager::AddDocument(wxDocument
*doc
)
1286 if (!m_docs
.Member(doc
))
1290 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1292 m_docs
.DeleteObject(doc
);
1295 // Views or windows should inform the document manager
1296 // when a view is going in or out of focus
1297 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1299 // If we're deactiving, and if we're not actually deleting the view, then
1300 // don't reset the current view because we may be going to
1301 // a window without a view.
1302 // WHAT DID I MEAN BY THAT EXACTLY?
1306 if (m_currentView == view)
1307 m_currentView = NULL;
1313 m_currentView
= view
;
1315 m_currentView
= (wxView
*) NULL
;
1320 * Default document child frame
1323 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1324 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1325 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1328 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1329 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1330 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1332 m_childDocument
= doc
;
1335 view
->SetFrame(this);
1338 wxDocChildFrame::~wxDocChildFrame(void)
1342 // Extend event processing to search the view's event table
1343 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1346 m_childView
->Activate(TRUE
);
1348 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1350 // Only hand up to the parent if it's a menu command
1351 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1352 return wxEvtHandler::ProcessEvent(event
);
1360 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1362 wxFrame::OnActivate(event
);
1365 m_childView
->Activate(event
.GetActive());
1368 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1373 if (!event
.CanVeto())
1374 ans
= TRUE
; // Must delete.
1376 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1380 m_childView
->Activate(FALSE
);
1382 m_childView
= (wxView
*) NULL
;
1383 m_childDocument
= (wxDocument
*) NULL
;
1395 * Default parent frame
1398 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1399 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1400 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1401 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1404 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1405 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1406 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1408 m_docManager
= manager
;
1411 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1416 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1418 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1420 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1423 // Extend event processing to search the view's event table
1424 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1426 // Try the document manager, then do default processing
1427 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1428 return wxEvtHandler::ProcessEvent(event
);
1433 // Define the behaviour for the frame closing
1434 // - must delete all frames except for the main one.
1435 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1437 if (m_docManager
->Clear(!event
.CanVeto()))
1445 #if wxUSE_PRINTING_ARCHITECTURE
1447 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
):
1448 wxPrintout(WXSTRINGCAST title
)
1450 m_printoutView
= view
;
1453 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1457 // Get the logical pixels per inch of screen and printer
1458 int ppiScreenX
, ppiScreenY
;
1459 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1460 int ppiPrinterX
, ppiPrinterY
;
1461 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1463 // This scales the DC so that the printout roughly represents the
1464 // the screen scaling. The text point size _should_ be the right size
1465 // but in fact is too small for some reason. This is a detail that will
1466 // need to be addressed at some point but can be fudged for the
1468 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1470 // Now we have to check in case our real page size is reduced
1471 // (e.g. because we're drawing to a print preview memory DC)
1472 int pageWidth
, pageHeight
;
1474 dc
->GetSize(&w
, &h
);
1475 GetPageSizePixels(&pageWidth
, &pageHeight
);
1477 // If printer pageWidth == current DC width, then this doesn't
1478 // change. But w might be the preview bitmap width, so scale down.
1479 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1480 dc
->SetUserScale(overallScale
, overallScale
);
1484 m_printoutView
->OnDraw(dc
);
1489 bool wxDocPrintout::HasPage(int pageNum
)
1491 return (pageNum
== 1);
1494 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1496 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1502 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1513 * Command processing framework
1516 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1518 m_canUndo
= canUndoIt
;
1519 m_commandName
= name
;
1522 wxCommand::~wxCommand(void)
1526 // Command processor
1527 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1529 m_maxNoCommands
= maxCommands
;
1530 m_currentCommand
= (wxNode
*) NULL
;
1531 m_commandEditMenu
= (wxMenu
*) NULL
;
1534 wxCommandProcessor::~wxCommandProcessor(void)
1539 // Pass a command to the processor. The processor calls Do();
1540 // if successful, is appended to the command history unless
1541 // storeIt is FALSE.
1542 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1544 bool success
= command
->Do();
1545 if (success
&& storeIt
)
1547 if (m_commands
.Number() == m_maxNoCommands
)
1549 wxNode
*firstNode
= m_commands
.First();
1550 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1551 delete firstCommand
;
1555 // Correct a bug: we must chop off the current 'branch'
1556 // so that we're at the end of the command list.
1557 if (!m_currentCommand
)
1561 wxNode
*node
= m_currentCommand
->Next();
1564 wxNode
*next
= node
->Next();
1565 delete (wxCommand
*)node
->Data();
1571 m_commands
.Append(command
);
1572 m_currentCommand
= m_commands
.Last();
1578 bool wxCommandProcessor::Undo(void)
1580 if (m_currentCommand
)
1582 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1583 if (command
->CanUndo())
1585 bool success
= command
->Undo();
1588 m_currentCommand
= m_currentCommand
->Previous();
1597 bool wxCommandProcessor::Redo(void)
1599 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
1600 wxNode
*redoNode
= (wxNode
*) NULL
;
1601 if (m_currentCommand
&& m_currentCommand
->Next())
1603 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1604 redoNode
= m_currentCommand
->Next();
1608 if (m_commands
.Number() > 0)
1610 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1611 redoNode
= m_commands
.First();
1617 bool success
= redoCommand
->Do();
1620 m_currentCommand
= redoNode
;
1628 bool wxCommandProcessor::CanUndo(void) const
1630 if (m_currentCommand
)
1631 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1635 bool wxCommandProcessor::CanRedo(void) const
1637 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() == (wxNode
*) NULL
))
1640 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() != (wxNode
*) NULL
))
1643 if ((m_currentCommand
== (wxNode
*) NULL
) && (m_commands
.Number() > 0))
1649 void wxCommandProcessor::Initialize(void)
1651 m_currentCommand
= m_commands
.Last();
1655 void wxCommandProcessor::SetMenuStrings(void)
1657 if (m_commandEditMenu
)
1660 if (m_currentCommand
)
1662 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1663 wxString
commandName(command
->GetName());
1664 if (commandName
== "") commandName
= _("Unnamed command");
1665 bool canUndo
= command
->CanUndo();
1667 buf
= wxString(_("&Undo ")) + commandName
;
1669 buf
= wxString(_("Can't &Undo ")) + commandName
;
1671 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1672 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1674 // We can redo, if we're not at the end of the history.
1675 if (m_currentCommand
->Next())
1677 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1678 wxString
redoCommandName(redoCommand
->GetName());
1679 if (redoCommandName
== "") redoCommandName
= _("Unnamed command");
1680 buf
= wxString(_("&Redo ")) + redoCommandName
;
1681 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1682 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1686 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1687 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1692 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
1693 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1695 if (m_commands
.Number() == 0)
1697 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1698 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1702 // currentCommand is NULL but there are commands: this means that
1703 // we've undone to the start of the list, but can redo the first.
1704 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1705 wxString
redoCommandName(redoCommand
->GetName());
1706 if (redoCommandName
== "") redoCommandName
= _("Unnamed command");
1707 buf
= wxString(_("&Redo ")) + redoCommandName
;
1708 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1709 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1715 void wxCommandProcessor::ClearCommands(void)
1717 wxNode
*node
= m_commands
.First();
1720 wxCommand
*command
= (wxCommand
*)node
->Data();
1723 node
= m_commands
.First();
1725 m_currentCommand
= (wxNode
*) NULL
;
1730 * File history processor
1733 wxFileHistory::wxFileHistory(int maxFiles
)
1735 m_fileMaxFiles
= maxFiles
;
1737 m_fileHistory
= new char *[m_fileMaxFiles
];
1740 wxFileHistory::~wxFileHistory(void)
1743 for (i
= 0; i
< m_fileHistoryN
; i
++)
1744 delete[] m_fileHistory
[i
];
1745 delete[] m_fileHistory
;
1748 // File history management
1749 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1752 // Check we don't already have this file
1753 for (i
= 0; i
< m_fileHistoryN
; i
++)
1755 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1759 // Add to the project file history:
1760 // Move existing files (if any) down so we can insert file at beginning.
1762 // First delete filename that has popped off the end of the array (if any)
1763 if (m_fileHistoryN
== m_fileMaxFiles
)
1765 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1766 m_fileHistory
[m_fileMaxFiles
-1] = (char *) NULL
;
1768 if (m_fileHistoryN
< m_fileMaxFiles
)
1770 wxNode
* node
= m_fileMenus
.First();
1773 wxMenu
* menu
= (wxMenu
*) node
->Data();
1774 if (m_fileHistoryN
== 0)
1775 menu
->AppendSeparator();
1776 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1777 node
= node
->Next();
1781 // Shuffle filenames down
1782 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1784 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1786 m_fileHistory
[0] = copystring(file
);
1788 for (i
= 0; i
< m_fileHistoryN
; i
++)
1789 if (m_fileHistory
[i
])
1792 buf
.Printf("&%d %s", i
+1, m_fileHistory
[i
]);
1793 wxNode
* node
= m_fileMenus
.First();
1796 wxMenu
* menu
= (wxMenu
*) node
->Data();
1797 menu
->SetLabel(wxID_FILE1
+i
, buf
);
1798 node
= node
->Next();
1803 wxString
wxFileHistory::GetHistoryFile(int i
) const
1805 if (i
< m_fileHistoryN
)
1806 return wxString(m_fileHistory
[i
]);
1808 return wxString("");
1811 void wxFileHistory::UseMenu(wxMenu
*menu
)
1813 if (!m_fileMenus
.Member(menu
))
1814 m_fileMenus
.Append(menu
);
1817 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
1819 m_fileMenus
.DeleteObject(menu
);
1823 void wxFileHistory::Load(wxConfigBase
& config
)
1827 buf
.Printf("file%d", m_fileHistoryN
+1);
1828 wxString historyFile
;
1829 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= ""))
1831 m_fileHistory
[m_fileHistoryN
] = copystring((const char*) historyFile
);
1833 buf
.Printf("file%d", m_fileHistoryN
+1);
1839 void wxFileHistory::Save(wxConfigBase
& config
)
1842 for (i
= 0; i
< m_fileHistoryN
; i
++)
1845 buf
.Printf("file%d", i
+1);
1846 config
.Write(buf
, wxString(m_fileHistory
[i
]));
1851 void wxFileHistory::AddFilesToMenu()
1853 if (m_fileHistoryN
> 0)
1855 wxNode
* node
= m_fileMenus
.First();
1858 wxMenu
* menu
= (wxMenu
*) node
->Data();
1859 menu
->AppendSeparator();
1861 for (i
= 0; i
< m_fileHistoryN
; i
++)
1863 if (m_fileHistory
[i
])
1866 buf
.Printf("&%d %s", i
+1, m_fileHistory
[i
]);
1867 menu
->Append(wxID_FILE1
+i
, buf
);
1870 node
= node
->Next();
1875 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
1877 if (m_fileHistoryN
> 0)
1879 menu
->AppendSeparator();
1881 for (i
= 0; i
< m_fileHistoryN
; i
++)
1883 if (m_fileHistory
[i
])
1886 buf
.Printf("&%d %s", i
+1, m_fileHistory
[i
]);
1887 menu
->Append(wxID_FILE1
+i
, buf
);
1898 wxPrintInfo::wxPrintInfo(void)
1903 wxPrintInfo::~wxPrintInfo(void)
1909 * Permits compatibility with existing file formats and functions
1910 * that manipulate files directly
1913 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1918 if ((fd1
= fopen (WXSTRINGCAST filename
, "rb")) == NULL
)
1921 while ((ch
= getc (fd1
)) != EOF
)
1922 stream
<< (unsigned char)ch
;
1928 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1933 if ((fd1
= fopen (WXSTRINGCAST filename
, "wb")) == NULL
)
1938 while (!stream
.eof())
1949 // End wxUSE_DOC_VIEW_ARCHITECTURE