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
33 #include "wx/dialog.h"
36 #include "wx/filedlg.h"
44 #include "wx/msgdlg.h"
45 #include "wx/choicdlg.h"
46 #include "wx/docview.h"
47 #include "wx/printdlg.h"
48 #include "wx/generic/prntdlgg.h"
49 #include "wx/generic/printps.h"
62 #if !USE_SHARED_LIBRARY
63 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
64 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
65 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
66 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
67 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
68 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
69 #if USE_PRINTING_ARCHITECTURE
70 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
72 IMPLEMENT_CLASS(wxCommand
, wxObject
)
73 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
74 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
75 // IMPLEMENT_DYNAMIC_CLASS(wxPrintInfo, wxObject)
79 * Definition of wxDocument
82 wxDocument::wxDocument(wxDocument
*parent
)
84 m_documentModified
=FALSE
;
87 m_documentParent
=parent
;
88 m_documentTemplate
= NULL
;
89 m_documentTypeName
= "";
93 bool wxDocument::DeleteContents(void)
98 wxDocument::~wxDocument(void)
102 if (m_commandProcessor
)
103 delete m_commandProcessor
;
105 GetDocumentManager()->RemoveDocument(this);
107 // Not safe to do here, since it'll
108 // invoke virtual view functions expecting to see
109 // valid derived objects: and by the time we get
110 // here, we've called destructors higher up.
114 bool wxDocument::Close(void)
116 if (OnSaveModified())
117 return OnCloseDocument();
122 bool wxDocument::OnCloseDocument(void)
129 // Note that this implicitly deletes the document when
130 // the last view is deleted.
131 bool wxDocument::DeleteAllViews(void)
133 wxNode
*node
= m_documentViews
.First();
136 wxView
*view
= (wxView
*)node
->Data();
140 wxNode
*next
= node
->Next();
142 delete view
; // Deletes node implicitly
148 wxView
*wxDocument::GetFirstView(void) const
150 if (m_documentViews
.Number() == 0)
152 return (wxView
*)m_documentViews
.First()->Data();
155 wxDocManager
*wxDocument::GetDocumentManager(void) const
157 return m_documentTemplate
->GetDocumentManager();
160 bool wxDocument::OnNewDocument(void)
162 if (!OnSaveModified())
165 if (OnCloseDocument()==FALSE
) return FALSE
;
168 SetDocumentSaved(FALSE
);
171 GetDocumentManager()->MakeDefaultName(name
);
173 SetFilename(name
, TRUE
);
178 bool wxDocument::Save(void)
182 if (!IsModified()) return TRUE
;
183 if (m_documentFile
== "" || !m_savedYet
)
186 ret
= OnSaveDocument(m_documentFile
);
188 SetDocumentSaved(TRUE
);
192 bool wxDocument::SaveAs(void)
194 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
198 char *tmp
= wxFileSelector(_("Save as"), docTemplate
->GetDirectory(), GetFilename(),
199 docTemplate
->GetDefaultExtension(), docTemplate
->GetFileFilter(),
200 wxSAVE
|wxOVERWRITE_PROMPT
, GetDocumentWindow());
206 wxString
fileName(tmp
);
210 wxSplitPath(fileName
, & path
, & name
, & ext
);
212 if (ext
.IsEmpty() || ext
== "")
215 fileName
+= docTemplate
->GetDefaultExtension();
218 SetFilename(fileName
);
219 SetTitle(wxFileNameFromPath(fileName
));
221 GetDocumentManager()->AddFileToHistory(fileName
);
223 // Notify the views that the filename has changed
224 wxNode
*node
= m_documentViews
.First();
227 wxView
*view
= (wxView
*)node
->Data();
228 view
->OnChangeFilename();
232 return OnSaveDocument(m_documentFile
);
235 bool wxDocument::OnSaveDocument(const wxString
& file
)
241 if (wxTheApp
->GetAppName() != "")
242 msgTitle
= wxTheApp
->GetAppName();
244 msgTitle
= wxString(_("File error"));
246 ofstream
store(file
);
247 if (store
.fail() || store
.bad())
249 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
250 GetDocumentWindow());
254 if (SaveObject(store
)==FALSE
)
256 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
257 GetDocumentWindow());
266 bool wxDocument::OnOpenDocument(const wxString
& file
)
268 if (!OnSaveModified())
272 if (wxTheApp
->GetAppName() != "")
273 msgTitle
= wxTheApp
->GetAppName();
275 msgTitle
= wxString(_("File error"));
277 ifstream
store(file
);
278 if (store
.fail() || store
.bad())
280 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
281 GetDocumentWindow());
284 if (LoadObject(store
)==FALSE
)
286 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
287 GetDocumentWindow());
290 SetFilename(file
, TRUE
);
298 istream
& wxDocument::LoadObject(istream
& stream
)
300 // wxObject::LoadObject(stream);
305 ostream
& wxDocument::SaveObject(ostream
& stream
)
307 // wxObject::SaveObject(stream);
312 bool wxDocument::Revert(void)
318 // Get title, or filename if no title, else unnamed
319 bool wxDocument::GetPrintableName(wxString
& buf
) const
321 if (m_documentTitle
!= "")
323 buf
= m_documentTitle
;
326 else if (m_documentFile
!= "")
328 buf
= wxFileNameFromPath(m_documentFile
);
338 wxWindow
*wxDocument::GetDocumentWindow(void) const
340 wxView
*view
= GetFirstView();
342 return view
->GetFrame();
344 return wxTheApp
->GetTopWindow();
347 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor(void)
349 return new wxCommandProcessor
;
352 // TRUE if safe to close
353 bool wxDocument::OnSaveModified(void)
359 GetPrintableName(title
);
362 if (wxTheApp
->GetAppName() != "")
363 msgTitle
= wxTheApp
->GetAppName();
365 msgTitle
= wxString(_("Warning"));
367 sprintf(buf
, _("Do you want to save changes to document %s?"), (const char *)title
);
368 int res
= wxMessageBox(buf
, msgTitle
, wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
369 GetDocumentWindow());
375 else if (res
== wxYES
)
377 else if (res
== wxCANCEL
)
383 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
388 bool wxDocument::AddView(wxView
*view
)
390 if (!m_documentViews
.Member(view
))
392 m_documentViews
.Append(view
);
398 bool wxDocument::RemoveView(wxView
*view
)
400 (void)m_documentViews
.DeleteObject(view
);
405 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
407 if (GetDocumentTemplate()->CreateView(this, flags
))
413 // Called after a view is added or removed.
414 // The default implementation deletes the document if
415 // there are no more views.
416 void wxDocument::OnChangedViewList(void)
418 if (m_documentViews
.Number() == 0)
420 if (OnSaveModified())
427 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
429 wxNode
*node
= m_documentViews
.First();
432 wxView
*view
= (wxView
*)node
->Data();
433 view
->OnUpdate(sender
, hint
);
438 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
440 m_documentFile
= filename
;
443 // Notify the views that the filename has changed
444 wxNode
*node
= m_documentViews
.First();
447 wxView
*view
= (wxView
*)node
->Data();
448 view
->OnChangeFilename();
459 wxView::wxView(wxDocument
*doc
)
467 wxView::~wxView(void)
469 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
470 m_viewDocument
->RemoveView(this);
473 // Extend event processing to search the document's event table
474 bool wxView::ProcessEvent(wxEvent
& event
)
476 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
477 return wxEvtHandler::ProcessEvent(event
);
482 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
486 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
491 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
495 void wxView::OnChangeFilename(void)
497 if (GetFrame() && GetDocument())
500 GetDocument()->GetPrintableName(name
);
502 GetFrame()->SetTitle(name
);
506 void wxView::SetDocument(wxDocument
*doc
)
508 m_viewDocument
= doc
;
513 bool wxView::Close(bool deleteWindow
)
515 if (OnClose(deleteWindow
))
521 void wxView::Activate(bool activate
)
523 if (GetDocumentManager())
525 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
526 GetDocumentManager()->ActivateView(this, activate
);
530 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
532 return GetDocument() ? GetDocument()->Close() : TRUE
;
535 #if USE_PRINTING_ARCHITECTURE
536 wxPrintout
*wxView::OnCreatePrintout(void)
538 return new wxDocPrintout(this);
547 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
,
548 const wxString
& filter
, const wxString
& dir
, const wxString
& ext
,
549 const wxString
& docTypeName
, const wxString
& viewTypeName
,
550 wxClassInfo
*docClassInfo
, wxClassInfo
*viewClassInfo
, long flags
)
552 m_documentManager
= manager
;
554 m_description
= descr
;
557 m_fileFilter
= filter
;
559 m_docTypeName
= docTypeName
;
560 m_viewTypeName
= viewTypeName
;
561 m_documentManager
->AssociateTemplate(this);
563 m_docClassInfo
= docClassInfo
;
564 m_viewClassInfo
= viewClassInfo
;
567 wxDocTemplate::~wxDocTemplate(void)
569 m_documentManager
->DisassociateTemplate(this);
572 // Tries to dynamically construct an object of the right
574 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
578 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
579 doc
->SetFilename(path
);
580 doc
->SetDocumentTemplate(this);
581 GetDocumentManager()->AddDocument(doc
);
582 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
584 if (doc
->OnCreate(path
, flags
))
593 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
595 if (!m_viewClassInfo
)
597 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
598 view
->SetDocument(doc
);
599 if (view
->OnCreate(doc
, flags
))
610 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
611 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
612 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
613 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
614 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
615 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
616 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
617 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
618 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
619 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
620 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
621 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
624 wxDocManager::wxDocManager(long flags
, bool initialize
)
626 m_defaultDocumentNameCounter
= 1;
628 m_currentView
= NULL
;
629 m_maxDocsOpen
= 10000;
630 m_fileHistory
= NULL
;
635 wxDocManager::~wxDocManager(void)
639 delete m_fileHistory
;
642 bool wxDocManager::Clear(bool force
)
644 wxNode
*node
= m_docs
.First();
647 wxDocument
*doc
= (wxDocument
*)node
->Data();
648 wxNode
*next
= node
->Next();
650 if (!doc
->Close() && !force
)
653 // Implicitly deletes the document when the last
654 // view is removed (deleted)
655 doc
->DeleteAllViews();
657 // Check document is deleted
658 if (m_docs
.Member(doc
))
661 // This assumes that documents are not connected in
662 // any way, i.e. deleting one document does NOT
666 node
= m_templates
.First();
669 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
670 wxNode
* next
= node
->Next();
677 bool wxDocManager::Initialize(void)
679 m_fileHistory
= OnCreateFileHistory();
683 wxFileHistory
*wxDocManager::OnCreateFileHistory(void)
685 return new wxFileHistory
;
688 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
690 wxDocument
*doc
= GetCurrentDocument();
695 doc
->DeleteAllViews();
696 if (m_docs
.Member(doc
))
701 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
703 CreateDocument(wxString(""), wxDOC_NEW
);
706 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
708 CreateDocument(wxString(""), 0);
711 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
713 wxDocument
*doc
= GetCurrentDocument();
719 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
721 wxDocument
*doc
= GetCurrentDocument();
727 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
729 wxDocument
*doc
= GetCurrentDocument();
735 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
737 wxView
*view
= GetCurrentView();
741 wxPrintout
*printout
= view
->OnCreatePrintout();
744 // TODO: trouble about this is that it pulls in the postscript
747 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
749 wxWindowsPrinter printer
;
750 printer
.Print(view
->GetFrame(), printout
, TRUE
);
755 wxPostScriptPrinter printer
;
756 printer
.Print(view
->GetFrame(), printout
, TRUE
);
763 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
765 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
766 wxView
*view
= GetCurrentView();
768 parentWin
= view
->GetFrame();
773 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
775 wxPrintDialog
printerDialog(parentWin
, & data
);
776 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
777 printerDialog
.ShowModal();
782 wxGenericPrintDialog
printerDialog(parentWin
, & data
);
783 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
784 printerDialog
.ShowModal();
788 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
790 wxView
*view
= GetCurrentView();
794 wxPrintout
*printout
= view
->OnCreatePrintout();
797 // Pass two printout objects: for preview, and possible printing.
798 wxPrintPreviewBase
*preview
= NULL
;
800 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
801 preview
= new wxWindowsPrintPreview(printout
, view
->OnCreatePrintout());
804 preview
= new wxPostScriptPrintPreview(printout
, view
->OnCreatePrintout());
806 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
807 wxPoint(100, 100), wxSize(600, 650));
808 frame
->Centre(wxBOTH
);
814 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
816 wxDocument
*doc
= GetCurrentDocument();
819 if (doc
->GetCommandProcessor())
820 doc
->GetCommandProcessor()->Undo();
823 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
825 wxDocument
*doc
= GetCurrentDocument();
828 if (doc
->GetCommandProcessor())
829 doc
->GetCommandProcessor()->Redo();
832 wxView
*wxDocManager::GetCurrentView(void) const
835 return m_currentView
;
836 if (m_docs
.Number() == 1)
838 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
839 return doc
->GetFirstView();
844 // Extend event processing to search the view's event table
845 bool wxDocManager::ProcessEvent(wxEvent
& event
)
847 wxView
* view
= GetCurrentView();
850 if (view
->ProcessEvent(event
))
853 return wxEvtHandler::ProcessEvent(event
);
856 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
858 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
861 for (i
= 0; i
< m_templates
.Number(); i
++)
863 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
864 if (temp
->IsVisible())
876 // If we've reached the max number of docs, close the
878 if (GetDocuments().Number() >= m_maxDocsOpen
)
880 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
883 // Implicitly deletes the document when
884 // the last view is deleted
885 doc
->DeleteAllViews();
887 // Check we're really deleted
888 if (m_docs
.Member(doc
))
895 // New document: user chooses a template, unless there's only one.
896 if (flags
& wxDOC_NEW
)
900 wxDocTemplate
*temp
= templates
[0];
902 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
905 newDoc
->SetDocumentName(temp
->GetDocumentName());
906 newDoc
->SetDocumentTemplate(temp
);
907 newDoc
->OnNewDocument();
912 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
916 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
919 newDoc
->SetDocumentName(temp
->GetDocumentName());
920 newDoc
->SetDocumentTemplate(temp
);
921 newDoc
->OnNewDocument();
930 wxDocTemplate
*temp
= NULL
;
936 if (flags
& wxDOC_SILENT
)
937 temp
= FindTemplateForPath(path2
);
939 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
945 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
948 newDoc
->SetDocumentName(temp
->GetDocumentName());
949 newDoc
->SetDocumentTemplate(temp
);
950 if (!newDoc
->OnOpenDocument(path2
))
955 AddFileToHistory(path2
);
963 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
965 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
968 for (i
= 0; i
< m_templates
.Number(); i
++)
970 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
971 if (temp
->IsVisible())
973 if (temp
->GetDocumentName() == doc
->GetDocumentName())
987 wxDocTemplate
*temp
= templates
[0];
989 wxView
*view
= temp
->CreateView(doc
, flags
);
991 view
->SetViewName(temp
->GetViewName());
995 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
999 wxView
*view
= temp
->CreateView(doc
, flags
);
1001 view
->SetViewName(temp
->GetViewName());
1008 // Not yet implemented
1009 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1013 // Not yet implemented
1014 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1019 wxDocument
*wxDocManager::GetCurrentDocument(void) const
1022 return m_currentView
->GetDocument();
1027 // Make a default document name
1028 bool wxDocManager::MakeDefaultName(wxString
& name
)
1031 sprintf(buf
, _("unnamed%d"), m_defaultDocumentNameCounter
);
1032 m_defaultDocumentNameCounter
++;
1037 // Not yet implemented
1038 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1043 // File history management
1044 void wxDocManager::AddFileToHistory(const wxString
& file
)
1047 m_fileHistory
->AddFileToHistory(file
);
1050 wxString
wxDocManager::GetHistoryFile(int i
) const
1053 return wxString(m_fileHistory
->GetHistoryFile(i
));
1055 return wxString("");
1058 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1061 m_fileHistory
->FileHistoryUseMenu(menu
);
1064 void wxDocManager::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1067 m_fileHistory
->FileHistoryLoad(resourceFile
, section
);
1070 void wxDocManager::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1073 m_fileHistory
->FileHistorySave(resourceFile
, section
);
1076 int wxDocManager::GetNoHistoryFiles(void) const
1079 return m_fileHistory
->GetNoHistoryFiles();
1084 static char *FindExtension(char *path
)
1086 static char ext
[10];
1087 int len
= strlen(path
);
1091 for (i
= (len
-1); i
> 0; i
--)
1097 for (j
= i
+1; j
< len
; j
++)
1098 ext
[(int)(j
-(i
+1))] = (char)wxToLower(path
[j
]); // NOTE Should not use tolower under UNIX
1109 // Given a path, try to find a matching template. Won't
1110 // always work, of course.
1111 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1113 char *theExt
= FindExtension((char *)(const char *)path
);
1116 wxDocTemplate
*theTemplate
= NULL
;
1118 if (m_templates
.Number() == 1)
1119 return (wxDocTemplate
*)m_templates
.First()->Data();
1121 // Find the template which this extension corresponds to
1123 for (i
= 0; i
< m_templates
.Number(); i
++)
1125 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1126 if (strcmp(temp
->GetDefaultExtension(), theExt
) == 0)
1135 // Prompts user to open a file, using file specs in templates.
1136 // How to implement in wxWindows? Must extend the file selector
1137 // dialog or implement own; OR match the extension to the
1138 // template extension.
1139 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1140 int noTemplates
, wxString
& path
, long WXUNUSED(flags
), bool WXUNUSED(save
))
1142 // We can only have multiple filters in Windows
1144 char *descrBuf
= new char[1000];
1147 for (i
= 0; i
< noTemplates
; i
++)
1149 if (templates
[i
]->IsVisible())
1151 strcat(descrBuf
, templates
[i
]->GetDescription());
1152 strcat(descrBuf
, " (");
1153 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1154 strcat(descrBuf
, ") ");
1155 strcat(descrBuf
, "|");
1156 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1157 strcat(descrBuf
, "|");
1160 int len
= strlen(descrBuf
);
1163 descrBuf
[len
-1] = 0;
1165 char *pathTmp
= wxFileSelector(_("Select a file"), "", "", "", descrBuf
, 0, wxTheApp
->GetTopWindow());
1170 char *theExt
= FindExtension((char *)(const char *)path
);
1174 // This is dodgy in that we're selecting the template on the
1175 // basis of the file extension, which may not be a standard
1176 // one. We really want to know exactly which template was
1177 // chosen by using a more advanced file selector.
1178 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1187 // In all other windowing systems, until we have more advanced
1188 // file selectors, we must select the document type (template) first, and
1189 // _then_ pop up the file selector.
1190 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1194 char *pathTmp
= wxFileSelector(_("Select a file"), "", "",
1195 temp
->GetDefaultExtension(),
1196 temp
->GetFileFilter(),
1197 0, wxTheApp
->GetTopWindow());
1209 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1212 char **strings
= new char *[noTemplates
];
1213 char **data
= new char *[noTemplates
];
1216 for (i
= 0; i
< noTemplates
; i
++)
1218 if (templates
[i
]->IsVisible())
1220 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1221 data
[n
] = (char *)templates
[i
];
1233 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1239 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document template"), _("Templates"), n
,
1246 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1249 char **strings
= new char *[noTemplates
];
1250 char **data
= new char *[noTemplates
];
1253 for (i
= 0; i
< noTemplates
; i
++)
1255 if (templates
[i
]->IsVisible() && templates
[i
]->GetViewName())
1257 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1258 data
[n
] = (char *)templates
[i
];
1262 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document view"), _("Views"), n
,
1269 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1271 if (!m_templates
.Member(temp
))
1272 m_templates
.Append(temp
);
1275 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1277 m_templates
.DeleteObject(temp
);
1280 // Add and remove a document from the manager's list
1281 void wxDocManager::AddDocument(wxDocument
*doc
)
1283 if (!m_docs
.Member(doc
))
1287 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1289 m_docs
.DeleteObject(doc
);
1292 // Views or windows should inform the document manager
1293 // when a view is going in or out of focus
1294 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1296 // If we're deactiving, and if we're not actually deleting the view, then
1297 // don't reset the current view because we may be going to
1298 // a window without a view.
1299 // WHAT DID I MEAN BY THAT EXACTLY?
1303 if (m_currentView == view)
1304 m_currentView = NULL;
1310 m_currentView
= view
;
1312 m_currentView
= NULL
;
1317 * Default document child frame
1320 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1321 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1324 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1325 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1326 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1328 m_childDocument
= doc
;
1331 view
->SetFrame(this);
1334 wxDocChildFrame::~wxDocChildFrame(void)
1338 // Extend event processing to search the view's event table
1339 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1342 m_childView
->Activate(TRUE
);
1344 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1346 // Only hand up to the parent if it's a menu command
1347 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1348 return wxEvtHandler::ProcessEvent(event
);
1356 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1358 wxFrame::OnActivate(event
);
1361 m_childView
->Activate(event
.GetActive());
1364 bool wxDocChildFrame::OnClose(void)
1366 // Close view but don't delete the frame while doing so!
1367 // ...since it will be deleted by wxWindows if we return TRUE.
1370 bool ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1373 m_childView
->Activate(FALSE
);
1376 m_childDocument
= NULL
;
1385 * Default parent frame
1388 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1389 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1390 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1393 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1394 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1395 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1397 m_docManager
= manager
;
1400 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1405 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1407 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1409 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1412 // Extend event processing to search the view's event table
1413 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1415 // Try the document manager, then do default processing
1416 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1417 return wxEvtHandler::ProcessEvent(event
);
1422 // Define the behaviour for the frame closing
1423 // - must delete all frames except for the main one.
1424 bool wxDocParentFrame::OnClose(void)
1426 return m_docManager
->Clear(FALSE
);
1429 #if USE_PRINTING_ARCHITECTURE
1431 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
):
1432 wxPrintout(WXSTRINGCAST title
)
1434 m_printoutView
= view
;
1437 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1441 // Get the logical pixels per inch of screen and printer
1442 int ppiScreenX
, ppiScreenY
;
1443 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1444 int ppiPrinterX
, ppiPrinterY
;
1445 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1447 // This scales the DC so that the printout roughly represents the
1448 // the screen scaling. The text point size _should_ be the right size
1449 // but in fact is too small for some reason. This is a detail that will
1450 // need to be addressed at some point but can be fudged for the
1452 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1454 // Now we have to check in case our real page size is reduced
1455 // (e.g. because we're drawing to a print preview memory DC)
1456 int pageWidth
, pageHeight
;
1458 dc
->GetSize(&w
, &h
);
1459 GetPageSizePixels(&pageWidth
, &pageHeight
);
1461 // If printer pageWidth == current DC width, then this doesn't
1462 // change. But w might be the preview bitmap width, so scale down.
1463 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1464 dc
->SetUserScale(overallScale
, overallScale
);
1468 m_printoutView
->OnDraw(dc
);
1473 bool wxDocPrintout::HasPage(int pageNum
)
1475 return (pageNum
== 1);
1478 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1480 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1486 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1497 * Command processing framework
1500 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1502 m_canUndo
= canUndoIt
;
1503 m_commandName
= name
;
1506 wxCommand::~wxCommand(void)
1510 // Command processor
1511 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1513 m_maxNoCommands
= maxCommands
;
1514 m_currentCommand
= NULL
;
1515 m_commandEditMenu
= NULL
;
1518 wxCommandProcessor::~wxCommandProcessor(void)
1523 // Pass a command to the processor. The processor calls Do();
1524 // if successful, is appended to the command history unless
1525 // storeIt is FALSE.
1526 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1528 bool success
= command
->Do();
1529 if (success
&& storeIt
)
1531 if (m_commands
.Number() == m_maxNoCommands
)
1533 wxNode
*firstNode
= m_commands
.First();
1534 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1535 delete firstCommand
;
1539 // Correct a bug: we must chop off the current 'branch'
1540 // so that we're at the end of the command list.
1541 if (!m_currentCommand
)
1545 wxNode
*node
= m_currentCommand
->Next();
1548 wxNode
*next
= node
->Next();
1549 delete (wxCommand
*)node
->Data();
1555 m_commands
.Append(command
);
1556 m_currentCommand
= m_commands
.Last();
1562 bool wxCommandProcessor::Undo(void)
1564 if (m_currentCommand
)
1566 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1567 if (command
->CanUndo())
1569 bool success
= command
->Undo();
1572 m_currentCommand
= m_currentCommand
->Previous();
1581 bool wxCommandProcessor::Redo(void)
1583 wxCommand
*redoCommand
= NULL
;
1584 wxNode
*redoNode
= NULL
;
1585 if (m_currentCommand
&& m_currentCommand
->Next())
1587 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1588 redoNode
= m_currentCommand
->Next();
1592 if (m_commands
.Number() > 0)
1594 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1595 redoNode
= m_commands
.First();
1601 bool success
= redoCommand
->Do();
1604 m_currentCommand
= redoNode
;
1612 bool wxCommandProcessor::CanUndo(void)
1614 if (m_currentCommand
)
1615 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1619 void wxCommandProcessor::Initialize(void)
1621 m_currentCommand
= m_commands
.Last();
1625 void wxCommandProcessor::SetMenuStrings(void)
1627 if (m_commandEditMenu
)
1630 if (m_currentCommand
)
1632 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1633 wxString
commandName(command
->GetName());
1634 if (commandName
== "") commandName
= _("Unnamed command");
1635 bool canUndo
= command
->CanUndo();
1637 buf
= wxString(_("&Undo ")) + commandName
;
1639 buf
= wxString(_("Can't &Undo ")) + commandName
;
1641 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1642 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1644 // We can redo, if we're not at the end of the history.
1645 if (m_currentCommand
->Next())
1647 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1648 wxString
redoCommandName(redoCommand
->GetName());
1649 if (redoCommandName
== "") redoCommandName
= _("Unnamed command");
1650 buf
= wxString(_("&Redo ")) + redoCommandName
;
1651 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1652 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1656 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1657 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1662 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
1663 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1665 if (m_commands
.Number() == 0)
1667 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1668 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1672 // currentCommand is NULL but there are commands: this means that
1673 // we've undone to the start of the list, but can redo the first.
1674 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1675 wxString
redoCommandName(redoCommand
->GetName());
1676 if (!redoCommandName
) redoCommandName
= _("Unnamed command");
1677 buf
= wxString(_("&Redo ")) + redoCommandName
;
1678 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1679 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1685 void wxCommandProcessor::ClearCommands(void)
1687 wxNode
*node
= m_commands
.First();
1690 wxCommand
*command
= (wxCommand
*)node
->Data();
1693 node
= m_commands
.First();
1695 m_currentCommand
= NULL
;
1700 * File history processor
1703 wxFileHistory::wxFileHistory(int maxFiles
)
1705 m_fileMaxFiles
= maxFiles
;
1708 m_fileHistory
= new char *[m_fileMaxFiles
];
1711 wxFileHistory::~wxFileHistory(void)
1714 for (i
= 0; i
< m_fileHistoryN
; i
++)
1715 delete[] m_fileHistory
[i
];
1716 delete[] m_fileHistory
;
1719 // File history management
1720 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1727 // Check we don't already have this file
1728 for (i
= 0; i
< m_fileHistoryN
; i
++)
1730 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1734 // Add to the project file history:
1735 // Move existing files (if any) down so we can insert file at beginning.
1737 // First delete filename that has popped off the end of the array (if any)
1738 if (m_fileHistoryN
== m_fileMaxFiles
)
1740 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1741 m_fileHistory
[m_fileMaxFiles
-1] = NULL
;
1743 if (m_fileHistoryN
< m_fileMaxFiles
)
1745 if (m_fileHistoryN
== 0)
1746 m_fileMenu
->AppendSeparator();
1747 m_fileMenu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1750 // Shuffle filenames down
1751 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1753 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1755 m_fileHistory
[0] = copystring(file
);
1757 for (i
= 0; i
< m_fileHistoryN
; i
++)
1758 if (m_fileHistory
[i
])
1761 sprintf(buf
, "&%d %s", i
+1, m_fileHistory
[i
]);
1762 m_fileMenu
->SetLabel(wxID_FILE1
+i
, buf
);
1766 wxString
wxFileHistory::GetHistoryFile(int i
) const
1768 if (i
< m_fileHistoryN
)
1769 return wxString(m_fileHistory
[i
]);
1771 return wxString("");
1774 void wxFileHistory::FileHistoryUseMenu(wxMenu
*menu
)
1779 void wxFileHistory::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1784 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1785 char *historyFile
= NULL
;
1786 while ((m_fileHistoryN
<= m_fileMaxFiles
) && wxGetResource(section
, buf
, &historyFile
, resourceFile
) && historyFile
)
1788 // wxGetResource allocates memory so this is o.k.
1789 m_fileHistory
[m_fileHistoryN
] = historyFile
;
1791 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1797 void wxFileHistory::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1802 for (i
= 0; i
< m_fileHistoryN
; i
++)
1804 sprintf(buf
, "file%d", i
+1);
1805 wxWriteResource(section
, buf
, m_fileHistory
[i
], resourceFile
);
1815 wxPrintInfo::wxPrintInfo(void)
1820 wxPrintInfo::~wxPrintInfo(void)
1826 * Permits compatibility with existing file formats and functions
1827 * that manipulate files directly
1830 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1835 if ((fd1
= fopen (WXSTRINGCAST filename
, "rb")) == NULL
)
1838 while ((ch
= getc (fd1
)) != EOF
)
1839 stream
<< (unsigned char)ch
;
1845 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1850 if ((fd1
= fopen (WXSTRINGCAST filename
, "wb")) == NULL
)
1855 while (!stream
.eof())
1866 // End USE_DOC_VIEW_ARCHITECTURE