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 USE_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/generic/prntdlgg.h"
50 #include "wx/generic/printps.h"
51 #include "wx/confbase.h"
64 #if !USE_SHARED_LIBRARY
65 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
66 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
67 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
68 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
69 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
70 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
71 #if USE_PRINTING_ARCHITECTURE
72 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
74 IMPLEMENT_CLASS(wxCommand
, wxObject
)
75 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
76 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
77 // IMPLEMENT_DYNAMIC_CLASS(wxPrintInfo, wxObject)
81 * Definition of wxDocument
84 wxDocument::wxDocument(wxDocument
*parent
)
86 m_documentModified
=FALSE
;
89 m_documentParent
=parent
;
90 m_documentTemplate
= (wxDocTemplate
*) NULL
;
91 m_documentTypeName
= "";
95 bool wxDocument::DeleteContents(void)
100 wxDocument::~wxDocument(void)
104 if (m_commandProcessor
)
105 delete m_commandProcessor
;
107 GetDocumentManager()->RemoveDocument(this);
109 // Not safe to do here, since it'll
110 // invoke virtual view functions expecting to see
111 // valid derived objects: and by the time we get
112 // here, we've called destructors higher up.
116 bool wxDocument::Close(void)
118 if (OnSaveModified())
119 return OnCloseDocument();
124 bool wxDocument::OnCloseDocument(void)
131 // Note that this implicitly deletes the document when
132 // the last view is deleted.
133 bool wxDocument::DeleteAllViews(void)
135 wxNode
*node
= m_documentViews
.First();
138 wxView
*view
= (wxView
*)node
->Data();
142 wxNode
*next
= node
->Next();
144 delete view
; // Deletes node implicitly
150 wxView
*wxDocument::GetFirstView(void) const
152 if (m_documentViews
.Number() == 0)
153 return (wxView
*) NULL
;
154 return (wxView
*)m_documentViews
.First()->Data();
157 wxDocManager
*wxDocument::GetDocumentManager(void) const
159 return m_documentTemplate
->GetDocumentManager();
162 bool wxDocument::OnNewDocument(void)
164 if (!OnSaveModified())
167 if (OnCloseDocument()==FALSE
) return FALSE
;
170 SetDocumentSaved(FALSE
);
173 GetDocumentManager()->MakeDefaultName(name
);
175 SetFilename(name
, TRUE
);
180 bool wxDocument::Save(void)
184 if (!IsModified()) return TRUE
;
185 if (m_documentFile
== "" || !m_savedYet
)
188 ret
= OnSaveDocument(m_documentFile
);
190 SetDocumentSaved(TRUE
);
194 bool wxDocument::SaveAs(void)
196 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
200 char *tmp
= wxFileSelector(_("Save as"), docTemplate
->GetDirectory(), GetFilename(),
201 docTemplate
->GetDefaultExtension(), docTemplate
->GetFileFilter(),
202 wxSAVE
|wxOVERWRITE_PROMPT
, GetDocumentWindow());
208 wxString
fileName(tmp
);
212 wxSplitPath(fileName
, & path
, & name
, & ext
);
214 if (ext
.IsEmpty() || ext
== "")
217 fileName
+= docTemplate
->GetDefaultExtension();
220 SetFilename(fileName
);
221 SetTitle(wxFileNameFromPath(fileName
));
223 GetDocumentManager()->AddFileToHistory(fileName
);
225 // Notify the views that the filename has changed
226 wxNode
*node
= m_documentViews
.First();
229 wxView
*view
= (wxView
*)node
->Data();
230 view
->OnChangeFilename();
234 return OnSaveDocument(m_documentFile
);
237 bool wxDocument::OnSaveDocument(const wxString
& file
)
243 if (wxTheApp
->GetAppName() != "")
244 msgTitle
= wxTheApp
->GetAppName();
246 msgTitle
= wxString(_("File error"));
248 ofstream
store(file
);
249 if (store
.fail() || store
.bad())
251 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
252 GetDocumentWindow());
256 if (SaveObject(store
)==FALSE
)
258 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
259 GetDocumentWindow());
268 bool wxDocument::OnOpenDocument(const wxString
& file
)
270 if (!OnSaveModified())
274 if (wxTheApp
->GetAppName() != "")
275 msgTitle
= wxTheApp
->GetAppName();
277 msgTitle
= wxString(_("File error"));
279 ifstream
store(file
);
280 if (store
.fail() || store
.bad())
282 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
283 GetDocumentWindow());
286 if (LoadObject(store
)==FALSE
)
288 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
289 GetDocumentWindow());
292 SetFilename(file
, TRUE
);
301 istream
& wxDocument::LoadObject(istream
& stream
)
303 // wxObject::LoadObject(stream);
308 ostream
& wxDocument::SaveObject(ostream
& stream
)
310 // wxObject::SaveObject(stream);
315 bool wxDocument::Revert(void)
321 // Get title, or filename if no title, else unnamed
322 bool wxDocument::GetPrintableName(wxString
& buf
) const
324 if (m_documentTitle
!= "")
326 buf
= m_documentTitle
;
329 else if (m_documentFile
!= "")
331 buf
= wxFileNameFromPath(m_documentFile
);
341 wxWindow
*wxDocument::GetDocumentWindow(void) const
343 wxView
*view
= GetFirstView();
345 return view
->GetFrame();
347 return wxTheApp
->GetTopWindow();
350 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor(void)
352 return new wxCommandProcessor
;
355 // TRUE if safe to close
356 bool wxDocument::OnSaveModified(void)
362 GetPrintableName(title
);
365 if (wxTheApp
->GetAppName() != "")
366 msgTitle
= wxTheApp
->GetAppName();
368 msgTitle
= wxString(_("Warning"));
370 sprintf(buf
, _("Do you want to save changes to document %s?"), (const char *)title
);
371 int res
= wxMessageBox(buf
, msgTitle
, wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
372 GetDocumentWindow());
378 else if (res
== wxYES
)
380 else if (res
== wxCANCEL
)
386 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
391 bool wxDocument::AddView(wxView
*view
)
393 if (!m_documentViews
.Member(view
))
395 m_documentViews
.Append(view
);
401 bool wxDocument::RemoveView(wxView
*view
)
403 (void)m_documentViews
.DeleteObject(view
);
408 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
410 if (GetDocumentTemplate()->CreateView(this, flags
))
416 // Called after a view is added or removed.
417 // The default implementation deletes the document if
418 // there are no more views.
419 void wxDocument::OnChangedViewList(void)
421 if (m_documentViews
.Number() == 0)
423 if (OnSaveModified())
430 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
432 wxNode
*node
= m_documentViews
.First();
435 wxView
*view
= (wxView
*)node
->Data();
436 view
->OnUpdate(sender
, hint
);
441 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
443 m_documentFile
= filename
;
446 // Notify the views that the filename has changed
447 wxNode
*node
= m_documentViews
.First();
450 wxView
*view
= (wxView
*)node
->Data();
451 view
->OnChangeFilename();
462 wxView::wxView(wxDocument
*doc
)
467 m_viewFrame
= (wxFrame
*) NULL
;
470 wxView::~wxView(void)
472 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
473 m_viewDocument
->RemoveView(this);
476 // Extend event processing to search the document's event table
477 bool wxView::ProcessEvent(wxEvent
& event
)
479 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
480 return wxEvtHandler::ProcessEvent(event
);
485 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
489 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
494 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
498 void wxView::OnChangeFilename(void)
500 if (GetFrame() && GetDocument())
503 GetDocument()->GetPrintableName(name
);
505 GetFrame()->SetTitle(name
);
509 void wxView::SetDocument(wxDocument
*doc
)
511 m_viewDocument
= doc
;
516 bool wxView::Close(bool deleteWindow
)
518 if (OnClose(deleteWindow
))
524 void wxView::Activate(bool activate
)
526 if (GetDocumentManager())
528 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
529 GetDocumentManager()->ActivateView(this, activate
);
533 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
535 return GetDocument() ? GetDocument()->Close() : TRUE
;
538 #if USE_PRINTING_ARCHITECTURE
539 wxPrintout
*wxView::OnCreatePrintout(void)
541 return new wxDocPrintout(this);
550 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
,
551 const wxString
& filter
, const wxString
& dir
, const wxString
& ext
,
552 const wxString
& docTypeName
, const wxString
& viewTypeName
,
553 wxClassInfo
*docClassInfo
, wxClassInfo
*viewClassInfo
, long flags
)
555 m_documentManager
= manager
;
557 m_description
= descr
;
560 m_fileFilter
= filter
;
562 m_docTypeName
= docTypeName
;
563 m_viewTypeName
= viewTypeName
;
564 m_documentManager
->AssociateTemplate(this);
566 m_docClassInfo
= docClassInfo
;
567 m_viewClassInfo
= viewClassInfo
;
570 wxDocTemplate::~wxDocTemplate(void)
572 m_documentManager
->DisassociateTemplate(this);
575 // Tries to dynamically construct an object of the right
577 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
580 return (wxDocument
*) NULL
;
581 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
582 doc
->SetFilename(path
);
583 doc
->SetDocumentTemplate(this);
584 GetDocumentManager()->AddDocument(doc
);
585 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
587 if (doc
->OnCreate(path
, flags
))
592 return (wxDocument
*) NULL
;
596 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
598 if (!m_viewClassInfo
)
599 return (wxView
*) NULL
;
600 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
601 view
->SetDocument(doc
);
602 if (view
->OnCreate(doc
, flags
))
609 return (wxView
*) NULL
;
613 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
614 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
615 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
616 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
617 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
618 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
619 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
620 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
621 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
622 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
623 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
624 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
627 wxDocManager::wxDocManager(long flags
, bool initialize
)
629 m_defaultDocumentNameCounter
= 1;
631 m_currentView
= (wxView
*) NULL
;
632 m_maxDocsOpen
= 10000;
633 m_fileHistory
= (wxFileHistory
*) NULL
;
638 wxDocManager::~wxDocManager(void)
642 delete m_fileHistory
;
645 bool wxDocManager::Clear(bool force
)
647 wxNode
*node
= m_docs
.First();
650 wxDocument
*doc
= (wxDocument
*)node
->Data();
651 wxNode
*next
= node
->Next();
653 if (!doc
->Close() && !force
)
656 // Implicitly deletes the document when the last
657 // view is removed (deleted)
658 doc
->DeleteAllViews();
660 // Check document is deleted
661 if (m_docs
.Member(doc
))
664 // This assumes that documents are not connected in
665 // any way, i.e. deleting one document does NOT
669 node
= m_templates
.First();
672 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
673 wxNode
* next
= node
->Next();
680 bool wxDocManager::Initialize(void)
682 m_fileHistory
= OnCreateFileHistory();
686 wxFileHistory
*wxDocManager::OnCreateFileHistory(void)
688 return new wxFileHistory
;
691 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
693 wxDocument
*doc
= GetCurrentDocument();
698 doc
->DeleteAllViews();
699 if (m_docs
.Member(doc
))
704 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
706 CreateDocument(wxString(""), wxDOC_NEW
);
709 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
711 CreateDocument(wxString(""), 0);
714 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
716 wxDocument
*doc
= GetCurrentDocument();
722 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
724 wxDocument
*doc
= GetCurrentDocument();
730 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
732 wxDocument
*doc
= GetCurrentDocument();
738 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
740 wxView
*view
= GetCurrentView();
744 wxPrintout
*printout
= view
->OnCreatePrintout();
747 // TODO: trouble about this is that it pulls in the postscript
750 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
752 wxWindowsPrinter printer
;
753 printer
.Print(view
->GetFrame(), printout
, TRUE
);
758 wxPostScriptPrinter printer
;
759 printer
.Print(view
->GetFrame(), printout
, TRUE
);
766 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
768 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
769 wxView
*view
= GetCurrentView();
771 parentWin
= view
->GetFrame();
776 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
778 wxPrintDialog
printerDialog(parentWin
, & data
);
779 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
780 printerDialog
.ShowModal();
785 wxGenericPrintDialog
printerDialog(parentWin
, & data
);
786 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
787 printerDialog
.ShowModal();
791 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
793 wxView
*view
= GetCurrentView();
797 wxPrintout
*printout
= view
->OnCreatePrintout();
800 // Pass two printout objects: for preview, and possible printing.
801 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
803 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
804 preview
= new wxWindowsPrintPreview(printout
, view
->OnCreatePrintout());
807 preview
= new wxPostScriptPrintPreview(printout
, view
->OnCreatePrintout());
809 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
810 wxPoint(100, 100), wxSize(600, 650));
811 frame
->Centre(wxBOTH
);
817 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
819 wxDocument
*doc
= GetCurrentDocument();
822 if (doc
->GetCommandProcessor())
823 doc
->GetCommandProcessor()->Undo();
826 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
828 wxDocument
*doc
= GetCurrentDocument();
831 if (doc
->GetCommandProcessor())
832 doc
->GetCommandProcessor()->Redo();
835 wxView
*wxDocManager::GetCurrentView(void) const
838 return m_currentView
;
839 if (m_docs
.Number() == 1)
841 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
842 return doc
->GetFirstView();
844 return (wxView
*) NULL
;
847 // Extend event processing to search the view's event table
848 bool wxDocManager::ProcessEvent(wxEvent
& event
)
850 wxView
* view
= GetCurrentView();
853 if (view
->ProcessEvent(event
))
856 return wxEvtHandler::ProcessEvent(event
);
859 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
861 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
864 for (i
= 0; i
< m_templates
.Number(); i
++)
866 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
867 if (temp
->IsVisible())
876 return (wxDocument
*) NULL
;
879 // If we've reached the max number of docs, close the
881 if (GetDocuments().Number() >= m_maxDocsOpen
)
883 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
886 // Implicitly deletes the document when
887 // the last view is deleted
888 doc
->DeleteAllViews();
890 // Check we're really deleted
891 if (m_docs
.Member(doc
))
895 return (wxDocument
*) NULL
;
898 // New document: user chooses a template, unless there's only one.
899 if (flags
& wxDOC_NEW
)
903 wxDocTemplate
*temp
= templates
[0];
905 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
908 newDoc
->SetDocumentName(temp
->GetDocumentName());
909 newDoc
->SetDocumentTemplate(temp
);
910 newDoc
->OnNewDocument();
915 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
919 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
922 newDoc
->SetDocumentName(temp
->GetDocumentName());
923 newDoc
->SetDocumentTemplate(temp
);
924 newDoc
->OnNewDocument();
929 return (wxDocument
*) NULL
;
933 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
939 if (flags
& wxDOC_SILENT
)
940 temp
= FindTemplateForPath(path2
);
942 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
948 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
951 newDoc
->SetDocumentName(temp
->GetDocumentName());
952 newDoc
->SetDocumentTemplate(temp
);
953 if (!newDoc
->OnOpenDocument(path2
))
956 return (wxDocument
*) NULL
;
958 AddFileToHistory(path2
);
963 return (wxDocument
*) NULL
;
966 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
968 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
971 for (i
= 0; i
< m_templates
.Number(); i
++)
973 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
974 if (temp
->IsVisible())
976 if (temp
->GetDocumentName() == doc
->GetDocumentName())
986 return (wxView
*) NULL
;
990 wxDocTemplate
*temp
= templates
[0];
992 wxView
*view
= temp
->CreateView(doc
, flags
);
994 view
->SetViewName(temp
->GetViewName());
998 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1002 wxView
*view
= temp
->CreateView(doc
, flags
);
1004 view
->SetViewName(temp
->GetViewName());
1008 return (wxView
*) NULL
;
1011 // Not yet implemented
1012 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1016 // Not yet implemented
1017 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1022 wxDocument
*wxDocManager::GetCurrentDocument(void) const
1025 return m_currentView
->GetDocument();
1027 return (wxDocument
*) NULL
;
1030 // Make a default document name
1031 bool wxDocManager::MakeDefaultName(wxString
& name
)
1034 sprintf(buf
, _("unnamed%d"), m_defaultDocumentNameCounter
);
1035 m_defaultDocumentNameCounter
++;
1040 // Not yet implemented
1041 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1043 return (wxDocTemplate
*) NULL
;
1046 // File history management
1047 void wxDocManager::AddFileToHistory(const wxString
& file
)
1050 m_fileHistory
->AddFileToHistory(file
);
1053 wxString
wxDocManager::GetHistoryFile(int i
) const
1056 return wxString(m_fileHistory
->GetHistoryFile(i
));
1058 return wxString("");
1061 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1064 m_fileHistory
->UseMenu(menu
);
1067 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1070 m_fileHistory
->RemoveMenu(menu
);
1073 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1076 m_fileHistory
->Load(config
);
1079 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1082 m_fileHistory
->Save(config
);
1085 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1088 m_fileHistory
->AddFilesToMenu(menu
);
1091 void wxDocManager::FileHistoryAddFilesToMenu()
1094 m_fileHistory
->AddFilesToMenu();
1097 int wxDocManager::GetNoHistoryFiles(void) const
1100 return m_fileHistory
->GetNoHistoryFiles();
1105 static char *FindExtension(char *path
)
1107 static char ext
[10];
1108 int len
= strlen(path
);
1112 for (i
= (len
-1); i
> 0; i
--)
1118 for (j
= i
+1; j
< len
; j
++)
1119 ext
[(int)(j
-(i
+1))] = (char)wxToLower(path
[j
]); // NOTE Should not use tolower under UNIX
1124 return (char *) NULL
;
1126 else return (char *) NULL
;
1130 // Given a path, try to find a matching template. Won't
1131 // always work, of course.
1132 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1134 char *theExt
= FindExtension((char *)(const char *)path
);
1136 return (wxDocTemplate
*) NULL
;
1137 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1139 if (m_templates
.Number() == 1)
1140 return (wxDocTemplate
*)m_templates
.First()->Data();
1142 // Find the template which this extension corresponds to
1144 for (i
= 0; i
< m_templates
.Number(); i
++)
1146 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1147 if (strcmp(temp
->GetDefaultExtension(), theExt
) == 0)
1156 // Prompts user to open a file, using file specs in templates.
1157 // How to implement in wxWindows? Must extend the file selector
1158 // dialog or implement own; OR match the extension to the
1159 // template extension.
1160 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1161 int noTemplates
, wxString
& path
, long WXUNUSED(flags
), bool WXUNUSED(save
))
1163 // We can only have multiple filters in Windows
1165 char *descrBuf
= new char[1000];
1168 for (i
= 0; i
< noTemplates
; i
++)
1170 if (templates
[i
]->IsVisible())
1172 strcat(descrBuf
, templates
[i
]->GetDescription());
1173 strcat(descrBuf
, " (");
1174 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1175 strcat(descrBuf
, ") ");
1176 strcat(descrBuf
, "|");
1177 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1178 strcat(descrBuf
, "|");
1181 int len
= strlen(descrBuf
);
1184 descrBuf
[len
-1] = 0;
1186 char *pathTmp
= wxFileSelector(_("Select a file"), "", "", "", descrBuf
, 0, wxTheApp
->GetTopWindow());
1191 char *theExt
= FindExtension((char *)(const char *)path
);
1193 return (wxDocTemplate
*) NULL
;
1195 // This is dodgy in that we're selecting the template on the
1196 // basis of the file extension, which may not be a standard
1197 // one. We really want to know exactly which template was
1198 // chosen by using a more advanced file selector.
1199 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1205 return (wxDocTemplate
*) NULL
;
1208 // In all other windowing systems, until we have more advanced
1209 // file selectors, we must select the document type (template) first, and
1210 // _then_ pop up the file selector.
1211 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1213 return (wxDocTemplate
*) NULL
;
1215 char *pathTmp
= wxFileSelector(_("Select a file"), "", "",
1216 temp
->GetDefaultExtension(),
1217 temp
->GetFileFilter(),
1218 0, wxTheApp
->GetTopWindow());
1226 return (wxDocTemplate
*) NULL
;
1230 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1233 char **strings
= new char *[noTemplates
];
1234 char **data
= new char *[noTemplates
];
1237 for (i
= 0; i
< noTemplates
; i
++)
1239 if (templates
[i
]->IsVisible())
1241 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1242 data
[n
] = (char *)templates
[i
];
1250 return (wxDocTemplate
*) NULL
;
1254 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1260 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document template"), _("Templates"), n
,
1267 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1270 char **strings
= new char *[noTemplates
];
1271 char **data
= new char *[noTemplates
];
1274 for (i
= 0; i
< noTemplates
; i
++)
1276 if (templates
[i
]->IsVisible() && templates
[i
]->GetViewName())
1278 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1279 data
[n
] = (char *)templates
[i
];
1283 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document view"), _("Views"), n
,
1290 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1292 if (!m_templates
.Member(temp
))
1293 m_templates
.Append(temp
);
1296 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1298 m_templates
.DeleteObject(temp
);
1301 // Add and remove a document from the manager's list
1302 void wxDocManager::AddDocument(wxDocument
*doc
)
1304 if (!m_docs
.Member(doc
))
1308 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1310 m_docs
.DeleteObject(doc
);
1313 // Views or windows should inform the document manager
1314 // when a view is going in or out of focus
1315 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1317 // If we're deactiving, and if we're not actually deleting the view, then
1318 // don't reset the current view because we may be going to
1319 // a window without a view.
1320 // WHAT DID I MEAN BY THAT EXACTLY?
1324 if (m_currentView == view)
1325 m_currentView = NULL;
1331 m_currentView
= view
;
1333 m_currentView
= (wxView
*) NULL
;
1338 * Default document child frame
1341 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1342 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1345 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1346 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1347 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1349 m_childDocument
= doc
;
1352 view
->SetFrame(this);
1355 wxDocChildFrame::~wxDocChildFrame(void)
1359 // Extend event processing to search the view's event table
1360 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1363 m_childView
->Activate(TRUE
);
1365 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1367 // Only hand up to the parent if it's a menu command
1368 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1369 return wxEvtHandler::ProcessEvent(event
);
1377 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1379 wxFrame::OnActivate(event
);
1382 m_childView
->Activate(event
.GetActive());
1385 bool wxDocChildFrame::OnClose(void)
1387 // Close view but don't delete the frame while doing so!
1388 // ...since it will be deleted by wxWindows if we return TRUE.
1391 bool ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1394 m_childView
->Activate(FALSE
);
1396 m_childView
= (wxView
*) NULL
;
1397 m_childDocument
= (wxDocument
*) NULL
;
1406 * Default parent frame
1409 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1410 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1411 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1414 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1415 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1416 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1418 m_docManager
= manager
;
1421 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1426 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1428 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1430 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1433 // Extend event processing to search the view's event table
1434 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1436 // Try the document manager, then do default processing
1437 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1438 return wxEvtHandler::ProcessEvent(event
);
1443 // Define the behaviour for the frame closing
1444 // - must delete all frames except for the main one.
1445 bool wxDocParentFrame::OnClose(void)
1447 return m_docManager
->Clear(FALSE
);
1450 #if USE_PRINTING_ARCHITECTURE
1452 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
):
1453 wxPrintout(WXSTRINGCAST title
)
1455 m_printoutView
= view
;
1458 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1462 // Get the logical pixels per inch of screen and printer
1463 int ppiScreenX
, ppiScreenY
;
1464 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1465 int ppiPrinterX
, ppiPrinterY
;
1466 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1468 // This scales the DC so that the printout roughly represents the
1469 // the screen scaling. The text point size _should_ be the right size
1470 // but in fact is too small for some reason. This is a detail that will
1471 // need to be addressed at some point but can be fudged for the
1473 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1475 // Now we have to check in case our real page size is reduced
1476 // (e.g. because we're drawing to a print preview memory DC)
1477 int pageWidth
, pageHeight
;
1479 dc
->GetSize(&w
, &h
);
1480 GetPageSizePixels(&pageWidth
, &pageHeight
);
1482 // If printer pageWidth == current DC width, then this doesn't
1483 // change. But w might be the preview bitmap width, so scale down.
1484 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1485 dc
->SetUserScale(overallScale
, overallScale
);
1489 m_printoutView
->OnDraw(dc
);
1494 bool wxDocPrintout::HasPage(int pageNum
)
1496 return (pageNum
== 1);
1499 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1501 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1507 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1518 * Command processing framework
1521 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1523 m_canUndo
= canUndoIt
;
1524 m_commandName
= name
;
1527 wxCommand::~wxCommand(void)
1531 // Command processor
1532 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1534 m_maxNoCommands
= maxCommands
;
1535 m_currentCommand
= (wxNode
*) NULL
;
1536 m_commandEditMenu
= (wxMenu
*) NULL
;
1539 wxCommandProcessor::~wxCommandProcessor(void)
1544 // Pass a command to the processor. The processor calls Do();
1545 // if successful, is appended to the command history unless
1546 // storeIt is FALSE.
1547 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1549 bool success
= command
->Do();
1550 if (success
&& storeIt
)
1552 if (m_commands
.Number() == m_maxNoCommands
)
1554 wxNode
*firstNode
= m_commands
.First();
1555 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1556 delete firstCommand
;
1560 // Correct a bug: we must chop off the current 'branch'
1561 // so that we're at the end of the command list.
1562 if (!m_currentCommand
)
1566 wxNode
*node
= m_currentCommand
->Next();
1569 wxNode
*next
= node
->Next();
1570 delete (wxCommand
*)node
->Data();
1576 m_commands
.Append(command
);
1577 m_currentCommand
= m_commands
.Last();
1583 bool wxCommandProcessor::Undo(void)
1585 if (m_currentCommand
)
1587 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1588 if (command
->CanUndo())
1590 bool success
= command
->Undo();
1593 m_currentCommand
= m_currentCommand
->Previous();
1602 bool wxCommandProcessor::Redo(void)
1604 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
1605 wxNode
*redoNode
= (wxNode
*) NULL
;
1606 if (m_currentCommand
&& m_currentCommand
->Next())
1608 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1609 redoNode
= m_currentCommand
->Next();
1613 if (m_commands
.Number() > 0)
1615 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1616 redoNode
= m_commands
.First();
1622 bool success
= redoCommand
->Do();
1625 m_currentCommand
= redoNode
;
1633 bool wxCommandProcessor::CanUndo(void) const
1635 if (m_currentCommand
)
1636 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1640 bool wxCommandProcessor::CanRedo(void) const
1642 return ((m_currentCommand
&& m_currentCommand
->Next()));
1645 void wxCommandProcessor::Initialize(void)
1647 m_currentCommand
= m_commands
.Last();
1651 void wxCommandProcessor::SetMenuStrings(void)
1653 if (m_commandEditMenu
)
1656 if (m_currentCommand
)
1658 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1659 wxString
commandName(command
->GetName());
1660 if (commandName
== "") commandName
= _("Unnamed command");
1661 bool canUndo
= command
->CanUndo();
1663 buf
= wxString(_("&Undo ")) + commandName
;
1665 buf
= wxString(_("Can't &Undo ")) + commandName
;
1667 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1668 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1670 // We can redo, if we're not at the end of the history.
1671 if (m_currentCommand
->Next())
1673 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1674 wxString
redoCommandName(redoCommand
->GetName());
1675 if (redoCommandName
== "") redoCommandName
= _("Unnamed command");
1676 buf
= wxString(_("&Redo ")) + redoCommandName
;
1677 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1678 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1682 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1683 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1688 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
1689 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1691 if (m_commands
.Number() == 0)
1693 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1694 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1698 // currentCommand is NULL but there are commands: this means that
1699 // we've undone to the start of the list, but can redo the first.
1700 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1701 wxString
redoCommandName(redoCommand
->GetName());
1702 if (!redoCommandName
) redoCommandName
= _("Unnamed command");
1703 buf
= wxString(_("&Redo ")) + redoCommandName
;
1704 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1705 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1711 void wxCommandProcessor::ClearCommands(void)
1713 wxNode
*node
= m_commands
.First();
1716 wxCommand
*command
= (wxCommand
*)node
->Data();
1719 node
= m_commands
.First();
1721 m_currentCommand
= (wxNode
*) NULL
;
1726 * File history processor
1729 wxFileHistory::wxFileHistory(int maxFiles
)
1731 m_fileMaxFiles
= maxFiles
;
1733 m_fileHistory
= new char *[m_fileMaxFiles
];
1736 wxFileHistory::~wxFileHistory(void)
1739 for (i
= 0; i
< m_fileHistoryN
; i
++)
1740 delete[] m_fileHistory
[i
];
1741 delete[] m_fileHistory
;
1744 // File history management
1745 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1748 // Check we don't already have this file
1749 for (i
= 0; i
< m_fileHistoryN
; i
++)
1751 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1755 // Add to the project file history:
1756 // Move existing files (if any) down so we can insert file at beginning.
1758 // First delete filename that has popped off the end of the array (if any)
1759 if (m_fileHistoryN
== m_fileMaxFiles
)
1761 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1762 m_fileHistory
[m_fileMaxFiles
-1] = (char *) NULL
;
1764 if (m_fileHistoryN
< m_fileMaxFiles
)
1766 wxNode
* node
= m_fileMenus
.First();
1769 wxMenu
* menu
= (wxMenu
*) node
->Data();
1770 if (m_fileHistoryN
== 0)
1771 menu
->AppendSeparator();
1772 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1773 node
= node
->Next();
1777 // Shuffle filenames down
1778 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1780 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1782 m_fileHistory
[0] = copystring(file
);
1784 for (i
= 0; i
< m_fileHistoryN
; i
++)
1785 if (m_fileHistory
[i
])
1788 sprintf(buf
, "&%d %s", i
+1, m_fileHistory
[i
]);
1789 wxNode
* node
= m_fileMenus
.First();
1792 wxMenu
* menu
= (wxMenu
*) node
->Data();
1793 menu
->SetLabel(wxID_FILE1
+i
, buf
);
1794 node
= node
->Next();
1799 wxString
wxFileHistory::GetHistoryFile(int i
) const
1801 if (i
< m_fileHistoryN
)
1802 return wxString(m_fileHistory
[i
]);
1804 return wxString("");
1807 void wxFileHistory::UseMenu(wxMenu
*menu
)
1809 if (!m_fileMenus
.Member(menu
))
1810 m_fileMenus
.Append(menu
);
1813 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
1815 m_fileMenus
.DeleteObject(menu
);
1818 void wxFileHistory::Load(wxConfigBase
& config
)
1822 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1823 wxString
historyFile("");
1824 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= ""))
1826 m_fileHistory
[m_fileHistoryN
] = copystring((const char*) historyFile
);
1828 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1834 void wxFileHistory::Save(wxConfigBase
& config
)
1837 for (i
= 0; i
< m_fileHistoryN
; i
++)
1840 buf
.Printf("file%d", i
+1);
1841 config
.Write(buf
, wxString(m_fileHistory
[i
]));
1845 void wxFileHistory::AddFilesToMenu()
1847 if (m_fileHistoryN
> 0)
1849 wxNode
* node
= m_fileMenus
.First();
1852 wxMenu
* menu
= (wxMenu
*) node
->Data();
1853 menu
->AppendSeparator();
1855 for (i
= 0; i
< m_fileHistoryN
; i
++)
1857 if (m_fileHistory
[i
])
1860 buf
.Printf("&%d %s", i
+1, m_fileHistory
[i
]);
1861 menu
->Append(wxID_FILE1
+i
, buf
);
1864 node
= node
->Next();
1869 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
1871 if (m_fileHistoryN
> 0)
1873 menu
->AppendSeparator();
1875 for (i
= 0; i
< m_fileHistoryN
; i
++)
1877 if (m_fileHistory
[i
])
1880 buf
.Printf("&%d %s", i
+1, m_fileHistory
[i
]);
1881 menu
->Append(wxID_FILE1
+i
, buf
);
1892 wxPrintInfo::wxPrintInfo(void)
1897 wxPrintInfo::~wxPrintInfo(void)
1903 * Permits compatibility with existing file formats and functions
1904 * that manipulate files directly
1907 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1912 if ((fd1
= fopen (WXSTRINGCAST filename
, "rb")) == NULL
)
1915 while ((ch
= getc (fd1
)) != EOF
)
1916 stream
<< (unsigned char)ch
;
1922 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1927 if ((fd1
= fopen (WXSTRINGCAST filename
, "wb")) == NULL
)
1932 while (!stream
.eof())
1943 // End USE_DOC_VIEW_ARCHITECTURE