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"
63 #if !USE_SHARED_LIBRARY
64 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
65 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
66 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
67 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
68 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
69 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
70 #if USE_PRINTING_ARCHITECTURE
71 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
73 IMPLEMENT_CLASS(wxCommand
, wxObject
)
74 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
75 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
76 // IMPLEMENT_DYNAMIC_CLASS(wxPrintInfo, wxObject)
80 * Definition of wxDocument
83 wxDocument::wxDocument(wxDocument
*parent
)
85 m_documentModified
=FALSE
;
88 m_documentParent
=parent
;
89 m_documentTemplate
= (wxDocTemplate
*) NULL
;
90 m_documentTypeName
= "";
94 bool wxDocument::DeleteContents(void)
99 wxDocument::~wxDocument(void)
103 if (m_commandProcessor
)
104 delete m_commandProcessor
;
106 GetDocumentManager()->RemoveDocument(this);
108 // Not safe to do here, since it'll
109 // invoke virtual view functions expecting to see
110 // valid derived objects: and by the time we get
111 // here, we've called destructors higher up.
115 bool wxDocument::Close(void)
117 if (OnSaveModified())
118 return OnCloseDocument();
123 bool wxDocument::OnCloseDocument(void)
130 // Note that this implicitly deletes the document when
131 // the last view is deleted.
132 bool wxDocument::DeleteAllViews(void)
134 wxNode
*node
= m_documentViews
.First();
137 wxView
*view
= (wxView
*)node
->Data();
141 wxNode
*next
= node
->Next();
143 delete view
; // Deletes node implicitly
149 wxView
*wxDocument::GetFirstView(void) const
151 if (m_documentViews
.Number() == 0)
152 return (wxView
*) NULL
;
153 return (wxView
*)m_documentViews
.First()->Data();
156 wxDocManager
*wxDocument::GetDocumentManager(void) const
158 return m_documentTemplate
->GetDocumentManager();
161 bool wxDocument::OnNewDocument(void)
163 if (!OnSaveModified())
166 if (OnCloseDocument()==FALSE
) return FALSE
;
169 SetDocumentSaved(FALSE
);
172 GetDocumentManager()->MakeDefaultName(name
);
174 SetFilename(name
, TRUE
);
179 bool wxDocument::Save(void)
183 if (!IsModified()) return TRUE
;
184 if (m_documentFile
== "" || !m_savedYet
)
187 ret
= OnSaveDocument(m_documentFile
);
189 SetDocumentSaved(TRUE
);
193 bool wxDocument::SaveAs(void)
195 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
199 char *tmp
= wxFileSelector(_("Save as"), docTemplate
->GetDirectory(), GetFilename(),
200 docTemplate
->GetDefaultExtension(), docTemplate
->GetFileFilter(),
201 wxSAVE
|wxOVERWRITE_PROMPT
, GetDocumentWindow());
207 wxString
fileName(tmp
);
211 wxSplitPath(fileName
, & path
, & name
, & ext
);
213 if (ext
.IsEmpty() || ext
== "")
216 fileName
+= docTemplate
->GetDefaultExtension();
219 SetFilename(fileName
);
220 SetTitle(wxFileNameFromPath(fileName
));
222 GetDocumentManager()->AddFileToHistory(fileName
);
224 // Notify the views that the filename has changed
225 wxNode
*node
= m_documentViews
.First();
228 wxView
*view
= (wxView
*)node
->Data();
229 view
->OnChangeFilename();
233 return OnSaveDocument(m_documentFile
);
236 bool wxDocument::OnSaveDocument(const wxString
& file
)
242 if (wxTheApp
->GetAppName() != "")
243 msgTitle
= wxTheApp
->GetAppName();
245 msgTitle
= wxString(_("File error"));
247 ofstream
store(file
);
248 if (store
.fail() || store
.bad())
250 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
251 GetDocumentWindow());
255 if (SaveObject(store
)==FALSE
)
257 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
258 GetDocumentWindow());
267 bool wxDocument::OnOpenDocument(const wxString
& file
)
269 if (!OnSaveModified())
273 if (wxTheApp
->GetAppName() != "")
274 msgTitle
= wxTheApp
->GetAppName();
276 msgTitle
= wxString(_("File error"));
278 ifstream
store(file
);
279 if (store
.fail() || store
.bad())
281 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
282 GetDocumentWindow());
285 if (LoadObject(store
)==FALSE
)
287 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
288 GetDocumentWindow());
291 SetFilename(file
, TRUE
);
299 istream
& wxDocument::LoadObject(istream
& stream
)
301 // wxObject::LoadObject(stream);
306 ostream
& wxDocument::SaveObject(ostream
& stream
)
308 // wxObject::SaveObject(stream);
313 bool wxDocument::Revert(void)
319 // Get title, or filename if no title, else unnamed
320 bool wxDocument::GetPrintableName(wxString
& buf
) const
322 if (m_documentTitle
!= "")
324 buf
= m_documentTitle
;
327 else if (m_documentFile
!= "")
329 buf
= wxFileNameFromPath(m_documentFile
);
339 wxWindow
*wxDocument::GetDocumentWindow(void) const
341 wxView
*view
= GetFirstView();
343 return view
->GetFrame();
345 return wxTheApp
->GetTopWindow();
348 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor(void)
350 return new wxCommandProcessor
;
353 // TRUE if safe to close
354 bool wxDocument::OnSaveModified(void)
360 GetPrintableName(title
);
363 if (wxTheApp
->GetAppName() != "")
364 msgTitle
= wxTheApp
->GetAppName();
366 msgTitle
= wxString(_("Warning"));
368 sprintf(buf
, _("Do you want to save changes to document %s?"), (const char *)title
);
369 int res
= wxMessageBox(buf
, msgTitle
, wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
370 GetDocumentWindow());
376 else if (res
== wxYES
)
378 else if (res
== wxCANCEL
)
384 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
389 bool wxDocument::AddView(wxView
*view
)
391 if (!m_documentViews
.Member(view
))
393 m_documentViews
.Append(view
);
399 bool wxDocument::RemoveView(wxView
*view
)
401 (void)m_documentViews
.DeleteObject(view
);
406 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
408 if (GetDocumentTemplate()->CreateView(this, flags
))
414 // Called after a view is added or removed.
415 // The default implementation deletes the document if
416 // there are no more views.
417 void wxDocument::OnChangedViewList(void)
419 if (m_documentViews
.Number() == 0)
421 if (OnSaveModified())
428 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
430 wxNode
*node
= m_documentViews
.First();
433 wxView
*view
= (wxView
*)node
->Data();
434 view
->OnUpdate(sender
, hint
);
439 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
441 m_documentFile
= filename
;
444 // Notify the views that the filename has changed
445 wxNode
*node
= m_documentViews
.First();
448 wxView
*view
= (wxView
*)node
->Data();
449 view
->OnChangeFilename();
460 wxView::wxView(wxDocument
*doc
)
465 m_viewFrame
= (wxFrame
*) NULL
;
468 wxView::~wxView(void)
470 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
471 m_viewDocument
->RemoveView(this);
474 // Extend event processing to search the document's event table
475 bool wxView::ProcessEvent(wxEvent
& event
)
477 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
478 return wxEvtHandler::ProcessEvent(event
);
483 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
487 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
492 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
496 void wxView::OnChangeFilename(void)
498 if (GetFrame() && GetDocument())
501 GetDocument()->GetPrintableName(name
);
503 GetFrame()->SetTitle(name
);
507 void wxView::SetDocument(wxDocument
*doc
)
509 m_viewDocument
= doc
;
514 bool wxView::Close(bool deleteWindow
)
516 if (OnClose(deleteWindow
))
522 void wxView::Activate(bool activate
)
524 if (GetDocumentManager())
526 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
527 GetDocumentManager()->ActivateView(this, activate
);
531 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
533 return GetDocument() ? GetDocument()->Close() : TRUE
;
536 #if USE_PRINTING_ARCHITECTURE
537 wxPrintout
*wxView::OnCreatePrintout(void)
539 return new wxDocPrintout(this);
548 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
,
549 const wxString
& filter
, const wxString
& dir
, const wxString
& ext
,
550 const wxString
& docTypeName
, const wxString
& viewTypeName
,
551 wxClassInfo
*docClassInfo
, wxClassInfo
*viewClassInfo
, long flags
)
553 m_documentManager
= manager
;
555 m_description
= descr
;
558 m_fileFilter
= filter
;
560 m_docTypeName
= docTypeName
;
561 m_viewTypeName
= viewTypeName
;
562 m_documentManager
->AssociateTemplate(this);
564 m_docClassInfo
= docClassInfo
;
565 m_viewClassInfo
= viewClassInfo
;
568 wxDocTemplate::~wxDocTemplate(void)
570 m_documentManager
->DisassociateTemplate(this);
573 // Tries to dynamically construct an object of the right
575 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
578 return (wxDocument
*) NULL
;
579 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
580 doc
->SetFilename(path
);
581 doc
->SetDocumentTemplate(this);
582 GetDocumentManager()->AddDocument(doc
);
583 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
585 if (doc
->OnCreate(path
, flags
))
590 return (wxDocument
*) NULL
;
594 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
596 if (!m_viewClassInfo
)
597 return (wxView
*) NULL
;
598 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
599 view
->SetDocument(doc
);
600 if (view
->OnCreate(doc
, flags
))
607 return (wxView
*) NULL
;
611 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
612 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
613 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
614 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
615 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
616 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
617 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
618 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
619 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
620 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
621 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
622 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
625 wxDocManager::wxDocManager(long flags
, bool initialize
)
627 m_defaultDocumentNameCounter
= 1;
629 m_currentView
= (wxView
*) NULL
;
630 m_maxDocsOpen
= 10000;
631 m_fileHistory
= (wxFileHistory
*) NULL
;
636 wxDocManager::~wxDocManager(void)
640 delete m_fileHistory
;
643 bool wxDocManager::Clear(bool force
)
645 wxNode
*node
= m_docs
.First();
648 wxDocument
*doc
= (wxDocument
*)node
->Data();
649 wxNode
*next
= node
->Next();
651 if (!doc
->Close() && !force
)
654 // Implicitly deletes the document when the last
655 // view is removed (deleted)
656 doc
->DeleteAllViews();
658 // Check document is deleted
659 if (m_docs
.Member(doc
))
662 // This assumes that documents are not connected in
663 // any way, i.e. deleting one document does NOT
667 node
= m_templates
.First();
670 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
671 wxNode
* next
= node
->Next();
678 bool wxDocManager::Initialize(void)
680 m_fileHistory
= OnCreateFileHistory();
684 wxFileHistory
*wxDocManager::OnCreateFileHistory(void)
686 return new wxFileHistory
;
689 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
691 wxDocument
*doc
= GetCurrentDocument();
696 doc
->DeleteAllViews();
697 if (m_docs
.Member(doc
))
702 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
704 CreateDocument(wxString(""), wxDOC_NEW
);
707 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
709 CreateDocument(wxString(""), 0);
712 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
714 wxDocument
*doc
= GetCurrentDocument();
720 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
722 wxDocument
*doc
= GetCurrentDocument();
728 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
730 wxDocument
*doc
= GetCurrentDocument();
736 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
738 wxView
*view
= GetCurrentView();
742 wxPrintout
*printout
= view
->OnCreatePrintout();
745 // TODO: trouble about this is that it pulls in the postscript
748 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
750 wxWindowsPrinter printer
;
751 printer
.Print(view
->GetFrame(), printout
, TRUE
);
756 wxPostScriptPrinter printer
;
757 printer
.Print(view
->GetFrame(), printout
, TRUE
);
764 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
766 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
767 wxView
*view
= GetCurrentView();
769 parentWin
= view
->GetFrame();
774 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
776 wxPrintDialog
printerDialog(parentWin
, & data
);
777 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
778 printerDialog
.ShowModal();
783 wxGenericPrintDialog
printerDialog(parentWin
, & data
);
784 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
785 printerDialog
.ShowModal();
789 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
791 wxView
*view
= GetCurrentView();
795 wxPrintout
*printout
= view
->OnCreatePrintout();
798 // Pass two printout objects: for preview, and possible printing.
799 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
801 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
802 preview
= new wxWindowsPrintPreview(printout
, view
->OnCreatePrintout());
805 preview
= new wxPostScriptPrintPreview(printout
, view
->OnCreatePrintout());
807 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
808 wxPoint(100, 100), wxSize(600, 650));
809 frame
->Centre(wxBOTH
);
815 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
817 wxDocument
*doc
= GetCurrentDocument();
820 if (doc
->GetCommandProcessor())
821 doc
->GetCommandProcessor()->Undo();
824 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
826 wxDocument
*doc
= GetCurrentDocument();
829 if (doc
->GetCommandProcessor())
830 doc
->GetCommandProcessor()->Redo();
833 wxView
*wxDocManager::GetCurrentView(void) const
836 return m_currentView
;
837 if (m_docs
.Number() == 1)
839 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
840 return doc
->GetFirstView();
842 return (wxView
*) NULL
;
845 // Extend event processing to search the view's event table
846 bool wxDocManager::ProcessEvent(wxEvent
& event
)
848 wxView
* view
= GetCurrentView();
851 if (view
->ProcessEvent(event
))
854 return wxEvtHandler::ProcessEvent(event
);
857 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
859 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
862 for (i
= 0; i
< m_templates
.Number(); i
++)
864 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
865 if (temp
->IsVisible())
874 return (wxDocument
*) NULL
;
877 // If we've reached the max number of docs, close the
879 if (GetDocuments().Number() >= m_maxDocsOpen
)
881 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
884 // Implicitly deletes the document when
885 // the last view is deleted
886 doc
->DeleteAllViews();
888 // Check we're really deleted
889 if (m_docs
.Member(doc
))
893 return (wxDocument
*) NULL
;
896 // New document: user chooses a template, unless there's only one.
897 if (flags
& wxDOC_NEW
)
901 wxDocTemplate
*temp
= templates
[0];
903 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
906 newDoc
->SetDocumentName(temp
->GetDocumentName());
907 newDoc
->SetDocumentTemplate(temp
);
908 newDoc
->OnNewDocument();
913 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
917 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
920 newDoc
->SetDocumentName(temp
->GetDocumentName());
921 newDoc
->SetDocumentTemplate(temp
);
922 newDoc
->OnNewDocument();
927 return (wxDocument
*) NULL
;
931 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
937 if (flags
& wxDOC_SILENT
)
938 temp
= FindTemplateForPath(path2
);
940 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
946 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
949 newDoc
->SetDocumentName(temp
->GetDocumentName());
950 newDoc
->SetDocumentTemplate(temp
);
951 if (!newDoc
->OnOpenDocument(path2
))
954 return (wxDocument
*) NULL
;
956 AddFileToHistory(path2
);
961 return (wxDocument
*) NULL
;
964 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
966 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
969 for (i
= 0; i
< m_templates
.Number(); i
++)
971 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
972 if (temp
->IsVisible())
974 if (temp
->GetDocumentName() == doc
->GetDocumentName())
984 return (wxView
*) NULL
;
988 wxDocTemplate
*temp
= templates
[0];
990 wxView
*view
= temp
->CreateView(doc
, flags
);
992 view
->SetViewName(temp
->GetViewName());
996 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1000 wxView
*view
= temp
->CreateView(doc
, flags
);
1002 view
->SetViewName(temp
->GetViewName());
1006 return (wxView
*) NULL
;
1009 // Not yet implemented
1010 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1014 // Not yet implemented
1015 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1020 wxDocument
*wxDocManager::GetCurrentDocument(void) const
1023 return m_currentView
->GetDocument();
1025 return (wxDocument
*) NULL
;
1028 // Make a default document name
1029 bool wxDocManager::MakeDefaultName(wxString
& name
)
1032 sprintf(buf
, _("unnamed%d"), m_defaultDocumentNameCounter
);
1033 m_defaultDocumentNameCounter
++;
1038 // Not yet implemented
1039 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1041 return (wxDocTemplate
*) NULL
;
1044 // File history management
1045 void wxDocManager::AddFileToHistory(const wxString
& file
)
1048 m_fileHistory
->AddFileToHistory(file
);
1051 wxString
wxDocManager::GetHistoryFile(int i
) const
1054 return wxString(m_fileHistory
->GetHistoryFile(i
));
1056 return wxString("");
1059 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1062 m_fileHistory
->FileHistoryUseMenu(menu
);
1065 void wxDocManager::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1068 m_fileHistory
->FileHistoryLoad(resourceFile
, section
);
1071 void wxDocManager::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1074 m_fileHistory
->FileHistorySave(resourceFile
, section
);
1077 int wxDocManager::GetNoHistoryFiles(void) const
1080 return m_fileHistory
->GetNoHistoryFiles();
1085 static char *FindExtension(char *path
)
1087 static char ext
[10];
1088 int len
= strlen(path
);
1092 for (i
= (len
-1); i
> 0; i
--)
1098 for (j
= i
+1; j
< len
; j
++)
1099 ext
[(int)(j
-(i
+1))] = (char)wxToLower(path
[j
]); // NOTE Should not use tolower under UNIX
1104 return (char *) NULL
;
1106 else return (char *) NULL
;
1110 // Given a path, try to find a matching template. Won't
1111 // always work, of course.
1112 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1114 char *theExt
= FindExtension((char *)(const char *)path
);
1116 return (wxDocTemplate
*) NULL
;
1117 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1119 if (m_templates
.Number() == 1)
1120 return (wxDocTemplate
*)m_templates
.First()->Data();
1122 // Find the template which this extension corresponds to
1124 for (i
= 0; i
< m_templates
.Number(); i
++)
1126 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1127 if (strcmp(temp
->GetDefaultExtension(), theExt
) == 0)
1136 // Prompts user to open a file, using file specs in templates.
1137 // How to implement in wxWindows? Must extend the file selector
1138 // dialog or implement own; OR match the extension to the
1139 // template extension.
1140 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1141 int noTemplates
, wxString
& path
, long WXUNUSED(flags
), bool WXUNUSED(save
))
1143 // We can only have multiple filters in Windows
1145 char *descrBuf
= new char[1000];
1148 for (i
= 0; i
< noTemplates
; i
++)
1150 if (templates
[i
]->IsVisible())
1152 strcat(descrBuf
, templates
[i
]->GetDescription());
1153 strcat(descrBuf
, " (");
1154 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1155 strcat(descrBuf
, ") ");
1156 strcat(descrBuf
, "|");
1157 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1158 strcat(descrBuf
, "|");
1161 int len
= strlen(descrBuf
);
1164 descrBuf
[len
-1] = 0;
1166 char *pathTmp
= wxFileSelector(_("Select a file"), "", "", "", descrBuf
, 0, wxTheApp
->GetTopWindow());
1171 char *theExt
= FindExtension((char *)(const char *)path
);
1173 return (wxDocTemplate
*) NULL
;
1175 // This is dodgy in that we're selecting the template on the
1176 // basis of the file extension, which may not be a standard
1177 // one. We really want to know exactly which template was
1178 // chosen by using a more advanced file selector.
1179 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1185 return (wxDocTemplate
*) NULL
;
1188 // In all other windowing systems, until we have more advanced
1189 // file selectors, we must select the document type (template) first, and
1190 // _then_ pop up the file selector.
1191 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1193 return (wxDocTemplate
*) NULL
;
1195 char *pathTmp
= wxFileSelector(_("Select a file"), "", "",
1196 temp
->GetDefaultExtension(),
1197 temp
->GetFileFilter(),
1198 0, wxTheApp
->GetTopWindow());
1206 return (wxDocTemplate
*) NULL
;
1210 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1213 char **strings
= new char *[noTemplates
];
1214 char **data
= new char *[noTemplates
];
1217 for (i
= 0; i
< noTemplates
; i
++)
1219 if (templates
[i
]->IsVisible())
1221 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1222 data
[n
] = (char *)templates
[i
];
1230 return (wxDocTemplate
*) NULL
;
1234 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1240 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document template"), _("Templates"), n
,
1247 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1250 char **strings
= new char *[noTemplates
];
1251 char **data
= new char *[noTemplates
];
1254 for (i
= 0; i
< noTemplates
; i
++)
1256 if (templates
[i
]->IsVisible() && templates
[i
]->GetViewName())
1258 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1259 data
[n
] = (char *)templates
[i
];
1263 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document view"), _("Views"), n
,
1270 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1272 if (!m_templates
.Member(temp
))
1273 m_templates
.Append(temp
);
1276 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1278 m_templates
.DeleteObject(temp
);
1281 // Add and remove a document from the manager's list
1282 void wxDocManager::AddDocument(wxDocument
*doc
)
1284 if (!m_docs
.Member(doc
))
1288 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1290 m_docs
.DeleteObject(doc
);
1293 // Views or windows should inform the document manager
1294 // when a view is going in or out of focus
1295 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1297 // If we're deactiving, and if we're not actually deleting the view, then
1298 // don't reset the current view because we may be going to
1299 // a window without a view.
1300 // WHAT DID I MEAN BY THAT EXACTLY?
1304 if (m_currentView == view)
1305 m_currentView = NULL;
1311 m_currentView
= view
;
1313 m_currentView
= (wxView
*) NULL
;
1318 * Default document child frame
1321 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1322 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1325 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1326 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1327 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1329 m_childDocument
= doc
;
1332 view
->SetFrame(this);
1335 wxDocChildFrame::~wxDocChildFrame(void)
1339 // Extend event processing to search the view's event table
1340 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1343 m_childView
->Activate(TRUE
);
1345 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1347 // Only hand up to the parent if it's a menu command
1348 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1349 return wxEvtHandler::ProcessEvent(event
);
1357 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1359 wxFrame::OnActivate(event
);
1362 m_childView
->Activate(event
.GetActive());
1365 bool wxDocChildFrame::OnClose(void)
1367 // Close view but don't delete the frame while doing so!
1368 // ...since it will be deleted by wxWindows if we return TRUE.
1371 bool ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1374 m_childView
->Activate(FALSE
);
1376 m_childView
= (wxView
*) NULL
;
1377 m_childDocument
= (wxDocument
*) NULL
;
1386 * Default parent frame
1389 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1390 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1391 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1394 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1395 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1396 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1398 m_docManager
= manager
;
1401 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1406 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1408 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1410 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1413 // Extend event processing to search the view's event table
1414 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1416 // Try the document manager, then do default processing
1417 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1418 return wxEvtHandler::ProcessEvent(event
);
1423 // Define the behaviour for the frame closing
1424 // - must delete all frames except for the main one.
1425 bool wxDocParentFrame::OnClose(void)
1427 return m_docManager
->Clear(FALSE
);
1430 #if USE_PRINTING_ARCHITECTURE
1432 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
):
1433 wxPrintout(WXSTRINGCAST title
)
1435 m_printoutView
= view
;
1438 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1442 // Get the logical pixels per inch of screen and printer
1443 int ppiScreenX
, ppiScreenY
;
1444 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1445 int ppiPrinterX
, ppiPrinterY
;
1446 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1448 // This scales the DC so that the printout roughly represents the
1449 // the screen scaling. The text point size _should_ be the right size
1450 // but in fact is too small for some reason. This is a detail that will
1451 // need to be addressed at some point but can be fudged for the
1453 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1455 // Now we have to check in case our real page size is reduced
1456 // (e.g. because we're drawing to a print preview memory DC)
1457 int pageWidth
, pageHeight
;
1459 dc
->GetSize(&w
, &h
);
1460 GetPageSizePixels(&pageWidth
, &pageHeight
);
1462 // If printer pageWidth == current DC width, then this doesn't
1463 // change. But w might be the preview bitmap width, so scale down.
1464 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1465 dc
->SetUserScale(overallScale
, overallScale
);
1469 m_printoutView
->OnDraw(dc
);
1474 bool wxDocPrintout::HasPage(int pageNum
)
1476 return (pageNum
== 1);
1479 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1481 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1487 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1498 * Command processing framework
1501 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1503 m_canUndo
= canUndoIt
;
1504 m_commandName
= name
;
1507 wxCommand::~wxCommand(void)
1511 // Command processor
1512 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1514 m_maxNoCommands
= maxCommands
;
1515 m_currentCommand
= (wxNode
*) NULL
;
1516 m_commandEditMenu
= (wxMenu
*) NULL
;
1519 wxCommandProcessor::~wxCommandProcessor(void)
1524 // Pass a command to the processor. The processor calls Do();
1525 // if successful, is appended to the command history unless
1526 // storeIt is FALSE.
1527 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1529 bool success
= command
->Do();
1530 if (success
&& storeIt
)
1532 if (m_commands
.Number() == m_maxNoCommands
)
1534 wxNode
*firstNode
= m_commands
.First();
1535 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1536 delete firstCommand
;
1540 // Correct a bug: we must chop off the current 'branch'
1541 // so that we're at the end of the command list.
1542 if (!m_currentCommand
)
1546 wxNode
*node
= m_currentCommand
->Next();
1549 wxNode
*next
= node
->Next();
1550 delete (wxCommand
*)node
->Data();
1556 m_commands
.Append(command
);
1557 m_currentCommand
= m_commands
.Last();
1563 bool wxCommandProcessor::Undo(void)
1565 if (m_currentCommand
)
1567 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1568 if (command
->CanUndo())
1570 bool success
= command
->Undo();
1573 m_currentCommand
= m_currentCommand
->Previous();
1582 bool wxCommandProcessor::Redo(void)
1584 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
1585 wxNode
*redoNode
= (wxNode
*) NULL
;
1586 if (m_currentCommand
&& m_currentCommand
->Next())
1588 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1589 redoNode
= m_currentCommand
->Next();
1593 if (m_commands
.Number() > 0)
1595 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1596 redoNode
= m_commands
.First();
1602 bool success
= redoCommand
->Do();
1605 m_currentCommand
= redoNode
;
1613 bool wxCommandProcessor::CanUndo(void)
1615 if (m_currentCommand
)
1616 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1620 void wxCommandProcessor::Initialize(void)
1622 m_currentCommand
= m_commands
.Last();
1626 void wxCommandProcessor::SetMenuStrings(void)
1628 if (m_commandEditMenu
)
1631 if (m_currentCommand
)
1633 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1634 wxString
commandName(command
->GetName());
1635 if (commandName
== "") commandName
= _("Unnamed command");
1636 bool canUndo
= command
->CanUndo();
1638 buf
= wxString(_("&Undo ")) + commandName
;
1640 buf
= wxString(_("Can't &Undo ")) + commandName
;
1642 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1643 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1645 // We can redo, if we're not at the end of the history.
1646 if (m_currentCommand
->Next())
1648 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1649 wxString
redoCommandName(redoCommand
->GetName());
1650 if (redoCommandName
== "") redoCommandName
= _("Unnamed command");
1651 buf
= wxString(_("&Redo ")) + redoCommandName
;
1652 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1653 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1657 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1658 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1663 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
1664 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1666 if (m_commands
.Number() == 0)
1668 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1669 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1673 // currentCommand is NULL but there are commands: this means that
1674 // we've undone to the start of the list, but can redo the first.
1675 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1676 wxString
redoCommandName(redoCommand
->GetName());
1677 if (!redoCommandName
) redoCommandName
= _("Unnamed command");
1678 buf
= wxString(_("&Redo ")) + redoCommandName
;
1679 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1680 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1686 void wxCommandProcessor::ClearCommands(void)
1688 wxNode
*node
= m_commands
.First();
1691 wxCommand
*command
= (wxCommand
*)node
->Data();
1694 node
= m_commands
.First();
1696 m_currentCommand
= (wxNode
*) NULL
;
1701 * File history processor
1704 wxFileHistory::wxFileHistory(int maxFiles
)
1706 m_fileMaxFiles
= maxFiles
;
1707 m_fileMenu
= (wxMenu
*) NULL
;
1709 m_fileHistory
= new char *[m_fileMaxFiles
];
1712 wxFileHistory::~wxFileHistory(void)
1715 for (i
= 0; i
< m_fileHistoryN
; i
++)
1716 delete[] m_fileHistory
[i
];
1717 delete[] m_fileHistory
;
1720 // File history management
1721 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1728 // Check we don't already have this file
1729 for (i
= 0; i
< m_fileHistoryN
; i
++)
1731 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1735 // Add to the project file history:
1736 // Move existing files (if any) down so we can insert file at beginning.
1738 // First delete filename that has popped off the end of the array (if any)
1739 if (m_fileHistoryN
== m_fileMaxFiles
)
1741 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1742 m_fileHistory
[m_fileMaxFiles
-1] = (char *) NULL
;
1744 if (m_fileHistoryN
< m_fileMaxFiles
)
1746 if (m_fileHistoryN
== 0)
1747 m_fileMenu
->AppendSeparator();
1748 m_fileMenu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1751 // Shuffle filenames down
1752 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1754 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1756 m_fileHistory
[0] = copystring(file
);
1758 for (i
= 0; i
< m_fileHistoryN
; i
++)
1759 if (m_fileHistory
[i
])
1762 sprintf(buf
, "&%d %s", i
+1, m_fileHistory
[i
]);
1763 m_fileMenu
->SetLabel(wxID_FILE1
+i
, buf
);
1767 wxString
wxFileHistory::GetHistoryFile(int i
) const
1769 if (i
< m_fileHistoryN
)
1770 return wxString(m_fileHistory
[i
]);
1772 return wxString("");
1775 void wxFileHistory::FileHistoryUseMenu(wxMenu
*menu
)
1780 void wxFileHistory::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1785 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1786 char *historyFile
= (char *) NULL
;
1787 while ((m_fileHistoryN
<= m_fileMaxFiles
) && wxGetResource(section
, buf
, &historyFile
, resourceFile
) && historyFile
)
1789 // wxGetResource allocates memory so this is o.k.
1790 m_fileHistory
[m_fileHistoryN
] = historyFile
;
1792 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1793 historyFile
= (char *) NULL
;
1798 void wxFileHistory::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1803 for (i
= 0; i
< m_fileHistoryN
; i
++)
1805 sprintf(buf
, "file%d", i
+1);
1806 wxWriteResource(section
, buf
, m_fileHistory
[i
], resourceFile
);
1816 wxPrintInfo::wxPrintInfo(void)
1821 wxPrintInfo::~wxPrintInfo(void)
1827 * Permits compatibility with existing file formats and functions
1828 * that manipulate files directly
1831 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1836 if ((fd1
= fopen (WXSTRINGCAST filename
, "rb")) == NULL
)
1839 while ((ch
= getc (fd1
)) != EOF
)
1840 stream
<< (unsigned char)ch
;
1846 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1851 if ((fd1
= fopen (WXSTRINGCAST filename
, "wb")) == NULL
)
1856 while (!stream
.eof())
1867 // End USE_DOC_VIEW_ARCHITECTURE