1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Document/view classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "docview.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #if wxUSE_DOC_VIEW_ARCHITECTURE
38 #include "wx/string.h"
42 #include "wx/dialog.h"
45 #include "wx/filedlg.h"
53 #include "wx/msgdlg.h"
54 #include "wx/choicdlg.h"
55 #include "wx/docview.h"
56 #include "wx/printdlg.h"
57 #include "wx/confbase.h"
62 #include "wx/ioswrap.h"
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 #if !USE_SHARED_LIBRARY
75 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
76 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
77 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
78 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
79 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
80 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
82 #if wxUSE_PRINTING_ARCHITECTURE
83 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
86 IMPLEMENT_CLASS(wxCommand
, wxObject
)
87 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
88 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
91 // ----------------------------------------------------------------------------
92 // function prototypes
93 // ----------------------------------------------------------------------------
95 static inline wxString
FindExtension(const char *path
);
97 // ============================================================================
99 // ============================================================================
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 static wxString
FindExtension(const char *path
)
108 wxSplitPath(path
, NULL
, NULL
, &ext
);
110 // VZ: extensions are considered not case sensitive - is this really a good
112 return ext
.MakeLower();
115 // ----------------------------------------------------------------------------
116 // Definition of wxDocument
117 // ----------------------------------------------------------------------------
119 wxDocument::wxDocument(wxDocument
*parent
)
121 m_documentModified
= FALSE
;
122 m_documentParent
= parent
;
123 m_documentTemplate
= (wxDocTemplate
*) NULL
;
127 bool wxDocument::DeleteContents()
132 wxDocument::~wxDocument()
136 if (m_commandProcessor
)
137 delete m_commandProcessor
;
139 GetDocumentManager()->RemoveDocument(this);
141 // Not safe to do here, since it'll invoke virtual view functions
142 // expecting to see valid derived objects: and by the time we get here,
143 // we've called destructors higher up.
147 bool wxDocument::Close()
149 if (OnSaveModified())
150 return OnCloseDocument();
155 bool wxDocument::OnCloseDocument()
162 // Note that this implicitly deletes the document when the last view is
164 bool wxDocument::DeleteAllViews()
166 wxNode
*node
= m_documentViews
.First();
169 wxView
*view
= (wxView
*)node
->Data();
173 wxNode
*next
= node
->Next();
175 delete view
; // Deletes node implicitly
181 wxView
*wxDocument::GetFirstView(void) const
183 if (m_documentViews
.Number() == 0)
184 return (wxView
*) NULL
;
185 return (wxView
*)m_documentViews
.First()->Data();
188 wxDocManager
*wxDocument::GetDocumentManager(void) const
190 return m_documentTemplate
->GetDocumentManager();
193 bool wxDocument::OnNewDocument()
195 if (!OnSaveModified())
198 if (OnCloseDocument()==FALSE
) return FALSE
;
201 SetDocumentSaved(FALSE
);
204 GetDocumentManager()->MakeDefaultName(name
);
206 SetFilename(name
, TRUE
);
211 bool wxDocument::Save()
215 if (!IsModified()) return TRUE
;
216 if (m_documentFile
== "" || !m_savedYet
)
219 ret
= OnSaveDocument(m_documentFile
);
221 SetDocumentSaved(TRUE
);
225 bool wxDocument::SaveAs()
227 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
231 wxString tmp
= wxFileSelector(_("Save as"),
232 docTemplate
->GetDirectory(),
234 docTemplate
->GetDefaultExtension(),
235 docTemplate
->GetFileFilter(),
236 wxSAVE
| wxOVERWRITE_PROMPT
,
237 GetDocumentWindow());
242 wxString
fileName(tmp
);
246 wxSplitPath(fileName
, & path
, & name
, & ext
);
248 if (ext
.IsEmpty() || ext
== "")
251 fileName
+= docTemplate
->GetDefaultExtension();
254 SetFilename(fileName
);
255 SetTitle(wxFileNameFromPath(fileName
));
257 GetDocumentManager()->AddFileToHistory(fileName
);
259 // Notify the views that the filename has changed
260 wxNode
*node
= m_documentViews
.First();
263 wxView
*view
= (wxView
*)node
->Data();
264 view
->OnChangeFilename();
268 return OnSaveDocument(m_documentFile
);
271 bool wxDocument::OnSaveDocument(const wxString
& file
)
277 if (wxTheApp
->GetAppName() != "")
278 msgTitle
= wxTheApp
->GetAppName();
280 msgTitle
= wxString(_("File error"));
282 ofstream
store(file
);
283 if (store
.fail() || store
.bad())
285 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
286 GetDocumentWindow());
290 if (SaveObject(store
)==FALSE
)
292 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
293 GetDocumentWindow());
302 bool wxDocument::OnOpenDocument(const wxString
& file
)
304 if (!OnSaveModified())
308 if (wxTheApp
->GetAppName() != "")
309 msgTitle
= wxTheApp
->GetAppName();
311 msgTitle
= wxString(_("File error"));
313 ifstream
store(file
);
314 if (store
.fail() || store
.bad())
316 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
317 GetDocumentWindow());
320 if (LoadObject(store
)==FALSE
)
322 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
323 GetDocumentWindow());
326 SetFilename(file
, TRUE
);
335 istream
& wxDocument::LoadObject(istream
& stream
)
337 // wxObject::LoadObject(stream);
342 ostream
& wxDocument::SaveObject(ostream
& stream
)
344 // wxObject::SaveObject(stream);
349 bool wxDocument::Revert()
355 // Get title, or filename if no title, else unnamed
356 bool wxDocument::GetPrintableName(wxString
& buf
) const
358 if (m_documentTitle
!= "")
360 buf
= m_documentTitle
;
363 else if (m_documentFile
!= "")
365 buf
= wxFileNameFromPath(m_documentFile
);
375 wxWindow
*wxDocument::GetDocumentWindow(void) const
377 wxView
*view
= GetFirstView();
379 return view
->GetFrame();
381 return wxTheApp
->GetTopWindow();
384 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
386 return new wxCommandProcessor
;
389 // TRUE if safe to close
390 bool wxDocument::OnSaveModified()
395 GetPrintableName(title
);
398 if (wxTheApp
->GetAppName() != "")
399 msgTitle
= wxTheApp
->GetAppName();
401 msgTitle
= wxString(_("Warning"));
404 prompt
.Printf(_("Do you want to save changes to document %s?"),
405 (const char *)title
);
406 int res
= wxMessageBox(prompt
, msgTitle
,
407 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
408 GetDocumentWindow());
414 else if (res
== wxYES
)
416 else if (res
== wxCANCEL
)
422 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
427 bool wxDocument::AddView(wxView
*view
)
429 if (!m_documentViews
.Member(view
))
431 m_documentViews
.Append(view
);
437 bool wxDocument::RemoveView(wxView
*view
)
439 (void)m_documentViews
.DeleteObject(view
);
444 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
446 if (GetDocumentTemplate()->CreateView(this, flags
))
452 // Called after a view is added or removed.
453 // The default implementation deletes the document if
454 // there are no more views.
455 void wxDocument::OnChangedViewList()
457 if (m_documentViews
.Number() == 0)
459 if (OnSaveModified())
466 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
468 wxNode
*node
= m_documentViews
.First();
471 wxView
*view
= (wxView
*)node
->Data();
472 view
->OnUpdate(sender
, hint
);
477 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
479 m_documentFile
= filename
;
482 // Notify the views that the filename has changed
483 wxNode
*node
= m_documentViews
.First();
486 wxView
*view
= (wxView
*)node
->Data();
487 view
->OnChangeFilename();
493 // ----------------------------------------------------------------------------
495 // ----------------------------------------------------------------------------
500 m_viewDocument
= (wxDocument
*) NULL
;
503 m_viewFrame
= (wxFrame
*) NULL
;
508 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
509 m_viewDocument
->RemoveView(this);
512 // Extend event processing to search the document's event table
513 bool wxView::ProcessEvent(wxEvent
& event
)
515 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
516 return wxEvtHandler::ProcessEvent(event
);
521 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
525 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
530 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
534 void wxView::OnChangeFilename()
536 if (GetFrame() && GetDocument())
539 GetDocument()->GetPrintableName(name
);
541 GetFrame()->SetTitle(name
);
545 void wxView::SetDocument(wxDocument
*doc
)
547 m_viewDocument
= doc
;
552 bool wxView::Close(bool deleteWindow
)
554 if (OnClose(deleteWindow
))
560 void wxView::Activate(bool activate
)
562 if (GetDocumentManager())
564 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
565 GetDocumentManager()->ActivateView(this, activate
);
569 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
571 return GetDocument() ? GetDocument()->Close() : TRUE
;
574 #if wxUSE_PRINTING_ARCHITECTURE
575 wxPrintout
*wxView::OnCreatePrintout()
577 return new wxDocPrintout(this);
581 // ----------------------------------------------------------------------------
583 // ----------------------------------------------------------------------------
585 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
586 const wxString
& descr
,
587 const wxString
& filter
,
590 const wxString
& docTypeName
,
591 const wxString
& viewTypeName
,
592 wxClassInfo
*docClassInfo
,
593 wxClassInfo
*viewClassInfo
,
596 m_documentManager
= manager
;
598 m_description
= descr
;
601 m_fileFilter
= filter
;
603 m_docTypeName
= docTypeName
;
604 m_viewTypeName
= viewTypeName
;
605 m_documentManager
->AssociateTemplate(this);
607 m_docClassInfo
= docClassInfo
;
608 m_viewClassInfo
= viewClassInfo
;
611 wxDocTemplate::~wxDocTemplate()
613 m_documentManager
->DisassociateTemplate(this);
616 // Tries to dynamically construct an object of the right class.
617 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
620 return (wxDocument
*) NULL
;
621 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
622 doc
->SetFilename(path
);
623 doc
->SetDocumentTemplate(this);
624 GetDocumentManager()->AddDocument(doc
);
625 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
627 if (doc
->OnCreate(path
, flags
))
632 return (wxDocument
*) NULL
;
636 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
638 if (!m_viewClassInfo
)
639 return (wxView
*) NULL
;
640 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
641 view
->SetDocument(doc
);
642 if (view
->OnCreate(doc
, flags
))
649 return (wxView
*) NULL
;
653 // ----------------------------------------------------------------------------
655 // ----------------------------------------------------------------------------
657 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
658 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
659 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
660 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
661 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
662 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
663 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
664 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
665 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
666 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
667 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
668 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
671 wxDocManager::wxDocManager(long flags
, bool initialize
)
673 m_defaultDocumentNameCounter
= 1;
675 m_currentView
= (wxView
*) NULL
;
676 m_maxDocsOpen
= 10000;
677 m_fileHistory
= (wxFileHistory
*) NULL
;
682 wxDocManager::~wxDocManager()
686 delete m_fileHistory
;
689 bool wxDocManager::Clear(bool force
)
691 wxNode
*node
= m_docs
.First();
694 wxDocument
*doc
= (wxDocument
*)node
->Data();
695 wxNode
*next
= node
->Next();
697 if (!doc
->Close() && !force
)
700 // Implicitly deletes the document when the last
701 // view is removed (deleted)
702 doc
->DeleteAllViews();
704 // Check document is deleted
705 if (m_docs
.Member(doc
))
708 // This assumes that documents are not connected in
709 // any way, i.e. deleting one document does NOT
713 node
= m_templates
.First();
716 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
717 wxNode
* next
= node
->Next();
724 bool wxDocManager::Initialize()
726 m_fileHistory
= OnCreateFileHistory();
730 wxFileHistory
*wxDocManager::OnCreateFileHistory()
732 return new wxFileHistory
;
735 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
737 wxDocument
*doc
= GetCurrentDocument();
742 doc
->DeleteAllViews();
743 if (m_docs
.Member(doc
))
748 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
750 CreateDocument(wxString(""), wxDOC_NEW
);
753 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
755 CreateDocument(wxString(""), 0);
758 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
760 wxDocument
*doc
= GetCurrentDocument();
766 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
768 wxDocument
*doc
= GetCurrentDocument();
774 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
776 wxDocument
*doc
= GetCurrentDocument();
782 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
784 wxView
*view
= GetCurrentView();
788 wxPrintout
*printout
= view
->OnCreatePrintout();
792 printer
.Print(view
->GetFrame(), printout
, TRUE
);
798 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
800 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
801 wxView
*view
= GetCurrentView();
803 parentWin
= view
->GetFrame();
805 wxPrintDialogData data
;
807 wxPrintDialog
printerDialog(parentWin
, & data
);
808 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
809 printerDialog
.ShowModal();
812 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
814 wxView
*view
= GetCurrentView();
818 wxPrintout
*printout
= view
->OnCreatePrintout();
821 // Pass two printout objects: for preview, and possible printing.
822 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
823 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
825 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
826 wxPoint(100, 100), wxSize(600, 650));
827 frame
->Centre(wxBOTH
);
833 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
835 wxDocument
*doc
= GetCurrentDocument();
838 if (doc
->GetCommandProcessor())
839 doc
->GetCommandProcessor()->Undo();
842 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
844 wxDocument
*doc
= GetCurrentDocument();
847 if (doc
->GetCommandProcessor())
848 doc
->GetCommandProcessor()->Redo();
851 wxView
*wxDocManager::GetCurrentView(void) const
854 return m_currentView
;
855 if (m_docs
.Number() == 1)
857 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
858 return doc
->GetFirstView();
860 return (wxView
*) NULL
;
863 // Extend event processing to search the view's event table
864 bool wxDocManager::ProcessEvent(wxEvent
& event
)
866 wxView
* view
= GetCurrentView();
869 if (view
->ProcessEvent(event
))
872 return wxEvtHandler::ProcessEvent(event
);
875 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
877 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
880 for (i
= 0; i
< m_templates
.Number(); i
++)
882 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
883 if (temp
->IsVisible())
892 return (wxDocument
*) NULL
;
895 // If we've reached the max number of docs, close the
897 if (GetDocuments().Number() >= m_maxDocsOpen
)
899 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
902 // Implicitly deletes the document when
903 // the last view is deleted
904 doc
->DeleteAllViews();
906 // Check we're really deleted
907 if (m_docs
.Member(doc
))
911 return (wxDocument
*) NULL
;
914 // New document: user chooses a template, unless there's only one.
915 if (flags
& wxDOC_NEW
)
919 wxDocTemplate
*temp
= templates
[0];
921 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
924 newDoc
->SetDocumentName(temp
->GetDocumentName());
925 newDoc
->SetDocumentTemplate(temp
);
926 newDoc
->OnNewDocument();
931 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
935 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
938 newDoc
->SetDocumentName(temp
->GetDocumentName());
939 newDoc
->SetDocumentTemplate(temp
);
940 newDoc
->OnNewDocument();
945 return (wxDocument
*) NULL
;
949 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
955 if (flags
& wxDOC_SILENT
)
956 temp
= FindTemplateForPath(path2
);
958 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
964 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
967 newDoc
->SetDocumentName(temp
->GetDocumentName());
968 newDoc
->SetDocumentTemplate(temp
);
969 if (!newDoc
->OnOpenDocument(path2
))
972 return (wxDocument
*) NULL
;
974 AddFileToHistory(path2
);
979 return (wxDocument
*) NULL
;
982 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
984 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
987 for (i
= 0; i
< m_templates
.Number(); i
++)
989 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
990 if (temp
->IsVisible())
992 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1002 return (wxView
*) NULL
;
1006 wxDocTemplate
*temp
= templates
[0];
1008 wxView
*view
= temp
->CreateView(doc
, flags
);
1010 view
->SetViewName(temp
->GetViewName());
1014 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1018 wxView
*view
= temp
->CreateView(doc
, flags
);
1020 view
->SetViewName(temp
->GetViewName());
1024 return (wxView
*) NULL
;
1027 // Not yet implemented
1028 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1032 // Not yet implemented
1033 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1038 wxDocument
*wxDocManager::GetCurrentDocument(void) const
1041 return m_currentView
->GetDocument();
1043 return (wxDocument
*) NULL
;
1046 // Make a default document name
1047 bool wxDocManager::MakeDefaultName(wxString
& name
)
1049 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1050 m_defaultDocumentNameCounter
++;
1055 // Not yet implemented
1056 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1058 return (wxDocTemplate
*) NULL
;
1061 // File history management
1062 void wxDocManager::AddFileToHistory(const wxString
& file
)
1065 m_fileHistory
->AddFileToHistory(file
);
1068 wxString
wxDocManager::GetHistoryFile(int i
) const
1073 histFile
= m_fileHistory
->GetHistoryFile(i
);
1078 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1081 m_fileHistory
->UseMenu(menu
);
1084 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1087 m_fileHistory
->RemoveMenu(menu
);
1091 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1094 m_fileHistory
->Load(config
);
1097 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1100 m_fileHistory
->Save(config
);
1104 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1107 m_fileHistory
->AddFilesToMenu(menu
);
1110 void wxDocManager::FileHistoryAddFilesToMenu()
1113 m_fileHistory
->AddFilesToMenu();
1116 int wxDocManager::GetNoHistoryFiles(void) const
1119 return m_fileHistory
->GetNoHistoryFiles();
1125 // Given a path, try to find a matching template. Won't always work, of
1127 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1129 wxString theExt
= FindExtension(path
);
1131 return (wxDocTemplate
*) NULL
;
1132 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1134 if (m_templates
.Number() == 1)
1135 return (wxDocTemplate
*)m_templates
.First()->Data();
1137 // Find the template which this extension corresponds to
1139 for (i
= 0; i
< m_templates
.Number(); i
++)
1141 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1142 if (strcmp(temp
->GetDefaultExtension(), theExt
) == 0)
1151 // Prompts user to open a file, using file specs in templates.
1152 // How to implement in wxWindows? Must extend the file selector
1153 // dialog or implement own; OR match the extension to the
1154 // template extension.
1156 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1159 long WXUNUSED(flags
),
1160 bool WXUNUSED(save
))
1162 // We can only have multiple filters in Windows
1167 for (i
= 0; i
< noTemplates
; i
++)
1169 if (templates
[i
]->IsVisible())
1171 // add a '|' to separate this filter from the previous one
1172 if ( !descrBuf
.IsEmpty() )
1175 descrBuf
<< templates
[i
]->GetDescription()
1176 << " (" << templates
[i
]->GetFileFilter() << ") |"
1177 << templates
[i
]->GetFileFilter();
1181 wxString descrBuf
= "*.*";
1184 wxString pathTmp
= wxFileSelector(_("Select a file"), "", "", "",
1185 descrBuf
, 0, wxTheApp
->GetTopWindow());
1187 if (!pathTmp
.IsEmpty())
1190 wxString theExt
= FindExtension(path
);
1192 return (wxDocTemplate
*) NULL
;
1194 // This is dodgy in that we're selecting the template on the
1195 // basis of the file extension, which may not be a standard
1196 // one. We really want to know exactly which template was
1197 // chosen by using a more advanced file selector.
1198 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1204 return (wxDocTemplate
*) NULL
;
1207 // In all other windowing systems, until we have more advanced
1208 // file selectors, we must select the document type (template) first, and
1209 // _then_ pop up the file selector.
1210 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1212 return (wxDocTemplate
*) NULL
;
1214 char *pathTmp
= wxFileSelector(_("Select a file"), "", "",
1215 temp
->GetDefaultExtension(),
1216 temp
->GetFileFilter(),
1217 0, wxTheApp
->GetTopWindow());
1225 return (wxDocTemplate
*) NULL
;
1229 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1232 char **strings
= new char *[noTemplates
];
1233 char **data
= new char *[noTemplates
];
1236 for (i
= 0; i
< noTemplates
; i
++)
1238 if (templates
[i
]->IsVisible())
1240 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1241 data
[n
] = (char *)templates
[i
];
1249 return (wxDocTemplate
*) NULL
;
1253 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1259 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document template"), _("Templates"), n
,
1266 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1269 char **strings
= new char *[noTemplates
];
1270 char **data
= new char *[noTemplates
];
1273 for (i
= 0; i
< noTemplates
; i
++)
1275 if (templates
[i
]->IsVisible() && (templates
[i
]->GetViewName() != ""))
1277 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1278 data
[n
] = (char *)templates
[i
];
1282 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document view"), _("Views"), n
,
1289 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1291 if (!m_templates
.Member(temp
))
1292 m_templates
.Append(temp
);
1295 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1297 m_templates
.DeleteObject(temp
);
1300 // Add and remove a document from the manager's list
1301 void wxDocManager::AddDocument(wxDocument
*doc
)
1303 if (!m_docs
.Member(doc
))
1307 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1309 m_docs
.DeleteObject(doc
);
1312 // Views or windows should inform the document manager
1313 // when a view is going in or out of focus
1314 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1316 // If we're deactiving, and if we're not actually deleting the view, then
1317 // don't reset the current view because we may be going to
1318 // a window without a view.
1319 // WHAT DID I MEAN BY THAT EXACTLY?
1323 if (m_currentView == view)
1324 m_currentView = NULL;
1330 m_currentView
= view
;
1332 m_currentView
= (wxView
*) NULL
;
1336 // ----------------------------------------------------------------------------
1337 // Default document child frame
1338 // ----------------------------------------------------------------------------
1340 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1341 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1342 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1345 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1349 const wxString
& title
,
1353 const wxString
& name
)
1354 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1356 m_childDocument
= doc
;
1359 view
->SetFrame(this);
1362 wxDocChildFrame::~wxDocChildFrame()
1366 // Extend event processing to search the view's event table
1367 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1370 m_childView
->Activate(TRUE
);
1372 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1374 // Only hand up to the parent if it's a menu command
1375 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1376 return wxEvtHandler::ProcessEvent(event
);
1384 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1386 wxFrame::OnActivate(event
);
1389 m_childView
->Activate(event
.GetActive());
1392 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1397 if (!event
.CanVeto())
1398 ans
= TRUE
; // Must delete.
1400 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1404 m_childView
->Activate(FALSE
);
1406 m_childView
= (wxView
*) NULL
;
1407 m_childDocument
= (wxDocument
*) NULL
;
1418 // ----------------------------------------------------------------------------
1419 // Default parent frame
1420 // ----------------------------------------------------------------------------
1422 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1423 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1424 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1425 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1428 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1431 const wxString
& title
,
1435 const wxString
& name
)
1436 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1438 m_docManager
= manager
;
1441 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1446 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1448 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1450 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1453 // Extend event processing to search the view's event table
1454 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1456 // Try the document manager, then do default processing
1457 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1458 return wxEvtHandler::ProcessEvent(event
);
1463 // Define the behaviour for the frame closing
1464 // - must delete all frames except for the main one.
1465 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1467 if (m_docManager
->Clear(!event
.CanVeto()))
1475 #if wxUSE_PRINTING_ARCHITECTURE
1477 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1478 : wxPrintout(WXSTRINGCAST title
)
1480 m_printoutView
= view
;
1483 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1487 // Get the logical pixels per inch of screen and printer
1488 int ppiScreenX
, ppiScreenY
;
1489 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1490 int ppiPrinterX
, ppiPrinterY
;
1491 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1493 // This scales the DC so that the printout roughly represents the
1494 // the screen scaling. The text point size _should_ be the right size
1495 // but in fact is too small for some reason. This is a detail that will
1496 // need to be addressed at some point but can be fudged for the
1498 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1500 // Now we have to check in case our real page size is reduced
1501 // (e.g. because we're drawing to a print preview memory DC)
1502 int pageWidth
, pageHeight
;
1504 dc
->GetSize(&w
, &h
);
1505 GetPageSizePixels(&pageWidth
, &pageHeight
);
1507 // If printer pageWidth == current DC width, then this doesn't
1508 // change. But w might be the preview bitmap width, so scale down.
1509 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1510 dc
->SetUserScale(overallScale
, overallScale
);
1514 m_printoutView
->OnDraw(dc
);
1519 bool wxDocPrintout::HasPage(int pageNum
)
1521 return (pageNum
== 1);
1524 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1526 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1532 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1540 #endif // wxUSE_PRINTING_ARCHITECTURE
1542 // ----------------------------------------------------------------------------
1543 // Command processing framework
1544 // ----------------------------------------------------------------------------
1546 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1548 m_canUndo
= canUndoIt
;
1549 m_commandName
= name
;
1552 wxCommand::~wxCommand()
1556 // Command processor
1557 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1559 m_maxNoCommands
= maxCommands
;
1560 m_currentCommand
= (wxNode
*) NULL
;
1561 m_commandEditMenu
= (wxMenu
*) NULL
;
1564 wxCommandProcessor::~wxCommandProcessor()
1569 // Pass a command to the processor. The processor calls Do();
1570 // if successful, is appended to the command history unless
1571 // storeIt is FALSE.
1572 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1574 bool success
= command
->Do();
1575 if (success
&& storeIt
)
1577 if (m_commands
.Number() == m_maxNoCommands
)
1579 wxNode
*firstNode
= m_commands
.First();
1580 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1581 delete firstCommand
;
1585 // Correct a bug: we must chop off the current 'branch'
1586 // so that we're at the end of the command list.
1587 if (!m_currentCommand
)
1591 wxNode
*node
= m_currentCommand
->Next();
1594 wxNode
*next
= node
->Next();
1595 delete (wxCommand
*)node
->Data();
1601 m_commands
.Append(command
);
1602 m_currentCommand
= m_commands
.Last();
1608 bool wxCommandProcessor::Undo()
1610 if (m_currentCommand
)
1612 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1613 if (command
->CanUndo())
1615 bool success
= command
->Undo();
1618 m_currentCommand
= m_currentCommand
->Previous();
1627 bool wxCommandProcessor::Redo()
1629 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
1630 wxNode
*redoNode
= (wxNode
*) NULL
;
1631 if (m_currentCommand
&& m_currentCommand
->Next())
1633 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1634 redoNode
= m_currentCommand
->Next();
1638 if (m_commands
.Number() > 0)
1640 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1641 redoNode
= m_commands
.First();
1647 bool success
= redoCommand
->Do();
1650 m_currentCommand
= redoNode
;
1658 bool wxCommandProcessor::CanUndo(void) const
1660 if (m_currentCommand
)
1661 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1665 bool wxCommandProcessor::CanRedo(void) const
1667 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() == (wxNode
*) NULL
))
1670 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() != (wxNode
*) NULL
))
1673 if ((m_currentCommand
== (wxNode
*) NULL
) && (m_commands
.Number() > 0))
1679 void wxCommandProcessor::Initialize()
1681 m_currentCommand
= m_commands
.Last();
1685 void wxCommandProcessor::SetMenuStrings()
1687 if (m_commandEditMenu
)
1690 if (m_currentCommand
)
1692 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1693 wxString
commandName(command
->GetName());
1694 if (commandName
== "") commandName
= _("Unnamed command");
1695 bool canUndo
= command
->CanUndo();
1697 buf
= wxString(_("&Undo ")) + commandName
;
1699 buf
= wxString(_("Can't &Undo ")) + commandName
;
1701 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1702 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1704 // We can redo, if we're not at the end of the history.
1705 if (m_currentCommand
->Next())
1707 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1708 wxString
redoCommandName(redoCommand
->GetName());
1709 if (redoCommandName
== "") redoCommandName
= _("Unnamed command");
1710 buf
= wxString(_("&Redo ")) + redoCommandName
;
1711 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1712 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1716 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1717 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1722 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
1723 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1725 if (m_commands
.Number() == 0)
1727 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1728 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1732 // currentCommand is NULL but there are commands: this means that
1733 // we've undone to the start of the list, but can redo the first.
1734 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1735 wxString
redoCommandName(redoCommand
->GetName());
1736 if (redoCommandName
== "") redoCommandName
= _("Unnamed command");
1737 buf
= wxString(_("&Redo ")) + redoCommandName
;
1738 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1739 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1745 void wxCommandProcessor::ClearCommands()
1747 wxNode
*node
= m_commands
.First();
1750 wxCommand
*command
= (wxCommand
*)node
->Data();
1753 node
= m_commands
.First();
1755 m_currentCommand
= (wxNode
*) NULL
;
1758 // ----------------------------------------------------------------------------
1759 // File history processor
1760 // ----------------------------------------------------------------------------
1762 wxFileHistory::wxFileHistory(int maxFiles
)
1764 m_fileMaxFiles
= maxFiles
;
1766 m_fileHistory
= new char *[m_fileMaxFiles
];
1769 wxFileHistory::~wxFileHistory()
1772 for (i
= 0; i
< m_fileHistoryN
; i
++)
1773 delete[] m_fileHistory
[i
];
1774 delete[] m_fileHistory
;
1777 // File history management
1778 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1781 // Check we don't already have this file
1782 for (i
= 0; i
< m_fileHistoryN
; i
++)
1784 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1788 // Add to the project file history:
1789 // Move existing files (if any) down so we can insert file at beginning.
1791 // First delete filename that has popped off the end of the array (if any)
1792 if (m_fileHistoryN
== m_fileMaxFiles
)
1794 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1795 m_fileHistory
[m_fileMaxFiles
-1] = (char *) NULL
;
1797 if (m_fileHistoryN
< m_fileMaxFiles
)
1799 wxNode
* node
= m_fileMenus
.First();
1802 wxMenu
* menu
= (wxMenu
*) node
->Data();
1803 if (m_fileHistoryN
== 0)
1804 menu
->AppendSeparator();
1805 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1806 node
= node
->Next();
1810 // Shuffle filenames down
1811 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1813 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1815 m_fileHistory
[0] = copystring(file
);
1817 for (i
= 0; i
< m_fileHistoryN
; i
++)
1818 if (m_fileHistory
[i
])
1821 buf
.Printf("&%d %s", i
+1, m_fileHistory
[i
]);
1822 wxNode
* node
= m_fileMenus
.First();
1825 wxMenu
* menu
= (wxMenu
*) node
->Data();
1826 menu
->SetLabel(wxID_FILE1
+i
, buf
);
1827 node
= node
->Next();
1832 wxString
wxFileHistory::GetHistoryFile(int i
) const
1834 if (i
< m_fileHistoryN
)
1835 return wxString(m_fileHistory
[i
]);
1837 return wxString("");
1840 void wxFileHistory::UseMenu(wxMenu
*menu
)
1842 if (!m_fileMenus
.Member(menu
))
1843 m_fileMenus
.Append(menu
);
1846 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
1848 m_fileMenus
.DeleteObject(menu
);
1852 void wxFileHistory::Load(wxConfigBase
& config
)
1856 buf
.Printf("file%d", m_fileHistoryN
+1);
1857 wxString historyFile
;
1858 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= ""))
1860 m_fileHistory
[m_fileHistoryN
] = copystring((const char*) historyFile
);
1862 buf
.Printf("file%d", m_fileHistoryN
+1);
1868 void wxFileHistory::Save(wxConfigBase
& config
)
1871 for (i
= 0; i
< m_fileHistoryN
; i
++)
1874 buf
.Printf("file%d", i
+1);
1875 config
.Write(buf
, wxString(m_fileHistory
[i
]));
1878 #endif // wxUSE_CONFIG
1880 void wxFileHistory::AddFilesToMenu()
1882 if (m_fileHistoryN
> 0)
1884 wxNode
* node
= m_fileMenus
.First();
1887 wxMenu
* menu
= (wxMenu
*) node
->Data();
1888 menu
->AppendSeparator();
1890 for (i
= 0; i
< m_fileHistoryN
; i
++)
1892 if (m_fileHistory
[i
])
1895 buf
.Printf("&%d %s", i
+1, m_fileHistory
[i
]);
1896 menu
->Append(wxID_FILE1
+i
, buf
);
1899 node
= node
->Next();
1904 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
1906 if (m_fileHistoryN
> 0)
1908 menu
->AppendSeparator();
1910 for (i
= 0; i
< m_fileHistoryN
; i
++)
1912 if (m_fileHistory
[i
])
1915 buf
.Printf("&%d %s", i
+1, m_fileHistory
[i
]);
1916 menu
->Append(wxID_FILE1
+i
, buf
);
1922 // ----------------------------------------------------------------------------
1923 // Permits compatibility with existing file formats and functions that
1924 // manipulate files directly
1925 // ----------------------------------------------------------------------------
1927 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1932 if ((fd1
= fopen (WXSTRINGCAST filename
, "rb")) == NULL
)
1935 while ((ch
= getc (fd1
)) != EOF
)
1936 stream
<< (unsigned char)ch
;
1942 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1947 if ((fd1
= fopen (WXSTRINGCAST filename
, "wb")) == NULL
)
1952 while (!stream
.eof())
1962 #endif // wxUSE_DOC_VIEW_ARCHITECTURE